ash: fix return_in_trap1.tests failure
[oweals/busybox.git] / shell / hush.c
index 6b0d8503927f729b820c4767a622d59c6e536014..8693d7562e1366ec64dd01daadebd7c0307de036 100644 (file)
 #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
@@ -459,19 +462,17 @@ static const char *const assignment_flag[] = {
 
 typedef struct in_str {
        const char *p;
-       /* eof_flag=1: last char in ->p is really an EOF */
-       char eof_flag; /* meaningless if ->p == NULL */
-       char peek_buf[2];
 #if ENABLE_HUSH_INTERACTIVE
        smallint promptmode; /* 0: PS1, 1: PS2 */
 #endif
+       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))
+#define i_peek(input)  ((input)->peek(input))
 
 /* The descrip member of this structure is only used to make
  * debugging output pretty */
@@ -711,10 +712,10 @@ enum {
 };
 
 
-/* Can be extended to handle a FIXME in setup_redirects about not saving script fds */
 struct FILE_list {
        struct FILE_list *next;
        FILE *fp;
+       int fd;
 };
 
 
@@ -776,6 +777,9 @@ struct globals {
         * 1: return is invoked, skip all till end of func
         */
        smallint flag_return_in_progress;
+# define G_flag_return_in_progress (G.flag_return_in_progress)
+#else
+# define G_flag_return_in_progress 0
 #endif
        smallint exiting; /* used to prevent EXIT trap recursion */
        /* These four support $?, $#, and $1 */
@@ -809,9 +813,7 @@ struct globals {
        unsigned handled_SIGCHLD;
        smallint we_have_children;
 #endif
-#if ENABLE_FEATURE_SH_STANDALONE
        struct FILE_list *FILE_list;
-#endif
        /* Which signals have non-DFL handler (even with no traps set)?
         * Set at the start to:
         * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS)
@@ -833,7 +835,9 @@ struct globals {
        int debug_indent;
 #endif
        struct sigaction sa;
-       char user_input_buf[ENABLE_FEATURE_EDITING ? CONFIG_FEATURE_EDITING_MAX_LEN : 2];
+#if ENABLE_FEATURE_EDITING
+       char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN];
+#endif
 };
 #define G (*ptr_to_globals)
 /* Not #defining name to G.name - this quickly gets unwieldy
@@ -1262,35 +1266,34 @@ static void free_strings(char **strings)
 }
 
 
+static int xdup_and_close(int fd, int F_DUPFD_maybe_CLOEXEC)
+{
+       /* We avoid taking stdio fds. Mimicking ash: use fds above 9 */
+       int newfd = fcntl(fd, F_DUPFD_maybe_CLOEXEC, 10);
+       if (newfd < 0) {
+               /* fd was not open? */
+               if (errno == EBADF)
+                       return fd;
+               xfunc_die();
+       }
+       close(fd);
+       return newfd;
+}
+
+
 /* Manipulating the list of open FILEs */
 static FILE *remember_FILE(FILE *fp)
 {
        if (fp) {
-#if ENABLE_FEATURE_SH_STANDALONE
                struct FILE_list *n = xmalloc(sizeof(*n));
-               n->fp = fp;
                n->next = G.FILE_list;
                G.FILE_list = n;
-#endif
-               close_on_exec_on(fileno(fp));
+               n->fp = fp;
+               n->fd = fileno(fp);
+               close_on_exec_on(n->fd);
        }
        return fp;
 }
-#if ENABLE_FEATURE_SH_STANDALONE
-static void close_all_FILE_list(void)
-{
-       struct FILE_list *fl = G.FILE_list;
-       while (fl) {
-               /* fclose would also free FILE object.
-                * It is disastrous if we share memory with a vforked parent.
-                * I'm not sure we never come here after vfork.
-                * Therefore just close fd, nothing more.
-                */
-               /*fclose(fl->fp); - unsafe */
-               close(fileno(fl->fp));
-               fl = fl->next;
-       }
-}
 static void fclose_and_forget(FILE *fp)
 {
        struct FILE_list **pp = &G.FILE_list;
@@ -1305,8 +1308,46 @@ static void fclose_and_forget(FILE *fp)
        }
        fclose(fp);
 }
