xref: /freebsd/contrib/libevent/WIN32-Code/getopt_long.c (revision b50261e21f39a6c7249a49e7b60aa878c98512a8)
1*b50261e2SCy Schubert 
2*b50261e2SCy Schubert /*
3*b50261e2SCy Schubert  * Copyright (c) 1987, 1993, 1994, 1996
4*b50261e2SCy Schubert  *	The Regents of the University of California.  All rights reserved.
5*b50261e2SCy Schubert  *
6*b50261e2SCy Schubert  * Redistribution and use in source and binary forms, with or without
7*b50261e2SCy Schubert  * modification, are permitted provided that the following conditions
8*b50261e2SCy Schubert  * are met:
9*b50261e2SCy Schubert  * 1. Redistributions of source code must retain the above copyright
10*b50261e2SCy Schubert  *    notice, this list of conditions and the following disclaimer.
11*b50261e2SCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
12*b50261e2SCy Schubert  *    notice, this list of conditions and the following disclaimer in the
13*b50261e2SCy Schubert  *    documentation and/or other materials provided with the distribution.
14*b50261e2SCy Schubert  * 3. Neither the names of the copyright holders nor the names of its
15*b50261e2SCy Schubert  *    contributors may be used to endorse or promote products derived from
16*b50261e2SCy Schubert  *    this software without specific prior written permission.
17*b50261e2SCy Schubert  *
18*b50261e2SCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
19*b50261e2SCy Schubert  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20*b50261e2SCy Schubert  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*b50261e2SCy Schubert  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
22*b50261e2SCy Schubert  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*b50261e2SCy Schubert  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*b50261e2SCy Schubert  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*b50261e2SCy Schubert  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*b50261e2SCy Schubert  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*b50261e2SCy Schubert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*b50261e2SCy Schubert  * POSSIBILITY OF SUCH DAMAGE.
29*b50261e2SCy Schubert  */
30*b50261e2SCy Schubert #include <assert.h>
31*b50261e2SCy Schubert #include <errno.h>
32*b50261e2SCy Schubert #include <stdio.h>
33*b50261e2SCy Schubert #include <stdlib.h>
34*b50261e2SCy Schubert #include <string.h>
35*b50261e2SCy Schubert #include "getopt.h"
36*b50261e2SCy Schubert 
37*b50261e2SCy Schubert extern int	  opterr;	/* if error message should be printed */
38*b50261e2SCy Schubert extern int	  optind;	/* index into parent argv vector */
39*b50261e2SCy Schubert extern int	  optopt;	/* character checked for validity */
40*b50261e2SCy Schubert extern int	  optreset;	/* reset getopt */
41*b50261e2SCy Schubert extern char *optarg;	/* argument associated with option */
42*b50261e2SCy Schubert 
43*b50261e2SCy Schubert #define __P(x) x
44*b50261e2SCy Schubert #define _DIAGASSERT(x) assert(x)
45*b50261e2SCy Schubert 
46*b50261e2SCy Schubert static char * __progname __P((char *));
47*b50261e2SCy Schubert int getopt_internal __P((int, char * const *, const char *));
48*b50261e2SCy Schubert 
49*b50261e2SCy Schubert static char *
__progname(nargv0)50*b50261e2SCy Schubert __progname(nargv0)
51*b50261e2SCy Schubert 	char * nargv0;
52*b50261e2SCy Schubert {
53*b50261e2SCy Schubert 	char * tmp;
54*b50261e2SCy Schubert 
55*b50261e2SCy Schubert 	_DIAGASSERT(nargv0 != NULL);
56*b50261e2SCy Schubert 
57*b50261e2SCy Schubert 	tmp = strrchr(nargv0, '/');
58*b50261e2SCy Schubert 	if (tmp)
59*b50261e2SCy Schubert 		tmp++;
60*b50261e2SCy Schubert 	else
61*b50261e2SCy Schubert 		tmp = nargv0;
62*b50261e2SCy Schubert 	return(tmp);
63*b50261e2SCy Schubert }
64*b50261e2SCy Schubert 
65*b50261e2SCy Schubert #define	BADCH	(int)'?'
66*b50261e2SCy Schubert #define	BADARG	(int)':'
67*b50261e2SCy Schubert #define	EMSG	""
68*b50261e2SCy Schubert 
69*b50261e2SCy Schubert /*
70*b50261e2SCy Schubert  * getopt --
71*b50261e2SCy Schubert  *	Parse argc/argv argument vector.
72*b50261e2SCy Schubert  */
73*b50261e2SCy Schubert int
getopt_internal(nargc,nargv,ostr)74*b50261e2SCy Schubert getopt_internal(nargc, nargv, ostr)
75*b50261e2SCy Schubert 	int nargc;
76*b50261e2SCy Schubert 	char * const *nargv;
77*b50261e2SCy Schubert 	const char *ostr;
78*b50261e2SCy Schubert {
79*b50261e2SCy Schubert 	static char *place = EMSG;		/* option letter processing */
80*b50261e2SCy Schubert 	char *oli;				/* option letter list index */
81*b50261e2SCy Schubert 
82*b50261e2SCy Schubert 	_DIAGASSERT(nargv != NULL);
83*b50261e2SCy Schubert 	_DIAGASSERT(ostr != NULL);
84*b50261e2SCy Schubert 
85*b50261e2SCy Schubert 	if (optreset || !*place) {		/* update scanning pointer */
86*b50261e2SCy Schubert 		optreset = 0;
87*b50261e2SCy Schubert 		if (optind >= nargc || *(place = nargv[optind]) != '-') {
88*b50261e2SCy Schubert 			place = EMSG;
89*b50261e2SCy Schubert 			return (-1);
90*b50261e2SCy Schubert 		}
91*b50261e2SCy Schubert 		if (place[1] && *++place == '-') {	/* found "--" */
92*b50261e2SCy Schubert 			/* ++optind; */
93*b50261e2SCy Schubert 			place = EMSG;
94*b50261e2SCy Schubert 			return (-2);
95*b50261e2SCy Schubert 		}
96*b50261e2SCy Schubert 	}					/* option letter okay? */
97*b50261e2SCy Schubert 	if ((optopt = (int)*place++) == (int)':' ||
98*b50261e2SCy Schubert 	    !(oli = strchr(ostr, optopt))) {
99*b50261e2SCy Schubert 		/*
100*b50261e2SCy Schubert 		 * if the user didn't specify '-' as an option,
101*b50261e2SCy Schubert 		 * assume it means -1.
102*b50261e2SCy Schubert 		 */
103*b50261e2SCy Schubert 		if (optopt == (int)'-')
104*b50261e2SCy Schubert 			return (-1);
105*b50261e2SCy Schubert 		if (!*place)
106*b50261e2SCy Schubert 			++optind;
107*b50261e2SCy Schubert 		if (opterr && *ostr != ':')
108*b50261e2SCy Schubert 			(void)fprintf(stderr,
109*b50261e2SCy Schubert 			    "%s: illegal option -- %c\n", __progname(nargv[0]), optopt);
110*b50261e2SCy Schubert 		return (BADCH);
111*b50261e2SCy Schubert 	}
112*b50261e2SCy Schubert 	if (*++oli != ':') {			/* don't need argument */
113*b50261e2SCy Schubert 		optarg = NULL;
114*b50261e2SCy Schubert 		if (!*place)
115*b50261e2SCy Schubert 			++optind;
116*b50261e2SCy Schubert 	} else {				/* need an argument */
117*b50261e2SCy Schubert 		if (*place)			/* no white space */
118*b50261e2SCy Schubert 			optarg = place;
119*b50261e2SCy Schubert 		else if (nargc <= ++optind) {	/* no arg */
120*b50261e2SCy Schubert 			place = EMSG;
121*b50261e2SCy Schubert 			if ((opterr) && (*ostr != ':'))
122*b50261e2SCy Schubert 				(void)fprintf(stderr,
123*b50261e2SCy Schubert 				    "%s: option requires an argument -- %c\n",
124*b50261e2SCy Schubert 				    __progname(nargv[0]), optopt);
125*b50261e2SCy Schubert 			return (BADARG);
126*b50261e2SCy Schubert 		} else				/* white space */
127*b50261e2SCy Schubert 			optarg = nargv[optind];
128*b50261e2SCy Schubert 		place = EMSG;
129*b50261e2SCy Schubert 		++optind;
130*b50261e2SCy Schubert 	}
131*b50261e2SCy Schubert 	return (optopt);			/* dump back option letter */
132*b50261e2SCy Schubert }
133*b50261e2SCy Schubert 
134*b50261e2SCy Schubert #if 0
135*b50261e2SCy Schubert /*
136*b50261e2SCy Schubert  * getopt --
137*b50261e2SCy Schubert  *	Parse argc/argv argument vector.
138*b50261e2SCy Schubert  */
139*b50261e2SCy Schubert int
140*b50261e2SCy Schubert getopt2(nargc, nargv, ostr)
141*b50261e2SCy Schubert 	int nargc;
142*b50261e2SCy Schubert 	char * const *nargv;
143*b50261e2SCy Schubert 	const char *ostr;
144*b50261e2SCy Schubert {
145*b50261e2SCy Schubert 	int retval;
146*b50261e2SCy Schubert 
147*b50261e2SCy Schubert 	if ((retval = getopt_internal(nargc, nargv, ostr)) == -2) {
148*b50261e2SCy Schubert 		retval = -1;
149*b50261e2SCy Schubert 		++optind;
150*b50261e2SCy Schubert 	}
151*b50261e2SCy Schubert 	return(retval);
152*b50261e2SCy Schubert }
153*b50261e2SCy Schubert #endif
154*b50261e2SCy Schubert 
155*b50261e2SCy Schubert /*
156*b50261e2SCy Schubert  * getopt_long --
157*b50261e2SCy Schubert  *	Parse argc/argv argument vector.
158*b50261e2SCy Schubert  */
159*b50261e2SCy Schubert int
getopt_long(nargc,nargv,options,long_options,index)160*b50261e2SCy Schubert getopt_long(nargc, nargv, options, long_options, index)
161*b50261e2SCy Schubert 	int nargc;
162*b50261e2SCy Schubert 	char ** nargv;
163*b50261e2SCy Schubert 	const char * options;
164*b50261e2SCy Schubert 	const struct option * long_options;
165*b50261e2SCy Schubert 	int * index;
166*b50261e2SCy Schubert {
167*b50261e2SCy Schubert 	int retval;
168*b50261e2SCy Schubert 
169*b50261e2SCy Schubert 	_DIAGASSERT(nargv != NULL);
170*b50261e2SCy Schubert 	_DIAGASSERT(options != NULL);
171*b50261e2SCy Schubert 	_DIAGASSERT(long_options != NULL);
172*b50261e2SCy Schubert 	/* index may be NULL */
173*b50261e2SCy Schubert 
174*b50261e2SCy Schubert 	if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
175*b50261e2SCy Schubert 		char *current_argv = nargv[optind++] + 2, *has_equal;
176*b50261e2SCy Schubert 		int i, match = -1;
177*b50261e2SCy Schubert 		size_t current_argv_len;
178*b50261e2SCy Schubert 
179*b50261e2SCy Schubert 		if (*current_argv == '\0') {
180*b50261e2SCy Schubert 			return(-1);
181*b50261e2SCy Schubert 		}
182*b50261e2SCy Schubert 		if ((has_equal = strchr(current_argv, '=')) != NULL) {
183*b50261e2SCy Schubert 			current_argv_len = has_equal - current_argv;
184*b50261e2SCy Schubert 			has_equal++;
185*b50261e2SCy Schubert 		} else
186*b50261e2SCy Schubert 			current_argv_len = strlen(current_argv);
187*b50261e2SCy Schubert 
188*b50261e2SCy Schubert 		for (i = 0; long_options[i].name; i++) {
189*b50261e2SCy Schubert 			if (strncmp(current_argv, long_options[i].name, current_argv_len))
190*b50261e2SCy Schubert 				continue;
191*b50261e2SCy Schubert 
192*b50261e2SCy Schubert 			if (strlen(long_options[i].name) == current_argv_len) {
193*b50261e2SCy Schubert 				match = i;
194*b50261e2SCy Schubert 				break;
195*b50261e2SCy Schubert 			}
196*b50261e2SCy Schubert 			if (match == -1)
197*b50261e2SCy Schubert 				match = i;
198*b50261e2SCy Schubert 		}
199*b50261e2SCy Schubert 		if (match != -1) {
200*b50261e2SCy Schubert 			if (long_options[match].has_arg == required_argument ||
201*b50261e2SCy Schubert 			    long_options[match].has_arg == optional_argument) {
202*b50261e2SCy Schubert 				if (has_equal)
203*b50261e2SCy Schubert 					optarg = has_equal;
204*b50261e2SCy Schubert 				else
205*b50261e2SCy Schubert 					optarg = nargv[optind++];
206*b50261e2SCy Schubert 			}
207*b50261e2SCy Schubert 			if ((long_options[match].has_arg == required_argument)
208*b50261e2SCy Schubert 			    && (optarg == NULL)) {
209*b50261e2SCy Schubert 				/*
210*b50261e2SCy Schubert 				 * Missing argument, leading :
211*b50261e2SCy Schubert 				 * indicates no error should be generated
212*b50261e2SCy Schubert 				 */
213*b50261e2SCy Schubert 				if ((opterr) && (*options != ':'))
214*b50261e2SCy Schubert 					(void)fprintf(stderr,
215*b50261e2SCy Schubert 				      "%s: option requires an argument -- %s\n",
216*b50261e2SCy Schubert 				      __progname(nargv[0]), current_argv);
217*b50261e2SCy Schubert 				return (BADARG);
218*b50261e2SCy Schubert 			}
219*b50261e2SCy Schubert 		} else { /* No matching argument */
220*b50261e2SCy Schubert 			if ((opterr) && (*options != ':'))
221*b50261e2SCy Schubert 				(void)fprintf(stderr,
222*b50261e2SCy Schubert 				    "%s: illegal option -- %s\n", __progname(nargv[0]), current_argv);
223*b50261e2SCy Schubert 			return (BADCH);
224*b50261e2SCy Schubert 		}
225*b50261e2SCy Schubert 		if (long_options[match].flag) {
226*b50261e2SCy Schubert 			*long_options[match].flag = long_options[match].val;
227*b50261e2SCy Schubert 			retval = 0;
228*b50261e2SCy Schubert 		} else
229*b50261e2SCy Schubert 			retval = long_options[match].val;
230*b50261e2SCy Schubert 		if (index)
231*b50261e2SCy Schubert 			*index = match;
232*b50261e2SCy Schubert 	}
233*b50261e2SCy Schubert 	return(retval);
234*b50261e2SCy Schubert }
235