xref: /openbsd/gnu/usr.bin/texinfo/lib/getopt1.c (revision a1acfa9b)
1840175f0Skstailey /* getopt_long and getopt_long_only entry points for GNU getopt.
2*a1acfa9bSespie    Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98,2004
31cc83814Sespie      Free Software Foundation, Inc.
4*a1acfa9bSespie    This file is part of the GNU C Library.
5840175f0Skstailey 
6*a1acfa9bSespie    This program is free software; you can redistribute it and/or modify
7*a1acfa9bSespie    it under the terms of the GNU General Public License as published by
8*a1acfa9bSespie    the Free Software Foundation; either version 2, or (at your option)
9*a1acfa9bSespie    any later version.
10840175f0Skstailey 
11840175f0Skstailey    This program is distributed in the hope that it will be useful,
12840175f0Skstailey    but WITHOUT ANY WARRANTY; without even the implied warranty of
13840175f0Skstailey    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14840175f0Skstailey    GNU General Public License for more details.
15840175f0Skstailey 
16*a1acfa9bSespie    You should have received a copy of the GNU General Public License along
17*a1acfa9bSespie    with this program; if not, write to the Free Software Foundation,
183fb98d4aSespie    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19840175f0Skstailey 
20840175f0Skstailey #ifdef HAVE_CONFIG_H
21840175f0Skstailey #include <config.h>
223fb98d4aSespie #endif
233fb98d4aSespie 
24*a1acfa9bSespie #ifdef _LIBC
25*a1acfa9bSespie # include <getopt.h>
26*a1acfa9bSespie #else
273fb98d4aSespie # include "getopt.h"
28*a1acfa9bSespie #endif
29*a1acfa9bSespie #include "getopt_int.h"
30840175f0Skstailey 
31840175f0Skstailey #include <stdio.h>
32840175f0Skstailey 
33840175f0Skstailey /* This needs to come after some library #include
34840175f0Skstailey    to get __GNU_LIBRARY__ defined.  */
35840175f0Skstailey #ifdef __GNU_LIBRARY__
36840175f0Skstailey #include <stdlib.h>
37840175f0Skstailey #endif
38840175f0Skstailey 
39840175f0Skstailey #ifndef	NULL
40840175f0Skstailey #define NULL 0
41840175f0Skstailey #endif
42840175f0Skstailey 
43840175f0Skstailey int
getopt_long(int argc,char * const * argv,const char * options,const struct option * long_options,int * opt_index)44*a1acfa9bSespie getopt_long (int argc, char *const *argv, const char *options,
45*a1acfa9bSespie 	     const struct option *long_options, int *opt_index)
46840175f0Skstailey {
47840175f0Skstailey   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
48840175f0Skstailey }
49840175f0Skstailey 
50*a1acfa9bSespie int
_getopt_long_r(int argc,char * const * argv,const char * options,const struct option * long_options,int * opt_index,struct _getopt_data * d)51*a1acfa9bSespie _getopt_long_r (int argc, char *const *argv, const char *options,
52*a1acfa9bSespie 		const struct option *long_options, int *opt_index,
53*a1acfa9bSespie 		struct _getopt_data *d)
54*a1acfa9bSespie {
55*a1acfa9bSespie   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
56*a1acfa9bSespie 			     0, d);
57*a1acfa9bSespie }
58*a1acfa9bSespie 
59840175f0Skstailey /* Like getopt_long, but '-' as well as '--' can indicate a long option.
60840175f0Skstailey    If an option that starts with '-' (not '--') doesn't match a long option,
61840175f0Skstailey    but does match a short option, it is parsed as a short option
62840175f0Skstailey    instead.  */
63840175f0Skstailey 
64840175f0Skstailey int
getopt_long_only(int argc,char * const * argv,const char * options,const struct option * long_options,int * opt_index)65*a1acfa9bSespie getopt_long_only (int argc, char *const *argv, const char *options,
66*a1acfa9bSespie 		  const struct option *long_options, int *opt_index)
67840175f0Skstailey {
68840175f0Skstailey   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
69840175f0Skstailey }
70840175f0Skstailey 
71*a1acfa9bSespie int
_getopt_long_only_r(int argc,char * const * argv,const char * options,const struct option * long_options,int * opt_index,struct _getopt_data * d)72*a1acfa9bSespie _getopt_long_only_r (int argc, char *const *argv, const char *options,
73*a1acfa9bSespie 		     const struct option *long_options, int *opt_index,
74*a1acfa9bSespie 		     struct _getopt_data *d)
75*a1acfa9bSespie {
76*a1acfa9bSespie   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
77*a1acfa9bSespie 			     1, d);
78*a1acfa9bSespie }
79840175f0Skstailey 
80840175f0Skstailey 
81840175f0Skstailey #ifdef TEST
82840175f0Skstailey 
83840175f0Skstailey #include <stdio.h>
84840175f0Skstailey 
85840175f0Skstailey int
main(int argc,char ** argv)86*a1acfa9bSespie main (int argc, char **argv)
87840175f0Skstailey {
88840175f0Skstailey   int c;
89840175f0Skstailey   int digit_optind = 0;
90840175f0Skstailey 
91840175f0Skstailey   while (1)
92840175f0Skstailey     {
93840175f0Skstailey       int this_option_optind = optind ? optind : 1;
94840175f0Skstailey       int option_index = 0;
95840175f0Skstailey       static struct option long_options[] =
96840175f0Skstailey       {
97840175f0Skstailey 	{"add", 1, 0, 0},
98840175f0Skstailey 	{"append", 0, 0, 0},
99840175f0Skstailey 	{"delete", 1, 0, 0},
100840175f0Skstailey 	{"verbose", 0, 0, 0},
101840175f0Skstailey 	{"create", 0, 0, 0},
102840175f0Skstailey 	{"file", 1, 0, 0},
103840175f0Skstailey 	{0, 0, 0, 0}
104840175f0Skstailey       };
105840175f0Skstailey 
106840175f0Skstailey       c = getopt_long (argc, argv, "abc:d:0123456789",
107840175f0Skstailey 		       long_options, &option_index);
108840175f0Skstailey       if (c == -1)
109840175f0Skstailey 	break;
110840175f0Skstailey 
111840175f0Skstailey       switch (c)
112840175f0Skstailey 	{
113840175f0Skstailey 	case 0:
114840175f0Skstailey 	  printf ("option %s", long_options[option_index].name);
115840175f0Skstailey 	  if (optarg)
116840175f0Skstailey 	    printf (" with arg %s", optarg);
117840175f0Skstailey 	  printf ("\n");
118840175f0Skstailey 	  break;
119840175f0Skstailey 
120840175f0Skstailey 	case '0':
121840175f0Skstailey 	case '1':
122840175f0Skstailey 	case '2':
123840175f0Skstailey 	case '3':
124840175f0Skstailey 	case '4':
125840175f0Skstailey 	case '5':
126840175f0Skstailey 	case '6':
127840175f0Skstailey 	case '7':
128840175f0Skstailey 	case '8':
129840175f0Skstailey 	case '9':
130840175f0Skstailey 	  if (digit_optind != 0 && digit_optind != this_option_optind)
131840175f0Skstailey 	    printf ("digits occur in two different argv-elements.\n");
132840175f0Skstailey 	  digit_optind = this_option_optind;
133840175f0Skstailey 	  printf ("option %c\n", c);
134840175f0Skstailey 	  break;
135840175f0Skstailey 
136840175f0Skstailey 	case 'a':
137840175f0Skstailey 	  printf ("option a\n");
138840175f0Skstailey 	  break;
139840175f0Skstailey 
140840175f0Skstailey 	case 'b':
141840175f0Skstailey 	  printf ("option b\n");
142840175f0Skstailey 	  break;
143840175f0Skstailey 
144840175f0Skstailey 	case 'c':
145840175f0Skstailey 	  printf ("option c with value `%s'\n", optarg);
146840175f0Skstailey 	  break;
147840175f0Skstailey 
148840175f0Skstailey 	case 'd':
149840175f0Skstailey 	  printf ("option d with value `%s'\n", optarg);
150840175f0Skstailey 	  break;
151840175f0Skstailey 
152840175f0Skstailey 	case '?':
153840175f0Skstailey 	  break;
154840175f0Skstailey 
155840175f0Skstailey 	default:
156840175f0Skstailey 	  printf ("?? getopt returned character code 0%o ??\n", c);
157840175f0Skstailey 	}
158840175f0Skstailey     }
159840175f0Skstailey 
160840175f0Skstailey   if (optind < argc)
161840175f0Skstailey     {
162840175f0Skstailey       printf ("non-option ARGV-elements: ");
163840175f0Skstailey       while (optind < argc)
164840175f0Skstailey 	printf ("%s ", argv[optind++]);
165840175f0Skstailey       printf ("\n");
166840175f0Skstailey     }
167840175f0Skstailey 
168840175f0Skstailey   exit (0);
169840175f0Skstailey }
170840175f0Skstailey 
171840175f0Skstailey #endif /* TEST */
172