Debian Codegraph @

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