home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume9 / elm2 / part01 / src / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-03-08  |  2.1 KB  |  104 lines

  1. /**            getopt.c            **/
  2.  
  3. /** starting argument parsing routine... 
  4.  
  5.     (C) Copyright 1986 Dave Taylor
  6. **/
  7.  
  8. #include "defs.h"
  9.  
  10. #ifndef NULL
  11. # define NULL        0
  12. #endif
  13.  
  14. #define DONE        0
  15. #define ERROR        -1
  16.  
  17. char *optional_arg;            /* optional argument as we go */
  18. int   opt_index;            /* argnum + 1 when we leave   */
  19.  
  20. /***********************
  21.  
  22.    Typical usage of this routine is exemplified by;
  23.  
  24.     register int c;
  25.  
  26.     while ((c = get_options(argc, argv, "ad:f:")) > 0) {
  27.        switch (c) {
  28.          case 'a' : arrow_cursor++;        break;
  29.          case 'd' : debug = atoi(optional_arg);    break;
  30.          case 'f' : strcpy(infile, optional_arg); 
  31.                     mbox_specified = 2;  break;
  32.         }
  33.      }
  34.  
  35.      if (c == ERROR) {
  36.        printf("Usage: %s [a] [-d level] [-f file] <names>\n\n", argv[0]);
  37.        exit(1);
  38.     }
  39.  
  40. ***********************/
  41.  
  42. int  _indx = 1, _argnum = 1;
  43.  
  44. int
  45. get_options(argc, argv, options)
  46. int argc;
  47. char *argv[], *options;
  48. {
  49.     /** Returns the character argument next, and optionally instantiates 
  50.         "argument" to the argument associated with the particular option 
  51.     **/
  52.     
  53.     char        *word, *strchr();
  54.  
  55.     if (_argnum >= argc) {    /* quick check first - no arguments! */
  56.       opt_index = argc;
  57.       return(DONE);
  58.     }
  59.  
  60.     if (_indx >= strlen(argv[_argnum]) && _indx > 1) {
  61.       _argnum++;
  62.       _indx = 1;        /* zeroeth char is '-' */
  63.     }
  64.  
  65.     if (_argnum >= argc) {
  66.       opt_index = _argnum; /* no more args */
  67.       return(DONE);
  68.     }
  69.  
  70.     if (argv[_argnum][0] != '-') {
  71.       opt_index = _argnum;
  72.       return(DONE);
  73.     }
  74.  
  75.         word = strchr(options, argv[_argnum][_indx++]);
  76.  
  77.     if (word == NULL)
  78.       return(ERROR);        /* Sun compatibility */
  79.  
  80.     if (word == NULL || strlen(word) == 0) 
  81.       return(ERROR);
  82.     
  83.     if (word[1] == ':') {
  84.  
  85.       /** Two possibilities - either tailing end of this argument or the 
  86.           next argument in the list **/
  87.  
  88.       if (_indx < strlen(argv[_argnum])) { /* first possibility */
  89.         optional_arg = (char *) (argv[_argnum] + _indx);
  90.         _argnum++;
  91.         _indx = 1;
  92.       }
  93.       else {                /* second choice     */
  94.         if (++_argnum >= argc) 
  95.           return(ERROR);            /* no argument!!     */
  96.  
  97.         optional_arg = (char *) argv[_argnum++];
  98.         _indx = 1;
  99.       }
  100.     }
  101.  
  102.     return((int) word[0]);
  103. }
  104.