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