Mercurial > hg > xemacs-beta
annotate lib-src/getopt.c @ 5856:27876789edc5
Be more careful about saving a non-nil value for START in message-stack.
lisp/ChangeLog addition:
2015-03-12 Aidan Kehoe <kehoea@parhasard.net>
* simple.el (append-message): Be more careful about saving a
non-nil value for START in message-stack.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Thu, 12 Mar 2015 00:59:27 +0000 |
parents | 061f4f90f874 |
children |
rev | line source |
---|---|
428 | 1 /* Getopt for GNU. |
2 NOTE: getopt is now part of the C library, so if you don't know what | |
3 "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu | |
4 before changing it! | |
5 | |
6 Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97 | |
7 Free Software Foundation, Inc. | |
8 | |
9 NOTE: The canonical source of this file is maintained with the GNU C Library. | |
10 Bugs can be reported to bug-glibc@prep.ai.mit.edu. | |
11 | |
5406
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
442
diff
changeset
|
12 This program is free software: you can redistribute it and/or modify it |
428 | 13 under the terms of the GNU General Public License as published by the |
5406
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
442
diff
changeset
|
14 Free Software Foundation, either version 3 of the License, or (at your |
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
442
diff
changeset
|
15 option) any later version. |
428 | 16 |
5406
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
442
diff
changeset
|
17 This program is distributed in the hope that it will be useful, but WITHOUT |
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
442
diff
changeset
|
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
442
diff
changeset
|
19 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
442
diff
changeset
|
20 for more details. |
428 | 21 |
22 You should have received a copy of the GNU General Public License | |
5406
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
442
diff
changeset
|
23 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
428 | 24 |
25 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. | |
26 Ditto for AIX 3.2 and <stdlib.h>. */ | |
27 #ifndef _NO_PROTO | |
28 #define _NO_PROTO | |
29 #endif | |
30 | |
31 #ifdef HAVE_CONFIG_H | |
438 | 32 #include <config.h> |
428 | 33 #endif |
34 | |
35 #include <stdio.h> | |
36 #include <string.h> | |
37 #include <stdlib.h> | |
38 | |
39 /* Comment out all this code if we are using the GNU C Library, and are not | |
40 actually compiling the library itself. This code is part of the GNU C | |
41 Library, but also included in many other GNU distributions. Compiling | |
42 and linking in this code is a waste when using the GNU C library | |
43 (especially if it is a shared library). Rather than having every GNU | |
44 program understand `configure --with-gnu-libc' and omit the object files, | |
45 it is simpler to just do this in the source for each such file. */ | |
46 | |
47 #define GETOPT_INTERFACE_VERSION 2 | |
48 #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2 | |
49 #include <gnu-versions.h> | |
50 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION | |
51 #define ELIDE_CODE | |
52 #endif | |
53 #endif | |
54 | |
55 #ifndef ELIDE_CODE | |
56 | |
57 | |
58 /* This needs to come after some library #include | |
59 to get __GNU_LIBRARY__ defined. */ | |
60 #ifdef __GNU_LIBRARY__ | |
61 /* Don't include stdlib.h for non-GNU C libraries because some of them | |
62 contain conflicting prototypes for getopt. */ | |
63 #include <stdlib.h> | |
64 #include <unistd.h> | |
65 #endif /* GNU C library. */ | |
66 | |
67 #ifdef VMS | |
68 #include <unixlib.h> | |
69 #if HAVE_STRING_H - 0 | |
70 #include <string.h> | |
71 #endif | |
72 #endif | |
73 | |
442 | 74 #if defined (WIN32_NATIVE) && !defined (CYGWIN) |
428 | 75 /* It's not Unix, really. See? Capital letters. */ |
76 #include <windows.h> | |
77 #undef getpid | |
78 #define getpid() GetCurrentProcessId() | |
79 #endif | |
80 | |
81 #ifndef _ | |
82 /* This is for other GNU distributions with internationalized messages. | |
83 When compiling libc, the _ macro is predefined. */ | |
84 #ifdef HAVE_LIBINTL_H | |
85 # include <libintl.h> | |
86 # define _(msgid) gettext (msgid) | |
87 #else | |
88 # define _(msgid) (msgid) | |
89 #endif | |
90 #endif | |
91 | |
92 /* This version of `getopt' appears to the caller like standard Unix `getopt' | |
93 but it behaves differently for the user, since it allows the user | |
94 to intersperse the options with the other arguments. | |
95 | |
96 As `getopt' works, it permutes the elements of ARGV so that, | |
97 when it is done, all the options precede everything else. Thus | |
98 all application programs are extended to handle flexible argument order. | |
99 | |
100 Setting the environment variable POSIXLY_CORRECT disables permutation. | |
101 Then the behavior is completely standard. | |
102 | |
103 GNU application programs can use a third alternative mode in which | |
104 they can distinguish the relative order of options and other arguments. */ | |
105 | |
106 #include "getopt.h" | |
107 | |
108 /* For communication from `getopt' to the caller. | |
109 When `getopt' finds an option that takes an argument, | |
110 the argument value is returned here. | |
111 Also, when `ordering' is RETURN_IN_ORDER, | |
112 each non-option ARGV-element is returned here. */ | |
113 | |
114 char *optarg = NULL; | |
115 | |
116 /* Index in ARGV of the next element to be scanned. | |
117 This is used for communication to and from the caller | |
118 and for communication between successive calls to `getopt'. | |
119 | |
120 On entry to `getopt', zero means this is the first call; initialize. | |
121 | |
122 When `getopt' returns -1, this is the index of the first of the | |
123 non-option elements that the caller should itself scan. | |
124 | |
125 Otherwise, `optind' communicates from one call to the next | |
126 how much of ARGV has been scanned so far. */ | |
127 | |
128 /* 1003.2 says this must be 1 before any call. */ | |
129 int optind = 1; | |
130 | |
131 /* Formerly, initialization of getopt depended on optind==0, which | |
132 causes problems with re-calling getopt as programs generally don't | |
133 know that. */ | |
134 | |
135 int __getopt_initialized = 0; | |
136 | |
137 /* The next char to be scanned in the option-element | |
138 in which the last option character we returned was found. | |
139 This allows us to pick up the scan where we left off. | |
140 | |
141 If this is zero, or a null string, it means resume the scan | |
142 by advancing to the next ARGV-element. */ | |
143 | |
144 static char *nextchar; | |
145 | |
146 /* Callers store zero here to inhibit the error message | |
147 for unrecognized options. */ | |
148 | |
149 int opterr = 1; | |
150 | |
151 /* Set to an option character which was unrecognized. | |
152 This must be initialized on some systems to avoid linking in the | |
153 system's own getopt implementation. */ | |
154 | |
155 int optopt = '?'; | |
156 | |
157 /* Describe how to deal with options that follow non-option ARGV-elements. | |
158 | |
159 If the caller did not specify anything, | |
160 the default is REQUIRE_ORDER if the environment variable | |
161 POSIXLY_CORRECT is defined, PERMUTE otherwise. | |
162 | |
163 REQUIRE_ORDER means don't recognize them as options; | |
164 stop option processing when the first non-option is seen. | |
165 This is what Unix does. | |
166 This mode of operation is selected by either setting the environment | |
167 variable POSIXLY_CORRECT, or using `+' as the first character | |
168 of the list of option characters. | |
169 | |
170 PERMUTE is the default. We permute the contents of ARGV as we scan, | |
171 so that eventually all the non-options are at the end. This allows options | |
172 to be given in any order, even with programs that were not written to | |
173 expect this. | |
174 | |
175 RETURN_IN_ORDER is an option available to programs that were written | |
176 to expect options and other ARGV-elements in any order and that care about | |
177 the ordering of the two. We describe each non-option ARGV-element | |
178 as if it were the argument of an option with character code 1. | |
179 Using `-' as the first character of the list of option characters | |
180 selects this mode of operation. | |
181 | |
182 The special argument `--' forces an end of option-scanning regardless | |
183 of the value of `ordering'. In the case of RETURN_IN_ORDER, only | |
184 `--' can cause `getopt' to return -1 with `optind' != ARGC. */ | |
185 | |
186 static enum | |
187 { | |
188 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER | |
189 } ordering; | |
190 | |
191 /* Value of POSIXLY_CORRECT environment variable. */ | |
192 static char *posixly_correct; | |
193 | |
194 #ifdef __GNU_LIBRARY__ | |
195 /* We want to avoid inclusion of string.h with non-GNU libraries | |
196 because there are many ways it can cause trouble. | |
197 On some systems, it contains special magic macros that don't work | |
198 in GCC. */ | |
199 #include <string.h> | |
200 #define my_index strchr | |
201 #else | |
202 | |
203 static char * | |
204 my_index (const char *str, int chr) | |
205 { | |
206 while (*str) | |
207 { | |
208 if (*str == chr) | |
209 return (char *) str; | |
210 str++; | |
211 } | |
212 return 0; | |
213 } | |
214 | |
215 /* If using GCC, we can safely declare strlen this way. | |
216 If not using GCC, it is ok not to declare it. */ | |
217 #ifdef __GNUC__ | |
218 /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. | |
219 That was relevant to code that was here before. */ | |
220 #if !defined (__STDC__) || !__STDC__ | |
221 /* gcc with -traditional declares the built-in strlen to return int, | |
222 and has done so at least since version 2.4.5. -- rms. */ | |
223 extern int strlen (const char *); | |
224 #endif /* not __STDC__ */ | |
225 #endif /* __GNUC__ */ | |
226 | |
227 #endif /* not __GNU_LIBRARY__ */ | |
228 | |
229 /* Handle permutation of arguments. */ | |
230 | |
231 /* Describe the part of ARGV that contains non-options that have | |
232 been skipped. `first_nonopt' is the index in ARGV of the first of them; | |
233 `last_nonopt' is the index after the last of them. */ | |
234 | |
235 static int first_nonopt; | |
236 static int last_nonopt; | |
237 | |
238 #ifdef _LIBC | |
239 /* Bash 2.0 gives us an environment variable containing flags | |
240 indicating ARGV elements that should not be considered arguments. */ | |
241 | |
242 /* Defined in getopt_init.c */ | |
243 extern char *__getopt_nonoption_flags; | |
244 | |
245 static int nonoption_flags_max_len; | |
246 static int nonoption_flags_len; | |
247 | |
248 static int original_argc; | |
249 static char *const *original_argv; | |
250 | |
251 extern pid_t __libc_pid; | |
252 | |
253 /* Make sure the environment variable bash 2.0 puts in the environment | |
254 is valid for the getopt call we must make sure that the ARGV passed | |
255 to getopt is that one passed to the process. */ | |
256 static void | |
257 __attribute__ ((unused)) | |
258 store_args_and_env (int argc, char *const *argv) | |
259 { | |
260 /* XXX This is no good solution. We should rather copy the args so | |
261 that we can compare them later. But we must not use malloc(3). */ | |
262 original_argc = argc; | |
263 original_argv = argv; | |
264 } | |
265 text_set_element (__libc_subinit, store_args_and_env); | |
266 | |
267 # define SWAP_FLAGS(ch1, ch2) \ | |
268 if (nonoption_flags_len > 0) \ | |
269 { \ | |
270 char __tmp = __getopt_nonoption_flags[ch1]; \ | |
271 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ | |
272 __getopt_nonoption_flags[ch2] = __tmp; \ | |
273 } | |
274 #else /* !_LIBC */ | |
275 # define SWAP_FLAGS(ch1, ch2) | |
276 #endif /* _LIBC */ | |
277 | |
278 /* Exchange two adjacent subsequences of ARGV. | |
279 One subsequence is elements [first_nonopt,last_nonopt) | |
280 which contains all the non-options that have been skipped so far. | |
281 The other is elements [last_nonopt,optind), which contains all | |
282 the options processed since those non-options were skipped. | |
283 | |
284 `first_nonopt' and `last_nonopt' are relocated so that they describe | |
285 the new indices of the non-options in ARGV after they are moved. */ | |
286 | |
287 #if defined (__STDC__) && __STDC__ | |
288 static void exchange (char **); | |
289 #endif | |
290 | |
291 static void | |
292 exchange (char **argv) | |
293 { | |
294 int bottom = first_nonopt; | |
295 int middle = last_nonopt; | |
296 int top = optind; | |
297 char *tem; | |
298 | |
299 /* Exchange the shorter segment with the far end of the longer segment. | |
300 That puts the shorter segment into the right place. | |
301 It leaves the longer segment in the right place overall, | |
302 but it consists of two parts that need to be swapped next. */ | |
303 | |
304 #ifdef _LIBC | |
305 /* First make sure the handling of the `__getopt_nonoption_flags' | |
306 string can work normally. Our top argument must be in the range | |
307 of the string. */ | |
308 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) | |
309 { | |
310 /* We must extend the array. The user plays games with us and | |
311 presents new arguments. */ | |
312 char *new_str = malloc (top + 1); | |
313 if (new_str == NULL) | |
314 nonoption_flags_len = nonoption_flags_max_len = 0; | |
315 else | |
316 { | |
317 memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len); | |
318 memset (&new_str[nonoption_flags_max_len], '\0', | |
319 top + 1 - nonoption_flags_max_len); | |
320 nonoption_flags_max_len = top + 1; | |
321 __getopt_nonoption_flags = new_str; | |
322 } | |
323 } | |
324 #endif | |
325 | |
326 while (top > middle && middle > bottom) | |
327 { | |
328 if (top - middle > middle - bottom) | |
329 { | |
330 /* Bottom segment is the short one. */ | |
331 int len = middle - bottom; | |
332 register int i; | |
333 | |
334 /* Swap it with the top part of the top segment. */ | |
335 for (i = 0; i < len; i++) | |
336 { | |
337 tem = argv[bottom + i]; | |
338 argv[bottom + i] = argv[top - (middle - bottom) + i]; | |
339 argv[top - (middle - bottom) + i] = tem; | |
340 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); | |
341 } | |
342 /* Exclude the moved bottom segment from further swapping. */ | |
343 top -= len; | |
344 } | |
345 else | |
346 { | |
347 /* Top segment is the short one. */ | |
348 int len = top - middle; | |
349 register int i; | |
350 | |
351 /* Swap it with the bottom part of the bottom segment. */ | |
352 for (i = 0; i < len; i++) | |
353 { | |
354 tem = argv[bottom + i]; | |
355 argv[bottom + i] = argv[middle + i]; | |
356 argv[middle + i] = tem; | |
357 SWAP_FLAGS (bottom + i, middle + i); | |
358 } | |
359 /* Exclude the moved top segment from further swapping. */ | |
360 bottom += len; | |
361 } | |
362 } | |
363 | |
364 /* Update records for the slots the non-options now occupy. */ | |
365 | |
366 first_nonopt += (optind - last_nonopt); | |
367 last_nonopt = optind; | |
368 } | |
369 | |
370 /* Initialize the internal data when the first call is made. */ | |
371 | |
372 #if defined (__STDC__) && __STDC__ | |
373 static const char *_getopt_initialize (int, char *const *, const char *); | |
374 #endif | |
375 static const char * | |
376 _getopt_initialize (int argc, char *const *argv, const char *optstring) | |
377 { | |
378 /* Start processing options with ARGV-element 1 (since ARGV-element 0 | |
379 is the program name); the sequence of previously skipped | |
380 non-option ARGV-elements is empty. */ | |
381 | |
382 first_nonopt = last_nonopt = optind; | |
383 | |
384 nextchar = NULL; | |
385 | |
386 posixly_correct = getenv ("POSIXLY_CORRECT"); | |
387 | |
388 /* Determine how to handle the ordering of options and nonoptions. */ | |
389 | |
390 if (optstring[0] == '-') | |
391 { | |
392 ordering = RETURN_IN_ORDER; | |
393 ++optstring; | |
394 } | |
395 else if (optstring[0] == '+') | |
396 { | |
397 ordering = REQUIRE_ORDER; | |
398 ++optstring; | |
399 } | |
400 else if (posixly_correct != NULL) | |
401 ordering = REQUIRE_ORDER; | |
402 else | |
403 ordering = PERMUTE; | |
404 | |
405 #ifdef _LIBC | |
406 if (posixly_correct == NULL | |
407 && argc == original_argc && argv == original_argv) | |
408 { | |
409 if (nonoption_flags_max_len == 0) | |
410 { | |
411 if (__getopt_nonoption_flags == NULL | |
412 || __getopt_nonoption_flags[0] == '\0') | |
413 nonoption_flags_max_len = -1; | |
414 else | |
415 { | |
416 const char *orig_str = __getopt_nonoption_flags; | |
417 int len = nonoption_flags_max_len = strlen (orig_str); | |
418 if (nonoption_flags_max_len < argc) | |
419 nonoption_flags_max_len = argc; | |
420 __getopt_nonoption_flags = | |
421 (char *) malloc (nonoption_flags_max_len); | |
422 if (__getopt_nonoption_flags == NULL) | |
423 nonoption_flags_max_len = -1; | |
424 else | |
425 { | |
426 memcpy (__getopt_nonoption_flags, orig_str, len); | |
427 memset (&__getopt_nonoption_flags[len], '\0', | |
428 nonoption_flags_max_len - len); | |
429 } | |
430 } | |
431 } | |
432 nonoption_flags_len = nonoption_flags_max_len; | |
433 } | |
434 else | |
435 nonoption_flags_len = 0; | |
436 #endif | |
437 | |
438 return optstring; | |
439 } | |
440 | |
441 /* Scan elements of ARGV (whose length is ARGC) for option characters | |
442 given in OPTSTRING. | |
443 | |
444 If an element of ARGV starts with '-', and is not exactly "-" or "--", | |
445 then it is an option element. The characters of this element | |
446 (aside from the initial '-') are option characters. If `getopt' | |
447 is called repeatedly, it returns successively each of the option characters | |
448 from each of the option elements. | |
449 | |
450 If `getopt' finds another option character, it returns that character, | |
451 updating `optind' and `nextchar' so that the next call to `getopt' can | |
452 resume the scan with the following option character or ARGV-element. | |
453 | |
454 If there are no more option characters, `getopt' returns -1. | |
455 Then `optind' is the index in ARGV of the first ARGV-element | |
456 that is not an option. (The ARGV-elements have been permuted | |
457 so that those that are not options now come last.) | |
458 | |
459 OPTSTRING is a string containing the legitimate option characters. | |
460 If an option character is seen that is not listed in OPTSTRING, | |
461 return '?' after printing an error message. If you set `opterr' to | |
462 zero, the error message is suppressed but we still return '?'. | |
463 | |
464 If a char in OPTSTRING is followed by a colon, that means it wants an arg, | |
465 so the following text in the same ARGV-element, or the text of the following | |
466 ARGV-element, is returned in `optarg'. Two colons mean an option that | |
467 wants an optional arg; if there is text in the current ARGV-element, | |
468 it is returned in `optarg', otherwise `optarg' is set to zero. | |
469 | |
470 If OPTSTRING starts with `-' or `+', it requests different methods of | |
471 handling the non-option ARGV-elements. | |
472 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. | |
473 | |
474 Long-named options begin with `--' instead of `-'. | |
475 Their names may be abbreviated as long as the abbreviation is unique | |
476 or is an exact match for some defined option. If they have an | |
477 argument, it follows the option name in the same ARGV-element, separated | |
478 from the option name by a `=', or else the in next ARGV-element. | |
479 When `getopt' finds a long-named option, it returns 0 if that option's | |
480 `flag' field is nonzero, the value of the option's `val' field | |
481 if the `flag' field is zero. | |
482 | |
483 The elements of ARGV aren't really const, because we permute them. | |
484 But we pretend they're const in the prototype to be compatible | |
485 with other systems. | |
486 | |
487 LONGOPTS is a vector of `struct option' terminated by an | |
488 element containing a name which is zero. | |
489 | |
490 LONGIND returns the index in LONGOPT of the long-named option found. | |
491 It is only valid when a long-named option has been found by the most | |
492 recent call. | |
493 | |
494 If LONG_ONLY is nonzero, '-' as well as '--' can introduce | |
495 long-named options. */ | |
496 | |
497 int | |
498 _getopt_internal (int argc, char *const *argv, const char *optstring, | |
499 const struct option *longopts, int *longind, int long_only) | |
500 { | |
501 optarg = NULL; | |
502 | |
503 if (optind == 0 || !__getopt_initialized) | |
504 { | |
505 if (optind == 0) | |
506 optind = 1; /* Don't scan ARGV[0], the program name. */ | |
507 optstring = _getopt_initialize (argc, argv, optstring); | |
508 __getopt_initialized = 1; | |
509 } | |
510 | |
511 /* Test whether ARGV[optind] points to a non-option argument. | |
512 Either it does not have option syntax, or there is an environment flag | |
513 from the shell indicating it is not an option. The later information | |
514 is only used when the used in the GNU libc. */ | |
515 #ifdef _LIBC | |
516 #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ | |
517 || (optind < nonoption_flags_len \ | |
518 && __getopt_nonoption_flags[optind] == '1')) | |
519 #else | |
520 #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') | |
521 #endif | |
522 | |
523 if (nextchar == NULL || *nextchar == '\0') | |
524 { | |
525 /* Advance to the next ARGV-element. */ | |
526 | |
527 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been | |
528 moved back by the user (who may also have changed the arguments). */ | |
529 if (last_nonopt > optind) | |
530 last_nonopt = optind; | |
531 if (first_nonopt > optind) | |
532 first_nonopt = optind; | |
533 | |
534 if (ordering == PERMUTE) | |
535 { | |
536 /* If we have just processed some options following some non-options, | |
537 exchange them so that the options come first. */ | |
538 | |
539 if (first_nonopt != last_nonopt && last_nonopt != optind) | |
540 exchange ((char **) argv); | |
541 else if (last_nonopt != optind) | |
542 first_nonopt = optind; | |
543 | |
544 /* Skip any additional non-options | |
545 and extend the range of non-options previously skipped. */ | |
546 | |
547 while (optind < argc && NONOPTION_P) | |
548 optind++; | |
549 last_nonopt = optind; | |
550 } | |
551 | |
552 /* The special ARGV-element `--' means premature end of options. | |
553 Skip it like a null option, | |
554 then exchange with previous non-options as if it were an option, | |
555 then skip everything else like a non-option. */ | |
556 | |
557 if (optind != argc && !strcmp (argv[optind], "--")) | |
558 { | |
559 optind++; | |
560 | |
561 if (first_nonopt != last_nonopt && last_nonopt != optind) | |
562 exchange ((char **) argv); | |
563 else if (first_nonopt == last_nonopt) | |
564 first_nonopt = optind; | |
565 last_nonopt = argc; | |
566 | |
567 optind = argc; | |
568 } | |
569 | |
570 /* If we have done all the ARGV-elements, stop the scan | |
571 and back over any non-options that we skipped and permuted. */ | |
572 | |
573 if (optind == argc) | |
574 { | |
575 /* Set the next-arg-index to point at the non-options | |
576 that we previously skipped, so the caller will digest them. */ | |
577 if (first_nonopt != last_nonopt) | |
578 optind = first_nonopt; | |
579 return -1; | |
580 } | |
581 | |
582 /* If we have come to a non-option and did not permute it, | |
583 either stop the scan or describe it to the caller and pass it by. */ | |
584 | |
585 if (NONOPTION_P) | |
586 { | |
587 if (ordering == REQUIRE_ORDER) | |
588 return -1; | |
589 optarg = argv[optind++]; | |
590 return 1; | |
591 } | |
592 | |
593 /* We have found another option-ARGV-element. | |
594 Skip the initial punctuation. */ | |
595 | |
596 nextchar = (argv[optind] + 1 | |
597 + (longopts != NULL && argv[optind][1] == '-')); | |
598 } | |
599 | |
600 /* Decode the current option-ARGV-element. */ | |
601 | |
602 /* Check whether the ARGV-element is a long option. | |
603 | |
604 If long_only and the ARGV-element has the form "-f", where f is | |
605 a valid short option, don't consider it an abbreviated form of | |
606 a long option that starts with f. Otherwise there would be no | |
607 way to give the -f short option. | |
608 | |
609 On the other hand, if there's a long option "fubar" and | |
610 the ARGV-element is "-fu", do consider that an abbreviation of | |
611 the long option, just like "--fu", and not "-f" with arg "u". | |
612 | |
613 This distinction seems to be the most useful approach. */ | |
614 | |
615 if (longopts != NULL | |
616 && (argv[optind][1] == '-' | |
617 || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) | |
618 { | |
619 char *nameend; | |
620 const struct option *p; | |
621 const struct option *pfound = NULL; | |
622 int exact = 0; | |
623 int ambig = 0; | |
624 int indfound = -1; | |
625 int option_index; | |
626 | |
627 for (nameend = nextchar; *nameend && *nameend != '='; nameend++) | |
628 /* Do nothing. */ ; | |
629 | |
630 /* Test all long options for either exact match | |
631 or abbreviated matches. */ | |
632 for (p = longopts, option_index = 0; p->name; p++, option_index++) | |
633 if (!strncmp (p->name, nextchar, nameend - nextchar)) | |
634 { | |
635 if ((unsigned int) (nameend - nextchar) | |
636 == (unsigned int) strlen (p->name)) | |
637 { | |
638 /* Exact match found. */ | |
639 pfound = p; | |
640 indfound = option_index; | |
641 exact = 1; | |
642 break; | |
643 } | |
644 else if (pfound == NULL) | |
645 { | |
646 /* First nonexact match found. */ | |
647 pfound = p; | |
648 indfound = option_index; | |
649 } | |
650 else | |
651 /* Second or later nonexact match found. */ | |
652 ambig = 1; | |
653 } | |
654 | |
655 if (ambig && !exact) | |
656 { | |
657 if (opterr) | |
658 fprintf (stderr, _("%s: option `%s' is ambiguous\n"), | |
659 argv[0], argv[optind]); | |
660 nextchar += strlen (nextchar); | |
661 optind++; | |
662 optopt = 0; | |
663 return '?'; | |
664 } | |
665 | |
666 if (pfound != NULL) | |
667 { | |
668 option_index = indfound; | |
669 optind++; | |
670 if (*nameend) | |
671 { | |
672 /* Don't test has_arg with >, because some C compilers don't | |
673 allow it to be used on enums. */ | |
674 if (pfound->has_arg) | |
675 optarg = nameend + 1; | |
676 else | |
677 { | |
678 if (opterr) { | |
679 if (argv[optind - 1][1] == '-') | |
680 /* --option */ | |
681 fprintf (stderr, | |
682 _("%s: option `--%s' doesn't allow an argument\n"), | |
683 argv[0], pfound->name); | |
684 else | |
685 /* +option or -option */ | |
686 fprintf (stderr, | |
687 _("%s: option `%c%s' doesn't allow an argument\n"), | |
688 argv[0], argv[optind - 1][0], pfound->name); | |
689 } | |
690 nextchar += strlen (nextchar); | |
691 | |
692 optopt = pfound->val; | |
693 return '?'; | |
694 } | |
695 } | |
696 else if (pfound->has_arg == 1) | |
697 { | |
698 if (optind < argc) | |
699 optarg = argv[optind++]; | |
700 else | |
701 { | |
702 if (opterr) | |
703 fprintf (stderr, | |
704 _("%s: option `%s' requires an argument\n"), | |
705 argv[0], argv[optind - 1]); | |
706 nextchar += strlen (nextchar); | |
707 optopt = pfound->val; | |
708 return optstring[0] == ':' ? ':' : '?'; | |
709 } | |
710 } | |
711 nextchar += strlen (nextchar); | |
712 if (longind != NULL) | |
713 *longind = option_index; | |
714 if (pfound->flag) | |
715 { | |
716 *(pfound->flag) = pfound->val; | |
717 return 0; | |
718 } | |
719 return pfound->val; | |
720 } | |
721 | |
722 /* Can't find it as a long option. If this is not getopt_long_only, | |
723 or the option starts with '--' or is not a valid short | |
724 option, then it's an error. | |
725 Otherwise interpret it as a short option. */ | |
726 if (!long_only || argv[optind][1] == '-' | |
727 || my_index (optstring, *nextchar) == NULL) | |
728 { | |
729 if (opterr) | |
730 { | |
731 if (argv[optind][1] == '-') | |
732 /* --option */ | |
733 fprintf (stderr, _("%s: unrecognized option `--%s'\n"), | |
734 argv[0], nextchar); | |
735 else | |
736 /* +option or -option */ | |
737 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), | |
738 argv[0], argv[optind][0], nextchar); | |
739 } | |
740 nextchar = (char *) ""; | |
741 optind++; | |
742 optopt = 0; | |
743 return '?'; | |
744 } | |
745 } | |
746 | |
747 /* Look at and handle the next short option-character. */ | |
748 | |
749 { | |
750 char c = *nextchar++; | |
751 char *temp = my_index (optstring, c); | |
752 | |
753 /* Increment `optind' when we start to process its last character. */ | |
754 if (*nextchar == '\0') | |
755 ++optind; | |
756 | |
757 if (temp == NULL || c == ':') | |
758 { | |
759 if (opterr) | |
760 { | |
761 if (posixly_correct) | |
762 /* 1003.2 specifies the format of this message. */ | |
763 fprintf (stderr, _("%s: illegal option -- %c\n"), | |
764 argv[0], c); | |
765 else | |
766 fprintf (stderr, _("%s: invalid option -- %c\n"), | |
767 argv[0], c); | |
768 } | |
769 optopt = c; | |
770 return '?'; | |
771 } | |
772 /* Convenience. Treat POSIX -W foo same as long option --foo */ | |
773 if (temp[0] == 'W' && temp[1] == ';') | |
774 { | |
775 char *nameend; | |
776 const struct option *p; | |
777 const struct option *pfound = NULL; | |
778 int exact = 0; | |
779 int ambig = 0; | |
780 int indfound = 0; | |
781 int option_index; | |
782 | |
783 /* This is an option that requires an argument. */ | |
784 if (*nextchar != '\0') | |
785 { | |
786 optarg = nextchar; | |
787 /* If we end this ARGV-element by taking the rest as an arg, | |
788 we must advance to the next element now. */ | |
789 optind++; | |
790 } | |
791 else if (optind == argc) | |
792 { | |
793 if (opterr) | |
794 { | |
795 /* 1003.2 specifies the format of this message. */ | |
796 fprintf (stderr, _("%s: option requires an argument -- %c\n"), | |
797 argv[0], c); | |
798 } | |
799 optopt = c; | |
800 if (optstring[0] == ':') | |
801 c = ':'; | |
802 else | |
803 c = '?'; | |
804 return c; | |
805 } | |
806 else | |
807 /* We already incremented `optind' once; | |
808 increment it again when taking next ARGV-elt as argument. */ | |
809 optarg = argv[optind++]; | |
810 | |
811 /* optarg is now the argument, see if it's in the | |
812 table of longopts. */ | |
813 | |
814 for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) | |
815 /* Do nothing. */ ; | |
816 | |
817 /* Test all long options for either exact match | |
818 or abbreviated matches. */ | |
819 for (p = longopts, option_index = 0; p->name; p++, option_index++) | |
820 if (!strncmp (p->name, nextchar, nameend - nextchar)) | |
821 { | |
822 if ((unsigned int) (nameend - nextchar) == strlen (p->name)) | |
823 { | |
824 /* Exact match found. */ | |
825 pfound = p; | |
826 indfound = option_index; | |
827 exact = 1; | |
828 break; | |
829 } | |
830 else if (pfound == NULL) | |
831 { | |
832 /* First nonexact match found. */ | |
833 pfound = p; | |
834 indfound = option_index; | |
835 } | |
836 else | |
837 /* Second or later nonexact match found. */ | |
838 ambig = 1; | |
839 } | |
840 if (ambig && !exact) | |
841 { | |
842 if (opterr) | |
843 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), | |
844 argv[0], argv[optind]); | |
845 nextchar += strlen (nextchar); | |
846 optind++; | |
847 return '?'; | |
848 } | |
849 if (pfound != NULL) | |
850 { | |
851 option_index = indfound; | |
852 if (*nameend) | |
853 { | |
854 /* Don't test has_arg with >, because some C compilers don't | |
855 allow it to be used on enums. */ | |
856 if (pfound->has_arg) | |
857 optarg = nameend + 1; | |
858 else | |
859 { | |
860 if (opterr) | |
861 fprintf (stderr, _("\ | |
862 %s: option `-W %s' doesn't allow an argument\n"), | |
863 argv[0], pfound->name); | |
864 | |
865 nextchar += strlen (nextchar); | |
866 return '?'; | |
867 } | |
868 } | |
869 else if (pfound->has_arg == 1) | |
870 { | |
871 if (optind < argc) | |
872 optarg = argv[optind++]; | |
873 else | |
874 { | |
875 if (opterr) | |
876 fprintf (stderr, | |
877 _("%s: option `%s' requires an argument\n"), | |
878 argv[0], argv[optind - 1]); | |
879 nextchar += strlen (nextchar); | |
880 return optstring[0] == ':' ? ':' : '?'; | |
881 } | |
882 } | |
883 nextchar += strlen (nextchar); | |
884 if (longind != NULL) | |
885 *longind = option_index; | |
886 if (pfound->flag) | |
887 { | |
888 *(pfound->flag) = pfound->val; | |
889 return 0; | |
890 } | |
891 return pfound->val; | |
892 } | |
893 nextchar = NULL; | |
894 return 'W'; /* Let the application handle it. */ | |
895 } | |
896 if (temp[1] == ':') | |
897 { | |
898 if (temp[2] == ':') | |
899 { | |
900 /* This is an option that accepts an argument optionally. */ | |
901 if (*nextchar != '\0') | |
902 { | |
903 optarg = nextchar; | |
904 optind++; | |
905 } | |
906 else | |
907 optarg = NULL; | |
908 nextchar = NULL; | |
909 } | |
910 else | |
911 { | |
912 /* This is an option that requires an argument. */ | |
913 if (*nextchar != '\0') | |
914 { | |
915 optarg = nextchar; | |
916 /* If we end this ARGV-element by taking the rest as an arg, | |
917 we must advance to the next element now. */ | |
918 optind++; | |
919 } | |
920 else if (optind == argc) | |
921 { | |
922 if (opterr) | |
923 { | |
924 /* 1003.2 specifies the format of this message. */ | |
925 fprintf (stderr, | |
926 _("%s: option requires an argument -- %c\n"), | |
927 argv[0], c); | |
928 } | |
929 optopt = c; | |
930 if (optstring[0] == ':') | |
931 c = ':'; | |
932 else | |
933 c = '?'; | |
934 } | |
935 else | |
936 /* We already incremented `optind' once; | |
937 increment it again when taking next ARGV-elt as argument. */ | |
938 optarg = argv[optind++]; | |
939 nextchar = NULL; | |
940 } | |
941 } | |
942 return c; | |
943 } | |
944 } | |
945 | |
946 int | |
947 getopt (int argc, char *const *argv, const char *optstring) | |
948 { | |
949 return _getopt_internal (argc, argv, optstring, | |
950 (const struct option *) 0, | |
951 (int *) 0, | |
952 0); | |
953 } | |
954 | |
955 #endif /* Not ELIDE_CODE. */ | |
956 | |
957 #ifdef TEST | |
958 | |
959 /* Compile with -DTEST to make an executable for use in testing | |
960 the above definition of `getopt'. */ | |
961 | |
962 int | |
963 main (int argc, char **argv) | |
964 { | |
965 int c; | |
966 int digit_optind = 0; | |
967 | |
968 while (1) | |
969 { | |
970 int this_option_optind = optind ? optind : 1; | |
971 | |
972 c = getopt (argc, argv, "abc:d:0123456789"); | |
973 if (c == -1) | |
974 break; | |
975 | |
976 switch (c) | |
977 { | |
978 case '0': | |
979 case '1': | |
980 case '2': | |
981 case '3': | |
982 case '4': | |
983 case '5': | |
984 case '6': | |
985 case '7': | |
986 case '8': | |
987 case '9': | |
988 if (digit_optind != 0 && digit_optind != this_option_optind) | |
989 printf ("digits occur in two different argv-elements.\n"); | |
990 digit_optind = this_option_optind; | |
991 printf ("option %c\n", c); | |
992 break; | |
993 | |
994 case 'a': | |
995 printf ("option a\n"); | |
996 break; | |
997 | |
998 case 'b': | |
999 printf ("option b\n"); | |
1000 break; | |
1001 | |
1002 case 'c': | |
1003 printf ("option c with value `%s'\n", optarg); | |
1004 break; | |
1005 | |
1006 case '?': | |
1007 break; | |
1008 | |
1009 default: | |
1010 printf ("?? getopt returned character code 0%o ??\n", c); | |
1011 } | |
1012 } | |
1013 | |
1014 if (optind < argc) | |
1015 { | |
1016 printf ("non-option ARGV-elements: "); | |
1017 while (optind < argc) | |
1018 printf ("%s ", argv[optind++]); | |
1019 printf ("\n"); | |
1020 } | |
1021 | |
1022 exit (0); | |
1023 } | |
1024 | |
1025 #endif /* TEST */ |