Mercurial > libpst
annotate src/XGetopt.c @ 395:fbe51207b963
changes to build on centos 6
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Sun, 12 Jan 2020 14:10:17 -0800 |
parents | fc11b1d1ad34 |
children |
rev | line source |
---|---|
16 | 1 // XGetopt.cpp Version 1.1 |
2 // | |
3 // Author: Hans Dietrich | |
4 // hdietrich2@hotmail.com | |
5 // | |
6 // Modified: David Smith | |
7 // dave.s@earthcorp.com | |
8 // Moved two char declarations from body of function so | |
9 // that it can compile as a C function. | |
10 // Thanks so much Hans | |
11 // | |
12 // This software is released into the public domain. | |
13 // You are free to use it in any way you like. | |
14 // | |
15 // This software is provided "as is" with no expressed | |
16 // or implied warranty. I accept no liability for any | |
17 // damage or loss of business that this software may cause. | |
18 // | |
19 /////////////////////////////////////////////////////////////////////////////// | |
20 | |
118
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
16
diff
changeset
|
21 #ifndef __MINGW32__ /* mingw has getopt() */ |
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
16
diff
changeset
|
22 |
123
ab2a11e72250
more cleanup of #include files.
Carl Byington <carl@five-ten-sg.com>
parents:
118
diff
changeset
|
23 #include "define.h" |
ab2a11e72250
more cleanup of #include files.
Carl Byington <carl@five-ten-sg.com>
parents:
118
diff
changeset
|
24 |
16 | 25 |
26 /////////////////////////////////////////////////////////////////////////////// | |
27 // | |
28 // X G e t o p t . c p p | |
29 // | |
30 // | |
31 // NAME | |
32 // getopt -- parse command line options | |
33 // | |
34 // SYNOPSIS | |
118
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
16
diff
changeset
|
35 // int getopt(int argc, char* const* argv, const char *optstring) |
16 | 36 // |
37 // extern char *optarg; | |
38 // extern int optind; | |
39 // | |
40 // DESCRIPTION | |
41 // The getopt() function parses the command line arguments. Its | |
42 // arguments argc and argv are the argument count and array as | |
43 // passed into the application on program invocation. In the case | |
44 // of Visual C++ programs, argc and argv are available via the | |
45 // variables __argc and __argv (double underscores), respectively. | |
46 // getopt returns the next option letter in argv that matches a | |
47 // letter in optstring. | |
48 // | |
49 // optstring is a string of recognized option letters; if a letter | |
50 // is followed by a colon, the option is expected to have an argument | |
51 // that may or may not be separated from it by white space. optarg | |
52 // is set to point to the start of the option argument on return from | |
53 // getopt. | |
54 // | |
55 // Option letters may be combined, e.g., "-ab" is equivalent to | |
56 // "-a -b". Option letters are case sensitive. | |
57 // | |
58 // getopt places in the external variable optind the argv index | |
59 // of the next argument to be processed. optind is initialized | |
60 // to 0 before the first call to getopt. | |
61 // | |
62 // When all options have been processed (i.e., up to the first | |
63 // non-option argument), getopt returns EOF, optarg will point | |
64 // to the argument, and optind will be set to the argv index of | |
65 // the argument. If there are no non-option arguments, optarg | |
66 // will be set to NULL. | |
67 // | |
68 // The special option "--" may be used to delimit the end of the | |
69 // options; EOF will be returned, and "--" (and everything after it) | |
70 // will be skipped. | |
71 // | |
72 // RETURN VALUE | |
73 // For option letters contained in the string optstring, getopt | |
74 // will return the option letter. getopt returns a question mark (?) | |
75 // when it encounters an option letter not included in optstring. | |
76 // EOF is returned when processing is finished. | |
77 // | |
78 // BUGS | |
79 // 1) Long options are not supported. | |
80 // 2) The GNU double-colon extension is not supported. | |
81 // 3) The environment variable POSIXLY_CORRECT is not supported. | |
82 // 4) The + syntax is not supported. | |
83 // 5) The automatic permutation of arguments is not supported. | |
84 // 6) This implementation of getopt() returns EOF if an error is | |
85 // encountered, instead of -1 as the latest standard requires. | |
86 // | |
87 // EXAMPLE | |
88 // BOOL CMyApp::ProcessCommandLine(int argc, char *argv[]) | |
89 // { | |
90 // int c; | |
91 // | |
92 // while ((c = getopt(argc, argv, "aBn:")) != EOF) | |
93 // { | |
94 // switch (c) | |
95 // { | |
96 // case 'a': | |
97 // TRACE(_T("option a\n")); | |
98 // // | |
99 // // set some flag here | |
100 // // | |
101 // break; | |
102 // | |
103 // case 'B': | |
104 // TRACE( _T("option B\n")); | |
105 // // | |
106 // // set some other flag here | |
107 // // | |
108 // break; | |
109 // | |
110 // case 'n': | |
111 // TRACE(_T("option n: value=%d\n"), atoi(optarg)); | |
112 // // | |
113 // // do something with value here | |
114 // // | |
115 // break; | |
116 // | |
117 // case '?': | |
118 // TRACE(_T("ERROR: illegal option %s\n"), argv[optind-1]); | |
119 // return FALSE; | |
120 // break; | |
121 // | |
122 // default: | |
123 // TRACE(_T("WARNING: no handler for option %c\n"), c); | |
124 // return FALSE; | |
125 // break; | |
126 // } | |
127 // } | |
128 // // | |
129 // // check for non-option args here | |
130 // // | |
131 // return TRUE; | |
132 // } | |
133 // | |
134 /////////////////////////////////////////////////////////////////////////////// | |
135 | |
136 char *optarg; // global argument pointer | |
137 int optind = 0; // global argv index | |
138 | |
118
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
16
diff
changeset
|
139 int getopt(int argc, char* const* argv, char *optstring) |
16 | 140 { |
118
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
16
diff
changeset
|
141 static char *next = NULL; |
16 | 142 char c, *cp; |
143 if (optind == 0) | |
144 next = NULL; | |
145 | |
146 optarg = NULL; | |
147 | |
148 if (next == NULL || *next == '\0') | |
149 { | |
150 if (optind == 0) | |
151 optind++; | |
152 | |
153 if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0') | |
154 { | |
155 optarg = NULL; | |
156 if (optind < argc) | |
157 optarg = argv[optind]; | |
158 return EOF; | |
159 } | |
160 | |
161 if (strcmp(argv[optind], "--") == 0) | |
162 { | |
163 optind++; | |
164 optarg = NULL; | |
165 if (optind < argc) | |
166 optarg = argv[optind]; | |
167 return EOF; | |
168 } | |
169 | |
170 next = argv[optind]+1; | |
171 optind++; | |
172 } | |
173 | |
174 c = *next++; | |
175 cp = strchr(optstring, c); | |
176 | |
177 if (cp == NULL || c == ':') | |
178 return '?'; | |
179 | |
180 cp++; | |
181 if (*cp == ':') | |
182 { | |
183 if (*next != '\0') | |
184 { | |
185 optarg = next; | |
186 next = NULL; | |
187 } | |
188 else if (optind < argc) | |
189 { | |
190 optarg = argv[optind]; | |
191 optind++; | |
192 } | |
193 else | |
194 { | |
195 return '?'; | |
196 } | |
197 } | |
198 | |
199 return c; | |
200 } | |
118
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
16
diff
changeset
|
201 |
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
16
diff
changeset
|
202 #endif /* !__MINGW32__ */ |