* <(list) and >(list) Process Substitution
* Tilde Expansion
*
- * Bash stuff (maybe optionally enable?):
+ * Bash stuff (optionally enabled):
* &> and >& redirection of stdout+stderr
- * Brace expansion
+ * Brace Expansion
* reserved words: [[ ]] function select
* substrings ${var:1:5}
+ * 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 "expression".
*
* TODOs:
* grep for "TODO" and fix (some of them are easy)
* builtins: ulimit
+ * special variables (PWD etc)
* follow IFS rules more precisely, including update semantics
+ * export builtin should be 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
*
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
*/
} nommu_save_t;
#endif
-/* 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[] = {
- { 0, 0, "??" },
- { O_RDONLY, 0, "<" },
- { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
- { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" },
- { O_RDONLY, 0, "<<" },
- { O_CREAT|O_RDWR, 1, "<>" },
-/* Should not be needed. Bogus default_fd helps in debugging */
-/* { O_RDONLY, 77, "<<" }, */
-};
-
typedef enum reserved_style {
RES_NONE = 0,
#if ENABLE_HUSH_IF
smallint promptmode; /* 0: PS1, 1: PS2 */
#endif
FILE *file;
- int (*get) (struct in_str *);
- int (*peek) (struct in_str *);
+ int (*get) (struct in_str *) FAST_FUNC;
+ int (*peek) (struct in_str *) FAST_FUNC;
} in_str;
#define i_getch(input) ((input)->get(input))
#define i_peek(input) ((input)->peek(input))
+/* 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 */
smallint rd_type; /* (enum redir_type) */
/* note: for heredocs, rd_filename contains heredoc delimiter,
* and subsequently heredoc itself; and rd_dup is a bitmask:
- * 1: do we need to trim leading tabs?
- * 2: is heredoc quoted (<<'delim' syntax) ?
+ * bit 0: do we need to trim leading tabs?
+ * bit 1: is heredoc quoted (<<'delim' syntax) ?
*/
};
typedef enum redir_type {
- REDIRECT_INVALID = 0,
- REDIRECT_INPUT = 1,
- REDIRECT_OVERWRITE = 2,
- REDIRECT_APPEND = 3,
+ REDIRECT_INPUT = 0,
+ REDIRECT_OVERWRITE = 1,
+ REDIRECT_APPEND = 2,
+ REDIRECT_IO = 3,
REDIRECT_HEREDOC = 4,
- REDIRECT_IO = 5,
- REDIRECT_HEREDOC2 = 6, /* REDIRECT_HEREDOC after heredoc is loaded */
+ REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */
REDIRFD_CLOSE = -3,
REDIRFD_SYNTAX_ERR = -2,
pid_t pid; /* 0 if exited */
int assignment_cnt; /* how many argv[i] are assignments? */
smallint is_stopped; /* is the command currently running? */
- smallint grp_type; /* GRP_xxx */
-#define GRP_NORMAL 0
-#define GRP_SUBSHELL 1
+ smallint cmd_type; /* CMD_xxx */
+#define CMD_NORMAL 0
+#define CMD_SUBSHELL 1
+
+/* used for "[[ EXPR ]]" */
+#if ENABLE_HUSH_BASH_COMPAT
+# define CMD_SINGLEWORD_NOGLOB 2
+#endif
+
+/* used for "export noglob=* glob* a=`echo a b`" */
+//#define CMD_SINGLEWORD_NOGLOB_COND 3
+// It is hard to implement correctly, it adds significant amounts of tricky code,
+// and all this is only useful for really obscure export statements
+// almost nobody would use anyway. #ifdef CMD_SINGLEWORD_NOGLOB_COND
+// guards the code which implements it, but I have doubts it works
+// in all cases (especially with mixed globbed/non-globbed arguments)
+
#if ENABLE_HUSH_FUNCTIONS
-# define GRP_FUNCTION 2
+# define CMD_FUNCDEF 3
#endif
+
/* if non-NULL, this "command" is { list }, ( list ), or a compound statement */
struct pipe *group;
#if !BB_MMU
#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
+ * 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 it's "parent cmd" struct is still "alive",
+ * 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.
/* Function prototypes for builtins */
-static int builtin_cd(char **argv);
-static int builtin_echo(char **argv);
-static int builtin_eval(char **argv);
-static int builtin_exec(char **argv);
-static int builtin_exit(char **argv);
-static int builtin_export(char **argv);
+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);
-static int builtin_jobs(char **argv);
+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);
+static int builtin_help(char **argv) FAST_FUNC;
#endif
#if ENABLE_HUSH_LOCAL
-static int builtin_local(char **argv);
+static int builtin_local(char **argv) FAST_FUNC;
#endif
#if HUSH_DEBUG
-static int builtin_memleak(char **argv);
-#endif
-static int builtin_pwd(char **argv);
-static int builtin_read(char **argv);
-static int builtin_set(char **argv);
-static int builtin_shift(char **argv);
-static int builtin_source(char **argv);
-static int builtin_test(char **argv);
-static int builtin_trap(char **argv);
-static int builtin_type(char **argv);
-static int builtin_true(char **argv);
-static int builtin_umask(char **argv);
-static int builtin_unset(char **argv);
-static int builtin_wait(char **argv);
+static int builtin_memleak(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);
-static int builtin_continue(char **argv);
+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);
+static int builtin_return(char **argv) FAST_FUNC;
#endif
/* Table of built-in functions. They can be forked or not, depending on
* still be set at the end. */
struct built_in_command {
const char *cmd;
- int (*function)(char **argv);
+ int (*function)(char **argv) FAST_FUNC;
#if ENABLE_HUSH_HELP
const char *descr;
# define BLTIN(cmd, func, help) { cmd, func, help }
#endif
};
-/* For now, echo and test are unconditionally enabled.
- * Maybe make it configurable? */
-static const struct built_in_command bltins[] = {
- BLTIN("." , builtin_source , "Run commands in a file"),
- BLTIN(":" , builtin_true , "No-op"),
- BLTIN("[" , builtin_test , "Test condition"),
+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"),
+ BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"),
#endif
#if ENABLE_HUSH_LOOPS
- BLTIN("break" , builtin_break , "Exit from a loop"),
+ BLTIN("break" , builtin_break , "Exit from a loop"),
#endif
- BLTIN("cd" , builtin_cd , "Change directory"),
+ BLTIN("cd" , builtin_cd , "Change directory"),
#if ENABLE_HUSH_LOOPS
- BLTIN("continue", builtin_continue, "Start new loop iteration"),
+ BLTIN("continue" , builtin_continue, "Start new loop iteration"),
#endif
- BLTIN("echo" , builtin_echo , "Write to stdout"),
- 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 variable"),
+ 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"),
+ BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"),
#endif
#if ENABLE_HUSH_HELP
- BLTIN("help" , builtin_help , "List shell built-in commands"),
+ BLTIN("help" , builtin_help , NULL),
#endif
#if ENABLE_HUSH_JOB
- BLTIN("jobs" , builtin_jobs , "List active jobs"),
+ BLTIN("jobs" , builtin_jobs , "List jobs"),
#endif
#if ENABLE_HUSH_LOCAL
- BLTIN("local" , builtin_local , "Set local variable"),
+ BLTIN("local" , builtin_local , "Set local variables"),
#endif
#if HUSH_DEBUG
- BLTIN("memleak" , builtin_memleak , "Debug tool"),
+ BLTIN("memleak" , builtin_memleak , NULL),
#endif
- BLTIN("pwd" , builtin_pwd , "Print current directory"),
- BLTIN("read" , builtin_read , "Input environment variable"),
+ 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 shell local variables"),
- BLTIN("shift" , builtin_shift , "Shift positional parameters"),
- BLTIN("test" , builtin_test , "Test condition"),
- BLTIN("trap" , builtin_trap , "Trap signals"),
- BLTIN("type" , builtin_type , "Write a description of command type"),
-// BLTIN("ulimit" , builtin_ulimit , "Control resource limits"),
- BLTIN("umask" , builtin_umask , "Set file creation mask"),
- BLTIN("unset" , builtin_unset , "Unset environment variable"),
- BLTIN("wait" , builtin_wait , "Wait for process"),
+ BLTIN("return" , builtin_return , "Return from a function"),
+#endif
+ BLTIN("set" , builtin_set , "Set/unset positional parameters"),
+ BLTIN("shift" , builtin_shift , "Shift positional parameters"),
+ BLTIN("trap" , builtin_trap , "Trap signals"),
+ BLTIN("type" , builtin_type , "Write a description of command type"),
+// BLTIN("ulimit" , 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),
+ BLTIN("pwd" , builtin_pwd , NULL),
+ BLTIN("test" , builtin_test , NULL),
};
}
-static const char *set_cwd(void)
+static const char *get_cwd(int force)
{
- /* xrealloc_getcwd_or_warn(arg) calls free(arg),
- * we must not try to free(bb_msg_unknown) */
- if (G.cwd == bb_msg_unknown)
- G.cwd = NULL;
- G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd);
- if (!G.cwd)
- G.cwd = bb_msg_unknown;
+ if (force || G.cwd == NULL) {
+ /* xrealloc_getcwd_or_warn(arg) calls free(arg),
+ * we must not try to free(bb_msg_unknown) */
+ if (G.cwd == bb_msg_unknown)
+ G.cwd = NULL;
+ G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd);
+ if (!G.cwd)
+ G.cwd = bb_msg_unknown;
+ }
return G.cwd;
}
/*
* in_str support
*/
-static int static_get(struct in_str *i)
+static int FAST_FUNC static_get(struct in_str *i)
{
int ch = *i->p++;
if (ch != '\0')
return EOF;
}
-static int static_peek(struct in_str *i)
+static int FAST_FUNC static_peek(struct in_str *i)
{
return *i->p;
}
/* Set up the prompt */
if (promptmode == 0) { /* PS1 */
free((char*)G.PS1);
- G.PS1 = xasprintf("%s %c ", G.cwd, (geteuid() != 0) ? '$' : '#');
+ G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#');
prompt_str = G.PS1;
} else
prompt_str = G.PS2;
/* This is the magic location that prints prompts
* and gets data back from the user */
-static int file_get(struct in_str *i)
+static int FAST_FUNC file_get(struct in_str *i)
{
int ch;
/* All callers guarantee this routine will never
* be used right after a newline, so prompting is not needed.
*/
-static int file_peek(struct in_str *i)
+static int FAST_FUNC file_peek(struct in_str *i)
{
int ch;
if (i->p && *i->p) {
n = 0;
v = argv;
while (*v) {
- n = expand_vars_to_list(&output, n, *v, (char)or_mask);
+ n = expand_vars_to_list(&output, n, *v, (unsigned char)or_mask);
v++;
}
debug_print_list("expand_variables", &output, n);
return expand_variables(argv, 0x100);
}
+#if ENABLE_HUSH_BASH_COMPAT
+static char **expand_strvec_to_strvec_singleword_noglob(char **argv)
+{
+ return expand_variables(argv, 0x80);
+}
+#endif
+
+#ifdef CMD_SINGLEWORD_NOGLOB_COND
+static char **expand_strvec_to_strvec_singleword_noglob_cond(char **argv)
+{
+ int n;
+ char **list;
+ char **v;
+ o_string output = NULL_O_STRING;
+
+ n = 0;
+ v = argv;
+ while (*v) {
+ int is_var = is_well_formed_var_name(*v, '=');
+ /* is_var * 0x80: singleword expansion for vars */
+ n = expand_vars_to_list(&output, n, *v, is_var * 0x80);
+
+ /* Subtle! expand_vars_to_list did not glob last word yet.
+ * It does this only when fed with further data.
+ * Therefore we set globbing flags AFTER it, not before:
+ */
+
+ /* if it is not recognizably abc=...; then: */
+ output.o_escape = !is_var; /* protect against globbing for "$var" */
+ /* (unquoted $var will temporarily switch it off) */
+ output.o_glob = !is_var; /* and indeed do globbing */
+ v++;
+ }
+ debug_print_list("expand_cond", &output, n);
+
+ /* output.data (malloced in one block) gets returned in "list" */
+ list = o_finalize_list(&output, n);
+ debug_print_strings("expand_cond[1]", list);
+ return list;
+}
+#endif
+
/* Used for expansion of right hand of assignments */
/* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs
* "v=/bin/c*" */
argv[0] = (char*)str;
argv[1] = NULL;
- list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */
+ list = expand_variables(argv, 0x80); /* 0x80: singleword expansion */
if (HUSH_DEBUG)
if (!list[0] || list[1])
bb_error_msg_and_die("BUG in varexp2");
#if BB_MMU
/* never called */
-void re_execute_shell(char ***to_free, const char *s, char *argv0, char **argv);
+void re_execute_shell(char ***to_free, const char *s,
+ char *g_argv0, char **g_argv,
+ char **builtin_argv) NORETURN;
static void reset_traps_to_defaults(void)
{
#else /* !BB_MMU */
-static void re_execute_shell(char ***to_free, const char *s, char *g_argv0, char **g_argv) NORETURN;
-static void re_execute_shell(char ***to_free, const char *s, char *g_argv0, char **g_argv)
+static void re_execute_shell(char ***to_free, const char *s,
+ char *g_argv0, char **g_argv,
+ char **builtin_argv) NORETURN;
+static void re_execute_shell(char ***to_free, const char *s,
+ char *g_argv0, char **g_argv,
+ char **builtin_argv)
{
- char param_buf[sizeof("-$%x:%x:%x:%x") + sizeof(unsigned) * 4];
+ char param_buf[sizeof("-$%x:%x:%x:%x:%x") + sizeof(unsigned) * 2];
char *heredoc_argv[4];
struct variable *cur;
#if ENABLE_HUSH_FUNCTIONS
goto do_exec;
}
- sprintf(param_buf, "-$%x:%x:%x" IF_HUSH_LOOPS(":%x")
+ cnt = 0;
+ pp = builtin_argv;
+ if (pp) while (*pp++)
+ cnt++;
+
+ sprintf(param_buf, "-$%x:%x:%x:%x" IF_HUSH_LOOPS(":%x")
, (unsigned) G.root_pid
, (unsigned) G.last_bg_pid
, (unsigned) G.last_exitcode
+ , cnt
IF_HUSH_LOOPS(, G.depth_of_loop)
);
/* 1:hush 2:-$<pid>:<pid>:<exitcode>:<depth> <vars...> <funcs...>
* 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL
*/
- cnt = 6;
+ cnt += 6;
for (cur = G.top_var; cur; cur = cur->next) {
if (!cur->flg_export || cur->flg_read_only)
cnt += 2;
*/
*pp++ = (char *) "-c";
*pp++ = (char *) s;
+ if (builtin_argv) {
+ while (*++builtin_argv)
+ *pp++ = *builtin_argv;
+ *pp++ = (char *) "";
+ }
*pp++ = g_argv0;
while (*g_argv)
*pp++ = *g_argv++;
#else
/* Delegate blocking writes to another process */
xmove_fd(pair.wr, STDOUT_FILENO);
- re_execute_shell(&to_free, heredoc, NULL, NULL);
+ re_execute_shell(&to_free, heredoc, NULL, NULL, NULL);
#endif
}
/* parent */
}
/* not "else if": on syntax error, we may have both! */
if (command->group) {
- debug_printf_clean(" begin group (grp_type:%d)\n",
- command->grp_type);
+ debug_printf_clean(" begin group (cmd_type:%d)\n",
+ command->cmd_type);
free_pipe_list(command->group);
debug_printf_clean(" end group\n");
command->group = NULL;
return ret;
}
-static const struct built_in_command* find_builtin(const char *name)
+static const struct built_in_command* find_builtin_helper(const char *name,
+ const struct built_in_command *x,
+ const struct built_in_command *end)
{
- const struct built_in_command *x;
- for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
- if (strcmp(name, x->cmd) != 0)
+ while (x != end) {
+ if (strcmp(name, x->cmd) != 0) {
+ x++;
continue;
+ }
debug_printf_exec("found builtin '%s'\n", name);
return x;
}
return NULL;
}
+static const struct built_in_command* find_builtin1(const char *name)
+{
+ return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]);
+}
+static const struct built_in_command* find_builtin(const char *name)
+{
+ const struct built_in_command *x = find_builtin1(name);
+ if (x)
+ return x;
+ return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]);
+}
#if ENABLE_HUSH_FUNCTIONS
static const struct function *find_function(const char *name)
}
# if BB_MMU
-#define exec_function(nommu_save, funcp, argv) \
+#define exec_function(to_free, funcp, argv) \
exec_function(funcp, argv)
# endif
-static void exec_function(nommu_save_t *nommu_save,
+static void exec_function(char ***to_free,
const struct function *funcp,
char **argv) NORETURN;
-static void exec_function(nommu_save_t *nommu_save,
+static void exec_function(char ***to_free,
const struct function *funcp,
char **argv)
{
fflush(NULL);
_exit(n);
# else
- re_execute_shell(&nommu_save->argv_from_re_execing,
+ re_execute_shell(to_free,
funcp->body_as_string,
G.global_argv[0],
- argv + 1);
+ argv + 1,
+ NULL);
# endif
}
#endif /* ENABLE_HUSH_FUNCTIONS */
+# if BB_MMU
+#define exec_builtin(to_free, x, argv) \
+ exec_builtin(x, argv)
+# else
+#define exec_builtin(to_free, x, argv) \
+ exec_builtin(to_free, argv)
+# endif
+static void exec_builtin(char ***to_free,
+ const struct built_in_command *x,
+ char **argv) NORETURN;
+static void exec_builtin(char ***to_free,
+ const struct built_in_command *x,
+ char **argv)
+{
+# if BB_MMU
+ int rcode = x->function(argv);
+ fflush(NULL);
+ _exit(rcode);
+# else
+ /* On NOMMU, we must never block!
+ * Example: { sleep 99 | read line; } & echo Ok
+ */
+ re_execute_shell(to_free,
+ argv[0],
+ G.global_argv[0],
+ G.global_argv + 1,
+ argv);
+# endif
+}
+
+
#if BB_MMU
#define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \
pseudo_exec_argv(argv, assignment_cnt, argv_expanded)
goto skip;
#endif
- /* On NOMMU, we must never block!
- * Example: { sleep 99999 | read line } & echo Ok
- * read builtin will block on read syscall, leaving parent blocked
- * in vfork. Therefore we can't do this:
- */
-#if BB_MMU
/* Check if the command matches any of the builtins.
* Depending on context, this might be redundant. But it's
* easier to waste a few CPU cycles than it is to figure out
* if this is one of those cases.
*/
{
- int rcode;
- const struct built_in_command *x = find_builtin(argv[0]);
+ /* On NOMMU, it is more expensive to re-execute shell
+ * just in order to run echo or test builtin.
+ * It's better to skip it here and run corresponding
+ * non-builtin later. */
+ const struct built_in_command *x;
+ x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]);
if (x) {
- rcode = x->function(argv);
- fflush(NULL);
- _exit(rcode);
+ exec_builtin(&nommu_save->argv_from_re_execing, x, argv);
}
}
-#endif
#if ENABLE_HUSH_FUNCTIONS
/* Check if the command matches any functions */
{
const struct function *funcp = find_function(argv[0]);
if (funcp) {
- exec_function(nommu_save, funcp, argv);
+ exec_function(&nommu_save->argv_from_re_execing, funcp, argv);
}
}
#endif
re_execute_shell(&nommu_save->argv_from_re_execing,
command->group_as_string,
G.global_argv[0],
- G.global_argv + 1);
+ G.global_argv + 1,
+ NULL);
#endif
}
if (pi->num_cmds != 1
|| pi->followup == PIPE_BG
- || command->grp_type == GRP_SUBSHELL
+ || command->cmd_type == CMD_SUBSHELL
) {
goto must_fork;
}
if (command->group) {
#if ENABLE_HUSH_FUNCTIONS
- if (command->grp_type == GRP_FUNCTION) {
+ if (command->cmd_type == CMD_FUNCDEF) {
/* "executing" func () { list } */
struct function *funcp;
}
/* Expand the rest into (possibly) many strings each */
- argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt);
+ if (0) {}
+#if ENABLE_HUSH_BASH_COMPAT
+ else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) {
+ argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt);
+ }
+#endif
+#ifdef CMD_SINGLEWORD_NOGLOB_COND
+ else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB_COND) {
+ argv_expanded = expand_strvec_to_strvec_singleword_noglob_cond(argv + command->assignment_cnt);
+
+ }
+#endif
+ else {
+ argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt);
+ }
x = find_builtin(argv_expanded[0]);
#if ENABLE_HUSH_FUNCTIONS
[RES_XXXX ] = "XXXX" ,
[RES_SNTX ] = "SNTX" ,
};
- static const char *const GRPTYPE[] = {
+ static const char *const CMDTYPE[] = {
"{}",
"()",
+ "[noglob]",
#if ENABLE_HUSH_FUNCTIONS
"func()",
#endif
command->assignment_cnt);
if (command->group) {
fprintf(stderr, " group %s: (argv=%p)\n",
- GRPTYPE[command->grp_type],
+ CMDTYPE[command->cmd_type],
argv);
debug_print_tree(command->group, lvl+1);
prn++;
debug_printf_parse("pop stack %p\n", ctx->stack);
old = ctx->stack;
old->command->group = ctx->list_head;
- old->command->grp_type = GRP_NORMAL;
+ old->command->cmd_type = CMD_NORMAL;
#if !BB_MMU
o_addstr(&old->as_string, ctx->as_string.data);
o_free_unsafe(&ctx->as_string);
(ctx->ctx_res_w == RES_SNTX));
return (ctx->ctx_res_w == RES_SNTX);
}
+# ifdef CMD_SINGLEWORD_NOGLOB_COND
+ if (strcmp(word->data, "export") == 0
+# if ENABLE_HUSH_LOCAL
+ || strcmp(word->data, "local") == 0
+# endif
+ ) {
+ command->cmd_type = CMD_SINGLEWORD_NOGLOB_COND;
+ } else
+# endif
+# if ENABLE_HUSH_BASH_COMPAT
+ if (strcmp(word->data, "[[") == 0) {
+ command->cmd_type = CMD_SINGLEWORD_NOGLOB;
+ }
+ /* fall through */
+# endif
}
#endif
if (command->group) {
char *p;
redir->rd_type = REDIRECT_HEREDOC2;
- /* redir->dup is (ab)used to indicate <<- */
+ /* redir->rd_dup is (ab)used to indicate <<- */
p = fetch_till_str(&ctx->as_string, input,
redir->rd_filename, redir->rd_dup & HEREDOC_SKIPTABS);
if (!p) {
re_execute_shell(&to_free,
s,
G.global_argv[0],
- G.global_argv + 1);
+ G.global_argv + 1,
+ NULL);
#endif
}
return 1;
}
nommu_addchr(&ctx->as_string, ch);
- command->grp_type = GRP_FUNCTION;
+ command->cmd_type = CMD_FUNCDEF;
goto skip;
}
#endif
endch = '}';
if (ch == '(') {
endch = ')';
- command->grp_type = GRP_SUBSHELL;
+ command->cmd_type = CMD_SUBSHELL;
} else {
/* bash does not allow "{echo...", requires whitespace */
ch = i_getch(input);
dest.o_assignment = NOT_ASSIGNMENT;
}
+ /* Note: nommu_addchr(&ctx.as_string, ch) is already done */
+
switch (ch) {
case '#':
if (dest.length == 0) {
ch = i_getch(input);
if (ch != '\n') {
o_addchr(&dest, '\\');
- nommu_addchr(&ctx.as_string, '\\');
+ /*nommu_addchr(&ctx.as_string, '\\'); - already done */
o_addchr(&dest, ch);
nommu_addchr(&ctx.as_string, ch);
/* Example: echo Hello \2>file
* we need to know that word 2 is quoted */
dest.o_quoted = 1;
+ } else {
+#if !BB_MMU
+ /* It's "\<newline>". Remove trailing '\' from ctx.as_string */
+ ctx.as_string.data[--ctx.as_string.length] = '\0';
+#endif
}
break;
case '$':
IF_HAS_KEYWORDS(struct parse_context *p2;)
/* Clean up allocated tree.
- * Samples for finding leaks on syntax error recovery path.
- * Run them from interactive shell, watch pmap `pidof hush`.
- * while if false; then false; fi do break; done
- * (bash accepts it)
+ * Sample for finding leaks on syntax error recovery path.
+ * Run it from interactive shell, watch pmap `pidof hush`.
* while if false; then false; fi; do break; fi
* Samples to catch leaks at execution:
* while if (true | {true;}); then echo ok; fi; do break; done
};
int signal_mask_is_inited = 0;
int opt;
+ unsigned builtin_argc;
char **e;
struct variable *cur_var;
G.global_argc = argc;
G.global_argv = argv;
/* Initialize some more globals to non-zero values */
- set_cwd();
cmdedit_update_prompt();
if (setjmp(die_jmp)) {
/* Parse options */
/* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */
+ builtin_argc = 0;
while (1) {
opt = getopt(argc, argv, "c:xins"
#if !BB_MMU
break;
switch (opt) {
case 'c':
+ /* Possibilities:
+ * sh ... -c 'script'
+ * sh ... -c 'script' ARG0 [ARG1...]
+ * On NOMMU, if builtin_argc != 0,
+ * sh ... -c 'builtin' [BARGV...] "" ARG0 [ARG1...]
+ * "" needs to be replaced with NULL
+ * and BARGV vector fed to builtin function.
+ * Note: this form never happens:
+ * sh ... -c 'builtin' [BARGV...] ""
+ */
if (!G.root_pid)
G.root_pid = getpid();
G.global_argv = argv + optind;
- if (!argv[optind]) {
- /* -c 'script' (no params): prevent empty $0 */
- *--G.global_argv = argv[0];
- optind--;
- } /* else -c 'script' PAR0 PAR1: $0 is PAR0 */
G.global_argc = argc - optind;
+ if (builtin_argc) {
+ /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */
+ const struct built_in_command *x;
+
+ block_signals(0); /* 0: called 1st time */
+ x = find_builtin(optarg);
+ if (x) { /* paranoia */
+ G.global_argc -= builtin_argc; /* skip [BARGV...] "" */
+ G.global_argv += builtin_argc;
+ G.global_argv[-1] = NULL; /* replace "" */
+ G.last_exitcode = x->function(argv + optind - 1);
+ }
+ goto final_return;
+ }
+ if (!G.global_argv[0]) {
+ /* -c 'script' (no params): prevent empty $0 */
+ G.global_argv--; /* points to argv[i] of 'script' */
+ G.global_argv[0] = argv[0];
+ G.global_argc--;
+ } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */
block_signals(0); /* 0: called 1st time */
parse_and_run_string(optarg);
goto final_return;
G.last_bg_pid = bb_strtou(optarg, &optarg, 16);
optarg++;
G.last_exitcode = bb_strtou(optarg, &optarg, 16);
+ optarg++;
+ builtin_argc = bb_strtou(optarg, &optarg, 16);
# if ENABLE_HUSH_LOOPS
optarg++;
G.depth_of_loop = bb_strtou(optarg, &optarg, 16);
/*
* Built-ins
*/
-static int builtin_true(char **argv UNUSED_PARAM)
+static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM)
{
return 0;
}
-static int builtin_test(char **argv)
+static int FAST_FUNC builtin_test(char **argv)
{
int argc = 0;
while (*argv) {
return test_main(argc, argv - argc);
}
-static int builtin_echo(char **argv)
+static int FAST_FUNC builtin_echo(char **argv)
{
int argc = 0;
while (*argv) {
return echo_main(argc, argv - argc);
}
-static int builtin_eval(char **argv)
+static int FAST_FUNC builtin_eval(char **argv)
{
int rcode = EXIT_SUCCESS;
return rcode;
}
-static int builtin_cd(char **argv)
+static int FAST_FUNC builtin_cd(char **argv)
{
const char *newdir = argv[1];
if (newdir == NULL) {
bb_perror_msg("cd: %s", newdir);
return EXIT_FAILURE;
}
- set_cwd();
+ get_cwd(1);
return EXIT_SUCCESS;
}
-static int builtin_exec(char **argv)
+static int FAST_FUNC builtin_exec(char **argv)
{
if (*++argv == NULL)
return EXIT_SUCCESS; /* bash does this */
}
}
-static int builtin_exit(char **argv)
+static int FAST_FUNC builtin_exit(char **argv)
{
debug_printf_exec("%s()\n", __func__);
static void print_escaped(const char *s)
{
+ if (*s == '\'')
+ goto squote;
do {
- if (*s != '\'') {
- const char *p;
-
- p = strchrnul(s, '\'');
- /* print 'xxxx', possibly just '' */
- printf("'%.*s'", (int)(p - s), s);
- if (*p == '\0')
- break;
- s = p;
- }
+ const char *p = strchrnul(s, '\'');
+ /* print 'xxxx', possibly just '' */
+ printf("'%.*s'", (int)(p - s), s);
+ if (*p == '\0')
+ break;
+ s = p;
+ squote:
/* s points to '; print "'''...'''" */
putchar('"');
do putchar('\''); while (*++s == '\'');
} while (*++argv);
}
-static int builtin_export(char **argv)
+static int FAST_FUNC builtin_export(char **argv)
{
unsigned opt_unexport;
- if (argv[1] == NULL) {
+#if ENABLE_HUSH_EXPORT_N
+ /* "!": do not abort on errors */
+ opt_unexport = getopt32(argv, "!n");
+ if (opt_unexport == (uint32_t)-1)
+ return EXIT_FAILURE;
+ argv += optind;
+#else
+ opt_unexport = 0;
+ argv++;
+#endif
+
+ if (argv[0] == NULL) {
char **e = environ;
if (e) {
while (*e) {
return EXIT_SUCCESS;
}
-#if ENABLE_HUSH_EXPORT_N
- /* "!": do not abort on errors */
- /* "+": stop at 1st non-option */
- opt_unexport = getopt32(argv, "!+n");
- if (opt_unexport == (unsigned)-1)
- return EXIT_FAILURE;
- argv += optind;
-#else
- opt_unexport = 0;
- argv++;
-#endif
-
helper_export_local(argv, (opt_unexport ? -1 : 1), 0);
return EXIT_SUCCESS;
}
#if ENABLE_HUSH_LOCAL
-static int builtin_local(char **argv)
+static int FAST_FUNC builtin_local(char **argv)
{
if (G.func_nest_level == 0) {
bb_error_msg("%s: not in a function", argv[0]);
}
#endif
-static int builtin_trap(char **argv)
+static int FAST_FUNC builtin_trap(char **argv)
{
int sig;
char *new_cmd;
}
/* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */
-static int builtin_type(char **argv)
+static int FAST_FUNC builtin_type(char **argv)
{
int ret = EXIT_SUCCESS;
#if ENABLE_HUSH_JOB
/* built-in 'fg' and 'bg' handler */
-static int builtin_fg_bg(char **argv)
+static int FAST_FUNC builtin_fg_bg(char **argv)
{
int i, jobnum;
struct pipe *pi;
#endif
#if ENABLE_HUSH_HELP
-static int builtin_help(char **argv UNUSED_PARAM)
+static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM)
{
const struct built_in_command *x;
- printf("\n"
+ printf(
"Built-in commands:\n"
"------------------\n");
- for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
- printf("%s\t%s\n", x->cmd, x->descr);
+ for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) {
+ if (x->descr)
+ printf("%s\t%s\n", x->cmd, x->descr);
}
- printf("\n\n");
+ bb_putchar('\n');
return EXIT_SUCCESS;
}
#endif
#if ENABLE_HUSH_JOB
-static int builtin_jobs(char **argv UNUSED_PARAM)
+static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM)
{
struct pipe *job;
const char *status_string;
#endif
#if HUSH_DEBUG
-static int builtin_memleak(char **argv UNUSED_PARAM)
+static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM)
{
void *p;
unsigned long l;
if (!G.memleak_value)
G.memleak_value = l;
-
+
l -= G.memleak_value;
if ((long)l < 0)
l = 0;
}
#endif
-static int builtin_pwd(char **argv UNUSED_PARAM)
+static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM)
{
- puts(set_cwd());
+ puts(get_cwd(0));
return EXIT_SUCCESS;
}
-static int builtin_read(char **argv)
+static int FAST_FUNC builtin_read(char **argv)
{
char *string;
const char *name = "REPLY";
*
* So far, we only support "set -- [argument...]" and some of the short names.
*/
-static int builtin_set(char **argv)
+static int FAST_FUNC builtin_set(char **argv)
{
int n;
char **pp, **g_argv;
return EXIT_FAILURE;
}
-static int builtin_shift(char **argv)
+static int FAST_FUNC builtin_shift(char **argv)
{
int n = 1;
if (argv[1]) {
return EXIT_FAILURE;
}
-static int builtin_source(char **argv)
+static int FAST_FUNC builtin_source(char **argv)
{
char *arg_path;
FILE *input;
return G.last_exitcode;
}
-static int builtin_umask(char **argv)
+static int FAST_FUNC builtin_umask(char **argv)
{
int rc;
mode_t mask;
}
/* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */
-static int builtin_unset(char **argv)
+static int FAST_FUNC builtin_unset(char **argv)
{
int ret;
unsigned opts;
}
/* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */
-static int builtin_wait(char **argv)
+static int FAST_FUNC builtin_wait(char **argv)
{
int ret = EXIT_SUCCESS;
int status, sig;
#endif
#if ENABLE_HUSH_LOOPS
-static int builtin_break(char **argv)
+static int FAST_FUNC builtin_break(char **argv)
{
unsigned depth;
if (G.depth_of_loop == 0) {
return EXIT_SUCCESS;
}
-static int builtin_continue(char **argv)
+static int FAST_FUNC builtin_continue(char **argv)
{
G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */
return builtin_break(argv);
#endif
#if ENABLE_HUSH_FUNCTIONS
-static int builtin_return(char **argv)
+static int FAST_FUNC builtin_return(char **argv)
{
int rc;