Exemple #1
0
// Function the reads command line arguments and sets up appropriate
// flags.
void set_options (int argc, char **argv) {
   static struct option long_options[] =
   {
      {"list", no_argument, NULL, 'l'},
      {"recurse", no_argument, NULL, 'R'},
      {"debug", required_argument, NULL, '@'},
      {NULL, 0, NULL, 0}
   };
   opterr = false;
   for (;;) {
      int option = getopt_long (argc, argv, "lR@:", long_options, NULL);
      if (option == EOF) break;
      switch (option) {
         char optopt_str[16]; // used for printing error
         case 'l': flags.long_listing = true;
                   break;
         case 'R': flags.recursive = true;
                   break;
         case '@': set_debugflags (optarg);
                   break;
         default : sprintf (optopt_str, "-%c", optopt); 
                   print_error (optopt_str, "invalid option");
                   exit (exit_status);
      }
   }
   // collect any operands in argv
   if (optind < argc) {
      flags.opct = argc - optind;    
      flags.pathname = calloc (flags.opct, sizeof (char*));
      assert (flags.pathname != NULL);
      for (int itor = optind; itor < argc; ++itor) {
         flags.pathname[itor - optind] = argv[itor];
      }
   }
}
int main (int argc, char **argv) {
   Exec_Name = basename (argv[0]);
   char *default_dictionary = DEFAULT_DICTNAME;
   char *user_dictionary = NULL;
   hashset_ref hashset = new_hashset ();
   yy_flex_debug = false;

   // Scan the arguments and set flags.
   opterr = false;
   for (;;) {
      int option = getopt (argc, argv, "nxyd:@:");
      if (option == EOF) break;
      switch (option) {
         char optopt_string[16]; // used in default:
         case 'd': user_dictionary = optarg;
                   break;
         case 'n': default_dictionary = NULL;
                   break;
         case 'x': option_x(hashset);
                   break;
         case 'y': yy_flex_debug = true;
                   break;
         case '@': set_debugflags (optarg);
                   if (strpbrk (optarg, "@y")) yy_flex_debug = true;
                   break;
         default : sprintf (optopt_string, "-%c", optopt);
                   print_error (optopt_string, "invalid option");
                   break;
      }
   }

   // Load the dictionaries into the hash table.
   load_dictionary (default_dictionary, hashset);
   load_dictionary (user_dictionary, hashset);

   // Read and do spell checking on each of the files.
   if (optind >= argc) {
      yyin = stdin;
      spellcheck (STDIN_NAME, hashset);
   }else {
      int fileix = optind;
      for (; fileix < argc; ++fileix) {
         DEBUGF ('m', "argv[%d] = \"%s\"\n", fileix, argv[fileix]);
         char *filename = argv[fileix];
         if (strcmp (filename, STDIN_NAME) == 0) {
            yyin = stdin;
            spellcheck (STDIN_NAME, hashset);
         }else {
            yyin = open_infile (filename);
            if (yyin == NULL) continue;
            spellcheck (filename, hashset);
            fclose (yyin);
         }
      }
   }
   
   yycleanup ();
   return Exit_Status;
}