1
/* $Id: getopt.c 3550 2011-05-05 05:33:27Z nanang $ */
3
* pj_getopt entry points
5
* modified by Mike Borella <mike_borella@mw.3com.com>
8
#include <pjlib-util/getopt.h>
11
/* Internal only. Users should not call this directly. */
13
int _getopt_internal (int argc, char *const *argv,
14
const char *shortopts,
15
const struct pj_getopt_option *longopts, int *longind,
18
/* pj_getopt_long and pj_getopt_long_only entry points for GNU pj_getopt.
19
Copyright (C) 1987,88,89,90,91,92,93,94,96,97 Free Software Foundation, Inc.
20
This file is part of the GNU C Library.
22
The GNU C Library is free software; you can redistribute it and/or
23
modify it under the terms of the GNU Library General Public License as
24
published by the Free Software Foundation; either version 2 of the
25
License, or (at your option) any later version.
27
The GNU C Library is distributed in the hope that it will be useful,
28
but WITHOUT ANY WARRANTY; without even the implied warranty of
29
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30
Library General Public License for more details.
32
You should have received a copy of the GNU Library General Public
33
License along with the GNU C Library; see the file COPYING.LIB. If not,
34
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
35
Boston, MA 02111-1307, USA. */
38
/* Comment out all this code if we are using the GNU C Library, and are not
39
actually compiling the library itself. This code is part of the GNU C
40
Library, but also included in many other GNU distributions. Compiling
41
and linking in this code is a waste when using the GNU C library
42
(especially if it is a shared library). Rather than having every GNU
43
program understand `configure --with-gnu-libc' and omit the object files,
44
it is simpler to just do this in the source for each such file. */
46
# define GETOPT_INTERFACE_VERSION 2
50
pj_getopt_long (int argc, char *const *argv, const char *options,
51
const struct pj_getopt_option *long_options, int *opt_index)
53
return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
56
/* Like pj_getopt_long, but '-' as well as '--' can indicate a long option.
57
If an option that starts with '-' (not '--') doesn't match a long option,
58
but does match a short option, it is parsed as a short option
62
pj_getopt (int argc, char * const * argv, const char * optstring)
64
return _getopt_internal (argc, argv, optstring,
65
(const struct pj_getopt_option *) 0,
71
#define _(msgid) (msgid)
73
/* This version of `pj_getopt' appears to the caller like standard Unix `pj_getopt'
74
but it behaves differently for the user, since it allows the user
75
to intersperse the options with the other arguments.
77
As `pj_getopt' works, it permutes the elements of ARGV so that,
78
when it is done, all the options precede everything else. Thus
79
all application programs are extended to handle flexible argument order.
81
Setting the environment variable POSIXLY_CORRECT disables permutation.
82
Then the behavior is completely standard.
84
GNU application programs can use a third alternative mode in which
85
they can distinguish the relative order of options and other arguments. */
87
/* For communication from `pj_getopt' to the caller.
88
When `pj_getopt' finds an option that takes an argument,
89
the argument value is returned here.
90
Also, when `ordering' is RETURN_IN_ORDER,
91
each non-option ARGV-element is returned here. */
93
char *pj_optarg = NULL;
95
/* Index in ARGV of the next element to be scanned.
96
This is used for communication to and from the caller
97
and for communication between successive calls to `pj_getopt'.
99
On entry to `pj_getopt', zero means this is the first call; initialize.
101
When `pj_getopt' returns -1, this is the index of the first of the
102
non-option elements that the caller should itself scan.
104
Otherwise, `pj_optind' communicates from one call to the next
105
how much of ARGV has been scanned so far. */
107
/* 1003.2 says this must be 1 before any call. */
110
/* Formerly, initialization of pj_getopt depended on pj_optind==0, which
111
causes problems with re-calling pj_getopt as programs generally don't
114
int __getopt_initialized = 0;
116
/* The next char to be scanned in the option-element
117
in which the last option character we returned was found.
118
This allows us to pick up the scan where we left off.
120
If this is zero, or a null string, it means resume the scan
121
by advancing to the next ARGV-element. */
123
static char *nextchar;
125
/* Set to an option character which was unrecognized.
126
This must be initialized on some systems to avoid linking in the
127
system's own pj_getopt implementation. */
131
/* Describe how to deal with options that follow non-option ARGV-elements.
133
If the caller did not specify anything,
134
the default is REQUIRE_ORDER if the environment variable
135
POSIXLY_CORRECT is defined, PERMUTE otherwise.
137
REQUIRE_ORDER means don't recognize them as options;
138
stop option processing when the first non-option is seen.
139
This is what Unix does.
140
This mode of operation is selected by either setting the environment
141
variable POSIXLY_CORRECT, or using `+' as the first character
142
of the list of option characters.
144
PERMUTE is the default. We permute the contents of ARGV as we scan,
145
so that eventually all the non-options are at the end. This allows options
146
to be given in any order, even with programs that were not written to
149
RETURN_IN_ORDER is an option available to programs that were written
150
to expect options and other ARGV-elements in any order and that care about
151
the ordering of the two. We describe each non-option ARGV-element
152
as if it were the argument of an option with character code 1.
153
Using `-' as the first character of the list of option characters
154
selects this mode of operation.
156
The special argument `--' forces an end of option-scanning regardless
157
of the value of `ordering'. In the case of RETURN_IN_ORDER, only
158
`--' can cause `pj_getopt' to return -1 with `pj_optind' != ARGC. */
162
REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
165
/* Value of POSIXLY_CORRECT environment variable. */
166
static char *posixly_correct;
169
my_index (const char *str, int chr)
181
/* Handle permutation of arguments. */
183
/* Describe the part of ARGV that contains non-options that have
184
been skipped. `first_nonopt' is the index in ARGV of the first of them;
185
`last_nonopt' is the index after the last of them. */
187
static int first_nonopt;
188
static int last_nonopt;
190
# define SWAP_FLAGS(ch1, ch2)
192
/* Exchange two adjacent subsequences of ARGV.
193
One subsequence is elements [first_nonopt,last_nonopt)
194
which contains all the non-options that have been skipped so far.
195
The other is elements [last_nonopt,pj_optind), which contains all
196
the options processed since those non-options were skipped.
198
`first_nonopt' and `last_nonopt' are relocated so that they describe
199
the new indices of the non-options in ARGV after they are moved. */
202
exchange (char **argv)
204
int bottom = first_nonopt;
205
int middle = last_nonopt;
209
/* Exchange the shorter segment with the far end of the longer segment.
210
That puts the shorter segment into the right place.
211
It leaves the longer segment in the right place overall,
212
but it consists of two parts that need to be swapped next. */
214
while (top > middle && middle > bottom)
216
if (top - middle > middle - bottom)
218
/* Bottom segment is the short one. */
219
int len = middle - bottom;
222
/* Swap it with the top part of the top segment. */
223
for (i = 0; i < len; i++)
225
tem = argv[bottom + i];
226
argv[bottom + i] = argv[top - (middle - bottom) + i];
227
argv[top - (middle - bottom) + i] = tem;
228
SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
230
/* Exclude the moved bottom segment from further swapping. */
235
/* Top segment is the short one. */
236
int len = top - middle;
239
/* Swap it with the bottom part of the bottom segment. */
240
for (i = 0; i < len; i++)
242
tem = argv[bottom + i];
243
argv[bottom + i] = argv[middle + i];
244
argv[middle + i] = tem;
245
SWAP_FLAGS (bottom + i, middle + i);
247
/* Exclude the moved top segment from further swapping. */
252
/* Update records for the slots the non-options now occupy. */
254
first_nonopt += (pj_optind - last_nonopt);
255
last_nonopt = pj_optind;
258
/* Initialize the internal data when the first call is made. */
260
static const char *_getopt_initialize (int argc, char *const *argv,
261
const char *optstring)
266
/* Start processing options with ARGV-element 1 (since ARGV-element 0
267
is the program name); the sequence of previously skipped
268
non-option ARGV-elements is empty. */
270
first_nonopt = last_nonopt = pj_optind;
274
//posixly_correct = getenv ("POSIXLY_CORRECT");
275
posixly_correct = NULL;
277
/* Determine how to handle the ordering of options and nonoptions. */
279
if (optstring[0] == '-')
281
ordering = RETURN_IN_ORDER;
284
else if (optstring[0] == '+')
286
ordering = REQUIRE_ORDER;
289
else if (posixly_correct != NULL)
290
ordering = REQUIRE_ORDER;
297
/* Scan elements of ARGV (whose length is ARGC) for option characters
300
If an element of ARGV starts with '-', and is not exactly "-" or "--",
301
then it is an option element. The characters of this element
302
(aside from the initial '-') are option characters. If `pj_getopt'
303
is called repeatedly, it returns successively each of the option characters
304
from each of the option elements.
306
If `pj_getopt' finds another option character, it returns that character,
307
updating `pj_optind' and `nextchar' so that the next call to `pj_getopt' can
308
resume the scan with the following option character or ARGV-element.
310
If there are no more option characters, `pj_getopt' returns -1.
311
Then `pj_optind' is the index in ARGV of the first ARGV-element
312
that is not an option. (The ARGV-elements have been permuted
313
so that those that are not options now come last.)
315
OPTSTRING is a string containing the legitimate option characters.
316
If an option character is seen that is not listed in OPTSTRING,
317
return '?' after printing an error message. If you set `pj_opterr' to
318
zero, the error message is suppressed but we still return '?'.
320
If a char in OPTSTRING is followed by a colon, that means it wants an arg,
321
so the following text in the same ARGV-element, or the text of the following
322
ARGV-element, is returned in `pj_optarg'. Two colons mean an option that
323
wants an optional arg; if there is text in the current ARGV-element,
324
it is returned in `pj_optarg', otherwise `pj_optarg' is set to zero.
326
If OPTSTRING starts with `-' or `+', it requests different methods of
327
handling the non-option ARGV-elements.
328
See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
330
Long-named options begin with `--' instead of `-'.
331
Their names may be abbreviated as long as the abbreviation is unique
332
or is an exact match for some defined option. If they have an
333
argument, it follows the option name in the same ARGV-element, separated
334
from the option name by a `=', or else the in next ARGV-element.
335
When `pj_getopt' finds a long-named option, it returns 0 if that option's
336
`flag' field is nonzero, the value of the option's `val' field
337
if the `flag' field is zero.
339
The elements of ARGV aren't really const, because we permute them.
340
But we pretend they're const in the prototype to be compatible
343
LONGOPTS is a vector of `struct pj_getopt_option' terminated by an
344
element containing a name which is zero.
346
LONGIND returns the index in LONGOPT of the long-named option found.
347
It is only valid when a long-named option has been found by the most
350
If LONG_ONLY is nonzero, '-' as well as '--' can introduce
351
long-named options. */
354
_getopt_internal (int argc, char *const *argv, const char *optstring,
355
const struct pj_getopt_option *longopts, int *longind,
360
if (pj_optind == 0 || !__getopt_initialized)
363
pj_optind = 1; /* Don't scan ARGV[0], the program name. */
364
optstring = _getopt_initialize (argc, argv, optstring);
365
__getopt_initialized = 1;
368
/* Test whether ARGV[pj_optind] points to a non-option argument.
369
Either it does not have option syntax, or there is an environment flag
370
from the shell indicating it is not an option. The later information
371
is only used when the used in the GNU libc. */
372
#define NONOPTION_P (argv[pj_optind][0] != '-' || argv[pj_optind][1] == '\0')
374
if (nextchar == NULL || *nextchar == '\0')
376
/* Advance to the next ARGV-element. */
378
/* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
379
moved back by the user (who may also have changed the arguments). */
380
if (last_nonopt > pj_optind)
381
last_nonopt = pj_optind;
382
if (first_nonopt > pj_optind)
383
first_nonopt = pj_optind;
385
if (ordering == PERMUTE)
387
/* If we have just processed some options following some non-options,
388
exchange them so that the options come first. */
390
if (first_nonopt != last_nonopt && last_nonopt != pj_optind)
391
exchange ((char **) argv);
392
else if (last_nonopt != pj_optind)
393
first_nonopt = pj_optind;
395
/* Skip any additional non-options
396
and extend the range of non-options previously skipped. */
398
while (pj_optind < argc && NONOPTION_P)
400
last_nonopt = pj_optind;
403
/* The special ARGV-element `--' means premature end of options.
404
Skip it like a null option,
405
then exchange with previous non-options as if it were an option,
406
then skip everything else like a non-option. */
408
if (pj_optind != argc && !pj_ansi_strcmp(argv[pj_optind], "--"))
412
if (first_nonopt != last_nonopt && last_nonopt != pj_optind)
413
exchange ((char **) argv);
414
else if (first_nonopt == last_nonopt)
415
first_nonopt = pj_optind;
421
/* If we have done all the ARGV-elements, stop the scan
422
and back over any non-options that we skipped and permuted. */
424
if (pj_optind == argc)
426
/* Set the next-arg-index to point at the non-options
427
that we previously skipped, so the caller will digest them. */
428
if (first_nonopt != last_nonopt)
429
pj_optind = first_nonopt;
433
/* If we have come to a non-option and did not permute it,
434
either stop the scan or describe it to the caller and pass it by. */
438
if (ordering == REQUIRE_ORDER)
440
pj_optarg = argv[pj_optind++];
444
/* We have found another option-ARGV-element.
445
Skip the initial punctuation. */
447
nextchar = (argv[pj_optind] + 1
448
+ (longopts != NULL && argv[pj_optind][1] == '-'));
451
/* Decode the current option-ARGV-element. */
453
/* Check whether the ARGV-element is a long option.
455
If long_only and the ARGV-element has the form "-f", where f is
456
a valid short option, don't consider it an abbreviated form of
457
a long option that starts with f. Otherwise there would be no
458
way to give the -f short option.
460
On the other hand, if there's a long option "fubar" and
461
the ARGV-element is "-fu", do consider that an abbreviation of
462
the long option, just like "--fu", and not "-f" with arg "u".
464
This distinction seems to be the most useful approach. */
467
&& (argv[pj_optind][1] == '-'
468
|| (long_only && (argv[pj_optind][2] || !my_index (optstring, argv[pj_optind][1])))))
471
const struct pj_getopt_option *p;
472
const struct pj_getopt_option *pfound = NULL;
478
for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
481
/* Test all long options for either exact match
482
or abbreviated matches. */
483
for (p = longopts, option_index = 0; p->name; p++, option_index++)
484
if (!strncmp (p->name, nextchar, nameend - nextchar))
486
if ((unsigned int) (nameend - nextchar)
487
== (unsigned int) strlen (p->name))
489
/* Exact match found. */
491
indfound = option_index;
495
else if (pfound == NULL)
497
/* First nonexact match found. */
499
indfound = option_index;
502
/* Second or later nonexact match found. */
508
nextchar += strlen (nextchar);
516
option_index = indfound;
520
/* Don't test has_arg with >, because some C compilers don't
521
allow it to be used on enums. */
523
pj_optarg = nameend + 1;
526
nextchar += strlen (nextchar);
528
pj_optopt = pfound->val;
532
else if (pfound->has_arg == 1)
534
if (pj_optind < argc)
535
pj_optarg = argv[pj_optind++];
538
nextchar += strlen (nextchar);
539
pj_optopt = pfound->val;
540
return optstring[0] == ':' ? ':' : '?';
543
nextchar += strlen (nextchar);
545
*longind = option_index;
548
*(pfound->flag) = pfound->val;
554
/* Can't find it as a long option. If this is not pj_getopt_long_only,
555
or the option starts with '--' or is not a valid short
556
option, then it's an error.
557
Otherwise interpret it as a short option. */
558
if (!long_only || argv[pj_optind][1] == '-'
559
|| my_index (optstring, *nextchar) == NULL)
561
nextchar = (char *) "";
568
/* Look at and handle the next short option-character. */
571
char c = *nextchar++;
572
char *temp = my_index (optstring, c);
574
/* Increment `pj_optind' when we start to process its last character. */
575
if (*nextchar == '\0')
578
if (temp == NULL || c == ':')
583
/* Convenience. Treat POSIX -W foo same as long option --foo */
584
if (temp[0] == 'W' && temp[1] == ';')
587
const struct pj_getopt_option *p;
588
const struct pj_getopt_option *pfound = NULL;
594
/* This is an option that requires an argument. */
595
if (*nextchar != '\0')
597
pj_optarg = nextchar;
598
/* If we end this ARGV-element by taking the rest as an arg,
599
we must advance to the next element now. */
602
else if (pj_optind == argc)
605
if (optstring[0] == ':')
612
/* We already incremented `pj_optind' once;
613
increment it again when taking next ARGV-elt as argument. */
614
pj_optarg = argv[pj_optind++];
616
/* pj_optarg is now the argument, see if it's in the
617
table of longopts. */
619
for (nextchar = nameend = pj_optarg; *nameend && *nameend != '='; nameend++)
622
/* Test all long options for either exact match
623
or abbreviated matches. */
624
for (p = longopts, option_index = 0; p->name; p++, option_index++)
625
if (!strncmp (p->name, nextchar, nameend - nextchar))
627
if ((unsigned int) (nameend - nextchar) == strlen (p->name))
629
/* Exact match found. */
631
indfound = option_index;
635
else if (pfound == NULL)
637
/* First nonexact match found. */
639
indfound = option_index;
642
/* Second or later nonexact match found. */
647
nextchar += strlen (nextchar);
653
option_index = indfound;
656
/* Don't test has_arg with >, because some C compilers don't
657
allow it to be used on enums. */
659
pj_optarg = nameend + 1;
662
nextchar += strlen (nextchar);
666
else if (pfound->has_arg == 1)
668
if (pj_optind < argc)
669
pj_optarg = argv[pj_optind++];
672
nextchar += strlen (nextchar);
673
return optstring[0] == ':' ? ':' : '?';
676
nextchar += strlen (nextchar);
678
*longind = option_index;
681
*(pfound->flag) = pfound->val;
687
return 'W'; /* Let the application handle it. */
693
/* This is an option that accepts an argument optionally. */
694
if (*nextchar != '\0')
696
pj_optarg = nextchar;
705
/* This is an option that requires an argument. */
706
if (*nextchar != '\0')
708
pj_optarg = nextchar;
709
/* If we end this ARGV-element by taking the rest as an arg,
710
we must advance to the next element now. */
713
else if (pj_optind == argc)
716
if (optstring[0] == ':')
722
/* We already incremented `pj_optind' once;
723
increment it again when taking next ARGV-elt as argument. */
724
pj_optarg = argv[pj_optind++];