/* Copyright 1985 The Balkan Group
 * Author:  Eric Balkan             Date written:  4/7/85
 * 
 * mfind: locates argument strings within a movie record
 *        movie files must consist of two letter filenames within the
 *          current directory on the currently logged-in drive
 *
 * Requires DeSmet C compiler and Greenleaf general function library
 *        or equivalent
 */

#include "stdio.h"

char *malloc();
char *gets();

char rec[23 * 99 + 1];
char *recs_found[25][200];   /* array of pointers to found records */
char *wd[25][4];             /* array of pointers to search arguments */
char *arg[25];
int cur_arg;
int match[25];
int om = 0;


main(argc, argv)
   int argc;
   char *argv[];
   {
   int  i, fp, end;
   char buf[99 + 1];
   char filename[15];
   char operator[51];
   char op[25];
   int num_args;
   char str[101];
   char wd1[51], wd3[51];
   int ctr = 0;
   for (i = 0; i < 25; ++i)
      match[i] = 0;
   for (cur_arg = 0; ; ++cur_arg)
      {
      printf(
       " Search criteria set #%d (e.g. Winger and Nolte) (Null line to end):\n",
                   cur_arg);
      gets(str);
      if (str[0] == '\0')
         break;
      if ((arg[cur_arg] = malloc(strlen(str) + 1)) == 0)
         {
         printf("Out of memory\n");
         return(ERR);
         }
      strcpy(arg[cur_arg], str);
      sscanf(str, "%s%s%s", wd1, operator, wd3);
      if ((wd[cur_arg][1] = malloc(strlen(wd1) + 1)) == 0 ||
           (wd[cur_arg][3] = malloc(strlen(wd3) + 1)) == 0)
         {
         printf("\nOut of memory\n");
         return(ERR);
         }
      strcpy(wd[cur_arg][1], wd1);
      strcpy(wd[cur_arg][3], wd3);
      if (strcmp(operator, "OR") == 0 || strcmp(operator, "or") == 0)
         op[cur_arg] = 'O';
      else if (strcmp(operator, "AND") == 0 || strcmp(operator, "and") == 0)
         op[cur_arg] = 'A';
      else
         op[cur_arg] = 0;
      }
   num_args = cur_arg - 1;
   dosfirst("??", 0, filename);
   printf("**Checking file:");
   for (; om == 0;)
      {
      fclose(fp);
      if ((fp = fopen(filename, "r")) != NULL)  /* opened OK */
         {
         end = 0;
         printf(" *%s* ", filename); 
         for (; om == 0;)
            {
            rec[0] = '\0';
            for (i = 0; (end = fgets(buf, 100, fp)) != NULL && om == 0; ++i)
               {
               if (buf[0] == '.' && buf[1] == '.')
                  {
                  ctr++;
                  break;
                  }
               strcat(rec, buf);
               }
            if (end == NULL)
               break;
            for (cur_arg = 0; cur_arg <= num_args; ++cur_arg)
               { 
               if (op[cur_arg] == 'O')
                  {
                  if (strfind(rec, wd[cur_arg][1]) > 0
                         || strfind(rec, wd[cur_arg][3]) > 0)
                     hit();
                  }
               else if (op[cur_arg] == 'A')
                  {
                  if (strfind(rec, wd[cur_arg][1]) > 0
                        && strfind(rec, wd[cur_arg][3]) > 0)
                     hit();
                  }
               else
                  {
                  if (strfind(rec, arg[cur_arg]) > 0)
                     hit();
                  }
               }
            }
         }
      else printf("Couldn't open file: %s\n", filename);
      if (dosnext("*", 0, filename) != 0)
         break;
      }
   for (cur_arg = 0; cur_arg <= num_args; ++cur_arg)
      {
      printf("\n\nSearched %d movies for:  %s ", ctr, arg[cur_arg]);
      printf("\nFound: %d\n", match[cur_arg]);
      printf("Review these again (Y/N)? ");
      if (getyn())
         {
         for (i = 0; i < match[cur_arg]; ++i)
            {
            puts("\n----------------------------------------\n\n");
            puts(recs_found[cur_arg][i]);
            puts("----------------------------------------\n");
            }
         }
      }
   return(0);
   
   }

hit()
   {
   puts("\n----------------------------------------\n\n");
   puts(rec);
   puts("----------------------------------------\n");
   if ((recs_found[cur_arg][match[cur_arg]] = malloc(strlen(rec) + 1)) == 0)
      {
      printf("\nOut of memory\n");
      om = 1;
      return(ERR);
      }
   strcpy(recs_found[cur_arg][match[cur_arg]++], rec);
   }

