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