PR bin/56464
After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...) All other ash descendants seem to have fixed this long ago.
While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges. Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.
We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).
One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.
XXX pullup -9 (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).
2021-10-26 03:05:38 +03:00
|
|
|
/* $NetBSD: options.c,v 1.56 2021/10/26 00:05:38 kre Exp $ */
|
1995-03-21 12:01:59 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
1994-05-11 21:09:42 +04:00
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* 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.
|
2003-08-07 13:05:01 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03: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.
|
|
|
|
*/
|
|
|
|
|
1997-07-05 01:01:48 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-03-21 12:01:59 +03:00
|
|
|
#if 0
|
1995-05-12 01:28:33 +04:00
|
|
|
static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95";
|
1995-03-21 12:01:59 +03:00
|
|
|
#else
|
PR bin/56464
After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...) All other ash descendants seem to have fixed this long ago.
While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges. Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.
We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).
One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.
XXX pullup -9 (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).
2021-10-26 03:05:38 +03:00
|
|
|
__RCSID("$NetBSD: options.c,v 1.56 2021/10/26 00:05:38 kre Exp $");
|
1995-03-21 12:01:59 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1995-05-12 01:28:33 +04:00
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "shell.h"
|
|
|
|
#define DEFINE_OPTIONS
|
|
|
|
#include "options.h"
|
|
|
|
#undef DEFINE_OPTIONS
|
2011-06-19 01:18:46 +04:00
|
|
|
#include "builtins.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
#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"
|
2018-07-14 01:43:44 +03:00
|
|
|
#include "syntax.h"
|
1997-03-14 04:42:18 +03:00
|
|
|
#ifndef SMALL
|
1995-05-12 01:28:33 +04:00
|
|
|
#include "myhistedit.h"
|
|
|
|
#endif
|
2002-11-25 01:35:38 +03:00
|
|
|
#include "show.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
char *arg0; /* value of $0 */
|
|
|
|
struct shparam shellparam; /* current positional parameters */
|
|
|
|
char **argptr; /* argument list for builtin commands */
|
2001-02-04 22:52:06 +03:00
|
|
|
char *optionarg; /* set by nextopt (like getopt) */
|
1993-03-21 12:45:37 +03:00
|
|
|
char *optptr; /* used by nextopt */
|
|
|
|
|
|
|
|
char *minusc; /* argument to -c option */
|
|
|
|
|
|
|
|
|
2002-11-25 01:35:38 +03:00
|
|
|
STATIC void options(int);
|
|
|
|
STATIC void minus_o(char *, int);
|
|
|
|
STATIC void setoption(int, int);
|
|
|
|
STATIC int getopts(char *, char *, char **, char ***, char **);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process the shell command line arguments.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
procargs(int argc, char **argv)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
2009-01-18 03:24:29 +03:00
|
|
|
size_t i;
|
Command line, and "set" command options processing cleanup.
sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.
In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).
Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...
This causes some of the commented out text from sh.1 to become uncommented.
2017-05-18 16:53:18 +03:00
|
|
|
int psx;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
argptr = argv;
|
|
|
|
if (argc > 0)
|
|
|
|
argptr++;
|
Command line, and "set" command options processing cleanup.
sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.
In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).
Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...
This causes some of the commented out text from sh.1 to become uncommented.
2017-05-18 16:53:18 +03:00
|
|
|
|
|
|
|
psx = posix; /* save what we set it to earlier */
|
|
|
|
/*
|
|
|
|
* option values are mostly boolean 0:off 1:on
|
|
|
|
* we use 2 (just in this routine) to mean "unknown yet"
|
|
|
|
*/
|
1994-05-11 21:09:42 +04:00
|
|
|
for (i = 0; i < NOPTS; i++)
|
|
|
|
optlist[i].val = 2;
|
Command line, and "set" command options processing cleanup.
sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.
In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).
Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...
This causes some of the commented out text from sh.1 to become uncommented.
2017-05-18 16:53:18 +03:00
|
|
|
posix = psx; /* restore before processing -o ... */
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
options(1);
|
Command line, and "set" command options processing cleanup.
sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.
In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).
Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...
This causes some of the commented out text from sh.1 to become uncommented.
2017-05-18 16:53:18 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*argptr == NULL && minusc == NULL)
|
|
|
|
sflag = 1;
|
2017-07-24 15:35:37 +03:00
|
|
|
if (iflag == 2 && sflag == 1 && isatty(0) && isatty(2))
|
1993-03-21 12:45:37 +03:00
|
|
|
iflag = 1;
|
2005-07-15 21:46:54 +04:00
|
|
|
if (iflag == 1 && sflag == 2)
|
|
|
|
iflag = 2;
|
1994-05-11 21:09:42 +04:00
|
|
|
if (mflag == 2)
|
|
|
|
mflag = iflag;
|
2016-02-24 17:38:40 +03:00
|
|
|
#ifndef DO_SHAREDVFORK
|
|
|
|
if (usefork == 2)
|
|
|
|
usefork = 1;
|
|
|
|
#endif
|
2017-05-29 17:03:23 +03:00
|
|
|
#if DEBUG >= 2
|
Command line, and "set" command options processing cleanup.
sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.
In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).
Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...
This causes some of the commented out text from sh.1 to become uncommented.
2017-05-18 16:53:18 +03:00
|
|
|
if (debug == 2)
|
|
|
|
debug = 1;
|
2002-11-25 01:35:38 +03:00
|
|
|
#endif
|
Command line, and "set" command options processing cleanup.
sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.
In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).
Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...
This causes some of the commented out text from sh.1 to become uncommented.
2017-05-18 16:53:18 +03:00
|
|
|
/*
|
|
|
|
* Any options not dealt with as special cases just above,
|
|
|
|
* and which were not set on the command line, are set to
|
|
|
|
* their expected default values (mostly "off")
|
|
|
|
*
|
|
|
|
* then as each option is initialised, save its setting now
|
|
|
|
* as its "default" value for future use ("set -o default").
|
|
|
|
*/
|
|
|
|
for (i = 0; i < NOPTS; i++) {
|
|
|
|
if (optlist[i].val == 2)
|
|
|
|
optlist[i].val = optlist[i].dflt;
|
|
|
|
optlist[i].dflt = optlist[i].val;
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
arg0 = argv[0];
|
|
|
|
if (sflag == 0 && minusc == NULL) {
|
2001-02-26 16:06:43 +03:00
|
|
|
commandname = argv[0];
|
|
|
|
arg0 = *argptr++;
|
|
|
|
setinputfile(arg0, 0);
|
|
|
|
commandname = arg0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1996-10-16 18:49:56 +04:00
|
|
|
/* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
|
2003-01-22 23:36:03 +03:00
|
|
|
if (minusc != NULL) {
|
|
|
|
if (argptr == NULL || *argptr == NULL)
|
|
|
|
error("Bad -c option");
|
|
|
|
minusc = *argptr++;
|
|
|
|
if (*argptr != 0)
|
|
|
|
arg0 = *argptr++;
|
|
|
|
}
|
1996-10-16 18:49:56 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
shellparam.p = argptr;
|
1996-11-06 04:17:10 +03:00
|
|
|
shellparam.reset = 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
/* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
|
|
|
|
while (*argptr) {
|
|
|
|
shellparam.nparam++;
|
|
|
|
argptr++;
|
|
|
|
}
|
1994-05-11 21:09:42 +04:00
|
|
|
optschanged();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-12-04 10:11:37 +03:00
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
optschanged(void)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1994-05-11 21:09:42 +04:00
|
|
|
setinteractive(iflag);
|
1997-03-14 04:42:18 +03:00
|
|
|
#ifndef SMALL
|
1994-05-11 21:09:42 +04:00
|
|
|
histedit();
|
1994-05-15 02:20:04 +04:00
|
|
|
#endif
|
1994-05-11 21:09:42 +04:00
|
|
|
setjobctl(mflag);
|
PR bin/56464
After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...) All other ash descendants seem to have fixed this long ago.
While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges. Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.
We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).
One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.
XXX pullup -9 (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).
2021-10-26 03:05:38 +03:00
|
|
|
|
|
|
|
if (privileged && !pflag) {
|
|
|
|
setuid(getuid());
|
|
|
|
setgid(getgid());
|
|
|
|
privileged = 0;
|
|
|
|
setvarsafe("PSc", (getuid() == 0 ? "#" : "$"), 0);
|
|
|
|
}
|
1994-05-11 21:09:42 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Process shell options. The global variable argptr contains a pointer
|
|
|
|
* to the argument list; we advance it past the options.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
options(int cmdline)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
2004-10-30 23:29:27 +04:00
|
|
|
static char empty[] = "";
|
1997-01-11 05:04:27 +03:00
|
|
|
char *p;
|
1993-03-21 12:45:37 +03:00
|
|
|
int val;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (cmdline)
|
|
|
|
minusc = NULL;
|
|
|
|
while ((p = *argptr) != NULL) {
|
|
|
|
argptr++;
|
|
|
|
if ((c = *p++) == '-') {
|
|
|
|
val = 1;
|
1995-05-12 01:28:33 +04:00
|
|
|
if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!cmdline) {
|
|
|
|
/* "-" means turn off -x and -v */
|
|
|
|
if (p[0] == '\0')
|
|
|
|
xflag = vflag = 0;
|
|
|
|
/* "--" means reset params */
|
1995-03-26 21:25:10 +04:00
|
|
|
else if (*argptr == NULL)
|
|
|
|
setparam(argptr);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
break; /* "-" or "--" terminates options */
|
|
|
|
}
|
|
|
|
} else if (c == '+') {
|
|
|
|
val = 0;
|
|
|
|
} else {
|
|
|
|
argptr--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while ((c = *p++) != '\0') {
|
Command line, and "set" command options processing cleanup.
sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.
In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).
Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...
This causes some of the commented out text from sh.1 to become uncommented.
2017-05-18 16:53:18 +03:00
|
|
|
if (val == 1 && c == 'c' && cmdline) {
|
2004-10-30 23:29:27 +04:00
|
|
|
/* command is after shell args*/
|
|
|
|
minusc = empty;
|
1994-05-11 21:09:42 +04:00
|
|
|
} else if (c == 'o') {
|
Command line, and "set" command options processing cleanup.
sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.
In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).
Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...
This causes some of the commented out text from sh.1 to become uncommented.
2017-05-18 16:53:18 +03:00
|
|
|
if (*p != '\0')
|
|
|
|
minus_o(p, val + (cmdline ? val : 0));
|
|
|
|
else if (*argptr)
|
|
|
|
minus_o(*argptr++,
|
|
|
|
val + (cmdline ? val : 0));
|
|
|
|
else if (!cmdline)
|
|
|
|
minus_o(NULL, val);
|
|
|
|
else
|
|
|
|
error("arg for %co missing", "+-"[val]);
|
|
|
|
break;
|
2017-05-15 23:00:36 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
} else if (c == 'D') {
|
|
|
|
if (*p) {
|
|
|
|
set_debug(p, val);
|
|
|
|
break;
|
|
|
|
} else if (*argptr)
|
|
|
|
set_debug(*argptr++, val);
|
|
|
|
else
|
|
|
|
set_debug("*$", val);
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
|
|
|
setoption(c, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-25 01:35:38 +03:00
|
|
|
static void
|
2009-01-18 03:24:29 +03:00
|
|
|
set_opt_val(size_t i, int val)
|
2002-11-25 01:35:38 +03:00
|
|
|
{
|
2009-01-18 03:24:29 +03:00
|
|
|
size_t j;
|
2002-11-25 01:35:38 +03:00
|
|
|
int flag;
|
|
|
|
|
|
|
|
if (val && (flag = optlist[i].opt_set)) {
|
|
|
|
/* some options (eg vi/emacs) are mutually exclusive */
|
|
|
|
for (j = 0; j < NOPTS; j++)
|
|
|
|
if (optlist[j].opt_set == flag)
|
|
|
|
optlist[j].val = 0;
|
|
|
|
}
|
2017-11-21 06:42:39 +03:00
|
|
|
#ifndef SMALL
|
2017-11-19 06:23:01 +03:00
|
|
|
if (i == _SH_OPT_Xflag)
|
|
|
|
xtracefdsetup(val);
|
2017-11-21 06:42:39 +03:00
|
|
|
#endif
|
2002-11-25 01:35:38 +03:00
|
|
|
optlist[i].val = val;
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (&optlist[i].val == &debug)
|
2017-11-19 06:23:01 +03:00
|
|
|
opentrace(); /* different "trace" than the -x one... */
|
2002-11-25 01:35:38 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1994-05-11 21:09:42 +04:00
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
minus_o(char *name, int val)
|
1994-05-11 21:09:42 +04:00
|
|
|
{
|
2009-01-18 03:24:29 +03:00
|
|
|
size_t i;
|
2016-03-31 19:16:35 +03:00
|
|
|
const char *sep = ": ";
|
1994-05-11 21:09:42 +04:00
|
|
|
|
|
|
|
if (name == NULL) {
|
2005-12-13 20:44:18 +03:00
|
|
|
if (val) {
|
2016-03-31 19:16:35 +03:00
|
|
|
out1str("Current option settings");
|
2005-12-13 20:44:18 +03:00
|
|
|
for (i = 0; i < NOPTS; i++) {
|
2016-03-31 19:16:35 +03:00
|
|
|
if (optlist[i].name == NULL) {
|
|
|
|
out1fmt("%s%c%c", sep,
|
|
|
|
"+-"[optlist[i].val],
|
|
|
|
optlist[i].letter);
|
|
|
|
sep = ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out1c('\n');
|
|
|
|
for (i = 0; i < NOPTS; i++) {
|
|
|
|
if (optlist[i].name)
|
Command line, and "set" command options processing cleanup.
sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.
In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).
Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...
This causes some of the commented out text from sh.1 to become uncommented.
2017-05-18 16:53:18 +03:00
|
|
|
out1fmt("%-19s %s\n", optlist[i].name,
|
2005-12-13 20:44:18 +03:00
|
|
|
optlist[i].val ? "on" : "off");
|
|
|
|
}
|
|
|
|
} else {
|
Command line, and "set" command options processing cleanup.
sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.
In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).
Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...
This causes some of the commented out text from sh.1 to become uncommented.
2017-05-18 16:53:18 +03:00
|
|
|
out1str("set -o default");
|
2005-12-13 20:44:18 +03:00
|
|
|
for (i = 0; i < NOPTS; i++) {
|
Command line, and "set" command options processing cleanup.
sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.
In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).
Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...
This causes some of the commented out text from sh.1 to become uncommented.
2017-05-18 16:53:18 +03:00
|
|
|
if (optlist[i].val == optlist[i].dflt)
|
|
|
|
continue;
|
2016-03-31 19:16:35 +03:00
|
|
|
if (optlist[i].name)
|
|
|
|
out1fmt(" %co %s",
|
2005-12-13 20:44:18 +03:00
|
|
|
"+-"[optlist[i].val], optlist[i].name);
|
2016-03-31 19:16:35 +03:00
|
|
|
else
|
|
|
|
out1fmt(" %c%c", "+-"[optlist[i].val],
|
|
|
|
optlist[i].letter);
|
2005-12-13 20:44:18 +03:00
|
|
|
}
|
2016-03-31 19:16:35 +03:00
|
|
|
out1c('\n');
|
2005-12-13 20:44:18 +03:00
|
|
|
}
|
1994-05-11 21:09:42 +04:00
|
|
|
} else {
|
Command line, and "set" command options processing cleanup.
sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.
In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).
Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...
This causes some of the commented out text from sh.1 to become uncommented.
2017-05-18 16:53:18 +03:00
|
|
|
if (val == 1 && equal(name, "default")) { /* special case */
|
|
|
|
for (i = 0; i < NOPTS; i++)
|
|
|
|
set_opt_val(i, optlist[i].dflt);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (val)
|
|
|
|
val = 1;
|
1994-05-11 21:09:42 +04:00
|
|
|
for (i = 0; i < NOPTS; i++)
|
2016-03-31 19:16:35 +03:00
|
|
|
if (optlist[i].name && equal(name, optlist[i].name)) {
|
2002-11-25 01:35:38 +03:00
|
|
|
set_opt_val(i, val);
|
2017-11-21 06:42:39 +03:00
|
|
|
#ifndef SMALL
|
2017-11-19 06:23:01 +03:00
|
|
|
if (i == _SH_OPT_Xflag)
|
|
|
|
set_opt_val(_SH_OPT_xflag, val);
|
2017-11-21 06:42:39 +03:00
|
|
|
#endif
|
1994-05-11 21:09:42 +04:00
|
|
|
return;
|
|
|
|
}
|
Command line, and "set" command options processing cleanup.
sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.
In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).
Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...
This causes some of the commented out text from sh.1 to become uncommented.
2017-05-18 16:53:18 +03:00
|
|
|
error("Illegal option %co %s", "+-"[val], name);
|
1994-05-11 21:09:42 +04:00
|
|
|
}
|
|
|
|
}
|
1996-10-16 18:49:56 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
setoption(int flag, int val)
|
|
|
|
{
|
2009-01-18 03:24:29 +03:00
|
|
|
size_t i;
|
1994-05-11 21:09:42 +04:00
|
|
|
|
|
|
|
for (i = 0; i < NOPTS; i++)
|
|
|
|
if (optlist[i].letter == flag) {
|
2017-11-19 06:23:01 +03:00
|
|
|
set_opt_val(i, val);
|
2017-11-21 06:42:39 +03:00
|
|
|
#ifndef SMALL
|
2017-11-19 06:23:01 +03:00
|
|
|
if (i == _SH_OPT_Xflag)
|
|
|
|
set_opt_val(_SH_OPT_xflag, val);
|
2017-11-21 06:42:39 +03:00
|
|
|
#endif
|
1994-05-11 21:09:42 +04:00
|
|
|
return;
|
|
|
|
}
|
Command line, and "set" command options processing cleanup.
sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.
In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).
Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...
This causes some of the commented out text from sh.1 to become uncommented.
2017-05-18 16:53:18 +03:00
|
|
|
error("Illegal option %c%c", "+-"[val], flag);
|
1998-07-28 15:41:40 +04:00
|
|
|
/* NOTREACHED */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef mkinit
|
|
|
|
INCLUDE "options.h"
|
|
|
|
|
|
|
|
SHELLPROC {
|
1994-05-11 21:09:42 +04:00
|
|
|
int i;
|
|
|
|
|
2002-11-25 01:35:38 +03:00
|
|
|
for (i = 0; optlist[i].name; i++)
|
1994-05-11 21:09:42 +04:00
|
|
|
optlist[i].val = 0;
|
|
|
|
optschanged();
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the shell parameters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
setparam(char **argv)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
char **newparam;
|
|
|
|
char **ap;
|
|
|
|
int nparam;
|
|
|
|
|
2005-03-21 00:38:17 +03:00
|
|
|
for (nparam = 0 ; argv[nparam] ; nparam++)
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
|
|
|
|
while (*argv) {
|
|
|
|
*ap++ = savestr(*argv++);
|
|
|
|
}
|
|
|
|
*ap = NULL;
|
|
|
|
freeparam(&shellparam);
|
|
|
|
shellparam.malloc = 1;
|
|
|
|
shellparam.nparam = nparam;
|
|
|
|
shellparam.p = newparam;
|
|
|
|
shellparam.optnext = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the list of positional parameters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
freeparam(volatile struct shparam *param)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
char **ap;
|
|
|
|
|
|
|
|
if (param->malloc) {
|
|
|
|
for (ap = param->p ; *ap ; ap++)
|
|
|
|
ckfree(*ap);
|
|
|
|
ckfree(param->p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The shift builtin command.
|
|
|
|
*/
|
|
|
|
|
1994-12-05 22:07:32 +03:00
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
shiftcmd(int argc, char **argv)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
int n;
|
|
|
|
char **ap1, **ap2;
|
|
|
|
|
2016-03-08 17:08:39 +03:00
|
|
|
if (argc > 2)
|
|
|
|
error("Usage: shift [n]");
|
1993-03-21 12:45:37 +03:00
|
|
|
n = 1;
|
|
|
|
if (argc > 1)
|
|
|
|
n = number(argv[1]);
|
|
|
|
if (n > shellparam.nparam)
|
1994-05-11 21:09:42 +04:00
|
|
|
error("can't shift that many");
|
1993-03-21 12:45:37 +03:00
|
|
|
INTOFF;
|
|
|
|
shellparam.nparam -= n;
|
|
|
|
for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
|
|
|
|
if (shellparam.malloc)
|
|
|
|
ckfree(*ap1);
|
|
|
|
}
|
|
|
|
ap2 = shellparam.p;
|
2016-03-08 17:08:39 +03:00
|
|
|
while ((*ap2++ = *ap1++) != NULL)
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
shellparam.optnext = NULL;
|
|
|
|
INTON;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The set command builtin.
|
|
|
|
*/
|
|
|
|
|
1994-12-05 22:07:32 +03:00
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
setcmd(int argc, char **argv)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
if (argc == 1)
|
2016-03-31 19:16:35 +03:00
|
|
|
return showvars(0, 0, 1, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
INTOFF;
|
|
|
|
options(0);
|
1994-05-11 21:09:42 +04:00
|
|
|
optschanged();
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*argptr != NULL) {
|
|
|
|
setparam(argptr);
|
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-06-25 20:47:43 +04:00
|
|
|
void
|
2012-03-20 22:42:28 +04:00
|
|
|
getoptsreset(const char *value)
|
1996-06-25 20:47:43 +04:00
|
|
|
{
|
2018-07-14 01:43:44 +03:00
|
|
|
/*
|
|
|
|
* This is just to detect the case where OPTIND=1
|
|
|
|
* is executed. Any other string assigned to OPTIND
|
|
|
|
* is OK, but is not a reset. No errors, so cannot use number()
|
|
|
|
*/
|
|
|
|
if (is_digit(*value) && strtol(value, NULL, 10) == 1) {
|
1996-11-02 21:26:03 +03:00
|
|
|
shellparam.optnext = NULL;
|
1996-11-06 04:17:10 +03:00
|
|
|
shellparam.reset = 1;
|
|
|
|
}
|
1996-06-25 20:47:43 +04:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03: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.
|
|
|
|
*/
|
|
|
|
|
1994-12-05 22:07:32 +03:00
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
getoptscmd(int argc, char **argv)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1996-06-05 01:00:09 +04:00
|
|
|
char **optbase;
|
|
|
|
|
|
|
|
if (argc < 3)
|
2004-01-06 02:23:32 +03:00
|
|
|
error("usage: getopts optstring var [arg]");
|
1996-06-05 01:00:09 +04:00
|
|
|
else if (argc == 3)
|
|
|
|
optbase = shellparam.p;
|
1996-10-16 18:49:56 +04:00
|
|
|
else
|
1996-06-05 01:00:09 +04:00
|
|
|
optbase = &argv[3];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1996-11-06 04:17:10 +03:00
|
|
|
if (shellparam.reset == 1) {
|
1996-06-05 01:00:09 +04:00
|
|
|
shellparam.optnext = optbase;
|
1993-03-21 12:45:37 +03:00
|
|
|
shellparam.optptr = NULL;
|
1996-11-06 04:17:10 +03:00
|
|
|
shellparam.reset = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1996-06-05 01:00:09 +04:00
|
|
|
|
|
|
|
return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
|
|
|
|
&shellparam.optptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int
|
2002-11-25 01:35:38 +03:00
|
|
|
getopts(char *optstr, char *optvar, char **optfirst, char ***optnext, char **optpptr)
|
1996-06-05 01:00:09 +04:00
|
|
|
{
|
1997-01-11 05:04:27 +03:00
|
|
|
char *p, *q;
|
1996-06-05 01:00:09 +04:00
|
|
|
char c = '?';
|
|
|
|
int done = 0;
|
|
|
|
int ind = 0;
|
1996-06-25 20:47:43 +04:00
|
|
|
int err = 0;
|
2003-04-15 11:40:56 +04:00
|
|
|
char s[12];
|
1996-06-05 01:00:09 +04:00
|
|
|
|
1999-07-09 07:05:49 +04:00
|
|
|
if ((p = *optpptr) == NULL || *p == '\0') {
|
1996-06-05 01:00:09 +04:00
|
|
|
/* Current word is done, advance */
|
1996-11-06 04:17:10 +03:00
|
|
|
if (*optnext == NULL)
|
|
|
|
return 1;
|
1996-06-05 01:00:09 +04:00
|
|
|
p = **optnext;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (p == NULL || *p != '-' || *++p == '\0') {
|
|
|
|
atend:
|
1996-06-05 01:00:09 +04:00
|
|
|
ind = *optnext - optfirst + 1;
|
1996-12-15 23:37:09 +03:00
|
|
|
*optnext = NULL;
|
|
|
|
p = NULL;
|
1996-06-05 01:00:09 +04:00
|
|
|
done = 1;
|
|
|
|
goto out;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1996-06-05 01:00:09 +04:00
|
|
|
(*optnext)++;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (p[0] == '-' && p[1] == '\0') /* check for "--" */
|
|
|
|
goto atend;
|
|
|
|
}
|
1996-06-05 01:00:09 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
c = *p++;
|
1996-06-05 01:00:09 +04:00
|
|
|
for (q = optstr; *q != c; ) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*q == '\0') {
|
1996-06-05 01:00:09 +04:00
|
|
|
if (optstr[0] == ':') {
|
|
|
|
s[0] = c;
|
|
|
|
s[1] = '\0';
|
1996-06-25 20:47:43 +04:00
|
|
|
err |= setvarsafe("OPTARG", s, 0);
|
2002-11-25 01:35:38 +03:00
|
|
|
} else {
|
1998-05-02 22:04:09 +04:00
|
|
|
outfmt(&errout, "Illegal option -%c\n", c);
|
2002-11-25 01:35:38 +03:00
|
|
|
(void) unsetvar("OPTARG", 0);
|
1996-06-05 01:00:09 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
c = '?';
|
1996-06-25 20:47:43 +04:00
|
|
|
goto bad;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (*++q == ':')
|
|
|
|
q++;
|
|
|
|
}
|
1996-06-05 01:00:09 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*++q == ':') {
|
1996-06-05 01:00:09 +04:00
|
|
|
if (*p == '\0' && (p = **optnext) == NULL) {
|
|
|
|
if (optstr[0] == ':') {
|
|
|
|
s[0] = c;
|
|
|
|
s[1] = '\0';
|
1996-06-25 20:47:43 +04:00
|
|
|
err |= setvarsafe("OPTARG", s, 0);
|
1996-06-05 01:00:09 +04:00
|
|
|
c = ':';
|
2002-11-25 01:35:38 +03:00
|
|
|
} else {
|
1998-05-02 22:04:09 +04:00
|
|
|
outfmt(&errout, "No arg for -%c option\n", c);
|
2002-11-25 01:35:38 +03:00
|
|
|
(void) unsetvar("OPTARG", 0);
|
1996-06-05 01:00:09 +04:00
|
|
|
c = '?';
|
|
|
|
}
|
1996-06-25 20:47:43 +04:00
|
|
|
goto bad;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1996-06-05 01:00:09 +04:00
|
|
|
|
|
|
|
if (p == **optnext)
|
|
|
|
(*optnext)++;
|
2002-11-25 01:35:38 +03:00
|
|
|
err |= setvarsafe("OPTARG", p, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
p = NULL;
|
2002-11-25 01:35:38 +03:00
|
|
|
} else
|
|
|
|
err |= setvarsafe("OPTARG", "", 0);
|
1996-11-06 04:17:10 +03:00
|
|
|
ind = *optnext - optfirst + 1;
|
|
|
|
goto out;
|
|
|
|
|
1996-06-25 20:47:43 +04:00
|
|
|
bad:
|
|
|
|
ind = 1;
|
|
|
|
*optnext = NULL;
|
|
|
|
p = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
out:
|
1999-07-09 07:05:49 +04:00
|
|
|
*optpptr = p;
|
1996-06-05 01:00:09 +04:00
|
|
|
fmtstr(s, sizeof(s), "%d", ind);
|
1996-06-25 20:47:43 +04:00
|
|
|
err |= setvarsafe("OPTIND", s, VNOFUNC);
|
1993-03-21 12:45:37 +03:00
|
|
|
s[0] = c;
|
|
|
|
s[1] = '\0';
|
1996-06-25 20:47:43 +04:00
|
|
|
err |= setvarsafe(optvar, s, 0);
|
|
|
|
if (err) {
|
|
|
|
*optnext = NULL;
|
1999-07-09 07:05:49 +04:00
|
|
|
*optpptr = NULL;
|
1996-06-25 20:47:43 +04:00
|
|
|
flushall();
|
|
|
|
exraise(EXERROR);
|
|
|
|
}
|
1996-06-05 01:00:09 +04:00
|
|
|
return done;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-05-11 21:09:42 +04:00
|
|
|
* XXX - should get rid of. have all builtins use getopt(3). the
|
|
|
|
* library getopt must have the BSD extension static variable "optreset"
|
|
|
|
* otherwise it can't be used within the shell safely.
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Standard option processing (a la getopt) for builtin routines. The
|
|
|
|
* only argument that is passed to nextopt is the option string; the
|
|
|
|
* other arguments are unnecessary. It return the character, or '\0' on
|
2020-02-04 19:06:59 +03:00
|
|
|
* end of input. If optstring is NULL, then there are no options, and
|
|
|
|
* args are allowed to begin with '-', but a single leading "--" will be
|
|
|
|
* discarded. This is for some POSIX special builtins that require
|
|
|
|
* -- processing, have no args, and we never did opt processing before
|
|
|
|
* and need to retain backwards compat.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
nextopt(const char *optstring)
|
|
|
|
{
|
1999-07-09 07:05:49 +04:00
|
|
|
char *p;
|
|
|
|
const char *q;
|
1993-03-21 12:45:37 +03:00
|
|
|
char c;
|
|
|
|
|
|
|
|
if ((p = optptr) == NULL || *p == '\0') {
|
|
|
|
p = *argptr;
|
|
|
|
if (p == NULL || *p != '-' || *++p == '\0')
|
|
|
|
return '\0';
|
|
|
|
argptr++;
|
|
|
|
if (p[0] == '-' && p[1] == '\0') /* check for "--" */
|
|
|
|
return '\0';
|
2020-02-05 17:56:25 +03:00
|
|
|
if (optstring == NULL) /* not processing the "option" */
|
|
|
|
argptr--; /* so make it be an arg again */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2020-02-04 19:06:59 +03:00
|
|
|
if (optstring == NULL)
|
|
|
|
return '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
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);
|
2001-02-04 22:52:06 +03:00
|
|
|
optionarg = p;
|
1993-03-21 12:45:37 +03:00
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
optptr = p;
|
|
|
|
return c;
|
|
|
|
}
|