home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / MM1 / SOUNDUTILS / mm1_tracker.lzh / TRACKER4.6 / getopt.c < prev    next >
Text File  |  1994-11-24  |  2KB  |  102 lines

  1. /* getopt.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: getopt.c,v 4.3 1994/11/15 16:11:01 espie Exp espie $
  6.  * $Log: getopt.c,v $
  7.  * Revision 4.3  1994/11/15  16:11:01  espie
  8.  * *** empty log message ***
  9.  *
  10.  *
  11.  * Revision 1.5  1993/12/04  16:12:50  espie
  12.  * New getopt semantics.
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <string.h>
  18.  
  19. #include "defs.h"
  20. #include "getopt.h"
  21.  
  22. ID("$Id: getopt.c,v 4.3 1994/11/15 16:11:01 espie Exp espie $")
  23. int optind = 1;
  24. char *optarg = 0;
  25. LOCAL not_an_option = 0;
  26.  
  27. LOCAL int parse_option(argv, option)
  28. char *argv[];
  29. struct long_option *option;
  30.     {
  31.     optind++;
  32.     if (option->argn)
  33.         optarg = argv[optind++];
  34.     return option->code;
  35.     }
  36.  
  37. int getlongopt(argc, argv, options)
  38. int argc;
  39. char *argv[];
  40. struct long_option *options;
  41.     {
  42.     if (not_an_option == optind)
  43.         return -1;
  44.     if (optind >= argc)
  45.         return -1;
  46.     if (argv[optind][0] == '-')
  47.         {
  48.         char *match = argv[optind]+1;
  49.         if (strlen(match) == 1)
  50.             {
  51.             if (match[0] == '-')
  52.                 {
  53.                 not_an_option = ++optind;
  54.                 return -1;
  55.                 }
  56.             while(options->fulltext)
  57.                 {
  58.                 if (options->abbrev == match[0])
  59.                     return parse_option(argv, options);
  60.                 else
  61.                     options++;
  62.                 }
  63.             return -1;
  64.             }
  65.         else
  66.             {
  67.             int max_match = 0;
  68.             struct long_option *best = 0;
  69.  
  70.             while (options->fulltext)
  71.                 {
  72.                 int i;
  73.                 for (i = 0; ; i++)
  74.                     {
  75.                     if (options->fulltext[i] == 0 && match[i] == 0)
  76.                         return parse_option(argv, options);
  77.                     if (match[i] == 0)
  78.                         {
  79.                         if (i > max_match)
  80.                             {
  81.                             max_match = i;
  82.                             best = options;
  83.                             }
  84.                         break;
  85.                         }
  86.                     if (tolower(options->fulltext[i]) != tolower(match[i]))
  87.                         break;
  88.                     }
  89.                 options++;
  90.                 }
  91.             if (max_match < 3)
  92.                 {
  93.                 fprintf(stderr, "Unrecognized option: %s\n", match);
  94.                 return -1;
  95.                 }
  96.             return parse_option(argv, best);
  97.             }
  98.         }
  99.     else
  100.         return -1;
  101.     }
  102.