1*a9fa9459Szrj /* getopt_long and getopt_long_only entry points for GNU getopt.
2*a9fa9459Szrj Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98,2005
3*a9fa9459Szrj Free Software Foundation, Inc.
4*a9fa9459Szrj
5*a9fa9459Szrj NOTE: This source is derived from an old version taken from the GNU C
6*a9fa9459Szrj Library (glibc).
7*a9fa9459Szrj
8*a9fa9459Szrj This program is free software; you can redistribute it and/or modify it
9*a9fa9459Szrj under the terms of the GNU General Public License as published by the
10*a9fa9459Szrj Free Software Foundation; either version 2, or (at your option) any
11*a9fa9459Szrj later version.
12*a9fa9459Szrj
13*a9fa9459Szrj This program is distributed in the hope that it will be useful,
14*a9fa9459Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
15*a9fa9459Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*a9fa9459Szrj GNU General Public License for more details.
17*a9fa9459Szrj
18*a9fa9459Szrj You should have received a copy of the GNU General Public License
19*a9fa9459Szrj along with this program; if not, write to the Free Software
20*a9fa9459Szrj Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
21*a9fa9459Szrj USA. */
22*a9fa9459Szrj
23*a9fa9459Szrj #ifdef HAVE_CONFIG_H
24*a9fa9459Szrj #include <config.h>
25*a9fa9459Szrj #endif
26*a9fa9459Szrj
27*a9fa9459Szrj #if !defined __STDC__ || !__STDC__
28*a9fa9459Szrj /* This is a separate conditional since some stdc systems
29*a9fa9459Szrj reject `defined (const)'. */
30*a9fa9459Szrj #ifndef const
31*a9fa9459Szrj #define const
32*a9fa9459Szrj #endif
33*a9fa9459Szrj #endif
34*a9fa9459Szrj
35*a9fa9459Szrj #include <stdio.h>
36*a9fa9459Szrj
37*a9fa9459Szrj #include "getopt.h"
38*a9fa9459Szrj
39*a9fa9459Szrj /* Comment out all this code if we are using the GNU C Library, and are not
40*a9fa9459Szrj actually compiling the library itself. This code is part of the GNU C
41*a9fa9459Szrj Library, but also included in many other GNU distributions. Compiling
42*a9fa9459Szrj and linking in this code is a waste when using the GNU C library
43*a9fa9459Szrj (especially if it is a shared library). Rather than having every GNU
44*a9fa9459Szrj program understand `configure --with-gnu-libc' and omit the object files,
45*a9fa9459Szrj it is simpler to just do this in the source for each such file. */
46*a9fa9459Szrj
47*a9fa9459Szrj #define GETOPT_INTERFACE_VERSION 2
48*a9fa9459Szrj #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
49*a9fa9459Szrj #include <gnu-versions.h>
50*a9fa9459Szrj #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
51*a9fa9459Szrj #define ELIDE_CODE
52*a9fa9459Szrj #endif
53*a9fa9459Szrj #endif
54*a9fa9459Szrj
55*a9fa9459Szrj #ifndef ELIDE_CODE
56*a9fa9459Szrj
57*a9fa9459Szrj
58*a9fa9459Szrj /* This needs to come after some library #include
59*a9fa9459Szrj to get __GNU_LIBRARY__ defined. */
60*a9fa9459Szrj #ifdef __GNU_LIBRARY__
61*a9fa9459Szrj #include <stdlib.h>
62*a9fa9459Szrj #endif
63*a9fa9459Szrj
64*a9fa9459Szrj #ifndef NULL
65*a9fa9459Szrj #define NULL 0
66*a9fa9459Szrj #endif
67*a9fa9459Szrj
68*a9fa9459Szrj int
getopt_long(int argc,char * const * argv,const char * options,const struct option * long_options,int * opt_index)69*a9fa9459Szrj getopt_long (int argc, char *const *argv, const char *options,
70*a9fa9459Szrj const struct option *long_options, int *opt_index)
71*a9fa9459Szrj {
72*a9fa9459Szrj return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
73*a9fa9459Szrj }
74*a9fa9459Szrj
75*a9fa9459Szrj /* Like getopt_long, but '-' as well as '--' can indicate a long option.
76*a9fa9459Szrj If an option that starts with '-' (not '--') doesn't match a long option,
77*a9fa9459Szrj but does match a short option, it is parsed as a short option
78*a9fa9459Szrj instead. */
79*a9fa9459Szrj
80*a9fa9459Szrj int
getopt_long_only(int argc,char * const * argv,const char * options,const struct option * long_options,int * opt_index)81*a9fa9459Szrj getopt_long_only (int argc, char *const *argv, const char *options,
82*a9fa9459Szrj const struct option *long_options, int *opt_index)
83*a9fa9459Szrj {
84*a9fa9459Szrj return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
85*a9fa9459Szrj }
86*a9fa9459Szrj
87*a9fa9459Szrj
88*a9fa9459Szrj #endif /* Not ELIDE_CODE. */
89*a9fa9459Szrj
90*a9fa9459Szrj #ifdef TEST
91*a9fa9459Szrj
92*a9fa9459Szrj #include <stdio.h>
93*a9fa9459Szrj
94*a9fa9459Szrj int
main(int argc,char ** argv)95*a9fa9459Szrj main (int argc, char **argv)
96*a9fa9459Szrj {
97*a9fa9459Szrj int c;
98*a9fa9459Szrj int digit_optind = 0;
99*a9fa9459Szrj
100*a9fa9459Szrj while (1)
101*a9fa9459Szrj {
102*a9fa9459Szrj int this_option_optind = optind ? optind : 1;
103*a9fa9459Szrj int option_index = 0;
104*a9fa9459Szrj static struct option long_options[] =
105*a9fa9459Szrj {
106*a9fa9459Szrj {"add", 1, 0, 0},
107*a9fa9459Szrj {"append", 0, 0, 0},
108*a9fa9459Szrj {"delete", 1, 0, 0},
109*a9fa9459Szrj {"verbose", 0, 0, 0},
110*a9fa9459Szrj {"create", 0, 0, 0},
111*a9fa9459Szrj {"file", 1, 0, 0},
112*a9fa9459Szrj {0, 0, 0, 0}
113*a9fa9459Szrj };
114*a9fa9459Szrj
115*a9fa9459Szrj c = getopt_long (argc, argv, "abc:d:0123456789",
116*a9fa9459Szrj long_options, &option_index);
117*a9fa9459Szrj if (c == -1)
118*a9fa9459Szrj break;
119*a9fa9459Szrj
120*a9fa9459Szrj switch (c)
121*a9fa9459Szrj {
122*a9fa9459Szrj case 0:
123*a9fa9459Szrj printf ("option %s", long_options[option_index].name);
124*a9fa9459Szrj if (optarg)
125*a9fa9459Szrj printf (" with arg %s", optarg);
126*a9fa9459Szrj printf ("\n");
127*a9fa9459Szrj break;
128*a9fa9459Szrj
129*a9fa9459Szrj case '0':
130*a9fa9459Szrj case '1':
131*a9fa9459Szrj case '2':
132*a9fa9459Szrj case '3':
133*a9fa9459Szrj case '4':
134*a9fa9459Szrj case '5':
135*a9fa9459Szrj case '6':
136*a9fa9459Szrj case '7':
137*a9fa9459Szrj case '8':
138*a9fa9459Szrj case '9':
139*a9fa9459Szrj if (digit_optind != 0 && digit_optind != this_option_optind)
140*a9fa9459Szrj printf ("digits occur in two different argv-elements.\n");
141*a9fa9459Szrj digit_optind = this_option_optind;
142*a9fa9459Szrj printf ("option %c\n", c);
143*a9fa9459Szrj break;
144*a9fa9459Szrj
145*a9fa9459Szrj case 'a':
146*a9fa9459Szrj printf ("option a\n");
147*a9fa9459Szrj break;
148*a9fa9459Szrj
149*a9fa9459Szrj case 'b':
150*a9fa9459Szrj printf ("option b\n");
151*a9fa9459Szrj break;
152*a9fa9459Szrj
153*a9fa9459Szrj case 'c':
154*a9fa9459Szrj printf ("option c with value `%s'\n", optarg);
155*a9fa9459Szrj break;
156*a9fa9459Szrj
157*a9fa9459Szrj case 'd':
158*a9fa9459Szrj printf ("option d with value `%s'\n", optarg);
159*a9fa9459Szrj break;
160*a9fa9459Szrj
161*a9fa9459Szrj case '?':
162*a9fa9459Szrj break;
163*a9fa9459Szrj
164*a9fa9459Szrj default:
165*a9fa9459Szrj printf ("?? getopt returned character code 0%o ??\n", c);
166*a9fa9459Szrj }
167*a9fa9459Szrj }
168*a9fa9459Szrj
169*a9fa9459Szrj if (optind < argc)
170*a9fa9459Szrj {
171*a9fa9459Szrj printf ("non-option ARGV-elements: ");
172*a9fa9459Szrj while (optind < argc)
173*a9fa9459Szrj printf ("%s ", argv[optind++]);
174*a9fa9459Szrj printf ("\n");
175*a9fa9459Szrj }
176*a9fa9459Szrj
177*a9fa9459Szrj exit (0);
178*a9fa9459Szrj }
179*a9fa9459Szrj
180*a9fa9459Szrj #endif /* TEST */
181