Spelling fixes in comments, documentation, tests and examples
[oweals/busybox.git] / shell / hush.c
index 8054d1d4561ec2abf3047179835f72f4281a3c2c..e18920f508f5b98daf5963c3fb7c9697d727c8a8 100644 (file)
  *              $ "export" i=`echo 'aaa  bbb'`; echo "$i"
  *              aaa
  */
-#if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
-       || defined(__APPLE__) \
-    )
-# include <malloc.h>   /* for malloc_trim */
-#endif
-#include <glob.h>
-/* #include <dmalloc.h> */
-#if ENABLE_HUSH_CASE
-# include <fnmatch.h>
-#endif
-#include <sys/utsname.h> /* for setting $HOSTNAME */
-
-#include "busybox.h"  /* for APPLET_IS_NOFORK/NOEXEC */
-#include "unicode.h"
-#include "shell_common.h"
-#include "math.h"
-#include "match.h"
-#if ENABLE_HUSH_RANDOM_SUPPORT
-# include "random.h"
-#else
-# define CLEAR_RANDOM_T(rnd) ((void)0)
-#endif
-#ifndef F_DUPFD_CLOEXEC
-# define F_DUPFD_CLOEXEC F_DUPFD
-#endif
-#ifndef PIPE_BUF
-# define PIPE_BUF 4096  /* amount of buffering in a pipe */
-#endif
-
 //config:config HUSH
 //config:      bool "hush"
 //config:      default y
 //config:config HUSH_BASH_COMPAT
 //config:      bool "bash-compatible extensions"
 //config:      default y
-//config:      depends on HUSH
-//config:      help
-//config:        Enable bash-compatible extensions.
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
 //config:
 //config:config HUSH_BRACE_EXPANSION
 //config:      bool "Brace expansion"
 //config:      help
 //config:        Enable {abc,def} extension.
 //config:
-//config:config HUSH_HELP
-//config:      bool "help builtin"
-//config:      default y
-//config:      depends on HUSH
-//config:      help
-//config:        Enable help builtin in hush. Code size + ~1 kbyte.
-//config:
 //config:config HUSH_INTERACTIVE
 //config:      bool "Interactive mode"
 //config:      default y
-//config:      depends on HUSH
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
 //config:      help
 //config:        Enable interactive mode (prompt and command editing).
 //config:        Without this, hush simply reads and executes commands
 //config:      bool "Save command history to .hush_history"
 //config:      default y
 //config:      depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY
-//config:      help
-//config:        Enable history saving in hush.
 //config:
 //config:config HUSH_JOB
 //config:      bool "Job control"
 //config:        but no separate process group is formed.
 //config:
 //config:config HUSH_TICK
-//config:      bool "Process substitution"
+//config:      bool "Support process substitution"
 //config:      default y
-//config:      depends on HUSH
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
 //config:      help
-//config:        Enable process substitution `command` and $(command) in hush.
+//config:        Enable `command` and $(command).
 //config:
 //config:config HUSH_IF
 //config:      bool "Support if/then/elif/else/fi"
 //config:      default y
-//config:      depends on HUSH
-//config:      help
-//config:        Enable if/then/elif/else/fi in hush.
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
 //config:
 //config:config HUSH_LOOPS
 //config:      bool "Support for, while and until loops"
 //config:      default y
-//config:      depends on HUSH
-//config:      help
-//config:        Enable for, while and until loops in hush.
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
 //config:
 //config:config HUSH_CASE
 //config:      bool "Support case ... esac statement"
 //config:      default y
-//config:      depends on HUSH
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
 //config:      help
-//config:        Enable case ... esac statement in hush. +400 bytes.
+//config:        Enable case ... esac statement. +400 bytes.
 //config:
 //config:config HUSH_FUNCTIONS
 //config:      bool "Support funcname() { commands; } syntax"
 //config:      default y
-//config:      depends on HUSH
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
 //config:      help
-//config:        Enable support for shell functions in hush. +800 bytes.
+//config:        Enable support for shell functions. +800 bytes.
 //config:
 //config:config HUSH_LOCAL
-//config:      bool "Support local builtin"
+//config:      bool "local builtin"
 //config:      default y
 //config:      depends on HUSH_FUNCTIONS
 //config:      help
 //config:config HUSH_RANDOM_SUPPORT
 //config:      bool "Pseudorandom generator and $RANDOM variable"
 //config:      default y
-//config:      depends on HUSH
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
 //config:      help
 //config:        Enable pseudorandom generator and dynamic variable "$RANDOM".
 //config:        Each read of "$RANDOM" will generate a new pseudorandom value.
 //config:
+//config:config HUSH_MODE_X
+//config:      bool "Support 'hush -x' option and 'set -x' command"
+//config:      default y
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:      help
+//config:        This instructs hush to print commands before execution.
+//config:        Adds ~300 bytes.
+//config:
+//config:config HUSH_ECHO
+//config:      bool "echo builtin"
+//config:      default y
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:
+//config:config HUSH_PRINTF
+//config:      bool "printf builtin"
+//config:      default y
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:
+//config:config HUSH_TEST
+//config:      bool "test builtin"
+//config:      default y
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:
+//config:config HUSH_HELP
+//config:      bool "help builtin"
+//config:      default y
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:
+//config:config HUSH_EXPORT
+//config:      bool "export builtin"
+//config:      default y
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:
 //config:config HUSH_EXPORT_N
 //config:      bool "Support 'export -n' option"
 //config:      default y
-//config:      depends on HUSH
+//config:      depends on HUSH_EXPORT
 //config:      help
 //config:        export -n unexports variables. It is a bash extension.
 //config:
-//config:config HUSH_MODE_X
-//config:      bool "Support 'hush -x' option and 'set -x' command"
+//config:config HUSH_KILL
+//config:      bool "kill builtin (supports kill %jobspec)"
 //config:      default y
-//config:      depends on HUSH
-//config:      help
-//config:        This instructs hush to print commands before execution.
-//config:        Adds ~300 bytes.
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:
+//config:config HUSH_WAIT
+//config:      bool "wait builtin"
+//config:      default y
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:
+//config:config HUSH_TRAP
+//config:      bool "trap builtin"
+//config:      default y
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:
+//config:config HUSH_TYPE
+//config:      bool "type builtin"
+//config:      default y
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:
+//config:config HUSH_READ
+//config:      bool "read builtin"
+//config:      default y
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:
+//config:config HUSH_SET
+//config:      bool "set builtin"
+//config:      default y
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:
+//config:config HUSH_UNSET
+//config:      bool "unset builtin"
+//config:      default y
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:
+//config:config HUSH_ULIMIT
+//config:      bool "ulimit builtin"
+//config:      default y
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:
+//config:config HUSH_UMASK
+//config:      bool "umask builtin"
+//config:      default y
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
+//config:
+//config:config HUSH_MEMLEAK
+//config:      bool "memleak builtin (debugging)"
+//config:      default n
+//config:      depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
 //config:
 //config:config MSH
 //config:      bool "msh (deprecated: aliased to hush)"
 //config:      select HUSH
 //config:      help
 //config:        msh is deprecated and will be removed, please migrate to hush.
-//config:
 
 //applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP))
-//applet:IF_MSH(APPLET(msh, BB_DIR_BIN, BB_SUID_DROP))
-//applet:IF_FEATURE_SH_IS_HUSH(APPLET_ODDNAME(sh, hush, BB_DIR_BIN, BB_SUID_DROP, sh))
-//applet:IF_FEATURE_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, BB_DIR_BIN, BB_SUID_DROP, bash))
+//                       APPLET_ODDNAME:name  main  location    suid_type     help
+//applet:IF_MSH(         APPLET_ODDNAME(msh,  hush, BB_DIR_BIN, BB_SUID_DROP, hush))
+//applet:IF_SH_IS_HUSH(  APPLET_ODDNAME(sh,   hush, BB_DIR_BIN, BB_SUID_DROP, hush))
+//applet:IF_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, BB_DIR_BIN, BB_SUID_DROP, hush))
 
 //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o
+//kbuild:lib-$(CONFIG_SH_IS_HUSH) += hush.o match.o shell_common.o
+//kbuild:lib-$(CONFIG_BASH_IS_HUSH) += hush.o match.o shell_common.o
 //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o
 
 /* -i (interactive) and -s (read stdin) are also accepted,
 //usage:#define hush_full_usage "\n\n"
 //usage:       "Unix shell interpreter"
 
-//usage:#define msh_trivial_usage hush_trivial_usage
-//usage:#define msh_full_usage hush_full_usage
+#if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
+       || defined(__APPLE__) \
+    )
+# include <malloc.h>   /* for malloc_trim */
+#endif
+#include <glob.h>
+/* #include <dmalloc.h> */
+#if ENABLE_HUSH_CASE
+# include <fnmatch.h>
+#endif
+#include <sys/utsname.h> /* for setting $HOSTNAME */
+
+#include "busybox.h"  /* for APPLET_IS_NOFORK/NOEXEC */
+#include "unicode.h"
+#include "shell_common.h"
+#include "math.h"
+#include "match.h"
+#if ENABLE_HUSH_RANDOM_SUPPORT
+# include "random.h"
+#else
+# define CLEAR_RANDOM_T(rnd) ((void)0)
+#endif
+#ifndef F_DUPFD_CLOEXEC
+# define F_DUPFD_CLOEXEC F_DUPFD
+#endif
+#ifndef PIPE_BUF
+# define PIPE_BUF 4096  /* amount of buffering in a pipe */
+#endif
+
 
-//usage:#if ENABLE_FEATURE_SH_IS_HUSH
-//usage:# define sh_trivial_usage hush_trivial_usage
-//usage:# define sh_full_usage    hush_full_usage
-//usage:#endif
-//usage:#if ENABLE_FEATURE_BASH_IS_HUSH
-//usage:# define bash_trivial_usage hush_trivial_usage
-//usage:# define bash_full_usage    hush_full_usage
-//usage:#endif
+/* So far, all bash compat is controlled by one config option */
+/* Separate defines document which part of code implements what */
+#define BASH_PATTERN_SUBST ENABLE_HUSH_BASH_COMPAT
+#define BASH_SUBSTR        ENABLE_HUSH_BASH_COMPAT
+#define BASH_TEST2         ENABLE_HUSH_BASH_COMPAT
+#define BASH_SOURCE        ENABLE_HUSH_BASH_COMPAT
+#define BASH_HOSTNAME_VAR  ENABLE_HUSH_BASH_COMPAT
 
 
 /* Build knobs */
 
 #define ERR_PTR ((void*)(long)1)
 
-#define JOB_STATUS_FORMAT    "[%d] %-22s %.40s\n"
+#define JOB_STATUS_FORMAT    "[%u] %-22s %.40s\n"
 
 #define _SPECIAL_VARS_STR     "_*@$!?#"
 #define SPECIAL_VARS_STR     ("_*@$!?#" + 1)
 #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3)
-#if ENABLE_HUSH_BASH_COMPAT
+#if BASH_PATTERN_SUBST
 /* Support / and // replace ops */
 /* Note that // is stored as \ in "encoded" string representation */
 # define VAR_ENCODED_SUBST_OPS      "\\/%#:-=+?"
@@ -468,11 +527,7 @@ typedef struct in_str {
        int peek_buf[2];
        int last_char;
        FILE *file;
-       int (*get) (struct in_str *) FAST_FUNC;
-       int (*peek) (struct in_str *) FAST_FUNC;
 } in_str;
-#define i_getch(input) ((input)->get(input))
-#define i_peek(input)  ((input)->peek(input))
 
 /* The descrip member of this structure is only used to make
  * debugging output pretty */
