annotate XGetopt.c @ 14:3f2fedec798c

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