home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d156 / flex.lha / Flex / Flex2 / gnu.lib.src / getopt.c < prev    next >
C/C++ Source or Header  |  1988-10-02  |  2KB  |  83 lines

  1. /*
  2.  *      $Header:$
  3.  */
  4. /********************************************************
  5.  *                            *
  6.  *                            *
  7.  *            getopt.c            *
  8.  *                            *
  9.  *  Added to Gnu-awk for the Amiga             *
  10.  *                            *
  11.  ********************************************************/
  12. #include <stdio.h>
  13. /*
  14.  *      External variables.
  15.  */
  16. extern char *index();
  17. /*
  18.  *      Accumulation variables.
  19.  */
  20. char *optarg;           /* Global argument pointer. */
  21. int optind = 0;         /* Global argv index. */
  22. /*
  23.  *      Getopt:
  24.  *
  25.  *        Parses the command line sequentially. Arguments starting with
  26.  *      a '-' are defined to be switches. The following character is the
  27.  *      switch value. If it is present in the option string, its value
  28.  *      is returned. If the option character is followed by the ':'
  29.  *      character, the most meaningful argument string is returned in
  30.  *      the argument 'optarg' and its index is returned as 'optind'. By
  31.  *      the way, 'optind' is the working index and can be carefully
  32.  *      fiddled with. A '\0' value is returned for unswitched arguments
  33.  *      and 'optarg'/'optind' refer to the argument. A '?' is returned
  34.  *      for switches that are unspecified. An EOF is returned if the
  35.  *      command line is empty.
  36.  */
  37. int getopt(argc,argv,optstring)
  38. register int argc;
  39. register char *argv[];
  40. register char *optstring;
  41. {
  42.   /*
  43.    *    Locals.
  44.    */
  45.   register int i;
  46.   register char *swc;
  47.   /*
  48.    *    Set up the global variables. Restrict the range of 'optind'
  49.    *    and terminate if it is upside out of bounds.
  50.    */
  51.   optarg = NULL;
  52.   if (++optind < 1)
  53.     optind  = 1;
  54.   else if (optind >= argc)
  55.     return (EOF);
  56.   /*
  57.    *    See a switch is present. If not, then return a switchless
  58.    *    argument.
  59.    */
  60.   if (*argv[optind] != '-'){
  61.     optarg = argv[optind];
  62.     return ('\0');
  63.   }
  64.   /*
  65.    *    See if the switch is in the option string.
  66.    */
  67.   if (!(swc = index(optstring,*(argv[optind] + 1))))
  68.     return ('?');
  69.   /*
  70.    *    See if an argument needs to be extracted.
  71.    */
  72.   if (swc[1] != ':')
  73.     return (swc[0]);
  74.   /*
  75.    *    Extract the argument.
  76.    */
  77.   if (*(argv[optind] + 2))
  78.     optarg = argv[optind] + 2;
  79.   else if (++optind < argc)
  80.     optarg = argv[optind];
  81.   return (swc[0]);
  82. }
  83.