-#else
-# define fclose_and_forget(fp) fclose(fp);
+static int save_FILEs_on_redirect(int fd)
+{
+       struct FILE_list *fl = G.FILE_list;
+       while (fl) {
+               if (fd == fl->fd) {
+                       /* We use it only on script files, they are all CLOEXEC */
+                       fl->fd = xdup_and_close(fd, F_DUPFD_CLOEXEC);
+                       return 1;
+               }
+               fl = fl->next;
+       }
+       return 0;
+}
+static void restore_redirected_FILEs(void)
+{
+       struct FILE_list *fl = G.FILE_list;
+       while (fl) {
+               int should_be = fileno(fl->fp);
+               if (fl->fd != should_be) {
+                       xmove_fd(fl->fd, should_be);
+                       fl->fd = should_be;
+               }
+               fl = fl->next;
+       }
+}
+#if ENABLE_FEATURE_SH_STANDALONE
+static void close_all_FILE_list(void)
+{
+       struct FILE_list *fl = G.FILE_list;
+       while (fl) {
+               /* fclose would also free FILE object.
+                * It is disastrous if we share memory with a vforked parent.
+                * I'm not sure we never come here after vfork.
+                * Therefore just close fd, nothing more.
+                */
+               /*fclose(fl->fp); - unsafe */
+               close(fl->fd);
+               fl = fl->next;
+       }
+}
 #endif
 
 
@@ -1698,6 +1739,7 @@ static int check_and_run_traps(void)
                break;
  got_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]) {
                                /* We have user-defined handler */
                                smalluint save_rcode;
@@ -1715,6 +1757,7 @@ static int check_and_run_traps(void)
                /* not a trap: special action */
                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;
@@ -1723,6 +1766,7 @@ static int check_and_run_traps(void)
 #if ENABLE_HUSH_JOB
                case SIGHUP: {
                        struct pipe *job;
+                       debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig);
                        /* bash is observed to signal whole process groups,
                         * not individual processes */
                        for (job = G.job_list; job; job = job->next) {
@@ -1737,6 +1781,7 @@ static int check_and_run_traps(void)
 #endif
 #if ENABLE_HUSH_FAST
                case SIGCHLD:
+                       debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig);
                        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:
@@ -1746,6 +1791,7 @@ static int check_and_run_traps(void)
                        break;
 #endif
                default: /* ignored: */
+                       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.
@@ -2084,28 +2130,19 @@ static void reinit_unicode_for_hush(void)
        }
 }
 
-
 /*
- * in_str support
+ * in_str support (strings, and "strings" read from files).
  */
-static int FAST_FUNC static_get(struct in_str *i)
-{
-       int ch = *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)
-{
-       return *i->p;
-}
 
 #if ENABLE_HUSH_INTERACTIVE
-
+/* To test correct lineedit/interactive behavior, type from command line:
+ *     echo $P\
+ *     \
+ *     AT\
+ *     H\
+ *     \
+ * It excercises a lot of corner cases.
+ */
 static void cmdedit_update_prompt(void)
 {
        if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) {
@@ -2119,7 +2156,6 @@ static void cmdedit_update_prompt(void)
        if (G.PS2 == NULL)
                G.PS2 = "> ";
 }
-
 static const char *setup_prompt_string(int promptmode)
 {
        const char *prompt_str;
@@ -2137,33 +2173,36 @@ static const char *setup_prompt_string(int promptmode)
                        prompt_str = G.PS2;
        } else
                prompt_str = (promptmode == 0) ? G.PS1 : G.PS2;
-       debug_printf("result '%s'\n", prompt_str);
+       debug_printf("prompt_str '%s'\n", prompt_str);
        return prompt_str;
 }
