home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / tc20 / getopt.c < prev    next >
Text File  |  1988-08-28  |  4KB  |  136 lines

  1. /*
  2.     getopt.c -- Turbo C
  3.  
  4.     Copyright (c) 1986,87,88 by Borland International Inc.
  5.     All Rights Reserved.
  6. */
  7.  
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <dos.h>
  11. #include <stdio.h>
  12.  
  13. int    optind    = 1;    /* index of which argument is next    */
  14. char   *optarg;        /* pointer to argument of current option */
  15. int    opterr    = 1;    /* allow error message    */
  16.  
  17. static    char   *letP = NULL;    /* remember next option char's location */
  18. static    char    SW = 0;        /* DOS switch character, either '-' or '/' */
  19.  
  20. /*
  21.   Parse the command line options, System V style.
  22.  
  23.   Standard option syntax is:
  24.  
  25.     option ::= SW [optLetter]* [argLetter space* argument]
  26.  
  27.   where
  28.     - SW is either '/' or '-', according to the current setting
  29.       of the MSDOS switchar (int 21h function 37h).
  30.     - there is no space before any optLetter or argLetter.
  31.     - opt/arg letters are alphabetic, not punctuation characters.
  32.     - optLetters, if present, must be matched in optionS.
  33.     - argLetters, if present, are found in optionS followed by ':'.
  34.     - argument is any white-space delimited string.  Note that it
  35.       can include the SW character.
  36.     - upper and lower case letters are distinct.
  37.  
  38.   There may be multiple option clusters on a command line, each
  39.   beginning with a SW, but all must appear before any non-option
  40.   arguments (arguments not introduced by SW).  Opt/arg letters may
  41.   be repeated: it is up to the caller to decide if that is an error.
  42.  
  43.   The character SW appearing alone as the last argument is an error.
  44.   The lead-in sequence SWSW ("--" or "//") causes itself and all the
  45.   rest of the line to be ignored (allowing non-options which begin
  46.   with the switch char).
  47.  
  48.   The string *optionS allows valid opt/arg letters to be recognized.
  49.   argLetters are followed with ':'.  Getopt () returns the value of
  50.   the option character found, or EOF if no more options are in the
  51.   command line.     If option is an argLetter then the global optarg is
  52.   set to point to the argument string (having skipped any white-space).
  53.  
  54.   The global optind is initially 1 and is always left as the index
  55.   of the next argument of argv[] which getopt has not taken.  Note
  56.   that if "--" or "//" are used then optind is stepped to the next
  57.   argument before getopt() returns EOF.
  58.  
  59.   If an error occurs, that is an SW char precedes an unknown letter,
  60.   then getopt() will return a '?' character and normally prints an
  61.   error message via perror().  If the global variable opterr is set
  62.   to false (zero) before calling getopt() then the error message is
  63.   not printed.
  64.  
  65.   For example, if the MSDOS switch char is '/' (the MSDOS norm) and
  66.  
  67.     *optionS == "A:F:PuU:wXZ:"
  68.  
  69.   then 'P', 'u', 'w', and 'X' are option letters and 'F', 'U', 'Z'
  70.   are followed by arguments.  A valid command line may be:
  71.  
  72.     aCommand  /uPFPi /X /A L someFile
  73.  
  74.   where:
  75.     - 'u' and 'P' will be returned as isolated option letters.
  76.     - 'F' will return with "Pi" as its argument string.
  77.     - 'X' is an isolated option.
  78.     - 'A' will return with "L" as its argument.
  79.     - "someFile" is not an option, and terminates getOpt.  The
  80.       caller may collect remaining arguments using argv pointers.
  81. */
  82.  
  83. int    getopt(int argc, char *argv[], char *optionS)
  84. {
  85.     unsigned char ch;
  86.     char *optP;
  87.  
  88.     if (SW == 0) {
  89.         /* get SW using dos call 0x37 */
  90.         _AX = 0x3700;
  91.         geninterrupt(0x21);
  92.         SW = _DL;
  93.     }
  94.  
  95.     if (argc > optind) {
  96.         if (letP == NULL) {
  97.             if ((letP = argv[optind]) == NULL || 
  98.                 *(letP++) != SW)  goto gopEOF;
  99.             if (*letP == SW) {
  100.                 optind++;  goto gopEOF;
  101.             }
  102.         }
  103.         if (0 == (ch = *(letP++))) {
  104.             optind++;  goto gopEOF;
  105.         }
  106.         if (':' == ch  ||  (optP = strchr(optionS, ch)) == NULL)  
  107.             goto gopError;
  108.         if (':' == *(++optP)) {
  109.             optind++;
  110.             if (0 == *letP) {
  111.                 if (argc <= optind)  goto  gopError;
  112.                 letP = argv[optind++];
  113.             }
  114.             optarg = letP;
  115.             letP = NULL;
  116.         } else {
  117.             if (0 == *letP) {
  118.                 optind++;
  119.                 letP = NULL;
  120.             }
  121.             optarg = NULL;
  122.         }
  123.         return ch;
  124.     }
  125. gopEOF:
  126.     optarg = letP = NULL;  
  127.     return EOF;
  128.  
  129. gopError:
  130.     optarg = NULL;
  131.     errno  = EINVAL;
  132.     if (opterr)
  133.         perror ("get command line option");
  134.     return ('?');
  135. }
  136.