1 /* vi: set sw=4 ts=4: */
3 * A prototype Bourne shell grammar parser.
4 * Intended to follow the original Thompson and Ritchie
5 * "small and simple is beautiful" philosophy, which
6 * incidentally is a good match to today's BusyBox.
8 * Copyright (C) 2000,2001 Larry Doolittle <larry@doolittle.boa.org>
9 * Copyright (C) 2008,2009 Denys Vlasenko <vda.linux@googlemail.com>
11 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
14 * The parser routines proper are all original material, first
15 * written Dec 2000 and Jan 2001 by Larry Doolittle. The
16 * execution engine, the builtins, and much of the underlying
17 * support has been adapted from busybox-0.49pre's lash, which is
18 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
19 * written by Erik Andersen <andersen@codepoet.org>. That, in turn,
20 * is based in part on ladsh.c, by Michael K. Johnson and Erik W.
21 * Troan, which they placed in the public domain. I don't know
22 * how much of the Johnson/Troan code has survived the repeated
26 * o_addchr derived from similar w_addchar function in glibc-2.2.
27 * parse_redirect, redirect_opt_num, and big chunks of main
28 * and many builtins derived from contributions by Erik Andersen.
29 * Miscellaneous bugfixes from Matt Kraai.
31 * There are two big (and related) architecture differences between
32 * this parser and the lash parser. One is that this version is
33 * actually designed from the ground up to understand nearly all
34 * of the Bourne grammar. The second, consequential change is that
35 * the parser and input reader have been turned inside out. Now,
36 * the parser is in control, and asks for input as needed. The old
37 * way had the input reader in control, and it asked for parsing to
38 * take place as needed. The new way makes it much easier to properly
39 * handle the recursion implicit in the various substitutions, especially
40 * across continuation lines.
43 * grep for "TODO" and fix (some of them are easy)
44 * make complex ${var%...} constructs support optional
45 * make here documents optional
46 * special variables (done: PWD, PPID, RANDOM)
47 * follow IFS rules more precisely, including update semantics
50 * "command" missing features:
51 * command -p CMD: run CMD using default $PATH
52 * (can use this to override standalone shell as well?)
53 * command BLTIN: disables special-ness (e.g. errors do not abort)
54 * command -V CMD1 CMD2 CMD3 (multiple args) (not in standard)
55 * builtins mandated by standards we don't support:
57 * fc -l[nr] [BEG] [END]: list range of commands in history
58 * fc [-e EDITOR] [BEG] [END]: edit/rerun range of commands
59 * fc -s [PAT=REP] [CMD]: rerun CMD, replacing PAT with REP
62 * redirection of stdout+stderr: &> and >&
63 * reserved words: function select
64 * advanced test: [[ ]]
65 * process substitution: <(list) and >(list)
68 * Each EXPR is an arithmetic expression (ARITHMETIC EVALUATION)
69 * If the last arg evaluates to 0, let returns 1; 0 otherwise.
70 * NB: let `echo 'a=a + 1'` - error (IOW: multi-word expansion is used)
72 * The EXPR is evaluated according to ARITHMETIC EVALUATION.
73 * This is exactly equivalent to let "EXPR".
74 * $[EXPR]: synonym for $((EXPR))
75 * indirect expansion: ${!VAR}
76 * substring op on @: ${@:n:m}
79 * Some builtins mandated by standards:
80 * newgrp [GRP]: not a builtin in bash but a suid binary
81 * which spawns a new shell with new group ID
83 * Status of [[ support:
84 * [[ args ]] are CMD_SINGLEWORD_NOGLOB:
85 * v='a b'; [[ $v = 'a b' ]]; echo 0:$?
86 * [[ /bin/n* ]]; echo 0:$?
88 * &&/|| are AND/OR ops, -a/-o are not
89 * quoting needs to be considered (-f is an operator, "-f" and ""-f are not; etc)
90 * = is glob match operator, not equality operator: STR = GLOB
91 * (in GLOB, quoting is significant on char-by-char basis: a*cd"*")
93 * add =~ regex match operator: STR =~ REGEX
96 //config: bool "hush (68 kb)"
99 //config: hush is a small shell. It handles the normal flow control
100 //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops,
101 //config: case/esac. Redirections, here documents, $((arithmetic))
102 //config: and functions are supported.
104 //config: It will compile and work on no-mmu systems.
106 //config: It does not handle select, aliases, tilde expansion,
107 //config: &>file and >&file redirection of stdout+stderr.
109 //config:config HUSH_BASH_COMPAT
110 //config: bool "bash-compatible extensions"
112 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
114 //config:config HUSH_BRACE_EXPANSION
115 //config: bool "Brace expansion"
117 //config: depends on HUSH_BASH_COMPAT
119 //config: Enable {abc,def} extension.
121 //config:config HUSH_LINENO_VAR
122 //config: bool "$LINENO variable"
124 //config: depends on HUSH_BASH_COMPAT
126 //config:config HUSH_BASH_SOURCE_CURDIR
127 //config: bool "'source' and '.' builtins search current directory after $PATH"
128 //config: default n # do not encourage non-standard behavior
129 //config: depends on HUSH_BASH_COMPAT
131 //config: This is not compliant with standards. Avoid if possible.
133 //config:config HUSH_INTERACTIVE
134 //config: bool "Interactive mode"
136 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
138 //config: Enable interactive mode (prompt and command editing).
139 //config: Without this, hush simply reads and executes commands
140 //config: from stdin just like a shell script from a file.
141 //config: No prompt, no PS1/PS2 magic shell variables.
143 //config:config HUSH_SAVEHISTORY
144 //config: bool "Save command history to .hush_history"
146 //config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY
148 //config:config HUSH_JOB
149 //config: bool "Job control"
151 //config: depends on HUSH_INTERACTIVE
153 //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current
154 //config: command (not entire shell), fg/bg builtins work. Without this option,
155 //config: "cmd &" still works by simply spawning a process and immediately
156 //config: prompting for next command (or executing next command in a script),
157 //config: but no separate process group is formed.
159 //config:config HUSH_TICK
160 //config: bool "Support command substitution"
162 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
164 //config: Enable `command` and $(command).
166 //config:config HUSH_IF
167 //config: bool "Support if/then/elif/else/fi"
169 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
171 //config:config HUSH_LOOPS
172 //config: bool "Support for, while and until loops"
174 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
176 //config:config HUSH_CASE
177 //config: bool "Support case ... esac statement"
179 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
181 //config: Enable case ... esac statement. +400 bytes.
183 //config:config HUSH_FUNCTIONS
184 //config: bool "Support funcname() { commands; } syntax"
186 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
188 //config: Enable support for shell functions. +800 bytes.
190 //config:config HUSH_LOCAL
191 //config: bool "local builtin"
193 //config: depends on HUSH_FUNCTIONS
195 //config: Enable support for local variables in functions.
197 //config:config HUSH_RANDOM_SUPPORT
198 //config: bool "Pseudorandom generator and $RANDOM variable"
200 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
202 //config: Enable pseudorandom generator and dynamic variable "$RANDOM".
203 //config: Each read of "$RANDOM" will generate a new pseudorandom value.
205 //config:config HUSH_MODE_X
206 //config: bool "Support 'hush -x' option and 'set -x' command"
208 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
210 //config: This instructs hush to print commands before execution.
211 //config: Adds ~300 bytes.
213 //config:config HUSH_ECHO
214 //config: bool "echo builtin"
216 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
218 //config:config HUSH_PRINTF
219 //config: bool "printf builtin"
221 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
223 //config:config HUSH_TEST
224 //config: bool "test builtin"
226 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
228 //config:config HUSH_HELP
229 //config: bool "help builtin"
231 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
233 //config:config HUSH_EXPORT
234 //config: bool "export builtin"
236 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
238 //config:config HUSH_EXPORT_N
239 //config: bool "Support 'export -n' option"
241 //config: depends on HUSH_EXPORT
243 //config: export -n unexports variables. It is a bash extension.
245 //config:config HUSH_READONLY
246 //config: bool "readonly builtin"
248 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
250 //config: Enable support for read-only variables.
252 //config:config HUSH_KILL
253 //config: bool "kill builtin (supports kill %jobspec)"
255 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
257 //config:config HUSH_WAIT
258 //config: bool "wait builtin"
260 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
262 //config:config HUSH_COMMAND
263 //config: bool "command builtin"
265 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
267 //config:config HUSH_TRAP
268 //config: bool "trap builtin"
270 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
272 //config:config HUSH_TYPE
273 //config: bool "type builtin"
275 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
277 //config:config HUSH_TIMES
278 //config: bool "times builtin"
280 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
282 //config:config HUSH_READ
283 //config: bool "read builtin"
285 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
287 //config:config HUSH_SET
288 //config: bool "set builtin"
290 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
292 //config:config HUSH_UNSET
293 //config: bool "unset builtin"
295 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
297 //config:config HUSH_ULIMIT
298 //config: bool "ulimit builtin"
300 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
302 //config:config HUSH_UMASK
303 //config: bool "umask builtin"
305 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
307 //config:config HUSH_GETOPTS
308 //config: bool "getopts builtin"
310 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
312 //config:config HUSH_MEMLEAK
313 //config: bool "memleak builtin (debugging)"
315 //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
317 //applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP))
318 // APPLET_ODDNAME:name main location suid_type help
319 //applet:IF_SH_IS_HUSH( APPLET_ODDNAME(sh, hush, BB_DIR_BIN, BB_SUID_DROP, hush))
320 //applet:IF_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, BB_DIR_BIN, BB_SUID_DROP, hush))
322 //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o
323 //kbuild:lib-$(CONFIG_SH_IS_HUSH) += hush.o match.o shell_common.o
324 //kbuild:lib-$(CONFIG_BASH_IS_HUSH) += hush.o match.o shell_common.o
325 //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o
327 /* -i (interactive) is also accepted,
328 * but does nothing, therefore not shown in help.
329 * NOMMU-specific options are not meant to be used by users,
330 * therefore we don't show them either.
332 //usage:#define hush_trivial_usage
333 //usage: "[-enxl] [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS] / -s [ARGS]]"
334 //usage:#define hush_full_usage "\n\n"
335 //usage: "Unix shell interpreter"
337 #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
338 || defined(__APPLE__) \
340 # include <malloc.h> /* for malloc_trim */
343 /* #include <dmalloc.h> */
345 # include <fnmatch.h>
347 #include <sys/times.h>
348 #include <sys/utsname.h> /* for setting $HOSTNAME */
350 #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */
352 #include "shell_common.h"
355 #if ENABLE_HUSH_RANDOM_SUPPORT
358 # define CLEAR_RANDOM_T(rnd) ((void)0)
363 #ifndef F_DUPFD_CLOEXEC
364 # define F_DUPFD_CLOEXEC F_DUPFD
367 #if ENABLE_FEATURE_SH_EMBEDDED_SCRIPTS && !(ENABLE_ASH || ENABLE_SH_IS_ASH || ENABLE_BASH_IS_ASH)
368 # include "embedded_scripts.h"
370 # define NUM_SCRIPTS 0
373 /* So far, all bash compat is controlled by one config option */
374 /* Separate defines document which part of code implements what */
375 #define BASH_PATTERN_SUBST ENABLE_HUSH_BASH_COMPAT
376 #define BASH_SUBSTR ENABLE_HUSH_BASH_COMPAT
377 #define BASH_SOURCE ENABLE_HUSH_BASH_COMPAT
378 #define BASH_HOSTNAME_VAR ENABLE_HUSH_BASH_COMPAT
379 #define BASH_EPOCH_VARS ENABLE_HUSH_BASH_COMPAT
380 #define BASH_TEST2 (ENABLE_HUSH_BASH_COMPAT && ENABLE_HUSH_TEST)
381 #define BASH_READ_D ENABLE_HUSH_BASH_COMPAT
385 #define LEAK_HUNTING 0
386 #define BUILD_AS_NOMMU 0
387 /* Enable/disable sanity checks. Ok to enable in production,
388 * only adds a bit of bloat. Set to >1 to get non-production level verbosity.
389 * Keeping 1 for now even in released versions.
392 /* Slightly bigger (+200 bytes), but faster hush.
393 * So far it only enables a trick with counting SIGCHLDs and forks,
394 * which allows us to do fewer waitpid's.
395 * (we can detect a case where neither forks were done nor SIGCHLDs happened
396 * and therefore waitpid will return the same result as last time)
398 #define ENABLE_HUSH_FAST 0
399 /* TODO: implement simplified code for users which do not need ${var%...} ops
400 * So far ${var%...} ops are always enabled:
402 #define ENABLE_HUSH_DOLLAR_OPS 1
407 # undef USE_FOR_NOMMU
410 # define USE_FOR_NOMMU(...) __VA_ARGS__
411 # define USE_FOR_MMU(...)
414 #include "NUM_APPLETS.h"
416 /* STANDALONE does not make sense, and won't compile */
417 # undef CONFIG_FEATURE_SH_STANDALONE
418 # undef ENABLE_FEATURE_SH_STANDALONE
419 # undef IF_FEATURE_SH_STANDALONE
420 # undef IF_NOT_FEATURE_SH_STANDALONE
421 # define ENABLE_FEATURE_SH_STANDALONE 0
422 # define IF_FEATURE_SH_STANDALONE(...)
423 # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__
426 #if !ENABLE_HUSH_INTERACTIVE
427 # undef ENABLE_FEATURE_EDITING
428 # define ENABLE_FEATURE_EDITING 0
429 # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT
430 # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0
431 # undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
432 # define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0
435 /* Do we support ANY keywords? */
436 #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
437 # define HAS_KEYWORDS 1
438 # define IF_HAS_KEYWORDS(...) __VA_ARGS__
439 # define IF_HAS_NO_KEYWORDS(...)
441 # define HAS_KEYWORDS 0
442 # define IF_HAS_KEYWORDS(...)
443 # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__
446 /* If you comment out one of these below, it will be #defined later
447 * to perform debug printfs to stderr: */
448 #define debug_printf(...) do {} while (0)
449 /* Finer-grained debug switches */
450 #define debug_printf_parse(...) do {} while (0)
451 #define debug_printf_heredoc(...) do {} while (0)
452 #define debug_print_tree(a, b) do {} while (0)
453 #define debug_printf_exec(...) do {} while (0)
454 #define debug_printf_env(...) do {} while (0)
455 #define debug_printf_jobs(...) do {} while (0)
456 #define debug_printf_expand(...) do {} while (0)
457 #define debug_printf_varexp(...) do {} while (0)
458 #define debug_printf_glob(...) do {} while (0)
459 #define debug_printf_redir(...) do {} while (0)
460 #define debug_printf_list(...) do {} while (0)
461 #define debug_printf_subst(...) do {} while (0)
462 #define debug_printf_prompt(...) do {} while (0)
463 #define debug_printf_clean(...) do {} while (0)
465 #define ERR_PTR ((void*)(long)1)
467 #define JOB_STATUS_FORMAT "[%u] %-22s %.40s\n"
469 #define _SPECIAL_VARS_STR "_*@$!?#-"
470 #define SPECIAL_VARS_STR ("_*@$!?#-" + 1)
471 #define NUMERIC_SPECVARS_STR ("_*@$!?#-" + 3)
472 #if BASH_PATTERN_SUBST
473 /* Support / and // replace ops */
474 /* Note that // is stored as \ in "encoded" string representation */
475 # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?"
476 # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1)
477 # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5)
479 # define VAR_ENCODED_SUBST_OPS "%#:-=+?"
480 # define VAR_SUBST_OPS "%#:-=+?"
481 # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3)
484 #define SPECIAL_VAR_SYMBOL_STR "\3"
485 #define SPECIAL_VAR_SYMBOL 3
486 /* The "variable" with name "\1" emits string "\3". Testcase: "echo ^C" */
487 #define SPECIAL_VAR_QUOTED_SVS 1
491 static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER;
493 /* This supports saving pointers malloced in vfork child,
494 * to be freed in the parent.
497 typedef struct nommu_save_t {
498 struct variable *old_vars;
500 char **argv_from_re_execing;
513 #if ENABLE_HUSH_LOOPS
520 #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
525 /* three pseudo-keywords support contrived "case" syntax: */
526 RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */
527 RES_MATCH , /* "word)" */
528 RES_CASE_BODY, /* "this command is inside CASE" */
535 typedef struct o_string {
537 int length; /* position where data is appended */
540 /* At least some part of the string was inside '' or "",
541 * possibly empty one: word"", wo''rd etc. */
542 smallint has_quoted_part;
543 smallint has_empty_slot;
544 smallint ended_in_ifs;
547 EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */
549 /* Protect newly added chars against globbing
550 * by prepending \ to *, ?, [, \ */
551 EXP_FLAG_ESC_GLOB_CHARS = 0x1,
553 /* Used for initialization: o_string foo = NULL_O_STRING; */
554 #define NULL_O_STRING { NULL }
556 #ifndef debug_printf_parse
557 static const char *const assignment_flag[] = {
559 "DEFINITELY_ASSIGNMENT",
565 /* We almost can use standard FILE api, but we need an ability to move
566 * its fd when redirects coincide with it. No api exists for that
567 * (RFE for it at https://sourceware.org/bugzilla/show_bug.cgi?id=21902).
568 * HFILE is our internal alternative. Only supports reading.
569 * Since we now can, we incorporate linked list of all opened HFILEs
570 * into the struct (used to be a separate mini-list).
572 typedef struct HFILE {
575 struct HFILE *next_hfile;
580 typedef struct in_str {
587 /* The descrip member of this structure is only used to make
588 * debugging output pretty */
589 static const struct {
591 signed char default_fd;
594 { O_RDONLY, 0, "<" },
595 { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
596 { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" },
597 { O_CREAT|O_RDWR, 1, "<>" },
598 { O_RDONLY, 0, "<<" },
599 /* Should not be needed. Bogus default_fd helps in debugging */
600 /* { O_RDONLY, 77, "<<" }, */
603 struct redir_struct {
604 struct redir_struct *next;
605 char *rd_filename; /* filename */
606 int rd_fd; /* fd to redirect */
607 /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */
609 smallint rd_type; /* (enum redir_type) */
610 /* note: for heredocs, rd_filename contains heredoc delimiter,
611 * and subsequently heredoc itself; and rd_dup is a bitmask:
612 * bit 0: do we need to trim leading tabs?
613 * bit 1: is heredoc quoted (<<'delim' syntax) ?
616 typedef enum redir_type {
618 REDIRECT_OVERWRITE = 1,
621 REDIRECT_HEREDOC = 4,
622 REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */
625 REDIRFD_SYNTAX_ERR = -2,
626 REDIRFD_TO_FILE = -1,
627 /* otherwise, rd_fd is redirected to rd_dup */
629 HEREDOC_SKIPTABS = 1,
635 pid_t pid; /* 0 if exited */
636 unsigned assignment_cnt; /* how many argv[i] are assignments? */
637 #if ENABLE_HUSH_LINENO_VAR
640 smallint cmd_type; /* CMD_xxx */
642 #define CMD_SUBSHELL 1
643 #if BASH_TEST2 || ENABLE_HUSH_LOCAL || ENABLE_HUSH_EXPORT || ENABLE_HUSH_READONLY
644 /* used for "[[ EXPR ]]", and to prevent word splitting and globbing in
647 # define CMD_SINGLEWORD_NOGLOB 2
649 #if ENABLE_HUSH_FUNCTIONS
650 # define CMD_FUNCDEF 3
653 smalluint cmd_exitcode;
654 /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */
657 char *group_as_string;
659 #if ENABLE_HUSH_FUNCTIONS
660 struct function *child_func;
661 /* This field is used to prevent a bug here:
662 * while...do f1() {a;}; f1; f1() {b;}; f1; done
663 * When we execute "f1() {a;}" cmd, we create new function and clear
664 * cmd->group, cmd->group_as_string, cmd->argv[0].
665 * When we execute "f1() {b;}", we notice that f1 exists,
666 * and that its "parent cmd" struct is still "alive",
667 * we put those fields back into cmd->xxx
668 * (struct function has ->parent_cmd ptr to facilitate that).
669 * When we loop back, we can execute "f1() {a;}" again and set f1 correctly.
670 * Without this trick, loop would execute a;b;b;b;...
671 * instead of correct sequence a;b;a;b;...
672 * When command is freed, it severs the link
673 * (sets ->child_func->parent_cmd to NULL).
676 char **argv; /* command name and arguments */
677 /* argv vector may contain variable references (^Cvar^C, ^C0^C etc)
678 * and on execution these are substituted with their values.
679 * Substitution can make _several_ words out of one argv[n]!
680 * Example: argv[0]=='.^C*^C.' here: echo .$*.
681 * References of the form ^C`cmd arg^C are `cmd arg` substitutions.
683 struct redir_struct *redirects; /* I/O redirections */
685 /* Is there anything in this command at all? */
686 #define IS_NULL_CMD(cmd) \
687 (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects)
691 int num_cmds; /* total number of commands in pipe */
692 int alive_cmds; /* number of commands running (not exited) */
693 int stopped_cmds; /* number of commands alive, but stopped */
695 unsigned jobid; /* job number */
696 pid_t pgrp; /* process group ID for the job */
697 char *cmdtext; /* name of job */
699 struct command *cmds; /* array of commands in pipe */
700 smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
701 IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */
702 IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */
704 typedef enum pipe_style {
710 /* Is there anything in this pipe at all? */
711 #define IS_NULL_PIPE(pi) \
712 ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE))
714 /* This holds pointers to the various results of parsing */
715 struct parse_context {
716 /* linked list of pipes */
717 struct pipe *list_head;
718 /* last pipe (being constructed right now) */
720 /* last command in pipe (being constructed right now) */
721 struct command *command;
722 /* last redirect in command->redirects list */
723 struct redir_struct *pending_redirect;
728 smallint is_assignment; /* 0:maybe, 1:yes, 2:no, 3:keyword */
731 smallint ctx_inverted; /* "! cmd | cmd" */
733 smallint ctx_dsemicolon; /* ";;" seen */
735 /* bitmask of FLAG_xxx, for figuring out valid reserved words */
737 /* group we are enclosed in:
738 * example: "if pipe1; pipe2; then pipe3; fi"
739 * when we see "if" or "then", we malloc and copy current context,
740 * and make ->stack point to it. then we parse pipeN.
741 * when closing "then" / fi" / whatever is found,
742 * we move list_head into ->stack->command->group,
743 * copy ->stack into current context, and delete ->stack.
744 * (parsing of { list } and ( list ) doesn't use this method)
746 struct parse_context *stack;
750 MAYBE_ASSIGNMENT = 0,
751 DEFINITELY_ASSIGNMENT = 1,
753 /* Not an assignment, but next word may be: "if v=xyz cmd;" */
757 /* On program start, environ points to initial environment.
758 * putenv adds new pointers into it, unsetenv removes them.
759 * Neither of these (de)allocates the strings.
760 * setenv allocates new strings in malloc space and does putenv,
761 * and thus setenv is unusable (leaky) for shell's purposes */
762 #define setenv(...) setenv_is_leaky_dont_use()
764 struct variable *next;
765 char *varstr; /* points to "name=" portion */
766 int max_len; /* if > 0, name is part of initial env; else name is malloced */
767 uint16_t var_nest_level;
768 smallint flg_export; /* putenv should be done on this var */
769 smallint flg_read_only;
777 #if ENABLE_HUSH_FUNCTIONS
779 struct function *next;
781 struct command *parent_cmd;
784 char *body_as_string;
790 /* set -/+o OPT support. (TODO: make it optional)
791 * bash supports the following opts:
802 * interactive-comments on
820 static const char o_opt_strings[] ALIGN1 =
824 #if ENABLE_HUSH_MODE_X
832 #if ENABLE_HUSH_MODE_X
838 /* "Globals" within this file */
839 /* Sorted roughly by size (smaller offsets == smaller code) */
841 /* interactive_fd != 0 means we are an interactive shell.
842 * If we are, then saved_tty_pgrp can also be != 0, meaning
843 * that controlling tty is available. With saved_tty_pgrp == 0,
844 * job control still works, but terminal signals
845 * (^C, ^Z, ^Y, ^\) won't work at all, and background
846 * process groups can only be created with "cmd &".
847 * With saved_tty_pgrp != 0, hush will use tcsetpgrp()
848 * to give tty to the foreground process group,
849 * and will take it back when the group is stopped (^Z)
852 #if ENABLE_HUSH_INTERACTIVE
853 /* 'interactive_fd' is a fd# open to ctty, if we have one
854 * _AND_ if we decided to act interactively */
856 IF_NOT_FEATURE_EDITING_FANCY_PROMPT(char *PS1;)
857 # define G_interactive_fd (G.interactive_fd)
859 # define G_interactive_fd 0
861 #if ENABLE_FEATURE_EDITING
862 line_input_t *line_input_state;
867 #if ENABLE_HUSH_RANDOM_SUPPORT
873 pid_t saved_tty_pgrp;
874 struct pipe *job_list;
875 # define G_saved_tty_pgrp (G.saved_tty_pgrp)
877 # define G_saved_tty_pgrp 0
879 /* How deeply are we in context where "set -e" is ignored */
881 /* "set -e" rules (do we follow them correctly?):
882 * Exit if pipe, list, or compound command exits with a non-zero status.
883 * Shell does not exit if failed command is part of condition in
884 * if/while, part of && or || list except the last command, any command
885 * in a pipe but the last, or if the command's return value is being
886 * inverted with !. If a compound command other than a subshell returns a
887 * non-zero status because a command failed while -e was being ignored, the
888 * shell does not exit. A trap on ERR, if set, is executed before the shell
889 * exits [ERR is a bashism].
891 * If a compound command or function executes in a context where -e is
892 * ignored, none of the commands executed within are affected by the -e
893 * setting. If a compound command or function sets -e while executing in a
894 * context where -e is ignored, that setting does not have any effect until
895 * the compound command or the command containing the function call completes.
898 char o_opt[NUM_OPT_O];
899 #if ENABLE_HUSH_MODE_X
900 # define G_x_mode (G.o_opt[OPT_O_XTRACE])
906 #if ENABLE_HUSH_INTERACTIVE
907 smallint promptmode; /* 0: PS1, 1: PS2 */
909 smallint flag_SIGINT;
910 #if ENABLE_HUSH_LOOPS
911 smallint flag_break_continue;
913 #if ENABLE_HUSH_FUNCTIONS
914 /* 0: outside of a function (or sourced file)
915 * -1: inside of a function, ok to use return builtin
916 * 1: return is invoked, skip all till end of func
918 smallint flag_return_in_progress;
919 # define G_flag_return_in_progress (G.flag_return_in_progress)
921 # define G_flag_return_in_progress 0
923 smallint exiting; /* used to prevent EXIT trap recursion */
924 /* These support $? */
925 smalluint last_exitcode;
926 smalluint expand_exitcode;
927 smalluint last_bg_pid_exitcode;
929 /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */
930 smalluint global_args_malloced;
931 # define G_global_args_malloced (G.global_args_malloced)
933 # define G_global_args_malloced 0
935 #if ENABLE_HUSH_BASH_COMPAT
936 int dead_job_exitcode; /* for "wait -n" */
938 /* how many non-NULL argv's we have. NB: $# + 1 */
942 char *argv0_for_re_execing;
944 #if ENABLE_HUSH_LOOPS
945 unsigned depth_break_continue;
946 unsigned depth_of_loop;
948 #if ENABLE_HUSH_GETOPTS
949 unsigned getopt_count;
952 char *ifs_whitespace; /* = G.ifs or malloced */
954 struct variable *top_var;
955 char **expanded_assignments;
956 struct variable **shadowed_vars_pp;
957 unsigned var_nest_level;
958 #if ENABLE_HUSH_FUNCTIONS
959 # if ENABLE_HUSH_LOCAL
960 unsigned func_nest_level; /* solely to prevent "local v" in non-functions */
962 struct function *top_func;
964 /* Signal and trap handling */
966 unsigned count_SIGCHLD;
967 unsigned handled_SIGCHLD;
968 smallint we_have_children;
970 #if ENABLE_HUSH_LINENO_VAR
971 unsigned parse_lineno;
972 unsigned execute_lineno;
976 /* Which signals have non-DFL handler (even with no traps set)?
977 * Set at the start to:
978 * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS)
979 * SPECIAL_INTERACTIVE_SIGS are cleared after fork.
980 * The rest is cleared right before execv syscalls.
981 * Other than these two times, never modified.
983 unsigned special_sig_mask;
985 unsigned fatal_sig_mask;
986 # define G_fatal_sig_mask (G.fatal_sig_mask)
988 # define G_fatal_sig_mask 0
991 # if ENABLE_HUSH_FUNCTIONS
994 char **traps; /* char *traps[NSIG] */
995 # define G_traps G.traps
997 # define G_traps ((char**)NULL)
999 sigset_t pending_set;
1000 #if ENABLE_HUSH_MEMLEAK
1001 unsigned long memleak_value;
1003 #if ENABLE_HUSH_MODE_X
1004 unsigned x_mode_depth;
1005 /* "set -x" output should not be redirectable with subsequent 2>FILE.
1006 * We dup fd#2 to x_mode_fd when "set -x" is executed, and use it
1007 * for all subsequent output.
1010 o_string x_mode_buf;
1015 struct sigaction sa;
1016 char optstring_buf[sizeof("eixcs")];
1018 char epoch_buf[sizeof("%lu.nnnnnn") + sizeof(long)*3];
1020 #if ENABLE_FEATURE_EDITING
1021 char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN];
1024 #define G (*ptr_to_globals)
1025 /* Not #defining name to G.name - this quickly gets unwieldy
1026 * (too many defines). Also, I actually prefer to see when a variable
1027 * is global, thus "G." prefix is a useful hint */
1028 #define INIT_G() do { \
1029 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
1030 /* memset(&G.sa, 0, sizeof(G.sa)); */ \
1031 sigfillset(&G.sa.sa_mask); \
1032 G.sa.sa_flags = SA_RESTART; \
1036 /* Function prototypes for builtins */
1037 static int builtin_cd(char **argv) FAST_FUNC;
1038 #if ENABLE_HUSH_ECHO
1039 static int builtin_echo(char **argv) FAST_FUNC;
1041 static int builtin_eval(char **argv) FAST_FUNC;
1042 static int builtin_exec(char **argv) FAST_FUNC;
1043 static int builtin_exit(char **argv) FAST_FUNC;
1044 #if ENABLE_HUSH_EXPORT
1045 static int builtin_export(char **argv) FAST_FUNC;
1047 #if ENABLE_HUSH_READONLY
1048 static int builtin_readonly(char **argv) FAST_FUNC;
1051 static int builtin_fg_bg(char **argv) FAST_FUNC;
1052 static int builtin_jobs(char **argv) FAST_FUNC;
1054 #if ENABLE_HUSH_GETOPTS
1055 static int builtin_getopts(char **argv) FAST_FUNC;
1057 #if ENABLE_HUSH_HELP
1058 static int builtin_help(char **argv) FAST_FUNC;
1060 #if MAX_HISTORY && ENABLE_FEATURE_EDITING
1061 static int builtin_history(char **argv) FAST_FUNC;
1063 #if ENABLE_HUSH_LOCAL
1064 static int builtin_local(char **argv) FAST_FUNC;
1066 #if ENABLE_HUSH_MEMLEAK
1067 static int builtin_memleak(char **argv) FAST_FUNC;
1069 #if ENABLE_HUSH_PRINTF
1070 static int builtin_printf(char **argv) FAST_FUNC;
1072 static int builtin_pwd(char **argv) FAST_FUNC;
1073 #if ENABLE_HUSH_READ
1074 static int builtin_read(char **argv) FAST_FUNC;
1077 static int builtin_set(char **argv) FAST_FUNC;
1079 static int builtin_shift(char **argv) FAST_FUNC;
1080 static int builtin_source(char **argv) FAST_FUNC;
1081 #if ENABLE_HUSH_TEST || BASH_TEST2
1082 static int builtin_test(char **argv) FAST_FUNC;
1084 #if ENABLE_HUSH_TRAP
1085 static int builtin_trap(char **argv) FAST_FUNC;
1087 #if ENABLE_HUSH_TYPE
1088 static int builtin_type(char **argv) FAST_FUNC;
1090 #if ENABLE_HUSH_TIMES
1091 static int builtin_times(char **argv) FAST_FUNC;
1093 static int builtin_true(char **argv) FAST_FUNC;
1094 #if ENABLE_HUSH_UMASK
1095 static int builtin_umask(char **argv) FAST_FUNC;
1097 #if ENABLE_HUSH_UNSET
1098 static int builtin_unset(char **argv) FAST_FUNC;
1100 #if ENABLE_HUSH_KILL
1101 static int builtin_kill(char **argv) FAST_FUNC;
1103 #if ENABLE_HUSH_WAIT
1104 static int builtin_wait(char **argv) FAST_FUNC;
1106 #if ENABLE_HUSH_LOOPS
1107 static int builtin_break(char **argv) FAST_FUNC;
1108 static int builtin_continue(char **argv) FAST_FUNC;
1110 #if ENABLE_HUSH_FUNCTIONS
1111 static int builtin_return(char **argv) FAST_FUNC;
1114 /* Table of built-in functions. They can be forked or not, depending on
1115 * context: within pipes, they fork. As simple commands, they do not.
1116 * When used in non-forking context, they can change global variables
1117 * in the parent shell process. If forked, of course they cannot.
1118 * For example, 'unset foo | whatever' will parse and run, but foo will
1119 * still be set at the end. */
1120 struct built_in_command {
1122 int (*b_function)(char **argv) FAST_FUNC;
1123 #if ENABLE_HUSH_HELP
1124 const char *b_descr;
1125 # define BLTIN(cmd, func, help) { cmd, func, help }
1127 # define BLTIN(cmd, func, help) { cmd, func }
1131 static const struct built_in_command bltins1[] = {
1132 BLTIN("." , builtin_source , "Run commands in file"),
1133 BLTIN(":" , builtin_true , NULL),
1135 BLTIN("bg" , builtin_fg_bg , "Resume job in background"),
1137 #if ENABLE_HUSH_LOOPS
1138 BLTIN("break" , builtin_break , "Exit loop"),
1140 BLTIN("cd" , builtin_cd , "Change directory"),
1141 #if ENABLE_HUSH_LOOPS
1142 BLTIN("continue" , builtin_continue, "Start new loop iteration"),
1144 BLTIN("eval" , builtin_eval , "Construct and run shell command"),
1145 BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"),
1146 BLTIN("exit" , builtin_exit , NULL),
1147 #if ENABLE_HUSH_EXPORT
1148 BLTIN("export" , builtin_export , "Set environment variables"),
1151 BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"),
1153 #if ENABLE_HUSH_GETOPTS
1154 BLTIN("getopts" , builtin_getopts , NULL),
1156 #if ENABLE_HUSH_HELP
1157 BLTIN("help" , builtin_help , NULL),
1159 #if MAX_HISTORY && ENABLE_FEATURE_EDITING
1160 BLTIN("history" , builtin_history , "Show history"),
1163 BLTIN("jobs" , builtin_jobs , "List jobs"),
1165 #if ENABLE_HUSH_KILL
1166 BLTIN("kill" , builtin_kill , "Send signals to processes"),
1168 #if ENABLE_HUSH_LOCAL
1169 BLTIN("local" , builtin_local , "Set local variables"),
1171 #if ENABLE_HUSH_MEMLEAK
1172 BLTIN("memleak" , builtin_memleak , NULL),
1174 #if ENABLE_HUSH_READ
1175 BLTIN("read" , builtin_read , "Input into variable"),
1177 #if ENABLE_HUSH_READONLY
1178 BLTIN("readonly" , builtin_readonly, "Make variables read-only"),
1180 #if ENABLE_HUSH_FUNCTIONS
1181 BLTIN("return" , builtin_return , "Return from function"),
1184 BLTIN("set" , builtin_set , "Set positional parameters"),
1186 BLTIN("shift" , builtin_shift , "Shift positional parameters"),
1188 BLTIN("source" , builtin_source , NULL),
1190 #if ENABLE_HUSH_TIMES
1191 BLTIN("times" , builtin_times , NULL),
1193 #if ENABLE_HUSH_TRAP
1194 BLTIN("trap" , builtin_trap , "Trap signals"),
1196 BLTIN("true" , builtin_true , NULL),
1197 #if ENABLE_HUSH_TYPE
1198 BLTIN("type" , builtin_type , "Show command type"),
1200 #if ENABLE_HUSH_ULIMIT
1201 BLTIN("ulimit" , shell_builtin_ulimit, "Control resource limits"),
1203 #if ENABLE_HUSH_UMASK
1204 BLTIN("umask" , builtin_umask , "Set file creation mask"),
1206 #if ENABLE_HUSH_UNSET
1207 BLTIN("unset" , builtin_unset , "Unset variables"),
1209 #if ENABLE_HUSH_WAIT
1210 BLTIN("wait" , builtin_wait , "Wait for process to finish"),
1213 /* These builtins won't be used if we are on NOMMU and need to re-exec
1214 * (it's cheaper to run an external program in this case):
1216 static const struct built_in_command bltins2[] = {
1217 #if ENABLE_HUSH_TEST
1218 BLTIN("[" , builtin_test , NULL),
1221 BLTIN("[[" , builtin_test , NULL),
1223 #if ENABLE_HUSH_ECHO
1224 BLTIN("echo" , builtin_echo , NULL),
1226 #if ENABLE_HUSH_PRINTF
1227 BLTIN("printf" , builtin_printf , NULL),
1229 BLTIN("pwd" , builtin_pwd , NULL),
1230 #if ENABLE_HUSH_TEST
1231 BLTIN("test" , builtin_test , NULL),
1239 /* prevent disasters with G.debug_indent < 0 */
1240 # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "")
1241 # define debug_enter() (G.debug_indent++)
1242 # define debug_leave() (G.debug_indent--)
1244 # define indent() ((void)0)
1245 # define debug_enter() ((void)0)
1246 # define debug_leave() ((void)0)
1249 #ifndef debug_printf
1250 # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__))
1253 #ifndef debug_printf_parse
1254 # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__))
1257 #ifndef debug_printf_heredoc
1258 # define debug_printf_heredoc(...) (indent(), fdprintf(2, __VA_ARGS__))
1261 #ifndef debug_printf_exec
1262 #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__))
1265 #ifndef debug_printf_env
1266 # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__))
1269 #ifndef debug_printf_jobs
1270 # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__))
1271 # define DEBUG_JOBS 1
1273 # define DEBUG_JOBS 0
1276 #ifndef debug_printf_expand
1277 # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__))
1278 # define DEBUG_EXPAND 1
1280 # define DEBUG_EXPAND 0
1283 #ifndef debug_printf_varexp
1284 # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__))
1287 #ifndef debug_printf_glob
1288 # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__))
1289 # define DEBUG_GLOB 1
1291 # define DEBUG_GLOB 0
1294 #ifndef debug_printf_redir
1295 # define debug_printf_redir(...) (indent(), fdprintf(2, __VA_ARGS__))
1298 #ifndef debug_printf_list
1299 # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__))
1302 #ifndef debug_printf_subst
1303 # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__))
1306 #ifndef debug_printf_prompt
1307 # define debug_printf_prompt(...) (indent(), fdprintf(2, __VA_ARGS__))
1310 #ifndef debug_printf_clean
1311 # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__))
1312 # define DEBUG_CLEAN 1
1314 # define DEBUG_CLEAN 0
1318 static void debug_print_strings(const char *prefix, char **vv)
1321 fdprintf(2, "%s:\n", prefix);
1323 fdprintf(2, " '%s'\n", *vv++);
1326 # define debug_print_strings(prefix, vv) ((void)0)
1330 /* Leak hunting. Use hush_leaktool.sh for post-processing.
1333 static void *xxmalloc(int lineno, size_t size)
1335 void *ptr = xmalloc((size + 0xff) & ~0xff);
1336 fdprintf(2, "line %d: malloc %p\n", lineno, ptr);
1339 static void *xxrealloc(int lineno, void *ptr, size_t size)
1341 ptr = xrealloc(ptr, (size + 0xff) & ~0xff);
1342 fdprintf(2, "line %d: realloc %p\n", lineno, ptr);
1345 static char *xxstrdup(int lineno, const char *str)
1347 char *ptr = xstrdup(str);
1348 fdprintf(2, "line %d: strdup %p\n", lineno, ptr);
1351 static void xxfree(void *ptr)
1353 fdprintf(2, "free %p\n", ptr);
1356 # define xmalloc(s) xxmalloc(__LINE__, s)
1357 # define xrealloc(p, s) xxrealloc(__LINE__, p, s)
1358 # define xstrdup(s) xxstrdup(__LINE__, s)
1359 # define free(p) xxfree(p)
1363 /* Syntax and runtime errors. They always abort scripts.
1364 * In interactive use they usually discard unparsed and/or unexecuted commands
1365 * and return to the prompt.
1366 * HUSH_DEBUG >= 2 prints line number in this file where it was detected.
1369 # define msg_and_die_if_script(lineno, ...) msg_and_die_if_script(__VA_ARGS__)
1370 # define syntax_error(lineno, msg) syntax_error(msg)
1371 # define syntax_error_at(lineno, msg) syntax_error_at(msg)
1372 # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch)
1373 # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s)
1374 # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch)
1377 static void die_if_script(void)
1379 if (!G_interactive_fd) {
1380 if (G.last_exitcode) /* sometines it's 2, not 1 (bash compat) */
1381 xfunc_error_retval = G.last_exitcode;
1386 static void msg_and_die_if_script(unsigned lineno, const char *fmt, ...)
1391 bb_error_msg("hush.c:%u", lineno);
1394 bb_verror_msg(fmt, p, NULL);
1399 static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg)
1402 bb_error_msg("syntax error: %s", msg);
1404 bb_simple_error_msg("syntax error");
1408 static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg)
1410 bb_error_msg("syntax error at '%s'", msg);
1414 static void syntax_error_unterm_str(unsigned lineno UNUSED_PARAM, const char *s)
1416 bb_error_msg("syntax error: unterminated %s", s);
1417 //? source4.tests fails: in bash, echo ${^} in script does not terminate the script
1421 static void syntax_error_unterm_ch(unsigned lineno, char ch)
1423 char msg[2] = { ch, '\0' };
1424 syntax_error_unterm_str(lineno, msg);
1427 static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch)
1433 bb_error_msg("hush.c:%u", lineno);
1435 bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg);
1440 # undef msg_and_die_if_script
1441 # undef syntax_error
1442 # undef syntax_error_at
1443 # undef syntax_error_unterm_ch
1444 # undef syntax_error_unterm_str
1445 # undef syntax_error_unexpected_ch
1447 # define msg_and_die_if_script(...) msg_and_die_if_script(__LINE__, __VA_ARGS__)
1448 # define syntax_error(msg) syntax_error(__LINE__, msg)
1449 # define syntax_error_at(msg) syntax_error_at(__LINE__, msg)
1450 # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch)
1451 # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s)
1452 # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch)
1456 /* Utility functions
1458 /* Replace each \x with x in place, return ptr past NUL. */
1459 static char *unbackslash(char *src)
1461 char *dst = src = strchrnul(src, '\\');
1470 /* else: "\<nul>". Do not delete this backslash.
1471 * Testcase: eval 'echo ok\'
1476 if ((*dst++ = *src++) == '\0')
1482 static char **add_strings_to_strings(char **strings, char **add, int need_to_dup)
1503 v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*));
1504 v[count1 + count2] = NULL;
1507 v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]);
1511 static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup)
1513 char **ptr = add_strings_to_strings(strings, add, need_to_dup);
1514 fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr);
1517 #define add_strings_to_strings(strings, add, need_to_dup) \
1518 xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup)
1521 /* Note: takes ownership of "add" ptr (it is not strdup'ed) */
1522 static char **add_string_to_strings(char **strings, char *add)
1527 return add_strings_to_strings(strings, v, /*dup:*/ 0);
1530 static char **xx_add_string_to_strings(int lineno, char **strings, char *add)
1532 char **ptr = add_string_to_strings(strings, add);
1533 fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr);
1536 #define add_string_to_strings(strings, add) \
1537 xx_add_string_to_strings(__LINE__, strings, add)
1540 static void free_strings(char **strings)
1554 static int dup_CLOEXEC(int fd, int avoid_fd)
1558 newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1);
1560 if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */
1561 fcntl(newfd, F_SETFD, FD_CLOEXEC);
1562 } else { /* newfd < 0 */
1571 static int xdup_CLOEXEC_and_close(int fd, int avoid_fd)
1575 newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1);
1581 /* fd was not open? */
1586 if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */
1587 fcntl(newfd, F_SETFD, FD_CLOEXEC);
1593 /* Manipulating HFILEs */
1594 static HFILE *hfopen(const char *name)
1601 fd = open(name, O_RDONLY | O_CLOEXEC);
1604 if (O_CLOEXEC == 0) /* ancient libc */
1605 close_on_exec_on(fd);
1608 fp = xmalloc(sizeof(*fp));
1612 fp->cur = fp->end = fp->buf;
1613 fp->next_hfile = G.HFILE_list;
1617 static void hfclose(HFILE *fp)
1619 HFILE **pp = &G.HFILE_list;
1623 *pp = cur->next_hfile;
1626 pp = &cur->next_hfile;
1632 static int refill_HFILE_and_getc(HFILE *fp)
1637 /* Already saw EOF */
1640 /* Try to buffer more input */
1642 n = safe_read(fp->fd, fp->buf, sizeof(fp->buf));
1644 bb_simple_perror_msg("read error");
1647 fp->end = fp->buf + n;
1654 return (unsigned char)(*fp->cur++);
1656 /* Inlined for common case of non-empty buffer.
1658 static ALWAYS_INLINE int hfgetc(HFILE *fp)
1660 if (fp->cur < fp->end)
1661 return (unsigned char)(*fp->cur++);
1663 return refill_HFILE_and_getc(fp);
1665 static int move_HFILEs_on_redirect(int fd, int avoid_fd)
1667 HFILE *fl = G.HFILE_list;
1670 /* We use it only on script files, they are all CLOEXEC */
1671 fl->fd = xdup_CLOEXEC_and_close(fd, avoid_fd);
1672 debug_printf_redir("redirect_fd %d: matches a script fd, moving it to %d\n", fd, fl->fd);
1673 return 1; /* "found and moved" */
1675 fl = fl->next_hfile;
1677 #if ENABLE_HUSH_MODE_X
1678 if (G.x_mode_fd > 0 && fd == G.x_mode_fd) {
1679 G.x_mode_fd = xdup_CLOEXEC_and_close(fd, avoid_fd);
1680 return 1; /* "found and moved" */
1683 return 0; /* "not in the list" */
1685 #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU
1686 static void close_all_HFILE_list(void)
1688 HFILE *fl = G.HFILE_list;
1690 /* hfclose would also free HFILE object.
1691 * It is disastrous if we share memory with a vforked parent.
1692 * I'm not sure we never come here after vfork.
1693 * Therefore just close fd, nothing more.
1695 * ">" instead of ">=": we don't close fd#0,
1696 * interactive shell uses hfopen(NULL) as stdin input
1697 * which has fl->fd == 0, but fd#0 gets redirected in pipes.
1698 * If we'd close it here, then e.g. interactive "set | sort"
1699 * with NOFORKed sort, would have sort's input fd closed.
1702 /*hfclose(fl); - unsafe */
1704 fl = fl->next_hfile;
1708 static int fd_in_HFILEs(int fd)
1710 HFILE *fl = G.HFILE_list;
1714 fl = fl->next_hfile;
1720 /* Helpers for setting new $n and restoring them back
1722 typedef struct save_arg_t {
1726 IF_HUSH_SET(smallint sv_g_malloced;)
1729 static void save_and_replace_G_args(save_arg_t *sv, char **argv)
1731 sv->sv_argv0 = argv[0];
1732 sv->sv_g_argv = G.global_argv;
1733 sv->sv_g_argc = G.global_argc;
1734 IF_HUSH_SET(sv->sv_g_malloced = G.global_args_malloced;)
1736 argv[0] = G.global_argv[0]; /* retain $0 */
1737 G.global_argv = argv;
1738 IF_HUSH_SET(G.global_args_malloced = 0;)
1740 G.global_argc = 1 + string_array_len(argv + 1);
1743 static void restore_G_args(save_arg_t *sv, char **argv)
1746 if (G.global_args_malloced) {
1747 /* someone ran "set -- arg1 arg2 ...", undo */
1748 char **pp = G.global_argv;
1749 while (*++pp) /* note: does not free $0 */
1751 free(G.global_argv);
1754 argv[0] = sv->sv_argv0;
1755 G.global_argv = sv->sv_g_argv;
1756 G.global_argc = sv->sv_g_argc;
1757 IF_HUSH_SET(G.global_args_malloced = sv->sv_g_malloced;)
1761 /* Basic theory of signal handling in shell
1762 * ========================================
1763 * This does not describe what hush does, rather, it is current understanding
1764 * what it _should_ do. If it doesn't, it's a bug.
1765 * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap
1767 * Signals are handled only after each pipe ("cmd | cmd | cmd" thing)
1768 * is finished or backgrounded. It is the same in interactive and
1769 * non-interactive shells, and is the same regardless of whether
1770 * a user trap handler is installed or a shell special one is in effect.
1771 * ^C or ^Z from keyboard seems to execute "at once" because it usually
1772 * backgrounds (i.e. stops) or kills all members of currently running
1775 * Wait builtin is interruptible by signals for which user trap is set
1776 * or by SIGINT in interactive shell.
1778 * Trap handlers will execute even within trap handlers. (right?)
1780 * User trap handlers are forgotten when subshell ("(cmd)") is entered,
1781 * except for handlers set to '' (empty string).
1783 * If job control is off, backgrounded commands ("cmd &")
1784 * have SIGINT, SIGQUIT set to SIG_IGN.
1786 * Commands which are run in command substitution ("`cmd`")
1787 * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN.
1789 * Ordinary commands have signals set to SIG_IGN/DFL as inherited
1790 * by the shell from its parent.
1792 * Signals which differ from SIG_DFL action
1793 * (note: child (i.e., [v]forked) shell is not an interactive shell):
1796 * SIGTERM (interactive): ignore
1797 * SIGHUP (interactive):
1798 * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit
1799 * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore
1800 * Note that ^Z is handled not by trapping SIGTSTP, but by seeing
1801 * that all pipe members are stopped. Try this in bash:
1802 * while :; do :; done - ^Z does not background it
1803 * (while :; do :; done) - ^Z backgrounds it
1804 * SIGINT (interactive): wait for last pipe, ignore the rest
1805 * of the command line, show prompt. NB: ^C does not send SIGINT
1806 * to interactive shell while shell is waiting for a pipe,
1807 * since shell is bg'ed (is not in foreground process group).
1808 * Example 1: this waits 5 sec, but does not execute ls:
1809 * "echo $$; sleep 5; ls -l" + "kill -INT <pid>"
1810 * Example 2: this does not wait and does not execute ls:
1811 * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>"
1812 * Example 3: this does not wait 5 sec, but executes ls:
1813 * "sleep 5; ls -l" + press ^C
1814 * Example 4: this does not wait and does not execute ls:
1815 * "sleep 5 & wait; ls -l" + press ^C
1817 * (What happens to signals which are IGN on shell start?)
1818 * (What happens with signal mask on shell start?)
1820 * Old implementation
1821 * ==================
1822 * We use in-kernel pending signal mask to determine which signals were sent.
1823 * We block all signals which we don't want to take action immediately,
1824 * i.e. we block all signals which need to have special handling as described
1825 * above, and all signals which have traps set.
1826 * After each pipe execution, we extract any pending signals via sigtimedwait()
1829 * unsigned special_sig_mask: a mask of such "special" signals
1830 * sigset_t blocked_set: current blocked signal set
1833 * clear bit in blocked_set unless it is also in special_sig_mask
1834 * "trap 'cmd' SIGxxx":
1835 * set bit in blocked_set (even if 'cmd' is '')
1836 * after [v]fork, if we plan to be a shell:
1837 * unblock signals with special interactive handling
1838 * (child shell is not interactive),
1839 * unset all traps except '' (note: regardless of child shell's type - {}, (), etc)
1840 * after [v]fork, if we plan to exec:
1841 * POSIX says fork clears pending signal mask in child - no need to clear it.
1842 * Restore blocked signal set to one inherited by shell just prior to exec.
1844 * Note: as a result, we do not use signal handlers much. The only uses
1845 * are to count SIGCHLDs
1846 * and to restore tty pgrp on signal-induced exit.
1849 * Standard says "When a subshell is entered, traps that are not being ignored
1850 * are set to the default actions". bash interprets it so that traps which
1851 * are set to '' (ignore) are NOT reset to defaults. We do the same.
1853 * Problem: the above approach makes it unwieldy to catch signals while
1854 * we are in read builtin, or while we read commands from stdin:
1855 * masked signals are not visible!
1857 * New implementation
1858 * ==================
1859 * We record each signal we are interested in by installing signal handler
1860 * for them - a bit like emulating kernel pending signal mask in userspace.
1861 * We are interested in: signals which need to have special handling
1862 * as described above, and all signals which have traps set.
1863 * Signals are recorded in pending_set.
1864 * After each pipe execution, we extract any pending signals
1867 * unsigned special_sig_mask: a mask of shell-special signals.
1868 * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp.
1869 * char *traps[sig] if trap for sig is set (even if it's '').
1870 * sigset_t pending_set: set of sigs we received.
1873 * if sig is in special_sig_mask, set handler back to:
1874 * record_pending_signo, or to IGN if it's a tty stop signal
1875 * if sig is in fatal_sig_mask, set handler back to sigexit.
1876 * else: set handler back to SIG_DFL
1877 * "trap 'cmd' SIGxxx":
1878 * set handler to record_pending_signo.
1880 * set handler to SIG_IGN.
1881 * after [v]fork, if we plan to be a shell:
1882 * set signals with special interactive handling to SIG_DFL
1883 * (because child shell is not interactive),
1884 * unset all traps except '' (note: regardless of child shell's type - {}, (), etc)
1885 * after [v]fork, if we plan to exec:
1886 * POSIX says fork clears pending signal mask in child - no need to clear it.
1888 * To make wait builtin interruptible, we handle SIGCHLD as special signal,
1889 * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it.
1892 * Standard says "When a subshell is entered, traps that are not being ignored
1893 * are set to the default actions". bash interprets it so that traps which
1894 * are set to '' (ignore) are NOT reset to defaults. We do the same.
1897 SPECIAL_INTERACTIVE_SIGS = 0
1902 SPECIAL_JOBSTOP_SIGS = 0
1911 static void record_pending_signo(int sig)
1913 sigaddset(&G.pending_set, sig);
1914 #if ENABLE_HUSH_FAST
1915 if (sig == SIGCHLD) {
1917 //bb_error_msg("[%d] SIGCHLD_handler: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
1922 static sighandler_t install_sighandler(int sig, sighandler_t handler)
1924 struct sigaction old_sa;
1926 /* We could use signal() to install handlers... almost:
1927 * except that we need to mask ALL signals while handlers run.
1928 * I saw signal nesting in strace, race window isn't small.
1929 * SA_RESTART is also needed, but in Linux, signal()
1930 * sets SA_RESTART too.
1932 /* memset(&G.sa, 0, sizeof(G.sa)); - already done */
1933 /* sigfillset(&G.sa.sa_mask); - already done */
1934 /* G.sa.sa_flags = SA_RESTART; - already done */
1935 G.sa.sa_handler = handler;
1936 sigaction(sig, &G.sa, &old_sa);
1937 return old_sa.sa_handler;
1940 static void hush_exit(int exitcode) NORETURN;
1942 static void restore_ttypgrp_and__exit(void) NORETURN;
1943 static void restore_ttypgrp_and__exit(void)
1945 /* xfunc has failed! die die die */
1946 /* no EXIT traps, this is an escape hatch! */
1948 hush_exit(xfunc_error_retval);
1953 /* Needed only on some libc:
1954 * It was observed that on exit(), fgetc'ed buffered data
1955 * gets "unwound" via lseek(fd, -NUM, SEEK_CUR).
1956 * With the net effect that even after fork(), not vfork(),
1957 * exit() in NOEXECed applet in "sh SCRIPT":
1958 * noexec_applet_here
1959 * echo END_OF_SCRIPT
1960 * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT".
1961 * This makes "echo END_OF_SCRIPT" executed twice.
1962 * Similar problems can be seen with msg_and_die_if_script() -> xfunc_die()
1963 * and in `cmd` handling.
1964 * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit():
1966 static void fflush_and__exit(void) NORETURN;
1967 static void fflush_and__exit(void)
1970 _exit(xfunc_error_retval);
1973 /* After [v]fork, in child: do not restore tty pgrp on xfunc death */
1974 # define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit)
1975 /* After [v]fork, in parent: restore tty pgrp on xfunc death */
1976 # define enable_restore_tty_pgrp_on_exit() (die_func = restore_ttypgrp_and__exit)
1978 /* Restores tty foreground process group, and exits.
1979 * May be called as signal handler for fatal signal
1980 * (will resend signal to itself, producing correct exit state)
1981 * or called directly with -EXITCODE.
1982 * We also call it if xfunc is exiting.
1984 static void sigexit(int sig) NORETURN;
1985 static void sigexit(int sig)
1987 /* Careful: we can end up here after [v]fork. Do not restore
1988 * tty pgrp then, only top-level shell process does that */
1989 if (G_saved_tty_pgrp && getpid() == G.root_pid) {
1990 /* Disable all signals: job control, SIGPIPE, etc.
1991 * Mostly paranoid measure, to prevent infinite SIGTTOU.
1993 sigprocmask_allsigs(SIG_BLOCK);
1994 tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp);
1997 /* Not a signal, just exit */
2001 kill_myself_with_sig(sig); /* does not return */
2005 # define disable_restore_tty_pgrp_on_exit() ((void)0)
2006 # define enable_restore_tty_pgrp_on_exit() ((void)0)
2010 static sighandler_t pick_sighandler(unsigned sig)
2012 sighandler_t handler = SIG_DFL;
2013 if (sig < sizeof(unsigned)*8) {
2014 unsigned sigmask = (1 << sig);
2018 if (G_fatal_sig_mask & sigmask)
2022 /* sig has special handling? */
2023 if (G.special_sig_mask & sigmask) {
2024 handler = record_pending_signo;
2025 /* TTIN/TTOU/TSTP can't be set to record_pending_signo
2026 * in order to ignore them: they will be raised
2027 * in an endless loop when we try to do some
2028 * terminal ioctls! We do have to _ignore_ these.
2030 if (SPECIAL_JOBSTOP_SIGS & sigmask)
2037 /* Restores tty foreground process group, and exits. */
2038 static void hush_exit(int exitcode)
2040 #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
2041 if (G.line_input_state)
2042 save_history(G.line_input_state);
2046 if (G.exiting <= 0 && G_traps && G_traps[0] && G_traps[0][0]) {
2048 /* argv[0] is unused */
2049 argv[1] = xstrdup(G_traps[0]); /* copy, since EXIT trap handler may modify G_traps[0] */
2051 G.exiting = 1; /* prevent EXIT trap recursion */
2052 /* Note: G_traps[0] is not cleared!
2053 * "trap" will still show it, if executed
2058 #if ENABLE_FEATURE_CLEAN_UP
2060 struct variable *cur_var;
2061 if (G.cwd != bb_msg_unknown)
2063 cur_var = G.top_var;
2065 struct variable *tmp = cur_var;
2066 if (!cur_var->max_len)
2067 free(cur_var->varstr);
2068 cur_var = cur_var->next;
2076 sigexit(- (exitcode & 0xff));
2083 //TODO: return a mask of ALL handled sigs?
2084 static int check_and_run_traps(void)
2091 if (sigisemptyset(&G.pending_set))
2096 if (sigismember(&G.pending_set, sig)) {
2097 sigdelset(&G.pending_set, sig);
2100 } while (sig < NSIG);
2103 #if ENABLE_HUSH_TRAP
2104 if (G_traps && G_traps[sig]) {
2105 debug_printf_exec("%s: sig:%d handler:'%s'\n", __func__, sig, G.traps[sig]);
2106 if (G_traps[sig][0]) {
2107 /* We have user-defined handler */
2108 smalluint save_rcode;
2110 /* argv[0] is unused */
2111 argv[1] = xstrdup(G_traps[sig]);
2112 /* why strdup? trap can modify itself: trap 'trap "echo oops" INT' INT */
2114 save_rcode = G.last_exitcode;
2117 G.last_exitcode = save_rcode;
2118 # if ENABLE_HUSH_FUNCTIONS
2119 if (G.return_exitcode >= 0) {
2120 debug_printf_exec("trap exitcode:%d\n", G.return_exitcode);
2121 G.last_exitcode = G.return_exitcode;
2125 } /* else: "" trap, ignoring signal */
2129 /* not a trap: special action */
2132 debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig);
2138 //TODO: why are we doing this? ash and dash don't do this,
2139 //they have no handler for SIGHUP at all,
2140 //they rely on kernel to send SIGHUP+SIGCONT to orphaned process groups
2142 debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig);
2143 /* bash is observed to signal whole process groups,
2144 * not individual processes */
2145 for (job = G.job_list; job; job = job->next) {
2148 debug_printf_exec("HUPing pgrp %d\n", job->pgrp);
2149 if (kill(- job->pgrp, SIGHUP) == 0)
2150 kill(- job->pgrp, SIGCONT);
2155 #if ENABLE_HUSH_FAST
2157 debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig);
2159 //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
2161 * We don't do 'last_sig = sig' here -> NOT returning this sig.
2162 * This simplifies wait builtin a bit.
2166 default: /* ignored: */
2167 debug_printf_exec("%s: sig:%d default handling is to ignore\n", __func__, sig);
2168 /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */
2170 * We don't do 'last_sig = sig' here -> NOT returning this sig.
2171 * Example: wait is not interrupted by TERM
2172 * in interactive shell, because TERM is ignored.
2181 static const char *get_cwd(int force)
2183 if (force || G.cwd == NULL) {
2184 /* xrealloc_getcwd_or_warn(arg) calls free(arg),
2185 * we must not try to free(bb_msg_unknown) */
2186 if (G.cwd == bb_msg_unknown)
2188 G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd);
2190 G.cwd = bb_msg_unknown;
2197 * Shell and environment variable support
2199 static struct variable **get_ptr_to_local_var(const char *name, unsigned len)
2201 struct variable **pp;
2202 struct variable *cur;
2205 while ((cur = *pp) != NULL) {
2206 if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=')
2213 static const char* FAST_FUNC get_local_var_value(const char *name)
2215 struct variable **vpp;
2216 unsigned len = strlen(name);
2218 if (G.expanded_assignments) {
2219 char **cpp = G.expanded_assignments;
2222 if (strncmp(cp, name, len) == 0 && cp[len] == '=')
2223 return cp + len + 1;
2228 vpp = get_ptr_to_local_var(name, len);
2230 return (*vpp)->varstr + len + 1;
2232 if (strcmp(name, "PPID") == 0)
2233 return utoa(G.root_ppid);
2234 // bash compat: UID? EUID?
2235 #if ENABLE_HUSH_RANDOM_SUPPORT
2236 if (strcmp(name, "RANDOM") == 0)
2237 return utoa(next_random(&G.random_gen));
2239 #if ENABLE_HUSH_LINENO_VAR
2240 if (strcmp(name, "LINENO") == 0)
2241 return utoa(G.execute_lineno);
2245 const char *fmt = NULL;
2246 if (strcmp(name, "EPOCHSECONDS") == 0)
2248 else if (strcmp(name, "EPOCHREALTIME") == 0)
2252 gettimeofday(&tv, NULL);
2253 sprintf(G.epoch_buf, fmt, (unsigned long)tv.tv_sec,
2254 (unsigned)tv.tv_usec);
2262 #if ENABLE_HUSH_GETOPTS
2263 static void handle_changed_special_names(const char *name, unsigned name_len)
2265 if (name_len == 6) {
2266 if (strncmp(name, "OPTIND", 6) == 0) {
2273 /* Do not even bother evaluating arguments */
2274 # define handle_changed_special_names(...) ((void)0)
2277 /* str holds "NAME=VAL" and is expected to be malloced.
2278 * We take ownership of it.
2280 #define SETFLAG_EXPORT (1 << 0)
2281 #define SETFLAG_UNEXPORT (1 << 1)
2282 #define SETFLAG_MAKE_RO (1 << 2)
2283 #define SETFLAG_VARLVL_SHIFT 3
2284 static int set_local_var(char *str, unsigned flags)
2286 struct variable **cur_pp;
2287 struct variable *cur;
2288 char *free_me = NULL;
2292 unsigned local_lvl = (flags >> SETFLAG_VARLVL_SHIFT);
2294 eq_sign = strchr(str, '=');
2295 if (HUSH_DEBUG && !eq_sign)
2296 bb_simple_error_msg_and_die("BUG in setvar");
2298 name_len = eq_sign - str + 1; /* including '=' */
2299 cur_pp = &G.top_var;
2300 while ((cur = *cur_pp) != NULL) {
2301 if (strncmp(cur->varstr, str, name_len) != 0) {
2302 cur_pp = &cur->next;
2306 /* We found an existing var with this name */
2307 if (cur->flg_read_only) {
2308 bb_error_msg("%s: readonly variable", str);
2310 //NOTE: in bash, assignment in "export READONLY_VAR=Z" fails, and sets $?=1,
2311 //but export per se succeeds (does put the var in env). We don't mimic that.
2314 if (flags & SETFLAG_UNEXPORT) { // && cur->flg_export ?
2315 debug_printf_env("%s: unsetenv '%s'\n", __func__, str);
2320 if (cur->var_nest_level < local_lvl) {
2321 /* bash 3.2.33(1) and exported vars:
2323 * # f() { local z=a; env | grep ^z; }
2329 if (cur->flg_export)
2330 flags |= SETFLAG_EXPORT;
2331 /* New variable is local ("local VAR=VAL" or
2333 * and existing one is global, or local
2334 * on a lower level that new one.
2335 * Remove it from global variable list:
2337 *cur_pp = cur->next;
2338 if (G.shadowed_vars_pp) {
2339 /* Save in "shadowed" list */
2340 debug_printf_env("shadowing %s'%s'/%u by '%s'/%u\n",
2341 cur->flg_export ? "exported " : "",
2342 cur->varstr, cur->var_nest_level, str, local_lvl
2344 cur->next = *G.shadowed_vars_pp;
2345 *G.shadowed_vars_pp = cur;
2347 /* Came from pseudo_exec_argv(), no need to save: delete it */
2348 debug_printf_env("shadow-deleting %s'%s'/%u by '%s'/%u\n",
2349 cur->flg_export ? "exported " : "",
2350 cur->varstr, cur->var_nest_level, str, local_lvl
2352 if (cur->max_len == 0) /* allocated "VAR=VAL"? */
2353 free_me = cur->varstr; /* then free it later */
2359 if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) {
2360 debug_printf_env("assignement '%s' does not change anything\n", str);
2366 /* Replace the value in the found "struct variable" */
2367 if (cur->max_len != 0) {
2368 if (cur->max_len >= strnlen(str, cur->max_len + 1)) {
2369 /* This one is from startup env, reuse space */
2370 debug_printf_env("reusing startup env for '%s'\n", str);
2371 strcpy(cur->varstr, str);
2376 goto set_str_and_exp;
2378 /* max_len == 0 signifies "malloced" var, which we can
2379 * (and have to) free. But we can't free(cur->varstr) here:
2380 * if cur->flg_export is 1, it is in the environment.
2381 * We should either unsetenv+free, or wait until putenv,
2382 * then putenv(new)+free(old).
2384 free_me = cur->varstr;
2385 goto set_str_and_exp;
2388 /* Not found or shadowed - create new variable struct */
2389 debug_printf_env("%s: alloc new var '%s'/%u\n", __func__, str, local_lvl);
2390 cur = xzalloc(sizeof(*cur));
2391 cur->var_nest_level = local_lvl;
2392 cur->next = *cur_pp;
2398 #if !BB_MMU || ENABLE_HUSH_READONLY
2399 if (flags & SETFLAG_MAKE_RO) {
2400 cur->flg_read_only = 1;
2403 if (flags & SETFLAG_EXPORT)
2404 cur->flg_export = 1;
2406 if (cur->flg_export) {
2407 if (flags & SETFLAG_UNEXPORT) {
2408 cur->flg_export = 0;
2409 /* unsetenv was already done */
2411 debug_printf_env("%s: putenv '%s'/%u\n", __func__, cur->varstr, cur->var_nest_level);
2412 retval = putenv(cur->varstr);
2413 /* fall through to "free(free_me)" -
2414 * only now we can free old exported malloced string
2420 handle_changed_special_names(cur->varstr, name_len - 1);
2425 static void FAST_FUNC set_local_var_from_halves(const char *name, const char *val)
2427 char *var = xasprintf("%s=%s", name, val);
2428 set_local_var(var, /*flag:*/ 0);
2431 /* Used at startup and after each cd */
2432 static void set_pwd_var(unsigned flag)
2434 set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), flag);
2437 #if ENABLE_HUSH_UNSET || ENABLE_HUSH_GETOPTS
2438 static int unset_local_var_len(const char *name, int name_len)
2440 struct variable *cur;
2441 struct variable **cur_pp;
2443 cur_pp = &G.top_var;
2444 while ((cur = *cur_pp) != NULL) {
2445 if (strncmp(cur->varstr, name, name_len) == 0
2446 && cur->varstr[name_len] == '='
2448 if (cur->flg_read_only) {
2449 bb_error_msg("%s: readonly variable", name);
2450 return EXIT_FAILURE;
2453 *cur_pp = cur->next;
2454 debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr);
2455 bb_unsetenv(cur->varstr);
2462 cur_pp = &cur->next;
2465 /* Handle "unset LINENO" et al even if did not find the variable to unset */
2466 handle_changed_special_names(name, name_len);
2468 return EXIT_SUCCESS;
2471 static int unset_local_var(const char *name)
2473 return unset_local_var_len(name, strlen(name));
2479 * Helpers for "var1=val1 var2=val2 cmd" feature
2481 static void add_vars(struct variable *var)
2483 struct variable *next;
2487 var->next = G.top_var;
2489 if (var->flg_export) {
2490 debug_printf_env("%s: restoring exported '%s'/%u\n", __func__, var->varstr, var->var_nest_level);
2491 putenv(var->varstr);
2493 debug_printf_env("%s: restoring variable '%s'/%u\n", __func__, var->varstr, var->var_nest_level);
2499 /* We put strings[i] into variable table and possibly putenv them.
2500 * If variable is read only, we can free the strings[i]
2501 * which attempts to overwrite it.
2502 * The strings[] vector itself is freed.
2504 static void set_vars_and_save_old(char **strings)
2513 struct variable *var_p;
2514 struct variable **var_pp;
2517 eq = strchr(*s, '=');
2518 if (HUSH_DEBUG && !eq)
2519 bb_simple_error_msg_and_die("BUG in varexp4");
2520 var_pp = get_ptr_to_local_var(*s, eq - *s);
2523 if (var_p->flg_read_only) {
2525 bb_error_msg("%s: readonly variable", *s);
2527 * "VAR=V BLTIN" unsets VARs after BLTIN completes.
2528 * If VAR is readonly, leaving it in the list
2529 * after asssignment error (msg above)
2530 * causes doubled error message later, on unset.
2532 debug_printf_env("removing/freeing '%s' element\n", *s);
2535 do { *p = p[1]; p++; } while (*p);
2538 /* below, set_local_var() with nest level will
2539 * "shadow" (remove) this variable from
2540 * global linked list.
2543 debug_printf_env("%s: env override '%s'/%u\n", __func__, *s, G.var_nest_level);
2544 set_local_var(*s, (G.var_nest_level << SETFLAG_VARLVL_SHIFT) | SETFLAG_EXPORT);
2555 static void reinit_unicode_for_hush(void)
2557 /* Unicode support should be activated even if LANG is set
2558 * _during_ shell execution, not only if it was set when
2559 * shell was started. Therefore, re-check LANG every time:
2561 if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
2562 || ENABLE_UNICODE_USING_LOCALE
2564 const char *s = get_local_var_value("LC_ALL");
2565 if (!s) s = get_local_var_value("LC_CTYPE");
2566 if (!s) s = get_local_var_value("LANG");
2572 * in_str support (strings, and "strings" read from files).
2575 #if ENABLE_HUSH_INTERACTIVE
2576 /* To test correct lineedit/interactive behavior, type from command line:
2582 * It exercises a lot of corner cases.
2584 static const char *setup_prompt_string(void)
2586 const char *prompt_str;
2588 debug_printf_prompt("%s promptmode:%d\n", __func__, G.promptmode);
2590 # if ENABLE_FEATURE_EDITING_FANCY_PROMPT
2591 prompt_str = get_local_var_value(G.promptmode == 0 ? "PS1" : "PS2");
2595 prompt_str = "> "; /* if PS2, else... */
2596 if (G.promptmode == 0) { /* PS1 */
2597 /* No fancy prompts supported, (re)generate "CURDIR $ " by hand */
2599 /* bash uses $PWD value, even if it is set by user.
2600 * It uses current dir only if PWD is unset.
2601 * We always use current dir. */
2602 G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#');
2605 debug_printf("prompt_str '%s'\n", prompt_str);
2608 static int get_user_input(struct in_str *i)
2611 const char *prompt_str;
2613 prompt_str = setup_prompt_string();
2614 # if ENABLE_FEATURE_EDITING
2616 reinit_unicode_for_hush();
2617 if (G.flag_SIGINT) {
2618 /* There was ^C'ed, make it look prettier: */
2622 /* buglet: SIGINT will not make new prompt to appear _at once_,
2623 * only after <Enter>. (^C works immediately) */
2624 r = read_line_input(G.line_input_state, prompt_str,
2625 G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1
2627 /* read_line_input intercepts ^C, "convert" it to SIGINT */
2630 check_and_run_traps();
2631 if (r != 0 && !G.flag_SIGINT)
2633 /* ^C or SIGINT: repeat */
2634 /* bash prints ^C even on real SIGINT (non-kbd generated) */
2635 write(STDOUT_FILENO, "^C", 2);
2636 G.last_exitcode = 128 + SIGINT;
2639 /* EOF/error detected */
2641 i->peek_buf[0] = r = EOF;
2644 i->p = G.user_input_buf;
2645 return (unsigned char)*i->p++;
2649 if (i->last_char == '\0' || i->last_char == '\n') {
2650 /* Why check_and_run_traps here? Try this interactively:
2651 * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) &
2652 * $ <[enter], repeatedly...>
2653 * Without check_and_run_traps, handler never runs.
2655 check_and_run_traps();
2656 fputs(prompt_str, stdout);
2659 //FIXME: here ^C or SIGINT will have effect only after <Enter>
2660 r = hfgetc(i->file);
2661 /* In !ENABLE_FEATURE_EDITING we don't use read_line_input,
2662 * no ^C masking happens during fgetc, no special code for ^C:
2663 * it generates SIGINT as usual.
2665 check_and_run_traps();
2667 G.last_exitcode = 128 + SIGINT;
2674 /* This is the magic location that prints prompts
2675 * and gets data back from the user */
2676 static int fgetc_interactive(struct in_str *i)
2679 /* If it's interactive stdin, get new line. */
2680 if (G_interactive_fd && i->file == G.HFILE_stdin) {
2681 /* Returns first char (or EOF), the rest is in i->p[] */
2682 ch = get_user_input(i);
2683 G.promptmode = 1; /* PS2 */
2684 debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode);
2686 /* Not stdin: script file, sourced file, etc */
2687 do ch = hfgetc(i->file); while (ch == '\0');
2692 static ALWAYS_INLINE int fgetc_interactive(struct in_str *i)
2695 do ch = hfgetc(i->file); while (ch == '\0');
2698 #endif /* INTERACTIVE */
2700 static int i_getch(struct in_str *i)
2705 /* string-based in_str */
2706 ch = (unsigned char)*i->p;
2715 /* FILE-based in_str */
2717 #if ENABLE_FEATURE_EDITING
2718 /* This can be stdin, check line editing char[] buffer */
2719 if (i->p && *i->p != '\0') {
2720 ch = (unsigned char)*i->p++;
2724 /* peek_buf[] is an int array, not char. Can contain EOF. */
2725 ch = i->peek_buf[0];
2727 int ch2 = i->peek_buf[1];
2728 i->peek_buf[0] = ch2;
2729 if (ch2 == 0) /* very likely, avoid redundant write */
2735 ch = fgetc_interactive(i);
2737 debug_printf("file_get: got '%c' %d\n", ch, ch);
2739 #if ENABLE_HUSH_LINENO_VAR
2742 debug_printf_parse("G.parse_lineno++ = %u\n", G.parse_lineno);
2748 static int i_peek(struct in_str *i)
2753 /* string-based in_str */
2754 /* Doesn't report EOF on NUL. None of the callers care. */
2755 return (unsigned char)*i->p;
2758 /* FILE-based in_str */
2760 #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE
2761 /* This can be stdin, check line editing char[] buffer */
2762 if (i->p && *i->p != '\0')
2763 return (unsigned char)*i->p;
2765 /* peek_buf[] is an int array, not char. Can contain EOF. */
2766 ch = i->peek_buf[0];
2770 /* Need to get a new char */
2771 ch = fgetc_interactive(i);
2772 debug_printf("file_peek: got '%c' %d\n", ch, ch);
2774 /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */
2775 #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE
2781 i->peek_buf[0] = ch;
2782 /*i->peek_buf[1] = 0; - already is */
2786 /* Only ever called if i_peek() was called, and did not return EOF.
2787 * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL,
2788 * not end-of-line. Therefore we never need to read a new editing line here.
2790 static int i_peek2(struct in_str *i)
2794 /* There are two cases when i->p[] buffer exists.
2795 * (1) it's a string in_str.
2796 * (2) It's a file, and we have a saved line editing buffer.
2797 * In both cases, we know that i->p[0] exists and not NUL, and
2798 * the peek2 result is in i->p[1].
2801 return (unsigned char)i->p[1];
2803 /* Now we know it is a file-based in_str. */
2805 /* peek_buf[] is an int array, not char. Can contain EOF. */
2806 /* Is there 2nd char? */
2807 ch = i->peek_buf[1];
2809 /* We did not read it yet, get it now */
2810 do ch = hfgetc(i->file); while (ch == '\0');
2811 i->peek_buf[1] = ch;
2814 debug_printf("file_peek2: got '%c' %d\n", ch, ch);
2818 static int i_getch_and_eat_bkslash_nl(struct in_str *input)
2823 ch = i_getch(input);
2826 ch2 = i_peek(input);
2829 /* backslash+newline, skip it */
2834 /* Note: this function _eats_ \<newline> pairs, safe to use plain
2835 * i_getch() after it instead of i_getch_and_eat_bkslash_nl().
2837 static int i_peek_and_eat_bkslash_nl(struct in_str *input)
2845 ch2 = i_peek2(input);
2848 /* backslash+newline, skip it */
2854 static void setup_file_in_str(struct in_str *i, HFILE *fp)
2856 memset(i, 0, sizeof(*i));
2861 static void setup_string_in_str(struct in_str *i, const char *s)
2863 memset(i, 0, sizeof(*i));
2864 /*i->file = NULL */;
2872 #define B_CHUNK (32 * sizeof(char*))
2874 static void o_reset_to_empty_unquoted(o_string *o)
2877 o->has_quoted_part = 0;
2882 static void o_free_and_set_NULL(o_string *o)
2885 memset(o, 0, sizeof(*o));
2888 static ALWAYS_INLINE void o_free(o_string *o)
2893 static void o_grow_by(o_string *o, int len)
2895 if (o->length + len > o->maxlen) {
2896 o->maxlen += (2 * len) | (B_CHUNK-1);
2897 o->data = xrealloc(o->data, 1 + o->maxlen);
2901 static void o_addchr(o_string *o, int ch)
2903 debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o);
2904 if (o->length < o->maxlen) {
2905 /* likely. avoid o_grow_by() call */
2907 o->data[o->length] = ch;
2909 o->data[o->length] = '\0';
2917 /* Valid only if we know o_string is not empty */
2918 static void o_delchr(o_string *o)
2921 o->data[o->length] = '\0';
2925 static void o_addblock(o_string *o, const char *str, int len)
2928 ((char*)mempcpy(&o->data[o->length], str, len))[0] = '\0';
2932 static void o_addstr(o_string *o, const char *str)
2934 o_addblock(o, str, strlen(str));
2937 static void o_addstr_with_NUL(o_string *o, const char *str)
2939 o_addblock(o, str, strlen(str) + 1);
2943 static void nommu_addchr(o_string *o, int ch)
2949 # define nommu_addchr(o, str) ((void)0)
2952 #if ENABLE_HUSH_MODE_X
2953 static void x_mode_addchr(int ch)
2955 o_addchr(&G.x_mode_buf, ch);
2957 static void x_mode_addstr(const char *str)
2959 o_addstr(&G.x_mode_buf, str);
2961 static void x_mode_addblock(const char *str, int len)
2963 o_addblock(&G.x_mode_buf, str, len);
2965 static void x_mode_prefix(void)
2967 int n = G.x_mode_depth;
2968 do x_mode_addchr('+'); while (--n >= 0);
2970 static void x_mode_flush(void)
2972 int len = G.x_mode_buf.length;
2975 if (G.x_mode_fd > 0) {
2976 G.x_mode_buf.data[len] = '\n';
2977 full_write(G.x_mode_fd, G.x_mode_buf.data, len + 1);
2979 G.x_mode_buf.length = 0;
2984 * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side.
2985 * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v.
2986 * Apparently, on unquoted $v bash still does globbing
2987 * ("v='*.txt'; echo $v" prints all .txt files),
2988 * but NOT brace expansion! Thus, there should be TWO independent
2989 * quoting mechanisms on $v expansion side: one protects
2990 * $v from brace expansion, and other additionally protects "$v" against globbing.
2991 * We have only second one.
2994 #if ENABLE_HUSH_BRACE_EXPANSION
2995 # define MAYBE_BRACES "{}"
2997 # define MAYBE_BRACES ""
3000 /* My analysis of quoting semantics tells me that state information
3001 * is associated with a destination, not a source.
3003 static void o_addqchr(o_string *o, int ch)
3006 char *found = strchr("*?[\\" MAYBE_BRACES, ch);
3011 o->data[o->length] = '\\';
3014 o->data[o->length] = ch;
3016 o->data[o->length] = '\0';
3019 static void o_addQchr(o_string *o, int ch)
3022 if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)
3023 && strchr("*?[\\" MAYBE_BRACES, ch)
3026 o->data[o->length] = '\\';
3030 o->data[o->length] = ch;
3032 o->data[o->length] = '\0';
3035 static void o_addqblock(o_string *o, const char *str, int len)
3040 int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES);
3041 if (ordinary_cnt > len) /* paranoia */
3043 o_addblock(o, str, ordinary_cnt);
3044 if (ordinary_cnt == len)
3045 return; /* NUL is already added by o_addblock */
3046 str += ordinary_cnt;
3047 len -= ordinary_cnt + 1; /* we are processing + 1 char below */
3051 if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */
3053 o->data[o->length] = '\\';
3057 o->data[o->length] = ch;
3060 o->data[o->length] = '\0';
3063 static void o_addQblock(o_string *o, const char *str, int len)
3065 if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) {
3066 o_addblock(o, str, len);
3069 o_addqblock(o, str, len);
3072 static void o_addQstr(o_string *o, const char *str)
3074 o_addQblock(o, str, strlen(str));
3077 /* A special kind of o_string for $VAR and `cmd` expansion.
3078 * It contains char* list[] at the beginning, which is grown in 16 element
3079 * increments. Actual string data starts at the next multiple of 16 * (char*).
3080 * list[i] contains an INDEX (int!) into this string data.
3081 * It means that if list[] needs to grow, data needs to be moved higher up
3082 * but list[i]'s need not be modified.
3083 * NB: remembering how many list[i]'s you have there is crucial.
3084 * o_finalize_list() operation post-processes this structure - calculates
3085 * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well.
3087 #if DEBUG_EXPAND || DEBUG_GLOB
3088 static void debug_print_list(const char *prefix, o_string *o, int n)
3090 char **list = (char**)o->data;
3091 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
3095 fdprintf(2, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d glob:%d quoted:%d escape:%d\n",
3096 prefix, list, n, string_start, o->length, o->maxlen,
3097 !!(o->o_expflags & EXP_FLAG_GLOB),
3099 !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
3102 fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i],
3103 o->data + (int)(uintptr_t)list[i] + string_start,
3104 o->data + (int)(uintptr_t)list[i] + string_start);
3108 const char *p = o->data + (int)(uintptr_t)list[n - 1] + string_start;
3110 fdprintf(2, " total_sz:%ld\n", (long)((p + strlen(p) + 1) - o->data));
3114 # define debug_print_list(prefix, o, n) ((void)0)
3117 /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value
3118 * in list[n] so that it points past last stored byte so far.
3119 * It returns n+1. */
3120 static int o_save_ptr_helper(o_string *o, int n)
3122 char **list = (char**)o->data;
3126 if (!o->has_empty_slot) {
3127 string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
3128 string_len = o->length - string_start;
3129 if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */
3130 debug_printf_list("list[%d]=%d string_start=%d (growing)\n", n, string_len, string_start);
3131 /* list[n] points to string_start, make space for 16 more pointers */
3132 o->maxlen += 0x10 * sizeof(list[0]);
3133 o->data = xrealloc(o->data, o->maxlen + 1);
3134 list = (char**)o->data;
3135 memmove(list + n + 0x10, list + n, string_len);
3137 * expand_on_ifs() has a "previous argv[] ends in IFS?"
3138 * check. (grep for -prev-ifs-check-).
3139 * Ensure that argv[-1][last] is not garbage
3140 * but zero bytes, to save index check there.
3142 list[n + 0x10 - 1] = 0;
3143 o->length += 0x10 * sizeof(list[0]);
3145 debug_printf_list("list[%d]=%d string_start=%d\n",
3146 n, string_len, string_start);
3149 /* We have empty slot at list[n], reuse without growth */
3150 string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */
3151 string_len = o->length - string_start;
3152 debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n",
3153 n, string_len, string_start);
3154 o->has_empty_slot = 0;
3156 o->has_quoted_part = 0;
3157 list[n] = (char*)(uintptr_t)string_len;
3161 /* "What was our last o_save_ptr'ed position (byte offset relative o->data)?" */
3162 static int o_get_last_ptr(o_string *o, int n)
3164 char **list = (char**)o->data;
3165 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
3167 return ((int)(uintptr_t)list[n-1]) + string_start;
3171 * Globbing routines.
3173 * Most words in commands need to be globbed, even ones which are
3174 * (single or double) quoted. This stems from the possiblity of
3175 * constructs like "abc"* and 'abc'* - these should be globbed.
3176 * Having a different code path for fully-quoted strings ("abc",
3177 * 'abc') would only help performance-wise, but we still need
3178 * code for partially-quoted strings.
3180 * Unfortunately, if we want to match bash and ash behavior in all cases,
3181 * the logic can't be "shell-syntax argument is first transformed
3182 * to a string, then globbed, and if globbing does not match anything,
3183 * it is used verbatim". Here are two examples where it fails:
3187 * The globbing can't be avoided (because of '?' at the end).
3188 * The glob pattern is: b\\\*? - IOW, both \ and * are literals
3189 * and are glob-escaped. If this does not match, bash/ash print b\*?
3190 * - IOW: they "unbackslash" the glob pattern.
3191 * Now, look at this:
3193 * v='\\\*'; echo b$v?
3195 * The glob pattern is the same here: b\\\*? - the unquoted $v expansion
3196 * should be used as glob pattern with no changes. However, if glob
3197 * does not match, bash/ash print b\\\*? - NOT THE SAME as first example!
3199 * ash implements this by having an encoded representation of the word
3200 * to glob, which IS NOT THE SAME as the glob pattern - it has more data.
3201 * Glob pattern is derived from it. If glob fails, the decision what result
3202 * should be is made using that encoded representation. Not glob pattern.
3205 #if ENABLE_HUSH_BRACE_EXPANSION
3206 /* There in a GNU extension, GLOB_BRACE, but it is not usable:
3207 * first, it processes even {a} (no commas), second,
3208 * I didn't manage to make it return strings when they don't match
3209 * existing files. Need to re-implement it.
3213 static int glob_needed(const char *s)
3222 if (*s == '*' || *s == '[' || *s == '?' || *s == '{')
3228 /* Return pointer to next closing brace or to comma */
3229 static const char *next_brace_sub(const char *cp)
3233 while (*cp != '\0') {
3240 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
3246 return *cp != '\0' ? cp : NULL;
3248 /* Recursive brace globber. Note: may garble pattern[]. */
3249 static int glob_brace(char *pattern, o_string *o, int n)
3251 char *new_pattern_buf;
3258 debug_printf_glob("glob_brace('%s')\n", pattern);
3264 if (*begin == '{') {
3265 /* Find the first sub-pattern and at the same time
3266 * find the rest after the closing brace */
3267 next = next_brace_sub(begin);
3269 /* An illegal expression */
3273 /* "{abc}" with no commas - illegal
3274 * brace expr, disregard and skip it */
3280 if (*begin == '\\' && begin[1] != '\0')
3284 debug_printf_glob("begin:%s\n", begin);
3285 debug_printf_glob("next:%s\n", next);
3287 /* Now find the end of the whole brace expression */
3289 while (*rest != '}') {
3290 rest = next_brace_sub(rest);
3292 /* An illegal expression */
3295 debug_printf_glob("rest:%s\n", rest);
3297 rest_len = strlen(++rest) + 1;
3299 /* We are sure the brace expression is well-formed */
3301 /* Allocate working buffer large enough for our work */
3302 new_pattern_buf = xmalloc(strlen(pattern));
3304 /* We have a brace expression. BEGIN points to the opening {,
3305 * NEXT points past the terminator of the first element, and REST
3306 * points past the final }. We will accumulate result names from
3307 * recursive runs for each brace alternative in the buffer using
3312 /* Construct the new glob expression */
3315 mempcpy(new_pattern_buf,
3316 /* We know the prefix for all sub-patterns */
3317 pattern, begin - pattern),
3321 /* Note: glob_brace() may garble new_pattern_buf[].
3322 * That's why we re-copy prefix every time (1st memcpy above).
3324 n = glob_brace(new_pattern_buf, o, n);
3326 /* We saw the last entry */
3330 next = next_brace_sub(next);
3332 free(new_pattern_buf);
3340 memset(&globdata, 0, sizeof(globdata));
3341 gr = glob(pattern, 0, NULL, &globdata);
3342 debug_printf_glob("glob('%s'):%d\n", pattern, gr);
3344 if (gr == GLOB_NOMATCH) {
3345 globfree(&globdata);
3346 /* NB: garbles parameter */
3347 unbackslash(pattern);
3348 o_addstr_with_NUL(o, pattern);
3349 debug_printf_glob("glob pattern '%s' is literal\n", pattern);
3350 return o_save_ptr_helper(o, n);
3352 if (gr == GLOB_NOSPACE)
3353 bb_die_memory_exhausted();
3354 /* GLOB_ABORTED? Only happens with GLOB_ERR flag,
3355 * but we didn't specify it. Paranoia again. */
3356 bb_error_msg_and_die("glob error %d on '%s'", gr, pattern);
3358 if (globdata.gl_pathv && globdata.gl_pathv[0]) {
3359 char **argv = globdata.gl_pathv;
3361 o_addstr_with_NUL(o, *argv);
3362 n = o_save_ptr_helper(o, n);
3368 globfree(&globdata);
3372 /* Performs globbing on last list[],
3373 * saving each result as a new list[].
3375 static int perform_glob(o_string *o, int n)
3377 char *pattern, *copy;
3379 debug_printf_glob("start perform_glob: n:%d o->data:%p\n", n, o->data);
3381 return o_save_ptr_helper(o, n);
3382 pattern = o->data + o_get_last_ptr(o, n);
3383 debug_printf_glob("glob pattern '%s'\n", pattern);
3384 if (!glob_needed(pattern)) {
3385 /* unbackslash last string in o in place, fix length */
3386 o->length = unbackslash(pattern) - o->data;
3387 debug_printf_glob("glob pattern '%s' is literal\n", pattern);
3388 return o_save_ptr_helper(o, n);
3391 copy = xstrdup(pattern);
3392 /* "forget" pattern in o */
3393 o->length = pattern - o->data;
3394 n = glob_brace(copy, o, n);
3397 debug_print_list("perform_glob returning", o, n);
3401 #else /* !HUSH_BRACE_EXPANSION */
3404 static int glob_needed(const char *s)
3413 if (*s == '*' || *s == '[' || *s == '?')
3419 /* Performs globbing on last list[],
3420 * saving each result as a new list[].
3422 static int perform_glob(o_string *o, int n)
3428 debug_printf_glob("start perform_glob: n:%d o->data:%p\n", n, o->data);
3430 return o_save_ptr_helper(o, n);
3431 pattern = o->data + o_get_last_ptr(o, n);
3432 debug_printf_glob("glob pattern '%s'\n", pattern);
3433 if (!glob_needed(pattern)) {
3435 /* unbackslash last string in o in place, fix length */
3436 o->length = unbackslash(pattern) - o->data;
3437 debug_printf_glob("glob pattern '%s' is literal\n", pattern);
3438 return o_save_ptr_helper(o, n);
3441 memset(&globdata, 0, sizeof(globdata));
3442 /* Can't use GLOB_NOCHECK: it does not unescape the string.
3443 * If we glob "*.\*" and don't find anything, we need
3444 * to fall back to using literal "*.*", but GLOB_NOCHECK
3445 * will return "*.\*"!
3447 gr = glob(pattern, 0, NULL, &globdata);
3448 debug_printf_glob("glob('%s'):%d\n", pattern, gr);
3450 if (gr == GLOB_NOMATCH) {
3451 globfree(&globdata);
3454 if (gr == GLOB_NOSPACE)
3455 bb_die_memory_exhausted();
3456 /* GLOB_ABORTED? Only happens with GLOB_ERR flag,
3457 * but we didn't specify it. Paranoia again. */
3458 bb_error_msg_and_die("glob error %d on '%s'", gr, pattern);
3460 if (globdata.gl_pathv && globdata.gl_pathv[0]) {
3461 char **argv = globdata.gl_pathv;
3462 /* "forget" pattern in o */
3463 o->length = pattern - o->data;
3465 o_addstr_with_NUL(o, *argv);
3466 n = o_save_ptr_helper(o, n);
3472 globfree(&globdata);
3474 debug_print_list("perform_glob returning", o, n);
3478 #endif /* !HUSH_BRACE_EXPANSION */
3480 /* If o->o_expflags & EXP_FLAG_GLOB, glob the string so far remembered.
3481 * Otherwise, just finish current list[] and start new */
3482 static int o_save_ptr(o_string *o, int n)
3484 if (o->o_expflags & EXP_FLAG_GLOB) {
3485 /* If o->has_empty_slot, list[n] was already globbed
3486 * (if it was requested back then when it was filled)
3487 * so don't do that again! */
3488 if (!o->has_empty_slot)
3489 return perform_glob(o, n); /* o_save_ptr_helper is inside */
3491 return o_save_ptr_helper(o, n);
3494 /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */
3495 static char **o_finalize_list(o_string *o, int n)
3501 debug_print_list("finalized", o, n);
3502 debug_printf_expand("finalized n:%d\n", n);
3503 list = (char**)o->data;
3504 string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
3508 list[n] = o->data + (int)(uintptr_t)list[n] + string_start;
3513 static void free_pipe_list(struct pipe *pi);
3515 /* Returns pi->next - next pipe in the list */
3516 static struct pipe *free_pipe(struct pipe *pi)
3521 debug_printf_clean("free_pipe (pid %d)\n", getpid());
3522 for (i = 0; i < pi->num_cmds; i++) {
3523 struct command *command;
3524 struct redir_struct *r, *rnext;
3526 command = &pi->cmds[i];
3527 debug_printf_clean(" command %d:\n", i);
3528 if (command->argv) {
3532 for (a = 0, p = command->argv; *p; a++, p++) {
3533 debug_printf_clean(" argv[%d] = %s\n", a, *p);
3536 free_strings(command->argv);
3537 //command->argv = NULL;
3539 /* not "else if": on syntax error, we may have both! */
3540 if (command->group) {
3541 debug_printf_clean(" begin group (cmd_type:%d)\n",
3543 free_pipe_list(command->group);
3544 debug_printf_clean(" end group\n");
3545 //command->group = NULL;
3547 /* else is crucial here.
3548 * If group != NULL, child_func is meaningless */
3549 #if ENABLE_HUSH_FUNCTIONS
3550 else if (command->child_func) {
3551 debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func);
3552 command->child_func->parent_cmd = NULL;
3556 free(command->group_as_string);
3557 //command->group_as_string = NULL;
3559 for (r = command->redirects; r; r = rnext) {
3560 debug_printf_clean(" redirect %d%s",
3561 r->rd_fd, redir_table[r->rd_type].descrip);
3562 /* guard against the case >$FOO, where foo is unset or blank */
3563 if (r->rd_filename) {
3564 debug_printf_clean(" fname:'%s'\n", r->rd_filename);
3565 free(r->rd_filename);
3566 //r->rd_filename = NULL;
3568 debug_printf_clean(" rd_dup:%d\n", r->rd_dup);
3572 //command->redirects = NULL;
3574 free(pi->cmds); /* children are an array, they get freed all at once */
3578 //pi->cmdtext = NULL;
3586 static void free_pipe_list(struct pipe *pi)
3590 debug_printf_clean("pipe reserved word %d\n", pi->res_word);
3592 debug_printf_clean("pipe followup code %d\n", pi->followup);
3598 /*** Parsing routines ***/
3600 #ifndef debug_print_tree
3601 static void debug_print_tree(struct pipe *pi, int lvl)
3603 static const char *const PIPE[] = {
3609 static const char *RES[] = {
3610 [RES_NONE ] = "NONE" ,
3613 [RES_THEN ] = "THEN" ,
3614 [RES_ELIF ] = "ELIF" ,
3615 [RES_ELSE ] = "ELSE" ,
3618 # if ENABLE_HUSH_LOOPS
3619 [RES_FOR ] = "FOR" ,
3620 [RES_WHILE] = "WHILE",
3621 [RES_UNTIL] = "UNTIL",
3623 [RES_DONE ] = "DONE" ,
3625 # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
3628 # if ENABLE_HUSH_CASE
3629 [RES_CASE ] = "CASE" ,
3630 [RES_CASE_IN ] = "CASE_IN" ,
3631 [RES_MATCH] = "MATCH",
3632 [RES_CASE_BODY] = "CASE_BODY",
3633 [RES_ESAC ] = "ESAC" ,
3635 [RES_XXXX ] = "XXXX" ,
3636 [RES_SNTX ] = "SNTX" ,
3638 static const char *const CMDTYPE[] = {
3642 # if ENABLE_HUSH_FUNCTIONS
3651 fdprintf(2, "%*spipe %d %sres_word=%s followup=%d %s\n",
3654 (IF_HAS_KEYWORDS(pi->pi_inverted ? "! " :) ""),
3656 pi->followup, PIPE[pi->followup]
3659 while (prn < pi->num_cmds) {
3660 struct command *command = &pi->cmds[prn];
3661 char **argv = command->argv;
3663 fdprintf(2, "%*s cmd %d assignment_cnt:%d",
3665 command->assignment_cnt);
3666 # if ENABLE_HUSH_LINENO_VAR
3667 fdprintf(2, " LINENO:%u", command->lineno);
3669 if (command->group) {
3670 fdprintf(2, " group %s: (argv=%p)%s%s\n",
3671 CMDTYPE[command->cmd_type],
3674 , " group_as_string:", command->group_as_string
3679 debug_print_tree(command->group, lvl+1);
3683 if (argv) while (*argv) {
3684 fdprintf(2, " '%s'", *argv);
3687 if (command->redirects)
3688 fdprintf(2, " {redir}");
3696 #endif /* debug_print_tree */
3698 static struct pipe *new_pipe(void)
3701 pi = xzalloc(sizeof(struct pipe));
3702 /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */
3706 /* Command (member of a pipe) is complete, or we start a new pipe
3707 * if ctx->command is NULL.
3708 * No errors possible here.
3710 static int done_command(struct parse_context *ctx)
3712 /* The command is really already in the pipe structure, so
3713 * advance the pipe counter and make a new, null command. */
3714 struct pipe *pi = ctx->pipe;
3715 struct command *command = ctx->command;
3717 #if 0 /* Instead we emit error message at run time */
3718 if (ctx->pending_redirect) {
3719 /* For example, "cmd >" (no filename to redirect to) */
3720 syntax_error("invalid redirect");
3721 ctx->pending_redirect = NULL;
3726 if (IS_NULL_CMD(command)) {
3727 debug_printf_parse("done_command: skipping null cmd, num_cmds=%d\n", pi->num_cmds);
3731 debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds);
3732 //debug_print_tree(ctx->list_head, 20);
3734 debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds);
3737 /* Only real trickiness here is that the uncommitted
3738 * command structure is not counted in pi->num_cmds. */
3739 pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1));
3740 ctx->command = command = &pi->cmds[pi->num_cmds];
3742 memset(command, 0, sizeof(*command));
3743 #if ENABLE_HUSH_LINENO_VAR
3744 command->lineno = G.parse_lineno;
3745 debug_printf_parse("command->lineno = G.parse_lineno (%u)\n", G.parse_lineno);
3747 return pi->num_cmds; /* used only for 0/nonzero check */
3750 static void done_pipe(struct parse_context *ctx, pipe_style type)
3754 debug_printf_parse("done_pipe entered, followup %d\n", type);
3755 /* Close previous command */
3756 not_null = done_command(ctx);
3758 ctx->pipe->pi_inverted = ctx->ctx_inverted;
3759 ctx->ctx_inverted = 0;
3760 ctx->pipe->res_word = ctx->ctx_res_w;
3762 if (type == PIPE_BG && ctx->list_head != ctx->pipe) {
3763 /* Necessary since && and || have precedence over &:
3764 * "cmd1 && cmd2 &" must spawn both cmds, not only cmd2,
3765 * in a backgrounded subshell.
3768 struct command *command;
3770 /* Is this actually this construct, all pipes end with && or ||? */
3771 pi = ctx->list_head;
3772 while (pi != ctx->pipe) {
3773 if (pi->followup != PIPE_AND && pi->followup != PIPE_OR)
3778 debug_printf_parse("BG with more than one pipe, converting to { p1 &&...pN; } &\n");
3779 pi->followup = PIPE_SEQ; /* close pN _not_ with "&"! */
3780 pi = xzalloc(sizeof(*pi));
3781 pi->followup = PIPE_BG;
3783 pi->cmds = xzalloc(sizeof(pi->cmds[0]));
3784 command = &pi->cmds[0];
3785 if (CMD_NORMAL != 0) /* "if xzalloc didn't do that already" */
3786 command->cmd_type = CMD_NORMAL;
3787 command->group = ctx->list_head;
3789 command->group_as_string = xstrndup(
3790 ctx->as_string.data,
3791 ctx->as_string.length - 1 /* do not copy last char, "&" */
3794 /* Replace all pipes in ctx with one newly created */
3795 ctx->list_head = ctx->pipe = pi;
3798 ctx->pipe->followup = type;
3801 /* Without this check, even just <enter> on command line generates
3802 * tree of three NOPs (!). Which is harmless but annoying.
3803 * IOW: it is safe to do it unconditionally. */
3806 || ctx->ctx_res_w == RES_FI
3808 #if ENABLE_HUSH_LOOPS
3809 || ctx->ctx_res_w == RES_DONE
3810 || ctx->ctx_res_w == RES_FOR
3811 || ctx->ctx_res_w == RES_IN
3813 #if ENABLE_HUSH_CASE
3814 || ctx->ctx_res_w == RES_ESAC
3818 debug_printf_parse("done_pipe: adding new pipe: "
3819 "not_null:%d ctx->ctx_res_w:%d\n",
3820 not_null, ctx->ctx_res_w);
3822 ctx->pipe->next = new_p;
3824 /* RES_THEN, RES_DO etc are "sticky" -
3825 * they remain set for pipes inside if/while.
3826 * This is used to control execution.
3827 * RES_FOR and RES_IN are NOT sticky (needed to support
3828 * cases where variable or value happens to match a keyword):
3830 #if ENABLE_HUSH_LOOPS
3831 if (ctx->ctx_res_w == RES_FOR
3832 || ctx->ctx_res_w == RES_IN)
3833 ctx->ctx_res_w = RES_NONE;
3835 #if ENABLE_HUSH_CASE
3836 if (ctx->ctx_res_w == RES_MATCH)
3837 ctx->ctx_res_w = RES_CASE_BODY;
3838 if (ctx->ctx_res_w == RES_CASE)
3839 ctx->ctx_res_w = RES_CASE_IN;
3841 ctx->command = NULL; /* trick done_command below */
3842 /* Create the memory for command, roughly:
3843 * ctx->pipe->cmds = new struct command;
3844 * ctx->command = &ctx->pipe->cmds[0];
3847 //debug_print_tree(ctx->list_head, 10);
3849 debug_printf_parse("done_pipe return\n");
3852 static void initialize_context(struct parse_context *ctx)
3854 memset(ctx, 0, sizeof(*ctx));
3855 if (MAYBE_ASSIGNMENT != 0)
3856 ctx->is_assignment = MAYBE_ASSIGNMENT;
3857 ctx->pipe = ctx->list_head = new_pipe();
3858 /* Create the memory for command, roughly:
3859 * ctx->pipe->cmds = new struct command;
3860 * ctx->command = &ctx->pipe->cmds[0];
3865 /* If a reserved word is found and processed, parse context is modified
3866 * and 1 is returned.
3869 struct reserved_combo {
3872 unsigned char assignment_flag;
3876 FLAG_END = (1 << RES_NONE ),
3878 FLAG_IF = (1 << RES_IF ),
3879 FLAG_THEN = (1 << RES_THEN ),
3880 FLAG_ELIF = (1 << RES_ELIF ),
3881 FLAG_ELSE = (1 << RES_ELSE ),
3882 FLAG_FI = (1 << RES_FI ),
3884 # if ENABLE_HUSH_LOOPS
3885 FLAG_FOR = (1 << RES_FOR ),
3886 FLAG_WHILE = (1 << RES_WHILE),
3887 FLAG_UNTIL = (1 << RES_UNTIL),
3888 FLAG_DO = (1 << RES_DO ),
3889 FLAG_DONE = (1 << RES_DONE ),
3890 FLAG_IN = (1 << RES_IN ),
3892 # if ENABLE_HUSH_CASE
3893 FLAG_MATCH = (1 << RES_MATCH),
3894 FLAG_ESAC = (1 << RES_ESAC ),
3896 FLAG_START = (1 << RES_XXXX ),
3899 static const struct reserved_combo* match_reserved_word(o_string *word)
3901 /* Mostly a list of accepted follow-up reserved words.
3902 * FLAG_END means we are done with the sequence, and are ready
3903 * to turn the compound list into a command.
3904 * FLAG_START means the word must start a new compound list.
3906 static const struct reserved_combo reserved_list[] = {
3908 { "!", RES_NONE, NOT_ASSIGNMENT , 0 },
3909 { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START },
3910 { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
3911 { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN },
3912 { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI },
3913 { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END },
3915 # if ENABLE_HUSH_LOOPS
3916 { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START },
3917 { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START },
3918 { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START },
3919 { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO },
3920 { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE },
3921 { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END },
3923 # if ENABLE_HUSH_CASE
3924 { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START },
3925 { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END },
3928 const struct reserved_combo *r;
3930 for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) {
3931 if (strcmp(word->data, r->literal) == 0)
3936 /* Return NULL: not a keyword, else: keyword
3938 static const struct reserved_combo* reserved_word(struct parse_context *ctx)
3940 # if ENABLE_HUSH_CASE
3941 static const struct reserved_combo reserved_match = {
3942 "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC
3945 const struct reserved_combo *r;
3947 if (ctx->word.has_quoted_part)
3949 r = match_reserved_word(&ctx->word);
3951 return r; /* NULL */
3953 debug_printf("found reserved word %s, res %d\n", r->literal, r->res);
3954 # if ENABLE_HUSH_CASE
3955 if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) {
3956 /* "case word IN ..." - IN part starts first MATCH part */
3957 r = &reserved_match;
3960 if (r->flag == 0) { /* '!' */
3961 if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */
3962 syntax_error("! ! command");
3963 ctx->ctx_res_w = RES_SNTX;
3965 ctx->ctx_inverted = 1;
3968 if (r->flag & FLAG_START) {
3969 struct parse_context *old;
3971 old = xmemdup(ctx, sizeof(*ctx));
3972 debug_printf_parse("push stack %p\n", old);
3973 initialize_context(ctx);
3975 } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) {
3976 syntax_error_at(ctx->word.data);
3977 ctx->ctx_res_w = RES_SNTX;
3980 /* "{...} fi" is ok. "{...} if" is not
3982 * if { echo foo; } then { echo bar; } fi */
3983 if (ctx->command->group)
3984 done_pipe(ctx, PIPE_SEQ);
3987 ctx->ctx_res_w = r->res;
3988 ctx->old_flag = r->flag;
3989 ctx->is_assignment = r->assignment_flag;
3990 debug_printf_parse("ctx->is_assignment='%s'\n", assignment_flag[ctx->is_assignment]);
3992 if (ctx->old_flag & FLAG_END) {
3993 struct parse_context *old;
3995 done_pipe(ctx, PIPE_SEQ);
3996 debug_printf_parse("pop stack %p\n", ctx->stack);
3998 old->command->group = ctx->list_head;
3999 old->command->cmd_type = CMD_NORMAL;
4001 /* At this point, the compound command's string is in
4002 * ctx->as_string... except for the leading keyword!
4003 * Consider this example: "echo a | if true; then echo a; fi"
4004 * ctx->as_string will contain "true; then echo a; fi",
4005 * with "if " remaining in old->as_string!
4009 int len = old->as_string.length;
4010 /* Concatenate halves */
4011 o_addstr(&old->as_string, ctx->as_string.data);
4012 o_free(&ctx->as_string);
4013 /* Find where leading keyword starts in first half */
4014 str = old->as_string.data + len;
4015 if (str > old->as_string.data)
4016 str--; /* skip whitespace after keyword */
4017 while (str > old->as_string.data && isalpha(str[-1]))
4019 /* Ugh, we're done with this horrid hack */
4020 old->command->group_as_string = xstrdup(str);
4021 debug_printf_parse("pop, remembering as:'%s'\n",
4022 old->command->group_as_string);
4025 *ctx = *old; /* physical copy */
4030 #endif /* HAS_KEYWORDS */
4032 /* Word is complete, look at it and update parsing context.
4033 * Normal return is 0. Syntax errors return 1.
4034 * Note: on return, word is reset, but not o_free'd!
4036 static int done_word(struct parse_context *ctx)
4038 struct command *command = ctx->command;
4040 debug_printf_parse("done_word entered: '%s' %p\n", ctx->word.data, command);
4041 if (ctx->word.length == 0 && !ctx->word.has_quoted_part) {
4042 debug_printf_parse("done_word return 0: true null, ignored\n");
4046 if (ctx->pending_redirect) {
4047 /* We do not glob in e.g. >*.tmp case. bash seems to glob here
4048 * only if run as "bash", not "sh" */
4049 /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
4051 * If the redirection operator is "<<" or "<<-", the word
4052 * that follows the redirection operator shall be
4053 * subjected to quote removal; it is unspecified whether
4054 * any of the other expansions occur. For the other
4055 * redirection operators, the word that follows the
4056 * redirection operator shall be subjected to tilde
4057 * expansion, parameter expansion, command substitution,
4058 * arithmetic expansion, and quote removal.
4059 * Pathname expansion shall not be performed
4060 * on the word by a non-interactive shell; an interactive
4061 * shell may perform it, but shall do so only when
4062 * the expansion would result in one word."
4064 //bash does not do parameter/command substitution or arithmetic expansion
4065 //for _heredoc_ redirection word: these constructs look for exact eof marker
4069 // <<EOF`true` [this case also makes heredoc "quoted", a-la <<"EOF". Probably bash-4.3.43 bug]
4071 ctx->pending_redirect->rd_filename = xstrdup(ctx->word.data);
4072 /* Cater for >\file case:
4073 * >\a creates file a; >\\a, >"\a", >"\\a" create file \a
4074 * Same with heredocs:
4075 * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H
4077 if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) {
4078 unbackslash(ctx->pending_redirect->rd_filename);
4079 /* Is it <<"HEREDOC"? */
4080 if (ctx->word.has_quoted_part) {
4081 ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED;
4084 debug_printf_parse("word stored in rd_filename: '%s'\n", ctx->word.data);
4085 ctx->pending_redirect = NULL;
4088 # if ENABLE_HUSH_CASE
4089 if (ctx->ctx_dsemicolon
4090 && strcmp(ctx->word.data, "esac") != 0 /* not "... pattern) cmd;; esac" */
4092 /* already done when ctx_dsemicolon was set to 1: */
4093 /* ctx->ctx_res_w = RES_MATCH; */
4094 ctx->ctx_dsemicolon = 0;
4097 if (!command->argv /* if it's the first word... */
4098 # if ENABLE_HUSH_LOOPS
4099 && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */
4100 && ctx->ctx_res_w != RES_IN
4102 # if ENABLE_HUSH_CASE
4103 && ctx->ctx_res_w != RES_CASE
4106 const struct reserved_combo *reserved;
4107 reserved = reserved_word(ctx);
4108 debug_printf_parse("checking for reserved-ness: %d\n", !!reserved);
4110 # if ENABLE_HUSH_LINENO_VAR
4114 * If we don't close the pipe _now_, immediately after "do", lineno logic
4115 * sees "cmd" as starting at "do" - i.e., at the previous line.
4118 IF_HUSH_IF(|| reserved->res == RES_THEN)
4119 IF_HUSH_IF(|| reserved->res == RES_ELIF)
4120 IF_HUSH_IF(|| reserved->res == RES_ELSE)
4121 IF_HUSH_LOOPS(|| reserved->res == RES_DO)
4123 done_pipe(ctx, PIPE_SEQ);
4126 o_reset_to_empty_unquoted(&ctx->word);
4127 debug_printf_parse("done_word return %d\n",
4128 (ctx->ctx_res_w == RES_SNTX));
4129 return (ctx->ctx_res_w == RES_SNTX);
4131 # if defined(CMD_SINGLEWORD_NOGLOB)
4134 || strcmp(ctx->word.data, "[[") == 0
4136 /* In bash, local/export/readonly are special, args
4137 * are assignments and therefore expansion of them
4138 * should be "one-word" expansion:
4139 * $ export i=`echo 'a b'` # one arg: "i=a b"
4141 * $ ls i=`echo 'a b'` # two args: "i=a" and "b"
4142 * ls: cannot access i=a: No such file or directory
4143 * ls: cannot access b: No such file or directory
4144 * Note: bash 3.2.33(1) does this only if export word
4145 * itself is not quoted:
4146 * $ export i=`echo 'aaa bbb'`; echo "$i"
4148 * $ "export" i=`echo 'aaa bbb'`; echo "$i"
4151 IF_HUSH_LOCAL( || strcmp(ctx->word.data, "local") == 0)
4152 IF_HUSH_EXPORT( || strcmp(ctx->word.data, "export") == 0)
4153 IF_HUSH_READONLY(|| strcmp(ctx->word.data, "readonly") == 0)
4155 command->cmd_type = CMD_SINGLEWORD_NOGLOB;
4160 #endif /* HAS_KEYWORDS */
4162 if (command->group) {
4163 /* "{ echo foo; } echo bar" - bad */
4164 syntax_error_at(ctx->word.data);
4165 debug_printf_parse("done_word return 1: syntax error, "
4166 "groups and arglists don't mix\n");
4170 /* If this word wasn't an assignment, next ones definitely
4171 * can't be assignments. Even if they look like ones. */
4172 if (ctx->is_assignment != DEFINITELY_ASSIGNMENT
4173 && ctx->is_assignment != WORD_IS_KEYWORD
4175 ctx->is_assignment = NOT_ASSIGNMENT;
4177 if (ctx->is_assignment == DEFINITELY_ASSIGNMENT) {
4178 command->assignment_cnt++;
4179 debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt);
4181 debug_printf_parse("ctx->is_assignment was:'%s'\n", assignment_flag[ctx->is_assignment]);
4182 ctx->is_assignment = MAYBE_ASSIGNMENT;
4184 debug_printf_parse("ctx->is_assignment='%s'\n", assignment_flag[ctx->is_assignment]);
4185 command->argv = add_string_to_strings(command->argv, xstrdup(ctx->word.data));
4186 debug_print_strings("word appended to argv", command->argv);
4189 #if ENABLE_HUSH_LOOPS
4190 if (ctx->ctx_res_w == RES_FOR) {
4191 if (ctx->word.has_quoted_part
4192 || endofname(command->argv[0])[0] != '\0'
4194 /* bash says just "not a valid identifier" */
4195 syntax_error("not a valid identifier in for");
4198 /* Force FOR to have just one word (variable name) */
4199 /* NB: basically, this makes hush see "for v in ..."
4200 * syntax as if it is "for v; in ...". FOR and IN become
4201 * two pipe structs in parse tree. */
4202 done_pipe(ctx, PIPE_SEQ);
4205 #if ENABLE_HUSH_CASE
4206 /* Force CASE to have just one word */
4207 if (ctx->ctx_res_w == RES_CASE) {
4208 done_pipe(ctx, PIPE_SEQ);
4212 o_reset_to_empty_unquoted(&ctx->word);
4214 debug_printf_parse("done_word return 0\n");
4219 /* Peek ahead in the input to find out if we have a "&n" construct,
4220 * as in "2>&1", that represents duplicating a file descriptor.
4222 * REDIRFD_CLOSE if >&- "close fd" construct is seen,
4223 * REDIRFD_SYNTAX_ERR if syntax error,
4224 * REDIRFD_TO_FILE if no & was seen,
4225 * or the number found.
4228 #define parse_redir_right_fd(as_string, input) \
4229 parse_redir_right_fd(input)
4231 static int parse_redir_right_fd(o_string *as_string, struct in_str *input)
4237 return REDIRFD_TO_FILE;
4239 ch = i_getch(input); /* get the & */
4240 nommu_addchr(as_string, ch);
4243 ch = i_getch(input);
4244 nommu_addchr(as_string, ch);
4245 return REDIRFD_CLOSE;
4249 while (ch != EOF && isdigit(ch)) {
4250 d = d*10 + (ch-'0');
4252 ch = i_getch(input);
4253 nommu_addchr(as_string, ch);
4258 //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2)
4260 bb_simple_error_msg("ambiguous redirect");
4261 return REDIRFD_SYNTAX_ERR;
4264 /* Return code is 0 normal, 1 if a syntax error is detected
4266 static int parse_redirect(struct parse_context *ctx,
4269 struct in_str *input)
4271 struct command *command = ctx->command;
4272 struct redir_struct *redir;
4273 struct redir_struct **redirp;
4276 dup_num = REDIRFD_TO_FILE;
4277 if (style != REDIRECT_HEREDOC) {
4278 /* Check for a '>&1' type redirect */
4279 dup_num = parse_redir_right_fd(&ctx->as_string, input);
4280 if (dup_num == REDIRFD_SYNTAX_ERR)
4283 int ch = i_peek_and_eat_bkslash_nl(input);
4284 dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */
4285 if (dup_num) { /* <<-... */
4286 ch = i_getch(input);
4287 nommu_addchr(&ctx->as_string, ch);
4292 if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) {
4293 int ch = i_peek_and_eat_bkslash_nl(input);
4295 /* >|FILE redirect ("clobbering" >).
4296 * Since we do not support "set -o noclobber" yet,
4297 * >| and > are the same for now. Just eat |.
4299 ch = i_getch(input);
4300 nommu_addchr(&ctx->as_string, ch);
4304 /* Create a new redir_struct and append it to the linked list */
4305 redirp = &command->redirects;
4306 while ((redir = *redirp) != NULL) {
4307 redirp = &(redir->next);
4309 *redirp = redir = xzalloc(sizeof(*redir));
4310 /* redir->next = NULL; */
4311 /* redir->rd_filename = NULL; */
4312 redir->rd_type = style;
4313 redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd;
4315 debug_printf_parse("redirect type %d %s\n", redir->rd_fd,
4316 redir_table[style].descrip);
4318 redir->rd_dup = dup_num;
4319 if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) {
4320 /* Erik had a check here that the file descriptor in question
4321 * is legit; I postpone that to "run time"
4322 * A "-" representation of "close me" shows up as a -3 here */
4323 debug_printf_parse("duplicating redirect '%d>&%d'\n",
4324 redir->rd_fd, redir->rd_dup);
4326 #if 0 /* Instead we emit error message at run time */
4327 if (ctx->pending_redirect) {
4328 /* For example, "cmd > <file" */
4329 syntax_error("invalid redirect");
4332 /* Set ctx->pending_redirect, so we know what to do at the
4333 * end of the next parsed word. */
4334 ctx->pending_redirect = redir;
4339 /* If a redirect is immediately preceded by a number, that number is
4340 * supposed to tell which file descriptor to redirect. This routine
4341 * looks for such preceding numbers. In an ideal world this routine
4342 * needs to handle all the following classes of redirects...
4343 * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
4344 * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
4345 * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
4346 * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
4348 * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html
4350 * ... If n is quoted, the number shall not be recognized as part of
4351 * the redirection expression. For example:
4353 * writes the character 2 into file a"
4354 * We are getting it right by setting ->has_quoted_part on any \<char>
4356 * A -1 return means no valid number was found,
4357 * the caller should use the appropriate default for this redirection.
4359 static int redirect_opt_num(o_string *o)
4363 if (o->data == NULL)
4365 num = bb_strtou(o->data, NULL, 10);
4366 if (errno || num < 0)
4368 o_reset_to_empty_unquoted(o);
4373 #define fetch_till_str(as_string, input, word, skip_tabs) \
4374 fetch_till_str(input, word, skip_tabs)
4376 static char *fetch_till_str(o_string *as_string,
4377 struct in_str *input,
4381 o_string heredoc = NULL_O_STRING;
4383 int prev = 0; /* not \ */
4386 /* Starting with "" is necessary for this case:
4392 heredoc.data = xzalloc(1); /* start as "", not as NULL */
4397 ch = i_getch(input);
4399 nommu_addchr(as_string, ch);
4400 if (ch == '\n' || ch == EOF) {
4402 if ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') {
4403 /* End-of-line, and not a line continuation */
4404 if (strcmp(heredoc.data + past_EOL, word) == 0) {
4405 heredoc.data[past_EOL] = '\0';
4406 debug_printf_heredoc("parsed '%s' heredoc '%s'\n", word, heredoc.data);
4407 return heredoc.data;
4410 /* This is a new line.
4411 * Remember position and backslash-escaping status.
4413 o_addchr(&heredoc, ch);
4416 past_EOL = heredoc.length;
4417 /* Get 1st char of next line, possibly skipping leading tabs */
4419 ch = i_getch(input);
4421 nommu_addchr(as_string, ch);
4422 } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t');
4423 /* If this immediately ended the line,
4424 * go back to end-of-line checks.
4427 goto check_heredoc_end;
4430 /* Backslash-line continuation in an unquoted
4431 * heredoc. This does not need special handling
4432 * for heredoc body (unquoted heredocs are
4433 * expanded on "execution" and that would take
4434 * care of this case too), but not the case
4435 * of line continuation *in terminator*:
4441 heredoc.data[--heredoc.length] = '\0';
4442 prev = 0; /* not '\' */
4448 return NULL; /* error */
4450 o_addchr(&heredoc, ch);
4451 nommu_addchr(as_string, ch);
4452 if (prev == '\\' && ch == '\\')
4453 /* Correctly handle foo\\<eol> (not a line cont.) */
4454 prev = 0; /* not '\' */
4460 /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs
4461 * and load them all. There should be exactly heredoc_cnt of them.
4464 #define fetch_heredocs(as_string, pi, heredoc_cnt, input) \
4465 fetch_heredocs(pi, heredoc_cnt, input)
4467 static int fetch_heredocs(o_string *as_string, struct pipe *pi, int heredoc_cnt, struct in_str *input)
4469 while (pi && heredoc_cnt) {
4471 struct command *cmd = pi->cmds;
4473 debug_printf_heredoc("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n",
4475 cmd->argv ? cmd->argv[0] : "NONE"
4477 for (i = 0; i < pi->num_cmds; i++) {
4478 struct redir_struct *redir = cmd->redirects;
4480 debug_printf_heredoc("fetch_heredocs: %d cmd argv0:'%s'\n",
4481 i, cmd->argv ? cmd->argv[0] : "NONE");
4483 if (redir->rd_type == REDIRECT_HEREDOC) {
4486 redir->rd_type = REDIRECT_HEREDOC2;
4487 /* redir->rd_dup is (ab)used to indicate <<- */
4488 p = fetch_till_str(as_string, input,
4489 redir->rd_filename, redir->rd_dup);
4491 syntax_error("unexpected EOF in here document");
4494 free(redir->rd_filename);
4495 redir->rd_filename = p;
4498 redir = redir->next;
4501 //bb_error_msg("%s:%u heredoc_cnt:%d", __func__, __LINE__, heredoc_cnt);
4502 heredoc_cnt = fetch_heredocs(as_string, cmd->group, heredoc_cnt, input);
4503 //bb_error_msg("%s:%u heredoc_cnt:%d", __func__, __LINE__, heredoc_cnt);
4504 if (heredoc_cnt < 0)
4505 return heredoc_cnt; /* error */
4515 static int run_list(struct pipe *pi);
4517 #define parse_stream(pstring, heredoc_cnt_ptr, input, end_trigger) \
4518 parse_stream(heredoc_cnt_ptr, input, end_trigger)
4520 static struct pipe *parse_stream(char **pstring,
4521 int *heredoc_cnt_ptr,
4522 struct in_str *input,
4525 /* Returns number of heredocs not yet consumed,
4528 static int parse_group(struct parse_context *ctx,
4529 struct in_str *input, int ch)
4531 /* ctx->word contains characters seen prior to ( or {.
4532 * Typically it's empty, but for function defs,
4533 * it contains function name (without '()'). */
4535 # define as_string NULL
4537 char *as_string = NULL;
4539 struct pipe *pipe_list;
4540 int heredoc_cnt = 0;
4542 struct command *command = ctx->command;
4544 debug_printf_parse("parse_group entered\n");
4545 #if ENABLE_HUSH_FUNCTIONS
4546 if (ch == '(' && !ctx->word.has_quoted_part) {
4547 if (ctx->word.length)
4551 goto skip; /* (... */
4552 if (command->argv[1]) { /* word word ... (... */
4553 syntax_error_unexpected_ch('(');
4556 /* it is "word(..." or "word (..." */
4558 ch = i_getch(input);
4559 while (ch == ' ' || ch == '\t');
4561 syntax_error_unexpected_ch(ch);
4564 nommu_addchr(&ctx->as_string, ch);
4566 ch = i_getch(input);
4567 while (ch == ' ' || ch == '\t' || ch == '\n');
4568 if (ch != '{' && ch != '(') {
4569 syntax_error_unexpected_ch(ch);
4572 nommu_addchr(&ctx->as_string, ch);
4573 command->cmd_type = CMD_FUNCDEF;
4578 #if 0 /* Prevented by caller */
4579 if (command->argv /* word [word]{... */
4580 || ctx->word.length /* word{... */
4581 || ctx->word.has_quoted_part /* ""{... */
4584 debug_printf_parse("parse_group return -1: "
4585 "syntax error, groups and arglists don't mix\n");
4590 IF_HUSH_FUNCTIONS(skip:)
4595 IF_HUSH_FUNCTIONS(if (command->cmd_type != CMD_FUNCDEF))
4596 command->cmd_type = CMD_SUBSHELL;
4598 /* bash does not allow "{echo...", requires whitespace */
4600 if (ch != ' ' && ch != '\t' && ch != '\n'
4601 && ch != '(' /* but "{(..." is allowed (without whitespace) */
4603 syntax_error_unexpected_ch(ch);
4607 ch = i_getch(input);
4608 nommu_addchr(&ctx->as_string, ch);
4612 debug_printf_heredoc("calling parse_stream, heredoc_cnt:%d\n", heredoc_cnt);
4613 pipe_list = parse_stream(&as_string, &heredoc_cnt, input, endch);
4614 debug_printf_heredoc("parse_stream returned: heredoc_cnt:%d\n", heredoc_cnt);
4617 o_addstr(&ctx->as_string, as_string);
4620 /* empty ()/{} or parse error? */
4621 if (!pipe_list || pipe_list == ERR_PTR) {
4622 /* parse_stream already emitted error msg */
4625 debug_printf_parse("parse_group return -1: "
4626 "parse_stream returned %p\n", pipe_list);
4630 as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */
4631 command->group_as_string = as_string;
4632 debug_printf_parse("end of group, remembering as:'%s'\n",
4633 command->group_as_string);
4636 #if ENABLE_HUSH_FUNCTIONS
4637 /* Convert "f() (cmds)" to "f() {(cmds)}" */
4638 if (command->cmd_type == CMD_FUNCDEF && endch == ')') {
4639 struct command *cmd2;
4641 cmd2 = xzalloc(sizeof(*cmd2));
4642 cmd2->cmd_type = CMD_SUBSHELL;
4643 cmd2->group = pipe_list;
4646 cmd2->group_as_string = command->group_as_string;
4647 command->group_as_string = xasprintf("(%s)", command->group_as_string);
4650 pipe_list = new_pipe();
4651 pipe_list->cmds = cmd2;
4652 pipe_list->num_cmds = 1;
4656 command->group = pipe_list;
4658 debug_printf_parse("parse_group return %d\n", heredoc_cnt);
4660 /* command remains "open", available for possible redirects */
4664 #if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS
4665 /* Subroutines for copying $(...) and `...` things */
4667 static int add_till_single_quote(o_string *dest, struct in_str *input)
4670 int ch = i_getch(input);
4672 syntax_error_unterm_ch('\'');
4680 static int add_till_single_quote_dquoted(o_string *dest, struct in_str *input)
4683 int ch = i_getch(input);
4685 syntax_error_unterm_ch('\'');
4690 o_addqchr(dest, ch);
4693 /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */
4694 static int add_till_backquote(o_string *dest, struct in_str *input, int in_dquote);
4695 static int add_till_double_quote(o_string *dest, struct in_str *input)
4698 int ch = i_getch(input);
4700 syntax_error_unterm_ch('"');
4705 if (ch == '\\') { /* \x. Copy both chars. */
4707 ch = i_getch(input);
4711 if (!add_till_backquote(dest, input, /*in_dquote:*/ 1))
4716 //if (ch == '$') ...
4719 /* Process `cmd` - copy contents until "`" is seen. Complicated by
4721 * "Within the backquoted style of command substitution, backslash
4722 * shall retain its literal meaning, except when followed by: '$', '`', or '\'.
4723 * The search for the matching backquote shall be satisfied by the first
4724 * backquote found without a preceding backslash; during this search,
4725 * if a non-escaped backquote is encountered within a shell comment,
4726 * a here-document, an embedded command substitution of the $(command)
4727 * form, or a quoted string, undefined results occur. A single-quoted
4728 * or double-quoted string that begins, but does not end, within the
4729 * "`...`" sequence produces undefined results."
4731 * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST
4733 static int add_till_backquote(o_string *dest, struct in_str *input, int in_dquote)
4736 int ch = i_getch(input);
4740 /* \x. Copy both unless it is \`, \$, \\ and maybe \" */
4741 ch = i_getch(input);
4745 && (!in_dquote || ch != '"')
4747 o_addchr(dest, '\\');
4751 syntax_error_unterm_ch('`');
4757 /* Process $(cmd) - copy contents until ")" is seen. Complicated by
4758 * quoting and nested ()s.
4759 * "With the $(command) style of command substitution, all characters
4760 * following the open parenthesis to the matching closing parenthesis
4761 * constitute the command. Any valid shell script can be used for command,
4762 * except a script consisting solely of redirections which produces
4763 * unspecified results."
4765 * echo $(echo '(TEST)' BEST) (TEST) BEST
4766 * echo $(echo 'TEST)' BEST) TEST) BEST
4767 * echo $(echo \(\(TEST\) BEST) ((TEST) BEST
4769 * Also adapted to eat ${var%...} and $((...)) constructs, since ... part
4770 * can contain arbitrary constructs, just like $(cmd).
4771 * In bash compat mode, it needs to also be able to stop on ':' or '/'
4772 * for ${var:N[:M]} and ${var/P[/R]} parsing.
4774 #define DOUBLE_CLOSE_CHAR_FLAG 0x80
4775 static int add_till_closing_bracket(o_string *dest, struct in_str *input, unsigned end_ch)
4778 char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG;
4779 # if BASH_SUBSTR || BASH_PATTERN_SUBST
4780 char end_char2 = end_ch >> 8;
4782 end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1);
4784 # if ENABLE_HUSH_INTERACTIVE
4785 G.promptmode = 1; /* PS2 */
4787 debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode);
4790 ch = i_getch(input);
4792 syntax_error_unterm_ch(end_ch);
4796 # if BASH_SUBSTR || BASH_PATTERN_SUBST
4802 /* we look for closing )) of $((EXPR)) */
4803 if (i_peek_and_eat_bkslash_nl(input) == end_ch) {
4804 i_getch(input); /* eat second ')' */
4809 //bb_error_msg("%s:o_addchr('%c')", __func__, ch);
4810 if (ch == '(' || ch == '{') {
4811 ch = (ch == '(' ? ')' : '}');
4812 if (!add_till_closing_bracket(dest, input, ch))
4818 if (!add_till_single_quote(dest, input))
4824 if (!add_till_double_quote(dest, input))
4830 if (!add_till_backquote(dest, input, /*in_dquote:*/ 0))
4836 /* \x. Copy verbatim. Important for \(, \) */
4837 ch = i_getch(input);
4839 syntax_error_unterm_ch(end_ch);
4844 /* "backslash+newline", ignore both */
4845 o_delchr(dest); /* undo insertion of '\' */
4850 //bb_error_msg("%s:o_addchr('%c') after '\\'", __func__, ch);
4854 debug_printf_parse("%s return '%s' ch:'%c'\n", __func__, dest->data, ch);
4857 #endif /* ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS */
4859 /* Return code: 0 for OK, 1 for syntax error */
4861 #define parse_dollar(as_string, dest, input, quote_mask) \
4862 parse_dollar(dest, input, quote_mask)
4863 #define as_string NULL
4865 static int parse_dollar(o_string *as_string,
4867 struct in_str *input, unsigned char quote_mask)
4869 int ch = i_peek_and_eat_bkslash_nl(input); /* first character after the $ */
4871 debug_printf_parse("parse_dollar entered: ch='%c'\n", ch);
4874 ch = i_getch(input);
4875 nommu_addchr(as_string, ch);
4877 o_addchr(dest, SPECIAL_VAR_SYMBOL);
4879 debug_printf_parse(": '%c'\n", ch);
4880 o_addchr(dest, ch | quote_mask);
4882 ch = i_peek_and_eat_bkslash_nl(input);
4883 if (!isalnum(ch) && ch != '_') {
4884 /* End of variable name reached */
4887 ch = i_getch(input);
4888 nommu_addchr(as_string, ch);
4890 o_addchr(dest, SPECIAL_VAR_SYMBOL);
4891 } else if (isdigit(ch)) {
4893 ch = i_getch(input);
4894 nommu_addchr(as_string, ch);
4895 o_addchr(dest, SPECIAL_VAR_SYMBOL);
4896 debug_printf_parse(": '%c'\n", ch);
4897 o_addchr(dest, ch | quote_mask);
4898 o_addchr(dest, SPECIAL_VAR_SYMBOL);
4899 } else switch (ch) {
4901 case '!': /* last bg pid */
4902 case '?': /* last exit code */
4903 case '#': /* number of args */
4904 case '*': /* args */
4905 case '@': /* args */
4906 case '-': /* $- option flags set by set builtin or shell options (-i etc) */
4907 goto make_one_char_var;
4911 o_addchr(dest, SPECIAL_VAR_SYMBOL);
4913 ch = i_getch(input); /* eat '{' */
4914 nommu_addchr(as_string, ch);
4916 ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */
4917 /* It should be ${?}, or ${#var},
4918 * or even ${?+subst} - operator acting on a special variable,
4919 * or the beginning of variable name.
4922 || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */
4925 syntax_error_unterm_str("${name}");
4926 debug_printf_parse("parse_dollar return 0: unterminated ${name}\n");
4929 nommu_addchr(as_string, ch);
4933 /* It's possible to just call add_till_closing_bracket() at this point.
4934 * However, this regresses some of our testsuite cases
4935 * which check invalid constructs like ${%}.
4936 * Oh well... let's check that the var name part is fine... */
4942 debug_printf_parse(": '%c'\n", ch);
4944 ch = i_getch(input);
4945 nommu_addchr(as_string, ch);
4949 if (!isalnum(ch) && ch != '_') {
4951 unsigned char last_ch;
4952 /* handle parameter expansions
4953 * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02
4955 if (!strchr(VAR_SUBST_OPS, ch)) { /* ${var<bad_char>... */
4956 if (len_single_ch != '#'
4957 /*|| !strchr(SPECIAL_VARS_STR, ch) - disallow errors like ${#+} ? */
4958 || i_peek(input) != '}'
4960 goto bad_dollar_syntax;
4962 /* else: it's "length of C" ${#C} op,
4963 * where C is a single char
4964 * special var name, e.g. ${#!}.
4967 /* Eat everything until closing '}' (or ':') */
4971 && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input))
4973 /* It's ${var:N[:M]} thing */
4974 end_ch = '}' * 0x100 + ':';
4976 if (BASH_PATTERN_SUBST
4979 /* It's ${var/[/]pattern[/repl]} thing */
4980 if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */
4982 nommu_addchr(as_string, '/');
4985 end_ch = '}' * 0x100 + '/';
4988 /* The pattern can't be empty.
4989 * IOW: if the first char after "${v//" is a slash,
4990 * it does not terminate the pattern - it's the first char of the pattern:
4991 * v=/dev/ram; echo ${v////-} prints -dev-ram (pattern is "/")
4992 * v=/dev/ram; echo ${v///r/-} prints /dev-am (pattern is "/r")
4994 if (i_peek(input) == '/') {
4995 o_addchr(dest, i_getch(input));
5000 #if ENABLE_HUSH_DOLLAR_OPS
5001 last_ch = add_till_closing_bracket(dest, input, end_ch);
5002 if (last_ch == 0) /* error? */
5005 # error Simple code to only allow ${var} is not implemented
5008 o_addstr(as_string, dest->data + pos);
5009 o_addchr(as_string, last_ch);
5012 if ((BASH_SUBSTR || BASH_PATTERN_SUBST)
5013 && (end_ch & 0xff00)
5015 /* close the first block: */
5016 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5017 /* while parsing N from ${var:N[:M]}
5018 * or pattern from ${var/[/]pattern[/repl]} */
5019 if ((end_ch & 0xff) == last_ch) {
5020 /* got ':' or '/'- parse the rest */
5025 if (BASH_SUBSTR && end_ch == '}' * 0x100 + ':') {
5026 /* it's ${var:N} - emulate :999999999 */
5027 o_addstr(dest, "999999999");
5028 } /* else: it's ${var/[/]pattern} */
5032 len_single_ch = 0; /* it can't be ${#C} op */
5034 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5037 #if ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_TICK
5041 ch = i_getch(input);
5042 nommu_addchr(as_string, ch);
5043 # if ENABLE_FEATURE_SH_MATH
5044 if (i_peek_and_eat_bkslash_nl(input) == '(') {
5045 ch = i_getch(input);
5046 nommu_addchr(as_string, ch);
5047 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5048 o_addchr(dest, /*quote_mask |*/ '+');
5051 if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG))
5052 return 0; /* error */
5054 o_addstr(as_string, dest->data + pos);
5055 o_addchr(as_string, ')');
5056 o_addchr(as_string, ')');
5058 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5062 # if ENABLE_HUSH_TICK
5063 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5064 o_addchr(dest, quote_mask | '`');
5067 if (!add_till_closing_bracket(dest, input, ')'))
5068 return 0; /* error */
5070 o_addstr(as_string, dest->data + pos);
5071 o_addchr(as_string, ')');
5073 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5082 /* $_ Shell or shell script name; or last argument of last command
5083 * (if last command wasn't a pipe; if it was, bash sets $_ to "");
5084 * but in command's env, set to full pathname used to invoke it */
5085 ch = i_getch(input);
5086 nommu_addchr(as_string, ch);
5087 ch = i_peek_and_eat_bkslash_nl(input);
5088 if (isalnum(ch)) { /* it's $_name or $_123 */
5095 o_addQchr(dest, '$');
5097 debug_printf_parse("parse_dollar return 1 (ok)\n");
5103 #define encode_string(as_string, dest, input, dquote_end) \
5104 encode_string(dest, input, dquote_end)
5105 #define as_string NULL
5107 static int encode_string(o_string *as_string,
5109 struct in_str *input,
5116 ch = i_getch(input);
5118 nommu_addchr(as_string, ch);
5119 if (ch == dquote_end) { /* may be only '"' or EOF */
5120 debug_printf_parse("encode_string return 1 (ok)\n");
5123 /* note: can't move it above ch == dquote_end check! */
5125 syntax_error_unterm_ch('"');
5126 return 0; /* error */
5130 next = i_peek(input);
5132 debug_printf_parse("\" ch=%c (%d) escape=%d\n",
5133 ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
5136 /* Testcase: in interactive shell a file with
5137 * echo "unterminated string\<eof>
5140 syntax_error_unterm_ch('"');
5141 return 0; /* error */
5144 * "The backslash retains its special meaning [in "..."]
5145 * only when followed by one of the following characters:
5146 * $, `, ", \, or <newline>. A double quote may be quoted
5147 * within double quotes by preceding it with a backslash."
5148 * NB: in (unquoted) heredoc, above does not apply to ",
5149 * therefore we check for it by "next == dquote_end" cond.
5151 if (next == dquote_end || strchr("$`\\\n", next)) {
5152 ch = i_getch(input); /* eat next */
5154 goto again; /* skip \<newline> */
5155 } /* else: ch remains == '\\', and we double it below: */
5156 o_addqchr(dest, ch); /* \c if c is a glob char, else just c */
5157 nommu_addchr(as_string, ch);
5161 if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) {
5162 debug_printf_parse("encode_string return 0: "
5163 "parse_dollar returned 0 (error)\n");
5168 #if ENABLE_HUSH_TICK
5170 //unsigned pos = dest->length;
5171 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5172 o_addchr(dest, 0x80 | '`');
5173 if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"'))
5174 return 0; /* error */
5175 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5176 //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos);
5180 o_addQchr(dest, ch);
5186 * Scan input until EOF or end_trigger char.
5187 * Return a list of pipes to execute, or NULL on EOF
5188 * or if end_trigger character is met.
5189 * On syntax error, exit if shell is not interactive,
5190 * reset parsing machinery and start parsing anew,
5191 * or return ERR_PTR.
5193 static struct pipe *parse_stream(char **pstring,
5194 int *heredoc_cnt_ptr,
5195 struct in_str *input,
5198 struct parse_context ctx;
5201 /* Single-quote triggers a bypass of the main loop until its mate is
5202 * found. When recursing, quote state is passed in via ctx.word.o_expflags.
5204 debug_printf_parse("parse_stream entered, end_trigger='%c'\n",
5205 end_trigger ? end_trigger : 'X');
5208 initialize_context(&ctx);
5210 /* If very first arg is "" or '', ctx.word.data may end up NULL.
5213 ctx.word.data = xzalloc(1); /* start as "", not as NULL */
5215 /* We used to separate words on $IFS here. This was wrong.
5216 * $IFS is used only for word splitting when $var is expanded,
5217 * here we should use blank chars as separators, not $IFS
5222 const char *is_blank;
5223 const char *is_special;
5227 redir_type redir_style;
5229 ch = i_getch(input);
5230 debug_printf_parse(": ch=%c (%d) escape=%d\n",
5231 ch, ch, !!(ctx.word.o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
5236 syntax_error_unterm_str("here document");
5239 if (end_trigger == ')') {
5240 syntax_error_unterm_ch('(');
5243 if (end_trigger == '}') {
5244 syntax_error_unterm_ch('{');
5248 if (done_word(&ctx)) {
5251 o_free_and_set_NULL(&ctx.word);
5252 done_pipe(&ctx, PIPE_SEQ);
5254 /* If we got nothing... */
5255 /* (this makes bare "&" cmd a no-op.
5256 * bash says: "syntax error near unexpected token '&'") */
5257 if (pi->num_cmds == 0
5258 IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE)
5264 debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data);
5266 *pstring = ctx.as_string.data;
5268 o_free(&ctx.as_string);
5270 // heredoc_cnt must be 0 here anyway
5271 //if (heredoc_cnt_ptr)
5272 // *heredoc_cnt_ptr = heredoc_cnt;
5274 debug_printf_heredoc("parse_stream return heredoc_cnt:%d\n", heredoc_cnt);
5275 debug_printf_parse("parse_stream return %p\n", pi);
5279 /* Handle "'" and "\" first, as they won't play nice with
5280 * i_peek_and_eat_bkslash_nl() anyway:
5288 ch = i_getch(input);
5290 continue; /* drop \<newline>, get next char */
5291 nommu_addchr(&ctx.as_string, '\\');
5292 o_addchr(&ctx.word, '\\');
5294 /* Testcase: eval 'echo Ok\' */
5295 /* bash-4.3.43 was removing backslash,
5296 * but 4.4.19 retains it, most other shells too
5298 continue; /* get next char */
5300 /* Example: echo Hello \2>file
5301 * we need to know that word 2 is quoted
5303 ctx.word.has_quoted_part = 1;
5304 nommu_addchr(&ctx.as_string, ch);
5305 o_addchr(&ctx.word, ch);
5306 continue; /* get next char */
5308 nommu_addchr(&ctx.as_string, ch);
5310 ctx.word.has_quoted_part = 1;
5311 next = i_getch(input);
5312 if (next == '\'' && !ctx.pending_redirect)
5313 goto insert_empty_quoted_str_marker;
5318 syntax_error_unterm_ch('\'');
5321 nommu_addchr(&ctx.as_string, ch);
5324 if (ch == SPECIAL_VAR_SYMBOL) {
5325 /* Convert raw ^C to corresponding special variable reference */
5326 o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL);
5327 o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS);
5329 o_addqchr(&ctx.word, ch);
5330 ch = i_getch(input);
5332 continue; /* get next char */
5337 next = i_peek_and_eat_bkslash_nl(input);
5339 is_special = "{}<>;&|()#" /* special outside of "str" */
5340 "$\"" IF_HUSH_TICK("`") /* always special */
5341 SPECIAL_VAR_SYMBOL_STR;
5342 /* Are { and } special here? */
5343 if (ctx.command->argv /* word [word]{... - non-special */
5344 || ctx.word.length /* word{... - non-special */
5345 || ctx.word.has_quoted_part /* ""{... - non-special */
5346 || (next != ';' /* }; - special */
5347 && next != ')' /* }) - special */
5348 && next != '(' /* {( - special */
5349 && next != '&' /* }& and }&& ... - special */
5350 && next != '|' /* }|| ... - special */
5351 && !strchr(defifs, next) /* {word - non-special */
5354 /* They are not special, skip "{}" */
5357 is_special = strchr(is_special, ch);
5358 is_blank = strchr(defifs, ch);
5360 if (!is_special && !is_blank) { /* ordinary char */
5362 o_addQchr(&ctx.word, ch);
5363 if ((ctx.is_assignment == MAYBE_ASSIGNMENT
5364 || ctx.is_assignment == WORD_IS_KEYWORD)
5366 && endofname(ctx.word.data)[0] == '='
5368 ctx.is_assignment = DEFINITELY_ASSIGNMENT;
5369 debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]);
5375 #if ENABLE_HUSH_LINENO_VAR
5377 * "while ...; do<whitespace><newline>
5379 * would think that "cmd" starts in <whitespace> -
5380 * i.e., at the previous line.
5381 * We need to skip all whitespace before newlines.
5383 while (ch != '\n') {
5384 next = i_peek(input);
5385 if (next != ' ' && next != '\t' && next != '\n')
5386 break; /* next char is not ws */
5387 ch = i_getch(input);
5389 /* ch == last eaten whitespace char */
5391 if (done_word(&ctx)) {
5395 /* Is this a case when newline is simply ignored?
5397 * "cmd | <newline> cmd ..."
5398 * "case ... in <newline> word) ..."
5400 if (IS_NULL_CMD(ctx.command)
5401 && ctx.word.length == 0
5402 && !ctx.word.has_quoted_part
5405 /* This newline can be ignored. But...
5406 * Without check #1, interactive shell
5407 * ignores even bare <newline>,
5408 * and shows the continuation prompt:
5409 * ps1_prompt$ <enter>
5410 * ps2> _ <=== wrong, should be ps1
5411 * Without check #2, "cmd & <newline>"
5412 * is similarly mistreated.
5413 * (BTW, this makes "cmd & cmd"
5414 * and "cmd && cmd" non-orthogonal.
5415 * Really, ask yourself, why
5416 * "cmd && <newline>" doesn't start
5417 * cmd but waits for more input?
5418 * The only reason is that it might be
5419 * a "cmd1 && <nl> cmd2 &" construct,
5420 * cmd1 may need to run in BG).
5422 struct pipe *pi = ctx.list_head;
5423 if (pi->num_cmds != 0 /* check #1 */
5424 && pi->followup != PIPE_BG /* check #2 */
5429 /* Treat newline as a command separator. */
5430 done_pipe(&ctx, PIPE_SEQ);
5431 debug_printf_heredoc("heredoc_cnt:%d\n", heredoc_cnt);
5433 heredoc_cnt = fetch_heredocs(&ctx.as_string, ctx.list_head, heredoc_cnt, input);
5434 if (heredoc_cnt != 0)
5437 ctx.is_assignment = MAYBE_ASSIGNMENT;
5438 debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]);
5440 /* note: if (is_blank) continue;
5441 * will still trigger for us */
5445 /* "cmd}" or "cmd }..." without semicolon or &:
5446 * } is an ordinary char in this case, even inside { cmd; }
5447 * Pathological example: { ""}; } should exec "}" cmd
5450 if (ctx.word.length != 0 /* word} */
5451 || ctx.word.has_quoted_part /* ""} */
5455 if (!IS_NULL_CMD(ctx.command)) { /* cmd } */
5456 /* Generally, there should be semicolon: "cmd; }"
5457 * However, bash allows to omit it if "cmd" is
5458 * a group. Examples:
5461 * { echo 0 >&2 | { echo 1; } }
5462 * { while false; do :; done }
5463 * { case a in b) ;; esac }
5465 if (ctx.command->group)
5469 if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */
5470 /* Can't be an end of {cmd}, skip the check */
5471 goto skip_end_trigger;
5472 /* else: } does terminate a group */
5475 if (end_trigger && end_trigger == ch
5476 && (ch != ';' || heredoc_cnt == 0)
5477 #if ENABLE_HUSH_CASE
5479 || ctx.ctx_res_w != RES_MATCH
5480 || (!ctx.word.has_quoted_part && strcmp(ctx.word.data, "esac") == 0)
5484 if (done_word(&ctx)) {
5487 done_pipe(&ctx, PIPE_SEQ);
5488 ctx.is_assignment = MAYBE_ASSIGNMENT;
5489 debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]);
5490 /* Do we sit outside of any if's, loops or case's? */
5492 IF_HAS_KEYWORDS(|| (ctx.ctx_res_w == RES_NONE && ctx.old_flag == 0))
5494 o_free_and_set_NULL(&ctx.word);
5496 debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data);
5498 *pstring = ctx.as_string.data;
5500 o_free(&ctx.as_string);
5502 if (ch != ';' && IS_NULL_PIPE(ctx.list_head)) {
5503 /* Example: bare "{ }", "()" */
5504 G.last_exitcode = 2; /* bash compat */
5505 syntax_error_unexpected_ch(ch);
5508 if (heredoc_cnt_ptr)
5509 *heredoc_cnt_ptr = heredoc_cnt;
5510 debug_printf_heredoc("parse_stream return heredoc_cnt:%d\n", heredoc_cnt);
5511 debug_printf_parse("parse_stream return %p: "
5512 "end_trigger char found\n",
5515 return ctx.list_head;
5522 /* Catch <, > before deciding whether this word is
5523 * an assignment. a=1 2>z b=2: b=2 is still assignment */
5526 redir_fd = redirect_opt_num(&ctx.word);
5527 if (done_word(&ctx)) {
5530 redir_style = REDIRECT_OVERWRITE;
5532 redir_style = REDIRECT_APPEND;
5533 ch = i_getch(input);
5534 nommu_addchr(&ctx.as_string, ch);
5537 else if (next == '(') {
5538 syntax_error(">(process) not supported");
5542 if (parse_redirect(&ctx, redir_fd, redir_style, input))
5544 continue; /* get next char */
5546 redir_fd = redirect_opt_num(&ctx.word);
5547 if (done_word(&ctx)) {
5550 redir_style = REDIRECT_INPUT;
5552 redir_style = REDIRECT_HEREDOC;
5554 debug_printf_heredoc("++heredoc_cnt=%d\n", heredoc_cnt);
5555 ch = i_getch(input);
5556 nommu_addchr(&ctx.as_string, ch);
5557 } else if (next == '>') {
5558 redir_style = REDIRECT_IO;
5559 ch = i_getch(input);
5560 nommu_addchr(&ctx.as_string, ch);
5563 else if (next == '(') {
5564 syntax_error("<(process) not supported");
5568 if (parse_redirect(&ctx, redir_fd, redir_style, input))
5570 continue; /* get next char */
5572 if (ctx.word.length == 0 && !ctx.word.has_quoted_part) {
5573 /* skip "#comment" */
5574 /* note: we do not add it to &ctx.as_string */
5576 * comment inside $() goes to the next \n, even inside quoted string (!):
5577 * cmd "$(cmd2 #comment)" - syntax error
5578 * cmd "`cmd2 #comment`" - ok
5579 * We accept both (comment ends where command subst ends, in both cases).
5584 nommu_addchr(&ctx.as_string, '\n');
5587 ch = i_getch(input);
5591 continue; /* get next char */
5597 if (ctx.is_assignment == MAYBE_ASSIGNMENT
5598 /* check that we are not in word in "a=1 2>word b=1": */
5599 && !ctx.pending_redirect
5601 /* ch is a special char and thus this word
5602 * cannot be an assignment */
5603 ctx.is_assignment = NOT_ASSIGNMENT;
5604 debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]);
5607 /* Note: nommu_addchr(&ctx.as_string, ch) is already done */
5610 case SPECIAL_VAR_SYMBOL:
5611 /* Convert raw ^C to corresponding special variable reference */
5612 o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL);
5613 o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS);
5616 /* non-comment #: "echo a#b" etc */
5617 o_addchr(&ctx.word, ch);
5618 continue; /* get next char */
5620 if (!parse_dollar(&ctx.as_string, &ctx.word, input, /*quote_mask:*/ 0)) {
5621 debug_printf_parse("parse_stream parse error: "
5622 "parse_dollar returned 0 (error)\n");
5625 continue; /* get next char */
5627 ctx.word.has_quoted_part = 1;
5628 if (next == '"' && !ctx.pending_redirect) {
5629 i_getch(input); /* eat second " */
5630 insert_empty_quoted_str_marker:
5631 nommu_addchr(&ctx.as_string, next);
5632 o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL);
5633 o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL);
5634 continue; /* get next char */
5636 if (ctx.is_assignment == NOT_ASSIGNMENT)
5637 ctx.word.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS;
5638 if (!encode_string(&ctx.as_string, &ctx.word, input, '"'))
5640 ctx.word.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS;
5641 continue; /* get next char */
5642 #if ENABLE_HUSH_TICK
5644 USE_FOR_NOMMU(unsigned pos;)
5646 o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL);
5647 o_addchr(&ctx.word, '`');
5648 USE_FOR_NOMMU(pos = ctx.word.length;)
5649 if (!add_till_backquote(&ctx.word, input, /*in_dquote:*/ 0))
5652 o_addstr(&ctx.as_string, ctx.word.data + pos);
5653 o_addchr(&ctx.as_string, '`');
5655 o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL);
5656 //debug_printf_subst("SUBST RES3 '%s'\n", ctx.word.data + pos);
5657 continue; /* get next char */
5661 #if ENABLE_HUSH_CASE
5664 if (done_word(&ctx)) {
5667 done_pipe(&ctx, PIPE_SEQ);
5668 #if ENABLE_HUSH_CASE
5669 /* Eat multiple semicolons, detect
5670 * whether it means something special */
5672 ch = i_peek_and_eat_bkslash_nl(input);
5675 ch = i_getch(input);
5676 nommu_addchr(&ctx.as_string, ch);
5677 if (ctx.ctx_res_w == RES_CASE_BODY) {
5678 ctx.ctx_dsemicolon = 1;
5679 ctx.ctx_res_w = RES_MATCH;
5685 /* We just finished a cmd. New one may start
5686 * with an assignment */
5687 ctx.is_assignment = MAYBE_ASSIGNMENT;
5688 debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]);
5689 continue; /* get next char */
5691 if (done_word(&ctx)) {
5695 ch = i_getch(input);
5696 nommu_addchr(&ctx.as_string, ch);
5697 done_pipe(&ctx, PIPE_AND);
5699 done_pipe(&ctx, PIPE_BG);
5703 if (done_word(&ctx)) {
5706 #if ENABLE_HUSH_CASE
5707 if (ctx.ctx_res_w == RES_MATCH)
5708 break; /* we are in case's "word | word)" */
5710 if (next == '|') { /* || */
5711 ch = i_getch(input);
5712 nommu_addchr(&ctx.as_string, ch);
5713 done_pipe(&ctx, PIPE_OR);
5715 /* we could pick up a file descriptor choice here
5716 * with redirect_opt_num(), but bash doesn't do it.
5717 * "echo foo 2| cat" yields "foo 2". */
5722 #if ENABLE_HUSH_CASE
5723 /* "case... in [(]word)..." - skip '(' */
5724 if (ctx.ctx_res_w == RES_MATCH
5725 && ctx.command->argv == NULL /* not (word|(... */
5726 && ctx.word.length == 0 /* not word(... */
5727 && ctx.word.has_quoted_part == 0 /* not ""(... */
5729 continue; /* get next char */
5734 int n = parse_group(&ctx, input, ch);
5738 debug_printf_heredoc("parse_group done, needs heredocs:%d\n", n);
5743 #if ENABLE_HUSH_CASE
5744 if (ctx.ctx_res_w == RES_MATCH)
5749 /* proper use of this character is caught by end_trigger:
5750 * if we see {, we call parse_group(..., end_trigger='}')
5751 * and it will match } earlier (not here). */
5752 G.last_exitcode = 2;
5753 syntax_error_unexpected_ch(ch);
5757 bb_error_msg_and_die("BUG: unexpected %c", ch);
5762 G.last_exitcode = 1;
5765 struct parse_context *pctx;
5766 IF_HAS_KEYWORDS(struct parse_context *p2;)
5768 /* Clean up allocated tree.
5769 * Sample for finding leaks on syntax error recovery path.
5770 * Run it from interactive shell, watch pmap `pidof hush`.
5771 * while if false; then false; fi; do break; fi
5772 * Samples to catch leaks at execution:
5773 * while if (true | { true;}); then echo ok; fi; do break; done
5774 * while if (true | { true;}); then echo ok; fi; do (if echo ok; break; then :; fi) | cat; break; done
5778 /* Update pipe/command counts,
5779 * otherwise freeing may miss some */
5780 done_pipe(pctx, PIPE_SEQ);
5781 debug_printf_clean("freeing list %p from ctx %p\n",
5782 pctx->list_head, pctx);
5783 debug_print_tree(pctx->list_head, 0);
5784 free_pipe_list(pctx->list_head);
5785 debug_printf_clean("freed list %p\n", pctx->list_head);
5787 o_free(&pctx->as_string);
5789 IF_HAS_KEYWORDS(p2 = pctx->stack;)
5793 IF_HAS_KEYWORDS(pctx = p2;)
5794 } while (HAS_KEYWORDS && pctx);
5807 /*** Execution routines ***/
5809 /* Expansion can recurse, need forward decls: */
5810 #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE
5811 #define expand_string_to_string(str, EXP_flags, do_unbackslash) \
5812 expand_string_to_string(str)
5814 static char *expand_string_to_string(const char *str, int EXP_flags, int do_unbackslash);
5815 #if ENABLE_HUSH_TICK
5816 static int process_command_subs(o_string *dest, const char *s);
5818 static int expand_vars_to_list(o_string *output, int n, char *arg);
5820 /* expand_strvec_to_strvec() takes a list of strings, expands
5821 * all variable references within and returns a pointer to
5822 * a list of expanded strings, possibly with larger number
5823 * of strings. (Think VAR="a b"; echo $VAR).
5824 * This new list is allocated as a single malloc block.
5825 * NULL-terminated list of char* pointers is at the beginning of it,
5826 * followed by strings themselves.
5827 * Caller can deallocate entire list by single free(list). */
5829 /* A horde of its helpers come first: */
5831 static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len)
5833 while (--len >= 0) {
5836 #if ENABLE_HUSH_BRACE_EXPANSION
5837 if (c == '{' || c == '}') {
5838 /* { -> \{, } -> \} */
5840 /* And now we want to add { or } and continue:
5843 * luckily, just falling through achieves this.
5849 /* \z -> \\\z; \<eol> -> \\<eol> */
5854 o_addchr(o, *str++);
5860 /* Store given string, finalizing the word and starting new one whenever
5861 * we encounter IFS char(s). This is used for expanding variable values.
5862 * End-of-string does NOT finalize word: think about 'echo -$VAR-'.
5863 * Return in output->ended_in_ifs:
5864 * 1 - ended with IFS char, else 0 (this includes case of empty str).
5866 static int expand_on_ifs(o_string *output, int n, const char *str)
5868 int last_is_ifs = 0;
5873 if (!*str) /* EOL - do not finalize word */
5875 word_len = strcspn(str, G.ifs);
5877 /* We have WORD_LEN leading non-IFS chars */
5878 if (!(output->o_expflags & EXP_FLAG_GLOB)) {
5879 o_addblock(output, str, word_len);
5881 /* Protect backslashes against globbing up :)
5882 * Example: "v='\*'; echo b$v" prints "b\*"
5883 * (and does not try to glob on "*")
5885 o_addblock_duplicate_backslash(output, str, word_len);
5886 /*/ Why can't we do it easier? */
5887 /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */
5888 /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */
5892 if (!*str) /* EOL - do not finalize word */
5896 /* We know str here points to at least one IFS char */
5898 str += strspn(str, G.ifs_whitespace); /* skip IFS whitespace chars */
5899 if (!*str) /* EOL - do not finalize word */
5902 if (G.ifs_whitespace != G.ifs /* usually false ($IFS is usually all whitespace), */
5903 && strchr(G.ifs, *str) /* the second check would fail */
5905 /* This is a non-whitespace $IFS char */
5906 /* Skip it and IFS whitespace chars, start new word */
5908 str += strspn(str, G.ifs_whitespace);
5912 /* Start new word... but not always! */
5913 /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */
5914 if (output->has_quoted_part
5916 * Case "v=' a'; echo $v":
5917 * here nothing precedes the space in $v expansion,
5918 * therefore we should not finish the word
5919 * (IOW: if there *is* word to finalize, only then do it):
5920 * It's okay if this accesses the byte before first argv[]:
5921 * past call to o_save_ptr() cleared it to zero byte
5922 * (grep for -prev-ifs-check-).
5924 || output->data[output->length - 1]
5927 o_addchr(output, '\0');
5928 debug_print_list("expand_on_ifs", output, n);
5929 n = o_save_ptr(output, n);
5933 output->ended_in_ifs = last_is_ifs;
5934 debug_print_list("expand_on_ifs[1]", output, n);
5938 /* Helper to expand $((...)) and heredoc body. These act as if
5939 * they are in double quotes, with the exception that they are not :).
5940 * Just the rules are similar: "expand only $var and `cmd`"
5942 * Returns malloced string.
5943 * As an optimization, we return NULL if expansion is not needed.
5945 static char *encode_then_expand_string(const char *str)
5948 struct in_str input;
5949 o_string dest = NULL_O_STRING;
5954 if (!*cp) return NULL; /* string has no special chars */
5955 if (*cp == '$') break;
5956 if (*cp == '\\') break;
5957 #if ENABLE_HUSH_TICK
5958 if (*cp == '`') break;
5963 /* We need to expand. Example:
5964 * echo $(($a + `echo 1`)) $((1 + $((2)) ))
5966 setup_string_in_str(&input, str);
5967 encode_string(NULL, &dest, &input, EOF);
5968 //TODO: error check (encode_string returns 0 on error)?
5969 //bb_error_msg("'%s' -> '%s'", str, dest.data);
5970 exp_str = expand_string_to_string(dest.data,
5971 EXP_FLAG_ESC_GLOB_CHARS,
5974 //bb_error_msg("'%s' -> '%s'", dest.data, exp_str);
5979 static const char *first_special_char_in_vararg(const char *cp)
5982 if (!*cp) return NULL; /* string has no special chars */
5983 if (*cp == '$') return cp;
5984 if (*cp == '\\') return cp;
5985 if (*cp == '\'') return cp;
5986 if (*cp == '"') return cp;
5987 #if ENABLE_HUSH_TICK
5988 if (*cp == '`') return cp;
5990 /* dquoted "${x:+ARG}" should not glob, therefore
5991 * '*' et al require some non-literal processing: */
5992 if (*cp == '*') return cp;
5993 if (*cp == '?') return cp;
5994 if (*cp == '[') return cp;
5999 /* Expanding ARG in ${var#ARG}, ${var%ARG}, or ${var/ARG/ARG}.
6000 * These can contain single- and double-quoted strings,
6001 * and treated as if the ARG string is initially unquoted. IOW:
6002 * ${var#ARG} and "${var#ARG}" treat ARG the same (ARG can even be
6003 * a dquoted string: "${var#"zz"}"), the difference only comes later
6004 * (word splitting and globbing of the ${var...} result).
6006 #if !BASH_PATTERN_SUBST
6007 #define encode_then_expand_vararg(str, handle_squotes, do_unbackslash) \
6008 encode_then_expand_vararg(str, handle_squotes)
6010 static char *encode_then_expand_vararg(const char *str, int handle_squotes, int do_unbackslash)
6012 #if !BASH_PATTERN_SUBST && ENABLE_HUSH_CASE
6013 const int do_unbackslash = 0;
6016 struct in_str input;
6017 o_string dest = NULL_O_STRING;
6019 if (!first_special_char_in_vararg(str)) {
6020 /* string has no special chars */
6024 setup_string_in_str(&input, str);
6025 dest.data = xzalloc(1); /* start as "", not as NULL */
6031 ch = i_getch(&input);
6032 debug_printf_parse("%s: ch=%c (%d) escape=%d\n",
6033 __func__, ch, ch, !!dest.o_expflags);
6035 if (!dest.o_expflags) {
6038 if (handle_squotes && ch == '\'') {
6039 if (!add_till_single_quote_dquoted(&dest, &input))
6040 goto ret; /* error */
6045 syntax_error_unterm_ch('"');
6046 goto ret; /* error */
6049 dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS;
6053 ch = i_getch(&input);
6055 //example? error message? syntax_error_unterm_ch('"');
6056 debug_printf_parse("%s: error: \\<eof>\n", __func__);
6059 o_addqchr(&dest, ch);
6063 if (!parse_dollar(NULL, &dest, &input, /*quote_mask:*/ 0x80)) {
6064 debug_printf_parse("%s: error: parse_dollar returned 0 (error)\n", __func__);
6069 #if ENABLE_HUSH_TICK
6071 //unsigned pos = dest->length;
6072 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6073 o_addchr(&dest, 0x80 | '`');
6074 if (!add_till_backquote(&dest, &input,
6075 /*in_dquote:*/ dest.o_expflags /* nonzero if EXP_FLAG_ESC_GLOB_CHARS set */
6078 goto ret; /* error */
6080 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6081 //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos);
6085 o_addQchr(&dest, ch);
6088 debug_printf_parse("encode: '%s' -> '%s'\n", str, dest.data);
6089 exp_str = expand_string_to_string(dest.data,
6090 do_unbackslash ? EXP_FLAG_ESC_GLOB_CHARS : 0,
6094 debug_printf_parse("expand: '%s' -> '%s'\n", dest.data, exp_str);
6099 /* Expanding ARG in ${var+ARG}, ${var-ARG}
6101 static int encode_then_append_var_plusminus(o_string *output, int n,
6102 char *str, int dquoted)
6104 struct in_str input;
6105 o_string dest = NULL_O_STRING;
6107 if (!first_special_char_in_vararg(str)
6108 && '\0' == str[strcspn(str, G.ifs)]
6110 /* string has no special chars
6111 * && string has no $IFS chars
6114 /* Prints 1 (quoted expansion is a "" word, not nothing):
6115 * set -- "${notexist-}"; echo $#
6117 output->has_quoted_part = 1;
6119 return expand_vars_to_list(output, n, str);
6122 setup_string_in_str(&input, str);
6127 ch = i_getch(&input);
6128 debug_printf_parse("%s: ch=%c (%d) escape=%x\n",
6129 __func__, ch, ch, dest.o_expflags);
6131 if (!dest.o_expflags) {
6134 if (!dquoted && strchr(G.ifs, ch)) {
6135 /* PREFIX${x:d${e}f ...} and we met space: expand "d${e}f" and start new word.
6136 * do not assume we are at the start of the word (PREFIX above).
6139 n = expand_vars_to_list(output, n, dest.data);
6140 o_free_and_set_NULL(&dest);
6141 o_addchr(output, '\0');
6142 n = o_save_ptr(output, n); /* create next word */
6144 if (output->length != o_get_last_ptr(output, n)
6145 || output->has_quoted_part
6148 * f() { for i; do echo "|$i|"; done; }; x=x
6149 * f a${x:+ }b # 1st condition
6152 * f ""${x:+ }b # 2nd condition
6156 o_addchr(output, '\0');
6157 n = o_save_ptr(output, n); /* create next word */
6161 if (!dquoted && ch == '\'') {
6162 if (!add_till_single_quote_dquoted(&dest, &input))
6163 goto ret; /* error */
6164 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6165 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6170 syntax_error_unterm_ch('"');
6171 goto ret; /* error */
6174 dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS;
6175 if (dest.o_expflags) {
6176 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6177 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6182 ch = i_getch(&input);
6184 //example? error message? syntax_error_unterm_ch('"');
6185 debug_printf_parse("%s: error: \\<eof>\n", __func__);
6188 o_addqchr(&dest, ch);
6192 if (!parse_dollar(NULL, &dest, &input, /*quote_mask:*/ (dest.o_expflags || dquoted) ? 0x80 : 0)) {
6193 debug_printf_parse("%s: error: parse_dollar returned 0 (error)\n", __func__);
6198 #if ENABLE_HUSH_TICK
6200 //unsigned pos = dest->length;
6201 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6202 o_addchr(&dest, (dest.o_expflags || dquoted) ? 0x80 | '`' : '`');
6203 if (!add_till_backquote(&dest, &input,
6204 /*in_dquote:*/ dest.o_expflags /* nonzero if EXP_FLAG_ESC_GLOB_CHARS set */
6207 goto ret; /* error */
6209 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6210 //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos);
6215 /* Always glob-protect if in dquotes:
6216 * x=x; echo "${x:+/bin/c*}" - prints: /bin/c*
6217 * x=x; echo "${x:+"/bin/c*"}" - prints: /bin/c*
6219 o_addqchr(&dest, ch);
6221 /* Glob-protect only if char is quoted:
6222 * x=x; echo ${x:+/bin/c*} - prints many filenames
6223 * x=x; echo ${x:+"/bin/c*"} - prints: /bin/c*
6225 o_addQchr(&dest, ch);
6230 n = expand_vars_to_list(output, n, dest.data);
6237 #if ENABLE_FEATURE_SH_MATH
6238 static arith_t expand_and_evaluate_arith(const char *arg, const char **errmsg_p)
6240 arith_state_t math_state;
6244 math_state.lookupvar = get_local_var_value;
6245 math_state.setvar = set_local_var_from_halves;
6246 //math_state.endofname = endofname;
6247 exp_str = encode_then_expand_string(arg);
6248 res = arith(&math_state, exp_str ? exp_str : arg);
6251 *errmsg_p = math_state.errmsg;
6252 if (math_state.errmsg)
6253 msg_and_die_if_script(math_state.errmsg);
6258 #if BASH_PATTERN_SUBST
6259 /* ${var/[/]pattern[/repl]} helpers */
6260 static char *strstr_pattern(char *val, const char *pattern, int *size)
6263 char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF);
6264 debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end);
6271 /* Optimization: if "*pat" did not match the start of "string",
6272 * we know that "tring", "ring" etc will not match too:
6274 if (pattern[0] == '*')
6279 static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op)
6281 char *result = NULL;
6282 unsigned res_len = 0;
6283 unsigned repl_len = strlen(repl);
6285 /* Null pattern never matches, including if "var" is empty */
6287 return result; /* NULL, no replaces happened */
6291 char *s = strstr_pattern(val, pattern, &size);
6295 result = xrealloc(result, res_len + (s - val) + repl_len + 1);
6296 strcpy(mempcpy(result + res_len, val, s - val), repl);
6297 res_len += (s - val) + repl_len;
6298 debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result);
6304 if (*val && result) {
6305 result = xrealloc(result, res_len + strlen(val) + 1);
6306 strcpy(result + res_len, val);
6307 debug_printf_varexp("val:'%s' result:'%s'\n", val, result);
6309 debug_printf_varexp("result:'%s'\n", result);
6312 #endif /* BASH_PATTERN_SUBST */
6314 static int append_str_maybe_ifs_split(o_string *output, int n,
6315 int first_ch, const char *val)
6317 if (!(first_ch & 0x80)) { /* unquoted $VAR */
6318 debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val,
6319 !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
6321 n = expand_on_ifs(output, n, val);
6322 } else { /* quoted "$VAR" */
6323 output->has_quoted_part = 1;
6324 debug_printf_expand("quoted '%s', output->o_escape:%d\n", val,
6325 !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
6327 o_addQstr(output, val);
6332 /* Handle <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct.
6334 static NOINLINE int expand_one_var(o_string *output, int n,
6335 int first_ch, char *arg, char **pp)
6342 char exp_save = exp_save; /* for compiler */
6343 char *exp_saveptr; /* points to expansion operator */
6344 char *exp_word = exp_word; /* for compiler */
6350 *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */
6352 exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL;
6354 arg[0] = (arg0 & 0x7f);
6357 if (arg[0] == '#' && arg[1] /* ${#...} but not ${#} */
6358 && (!exp_saveptr /* and ( not(${#<op_char>...}) */
6359 || (arg[2] == '\0' && strchr(SPECIAL_VARS_STR, arg[1])) /* or ${#C} "len of $C" ) */
6360 ) /* NB: skipping ^^^specvar check mishandles ${#::2} */
6362 /* It must be length operator: ${#var} */
6366 /* Maybe handle parameter expansion */
6367 if (exp_saveptr /* if 2nd char is one of expansion operators */
6368 && strchr(NUMERIC_SPECVARS_STR, arg[0]) /* 1st char is special variable */
6370 /* ${?:0}, ${#[:]%0} etc */
6371 exp_saveptr = var + 1;
6373 /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */
6374 exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS);
6376 exp_op = exp_save = *exp_saveptr;
6378 exp_word = exp_saveptr + 1;
6379 if (exp_op == ':') {
6380 exp_op = *exp_word++;
6381 //TODO: try ${var:} and ${var:bogus} in non-bash config
6383 && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op))
6385 /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */
6390 *exp_saveptr = '\0';
6391 } /* else: it's not an expansion op, but bare ${var} */
6394 /* Look up the variable in question */
6395 if (isdigit(var[0])) {
6396 /* parse_dollar should have vetted var for us */
6397 int nn = xatoi_positive(var);
6398 if (nn < G.global_argc)
6399 val = G.global_argv[nn];
6400 /* else val remains NULL: $N with too big N */
6404 val = utoa(G.root_pid);
6406 case '!': /* bg pid */
6407 val = G.last_bg_pid ? utoa(G.last_bg_pid) : "";
6409 case '?': /* exitcode */
6410 val = utoa(G.last_exitcode);
6412 case '#': /* argc */
6413 val = utoa(G.global_argc ? G.global_argc-1 : 0);
6415 case '-': { /* active options */
6416 /* Check set_mode() to see what option chars we support */
6418 val = cp = G.optstring_buf;
6419 if (G.o_opt[OPT_O_ERREXIT])
6421 if (G_interactive_fd)
6425 /* If G.o_opt[OPT_O_NOEXEC] is true,
6426 * commands read but are not executed,
6427 * so $- can not execute too, 'n' is never seen in $-.
6437 val = get_local_var_value(var);
6441 /* Handle any expansions */
6442 if (exp_op == 'L') {
6443 reinit_unicode_for_hush();
6444 debug_printf_expand("expand: length(%s)=", val);
6445 val = utoa(val ? unicode_strlen(val) : 0);
6446 debug_printf_expand("%s\n", val);
6447 } else if (exp_op) {
6448 if (exp_op == '%' || exp_op == '#') {
6449 /* Standard-mandated substring removal ops:
6450 * ${parameter%word} - remove smallest suffix pattern
6451 * ${parameter%%word} - remove largest suffix pattern
6452 * ${parameter#word} - remove smallest prefix pattern
6453 * ${parameter##word} - remove largest prefix pattern
6455 * Word is expanded to produce a glob pattern.
6456 * Then var's value is matched to it and matching part removed.
6458 //FIXME: ${x#...${...}...}
6459 //should evaluate inner ${...} even if x is "" and no shrinking of it is possible -
6460 //inner ${...} may have side effects!
6461 if (val && val[0]) {
6465 unsigned scan_flags = pick_scan(exp_op, *exp_word);
6466 if (exp_op == *exp_word) /* ## or %% */
6468 debug_printf_expand("expand: exp_word:'%s'\n", exp_word);
6469 exp_exp_word = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 0);
6471 exp_word = exp_exp_word;
6472 debug_printf_expand("expand: exp_word:'%s'\n", exp_word);
6474 * HACK ALERT. We depend here on the fact that
6475 * G.global_argv and results of utoa and get_local_var_value
6476 * are actually in writable memory:
6477 * scan_and_match momentarily stores NULs there.
6480 loc = scan_and_match(t, exp_word, scan_flags);
6481 debug_printf_expand("op:%c str:'%s' pat:'%s' res:'%s'\n", exp_op, t, exp_word, loc);
6483 if (loc) { /* match was found */
6484 if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */
6485 val = loc; /* take right part */
6487 val = to_be_freed = xstrndup(val, loc - val); /* left */
6491 #if BASH_PATTERN_SUBST
6492 else if (exp_op == '/' || exp_op == '\\') {
6493 /* It's ${var/[/]pattern[/repl]} thing.
6494 * Note that in encoded form it has TWO parts:
6495 * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL>
6496 * and if // is used, it is encoded as \:
6497 * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL>
6499 if (val && val[0]) {
6500 /* pattern uses non-standard expansion.
6501 * repl should be unbackslashed and globbed
6502 * by the usual expansion rules:
6504 * v='a bz'; echo "${v/a*z/a*z}" #prints "a*z"
6505 * v='a bz'; echo "${v/a*z/\z}" #prints "z"
6506 * v='a bz'; echo ${v/a*z/a*z} #prints "az"
6507 * v='a bz'; echo ${v/a*z/\z} #prints "z"
6508 * (note that a*z _pattern_ is never globbed!)
6510 char *pattern, *repl, *t;
6511 pattern = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 0);
6513 pattern = xstrdup(exp_word);
6514 debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern);
6515 *p++ = SPECIAL_VAR_SYMBOL;
6517 p = strchr(p, SPECIAL_VAR_SYMBOL);
6519 repl = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 1);
6520 debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl);
6521 /* HACK ALERT. We depend here on the fact that
6522 * G.global_argv and results of utoa and get_local_var_value
6523 * are actually in writable memory:
6524 * replace_pattern momentarily stores NULs there. */
6526 to_be_freed = replace_pattern(t,
6528 (repl ? repl : exp_word),
6530 if (to_be_freed) /* at least one replace happened */
6535 /* Empty variable always gives nothing */
6536 // "v=''; echo ${v/*/w}" prints "", not "w"
6537 /* Just skip "replace" part */
6538 *p++ = SPECIAL_VAR_SYMBOL;
6539 p = strchr(p, SPECIAL_VAR_SYMBOL);
6543 #endif /* BASH_PATTERN_SUBST */
6544 else if (exp_op == ':') {
6545 #if BASH_SUBSTR && ENABLE_FEATURE_SH_MATH
6546 /* It's ${var:N[:M]} bashism.
6547 * Note that in encoded form it has TWO parts:
6548 * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL>
6553 beg = expand_and_evaluate_arith(exp_word, &errmsg);
6556 debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg);
6557 *p++ = SPECIAL_VAR_SYMBOL;
6559 p = strchr(p, SPECIAL_VAR_SYMBOL);
6561 len = expand_and_evaluate_arith(exp_word, &errmsg);
6564 debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len);
6566 /* negative beg counts from the end */
6567 beg = (arith_t)strlen(val) + beg;
6568 if (beg < 0) /* ${v: -999999} is "" */
6571 debug_printf_varexp("from val:'%s'\n", val);
6573 /* in bash, len=-n means strlen()-n */
6574 len = (arith_t)strlen(val) - beg + len;
6575 if (len < 0) /* bash compat */
6576 msg_and_die_if_script("%s: substring expression < 0", var);
6578 if (len <= 0 || !val || beg >= strlen(val)) {
6582 /* Paranoia. What if user entered 9999999999999
6583 * which fits in arith_t but not int? */
6586 val = to_be_freed = xstrndup(val + beg, len);
6588 debug_printf_varexp("val:'%s'\n", val);
6589 #else /* not (HUSH_SUBSTR_EXPANSION && FEATURE_SH_MATH) */
6590 msg_and_die_if_script("malformed ${%s:...}", var);
6593 } else { /* one of "-=+?" */
6594 /* Standard-mandated substitution ops:
6595 * ${var?word} - indicate error if unset
6596 * If var is unset, word (or a message indicating it is unset
6597 * if word is null) is written to standard error
6598 * and the shell exits with a non-zero exit status.
6599 * Otherwise, the value of var is substituted.
6600 * ${var-word} - use default value
6601 * If var is unset, word is substituted.
6602 * ${var=word} - assign and use default value
6603 * If var is unset, word is assigned to var.
6604 * In all cases, final value of var is substituted.
6605 * ${var+word} - use alternative value
6606 * If var is unset, null is substituted.
6607 * Otherwise, word is substituted.
6609 * Word is subjected to tilde expansion, parameter expansion,
6610 * command substitution, and arithmetic expansion.
6611 * If word is not needed, it is not expanded.
6613 * Colon forms (${var:-word}, ${var:=word} etc) do the same,
6614 * but also treat null var as if it is unset.
6616 * Word-splitting and single quote behavior:
6618 * $ f() { for i; do echo "|$i|"; done; };
6620 * $ x=; f ${x:?'x y' z}
6621 * bash: x: x y z #BUG: does not abort, ${} results in empty expansion
6622 * $ x=; f "${x:?'x y' z}"
6623 * bash: x: x y z # dash prints: dash: x: 'x y' z #BUG: does not abort, ${} results in ""
6625 * $ x=; f ${x:='x y' z}
6629 * $ x=; f "${x:='x y' z}"
6632 * $ x=x; f ${x:+'x y' z}
6635 * $ x=x; f "${x:+'x y' z}"
6638 * $ x=; f ${x:-'x y' z}
6641 * $ x=; f "${x:-'x y' z}"
6644 int use_word = (!val || ((exp_save == ':') && !val[0]));
6646 use_word = !use_word;
6647 debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op,
6648 (exp_save == ':') ? "true" : "false", use_word);
6650 if (exp_op == '+' || exp_op == '-') {
6651 /* ${var+word} - use alternative value */
6652 /* ${var-word} - use default value */
6653 n = encode_then_append_var_plusminus(output, n, exp_word,
6654 /*dquoted:*/ (arg0 & 0x80)
6658 /* ${var?word} - indicate error if unset */
6659 /* ${var=word} - assign and use default value */
6660 to_be_freed = encode_then_expand_vararg(exp_word,
6661 /*handle_squotes:*/ !(arg0 & 0x80),
6665 exp_word = to_be_freed;
6666 if (exp_op == '?') {
6667 /* mimic bash message */
6668 msg_and_die_if_script("%s: %s",
6672 : "parameter null or not set"
6673 /* ash has more specific messages, a-la: */
6674 /*: (exp_save == ':' ? "parameter null or not set" : "parameter not set")*/
6676 //TODO: how interactive bash aborts expansion mid-command?
6677 //It aborts the entire line, returns to prompt:
6678 // $ f() { for i; do echo "|$i|"; done; }; x=; f "${x:?'x y' z}"; echo YO
6681 // ("echo YO" is not executed, neither the f function call)
6685 if (exp_op == '=') {
6686 /* ${var=[word]} or ${var:=[word]} */
6687 if (isdigit(var[0]) || var[0] == '#') {
6688 /* mimic bash message */
6689 msg_and_die_if_script("$%s: cannot assign in this way", var);
6692 char *new_var = xasprintf("%s=%s", var, val);
6693 set_local_var(new_var, /*flag:*/ 0);
6698 } /* one of "-=+?" */
6700 *exp_saveptr = exp_save;
6706 n = append_str_maybe_ifs_split(output, n, first_ch, val);
6712 /* Expand all variable references in given string, adding words to list[]
6713 * at n, n+1,... positions. Return updated n (so that list[n] is next one
6714 * to be filled). This routine is extremely tricky: has to deal with
6715 * variables/parameters with whitespace, $* and $@, and constructs like
6716 * 'echo -$*-'. If you play here, you must run testsuite afterwards! */
6717 static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg)
6719 /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in
6720 * expansion of right-hand side of assignment == 1-element expand.
6722 char cant_be_null = 0; /* only bit 0x80 matters */
6725 debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg,
6726 !!(output->o_expflags & EXP_FLAG_SINGLEWORD));
6727 debug_print_list("expand_vars_to_list[0]", output, n);
6729 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) {
6731 #if ENABLE_FEATURE_SH_MATH
6732 char arith_buf[sizeof(arith_t)*3 + 2];
6735 if (output->ended_in_ifs) {
6736 o_addchr(output, '\0');
6737 n = o_save_ptr(output, n);
6738 output->ended_in_ifs = 0;
6741 o_addblock(output, arg, p - arg);
6742 debug_print_list("expand_vars_to_list[1]", output, n);
6744 p = strchr(p, SPECIAL_VAR_SYMBOL);
6746 /* Fetch special var name (if it is indeed one of them)
6747 * and quote bit, force the bit on if singleword expansion -
6748 * important for not getting v=$@ expand to many words. */
6749 first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD);
6751 /* Is this variable quoted and thus expansion can't be null?
6752 * "$@" is special. Even if quoted, it can still
6753 * expand to nothing (not even an empty string),
6754 * thus it is excluded. */
6755 if ((first_ch & 0x7f) != '@')
6756 cant_be_null |= first_ch;
6758 switch (first_ch & 0x7f) {
6759 /* Highest bit in first_ch indicates that var is double-quoted */
6763 if (!G.global_argv[1])
6766 cant_be_null |= first_ch; /* do it for "$@" _now_, when we know it's not empty */
6767 if (!(first_ch & 0x80)) { /* unquoted $* or $@ */
6768 while (G.global_argv[i]) {
6769 n = expand_on_ifs(output, n, G.global_argv[i]);
6770 debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1);
6771 if (G.global_argv[i++][0] && G.global_argv[i]) {
6772 /* this argv[] is not empty and not last:
6773 * put terminating NUL, start new word */
6774 o_addchr(output, '\0');
6775 debug_print_list("expand_vars_to_list[2]", output, n);
6776 n = o_save_ptr(output, n);
6777 debug_print_list("expand_vars_to_list[3]", output, n);
6781 /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....'
6782 * and in this case should treat it like '$*' - see 'else...' below */
6783 if (first_ch == (char)('@'|0x80) /* quoted $@ */
6784 && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */
6787 o_addQstr(output, G.global_argv[i]);
6788 if (++i >= G.global_argc)
6790 o_addchr(output, '\0');
6791 debug_print_list("expand_vars_to_list[4]", output, n);
6792 n = o_save_ptr(output, n);
6794 } else { /* quoted $* (or v="$@" case): add as one word */
6796 o_addQstr(output, G.global_argv[i]);
6797 if (!G.global_argv[++i])
6800 o_addchr(output, G.ifs[0]);
6802 output->has_quoted_part = 1;
6806 case SPECIAL_VAR_SYMBOL: {
6807 /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */
6808 /* "Empty variable", used to make "" etc to not disappear */
6809 output->has_quoted_part = 1;
6810 cant_be_null = 0x80;
6814 case SPECIAL_VAR_QUOTED_SVS:
6815 /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_QUOTED_SVS><SPECIAL_VAR_SYMBOL> */
6816 /* "^C variable", represents literal ^C char (possible in scripts) */
6817 o_addchr(output, SPECIAL_VAR_SYMBOL);
6820 #if ENABLE_HUSH_TICK
6822 /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */
6823 o_string subst_result = NULL_O_STRING;
6825 *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */
6827 /* Can't just stuff it into output o_string,
6828 * expanded result may need to be globbed
6830 debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch);
6831 G.last_exitcode = process_command_subs(&subst_result, arg);
6832 G.expand_exitcode = G.last_exitcode;
6833 debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data);
6834 n = append_str_maybe_ifs_split(output, n, first_ch, subst_result.data);
6835 o_free(&subst_result);
6839 #if ENABLE_FEATURE_SH_MATH
6841 /* <SPECIAL_VAR_SYMBOL>+arith<SPECIAL_VAR_SYMBOL> */
6844 arg++; /* skip '+' */
6845 *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */
6846 debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch);
6847 res = expand_and_evaluate_arith(arg, NULL);
6848 debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res);
6849 sprintf(arith_buf, ARITH_FMT, res);
6850 o_addstr(output, arith_buf);
6855 /* <SPECIAL_VAR_SYMBOL>varname[ops]<SPECIAL_VAR_SYMBOL> */
6856 n = expand_one_var(output, n, first_ch, arg, &p);
6858 } /* switch (char after <SPECIAL_VAR_SYMBOL>) */
6860 /* Restore NULL'ed SPECIAL_VAR_SYMBOL.
6861 * Do the check to avoid writing to a const string. */
6862 if (*p != SPECIAL_VAR_SYMBOL)
6863 *p = SPECIAL_VAR_SYMBOL;
6865 } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */
6868 /* handle trailing string */
6869 if (output->ended_in_ifs) {
6870 o_addchr(output, '\0');
6871 n = o_save_ptr(output, n);
6873 debug_print_list("expand_vars_to_list[a]", output, n);
6874 /* this part is literal, and it was already pre-quoted
6875 * if needed (much earlier), do not use o_addQstr here!
6877 o_addstr(output, arg);
6878 debug_print_list("expand_vars_to_list[b]", output, n);
6880 if (output->length == o_get_last_ptr(output, n) /* expansion is empty */
6881 && !(cant_be_null & 0x80) /* and all vars were not quoted */
6882 && !output->has_quoted_part
6885 /* allow to reuse list[n] later without re-growth */
6886 output->has_empty_slot = 1;
6892 static char **expand_variables(char **argv, unsigned expflags)
6896 o_string output = NULL_O_STRING;
6898 output.o_expflags = expflags;
6902 /* go to next list[n] */
6903 output.ended_in_ifs = 0;
6904 n = o_save_ptr(&output, n);
6909 /* expand argv[i] */
6910 n = expand_vars_to_list(&output, n, *argv++);
6911 /* if (!output->has_empty_slot) -- need this?? */
6912 o_addchr(&output, '\0');
6914 debug_print_list("expand_variables", &output, n);
6916 /* output.data (malloced in one block) gets returned in "list" */
6917 list = o_finalize_list(&output, n);
6918 debug_print_strings("expand_variables[1]", list);
6922 static char **expand_strvec_to_strvec(char **argv)
6924 return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS);
6927 #if defined(CMD_SINGLEWORD_NOGLOB)
6928 static char **expand_strvec_to_strvec_singleword_noglob(char **argv)
6930 return expand_variables(argv, EXP_FLAG_SINGLEWORD);
6934 /* Used for expansion of right hand of assignments,
6935 * $((...)), heredocs, variable expansion parts.
6937 * NB: should NOT do globbing!
6938 * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*"
6940 static char *expand_string_to_string(const char *str, int EXP_flags, int do_unbackslash)
6942 #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE
6943 const int do_unbackslash = 1;
6944 const int EXP_flags = EXP_FLAG_ESC_GLOB_CHARS;
6946 char *argv[2], **list;
6948 debug_printf_expand("string_to_string<='%s'\n", str);
6949 /* This is generally an optimization, but it also
6950 * handles "", which otherwise trips over !list[0] check below.
6951 * (is this ever happens that we actually get str="" here?)
6953 if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) {
6954 //TODO: Can use on strings with \ too, just unbackslash() them?
6955 debug_printf_expand("string_to_string(fast)=>'%s'\n", str);
6956 return xstrdup(str);
6959 argv[0] = (char*)str;
6961 list = expand_variables(argv, EXP_flags | EXP_FLAG_SINGLEWORD);
6963 /* Example where it happens:
6964 * x=; echo ${x:-"$@"}
6966 ((char*)list)[0] = '\0';
6970 bb_simple_error_msg_and_die("BUG in varexp2");
6971 /* actually, just move string 2*sizeof(char*) bytes back */
6972 overlapping_strcpy((char*)list, list[0]);
6974 unbackslash((char*)list);
6976 debug_printf_expand("string_to_string=>'%s'\n", (char*)list);
6981 static char* expand_strvec_to_string(char **argv)
6985 list = expand_variables(argv, EXP_FLAG_SINGLEWORD);
6986 /* Convert all NULs to spaces */
6991 if (list[n-1] + strlen(list[n-1]) + 1 != list[n])
6992 bb_error_msg_and_die("BUG in varexp3");
6993 /* bash uses ' ' regardless of $IFS contents */
6998 overlapping_strcpy((char*)list, list[0] ? list[0] : "");
6999 debug_printf_expand("strvec_to_string='%s'\n", (char*)list);
7004 static char **expand_assignments(char **argv, int count)
7009 G.expanded_assignments = p = NULL;
7010 /* Expand assignments into one string each */
7011 for (i = 0; i < count; i++) {
7012 p = add_string_to_strings(p,
7013 expand_string_to_string(argv[i],
7014 EXP_FLAG_ESC_GLOB_CHARS,
7018 G.expanded_assignments = p;
7020 G.expanded_assignments = NULL;
7025 static void switch_off_special_sigs(unsigned mask)
7028 while ((mask >>= 1) != 0) {
7032 #if ENABLE_HUSH_TRAP
7034 if (G_traps[sig] && !G_traps[sig][0])
7035 /* trap is '', has to remain SIG_IGN */
7038 G_traps[sig] = NULL;
7041 /* We are here only if no trap or trap was not '' */
7042 install_sighandler(sig, SIG_DFL);
7048 void re_execute_shell(char ***to_free, const char *s,
7049 char *g_argv0, char **g_argv,
7050 char **builtin_argv) NORETURN;
7052 static void reset_traps_to_defaults(void)
7054 /* This function is always called in a child shell
7055 * after fork (not vfork, NOMMU doesn't use this function).
7057 IF_HUSH_TRAP(unsigned sig;)
7060 /* Child shells are not interactive.
7061 * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling.
7062 * Testcase: (while :; do :; done) + ^Z should background.
7063 * Same goes for SIGTERM, SIGHUP, SIGINT.
7065 mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask;
7066 if (!G_traps && !mask)
7067 return; /* already no traps and no special sigs */
7069 /* Switch off special sigs */
7070 switch_off_special_sigs(mask);
7071 # if ENABLE_HUSH_JOB
7072 G_fatal_sig_mask = 0;
7074 G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS;
7075 /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS
7076 * remain set in G.special_sig_mask */
7078 # if ENABLE_HUSH_TRAP
7082 /* Reset all sigs to default except ones with empty traps */
7083 for (sig = 0; sig < NSIG; sig++) {
7085 continue; /* no trap: nothing to do */
7086 if (!G_traps[sig][0])
7087 continue; /* empty trap: has to remain SIG_IGN */
7088 /* sig has non-empty trap, reset it: */
7090 G_traps[sig] = NULL;
7091 /* There is no signal for trap 0 (EXIT) */
7094 install_sighandler(sig, pick_sighandler(sig));
7101 static void re_execute_shell(char ***to_free, const char *s,
7102 char *g_argv0, char **g_argv,
7103 char **builtin_argv) NORETURN;
7104 static void re_execute_shell(char ***to_free, const char *s,
7105 char *g_argv0, char **g_argv,
7106 char **builtin_argv)
7108 # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x"))
7109 /* delims + 2 * (number of bytes in printed hex numbers) */
7110 char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)];
7111 char *heredoc_argv[4];
7112 struct variable *cur;
7113 # if ENABLE_HUSH_FUNCTIONS
7114 struct function *funcp;
7118 unsigned long long empty_trap_mask;
7120 if (!g_argv0) { /* heredoc */
7121 argv = heredoc_argv;
7122 argv[0] = (char *) G.argv0_for_re_execing;
7123 argv[1] = (char *) "-<";
7124 argv[2] = (char *) s;
7126 pp = &argv[3]; /* used as pointer to empty environment */
7132 if (pp) while (*pp++)
7135 empty_trap_mask = 0;
7138 for (sig = 1; sig < NSIG; sig++) {
7139 if (G_traps[sig] && !G_traps[sig][0])
7140 empty_trap_mask |= 1LL << sig;
7144 sprintf(param_buf, NOMMU_HACK_FMT
7145 , (unsigned) G.root_pid
7146 , (unsigned) G.root_ppid
7147 , (unsigned) G.last_bg_pid
7148 , (unsigned) G.last_exitcode
7151 IF_HUSH_LOOPS(, G.depth_of_loop)
7153 # undef NOMMU_HACK_FMT
7154 /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...>
7155 * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL
7158 for (cur = G.top_var; cur; cur = cur->next) {
7159 if (!cur->flg_export || cur->flg_read_only)
7162 # if ENABLE_HUSH_FUNCTIONS
7163 for (funcp = G.top_func; funcp; funcp = funcp->next)
7169 *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt);
7170 *pp++ = (char *) G.argv0_for_re_execing;
7172 for (cur = G.top_var; cur; cur = cur->next) {
7173 if (strcmp(cur->varstr, hush_version_str) == 0)
7175 if (cur->flg_read_only) {
7176 *pp++ = (char *) "-R";
7177 *pp++ = cur->varstr;
7178 } else if (!cur->flg_export) {
7179 *pp++ = (char *) "-V";
7180 *pp++ = cur->varstr;
7183 # if ENABLE_HUSH_FUNCTIONS
7184 for (funcp = G.top_func; funcp; funcp = funcp->next) {
7185 *pp++ = (char *) "-F";
7186 *pp++ = funcp->name;
7187 *pp++ = funcp->body_as_string;
7190 /* We can pass activated traps here. Say, -Tnn:trap_string
7192 * However, POSIX says that subshells reset signals with traps
7194 * I tested bash-3.2 and it not only does that with true subshells
7195 * of the form ( list ), but with any forked children shells.
7196 * I set trap "echo W" WINCH; and then tried:
7198 * { echo 1; sleep 20; echo 2; } &
7199 * while true; do echo 1; sleep 20; echo 2; break; done &
7200 * true | { echo 1; sleep 20; echo 2; } | cat
7202 * In all these cases sending SIGWINCH to the child shell
7203 * did not run the trap. If I add trap "echo V" WINCH;
7204 * _inside_ group (just before echo 1), it works.
7206 * I conclude it means we don't need to pass active traps here.
7208 *pp++ = (char *) "-c";
7211 while (*++builtin_argv)
7212 *pp++ = *builtin_argv;
7213 *pp++ = (char *) "";
7218 /* *pp = NULL; - is already there */
7222 debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s);
7223 /* Don't propagate SIG_IGN to the child */
7224 if (SPECIAL_JOBSTOP_SIGS != 0)
7225 switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS);
7226 execve(bb_busybox_exec_path, argv, pp);
7227 /* Fallback. Useful for init=/bin/hush usage etc */
7228 if (argv[0][0] == '/')
7229 execve(argv[0], argv, pp);
7230 xfunc_error_retval = 127;
7231 bb_simple_error_msg_and_die("can't re-execute the shell");
7233 #endif /* !BB_MMU */
7236 static int run_and_free_list(struct pipe *pi);
7238 /* Executing from string: eval, sh -c '...'
7239 * or from file: /etc/profile, . file, sh <script>, sh (intereactive)
7240 * end_trigger controls how often we stop parsing
7241 * NUL: parse all, execute, return
7242 * ';': parse till ';' or newline, execute, repeat till EOF
7244 static void parse_and_run_stream(struct in_str *inp, int end_trigger)
7246 /* Why we need empty flag?
7247 * An obscure corner case "false; ``; echo $?":
7248 * empty command in `` should still set $? to 0.
7249 * But we can't just set $? to 0 at the start,
7250 * this breaks "false; echo `echo $?`" case.
7254 struct pipe *pipe_list;
7256 #if ENABLE_HUSH_INTERACTIVE
7257 if (end_trigger == ';') {
7258 G.promptmode = 0; /* PS1 */
7259 debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode);
7262 pipe_list = parse_stream(NULL, NULL, inp, end_trigger);
7263 if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */
7264 /* If we are in "big" script
7265 * (not in `cmd` or something similar)...
7267 if (pipe_list == ERR_PTR && end_trigger == ';') {
7268 /* Discard cached input (rest of line) */
7269 int ch = inp->last_char;
7270 while (ch != EOF && ch != '\n') {
7271 //bb_error_msg("Discarded:'%c'", ch);
7276 /* This stream isn't empty */
7280 if (!pipe_list && empty)
7281 G.last_exitcode = 0;
7284 debug_print_tree(pipe_list, 0);
7285 debug_printf_exec("parse_and_run_stream: run_and_free_list\n");
7286 run_and_free_list(pipe_list);
7288 if (G_flag_return_in_progress == 1)
7293 static void parse_and_run_string(const char *s)
7295 struct in_str input;
7296 //IF_HUSH_LINENO_VAR(unsigned sv = G.parse_lineno;)
7298 setup_string_in_str(&input, s);
7299 parse_and_run_stream(&input, '\0');
7300 //IF_HUSH_LINENO_VAR(G.parse_lineno = sv;)
7303 static void parse_and_run_file(HFILE *fp)
7305 struct in_str input;
7306 IF_HUSH_LINENO_VAR(unsigned sv = G.parse_lineno;)
7308 IF_HUSH_LINENO_VAR(G.parse_lineno = 1;)
7309 setup_file_in_str(&input, fp);
7310 parse_and_run_stream(&input, ';');
7311 IF_HUSH_LINENO_VAR(G.parse_lineno = sv;)
7314 #if ENABLE_HUSH_TICK
7315 static int generate_stream_from_string(const char *s, pid_t *pid_p)
7320 char **to_free = NULL;
7324 pid = BB_MMU ? xfork() : xvfork();
7325 if (pid == 0) { /* child */
7326 disable_restore_tty_pgrp_on_exit();
7327 /* Process substitution is not considered to be usual
7328 * 'command execution'.
7329 * SUSv3 says ctrl-Z should be ignored, ctrl-C should not.
7336 close(channel[0]); /* NB: close _first_, then move fd! */
7337 xmove_fd(channel[1], 1);
7338 # if ENABLE_HUSH_TRAP
7339 /* Awful hack for `trap` or $(trap).
7341 * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html
7342 * contains an example where "trap" is executed in a subshell:
7344 * save_traps=$(trap)
7346 * eval "$save_traps"
7348 * Standard does not say that "trap" in subshell shall print
7349 * parent shell's traps. It only says that its output
7350 * must have suitable form, but then, in the above example
7351 * (which is not supposed to be normative), it implies that.
7353 * bash (and probably other shell) does implement it
7354 * (traps are reset to defaults, but "trap" still shows them),
7355 * but as a result, "trap" logic is hopelessly messed up:
7358 * trap -- 'echo Ho' SIGWINCH <--- we have a handler
7359 * # (trap) <--- trap is in subshell - no output (correct, traps are reset)
7360 * # true | trap <--- trap is in subshell - no output (ditto)
7361 * # echo `true | trap` <--- in subshell - output (but traps are reset!)
7362 * trap -- 'echo Ho' SIGWINCH
7363 * # echo `(trap)` <--- in subshell in subshell - output
7364 * trap -- 'echo Ho' SIGWINCH
7365 * # echo `true | (trap)` <--- in subshell in subshell in subshell - output!
7366 * trap -- 'echo Ho' SIGWINCH
7368 * The rules when to forget and when to not forget traps
7369 * get really complex and nonsensical.
7371 * Our solution: ONLY bare $(trap) or `trap` is special.
7373 s = skip_whitespace(s);
7374 if (is_prefixed_with(s, "trap")
7375 && skip_whitespace(s + 4)[0] == '\0'
7377 static const char *const argv[] = { NULL, NULL };
7378 builtin_trap((char**)argv);
7379 fflush_all(); /* important */
7384 /* Prevent it from trying to handle ctrl-z etc */
7385 IF_HUSH_JOB(G.run_list_level = 1;)
7386 CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */
7387 reset_traps_to_defaults();
7388 IF_HUSH_MODE_X(G.x_mode_depth++;)
7389 //bb_error_msg("%s: ++x_mode_depth=%d", __func__, G.x_mode_depth);
7390 parse_and_run_string(s);
7391 _exit(G.last_exitcode);
7393 /* We re-execute after vfork on NOMMU. This makes this script safe:
7394 * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG
7395 * huge=`cat BIG` # was blocking here forever
7398 re_execute_shell(&to_free,
7408 # if ENABLE_HUSH_FAST
7410 //bb_error_msg("[%d] fork in generate_stream_from_string:"
7411 // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d",
7412 // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
7414 enable_restore_tty_pgrp_on_exit();
7422 /* Return code is exit status of the process that is run. */
7423 static int process_command_subs(o_string *dest, const char *s)
7427 int status, ch, eol_cnt;
7429 fp = xfdopen_for_read(generate_stream_from_string(s, &pid));
7431 /* Now send results of command back into original context */
7433 while ((ch = getc(fp)) != EOF) {
7441 o_addchr(dest, '\n');
7444 o_addQchr(dest, ch);
7447 debug_printf("done reading from `cmd` pipe, closing it\n");
7449 /* We need to extract exitcode. Test case
7450 * "true; echo `sleep 1; false` $?"
7452 safe_waitpid(pid, &status, 0);
7453 debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status));
7454 return WEXITSTATUS(status);
7456 #endif /* ENABLE_HUSH_TICK */
7459 static void setup_heredoc(struct redir_struct *redir)
7461 struct fd_pair pair;
7464 /* the _body_ of heredoc (misleading field name) */
7465 const char *heredoc = redir->rd_filename;
7472 if (!(redir->rd_dup & HEREDOC_QUOTED)) {
7473 expanded = encode_then_expand_string(heredoc);
7477 len = strlen(heredoc);
7479 close(redir->rd_fd); /* often saves dup2+close in xmove_fd */
7481 xmove_fd(pair.rd, redir->rd_fd);
7483 /* Try writing without forking. Newer kernels have
7484 * dynamically growing pipes. Must use non-blocking write! */
7487 written = write(pair.wr, heredoc, len);
7498 ndelay_off(pair.wr);
7500 /* Okay, pipe buffer was not big enough */
7501 /* Note: we must not create a stray child (bastard? :)
7502 * for the unsuspecting parent process. Child creates a grandchild
7503 * and exits before parent execs the process which consumes heredoc
7504 * (that exec happens after we return from this function) */
7511 disable_restore_tty_pgrp_on_exit();
7512 pid = BB_MMU ? xfork() : xvfork();
7516 close(redir->rd_fd); /* read side of the pipe */
7518 full_write(pair.wr, heredoc, len); /* may loop or block */
7521 /* Delegate blocking writes to another process */
7522 xmove_fd(pair.wr, STDOUT_FILENO);
7523 re_execute_shell(&to_free, heredoc, NULL, NULL, NULL);
7527 #if ENABLE_HUSH_FAST
7529 //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
7531 enable_restore_tty_pgrp_on_exit();
7537 wait(NULL); /* wait till child has died */
7543 /* moved_to = n: fd was moved to n; restore back to orig_fd after redir */
7544 /* moved_to = -1: fd was opened by redirect; close orig_fd after redir */
7547 static struct squirrel *append_squirrel(struct squirrel *sq, int i, int orig, int moved)
7549 sq = xrealloc(sq, (i + 2) * sizeof(sq[0]));
7550 sq[i].orig_fd = orig;
7551 sq[i].moved_to = moved;
7552 sq[i+1].orig_fd = -1; /* end marker */
7556 static struct squirrel *add_squirrel(struct squirrel *sq, int fd, int avoid_fd)
7562 if (sq) for (; sq[i].orig_fd >= 0; i++) {
7563 /* If we collide with an already moved fd... */
7564 if (fd == sq[i].moved_to) {
7565 sq[i].moved_to = dup_CLOEXEC(sq[i].moved_to, avoid_fd);
7566 debug_printf_redir("redirect_fd %d: already busy, moving to %d\n", fd, sq[i].moved_to);
7567 if (sq[i].moved_to < 0) /* what? */
7571 if (fd == sq[i].orig_fd) {
7572 /* Example: echo Hello >/dev/null 1>&2 */
7573 debug_printf_redir("redirect_fd %d: already moved\n", fd);
7578 /* If this fd is open, we move and remember it; if it's closed, moved_to = -1 */
7579 moved_to = dup_CLOEXEC(fd, avoid_fd);
7580 debug_printf_redir("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, moved_to);
7581 if (moved_to < 0 && errno != EBADF)
7583 return append_squirrel(sq, i, fd, moved_to);
7586 static struct squirrel *add_squirrel_closed(struct squirrel *sq, int fd)
7591 if (sq) for (; sq[i].orig_fd >= 0; i++) {
7592 /* If we collide with an already moved fd... */
7593 if (fd == sq[i].orig_fd) {
7595 * "echo 3>FILE 3>&- 3>FILE"
7596 * "echo 3>&- 3>FILE"
7597 * No need for last redirect to insert
7598 * another "need to close 3" indicator.
7600 debug_printf_redir("redirect_fd %d: already moved or closed\n", fd);
7605 debug_printf_redir("redirect_fd %d: previous fd was closed\n", fd);
7606 return append_squirrel(sq, i, fd, -1);
7609 /* fd: redirect wants this fd to be used (e.g. 3>file).
7610 * Move all conflicting internally used fds,
7611 * and remember them so that we can restore them later.
7613 static int save_fd_on_redirect(int fd, int avoid_fd, struct squirrel **sqp)
7615 if (avoid_fd < 9) /* the important case here is that it can be -1 */
7618 #if ENABLE_HUSH_INTERACTIVE
7619 if (fd != 0 /* don't trigger for G_interactive_fd == 0 (that's "not interactive" flag) */
7620 && fd == G_interactive_fd
7622 /* Testcase: "ls -l /proc/$$/fd 255>&-" should work */
7623 G_interactive_fd = xdup_CLOEXEC_and_close(G_interactive_fd, avoid_fd);
7624 debug_printf_redir("redirect_fd %d: matches interactive_fd, moving it to %d\n", fd, G_interactive_fd);
7625 return 1; /* "we closed fd" */
7628 /* Are we called from setup_redirects(squirrel==NULL)
7629 * in redirect in a [v]forked child?
7632 /* No need to move script fds.
7633 * For NOMMU case, it's actively wrong: we'd change ->fd
7634 * fields in memory for the parent, but parent's fds
7635 * aren't moved, it would use wrong fd!
7636 * Reproducer: "cmd 3>FILE" in script.
7637 * If we would call move_HFILEs_on_redirect(), child would:
7638 * fcntl64(3, F_DUPFD_CLOEXEC, 10) = 10
7640 * and change ->fd to 10 if fd#3 is a script fd. WRONG.
7642 //bb_error_msg("sqp == NULL: [v]forked child");
7646 /* If this one of script's fds? */
7647 if (move_HFILEs_on_redirect(fd, avoid_fd))
7648 return 1; /* yes. "we closed fd" (actually moved it) */
7650 /* Are we called for "exec 3>FILE"? Came through
7651 * redirect_and_varexp_helper(squirrel=ERR_PTR) -> setup_redirects(ERR_PTR)
7652 * This case used to fail for this script:
7655 * ...100000 more lines...
7658 * read(3, "exec 3>FILE\necho Ok\necho Ok"..., 1024) = 1024
7659 * open("FILE", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 4
7661 * ^^^^^^^^ oops, we lost fd#3 opened to our script!
7663 * write(1, "Ok\n", 3) = 3
7665 * write(1, "Ok\n", 3) = 3
7666 * read(3, 0x94fbc08, 1024) = -1 EBADF (Bad file descriptor)
7667 * ^^^^^^^^ oops, wrong fd!!!
7668 * With this case separate from sqp == NULL and *after* move_HFILEs,
7671 if (sqp == ERR_PTR) {
7672 /* Don't preserve redirected fds: exec is _meant_ to change these */
7673 //bb_error_msg("sqp == ERR_PTR: exec >FILE");
7677 /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */
7678 *sqp = add_squirrel(*sqp, fd, avoid_fd);
7679 return 0; /* "we did not close fd" */
7682 static void restore_redirects(struct squirrel *sq)
7686 for (i = 0; sq[i].orig_fd >= 0; i++) {
7687 if (sq[i].moved_to >= 0) {
7688 /* We simply die on error */
7689 debug_printf_redir("restoring redirected fd from %d to %d\n", sq[i].moved_to, sq[i].orig_fd);
7690 xmove_fd(sq[i].moved_to, sq[i].orig_fd);
7692 /* cmd1 9>FILE; cmd2_should_see_fd9_closed */
7693 debug_printf_redir("restoring redirected fd %d: closing it\n", sq[i].orig_fd);
7694 close(sq[i].orig_fd);
7700 && G.HFILE_stdin->fd != STDIN_FILENO
7702 /* Testcase: interactive "read r <FILE; echo $r; read r; echo $r".
7703 * Redirect moves ->fd to e.g. 10,
7704 * and it is not restored above (we do not restore script fds
7705 * after redirects, we just use new, "moved" fds).
7706 * However for stdin, get_user_input() -> read_line_input(),
7707 * and read builtin, depend on fd == STDIN_FILENO.
7709 debug_printf_redir("restoring %d to stdin\n", G.HFILE_stdin->fd);
7710 xmove_fd(G.HFILE_stdin->fd, STDIN_FILENO);
7711 G.HFILE_stdin->fd = STDIN_FILENO;
7714 /* If moved, G_interactive_fd stays on new fd, not restoring it */
7717 #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU
7718 static void close_saved_fds_and_FILE_fds(void)
7720 if (G_interactive_fd)
7721 close(G_interactive_fd);
7722 close_all_HFILE_list();
7726 static int internally_opened_fd(int fd, struct squirrel *sq)
7730 #if ENABLE_HUSH_INTERACTIVE
7731 if (fd == G_interactive_fd)
7734 /* If this one of script's fds? */
7735 if (fd_in_HFILEs(fd))
7738 if (sq) for (i = 0; sq[i].orig_fd >= 0; i++) {
7739 if (fd == sq[i].moved_to)
7745 /* squirrel != NULL means we squirrel away copies of stdin, stdout,
7746 * and stderr if they are redirected. */
7747 static int setup_redirects(struct command *prog, struct squirrel **sqp)
7749 struct redir_struct *redir;
7751 for (redir = prog->redirects; redir; redir = redir->next) {
7755 if (redir->rd_type == REDIRECT_HEREDOC2) {
7756 /* "rd_fd<<HERE" case */
7757 save_fd_on_redirect(redir->rd_fd, /*avoid:*/ 0, sqp);
7758 /* for REDIRECT_HEREDOC2, rd_filename holds _contents_
7760 debug_printf_redir("set heredoc '%s'\n",
7761 redir->rd_filename);
7762 setup_heredoc(redir);
7766 if (redir->rd_dup == REDIRFD_TO_FILE) {
7767 /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */
7771 if (redir->rd_filename == NULL) {
7773 * "cmd >" (no filename)
7774 * "cmd > <file" (2nd redirect starts too early)
7776 syntax_error("invalid redirect");
7779 mode = redir_table[redir->rd_type].mode;
7780 p = expand_string_to_string(redir->rd_filename,
7781 EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1);
7782 newfd = open_or_warn(p, mode);
7785 /* Error message from open_or_warn can be lost
7786 * if stderr has been redirected, but bash
7787 * and ash both lose it as well
7788 * (though zsh doesn't!)
7792 if (newfd == redir->rd_fd && sqp) {
7793 /* open() gave us precisely the fd we wanted.
7794 * This means that this fd was not busy
7795 * (not opened to anywhere).
7796 * Remember to close it on restore:
7798 *sqp = add_squirrel_closed(*sqp, newfd);
7799 debug_printf_redir("redir to previously closed fd %d\n", newfd);
7802 /* "rd_fd>&rd_dup" or "rd_fd>&-" case */
7803 newfd = redir->rd_dup;
7806 if (newfd == redir->rd_fd)
7809 /* if "N>FILE": move newfd to redir->rd_fd */
7810 /* if "N>&M": dup newfd to redir->rd_fd */
7811 /* if "N>&-": close redir->rd_fd (newfd is REDIRFD_CLOSE) */
7813 closed = save_fd_on_redirect(redir->rd_fd, /*avoid:*/ newfd, sqp);
7814 if (newfd == REDIRFD_CLOSE) {
7815 /* "N>&-" means "close me" */
7817 /* ^^^ optimization: saving may already
7818 * have closed it. If not... */
7819 close(redir->rd_fd);
7821 /* Sometimes we do another close on restore, getting EBADF.
7822 * Consider "echo 3>FILE 3>&-"
7823 * first redirect remembers "need to close 3",
7824 * and second redirect closes 3! Restore code then closes 3 again.
7827 /* if newfd is a script fd or saved fd, simulate EBADF */
7828 if (internally_opened_fd(newfd, sqp && sqp != ERR_PTR ? *sqp : NULL)) {
7830 //bb_perror_msg_and_die("can't duplicate file descriptor");
7831 newfd = -1; /* same effect as code above */
7833 xdup2(newfd, redir->rd_fd);
7834 if (redir->rd_dup == REDIRFD_TO_FILE)
7835 /* "rd_fd > FILE" */
7837 /* else: "rd_fd > rd_dup" */
7843 static char *find_in_path(const char *arg)
7846 const char *PATH = get_local_var_value("PATH");
7852 const char *end = strchrnul(PATH, ':');
7853 int sz = end - PATH; /* must be int! */
7857 ret = xasprintf("%.*s/%s", sz, PATH, arg);
7859 /* We have xxx::yyyy in $PATH,
7860 * it means "use current dir" */
7863 if (access(ret, F_OK) == 0)
7876 static const struct built_in_command *find_builtin_helper(const char *name,
7877 const struct built_in_command *x,
7878 const struct built_in_command *end)
7881 if (strcmp(name, x->b_cmd) != 0) {
7885 debug_printf_exec("found builtin '%s'\n", name);
7890 static const struct built_in_command *find_builtin1(const char *name)
7892 return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]);
7894 static const struct built_in_command *find_builtin(const char *name)
7896 const struct built_in_command *x = find_builtin1(name);
7899 return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]);
7902 #if EDITING_HAS_get_exe_name
7903 static const char * FAST_FUNC get_builtin_name(int i)
7905 if (/*i >= 0 && */ i < ARRAY_SIZE(bltins1)) {
7906 return bltins1[i].b_cmd;
7908 i -= ARRAY_SIZE(bltins1);
7909 if (i < ARRAY_SIZE(bltins2)) {
7910 return bltins2[i].b_cmd;
7916 static void remove_nested_vars(void)
7918 struct variable *cur;
7919 struct variable **cur_pp;
7921 cur_pp = &G.top_var;
7922 while ((cur = *cur_pp) != NULL) {
7923 if (cur->var_nest_level <= G.var_nest_level) {
7924 cur_pp = &cur->next;
7928 if (cur->flg_export) {
7929 debug_printf_env("unexporting nested '%s'/%u\n", cur->varstr, cur->var_nest_level);
7930 bb_unsetenv(cur->varstr);
7932 /* Remove from global list */
7933 *cur_pp = cur->next;
7935 if (!cur->max_len) {
7936 debug_printf_env("freeing nested '%s'/%u\n", cur->varstr, cur->var_nest_level);
7943 static void enter_var_nest_level(void)
7946 debug_printf_env("var_nest_level++ %u\n", G.var_nest_level);
7948 /* Try: f() { echo -n .; f; }; f
7949 * struct variable::var_nest_level is uint16_t,
7950 * thus limiting recursion to < 2^16.
7951 * In any case, with 8 Mbyte stack SEGV happens
7952 * not too long after 2^16 recursions anyway.
7954 if (G.var_nest_level > 0xff00)
7955 bb_error_msg_and_die("fatal recursion (depth %u)", G.var_nest_level);
7958 static void leave_var_nest_level(void)
7961 debug_printf_env("var_nest_level-- %u\n", G.var_nest_level);
7962 if (HUSH_DEBUG && (int)G.var_nest_level < 0)
7963 bb_simple_error_msg_and_die("BUG: nesting underflow");
7965 remove_nested_vars();
7968 #if ENABLE_HUSH_FUNCTIONS
7969 static struct function **find_function_slot(const char *name)
7971 struct function *funcp;
7972 struct function **funcpp = &G.top_func;
7974 while ((funcp = *funcpp) != NULL) {
7975 if (strcmp(name, funcp->name) == 0) {
7976 debug_printf_exec("found function '%s'\n", name);
7979 funcpp = &funcp->next;
7984 static ALWAYS_INLINE const struct function *find_function(const char *name)
7986 const struct function *funcp = *find_function_slot(name);
7990 /* Note: takes ownership on name ptr */
7991 static struct function *new_function(char *name)
7993 struct function **funcpp = find_function_slot(name);
7994 struct function *funcp = *funcpp;
7996 if (funcp != NULL) {
7997 struct command *cmd = funcp->parent_cmd;
7998 debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd);
8000 debug_printf_exec("freeing & replacing function '%s'\n", funcp->name);
8002 /* Note: if !funcp->body, do not free body_as_string!
8003 * This is a special case of "-F name body" function:
8004 * body_as_string was not malloced! */
8006 free_pipe_list(funcp->body);
8008 free(funcp->body_as_string);
8012 debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name);
8013 cmd->argv[0] = funcp->name;
8014 cmd->group = funcp->body;
8016 cmd->group_as_string = funcp->body_as_string;
8020 debug_printf_exec("remembering new function '%s'\n", name);
8021 funcp = *funcpp = xzalloc(sizeof(*funcp));
8022 /*funcp->next = NULL;*/
8029 # if ENABLE_HUSH_UNSET
8030 static void unset_func(const char *name)
8032 struct function **funcpp = find_function_slot(name);
8033 struct function *funcp = *funcpp;
8035 if (funcp != NULL) {
8036 debug_printf_exec("freeing function '%s'\n", funcp->name);
8037 *funcpp = funcp->next;
8038 /* funcp is unlinked now, deleting it.
8039 * Note: if !funcp->body, the function was created by
8040 * "-F name body", do not free ->body_as_string
8041 * and ->name as they were not malloced. */
8043 free_pipe_list(funcp->body);
8046 free(funcp->body_as_string);
8055 #define exec_function(to_free, funcp, argv) \
8056 exec_function(funcp, argv)
8058 static void exec_function(char ***to_free,
8059 const struct function *funcp,
8060 char **argv) NORETURN;
8061 static void exec_function(char ***to_free,
8062 const struct function *funcp,
8068 argv[0] = G.global_argv[0];
8069 G.global_argv = argv;
8070 G.global_argc = n = 1 + string_array_len(argv + 1);
8072 // Example when we are here: "cmd | func"
8073 // func will run with saved-redirect fds open.
8074 // $ f() { echo /proc/self/fd/*; }
8076 // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3
8077 // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ DIR fd for glob
8080 // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 /proc/self/fd/4
8081 // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ opened ./SCRIPT DIR fd for glob
8082 // They are CLOEXEC so external programs won't see them, but
8083 // for "more correctness" we might want to close those extra fds here:
8084 //? close_saved_fds_and_FILE_fds();
8086 /* "we are in a function, ok to use return" */
8087 G_flag_return_in_progress = -1;
8088 enter_var_nest_level();
8089 IF_HUSH_LOCAL(G.func_nest_level++;)
8091 /* On MMU, funcp->body is always non-NULL */
8092 n = run_list(funcp->body);
8096 //? close_saved_fds_and_FILE_fds();
8098 //TODO: check whether "true | func_with_return" works
8100 re_execute_shell(to_free,
8101 funcp->body_as_string,
8108 static int run_function(const struct function *funcp, char **argv)
8114 save_and_replace_G_args(&sv, argv);
8116 /* "We are in function, ok to use return" */
8117 sv_flg = G_flag_return_in_progress;
8118 G_flag_return_in_progress = -1;
8120 /* Make "local" variables properly shadow previous ones */
8121 IF_HUSH_LOCAL(enter_var_nest_level();)
8122 IF_HUSH_LOCAL(G.func_nest_level++;)
8124 /* On MMU, funcp->body is always non-NULL */
8127 /* Function defined by -F */
8128 parse_and_run_string(funcp->body_as_string);
8129 rc = G.last_exitcode;
8133 rc = run_list(funcp->body);
8136 IF_HUSH_LOCAL(G.func_nest_level--;)
8137 IF_HUSH_LOCAL(leave_var_nest_level();)
8139 G_flag_return_in_progress = sv_flg;
8140 # if ENABLE_HUSH_TRAP
8141 debug_printf_exec("G.return_exitcode=-1\n");
8142 G.return_exitcode = -1; /* invalidate stashed return value */
8145 restore_G_args(&sv, argv);
8149 #endif /* ENABLE_HUSH_FUNCTIONS */
8153 #define exec_builtin(to_free, x, argv) \
8154 exec_builtin(x, argv)
8156 #define exec_builtin(to_free, x, argv) \
8157 exec_builtin(to_free, argv)
8159 static void exec_builtin(char ***to_free,
8160 const struct built_in_command *x,
8161 char **argv) NORETURN;
8162 static void exec_builtin(char ***to_free,
8163 const struct built_in_command *x,
8169 //? close_saved_fds_and_FILE_fds();
8170 rcode = x->b_function(argv);
8175 /* On NOMMU, we must never block!
8176 * Example: { sleep 99 | read line; } & echo Ok
8178 re_execute_shell(to_free,
8187 static void execvp_or_die(char **argv) NORETURN;
8188 static void execvp_or_die(char **argv)
8191 debug_printf_exec("execing '%s'\n", argv[0]);
8192 /* Don't propagate SIG_IGN to the child */
8193 if (SPECIAL_JOBSTOP_SIGS != 0)
8194 switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS);
8195 execvp(argv[0], argv);
8197 if (errno == EACCES) e = 126;
8198 if (errno == ENOENT) e = 127;
8199 bb_perror_msg("can't execute '%s'", argv[0]);
8203 #if ENABLE_HUSH_MODE_X
8204 static void x_mode_print_optionally_squoted(const char *str)
8211 /* the set of chars which-cause-string-to-be-squoted mimics bash */
8212 /* test a char with: bash -c 'set -x; echo "CH"' */
8213 if (str[strcspn(str, "\\\"'`$(){}[]<>;#&|~*?!^"
8214 " " "\001\002\003\004\005\006\007"
8215 "\010\011\012\013\014\015\016\017"
8216 "\020\021\022\023\024\025\026\027"
8217 "\030\031\032\033\034\035\036\037"
8221 /* string has no special chars */
8228 /* print '....' up to EOL or first squote */
8229 len = (int)(strchrnul(cp, '\'') - cp);
8231 x_mode_addchr('\'');
8232 x_mode_addblock(cp, len);
8233 x_mode_addchr('\'');
8238 /* string contains squote(s), print them as \' */
8239 x_mode_addchr('\\');
8240 x_mode_addchr('\'');
8244 static void dump_cmd_in_x_mode(char **argv)
8246 if (G_x_mode && argv) {
8249 /* "+[+++...][ cmd...]\n\0" */
8254 if (argv[n][0] == '\0') {
8255 x_mode_addchr('\'');
8256 x_mode_addchr('\'');
8258 x_mode_print_optionally_squoted(argv[n]);
8266 # define dump_cmd_in_x_mode(argv) ((void)0)
8269 #if ENABLE_HUSH_COMMAND
8270 static void if_command_vV_print_and_exit(char opt_vV, char *cmd, const char *explanation)
8279 char *path = getenv("PATH");
8280 explanation = to_free = find_executable(cmd, &path); /* path == NULL is ok */
8282 _exit(1); /* PROG was not found */
8284 cmd = to_free; /* -v PROG prints "/path/to/PROG" */
8286 printf((opt_vV == 'V') ? "%s is %s\n" : "%s\n", cmd, explanation);
8292 # define if_command_vV_print_and_exit(a,b,c) ((void)0)
8296 #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \
8297 pseudo_exec_argv(argv, assignment_cnt, argv_expanded)
8298 #define pseudo_exec(nommu_save, command, argv_expanded) \
8299 pseudo_exec(command, argv_expanded)
8302 /* Called after [v]fork() in run_pipe, or from builtin_exec.
8304 * Don't exit() here. If you don't exec, use _exit instead.
8305 * The at_exit handlers apparently confuse the calling process,
8306 * in particular stdin handling. Not sure why? -- because of vfork! (vda)
8308 static void pseudo_exec_argv(nommu_save_t *nommu_save,
8309 char **argv, int assignment_cnt,
8310 char **argv_expanded) NORETURN;
8311 static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save,
8312 char **argv, int assignment_cnt,
8313 char **argv_expanded)
8315 const struct built_in_command *x;
8316 struct variable **sv_shadowed;
8318 IF_HUSH_COMMAND(char opt_vV = 0;)
8319 IF_HUSH_FUNCTIONS(const struct function *funcp;)
8321 new_env = expand_assignments(argv, assignment_cnt);
8322 dump_cmd_in_x_mode(new_env);
8324 if (!argv[assignment_cnt]) {
8325 /* Case when we are here: ... | var=val | ...
8326 * (note that we do not exit early, i.e., do not optimize out
8327 * expand_assignments(): think about ... | var=`sleep 1` | ...
8329 free_strings(new_env);
8330 _exit(EXIT_SUCCESS);
8333 sv_shadowed = G.shadowed_vars_pp;
8335 G.shadowed_vars_pp = NULL; /* "don't save, free them instead" */
8337 G.shadowed_vars_pp = &nommu_save->old_vars;
8340 set_vars_and_save_old(new_env);
8341 G.shadowed_vars_pp = sv_shadowed;
8343 if (argv_expanded) {
8344 argv = argv_expanded;
8346 argv = expand_strvec_to_strvec(argv + assignment_cnt);
8348 nommu_save->argv = argv;
8351 dump_cmd_in_x_mode(argv);
8353 #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU
8354 if (strchr(argv[0], '/') != NULL)
8358 #if ENABLE_HUSH_FUNCTIONS
8359 /* Check if the command matches any functions (this goes before bltins) */
8360 funcp = find_function(argv[0]);
8362 exec_function(&nommu_save->argv_from_re_execing, funcp, argv);
8365 #if ENABLE_HUSH_COMMAND
8366 /* "command BAR": run BAR without looking it up among functions
8367 * "command -v BAR": print "BAR" or "/path/to/BAR"; or exit 1
8368 * "command -V BAR": print "BAR is {a function,a shell builtin,/path/to/BAR}"
8370 while (strcmp(argv[0], "command") == 0 && argv[1]) {
8375 if (p[0] != '-' || !p[1])
8376 continue; /* bash allows "command command command [-OPT] BAR" */
8384 if (p[0] != '-' || !p[1])
8386 continue; /* next arg is also -opts, process it too */
8392 bb_error_msg_and_die("%s: %s: invalid option", "command", argv[0]);
8397 # if ENABLE_HUSH_FUNCTIONS
8398 if (opt_vV && find_function(argv[0]))
8399 if_command_vV_print_and_exit(opt_vV, argv[0], "a function");
8403 /* Check if the command matches any of the builtins.
8404 * Depending on context, this might be redundant. But it's
8405 * easier to waste a few CPU cycles than it is to figure out
8406 * if this is one of those cases.
8408 /* Why "BB_MMU ? :" difference in logic? -
8409 * On NOMMU, it is more expensive to re-execute shell
8410 * just in order to run echo or test builtin.
8411 * It's better to skip it here and run corresponding
8412 * non-builtin later. */
8413 x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]);
8415 if_command_vV_print_and_exit(opt_vV, argv[0], "a shell builtin");
8416 exec_builtin(&nommu_save->argv_from_re_execing, x, argv);
8419 #if ENABLE_FEATURE_SH_STANDALONE
8420 /* Check if the command matches any busybox applets */
8422 int a = find_applet_by_name(argv[0]);
8424 if_command_vV_print_and_exit(opt_vV, argv[0], "an applet");
8425 # if BB_MMU /* see above why on NOMMU it is not allowed */
8426 if (APPLET_IS_NOEXEC(a)) {
8427 /* Do not leak open fds from opened script files etc.
8428 * Testcase: interactive "ls -l /proc/self/fd"
8429 * should not show tty fd open.
8431 close_saved_fds_and_FILE_fds();
8432 //FIXME: should also close saved redir fds
8433 //This casuses test failures in
8434 //redir_children_should_not_see_saved_fd_2.tests
8435 //redir_children_should_not_see_saved_fd_3.tests
8436 //if you replace "busybox find" with just "find" in them
8437 /* Without this, "rm -i FILE" can't be ^C'ed: */
8438 switch_off_special_sigs(G.special_sig_mask);
8439 debug_printf_exec("running applet '%s'\n", argv[0]);
8440 run_noexec_applet_and_exit(a, argv[0], argv);
8443 /* Re-exec ourselves */
8444 debug_printf_exec("re-execing applet '%s'\n", argv[0]);
8445 /* Don't propagate SIG_IGN to the child */
8446 if (SPECIAL_JOBSTOP_SIGS != 0)
8447 switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS);
8448 execv(bb_busybox_exec_path, argv);
8449 /* If they called chroot or otherwise made the binary no longer
8450 * executable, fall through */
8455 #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU
8458 if_command_vV_print_and_exit(opt_vV, argv[0], NULL);
8459 execvp_or_die(argv);
8462 /* Called after [v]fork() in run_pipe
8464 static void pseudo_exec(nommu_save_t *nommu_save,
8465 struct command *command,
8466 char **argv_expanded) NORETURN;
8467 static void pseudo_exec(nommu_save_t *nommu_save,
8468 struct command *command,
8469 char **argv_expanded)
8471 #if ENABLE_HUSH_FUNCTIONS
8472 if (command->cmd_type == CMD_FUNCDEF) {
8473 /* Ignore funcdefs in pipes:
8474 * true | f() { cmd }
8480 if (command->argv) {
8481 pseudo_exec_argv(nommu_save, command->argv,
8482 command->assignment_cnt, argv_expanded);
8485 if (command->group) {
8486 /* Cases when we are here:
8489 * ... | ( list ) | ...
8490 * ... | { list } | ...
8494 debug_printf_exec("pseudo_exec: run_list\n");
8495 reset_traps_to_defaults();
8496 rcode = run_list(command->group);
8497 /* OK to leak memory by not calling free_pipe_list,
8498 * since this process is about to exit */
8501 re_execute_shell(&nommu_save->argv_from_re_execing,
8502 command->group_as_string,
8509 /* Case when we are here: ... | >file */
8510 debug_printf_exec("pseudo_exec'ed null command\n");
8511 _exit(EXIT_SUCCESS);
8515 static const char *get_cmdtext(struct pipe *pi)
8521 /* This is subtle. ->cmdtext is created only on first backgrounding.
8522 * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...)
8523 * On subsequent bg argv is trashed, but we won't use it */
8527 argv = pi->cmds[0].argv;
8529 pi->cmdtext = xzalloc(1);
8534 len += strlen(*argv) + 1;
8538 argv = pi->cmds[0].argv;
8540 p = stpcpy(p, *argv);
8547 static void remove_job_from_table(struct pipe *pi)
8549 struct pipe *prev_pipe;
8551 if (pi == G.job_list) {
8552 G.job_list = pi->next;
8554 prev_pipe = G.job_list;
8555 while (prev_pipe->next != pi)
8556 prev_pipe = prev_pipe->next;
8557 prev_pipe->next = pi->next;
8561 G.last_jobid = G.job_list->jobid;
8564 static void delete_finished_job(struct pipe *pi)
8566 remove_job_from_table(pi);
8570 static void clean_up_last_dead_job(void)
8572 if (G.job_list && !G.job_list->alive_cmds)
8573 delete_finished_job(G.job_list);
8576 static void insert_job_into_table(struct pipe *pi)
8578 struct pipe *job, **jobp;
8581 clean_up_last_dead_job();
8583 /* Find the end of the list, and find next job ID to use */
8586 while ((job = *jobp) != NULL) {
8593 /* Create a new job struct at the end */
8594 job = *jobp = xmemdup(pi, sizeof(*pi));
8596 job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds);
8597 /* Cannot copy entire pi->cmds[] vector! This causes double frees */
8598 for (i = 0; i < pi->num_cmds; i++) {
8599 job->cmds[i].pid = pi->cmds[i].pid;
8600 /* all other fields are not used and stay zero */
8602 job->cmdtext = xstrdup(get_cmdtext(pi));
8604 if (G_interactive_fd)
8605 printf("[%u] %u %s\n", job->jobid, (unsigned)job->cmds[0].pid, job->cmdtext);
8606 G.last_jobid = job->jobid;
8610 static int job_exited_or_stopped(struct pipe *pi)
8614 if (pi->alive_cmds != pi->stopped_cmds)
8617 /* All processes in fg pipe have exited or stopped */
8621 rcode = pi->cmds[i].cmd_exitcode;
8622 /* usually last process gives overall exitstatus,
8623 * but with "set -o pipefail", last *failed* process does */
8624 if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0)
8627 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
8631 static int process_wait_result(struct pipe *fg_pipe, pid_t childpid, int status)
8638 dead = WIFEXITED(status) || WIFSIGNALED(status);
8641 if (WIFSTOPPED(status))
8642 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
8643 childpid, WSTOPSIG(status), WEXITSTATUS(status));
8644 if (WIFSIGNALED(status))
8645 debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
8646 childpid, WTERMSIG(status), WEXITSTATUS(status));
8647 if (WIFEXITED(status))
8648 debug_printf_jobs("pid %d exited, exitcode %d\n",
8649 childpid, WEXITSTATUS(status));
8651 /* Were we asked to wait for a fg pipe? */
8653 i = fg_pipe->num_cmds;
8658 debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid);
8659 if (fg_pipe->cmds[i].pid != childpid)
8663 fg_pipe->cmds[i].pid = 0;
8664 fg_pipe->alive_cmds--;
8665 ex = WEXITSTATUS(status);
8666 /* bash prints killer signal's name for *last*
8667 * process in pipe (prints just newline for SIGINT/SIGPIPE).
8668 * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT)
8670 if (WIFSIGNALED(status)) {
8671 int sig = WTERMSIG(status);
8672 if (i == fg_pipe->num_cmds-1)
8673 /* TODO: use strsignal() instead for bash compat? but that's bloat... */
8674 puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig));
8675 /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */
8676 /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here?
8677 * Maybe we need to use sig | 128? */
8680 fg_pipe->cmds[i].cmd_exitcode = ex;
8682 fg_pipe->stopped_cmds++;
8684 debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n",
8685 fg_pipe->alive_cmds, fg_pipe->stopped_cmds);
8686 rcode = job_exited_or_stopped(fg_pipe);
8688 /* Note: *non-interactive* bash does not continue if all processes in fg pipe
8689 * are stopped. Testcase: "cat | cat" in a script (not on command line!)
8690 * and "killall -STOP cat" */
8691 if (G_interactive_fd) {
8693 if (fg_pipe->alive_cmds != 0)
8694 insert_job_into_table(fg_pipe);
8698 if (fg_pipe->alive_cmds == 0)
8701 /* There are still running processes in the fg_pipe */
8704 /* It wasn't in fg_pipe, look for process in bg pipes */
8708 /* We were asked to wait for bg or orphaned children */
8709 /* No need to remember exitcode in this case */
8710 for (pi = G.job_list; pi; pi = pi->next) {
8711 for (i = 0; i < pi->num_cmds; i++) {
8712 if (pi->cmds[i].pid == childpid)
8713 goto found_pi_and_prognum;
8716 /* Happens when shell is used as init process (init=/bin/sh) */
8717 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
8718 return -1; /* this wasn't a process from fg_pipe */
8720 found_pi_and_prognum:
8723 int rcode = WEXITSTATUS(status);
8724 if (WIFSIGNALED(status))
8725 rcode = 128 + WTERMSIG(status);
8726 pi->cmds[i].cmd_exitcode = rcode;
8727 if (G.last_bg_pid == pi->cmds[i].pid)
8728 G.last_bg_pid_exitcode = rcode;
8729 pi->cmds[i].pid = 0;
8731 if (!pi->alive_cmds) {
8732 # if ENABLE_HUSH_BASH_COMPAT
8733 G.dead_job_exitcode = job_exited_or_stopped(pi);
8735 if (G_interactive_fd) {
8736 printf(JOB_STATUS_FORMAT, pi->jobid,
8737 "Done", pi->cmdtext);
8738 delete_finished_job(pi);
8741 * bash deletes finished jobs from job table only in interactive mode,
8742 * after "jobs" cmd, or if pid of a new process matches one of the old ones
8743 * (see cleanup_dead_jobs(), delete_old_job(), J_NOTIFIED in bash source).
8744 * Testcase script: "(exit 3) & sleep 1; wait %1; echo $?" prints 3 in bash.
8745 * We only retain one "dead" job, if it's the single job on the list.
8746 * This covers most of real-world scenarios where this is useful.
8748 if (pi != G.job_list)
8749 delete_finished_job(pi);
8757 return -1; /* this wasn't a process from fg_pipe */
8760 /* Check to see if any processes have exited -- if they have,
8761 * figure out why and see if a job has completed.
8763 * If non-NULL fg_pipe: wait for its completion or stop.
8764 * Return its exitcode or zero if stopped.
8766 * Alternatively (fg_pipe == NULL, waitfor_pid != 0):
8767 * waitpid(WNOHANG), if waitfor_pid exits or stops, return exitcode+1,
8768 * else return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for)
8769 * or 0 if no children changed status.
8771 * Alternatively (fg_pipe == NULL, waitfor_pid == 0),
8772 * return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for)
8773 * or 0 if no children changed status.
8775 static int checkjobs(struct pipe *fg_pipe, pid_t waitfor_pid)
8781 debug_printf_jobs("checkjobs %p\n", fg_pipe);
8783 attributes = WUNTRACED;
8784 if (fg_pipe == NULL)
8785 attributes |= WNOHANG;
8788 #if ENABLE_HUSH_FAST
8789 if (G.handled_SIGCHLD == G.count_SIGCHLD) {
8790 //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p",
8791 //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe);
8792 /* There was neither fork nor SIGCHLD since last waitpid */
8793 /* Avoid doing waitpid syscall if possible */
8794 if (!G.we_have_children) {
8798 if (fg_pipe == NULL) { /* is WNOHANG set? */
8799 /* We have children, but they did not exit
8800 * or stop yet (we saw no SIGCHLD) */
8803 /* else: !WNOHANG, waitpid will block, can't short-circuit */
8807 /* Do we do this right?
8808 * bash-3.00# sleep 20 | false
8810 * [3]+ Stopped sleep 20 | false
8811 * bash-3.00# echo $?
8812 * 1 <========== bg pipe is not fully done, but exitcode is already known!
8813 * [hush 1.14.0: yes we do it right]
8817 #if ENABLE_HUSH_FAST
8819 i = G.count_SIGCHLD;
8821 childpid = waitpid(-1, &status, attributes);
8822 if (childpid <= 0) {
8823 if (childpid && errno != ECHILD)
8824 bb_simple_perror_msg("waitpid");
8825 #if ENABLE_HUSH_FAST
8826 else { /* Until next SIGCHLD, waitpid's are useless */
8827 G.we_have_children = (childpid == 0);
8828 G.handled_SIGCHLD = i;
8829 //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
8832 /* ECHILD (no children), or 0 (no change in children status) */
8836 rcode = process_wait_result(fg_pipe, childpid, status);
8838 /* fg_pipe exited or stopped */
8841 if (childpid == waitfor_pid) { /* "wait PID" */
8842 debug_printf_exec("childpid==waitfor_pid:%d status:0x%08x\n", childpid, status);
8843 rcode = WEXITSTATUS(status);
8844 if (WIFSIGNALED(status))
8845 rcode = 128 + WTERMSIG(status);
8846 if (WIFSTOPPED(status))
8847 /* bash: "cmd & wait $!" and cmd stops: $? = 128 + stopsig */
8848 rcode = 128 + WSTOPSIG(status);
8850 break; /* "wait PID" called us, give it exitcode+1 */
8852 #if ENABLE_HUSH_BASH_COMPAT
8853 if (-1 == waitfor_pid /* "wait -n" (wait for any one job) */
8854 && G.dead_job_exitcode >= 0 /* some job did finish */
8856 debug_printf_exec("waitfor_pid:-1\n");
8857 rcode = G.dead_job_exitcode + 1;
8861 /* This wasn't one of our processes, or */
8862 /* fg_pipe still has running processes, do waitpid again */
8863 } /* while (waitpid succeeds)... */
8869 static int checkjobs_and_fg_shell(struct pipe *fg_pipe)
8872 int rcode = checkjobs(fg_pipe, 0 /*(no pid to wait for)*/);
8873 if (G_saved_tty_pgrp) {
8874 /* Job finished, move the shell to the foreground */
8875 p = getpgrp(); /* our process group id */
8876 debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p);
8877 tcsetpgrp(G_interactive_fd, p);
8883 /* Start all the jobs, but don't wait for anything to finish.
8886 * Return code is normally -1, when the caller has to wait for children
8887 * to finish to determine the exit status of the pipe. If the pipe
8888 * is a simple builtin command, however, the action is done by the
8889 * time run_pipe returns, and the exit code is provided as the
8892 * Returns -1 only if started some children. IOW: we have to
8893 * mask out retvals of builtins etc with 0xff!
8895 * The only case when we do not need to [v]fork is when the pipe
8896 * is single, non-backgrounded, non-subshell command. Examples:
8897 * cmd ; ... { list } ; ...
8898 * cmd && ... { list } && ...
8899 * cmd || ... { list } || ...
8900 * If it is, then we can run cmd as a builtin, NOFORK,
8901 * or (if SH_STANDALONE) an applet, and we can run the { list }
8902 * with run_list. If it isn't one of these, we fork and exec cmd.
8904 * Cases when we must fork:
8905 * non-single: cmd | cmd
8906 * backgrounded: cmd & { list } &
8907 * subshell: ( list ) [&]
8909 #if !ENABLE_HUSH_MODE_X
8910 #define redirect_and_varexp_helper(command, sqp, argv_expanded) \
8911 redirect_and_varexp_helper(command, sqp)
8913 static int redirect_and_varexp_helper(
8914 struct command *command,
8915 struct squirrel **sqp,
8916 char **argv_expanded)
8918 /* Assignments occur before redirects. Try:
8919 * a=`sleep 1` sleep 2 3>/qwe/rty
8922 char **new_env = expand_assignments(command->argv, command->assignment_cnt);
8923 dump_cmd_in_x_mode(new_env);
8924 dump_cmd_in_x_mode(argv_expanded);
8925 /* this takes ownership of new_env[i] elements, and frees new_env: */
8926 set_vars_and_save_old(new_env);
8928 return setup_redirects(command, sqp);
8930 static NOINLINE int run_pipe(struct pipe *pi)
8932 static const char *const null_ptr = NULL;
8936 struct command *command;
8937 char **argv_expanded;
8939 struct squirrel *squirrel = NULL;
8942 debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds);
8945 /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*"
8946 * Result should be 3 lines: q w e, qwe, q w e
8948 if (G.ifs_whitespace != G.ifs)
8949 free(G.ifs_whitespace);
8950 G.ifs = get_local_var_value("IFS");
8953 G.ifs_whitespace = (char*)G.ifs;
8954 p = skip_whitespace(G.ifs);
8956 /* Not all $IFS is whitespace */
8958 int len = p - G.ifs;
8959 p = skip_non_whitespace(p);
8960 G.ifs_whitespace = xmalloc(len + strlen(p) + 1); /* can overestimate */
8961 d = mempcpy(G.ifs_whitespace, G.ifs, len);
8971 G.ifs_whitespace = (char*)G.ifs;
8974 IF_HUSH_JOB(pi->pgrp = -1;)
8975 pi->stopped_cmds = 0;
8976 command = &pi->cmds[0];
8977 argv_expanded = NULL;
8979 if (pi->num_cmds != 1
8980 || pi->followup == PIPE_BG
8981 || command->cmd_type == CMD_SUBSHELL
8988 debug_printf_exec(": group:%p argv:'%s'\n",
8989 command->group, command->argv ? command->argv[0] : "NONE");
8991 if (command->group) {
8992 #if ENABLE_HUSH_FUNCTIONS
8993 if (command->cmd_type == CMD_FUNCDEF) {
8994 /* "executing" func () { list } */
8995 struct function *funcp;
8997 funcp = new_function(command->argv[0]);
8998 /* funcp->name is already set to argv[0] */
8999 funcp->body = command->group;
9001 funcp->body_as_string = command->group_as_string;
9002 command->group_as_string = NULL;
9004 command->group = NULL;
9005 command->argv[0] = NULL;
9006 debug_printf_exec("cmd %p has child func at %p\n", command, funcp);
9007 funcp->parent_cmd = command;
9008 command->child_func = funcp;
9010 debug_printf_exec("run_pipe: return EXIT_SUCCESS\n");
9012 return EXIT_SUCCESS;
9016 debug_printf_exec("non-subshell group\n");
9017 rcode = 1; /* exitcode if redir failed */
9018 if (setup_redirects(command, &squirrel) == 0) {
9019 debug_printf_exec(": run_list\n");
9020 //FIXME: we need to pass squirrel down into run_list()
9021 //for SH_STANDALONE case, or else this construct:
9022 // { find /proc/self/fd; true; } >FILE; cmd2
9023 //has no way of closing saved fd#1 for "find",
9024 //and in SH_STANDALONE mode, "find" is not execed,
9025 //therefore CLOEXEC on saved fd does not help.
9026 rcode = run_list(command->group) & 0xff;
9028 restore_redirects(squirrel);
9029 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
9031 debug_printf_exec("run_pipe: return %d\n", rcode);
9035 argv = command->argv ? command->argv : (char **) &null_ptr;
9037 const struct built_in_command *x;
9038 IF_HUSH_FUNCTIONS(const struct function *funcp;)
9039 IF_NOT_HUSH_FUNCTIONS(enum { funcp = 0 };)
9040 struct variable **sv_shadowed;
9041 struct variable *old_vars;
9043 #if ENABLE_HUSH_LINENO_VAR
9044 G.execute_lineno = command->lineno;
9047 if (argv[command->assignment_cnt] == NULL) {
9048 /* Assignments, but no command.
9049 * Ensure redirects take effect (that is, create files).
9053 G.expand_exitcode = 0;
9055 rcode = setup_redirects(command, &squirrel);
9056 restore_redirects(squirrel);
9058 /* Set shell variables */
9060 while (i < command->assignment_cnt) {
9061 char *p = expand_string_to_string(argv[i],
9062 EXP_FLAG_ESC_GLOB_CHARS,
9065 #if ENABLE_HUSH_MODE_X
9071 eq = strchrnul(p, '=');
9073 x_mode_addblock(p, (eq - p));
9074 x_mode_print_optionally_squoted(eq);
9078 debug_printf_env("set shell var:'%s'->'%s'\n", *argv, p);
9079 if (set_local_var(p, /*flag:*/ 0)) {
9080 /* assignment to readonly var / putenv error? */
9085 /* Redirect error sets $? to 1. Otherwise,
9086 * if evaluating assignment value set $?, retain it.
9088 * false; q=`exit 2`; echo $? - should print 2
9089 * false; x=1; echo $? - should print 0
9090 * Because of the 2nd case, we can't just use G.last_exitcode.
9093 rcode = G.expand_exitcode;
9094 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
9096 debug_printf_exec("run_pipe: return %d\n", rcode);
9100 /* Expand the rest into (possibly) many strings each */
9101 #if defined(CMD_SINGLEWORD_NOGLOB)
9102 if (command->cmd_type == CMD_SINGLEWORD_NOGLOB)
9103 argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt);
9106 argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt);
9108 /* If someone gives us an empty string: `cmd with empty output` */
9109 if (!argv_expanded[0]) {
9110 free(argv_expanded);
9111 /* `false` still has to set exitcode 1 */
9112 G.expand_exitcode = G.last_exitcode;
9113 goto only_assignments;
9117 sv_shadowed = G.shadowed_vars_pp;
9119 /* Check if argv[0] matches any functions (this goes before bltins) */
9120 IF_HUSH_FUNCTIONS(funcp = find_function(argv_expanded[0]);)
9121 IF_HUSH_FUNCTIONS(x = NULL;)
9122 IF_HUSH_FUNCTIONS(if (!funcp))
9123 x = find_builtin(argv_expanded[0]);
9125 if (x && x->b_function == builtin_exec && argv_expanded[1] == NULL) {
9126 debug_printf("exec with redirects only\n");
9128 * Variable assignments are executed, but then "forgotten":
9129 * a=`sleep 1;echo A` exec 3>&-; echo $a
9130 * sleeps, but prints nothing.
9132 enter_var_nest_level();
9133 G.shadowed_vars_pp = &old_vars;
9134 rcode = redirect_and_varexp_helper(command,
9135 /*squirrel:*/ ERR_PTR,
9138 G.shadowed_vars_pp = sv_shadowed;
9139 /* rcode=1 can be if redir file can't be opened */
9141 goto clean_up_and_ret1;
9144 /* Bump var nesting, or this will leak exported $a:
9145 * a=b true; env | grep ^a=
9147 enter_var_nest_level();
9148 /* Collect all variables "shadowed" by helper
9149 * (IOW: old vars overridden by "var1=val1 var2=val2 cmd..." syntax)
9150 * into old_vars list:
9152 G.shadowed_vars_pp = &old_vars;
9153 rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded);
9156 /* Do not collect *to old_vars list* vars shadowed
9157 * by e.g. "local VAR" builtin (collect them
9158 * in the previously nested list instead):
9159 * don't want them to be restored immediately
9160 * after "local" completes.
9162 G.shadowed_vars_pp = sv_shadowed;
9164 debug_printf_exec(": builtin '%s' '%s'...\n",
9165 x->b_cmd, argv_expanded[1]);
9167 rcode = x->b_function(argv_expanded) & 0xff;
9170 #if ENABLE_HUSH_FUNCTIONS
9172 debug_printf_exec(": function '%s' '%s'...\n",
9173 funcp->name, argv_expanded[1]);
9174 rcode = run_function(funcp, argv_expanded) & 0xff;
9176 * But do collect *to old_vars list* vars shadowed
9177 * within function execution. To that end, restore
9178 * this pointer _after_ function run:
9180 G.shadowed_vars_pp = sv_shadowed;
9185 if (ENABLE_FEATURE_SH_NOFORK && NUM_APPLETS > 1) {
9186 int n = find_applet_by_name(argv_expanded[0]);
9187 if (n < 0 || !APPLET_IS_NOFORK(n))
9190 enter_var_nest_level();
9191 /* Collect all variables "shadowed" by helper into old_vars list */
9192 G.shadowed_vars_pp = &old_vars;
9193 rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded);
9194 G.shadowed_vars_pp = sv_shadowed;
9197 debug_printf_exec(": run_nofork_applet '%s' '%s'...\n",
9198 argv_expanded[0], argv_expanded[1]);
9200 * Note: signals (^C) can't interrupt here.
9201 * We remember them and they will be acted upon
9202 * after applet returns.
9203 * This makes applets which can run for a long time
9204 * and/or wait for user input ineligible for NOFORK:
9205 * for example, "yes" or "rm" (rm -i waits for input).
9207 rcode = run_nofork_applet(n, argv_expanded);
9212 restore_redirects(squirrel);
9214 leave_var_nest_level();
9218 * Try "usleep 99999999" + ^C + "echo $?"
9219 * with FEATURE_SH_NOFORK=y.
9222 /* It was builtin or nofork.
9223 * if this would be a real fork/execed program,
9224 * it should have died if a fatal sig was received.
9225 * But OTOH, there was no separate process,
9226 * the sig was sent to _shell_, not to non-existing
9228 * Let's just handle ^C only, this one is obvious:
9229 * we aren't ok with exitcode 0 when ^C was pressed
9230 * during builtin/nofork.
9232 if (sigismember(&G.pending_set, SIGINT))
9233 rcode = 128 + SIGINT;
9235 free(argv_expanded);
9236 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
9238 debug_printf_exec("run_pipe return %d\n", rcode);
9243 /* NB: argv_expanded may already be created, and that
9244 * might include `cmd` runs! Do not rerun it! We *must*
9245 * use argv_expanded if it's non-NULL */
9247 /* Going to fork a child per each pipe member */
9252 while (cmd_no < pi->num_cmds) {
9253 struct fd_pair pipefds;
9255 int sv_var_nest_level = G.var_nest_level;
9256 volatile nommu_save_t nommu_save;
9257 nommu_save.old_vars = NULL;
9258 nommu_save.argv = NULL;
9259 nommu_save.argv_from_re_execing = NULL;
9261 command = &pi->cmds[cmd_no];
9263 if (command->argv) {
9264 debug_printf_exec(": pipe member '%s' '%s'...\n",
9265 command->argv[0], command->argv[1]);
9267 debug_printf_exec(": pipe member with no argv\n");
9270 /* pipes are inserted between pairs of commands */
9273 if (cmd_no < pi->num_cmds)
9274 xpiped_pair(pipefds);
9276 #if ENABLE_HUSH_LINENO_VAR
9277 G.execute_lineno = command->lineno;
9280 command->pid = BB_MMU ? fork() : vfork();
9281 if (!command->pid) { /* child */
9283 disable_restore_tty_pgrp_on_exit();
9284 CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */
9286 /* Every child adds itself to new process group
9287 * with pgid == pid_of_first_child_in_pipe */
9288 if (G.run_list_level == 1 && G_interactive_fd) {
9291 if (pgrp < 0) /* true for 1st process only */
9293 if (setpgid(0, pgrp) == 0
9294 && pi->followup != PIPE_BG
9295 && G_saved_tty_pgrp /* we have ctty */
9297 /* We do it in *every* child, not just first,
9299 tcsetpgrp(G_interactive_fd, pgrp);
9303 if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) {
9304 /* 1st cmd in backgrounded pipe
9305 * should have its stdin /dev/null'ed */
9307 if (open(bb_dev_null, O_RDONLY))
9308 xopen("/", O_RDONLY);
9310 xmove_fd(next_infd, 0);
9312 xmove_fd(pipefds.wr, 1);
9315 /* Like bash, explicit redirects override pipes,
9316 * and the pipe fd (fd#1) is available for dup'ing:
9317 * "cmd1 2>&1 | cmd2": fd#1 is duped to fd#2, thus stderr
9318 * of cmd1 goes into pipe.
9320 if (setup_redirects(command, NULL)) {
9321 /* Happens when redir file can't be opened:
9322 * $ hush -c 'echo FOO >&2 | echo BAR 3>/qwe/rty; echo BAZ'
9324 * hush: can't open '/qwe/rty': No such file or directory
9326 * (echo BAR is not executed, it hits _exit(1) below)
9331 /* Stores to nommu_save list of env vars putenv'ed
9332 * (NOMMU, on MMU we don't need that) */
9333 /* cast away volatility... */
9334 pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded);
9335 /* pseudo_exec() does not return */
9338 /* parent or error */
9339 #if ENABLE_HUSH_FAST
9341 //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
9343 enable_restore_tty_pgrp_on_exit();
9345 /* Clean up after vforked child */
9346 free(nommu_save.argv);
9347 free(nommu_save.argv_from_re_execing);
9348 G.var_nest_level = sv_var_nest_level;
9349 remove_nested_vars();
9350 add_vars(nommu_save.old_vars);
9352 free(argv_expanded);
9353 argv_expanded = NULL;
9354 if (command->pid < 0) { /* [v]fork failed */
9355 /* Clearly indicate, was it fork or vfork */
9356 bb_simple_perror_msg(BB_MMU ? "vfork"+1 : "vfork");
9360 /* Second and next children need to know pid of first one */
9362 pi->pgrp = command->pid;
9368 if (cmd_no < pi->num_cmds)
9370 /* Pass read (output) pipe end to next iteration */
9371 next_infd = pipefds.rd;
9374 if (!pi->alive_cmds) {
9376 debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n");
9381 debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds);
9385 /* NB: called by pseudo_exec, and therefore must not modify any
9386 * global data until exec/_exit (we can be a child after vfork!) */
9387 static int run_list(struct pipe *pi)
9389 #if ENABLE_HUSH_CASE
9390 char *case_word = NULL;
9392 #if ENABLE_HUSH_LOOPS
9393 struct pipe *loop_top = NULL;
9394 char **for_lcur = NULL;
9395 char **for_list = NULL;
9397 smallint last_followup;
9399 #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE
9400 smalluint cond_code = 0;
9402 enum { cond_code = 0 };
9405 smallint rword; /* RES_foo */
9406 smallint last_rword; /* ditto */
9409 debug_printf_exec("run_list start lvl %d\n", G.run_list_level);
9412 #if ENABLE_HUSH_LOOPS
9413 /* Check syntax for "for" */
9416 for (cpipe = pi; cpipe; cpipe = cpipe->next) {
9417 if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN)
9419 /* current word is FOR or IN (BOLD in comments below) */
9420 if (cpipe->next == NULL) {
9421 syntax_error("malformed for");
9423 debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level);
9426 /* "FOR v; do ..." and "for v IN a b; do..." are ok */
9427 if (cpipe->next->res_word == RES_DO)
9429 /* next word is not "do". It must be "in" then ("FOR v in ...") */
9430 if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */
9431 || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */
9433 syntax_error("malformed for");
9435 debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level);
9442 /* Past this point, all code paths should jump to ret: label
9443 * in order to return, no direct "return" statements please.
9444 * This helps to ensure that no memory is leaked. */
9452 last_rword = RES_XXXX;
9454 last_followup = PIPE_SEQ;
9455 rcode = G.last_exitcode;
9457 /* Go through list of pipes, (maybe) executing them. */
9458 for (; pi; pi = IF_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) {
9460 int sv_errexit_depth;
9464 if (G_flag_return_in_progress == 1)
9467 IF_HAS_KEYWORDS(rword = pi->res_word;)
9468 debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n",
9469 rword, cond_code, last_rword);
9471 sv_errexit_depth = G.errexit_depth;
9474 rword == RES_IF || rword == RES_ELIF ||
9476 pi->followup != PIPE_SEQ
9480 #if ENABLE_HUSH_LOOPS
9481 if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR)
9482 && loop_top == NULL /* avoid bumping G.depth_of_loop twice */
9484 /* start of a loop: remember where loop starts */
9489 /* Still in the same "if...", "then..." or "do..." branch? */
9490 if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) {
9491 if ((rcode == 0 && last_followup == PIPE_OR)
9492 || (rcode != 0 && last_followup == PIPE_AND)
9494 /* It is "<true> || CMD" or "<false> && CMD"
9495 * and we should not execute CMD */
9496 debug_printf_exec("skipped cmd because of || or &&\n");
9497 last_followup = pi->followup;
9498 goto dont_check_jobs_but_continue;
9501 last_followup = pi->followup;
9502 IF_HAS_KEYWORDS(last_rword = rword;)
9505 if (rword == RES_THEN) {
9506 /* if false; then ... fi has exitcode 0! */
9507 G.last_exitcode = rcode = EXIT_SUCCESS;
9508 /* "if <false> THEN cmd": skip cmd */
9512 if (rword == RES_ELSE || rword == RES_ELIF) {
9513 /* "if <true> then ... ELSE/ELIF cmd":
9514 * skip cmd and all following ones */
9519 #if ENABLE_HUSH_LOOPS
9520 if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */
9522 /* first loop through for */
9524 static const char encoded_dollar_at[] ALIGN1 = {
9525 SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0'
9526 }; /* encoded representation of "$@" */
9527 static const char *const encoded_dollar_at_argv[] = {
9528 encoded_dollar_at, NULL
9529 }; /* argv list with one element: "$@" */
9532 G.last_exitcode = rcode = EXIT_SUCCESS;
9533 vals = (char**)encoded_dollar_at_argv;
9534 if (pi->next->res_word == RES_IN) {
9535 /* if no variable values after "in" we skip "for" */
9536 if (!pi->next->cmds[0].argv) {
9537 debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n");
9540 vals = pi->next->cmds[0].argv;
9541 } /* else: "for var; do..." -> assume "$@" list */
9542 /* create list of variable values */
9543 debug_print_strings("for_list made from", vals);
9544 for_list = expand_strvec_to_strvec(vals);
9545 for_lcur = for_list;
9546 debug_print_strings("for_list", for_list);
9549 /* "for" loop is over, clean up */
9555 /* Insert next value from for_lcur */
9556 /* note: *for_lcur already has quotes removed, $var expanded, etc */
9557 set_local_var(xasprintf("%s=%s", pi->cmds[0].argv[0], *for_lcur++), /*flag:*/ 0);
9560 if (rword == RES_IN) {
9561 continue; /* "for v IN list;..." - "in" has no cmds anyway */
9563 if (rword == RES_DONE) {
9564 continue; /* "done" has no cmds too */
9567 #if ENABLE_HUSH_CASE
9568 if (rword == RES_CASE) {
9569 debug_printf_exec("CASE cond_code:%d\n", cond_code);
9570 case_word = expand_string_to_string(pi->cmds->argv[0],
9571 EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1);
9572 debug_printf_exec("CASE word1:'%s'\n", case_word);
9573 //unbackslash(case_word);
9574 //debug_printf_exec("CASE word2:'%s'\n", case_word);
9577 if (rword == RES_MATCH) {
9580 debug_printf_exec("MATCH cond_code:%d\n", cond_code);
9581 if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */
9583 /* all prev words didn't match, does this one match? */
9584 argv = pi->cmds->argv;
9587 debug_printf_exec("expand_string_to_string('%s')\n", *argv);
9588 pattern = expand_string_to_string(*argv,
9589 EXP_FLAG_ESC_GLOB_CHARS,
9592 /* TODO: which FNM_xxx flags to use? */
9593 cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0);
9594 debug_printf_exec("fnmatch(pattern:'%s',str:'%s'):%d\n",
9595 pattern, case_word, cond_code);
9597 if (cond_code == 0) {
9598 /* match! we will execute this branch */
9600 case_word = NULL; /* make future "word)" stop */
9607 if (rword == RES_CASE_BODY) { /* inside of a case branch */
9608 debug_printf_exec("CASE_BODY cond_code:%d\n", cond_code);
9610 continue; /* not matched yet, skip this pipe */
9612 if (rword == RES_ESAC) {
9613 debug_printf_exec("ESAC cond_code:%d\n", cond_code);
9615 /* "case" did not match anything: still set $? (to 0) */
9616 G.last_exitcode = rcode = EXIT_SUCCESS;
9620 /* Just pressing <enter> in shell should check for jobs.
9621 * OTOH, in non-interactive shell this is useless
9622 * and only leads to extra job checks */
9623 if (pi->num_cmds == 0) {
9624 if (G_interactive_fd)
9625 goto check_jobs_and_continue;
9629 /* After analyzing all keywords and conditions, we decided
9630 * to execute this pipe. NB: have to do checkjobs(NULL)
9631 * after run_pipe to collect any background children,
9632 * even if list execution is to be stopped. */
9633 debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds);
9634 #if ENABLE_HUSH_LOOPS
9635 G.flag_break_continue = 0;
9637 rcode = r = run_pipe(pi); /* NB: rcode is a smalluint, r is int */
9639 /* We ran a builtin, function, or group.
9640 * rcode is already known
9641 * and we don't need to wait for anything. */
9642 debug_printf_exec(": builtin/func exitcode %d\n", rcode);
9643 G.last_exitcode = rcode;
9644 check_and_run_traps();
9645 #if ENABLE_HUSH_TRAP && ENABLE_HUSH_FUNCTIONS
9646 rcode = G.last_exitcode; /* "return" in trap can change it, read back */
9648 #if ENABLE_HUSH_LOOPS
9649 /* Was it "break" or "continue"? */
9650 if (G.flag_break_continue) {
9651 smallint fbc = G.flag_break_continue;
9652 /* We might fall into outer *loop*,
9653 * don't want to break it too */
9655 G.depth_break_continue--;
9656 if (G.depth_break_continue == 0)
9657 G.flag_break_continue = 0;
9658 /* else: e.g. "continue 2" should *break* once, *then* continue */
9659 } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */
9660 if (G.depth_break_continue != 0 || fbc == BC_BREAK) {
9661 checkjobs(NULL, 0 /*(no pid to wait for)*/);
9664 /* "continue": simulate end of loop */
9669 if (G_flag_return_in_progress == 1) {
9670 checkjobs(NULL, 0 /*(no pid to wait for)*/);
9673 } else if (pi->followup == PIPE_BG) {
9674 /* What does bash do with attempts to background builtins? */
9675 /* even bash 3.2 doesn't do that well with nested bg:
9676 * try "{ { sleep 10; echo DEEP; } & echo HERE; } &".
9677 * I'm NOT treating inner &'s as jobs */
9679 if (G.run_list_level == 1)
9680 insert_job_into_table(pi);
9682 /* Last command's pid goes to $! */
9683 G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid;
9684 G.last_bg_pid_exitcode = 0;
9685 debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n");
9686 /* Check pi->pi_inverted? "! sleep 1 & echo $?": bash says 1. dash and ash say 0 */
9687 rcode = EXIT_SUCCESS;
9691 if (G.run_list_level == 1 && G_interactive_fd) {
9692 /* Waits for completion, then fg's main shell */
9693 rcode = checkjobs_and_fg_shell(pi);
9694 debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode);
9698 /* This one just waits for completion */
9699 rcode = checkjobs(pi, 0 /*(no pid to wait for)*/);
9700 debug_printf_exec(": checkjobs exitcode %d\n", rcode);
9702 G.last_exitcode = rcode;
9703 check_and_run_traps();
9704 #if ENABLE_HUSH_TRAP && ENABLE_HUSH_FUNCTIONS
9705 rcode = G.last_exitcode; /* "return" in trap can change it, read back */
9709 /* Handle "set -e" */
9710 if (rcode != 0 && G.o_opt[OPT_O_ERREXIT]) {
9711 debug_printf_exec("ERREXIT:1 errexit_depth:%d\n", G.errexit_depth);
9712 if (G.errexit_depth == 0)
9715 G.errexit_depth = sv_errexit_depth;
9717 /* Analyze how result affects subsequent commands */
9719 if (rword == RES_IF || rword == RES_ELIF)
9722 check_jobs_and_continue:
9723 checkjobs(NULL, 0 /*(no pid to wait for)*/);
9724 dont_check_jobs_but_continue: ;
9725 #if ENABLE_HUSH_LOOPS
9726 /* Beware of "while false; true; do ..."! */
9728 && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE)
9729 /* check for RES_DONE is needed for "while ...; do \n done" case */
9731 if (rword == RES_WHILE) {
9733 /* "while false; do...done" - exitcode 0 */
9734 G.last_exitcode = rcode = EXIT_SUCCESS;
9735 debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n");
9739 if (rword == RES_UNTIL) {
9741 debug_printf_exec(": until expr is true: breaking\n");
9752 #if ENABLE_HUSH_LOOPS
9757 #if ENABLE_HUSH_CASE
9761 debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode);
9765 /* Select which version we will use */
9766 static int run_and_free_list(struct pipe *pi)
9769 debug_printf_exec("run_and_free_list entered\n");
9770 if (!G.o_opt[OPT_O_NOEXEC]) {
9771 debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds);
9772 rcode = run_list(pi);
9774 /* free_pipe_list has the side effect of clearing memory.
9775 * In the long run that function can be merged with run_list,
9776 * but doing that now would hobble the debugging effort. */
9778 debug_printf_exec("run_and_free_list return %d\n", rcode);
9783 static void install_sighandlers(unsigned mask)
9785 sighandler_t old_handler;
9787 while ((mask >>= 1) != 0) {
9791 old_handler = install_sighandler(sig, pick_sighandler(sig));
9792 /* POSIX allows shell to re-enable SIGCHLD
9793 * even if it was SIG_IGN on entry.
9794 * Therefore we skip IGN check for it:
9798 /* Interactive bash re-enables SIGHUP which is SIG_IGNed on entry.
9800 * trap '' hup; bash; echo RET # type "kill -hup $$", see SIGHUP having effect
9801 * trap '' hup; bash -c 'kill -hup $$; echo ALIVE' # here SIGHUP is SIG_IGNed
9803 if (sig == SIGHUP && G_interactive_fd)
9805 /* Unless one of the above signals, is it SIG_IGN? */
9806 if (old_handler == SIG_IGN) {
9807 /* oops... restore back to IGN, and record this fact */
9808 install_sighandler(sig, old_handler);
9809 #if ENABLE_HUSH_TRAP
9811 G_traps = xzalloc(sizeof(G_traps[0]) * NSIG);
9813 G_traps[sig] = xzalloc(1); /* == xstrdup(""); */
9819 /* Called a few times only (or even once if "sh -c") */
9820 static void install_special_sighandlers(void)
9824 /* Which signals are shell-special? */
9825 mask = (1 << SIGQUIT) | (1 << SIGCHLD);
9826 if (G_interactive_fd) {
9827 mask |= SPECIAL_INTERACTIVE_SIGS;
9828 if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */
9829 mask |= SPECIAL_JOBSTOP_SIGS;
9831 /* Careful, do not re-install handlers we already installed */
9832 if (G.special_sig_mask != mask) {
9833 unsigned diff = mask & ~G.special_sig_mask;
9834 G.special_sig_mask = mask;
9835 install_sighandlers(diff);
9841 /* Set handlers to restore tty pgrp and exit */
9842 static void install_fatal_sighandlers(void)
9846 /* We will restore tty pgrp on these signals */
9848 /*+ (1 << SIGILL ) * HUSH_DEBUG*/
9849 /*+ (1 << SIGFPE ) * HUSH_DEBUG*/
9850 + (1 << SIGBUS ) * HUSH_DEBUG
9851 + (1 << SIGSEGV) * HUSH_DEBUG
9852 /*+ (1 << SIGTRAP) * HUSH_DEBUG*/
9854 /* bash 3.2 seems to handle these just like 'fatal' ones */
9857 /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs.
9858 * if we aren't interactive... but in this case
9859 * we never want to restore pgrp on exit, and this fn is not called
9861 /*+ (1 << SIGHUP )*/
9862 /*+ (1 << SIGTERM)*/
9863 /*+ (1 << SIGINT )*/
9865 G_fatal_sig_mask = mask;
9867 install_sighandlers(mask);
9871 static int set_mode(int state, char mode, const char *o_opt)
9876 G.o_opt[OPT_O_NOEXEC] = state;
9879 IF_HUSH_MODE_X(G_x_mode = state;)
9880 IF_HUSH_MODE_X(if (G.x_mode_fd <= 0) G.x_mode_fd = dup_CLOEXEC(2, 10);)
9883 G.o_opt[OPT_O_ERREXIT] = state;
9887 /* "set -o" or "set +o" without parameter.
9888 * in bash, set -o produces this output:
9892 * We always use the second form.
9894 const char *p = o_opt_strings;
9897 printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p);
9903 idx = index_in_strings(o_opt_strings, o_opt);
9905 G.o_opt[idx] = state;
9908 /* fall through to error */
9910 return EXIT_FAILURE;
9912 return EXIT_SUCCESS;
9915 int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
9916 int hush_main(int argc, char **argv)
9919 OPT_login = (1 << 0),
9922 unsigned builtin_argc;
9924 struct variable *cur_var;
9925 struct variable *shell_ver;
9928 if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */
9929 G.last_exitcode = EXIT_SUCCESS;
9930 #if ENABLE_HUSH_TRAP && ENABLE_HUSH_FUNCTIONS
9931 G.return_exitcode = -1;
9934 #if ENABLE_HUSH_FAST
9935 G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */
9938 G.argv0_for_re_execing = argv[0];
9941 /* Deal with HUSH_VERSION */
9942 debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION");
9943 unsetenv("HUSH_VERSION"); /* in case it exists in initial env */
9944 shell_ver = xzalloc(sizeof(*shell_ver));
9945 shell_ver->flg_export = 1;
9946 shell_ver->flg_read_only = 1;
9947 /* Code which handles ${var<op>...} needs writable values for all variables,
9948 * therefore we xstrdup: */
9949 shell_ver->varstr = xstrdup(hush_version_str);
9951 /* Create shell local variables from the values
9952 * currently living in the environment */
9953 G.top_var = shell_ver;
9954 cur_var = G.top_var;
9957 char *value = strchr(*e, '=');
9958 if (value) { /* paranoia */
9959 cur_var->next = xzalloc(sizeof(*cur_var));
9960 cur_var = cur_var->next;
9961 cur_var->varstr = *e;
9962 cur_var->max_len = strlen(*e);
9963 cur_var->flg_export = 1;
9967 /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */
9968 debug_printf_env("putenv '%s'\n", shell_ver->varstr);
9969 putenv(shell_ver->varstr);
9972 set_pwd_var(SETFLAG_EXPORT);
9974 #if BASH_HOSTNAME_VAR
9975 /* Set (but not export) HOSTNAME unless already set */
9976 if (!get_local_var_value("HOSTNAME")) {
9979 set_local_var_from_halves("HOSTNAME", uts.nodename);
9982 /* IFS is not inherited from the parent environment */
9983 set_local_var_from_halves("IFS", defifs);
9985 if (!get_local_var_value("PATH"))
9986 set_local_var_from_halves("PATH", bb_default_root_path);
9988 /* PS1/PS2 are set later, if we determine that we are interactive */
9990 /* bash also exports SHLVL and _,
9991 * and sets (but doesn't export) the following variables:
9993 * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu")
9994 * BASH_VERSION='3.2.0(1)-release'
9996 * MACHTYPE=i386-pc-linux-gnu
9998 * PPID=<NNNNN> - we also do it elsewhere
10009 * PIPESTATUS=([0]="0")
10010 * HISTFILE=/<xxx>/.bash_history
10014 * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:.
10016 * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
10023 /* Initialize some more globals to non-zero values */
10024 die_func = restore_ttypgrp_and__exit;
10026 /* Shell is non-interactive at first. We need to call
10027 * install_special_sighandlers() if we are going to execute "sh <script>",
10028 * "sh -c <cmds>" or login shell's /etc/profile and friends.
10029 * If we later decide that we are interactive, we run install_special_sighandlers()
10030 * in order to intercept (more) signals.
10033 /* Parse options */
10034 /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */
10035 flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0;
10037 #if NUM_SCRIPTS > 0
10039 optarg = get_script_content(-argc - 1);
10041 argc = string_array_len(argv);
10046 int opt = getopt(argc, argv, "+c:exinsl"
10049 # if ENABLE_HUSH_FUNCTIONS
10059 * sh ... -c 'script'
10060 * sh ... -c 'script' ARG0 [ARG1...]
10061 * On NOMMU, if builtin_argc != 0,
10062 * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...]
10063 * "" needs to be replaced with NULL
10064 * and BARGV vector fed to builtin function.
10065 * Note: the form without ARG0 never happens:
10066 * sh ... -c 'builtin' BARGV... ""
10068 #if NUM_SCRIPTS > 0
10072 G.root_pid = getpid();
10073 G.root_ppid = getppid();
10075 G.global_argv = argv + optind;
10076 G.global_argc = argc - optind;
10077 if (builtin_argc) {
10078 /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */
10079 const struct built_in_command *x;
10081 install_special_sighandlers();
10082 x = find_builtin(optarg);
10083 if (x) { /* paranoia */
10084 G.global_argc -= builtin_argc; /* skip [BARGV...] "" */
10085 G.global_argv += builtin_argc;
10086 G.global_argv[-1] = NULL; /* replace "" */
10088 G.last_exitcode = x->b_function(argv + optind - 1);
10093 if (!G.global_argv[0]) {
10094 /* -c 'script' (no params): prevent empty $0 */
10095 G.global_argv--; /* points to argv[i] of 'script' */
10096 G.global_argv[0] = argv[0];
10098 } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */
10099 install_special_sighandlers();
10100 parse_and_run_string(optarg);
10103 /* Well, we cannot just declare interactiveness,
10104 * we have to have some stuff (ctty, etc) */
10105 /* G_interactive_fd++; */
10111 flags |= OPT_login;
10114 case '<': /* "big heredoc" support */
10115 full_write1_str(optarg);
10118 unsigned long long empty_trap_mask;
10120 G.root_pid = bb_strtou(optarg, &optarg, 16);
10122 G.root_ppid = bb_strtou(optarg, &optarg, 16);
10124 G.last_bg_pid = bb_strtou(optarg, &optarg, 16);
10126 G.last_exitcode = bb_strtou(optarg, &optarg, 16);
10128 builtin_argc = bb_strtou(optarg, &optarg, 16);
10130 empty_trap_mask = bb_strtoull(optarg, &optarg, 16);
10131 if (empty_trap_mask != 0) {
10132 IF_HUSH_TRAP(int sig;)
10133 install_special_sighandlers();
10134 # if ENABLE_HUSH_TRAP
10135 G_traps = xzalloc(sizeof(G_traps[0]) * NSIG);
10136 for (sig = 1; sig < NSIG; sig++) {
10137 if (empty_trap_mask & (1LL << sig)) {
10138 G_traps[sig] = xzalloc(1); /* == xstrdup(""); */
10139 install_sighandler(sig, SIG_IGN);
10144 # if ENABLE_HUSH_LOOPS
10146 G.depth_of_loop = bb_strtou(optarg, &optarg, 16);
10148 # if ENABLE_HUSH_FUNCTIONS
10149 /* nommu uses re-exec trick for "... | func | ...",
10150 * should allow "return".
10151 * This accidentally allows returns in subshells.
10153 G_flag_return_in_progress = -1;
10159 set_local_var(xstrdup(optarg), opt == 'R' ? SETFLAG_MAKE_RO : 0);
10161 # if ENABLE_HUSH_FUNCTIONS
10163 struct function *funcp = new_function(optarg);
10164 /* funcp->name is already set to optarg */
10165 /* funcp->body is set to NULL. It's a special case. */
10166 funcp->body_as_string = argv[optind];
10175 if (set_mode(1, opt, NULL) == 0) /* no error */
10179 fprintf(stderr, "Usage: sh [FILE]...\n"
10180 " or: sh -c command [args]...\n\n");
10181 exit(EXIT_FAILURE);
10186 } /* option parsing loop */
10188 /* Skip options. Try "hush -l": $1 should not be "-l"! */
10189 G.global_argc = argc - (optind - 1);
10190 G.global_argv = argv + (optind - 1);
10191 G.global_argv[0] = argv[0];
10194 G.root_pid = getpid();
10195 G.root_ppid = getppid();
10198 /* If we are login shell... */
10199 if (flags & OPT_login) {
10201 debug_printf("sourcing /etc/profile\n");
10202 input = hfopen("/etc/profile");
10203 if (input != NULL) {
10204 install_special_sighandlers();
10205 parse_and_run_file(input);
10208 /* bash: after sourcing /etc/profile,
10209 * tries to source (in the given order):
10210 * ~/.bash_profile, ~/.bash_login, ~/.profile,
10211 * stopping on first found. --noprofile turns this off.
10212 * bash also sources ~/.bash_logout on exit.
10213 * If called as sh, skips .bash_XXX files.
10217 /* -s is: hush -s ARGV1 ARGV2 (no SCRIPT) */
10218 if (!G.opt_s && G.global_argv[1]) {
10221 * "bash <script>" (which is never interactive (unless -i?))
10222 * sources $BASH_ENV here (without scanning $PATH).
10223 * If called as sh, does the same but with $ENV.
10224 * Also NB, per POSIX, $ENV should undergo parameter expansion.
10228 debug_printf("running script '%s'\n", G.global_argv[0]);
10229 xfunc_error_retval = 127; /* for "hush /does/not/exist" case */
10230 input = hfopen(G.global_argv[0]);
10232 bb_simple_perror_msg_and_die(G.global_argv[0]);
10234 xfunc_error_retval = 1;
10235 install_special_sighandlers();
10236 parse_and_run_file(input);
10237 #if ENABLE_FEATURE_CLEAN_UP
10242 /* "implicit" -s: bare interactive hush shows 's' in $- */
10245 /* Up to here, shell was non-interactive. Now it may become one.
10246 * NB: don't forget to (re)run install_special_sighandlers() as needed.
10249 /* A shell is interactive if the '-i' flag was given,
10250 * or if all of the following conditions are met:
10252 * no arguments remaining or the -s flag given
10253 * standard input is a terminal
10254 * standard output is a terminal
10255 * Refer to Posix.2, the description of the 'sh' utility.
10257 #if ENABLE_HUSH_JOB
10258 if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) {
10259 G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO);
10260 debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp);
10261 if (G_saved_tty_pgrp < 0)
10262 G_saved_tty_pgrp = 0;
10264 /* try to dup stdin to high fd#, >= 255 */
10265 G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254);
10266 if (G_interactive_fd < 0) {
10267 /* try to dup to any fd */
10268 G_interactive_fd = dup(STDIN_FILENO);
10269 if (G_interactive_fd < 0) {
10271 G_interactive_fd = 0;
10272 G_saved_tty_pgrp = 0;
10276 debug_printf("interactive_fd:%d\n", G_interactive_fd);
10277 if (G_interactive_fd) {
10278 close_on_exec_on(G_interactive_fd);
10280 if (G_saved_tty_pgrp) {
10281 /* If we were run as 'hush &', sleep until we are
10282 * in the foreground (tty pgrp == our pgrp).
10283 * If we get started under a job aware app (like bash),
10284 * make sure we are now in charge so we don't fight over
10285 * who gets the foreground */
10287 pid_t shell_pgrp = getpgrp();
10288 G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd);
10289 if (G_saved_tty_pgrp == shell_pgrp)
10291 /* send TTIN to ourself (should stop us) */
10292 kill(- shell_pgrp, SIGTTIN);
10296 /* Install more signal handlers */
10297 install_special_sighandlers();
10299 if (G_saved_tty_pgrp) {
10300 /* Set other signals to restore saved_tty_pgrp */
10301 install_fatal_sighandlers();
10302 /* Put ourselves in our own process group
10303 * (bash, too, does this only if ctty is available) */
10304 bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */
10305 /* Grab control of the terminal */
10306 tcsetpgrp(G_interactive_fd, getpid());
10308 enable_restore_tty_pgrp_on_exit();
10310 # if ENABLE_FEATURE_EDITING
10311 G.line_input_state = new_line_input_t(FOR_SHELL);
10312 # if EDITING_HAS_get_exe_name
10313 G.line_input_state->get_exe_name = get_builtin_name;
10316 # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0
10318 const char *hp = get_local_var_value("HISTFILE");
10320 hp = get_local_var_value("HOME");
10322 hp = concat_path_file(hp, ".hush_history");
10327 G.line_input_state->hist_file = hp;
10328 //set_local_var(xasprintf("HISTFILE=%s", ...));
10330 # if ENABLE_FEATURE_SH_HISTFILESIZE
10331 hp = get_local_var_value("HISTFILESIZE");
10332 G.line_input_state->max_history = size_from_HISTFILESIZE(hp);
10337 install_special_sighandlers();
10339 #elif ENABLE_HUSH_INTERACTIVE
10340 /* No job control compiled in, only prompt/line editing */
10341 if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) {
10342 G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254);
10343 if (G_interactive_fd < 0) {
10344 /* try to dup to any fd */
10345 G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, -1);
10346 if (G_interactive_fd < 0)
10348 G_interactive_fd = 0;
10351 if (G_interactive_fd) {
10352 close_on_exec_on(G_interactive_fd);
10354 install_special_sighandlers();
10356 /* We have interactiveness code disabled */
10357 install_special_sighandlers();
10360 * if interactive but not a login shell, sources ~/.bashrc
10361 * (--norc turns this off, --rcfile <file> overrides)
10364 if (G_interactive_fd) {
10365 #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT
10366 /* Set (but not export) PS1/2 unless already set */
10367 if (!get_local_var_value("PS1"))
10368 set_local_var_from_halves("PS1", "\\w \\$ ");
10369 if (!get_local_var_value("PS2"))
10370 set_local_var_from_halves("PS2", "> ");
10372 if (!ENABLE_FEATURE_SH_EXTRA_QUIET) {
10373 /* note: ash and hush share this string */
10374 printf("\n\n%s %s\n"
10375 IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n")
10378 "hush - the humble shell"
10383 parse_and_run_file(hfopen(NULL)); /* stdin */
10386 hush_exit(G.last_exitcode);
10393 static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM)
10398 #if ENABLE_HUSH_TEST || ENABLE_HUSH_ECHO || ENABLE_HUSH_PRINTF || ENABLE_HUSH_KILL
10399 static int run_applet_main(char **argv, int (*applet_main_func)(int argc, char **argv))
10401 int argc = string_array_len(argv);
10402 return applet_main_func(argc, argv);
10405 #if ENABLE_HUSH_TEST || BASH_TEST2
10406 static int FAST_FUNC builtin_test(char **argv)
10408 return run_applet_main(argv, test_main);
10411 #if ENABLE_HUSH_ECHO
10412 static int FAST_FUNC builtin_echo(char **argv)
10414 return run_applet_main(argv, echo_main);
10417 #if ENABLE_HUSH_PRINTF
10418 static int FAST_FUNC builtin_printf(char **argv)
10420 return run_applet_main(argv, printf_main);
10424 #if ENABLE_HUSH_HELP
10425 static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM)
10427 const struct built_in_command *x;
10430 "Built-in commands:\n"
10431 "------------------\n");
10432 for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) {
10434 printf("%-10s%s\n", x->b_cmd, x->b_descr);
10436 return EXIT_SUCCESS;
10440 #if MAX_HISTORY && ENABLE_FEATURE_EDITING
10441 static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM)
10443 if (G.line_input_state)
10444 show_history(G.line_input_state);
10445 return EXIT_SUCCESS;
10449 static char **skip_dash_dash(char **argv)
10452 if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0')
10457 static int FAST_FUNC builtin_cd(char **argv)
10459 const char *newdir;
10461 argv = skip_dash_dash(argv);
10463 if (newdir == NULL) {
10464 /* bash does nothing (exitcode 0) if HOME is ""; if it's unset,
10465 * bash says "bash: cd: HOME not set" and does nothing
10468 const char *home = get_local_var_value("HOME");
10469 newdir = home ? home : "/";
10471 if (chdir(newdir)) {
10472 /* Mimic bash message exactly */
10473 bb_perror_msg("cd: %s", newdir);
10474 return EXIT_FAILURE;
10476 /* Read current dir (get_cwd(1) is inside) and set PWD.
10477 * Note: do not enforce exporting. If PWD was unset or unexported,
10478 * set it again, but do not export. bash does the same.
10480 set_pwd_var(/*flag:*/ 0);
10481 return EXIT_SUCCESS;
10484 static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM)
10487 return EXIT_SUCCESS;
10490 static int FAST_FUNC builtin_eval(char **argv)
10492 argv = skip_dash_dash(argv);
10495 return EXIT_SUCCESS;
10497 IF_HUSH_MODE_X(G.x_mode_depth++;)
10498 //bb_error_msg("%s: ++x_mode_depth=%d", __func__, G.x_mode_depth);
10501 * eval "echo Hi; done" ("done" is syntax error):
10502 * "echo Hi" will not execute too.
10504 parse_and_run_string(argv[0]);
10506 /* "The eval utility shall construct a command by
10507 * concatenating arguments together, separating
10508 * each with a <space> character."
10514 len += strlen(*pp) + 1;
10516 str = p = xmalloc(len);
10519 p = stpcpy(p, *pp);
10525 parse_and_run_string(str);
10528 IF_HUSH_MODE_X(G.x_mode_depth--;)
10529 //bb_error_msg("%s: --x_mode_depth=%d", __func__, G.x_mode_depth);
10530 return G.last_exitcode;
10533 static int FAST_FUNC builtin_exec(char **argv)
10535 argv = skip_dash_dash(argv);
10536 if (argv[0] == NULL)
10537 return EXIT_SUCCESS; /* bash does this */
10539 /* Careful: we can end up here after [v]fork. Do not restore
10540 * tty pgrp then, only top-level shell process does that */
10541 if (G_saved_tty_pgrp && getpid() == G.root_pid)
10542 tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp);
10544 /* Saved-redirect fds, script fds and G_interactive_fd are still
10545 * open here. However, they are all CLOEXEC, and execv below
10546 * closes them. Try interactive "exec ls -l /proc/self/fd",
10547 * it should show no extra open fds in the "ls" process.
10548 * If we'd try to run builtins/NOEXECs, this would need improving.
10550 //close_saved_fds_and_FILE_fds();
10552 /* TODO: if exec fails, bash does NOT exit! We do.
10553 * We'll need to undo trap cleanup (it's inside execvp_or_die)
10554 * and tcsetpgrp, and this is inherently racy.
10556 execvp_or_die(argv);
10559 static int FAST_FUNC builtin_exit(char **argv)
10561 debug_printf_exec("%s()\n", __func__);
10563 /* interactive bash:
10564 * # trap "echo EEE" EXIT
10567 * There are stopped jobs.
10568 * (if there are _stopped_ jobs, running ones don't count)
10571 * EEE (then bash exits)
10573 * TODO: we can use G.exiting = -1 as indicator "last cmd was exit"
10576 /* note: EXIT trap is run by hush_exit */
10577 argv = skip_dash_dash(argv);
10578 if (argv[0] == NULL)
10579 hush_exit(G.last_exitcode);
10580 /* mimic bash: exit 123abc == exit 255 + error msg */
10581 xfunc_error_retval = 255;
10582 /* bash: exit -2 == exit 254, no error msg */
10583 hush_exit(xatoi(argv[0]) & 0xff);
10586 #if ENABLE_HUSH_TYPE
10587 /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */
10588 static int FAST_FUNC builtin_type(char **argv)
10590 int ret = EXIT_SUCCESS;
10596 if (0) {} /* make conditional compile easier below */
10597 /*else if (find_alias(*argv))
10598 type = "an alias";*/
10599 # if ENABLE_HUSH_FUNCTIONS
10600 else if (find_function(*argv))
10601 type = "a function";
10603 else if (find_builtin(*argv))
10604 type = "a shell builtin";
10605 else if ((path = find_in_path(*argv)) != NULL)
10608 bb_error_msg("type: %s: not found", *argv);
10609 ret = EXIT_FAILURE;
10613 printf("%s is %s\n", *argv, type);
10621 #if ENABLE_HUSH_READ
10622 /* Interruptibility of read builtin in bash
10623 * (tested on bash-4.2.8 by sending signals (not by ^C)):
10625 * Empty trap makes read ignore corresponding signal, for any signal.
10628 * - terminates non-interactive shell;
10629 * - interrupts read in interactive shell;
10630 * if it has non-empty trap:
10631 * - executes trap and returns to command prompt in interactive shell;
10632 * - executes trap and returns to read in non-interactive shell;
10634 * - is ignored (does not interrupt) read in interactive shell;
10635 * - terminates non-interactive shell;
10636 * if it has non-empty trap:
10637 * - executes trap and returns to read;
10639 * - terminates shell (regardless of interactivity);
10640 * if it has non-empty trap:
10641 * - executes trap and returns to read;
10642 * SIGCHLD from children:
10643 * - does not interrupt read regardless of interactivity:
10644 * try: sleep 1 & read x; echo $x
10646 static int FAST_FUNC builtin_read(char **argv)
10649 struct builtin_read_params params;
10651 memset(¶ms, 0, sizeof(params));
10653 /* "!": do not abort on errors.
10654 * Option string must start with "sr" to match BUILTIN_READ_xxx
10656 params.read_flags = getopt32(argv,
10658 "!srn:p:t:u:d:", ¶ms.opt_n, ¶ms.opt_p, ¶ms.opt_t, ¶ms.opt_u, ¶ms.opt_d
10660 "!srn:p:t:u:", ¶ms.opt_n, ¶ms.opt_p, ¶ms.opt_t, ¶ms.opt_u
10663 if ((uint32_t)params.read_flags == (uint32_t)-1)
10664 return EXIT_FAILURE;
10666 params.argv = argv;
10667 params.setvar = set_local_var_from_halves;
10668 params.ifs = get_local_var_value("IFS"); /* can be NULL */
10671 r = shell_builtin_read(¶ms);
10673 if ((uintptr_t)r == 1 && errno == EINTR) {
10674 unsigned sig = check_and_run_traps();
10679 if ((uintptr_t)r > 1) {
10680 bb_simple_error_msg(r);
10681 r = (char*)(uintptr_t)1;
10684 return (uintptr_t)r;
10688 #if ENABLE_HUSH_UMASK
10689 static int FAST_FUNC builtin_umask(char **argv)
10696 argv = skip_dash_dash(argv);
10698 mode_t old_mask = mask;
10700 /* numeric umasks are taken as-is */
10701 /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */
10702 if (!isdigit(argv[0][0]))
10704 mask = bb_parse_mode(argv[0], mask);
10705 if (!isdigit(argv[0][0]))
10707 if ((unsigned)mask > 0777) {
10710 * bash: umask: 'q': invalid symbolic mode operator
10711 * bash: umask: 999: octal number out of range
10713 bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]);
10718 printf("%04o\n", (unsigned) mask);
10719 /* fall through and restore mask which we set to 0 */
10723 return !rc; /* rc != 0 - success */
10727 #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_TRAP
10728 static void print_escaped(const char *s)
10733 const char *p = strchrnul(s, '\'');
10734 /* print 'xxxx', possibly just '' */
10735 printf("'%.*s'", (int)(p - s), s);
10740 /* s points to '; print "'''...'''" */
10742 do putchar('\''); while (*++s == '\'');
10748 #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_LOCAL || ENABLE_HUSH_READONLY
10749 static int helper_export_local(char **argv, unsigned flags)
10752 char *name = *argv;
10753 const char *name_end = endofname(name);
10755 if (*name_end == '\0') {
10756 struct variable *var, **vpp;
10758 vpp = get_ptr_to_local_var(name, name_end - name);
10759 var = vpp ? *vpp : NULL;
10761 if (flags & SETFLAG_UNEXPORT) {
10762 /* export -n NAME (without =VALUE) */
10764 var->flg_export = 0;
10765 debug_printf_env("%s: unsetenv '%s'\n", __func__, name);
10767 } /* else: export -n NOT_EXISTING_VAR: no-op */
10770 if (flags & SETFLAG_EXPORT) {
10771 /* export NAME (without =VALUE) */
10773 var->flg_export = 1;
10774 debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr);
10775 putenv(var->varstr);
10779 if (flags & SETFLAG_MAKE_RO) {
10780 /* readonly NAME (without =VALUE) */
10782 var->flg_read_only = 1;
10786 # if ENABLE_HUSH_LOCAL
10787 /* Is this "local" bltin? */
10788 if (!(flags & (SETFLAG_EXPORT|SETFLAG_UNEXPORT|SETFLAG_MAKE_RO))) {
10789 unsigned lvl = flags >> SETFLAG_VARLVL_SHIFT;
10790 if (var && var->var_nest_level == lvl) {
10791 /* "local x=abc; ...; local x" - ignore second local decl */
10796 /* Exporting non-existing variable.
10797 * bash does not put it in environment,
10798 * but remembers that it is exported,
10799 * and does put it in env when it is set later.
10800 * We just set it to "" and export.
10802 /* Or, it's "local NAME" (without =VALUE).
10803 * bash sets the value to "".
10805 /* Or, it's "readonly NAME" (without =VALUE).
10806 * bash remembers NAME and disallows its creation
10809 name = xasprintf("%s=", name);
10811 if (*name_end != '=') {
10812 bb_error_msg("'%s': bad variable name", name);
10813 /* do not parse following argv[]s: */
10816 /* (Un)exporting/making local NAME=VALUE */
10817 name = xstrdup(name);
10818 /* Testcase: export PS1='\w \$ ' */
10821 debug_printf_env("%s: set_local_var('%s')\n", __func__, name);
10822 if (set_local_var(name, flags))
10823 return EXIT_FAILURE;
10825 return EXIT_SUCCESS;
10829 #if ENABLE_HUSH_EXPORT
10830 static int FAST_FUNC builtin_export(char **argv)
10832 unsigned opt_unexport;
10834 # if ENABLE_HUSH_EXPORT_N
10835 /* "!": do not abort on errors */
10836 opt_unexport = getopt32(argv, "!n");
10837 if (opt_unexport == (uint32_t)-1)
10838 return EXIT_FAILURE;
10845 if (argv[0] == NULL) {
10846 char **e = environ;
10852 /* ash emits: export VAR='VAL'
10853 * bash: declare -x VAR="VAL"
10854 * we follow ash example */
10855 const char *s = *e++;
10856 const char *p = strchr(s, '=');
10858 if (!p) /* wtf? take next variable */
10861 printf("export %.*s", (int)(p - s) + 1, s);
10862 print_escaped(p + 1);
10866 /*fflush_all(); - done after each builtin anyway */
10868 return EXIT_SUCCESS;
10871 return helper_export_local(argv, opt_unexport ? SETFLAG_UNEXPORT : SETFLAG_EXPORT);
10875 #if ENABLE_HUSH_LOCAL
10876 static int FAST_FUNC builtin_local(char **argv)
10878 if (G.func_nest_level == 0) {
10879 bb_error_msg("%s: not in a function", argv[0]);
10880 return EXIT_FAILURE; /* bash compat */
10883 /* Since all builtins run in a nested variable level,
10884 * need to use level - 1 here. Or else the variable will be removed at once
10885 * after builtin returns.
10887 return helper_export_local(argv, (G.var_nest_level - 1) << SETFLAG_VARLVL_SHIFT);
10891 #if ENABLE_HUSH_READONLY
10892 static int FAST_FUNC builtin_readonly(char **argv)
10895 if (*argv == NULL) {
10896 /* bash: readonly [-p]: list all readonly VARs
10897 * (-p has no effect in bash)
10899 struct variable *e;
10900 for (e = G.top_var; e; e = e->next) {
10901 if (e->flg_read_only) {
10902 //TODO: quote value: readonly VAR='VAL'
10903 printf("readonly %s\n", e->varstr);
10906 return EXIT_SUCCESS;
10908 return helper_export_local(argv, SETFLAG_MAKE_RO);
10912 #if ENABLE_HUSH_UNSET
10913 /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */
10914 static int FAST_FUNC builtin_unset(char **argv)
10919 /* "!": do not abort on errors */
10920 /* "+": stop at 1st non-option */
10921 opts = getopt32(argv, "!+vf");
10922 if (opts == (unsigned)-1)
10923 return EXIT_FAILURE;
10925 bb_simple_error_msg("unset: -v and -f are exclusive");
10926 return EXIT_FAILURE;
10930 ret = EXIT_SUCCESS;
10932 if (!(opts & 2)) { /* not -f */
10933 if (unset_local_var(*argv)) {
10934 /* unset <nonexistent_var> doesn't fail.
10935 * Error is when one tries to unset RO var.
10936 * Message was printed by unset_local_var. */
10937 ret = EXIT_FAILURE;
10940 # if ENABLE_HUSH_FUNCTIONS
10951 #if ENABLE_HUSH_SET
10952 /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set
10953 * built-in 'set' handler
10955 * set [-abCefhmnuvx] [-o option] [argument...]
10956 * set [+abCefhmnuvx] [+o option] [argument...]
10957 * set -- [argument...]
10960 * Implementations shall support the options in both their hyphen and
10961 * plus-sign forms. These options can also be specified as options to sh.
10963 * Write out all variables and their values: set
10964 * Set $1, $2, and $3 and set "$#" to 3: set c a b
10965 * Turn on the -x and -v options: set -xv
10966 * Unset all positional parameters: set --
10967 * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x"
10968 * Set the positional parameters to the expansion of x, even if x expands
10969 * with a leading '-' or '+': set -- $x
10971 * So far, we only support "set -- [argument...]" and some of the short names.
10973 static int FAST_FUNC builtin_set(char **argv)
10976 char **pp, **g_argv;
10977 char *arg = *++argv;
10980 struct variable *e;
10981 for (e = G.top_var; e; e = e->next)
10983 return EXIT_SUCCESS;
10987 if (strcmp(arg, "--") == 0) {
10991 if (arg[0] != '+' && arg[0] != '-')
10993 for (n = 1; arg[n]; ++n) {
10994 if (set_mode((arg[0] == '-'), arg[n], argv[1])) {
10995 bb_error_msg("%s: %s: invalid option", "set", arg);
10996 return EXIT_FAILURE;
10998 if (arg[n] == 'o' && argv[1])
11001 } while ((arg = *++argv) != NULL);
11002 /* Now argv[0] is 1st argument */
11005 return EXIT_SUCCESS;
11008 /* NB: G.global_argv[0] ($0) is never freed/changed */
11009 g_argv = G.global_argv;
11010 if (G.global_args_malloced) {
11016 G.global_args_malloced = 1;
11017 pp = xzalloc(sizeof(pp[0]) * 2);
11018 pp[0] = g_argv[0]; /* retain $0 */
11021 /* This realloc's G.global_argv */
11022 G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1);
11024 G.global_argc = 1 + string_array_len(pp + 1);
11026 return EXIT_SUCCESS;
11030 static int FAST_FUNC builtin_shift(char **argv)
11033 argv = skip_dash_dash(argv);
11035 n = bb_strtou(argv[0], NULL, 10);
11036 if (errno || n < 0) {
11037 /* shared string with ash.c */
11038 bb_error_msg("Illegal number: %s", argv[0]);
11040 * ash aborts in this case.
11041 * bash prints error message and set $? to 1.
11042 * Interestingly, for "shift 99999" bash does not
11043 * print error message, but does set $? to 1
11044 * (and does no shifting at all).
11048 if (n >= 0 && n < G.global_argc) {
11049 if (G_global_args_malloced) {
11052 free(G.global_argv[m++]);
11054 G.global_argc -= n;
11055 memmove(&G.global_argv[1], &G.global_argv[n+1],
11056 G.global_argc * sizeof(G.global_argv[0]));
11057 return EXIT_SUCCESS;
11059 return EXIT_FAILURE;
11062 #if ENABLE_HUSH_GETOPTS
11063 static int FAST_FUNC builtin_getopts(char **argv)
11065 /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html
11068 If a required argument is not found, and getopts is not silent,
11069 a question mark (?) is placed in VAR, OPTARG is unset, and a
11070 diagnostic message is printed. If getopts is silent, then a
11071 colon (:) is placed in VAR and OPTARG is set to the option
11074 Test that VAR is a valid variable name?
11076 "Whenever the shell is invoked, OPTIND shall be initialized to 1"
11079 const char *cp, *optstring, *var;
11080 int c, n, exitcode, my_opterr;
11083 optstring = *++argv;
11084 if (!optstring || !(var = *++argv)) {
11085 bb_simple_error_msg("usage: getopts OPTSTRING VAR [ARGS]");
11086 return EXIT_FAILURE;
11090 argv[0] = G.global_argv[0]; /* for error messages in getopt() */
11092 argv = G.global_argv;
11096 if (optstring[0] != ':') {
11097 cp = get_local_var_value("OPTERR");
11098 /* 0 if "OPTERR=0", 1 otherwise */
11099 my_opterr = (!cp || NOT_LONE_CHAR(cp, '0'));
11102 /* getopts stops on first non-option. Add "+" to force that */
11103 /*if (optstring[0] != '+')*/ {
11104 char *s = alloca(strlen(optstring) + 2);
11105 sprintf(s, "+%s", optstring);
11109 /* Naively, now we should just
11110 * cp = get_local_var_value("OPTIND");
11111 * optind = cp ? atoi(cp) : 0;
11113 * opterr = my_opterr;
11114 * c = getopt(string_array_len(argv), argv, optstring);
11115 * and be done? Not so fast...
11116 * Unlike normal getopt() usage in C programs, here
11117 * each successive call will (usually) have the same argv[] CONTENTS,
11118 * but not the ADDRESSES. Worse yet, it's possible that between
11119 * invocations of "getopts", there will be calls to shell builtins
11120 * which use getopt() internally. Example:
11121 * while getopts "abc" RES -a -bc -abc de; do
11124 * This would not work correctly: getopt() call inside "unset"
11125 * modifies internal libc state which is tracking position in
11126 * multi-option strings ("-abc"). At best, it can skip options
11127 * or return the same option infinitely. With glibc implementation
11128 * of getopt(), it would use outright invalid pointers and return
11129 * garbage even _without_ "unset" mangling internal state.
11131 * We resort to resetting getopt() state and calling it N times,
11132 * until we get Nth result (or failure).
11133 * (N == G.getopt_count is reset to 0 whenever OPTIND is [un]set).
11137 n = string_array_len(argv);
11140 opterr = (count < G.getopt_count) ? 0 : my_opterr;
11141 c = getopt(n, argv, optstring);
11145 } while (count <= G.getopt_count);
11147 /* Set OPTIND. Prevent resetting of the magic counter! */
11148 set_local_var_from_halves("OPTIND", utoa(optind));
11149 G.getopt_count = count; /* "next time, give me N+1'th result" */
11150 GETOPT_RESET(); /* just in case */
11153 /* Always set or unset, never left as-is, even on exit/error:
11154 * "If no option was found, or if the option that was found
11155 * does not have an option-argument, OPTARG shall be unset."
11159 /* If ":optstring" and unknown option is seen,
11160 * it is stored to OPTARG.
11162 if (optstring[1] == ':') {
11168 set_local_var_from_halves("OPTARG", cp);
11170 unset_local_var("OPTARG");
11172 /* Convert -1 to "?" */
11173 exitcode = EXIT_SUCCESS;
11174 if (c < 0) { /* -1: end of options */
11175 exitcode = EXIT_FAILURE;
11181 set_local_var_from_halves(var, cbuf);
11187 static int FAST_FUNC builtin_source(char **argv)
11189 char *arg_path, *filename;
11192 char *args_need_save;
11193 #if ENABLE_HUSH_FUNCTIONS
11197 argv = skip_dash_dash(argv);
11198 filename = argv[0];
11200 /* bash says: "bash: .: filename argument required" */
11201 return 2; /* bash compat */
11204 if (!strchr(filename, '/')) {
11205 arg_path = find_in_path(filename);
11207 filename = arg_path;
11208 else if (!ENABLE_HUSH_BASH_SOURCE_CURDIR) {
11210 bb_simple_perror_msg(filename);
11211 return EXIT_FAILURE;
11214 input = hfopen(filename);
11217 bb_perror_msg("%s", filename);
11218 /* POSIX: non-interactive shell should abort here,
11219 * not merely fail. So far no one complained :)
11221 return EXIT_FAILURE;
11224 #if ENABLE_HUSH_FUNCTIONS
11225 sv_flg = G_flag_return_in_progress;
11226 /* "we are inside sourced file, ok to use return" */
11227 G_flag_return_in_progress = -1;
11229 args_need_save = argv[1]; /* used as a boolean variable */
11230 if (args_need_save)
11231 save_and_replace_G_args(&sv, argv);
11233 /* "false; . ./empty_line; echo Zero:$?" should print 0 */
11234 G.last_exitcode = 0;
11235 parse_and_run_file(input);
11238 if (args_need_save) /* can't use argv[1] instead: "shift" can mangle it */
11239 restore_G_args(&sv, argv);
11240 #if ENABLE_HUSH_FUNCTIONS
11241 G_flag_return_in_progress = sv_flg;
11244 return G.last_exitcode;
11247 #if ENABLE_HUSH_TRAP
11248 static int FAST_FUNC builtin_trap(char **argv)
11254 G_traps = xzalloc(sizeof(G_traps[0]) * NSIG);
11259 /* No args: print all trapped */
11260 for (i = 0; i < NSIG; ++i) {
11262 printf("trap -- ");
11263 print_escaped(G_traps[i]);
11264 /* note: bash adds "SIG", but only if invoked
11265 * as "bash". If called as "sh", or if set -o posix,
11266 * then it prints short signal names.
11267 * We are printing short names: */
11268 printf(" %s\n", get_signame(i));
11271 /*fflush_all(); - done after each builtin anyway */
11272 return EXIT_SUCCESS;
11276 /* If first arg is a number: reset all specified signals */
11277 sig = bb_strtou(*argv, NULL, 10);
11281 ret = EXIT_SUCCESS;
11283 sighandler_t handler;
11285 sig = get_signum(*argv++);
11287 ret = EXIT_FAILURE;
11288 /* Mimic bash message exactly */
11289 bb_error_msg("trap: %s: invalid signal specification", argv[-1]);
11293 free(G_traps[sig]);
11294 G_traps[sig] = xstrdup(new_cmd);
11296 debug_printf("trap: setting SIG%s (%i) to '%s'\n",
11297 get_signame(sig), sig, G_traps[sig]);
11299 /* There is no signal for 0 (EXIT) */
11304 handler = (new_cmd[0] ? record_pending_signo : SIG_IGN);
11306 /* We are removing trap handler */
11307 handler = pick_sighandler(sig);
11308 install_sighandler(sig, handler);
11313 if (!argv[1]) { /* no second arg */
11314 bb_simple_error_msg("trap: invalid arguments");
11315 return EXIT_FAILURE;
11318 /* First arg is "-": reset all specified to default */
11319 /* First arg is "--": skip it, the rest is "handler SIGs..." */
11320 /* Everything else: set arg as signal handler
11321 * (includes "" case, which ignores signal) */
11322 if (argv[0][0] == '-') {
11323 if (argv[0][1] == '\0') { /* "-" */
11324 /* new_cmd remains NULL: "reset these sigs" */
11327 if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */
11330 /* else: "-something", no special meaning */
11335 goto process_sig_list;
11339 #if ENABLE_HUSH_JOB
11340 static struct pipe *parse_jobspec(const char *str)
11345 if (sscanf(str, "%%%u", &jobnum) != 1) {
11347 || (str[1] != '%' && str[1] != '+' && str[1] != '\0')
11349 bb_error_msg("bad argument '%s'", str);
11352 /* It is "%%", "%+" or "%" - current job */
11353 jobnum = G.last_jobid;
11355 bb_simple_error_msg("no current job");
11359 for (pi = G.job_list; pi; pi = pi->next) {
11360 if (pi->jobid == jobnum) {
11364 bb_error_msg("%u: no such job", jobnum);
11368 static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM)
11371 const char *status_string;
11373 checkjobs(NULL, 0 /*(no pid to wait for)*/);
11374 for (job = G.job_list; job; job = job->next) {
11375 if (job->alive_cmds == job->stopped_cmds)
11376 status_string = "Stopped";
11378 status_string = "Running";
11380 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
11383 clean_up_last_dead_job();
11385 return EXIT_SUCCESS;
11388 /* built-in 'fg' and 'bg' handler */
11389 static int FAST_FUNC builtin_fg_bg(char **argv)
11394 if (!G_interactive_fd)
11395 return EXIT_FAILURE;
11397 /* If they gave us no args, assume they want the last backgrounded task */
11399 for (pi = G.job_list; pi; pi = pi->next) {
11400 if (pi->jobid == G.last_jobid) {
11404 bb_error_msg("%s: no current job", argv[0]);
11405 return EXIT_FAILURE;
11408 pi = parse_jobspec(argv[1]);
11410 return EXIT_FAILURE;
11412 /* TODO: bash prints a string representation
11413 * of job being foregrounded (like "sleep 1 | cat") */
11414 if (argv[0][0] == 'f' && G_saved_tty_pgrp) {
11415 /* Put the job into the foreground. */
11416 tcsetpgrp(G_interactive_fd, pi->pgrp);
11419 /* Restart the processes in the job */
11420 debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp);
11421 for (i = 0; i < pi->num_cmds; i++) {
11422 debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid);
11424 pi->stopped_cmds = 0;
11426 i = kill(- pi->pgrp, SIGCONT);
11428 if (errno == ESRCH) {
11429 delete_finished_job(pi);
11430 return EXIT_SUCCESS;
11432 bb_simple_perror_msg("kill (SIGCONT)");
11435 if (argv[0][0] == 'f') {
11436 remove_job_from_table(pi); /* FG job shouldn't be in job table */
11437 return checkjobs_and_fg_shell(pi);
11439 return EXIT_SUCCESS;
11443 #if ENABLE_HUSH_KILL
11444 static int FAST_FUNC builtin_kill(char **argv)
11448 # if ENABLE_HUSH_JOB
11449 if (argv[1] && strcmp(argv[1], "-l") != 0) {
11457 if (argv[i][0] != '%')
11460 * "kill %N" - job kill
11461 * Converting to pgrp / pid kill
11463 pi = parse_jobspec(argv[i]);
11465 /* Eat bad jobspec */
11469 argv[j - 1] = argv[j];
11476 * In jobs started under job control, we signal
11477 * entire process group by kill -PGRP_ID.
11478 * This happens, f.e., in interactive shell.
11480 * Otherwise, we signal each child via
11481 * kill PID1 PID2 PID3.
11483 * sh -c 'sleep 1|sleep 1 & kill %1'
11484 * sh -c 'true|sleep 2 & sleep 1; kill %1'
11485 * sh -c 'true|sleep 1 & sleep 2; kill %1'
11487 n = G_interactive_fd ? 1 : pi->num_cmds;
11488 dst = alloca(n * sizeof(int)*4);
11490 if (G_interactive_fd)
11491 dst += sprintf(dst, " -%u", (int)pi->pgrp);
11492 else for (j = 0; j < n; j++) {
11493 struct command *cmd = &pi->cmds[j];
11494 /* Skip exited members of the job */
11498 * kill_main has matching code to expect
11499 * leading space. Needed to not confuse
11500 * negative pids with "kill -SIGNAL_NO" syntax
11502 dst += sprintf(dst, " %u", (int)cmd->pid);
11505 } while (argv[++i]);
11509 if (argv[1] || ret == 0) {
11510 ret = run_applet_main(argv, kill_main);
11512 /* else: ret = 1, "kill %bad_jobspec" case */
11517 #if ENABLE_HUSH_WAIT
11518 /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */
11519 # if !ENABLE_HUSH_JOB
11520 # define wait_for_child_or_signal(pipe,pid) wait_for_child_or_signal(pid)
11522 static int wait_for_child_or_signal(struct pipe *waitfor_pipe, pid_t waitfor_pid)
11529 if (!sigisemptyset(&G.pending_set))
11532 /* waitpid is not interruptible by SA_RESTARTed
11533 * signals which we use. Thus, this ugly dance:
11536 /* Make sure possible SIGCHLD is stored in kernel's
11537 * pending signal mask before we call waitpid.
11538 * Or else we may race with SIGCHLD, lose it,
11539 * and get stuck in sigsuspend...
11541 sigfillset(&oldset); /* block all signals, remember old set */
11542 sigprocmask2(SIG_SETMASK, &oldset);
11544 if (!sigisemptyset(&G.pending_set)) {
11545 /* Crap! we raced with some signal! */
11549 /*errno = 0; - checkjobs does this */
11550 /* Can't pass waitfor_pipe into checkjobs(): it won't be interruptible */
11551 ret = checkjobs(NULL, waitfor_pid); /* waitpid(WNOHANG) inside */
11552 debug_printf_exec("checkjobs:%d\n", ret);
11553 # if ENABLE_HUSH_JOB
11554 if (waitfor_pipe) {
11555 int rcode = job_exited_or_stopped(waitfor_pipe);
11556 debug_printf_exec("job_exited_or_stopped:%d\n", rcode);
11559 sigprocmask(SIG_SETMASK, &oldset, NULL);
11564 /* if ECHILD, there are no children (ret is -1 or 0) */
11565 /* if ret == 0, no children changed state */
11566 /* if ret != 0, it's exitcode+1 of exited waitfor_pid child */
11567 if (errno == ECHILD || ret) {
11569 if (ret < 0) /* if ECHILD, may need to fix "ret" */
11571 # if ENABLE_HUSH_BASH_COMPAT
11572 if (waitfor_pid == -1 && errno == ECHILD) {
11573 /* exitcode of "wait -n" with no children is 127, not 0 */
11577 sigprocmask(SIG_SETMASK, &oldset, NULL);
11580 /* Wait for SIGCHLD or any other signal */
11581 /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */
11582 /* Note: sigsuspend invokes signal handler */
11583 sigsuspend(&oldset);
11584 /* ^^^ add "sigdelset(&oldset, SIGCHLD)" before sigsuspend
11585 * to make sure SIGCHLD is not masked off?
11586 * It was reported that this:
11587 * fn() { : | return; }
11588 * shopt -s lastpipe
11591 * under bash 4.4.23 runs SCRIPT with SIGCHLD masked,
11592 * making "wait" commands in SCRIPT block forever.
11595 sigprocmask(SIG_SETMASK, &oldset, NULL);
11597 /* So, did we get a signal? */
11598 sig = check_and_run_traps();
11599 if (sig /*&& sig != SIGCHLD - always true */) {
11600 /* Do this for any (non-ignored) signal, not only for ^C */
11604 /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */
11609 static int FAST_FUNC builtin_wait(char **argv)
11614 argv = skip_dash_dash(argv);
11615 # if ENABLE_HUSH_BASH_COMPAT
11616 if (argv[0] && strcmp(argv[0], "-n") == 0) {
11618 /* (bash accepts "wait -n PID" too and ignores PID) */
11619 G.dead_job_exitcode = -1;
11620 return wait_for_child_or_signal(NULL, -1 /*no job, wait for one job*/);
11623 if (argv[0] == NULL) {
11624 /* Don't care about wait results */
11625 /* Note 1: must wait until there are no more children */
11626 /* Note 2: must be interruptible */
11628 * $ sleep 3 & sleep 6 & wait
11629 * [1] 30934 sleep 3
11630 * [2] 30935 sleep 6
11633 * $ sleep 3 & sleep 6 & wait
11634 * [1] 30936 sleep 3
11635 * [2] 30937 sleep 6
11637 * ^C <-- after ~4 sec from keyboard
11640 return wait_for_child_or_signal(NULL, 0 /*no job and no pid to wait for*/);
11644 pid_t pid = bb_strtou(*argv, NULL, 10);
11645 if (errno || pid <= 0) {
11646 # if ENABLE_HUSH_JOB
11647 if (argv[0][0] == '%') {
11648 struct pipe *wait_pipe;
11649 ret = 127; /* bash compat for bad jobspecs */
11650 wait_pipe = parse_jobspec(*argv);
11652 ret = job_exited_or_stopped(wait_pipe);
11654 ret = wait_for_child_or_signal(wait_pipe, 0);
11656 /* waiting on "last dead job" removes it */
11657 clean_up_last_dead_job();
11660 /* else: parse_jobspec() already emitted error msg */
11664 /* mimic bash message */
11665 bb_error_msg("wait: '%s': not a pid or valid job spec", *argv);
11666 ret = EXIT_FAILURE;
11667 continue; /* bash checks all argv[] */
11670 /* Do we have such child? */
11671 ret = waitpid(pid, &status, WNOHANG);
11675 if (errno == ECHILD) {
11676 if (pid == G.last_bg_pid) {
11677 /* "wait $!" but last bg task has already exited. Try:
11678 * (sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $?
11679 * In bash it prints exitcode 0, then 3.
11680 * In dash, it is 127.
11682 ret = G.last_bg_pid_exitcode;
11684 /* Example: "wait 1". mimic bash message */
11685 bb_error_msg("wait: pid %u is not a child of this shell", (unsigned)pid);
11689 bb_perror_msg("wait %s", *argv);
11691 continue; /* bash checks all argv[] */
11694 /* Yes, and it still runs */
11695 ret = wait_for_child_or_signal(NULL, pid);
11697 /* Yes, and it just exited */
11698 process_wait_result(NULL, pid, status);
11699 ret = WEXITSTATUS(status);
11700 if (WIFSIGNALED(status))
11701 ret = 128 + WTERMSIG(status);
11709 #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS
11710 static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min)
11713 def = bb_strtou(argv[1], NULL, 10);
11714 if (errno || def < def_min || argv[2]) {
11715 bb_error_msg("%s: bad arguments", argv[0]);
11723 #if ENABLE_HUSH_LOOPS
11724 static int FAST_FUNC builtin_break(char **argv)
11727 if (G.depth_of_loop == 0) {
11728 bb_error_msg("%s: only meaningful in a loop", argv[0]);
11729 /* if we came from builtin_continue(), need to undo "= 1" */
11730 G.flag_break_continue = 0;
11731 return EXIT_SUCCESS; /* bash compat */
11733 G.flag_break_continue++; /* BC_BREAK = 1, or BC_CONTINUE = 2 */
11735 G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1);
11736 if (depth == UINT_MAX)
11737 G.flag_break_continue = BC_BREAK;
11738 if (G.depth_of_loop < depth)
11739 G.depth_break_continue = G.depth_of_loop;
11741 return EXIT_SUCCESS;
11744 static int FAST_FUNC builtin_continue(char **argv)
11746 G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */
11747 return builtin_break(argv);
11751 #if ENABLE_HUSH_FUNCTIONS
11752 static int FAST_FUNC builtin_return(char **argv)
11756 if (G_flag_return_in_progress != -1) {
11757 bb_error_msg("%s: not in a function or sourced script", argv[0]);
11758 return EXIT_FAILURE; /* bash compat */
11761 G_flag_return_in_progress = 1;
11764 * out of range: wraps around at 256, does not error out
11765 * non-numeric param:
11766 * f() { false; return qwe; }; f; echo $?
11767 * bash: return: qwe: numeric argument required <== we do this
11768 * 255 <== we also do this
11770 rc = parse_numeric_argv1(argv, G.last_exitcode, 0);
11771 # if ENABLE_HUSH_TRAP
11772 if (argv[1]) { /* "return ARG" inside a running trap sets $? */
11773 debug_printf_exec("G.return_exitcode=%d\n", rc);
11774 G.return_exitcode = rc;
11781 #if ENABLE_HUSH_TIMES
11782 static int FAST_FUNC builtin_times(char **argv UNUSED_PARAM)
11784 static const uint8_t times_tbl[] ALIGN1 = {
11785 ' ', offsetof(struct tms, tms_utime),
11786 '\n', offsetof(struct tms, tms_stime),
11787 ' ', offsetof(struct tms, tms_cutime),
11788 '\n', offsetof(struct tms, tms_cstime),
11795 clk_tck = bb_clk_tck();
11800 unsigned sec, frac;
11802 t = *(clock_t *)(((char *) &buf) + p[1]);
11804 frac = t % clk_tck;
11805 printf("%um%u.%03us%c",
11806 sec / 60, sec % 60,
11807 (frac * 1000) / clk_tck,
11812 return EXIT_SUCCESS;
11816 #if ENABLE_HUSH_MEMLEAK
11817 static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM)
11822 # ifdef M_TRIM_THRESHOLD
11823 /* Optional. Reduces probability of false positives */
11826 /* Crude attempt to find where "free memory" starts,
11827 * sans fragmentation. */
11829 l = (unsigned long)p;
11832 if (l < (unsigned long)p) l = (unsigned long)p;
11838 struct mallinfo mi = mallinfo();
11839 printf("top alloc:0x%lx malloced:%d+%d=%d\n", l,
11840 mi.arena, mi.hblkhd, mi.arena + mi.hblkhd);
11844 if (!G.memleak_value)
11845 G.memleak_value = l;
11847 l -= G.memleak_value;
11854 /* Exitcode is "how many kilobytes we leaked since 1st call" */