annotate src/XGetopt.c @ 90:631d02d30a1c

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