home *** CD-ROM | disk | FTP | other *** search
/ Zodiac Super OZ / MEDIADEPOT.ISO / FILES / 16 / FREEDOS.ZIP / FD_A4PRE.ZIP / SOURCE / MICROC.ZIP / GETOPT.C < prev    next >
C/C++ Source or Header  |  1995-05-21  |  5KB  |  169 lines

  1. /*
  2.    Free-DOS Function Library GETOPT.C - Returns command line options
  3.    Written by: Morgan "Hannibal" Toal, based upon original source by James Hall
  4.    Compiler: MICRO-C 3.13/ARROWASM/VALLINK
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. int optind = 1, optchar = 0;
  22. char *optarg;
  23.  
  24. void usage (void);
  25.  
  26. int getopt (int argc, char *argv[], char *options, char *argoptions)
  27.  
  28. {
  29.  
  30.     int c;
  31.     while (optind < argc)
  32.  
  33.     {
  34.         optarg = NULL;
  35.  
  36.         /* If the current argv is a switch (i.e. starts with SWCHAR)... */
  37.  
  38.         if (argv[optind][0] == SWCHAR)
  39.             switch (c = toupper(argv[optind][optchar]))
  40.  
  41.             {
  42.  
  43.             /* ...advance to next argument if optchar is NULL or ARGCHAR */
  44.  
  45.             case NULL:
  46.                 optchar = 0;
  47.                 optind++; 
  48.                 break;
  49.  
  50.             /* ...skip SWCHAR, but return ? if next optchar is SWCHAR or NULL */
  51.  
  52.             case SWCHAR:
  53.                 c = toupper(argv[optind][++optchar]);
  54.                 if ((c == SWCHAR) || (c == NULL))
  55.                     return ('?');
  56.                 break;
  57.  
  58.             /* ...return the optchar if in options or argoptions, or return ? */
  59.  
  60.             default:
  61.  
  62.                 optchar++;
  63.  
  64.             /* ...call usage if we have a "?" */
  65.  
  66.                 if (c == '?')
  67.                     usage();
  68.  
  69.             /* ...check to see if it is an argument-required option */
  70.  
  71.                 if (strchr(argoptions, c) != NULL)
  72.                     if ((argv[optind][optchar] == ARGCHAR)
  73.                         && (argv[optind][optchar+1] != NULL))
  74.                     {
  75.                         optarg = &argv[optind++][optchar+1];
  76.                         optchar = 0;
  77.                         return (c);
  78.                     }
  79.                 else
  80.                     {
  81.                     fprintf (stderr, "Argument not specified for: %c%c\n", SWCHAR, c);
  82.                     usage ();
  83.                 }
  84.  
  85.            /* ...check to see if it is a non-argument option */
  86.  
  87.                 if (strchr(options, c) != NULL)
  88.                     if (argv[optind][optchar] != ARGCHAR)
  89.                         return (c);
  90.                 else
  91.                     {
  92.                     fprintf (stderr, "Argument not allowed for: %c%c\n", SWCHAR, c);
  93.                     usage ();
  94.                 }
  95.             /* ...what the hell is it? */
  96.  
  97.                 else
  98.                     fprintf (stderr, "Option not recognized: %c%c\n", SWCHAR, c);
  99.                 usage ();
  100.             }
  101.  
  102.         /* return EOF because we have discovered a non-option argument */
  103.  
  104.         else 
  105.  
  106.             return (EOF);
  107.  
  108.     }
  109.  
  110.     /* return EOF because we have run out of arguments */
  111.  
  112.     return (EOF);
  113.  
  114. }
  115.  
  116. #ifdef TEST
  117.  
  118. main (int argc, char **argv)
  119.  
  120. {
  121.     int i;
  122.     while ((i = getopt (argc, argv, "ABC", "XYZ")) != EOF)
  123.     { 
  124.         switch (i)
  125.         {
  126.         case 'A':
  127.         case 'B':
  128.         case 'C':
  129.             printf ("Option %c selected.\n", i);
  130.             break;
  131.  
  132.         case 'X':
  133.         case 'Y':
  134.         case 'Z':
  135.             printf ("Option %c selected, with argument %s.\n", i, optarg);
  136.             break;
  137.  
  138.         default:
  139.             printf ("Unknown Error.\n");
  140.  
  141.         }
  142.     }
  143.     if (optind < argc)
  144.     {
  145.         printf ("The rest of the arguments are...\n");
  146.         for (i = optind ; i < argc; i++)
  147.             printf ("%d. %s\n", 1+i-optind, argv[i]);
  148.  
  149.     }
  150.  
  151.     printf ("This program would now be doing something useful.\n");
  152.  
  153. }
  154.  
  155. void usage(void)
  156. {
  157.     printp ("GETOPT", "Test program for GETOPT.C function");
  158.     printu ("GETOPT", "[\A] [\B] [\C] [\X=argument] [\Y=argument] [\Z=argument] [argument..]");
  159.     printo ("A", "Bogus option");
  160.     printo ("B", "Bogus option");
  161.     printo ("C", "Bogus option");
  162.     printo ("X", "Bogus option that requires an argument");
  163.     printo ("Y", "Bogus option that requires an argument");
  164.     printo ("Z", "Bogus option that requires an argument");
  165.     abort (1);
  166. }
  167.  
  168. #endif
  169.