@@ -527,7 +582,7 @@ struct command {
        smallint cmd_type;          /* CMD_xxx */
 #define CMD_NORMAL   0
 #define CMD_SUBSHELL 1
-#if ENABLE_HUSH_BASH_COMPAT
+#if BASH_TEST2
 /* used for "[[ EXPR ]]" */
 # define CMD_SINGLEWORD_NOGLOB 2
 #endif
@@ -577,7 +632,7 @@ struct pipe {
        int alive_cmds;             /* number of commands running (not exited) */
        int stopped_cmds;           /* number of commands alive, but stopped */
 #if ENABLE_HUSH_JOB
-       int jobid;                  /* job number */
+       unsigned jobid;             /* job number */
        pid_t pgrp;                 /* process group ID for the job */
        char *cmdtext;              /* name of job */
 #endif
@@ -587,10 +642,10 @@ struct pipe {
        IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */
 };
 typedef enum pipe_style {
-       PIPE_SEQ = 1,
-       PIPE_AND = 2,
-       PIPE_OR  = 3,
-       PIPE_BG  = 4,
+       PIPE_SEQ = 0,
+       PIPE_AND = 1,
+       PIPE_OR  = 2,
+       PIPE_BG  = 3,
 } pipe_style;
 /* Is there anything in this pipe at all? */
 #define IS_NULL_PIPE(pi) \
@@ -754,7 +809,7 @@ struct globals {
 #endif
 #if ENABLE_HUSH_JOB
        int run_list_level;
-       int last_jobid;
+       unsigned last_jobid;
        pid_t saved_tty_pgrp;
        struct pipe *job_list;
 # define G_saved_tty_pgrp (G.saved_tty_pgrp)
@@ -784,8 +839,13 @@ struct globals {
        smallint exiting; /* used to prevent EXIT trap recursion */
        /* These four support $?, $#, and $1 */
        smalluint last_exitcode;
+#if ENABLE_HUSH_SET
        /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */
        smalluint global_args_malloced;
+# define G_global_args_malloced (G.global_args_malloced)
+#else
+# define G_global_args_malloced 0
+#endif
        /* how many non-NULL argv's we have. NB: $# + 1 */
        int global_argc;
        char **global_argv;
@@ -824,14 +884,21 @@ struct globals {
        unsigned special_sig_mask;
 #if ENABLE_HUSH_JOB
        unsigned fatal_sig_mask;
-# define G_fatal_sig_mask G.fatal_sig_mask
+# define G_fatal_sig_mask (G.fatal_sig_mask)
 #else
 # define G_fatal_sig_mask 0
 #endif
+#if ENABLE_HUSH_TRAP
        char **traps; /* char *traps[NSIG] */
+# define G_traps G.traps
+#else
+# define G_traps ((char**)NULL)
+#endif
        sigset_t pending_set;
-#if HUSH_DEBUG
+#if ENABLE_HUSH_MEMLEAK
        unsigned long memleak_value;
+#endif
+#if HUSH_DEBUG
        int debug_indent;
 #endif
        struct sigaction sa;
@@ -853,11 +920,15 @@ struct globals {
 
 /* Function prototypes for builtins */
 static int builtin_cd(char **argv) FAST_FUNC;
+#if ENABLE_HUSH_ECHO
 static int builtin_echo(char **argv) FAST_FUNC;
+#endif
 static int builtin_eval(char **argv) FAST_FUNC;
 static int builtin_exec(char **argv) FAST_FUNC;
 static int builtin_exit(char **argv) FAST_FUNC;
+#if ENABLE_HUSH_EXPORT
 static int builtin_export(char **argv) FAST_FUNC;
+#endif
 #if ENABLE_HUSH_JOB
 static int builtin_fg_bg(char **argv) FAST_FUNC;
 static int builtin_jobs(char **argv) FAST_FUNC;
@@ -871,24 +942,43 @@ static int builtin_history(char **argv) FAST_FUNC;
 #if ENABLE_HUSH_LOCAL
 static int builtin_local(char **argv) FAST_FUNC;
 #endif
-#if HUSH_DEBUG
+#if ENABLE_HUSH_MEMLEAK
 static int builtin_memleak(char **argv) FAST_FUNC;
 #endif
-#if ENABLE_PRINTF
+#if ENABLE_HUSH_PRINTF
 static int builtin_printf(char **argv) FAST_FUNC;
 #endif
 static int builtin_pwd(char **argv) FAST_FUNC;
+#if ENABLE_HUSH_READ
 static int builtin_read(char **argv) FAST_FUNC;
+#endif
+#if ENABLE_HUSH_SET
 static int builtin_set(char **argv) FAST_FUNC;
+#endif
 static int builtin_shift(char **argv) FAST_FUNC;
 static int builtin_source(char **argv) FAST_FUNC;
+#if ENABLE_HUSH_TEST || BASH_TEST2
 static int builtin_test(char **argv) FAST_FUNC;
+#endif
+#if ENABLE_HUSH_TRAP
 static int builtin_trap(char **argv) FAST_FUNC;
+#endif
+#if ENABLE_HUSH_TYPE
 static int builtin_type(char **argv) FAST_FUNC;
+#endif
 static int builtin_true(char **argv) FAST_FUNC;
+#if ENABLE_HUSH_UMASK
 static int builtin_umask(char **argv) FAST_FUNC;
+#endif
+#if ENABLE_HUSH_UNSET
 static int builtin_unset(char **argv) FAST_FUNC;
+#endif
+#if ENABLE_HUSH_KILL
+static int builtin_kill(char **argv) FAST_FUNC;
+#endif
+#if ENABLE_HUSH_WAIT
 static int builtin_wait(char **argv) FAST_FUNC;
+#endif
 #if ENABLE_HUSH_LOOPS
 static int builtin_break(char **argv) FAST_FUNC;
 static int builtin_continue(char **argv) FAST_FUNC;
@@ -915,13 +1005,13 @@ struct built_in_command {
 };
 
 static const struct built_in_command bltins1[] = {
-       BLTIN("."        , builtin_source  , "Run commands in file"),
+       BLTIN("."        , builtin_source  , "Run commands in file"),
        BLTIN(":"        , builtin_true    , NULL),
 #if ENABLE_HUSH_JOB
-       BLTIN("bg"       , builtin_fg_bg   , "Resume a job in the background"),
+       BLTIN("bg"       , builtin_fg_bg   , "Resume job in background"),
 #endif
 #if ENABLE_HUSH_LOOPS
-       BLTIN("break"    , builtin_break   , "Exit from a loop"),
+       BLTIN("break"    , builtin_break   , "Exit loop"),
 #endif
        BLTIN("cd"       , builtin_cd      , "Change directory"),
 #if ENABLE_HUSH_LOOPS
@@ -929,53 +1019,84 @@ static const struct built_in_command bltins1[] = {
 #endif
        BLTIN("eval"     , builtin_eval    , "Construct and run shell command"),
        BLTIN("exec"     , builtin_exec    , "Execute command, don't return to shell"),
-       BLTIN("exit"     , builtin_exit    , "Exit"),
+       BLTIN("exit"     , builtin_exit    , NULL),
+#if ENABLE_HUSH_EXPORT
        BLTIN("export"   , builtin_export  , "Set environment variables"),
+#endif
 #if ENABLE_HUSH_JOB
-       BLTIN("fg"       , builtin_fg_bg   , "Bring job into the foreground"),
+       BLTIN("fg"       , builtin_fg_bg   , "Bring job into foreground"),
 #endif
 #if ENABLE_HUSH_HELP
        BLTIN("help"     , builtin_help    , NULL),
 #endif
 #if MAX_HISTORY && ENABLE_FEATURE_EDITING
-       BLTIN("history"  , builtin_history , "Show command history"),
+       BLTIN("history"  , builtin_history , "Show history"),
 #endif
 #if ENABLE_HUSH_JOB
        BLTIN("jobs"     , builtin_jobs    , "List jobs"),
 #endif
+#if ENABLE_HUSH_KILL
+       BLTIN("kill"     , builtin_kill    , "Send signals to processes"),
+#endif
 #if ENABLE_HUSH_LOCAL
        BLTIN("local"    , builtin_local   , "Set local variables"),
 #endif
-#if HUSH_DEBUG
+#if ENABLE_HUSH_MEMLEAK
        BLTIN("memleak"  , builtin_memleak , NULL),
 #endif
+#if ENABLE_HUSH_READ
        BLTIN("read"     , builtin_read    , "Input into variable"),
+#endif
 #if ENABLE_HUSH_FUNCTIONS
-       BLTIN("return"   , builtin_return  , "Return from a function"),
+       BLTIN("return"   , builtin_return  , "Return from function"),
+#endif
+#if ENABLE_HUSH_SET
+       BLTIN("set"      , builtin_set     , "Set positional parameters"),
 #endif
-       BLTIN("set"      , builtin_set     , "Set/unset positional parameters"),
        BLTIN("shift"    , builtin_shift   , "Shift positional parameters"),
-#if ENABLE_HUSH_BASH_COMPAT
-       BLTIN("source"   , builtin_source  , "Run commands in a file"),
+#if BASH_SOURCE
+       BLTIN("source"   , builtin_source  , NULL),
 #endif
+#if ENABLE_HUSH_TRAP
        BLTIN("trap"     , builtin_trap    , "Trap signals"),
+#endif
        BLTIN("true"     , builtin_true    , NULL),
+#if ENABLE_HUSH_TYPE
        BLTIN("type"     , builtin_type    , "Show command type"),
-       BLTIN("ulimit"   , shell_builtin_ulimit  , "Control resource limits"),
+#endif
+#if ENABLE_HUSH_ULIMIT
+       BLTIN("ulimit"   , shell_builtin_ulimit, "Control resource limits"),
+#endif
+#if ENABLE_HUSH_UMASK
        BLTIN("umask"    , builtin_umask   , "Set file creation mask"),
+#endif
+#if ENABLE_HUSH_UNSET
        BLTIN("unset"    , builtin_unset   , "Unset variables"),
+#endif
+#if ENABLE_HUSH_WAIT
        BLTIN("wait"     , builtin_wait    , "Wait for process"),
+#endif
 };
-/* For now, echo and test are unconditionally enabled.
- * Maybe make it configurable? */
+/* These builtins won't be used if we are on NOMMU and need to re-exec
+ * (it's cheaper to run an external program in this case):
+ */
 static const struct built_in_command bltins2[] = {
+#if ENABLE_HUSH_TEST
        BLTIN("["        , builtin_test    , NULL),
+#endif
+#if BASH_TEST2
+       BLTIN("[["       , builtin_test    , NULL),
+#endif
+#if ENABLE_HUSH_ECHO
        BLTIN("echo"     , builtin_echo    , NULL),
-#if ENABLE_PRINTF
+#endif
+#if ENABLE_HUSH_PRINTF
        BLTIN("printf"   , builtin_printf  , NULL),
 #endif
        BLTIN("pwd"      , builtin_pwd     , NULL),
+#if ENABLE_HUSH_TEST
        BLTIN("test"     , builtin_test    , NULL),
+#endif
 };
 
 
@@ -1151,6 +1272,9 @@ static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch)
        char msg[2];
        msg[0] = ch;
        msg[1] = '\0';
+#if HUSH_DEBUG >= 2
+       bb_error_msg("hush.c:%u", lineno);
+#endif
        bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg);
 }
 
@@ -1357,7 +1481,7 @@ typedef struct save_arg_t {
        char *sv_argv0;
        char **sv_g_argv;
        int sv_g_argc;
-       smallint sv_g_malloced;
+       IF_HUSH_SET(smallint sv_g_malloced;)
 } save_arg_t;
 
 static void save_and_replace_G_args(save_arg_t *sv, char **argv)
@@ -1367,11 +1491,11 @@ static void save_and_replace_G_args(save_arg_t *sv, char **argv)
        sv->sv_argv0 = argv[0];
        sv->sv_g_argv = G.global_argv;
        sv->sv_g_argc = G.global_argc;
-       sv->sv_g_malloced = G.global_args_malloced;
+       IF_HUSH_SET(sv->sv_g_malloced = G.global_args_malloced;)
 
        argv[0] = G.global_argv[0]; /* retain $0 */
        G.global_argv = argv;
-       G.global_args_malloced = 0;
+       IF_HUSH_SET(G.global_args_malloced = 0;)
 
        n = 1;
        while (*++argv)
@@ -1381,19 +1505,19 @@ static void save_and_replace_G_args(save_arg_t *sv, char **argv)
 
 static void restore_G_args(save_arg_t *sv, char **argv)
 {
-       char **pp;
-
+#if ENABLE_HUSH_SET
        if (G.global_args_malloced) {
                /* someone ran "set -- arg1 arg2 ...", undo */
-               pp = G.global_argv;
+               char **pp = G.global_argv;
                while (*++pp) /* note: does not free $0 */
                        free(*pp);
                free(G.global_argv);
        }
+#endif
        argv[0] = sv->sv_argv0;
        G.global_argv = sv->sv_g_argv;
        G.global_argc = sv->sv_g_argc;
-       G.global_args_malloced = sv->sv_g_malloced;
+       IF_HUSH_SET(G.global_args_malloced = sv->sv_g_malloced;)
 }
 
 
@@ -1577,9 +1701,8 @@ static sighandler_t install_sighandler(int sig, sighandler_t handler)
 }
 
 static void hush_exit(int exitcode) NORETURN;
-static void fflush_and__exit(void) NORETURN;
-static void restore_ttypgrp_and__exit(void) NORETURN;
 
+static void restore_ttypgrp_and__exit(void) NORETURN;
 static void restore_ttypgrp_and__exit(void)
 {
        /* xfunc has failed! die die die */
@@ -1588,6 +1711,8 @@ static void restore_ttypgrp_and__exit(void)
        hush_exit(xfunc_error_retval);
 }
 
+#if ENABLE_HUSH_JOB
+
 /* Needed only on some libc:
  * It was observed that on exit(), fgetc'ed buffered data
  * gets "unwound" via lseek(fd, -NUM, SEEK_CUR).
@@ -1601,14 +1726,13 @@ static void restore_ttypgrp_and__exit(void)
  * and in `cmd` handling.
  * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit():
  */
+static void fflush_and__exit(void) NORETURN;
 static void fflush_and__exit(void)
 {
        fflush_all();
        _exit(xfunc_error_retval);
 }
 
-#if ENABLE_HUSH_JOB
-
 /* After [v]fork, in child: do not restore tty pgrp on xfunc death */
 # define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit)
 /* After [v]fork, in parent: restore tty pgrp on xfunc death */
@@ -1681,13 +1805,13 @@ static void hush_exit(int exitcode)
 #endif
 
        fflush_all();
-       if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) {
+       if (G.exiting <= 0 && G_traps && G_traps[0] && G_traps[0][0]) {
                char *argv[3];
                /* argv[0] is unused */
-               argv[1] = G.traps[0];
+               argv[1] = G_traps[0];
                argv[2] = NULL;
                G.exiting = 1; /* prevent EXIT trap recursion */
-               /* Note: G.traps[0] is not cleared!
+               /* Note: G_traps[0] is not cleared!
                 * "trap" will still show it, if executed
                 * in the handler */
                builtin_eval(argv);
@@ -1738,17 +1862,18 @@ static int check_and_run_traps(void)
                } while (sig < NSIG);
                break;
  got_sig:
-               if (G.traps && G.traps[sig]) {
+               if (G_traps && G_traps[sig]) {
                        debug_printf_exec("%s: sig:%d handler:'%s'\n", __func__, sig, G.traps[sig]);
-                       if (G.traps[sig][0]) {
+                       if (G_traps[sig][0]) {
                                /* We have user-defined handler */
                                smalluint save_rcode;
                                char *argv[3];
                                /* argv[0] is unused */
-                               argv[1] = G.traps[sig];
+                               argv[1] = G_traps[sig];
                                argv[2] = NULL;
                                save_rcode = G.last_exitcode;
                                builtin_eval(argv);
+//FIXME: shouldn't it be set to 128 + sig instead?
                                G.last_exitcode = save_rcode;
                                last_sig = sig;
                        } /* else: "" trap, ignoring signal */
@@ -1758,8 +1883,6 @@ static int check_and_run_traps(void)
                switch (sig) {
                case SIGINT:
                        debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig);
-                       /* Builtin was ^C'ed, make it look prettier: */
-                       bb_putchar('\n');
                        G.flag_SIGINT = 1;
                        last_sig = sig;
                        break;
@@ -1785,7 +1908,7 @@ static int check_and_run_traps(void)
                        G.count_SIGCHLD++;
 //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
                        /* Note:
-                        * We dont do 'last_sig = sig' here -> NOT returning this sig.
+                        * We don't do 'last_sig = sig' here -> NOT returning this sig.
                         * This simplifies wait builtin a bit.
                         */
                        break;
@@ -1794,7 +1917,7 @@ static int check_and_run_traps(void)
                        debug_printf_exec("%s: sig:%d default handling is to ignore\n", __func__, sig);
                        /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */
                        /* Note:
-                        * We dont do 'last_sig = sig' here -> NOT returning this sig.
+                        * We don't do 'last_sig = sig' here -> NOT returning this sig.
                         * Example: wait is not interrupted by TERM
                         * in interactive shell, because TERM is ignored.
                         */
@@ -1891,6 +2014,7 @@ static int set_local_var(char *str, int flg_export, int local_lvl, int flg_read_
 {
        struct variable **var_pp;
        struct variable *cur;
+       char *free_me = NULL;
        char *eq_sign;
        int name_len;
 
@@ -1907,6 +2031,7 @@ static int set_local_var(char *str, int flg_export, int local_lvl, int flg_read_
                        var_pp = &cur->next;
                        continue;
                }
+
                /* We found an existing var with this name */
                if (cur->flg_read_only) {
 #if !BB_MMU
@@ -1955,12 +2080,17 @@ static int set_local_var(char *str, int flg_export, int local_lvl, int flg_read_
                                strcpy(cur->varstr, str);
                                goto free_and_exp;
                        }
-               } else {
-                       /* max_len == 0 signifies "malloced" var, which we can
-                        * (and has to) free */
-                       free(cur->varstr);
-               }
-               cur->max_len = 0;
+                       /* Can't reuse */
+                       cur->max_len = 0;
+                       goto set_str_and_exp;
+               }
+               /* max_len == 0 signifies "malloced" var, which we can
+                * (and have to) free. But we can't free(cur->varstr) here:
+                * if cur->flg_export is 1, it is in the environment.
+                * We should either unsetenv+free, or wait until putenv,
+                * then putenv(new)+free(old).
+                */
+               free_me = cur->varstr;
                goto set_str_and_exp;
        }
 
@@ -1987,10 +2117,15 @@ static int set_local_var(char *str, int flg_export, int local_lvl, int flg_read_
                        cur->flg_export = 0;
                        /* unsetenv was already done */
                } else {
+                       int i;
                        debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr);
-                       return putenv(cur->varstr);
+                       i = putenv(cur->varstr);
+                       /* only now we can free old exported malloced string */
+                       free(free_me);
+                       return i;
                }
        }
+       free(free_me);
        return 0;
 }
 
@@ -2030,10 +2165,12 @@ static int unset_local_var_len(const char *name, int name_len)
        return EXIT_SUCCESS;
 }
 
+#if ENABLE_HUSH_UNSET
 static int unset_local_var(const char *name)
 {
        return unset_local_var_len(name, strlen(name));
 }
+#endif
 
 static void unset_vars(char **strings)
 {
@@ -2050,11 +2187,13 @@ static void unset_vars(char **strings)
        free(strings);
 }
 
+#if BASH_HOSTNAME_VAR || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_READ
 static void FAST_FUNC set_local_var_from_halves(const char *name, const char *val)
 {
        char *var = xasprintf("%s=%s", name, val);
        set_local_var(var, /*flags:*/ 0, /*lvl:*/ 0, /*ro:*/ 0);
 }
+#endif
 
 
 /*
@@ -2141,7 +2280,7 @@ static void reinit_unicode_for_hush(void)
  *     AT\
  *     H\
  *     \
- * It excercises a lot of corner cases.
+ * It exercises a lot of corner cases.
  */
 static void cmdedit_update_prompt(void)
 {
@@ -2183,18 +2322,30 @@ static int get_user_input(struct in_str *i)
 
        prompt_str = setup_prompt_string(i->promptmode);
 # if ENABLE_FEATURE_EDITING
-       do {
+       for (;;) {
                reinit_unicode_for_hush();
-               G.flag_SIGINT = 0;
+               if (G.flag_SIGINT) {
+                       /* There was ^C'ed, make it look prettier: */
+                       bb_putchar('\n');
+                       G.flag_SIGINT = 0;
+               }
                /* buglet: SIGINT will not make new prompt to appear _at once_,
-                * only after <Enter>. (^C will work) */
+                * only after <Enter>. (^C works immediately) */
                r = read_line_input(G.line_input_state, prompt_str,
                                G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1,
                                /*timeout*/ -1
                );
-               /* catch *SIGINT* etc (^C is handled by read_line_input) */
+               /* read_line_input intercepts ^C, "convert" it to SIGINT */
+               if (r == 0) {
+                       write(STDOUT_FILENO, "^C", 2);
+                       raise(SIGINT);
+               }
                check_and_run_traps();
-       } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */
+               if (r != 0 && !G.flag_SIGINT)
+                       break;
+               /* ^C or SIGINT: repeat */
+               G.last_exitcode = 128 + SIGINT;
+       }
        if (r < 0) {
                /* EOF/error detected */
                i->p = NULL;
@@ -2204,7 +2355,7 @@ static int get_user_input(struct in_str *i)
        i->p = G.user_input_buf;
        return (unsigned char)*i->p++;
 # else
-       do {
+       for (;;) {
                G.flag_SIGINT = 0;
                if (i->last_char == '\0' || i->last_char == '\n') {
                        /* Why check_and_run_traps here? Try this interactively:
@@ -2216,8 +2367,18 @@ static int get_user_input(struct in_str *i)
                        fputs(prompt_str, stdout);
                }
                fflush_all();
+//FIXME: here ^C or SIGINT will have effect only after <Enter>
                r = fgetc(i->file);
-       } while (G.flag_SIGINT || r == '\0');
+               /* In !ENABLE_FEATURE_EDITING we don't use read_line_input,
+                * no ^C masking happens during fgetc, no special code for ^C:
+                * it generates SIGINT as usual.
+                */
+               check_and_run_traps();
+               if (G.flag_SIGINT)
+                       G.last_exitcode = 128 + SIGINT;
+               if (r != '\0')
+                       break;
+       }
        return r;
 # endif
 }
@@ -2246,10 +2407,23 @@ static inline int fgetc_interactive(struct in_str *i)
 }
 #endif  /* INTERACTIVE */
 
-static int FAST_FUNC file_get(struct in_str *i)
+static int i_getch(struct in_str *i)
 {
        int ch;
 
+       if (!i->file) {
+               /* string-based in_str */
+               ch = (unsigned char)*i->p;
+               if (ch != '\0') {
+                       i->p++;
+                       i->last_char = ch;
+                       return ch;
+               }
+               return EOF;
+       }
+
+       /* FILE-based in_str */
+
 #if ENABLE_FEATURE_EDITING
        /* This can be stdin, check line editing char[] buffer */
        if (i->p && *i->p != '\0') {
@@ -2275,10 +2449,18 @@ static int FAST_FUNC file_get(struct in_str *i)
        return ch;
 }
 
-static int FAST_FUNC file_peek(struct in_str *i)
+static int i_peek(struct in_str *i)
 {
        int ch;
 
+       if (!i->file) {
+               /* string-based in_str */
+               /* Doesn't report EOF on NUL. None of the callers care. */
+               return (unsigned char)*i->p;
+       }
+
+       /* FILE-based in_str */
+
 #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE
        /* This can be stdin, check line editing char[] buffer */
        if (i->p && *i->p != '\0')
@@ -2305,23 +2487,6 @@ static int FAST_FUNC file_peek(struct in_str *i)
        return ch;
 }
 
-static int FAST_FUNC static_get(struct in_str *i)
-{
-       int ch = (unsigned char)*i->p;
-       if (ch != '\0') {
-               i->p++;
-               i->last_char = ch;
-               return ch;
-       }
-       return EOF;
-}
-
-static int FAST_FUNC static_peek(struct in_str *i)
-{
-       /* Doesn't report EOF on NUL. None of the callers care. */
-       return (unsigned char)*i->p;
-}
-
 /* Only ever called if i_peek() was called, and did not return EOF.
  * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL,
  * not end-of-line. Therefore we never need to read a new editing line here.
@@ -2357,8 +2522,6 @@ static int i_peek2(struct in_str *i)
 static void setup_file_in_str(struct in_str *i, FILE *f)
 {
        memset(i, 0, sizeof(*i));
-       i->get = file_get;
-       i->peek = file_peek;
        /* i->promptmode = 0; - PS1 (memset did it) */
        i->file = f;
        /* i->p = NULL; */
@@ -2367,9 +2530,8 @@ static void setup_file_in_str(struct in_str *i, FILE *f)
 static void setup_string_in_str(struct in_str *i, const char *s)
 {
        memset(i, 0, sizeof(*i));
-       i->get = static_get;
-       i->peek = static_peek;
        /* i->promptmode = 0; - PS1 (memset did it) */
+       /*i->file = NULL */;
        i->p = s;
 }
 
@@ -3126,7 +3288,6 @@ static struct pipe *new_pipe(void)
 {
        struct pipe *pi;
        pi = xzalloc(sizeof(struct pipe));
-       /*pi->followup = 0; - deliberately invalid value */
        /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */
        return pi;
 }
@@ -3485,7 +3646,7 @@ static int done_word(o_string *word, struct parse_context *ctx)
                                                (ctx->ctx_res_w == RES_SNTX));
                                return (ctx->ctx_res_w == RES_SNTX);
                        }
-# if ENABLE_HUSH_BASH_COMPAT
+# if BASH_TEST2
                        if (strcmp(word->data, "[[") == 0) {
                                command->cmd_type = CMD_SINGLEWORD_NOGLOB;
                        }
@@ -3901,12 +4062,17 @@ static int parse_group(o_string *dest, struct parse_context *ctx,
                command->cmd_type = CMD_SUBSHELL;
        } else {
                /* bash does not allow "{echo...", requires whitespace */
-               ch = i_getch(input);
-               if (ch != ' ' && ch != '\t' && ch != '\n') {
+               ch = i_peek(input);
+               if (ch != ' ' && ch != '\t' && ch != '\n'
+                && ch != '('   /* but "{(..." is allowed (without whitespace) */
+               ) {
                        syntax_error_unexpected_ch(ch);
                        return 1;
                }
-               nommu_addchr(&ctx->as_string, ch);
+               if (ch != '(') {
+                       ch = i_getch(input);
+                       nommu_addchr(&ctx->as_string, ch);
+               }
        }
 
        {
@@ -3976,7 +4142,7 @@ static int i_peek_and_eat_bkslash_nl(struct in_str *input)
        }
 }
 
-#if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS
+#if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS
 /* Subroutines for copying $(...) and `...` things */
 static int add_till_backquote(o_string *dest, struct in_str *input, int in_dquote);
 /* '...' */
@@ -4078,7 +4244,7 @@ static int add_till_closing_bracket(o_string *dest, struct in_str *input, unsign
 {
        int ch;
        char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG;
-# if ENABLE_HUSH_BASH_COMPAT
+# if BASH_SUBSTR || BASH_PATTERN_SUBST
        char end_char2 = end_ch >> 8;
 # endif
        end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1);
@@ -4089,7 +4255,11 @@ static int add_till_closing_bracket(o_string *dest, struct in_str *input, unsign
                        syntax_error_unterm_ch(end_ch);
                        return 0;
                }
-               if (ch == end_ch  IF_HUSH_BASH_COMPAT( || ch == end_char2)) {
+               if (ch == end_ch
+# if BASH_SUBSTR || BASH_PATTERN_SUBST
+                       || ch == end_char2
+# endif
+               ) {
                        if (!dbl)
                                break;
                        /* we look for closing )) of $((EXPR)) */
@@ -4144,7 +4314,7 @@ static int add_till_closing_bracket(o_string *dest, struct in_str *input, unsign
        }
        return ch;
 }
-#endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS */
+#endif /* ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS */
 
 /* Return code: 0 for OK, 1 for syntax error */
 #if BB_MMU
@@ -4242,14 +4412,14 @@ static int parse_dollar(o_string *as_string,
 
                                /* Eat everything until closing '}' (or ':') */
                                end_ch = '}';
-                               if (ENABLE_HUSH_BASH_COMPAT
+                               if (BASH_SUBSTR
                                 && ch == ':'
                                 && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input))
                                ) {
                                        /* It's ${var:N[:M]} thing */
                                        end_ch = '}' * 0x100 + ':';
                                }
-                               if (ENABLE_HUSH_BASH_COMPAT
+                               if (BASH_PATTERN_SUBST
                                 && ch == '/'
                                ) {
                                        /* It's ${var/[/]pattern[/repl]} thing */
@@ -4276,7 +4446,9 @@ static int parse_dollar(o_string *as_string,
                                        o_addchr(as_string, last_ch);
                                }
 
-                               if (ENABLE_HUSH_BASH_COMPAT && (end_ch & 0xff00)) {
+                               if ((BASH_SUBSTR || BASH_PATTERN_SUBST)
+                                        && (end_ch & 0xff00)
+                               ) {
                                        /* close the first block: */
                                        o_addchr(dest, SPECIAL_VAR_SYMBOL);
                                        /* while parsing N from ${var:N[:M]}
@@ -4287,7 +4459,7 @@ static int parse_dollar(o_string *as_string,
                                                goto again;
                                        }
                                        /* got '}' */
-                                       if (end_ch == '}' * 0x100 + ':') {
+                                       if (BASH_SUBSTR && end_ch == '}' * 0x100 + ':') {
                                                /* it's ${var:N} - emulate :999999999 */
                                                o_addstr(dest, "999999999");
                                        } /* else: it's ${var/[/]pattern} */
@@ -4298,13 +4470,13 @@ static int parse_dollar(o_string *as_string,
                o_addchr(dest, SPECIAL_VAR_SYMBOL);
                break;
        }
-#if ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK
+#if ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_TICK
        case '(': {
                unsigned pos;
 
                ch = i_getch(input);
                nommu_addchr(as_string, ch);
-# if ENABLE_SH_MATH_SUPPORT
+# if ENABLE_FEATURE_SH_MATH
                if (i_peek_and_eat_bkslash_nl(input) == '(') {
                        ch = i_getch(input);
                        nommu_addchr(as_string, ch);
@@ -4362,7 +4534,7 @@ static int parse_dollar(o_string *as_string,
 }
 
 #if BB_MMU
-# if ENABLE_HUSH_BASH_COMPAT
+# if BASH_PATTERN_SUBST
 #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \
        encode_string(dest, input, dquote_end, process_bkslash)
 # else
@@ -4374,7 +4546,7 @@ static int parse_dollar(o_string *as_string,
 
 #else /* !MMU */
 
-# if ENABLE_HUSH_BASH_COMPAT
+# if BASH_PATTERN_SUBST
 /* all parameters are needed, no macro tricks */
 # else
 #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \
@@ -4387,7 +4559,7 @@ static int encode_string(o_string *as_string,
                int dquote_end,
                int process_bkslash)
 {
-#if !ENABLE_HUSH_BASH_COMPAT
+#if !BASH_PATTERN_SUBST
        const int process_bkslash = 1;
 #endif
        int ch;
@@ -4514,12 +4686,14 @@ static struct pipe *parse_stream(char **pstring,
                                syntax_error_unterm_str("here document");
                                goto parse_error;
                        }
-                       /* end_trigger == '}' case errors out earlier,
-                        * checking only ')' */
                        if (end_trigger == ')') {
                                syntax_error_unterm_ch('(');
                                goto parse_error;
                        }
+                       if (end_trigger == '}') {
+                               syntax_error_unterm_ch('{');
+                               goto parse_error;
+                       }
 
                        if (done_word(&dest, &ctx)) {
                                goto parse_error;
@@ -4561,6 +4735,7 @@ static struct pipe *parse_stream(char **pstring,
                 || dest.has_quoted_part     /* ""{... - non-special */
                 || (next != ';'             /* }; - special */
                    && next != ')'           /* }) - special */
+                   && next != '('           /* {( - special */
                    && next != '&'           /* }& and }&& ... - special */
                    && next != '|'           /* }|| ... - special */
                    && !strchr(defifs, next) /* {word - non-special */
@@ -4643,17 +4818,31 @@ static struct pipe *parse_stream(char **pstring,
                 * Pathological example: { ""}; } should exec "}" cmd
                 */
                if (ch == '}') {
-                       if (!IS_NULL_CMD(ctx.command) /* cmd } */
-                        || dest.length != 0 /* word} */
+                       if (dest.length != 0 /* word} */
                         || dest.has_quoted_part    /* ""} */
                        ) {
                                goto ordinary_char;
                        }
+                       if (!IS_NULL_CMD(ctx.command)) { /* cmd } */
+                               /* Generally, there should be semicolon: "cmd; }"
+                                * However, bash allows to omit it if "cmd" is
+                                * a group. Examples:
+                                * { { echo 1; } }
+                                * {(echo 1)}
+                                * { echo 0 >&2 | { echo 1; } }
+                                * { while false; do :; done }
+                                * { case a in b) ;; esac }
+                                */
+                               if (ctx.command->group)
+                                       goto term_group;
+                               goto ordinary_char;
+                       }
                        if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */
+                               /* Can't be an end of {cmd}, skip the check */
                                goto skip_end_trigger;
                        /* else: } does terminate a group */
                }
-
+ term_group:
                if (end_trigger && end_trigger == ch
                 && (ch != ';' || heredoc_cnt == 0)
 #if ENABLE_HUSH_CASE
@@ -4956,7 +5145,8 @@ static struct pipe *parse_stream(char **pstring,
                         * if we see {, we call parse_group(..., end_trigger='}')
                         * and it will match } earlier (not here). */
                        syntax_error_unexpected_ch(ch);
-                       goto parse_error;
+                       G.last_exitcode = 2;
+                       goto parse_error1;
                default:
                        if (HUSH_DEBUG)
                                bb_error_msg_and_die("BUG: unexpected %c\n", ch);
@@ -4964,6 +5154,8 @@ static struct pipe *parse_stream(char **pstring,
        } /* while (1) */
 
  parse_error:
+       G.last_exitcode = 1;
+ parse_error1:
        {
                struct parse_context *pctx;
                IF_HAS_KEYWORDS(struct parse_context *p2;)
@@ -4973,8 +5165,8 @@ static struct pipe *parse_stream(char **pstring,
                 * Run it from interactive shell, watch pmap `pidof hush`.
                 * while if false; then false; fi; do break; fi
                 * Samples to catch leaks at execution:
-                * while if (true | {true;}); then echo ok; fi; do break; done
-                * while if (true | {true;}); then echo ok; fi; do (if echo ok; break; then :; fi) | cat; break; done
+                * while if (true | { true;}); then echo ok; fi; do break; done
+                * while if (true | { true;}); then echo ok; fi; do (if echo ok; break; then :; fi) | cat; break; done
                 */
                pctx = &ctx;
                do {
@@ -4997,7 +5189,6 @@ static struct pipe *parse_stream(char **pstring,
                } while (HAS_KEYWORDS && pctx);
 
                o_free(&dest);
-               G.last_exitcode = 1;
 #if !BB_MMU
                if (pstring)
                        *pstring = NULL;
@@ -5011,7 +5202,7 @@ static struct pipe *parse_stream(char **pstring,
 /*** Execution routines ***/
 
 /* Expansion can recurse, need forward decls: */
-#if !ENABLE_HUSH_BASH_COMPAT
+#if !BASH_PATTERN_SUBST
 /* only ${var/pattern/repl} (its pattern part) needs additional mode */
 #define expand_string_to_string(str, do_unbackslash) \
        expand_string_to_string(str)
@@ -5044,7 +5235,7 @@ static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len
                        /* And now we want to add { or } and continue:
                         *  o_addchr(o, c);
                         *  continue;
-                        * luckily, just falling throught achieves this.
+                        * luckily, just falling through achieves this.
                         */
                }
 #endif
@@ -5132,7 +5323,7 @@ static int expand_on_ifs(int *ended_with_ifs, o_string *output, int n, const cha
  * Returns malloced string.
  * As an optimization, we return NULL if expansion is not needed.
  */
-#if !ENABLE_HUSH_BASH_COMPAT
+#if !BASH_PATTERN_SUBST
 /* only ${var/pattern/repl} (its pattern part) needs additional mode */
 #define encode_then_expand_string(str, process_bkslash, do_unbackslash) \
        encode_then_expand_string(str)
@@ -5165,7 +5356,7 @@ static char *encode_then_expand_string(const char *str, int process_bkslash, int
        return exp_str;
 }
 
-#if ENABLE_SH_MATH_SUPPORT
+#if ENABLE_FEATURE_SH_MATH
 static arith_t expand_and_evaluate_arith(const char *arg, const char **errmsg_p)
 {
        arith_state_t math_state;
@@ -5186,7 +5377,7 @@ static arith_t expand_and_evaluate_arith(const char *arg, const char **errmsg_p)
 }
 #endif
 
-#if ENABLE_HUSH_BASH_COMPAT
+#if BASH_PATTERN_SUBST
 /* ${var/[/]pattern[/repl]} helpers */
 static char *strstr_pattern(char *val, const char *pattern, int *size)
 {
@@ -5238,7 +5429,7 @@ static char *replace_pattern(char *val, const char *pattern, const char *repl, c
        debug_printf_varexp("result:'%s'\n", result);
        return result;
 }
-#endif
+#endif /* BASH_PATTERN_SUBST */
 
 /* Helper:
  * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct.
@@ -5286,7 +5477,7 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha
                        if (exp_op == ':') {
                                exp_op = *exp_word++;
 //TODO: try ${var:} and ${var:bogus} in non-bash config
-                               if (ENABLE_HUSH_BASH_COMPAT
+                               if (BASH_SUBSTR
                                 && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op))
                                ) {
                                        /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */
@@ -5368,7 +5559,7 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha
                                }
                        }
                }
-#if ENABLE_HUSH_BASH_COMPAT
+#if BASH_PATTERN_SUBST
                else if (exp_op == '/' || exp_op == '\\') {
                        /* It's ${var/[/]pattern[/repl]} thing.
                         * Note that in encoded form it has TWO parts:
@@ -5415,9 +5606,9 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha
                                free(repl);
                        }
                }
-#endif
+#endif /* BASH_PATTERN_SUBST */
                else if (exp_op == ':') {
-#if ENABLE_HUSH_BASH_COMPAT && ENABLE_SH_MATH_SUPPORT
+#if BASH_SUBSTR && ENABLE_FEATURE_SH_MATH
                        /* It's ${var:N[:M]} bashism.
                         * Note that in encoded form it has TWO parts:
                         * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL>
@@ -5453,7 +5644,7 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha
                                }
                                debug_printf_varexp("val:'%s'\n", val);
                        } else
-#endif
+#endif /* HUSH_SUBSTR_EXPANSION && FEATURE_SH_MATH */
                        {
                                die_if_script("malformed ${%s:...}", var);
                                val = NULL;
@@ -5552,7 +5743,7 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg)
 #if ENABLE_HUSH_TICK
                o_string subst_result = NULL_O_STRING;
 #endif
-#if ENABLE_SH_MATH_SUPPORT
+#if ENABLE_FEATURE_SH_MATH
                char arith_buf[sizeof(arith_t)*3 + 2];
 #endif
 
@@ -5639,14 +5830,14 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg)
                        arg++;
                        /* Can't just stuff it into output o_string,
                         * expanded result may need to be globbed
-                        * and $IFS-splitted */
+                        * and $IFS-split */
                        debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch);
                        G.last_exitcode = process_command_subs(&subst_result, arg);
                        debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data);
                        val = subst_result.data;
                        goto store_val;
 #endif
-#if ENABLE_SH_MATH_SUPPORT
+#if ENABLE_FEATURE_SH_MATH
                case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */
                        arith_t res;
 
@@ -5743,7 +5934,7 @@ static char **expand_strvec_to_strvec(char **argv)
        return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS);
 }
 
-#if ENABLE_HUSH_BASH_COMPAT
+#if BASH_TEST2
 static char **expand_strvec_to_strvec_singleword_noglob(char **argv)
 {
        return expand_variables(argv, EXP_FLAG_SINGLEWORD);
@@ -5758,7 +5949,7 @@ static char **expand_strvec_to_strvec_singleword_noglob(char **argv)
  */
 static char *expand_string_to_string(const char *str, int do_unbackslash)
 {
-#if !ENABLE_HUSH_BASH_COMPAT
+#if !BASH_PATTERN_SUBST
        const int do_unbackslash = 1;
 #endif
        char *argv[2], **list;
@@ -5836,13 +6027,15 @@ static void switch_off_special_sigs(unsigned mask)
                sig++;
                if (!(mask & 1))
                        continue;
-               if (G.traps) {
-                       if (G.traps[sig] && !G.traps[sig][0])
+#if ENABLE_HUSH_TRAP
+               if (G_traps) {
+                       if (G_traps[sig] && !G_traps[sig][0])
                                /* trap is '', has to remain SIG_IGN */
                                continue;
-                       free(G.traps[sig]);
-                       G.traps[sig] = NULL;
+                       free(G_traps[sig]);
+                       G_traps[sig] = NULL;
                }
+#endif
                /* We are here only if no trap or trap was not '' */
                install_sighandler(sig, SIG_DFL);
        }
@@ -5859,7 +6052,7 @@ static void reset_traps_to_defaults(void)
        /* This function is always called in a child shell
         * after fork (not vfork, NOMMU doesn't use this function).
         */
-       unsigned sig;
+       IF_HUSH_TRAP(unsigned sig;)
        unsigned mask;
 
        /* Child shells are not interactive.
@@ -5868,35 +6061,37 @@ static void reset_traps_to_defaults(void)
         * Same goes for SIGTERM, SIGHUP, SIGINT.
         */
        mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask;
-       if (!G.traps && !mask)
+       if (!G_traps && !mask)
                return; /* already no traps and no special sigs */
 
        /* Switch off special sigs */
        switch_off_special_sigs(mask);
-#if ENABLE_HUSH_JOB
+# if ENABLE_HUSH_JOB
        G_fatal_sig_mask = 0;
-#endif
+# endif
        G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS;
        /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS
         * remain set in G.special_sig_mask */
 
-       if (!G.traps)
+# if ENABLE_HUSH_TRAP
+       if (!G_traps)
                return;
 
        /* Reset all sigs to default except ones with empty traps */
        for (sig = 0; sig < NSIG; sig++) {
-               if (!G.traps[sig])
+               if (!G_traps[sig])
                        continue; /* no trap: nothing to do */
-               if (!G.traps[sig][0])
+               if (!G_traps[sig][0])
                        continue; /* empty trap: has to remain SIG_IGN */
                /* sig has non-empty trap, reset it: */
-               free(G.traps[sig]);
-               G.traps[sig] = NULL;
+               free(G_traps[sig]);
+               G_traps[sig] = NULL;
                /* There is no signal for trap 0 (EXIT) */
                if (sig == 0)
                        continue;
                install_sighandler(sig, pick_sighandler(sig));
        }
+# endif
 }
 
 #else /* !BB_MMU */
@@ -5936,10 +6131,10 @@ static void re_execute_shell(char ***to_free, const char *s,
                cnt++;
 
        empty_trap_mask = 0;
-       if (G.traps) {
+       if (G_traps) {
                int sig;
                for (sig = 1; sig < NSIG; sig++) {
-                       if (G.traps[sig] && !G.traps[sig][0])
+                       if (G_traps[sig] && !G_traps[sig][0])
                                empty_trap_mask |= 1LL << sig;
                }
        }
@@ -6132,6 +6327,7 @@ static FILE *generate_stream_from_string(const char *s, pid_t *pid_p)
                xmove_fd(channel[1], 1);
                /* Prevent it from trying to handle ctrl-z etc */
                IF_HUSH_JOB(G.run_list_level = 1;)
+# if ENABLE_HUSH_TRAP
                /* Awful hack for `trap` or $(trap).
                 *
                 * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html
@@ -6175,6 +6371,7 @@ static FILE *generate_stream_from_string(const char *s, pid_t *pid_p)
                        fflush_all(); /* important */
                        _exit(0);
                }
+# endif
 # if BB_MMU
                reset_traps_to_defaults();
                parse_and_run_string(s);
@@ -6583,6 +6780,7 @@ static struct function *new_function(char *name)
        return funcp;
 }
 
+# if ENABLE_HUSH_UNSET
 static void unset_func(const char *name)
 {
        struct function **funcpp = find_function_slot(name);
@@ -6598,13 +6796,14 @@ static void unset_func(const char *name)
                if (funcp->body) {
                        free_pipe_list(funcp->body);
                        free(funcp->name);
-# if !BB_MMU
+#  if !BB_MMU
                        free(funcp->body_as_string);
-# endif
+#  endif
                }
                free(funcp);
        }
 }
+# endif
 
 # if BB_MMU
 #define exec_function(to_free, funcp, argv) \
@@ -6735,13 +6934,17 @@ static void exec_builtin(char ***to_free,
 static void execvp_or_die(char **argv) NORETURN;
 static void execvp_or_die(char **argv)
 {
+       int e;
        debug_printf_exec("execing '%s'\n", argv[0]);
        /* Don't propagate SIG_IGN to the child */
        if (SPECIAL_JOBSTOP_SIGS != 0)
                switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS);
        execvp(argv[0], argv);
+       e = 2;
+       if (errno == EACCES) e = 126;
+       if (errno == ENOENT) e = 127;
        bb_perror_msg("can't execute '%s'", argv[0]);
-       _exit(127); /* bash compat */
+       _exit(e);
 }
 
 #if ENABLE_HUSH_MODE_X
@@ -6944,12 +7147,12 @@ static const char *get_cmdtext(struct pipe *pi)
         * On subsequent bg argv is trashed, but we won't use it */
        if (pi->cmdtext)
                return pi->cmdtext;
+
        argv = pi->cmds[0].argv;
-       if (!argv || !argv[0]) {
+       if (!argv) {
                pi->cmdtext = xzalloc(1);
                return pi->cmdtext;
        }
-
        len = 0;
        do {
                len += strlen(*argv) + 1;
@@ -6958,9 +7161,7 @@ static const char *get_cmdtext(struct pipe *pi)
        pi->cmdtext = p;
        argv = pi->cmds[0].argv;
        do {
-               len = strlen(*argv);
-               memcpy(p, *argv, len);
-               p += len;
+               p = stpcpy(p, *argv);
                *p++ = ' ';
        } while (*++argv);
        p[-1] = '\0';
@@ -6995,7 +7196,7 @@ static void insert_bg_job(struct pipe *pi)
        job->cmdtext = xstrdup(get_cmdtext(pi));
 
        if (G_interactive_fd)
-               printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext);
+               printf("[%u] %u %s\n", job->jobid, (unsigned)job->cmds[0].pid, job->cmdtext);
        G.last_jobid = job->jobid;
 }
 
@@ -7025,34 +7226,175 @@ static void delete_finished_bg_job(struct pipe *pi)
 }
 #endif /* JOB */
 
-/* Check to see if any processes have exited -- if they
- * have, figure out why and see if a job has completed */
-static int checkjobs(struct pipe *fg_pipe)
+static int job_exited_or_stopped(struct pipe *pi)
+{
+       int rcode, i;
+
+       if (pi->alive_cmds != pi->stopped_cmds)
+               return -1;
+
+       /* All processes in fg pipe have exited or stopped */
+       rcode = 0;
+       i = pi->num_cmds;
+       while (--i >= 0) {
+               rcode = pi->cmds[i].cmd_exitcode;
+               /* usually last process gives overall exitstatus,
+                * but with "set -o pipefail", last *failed* process does */
+               if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0)
+                       break;
+       }
+       IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
+       return rcode;
+}
+
+static int process_wait_result(struct pipe *fg_pipe, pid_t childpid, int status)
 {
-       int attributes;
-       int status;
 #if ENABLE_HUSH_JOB
        struct pipe *pi;
 #endif
-       pid_t childpid;
-       int rcode = 0;
-
-       debug_printf_jobs("checkjobs %p\n", fg_pipe);
+       int i, dead;
 
-       attributes = WUNTRACED;
-       if (fg_pipe == NULL)
-               attributes |= WNOHANG;
+       dead = WIFEXITED(status) || WIFSIGNALED(status);
 
-       errno = 0;
-#if ENABLE_HUSH_FAST
-       if (G.handled_SIGCHLD == G.count_SIGCHLD) {
-//bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p",
-//getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe);
-               /* There was neither fork nor SIGCHLD since last waitpid */
-               /* Avoid doing waitpid syscall if possible */
-               if (!G.we_have_children) {
-                       errno = ECHILD;
-                       return -1;
+#if DEBUG_JOBS
+       if (WIFSTOPPED(status))
+               debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
+                               childpid, WSTOPSIG(status), WEXITSTATUS(status));
+       if (WIFSIGNALED(status))
+               debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
+                               childpid, WTERMSIG(status), WEXITSTATUS(status));
+       if (WIFEXITED(status))
+               debug_printf_jobs("pid %d exited, exitcode %d\n",
+                               childpid, WEXITSTATUS(status));
+#endif
+       /* Were we asked to wait for a fg pipe? */
+       if (fg_pipe) {
+               i = fg_pipe->num_cmds;
+
+               while (--i >= 0) {
+                       int rcode;
+
+                       debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid);
+                       if (fg_pipe->cmds[i].pid != childpid)
+                               continue;
+                       if (dead) {
+                               int ex;
+                               fg_pipe->cmds[i].pid = 0;
+                               fg_pipe->alive_cmds--;
+                               ex = WEXITSTATUS(status);
+                               /* bash prints killer signal's name for *last*
+                                * process in pipe (prints just newline for SIGINT/SIGPIPE).
+                                * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT)
+                                */
+                               if (WIFSIGNALED(status)) {
+                                       int sig = WTERMSIG(status);
+                                       if (i == fg_pipe->num_cmds-1)
+                                               /* TODO: use strsignal() instead for bash compat? but that's bloat... */
+                                               puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig));
+                                       /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */
+                                       /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here?
+                                        * Maybe we need to use sig | 128? */
+                                       ex = sig + 128;
+                               }
+                               fg_pipe->cmds[i].cmd_exitcode = ex;
+                       } else {
+                               fg_pipe->stopped_cmds++;
+                       }
+                       debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n",
+                                       fg_pipe->alive_cmds, fg_pipe->stopped_cmds);
+                       rcode = job_exited_or_stopped(fg_pipe);
+                       if (rcode >= 0) {
+/* Note: *non-interactive* bash does not continue if all processes in fg pipe
+ * are stopped. Testcase: "cat | cat" in a script (not on command line!)
+ * and "killall -STOP cat" */
+                               if (G_interactive_fd) {
+#if ENABLE_HUSH_JOB
+                                       if (fg_pipe->alive_cmds != 0)
+                                               insert_bg_job(fg_pipe);
+#endif
+                                       return rcode;
+                               }
+                               if (fg_pipe->alive_cmds == 0)
+                                       return rcode;
+                       }
+                       /* There are still running processes in the fg_pipe */
+                       return -1;
+               }
+               /* It wasn't in fg_pipe, look for process in bg pipes */
+       }
+
+#if ENABLE_HUSH_JOB
+       /* We were asked to wait for bg or orphaned children */
+       /* No need to remember exitcode in this case */
+       for (pi = G.job_list; pi; pi = pi->next) {
+               for (i = 0; i < pi->num_cmds; i++) {
+                       if (pi->cmds[i].pid == childpid)
+                               goto found_pi_and_prognum;
+               }
+       }
+       /* Happens when shell is used as init process (init=/bin/sh) */
+       debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
+       return -1; /* this wasn't a process from fg_pipe */
+
+ found_pi_and_prognum:
+       if (dead) {
+               /* child exited */
+               pi->cmds[i].pid = 0;
+               pi->cmds[i].cmd_exitcode = WEXITSTATUS(status);
+               if (WIFSIGNALED(status))
+                       pi->cmds[i].cmd_exitcode = 128 + WTERMSIG(status);
+               pi->alive_cmds--;
+               if (!pi->alive_cmds) {
+                       if (G_interactive_fd)
+                               printf(JOB_STATUS_FORMAT, pi->jobid,
+                                               "Done", pi->cmdtext);
+                       delete_finished_bg_job(pi);
+               }
+       } else {
+               /* child stopped */
+               pi->stopped_cmds++;
+       }
+#endif
+       return -1; /* this wasn't a process from fg_pipe */
+}
+
+/* Check to see if any processes have exited -- if they have,
+ * figure out why and see if a job has completed.
+ *
+ * If non-NULL fg_pipe: wait for its completion or stop.
+ * Return its exitcode or zero if stopped.
+ *
+ * Alternatively (fg_pipe == NULL, waitfor_pid != 0):
+ * waitpid(WNOHANG), if waitfor_pid exits or stops, return exitcode+1,
+ * else return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for)
+ * or 0 if no children changed status.
+ *
+ * Alternatively (fg_pipe == NULL, waitfor_pid == 0),
+ * return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for)
+ * or 0 if no children changed status.
+ */
+static int checkjobs(struct pipe *fg_pipe, pid_t waitfor_pid)
+{
+       int attributes;
+       int status;
+       int rcode = 0;
+
+       debug_printf_jobs("checkjobs %p\n", fg_pipe);
+
+       attributes = WUNTRACED;
+       if (fg_pipe == NULL)
+               attributes |= WNOHANG;
+
+       errno = 0;
+#if ENABLE_HUSH_FAST
+       if (G.handled_SIGCHLD == G.count_SIGCHLD) {
+//bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p",
+//getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe);
+               /* There was neither fork nor SIGCHLD since last waitpid */
+               /* Avoid doing waitpid syscall if possible */
+               if (!G.we_have_children) {
+                       errno = ECHILD;
+                       return -1;
                }
                if (fg_pipe == NULL) { /* is WNOHANG set? */
                        /* We have children, but they did not exit
@@ -7071,12 +7413,10 @@ static int checkjobs(struct pipe *fg_pipe)
  * 1   <========== bg pipe is not fully done, but exitcode is already known!
  * [hush 1.14.0: yes we do it right]
  */
- wait_more:
        while (1) {
-               int i;
-               int dead;
-
+               pid_t childpid;
 #if ENABLE_HUSH_FAST
+               int i;
                i = G.count_SIGCHLD;
 #endif
                childpid = waitpid(-1, &status, attributes);
@@ -7090,112 +7430,28 @@ static int checkjobs(struct pipe *fg_pipe)
 //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
                        }
 #endif
+                       /* ECHILD (no children), or 0 (no change in children status) */
+                       rcode = childpid;
                        break;
                }
-               dead = WIFEXITED(status) || WIFSIGNALED(status);
-
-#if DEBUG_JOBS
-               if (WIFSTOPPED(status))
-                       debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
-                                       childpid, WSTOPSIG(status), WEXITSTATUS(status));
-               if (WIFSIGNALED(status))
-                       debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
-                                       childpid, WTERMSIG(status), WEXITSTATUS(status));
-               if (WIFEXITED(status))
-                       debug_printf_jobs("pid %d exited, exitcode %d\n",
-                                       childpid, WEXITSTATUS(status));
-#endif
-               /* Were we asked to wait for fg pipe? */
-               if (fg_pipe) {
-                       i = fg_pipe->num_cmds;
-                       while (--i >= 0) {
-                               debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid);
-                               if (fg_pipe->cmds[i].pid != childpid)
-                                       continue;
-                               if (dead) {
-                                       int ex;
-                                       fg_pipe->cmds[i].pid = 0;
-                                       fg_pipe->alive_cmds--;
-                                       ex = WEXITSTATUS(status);
-                                       /* bash prints killer signal's name for *last*
-                                        * process in pipe (prints just newline for SIGINT/SIGPIPE).
-                                        * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT)
-                                        */
-                                       if (WIFSIGNALED(status)) {
-                                               int sig = WTERMSIG(status);
-                                               if (i == fg_pipe->num_cmds-1)
-                                                       /* TODO: use strsignal() instead for bash compat? but that's bloat... */
-                                                       puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig));
-                                               /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */
-                                               /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here?
-                                                * Maybe we need to use sig | 128? */
-                                               ex = sig + 128;
-                                       }
-                                       fg_pipe->cmds[i].cmd_exitcode = ex;
-                               } else {
-                                       fg_pipe->stopped_cmds++;
-                               }
-                               debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n",
-                                               fg_pipe->alive_cmds, fg_pipe->stopped_cmds);
-                               if (fg_pipe->alive_cmds == fg_pipe->stopped_cmds) {
-                                       /* All processes in fg pipe have exited or stopped */
-                                       i = fg_pipe->num_cmds;
-                                       while (--i >= 0) {
-                                               rcode = fg_pipe->cmds[i].cmd_exitcode;
-                                               /* usually last process gives overall exitstatus,
-                                                * but with "set -o pipefail", last *failed* process does */
-                                               if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0)
-                                                       break;
-                                       }
-                                       IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;)
-/* Note: *non-interactive* bash does not continue if all processes in fg pipe
- * are stopped. Testcase: "cat | cat" in a script (not on command line!)
- * and "killall -STOP cat" */
-                                       if (G_interactive_fd) {
-#if ENABLE_HUSH_JOB
-                                               if (fg_pipe->alive_cmds != 0)
-                                                       insert_bg_job(fg_pipe);
-#endif
-                                               return rcode;
-                                       }
-                                       if (fg_pipe->alive_cmds == 0)
-                                               return rcode;
-                               }
-                               /* There are still running processes in the fg pipe */
-                               goto wait_more; /* do waitpid again */
-                       }
-                       /* it wasnt fg_pipe, look for process in bg pipes */
-               }
-
-#if ENABLE_HUSH_JOB
-               /* We asked to wait for bg or orphaned children */
-               /* No need to remember exitcode in this case */
-               for (pi = G.job_list; pi; pi = pi->next) {
-                       for (i = 0; i < pi->num_cmds; i++) {
-                               if (pi->cmds[i].pid == childpid)
-                                       goto found_pi_and_prognum;
-                       }
-               }
-               /* Happens when shell is used as init process (init=/bin/sh) */
-               debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
-               continue; /* do waitpid again */
-
- found_pi_and_prognum:
-               if (dead) {
-                       /* child exited */
-                       pi->cmds[i].pid = 0;
-                       pi->alive_cmds--;
-                       if (!pi->alive_cmds) {
-                               if (G_interactive_fd)
-                                       printf(JOB_STATUS_FORMAT, pi->jobid,
-                                                       "Done", pi->cmdtext);
-                               delete_finished_bg_job(pi);
-                       }
-               } else {
-                       /* child stopped */
-                       pi->stopped_cmds++;
+               rcode = process_wait_result(fg_pipe, childpid, status);
+               if (rcode >= 0) {
+                       /* fg_pipe exited or stopped */
+                       break;
                }
-#endif
+               if (childpid == waitfor_pid) {
+                       debug_printf_exec("childpid==waitfor_pid:%d status:0x%08x\n", childpid, status);
+                       rcode = WEXITSTATUS(status);
+                       if (WIFSIGNALED(status))
+                               rcode = 128 + WTERMSIG(status);
+                       if (WIFSTOPPED(status))
+                               /* bash: "cmd & wait $!" and cmd stops: $? = 128 + stopsig */
+                               rcode = 128 + WSTOPSIG(status);
+                       rcode++;
+                       break; /* "wait PID" called us, give it exitcode+1 */
+               }
+               /* This wasn't one of our processes, or */
+               /* fg_pipe still has running processes, do waitpid again */
        } /* while (waitpid succeeds)... */
 
        return rcode;
@@ -7205,7 +7461,7 @@ static int checkjobs(struct pipe *fg_pipe)
 static int checkjobs_and_fg_shell(struct pipe *fg_pipe)
 {
        pid_t p;
-       int rcode = checkjobs(fg_pipe);
+       int rcode = checkjobs(fg_pipe, 0 /*(no pid to wait for)*/);
        if (G_saved_tty_pgrp) {
                /* Job finished, move the shell to the foreground */
                p = getpgrp(); /* our process group id */
@@ -7415,7 +7671,7 @@ static NOINLINE int run_pipe(struct pipe *pi)
                }
 
                /* Expand the rest into (possibly) many strings each */
-#if ENABLE_HUSH_BASH_COMPAT
+#if BASH_TEST2
                if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) {
                        argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt);
                } else
@@ -7711,6 +7967,8 @@ static int run_list(struct pipe *pi)
 
        /* Go through list of pipes, (maybe) executing them. */
        for (; pi; pi = IF_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) {
+               int r;
+
                if (G.flag_SIGINT)
                        break;
                if (G_flag_return_in_progress == 1)
@@ -7808,12 +8066,14 @@ static int run_list(struct pipe *pi)
 #endif
 #if ENABLE_HUSH_CASE
                if (rword == RES_CASE) {
+                       debug_printf_exec("CASE cond_code:%d\n", cond_code);
                        case_word = expand_strvec_to_string(pi->cmds->argv);
                        continue;
                }
                if (rword == RES_MATCH) {
                        char **argv;
 
+                       debug_printf_exec("MATCH cond_code:%d\n", cond_code);
                        if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */
                                break;
                        /* all prev words didn't match, does this one match? */
@@ -7824,8 +8084,8 @@ static int run_list(struct pipe *pi)
                                cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0);
                                free(pattern);
                                if (cond_code == 0) { /* match! we will execute this branch */
-                                       free(case_word); /* make future "word)" stop */
-                                       case_word = NULL;
+                                       free(case_word);
+                                       case_word = NULL; /* make future "word)" stop */
                                        break;
                                }
                                argv++;
@@ -7833,9 +8093,17 @@ static int run_list(struct pipe *pi)
                        continue;
                }
                if (rword == RES_CASE_BODY) { /* inside of a case branch */
+                       debug_printf_exec("CASE_BODY cond_code:%d\n", cond_code);
                        if (cond_code != 0)
                                continue; /* not matched yet, skip this pipe */
                }
+               if (rword == RES_ESAC) {
+                       debug_printf_exec("ESAC cond_code:%d\n", cond_code);
+                       if (case_word) {
+                               /* "case" did not match anything: still set $? (to 0) */
+                               G.last_exitcode = rcode = EXIT_SUCCESS;
+                       }
+               }
 #endif
                /* Just pressing <enter> in shell should check for jobs.
                 * OTOH, in non-interactive shell this is useless
@@ -7851,74 +8119,72 @@ static int run_list(struct pipe *pi)
                 * after run_pipe to collect any background children,
                 * even if list execution is to be stopped. */
                debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds);
-               {
-                       int r;
 #if ENABLE_HUSH_LOOPS
-                       G.flag_break_continue = 0;
-#endif
-                       rcode = r = run_pipe(pi); /* NB: rcode is a smallint */
-                       if (r != -1) {
-                               /* We ran a builtin, function, or group.
-                                * rcode is already known
-                                * and we don't need to wait for anything. */
-                               G.last_exitcode = rcode;
-                               debug_printf_exec(": builtin/func exitcode %d\n", rcode);
-                               check_and_run_traps();
-#if ENABLE_HUSH_LOOPS
-                               /* Was it "break" or "continue"? */
-                               if (G.flag_break_continue) {
-                                       smallint fbc = G.flag_break_continue;
-                                       /* We might fall into outer *loop*,
-                                        * don't want to break it too */
-                                       if (loop_top) {
-                                               G.depth_break_continue--;
-                                               if (G.depth_break_continue == 0)
-                                                       G.flag_break_continue = 0;
-                                               /* else: e.g. "continue 2" should *break* once, *then* continue */
-                                       } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */
-                                       if (G.depth_break_continue != 0 || fbc == BC_BREAK) {
-                                               checkjobs(NULL);
-                                               break;
-                                       }
-                                       /* "continue": simulate end of loop */
-                                       rword = RES_DONE;
-                                       continue;
-                               }
+               G.flag_break_continue = 0;
 #endif
-                               if (G_flag_return_in_progress == 1) {
-                                       checkjobs(NULL);
+               rcode = r = run_pipe(pi); /* NB: rcode is a smalluint, r is int */
+               if (r != -1) {
+                       /* We ran a builtin, function, or group.
+                        * rcode is already known
+                        * and we don't need to wait for anything. */
+                       debug_printf_exec(": builtin/func exitcode %d\n", rcode);
+                       G.last_exitcode = rcode;
+                       check_and_run_traps();
+#if ENABLE_HUSH_LOOPS
+                       /* Was it "break" or "continue"? */
+                       if (G.flag_break_continue) {
+                               smallint fbc = G.flag_break_continue;
+                               /* We might fall into outer *loop*,
+                                * don't want to break it too */
+                               if (loop_top) {
+                                       G.depth_break_continue--;
+                                       if (G.depth_break_continue == 0)
+                                               G.flag_break_continue = 0;
+                                       /* else: e.g. "continue 2" should *break* once, *then* continue */
+                               } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */
+                               if (G.depth_break_continue != 0 || fbc == BC_BREAK) {
+                                       checkjobs(NULL, 0 /*(no pid to wait for)*/);
                                        break;
                                }
-                       } else if (pi->followup == PIPE_BG) {
-                               /* What does bash do with attempts to background builtins? */
-                               /* even bash 3.2 doesn't do that well with nested bg:
-                                * try "{ { sleep 10; echo DEEP; } & echo HERE; } &".
-                                * I'm NOT treating inner &'s as jobs */
-                               check_and_run_traps();
-#if ENABLE_HUSH_JOB
-                               if (G.run_list_level == 1)
-                                       insert_bg_job(pi);
+                               /* "continue": simulate end of loop */
+                               rword = RES_DONE;
+                               continue;
+                       }
 #endif
-                               /* Last command's pid goes to $! */
-                               G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid;
-                               G.last_exitcode = rcode = EXIT_SUCCESS;
-                               debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n");
-                       } else {
+                       if (G_flag_return_in_progress == 1) {
+                               checkjobs(NULL, 0 /*(no pid to wait for)*/);
+                               break;
+                       }
+               } else if (pi->followup == PIPE_BG) {
+                       /* What does bash do with attempts to background builtins? */
+                       /* even bash 3.2 doesn't do that well with nested bg:
+                        * try "{ { sleep 10; echo DEEP; } & echo HERE; } &".
+                        * I'm NOT treating inner &'s as jobs */
 #if ENABLE_HUSH_JOB
-                               if (G.run_list_level == 1 && G_interactive_fd) {
-                                       /* Waits for completion, then fg's main shell */
-                                       rcode = checkjobs_and_fg_shell(pi);
-                                       debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode);
-                                       check_and_run_traps();
-                               } else
-#endif
-                               { /* This one just waits for completion */
-                                       rcode = checkjobs(pi);
-                                       debug_printf_exec(": checkjobs exitcode %d\n", rcode);
-                                       check_and_run_traps();
-                               }
-                               G.last_exitcode = rcode;
+                       if (G.run_list_level == 1)
+                               insert_bg_job(pi);
+#endif
+                       /* Last command's pid goes to $! */
+                       G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid;
+                       debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n");
+/* Check pi->pi_inverted? "! sleep 1 & echo $?": bash says 1. dash and ash says 0 */
+                       rcode = EXIT_SUCCESS;
+                       goto check_traps;
+               } else {
+#if ENABLE_HUSH_JOB
+                       if (G.run_list_level == 1 && G_interactive_fd) {
+                               /* Waits for completion, then fg's main shell */
+                               rcode = checkjobs_and_fg_shell(pi);
+                               debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode);
+                               goto check_traps;
                        }
+#endif
+                       /* This one just waits for completion */
+                       rcode = checkjobs(pi, 0 /*(no pid to wait for)*/);
+                       debug_printf_exec(": checkjobs exitcode %d\n", rcode);
+ check_traps:
+                       G.last_exitcode = rcode;
+                       check_and_run_traps();
                }
 
                /* Analyze how result affects subsequent commands */
@@ -7927,7 +8193,7 @@ static int run_list(struct pipe *pi)
                        cond_code = rcode;
 #endif
  check_jobs_and_continue:
-               checkjobs(NULL);
+               checkjobs(NULL, 0 /*(no pid to wait for)*/);
  dont_check_jobs_but_continue: ;
 #if ENABLE_HUSH_LOOPS
                /* Beware of "while false; true; do ..."! */
@@ -8005,10 +8271,12 @@ static void install_sighandlers(unsigned mask)
                if (old_handler == SIG_IGN) {
                        /* oops... restore back to IGN, and record this fact */
                        install_sighandler(sig, old_handler);
-                       if (!G.traps)
-                               G.traps = xzalloc(sizeof(G.traps[0]) * NSIG);
-                       free(G.traps[sig]);
-                       G.traps[sig] = xzalloc(1); /* == xstrdup(""); */
+#if ENABLE_HUSH_TRAP
+                       if (!G_traps)
+                               G_traps = xzalloc(sizeof(G_traps[0]) * NSIG);
+                       free(G_traps[sig]);
+                       G_traps[sig] = xzalloc(1); /* == xstrdup(""); */
+#endif
                }
        }
 }
@@ -8042,11 +8310,11 @@ static void install_fatal_sighandlers(void)
 
        /* We will restore tty pgrp on these signals */
        mask = 0
-               + (1 << SIGILL ) * HUSH_DEBUG
-               + (1 << SIGFPE ) * HUSH_DEBUG
+               /*+ (1 << SIGILL ) * HUSH_DEBUG*/
+               /*+ (1 << SIGFPE ) * HUSH_DEBUG*/
                + (1 << SIGBUS ) * HUSH_DEBUG
                + (1 << SIGSEGV) * HUSH_DEBUG
-               + (1 << SIGTRAP) * HUSH_DEBUG
+               /*+ (1 << SIGTRAP) * HUSH_DEBUG*/
                + (1 << SIGABRT)
        /* bash 3.2 seems to handle these just like 'fatal' ones */
                + (1 << SIGPIPE)
@@ -8159,7 +8427,7 @@ int hush_main(int argc, char **argv)
        /* Export PWD */
        set_pwd_var(/*exp:*/ 1);
 
-#if ENABLE_HUSH_BASH_COMPAT
+#if BASH_HOSTNAME_VAR
        /* Set (but not export) HOSTNAME unless already set */
        if (!get_local_var_value("HOSTNAME")) {
                struct utsname uts;
@@ -8309,10 +8577,10 @@ int hush_main(int argc, char **argv)
                        if (empty_trap_mask != 0) {
                                int sig;
                                install_special_sighandlers();
-                               G.traps = xzalloc(sizeof(G.traps[0]) * NSIG);
+                               G_traps = xzalloc(sizeof(G_traps[0]) * NSIG);
                                for (sig = 1; sig < NSIG; sig++) {
                                        if (empty_trap_mask & (1LL << sig)) {
-                                               G.traps[sig] = xzalloc(1); /* == xstrdup(""); */
+                                               G_traps[sig] = xzalloc(1); /* == xstrdup(""); */
                                                install_sighandler(sig, SIG_IGN);
                                        }
                                }
@@ -8389,11 +8657,14 @@ int hush_main(int argc, char **argv)
                 * "bash <script>" (which is never interactive (unless -i?))
                 * sources $BASH_ENV here (without scanning $PATH).
                 * If called as sh, does the same but with $ENV.
+                * Also NB, per POSIX, $ENV should undergo parameter expansion.
                 */
                G.global_argc--;
                G.global_argv++;
                debug_printf("running script '%s'\n", G.global_argv[0]);
+               xfunc_error_retval = 127; /* for "hush /does/not/exist" case */
                input = xfopen_for_read(G.global_argv[0]);
+               xfunc_error_retval = 1;
                remember_FILE(input);
                install_special_sighandlers();
                parse_and_run_file(input);
@@ -8553,6 +8824,7 @@ static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM)
        return 0;
 }
 
+#if ENABLE_HUSH_TEST || ENABLE_HUSH_ECHO || ENABLE_HUSH_PRINTF || ENABLE_HUSH_KILL
 static int run_applet_main(char **argv, int (*applet_main_func)(int argc, char **argv))
 {
        int argc = 0;
@@ -8562,24 +8834,50 @@ static int run_applet_main(char **argv, int (*applet_main_func)(int argc, char *
        }
        return applet_main_func(argc, argv - argc);
 }
-
+#endif
+#if ENABLE_HUSH_TEST || BASH_TEST2
 static int FAST_FUNC builtin_test(char **argv)
 {
        return run_applet_main(argv, test_main);
 }
-
+#endif
+#if ENABLE_HUSH_ECHO
 static int FAST_FUNC builtin_echo(char **argv)
 {
        return run_applet_main(argv, echo_main);
 }
-
-#if ENABLE_PRINTF
+#endif
+#if ENABLE_HUSH_PRINTF
 static int FAST_FUNC builtin_printf(char **argv)
 {
        return run_applet_main(argv, printf_main);
 }
 #endif
 
+#if ENABLE_HUSH_HELP
+static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM)
+{
+       const struct built_in_command *x;
+
+       printf(
+               "Built-in commands:\n"
+               "------------------\n");
+       for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) {
+               if (x->b_descr)
+                       printf("%-10s%s\n", x->b_cmd, x->b_descr);
+       }
+       return EXIT_SUCCESS;
+}
+#endif
+
+#if MAX_HISTORY && ENABLE_FEATURE_EDITING
+static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM)
+{
+       show_history(G.line_input_state);
+       return EXIT_SUCCESS;
+}
+#endif
+
 static char **skip_dash_dash(char **argv)
 {
        argv++;
@@ -8588,24 +8886,6 @@ static char **skip_dash_dash(char **argv)
        return argv;
 }
 
-static int FAST_FUNC builtin_eval(char **argv)
-{
-       int rcode = EXIT_SUCCESS;
-
-       argv = skip_dash_dash(argv);
-       if (*argv) {
-               char *str = expand_strvec_to_string(argv);
-               /* bash:
-                * eval "echo Hi; done" ("done" is syntax error):
-                * "echo Hi" will not execute too.
-                */
-               parse_and_run_string(str);
-               free(str);
-               rcode = G.last_exitcode;
-       }
-       return rcode;
-}
-
 static int FAST_FUNC builtin_cd(char **argv)
 {
        const char *newdir;
@@ -8633,6 +8913,30 @@ static int FAST_FUNC builtin_cd(char **argv)
        return EXIT_SUCCESS;
 }
 
+static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM)
+{
+       puts(get_cwd(0));
+       return EXIT_SUCCESS;
+}
+
+static int FAST_FUNC builtin_eval(char **argv)
+{
+       int rcode = EXIT_SUCCESS;
+
+       argv = skip_dash_dash(argv);
+       if (*argv) {
+               char *str = expand_strvec_to_string(argv);
+               /* bash:
+                * eval "echo Hi; done" ("done" is syntax error):
+                * "echo Hi" will not execute too.
+                */
+               parse_and_run_string(str);
+               free(str);
+               rcode = G.last_exitcode;
+       }
+       return rcode;
+}
+
 static int FAST_FUNC builtin_exec(char **argv)
 {
        argv = skip_dash_dash(argv);
@@ -8678,51 +8982,195 @@ static int FAST_FUNC builtin_exit(char **argv)
        hush_exit(xatoi(argv[0]) & 0xff);
 }
 
-static void print_escaped(const char *s)
+#if ENABLE_HUSH_TYPE
+/* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */
+static int FAST_FUNC builtin_type(char **argv)
 {
-       if (*s == '\'')
-               goto squote;
-       do {
-               const char *p = strchrnul(s, '\'');
-               /* print 'xxxx', possibly just '' */
-               printf("'%.*s'", (int)(p - s), s);
-               if (*p == '\0')
-                       break;
-               s = p;
- squote:
-               /* s points to '; print "'''...'''" */
-               putchar('"');
-               do putchar('\''); while (*++s == '\'');
-               putchar('"');
-       } while (*s);
-}
+       int ret = EXIT_SUCCESS;
 
-#if !ENABLE_HUSH_LOCAL
-#define helper_export_local(argv, exp, lvl) \
-       helper_export_local(argv, exp)
-#endif
-static void helper_export_local(char **argv, int exp, int lvl)
-{
-       do {
-               char *name = *argv;
-               char *name_end = strchrnul(name, '=');
+       while (*++argv) {
+               const char *type;
+               char *path = NULL;
 
-               /* So far we do not check that name is valid (TODO?) */
+               if (0) {} /* make conditional compile easier below */
+               /*else if (find_alias(*argv))
+                       type = "an alias";*/
+#if ENABLE_HUSH_FUNCTIONS
+               else if (find_function(*argv))
+                       type = "a function";
+#endif
+               else if (find_builtin(*argv))
+                       type = "a shell builtin";
+               else if ((path = find_in_path(*argv)) != NULL)
+                       type = path;
+               else {
+                       bb_error_msg("type: %s: not found", *argv);
+                       ret = EXIT_FAILURE;
+                       continue;
+               }
 
-               if (*name_end == '\0') {
-                       struct variable *var, **vpp;
+               printf("%s is %s\n", *argv, type);
+               free(path);
+       }
 
-                       vpp = get_ptr_to_local_var(name, name_end - name);
-                       var = vpp ? *vpp : NULL;
+       return ret;
+}
+#endif
 
-                       if (exp == -1) { /* unexporting? */
-                               /* export -n NAME (without =VALUE) */
-                               if (var) {
-                                       var->flg_export = 0;
-                                       debug_printf_env("%s: unsetenv '%s'\n", __func__, name);
-                                       unsetenv(name);
-                               } /* else: export -n NOT_EXISTING_VAR: no-op */
-                               continue;
+#if ENABLE_HUSH_READ
+/* Interruptibility of read builtin in bash
+ * (tested on bash-4.2.8 by sending signals (not by ^C)):
+ *
+ * Empty trap makes read ignore corresponding signal, for any signal.
+ *
+ * SIGINT:
+ * - terminates non-interactive shell;
+ * - interrupts read in interactive shell;
+ * if it has non-empty trap:
+ * - executes trap and returns to command prompt in interactive shell;
+ * - executes trap and returns to read in non-interactive shell;
+ * SIGTERM:
+ * - is ignored (does not interrupt) read in interactive shell;
+ * - terminates non-interactive shell;
+ * if it has non-empty trap:
+ * - executes trap and returns to read;
+ * SIGHUP:
+ * - terminates shell (regardless of interactivity);
+ * if it has non-empty trap:
+ * - executes trap and returns to read;
+ */
+static int FAST_FUNC builtin_read(char **argv)
+{
+       const char *r;
+       char *opt_n = NULL;
+       char *opt_p = NULL;
+       char *opt_t = NULL;
+       char *opt_u = NULL;
+       const char *ifs;
+       int read_flags;
+
+       /* "!": do not abort on errors.
+        * Option string must start with "sr" to match BUILTIN_READ_xxx
+        */
+       read_flags = getopt32(argv, "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u);
+       if (read_flags == (uint32_t)-1)
+               return EXIT_FAILURE;
+       argv += optind;
+       ifs = get_local_var_value("IFS"); /* can be NULL */
+
+ again:
+       r = shell_builtin_read(set_local_var_from_halves,
+               argv,
+               ifs,
+               read_flags,
+               opt_n,
+               opt_p,
+               opt_t,
+               opt_u
+       );
+
+       if ((uintptr_t)r == 1 && errno == EINTR) {
+               unsigned sig = check_and_run_traps();
+               if (sig && sig != SIGINT)
+                       goto again;
+       }
+
+       if ((uintptr_t)r > 1) {
+               bb_error_msg("%s", r);
+               r = (char*)(uintptr_t)1;
+       }
+
+       return (uintptr_t)r;
+}
+#endif
+
+#if ENABLE_HUSH_UMASK
+static int FAST_FUNC builtin_umask(char **argv)
+{
+       int rc;
+       mode_t mask;
+
+       rc = 1;
+       mask = umask(0);
+       argv = skip_dash_dash(argv);
+       if (argv[0]) {
+               mode_t old_mask = mask;
+
+               /* numeric umasks are taken as-is */
+               /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */
+               if (!isdigit(argv[0][0]))
+                       mask ^= 0777;
+               mask = bb_parse_mode(argv[0], mask);
+               if (!isdigit(argv[0][0]))
+                       mask ^= 0777;
+               if ((unsigned)mask > 0777) {
+                       mask = old_mask;
+                       /* bash messages:
+                        * bash: umask: 'q': invalid symbolic mode operator
+                        * bash: umask: 999: octal number out of range
+                        */
+                       bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]);
+                       rc = 0;
+               }
+       } else {
+               /* Mimic bash */
+               printf("%04o\n", (unsigned) mask);
+               /* fall through and restore mask which we set to 0 */
+       }
+       umask(mask);
+
+       return !rc; /* rc != 0 - success */
+}
+#endif
+
+#if ENABLE_HUSH_EXPORT || ENABLE_HUSH_TRAP
+static void print_escaped(const char *s)
+{
+       if (*s == '\'')
+               goto squote;
+       do {
+               const char *p = strchrnul(s, '\'');
+               /* print 'xxxx', possibly just '' */
+               printf("'%.*s'", (int)(p - s), s);
+               if (*p == '\0')
+                       break;
+               s = p;
+ squote:
+               /* s points to '; print "'''...'''" */
+               putchar('"');
+               do putchar('\''); while (*++s == '\'');
+               putchar('"');
+       } while (*s);
+}
+#endif
+
+#if ENABLE_HUSH_EXPORT || ENABLE_HUSH_LOCAL
+# if !ENABLE_HUSH_LOCAL
+#define helper_export_local(argv, exp, lvl) \
+       helper_export_local(argv, exp)
+# endif
+static void helper_export_local(char **argv, int exp, int lvl)
+{
+       do {
+               char *name = *argv;
+               char *name_end = strchrnul(name, '=');
+
+               /* So far we do not check that name is valid (TODO?) */
+
+               if (*name_end == '\0') {
+                       struct variable *var, **vpp;
+
+                       vpp = get_ptr_to_local_var(name, name_end - name);
+                       var = vpp ? *vpp : NULL;
+
+                       if (exp == -1) { /* unexporting? */
+                               /* export -n NAME (without =VALUE) */
+                               if (var) {
+                                       var->flg_export = 0;
+                                       debug_printf_env("%s: unsetenv '%s'\n", __func__, name);
+                                       unsetenv(name);
+                               } /* else: export -n NOT_EXISTING_VAR: no-op */
+                               continue;
                        }
                        if (exp == 1) { /* exporting? */
                                /* export NAME (without =VALUE) */
@@ -8733,6 +9181,14 @@ static void helper_export_local(char **argv, int exp, int lvl)
                                        continue;
                                }
                        }
+# if ENABLE_HUSH_LOCAL
+                       if (exp == 0 /* local? */
+                        && var && var->func_nest_level == lvl
+                       ) {
+                               /* "local x=abc; ...; local x" - ignore second local decl */
+                               continue;
+                       }
+# endif
                        /* Exporting non-existing variable.
                         * bash does not put it in environment,
                         * but remembers that it is exported,
@@ -8748,7 +9204,9 @@ static void helper_export_local(char **argv, int exp, int lvl)
                set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0);
        } while (*++argv);
 }
+#endif
 
+#if ENABLE_HUSH_EXPORT
 static int FAST_FUNC builtin_export(char **argv)
 {
        unsigned opt_unexport;
@@ -8785,361 +9243,69 @@ static int FAST_FUNC builtin_export(char **argv)
                                putchar('\n');
 #endif
                        }
-                       /*fflush_all(); - done after each builtin anyway */
-               }
-               return EXIT_SUCCESS;
-       }
-
-       helper_export_local(argv, (opt_unexport ? -1 : 1), 0);
-
-       return EXIT_SUCCESS;
-}
-
-#if ENABLE_HUSH_LOCAL
-static int FAST_FUNC builtin_local(char **argv)
-{
-       if (G.func_nest_level == 0) {
-               bb_error_msg("%s: not in a function", argv[0]);
-               return EXIT_FAILURE; /* bash compat */
-       }
-       helper_export_local(argv, 0, G.func_nest_level);
-       return EXIT_SUCCESS;
-}
-#endif
-
-static int FAST_FUNC builtin_trap(char **argv)
-{
-       int sig;
-       char *new_cmd;
-
-       if (!G.traps)
-               G.traps = xzalloc(sizeof(G.traps[0]) * NSIG);
-
-       argv++;
-       if (!*argv) {
-               int i;
-               /* No args: print all trapped */
-               for (i = 0; i < NSIG; ++i) {
-                       if (G.traps[i]) {
-                               printf("trap -- ");
-                               print_escaped(G.traps[i]);
-                               /* note: bash adds "SIG", but only if invoked
-                                * as "bash". If called as "sh", or if set -o posix,
-                                * then it prints short signal names.
-                                * We are printing short names: */
-                               printf(" %s\n", get_signame(i));
-                       }
-               }
-               /*fflush_all(); - done after each builtin anyway */
-               return EXIT_SUCCESS;
-       }
-
-       new_cmd = NULL;
-       /* If first arg is a number: reset all specified signals */
-       sig = bb_strtou(*argv, NULL, 10);
-       if (errno == 0) {
-               int ret;
- process_sig_list:
-               ret = EXIT_SUCCESS;
-               while (*argv) {
-                       sighandler_t handler;
-
-                       sig = get_signum(*argv++);
-                       if (sig < 0 || sig >= NSIG) {
-                               ret = EXIT_FAILURE;
-                               /* Mimic bash message exactly */
-                               bb_perror_msg("trap: %s: invalid signal specification", argv[-1]);
-                               continue;
-                       }
-
-                       free(G.traps[sig]);
-                       G.traps[sig] = xstrdup(new_cmd);
-
-                       debug_printf("trap: setting SIG%s (%i) to '%s'\n",
-                               get_signame(sig), sig, G.traps[sig]);
-
-                       /* There is no signal for 0 (EXIT) */
-                       if (sig == 0)
-                               continue;
-
-                       if (new_cmd)
-                               handler = (new_cmd[0] ? record_pending_signo : SIG_IGN);
-                       else
-                               /* We are removing trap handler */
-                               handler = pick_sighandler(sig);
-                       install_sighandler(sig, handler);
-               }
-               return ret;
-       }
-
-       if (!argv[1]) { /* no second arg */
-               bb_error_msg("trap: invalid arguments");
-               return EXIT_FAILURE;
-       }
-
-       /* First arg is "-": reset all specified to default */
-       /* First arg is "--": skip it, the rest is "handler SIGs..." */
-       /* Everything else: set arg as signal handler
-        * (includes "" case, which ignores signal) */
-       if (argv[0][0] == '-') {
-               if (argv[0][1] == '\0') { /* "-" */
-                       /* new_cmd remains NULL: "reset these sigs" */
-                       goto reset_traps;
-               }
-               if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */
-                       argv++;
-               }
-               /* else: "-something", no special meaning */
-       }
-       new_cmd = *argv;
- reset_traps:
-       argv++;
-       goto process_sig_list;
-}
-
-/* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */
-static int FAST_FUNC builtin_type(char **argv)
-{
-       int ret = EXIT_SUCCESS;
-
-       while (*++argv) {
-               const char *type;
-               char *path = NULL;
-
-               if (0) {} /* make conditional compile easier below */
-               /*else if (find_alias(*argv))
-                       type = "an alias";*/
-#if ENABLE_HUSH_FUNCTIONS
-               else if (find_function(*argv))
-                       type = "a function";
-#endif
-               else if (find_builtin(*argv))
-                       type = "a shell builtin";
-               else if ((path = find_in_path(*argv)) != NULL)
-                       type = path;
-               else {
-                       bb_error_msg("type: %s: not found", *argv);
-                       ret = EXIT_FAILURE;
-                       continue;
-               }
-
-               printf("%s is %s\n", *argv, type);
-               free(path);
-       }
-
-       return ret;
-}
-
-#if ENABLE_HUSH_JOB
-/* built-in 'fg' and 'bg' handler */
-static int FAST_FUNC builtin_fg_bg(char **argv)
-{
-       int i, jobnum;
-       struct pipe *pi;
-
-       if (!G_interactive_fd)
-               return EXIT_FAILURE;
-
-       /* If they gave us no args, assume they want the last backgrounded task */
-       if (!argv[1]) {
-               for (pi = G.job_list; pi; pi = pi->next) {
-                       if (pi->jobid == G.last_jobid) {
-                               goto found;
-                       }
-               }
-               bb_error_msg("%s: no current job", argv[0]);
-               return EXIT_FAILURE;
-       }
-       if (sscanf(argv[1], "%%%d", &jobnum) != 1) {
-               bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]);
-               return EXIT_FAILURE;
-       }
-       for (pi = G.job_list; pi; pi = pi->next) {
-               if (pi->jobid == jobnum) {
-                       goto found;
-               }
-       }
-       bb_error_msg("%s: %d: no such job", argv[0], jobnum);
-       return EXIT_FAILURE;
- found:
-       /* TODO: bash prints a string representation
-        * of job being foregrounded (like "sleep 1 | cat") */
-       if (argv[0][0] == 'f' && G_saved_tty_pgrp) {
-               /* Put the job into the foreground.  */
-               tcsetpgrp(G_interactive_fd, pi->pgrp);
-       }
-
-       /* Restart the processes in the job */
-       debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp);
-       for (i = 0; i < pi->num_cmds; i++) {
-               debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid);
-       }
-       pi->stopped_cmds = 0;
-
-       i = kill(- pi->pgrp, SIGCONT);
-       if (i < 0) {
-               if (errno == ESRCH) {
-                       delete_finished_bg_job(pi);
-                       return EXIT_SUCCESS;
-               }
-               bb_perror_msg("kill (SIGCONT)");
-       }
-
-       if (argv[0][0] == 'f') {
-               remove_bg_job(pi);
-               return checkjobs_and_fg_shell(pi);
-       }
-       return EXIT_SUCCESS;
-}
-#endif
-
-#if ENABLE_HUSH_HELP
-static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM)
-{
-       const struct built_in_command *x;
-
-       printf(
-               "Built-in commands:\n"
-               "------------------\n");
-       for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) {
-               if (x->b_descr)
-                       printf("%-10s%s\n", x->b_cmd, x->b_descr);
-       }
-       return EXIT_SUCCESS;
-}
-#endif
-
-#if MAX_HISTORY && ENABLE_FEATURE_EDITING
-static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM)
-{
-       show_history(G.line_input_state);
-       return EXIT_SUCCESS;
-}
-#endif
-
-#if ENABLE_HUSH_JOB
-static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM)
-{
-       struct pipe *job;
-       const char *status_string;
-
-       for (job = G.job_list; job; job = job->next) {
-               if (job->alive_cmds == job->stopped_cmds)
-                       status_string = "Stopped";
-               else
-                       status_string = "Running";
-
-               printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
-       }
-       return EXIT_SUCCESS;
-}
-#endif
-
-#if HUSH_DEBUG
-static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM)
-{
-       void *p;
-       unsigned long l;
-
-# ifdef M_TRIM_THRESHOLD
-       /* Optional. Reduces probability of false positives */
-       malloc_trim(0);
-# endif
-       /* Crude attempt to find where "free memory" starts,
-        * sans fragmentation. */
-       p = malloc(240);
-       l = (unsigned long)p;
-       free(p);
-       p = malloc(3400);
-       if (l < (unsigned long)p) l = (unsigned long)p;
-       free(p);
-
-       if (!G.memleak_value)
-               G.memleak_value = l;
-
-       l -= G.memleak_value;
-       if ((long)l < 0)
-               l = 0;
-       l /= 1024;
-       if (l > 127)
-               l = 127;
-
-       /* Exitcode is "how many kilobytes we leaked since 1st call" */
-       return l;
-}
-#endif
-
-static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM)
-{
-       puts(get_cwd(0));
-       return EXIT_SUCCESS;
-}
-
-/* Interruptibility of read builtin in bash
- * (tested on bash-4.2.8 by sending signals (not by ^C)):
- *
- * Empty trap makes read ignore corresponding signal, for any signal.
- *
- * SIGINT:
- * - terminates non-interactive shell;
- * - interrupts read in interactive shell;
- * if it has non-empty trap:
- * - executes trap and returns to command prompt in interactive shell;
- * - executes trap and returns to read in non-interactive shell;
- * SIGTERM:
- * - is ignored (does not interrupt) read in interactive shell;
- * - terminates non-interactive shell;
- * if it has non-empty trap:
- * - executes trap and returns to read;
- * SIGHUP:
- * - terminates shell (regardless of interactivity);
- * if it has non-empty trap:
- * - executes trap and returns to read;
- */
-static int FAST_FUNC builtin_read(char **argv)
-{
-       const char *r;
-       char *opt_n = NULL;
-       char *opt_p = NULL;
-       char *opt_t = NULL;
-       char *opt_u = NULL;
-       const char *ifs;
-       int read_flags;
-
-       /* "!": do not abort on errors.
-        * Option string must start with "sr" to match BUILTIN_READ_xxx
-        */
-       read_flags = getopt32(argv, "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u);
-       if (read_flags == (uint32_t)-1)
-               return EXIT_FAILURE;
-       argv += optind;
-       ifs = get_local_var_value("IFS"); /* can be NULL */
-
- again:
-       r = shell_builtin_read(set_local_var_from_halves,
-               argv,
-               ifs,
-               read_flags,
-               opt_n,
-               opt_p,
-               opt_t,
-               opt_u
-       );
+                       /*fflush_all(); - done after each builtin anyway */
+               }
+               return EXIT_SUCCESS;
+       }
 
