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