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