options.c revision 1.51 1 1.51 kre /* $NetBSD: options.c,v 1.51 2017/11/19 03:23:01 kre Exp $ */
2 1.11 cgd
3 1.1 cgd /*-
4 1.5 jtc * Copyright (c) 1991, 1993
5 1.5 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.35 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.25 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.11 cgd #if 0
38 1.14 christos static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95";
39 1.11 cgd #else
40 1.51 kre __RCSID("$NetBSD: options.c,v 1.51 2017/11/19 03:23:01 kre Exp $");
41 1.11 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.14 christos #include <signal.h>
45 1.14 christos #include <unistd.h>
46 1.14 christos #include <stdlib.h>
47 1.14 christos
48 1.1 cgd #include "shell.h"
49 1.1 cgd #define DEFINE_OPTIONS
50 1.1 cgd #include "options.h"
51 1.1 cgd #undef DEFINE_OPTIONS
52 1.42 christos #include "builtins.h"
53 1.1 cgd #include "nodes.h" /* for other header files */
54 1.1 cgd #include "eval.h"
55 1.1 cgd #include "jobs.h"
56 1.1 cgd #include "input.h"
57 1.1 cgd #include "output.h"
58 1.1 cgd #include "trap.h"
59 1.1 cgd #include "var.h"
60 1.1 cgd #include "memalloc.h"
61 1.1 cgd #include "error.h"
62 1.1 cgd #include "mystring.h"
63 1.24 christos #ifndef SMALL
64 1.14 christos #include "myhistedit.h"
65 1.14 christos #endif
66 1.32 christos #include "show.h"
67 1.1 cgd
68 1.1 cgd char *arg0; /* value of $0 */
69 1.1 cgd struct shparam shellparam; /* current positional parameters */
70 1.1 cgd char **argptr; /* argument list for builtin commands */
71 1.30 christos char *optionarg; /* set by nextopt (like getopt) */
72 1.1 cgd char *optptr; /* used by nextopt */
73 1.1 cgd
74 1.1 cgd char *minusc; /* argument to -c option */
75 1.1 cgd
76 1.1 cgd
77 1.32 christos STATIC void options(int);
78 1.32 christos STATIC void minus_o(char *, int);
79 1.32 christos STATIC void setoption(int, int);
80 1.32 christos STATIC int getopts(char *, char *, char **, char ***, char **);
81 1.1 cgd
82 1.1 cgd
83 1.1 cgd /*
84 1.1 cgd * Process the shell command line arguments.
85 1.1 cgd */
86 1.1 cgd
87 1.1 cgd void
88 1.32 christos procargs(int argc, char **argv)
89 1.10 cgd {
90 1.41 lukem size_t i;
91 1.48 kre int psx;
92 1.1 cgd
93 1.1 cgd argptr = argv;
94 1.1 cgd if (argc > 0)
95 1.1 cgd argptr++;
96 1.48 kre
97 1.48 kre psx = posix; /* save what we set it to earlier */
98 1.48 kre /*
99 1.48 kre * option values are mostly boolean 0:off 1:on
100 1.48 kre * we use 2 (just in this routine) to mean "unknown yet"
101 1.48 kre */
102 1.5 jtc for (i = 0; i < NOPTS; i++)
103 1.5 jtc optlist[i].val = 2;
104 1.48 kre posix = psx; /* restore before processing -o ... */
105 1.48 kre
106 1.1 cgd options(1);
107 1.48 kre
108 1.1 cgd if (*argptr == NULL && minusc == NULL)
109 1.1 cgd sflag = 1;
110 1.50 kre if (iflag == 2 && sflag == 1 && isatty(0) && isatty(2))
111 1.1 cgd iflag = 1;
112 1.39 christos if (iflag == 1 && sflag == 2)
113 1.39 christos iflag = 2;
114 1.5 jtc if (mflag == 2)
115 1.5 jtc mflag = iflag;
116 1.44 christos #ifndef DO_SHAREDVFORK
117 1.44 christos if (usefork == 2)
118 1.44 christos usefork = 1;
119 1.44 christos #endif
120 1.49 kre #if DEBUG >= 2
121 1.48 kre if (debug == 2)
122 1.48 kre debug = 1;
123 1.32 christos #endif
124 1.48 kre /*
125 1.48 kre * Any options not dealt with as special cases just above,
126 1.48 kre * and which were not set on the command line, are set to
127 1.48 kre * their expected default values (mostly "off")
128 1.48 kre *
129 1.48 kre * then as each option is initialised, save its setting now
130 1.48 kre * as its "default" value for future use ("set -o default").
131 1.48 kre */
132 1.48 kre for (i = 0; i < NOPTS; i++) {
133 1.48 kre if (optlist[i].val == 2)
134 1.48 kre optlist[i].val = optlist[i].dflt;
135 1.48 kre optlist[i].dflt = optlist[i].val;
136 1.48 kre }
137 1.48 kre
138 1.1 cgd arg0 = argv[0];
139 1.1 cgd if (sflag == 0 && minusc == NULL) {
140 1.31 wiz commandname = argv[0];
141 1.31 wiz arg0 = *argptr++;
142 1.31 wiz setinputfile(arg0, 0);
143 1.31 wiz commandname = arg0;
144 1.1 cgd }
145 1.17 christos /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
146 1.33 dsl if (minusc != NULL) {
147 1.33 dsl if (argptr == NULL || *argptr == NULL)
148 1.33 dsl error("Bad -c option");
149 1.33 dsl minusc = *argptr++;
150 1.33 dsl if (*argptr != 0)
151 1.33 dsl arg0 = *argptr++;
152 1.33 dsl }
153 1.17 christos
154 1.1 cgd shellparam.p = argptr;
155 1.19 christos shellparam.reset = 1;
156 1.1 cgd /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
157 1.1 cgd while (*argptr) {
158 1.1 cgd shellparam.nparam++;
159 1.1 cgd argptr++;
160 1.1 cgd }
161 1.5 jtc optschanged();
162 1.1 cgd }
163 1.1 cgd
164 1.1 cgd
165 1.9 cgd void
166 1.32 christos optschanged(void)
167 1.9 cgd {
168 1.5 jtc setinteractive(iflag);
169 1.24 christos #ifndef SMALL
170 1.5 jtc histedit();
171 1.7 cgd #endif
172 1.5 jtc setjobctl(mflag);
173 1.5 jtc }
174 1.1 cgd
175 1.1 cgd /*
176 1.1 cgd * Process shell options. The global variable argptr contains a pointer
177 1.1 cgd * to the argument list; we advance it past the options.
178 1.1 cgd */
179 1.1 cgd
180 1.1 cgd STATIC void
181 1.32 christos options(int cmdline)
182 1.10 cgd {
183 1.37 christos static char empty[] = "";
184 1.22 tls char *p;
185 1.1 cgd int val;
186 1.1 cgd int c;
187 1.1 cgd
188 1.1 cgd if (cmdline)
189 1.1 cgd minusc = NULL;
190 1.1 cgd while ((p = *argptr) != NULL) {
191 1.1 cgd argptr++;
192 1.1 cgd if ((c = *p++) == '-') {
193 1.1 cgd val = 1;
194 1.14 christos if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) {
195 1.1 cgd if (!cmdline) {
196 1.1 cgd /* "-" means turn off -x and -v */
197 1.1 cgd if (p[0] == '\0')
198 1.1 cgd xflag = vflag = 0;
199 1.1 cgd /* "--" means reset params */
200 1.13 christos else if (*argptr == NULL)
201 1.13 christos setparam(argptr);
202 1.1 cgd }
203 1.1 cgd break; /* "-" or "--" terminates options */
204 1.1 cgd }
205 1.1 cgd } else if (c == '+') {
206 1.1 cgd val = 0;
207 1.1 cgd } else {
208 1.1 cgd argptr--;
209 1.1 cgd break;
210 1.1 cgd }
211 1.1 cgd while ((c = *p++) != '\0') {
212 1.48 kre if (val == 1 && c == 'c' && cmdline) {
213 1.37 christos /* command is after shell args*/
214 1.37 christos minusc = empty;
215 1.5 jtc } else if (c == 'o') {
216 1.48 kre if (*p != '\0')
217 1.48 kre minus_o(p, val + (cmdline ? val : 0));
218 1.48 kre else if (*argptr)
219 1.48 kre minus_o(*argptr++,
220 1.48 kre val + (cmdline ? val : 0));
221 1.48 kre else if (!cmdline)
222 1.48 kre minus_o(NULL, val);
223 1.48 kre else
224 1.48 kre error("arg for %co missing", "+-"[val]);
225 1.48 kre break;
226 1.47 kre #ifdef DEBUG
227 1.47 kre } else if (c == 'D') {
228 1.47 kre if (*p) {
229 1.47 kre set_debug(p, val);
230 1.47 kre break;
231 1.47 kre } else if (*argptr)
232 1.47 kre set_debug(*argptr++, val);
233 1.47 kre else
234 1.47 kre set_debug("*$", val);
235 1.47 kre #endif
236 1.1 cgd } else {
237 1.1 cgd setoption(c, val);
238 1.1 cgd }
239 1.1 cgd }
240 1.1 cgd }
241 1.1 cgd }
242 1.1 cgd
243 1.32 christos static void
244 1.41 lukem set_opt_val(size_t i, int val)
245 1.32 christos {
246 1.41 lukem size_t j;
247 1.32 christos int flag;
248 1.32 christos
249 1.32 christos if (val && (flag = optlist[i].opt_set)) {
250 1.32 christos /* some options (eg vi/emacs) are mutually exclusive */
251 1.32 christos for (j = 0; j < NOPTS; j++)
252 1.32 christos if (optlist[j].opt_set == flag)
253 1.32 christos optlist[j].val = 0;
254 1.32 christos }
255 1.51 kre if (i == _SH_OPT_Xflag)
256 1.51 kre xtracefdsetup(val);
257 1.32 christos optlist[i].val = val;
258 1.32 christos #ifdef DEBUG
259 1.32 christos if (&optlist[i].val == &debug)
260 1.51 kre opentrace(); /* different "trace" than the -x one... */
261 1.32 christos #endif
262 1.32 christos }
263 1.32 christos
264 1.5 jtc STATIC void
265 1.32 christos minus_o(char *name, int val)
266 1.5 jtc {
267 1.41 lukem size_t i;
268 1.46 christos const char *sep = ": ";
269 1.5 jtc
270 1.5 jtc if (name == NULL) {
271 1.40 dsl if (val) {
272 1.46 christos out1str("Current option settings");
273 1.40 dsl for (i = 0; i < NOPTS; i++) {
274 1.46 christos if (optlist[i].name == NULL) {
275 1.46 christos out1fmt("%s%c%c", sep,
276 1.46 christos "+-"[optlist[i].val],
277 1.46 christos optlist[i].letter);
278 1.46 christos sep = ", ";
279 1.46 christos }
280 1.46 christos }
281 1.46 christos out1c('\n');
282 1.46 christos for (i = 0; i < NOPTS; i++) {
283 1.46 christos if (optlist[i].name)
284 1.48 kre out1fmt("%-19s %s\n", optlist[i].name,
285 1.40 dsl optlist[i].val ? "on" : "off");
286 1.40 dsl }
287 1.40 dsl } else {
288 1.48 kre out1str("set -o default");
289 1.40 dsl for (i = 0; i < NOPTS; i++) {
290 1.48 kre if (optlist[i].val == optlist[i].dflt)
291 1.48 kre continue;
292 1.46 christos if (optlist[i].name)
293 1.46 christos out1fmt(" %co %s",
294 1.40 dsl "+-"[optlist[i].val], optlist[i].name);
295 1.46 christos else
296 1.46 christos out1fmt(" %c%c", "+-"[optlist[i].val],
297 1.46 christos optlist[i].letter);
298 1.40 dsl }
299 1.46 christos out1c('\n');
300 1.40 dsl }
301 1.5 jtc } else {
302 1.48 kre if (val == 1 && equal(name, "default")) { /* special case */
303 1.48 kre for (i = 0; i < NOPTS; i++)
304 1.48 kre set_opt_val(i, optlist[i].dflt);
305 1.48 kre return;
306 1.48 kre }
307 1.48 kre if (val)
308 1.48 kre val = 1;
309 1.5 jtc for (i = 0; i < NOPTS; i++)
310 1.46 christos if (optlist[i].name && equal(name, optlist[i].name)) {
311 1.32 christos set_opt_val(i, val);
312 1.51 kre if (i == _SH_OPT_Xflag)
313 1.51 kre set_opt_val(_SH_OPT_xflag, val);
314 1.5 jtc return;
315 1.5 jtc }
316 1.48 kre error("Illegal option %co %s", "+-"[val], name);
317 1.5 jtc }
318 1.5 jtc }
319 1.17 christos
320 1.1 cgd
321 1.1 cgd STATIC void
322 1.32 christos setoption(int flag, int val)
323 1.32 christos {
324 1.41 lukem size_t i;
325 1.1 cgd
326 1.5 jtc for (i = 0; i < NOPTS; i++)
327 1.5 jtc if (optlist[i].letter == flag) {
328 1.51 kre set_opt_val(i, val);
329 1.51 kre if (i == _SH_OPT_Xflag)
330 1.51 kre set_opt_val(_SH_OPT_xflag, val);
331 1.5 jtc return;
332 1.5 jtc }
333 1.48 kre error("Illegal option %c%c", "+-"[val], flag);
334 1.28 mycroft /* NOTREACHED */
335 1.1 cgd }
336 1.1 cgd
337 1.1 cgd
338 1.1 cgd
339 1.1 cgd #ifdef mkinit
340 1.1 cgd INCLUDE "options.h"
341 1.1 cgd
342 1.1 cgd SHELLPROC {
343 1.5 jtc int i;
344 1.5 jtc
345 1.32 christos for (i = 0; optlist[i].name; i++)
346 1.5 jtc optlist[i].val = 0;
347 1.5 jtc optschanged();
348 1.1 cgd
349 1.1 cgd }
350 1.1 cgd #endif
351 1.1 cgd
352 1.1 cgd
353 1.1 cgd /*
354 1.1 cgd * Set the shell parameters.
355 1.1 cgd */
356 1.1 cgd
357 1.1 cgd void
358 1.32 christos setparam(char **argv)
359 1.32 christos {
360 1.1 cgd char **newparam;
361 1.1 cgd char **ap;
362 1.1 cgd int nparam;
363 1.1 cgd
364 1.38 dsl for (nparam = 0 ; argv[nparam] ; nparam++)
365 1.38 dsl continue;
366 1.1 cgd ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
367 1.1 cgd while (*argv) {
368 1.1 cgd *ap++ = savestr(*argv++);
369 1.1 cgd }
370 1.1 cgd *ap = NULL;
371 1.1 cgd freeparam(&shellparam);
372 1.1 cgd shellparam.malloc = 1;
373 1.1 cgd shellparam.nparam = nparam;
374 1.1 cgd shellparam.p = newparam;
375 1.1 cgd shellparam.optnext = NULL;
376 1.1 cgd }
377 1.1 cgd
378 1.1 cgd
379 1.1 cgd /*
380 1.1 cgd * Free the list of positional parameters.
381 1.1 cgd */
382 1.1 cgd
383 1.1 cgd void
384 1.32 christos freeparam(volatile struct shparam *param)
385 1.32 christos {
386 1.1 cgd char **ap;
387 1.1 cgd
388 1.1 cgd if (param->malloc) {
389 1.1 cgd for (ap = param->p ; *ap ; ap++)
390 1.1 cgd ckfree(*ap);
391 1.1 cgd ckfree(param->p);
392 1.1 cgd }
393 1.1 cgd }
394 1.1 cgd
395 1.1 cgd
396 1.1 cgd
397 1.1 cgd /*
398 1.1 cgd * The shift builtin command.
399 1.1 cgd */
400 1.1 cgd
401 1.10 cgd int
402 1.32 christos shiftcmd(int argc, char **argv)
403 1.10 cgd {
404 1.1 cgd int n;
405 1.1 cgd char **ap1, **ap2;
406 1.1 cgd
407 1.45 christos if (argc > 2)
408 1.45 christos error("Usage: shift [n]");
409 1.1 cgd n = 1;
410 1.1 cgd if (argc > 1)
411 1.1 cgd n = number(argv[1]);
412 1.1 cgd if (n > shellparam.nparam)
413 1.5 jtc error("can't shift that many");
414 1.1 cgd INTOFF;
415 1.1 cgd shellparam.nparam -= n;
416 1.1 cgd for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
417 1.1 cgd if (shellparam.malloc)
418 1.1 cgd ckfree(*ap1);
419 1.1 cgd }
420 1.1 cgd ap2 = shellparam.p;
421 1.45 christos while ((*ap2++ = *ap1++) != NULL)
422 1.45 christos continue;
423 1.1 cgd shellparam.optnext = NULL;
424 1.1 cgd INTON;
425 1.1 cgd return 0;
426 1.1 cgd }
427 1.1 cgd
428 1.1 cgd
429 1.1 cgd
430 1.1 cgd /*
431 1.1 cgd * The set command builtin.
432 1.1 cgd */
433 1.1 cgd
434 1.10 cgd int
435 1.32 christos setcmd(int argc, char **argv)
436 1.10 cgd {
437 1.1 cgd if (argc == 1)
438 1.46 christos return showvars(0, 0, 1, 0);
439 1.1 cgd INTOFF;
440 1.1 cgd options(0);
441 1.5 jtc optschanged();
442 1.1 cgd if (*argptr != NULL) {
443 1.1 cgd setparam(argptr);
444 1.1 cgd }
445 1.1 cgd INTON;
446 1.1 cgd return 0;
447 1.1 cgd }
448 1.1 cgd
449 1.1 cgd
450 1.16 christos void
451 1.43 matt getoptsreset(const char *value)
452 1.16 christos {
453 1.19 christos if (number(value) == 1) {
454 1.18 christos shellparam.optnext = NULL;
455 1.19 christos shellparam.reset = 1;
456 1.19 christos }
457 1.16 christos }
458 1.16 christos
459 1.1 cgd /*
460 1.1 cgd * The getopts builtin. Shellparam.optnext points to the next argument
461 1.1 cgd * to be processed. Shellparam.optptr points to the next character to
462 1.1 cgd * be processed in the current argument. If shellparam.optnext is NULL,
463 1.1 cgd * then it's the first time getopts has been called.
464 1.1 cgd */
465 1.1 cgd
466 1.10 cgd int
467 1.32 christos getoptscmd(int argc, char **argv)
468 1.10 cgd {
469 1.15 christos char **optbase;
470 1.15 christos
471 1.15 christos if (argc < 3)
472 1.36 jmmv error("usage: getopts optstring var [arg]");
473 1.15 christos else if (argc == 3)
474 1.15 christos optbase = shellparam.p;
475 1.17 christos else
476 1.15 christos optbase = &argv[3];
477 1.1 cgd
478 1.19 christos if (shellparam.reset == 1) {
479 1.15 christos shellparam.optnext = optbase;
480 1.1 cgd shellparam.optptr = NULL;
481 1.19 christos shellparam.reset = 0;
482 1.1 cgd }
483 1.15 christos
484 1.15 christos return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
485 1.15 christos &shellparam.optptr);
486 1.15 christos }
487 1.15 christos
488 1.15 christos STATIC int
489 1.32 christos getopts(char *optstr, char *optvar, char **optfirst, char ***optnext, char **optpptr)
490 1.15 christos {
491 1.22 tls char *p, *q;
492 1.15 christos char c = '?';
493 1.15 christos int done = 0;
494 1.15 christos int ind = 0;
495 1.16 christos int err = 0;
496 1.34 itojun char s[12];
497 1.15 christos
498 1.29 christos if ((p = *optpptr) == NULL || *p == '\0') {
499 1.15 christos /* Current word is done, advance */
500 1.19 christos if (*optnext == NULL)
501 1.19 christos return 1;
502 1.15 christos p = **optnext;
503 1.1 cgd if (p == NULL || *p != '-' || *++p == '\0') {
504 1.1 cgd atend:
505 1.21 christos ind = *optnext - optfirst + 1;
506 1.20 christos *optnext = NULL;
507 1.21 christos p = NULL;
508 1.15 christos done = 1;
509 1.15 christos goto out;
510 1.1 cgd }
511 1.15 christos (*optnext)++;
512 1.1 cgd if (p[0] == '-' && p[1] == '\0') /* check for "--" */
513 1.1 cgd goto atend;
514 1.1 cgd }
515 1.15 christos
516 1.1 cgd c = *p++;
517 1.15 christos for (q = optstr; *q != c; ) {
518 1.1 cgd if (*q == '\0') {
519 1.15 christos if (optstr[0] == ':') {
520 1.15 christos s[0] = c;
521 1.15 christos s[1] = '\0';
522 1.16 christos err |= setvarsafe("OPTARG", s, 0);
523 1.32 christos } else {
524 1.26 christos outfmt(&errout, "Illegal option -%c\n", c);
525 1.32 christos (void) unsetvar("OPTARG", 0);
526 1.15 christos }
527 1.1 cgd c = '?';
528 1.16 christos goto bad;
529 1.1 cgd }
530 1.1 cgd if (*++q == ':')
531 1.1 cgd q++;
532 1.1 cgd }
533 1.15 christos
534 1.1 cgd if (*++q == ':') {
535 1.15 christos if (*p == '\0' && (p = **optnext) == NULL) {
536 1.15 christos if (optstr[0] == ':') {
537 1.15 christos s[0] = c;
538 1.15 christos s[1] = '\0';
539 1.16 christos err |= setvarsafe("OPTARG", s, 0);
540 1.15 christos c = ':';
541 1.32 christos } else {
542 1.26 christos outfmt(&errout, "No arg for -%c option\n", c);
543 1.32 christos (void) unsetvar("OPTARG", 0);
544 1.15 christos c = '?';
545 1.15 christos }
546 1.16 christos goto bad;
547 1.1 cgd }
548 1.15 christos
549 1.15 christos if (p == **optnext)
550 1.15 christos (*optnext)++;
551 1.32 christos err |= setvarsafe("OPTARG", p, 0);
552 1.1 cgd p = NULL;
553 1.32 christos } else
554 1.32 christos err |= setvarsafe("OPTARG", "", 0);
555 1.19 christos ind = *optnext - optfirst + 1;
556 1.19 christos goto out;
557 1.19 christos
558 1.16 christos bad:
559 1.16 christos ind = 1;
560 1.16 christos *optnext = NULL;
561 1.16 christos p = NULL;
562 1.1 cgd out:
563 1.29 christos *optpptr = p;
564 1.15 christos fmtstr(s, sizeof(s), "%d", ind);
565 1.16 christos err |= setvarsafe("OPTIND", s, VNOFUNC);
566 1.1 cgd s[0] = c;
567 1.1 cgd s[1] = '\0';
568 1.16 christos err |= setvarsafe(optvar, s, 0);
569 1.16 christos if (err) {
570 1.16 christos *optnext = NULL;
571 1.29 christos *optpptr = NULL;
572 1.16 christos flushall();
573 1.16 christos exraise(EXERROR);
574 1.16 christos }
575 1.15 christos return done;
576 1.1 cgd }
577 1.1 cgd
578 1.1 cgd /*
579 1.5 jtc * XXX - should get rid of. have all builtins use getopt(3). the
580 1.5 jtc * library getopt must have the BSD extension static variable "optreset"
581 1.5 jtc * otherwise it can't be used within the shell safely.
582 1.5 jtc *
583 1.1 cgd * Standard option processing (a la getopt) for builtin routines. The
584 1.1 cgd * only argument that is passed to nextopt is the option string; the
585 1.1 cgd * other arguments are unnecessary. It return the character, or '\0' on
586 1.1 cgd * end of input.
587 1.1 cgd */
588 1.1 cgd
589 1.1 cgd int
590 1.32 christos nextopt(const char *optstring)
591 1.32 christos {
592 1.29 christos char *p;
593 1.29 christos const char *q;
594 1.1 cgd char c;
595 1.1 cgd
596 1.1 cgd if ((p = optptr) == NULL || *p == '\0') {
597 1.1 cgd p = *argptr;
598 1.1 cgd if (p == NULL || *p != '-' || *++p == '\0')
599 1.1 cgd return '\0';
600 1.1 cgd argptr++;
601 1.1 cgd if (p[0] == '-' && p[1] == '\0') /* check for "--" */
602 1.1 cgd return '\0';
603 1.1 cgd }
604 1.1 cgd c = *p++;
605 1.1 cgd for (q = optstring ; *q != c ; ) {
606 1.1 cgd if (*q == '\0')
607 1.1 cgd error("Illegal option -%c", c);
608 1.1 cgd if (*++q == ':')
609 1.1 cgd q++;
610 1.1 cgd }
611 1.1 cgd if (*++q == ':') {
612 1.1 cgd if (*p == '\0' && (p = *argptr++) == NULL)
613 1.1 cgd error("No arg for -%c option", c);
614 1.30 christos optionarg = p;
615 1.1 cgd p = NULL;
616 1.1 cgd }
617 1.1 cgd optptr = p;
618 1.1 cgd return c;
619 1.1 cgd }
620