egetopt.c revision 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 cgd /* from: static char sccsid[] = "@(#)egetopt.c 8.1 (Berkeley) 6/6/93"; */
40 1.1 cgd static char *rcsid = "$Id: egetopt.c,v 1.1 1994/01/06 15:57:18 cgd Exp $";
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #include <ctype.h>
44 1.1 cgd #include <stdio.h>
45 1.1 cgd #include <stdlib.h>
46 1.1 cgd #include <string.h>
47 1.1 cgd
48 1.1 cgd #include "extern.h"
49 1.1 cgd
50 1.1 cgd /*
51 1.1 cgd * egetopt: get option letter from argument vector (an extended
52 1.1 cgd * version of getopt).
53 1.1 cgd *
54 1.1 cgd * Non standard additions to the ostr specs are:
55 1.1 cgd * 1) '?': immediate value following arg is optional (no white space
56 1.1 cgd * between the arg and the value)
57 1.1 cgd * 2) '#': +/- followed by a number (with an optional sign but
58 1.1 cgd * no white space between the arg and the number). The - may be
59 1.1 cgd * combined with other options, but the + cannot.
60 1.1 cgd */
61 1.1 cgd
62 1.1 cgd int eopterr = 1; /* if error message should be printed */
63 1.1 cgd int eoptind = 1; /* index into parent argv vector */
64 1.1 cgd int eoptopt; /* character checked for validity */
65 1.1 cgd char *eoptarg; /* argument associated with option */
66 1.1 cgd
67 1.1 cgd #define BADCH (int)'?'
68 1.1 cgd #define EMSG ""
69 1.1 cgd
70 1.1 cgd int
71 1.1 cgd egetopt(nargc, nargv, ostr)
72 1.1 cgd int nargc;
73 1.1 cgd char * const *nargv;
74 1.1 cgd const char *ostr;
75 1.1 cgd {
76 1.1 cgd static char *place = EMSG; /* option letter processing */
77 1.1 cgd register char *oli; /* option letter list index */
78 1.1 cgd static int delim; /* which option delimeter */
79 1.1 cgd register char *p;
80 1.1 cgd static char savec = '\0';
81 1.1 cgd
82 1.1 cgd if (savec != '\0') {
83 1.1 cgd *place = savec;
84 1.1 cgd savec = '\0';
85 1.1 cgd }
86 1.1 cgd
87 1.1 cgd if (!*place) {
88 1.1 cgd /*
89 1.1 cgd * update scanning pointer
90 1.1 cgd */
91 1.1 cgd if ((eoptind >= nargc) ||
92 1.1 cgd ((*(place = nargv[eoptind]) != '-') && (*place != '+'))) {
93 1.1 cgd place = EMSG;
94 1.1 cgd return (EOF);
95 1.1 cgd }
96 1.1 cgd
97 1.1 cgd delim = (int)*place;
98 1.1 cgd if (place[1] && *++place == '-' && !place[1]) {
99 1.1 cgd /*
100 1.1 cgd * found "--"
101 1.1 cgd */
102 1.1 cgd ++eoptind;
103 1.1 cgd place = EMSG;
104 1.1 cgd return (EOF);
105 1.1 cgd }
106 1.1 cgd }
107 1.1 cgd
108 1.1 cgd /*
109 1.1 cgd * check option letter
110 1.1 cgd */
111 1.1 cgd if ((eoptopt = (int)*place++) == (int)':' || (eoptopt == (int)'?') ||
112 1.1 cgd !(oli = strchr(ostr, eoptopt))) {
113 1.1 cgd /*
114 1.1 cgd * if the user didn't specify '-' as an option,
115 1.1 cgd * assume it means EOF when by itself.
116 1.1 cgd */
117 1.1 cgd if ((eoptopt == (int)'-') && !*place)
118 1.1 cgd return (EOF);
119 1.1 cgd if (strchr(ostr, '#') && (isdigit(eoptopt) ||
120 1.1 cgd (((eoptopt == (int)'-') || (eoptopt == (int)'+')) &&
121 1.1 cgd isdigit(*place)))) {
122 1.1 cgd /*
123 1.1 cgd * # option: +/- with a number is ok
124 1.1 cgd */
125 1.1 cgd for (p = place; *p != '\0'; ++p) {
126 1.1 cgd if (!isdigit(*p))
127 1.1 cgd break;
128 1.1 cgd }
129 1.1 cgd eoptarg = place-1;
130 1.1 cgd
131 1.1 cgd if (*p == '\0') {
132 1.1 cgd place = EMSG;
133 1.1 cgd ++eoptind;
134 1.1 cgd } else {
135 1.1 cgd place = p;
136 1.1 cgd savec = *p;
137 1.1 cgd *place = '\0';
138 1.1 cgd }
139 1.1 cgd return (delim);
140 1.1 cgd }
141 1.1 cgd
142 1.1 cgd if (!*place)
143 1.1 cgd ++eoptind;
144 1.1 cgd if (eopterr) {
145 1.1 cgd if (!(p = strrchr(*nargv, '/')))
146 1.1 cgd p = *nargv;
147 1.1 cgd else
148 1.1 cgd ++p;
149 1.1 cgd (void)fprintf(stderr, "%s: illegal option -- %c\n",
150 1.1 cgd p, eoptopt);
151 1.1 cgd }
152 1.1 cgd return (BADCH);
153 1.1 cgd }
154 1.1 cgd if (delim == (int)'+') {
155 1.1 cgd /*
156 1.1 cgd * '+' is only allowed with numbers
157 1.1 cgd */
158 1.1 cgd if (!*place)
159 1.1 cgd ++eoptind;
160 1.1 cgd if (eopterr) {
161 1.1 cgd if (!(p = strrchr(*nargv, '/')))
162 1.1 cgd p = *nargv;
163 1.1 cgd else
164 1.1 cgd ++p;
165 1.1 cgd (void)fprintf(stderr,
166 1.1 cgd "%s: illegal '+' delimiter with option -- %c\n",
167 1.1 cgd p, eoptopt);
168 1.1 cgd }
169 1.1 cgd return (BADCH);
170 1.1 cgd }
171 1.1 cgd ++oli;
172 1.1 cgd if ((*oli != ':') && (*oli != '?')) {
173 1.1 cgd /*
174 1.1 cgd * don't need argument
175 1.1 cgd */
176 1.1 cgd eoptarg = NULL;
177 1.1 cgd if (!*place)
178 1.1 cgd ++eoptind;
179 1.1 cgd return (eoptopt);
180 1.1 cgd }
181 1.1 cgd
182 1.1 cgd if (*place) {
183 1.1 cgd /*
184 1.1 cgd * no white space
185 1.1 cgd */
186 1.1 cgd eoptarg = place;
187 1.1 cgd } else if (*oli == '?') {
188 1.1 cgd /*
189 1.1 cgd * no arg, but NOT required
190 1.1 cgd */
191 1.1 cgd eoptarg = NULL;
192 1.1 cgd } else if (nargc <= ++eoptind) {
193 1.1 cgd /*
194 1.1 cgd * no arg, but IS required
195 1.1 cgd */
196 1.1 cgd place = EMSG;
197 1.1 cgd if (eopterr) {
198 1.1 cgd if (!(p = strrchr(*nargv, '/')))
199 1.1 cgd p = *nargv;
200 1.1 cgd else
201 1.1 cgd ++p;
202 1.1 cgd (void)fprintf(stderr,
203 1.1 cgd "%s: option requires an argument -- %c\n", p,
204 1.1 cgd eoptopt);
205 1.1 cgd }
206 1.1 cgd return (BADCH);
207 1.1 cgd } else {
208 1.1 cgd /*
209 1.1 cgd * arg has white space
210 1.1 cgd */
211 1.1 cgd eoptarg = nargv[eoptind];
212 1.1 cgd }
213 1.1 cgd place = EMSG;
214 1.1 cgd ++eoptind;
215 1.1 cgd return (eoptopt);
216 1.1 cgd }
217