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