]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/compat/getopt.c
Restore win32 dir from Branch-5.2 and update it
[bacula/bacula] / bacula / src / win32 / compat / getopt.c
1 /*****************************************************************************
2  *
3  *  MODULE NAME : GETOPT.C
4  *
5  *  COPYRIGHTS:
6  *             This module contains code made available by IBM
7  *             Corporation on an AS IS basis.  Any one receiving the
8  *             module is considered to be licensed under IBM copyrights
9  *             to use the IBM-provided source code in any way he or she
10  *             deems fit, including copying it, compiling it, modifying
11  *             it, and redistributing it, with or without
12  *             modifications.  No license under any IBM patents or
13  *             patent applications is to be implied from this copyright
14  *             license.
15  *
16  *             A user of the module should understand that IBM cannot
17  *             provide technical support for the module and will not be
18  *             responsible for any consequences of use of the program.
19  *
20  *             Any notices, including this one, are not to be removed
21  *             from the module without the prior written consent of
22  *             IBM.
23  *
24  *  AUTHOR:   Original author:
25  *                 G. R. Blair (BOBBLAIR at AUSVM1)
26  *                 Internet: bobblair@bobblair.austin.ibm.com
27  *
28  *            Extensively revised by:
29  *                 John Q. Walker II, Ph.D. (JOHHQ at RALVM6)
30  *                 Internet: johnq@ralvm6.vnet.ibm.com
31  *
32  *            Tweaked by Kern Sibbald for use in Bacula September 2007
33  *
34  *****************************************************************************/
35
36 /******************************************************************************
37  * getopt()
38  *
39  * The getopt() function is a command line parser.  It returns the next
40  * option character in argv that matches an option character in opstring.
41  *
42  * The argv argument points to an array of argc+1 elements containing argc
43  * pointers to character strings followed by a null pointer.
44  *
45  * The opstring argument points to a string of option characters; if an
46  * option character is followed by a colon, the option is expected to have
47  * an argument that may or may not be separated from it by white space.
48  * The external variable optarg is set to point to the start of the option
49  * argument on return from getopt().
50  *
51  * The getopt() function places in optind the argv index of the next argument
52  * to be processed.  The system initializes the external variable optind to
53  * 1 before the first call to getopt().
54  *
55  * When all options have been processed (that is, up to the first nonoption
56  * argument), getopt() returns EOF.  The special option "--" may be used to
57  * delimit the end of the options; EOF will be returned, and "--" will be
58  * skipped.
59  *
60  * The getopt() function returns a question mark (?) when it encounters an
61  * option character not included in opstring.  This error message can be
62  * disabled by setting opterr to zero.  Otherwise, it returns the option
63  * character that was detected.
64  *
65  * If the special option "--" is detected, or all options have been
66  * processed, EOF is returned.
67  *
68  * Options are marked by either a minus sign (-) or a slash (/) if
69  * GETOPT_USE_SLASH is defined.
70  *
71  * No errors are defined.
72  *****************************************************************************/
73
74 #include <stdio.h>              /* for EOF */
75 #include <string.h>             /* for strchr() */
76 #include "getopt.h"
77
78
79 /* static (global) variables that are specified as exported by getopt() */
80 char *optarg = NULL;            /* pointer to the start of the option argument  */
81 int optind = 1;                 /* number of the next argv[] to be evaluated    */
82 int opterr = 1;                 /* non-zero if a question mark should be returned
83                                    when a non-valid option character is detected */
84 int optopt = '?';               /* Not used */
85
86 /* handle possible future character set concerns by putting this in a macro */
87 #define _next_char(string)  (char)(*(string+1))
88
89 int getopt(int argc, char *const argv[], const char *opstring)
90 {
91    static char *pIndexPosition = NULL;  /* place inside current argv string */
92    char *pArgString = NULL;     /* where to start from next */
93    char *pOptString;            /* the string in our program */
94
95
96    if (pIndexPosition != NULL) {
97       /* we last left off inside an argv string */
98       if (*(++pIndexPosition)) {
99          /* there is more to come in the most recent argv */
100          pArgString = pIndexPosition;
101       }
102    }
103
104    if (pArgString == NULL) {
105       /* we didn't leave off in the middle of an argv string */
106       if (optind >= argc) {
107          /* more command-line arguments than the argument count */
108          pIndexPosition = NULL; /* not in the middle of anything */
109          return EOF;            /* used up all command-line arguments */
110       }
111
112       /*---------------------------------------------------------------------
113        * If the next argv[] is not an option, there can be no more options.
114        *-------------------------------------------------------------------*/
115       pArgString = argv[optind++];      /* set this to the next argument ptr */
116
117 #ifdef GETOPT_USE_SLASH
118       if (('/' != *pArgString) &&       /* doesn't start with a slash or a dash? */
119           ('-' != *pArgString)) {
120          --optind;              /* point to current arg once we're done */
121          optarg = NULL;         /* no argument follows the option */
122          pIndexPosition = NULL; /* not in the middle of anything */
123          return EOF;            /* used up all the command-line flags */
124       }
125 #else
126       if ('-' != *pArgString) { /* doesn't start with a dash? */
127          --optind;              /* point to current arg once we're done */
128          optarg = NULL;         /* no argument follows the option */
129          pIndexPosition = NULL; /* not in the middle of anything */
130          return EOF;            /* used up all the command-line flags */
131       }
132 #endif
133
134       /* check for special end-of-flags markers */
135       if ((strcmp(pArgString, "-") == 0) ||
136           (strcmp(pArgString, "--") == 0)) {
137          optarg = NULL;         /* no argument follows the option */
138          pIndexPosition = NULL; /* not in the middle of anything */
139          return EOF;            /* encountered the special flag */
140       }
141
142       pArgString++;             /* look past the / or - */
143    }
144
145    if (':' == *pArgString) {    /* is it a colon? */
146       /*---------------------------------------------------------------------
147        * Rare case: if opterr is non-zero, return a question mark;
148        * otherwise, just return the colon we're on.
149        *-------------------------------------------------------------------*/
150       return (opterr ? (int) '?' : (int) ':');
151    } else if ((pOptString = strchr(opstring, *pArgString)) == 0) {
152       /*---------------------------------------------------------------------
153        * The letter on the command-line wasn't any good.
154        *-------------------------------------------------------------------*/
155       optarg = NULL;            /* no argument follows the option */
156       pIndexPosition = NULL;    /* not in the middle of anything */
157       return (opterr ? (int) '?' : (int) *pArgString);
158    } else {
159       /*---------------------------------------------------------------------
160        * The letter on the command-line matches one we expect to see
161        *-------------------------------------------------------------------*/
162       if (':' == _next_char(pOptString)) {      /* is the next letter a colon? */
163          /* It is a colon.  Look for an argument string. */
164          if ('\0' != _next_char(pArgString)) {  /* argument in this argv? */
165             optarg = &pArgString[1];    /* Yes, it is */
166          } else {
167         /*-------------------------------------------------------------
168          * The argument string must be in the next argv.
169          * But, what if there is none (bad input from the user)?
170          * In that case, return the letter, and optarg as NULL.
171          *-----------------------------------------------------------*/
172             if (optind < argc)
173                optarg = argv[optind++];
174             else {
175                optarg = NULL;
176                return (opterr ? (int) '?' : (int) *pArgString);
177             }
178          }
179          pIndexPosition = NULL; /* not in the middle of anything */
180       } else {
181          /* it's not a colon, so just return the letter */
182          optarg = NULL;         /* no argument follows the option */
183          pIndexPosition = pArgString;   /* point to the letter we're on */
184       }
185       return (int) *pArgString; /* return the letter that matched */
186    }
187 }