nrelease - fix/improve livecd
[dragonfly.git] / bin / sh / options.c
CommitLineData
984263bc 1/*-
3e3895bf
KP
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
984263bc
MD
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
69763be3 18 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
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
02d0b1ce
MD
35#ifndef lint
36#if 0
37static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95";
38#endif
39#endif /* not lint */
40#include <sys/cdefs.h>
3e3895bf 41__FBSDID("$FreeBSD: head/bin/sh/options.c 326025 2017-11-20 19:49:47Z pfg $");
02d0b1ce 42
984263bc
MD
43#include <signal.h>
44#include <unistd.h>
45#include <stdlib.h>
46
47#include "shell.h"
48#define DEFINE_OPTIONS
49#include "options.h"
50#undef DEFINE_OPTIONS
51#include "nodes.h" /* for other header files */
52#include "eval.h"
53#include "jobs.h"
54#include "input.h"
55#include "output.h"
56#include "trap.h"
57#include "var.h"
58#include "memalloc.h"
59#include "error.h"
60#include "mystring.h"
c3e3bc7a 61#include "builtins.h"
984263bc
MD
62#ifndef NO_HISTORY
63#include "myhistedit.h"
64#endif
65
66char *arg0; /* value of $0 */
67struct shparam shellparam; /* current positional parameters */
68char **argptr; /* argument list for builtin commands */
02d0b1ce 69char *shoptarg; /* set by nextopt (like getopt) */
99512ac4 70char *nextopt_optptr; /* used by nextopt */
984263bc
MD
71
72char *minusc; /* argument to -c option */
73
74
99512ac4
PA
75static void options(int);
76static void minus_o(char *, int);
77static void setoption(int, int);
3e3895bf
KP
78static void setoptionbyindex(int, int);
79static void setparam(int, char **);
99512ac4 80static int getopts(char *, char *, char **, char ***, char **);
984263bc
MD
81
82
83/*
84 * Process the shell command line arguments.
85 */
86
87void
88procargs(int argc, char **argv)
89{
90 int i;
84021a35 91 char *scriptname;
984263bc
MD
92
93 argptr = argv;
94 if (argc > 0)
95 argptr++;
96 for (i = 0; i < NOPTS; i++)
3e3895bf 97 optval[i] = 2;
984263bc
MD
98 privileged = (getuid() != geteuid() || getgid() != getegid());
99 options(1);
100 if (*argptr == NULL && minusc == NULL)
101 sflag = 1;
99512ac4 102 if (iflag != 0 && sflag == 1 && isatty(0) && isatty(1)) {
984263bc 103 iflag = 1;
99512ac4
PA
104 if (Eflag == 2)
105 Eflag = 1;
106 }
984263bc
MD
107 if (mflag == 2)
108 mflag = iflag;
109 for (i = 0; i < NOPTS; i++)
3e3895bf
KP
110 if (optval[i] == 2)
111 optval[i] = 0;
984263bc
MD
112 arg0 = argv[0];
113 if (sflag == 0 && minusc == NULL) {
84021a35
PA
114 scriptname = *argptr++;
115 setinputfile(scriptname, 0);
116 commandname = arg0 = scriptname;
984263bc
MD
117 }
118 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
119 if (argptr && minusc && *argptr)
120 arg0 = *argptr++;
121
122 shellparam.p = argptr;
123 shellparam.reset = 1;
124 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
125 while (*argptr) {
126 shellparam.nparam++;
127 argptr++;
128 }
129 optschanged();
130}
131
132
133void
134optschanged(void)
135{
3e3895bf 136 setinteractive();
984263bc
MD
137#ifndef NO_HISTORY
138 histedit();
139#endif
140 setjobctl(mflag);
141}
142
143/*
144 * Process shell options. The global variable argptr contains a pointer
145 * to the argument list; we advance it past the options.
3e3895bf
KP
146 * If cmdline is true, process the shell's argv; otherwise, process arguments
147 * to the set special builtin.
984263bc
MD
148 */
149
99512ac4 150static void
984263bc
MD
151options(int cmdline)
152{
67560595 153 char *kp, *p;
984263bc
MD
154 int val;
155 int c;
156
157 if (cmdline)
158 minusc = NULL;
159 while ((p = *argptr) != NULL) {
160 argptr++;
161 if ((c = *p++) == '-') {
162 val = 1;
67560595
PA
163 /* A "-" or "--" terminates options */
164 if (p[0] == '\0')
165 goto end_options1;
166 if (p[0] == '-' && p[1] == '\0')
167 goto end_options2;
168 /**
169 * For the benefit of `#!' lines in shell scripts,
170 * treat a string of '-- *#.*' the same as '--'.
171 * This is needed so that a script starting with:
172 * #!/bin/sh -- # -*- perl -*-
173 * will continue to work after a change is made to
174 * kern/imgact_shell.c to NOT token-ize the options
175 * specified on a '#!' line. A bit of a kludge,
176 * but that trick is recommended in documentation
177 * for some scripting languages, and we might as
178 * well continue to support it.
179 */
180 if (p[0] == '-') {
181 kp = p + 1;
182 while (*kp == ' ' || *kp == '\t')
183 kp++;
184 if (*kp == '#' || *kp == '\0')
185 goto end_options2;
984263bc
MD
186 }
187 } else if (c == '+') {
188 val = 0;
189 } else {
190 argptr--;
191 break;
192 }
193 while ((c = *p++) != '\0') {
194 if (c == 'c' && cmdline) {
195 char *q;
3e3895bf
KP
196
197 q = *argptr++;
984263bc
MD
198 if (q == NULL || minusc != NULL)
199 error("Bad -c option");
200 minusc = q;
984263bc
MD
201 } else if (c == 'o') {
202 minus_o(*argptr, val);
203 if (*argptr)
204 argptr++;
1bf98173 205 } else
984263bc 206 setoption(c, val);
984263bc
MD
207 }
208 }
67560595
PA
209 return;
210
211 /* When processing `set', a single "-" means turn off -x and -v */
212end_options1:
213 if (!cmdline) {
214 xflag = vflag = 0;
215 return;
216 }
217
218 /*
219 * When processing `set', a "--" means the remaining arguments
220 * replace the positional parameters in the active shell. If
221 * there are no remaining options, then all the positional
222 * parameters are cleared (equivalent to doing ``shift $#'').
223 */
224end_options2:
225 if (!cmdline) {
226 if (*argptr == NULL)
3e3895bf 227 setparam(0, argptr);
67560595
PA
228 return;
229 }
230
231 /*
232 * At this point we are processing options given to 'sh' on a command
233 * line. If an end-of-options marker ("-" or "--") is followed by an
234 * arg of "#", then skip over all remaining arguments. Some scripting
235 * languages (e.g.: perl) document that /bin/sh will implement this
236 * behavior, and they recommend that users take advantage of it to
237 * solve certain issues that can come up when writing a perl script.
238 * Yes, this feature is in /bin/sh to help users write perl scripts.
239 */
240 p = *argptr;
241 if (p != NULL && p[0] == '#' && p[1] == '\0') {
242 while (*argptr != NULL)
243 argptr++;
244 /* We need to keep the final argument */
245 argptr--;
246 }
984263bc
MD
247}
248
99512ac4 249static void
984263bc
MD
250minus_o(char *name, int val)
251{
67560595 252 int i;
3e3895bf
KP
253 const unsigned char *on;
254 size_t len;
984263bc
MD
255
256 if (name == NULL) {
257 if (val) {
258 /* "Pretty" output. */
259 out1str("Current option settings\n");
3e3895bf
KP
260 for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
261 out1fmt("%-16.*s%s\n", *on, on + 1,
262 optval[i] ? "on" : "off");
984263bc
MD
263 } else {
264 /* Output suitable for re-input to shell. */
3e3895bf
KP
265 for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
266 out1fmt("%s %co %.*s%s",
99512ac4 267 i % 6 == 0 ? "set" : "",
3e3895bf
KP
268 optval[i] ? '-' : '+',
269 *on, on + 1,
99512ac4 270 i % 6 == 5 || i == NOPTS - 1 ? "\n" : "");
984263bc
MD
271 }
272 } else {
3e3895bf
KP
273 len = strlen(name);
274 for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
275 if (*on == len && memcmp(on + 1, name, len) == 0) {
276 setoptionbyindex(i, val);
984263bc
MD
277 return;
278 }
279 error("Illegal option -o %s", name);
280 }
281}
282
283
99512ac4 284static void
3e3895bf 285setoptionbyindex(int idx, int val)
984263bc 286{
3e3895bf 287 if (&optval[idx] == &privileged && !val && privileged) {
1bf98173
PA
288 if (setgid(getgid()) == -1)
289 error("setgid");
290 if (setuid(getuid()) == -1)
291 error("setuid");
292 }
3e3895bf
KP
293 optval[idx] = val;
294 if (val) {
295 /* #%$ hack for ksh semantics */
296 if (&optval[idx] == &Vflag)
297 Eflag = 0;
298 else if (&optval[idx] == &Eflag)
299 Vflag = 0;
300 }
301}
302
303static void
304setoption(int flag, int val)
305{
306 int i;
307
308 for (i = 0; i < NSHORTOPTS; i++)
309 if (optletter[i] == flag) {
310 setoptionbyindex(i, val);
984263bc
MD
311 return;
312 }
313 error("Illegal option -%c", flag);
314}
315
316
984263bc
MD
317/*
318 * Set the shell parameters.
319 */
320
3e3895bf
KP
321static void
322setparam(int argc, char **argv)
984263bc
MD
323{
324 char **newparam;
325 char **ap;
984263bc 326
3e3895bf 327 ap = newparam = ckmalloc((argc + 1) * sizeof *ap);
984263bc
MD
328 while (*argv) {
329 *ap++ = savestr(*argv++);
330 }
331 *ap = NULL;
332 freeparam(&shellparam);
333 shellparam.malloc = 1;
3e3895bf 334 shellparam.nparam = argc;
984263bc 335 shellparam.p = newparam;
02d0b1ce 336 shellparam.optp = NULL;
99512ac4 337 shellparam.reset = 1;
984263bc
MD
338 shellparam.optnext = NULL;
339}
340
341
342/*
343 * Free the list of positional parameters.
344 */
345
346void
02d0b1ce 347freeparam(struct shparam *param)
984263bc
MD
348{
349 char **ap;
350
351 if (param->malloc) {
352 for (ap = param->p ; *ap ; ap++)
353 ckfree(*ap);
354 ckfree(param->p);
355 }
02d0b1ce
MD
356 if (param->optp) {
357 for (ap = param->optp ; *ap ; ap++)
358 ckfree(*ap);
359 ckfree(param->optp);
360 }
984263bc
MD
361}
362
363
364
365/*
366 * The shift builtin command.
367 */
368
369int
370shiftcmd(int argc, char **argv)
371{
3e3895bf 372 int i, n;
984263bc
MD
373
374 n = 1;
375 if (argc > 1)
376 n = number(argv[1]);
377 if (n > shellparam.nparam)
0d5aaed6 378 return 1;
984263bc
MD
379 INTOFF;
380 shellparam.nparam -= n;
3e3895bf
KP
381 if (shellparam.malloc)
382 for (i = 0; i < n; i++)
383 ckfree(shellparam.p[i]);
384 memmove(shellparam.p, shellparam.p + n,
385 (shellparam.nparam + 1) * sizeof(shellparam.p[0]));
99512ac4 386 shellparam.reset = 1;
984263bc
MD
387 INTON;
388 return 0;
389}
390
391
392
393/*
3e3895bf 394 * The set builtin command.
984263bc
MD
395 */
396
397int
398setcmd(int argc, char **argv)
399{
400 if (argc == 1)
401 return showvarscmd(argc, argv);
402 INTOFF;
403 options(0);
404 optschanged();
405 if (*argptr != NULL) {
3e3895bf 406 setparam(argc - (argptr - argv), argptr);
984263bc
MD
407 }
408 INTON;
409 return 0;
410}
411
412
413void
414getoptsreset(const char *value)
415{
45ae67c3
PA
416 while (*value == '0')
417 value++;
418 if (strcmp(value, "1") == 0)
984263bc 419 shellparam.reset = 1;
984263bc
MD
420}
421
422/*
423 * The getopts builtin. Shellparam.optnext points to the next argument
424 * to be processed. Shellparam.optptr points to the next character to
425 * be processed in the current argument. If shellparam.optnext is NULL,
426 * then it's the first time getopts has been called.
427 */
428
429int
430getoptscmd(int argc, char **argv)
431{
02d0b1ce
MD
432 char **optbase = NULL, **ap;
433 int i;
984263bc
MD
434
435 if (argc < 3)
436 error("usage: getopts optstring var [arg]");
984263bc
MD
437
438 if (shellparam.reset == 1) {
02d0b1ce
MD
439 INTOFF;
440 if (shellparam.optp) {
441 for (ap = shellparam.optp ; *ap ; ap++)
442 ckfree(*ap);
443 ckfree(shellparam.optp);
444 shellparam.optp = NULL;
445 }
446 if (argc > 3) {
447 shellparam.optp = ckmalloc((argc - 2) * sizeof *ap);
448 memset(shellparam.optp, '\0', (argc - 2) * sizeof *ap);
449 for (i = 0; i < argc - 3; i++)
450 shellparam.optp[i] = savestr(argv[i + 3]);
451 }
452 INTON;
453 optbase = argc == 3 ? shellparam.p : shellparam.optp;
984263bc
MD
454 shellparam.optnext = optbase;
455 shellparam.optptr = NULL;
456 shellparam.reset = 0;
02d0b1ce
MD
457 } else
458 optbase = shellparam.optp ? shellparam.optp : shellparam.p;
984263bc
MD
459
460 return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
461 &shellparam.optptr);
462}
463
99512ac4 464static int
984263bc 465getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
02d0b1ce 466 char **optptr)
984263bc
MD
467{
468 char *p, *q;
469 char c = '?';
470 int done = 0;
471 int ind = 0;
472 int err = 0;
473 char s[10];
02d0b1ce 474 const char *newoptarg = NULL;
984263bc 475
02d0b1ce 476 if ((p = *optptr) == NULL || *p == '\0') {
984263bc
MD
477 /* Current word is done, advance */
478 if (*optnext == NULL)
479 return 1;
480 p = **optnext;
481 if (p == NULL || *p != '-' || *++p == '\0') {
482atend:
483 ind = *optnext - optfirst + 1;
484 *optnext = NULL;
485 p = NULL;
486 done = 1;
487 goto out;
488 }
489 (*optnext)++;
490 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
491 goto atend;
492 }
493
494 c = *p++;
495 for (q = optstr; *q != c; ) {
496 if (*q == '\0') {
497 if (optstr[0] == ':') {
498 s[0] = c;
499 s[1] = '\0';
02d0b1ce 500 newoptarg = s;
984263bc 501 }
02d0b1ce
MD
502 else
503 out2fmt_flush("Illegal option -%c\n", c);
984263bc 504 c = '?';
02d0b1ce 505 goto out;
984263bc
MD
506 }
507 if (*++q == ':')
508 q++;
509 }
510
511 if (*++q == ':') {
512 if (*p == '\0' && (p = **optnext) == NULL) {
513 if (optstr[0] == ':') {
514 s[0] = c;
515 s[1] = '\0';
02d0b1ce 516 newoptarg = s;
984263bc
MD
517 c = ':';
518 }
519 else {
02d0b1ce 520 out2fmt_flush("No arg for -%c option\n", c);
984263bc
MD
521 c = '?';
522 }
02d0b1ce 523 goto out;
984263bc
MD
524 }
525
526 if (p == **optnext)
527 (*optnext)++;
02d0b1ce 528 newoptarg = p;
984263bc
MD
529 p = NULL;
530 }
02d0b1ce 531
984263bc 532out:
02d0b1ce
MD
533 if (*optnext != NULL)
534 ind = *optnext - optfirst + 1;
535 *optptr = p;
536 if (newoptarg != NULL)
537 err |= setvarsafe("OPTARG", newoptarg, 0);
538 else {
539 INTOFF;
540 err |= unsetvar("OPTARG");
541 INTON;
542 }
984263bc
MD
543 fmtstr(s, sizeof(s), "%d", ind);
544 err |= setvarsafe("OPTIND", s, VNOFUNC);
545 s[0] = c;
546 s[1] = '\0';
547 err |= setvarsafe(optvar, s, 0);
548 if (err) {
549 *optnext = NULL;
02d0b1ce 550 *optptr = NULL;
984263bc
MD
551 flushall();
552 exraise(EXERROR);
553 }
554 return done;
555}
556
557/*
984263bc
MD
558 * Standard option processing (a la getopt) for builtin routines. The
559 * only argument that is passed to nextopt is the option string; the
3e3895bf 560 * other arguments are unnecessary. It returns the option, or '\0' on
984263bc
MD
561 * end of input.
562 */
563
564int
492efe05 565nextopt(const char *optstring)
984263bc 566{
99512ac4
PA
567 char *p;
568 const char *q;
984263bc
MD
569 char c;
570
99512ac4 571 if ((p = nextopt_optptr) == NULL || *p == '\0') {
984263bc
MD
572 p = *argptr;
573 if (p == NULL || *p != '-' || *++p == '\0')
574 return '\0';
575 argptr++;
576 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
577 return '\0';
578 }
579 c = *p++;
580 for (q = optstring ; *q != c ; ) {
581 if (*q == '\0')
582 error("Illegal option -%c", c);
583 if (*++q == ':')
584 q++;
585 }
586 if (*++q == ':') {
587 if (*p == '\0' && (p = *argptr++) == NULL)
588 error("No arg for -%c option", c);
589 shoptarg = p;
590 p = NULL;
591 }
99512ac4 592 nextopt_optptr = p;
984263bc
MD
593 return c;
594}