/*
*
* @(#)71        1.5  src/examples/common/os2/getopt.c, examples.src, os2dce21.dss, 960602a.1  1/12/96  14:52:01
*
* COMPONENT_NAME:  examples.src
*
* FUNCTIONS:
*
* ORIGINS: 27
*
* OBJECT CODE ONLY SOURCE MATERIALS
*
* (C) COPYRIGHT International Business Machines Corp. 1992, 1994
*  All Rights Reserved
*  Licensed Materials - Property of IBM
*
*  US Government Users Restricted Rights - Use, duplication or
*  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*/
 /********************************************************************/
 /*                                                                  */
 /* PROGRAM NAME: getopts.c                                          */
 /*                                                                  */
 /* DESCRIPTIVE NAME: Standard command  line parsing routine         */
 /*                                                                  */
 /* FUNCTION: Parses switches on the command line                    */
 /*                                                                  */
 /********************************************************************/
/* -------------------------------------------------------------------------- */
/* getopt()                                                                   */
/*                                                                            */
/* The getopt() function is a command line parser.  It returns the next       */
/* option character in argv that matches an option character in opstring.     */
/*                                                                            */
/* The argv argument points to an array of argc+1 elements containing argc    */
/* pointers to character strings followed by a null pointer.                  */
/*                                                                            */
/* The opstring argument points to a string of option characters; if an       */
/* option character is followed by a colon, the option is expected to have    */
/* an argument that may or may not be seperated from it by white space.  The  */
/* external variable optarg is set to point to the start of the option        */
/* argument on return from getopt().                                          */
/*                                                                            */
/* The getopt() function places in optind the argv index of the next argument */
/* to be processed.  The system initializes the external variable optind to   */
/* 1 before the first call to getopt().                                       */
/*                                                                            */
/* When all options have been processed (that is, up to the first nonoption   */
/* argument), getopt() returns EOF.  The special option "--" may be used to   */
/* delimit the end of the options; EOF will be returned, and "--" will be     */
/* skipped.                                                                   */
/*                                                                            */
/* The getopt() function returns a question mark (?) when it encounters an    */
/* option character not included in opstring.  This error message can be      */
/* disabled by setting opterr to zero.  Otherwise, it returns the option      */
/* character that was detected.                                               */
/*                                                                            */
/* If the special option "--" is detected, or all options have been           */
/* processed, EOF is returned.                                                */
/*                                                                            */
/* No errors are defined.                                                     */
/* -------------------------------------------------------------------------- */

#include <stdio.h>                   /* for EOF */
#include <string.h>                  /* for strchr() */


/* static (global) variables that are specified as exported by getopt() */
char *optarg = NULL;    /* pointer to the start of the option argument  */
int   optind = 1;       /* number of the next argv[] to be evaluated    */
int   opterr = 1;       /* non-zero if a question mark should be returned
                           when a non-valid option character is detected */

#define COLON        ':'
#define QUESTIONMARK '?'
#define SLASH        '/'
#define MINUS        '-'
#define ISOPT(p) (*p == MINUS)    /*$A4*/
#define BADOPTERROR(c)  optind++, return( opterr ? QUESTIONMARK : c )

int getopt(int argc, char *argv[], char *opstring)
{
  static char *indpos = NULL;
  static int   indind;            /*@A2A*/
  char c, *p, *q;

  p = NULL;

  if (optind == 1)                /*@A2A*/
  {                               /*@A2A*/
    indind = argc;                /*@A2A*/
  }                               /*@A2A*/

  if (indpos)
    if (*(++indpos))
      p = indpos;
  if (p == NULL)
    {
      if (optind >= indind)       /*@A2A*/
        {
          indpos = NULL;
          return(EOF);
        }
      p = argv[optind++];
      if (!ISOPT(p))         /* If the next argv[] is not an option */
        {                    /*   there can be no more options      */
          --optind;            /* we point to the current arg once we're done*/
          optarg = NULL;
          indpos = NULL;
          return(EOF);
        }
      p++;
    }
  c = *p;
  if (*p == COLON)
     return(opterr ? QUESTIONMARK : c);
  else
     if ((q = strchr(opstring, *p)) == 0)
        {
           optarg = NULL;
           indpos = NULL;
           return(opterr ? QUESTIONMARK : c);
        }
     else
        {
          if (*(q + 1) != COLON)
             {
                optarg =  NULL;        /* no argument follows the option */
                indpos = p;
             }
          else
             {
                if (*(p + 1) != '\0')  /* Arg follows.  Is it in this argv? */
                   optarg = ++p;            /* Yes, it is */
                else
                   optarg = argv[optind++]; /*No, it isn't--it is in the next*/
                indpos = NULL;
             }
          return(*q);
        }
}
