egetopt.c revision 1.2 1 1.2 tls /* $NetBSD: egetopt.c,v 1.2 1997/01/09 15:01:33 tls Exp $ */
2 1.2 tls
3 1.1 cgd /*-
4 1.1 cgd * Copyright (c) 1991 Keith Muller.
5 1.1 cgd * Copyright (c) 1993
6 1.1 cgd * The Regents of the University of California. All rights reserved.
7 1.1 cgd *
8 1.1 cgd * This code is derived from software contributed to Berkeley by
9 1.1 cgd * Keith Muller of the University of California, San Diego.
10 1.1 cgd *
11 1.1 cgd * Redistribution and use in source and binary forms, with or without
12 1.1 cgd * modification, are permitted provided that the following conditions
13 1.1 cgd * are met:
14 1.1 cgd * 1. Redistributions of source code must retain the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer.
16 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 cgd * notice, this list of conditions and the following disclaimer in the
18 1.1 cgd * documentation and/or other materials provided with the distribution.
19 1.1 cgd * 3. All advertising materials mentioning features or use of this software
20 1.1 cgd * must display the following acknowledgement:
21 1.1 cgd * This product includes software developed by the University of
22 1.1 cgd * California, Berkeley and its contributors.
23 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
24 1.1 cgd * may be used to endorse or promote products derived from this software
25 1.1 cgd * without specific prior written permission.
26 1.1 cgd *
27 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1 cgd * SUCH DAMAGE.
38 1.1 cgd */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.1 cgd /* from: static char sccsid[] = "@(#)egetopt.c 8.1 (Berkeley) 6/6/93"; */
42 1.2 tls static char *rcsid = "$NetBSD: egetopt.c,v 1.2 1997/01/09 15:01:33 tls Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <ctype.h>
46 1.1 cgd #include <stdio.h>
47 1.1 cgd #include <stdlib.h>
48 1.1 cgd #include <string.h>
49 1.1 cgd
50 1.1 cgd #include "extern.h"
51 1.1 cgd
52 1.1 cgd /*
53 1.1 cgd * egetopt: get option letter from argument vector (an extended
54 1.1 cgd * version of getopt).
55 1.1 cgd *
56 1.1 cgd * Non standard additions to the ostr specs are:
57 1.1 cgd * 1) '?': immediate value following arg is optional (no white space
58 1.1 cgd * between the arg and the value)
59 1.1 cgd * 2) '#': +/- followed by a number (with an optional sign but
60 1.1 cgd * no white space between the arg and the number). The - may be
61 1.1 cgd * combined with other options, but the + cannot.
62 1.1 cgd */
63 1.1 cgd
64 1.1 cgd int eopterr = 1; /* if error message should be printed */
65 1.1 cgd int eoptind = 1; /* index into parent argv vector */
66 1.1 cgd int eoptopt; /* character checked for validity */
67 1.1 cgd char *eoptarg; /* argument associated with option */
68 1.1 cgd
69 1.1 cgd #define BADCH (int)'?'
70 1.1 cgd #define EMSG ""
71 1.1 cgd
72 1.1 cgd int
73 1.1 cgd egetopt(nargc, nargv, ostr)
74 1.1 cgd int nargc;
75 1.1 cgd char * const *nargv;
76 1.1 cgd const char *ostr;
77 1.1 cgd {
78 1.1 cgd static char *place = EMSG; /* option letter processing */
79 1.1 cgd register char *oli; /* option letter list index */
80 1.1 cgd static int delim; /* which option delimeter */
81 1.1 cgd register char *p;
82 1.1 cgd static char savec = '\0';
83 1.1 cgd
84 1.1 cgd if (savec != '\0') {
85 1.1 cgd *place = savec;
86 1.1 cgd savec = '\0';
87 1.1 cgd }
88 1.1 cgd
89 1.1 cgd if (!*place) {
90 1.1 cgd /*
91 1.1 cgd * update scanning pointer
92 1.1 cgd */
93 1.1 cgd if ((eoptind >= nargc) ||
94 1.1 cgd ((*(place = nargv[eoptind]) != '-') && (*place != '+'))) {
95 1.1 cgd place = EMSG;
96 1.1 cgd return (EOF);
97 1.1 cgd }
98 1.1 cgd
99 1.1 cgd delim = (int)*place;
100 1.1 cgd if (place[1] && *++place == '-' && !place[1]) {
101 1.1 cgd /*
102 1.1 cgd * found "--"
103 1.1 cgd */
104 1.1 cgd ++eoptind;
105 1.1 cgd place = EMSG;
106 1.1 cgd return (EOF);
107 1.1 cgd }
108 1.1 cgd }
109 1.1 cgd
110 1.1 cgd /*
111 1.1 cgd * check option letter
112 1.1 cgd */
113 1.1 cgd if ((eoptopt = (int)*place++) == (int)':' || (eoptopt == (int)'?') ||
114 1.1 cgd !(oli = strchr(ostr, eoptopt))) {
115 1.1 cgd /*
116 1.1 cgd * if the user didn't specify '-' as an option,
117 1.1 cgd * assume it means EOF when by itself.
118 1.1 cgd */
119 1.1 cgd if ((eoptopt == (int)'-') && !*place)
120 1.1 cgd return (EOF);
121 1.1 cgd if (strchr(ostr, '#') && (isdigit(eoptopt) ||
122 1.1 cgd (((eoptopt == (int)'-') || (eoptopt == (int)'+')) &&
123 1.1 cgd isdigit(*place)))) {
124 1.1 cgd /*
125 1.1 cgd * # option: +/- with a number is ok
126 1.1 cgd */
127 1.1 cgd for (p = place; *p != '\0'; ++p) {
128 1.1 cgd if (!isdigit(*p))
129 1.1 cgd break;
130 1.1 cgd }
131 1.1 cgd eoptarg = place-1;
132 1.1 cgd
133 1.1 cgd if (*p == '\0') {
134 1.1 cgd place = EMSG;
135 1.1 cgd ++eoptind;
136 1.1 cgd } else {
137 1.1 cgd place = p;
138 1.1 cgd savec = *p;
139 1.1 cgd *place = '\0';
140 1.1 cgd }
141 1.1 cgd return (delim);
142 1.1 cgd }
143 1.1 cgd
144 1.1 cgd if (!*place)
145 1.1 cgd ++eoptind;
146 1.1 cgd if (eopterr) {
147 1.1 cgd if (!(p = strrchr(*nargv, '/')))
148 1.1 cgd p = *nargv;
149 1.1 cgd else
150 1.1 cgd ++p;
151 1.1 cgd (void)fprintf(stderr, "%s: illegal option -- %c\n",
152 1.1 cgd p, eoptopt);
153 1.1 cgd }
154 1.1 cgd return (BADCH);
155 1.1 cgd }
156 1.1 cgd if (delim == (int)'+') {
157 1.1 cgd /*
158 1.1 cgd * '+' is only allowed with numbers
159 1.1 cgd */
160 1.1 cgd if (!*place)
161 1.1 cgd ++eoptind;
162 1.1 cgd if (eopterr) {
163 1.1 cgd if (!(p = strrchr(*nargv, '/')))
164 1.1 cgd p = *nargv;
165 1.1 cgd else
166 1.1 cgd ++p;
167 1.1 cgd (void)fprintf(stderr,
168 1.1 cgd "%s: illegal '+' delimiter with option -- %c\n",
169 1.1 cgd p, eoptopt);
170 1.1 cgd }
171 1.1 cgd return (BADCH);
172 1.1 cgd }
173 1.1 cgd ++oli;
174 1.1 cgd if ((*oli != ':') && (*oli != '?')) {
175 1.1 cgd /*
176 1.1 cgd * don't need argument
177 1.1 cgd */
178 1.1 cgd eoptarg = NULL;
179 1.1 cgd if (!*place)
180 1.1 cgd ++eoptind;
181 1.1 cgd return (eoptopt);
182 1.1 cgd }
183 1.1 cgd
184 1.1 cgd if (*place) {
185 1.1 cgd /*
186 1.1 cgd * no white space
187 1.1 cgd */
188 1.1 cgd eoptarg = place;
189 1.1 cgd } else if (*oli == '?') {
190 1.1 cgd /*
191 1.1 cgd * no arg, but NOT required
192 1.1 cgd */
193 1.1 cgd eoptarg = NULL;
194 1.1 cgd } else if (nargc <= ++eoptind) {
195 1.1 cgd /*
196 1.1 cgd * no arg, but IS required
197 1.1 cgd */
198 1.1 cgd place = EMSG;
199 1.1 cgd if (eopterr) {
200 1.1 cgd if (!(p = strrchr(*nargv, '/')))
201 1.1 cgd p = *nargv;
202 1.1 cgd else
203 1.1 cgd ++p;
204 1.1 cgd (void)fprintf(stderr,
205 1.1 cgd "%s: option requires an argument -- %c\n", p,
206 1.1 cgd eoptopt);
207 1.1 cgd }
208 1.1 cgd return (BADCH);
209 1.1 cgd } else {
210 1.1 cgd /*
211 1.1 cgd * arg has white space
212 1.1 cgd */
213 1.1 cgd eoptarg = nargv[eoptind];
214 1.1 cgd }
215 1.1 cgd place = EMSG;
216 1.1 cgd ++eoptind;
217 1.1 cgd return (eoptopt);
218 1.1 cgd }
219