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