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