Debian Codegraph @

third_party/getopt/src/getopt_long.c

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