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