-       if ((uintptr_t)r == 1 && errno == EINTR) {
-               unsigned sig = check_and_run_traps();
-               if (sig && sig != SIGINT)
-                       goto again;
+       helper_export_local(argv, (opt_unexport ? -1 : 1), 0);
+
+       return EXIT_SUCCESS;
+}
+#endif
+
+#if ENABLE_HUSH_LOCAL
+static int FAST_FUNC builtin_local(char **argv)
+{
+       if (G.func_nest_level == 0) {
+               bb_error_msg("%s: not in a function", argv[0]);
+               return EXIT_FAILURE; /* bash compat */
        }
+       helper_export_local(argv, 0, G.func_nest_level);
+       return EXIT_SUCCESS;
+}
+#endif
 
-       if ((uintptr_t)r > 1) {
-               bb_error_msg("%s", r);
-               r = (char*)(uintptr_t)1;
+#if ENABLE_HUSH_UNSET
+/* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */
+static int FAST_FUNC builtin_unset(char **argv)
+{
+       int ret;
+       unsigned opts;
+
+       /* "!": do not abort on errors */
+       /* "+": stop at 1st non-option */
+       opts = getopt32(argv, "!+vf");
+       if (opts == (unsigned)-1)
+               return EXIT_FAILURE;
+       if (opts == 3) {
+               bb_error_msg("unset: -v and -f are exclusive");
+               return EXIT_FAILURE;
        }
+       argv += optind;
 
-       return (uintptr_t)r;
+       ret = EXIT_SUCCESS;
+       while (*argv) {
+               if (!(opts & 2)) { /* not -f */
+                       if (unset_local_var(*argv)) {
+                               /* unset <nonexistent_var> doesn't fail.
+                                * Error is when one tries to unset RO var.
+                                * Message was printed by unset_local_var. */
+                               ret = EXIT_FAILURE;
+                       }
+               }
+# if ENABLE_HUSH_FUNCTIONS
+               else {
+                       unset_func(*argv);
+               }
+# endif
+               argv++;
+       }
+       return ret;
 }
+#endif
 
+#if ENABLE_HUSH_SET
 /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set
  * built-in 'set' handler
  * SUSv3 says:
@@ -9222,6 +9388,7 @@ static int FAST_FUNC builtin_set(char **argv)
        bb_error_msg("set: %s: invalid option", arg);
        return EXIT_FAILURE;
 }
