1994-05-26 06:18:55 +00:00
|
|
|
/*-
|
2017-11-20 19:49:47 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1994-05-26 06:18:55 +00:00
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Kenneth Almquist.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-26 06:18:55 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-05-18 06:44:24 +00:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95";
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:15:05 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "shell.h"
|
|
|
|
#define DEFINE_OPTIONS
|
|
|
|
#include "options.h"
|
|
|
|
#undef DEFINE_OPTIONS
|
|
|
|
#include "nodes.h" /* for other header files */
|
|
|
|
#include "eval.h"
|
|
|
|
#include "jobs.h"
|
|
|
|
#include "input.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "trap.h"
|
|
|
|
#include "var.h"
|
|
|
|
#include "memalloc.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "mystring.h"
|
2011-06-13 21:03:27 +00:00
|
|
|
#include "builtins.h"
|
1996-09-01 10:22:36 +00:00
|
|
|
#ifndef NO_HISTORY
|
|
|
|
#include "myhistedit.h"
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
char *arg0; /* value of $0 */
|
|
|
|
struct shparam shellparam; /* current positional parameters */
|
|
|
|
char **argptr; /* argument list for builtin commands */
|
2000-04-20 09:49:16 +00:00
|
|
|
char *shoptarg; /* set by nextopt (like getopt) */
|
2009-12-27 18:04:05 +00:00
|
|
|
char *nextopt_optptr; /* used by nextopt */
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
char *minusc; /* argument to -c option */
|
|
|
|
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void options(int);
|
|
|
|
static void minus_o(char *, int);
|
|
|
|
static void setoption(int, int);
|
2015-08-14 21:44:15 +00:00
|
|
|
static void setoptionbyindex(int, int);
|
2016-01-19 22:41:26 +00:00
|
|
|
static void setparam(int, char **);
|
2010-10-13 22:18:03 +00:00
|
|
|
static int getopts(char *, char *, char **, char ***, char **);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process the shell command line arguments.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
procargs(int argc, char **argv)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int i;
|
2011-06-04 22:19:00 +00:00
|
|
|
char *scriptname;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
argptr = argv;
|
|
|
|
if (argc > 0)
|
|
|
|
argptr++;
|
|
|
|
for (i = 0; i < NOPTS; i++)
|
2016-01-07 23:13:20 +00:00
|
|
|
optval[i] = 2;
|
1996-10-29 03:12:51 +00:00
|
|
|
privileged = (getuid() != geteuid() || getgid() != getegid());
|
1994-05-26 06:18:55 +00:00
|
|
|
options(1);
|
|
|
|
if (*argptr == NULL && minusc == NULL)
|
|
|
|
sflag = 1;
|
2010-04-05 14:15:51 +00:00
|
|
|
if (iflag != 0 && sflag == 1 && isatty(0) && isatty(1)) {
|
1994-05-26 06:18:55 +00:00
|
|
|
iflag = 1;
|
2010-04-05 14:15:51 +00:00
|
|
|
if (Eflag == 2)
|
|
|
|
Eflag = 1;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
if (mflag == 2)
|
|
|
|
mflag = iflag;
|
|
|
|
for (i = 0; i < NOPTS; i++)
|
2016-01-07 23:13:20 +00:00
|
|
|
if (optval[i] == 2)
|
|
|
|
optval[i] = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
arg0 = argv[0];
|
|
|
|
if (sflag == 0 && minusc == NULL) {
|
2011-06-04 22:19:00 +00:00
|
|
|
scriptname = *argptr++;
|
2021-09-23 08:40:59 +00:00
|
|
|
setinputfile(scriptname, 0, -1 /* verify */);
|
2011-06-04 22:19:00 +00:00
|
|
|
commandname = arg0 = scriptname;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
1996-12-14 06:20:03 +00:00
|
|
|
/* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
|
1997-04-28 03:16:12 +00:00
|
|
|
if (argptr && minusc && *argptr)
|
1995-10-01 15:11:42 +00:00
|
|
|
arg0 = *argptr++;
|
1996-12-21 13:21:57 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
shellparam.p = argptr;
|
1996-12-21 13:21:57 +00:00
|
|
|
shellparam.reset = 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
/* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
|
|
|
|
while (*argptr) {
|
|
|
|
shellparam.nparam++;
|
|
|
|
argptr++;
|
|
|
|
}
|
|
|
|
optschanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
optschanged(void)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2017-04-22 21:31:37 +00:00
|
|
|
setinteractive();
|
1996-09-01 10:22:36 +00:00
|
|
|
#ifndef NO_HISTORY
|
1994-05-26 06:18:55 +00:00
|
|
|
histedit();
|
1996-09-01 10:22:36 +00:00
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
setjobctl(mflag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process shell options. The global variable argptr contains a pointer
|
|
|
|
* to the argument list; we advance it past the options.
|
2017-05-06 13:28:42 +00:00
|
|
|
* If cmdline is true, process the shell's argv; otherwise, process arguments
|
|
|
|
* to the set special builtin.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
options(int cmdline)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2005-05-16 04:32:41 +00:00
|
|
|
char *kp, *p;
|
1994-05-26 06:18:55 +00:00
|
|
|
int val;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (cmdline)
|
|
|
|
minusc = NULL;
|
|
|
|
while ((p = *argptr) != NULL) {
|
|
|
|
argptr++;
|
|
|
|
if ((c = *p++) == '-') {
|
|
|
|
val = 1;
|
2005-02-16 05:17:58 +00:00
|
|
|
/* A "-" or "--" terminates options */
|
|
|
|
if (p[0] == '\0')
|
|
|
|
goto end_options1;
|
|
|
|
if (p[0] == '-' && p[1] == '\0')
|
|
|
|
goto end_options2;
|
2005-05-16 04:32:41 +00:00
|
|
|
/**
|
|
|
|
* For the benefit of `#!' lines in shell scripts,
|
|
|
|
* treat a string of '-- *#.*' the same as '--'.
|
|
|
|
* This is needed so that a script starting with:
|
|
|
|
* #!/bin/sh -- # -*- perl -*-
|
|
|
|
* will continue to work after a change is made to
|
|
|
|
* kern/imgact_shell.c to NOT token-ize the options
|
|
|
|
* specified on a '#!' line. A bit of a kludge,
|
|
|
|
* but that trick is recommended in documentation
|
|
|
|
* for some scripting languages, and we might as
|
|
|
|
* well continue to support it.
|
|
|
|
*/
|
|
|
|
if (p[0] == '-') {
|
|
|
|
kp = p + 1;
|
|
|
|
while (*kp == ' ' || *kp == '\t')
|
|
|
|
kp++;
|
|
|
|
if (*kp == '#' || *kp == '\0')
|
|
|
|
goto end_options2;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
} else if (c == '+') {
|
|
|
|
val = 0;
|
|
|
|
} else {
|
|
|
|
argptr--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while ((c = *p++) != '\0') {
|
|
|
|
if (c == 'c' && cmdline) {
|
|
|
|
char *q;
|
2017-07-18 19:00:15 +00:00
|
|
|
|
|
|
|
q = *argptr++;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (q == NULL || minusc != NULL)
|
|
|
|
error("Bad -c option");
|
|
|
|
minusc = q;
|
|
|
|
} else if (c == 'o') {
|
|
|
|
minus_o(*argptr, val);
|
|
|
|
if (*argptr)
|
|
|
|
argptr++;
|
2011-04-25 10:08:34 +00:00
|
|
|
} else
|
1994-05-26 06:18:55 +00:00
|
|
|
setoption(c, val);
|
|
|
|
}
|
|
|
|
}
|
2005-02-16 05:17:58 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* When processing `set', a single "-" means turn off -x and -v */
|
|
|
|
end_options1:
|
|
|
|
if (!cmdline) {
|
|
|
|
xflag = vflag = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When processing `set', a "--" means the remaining arguments
|
|
|
|
* replace the positional parameters in the active shell. If
|
|
|
|
* there are no remaining options, then all the positional
|
|
|
|
* parameters are cleared (equivalent to doing ``shift $#'').
|
|
|
|
*/
|
|
|
|
end_options2:
|
|
|
|
if (!cmdline) {
|
|
|
|
if (*argptr == NULL)
|
2016-01-19 22:41:26 +00:00
|
|
|
setparam(0, argptr);
|
2005-02-16 05:17:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point we are processing options given to 'sh' on a command
|
|
|
|
* line. If an end-of-options marker ("-" or "--") is followed by an
|
|
|
|
* arg of "#", then skip over all remaining arguments. Some scripting
|
|
|
|
* languages (e.g.: perl) document that /bin/sh will implement this
|
|
|
|
* behavior, and they recommend that users take advantage of it to
|
|
|
|
* solve certain issues that can come up when writing a perl script.
|
|
|
|
* Yes, this feature is in /bin/sh to help users write perl scripts.
|
|
|
|
*/
|
|
|
|
p = *argptr;
|
|
|
|
if (p != NULL && p[0] == '#' && p[1] == '\0') {
|
|
|
|
while (*argptr != NULL)
|
|
|
|
argptr++;
|
|
|
|
/* We need to keep the final argument */
|
|
|
|
argptr--;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
minus_o(char *name, int val)
|
1994-05-26 06:18:55 +00:00
|
|
|
{
|
2005-10-29 18:41:35 +00:00
|
|
|
int i;
|
2016-01-07 23:13:20 +00:00
|
|
|
const unsigned char *on;
|
|
|
|
size_t len;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
if (name == NULL) {
|
2002-05-25 12:11:58 +00:00
|
|
|
if (val) {
|
|
|
|
/* "Pretty" output. */
|
|
|
|
out1str("Current option settings\n");
|
2016-01-07 23:13:20 +00:00
|
|
|
for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
|
|
|
|
out1fmt("%-16.*s%s\n", *on, on + 1,
|
|
|
|
optval[i] ? "on" : "off");
|
2002-05-25 12:11:58 +00:00
|
|
|
} else {
|
|
|
|
/* Output suitable for re-input to shell. */
|
2016-01-07 23:13:20 +00:00
|
|
|
for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
|
|
|
|
out1fmt("%s %co %.*s%s",
|
2010-11-20 14:14:52 +00:00
|
|
|
i % 6 == 0 ? "set" : "",
|
2016-01-07 23:13:20 +00:00
|
|
|
optval[i] ? '-' : '+',
|
|
|
|
*on, on + 1,
|
2010-11-20 14:14:52 +00:00
|
|
|
i % 6 == 5 || i == NOPTS - 1 ? "\n" : "");
|
2002-05-25 12:11:58 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
} else {
|
2016-01-07 23:13:20 +00:00
|
|
|
len = strlen(name);
|
|
|
|
for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
|
|
|
|
if (*on == len && memcmp(on + 1, name, len) == 0) {
|
2015-08-14 21:44:15 +00:00
|
|
|
setoptionbyindex(i, val);
|
1994-05-26 06:18:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
error("Illegal option -o %s", name);
|
|
|
|
}
|
|
|
|
}
|
1995-05-30 00:07:29 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2015-08-14 21:44:15 +00:00
|
|
|
setoptionbyindex(int idx, int val)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
2016-03-09 21:00:57 +00:00
|
|
|
if (&optval[idx] == &privileged && !val && privileged) {
|
2011-04-25 10:14:29 +00:00
|
|
|
if (setgid(getgid()) == -1)
|
|
|
|
error("setgid");
|
|
|
|
if (setuid(getuid()) == -1)
|
|
|
|
error("setuid");
|
2011-04-25 10:08:34 +00:00
|
|
|
}
|
2016-01-07 23:13:20 +00:00
|
|
|
optval[idx] = val;
|
2015-08-14 21:44:15 +00:00
|
|
|
if (val) {
|
|
|
|
/* #%$ hack for ksh semantics */
|
2016-03-09 21:00:57 +00:00
|
|
|
if (&optval[idx] == &Vflag)
|
2015-08-14 21:44:15 +00:00
|
|
|
Eflag = 0;
|
2016-03-09 21:00:57 +00:00
|
|
|
else if (&optval[idx] == &Eflag)
|
2015-08-14 21:44:15 +00:00
|
|
|
Vflag = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setoption(int flag, int val)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-08-29 19:41:47 +00:00
|
|
|
for (i = 0; i < NSHORTOPTS; i++)
|
2016-01-07 23:13:20 +00:00
|
|
|
if (optletter[i] == flag) {
|
2015-08-14 21:44:15 +00:00
|
|
|
setoptionbyindex(i, val);
|
1994-05-26 06:18:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
error("Illegal option -%c", flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the shell parameters.
|
|
|
|
*/
|
|
|
|
|
2016-01-19 22:41:26 +00:00
|
|
|
static void
|
|
|
|
setparam(int argc, char **argv)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
char **newparam;
|
|
|
|
char **ap;
|
|
|
|
|
2016-01-19 22:41:26 +00:00
|
|
|
ap = newparam = ckmalloc((argc + 1) * sizeof *ap);
|
1994-05-26 06:18:55 +00:00
|
|
|
while (*argv) {
|
|
|
|
*ap++ = savestr(*argv++);
|
|
|
|
}
|
|
|
|
*ap = NULL;
|
|
|
|
freeparam(&shellparam);
|
|
|
|
shellparam.malloc = 1;
|
2016-01-19 22:41:26 +00:00
|
|
|
shellparam.nparam = argc;
|
1994-05-26 06:18:55 +00:00
|
|
|
shellparam.p = newparam;
|
2014-10-26 17:50:33 +00:00
|
|
|
shellparam.optp = NULL;
|
2008-08-27 20:16:06 +00:00
|
|
|
shellparam.reset = 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
shellparam.optnext = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the list of positional parameters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
freeparam(struct shparam *param)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
char **ap;
|
|
|
|
|
|
|
|
if (param->malloc) {
|
|
|
|
for (ap = param->p ; *ap ; ap++)
|
|
|
|
ckfree(*ap);
|
|
|
|
ckfree(param->p);
|
|
|
|
}
|
2014-10-26 17:50:33 +00:00
|
|
|
if (param->optp) {
|
|
|
|
for (ap = param->optp ; *ap ; ap++)
|
|
|
|
ckfree(*ap);
|
|
|
|
ckfree(param->optp);
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The shift builtin command.
|
|
|
|
*/
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
shiftcmd(int argc, char **argv)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2016-01-19 22:41:26 +00:00
|
|
|
int i, n;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
n = 1;
|
|
|
|
if (argc > 1)
|
|
|
|
n = number(argv[1]);
|
|
|
|
if (n > shellparam.nparam)
|
2006-04-09 12:21:20 +00:00
|
|
|
return 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
INTOFF;
|
|
|
|
shellparam.nparam -= n;
|
2016-01-19 22:41:26 +00:00
|
|
|
if (shellparam.malloc)
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
ckfree(shellparam.p[i]);
|
|
|
|
memmove(shellparam.p, shellparam.p + n,
|
|
|
|
(shellparam.nparam + 1) * sizeof(shellparam.p[0]));
|
2008-03-22 14:06:01 +00:00
|
|
|
shellparam.reset = 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2017-05-06 13:28:42 +00:00
|
|
|
* The set builtin command.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
setcmd(int argc, char **argv)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
if (argc == 1)
|
|
|
|
return showvarscmd(argc, argv);
|
|
|
|
INTOFF;
|
|
|
|
options(0);
|
|
|
|
optschanged();
|
|
|
|
if (*argptr != NULL) {
|
2016-01-19 22:41:26 +00:00
|
|
|
setparam(argc - (argptr - argv), argptr);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-14 06:20:03 +00:00
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
getoptsreset(const char *value)
|
1996-12-14 06:20:03 +00:00
|
|
|
{
|
2011-11-20 21:48:50 +00:00
|
|
|
while (*value == '0')
|
|
|
|
value++;
|
|
|
|
if (strcmp(value, "1") == 0)
|
1996-12-14 06:20:03 +00:00
|
|
|
shellparam.reset = 1;
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* The getopts builtin. Shellparam.optnext points to the next argument
|
|
|
|
* to be processed. Shellparam.optptr points to the next character to
|
|
|
|
* be processed in the current argument. If shellparam.optnext is NULL,
|
|
|
|
* then it's the first time getopts has been called.
|
|
|
|
*/
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
getoptscmd(int argc, char **argv)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2014-10-26 17:50:33 +00:00
|
|
|
char **optbase = NULL, **ap;
|
|
|
|
int i;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1996-12-14 06:20:03 +00:00
|
|
|
if (argc < 3)
|
2002-04-22 13:44:47 +00:00
|
|
|
error("usage: getopts optstring var [arg]");
|
1996-12-14 06:20:03 +00:00
|
|
|
|
|
|
|
if (shellparam.reset == 1) {
|
2014-10-26 17:50:33 +00:00
|
|
|
INTOFF;
|
|
|
|
if (shellparam.optp) {
|
|
|
|
for (ap = shellparam.optp ; *ap ; ap++)
|
|
|
|
ckfree(*ap);
|
|
|
|
ckfree(shellparam.optp);
|
|
|
|
shellparam.optp = NULL;
|
|
|
|
}
|
|
|
|
if (argc > 3) {
|
|
|
|
shellparam.optp = ckmalloc((argc - 2) * sizeof *ap);
|
|
|
|
memset(shellparam.optp, '\0', (argc - 2) * sizeof *ap);
|
|
|
|
for (i = 0; i < argc - 3; i++)
|
|
|
|
shellparam.optp[i] = savestr(argv[i + 3]);
|
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
optbase = argc == 3 ? shellparam.p : shellparam.optp;
|
1996-12-14 06:20:03 +00:00
|
|
|
shellparam.optnext = optbase;
|
1994-05-26 06:18:55 +00:00
|
|
|
shellparam.optptr = NULL;
|
1996-12-14 06:20:03 +00:00
|
|
|
shellparam.reset = 0;
|
2014-10-26 17:50:33 +00:00
|
|
|
} else
|
|
|
|
optbase = shellparam.optp ? shellparam.optp : shellparam.p;
|
1996-12-14 06:20:03 +00:00
|
|
|
|
|
|
|
return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
|
|
|
|
&shellparam.optptr);
|
|
|
|
}
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static int
|
2002-02-02 06:50:57 +00:00
|
|
|
getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
|
|
|
|
char **optptr)
|
1996-12-14 06:20:03 +00:00
|
|
|
{
|
1997-04-28 03:16:12 +00:00
|
|
|
char *p, *q;
|
1996-12-14 06:20:03 +00:00
|
|
|
char c = '?';
|
|
|
|
int done = 0;
|
|
|
|
int ind = 0;
|
|
|
|
int err = 0;
|
|
|
|
char s[10];
|
2015-03-01 22:32:23 +00:00
|
|
|
const char *newoptarg = NULL;
|
1996-12-14 06:20:03 +00:00
|
|
|
|
|
|
|
if ((p = *optptr) == NULL || *p == '\0') {
|
|
|
|
/* Current word is done, advance */
|
|
|
|
if (*optnext == NULL)
|
|
|
|
return 1;
|
|
|
|
p = **optnext;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (p == NULL || *p != '-' || *++p == '\0') {
|
|
|
|
atend:
|
1996-12-14 06:20:03 +00:00
|
|
|
ind = *optnext - optfirst + 1;
|
1996-12-21 13:21:57 +00:00
|
|
|
*optnext = NULL;
|
|
|
|
p = NULL;
|
1996-12-14 06:20:03 +00:00
|
|
|
done = 1;
|
|
|
|
goto out;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
1996-12-14 06:20:03 +00:00
|
|
|
(*optnext)++;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (p[0] == '-' && p[1] == '\0') /* check for "--" */
|
|
|
|
goto atend;
|
|
|
|
}
|
1996-12-14 06:20:03 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
c = *p++;
|
1996-12-14 06:20:03 +00:00
|
|
|
for (q = optstr; *q != c; ) {
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*q == '\0') {
|
1996-12-14 06:20:03 +00:00
|
|
|
if (optstr[0] == ':') {
|
|
|
|
s[0] = c;
|
|
|
|
s[1] = '\0';
|
2015-03-01 22:32:23 +00:00
|
|
|
newoptarg = s;
|
1996-12-14 06:20:03 +00:00
|
|
|
}
|
2014-05-10 19:18:49 +00:00
|
|
|
else
|
2014-05-09 13:32:36 +00:00
|
|
|
out2fmt_flush("Illegal option -%c\n", c);
|
1994-05-26 06:18:55 +00:00
|
|
|
c = '?';
|
2014-05-10 17:42:21 +00:00
|
|
|
goto out;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
if (*++q == ':')
|
|
|
|
q++;
|
|
|
|
}
|
1996-12-14 06:20:03 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*++q == ':') {
|
1996-12-14 06:20:03 +00:00
|
|
|
if (*p == '\0' && (p = **optnext) == NULL) {
|
|
|
|
if (optstr[0] == ':') {
|
|
|
|
s[0] = c;
|
|
|
|
s[1] = '\0';
|
2015-03-01 22:32:23 +00:00
|
|
|
newoptarg = s;
|
1996-12-14 06:20:03 +00:00
|
|
|
c = ':';
|
|
|
|
}
|
|
|
|
else {
|
2014-05-09 13:32:36 +00:00
|
|
|
out2fmt_flush("No arg for -%c option\n", c);
|
1996-12-14 06:20:03 +00:00
|
|
|
c = '?';
|
|
|
|
}
|
2014-05-10 17:42:21 +00:00
|
|
|
goto out;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
1996-12-14 06:20:03 +00:00
|
|
|
|
|
|
|
if (p == **optnext)
|
|
|
|
(*optnext)++;
|
2015-03-01 22:32:23 +00:00
|
|
|
newoptarg = p;
|
1994-05-26 06:18:55 +00:00
|
|
|
p = NULL;
|
|
|
|
}
|
1996-12-14 06:20:03 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
out:
|
2014-05-10 17:42:21 +00:00
|
|
|
if (*optnext != NULL)
|
|
|
|
ind = *optnext - optfirst + 1;
|
1996-12-14 06:20:03 +00:00
|
|
|
*optptr = p;
|
2015-03-01 22:32:23 +00:00
|
|
|
if (newoptarg != NULL)
|
|
|
|
err |= setvarsafe("OPTARG", newoptarg, 0);
|
2014-05-10 19:18:49 +00:00
|
|
|
else {
|
|
|
|
INTOFF;
|
|
|
|
err |= unsetvar("OPTARG");
|
|
|
|
INTON;
|
|
|
|
}
|
1996-12-14 06:20:03 +00:00
|
|
|
fmtstr(s, sizeof(s), "%d", ind);
|
|
|
|
err |= setvarsafe("OPTIND", s, VNOFUNC);
|
1994-05-26 06:18:55 +00:00
|
|
|
s[0] = c;
|
|
|
|
s[1] = '\0';
|
1996-12-14 06:20:03 +00:00
|
|
|
err |= setvarsafe(optvar, s, 0);
|
|
|
|
if (err) {
|
|
|
|
*optnext = NULL;
|
|
|
|
*optptr = NULL;
|
|
|
|
flushall();
|
|
|
|
exraise(EXERROR);
|
|
|
|
}
|
|
|
|
return done;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Standard option processing (a la getopt) for builtin routines. The
|
|
|
|
* only argument that is passed to nextopt is the option string; the
|
2017-05-06 13:28:42 +00:00
|
|
|
* other arguments are unnecessary. It returns the option, or '\0' on
|
1994-05-26 06:18:55 +00:00
|
|
|
* end of input.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2009-12-24 18:41:14 +00:00
|
|
|
nextopt(const char *optstring)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
2009-12-24 18:41:14 +00:00
|
|
|
char *p;
|
|
|
|
const char *q;
|
1994-05-26 06:18:55 +00:00
|
|
|
char c;
|
|
|
|
|
2009-12-27 18:04:05 +00:00
|
|
|
if ((p = nextopt_optptr) == NULL || *p == '\0') {
|
1994-05-26 06:18:55 +00:00
|
|
|
p = *argptr;
|
|
|
|
if (p == NULL || *p != '-' || *++p == '\0')
|
|
|
|
return '\0';
|
|
|
|
argptr++;
|
|
|
|
if (p[0] == '-' && p[1] == '\0') /* check for "--" */
|
|
|
|
return '\0';
|
|
|
|
}
|
|
|
|
c = *p++;
|
|
|
|
for (q = optstring ; *q != c ; ) {
|
|
|
|
if (*q == '\0')
|
|
|
|
error("Illegal option -%c", c);
|
|
|
|
if (*++q == ':')
|
|
|
|
q++;
|
|
|
|
}
|
|
|
|
if (*++q == ':') {
|
|
|
|
if (*p == '\0' && (p = *argptr++) == NULL)
|
|
|
|
error("No arg for -%c option", c);
|
2000-04-20 09:49:16 +00:00
|
|
|
shoptarg = p;
|
1994-05-26 06:18:55 +00:00
|
|
|
p = NULL;
|
|
|
|
}
|
2009-12-27 18:04:05 +00:00
|
|
|
nextopt_optptr = p;
|
1994-05-26 06:18:55 +00:00
|
|
|
return c;
|
|
|
|
}
|