-
-static void get_user_input(struct in_str *i)
+static int get_user_input(struct in_str *i)
 {
        int r;
        const char *prompt_str;
 
        prompt_str = setup_prompt_string(i->promptmode);
 # if ENABLE_FEATURE_EDITING
-       /* Enable command line editing only while a command line
-        * is actually being read */
        do {
                reinit_unicode_for_hush();
                G.flag_SIGINT = 0;
                /* buglet: SIGINT will not make new prompt to appear _at once_,
                 * only after <Enter>. (^C will work) */
-               r = read_line_input(G.line_input_state, prompt_str, G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1, /*timeout*/ -1);
+               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) */
                check_and_run_traps();
        } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */
-       i->eof_flag = (r < 0);
-       if (i->eof_flag) { /* EOF/error detected */
-               G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */
-               G.user_input_buf[1] = '\0';
+       if (r < 0) {
+               /* EOF/error detected */
+               i->p = NULL;
+               i->peek_buf[0] = r = EOF;
+               return r;
        }
+       i->p = G.user_input_buf;
+       return (unsigned char)*i->p++;
 # else
        do {
                G.flag_SIGINT = 0;
@@ -2177,76 +2216,149 @@ static void get_user_input(struct in_str *i)
                        fputs(prompt_str, stdout);
                }
                fflush_all();
-               G.user_input_buf[0] = r = fgetc(i->file);
-               /*G.user_input_buf[1] = '\0'; - already is and never changed */
-       } while (G.flag_SIGINT);
-       i->eof_flag = (r == EOF);
+               r = fgetc(i->file);
+       } while (G.flag_SIGINT || r == '\0');
+       return r;
 # endif
-       i->p = G.user_input_buf;
 }
-
-#endif  /* INTERACTIVE */
-
 /* This is the magic location that prints prompts
  * and gets data back from the user */