+#endif
 
 static int FAST_FUNC builtin_shift(char **argv)
 {
@@ -9231,150 +9398,419 @@ static int FAST_FUNC builtin_shift(char **argv)
                n = atoi(argv[0]);
        }
        if (n >= 0 && n < G.global_argc) {
-               if (G.global_args_malloced) {
+               if (G_global_args_malloced) {
                        int m = 1;
                        while (m <= n)
                                free(G.global_argv[m++]);
                }
-               G.global_argc -= n;
-               memmove(&G.global_argv[1], &G.global_argv[n+1],
-                               G.global_argc * sizeof(G.global_argv[0]));
-               return EXIT_SUCCESS;
+               G.global_argc -= n;
+               memmove(&G.global_argv[1], &G.global_argv[n+1],
+                               G.global_argc * sizeof(G.global_argv[0]));
+               return EXIT_SUCCESS;
+       }
+       return EXIT_FAILURE;
+}
+
+static int FAST_FUNC builtin_source(char **argv)
+{
+       char *arg_path, *filename;
+       FILE *input;
+       save_arg_t sv;
+       char *args_need_save;
+#if ENABLE_HUSH_FUNCTIONS
+       smallint sv_flg;
+#endif
+
+       argv = skip_dash_dash(argv);
+       filename = argv[0];
+       if (!filename) {
+               /* bash says: "bash: .: filename argument required" */
+               return 2; /* bash compat */
+       }
+       arg_path = NULL;
+       if (!strchr(filename, '/')) {
+               arg_path = find_in_path(filename);
+               if (arg_path)
+                       filename = arg_path;
+       }
+       input = remember_FILE(fopen_or_warn(filename, "r"));
+       free(arg_path);
+       if (!input) {
+               /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */
+               /* POSIX: non-interactive shell should abort here,
+                * not merely fail. So far no one complained :)
+                */
+               return EXIT_FAILURE;
+       }
+
+#if ENABLE_HUSH_FUNCTIONS
+       sv_flg = G_flag_return_in_progress;
+       /* "we are inside sourced file, ok to use return" */
+       G_flag_return_in_progress = -1;
+#endif
+       args_need_save = argv[1]; /* used as a boolean variable */
+       if (args_need_save)
+               save_and_replace_G_args(&sv, argv);
+
+       /* "false; . ./empty_line; echo Zero:$?" should print 0 */
+       G.last_exitcode = 0;
+       parse_and_run_file(input);
+       fclose_and_forget(input);
+
+       if (args_need_save) /* can't use argv[1] instead: "shift" can mangle it */
+               restore_G_args(&sv, argv);
+#if ENABLE_HUSH_FUNCTIONS
+       G_flag_return_in_progress = sv_flg;
+#endif
+
+       return G.last_exitcode;
+}
+
+#if ENABLE_HUSH_TRAP
+static int FAST_FUNC builtin_trap(char **argv)
+{
+       int sig;
+       char *new_cmd;
+
+       if (!G_traps)
+               G_traps = xzalloc(sizeof(G_traps[0]) * NSIG);
+
+       argv++;
+       if (!*argv) {
+               int i;
+               /* No args: print all trapped */
+               for (i = 0; i < NSIG; ++i) {
+                       if (G_traps[i]) {
+                               printf("trap -- ");
+                               print_escaped(G_traps[i]);
+                               /* note: bash adds "SIG", but only if invoked
+                                * as "bash". If called as "sh", or if set -o posix,
+                                * then it prints short signal names.
+                                * We are printing short names: */
+                               printf(" %s\n", get_signame(i));
+                       }
+               }
+               /*fflush_all(); - done after each builtin anyway */
+               return EXIT_SUCCESS;
+       }
+
+       new_cmd = NULL;
+       /* If first arg is a number: reset all specified signals */
+       sig = bb_strtou(*argv, NULL, 10);
+       if (errno == 0) {
+               int ret;
+ process_sig_list:
+               ret = EXIT_SUCCESS;
+               while (*argv) {
+                       sighandler_t handler;
+
+                       sig = get_signum(*argv++);
+                       if (sig < 0 || sig >= NSIG) {
+                               ret = EXIT_FAILURE;
+                               /* Mimic bash message exactly */
+                               bb_perror_msg("trap: %s: invalid signal specification", argv[-1]);
+                               continue;
+                       }
+
+                       free(G_traps[sig]);
+                       G_traps[sig] = xstrdup(new_cmd);
+
+                       debug_printf("trap: setting SIG%s (%i) to '%s'\n",
+                               get_signame(sig), sig, G_traps[sig]);
+
+                       /* There is no signal for 0 (EXIT) */
+                       if (sig == 0)
+                               continue;
+
+                       if (new_cmd)
+                               handler = (new_cmd[0] ? record_pending_signo : SIG_IGN);
+                       else
+                               /* We are removing trap handler */
+                               handler = pick_sighandler(sig);
+                       install_sighandler(sig, handler);
+               }
+               return ret;
+       }
+
+       if (!argv[1]) { /* no second arg */
+               bb_error_msg("trap: invalid arguments");
+               return EXIT_FAILURE;
+       }
+
+       /* First arg is "-": reset all specified to default */
+       /* First arg is "--": skip it, the rest is "handler SIGs..." */
+       /* Everything else: set arg as signal handler
+        * (includes "" case, which ignores signal) */
+       if (argv[0][0] == '-') {
+               if (argv[0][1] == '\0') { /* "-" */
+                       /* new_cmd remains NULL: "reset these sigs" */
+                       goto reset_traps;
+               }
+               if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */
+                       argv++;
+               }
+               /* else: "-something", no special meaning */
+       }
+       new_cmd = *argv;
+ reset_traps:
+       argv++;
+       goto process_sig_list;
+}
+#endif
+
+#if ENABLE_HUSH_JOB
+static struct pipe *parse_jobspec(const char *str)
+{
+       struct pipe *pi;
+       unsigned jobnum;
+
+       if (sscanf(str, "%%%u", &jobnum) != 1) {
+               if (str[0] != '%'
+                || (str[1] != '%' && str[1] != '+' && str[1] != '\0')
+               ) {
+                       bb_error_msg("bad argument '%s'", str);
+                       return NULL;
+               }
+               /* It is "%%", "%+" or "%" - current job */
+               jobnum = G.last_jobid;
+               if (jobnum == 0) {
+                       bb_error_msg("no current job");
+                       return NULL;
+               }
+       }
+       for (pi = G.job_list; pi; pi = pi->next) {
+               if (pi->jobid == jobnum) {
+                       return pi;
+               }
+       }
+       bb_error_msg("%u: no such job", jobnum);
+       return NULL;
+}
+
+static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM)
+{
+       struct pipe *job;
+       const char *status_string;
+
+       checkjobs(NULL, 0 /*(no pid to wait for)*/);
+       for (job = G.job_list; job; job = job->next) {
+               if (job->alive_cmds == job->stopped_cmds)
+                       status_string = "Stopped";
+               else
+                       status_string = "Running";
+
+               printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
+       }
+       return EXIT_SUCCESS;
+}
+
+/* built-in 'fg' and 'bg' handler */
+static int FAST_FUNC builtin_fg_bg(char **argv)
+{
+       int i;
+       struct pipe *pi;
+
+       if (!G_interactive_fd)
+               return EXIT_FAILURE;
+
+       /* If they gave us no args, assume they want the last backgrounded task */
+       if (!argv[1]) {
+               for (pi = G.job_list; pi; pi = pi->next) {
+                       if (pi->jobid == G.last_jobid) {
+                               goto found;
+                       }
+               }
+               bb_error_msg("%s: no current job", argv[0]);
+               return EXIT_FAILURE;
        }
-       return EXIT_FAILURE;
-}
-
-static int FAST_FUNC builtin_source(char **argv)
-{
-       char *arg_path, *filename;
-       FILE *input;
-       save_arg_t sv;
-#if ENABLE_HUSH_FUNCTIONS
-       smallint sv_flg;
-#endif
 
-       argv = skip_dash_dash(argv);
-       filename = argv[0];
-       if (!filename) {
-               /* bash says: "bash: .: filename argument required" */
-               return 2; /* bash compat */
-       }
-       arg_path = NULL;
-       if (!strchr(filename, '/')) {
-               arg_path = find_in_path(filename);
-               if (arg_path)
-                       filename = arg_path;
-       }
-       input = remember_FILE(fopen_or_warn(filename, "r"));
-       free(arg_path);
-       if (!input) {
-               /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */
-               /* POSIX: non-interactive shell should abort here,
-                * not merely fail. So far no one complained :)
-                */
+       pi = parse_jobspec(argv[1]);
+       if (!pi)
                return EXIT_FAILURE;
+ found:
+       /* TODO: bash prints a string representation
+        * of job being foregrounded (like "sleep 1 | cat") */
+       if (argv[0][0] == 'f' && G_saved_tty_pgrp) {
+               /* Put the job into the foreground.  */
+               tcsetpgrp(G_interactive_fd, pi->pgrp);
        }
 
-#if ENABLE_HUSH_FUNCTIONS
-       sv_flg = G_flag_return_in_progress;
-       /* "we are inside sourced file, ok to use return" */
-       G_flag_return_in_progress = -1;
-#endif
-       if (argv[1])
-               save_and_replace_G_args(&sv, argv);
-
-       /* "false; . ./empty_line; echo Zero:$?" should print 0 */
-       G.last_exitcode = 0;
-       parse_and_run_file(input);
-       fclose_and_forget(input);
+       /* Restart the processes in the job */
+       debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp);
+       for (i = 0; i < pi->num_cmds; i++) {
+               debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid);
+       }
+       pi->stopped_cmds = 0;
 
