14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
44b88c807SRodney W. Grimes * Copyright (c) 1991, 1993
54b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved.
64b88c807SRodney W. Grimes *
74b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes * Kenneth Almquist.
94b88c807SRodney W. Grimes *
104b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
114b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions
124b88c807SRodney W. Grimes * are met:
134b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
154b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
164b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
174b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
194b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software
204b88c807SRodney W. Grimes * without specific prior written permission.
214b88c807SRodney W. Grimes *
224b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324b88c807SRodney W. Grimes * SUCH DAMAGE.
334b88c807SRodney W. Grimes */
344b88c807SRodney W. Grimes
35aa9caaf6SPeter Wemm #include <signal.h>
36aa9caaf6SPeter Wemm #include <unistd.h>
37aa9caaf6SPeter Wemm #include <stdlib.h>
38aa9caaf6SPeter Wemm
394b88c807SRodney W. Grimes #include "shell.h"
404b88c807SRodney W. Grimes #define DEFINE_OPTIONS
414b88c807SRodney W. Grimes #include "options.h"
424b88c807SRodney W. Grimes #undef DEFINE_OPTIONS
434b88c807SRodney W. Grimes #include "nodes.h" /* for other header files */
444b88c807SRodney W. Grimes #include "eval.h"
454b88c807SRodney W. Grimes #include "jobs.h"
464b88c807SRodney W. Grimes #include "input.h"
474b88c807SRodney W. Grimes #include "output.h"
484b88c807SRodney W. Grimes #include "trap.h"
494b88c807SRodney W. Grimes #include "var.h"
504b88c807SRodney W. Grimes #include "memalloc.h"
514b88c807SRodney W. Grimes #include "error.h"
524b88c807SRodney W. Grimes #include "mystring.h"
53454a02b3SJilles Tjoelker #include "builtins.h"
54aa9caaf6SPeter Wemm #ifndef NO_HISTORY
55aa9caaf6SPeter Wemm #include "myhistedit.h"
56aa9caaf6SPeter Wemm #endif
574b88c807SRodney W. Grimes
584b88c807SRodney W. Grimes char *arg0; /* value of $0 */
594b88c807SRodney W. Grimes struct shparam shellparam; /* current positional parameters */
604b88c807SRodney W. Grimes char **argptr; /* argument list for builtin commands */
61f01e3d0cSMartin Cracauer char *shoptarg; /* set by nextopt (like getopt) */
62384aedabSJilles Tjoelker char *nextopt_optptr; /* used by nextopt */
634b88c807SRodney W. Grimes
644b88c807SRodney W. Grimes char *minusc; /* argument to -c option */
654b88c807SRodney W. Grimes
664b88c807SRodney W. Grimes
6765f5dd42SBaptiste Daroussin static int options(int);
6888328642SDavid E. O'Brien static void minus_o(char *, int);
6988328642SDavid E. O'Brien static void setoption(int, int);
7058bbcdb1SJilles Tjoelker static void setoptionbyindex(int, int);
716b8e48f4SJilles Tjoelker static void setparam(int, char **);
7288328642SDavid E. O'Brien static int getopts(char *, char *, char **, char ***, char **);
734b88c807SRodney W. Grimes
744b88c807SRodney W. Grimes
754b88c807SRodney W. Grimes /*
764b88c807SRodney W. Grimes * Process the shell command line arguments.
774b88c807SRodney W. Grimes */
784b88c807SRodney W. Grimes
7965f5dd42SBaptiste Daroussin int
procargs(int argc,char ** argv)805134c3f7SWarner Losh procargs(int argc, char **argv)
814b88c807SRodney W. Grimes {
8265f5dd42SBaptiste Daroussin int i, login;
83af449f7fSJilles Tjoelker char *scriptname;
844b88c807SRodney W. Grimes
854b88c807SRodney W. Grimes argptr = argv;
8665f5dd42SBaptiste Daroussin login = argptr[0] != NULL && argptr[0][0] == '-';
874b88c807SRodney W. Grimes if (argc > 0)
884b88c807SRodney W. Grimes argptr++;
894b88c807SRodney W. Grimes for (i = 0; i < NOPTS; i++)
903da40d4aSJilles Tjoelker optval[i] = 2;
91621a31c6SSteve Price privileged = (getuid() != geteuid() || getgid() != getegid());
9265f5dd42SBaptiste Daroussin login |= options(1);
934b88c807SRodney W. Grimes if (*argptr == NULL && minusc == NULL)
944b88c807SRodney W. Grimes sflag = 1;
95d5f6b530SJilles Tjoelker if (iflag != 0 && sflag == 1 && isatty(0) && isatty(1)) {
964b88c807SRodney W. Grimes iflag = 1;
97d5f6b530SJilles Tjoelker if (Eflag == 2)
98d5f6b530SJilles Tjoelker Eflag = 1;
99d5f6b530SJilles Tjoelker }
1004b88c807SRodney W. Grimes if (mflag == 2)
1014b88c807SRodney W. Grimes mflag = iflag;
1024b88c807SRodney W. Grimes for (i = 0; i < NOPTS; i++)
1033da40d4aSJilles Tjoelker if (optval[i] == 2)
1043da40d4aSJilles Tjoelker optval[i] = 0;
1054b88c807SRodney W. Grimes arg0 = argv[0];
1064b88c807SRodney W. Grimes if (sflag == 0 && minusc == NULL) {
107af449f7fSJilles Tjoelker scriptname = *argptr++;
108d2c23317SStephane Rochoy setinputfile(scriptname, 0, -1 /* verify */);
109af449f7fSJilles Tjoelker commandname = arg0 = scriptname;
1104b88c807SRodney W. Grimes }
111ab0a2172SSteve Price /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
112904a3dc8SSteve Price if (argptr && minusc && *argptr)
1136d753bddSJoerg Wunsch arg0 = *argptr++;
11493d0e5efSSteve Price
1154b88c807SRodney W. Grimes shellparam.p = argptr;
11693d0e5efSSteve Price shellparam.reset = 1;
1174b88c807SRodney W. Grimes /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
1184b88c807SRodney W. Grimes while (*argptr) {
1194b88c807SRodney W. Grimes shellparam.nparam++;
1204b88c807SRodney W. Grimes argptr++;
1214b88c807SRodney W. Grimes }
1224b88c807SRodney W. Grimes optschanged();
12365f5dd42SBaptiste Daroussin
12465f5dd42SBaptiste Daroussin return (login);
1254b88c807SRodney W. Grimes }
1264b88c807SRodney W. Grimes
1274b88c807SRodney W. Grimes
128aa9caaf6SPeter Wemm void
optschanged(void)1295134c3f7SWarner Losh optschanged(void)
130aa9caaf6SPeter Wemm {
1313be4e97dSJilles Tjoelker setinteractive();
132aa9caaf6SPeter Wemm #ifndef NO_HISTORY
1334b88c807SRodney W. Grimes histedit();
134aa9caaf6SPeter Wemm #endif
1354b88c807SRodney W. Grimes setjobctl(mflag);
1364b88c807SRodney W. Grimes }
1374b88c807SRodney W. Grimes
1384b88c807SRodney W. Grimes /*
1394b88c807SRodney W. Grimes * Process shell options. The global variable argptr contains a pointer
1404b88c807SRodney W. Grimes * to the argument list; we advance it past the options.
141b9807277SJilles Tjoelker * If cmdline is true, process the shell's argv; otherwise, process arguments
142b9807277SJilles Tjoelker * to the set special builtin.
1434b88c807SRodney W. Grimes */
1444b88c807SRodney W. Grimes
14565f5dd42SBaptiste Daroussin static int
options(int cmdline)1465134c3f7SWarner Losh options(int cmdline)
147aa9caaf6SPeter Wemm {
14801c7bd53SGarance A Drosehn char *kp, *p;
1494b88c807SRodney W. Grimes int val;
1504b88c807SRodney W. Grimes int c;
15165f5dd42SBaptiste Daroussin int login = 0;
1524b88c807SRodney W. Grimes
1534b88c807SRodney W. Grimes if (cmdline)
1544b88c807SRodney W. Grimes minusc = NULL;
1554b88c807SRodney W. Grimes while ((p = *argptr) != NULL) {
1564b88c807SRodney W. Grimes argptr++;
1574b88c807SRodney W. Grimes if ((c = *p++) == '-') {
1584b88c807SRodney W. Grimes val = 1;
1595cc540d4SGarance A Drosehn /* A "-" or "--" terminates options */
1604b88c807SRodney W. Grimes if (p[0] == '\0')
1615cc540d4SGarance A Drosehn goto end_options1;
1625cc540d4SGarance A Drosehn if (p[0] == '-' && p[1] == '\0')
1635cc540d4SGarance A Drosehn goto end_options2;
16401c7bd53SGarance A Drosehn /**
16501c7bd53SGarance A Drosehn * For the benefit of `#!' lines in shell scripts,
16601c7bd53SGarance A Drosehn * treat a string of '-- *#.*' the same as '--'.
16701c7bd53SGarance A Drosehn * This is needed so that a script starting with:
16801c7bd53SGarance A Drosehn * #!/bin/sh -- # -*- perl -*-
16901c7bd53SGarance A Drosehn * will continue to work after a change is made to
17001c7bd53SGarance A Drosehn * kern/imgact_shell.c to NOT token-ize the options
17101c7bd53SGarance A Drosehn * specified on a '#!' line. A bit of a kludge,
17201c7bd53SGarance A Drosehn * but that trick is recommended in documentation
17301c7bd53SGarance A Drosehn * for some scripting languages, and we might as
17401c7bd53SGarance A Drosehn * well continue to support it.
17501c7bd53SGarance A Drosehn */
17601c7bd53SGarance A Drosehn if (p[0] == '-') {
17701c7bd53SGarance A Drosehn kp = p + 1;
17801c7bd53SGarance A Drosehn while (*kp == ' ' || *kp == '\t')
17901c7bd53SGarance A Drosehn kp++;
18001c7bd53SGarance A Drosehn if (*kp == '#' || *kp == '\0')
18101c7bd53SGarance A Drosehn goto end_options2;
18201c7bd53SGarance A Drosehn }
1834b88c807SRodney W. Grimes } else if (c == '+') {
1844b88c807SRodney W. Grimes val = 0;
1854b88c807SRodney W. Grimes } else {
1864b88c807SRodney W. Grimes argptr--;
1874b88c807SRodney W. Grimes break;
1884b88c807SRodney W. Grimes }
1894b88c807SRodney W. Grimes while ((c = *p++) != '\0') {
1904b88c807SRodney W. Grimes if (c == 'c' && cmdline) {
1914b88c807SRodney W. Grimes char *q;
19293c3eab5SJilles Tjoelker
1934b88c807SRodney W. Grimes q = *argptr++;
1944b88c807SRodney W. Grimes if (q == NULL || minusc != NULL)
1954b88c807SRodney W. Grimes error("Bad -c option");
1964b88c807SRodney W. Grimes minusc = q;
197*0df81552SBaptiste Daroussin } else if (c == 'l' && cmdline) {
19865f5dd42SBaptiste Daroussin login = 1;
1994b88c807SRodney W. Grimes } else if (c == 'o') {
2004b88c807SRodney W. Grimes minus_o(*argptr, val);
2014b88c807SRodney W. Grimes if (*argptr)
2024b88c807SRodney W. Grimes argptr++;
203b7b23db5SJilles Tjoelker } else
2044b88c807SRodney W. Grimes setoption(c, val);
2054b88c807SRodney W. Grimes }
2064b88c807SRodney W. Grimes }
20765f5dd42SBaptiste Daroussin return (login);
2085cc540d4SGarance A Drosehn
2095cc540d4SGarance A Drosehn /* When processing `set', a single "-" means turn off -x and -v */
2105cc540d4SGarance A Drosehn end_options1:
2115cc540d4SGarance A Drosehn if (!cmdline) {
2125cc540d4SGarance A Drosehn xflag = vflag = 0;
21365f5dd42SBaptiste Daroussin return (login);
2145cc540d4SGarance A Drosehn }
2155cc540d4SGarance A Drosehn
2165cc540d4SGarance A Drosehn /*
2175cc540d4SGarance A Drosehn * When processing `set', a "--" means the remaining arguments
2185cc540d4SGarance A Drosehn * replace the positional parameters in the active shell. If
2195cc540d4SGarance A Drosehn * there are no remaining options, then all the positional
2205cc540d4SGarance A Drosehn * parameters are cleared (equivalent to doing ``shift $#'').
2215cc540d4SGarance A Drosehn */
2225cc540d4SGarance A Drosehn end_options2:
2235cc540d4SGarance A Drosehn if (!cmdline) {
2245cc540d4SGarance A Drosehn if (*argptr == NULL)
2256b8e48f4SJilles Tjoelker setparam(0, argptr);
22665f5dd42SBaptiste Daroussin return (login);
2275cc540d4SGarance A Drosehn }
2285cc540d4SGarance A Drosehn
2295cc540d4SGarance A Drosehn /*
2305cc540d4SGarance A Drosehn * At this point we are processing options given to 'sh' on a command
2315cc540d4SGarance A Drosehn * line. If an end-of-options marker ("-" or "--") is followed by an
2325cc540d4SGarance A Drosehn * arg of "#", then skip over all remaining arguments. Some scripting
2335cc540d4SGarance A Drosehn * languages (e.g.: perl) document that /bin/sh will implement this
2345cc540d4SGarance A Drosehn * behavior, and they recommend that users take advantage of it to
2355cc540d4SGarance A Drosehn * solve certain issues that can come up when writing a perl script.
2365cc540d4SGarance A Drosehn * Yes, this feature is in /bin/sh to help users write perl scripts.
2375cc540d4SGarance A Drosehn */
2385cc540d4SGarance A Drosehn p = *argptr;
2395cc540d4SGarance A Drosehn if (p != NULL && p[0] == '#' && p[1] == '\0') {
2405cc540d4SGarance A Drosehn while (*argptr != NULL)
2415cc540d4SGarance A Drosehn argptr++;
2425cc540d4SGarance A Drosehn /* We need to keep the final argument */
2435cc540d4SGarance A Drosehn argptr--;
2445cc540d4SGarance A Drosehn }
24565f5dd42SBaptiste Daroussin
24665f5dd42SBaptiste Daroussin return (login);
2474b88c807SRodney W. Grimes }
2484b88c807SRodney W. Grimes
24988328642SDavid E. O'Brien static void
minus_o(char * name,int val)2505134c3f7SWarner Losh minus_o(char *name, int val)
2514b88c807SRodney W. Grimes {
25270293cc7SStefan Farfeleder int i;
2533da40d4aSJilles Tjoelker const unsigned char *on;
2543da40d4aSJilles Tjoelker size_t len;
2554b88c807SRodney W. Grimes
2564b88c807SRodney W. Grimes if (name == NULL) {
257d513af6aSTim J. Robbins if (val) {
258d513af6aSTim J. Robbins /* "Pretty" output. */
2594b88c807SRodney W. Grimes out1str("Current option settings\n");
2603da40d4aSJilles Tjoelker for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
2613da40d4aSJilles Tjoelker out1fmt("%-16.*s%s\n", *on, on + 1,
2623da40d4aSJilles Tjoelker optval[i] ? "on" : "off");
2634b88c807SRodney W. Grimes } else {
264d513af6aSTim J. Robbins /* Output suitable for re-input to shell. */
2653da40d4aSJilles Tjoelker for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
2663da40d4aSJilles Tjoelker out1fmt("%s %co %.*s%s",
267aeb5d065SJilles Tjoelker i % 6 == 0 ? "set" : "",
2683da40d4aSJilles Tjoelker optval[i] ? '-' : '+',
2693da40d4aSJilles Tjoelker *on, on + 1,
270aeb5d065SJilles Tjoelker i % 6 == 5 || i == NOPTS - 1 ? "\n" : "");
271d513af6aSTim J. Robbins }
272d513af6aSTim J. Robbins } else {
2733da40d4aSJilles Tjoelker len = strlen(name);
2743da40d4aSJilles Tjoelker for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
2753da40d4aSJilles Tjoelker if (*on == len && memcmp(on + 1, name, len) == 0) {
27658bbcdb1SJilles Tjoelker setoptionbyindex(i, val);
2774b88c807SRodney W. Grimes return;
2784b88c807SRodney W. Grimes }
2794b88c807SRodney W. Grimes error("Illegal option -o %s", name);
2804b88c807SRodney W. Grimes }
2814b88c807SRodney W. Grimes }
2824b88c807SRodney W. Grimes
2834b88c807SRodney W. Grimes
28488328642SDavid E. O'Brien static void
setoptionbyindex(int idx,int val)28558bbcdb1SJilles Tjoelker setoptionbyindex(int idx, int val)
2864b88c807SRodney W. Grimes {
287c94a041fSJilles Tjoelker if (&optval[idx] == &privileged && !val && privileged) {
28892a1de47SJilles Tjoelker if (setgid(getgid()) == -1)
28992a1de47SJilles Tjoelker error("setgid");
29092a1de47SJilles Tjoelker if (setuid(getuid()) == -1)
29192a1de47SJilles Tjoelker error("setuid");
292b7b23db5SJilles Tjoelker }
2933da40d4aSJilles Tjoelker optval[idx] = val;
2944b88c807SRodney W. Grimes if (val) {
2954b88c807SRodney W. Grimes /* #%$ hack for ksh semantics */
296c94a041fSJilles Tjoelker if (&optval[idx] == &Vflag)
2974b88c807SRodney W. Grimes Eflag = 0;
298c94a041fSJilles Tjoelker else if (&optval[idx] == &Eflag)
2994b88c807SRodney W. Grimes Vflag = 0;
3004b88c807SRodney W. Grimes }
30158bbcdb1SJilles Tjoelker }
30258bbcdb1SJilles Tjoelker
30358bbcdb1SJilles Tjoelker static void
setoption(int flag,int val)30458bbcdb1SJilles Tjoelker setoption(int flag, int val)
30558bbcdb1SJilles Tjoelker {
30658bbcdb1SJilles Tjoelker int i;
30758bbcdb1SJilles Tjoelker
30862c37116SJilles Tjoelker for (i = 0; i < NSHORTOPTS; i++)
3093da40d4aSJilles Tjoelker if (optletter[i] == flag) {
31058bbcdb1SJilles Tjoelker setoptionbyindex(i, val);
3114b88c807SRodney W. Grimes return;
3124b88c807SRodney W. Grimes }
3134b88c807SRodney W. Grimes error("Illegal option -%c", flag);
3144b88c807SRodney W. Grimes }
3154b88c807SRodney W. Grimes
3164b88c807SRodney W. Grimes
3174b88c807SRodney W. Grimes /*
3184b88c807SRodney W. Grimes * Set the shell parameters.
3194b88c807SRodney W. Grimes */
3204b88c807SRodney W. Grimes
3216b8e48f4SJilles Tjoelker static void
setparam(int argc,char ** argv)3226b8e48f4SJilles Tjoelker setparam(int argc, char **argv)
3234b88c807SRodney W. Grimes {
3244b88c807SRodney W. Grimes char **newparam;
3254b88c807SRodney W. Grimes char **ap;
3264b88c807SRodney W. Grimes
3276b8e48f4SJilles Tjoelker ap = newparam = ckmalloc((argc + 1) * sizeof *ap);
3284b88c807SRodney W. Grimes while (*argv) {
3294b88c807SRodney W. Grimes *ap++ = savestr(*argv++);
3304b88c807SRodney W. Grimes }
3314b88c807SRodney W. Grimes *ap = NULL;
3324b88c807SRodney W. Grimes freeparam(&shellparam);
3334b88c807SRodney W. Grimes shellparam.malloc = 1;
3346b8e48f4SJilles Tjoelker shellparam.nparam = argc;
3354b88c807SRodney W. Grimes shellparam.p = newparam;
3361bc2fdfaSJilles Tjoelker shellparam.optp = NULL;
3379144fae1SStefan Farfeleder shellparam.reset = 1;
3384b88c807SRodney W. Grimes shellparam.optnext = NULL;
3394b88c807SRodney W. Grimes }
3404b88c807SRodney W. Grimes
3414b88c807SRodney W. Grimes
3424b88c807SRodney W. Grimes /*
3434b88c807SRodney W. Grimes * Free the list of positional parameters.
3444b88c807SRodney W. Grimes */
3454b88c807SRodney W. Grimes
3464b88c807SRodney W. Grimes void
freeparam(struct shparam * param)3475134c3f7SWarner Losh freeparam(struct shparam *param)
3484b88c807SRodney W. Grimes {
3494b88c807SRodney W. Grimes char **ap;
3504b88c807SRodney W. Grimes
3514b88c807SRodney W. Grimes if (param->malloc) {
3524b88c807SRodney W. Grimes for (ap = param->p ; *ap ; ap++)
3534b88c807SRodney W. Grimes ckfree(*ap);
3544b88c807SRodney W. Grimes ckfree(param->p);
3554b88c807SRodney W. Grimes }
3561bc2fdfaSJilles Tjoelker if (param->optp) {
3571bc2fdfaSJilles Tjoelker for (ap = param->optp ; *ap ; ap++)
3581bc2fdfaSJilles Tjoelker ckfree(*ap);
3591bc2fdfaSJilles Tjoelker ckfree(param->optp);
3601bc2fdfaSJilles Tjoelker }
3614b88c807SRodney W. Grimes }
3624b88c807SRodney W. Grimes
3634b88c807SRodney W. Grimes
3644b88c807SRodney W. Grimes
3654b88c807SRodney W. Grimes /*
3664b88c807SRodney W. Grimes * The shift builtin command.
3674b88c807SRodney W. Grimes */
3684b88c807SRodney W. Grimes
369aa9caaf6SPeter Wemm int
shiftcmd(int argc,char ** argv)3705134c3f7SWarner Losh shiftcmd(int argc, char **argv)
371aa9caaf6SPeter Wemm {
3726b8e48f4SJilles Tjoelker int i, n;
3734b88c807SRodney W. Grimes
3744b88c807SRodney W. Grimes n = 1;
3754b88c807SRodney W. Grimes if (argc > 1)
3764b88c807SRodney W. Grimes n = number(argv[1]);
3774b88c807SRodney W. Grimes if (n > shellparam.nparam)
37885170a4aSStefan Farfeleder return 1;
3794b88c807SRodney W. Grimes INTOFF;
3804b88c807SRodney W. Grimes shellparam.nparam -= n;
3814b88c807SRodney W. Grimes if (shellparam.malloc)
3826b8e48f4SJilles Tjoelker for (i = 0; i < n; i++)
3836b8e48f4SJilles Tjoelker ckfree(shellparam.p[i]);
3846b8e48f4SJilles Tjoelker memmove(shellparam.p, shellparam.p + n,
3856b8e48f4SJilles Tjoelker (shellparam.nparam + 1) * sizeof(shellparam.p[0]));
386f9ec075eSStefan Farfeleder shellparam.reset = 1;
3874b88c807SRodney W. Grimes INTON;
3884b88c807SRodney W. Grimes return 0;
3894b88c807SRodney W. Grimes }
3904b88c807SRodney W. Grimes
3914b88c807SRodney W. Grimes
3924b88c807SRodney W. Grimes
3934b88c807SRodney W. Grimes /*
394b9807277SJilles Tjoelker * The set builtin command.
3954b88c807SRodney W. Grimes */
3964b88c807SRodney W. Grimes
397aa9caaf6SPeter Wemm int
setcmd(int argc,char ** argv)3985134c3f7SWarner Losh setcmd(int argc, char **argv)
399aa9caaf6SPeter Wemm {
4004b88c807SRodney W. Grimes if (argc == 1)
4014b88c807SRodney W. Grimes return showvarscmd(argc, argv);
4024b88c807SRodney W. Grimes INTOFF;
4034b88c807SRodney W. Grimes options(0);
4044b88c807SRodney W. Grimes optschanged();
4054b88c807SRodney W. Grimes if (*argptr != NULL) {
4066b8e48f4SJilles Tjoelker setparam(argc - (argptr - argv), argptr);
4074b88c807SRodney W. Grimes }
4084b88c807SRodney W. Grimes INTON;
4094b88c807SRodney W. Grimes return 0;
4104b88c807SRodney W. Grimes }
4114b88c807SRodney W. Grimes
4124b88c807SRodney W. Grimes
413ab0a2172SSteve Price void
getoptsreset(const char * value)4145134c3f7SWarner Losh getoptsreset(const char *value)
415ab0a2172SSteve Price {
416bf3db314SJilles Tjoelker while (*value == '0')
417bf3db314SJilles Tjoelker value++;
418bf3db314SJilles Tjoelker if (strcmp(value, "1") == 0)
419ab0a2172SSteve Price shellparam.reset = 1;
420ab0a2172SSteve Price }
421ab0a2172SSteve Price
4224b88c807SRodney W. Grimes /*
4234b88c807SRodney W. Grimes * The getopts builtin. Shellparam.optnext points to the next argument
4244b88c807SRodney W. Grimes * to be processed. Shellparam.optptr points to the next character to
4254b88c807SRodney W. Grimes * be processed in the current argument. If shellparam.optnext is NULL,
4264b88c807SRodney W. Grimes * then it's the first time getopts has been called.
4274b88c807SRodney W. Grimes */
4284b88c807SRodney W. Grimes
429aa9caaf6SPeter Wemm int
getoptscmd(int argc,char ** argv)4305134c3f7SWarner Losh getoptscmd(int argc, char **argv)
431aa9caaf6SPeter Wemm {
4321bc2fdfaSJilles Tjoelker char **optbase = NULL, **ap;
4331bc2fdfaSJilles Tjoelker int i;
434ab0a2172SSteve Price
435ab0a2172SSteve Price if (argc < 3)
436d3974088SDag-Erling Smørgrav error("usage: getopts optstring var [arg]");
437ab0a2172SSteve Price
438ab0a2172SSteve Price if (shellparam.reset == 1) {
4391bc2fdfaSJilles Tjoelker INTOFF;
4401bc2fdfaSJilles Tjoelker if (shellparam.optp) {
4411bc2fdfaSJilles Tjoelker for (ap = shellparam.optp ; *ap ; ap++)
4421bc2fdfaSJilles Tjoelker ckfree(*ap);
4431bc2fdfaSJilles Tjoelker ckfree(shellparam.optp);
4441bc2fdfaSJilles Tjoelker shellparam.optp = NULL;
4451bc2fdfaSJilles Tjoelker }
4461bc2fdfaSJilles Tjoelker if (argc > 3) {
4471bc2fdfaSJilles Tjoelker shellparam.optp = ckmalloc((argc - 2) * sizeof *ap);
4481bc2fdfaSJilles Tjoelker memset(shellparam.optp, '\0', (argc - 2) * sizeof *ap);
4491bc2fdfaSJilles Tjoelker for (i = 0; i < argc - 3; i++)
4501bc2fdfaSJilles Tjoelker shellparam.optp[i] = savestr(argv[i + 3]);
4511bc2fdfaSJilles Tjoelker }
4521bc2fdfaSJilles Tjoelker INTON;
4531bc2fdfaSJilles Tjoelker optbase = argc == 3 ? shellparam.p : shellparam.optp;
454ab0a2172SSteve Price shellparam.optnext = optbase;
455ab0a2172SSteve Price shellparam.optptr = NULL;
456ab0a2172SSteve Price shellparam.reset = 0;
4571bc2fdfaSJilles Tjoelker } else
4581bc2fdfaSJilles Tjoelker optbase = shellparam.optp ? shellparam.optp : shellparam.p;
459ab0a2172SSteve Price
460ab0a2172SSteve Price return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
461ab0a2172SSteve Price &shellparam.optptr);
462ab0a2172SSteve Price }
463ab0a2172SSteve Price
46488328642SDavid E. O'Brien static int
getopts(char * optstr,char * optvar,char ** optfirst,char *** optnext,char ** optptr)4655134c3f7SWarner Losh getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
4665134c3f7SWarner Losh char **optptr)
467ab0a2172SSteve Price {
468904a3dc8SSteve Price char *p, *q;
469ab0a2172SSteve Price char c = '?';
470ab0a2172SSteve Price int done = 0;
471ab0a2172SSteve Price int ind = 0;
472ab0a2172SSteve Price int err = 0;
4734b88c807SRodney W. Grimes char s[10];
47422afca9bSJilles Tjoelker const char *newoptarg = NULL;
4754b88c807SRodney W. Grimes
476ab0a2172SSteve Price if ((p = *optptr) == NULL || *p == '\0') {
477ab0a2172SSteve Price /* Current word is done, advance */
478ab0a2172SSteve Price if (*optnext == NULL)
479ab0a2172SSteve Price return 1;
480ab0a2172SSteve Price p = **optnext;
4814b88c807SRodney W. Grimes if (p == NULL || *p != '-' || *++p == '\0') {
4824b88c807SRodney W. Grimes atend:
483ab0a2172SSteve Price ind = *optnext - optfirst + 1;
48493d0e5efSSteve Price *optnext = NULL;
48593d0e5efSSteve Price p = NULL;
486ab0a2172SSteve Price done = 1;
487ab0a2172SSteve Price goto out;
4884b88c807SRodney W. Grimes }
489ab0a2172SSteve Price (*optnext)++;
4904b88c807SRodney W. Grimes if (p[0] == '-' && p[1] == '\0') /* check for "--" */
4914b88c807SRodney W. Grimes goto atend;
4924b88c807SRodney W. Grimes }
493ab0a2172SSteve Price
4944b88c807SRodney W. Grimes c = *p++;
495ab0a2172SSteve Price for (q = optstr; *q != c; ) {
4964b88c807SRodney W. Grimes if (*q == '\0') {
497ab0a2172SSteve Price if (optstr[0] == ':') {
498ab0a2172SSteve Price s[0] = c;
499ab0a2172SSteve Price s[1] = '\0';
50022afca9bSJilles Tjoelker newoptarg = s;
501ab0a2172SSteve Price }
502c8fb3e69SJilles Tjoelker else
5038c4e5fc0SJilles Tjoelker out2fmt_flush("Illegal option -%c\n", c);
5044b88c807SRodney W. Grimes c = '?';
5056e76445cSJilles Tjoelker goto out;
5064b88c807SRodney W. Grimes }
5074b88c807SRodney W. Grimes if (*++q == ':')
5084b88c807SRodney W. Grimes q++;
5094b88c807SRodney W. Grimes }
510ab0a2172SSteve Price
5114b88c807SRodney W. Grimes if (*++q == ':') {
512ab0a2172SSteve Price if (*p == '\0' && (p = **optnext) == NULL) {
513ab0a2172SSteve Price if (optstr[0] == ':') {
5144b88c807SRodney W. Grimes s[0] = c;
5154b88c807SRodney W. Grimes s[1] = '\0';
51622afca9bSJilles Tjoelker newoptarg = s;
517ab0a2172SSteve Price c = ':';
518ab0a2172SSteve Price }
519ab0a2172SSteve Price else {
5208c4e5fc0SJilles Tjoelker out2fmt_flush("No arg for -%c option\n", c);
521ab0a2172SSteve Price c = '?';
522ab0a2172SSteve Price }
5236e76445cSJilles Tjoelker goto out;
524ab0a2172SSteve Price }
525ab0a2172SSteve Price
526ab0a2172SSteve Price if (p == **optnext)
527ab0a2172SSteve Price (*optnext)++;
52822afca9bSJilles Tjoelker newoptarg = p;
529ab0a2172SSteve Price p = NULL;
530ab0a2172SSteve Price }
531ab0a2172SSteve Price
532ab0a2172SSteve Price out:
5336e76445cSJilles Tjoelker if (*optnext != NULL)
5346e76445cSJilles Tjoelker ind = *optnext - optfirst + 1;
535ab0a2172SSteve Price *optptr = p;
53622afca9bSJilles Tjoelker if (newoptarg != NULL)
53722afca9bSJilles Tjoelker err |= setvarsafe("OPTARG", newoptarg, 0);
538c8fb3e69SJilles Tjoelker else {
539c8fb3e69SJilles Tjoelker INTOFF;
540c8fb3e69SJilles Tjoelker err |= unsetvar("OPTARG");
541c8fb3e69SJilles Tjoelker INTON;
542c8fb3e69SJilles Tjoelker }
543ab0a2172SSteve Price fmtstr(s, sizeof(s), "%d", ind);
544ab0a2172SSteve Price err |= setvarsafe("OPTIND", s, VNOFUNC);
545ab0a2172SSteve Price s[0] = c;
546ab0a2172SSteve Price s[1] = '\0';
547ab0a2172SSteve Price err |= setvarsafe(optvar, s, 0);
548ab0a2172SSteve Price if (err) {
549ab0a2172SSteve Price *optnext = NULL;
550ab0a2172SSteve Price *optptr = NULL;
551ab0a2172SSteve Price flushall();
552ab0a2172SSteve Price exraise(EXERROR);
553ab0a2172SSteve Price }
554ab0a2172SSteve Price return done;
5554b88c807SRodney W. Grimes }
5564b88c807SRodney W. Grimes
5574b88c807SRodney W. Grimes /*
5584b88c807SRodney W. Grimes * Standard option processing (a la getopt) for builtin routines. The
5594b88c807SRodney W. Grimes * only argument that is passed to nextopt is the option string; the
560b9807277SJilles Tjoelker * other arguments are unnecessary. It returns the option, or '\0' on
5614b88c807SRodney W. Grimes * end of input.
5624b88c807SRodney W. Grimes */
5634b88c807SRodney W. Grimes
5644b88c807SRodney W. Grimes int
nextopt(const char * optstring)5652cac6e36SJilles Tjoelker nextopt(const char *optstring)
5664b88c807SRodney W. Grimes {
5672cac6e36SJilles Tjoelker char *p;
5682cac6e36SJilles Tjoelker const char *q;
5694b88c807SRodney W. Grimes char c;
5704b88c807SRodney W. Grimes
571384aedabSJilles Tjoelker if ((p = nextopt_optptr) == NULL || *p == '\0') {
5724b88c807SRodney W. Grimes p = *argptr;
5734b88c807SRodney W. Grimes if (p == NULL || *p != '-' || *++p == '\0')
5744b88c807SRodney W. Grimes return '\0';
5754b88c807SRodney W. Grimes argptr++;
5764b88c807SRodney W. Grimes if (p[0] == '-' && p[1] == '\0') /* check for "--" */
5774b88c807SRodney W. Grimes return '\0';
5784b88c807SRodney W. Grimes }
5794b88c807SRodney W. Grimes c = *p++;
5804b88c807SRodney W. Grimes for (q = optstring ; *q != c ; ) {
5814b88c807SRodney W. Grimes if (*q == '\0')
5824b88c807SRodney W. Grimes error("Illegal option -%c", c);
5834b88c807SRodney W. Grimes if (*++q == ':')
5844b88c807SRodney W. Grimes q++;
5854b88c807SRodney W. Grimes }
5864b88c807SRodney W. Grimes if (*++q == ':') {
5874b88c807SRodney W. Grimes if (*p == '\0' && (p = *argptr++) == NULL)
5884b88c807SRodney W. Grimes error("No arg for -%c option", c);
589f01e3d0cSMartin Cracauer shoptarg = p;
5904b88c807SRodney W. Grimes p = NULL;
5914b88c807SRodney W. Grimes }
592755a1be6SPiotr Pawel Stefaniak if (p != NULL && *p != '\0')
593384aedabSJilles Tjoelker nextopt_optptr = p;
594755a1be6SPiotr Pawel Stefaniak else
595755a1be6SPiotr Pawel Stefaniak nextopt_optptr = NULL;
5964b88c807SRodney W. Grimes return c;
5974b88c807SRodney W. Grimes }
598