+static int fgetc_interactive(struct in_str *i)
+{
+       int ch;
+       /* If it's interactive stdin, get new line. */
+       if (G_interactive_fd && i->file == stdin) {
+               /* Returns first char (or EOF), the rest is in i->p[] */
+               ch = get_user_input(i);
+               i->promptmode = 1; /* PS2 */
+       } else {
+               /* Not stdin: script file, sourced file, etc */
+               do ch = fgetc(i->file); while (ch == '\0');
+       }
+       return ch;
+}
+#else
+static inline int fgetc_interactive(struct in_str *i)
+{
+       int ch;
+       do ch = fgetc(i->file); while (ch == '\0');
+       return ch;
+}
+#endif  /* INTERACTIVE */
+
 static int FAST_FUNC file_get(struct in_str *i)
 {
        int ch;
 
-       /* If there is data waiting, eat it up */
-       if (i->p && *i->p) {
-#if ENABLE_HUSH_INTERACTIVE
- take_cached:
-#endif
-               ch = *i->p++;
-               if (i->eof_flag && !*i->p)
-                       ch = EOF;
-               /* note: ch is never NUL */
-       } else {
-               /* need to double check i->file because we might be doing something
-                * more complicated by now, like sourcing or substituting. */
-#if ENABLE_HUSH_INTERACTIVE
-               if (G_interactive_fd && i->file == stdin) {
-                       do {
-                               get_user_input(i);
-                       } while (!*i->p); /* need non-empty line */
-                       i->promptmode = 1; /* PS2 */
-                       goto take_cached;
-               }
+#if ENABLE_FEATURE_EDITING
+       /* This can be stdin, check line editing char[] buffer */
+       if (i->p && *i->p != '\0') {
+               ch = (unsigned char)*i->p++;
+               goto out;
+       }
 #endif
-               do ch = fgetc(i->file); while (ch == '\0');
+       /* peek_buf[] is an int array, not char. Can contain EOF. */
+       ch = i->peek_buf[0];
+       if (ch != 0) {
+               int ch2 = i->peek_buf[1];
+               i->peek_buf[0] = ch2;
+               if (ch2 == 0) /* very likely, avoid redundant write */
+                       goto out;
+               i->peek_buf[1] = 0;
+               goto out;
        }
+
+       ch = fgetc_interactive(i);
+ out:
        debug_printf("file_get: got '%c' %d\n", ch, ch);
        i->last_char = ch;
        return ch;
 }
 
-/* All callers guarantee this routine will never
- * be used right after a newline, so prompting is not needed.
- */
 static int FAST_FUNC file_peek(struct in_str *i)
 {
        int ch;
-       if (i->p && *i->p) {
-               if (i->eof_flag && !i->p[1])
-                       return EOF;
-               return *i->p;
-               /* note: ch is never NUL */
+
+#if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE
+       /* This can be stdin, check line editing char[] buffer */
+       if (i->p && *i->p != '\0')
+               return (unsigned char)*i->p;
+#endif
+       /* peek_buf[] is an int array, not char. Can contain EOF. */
+       ch = i->peek_buf[0];
+       if (ch != 0)
+               return ch;
+
+       /* Need to get a new char */
+       ch = fgetc_interactive(i);
+       debug_printf("file_peek: got '%c' %d\n", ch, ch);
+
+       /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */
+#if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE
+       if (i->p) {
+               i->p -= 1;
+               return ch;
        }
-       do ch = fgetc(i->file); while (ch == '\0');
-       i->eof_flag = (ch == EOF);
+#endif
        i->peek_buf[0] = ch;
-       i->peek_buf[1] = '\0';
-       i->p = i->peek_buf;
-       debug_printf("file_peek: got '%c' %d\n", ch, ch);
+       /*i->peek_buf[1] = 0; - already is */
+       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.
+ */
+static int i_peek2(struct in_str *i)
+{
+       int ch;
+
+       /* There are two cases when i->p[] buffer exists.
+        * (1) it's a string in_str.
+        * (2) It's a file, and we have a saved line editing buffer.
+        * In both cases, we know that i->p[0] exists and not NUL, and
+        * the peek2 result is in i->p[1].
+        */
+       if (i->p)
+               return (unsigned char)i->p[1];
+
+       /* Now we know it is a file-based in_str. */
+
+       /* peek_buf[] is an int array, not char. Can contain EOF. */
+       /* Is there 2nd char? */
+       ch = i->peek_buf[1];
+       if (ch == 0) {
+               /* We did not read it yet, get it now */
+               do ch = fgetc(i->file); while (ch == '\0');
+               i->peek_buf[1] = ch;
+       }
+
+       debug_printf("file_peek2: got '%c' %d\n", ch, ch);
        return ch;
 }
 
 static void setup_file_in_str(struct in_str *i, FILE *f)
 {
        memset(i, 0, sizeof(*i));
-       i->peek = file_peek;
        i->get = file_get;
+       i->peek = file_peek;
        /* i->promptmode = 0; - PS1 (memset did it) */
        i->file = f;
        /* i->p = NULL; */
@@ -2255,11 +2367,10 @@ 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->peek = static_peek;
        i->get = static_get;
+       i->peek = static_peek;
        /* i->promptmode = 0; - PS1 (memset did it) */
        i->p = s;
-       /* i->eof_flag = 0; */
 }
 
 
@@ -2290,7 +2401,7 @@ static ALWAYS_INLINE void o_free_unsafe(o_string *o)
 static void o_grow_by(o_string *o, int len)
 {
        if (o->length + len > o->maxlen) {
-               o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK);
+               o->maxlen += (2 * len) | (B_CHUNK-1);
                o->data = xrealloc(o->data, 1 + o->maxlen);
        }
 }
@@ -2298,11 +2409,26 @@ static void o_grow_by(o_string *o, int len)
 static void o_addchr(o_string *o, int ch)
 {
        debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o);
+       if (o->length < o->maxlen) {
+               /* likely. avoid o_grow_by() call */
+ add:
+               o->data[o->length] = ch;
+               o->length++;
+               o->data[o->length] = '\0';
+               return;
+       }
        o_grow_by(o, 1);
-       o->data[o->length] = ch;
-       o->length++;
+       goto add;
+}
+
+#if 0
+/* Valid only if we know o_string is not empty */
+static void o_delchr(o_string *o)
+{
+       o->length--;
        o->data[o->length] = '\0';
 }
+#endif
 
 static void o_addblock(o_string *o, const char *str, int len)
 {
@@ -3016,6 +3142,14 @@ static int done_command(struct parse_context *ctx)
        struct pipe *pi = ctx->pipe;
        struct command *command = ctx->command;
 
+#if 0  /* Instead we emit error message at run time */
+       if (ctx->pending_redirect) {
+               /* For example, "cmd >" (no filename to redirect to) */
+               die_if_script("syntax error: %s", "invalid redirect");
+               ctx->pending_redirect = NULL;
+       }
+#endif
+
        if (command) {
                if (IS_NULL_CMD(command)) {
                        debug_printf_parse("done_command: skipping null cmd, num_cmds=%d\n", pi->num_cmds);
@@ -3538,6 +3672,12 @@ static int parse_redirect(struct parse_context *ctx,
                debug_printf_parse("duplicating redirect '%d>&%d'\n",
                                redir->rd_fd, redir->rd_dup);
        } else {
+#if 0          /* Instead we emit error message at run time */
+               if (ctx->pending_redirect) {
+                       /* For example, "cmd > <file" */
+                       die_if_script("syntax error: %s", "invalid redirect");
+               }
+#endif
                /* Set ctx->pending_redirect, so we know what to do at the
                 * end of the next parsed word. */
                ctx->pending_redirect = redir;
@@ -3803,6 +3943,39 @@ static int parse_group(o_string *dest, struct parse_context *ctx,
        /* command remains "open", available for possible redirects */
 }
 
+static int i_getch_and_eat_bkslash_nl(struct in_str *input)
+{
+       for (;;) {
+               int ch, ch2;
+
+               ch = i_getch(input);
+               if (ch != '\\')
+                       return ch;
+               ch2 = i_peek(input);
+               if (ch2 != '\n')
+                       return ch;
+               /* backslash+newline, skip it */
+               i_getch(input);
+       }
+}
+
+static int i_peek_and_eat_bkslash_nl(struct in_str *input)
+{
+       for (;;) {
+               int ch, ch2;
+
+               ch = i_peek(input);
+               if (ch != '\\')
+                       return ch;
+               ch2 = i_peek2(input);
+               if (ch2 != '\n')
+                       return ch;
+               /* backslash+newline, skip it */
+               i_getch(input);
+               i_getch(input);
+       }
+}
+
 #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS
 /* Subroutines for copying $(...) and `...` things */
 static int add_till_backquote(o_string *dest, struct in_str *input, int in_dquote);
@@ -3920,7 +4093,7 @@ static int add_till_closing_bracket(o_string *dest, struct in_str *input, unsign
                        if (!dbl)
                                break;
                        /* we look for closing )) of $((EXPR)) */
-                       if (i_peek(input) == end_ch) {
+                       if (i_peek_and_eat_bkslash_nl(input) == end_ch) {
                                i_getch(input); /* eat second ')' */
                                break;
                        }
@@ -3958,6 +4131,13 @@ static int add_till_closing_bracket(o_string *dest, struct in_str *input, unsign
                                syntax_error_unterm_ch(')');
                                return 0;
                        }
+#if 0
+                       if (ch == '\n') {
+                               /* "backslash+newline", ignore both */
+                               o_delchr(dest); /* undo insertion of '\' */
+                               continue;
+                       }
+#endif
                        o_addchr(dest, ch);
                        continue;
                }
@@ -3976,7 +4156,7 @@ static int parse_dollar(o_string *as_string,
                o_string *dest,
                struct in_str *input, unsigned char quote_mask)
 {
-       int ch = i_peek(input);  /* first character after the $ */
+       int ch = i_peek_and_eat_bkslash_nl(input);  /* first character after the $ */
 
        debug_printf_parse("parse_dollar entered: ch='%c'\n", ch);
        if (isalpha(ch)) {
@@ -3988,9 +4168,11 @@ static int parse_dollar(o_string *as_string,
                        debug_printf_parse(": '%c'\n", ch);
                        o_addchr(dest, ch | quote_mask);
                        quote_mask = 0;
-                       ch = i_peek(input);
-                       if (!isalnum(ch) && ch != '_')
+                       ch = i_peek_and_eat_bkslash_nl(input);
+                       if (!isalnum(ch) && ch != '_') {
+                               /* End of variable name reached */
                                break;
+                       }
                        ch = i_getch(input);
                        nommu_addchr(as_string, ch);
                }
@@ -4017,7 +4199,7 @@ static int parse_dollar(o_string *as_string,
                ch = i_getch(input); /* eat '{' */
                nommu_addchr(as_string, ch);
 
-               ch = i_getch(input); /* first char after '{' */
+               ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */
                /* It should be ${?}, or ${#var},
                 * or even ${?+subst} - operator acting on a special variable,
                 * or the beginning of variable name.
@@ -4123,7 +4305,7 @@ static int parse_dollar(o_string *as_string,
                ch = i_getch(input);
                nommu_addchr(as_string, ch);
 # if ENABLE_SH_MATH_SUPPORT
-               if (i_peek(input) == '(') {
+               if (i_peek_and_eat_bkslash_nl(input) == '(') {
                        ch = i_getch(input);
                        nommu_addchr(as_string, ch);
                        o_addchr(dest, SPECIAL_VAR_SYMBOL);
@@ -4160,7 +4342,7 @@ static int parse_dollar(o_string *as_string,
        case '_':
                ch = i_getch(input);
                nommu_addchr(as_string, ch);
-               ch = i_peek(input);
+               ch = i_peek_and_eat_bkslash_nl(input);
                if (isalnum(ch)) { /* it's $_name or $_123 */
                        ch = '_';
                        goto make_var;
@@ -5627,7 +5809,7 @@ static char* expand_strvec_to_string(char **argv)
                        n++;
                }
        }
-       overlapping_strcpy((char*)list, list[0]);
+       overlapping_strcpy((char*)list, list[0] ? list[0] : "");
        debug_printf_expand("strvec_to_string='%s'\n", (char*)list);
        return (char*)list;
 }
@@ -5904,10 +6086,8 @@ static void parse_and_run_stream(struct in_str *inp, int end_trigger)
                debug_printf_exec("parse_and_run_stream: run_and_free_list\n");
                run_and_free_list(pipe_list);
                empty = 0;
-#if ENABLE_HUSH_FUNCTIONS
-               if (G.flag_return_in_progress == 1)
+               if (G_flag_return_in_progress == 1)
                        break;
-#endif
        }
 }
 
@@ -6147,6 +6327,74 @@ static void setup_heredoc(struct redir_struct *redir)
        wait(NULL); /* wait till child has died */
 }
 
+/* fd: redirect wants this fd to be used (e.g. 3>file).
+ * Move all conflicting internally used fds,
+ * and remember them so that we can restore them later.
+ */
+static int save_fds_on_redirect(int fd, int squirrel[3])
+{
+       if (squirrel) {
+               /* Handle redirects of fds 0,1,2 */
+
+               /* If we collide with an already moved stdio fd... */
+               if (fd == squirrel[0]) {
+                       squirrel[0] = xdup_and_close(squirrel[0], F_DUPFD);
+                       return 1;
+               }
+               if (fd == squirrel[1]) {
+                       squirrel[1] = xdup_and_close(squirrel[1], F_DUPFD);
+                       return 1;
+               }
+               if (fd == squirrel[2]) {
+                       squirrel[2] = xdup_and_close(squirrel[2], F_DUPFD);
+                       return 1;
+               }
+               /* If we are about to redirect stdio fd, and did not yet move it... */
+               if (fd <= 2 && squirrel[fd] < 0) {
+                       /* We avoid taking stdio fds */
+                       squirrel[fd] = fcntl(fd, F_DUPFD, 10);
+                       if (squirrel[fd] < 0 && errno != EBADF)
+                               xfunc_die();
+                       return 0; /* "we did not close fd" */
+               }
+       }
+
+#if ENABLE_HUSH_INTERACTIVE
+       if (fd != 0 && fd == G.interactive_fd) {
+               G.interactive_fd = xdup_and_close(G.interactive_fd, F_DUPFD_CLOEXEC);
+               return 1;
+       }
+#endif
+
+       /* Are we called from setup_redirects(squirrel==NULL)? Two cases:
+        * (1) Redirect in a forked child. No need to save FILEs' fds,
+        * we aren't going to use them anymore, ok to trash.
+        * (2) "exec 3>FILE". Bummer. We can save FILEs' fds,
+        * but how are we doing to use them?
+        * "fileno(fd) = new_fd" can't be done.
+        */
+       if (!squirrel)
+               return 0;
+
+       return save_FILEs_on_redirect(fd);
+}
+
+static void restore_redirects(int squirrel[3])
+{
+       int i, fd;
+       for (i = 0; i <= 2; i++) {
+               fd = squirrel[i];
+               if (fd != -1) {
+                       /* We simply die on error */
+                       xmove_fd(fd, i);
+               }
+       }
+
+       /* Moved G.interactive_fd stays on new fd, not doing anything for it */
+
+       restore_redirected_FILEs();
+}
+
 /* squirrel != NULL means we squirrel away copies of stdin, stdout,
  * and stderr if they are redirected. */
 static int setup_redirects(struct command *prog, int squirrel[])
@@ -6157,13 +6405,7 @@ static int setup_redirects(struct command *prog, int squirrel[])
        for (redir = prog->redirects; redir; redir = redir->next) {
                if (redir->rd_type == REDIRECT_HEREDOC2) {
                        /* "rd_fd<<HERE" case */
-                       if (redir->rd_fd <= 2
-                        && squirrel
-                        && squirrel[redir->rd_fd] < 0
-                       ) {
-                               /* Save old fds 0..2 if redirect uses them */
-                               squirrel[redir->rd_fd] = dup(redir->rd_fd);
-                       }
+                       save_fds_on_redirect(redir->rd_fd, squirrel);
                        /* for REDIRECT_HEREDOC2, rd_filename holds _contents_
                         * of the heredoc */
                        debug_printf_parse("set heredoc '%s'\n",
@@ -6176,9 +6418,12 @@ static int setup_redirects(struct command *prog, int squirrel[])
                        /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */
                        char *p;
                        if (redir->rd_filename == NULL) {
-                               /* Something went wrong in the parse.
-                                * Pretend it didn't happen */
-                               bb_error_msg("bug in redirect parse");
+                               /*
+                                * Examples:
+                                * "cmd >" (no filename)
+                                * "cmd > <file" (2nd redirect starts too early)
+                                */
+                               die_if_script("syntax error: %s", "invalid redirect");
                                continue;
                        }
                        mode = redir_table[redir->rd_type].mode;
@@ -6199,47 +6444,26 @@ static int setup_redirects(struct command *prog, int squirrel[])
                }
 
                if (openfd != redir->rd_fd) {
-                       if (redir->rd_fd <= 2
-                        && squirrel
-                        && squirrel[redir->rd_fd] < 0
-                       ) {
-                               /* Save old fds 0..2 if redirect uses them */
-//FIXME: script fd's also need this! "sh SCRIPT" and SCRIPT has "echo FOO 3>&-":
-// open("SCRIPT", O_RDONLY)          = 3
-// fcntl(3, F_SETFD, FD_CLOEXEC)     = 0
-// read(3, "echo FOO 3>&-\n....", 4096) = N
-// close(3)                          = 0
-// write(1, "FOO\n", 4)              = 4
-// ...
-// read(3, 0x205f8e0, 4096)          = -1 EBADF <== BUG
-//
-                               squirrel[redir->rd_fd] = dup(redir->rd_fd);
-                       }
+                       int closed = save_fds_on_redirect(redir->rd_fd, squirrel);
                        if (openfd == REDIRFD_CLOSE) {
-                               /* "n>-" means "close me" */
-                               close(redir->rd_fd);
+                               /* "rd_fd >&-" means "close me" */
+                               if (!closed) {
+                                       /* ^^^ optimization: saving may already
+                                        * have closed it. If not... */
+                                       close(redir->rd_fd);
+                               }
                        } else {
                                xdup2(openfd, redir->rd_fd);
                                if (redir->rd_dup == REDIRFD_TO_FILE)
+                                       /* "rd_fd > FILE" */
                                        close(openfd);
+                               /* else: "rd_fd > rd_dup" */
                        }
                }
        }
        return 0;
 }
 
-static void restore_redirects(int squirrel[])
-{
-       int i, fd;
-       for (i = 0; i <= 2; i++) {
-               fd = squirrel[i];
-               if (fd != -1) {
-                       /* We simply die on error */
-                       xmove_fd(fd, i);
-               }
-       }
-}
-
 static char *find_in_path(const char *arg)
 {
        char *ret = NULL;
@@ -6423,8 +6647,8 @@ static int run_function(const struct function *funcp, char **argv)
        save_and_replace_G_args(&sv, argv);
 
        /* "we are in function, ok to use return" */
-       sv_flg = G.flag_return_in_progress;
-       G.flag_return_in_progress = -1;
+       sv_flg = G_flag_return_in_progress;
+       G_flag_return_in_progress = -1;
 # if ENABLE_HUSH_LOCAL
        G.func_nest_level++;
 # endif
@@ -6465,7 +6689,7 @@ static int run_function(const struct function *funcp, char **argv)
                G.func_nest_level--;
        }
 # endif
-       G.flag_return_in_progress = sv_flg;
+       G_flag_return_in_progress = sv_flg;
 
        restore_G_args(&sv, argv);
 
@@ -7489,6 +7713,8 @@ static int run_list(struct pipe *pi)
        for (; pi; pi = IF_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) {
                if (G.flag_SIGINT)
                        break;
+               if (G_flag_return_in_progress == 1)
+                       break;
 
                IF_HAS_KEYWORDS(rword = pi->res_word;)
                debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n",
@@ -7659,12 +7885,10 @@ static int run_list(struct pipe *pi)
                                        continue;
                                }
 #endif
-#if ENABLE_HUSH_FUNCTIONS
-                               if (G.flag_return_in_progress == 1) {
+                               if (G_flag_return_in_progress == 1) {
                                        checkjobs(NULL);
                                        break;
                                }
-#endif
                        } 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:
@@ -8315,7 +8539,7 @@ int hush_main(int argc, char **argv)
 int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int msh_main(int argc, char **argv)
 {
-       //bb_error_msg("msh is deprecated, please use hush instead");
+       bb_error_msg("msh is deprecated, please use hush instead");
        return hush_main(argc, argv);
 }
 #endif
@@ -9053,20 +9277,22 @@ static int FAST_FUNC builtin_source(char **argv)
        }
 
 #if ENABLE_HUSH_FUNCTIONS
-       sv_flg = G.flag_return_in_progress;
+       sv_flg = G_flag_return_in_progress;
        /* "we are inside sourced file, ok to use return" */
-       G.flag_return_in_progress = -1;
+       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);
 
        if (argv[1])
                restore_G_args(&sv, argv);
 #if ENABLE_HUSH_FUNCTIONS
-       G.flag_return_in_progress = sv_flg;
+       G_flag_return_in_progress = sv_flg;
 #endif
 
        return G.last_exitcode;
@@ -9289,12 +9515,12 @@ static int FAST_FUNC builtin_return(char **argv)
 {
        int rc;
 
-       if (G.flag_return_in_progress != -1) {
+       if (G_flag_return_in_progress != -1) {
                bb_error_msg("%s: not in a function or sourced script", argv[0]);
                return EXIT_FAILURE; /* bash compat */
        }
 
-       G.flag_return_in_progress = 1;
+       G_flag_return_in_progress = 1;
 
        /* bash:
         * out of range: wraps around at 256, does not error out