-       if (argv[1])
-               restore_G_args(&sv, argv);
-#if ENABLE_HUSH_FUNCTIONS
-       G_flag_return_in_progress = sv_flg;
-#endif
+       i = kill(- pi->pgrp, SIGCONT);
+       if (i < 0) {
+               if (errno == ESRCH) {
+                       delete_finished_bg_job(pi);
+                       return EXIT_SUCCESS;
+               }
+               bb_perror_msg("kill (SIGCONT)");
+       }
 
-       return G.last_exitcode;
+       if (argv[0][0] == 'f') {
+               remove_bg_job(pi);
+               return checkjobs_and_fg_shell(pi);
+       }
+       return EXIT_SUCCESS;
 }
+#endif
 
-static int FAST_FUNC builtin_umask(char **argv)
+#if ENABLE_HUSH_KILL
+static int FAST_FUNC builtin_kill(char **argv)
 {
-       int rc;
-       mode_t mask;
+       int ret = 0;
 
-       rc = 1;
-       mask = umask(0);
-       argv = skip_dash_dash(argv);
-       if (argv[0]) {
-               mode_t old_mask = mask;
+# if ENABLE_HUSH_JOB
+       if (argv[1] && strcmp(argv[1], "-l") != 0) {
+               int i = 1;
 
-               /* numeric umasks are taken as-is */
-               /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */
-               if (!isdigit(argv[0][0]))
-                       mask ^= 0777;
-               mask = bb_parse_mode(argv[0], mask);
-               if (!isdigit(argv[0][0]))
-                       mask ^= 0777;
-               if ((unsigned)mask > 0777) {
-                       mask = old_mask;
-                       /* bash messages:
-                        * bash: umask: 'q': invalid symbolic mode operator
-                        * bash: umask: 999: octal number out of range
+               do {
+                       struct pipe *pi;
+                       char *dst;
+                       int j, n;
+
+                       if (argv[i][0] != '%')
+                               continue;
+                       /*
+                        * "kill %N" - job kill
+                        * Converting to pgrp / pid kill
                         */
-                       bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]);
-                       rc = 0;
-               }
-       } else {
-               /* Mimic bash */
-               printf("%04o\n", (unsigned) mask);
-               /* fall through and restore mask which we set to 0 */
+                       pi = parse_jobspec(argv[i]);
+                       if (!pi) {
+                               /* Eat bad jobspec */
+                               j = i;
+                               do {
+                                       j++;
+                                       argv[j - 1] = argv[j];
+                               } while (argv[j]);
+                               ret = 1;
+                               i--;
+                               continue;
+                       }
+                       /*
+                        * In jobs started under job control, we signal
+                        * entire process group by kill -PGRP_ID.
+                        * This happens, f.e., in interactive shell.
+                        *
+                        * Otherwise, we signal each child via
+                        * kill PID1 PID2 PID3.
+                        * Testcases:
+                        * sh -c 'sleep 1|sleep 1 & kill %1'
+                        * sh -c 'true|sleep 2 & sleep 1; kill %1'
+                        * sh -c 'true|sleep 1 & sleep 2; kill %1'
+                        */
+                       n = G_interactive_fd ? 1 : pi->num_cmds;
+                       dst = alloca(n * sizeof(int)*4);
+                       argv[i] = dst;
+                       if (G_interactive_fd)
+                               dst += sprintf(dst, " -%u", (int)pi->pgrp);
+                       else for (j = 0; j < n; j++) {
+                               struct command *cmd = &pi->cmds[j];
+                               /* Skip exited members of the job */
+                               if (cmd->pid == 0)
+                                       continue;
+                               /*
+                                * kill_main has matching code to expect
+                                * leading space. Needed to not confuse
+                                * negative pids with "kill -SIGNAL_NO" syntax
+                                */
+                               dst += sprintf(dst, " %u", (int)cmd->pid);
+                       }
+                       *dst = '\0';
+               } while (argv[++i]);
        }
