/* vi: set sw=4 ts=4: */
/*
* A prototype Bourne shell grammar parser.
* Intended to follow the original Thompson and Ritchie
* "small and simple is beautiful" philosophy, which
* incidentally is a good match to today's BusyBox.
*
* Copyright (C) 2000,2001 Larry Doolittle <larry@doolittle.boa.org>
* Copyright (C) 2008,2009 Denys Vlasenko <vda.linux@googlemail.com>
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*
* Credits:
* The parser routines proper are all original material, first
* written Dec 2000 and Jan 2001 by Larry Doolittle. The
* execution engine, the builtins, and much of the underlying
* support has been adapted from busybox-0.49pre's lash, which is
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
* written by Erik Andersen <andersen@codepoet.org>. That, in turn,
* is based in part on ladsh.c, by Michael K. Johnson and Erik W.
* Troan, which they placed in the public domain. I don't know
* how much of the Johnson/Troan code has survived the repeated
* rewrites.
*
* Other credits:
* o_addchr derived from similar w_addchar function in glibc-2.2.
* parse_redirect, redirect_opt_num, and big chunks of main
* and many builtins derived from contributions by Erik Andersen.
* Miscellaneous bugfixes from Matt Kraai.
*
* There are two big (and related) architecture differences between
* this parser and the lash parser. One is that this version is
* actually designed from the ground up to understand nearly all
* of the Bourne grammar. The second, consequential change is that
* the parser and input reader have been turned inside out. Now,
* the parser is in control, and asks for input as needed. The old
* way had the input reader in control, and it asked for parsing to
* take place as needed. The new way makes it much easier to properly
* handle the recursion implicit in the various substitutions, especially
* across continuation lines.
*
* TODOs:
* grep for "TODO" and fix (some of them are easy)
* special variables (done: PWD, PPID, RANDOM)
* tilde expansion
* aliases
* kill %jobspec
* follow IFS rules more precisely, including update semantics
* builtins mandated by standards we don't support:
* [un]alias, command, fc, getopts, newgrp, readonly, times
* make complex ${var%...} constructs support optional
* make here documents optional
*
* Bash compat TODO:
* redirection of stdout+stderr: &> and >&
* reserved words: function select
* advanced test: [[ ]]
* process substitution: <(list) and >(list)
* =~: regex operator
* let EXPR [EXPR...]
* Each EXPR is an arithmetic expression (ARITHMETIC EVALUATION)
* If the last arg evaluates to 0, let returns 1; 0 otherwise.
* NB: let `echo 'a=a + 1'` - error (IOW: multi-word expansion is used)
* ((EXPR))
* The EXPR is evaluated according to ARITHMETIC EVALUATION.
* This is exactly equivalent to let "EXPR".
* $[EXPR]: synonym for $((EXPR))
*
* Won't do:
* In bash, export builtin is special, its arguments are assignments
* and therefore expansion of them should be "one-word" expansion:
* $ export i=`echo 'a b'` # export has one arg: "i=a b"
* compare with:
* $ ls i=`echo 'a b'` # ls has two args: "i=a" and "b"
* ls: cannot access i=a: No such file or directory
* ls: cannot access b: No such file or directory
* Note1: same applies to local builtin.
* Note2: bash 3.2.33(1) does this only if export word itself
* is not quoted:
* $ export i=`echo 'aaa bbb'`; echo "$i"
* aaa bbb
* $ "export" i=`echo 'aaa bbb'`; echo "$i"
* aaa
*/
#if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
|| defined(__APPLE__) \
)
# include <malloc.h> /* for malloc_trim */
#endif
#include <glob.h>
/* #include <dmalloc.h> */
#if ENABLE_HUSH_CASE
# include <fnmatch.h>
#endif
#include <sys/utsname.h> /* for setting $HOSTNAME */
#include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */
#include "unicode.h"
#include "shell_common.h"
#include "math.h"
#include "match.h"
#if ENABLE_HUSH_RANDOM_SUPPORT
# include "random.h"
#else
# define CLEAR_RANDOM_T(rnd) ((void)0)
#endif
#ifndef F_DUPFD_CLOEXEC
# define F_DUPFD_CLOEXEC F_DUPFD
#endif
#ifndef PIPE_BUF
# define PIPE_BUF 4096 /* amount of buffering in a pipe */
#endif
//config:config HUSH
//config: bool "hush"
//config: default y
//config: help
//config: hush is a small shell (25k). It handles the normal flow control
//config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops,
//config: case/esac. Redirections, here documents, $((arithmetic))
//config: and functions are supported.
//config:
//config: It will compile and work on no-mmu systems.
//config:
//config: It does not handle select, aliases, tilde expansion,
//config: &>file and >&file redirection of stdout+stderr.
//config:
//config:config HUSH_BASH_COMPAT
//config: bool "bash-compatible extensions"
//config: default y
//config: depends on HUSH
//config: help
//config: Enable bash-compatible extensions.
//config:
//config:config HUSH_BRACE_EXPANSION
//config: bool "Brace expansion"
//config: default y
//config: depends on HUSH_BASH_COMPAT
//config: help
//config: Enable {abc,def} extension.
//config:
//config:config HUSH_HELP
//config: bool "help builtin"
//config: default y
//config: depends on HUSH
//config: help
//config: Enable help builtin in hush. Code size + ~1 kbyte.
//config:
//config:config HUSH_INTERACTIVE
//config: bool "Interactive mode"
//config: default y
//config: depends on HUSH
//config: help
//config: Enable interactive mode (prompt and command editing).
//config: Without this, hush simply reads and executes commands
//config: from stdin just like a shell script from a file.
//config: No prompt, no PS1/PS2 magic shell variables.
//config:
//config:config HUSH_SAVEHISTORY
//config: bool "Save command history to .hush_history"
//config: default y
//config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY
//config: help
//config: Enable history saving in hush.
//config:
//config:config HUSH_JOB
//config: bool "Job control"
//config: default y
//config: depends on HUSH_INTERACTIVE
//config: help
//config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current
//config: command (not entire shell), fg/bg builtins work. Without this option,
//config: "cmd &" still works by simply spawning a process and immediately
//config: prompting for next command (or executing next command in a script),
//config: but no separate process group is formed.
//config:
//config:config HUSH_TICK
//config: bool "Process substitution"
//config: default y
//config: depends on HUSH
//config: help
//config: Enable process substitution `command` and $(command) in hush.
//config:
//config:config HUSH_IF
//config: bool "Support if/then/elif/else/fi"
//config: default y
//config: depends on HUSH
//config: help
//config: Enable if/then/elif/else/fi in hush.
//config:
//config:config HUSH_LOOPS
//config: bool "Support for, while and until loops"
//config: default y
//config: depends on HUSH
//config: help
//config: Enable for, while and until loops in hush.
//config:
//config:config HUSH_CASE
//config: bool "Support case ... esac statement"
//config: default y
//config: depends on HUSH
//config: help
//config: Enable case ... esac statement in hush. +400 bytes.
//config:
//config:config HUSH_FUNCTIONS
//config: bool "Support funcname() { commands; } syntax"
//config: default y
//config: depends on HUSH
//config: help
//config: Enable support for shell functions in hush. +800 bytes.
//config:
//config:config HUSH_LOCAL
//config: bool "Support local builtin"
//config: default y
//config: depends on HUSH_FUNCTIONS
//config: help
//config: Enable support for local variables in functions.
//config:
//config:config HUSH_RANDOM_SUPPORT
//config: bool "Pseudorandom generator and $RANDOM variable"
//config: default y
//config: depends on HUSH
//config: help
//config: Enable pseudorandom generator and dynamic variable "$RANDOM".
//config: Each read of "$RANDOM" will generate a new pseudorandom value.
//config:
//config:config HUSH_EXPORT_N
//config: bool "Support 'export -n' option"
//config: default y
//config: depends on HUSH
//config: help
//config: export -n unexports variables. It is a bash extension.
//config:
//config:config HUSH_MODE_X
//config: bool "Support 'hush -x' option and 'set -x' command"
//config: default y
//config: depends on HUSH
//config: help
//config: This instructs hush to print commands before execution.
//config: Adds ~300 bytes.
//config:
//config:config MSH
//config: bool "msh (deprecated: aliased to hush)"
//config: default n
//config: select HUSH
//config: help
//config: msh is deprecated and will be removed, please migrate to hush.
//config:
//applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP))
//applet:IF_MSH(APPLET(msh, BB_DIR_BIN, BB_SUID_DROP))
//applet:IF_FEATURE_SH_IS_HUSH(APPLET_ODDNAME(sh, hush, BB_DIR_BIN, BB_SUID_DROP, sh))
//applet:IF_FEATURE_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, BB_DIR_BIN, BB_SUID_DROP, bash))
//kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o
//kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o
/* -i (interactive) and -s (read stdin) are also accepted,
* but currently do nothing, therefore aren't shown in help.
* NOMMU-specific options are not meant to be used by users,
* therefore we don't show them either.
*/
//usage:#define hush_trivial_usage
//usage: "[-nxl] [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS]]"
//usage:#define hush_full_usage "\n\n"
//usage: "Unix shell interpreter"
//usage:#define msh_trivial_usage hush_trivial_usage
//usage:#define msh_full_usage hush_full_usage
//usage:#if ENABLE_FEATURE_SH_IS_HUSH
//usage:# define sh_trivial_usage hush_trivial_usage
//usage:# define sh_full_usage hush_full_usage
//usage:#endif
//usage:#if ENABLE_FEATURE_BASH_IS_HUSH
//usage:# define bash_trivial_usage hush_trivial_usage
//usage:# define bash_full_usage hush_full_usage
//usage:#endif
/* Build knobs */
#define LEAK_HUNTING 0
#define BUILD_AS_NOMMU 0
/* Enable/disable sanity checks. Ok to enable in production,
* only adds a bit of bloat. Set to >1 to get non-production level verbosity.
* Keeping 1 for now even in released versions.
*/
#define HUSH_DEBUG 1
/* Slightly bigger (+200 bytes), but faster hush.
* So far it only enables a trick with counting SIGCHLDs and forks,
* which allows us to do fewer waitpid's.
* (we can detect a case where neither forks were done nor SIGCHLDs happened
* and therefore waitpid will return the same result as last time)
*/
#define ENABLE_HUSH_FAST 0
/* TODO: implement simplified code for users which do not need ${var%...} ops
* So far ${var%...} ops are always enabled:
*/
#define ENABLE_HUSH_DOLLAR_OPS 1
#if BUILD_AS_NOMMU
# undef BB_MMU
# undef USE_FOR_NOMMU
# undef USE_FOR_MMU
# define BB_MMU 0
# define USE_FOR_NOMMU(...) __VA_ARGS__
# define USE_FOR_MMU(...)
#endif
#include "NUM_APPLETS.h"
#if NUM_APPLETS == 1
/* STANDALONE does not make sense, and won't compile */
# undef CONFIG_FEATURE_SH_STANDALONE
# undef ENABLE_FEATURE_SH_STANDALONE
# undef IF_FEATURE_SH_STANDALONE
# undef IF_NOT_FEATURE_SH_STANDALONE
# define ENABLE_FEATURE_SH_STANDALONE 0
# define IF_FEATURE_SH_STANDALONE(...)
# define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__
#endif
#if !ENABLE_HUSH_INTERACTIVE
# undef ENABLE_FEATURE_EDITING
# define ENABLE_FEATURE_EDITING 0
# undef ENABLE_FEATURE_EDITING_FANCY_PROMPT
# define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0
# undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
# define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0
#endif
/* Do we support ANY keywords? */
#if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
# define HAS_KEYWORDS 1
# define IF_HAS_KEYWORDS(...) __VA_ARGS__
# define IF_HAS_NO_KEYWORDS(...)
#else
# define HAS_KEYWORDS 0
# define IF_HAS_KEYWORDS(...)
# define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__
#endif
/* If you comment out one of these below, it will be #defined later
* to perform debug printfs to stderr: */
#define debug_printf(...) do {} while (0)
/* Finer-grained debug switches */
#define debug_printf_parse(...) do {} while (0)
#define debug_print_tree(a, b) do {} while (0)
#define debug_printf_exec(...) do {} while (0)
#define debug_printf_env(...) do {} while (0)
#define debug_printf_jobs(...) do {} while (0)
#define debug_printf_expand(...) do {} while (0)
#define debug_printf_varexp(...) do {} while (0)
#define debug_printf_glob(...) do {} while (0)
#define debug_printf_list(...) do {} while (0)
#define debug_printf_subst(...) do {} while (0)
#define debug_printf_clean(...) do {} while (0)
#define ERR_PTR ((void*)(long)1)
#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
#define _SPECIAL_VARS_STR "_*@$!?#"
#define SPECIAL_VARS_STR ("_*@$!?#" + 1)
#define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3)
#if ENABLE_HUSH_BASH_COMPAT
/* Support / and // replace ops */
/* Note that // is stored as \ in "encoded" string representation */
# define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?"
# define VAR_SUBST_OPS ("\\/%#:-=+?" + 1)
# define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5)
#else
# define VAR_ENCODED_SUBST_OPS "%#:-=+?"
# define VAR_SUBST_OPS "%#:-=+?"
# define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3)
#endif
#define SPECIAL_VAR_SYMBOL 3
struct variable;
static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER;
/* This supports saving pointers malloced in vfork child,
* to be freed in the parent.
*/
#if !BB_MMU
typedef struct nommu_save_t {
char **new_env;
struct variable *old_vars;
char **argv;
char **argv_from_re_execing;
} nommu_save_t;
#endif
enum {
RES_NONE = 0,
#if ENABLE_HUSH_IF
RES_IF ,
RES_THEN ,
RES_ELIF ,
RES_ELSE ,
RES_FI ,
#endif
#if ENABLE_HUSH_LOOPS
RES_FOR ,
RES_WHILE ,
RES_UNTIL ,
RES_DO ,
RES_DONE ,
#endif
#if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
RES_IN ,
#endif
#if ENABLE_HUSH_CASE
RES_CASE ,
/* three pseudo-keywords support contrived "case" syntax: */
RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */
RES_MATCH , /* "word)" */
RES_CASE_BODY, /* "this command is inside CASE" */
RES_ESAC ,
#endif
RES_XXXX ,
RES_SNTX
};
typedef struct o_string {
char *data;
int length; /* position where data is appended */
int maxlen;
int o_expflags;
/* At least some part of the string was inside '' or "",
* possibly empty one: word"", wo''rd etc. */
smallint has_quoted_part;
smallint has_empty_slot;
smallint o_assignment; /* 0:maybe, 1:yes, 2:no */
} o_string;
enum {
EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */
EXP_FLAG_GLOB = 0x2,
/* Protect newly added chars against globbing
* by prepending \ to *, ?, [, \ */
EXP_FLAG_ESC_GLOB_CHARS = 0x1,
};
enum {
MAYBE_ASSIGNMENT = 0,
DEFINITELY_ASSIGNMENT = 1,
NOT_ASSIGNMENT = 2,
/* Not an assignment, but next word may be: "if v=xyz cmd;" */
WORD_IS_KEYWORD = 3,
};
/* Used for initialization: o_string foo = NULL_O_STRING; */
#define NULL_O_STRING { NULL }
#ifndef debug_printf_parse
static const char *const assignment_flag[] = {
"MAYBE_ASSIGNMENT",
"DEFINITELY_ASSIGNMENT",
"NOT_ASSIGNMENT",
"WORD_IS_KEYWORD",
};
#endif
typedef struct in_str {
const char *p;
#if ENABLE_HUSH_INTERACTIVE
smallint promptmode; /* 0: PS1, 1: PS2 */
#endif
int peek_buf[2];
int last_char;
FILE *file;
} in_str;
/* The descrip member of this structure is only used to make
* debugging output pretty */
static const struct {
int mode;
signed char default_fd;
char descrip[3];
} redir_table[] = {
{ O_RDONLY, 0, "<" },
{ O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
{ O_CREAT|O_APPEND|O_WRONLY, 1, ">>" },
{ O_CREAT|O_RDWR, 1, "<>" },
{ O_RDONLY, 0, "<<" },
/* Should not be needed. Bogus default_fd helps in debugging */
/* { O_RDONLY, 77, "<<" }, */
};
struct redir_struct {
struct redir_struct *next;
char *rd_filename; /* filename */
int rd_fd; /* fd to redirect */
/* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */
int rd_dup;
smallint rd_type; /* (enum redir_type) */
/* note: for heredocs, rd_filename contains heredoc delimiter,
* and subsequently heredoc itself; and rd_dup is a bitmask:
* bit 0: do we need to trim leading tabs?
* bit 1: is heredoc quoted (<<'delim' syntax) ?
*/
};
typedef enum redir_type {
REDIRECT_INPUT = 0,
REDIRECT_OVERWRITE = 1,
REDIRECT_APPEND = 2,
REDIRECT_IO = 3,
REDIRECT_HEREDOC = 4,
REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */
REDIRFD_CLOSE = -3,
REDIRFD_SYNTAX_ERR = -2,
REDIRFD_TO_FILE = -1,
/* otherwise, rd_fd is redirected to rd_dup */
HEREDOC_SKIPTABS = 1,
HEREDOC_QUOTED = 2,
} redir_type;
struct command {
pid_t pid; /* 0 if exited */
int assignment_cnt; /* how many argv[i] are assignments? */
smallint cmd_type; /* CMD_xxx */
#define CMD_NORMAL 0
#define CMD_SUBSHELL 1
#if ENABLE_HUSH_BASH_COMPAT
/* used for "[[ EXPR ]]" */
# define CMD_SINGLEWORD_NOGLOB 2
#endif
#if ENABLE_HUSH_FUNCTIONS
# define CMD_FUNCDEF 3
#endif
smalluint cmd_exitcode;
/* if non-NULL, this "command" is { list }, ( list ), or a compound statement */
struct pipe *group;
#if !BB_MMU
char *group_as_string;
#endif
#if ENABLE_HUSH_FUNCTIONS
struct function *child_func;
/* This field is used to prevent a bug here:
* while...do f1() {a;}; f1; f1() {b;}; f1; done
* When we execute "f1() {a;}" cmd, we create new function and clear
* cmd->group, cmd->group_as_string, cmd->argv[0].
* When we execute "f1() {b;}", we notice that f1 exists,
* and that its "parent cmd" struct is still "alive",
* we put those fields back into cmd->xxx
* (struct function has ->parent_cmd ptr to facilitate that).
* When we loop back, we can execute "f1() {a;}" again and set f1 correctly.
* Without this trick, loop would execute a;b;b;b;...
* instead of correct sequence a;b;a;b;...
* When command is freed, it severs the link
* (sets ->child_func->parent_cmd to NULL).
*/
#endif
char **argv; /* command name and arguments */
/* argv vector may contain variable references (^Cvar^C, ^C0^C etc)
* and on execution these are substituted with their values.
* Substitution can make _several_ words out of one argv[n]!
* Example: argv[0]=='.^C*^C.' here: echo .$*.
* References of the form ^C`cmd arg^C are `cmd arg` substitutions.
*/
struct redir_struct *redirects; /* I/O redirections */
};
/* Is there anything in this command at all? */
#define IS_NULL_CMD(cmd) \
(!(cmd)->group && !(cmd)->argv && !(cmd)->redirects)
struct pipe {
struct pipe *next;
int num_cmds; /* total number of commands in pipe */
int alive_cmds; /* number of commands running (not exited) */
int stopped_cmds; /* number of commands alive, but stopped */
#if ENABLE_HUSH_JOB
int jobid; /* job number */
pid_t pgrp; /* process group ID for the job */
char *cmdtext; /* name of job */
#endif
struct command *cmds; /* array of commands in pipe */
smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */
IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */
};
typedef enum pipe_style {
PIPE_SEQ = 0,
PIPE_AND = 1,
PIPE_OR = 2,
PIPE_BG = 3,
} pipe_style;
/* Is there anything in this pipe at all? */
#define IS_NULL_PIPE(pi) \
((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE))
/* This holds pointers to the various results of parsing */
struct parse_context {
/* linked list of pipes */
struct pipe *list_head;
/* last pipe (being constructed right now) */
struct pipe *pipe;
/* last command in pipe (being constructed right now) */
struct command *command;
/* last redirect in command->redirects list */
struct redir_struct *pending_redirect;
#if !BB_MMU
o_string as_string;
#endif
#if HAS_KEYWORDS
smallint ctx_res_w;
smallint ctx_inverted; /* "! cmd | cmd" */
#if ENABLE_HUSH_CASE
smallint ctx_dsemicolon; /* ";;" seen */
#endif
/* bitmask of FLAG_xxx, for figuring out valid reserved words */
int old_flag;
/* group we are enclosed in:
* example: "if pipe1; pipe2; then pipe3; fi"
* when we see "if" or "then", we malloc and copy current context,
* and make ->stack point to it. then we parse pipeN.
* when closing "then" / fi" / whatever is found,
* we move list_head into ->stack->command->group,
* copy ->stack into current context, and delete ->stack.
* (parsing of { list } and ( list ) doesn't use this method)
*/
struct parse_context *stack;
#endif
};
/* On program start, environ points to initial environment.
* putenv adds new pointers into it, unsetenv removes them.
* Neither of these (de)allocates the strings.
* setenv allocates new strings in malloc space and does putenv,
* and thus setenv is unusable (leaky) for shell's purposes */
#define setenv(...) setenv_is_leaky_dont_use()
struct variable {
struct variable *next;
char *varstr; /* points to "name=" portion */
#if ENABLE_HUSH_LOCAL
unsigned func_nest_level;
#endif
int max_len; /* if > 0, name is part of initial env; else name is malloced */
smallint flg_export; /* putenv should be done on this var */
smallint flg_read_only;
};
enum {
BC_BREAK = 1,
BC_CONTINUE = 2,
};
#if ENABLE_HUSH_FUNCTIONS
struct function {
struct function *next;
char *name;
struct command *parent_cmd;
struct pipe *body;
# if !BB_MMU
char *body_as_string;
# endif
};
#endif
/* set -/+o OPT support. (TODO: make it optional)
* bash supports the following opts:
* allexport off
* braceexpand on
* emacs on
* errexit off
* errtrace off
* functrace off
* hashall on
* histexpand off
* history on
* ignoreeof off
* interactive-comments on
* keyword off
* monitor on
* noclobber off
* noexec off
* noglob off
* nolog off
* notify off
* nounset off
* onecmd off
* physical off
* pipefail off
* posix off
* privileged off
* verbose off
* vi off
* xtrace off
*/
static const char o_opt_strings[] ALIGN1 =
"pipefail\0"
"noexec\0"
#if ENABLE_HUSH_MODE_X
"xtrace\0"
#endif
;
enum {
OPT_O_PIPEFAIL,
OPT_O_NOEXEC,
#if ENABLE_HUSH_MODE_X
OPT_O_XTRACE,
#endif
NUM_OPT_O
};
struct FILE_list {
struct FILE_list *next;
FILE *fp;
int fd;
};
/* "Globals" within this file */
/* Sorted roughly by size (smaller offsets == smaller code) */
struct globals {
/* interactive_fd != 0 means we are an interactive shell.
* If we are, then saved_tty_pgrp can also be != 0, meaning
* that controlling tty is available. With saved_tty_pgrp == 0,
* job control still works, but terminal signals
* (^C, ^Z, ^Y, ^\) won't work at all, and background
* process groups can only be created with "cmd &".
* With saved_tty_pgrp != 0, hush will use tcsetpgrp()
* to give tty to the foreground process group,
* and will take it back when the group is stopped (^Z)
* or killed (^C).
*/
#if ENABLE_HUSH_INTERACTIVE
/* 'interactive_fd' is a fd# open to ctty, if we have one
* _AND_ if we decided to act interactively */
int interactive_fd;
const char *PS1;
const char *PS2;
# define G_interactive_fd (G.interactive_fd)
#else
# define G_interactive_fd 0
#endif
#if ENABLE_FEATURE_EDITING
line_input_t *line_input_state;
#endif
pid_t root_pid;
pid_t root_ppid;
pid_t last_bg_pid;
#if ENABLE_HUSH_RANDOM_SUPPORT
random_t random_gen;
#endif
#if ENABLE_HUSH_JOB
int run_list_level;
int last_jobid;
pid_t saved_tty_pgrp;
struct pipe *job_list;
# define G_saved_tty_pgrp (G.saved_tty_pgrp)
#else
# define G_saved_tty_pgrp 0
#endif
char o_opt[NUM_OPT_O];
#if ENABLE_HUSH_MODE_X
# define G_x_mode (G.o_opt[OPT_O_XTRACE])
#else
# define G_x_mode 0
#endif
smallint flag_SIGINT;
#if ENABLE_HUSH_LOOPS
smallint flag_break_continue;
#endif
#if ENABLE_HUSH_FUNCTIONS
/* 0: outside of a function (or sourced file)
* -1: inside of a function, ok to use return builtin
* 1: return is invoked, skip all till end of func
*/
smallint flag_return_in_progress;
# define G_flag_return_in_progress (G.flag_return_in_progress)
#else
# define G_flag_return_in_progress 0
#endif
smallint exiting; /* used to prevent EXIT trap recursion */
/* These four support $?, $#, and $1 */
smalluint last_exitcode;
/* are global_argv and global_argv[1..n] malloced? (note: not [0]) */
smalluint global_args_malloced;
/* how many non-NULL argv's we have. NB: $# + 1 */
int global_argc;
char **global_argv;
#if !BB_MMU
char *argv0_for_re_execing;
#endif
#if ENABLE_HUSH_LOOPS
unsigned depth_break_continue;
unsigned depth_of_loop;
#endif
const char *ifs;
const char *cwd;
struct variable *top_var;
char **expanded_assignments;
#if ENABLE_HUSH_FUNCTIONS
struct function *top_func;
# if ENABLE_HUSH_LOCAL
struct variable **shadowed_vars_pp;
unsigned func_nest_level;
# endif
#endif
/* Signal and trap handling */
#if ENABLE_HUSH_FAST
unsigned count_SIGCHLD;
unsigned handled_SIGCHLD;
smallint we_have_children;
#endif
struct FILE_list *FILE_list;
/* Which signals have non-DFL handler (even with no traps set)?
* Set at the start to:
* (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS)
* SPECIAL_INTERACTIVE_SIGS are cleared after fork.
* The rest is cleared right before execv syscalls.
* Other than these two times, never modified.
*/
unsigned special_sig_mask;
#if ENABLE_HUSH_JOB
unsigned fatal_sig_mask;
# define G_fatal_sig_mask G.fatal_sig_mask
#else
# define G_fatal_sig_mask 0
#endif
char **traps; /* char *traps[NSIG] */
sigset_t pending_set;
#if HUSH_DEBUG
unsigned long memleak_value;
int debug_indent;
#endif
struct sigaction sa;
#if ENABLE_FEATURE_EDITING
char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN];
#endif
};
#define G (*ptr_to_globals)
/* Not #defining name to G.name - this quickly gets unwieldy
* (too many defines). Also, I actually prefer to see when a variable
* is global, thus "G." prefix is a useful hint */
#define INIT_G() do { \
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
/* memset(&G.sa, 0, sizeof(G.sa)); */ \
sigfillset(&G.sa.sa_mask); \
G.sa.sa_flags = SA_RESTART; \
} while (0)
/* Function prototypes for builtins */
static int builtin_cd(char **argv) FAST_FUNC;
static int builtin_echo(char **argv) FAST_FUNC;
static int builtin_eval(char **argv) FAST_FUNC;
static int builtin_exec(char **argv) FAST_FUNC;
static int builtin_exit(char **argv) FAST_FUNC;
static int builtin_export(char **argv) FAST_FUNC;
#if ENABLE_HUSH_JOB
static int builtin_fg_bg(char **argv) FAST_FUNC;
static int builtin_jobs(char **argv) FAST_FUNC;
#endif
#if ENABLE_HUSH_HELP
static int builtin_help(char **argv) FAST_FUNC;
#endif
#if MAX_HISTORY && ENABLE_FEATURE_EDITING
static int builtin_history(char **argv) FAST_FUNC;
#endif
#if ENABLE_HUSH_LOCAL
static int builtin_local(char **argv) FAST_FUNC;
#endif
#if HUSH_DEBUG
static int builtin_memleak(char **argv) FAST_FUNC;
#endif
#if ENABLE_PRINTF
static int builtin_printf(char **argv) FAST_FUNC;
#endif
static int builtin_pwd(char **argv) FAST_FUNC;
static int builtin_read(char **argv) FAST_FUNC;
static int builtin_set(char **argv) FAST_FUNC;
static int builtin_shift(char **argv) FAST_FUNC;
static int builtin_source(char **argv) FAST_FUNC;
static int builtin_test(char **argv) FAST_FUNC;
static int builtin_trap(char **argv) FAST_FUNC;
static int builtin_type(char **argv) FAST_FUNC;
static int builtin_true(char **argv) FAST_FUNC;
static int builtin_umask(char **argv) FAST_FUNC;
static int builtin_unset(char **argv) FAST_FUNC;
static int builtin_wait(char **argv) FAST_FUNC;
#if ENABLE_HUSH_LOOPS
static int builtin_break(char **argv) FAST_FUNC;
static int builtin_continue(char **argv) FAST_FUNC;
#endif
#if ENABLE_HUSH_FUNCTIONS
static int builtin_return(char **argv) FAST_FUNC;
#endif
/* Table of built-in functions. They can be forked or not, depending on
* context: within pipes, they fork. As simple commands, they do not.
* When used in non-forking context, they can change global variables
* in the parent shell process. If forked, of course they cannot.
* For example, 'unset foo | whatever' will parse and run, but foo will
* still be set at the end. */
struct built_in_command {
const char *b_cmd;
int (*b_function)(char **argv) FAST_FUNC;
#if ENABLE_HUSH_HELP
const char *b_descr;
# define BLTIN(cmd, func, help) { cmd, func, help }
#else
# define BLTIN(cmd, func, help) { cmd, func }
#endif
};
static const struct built_in_command bltins1[] = {
BLTIN("." , builtin_source , "Run commands in a file"),
BLTIN(":" , builtin_true , NULL),
#if ENABLE_HUSH_JOB
BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"),
#endif
#if ENABLE_HUSH_LOOPS
BLTIN("break" , builtin_break , "Exit from a loop"),
#endif
BLTIN("cd" , builtin_cd , "Change directory"),
#if ENABLE_HUSH_LOOPS
BLTIN("continue" , builtin_continue, "Start new loop iteration"),
#endif
BLTIN("eval" , builtin_eval , "Construct and run shell command"),
BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"),
BLTIN("exit" , builtin_exit , "Exit"),
BLTIN("export" , builtin_export , "Set environment variables"),
#if ENABLE_HUSH_JOB
BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"),
#endif
#if ENABLE_HUSH_HELP
BLTIN("help" , builtin_help , NULL),
#endif
#if MAX_HISTORY && ENABLE_FEATURE_EDITING
BLTIN("history" , builtin_history , "Show command history"),
#endif
#if ENABLE_HUSH_JOB
BLTIN("jobs" , builtin_jobs , "List jobs"),
#endif
#if ENABLE_HUSH_LOCAL
BLTIN("local" , builtin_local , "Set local variables"),
#endif
#if HUSH_DEBUG
BLTIN("memleak" , builtin_memleak , NULL),
#endif
BLTIN("read" , builtin_read , "Input into variable"),
#if ENABLE_HUSH_FUNCTIONS
BLTIN("return" , builtin_return , "Return from a function"),
#endif
BLTIN("set" , builtin_set , "Set/unset positional parameters"),
BLTIN("shift" , builtin_shift , "Shift positional parameters"),
#if ENABLE_HUSH_BASH_COMPAT
BLTIN("source" , builtin_source , "Run commands in a file"),
#endif
BLTIN("trap" , builtin_trap , "Trap signals"),
BLTIN("true" , builtin_true , NULL),
BLTIN("type" , builtin_type , "Show command type"),
BLTIN("ulimit" , shell_builtin_ulimit , "Control resource limits"),
BLTIN("umask" , builtin_umask , "Set file creation mask"),
BLTIN("unset" , builtin_unset , "Unset variables"),
BLTIN("wait" , builtin_wait , "Wait for process"),
};
/* For now, echo and test are unconditionally enabled.
* Maybe make it configurable? */
static const struct built_in_command bltins2[] = {
BLTIN("[" , builtin_test , NULL),
BLTIN("echo" , builtin_echo , NULL),
#if ENABLE_PRINTF
BLTIN("printf" , builtin_printf , NULL),
#endif
BLTIN("pwd" , builtin_pwd , NULL),
BLTIN("test" , builtin_test , NULL),
};
/* Debug printouts.
*/
#if HUSH_DEBUG
/* prevent disasters with G.debug_indent < 0 */
# define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "")
# define debug_enter() (G.debug_indent++)
# define debug_leave() (G.debug_indent--)
#else
# define indent() ((void)0)
# define debug_enter() ((void)0)
# define debug_leave() ((void)0)
#endif
#ifndef debug_printf
# define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__))
#endif
#ifndef debug_printf_parse
# define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__))
#endif
#ifndef debug_printf_exec
#define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__))
#endif
#ifndef debug_printf_env
# define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__))
#endif
#ifndef debug_printf_jobs
# define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__))
# define DEBUG_JOBS 1
#else
# define DEBUG_JOBS 0
#endif
#ifndef debug_printf_expand
# define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__))
# define DEBUG_EXPAND 1
#else
# define DEBUG_EXPAND 0
#endif
#ifndef debug_printf_varexp
# define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__))
#endif
#ifndef debug_printf_glob
# define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__))
# define DEBUG_GLOB 1
#else
# define DEBUG_GLOB 0
#endif
#ifndef debug_printf_list
# define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__))
#endif
#ifndef debug_printf_subst
# define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__))
#endif
#ifndef debug_printf_clean
# define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__))
# define DEBUG_CLEAN 1
#else
# define DEBUG_CLEAN 0
#endif
#if DEBUG_EXPAND
static void debug_print_strings(const char *prefix, char **vv)
{
indent();
fdprintf(2, "%s:\n", prefix);
while (*vv)
fdprintf(2, " '%s'\n", *vv++);
}
#else
# define debug_print_strings(prefix, vv) ((void)0)
#endif
/* Leak hunting. Use hush_leaktool.sh for post-processing.
*/
#if LEAK_HUNTING
static void *xxmalloc(int lineno, size_t size)
{
void *ptr = xmalloc((size + 0xff) & ~0xff);
fdprintf(2, "line %d: malloc %p\n", lineno, ptr);
return ptr;
}
static void *xxrealloc(int lineno, void *ptr, size_t size)
{
ptr = xrealloc(ptr, (size + 0xff) & ~0xff);
fdprintf(2, "line %d: realloc %p\n", lineno, ptr);
return ptr;
}
static char *xxstrdup(int lineno, const char *str)
{
char *ptr = xstrdup(str);
fdprintf(2, "line %d: strdup %p\n", lineno, ptr);
return ptr;
}
static void xxfree(void *ptr)
{
fdprintf(2, "free %p\n", ptr);
free(ptr);
}
# define xmalloc(s) xxmalloc(__LINE__, s)
# define xrealloc(p, s) xxrealloc(__LINE__, p, s)
# define xstrdup(s) xxstrdup(__LINE__, s)
# define free(p) xxfree(p)
#endif
/* Syntax and runtime errors. They always abort scripts.
* In interactive use they usually discard unparsed and/or unexecuted commands
* and return to the prompt.
* HUSH_DEBUG >= 2 prints line number in this file where it was detected.
*/
#if HUSH_DEBUG < 2
# define die_if_script(lineno, ...) die_if_script(__VA_ARGS__)
# define syntax_error(lineno, msg) syntax_error(msg)
# define syntax_error_at(lineno, msg) syntax_error_at(msg)
# define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch)
# define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s)
# define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch)
#endif
static void die_if_script(unsigned lineno, const char *fmt, ...)
{
va_list p;
#if HUSH_DEBUG >= 2
bb_error_msg("hush.c:%u", lineno);
#endif
va_start(p, fmt);
bb_verror_msg(fmt, p, NULL);
va_end(p);
if (!G_interactive_fd)
xfunc_die();
}
static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg)
{
if (msg)
bb_error_msg("syntax error: %s", msg);
else
bb_error_msg("syntax error");
}
static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg)
{
bb_error_msg("syntax error at '%s'", msg);
}
static void syntax_error_unterm_str(unsigned lineno UNUSED_PARAM, const char *s)
{
bb_error_msg("syntax error: unterminated %s", s);
}
static void syntax_error_unterm_ch(unsigned lineno, char ch)
{
char msg[2] = { ch, '\0' };
syntax_error_unterm_str(lineno, msg);
}
static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch)
{
char msg[2];
msg[0] = ch;
msg[1] = '\0';
bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg);
}
#if HUSH_DEBUG < 2
# undef die_if_script
# undef syntax_error
# undef syntax_error_at
# undef syntax_error_unterm_ch
# undef syntax_error_unterm_str
# undef syntax_error_unexpected_ch
#else
# define die_if_script(...) die_if_script(__LINE__, __VA_ARGS__)
# define syntax_error(msg) syntax_error(__LINE__, msg)
# define syntax_error_at(msg) syntax_error_at(__LINE__, msg)
# define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch)
# define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s)
# define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch)
#endif
#if ENABLE_HUSH_INTERACTIVE
static void cmdedit_update_prompt(void);
#else
# define cmdedit_update_prompt() ((void)0)
#endif
/* Utility functions
*/
/* Replace each \x with x in place, return ptr past NUL. */
static char *unbackslash(char *src)
{
char *dst = src = strchrnul(src, '\\');
while (1) {
if (*src == '\\')
src++;
if ((*dst++ = *src++) == '\0')
break;
}
return dst;
}
static char **add_strings_to_strings(char **strings, char **add, int need_to_dup)
{
int i;
unsigned count1;
unsigned count2;
char **v;
v = strings;
count1 = 0;
if (v) {
while (*v) {
count1++;
v++;
}
}
count2 = 0;
v = add;
while (*v) {
count2++;
v++;
}
v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*));
v[count1 + count2] = NULL;
i = count2;
while (--i >= 0)
v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]);
return v;
}
#if LEAK_HUNTING
static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup)
{
char **ptr = add_strings_to_strings(strings,