home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / SH162_2S.ZIP / GETOPT.C < prev    next >
C/C++ Source or Header  |  1990-02-17  |  4KB  |  164 lines

  1. /*
  2.  *  MODULE NAME:   getopt.c                Revision 1.0
  3.  *
  4.  *  AUTHOR:        I. Stewartson
  5.  *                 Data Logic Ltd.,
  6.  *                 Queens House,
  7.  *                 Greenhill Way,
  8.  *                 Harrow,
  9.  *                 Middlesex HA1 1YR.
  10.  *                 Telephone: London (01) 863 0383
  11.  *
  12. #include <logo.h>
  13.  *  MODULE DESCRIPTION: This function is based on the UNIX library function.
  14.  *            getopt return the next option letter in argv that
  15.  *            matches a letter in opstring.  optstring is a string
  16.  *            of recognised option letters; if a letter is followed
  17.  *            by a colon, the option is expected to have an argument
  18.  *            that may or may not be separated from it by white
  19.  *            space.  optarg is set to point to the start of the
  20.  *            option argument on return from getopt.
  21.  *
  22.  *            getopt places in optind the argv index of the next
  23.  *            argument to be processed.  Because optind is external,
  24.  *            it is normally initialised to zero automatically before
  25.  *            the first call to getopt.
  26.  *
  27.  *            When all options have been processed (i.e. up to the
  28.  *            first non-option argument), getopt returns EOF.  The
  29.  *            special option -- may be used to delimit the end of
  30.  *            the options; EOF will be returned, and -- will be
  31.  *            skipped.
  32.  *
  33.  *            getopt prints an error message on stderr and returns a
  34.  *            question mark (?) when it encounters an option letter
  35.  *            not included in optstring.  This error message may be
  36.  *            disabled by setting opterr to a non-zero value.
  37.  *
  38.  *  CALLING SEQUENCE:    The following calling sequences are used:
  39.  *
  40.  *            int    getopt(argc, argv, optstring)
  41.  *            int    argc;
  42.  *            char    **argv;
  43.  *            char    *optstring;
  44.  *
  45.  *  ERROR MESSAGES:
  46.  *            %s: illegal option -- %c
  47.  *            %s: option requires an argument -- %c
  48.  *
  49.  *  INCLUDE FILES:
  50.  */
  51.  
  52. #include <stdio.h>            /* Standard Input/Output    */
  53. #include <string.h>            /* String function declarations    */
  54. #include <stdlib.h>            /* Standard library declarations*/
  55.  
  56. /*
  57.  *  DATA DECLARATIONS:
  58.  */
  59.  
  60. int        opterr = 0;
  61. int        optind = 1;
  62. int        optopt;
  63. int        optvar = 0;
  64. char        *optarg;
  65.  
  66. static char    *errmes1 = "%s: illegal option -- %c\n";
  67. static char    *errmes2 = "%s: option requires an argument -- %c\n";
  68.  
  69. /*
  70.  *  MODULE ABSTRACT:
  71.  *
  72.  *  EXECUTABLE CODE:
  73.  */
  74.  
  75. int    getopt(argc, argv, optstring)
  76. int    argc;                /* Argument count        */
  77. char    **argv;                /* Argument string vector    */
  78. char    *optstring;            /* Valid options        */
  79. {
  80.     static int    string_off = 1;        /* Current position        */
  81.     int        cur_option;        /* Current option        */
  82.     char    *cp;            /* Character pointer        */
  83.  
  84.     if (string_off == 1)
  85.     {
  86.     if ((optind >= argc) || (argv[optind][0] != '-') || (!argv[optind][1]))
  87.         return (EOF);
  88.  
  89.     else if (!strcmp(argv[optind], "--"))
  90.     {
  91.         optind++;
  92.         return (EOF);
  93.     }
  94.     }
  95.  
  96. /* Get the current character from the current argument vector */
  97.  
  98.     optopt = cur_option = argv[optind][string_off];
  99.  
  100. /* Validate it */
  101.  
  102.     if ((cur_option == ':') || ((cur_option == '*') && optvar) ||
  103.     ((cp = strchr(optstring, cur_option)) == (char *)NULL))
  104.     {
  105.     if (opterr)
  106.         fprintf(stderr, errmes1, cur_option, argv[0]);
  107.  
  108.     if (!argv[optind][++string_off])
  109.     {
  110.         optind++;
  111.         string_off = 1;
  112.     }
  113.  
  114.     return ('?');
  115.     }
  116.  
  117. /* Parameters following ? */
  118.  
  119.     if (*(++cp) == ':')
  120.     {
  121.     if (argv[optind][string_off + 1])
  122.         optarg = &argv[optind++][string_off + 1];
  123.  
  124.     else if (++optind >= argc)
  125.     {
  126.         if (opterr)
  127.         fprintf(stderr, errmes2, cur_option, argv[0]);
  128.  
  129.         string_off = 1;
  130.         return ('?');
  131.     }
  132.  
  133.     else
  134.         optarg = argv[optind++];
  135.  
  136.     string_off = 1;
  137.     }
  138.  
  139.     else if ((*cp == '*') && optvar)
  140.     {
  141.     if (argv[optind][string_off + 1] != 0)
  142.         optarg = &argv[optind++][string_off + 1];
  143.     else
  144.     {
  145.         optarg = "";
  146.         optind++;
  147.         string_off = 1;
  148.     }
  149.     }
  150.  
  151.     else
  152.     {
  153.     if (!argv[optind][++string_off])
  154.     {
  155.         string_off = 1;
  156.         optind++;
  157.     }
  158.  
  159.     optarg = (char *)NULL;
  160.     }
  161.  
  162.     return (cur_option);
  163. }
  164.