Debian Codegraph @

src/bin/dwarfdump/dd_getopt.c

raw
1 /* $NetBSD: getopt.c,v 1.1 2009/03/22 22:33:13 joerg Exp $*/
2 /* Modified by David Anderson to work with GNU/Linux and freebsd.
3 Added {} for clarity.
4 Switched to standard dwarfdump formatting.
5 Treatment of : modified so that :: gets dwoptarg NULL
6 if space follows the letter
7 (the dwoptarg is set to null).
8 renamed to make it clear this is a private version.
9 Oct 17 2017: Created dwgetopt_long(). See dwgetopt.h
10 */
11 /*
12 * Copyright (c) 1987, 1993, 1994
13 * The Regents of the University of California. All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 * 3. Neither the name of the University nor the names of its
25 * contributors may be used to endorse or promote products
26 * derived from this software without specific prior written
27 * permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
30 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
33 * SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
37 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
38 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 /* This does not presently handle the option string
45 leading + or leading - features. Such are not used
46 by by libdwarfdump. Nor does it understand the
47 GNU Env var POSIXLY_CORRECT .
48 It does know of the leading ":" in the option string.
49 See BADCH below.
50 */
51
52 #include <config.h>
53
54 #include <stddef.h> /* NULL size_t */
55 #include <stdio.h> /* printf() */
56 #include <string.h> /* strlen() strchr() strcmp() strncmp() */
57 #include "dd_minimal.h" /* dd_minimal_count_global_error */
58
59 #include "dd_getopt.h"
60
61 #define STRIP_OFF_CONSTNESS(a) ((void *)(size_t)(const void *)(a))
62
63 int dwopterr = 1, /* if error message should be printed */
64 dwoptind = 1, /* index into parent argv vector */
65 dwoptopt, /* character checked for validity */
66 dwoptreset; /* reset getopt */
67 char *dwoptarg; /* argument associated with option */
68
69 #define BADCH (int)'?'
70 #define BADARG (int)':'
71 #define EMSG ""
72
73 #define TRUE 1
74 #define FALSE 0
75
76 #if 0 /* FOR DEBUGGING ONLY */
77 /* Use for testing dwgetopt only.
78 Not a standard function. */
79 void
80 dwgetoptresetfortestingonly(void)
81 {
82 dwopterr = 1;
83 dwoptind = 1;
84 dwoptopt = 0;
85 dwoptreset = 0;
86 dwoptarg = 0;
87 }
88 #endif /* FOR DEBUGGING ONLY */
89
90 static const char *place = EMSG;/* option letter processing */
91
92 /* Post Condition:
93 if return FALSE then *argerr is set false. */
94 static int
95 dwoptnamematches(
96 const struct dwoption *dwlopt,
97 const char *iplace,
98 const char **argloc,
99 int *argerr)
100 {
101
102 const char *eq = 0;
103 unsigned long namelen = 0;
104 size_t arglenszt = 0;
105 int d = 0;
106
107 for (eq = iplace; *eq; ++eq) {
108 if (*eq != '=') {
109 continue;
110 }
111 /* Found =, arg should follow */
112 namelen = (unsigned long)(eq - iplace);
113 if (namelen != (unsigned long)strlen(dwlopt->name)) {
114 return FALSE;
115 }
116 eq++;
117 arglenszt = strlen(eq);
118 break;
119 }
120 if (namelen) {
121 d = strncmp(iplace,dwlopt->name,namelen);
122 if (d) {
123 return FALSE;
124 }
125 if (dwlopt->has_arg == 0) {
126 *argerr = TRUE;
127 return TRUE;
128 }
129 if (arglenszt) {
130 /* Discarding const, avoiding warning.
131 Data is in user space, so this is ok. */
132 dwoptarg = (char *)eq;
133 *argloc = (const char *)eq;
134 } else {
135 /* Has arg = but arg is empty. */
136 dwoptarg = 0;
137 }
138 return TRUE;
139 } else {
140 d = strcmp(iplace,dwlopt->name);
141 if (d) {
142 return FALSE;
143 }
144 if (dwlopt->has_arg == 1) {
145 *argerr = TRUE;
146 return TRUE;
147 }
148 dwoptarg = 0;
149 return TRUE;
150 }
151 }
152
153 /* dwgetopt_long
154 A reimplemention of a portion of
155 the getopt(3) GNU/Linux getopt_long().
156 See dwgetopt.h for more details.
157 */
158 int dwgetopt_long(int nargc, char *const nargv[],
159 const char *ostr,
160 const struct dwoption* longopts,
161 int *longindex)
162 {
163 char *lplace = 0;
164 if (dwoptreset) {
165 /* Not really supported. */
166 place = EMSG;
167 return (-1);
168 }
169 if (*place) {
170 int v = dwgetopt(nargc,nargv,ostr);
171 return v;
172 }
173 /* Use local lplace in case we need to call getopt()
174 just below. */
175 lplace = nargv[dwoptind];
176 if (dwoptind >= nargc || *lplace++ != '-') {
177 /* Argument is absent or is not an option */
178 place = EMSG;
179 return (-1);
180 }
181 if (*lplace != '-') {
182 /* Notice place not disturbed. */
183 int v = dwgetopt(nargc,nargv,ostr);
184 return v;
185 }
186 /* Starts with two dashes.
187 Now we set the global place */
188 place = lplace+1;
189 if (!*place) {
190 /* "--" => end of options */
191 ++dwoptind;
192 place = EMSG;
193 return (-1);
194 }
195
196 /* We think this is a longopt. */
197 {
198 int lo_num = 0;
199 int dwlopt_val = 0;
200
201 for (;;lo_num++) {
202 const struct dwoption *dwlopt = longopts +lo_num;
203 const char * argloc = 0;
204 int argerr = 0;
205 int resmatch = 0;
206
207 if (!dwlopt->name) {
208 dwoptind++;
209 (void)printf("ERROR:"
210 " invalid long option '--%s'\n", place);
211 /* Leave longindex unchanged. */
212 dd_minimal_count_global_error();
213 place = EMSG;
214 return (BADCH);
215 }
216 resmatch= dwoptnamematches(dwlopt,place,
217 &argloc,&argerr);
218 if (resmatch) {
219 dwoptarg = 0;
220 if (argloc) {
221 /* Must drop const here. Ugh. */
222 dwoptarg = (char *)argloc;
223 }
224 }
225 if (argerr) {
226 /* resmatch == TRUE
227
228 arg option missing if required, present
229 but not allowed.
230 GNU Behavior not well documented.
231 Had to experiment.
232
233 if argument-not-allowed, and we have one,
234 do ???
235
236 If argument-required,
237 then here GNU
238 would take the next argv as the argument.
239 we are not currently doing that. */
240 /**longindex = lo_num; */
241 if (dwlopt->has_arg) {
242 /* Missing required arg, this does not
243 match GNU getopt_long behavior
244 of taking next argv as the arg value.
245 and thus making getopt_long succeed. */
246 (void)printf("ERROR:"
247 " missing required long option "
248 "argument '--%s'\n", place);
249 dd_minimal_count_global_error();
250 } else {
251 /* has arg but should not */
252 (void)printf("ERROR:"
253 " option '--%s' does not allow an argument\n",
254 dwlopt->name);
255 dd_minimal_count_global_error();
256 }
257 dwoptind++;
258 place = EMSG;
259 return (BADCH);
260 }
261 if (resmatch) {
262 *longindex = lo_num;
263 dwlopt_val = dwlopt->val;
264 break;
265 }
266 }
267 place = EMSG;
268 dwoptind++;
269 return dwlopt_val;
270 }
271 }
272
273 /*
274 * getopt --
275 * Parse argc/argv argument vector.
276 * a: means
277 * -afoo
278 * -a foo
279 * and 'foo' is returned in dwoptarg
280 * b:: means
281 * -b
282 * and dwoptarg is null
283 * -bother
284 * and dwoptarg is 'other'
285 */
286 int
287 dwgetopt(int nargc, char * const nargv[], const char *ostr)
288 {
289 char *oli; /* option letter list index */
290
291 if (dwoptreset || *place == 0) { /* update scanning pointer */
292 dwoptreset = 0;
293 place = nargv[dwoptind];
294
295 if (dwoptind >= nargc || *place++ != '-') {
296 /* Argument is absent or is not an option */
297 place = EMSG;
298 return (-1);
299 }
300 dwoptopt = *place++;
301 if (dwoptopt == '-' && *place == 0) {
302 /* "--" => end of options */
303 ++dwoptind;
304 place = EMSG;
305 return (-1);
306 }
307 if (dwoptopt == 0) {
308 /* Solitary '-', treat as a '-' option
309 if the program (eg su) is looking for it. */
310 place = EMSG;
311 if (strchr(ostr, '-') == NULL) {
312 return -1;
313 }
314 dwoptopt = '-';
315 }
316 } else {
317 dwoptopt = *place++;
318 }
319 /* See if option letter is one the caller wanted... */
320 if (dwoptopt == ':' || (oli = strchr(ostr, dwoptopt)) == NULL) {
321 if (*place == 0) {
322 ++dwoptind;
323 }
324 if (dwopterr && *ostr != ':') {
325 (void)printf("ERROR:"
326 " invalid option -- '%c'\n", dwoptopt);
327 dd_minimal_count_global_error();
328 }
329 return (BADCH);
330 }
331
332 /* Does this option need an argument? */
333 if (oli[1] != ':') {
334 /* don't need argument */
335 dwoptarg = NULL;
336 if (*place == 0) {
337 ++dwoptind;
338 }
339 } else {
340 int reqnextarg = 1;
341 if (oli[1] && (oli[2] == ':')) {
342 /* Pair of :: means special treatment of dwoptarg */
343 reqnextarg = 0;
344 }
345 /* Option-argument is either the rest of this argument or the
346 entire next argument. */
347 if (*place ) {
348 /* Whether : or :: */
349 dwoptarg = STRIP_OFF_CONSTNESS(place);
350 } else if (reqnextarg) {
351 /* ! *place */
352 if (nargc > (++dwoptind)) {
353 dwoptarg = nargv[dwoptind];
354 } else {
355 place=EMSG;
356 /* Next arg required, but is missing */
357 if (*ostr == ':') {
358 /* Leading : in ostr calls for BADARG return. */
359 return (BADARG);
360 }
361 if (dwopterr) {
362 (void)printf("ERROR:"
363 " option requires an argument. -- '%c'\n",
364 dwoptopt);
365 dd_minimal_count_global_error();
366 }
367 return (BADCH);
368 }
369 } else {
370 /* ! *place */
371 /* The key part of :: treatment. */
372 dwoptarg = NULL;
373 }
374 place = EMSG;
375 ++dwoptind;
376 }
377 return (dwoptopt); /* return option letter */
378 }