-       umask(mask);
+# endif
 
-       return !rc; /* rc != 0 - success */
+       if (argv[1] || ret == 0) {
+               ret = run_applet_main(argv, kill_main);
+       }
+       /* else: ret = 1, "kill %bad_jobspec" case */
+       return ret;
 }
+#endif
 
-/* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */
-static int FAST_FUNC builtin_unset(char **argv)
+#if ENABLE_HUSH_WAIT
+/* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */
+#if !ENABLE_HUSH_JOB
+# define wait_for_child_or_signal(pipe,pid) wait_for_child_or_signal(pid)
+#endif
+static int wait_for_child_or_signal(struct pipe *waitfor_pipe, pid_t waitfor_pid)
 {
-       int ret;
-       unsigned opts;
+       int ret = 0;
+       for (;;) {
+               int sig;
+               sigset_t oldset;
 
-       /* "!": do not abort on errors */
-       /* "+": stop at 1st non-option */
-       opts = getopt32(argv, "!+vf");
-       if (opts == (unsigned)-1)
-               return EXIT_FAILURE;
-       if (opts == 3) {
-               bb_error_msg("unset: -v and -f are exclusive");
-               return EXIT_FAILURE;
-       }
-       argv += optind;
+               if (!sigisemptyset(&G.pending_set))
+                       goto check_sig;
 
-       ret = EXIT_SUCCESS;
-       while (*argv) {
-               if (!(opts & 2)) { /* not -f */
-                       if (unset_local_var(*argv)) {
-                               /* unset <nonexistent_var> doesn't fail.
-                                * Error is when one tries to unset RO var.
-                                * Message was printed by unset_local_var. */
-                               ret = EXIT_FAILURE;
-                       }
+               /* waitpid is not interruptible by SA_RESTARTed
+                * signals which we use. Thus, this ugly dance:
+                */
+
+               /* Make sure possible SIGCHLD is stored in kernel's
+                * pending signal mask before we call waitpid.
+                * Or else we may race with SIGCHLD, lose it,
+                * and get stuck in sigsuspend...
+                */
+               sigfillset(&oldset); /* block all signals, remember old set */
+               sigprocmask(SIG_SETMASK, &oldset, &oldset);
+
+               if (!sigisemptyset(&G.pending_set)) {
+                       /* Crap! we raced with some signal! */
+                       goto restore;
                }
-#if ENABLE_HUSH_FUNCTIONS
-               else {
-                       unset_func(*argv);
+
+               /*errno = 0; - checkjobs does this */
+/* Can't pass waitfor_pipe into checkjobs(): it won't be interruptible */
+               ret = checkjobs(NULL, waitfor_pid); /* waitpid(WNOHANG) inside */
+               debug_printf_exec("checkjobs:%d\n", ret);
+#if ENABLE_HUSH_JOB
+               if (waitfor_pipe) {
+                       int rcode = job_exited_or_stopped(waitfor_pipe);
+                       debug_printf_exec("job_exited_or_stopped:%d\n", rcode);
+                       if (rcode >= 0) {
+                               ret = rcode;
+                               sigprocmask(SIG_SETMASK, &oldset, NULL);
+                               break;
+                       }
                }
 #endif
-               argv++;
+               /* if ECHILD, there are no children (ret is -1 or 0) */
+               /* if ret == 0, no children changed state */
+               /* if ret != 0, it's exitcode+1 of exited waitfor_pid child */
+               if (errno == ECHILD || ret) {
+                       ret--;
+                       if (ret < 0) /* if ECHILD, may need to fix "ret" */
+                               ret = 0;
+                       sigprocmask(SIG_SETMASK, &oldset, NULL);
+                       break;
+               }
+               /* Wait for SIGCHLD or any other signal */
+               /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */
+               /* Note: sigsuspend invokes signal handler */
+               sigsuspend(&oldset);
+ restore:
+               sigprocmask(SIG_SETMASK, &oldset, NULL);
+ check_sig:
+               /* So, did we get a signal? */
+               sig = check_and_run_traps();
+               if (sig /*&& sig != SIGCHLD - always true */) {
+                       ret = 128 + sig;
+                       break;
+               }
+               /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */
        }
        return ret;
 }
 
-/* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */
 static int FAST_FUNC builtin_wait(char **argv)
 {
-       int ret = EXIT_SUCCESS;
+       int ret;
        int status;
 
        argv = skip_dash_dash(argv);
@@ -9395,77 +9831,70 @@ static int FAST_FUNC builtin_wait(char **argv)
                 * ^C <-- after ~4 sec from keyboard
                 * $
                 */
-               while (1) {
-                       int sig;
-                       sigset_t oldset, allsigs;
-
-                       /* waitpid is not interruptible by SA_RESTARTed
-                        * signals which we use. Thus, this ugly dance:
-                        */
-
-                       /* Make sure possible SIGCHLD is stored in kernel's
-                        * pending signal mask before we call waitpid.
-                        * Or else we may race with SIGCHLD, lose it,
-                        * and get stuck in sigwaitinfo...
-                        */
-                       sigfillset(&allsigs);
-                       sigprocmask(SIG_SETMASK, &allsigs, &oldset);
-
-                       if (!sigisemptyset(&G.pending_set)) {
-                               /* Crap! we raced with some signal! */
-                       //      sig = 0;
-                               goto restore;
-                       }
-
-                       checkjobs(NULL); /* waitpid(WNOHANG) inside */
-                       if (errno == ECHILD) {
-                               sigprocmask(SIG_SETMASK, &oldset, NULL);
-                               break;
-                       }
-
-                       /* Wait for SIGCHLD or any other signal */
-                       //sig = sigwaitinfo(&allsigs, NULL);
-                       /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */
-                       /* Note: sigsuspend invokes signal handler */
-                       sigsuspend(&oldset);
- restore:
-                       sigprocmask(SIG_SETMASK, &oldset, NULL);
-
-                       /* So, did we get a signal? */
-                       //if (sig > 0)
-                       //      raise(sig); /* run handler */
-                       sig = check_and_run_traps();
-                       if (sig /*&& sig != SIGCHLD - always true */) {
-                               /* see note 2 */
-                               ret = 128 + sig;
-                               break;
-                       }
-                       /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */
-               }
-               return ret;
+               return wait_for_child_or_signal(NULL, 0 /*(no job and no pid to wait for)*/);
        }
 
-       /* This is probably buggy wrt interruptible-ness */
-       while (*argv) {
+       do {
                pid_t pid = bb_strtou(*argv, NULL, 10);
-               if (errno) {
+               if (errno || pid <= 0) {
+#if ENABLE_HUSH_JOB
+                       if (argv[0][0] == '%') {
+                               struct pipe *wait_pipe;
+                               ret = 127; /* bash compat for bad jobspecs */
+                               wait_pipe = parse_jobspec(*argv);
+                               if (wait_pipe) {
+                                       ret = job_exited_or_stopped(wait_pipe);
+                                       if (ret < 0)
+                                               ret = wait_for_child_or_signal(wait_pipe, 0);
+                               }
+                               /* else: parse_jobspec() already emitted error msg */
+                               continue;
+                       }
+#endif
                        /* mimic bash message */
                        bb_error_msg("wait: '%s': not a pid or valid job spec", *argv);
-                       return EXIT_FAILURE;
+                       ret = EXIT_FAILURE;
+                       continue; /* bash checks all argv[] */
+               }
+
+               /* Do we have such child? */
+               ret = waitpid(pid, &status, WNOHANG);
+               if (ret < 0) {
+                       /* No */
+                       if (errno == ECHILD) {
+                               if (G.last_bg_pid > 0 && pid == G.last_bg_pid) {
+                                       /* "wait $!" but last bg task has already exited. Try:
+                                        * (sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $?
+                                        * In bash it prints exitcode 0, then 3.
+                                        * In dash, it is 127.
+                                        */
+                                       /* ret = G.last_bg_pid_exitstatus - FIXME */
+                               } else {
+                                       /* Example: "wait 1". mimic bash message */
+                                       bb_error_msg("wait: pid %d is not a child of this shell", (int)pid);
+                               }
+                       } else {
+                               /* ??? */
+                               bb_perror_msg("wait %s", *argv);
+                       }
+                       ret = 127;
+                       continue; /* bash checks all argv[] */
                }
-               if (waitpid(pid, &status, 0) == pid) {
+               if (ret == 0) {
+                       /* Yes, and it still runs */
+                       ret = wait_for_child_or_signal(NULL, pid);
+               } else {
+                       /* Yes, and it just exited */
+                       process_wait_result(NULL, pid, status);
                        ret = WEXITSTATUS(status);
                        if (WIFSIGNALED(status))
                                ret = 128 + WTERMSIG(status);
-               } else {
-                       bb_perror_msg("wait %s", *argv);
-                       ret = 127;
                }
-               argv++;
-       }
+       } while (*++argv);
 
        return ret;
 }
+#endif
 
 #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS
 static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min)
@@ -9532,3 +9961,46 @@ static int FAST_FUNC builtin_return(char **argv)
        return rc;
 }
 #endif
+
+#if ENABLE_HUSH_MEMLEAK
+static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM)
+{
+       void *p;
+       unsigned long l;
+
+# ifdef M_TRIM_THRESHOLD
+       /* Optional. Reduces probability of false positives */
+       malloc_trim(0);
+# endif
+       /* Crude attempt to find where "free memory" starts,
+        * sans fragmentation. */
+       p = malloc(240);
+       l = (unsigned long)p;
+       free(p);
+       p = malloc(3400);
+       if (l < (unsigned long)p) l = (unsigned long)p;
+       free(p);
+
+
+# if 0  /* debug */
+       {
+               struct mallinfo mi = mallinfo();
+               printf("top alloc:0x%lx malloced:%d+%d=%d\n", l,
+                       mi.arena, mi.hblkhd, mi.arena + mi.hblkhd);
+       }
+# endif
+
+       if (!G.memleak_value)
+               G.memleak_value = l;
+
+       l -= G.memleak_value;
+       if ((long)l < 0)
+               l = 0;
+       l /= 1024;
+       if (l > 127)
+               l = 127;
+
+       /* Exitcode is "how many kilobytes we leaked since 1st call" */
+       return l;
+}
+#endif