1 /* -*- mode: c; c-file-style: "bsd"; indent-tabs-mode: t -*- */
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 #ifndef HAVE_GETOPT_LONG
35 #include <assert.h>
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include "k5-platform.h"
41
42 #define __P(x) x
43 #define _DIAGASSERT(x) assert(x)
44
45 static char * __progname __P((char *));
46 int getopt_internal __P((int, char * const *, const char *));
47
48 static char *
__progname(nargv0)49 __progname(nargv0)
50 char * nargv0;
51 {
52 char * tmp;
53
54 _DIAGASSERT(nargv0 != NULL);
55
56 tmp = strrchr(nargv0, '/');
57 if (tmp)
58 tmp++;
59 else
60 tmp = nargv0;
61 return(tmp);
62 }
63
64 #define BADCH (int)'?'
65 #define BADARG (int)':'
66 #define EMSG ""
67
68 /*
69 * getopt --
70 * Parse argc/argv argument vector.
71 */
72 int
getopt_internal(nargc,nargv,ostr)73 getopt_internal(nargc, nargv, ostr)
74 int nargc;
75 char * const *nargv;
76 const char *ostr;
77 {
78 static char *place = EMSG; /* option letter processing */
79 char *oli; /* option letter list index */
80
81 _DIAGASSERT(nargv != NULL);
82 _DIAGASSERT(ostr != NULL);
83
84 if (!*place) { /* update scanning pointer */
85 if (optind >= nargc || *(place = nargv[optind]) != '-') {
86 place = EMSG;
87 return (-1);
88 }
89 if (place[1] && *++place == '-') { /* found "--" */
90 /* ++optind; */
91 place = EMSG;
92 return (-2);
93 }
94 } /* option letter okay? */
95 if ((optopt = (int)*place++) == (int)':' ||
96 !(oli = strchr(ostr, optopt))) {
97 /*
98 * if the user didn't specify '-' as an option,
99 * assume it means -1.
100 */
101 if (optopt == (int)'-')
102 return (-1);
103 if (!*place)
104 ++optind;
105 if (opterr && *ostr != ':')
106 (void)fprintf(stderr,
107 "%s: illegal option -- %c\n", __progname(nargv[0]), optopt);
108 return (BADCH);
109 }
110 if (*++oli != ':') { /* don't need argument */
111 optarg = NULL;
112 if (!*place)
113 ++optind;
114 } else { /* need an argument */
115 if (*place) /* no white space */
116 optarg = place;
117 else if (nargc <= ++optind) { /* no arg */
118 place = EMSG;
119 if ((opterr) && (*ostr != ':'))
120 (void)fprintf(stderr,
121 "%s: option requires an argument -- %c\n",
122 __progname(nargv[0]), optopt);
123 return (BADARG);
124 } else /* white space */
125 optarg = nargv[optind];
126 place = EMSG;
127 ++optind;
128 }
129 return (optopt); /* dump back option letter */
130 }
131
132 #if 0
133 /*
134 * getopt --
135 * Parse argc/argv argument vector.
136 */
137 int
138 getopt2(nargc, nargv, ostr)
139 int nargc;
140 char * const *nargv;
141 const char *ostr;
142 {
143 int retval;
144
145 if ((retval = getopt_internal(nargc, nargv, ostr)) == -2) {
146 retval = -1;
147 ++optind;
148 }
149 return(retval);
150 }
151 #endif
152
153 /*
154 * getopt_long --
155 * Parse argc/argv argument vector.
156 */
157 int
getopt_long(nargc,nargv,options,long_options,index)158 getopt_long(nargc, nargv, options, long_options, index)
159 int nargc;
160 char ** nargv;
161 char * options;
162 struct option * long_options;
163 int * index;
164 {
165 int retval;
166
167 _DIAGASSERT(nargv != NULL);
168 _DIAGASSERT(options != NULL);
169 _DIAGASSERT(long_options != NULL);
170 /* index may be NULL */
171
172 if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
173 char *current_argv = nargv[optind++] + 2, *has_equal;
174 int i, current_argv_len, match = -1;
175
176 if (*current_argv == '\0') {
177 return(-1);
178 }
179 if ((has_equal = strchr(current_argv, '=')) != NULL) {
180 current_argv_len = has_equal - current_argv;
181 has_equal++;
182 } else
183 current_argv_len = strlen(current_argv);
184
185 for (i = 0; long_options[i].name; i++) {
186 if (strncmp(current_argv, long_options[i].name, current_argv_len))
187 continue;
188
189 if (strlen(long_options[i].name) == (unsigned)current_argv_len) {
190 match = i;
191 break;
192 }
193 if (match == -1)
194 match = i;
195 }
196 if (match != -1) {
197 if (long_options[match].has_arg == required_argument ||
198 long_options[match].has_arg == optional_argument) {
199 if (has_equal)
200 optarg = has_equal;
201 else
202 optarg = nargv[optind++];
203 }
204 if ((long_options[match].has_arg == required_argument)
205 && (optarg == NULL)) {
206 /*
207 * Missing argument, leading :
208 * indicates no error should be generated
209 */
210 if ((opterr) && (*options != ':'))
211 (void)fprintf(stderr,
212 "%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,
219 "%s: illegal option -- %s\n", __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 }
232 #endif /* not HAVE_GETOPT_LONG */
233