Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3 | * A prototype Bourne shell grammar parser. |
| 4 | * Intended to follow the original Thompson and Ritchie |
| 5 | * "small and simple is beautiful" philosophy, which |
| 6 | * incidentally is a good match to today's BusyBox. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7 | * |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 8 | * Copyright (C) 2000,2001 Larry Doolittle <larry@doolittle.boa.org> |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 9 | * Copyright (C) 2008,2009 Denys Vlasenko <vda.linux@googlemail.com> |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10 | * |
| 11 | * Credits: |
| 12 | * The parser routines proper are all original material, first |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 13 | * written Dec 2000 and Jan 2001 by Larry Doolittle. The |
| 14 | * execution engine, the builtins, and much of the underlying |
| 15 | * support has been adapted from busybox-0.49pre's lash, which is |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 16 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 17 | * written by Erik Andersen <andersen@codepoet.org>. That, in turn, |
| 18 | * is based in part on ladsh.c, by Michael K. Johnson and Erik W. |
| 19 | * Troan, which they placed in the public domain. I don't know |
| 20 | * how much of the Johnson/Troan code has survived the repeated |
| 21 | * rewrites. |
| 22 | * |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 23 | * Other credits: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 24 | * o_addchr derived from similar w_addchar function in glibc-2.2. |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 25 | * parse_redirect, redirect_opt_num, and big chunks of main |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 26 | * and many builtins derived from contributions by Erik Andersen. |
| 27 | * Miscellaneous bugfixes from Matt Kraai. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 28 | * |
| 29 | * There are two big (and related) architecture differences between |
| 30 | * this parser and the lash parser. One is that this version is |
| 31 | * actually designed from the ground up to understand nearly all |
| 32 | * of the Bourne grammar. The second, consequential change is that |
| 33 | * the parser and input reader have been turned inside out. Now, |
| 34 | * the parser is in control, and asks for input as needed. The old |
| 35 | * way had the input reader in control, and it asked for parsing to |
| 36 | * take place as needed. The new way makes it much easier to properly |
| 37 | * handle the recursion implicit in the various substitutions, especially |
| 38 | * across continuation lines. |
| 39 | * |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 40 | * POSIX syntax not implemented: |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 41 | * aliases |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 42 | * <(list) and >(list) Process Substitution |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 43 | * Tilde Expansion |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 44 | * |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 45 | * Bash stuff (maybe optionally enable?): |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 46 | * &> and >& redirection of stdout+stderr |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 47 | * Brace Expansion |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 48 | * reserved words: [[ ]] function select |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 49 | * substrings ${var:1:5} |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 50 | * |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 51 | * TODOs: |
| 52 | * grep for "TODO" and fix (some of them are easy) |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 53 | * builtins: ulimit, local |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 54 | * follow IFS rules more precisely, including update semantics |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 55 | * export builtin should be special, its arguments are assignments |
| 56 | * and therefore expansion of them should be "one-word" expansion: |
| 57 | * $ export i=`echo 'a b'` # export has one arg: "i=a b" |
| 58 | * compare with: |
| 59 | * $ ls i=`echo 'a b'` # ls has two args: "i=a" and "b" |
| 60 | * ls: cannot access i=a: No such file or directory |
| 61 | * ls: cannot access b: No such file or directory |
| 62 | * Note1: same applies to local builtin when we'll have it. |
| 63 | * Note2: bash 3.2.33(1) does this only if export word itself |
| 64 | * is not quoted: |
| 65 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 66 | * aaa bbb |
| 67 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 68 | * aaa |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 69 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 70 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 71 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 72 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 73 | #include <glob.h> |
| 74 | /* #include <dmalloc.h> */ |
| 75 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 76 | # include <fnmatch.h> |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 77 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 78 | #include "math.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 79 | #include "match.h" |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 80 | #ifndef PIPE_BUF |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 81 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 82 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 83 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 84 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 85 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 86 | #define LEAK_HUNTING 0 |
| 87 | #define BUILD_AS_NOMMU 0 |
| 88 | /* Enable/disable sanity checks. Ok to enable in production, |
| 89 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 90 | * Keeping 1 for now even in released versions. |
| 91 | */ |
| 92 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 93 | /* Slightly bigger (+200 bytes), but faster hush. |
| 94 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 95 | * which allows us to do fewer waitpid's. |
| 96 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 97 | * and therefore waitpid will return the same result as last time) |
| 98 | */ |
| 99 | #define ENABLE_HUSH_FAST 0 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 100 | |
| 101 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 102 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 103 | # undef BB_MMU |
| 104 | # undef USE_FOR_NOMMU |
| 105 | # undef USE_FOR_MMU |
| 106 | # define BB_MMU 0 |
| 107 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 108 | # define USE_FOR_MMU(...) |
| 109 | #endif |
| 110 | |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 111 | #if defined SINGLE_APPLET_MAIN |
| 112 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 113 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 114 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 115 | # undef IF_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 116 | # define IF_FEATURE_SH_STANDALONE(...) |
| 117 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denys Vlasenko | 673e945 | 2009-05-27 14:39:35 +0200 | [diff] [blame] | 118 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 119 | #endif |
| 120 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 121 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 122 | # undef ENABLE_FEATURE_EDITING |
| 123 | # define ENABLE_FEATURE_EDITING 0 |
| 124 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 125 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 126 | #endif |
| 127 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 128 | /* Do we support ANY keywords? */ |
| 129 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 130 | # define HAS_KEYWORDS 1 |
| 131 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 132 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 133 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 134 | # define HAS_KEYWORDS 0 |
| 135 | # define IF_HAS_KEYWORDS(...) |
| 136 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 137 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 138 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 139 | /* If you comment out one of these below, it will be #defined later |
| 140 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 141 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 142 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 143 | #define debug_printf_parse(...) do {} while (0) |
| 144 | #define debug_print_tree(a, b) do {} while (0) |
| 145 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 146 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 147 | #define debug_printf_jobs(...) do {} while (0) |
| 148 | #define debug_printf_expand(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 149 | #define debug_printf_glob(...) do {} while (0) |
| 150 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 151 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 152 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 153 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 154 | #define ERR_PTR ((void*)(long)1) |
| 155 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 156 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 157 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 158 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 159 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 160 | struct variable; |
| 161 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 162 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 163 | |
| 164 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 165 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 166 | */ |
| 167 | #if !BB_MMU |
| 168 | typedef struct nommu_save_t { |
| 169 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 170 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 171 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 172 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 173 | } nommu_save_t; |
| 174 | #endif |
| 175 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 176 | /* The descrip member of this structure is only used to make |
| 177 | * debugging output pretty */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 178 | static const struct { |
| 179 | int mode; |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 180 | signed char default_fd; |
| 181 | char descrip[3]; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 182 | } redir_table[] = { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 183 | { 0, 0, "??" }, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 184 | { O_RDONLY, 0, "<" }, |
| 185 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 186 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 187 | { O_RDONLY, 0, "<<" }, |
| 188 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 189 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 190 | /* { O_RDONLY, 77, "<<" }, */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 193 | typedef enum reserved_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 194 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 195 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 196 | RES_IF , |
| 197 | RES_THEN , |
| 198 | RES_ELIF , |
| 199 | RES_ELSE , |
| 200 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 201 | #endif |
| 202 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 203 | RES_FOR , |
| 204 | RES_WHILE , |
| 205 | RES_UNTIL , |
| 206 | RES_DO , |
| 207 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 208 | #endif |
| 209 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 210 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 211 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 212 | #if ENABLE_HUSH_CASE |
| 213 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 214 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 215 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 216 | RES_MATCH , /* "word)" */ |
| 217 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 218 | RES_ESAC , |
| 219 | #endif |
| 220 | RES_XXXX , |
| 221 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 222 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 223 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 224 | typedef struct o_string { |
| 225 | char *data; |
| 226 | int length; /* position where data is appended */ |
| 227 | int maxlen; |
| 228 | /* Protect newly added chars against globbing |
| 229 | * (by prepending \ to *, ?, [, \) */ |
| 230 | smallint o_escape; |
| 231 | smallint o_glob; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 232 | /* At least some part of the string was inside '' or "", |
| 233 | * possibly empty one: word"", wo''rd etc. */ |
| 234 | smallint o_quoted; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 235 | smallint has_empty_slot; |
| 236 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 237 | } o_string; |
| 238 | enum { |
| 239 | MAYBE_ASSIGNMENT = 0, |
| 240 | DEFINITELY_ASSIGNMENT = 1, |
| 241 | NOT_ASSIGNMENT = 2, |
| 242 | WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */ |
| 243 | }; |
| 244 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 245 | #define NULL_O_STRING { NULL } |
| 246 | |
| 247 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
| 248 | * available? Where is it documented? */ |
| 249 | typedef struct in_str { |
| 250 | const char *p; |
| 251 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 252 | char eof_flag; /* meaningless if ->p == NULL */ |
| 253 | char peek_buf[2]; |
| 254 | #if ENABLE_HUSH_INTERACTIVE |
| 255 | smallint promptme; |
| 256 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 257 | #endif |
| 258 | FILE *file; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 259 | int (*get) (struct in_str *) FAST_FUNC; |
| 260 | int (*peek) (struct in_str *) FAST_FUNC; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 261 | } in_str; |
| 262 | #define i_getch(input) ((input)->get(input)) |
| 263 | #define i_peek(input) ((input)->peek(input)) |
| 264 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 265 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 266 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 267 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 268 | int rd_fd; /* fd to redirect */ |
| 269 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 270 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 271 | smallint rd_type; /* (enum redir_type) */ |
| 272 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 273 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
| 274 | * 1: do we need to trim leading tabs? |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 275 | * 2: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 276 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 277 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 278 | typedef enum redir_type { |
| 279 | REDIRECT_INVALID = 0, |
| 280 | REDIRECT_INPUT = 1, |
| 281 | REDIRECT_OVERWRITE = 2, |
| 282 | REDIRECT_APPEND = 3, |
| 283 | REDIRECT_HEREDOC = 4, |
| 284 | REDIRECT_IO = 5, |
| 285 | REDIRECT_HEREDOC2 = 6, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 286 | |
| 287 | REDIRFD_CLOSE = -3, |
| 288 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 289 | REDIRFD_TO_FILE = -1, |
| 290 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 291 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 292 | HEREDOC_SKIPTABS = 1, |
| 293 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 294 | } redir_type; |
| 295 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 296 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 297 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 298 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 299 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 300 | smallint is_stopped; /* is the command currently running? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 301 | smallint grp_type; /* GRP_xxx */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 302 | #define GRP_NORMAL 0 |
| 303 | #define GRP_SUBSHELL 1 |
| 304 | #if ENABLE_HUSH_FUNCTIONS |
| 305 | # define GRP_FUNCTION 2 |
| 306 | #endif |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 307 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 308 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 309 | #if !BB_MMU |
| 310 | char *group_as_string; |
| 311 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 312 | #if ENABLE_HUSH_FUNCTIONS |
| 313 | struct function *child_func; |
| 314 | /* This field is used to prevent a bug here: |
| 315 | * while...do f1() {a;}; f1; f1 {b;}; f1; done |
| 316 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 317 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
| 318 | * when we execute "f1 {b;}", we notice that f1 exists, |
| 319 | * and that it's "parent cmd" struct is still "alive", |
| 320 | * we put those fields back into cmd->xxx |
| 321 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 322 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 323 | * Without this trick, loop would execute a;b;b;b;... |
| 324 | * instead of correct sequence a;b;a;b;... |
| 325 | * When command is freed, it severs the link |
| 326 | * (sets ->child_func->parent_cmd to NULL). |
| 327 | */ |
| 328 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 329 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 330 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 331 | * and on execution these are substituted with their values. |
| 332 | * Substitution can make _several_ words out of one argv[n]! |
| 333 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 334 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 335 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 336 | struct redir_struct *redirects; /* I/O redirections */ |
| 337 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 338 | /* Is there anything in this command at all? */ |
| 339 | #define IS_NULL_CMD(cmd) \ |
| 340 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 341 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 342 | |
| 343 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 344 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 345 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 346 | int alive_cmds; /* number of commands running (not exited) */ |
| 347 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 348 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 349 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 350 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 351 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 352 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 353 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 354 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 355 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 356 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 357 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 358 | typedef enum pipe_style { |
| 359 | PIPE_SEQ = 1, |
| 360 | PIPE_AND = 2, |
| 361 | PIPE_OR = 3, |
| 362 | PIPE_BG = 4, |
| 363 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 364 | /* Is there anything in this pipe at all? */ |
| 365 | #define IS_NULL_PIPE(pi) \ |
| 366 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 367 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 368 | /* This holds pointers to the various results of parsing */ |
| 369 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 370 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 371 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 372 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 373 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 374 | /* last command in pipe (being constructed right now) */ |
| 375 | struct command *command; |
| 376 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 377 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 378 | #if !BB_MMU |
| 379 | o_string as_string; |
| 380 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 381 | #if HAS_KEYWORDS |
| 382 | smallint ctx_res_w; |
| 383 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 384 | #if ENABLE_HUSH_CASE |
| 385 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 386 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 387 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 388 | int old_flag; |
| 389 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 390 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 391 | * when we see "if" or "then", we malloc and copy current context, |
| 392 | * and make ->stack point to it. then we parse pipeN. |
| 393 | * when closing "then" / fi" / whatever is found, |
| 394 | * we move list_head into ->stack->command->group, |
| 395 | * copy ->stack into current context, and delete ->stack. |
| 396 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 397 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 398 | struct parse_context *stack; |
| 399 | #endif |
| 400 | }; |
| 401 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 402 | /* On program start, environ points to initial environment. |
| 403 | * putenv adds new pointers into it, unsetenv removes them. |
| 404 | * Neither of these (de)allocates the strings. |
| 405 | * setenv allocates new strings in malloc space and does putenv, |
| 406 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 407 | #define setenv(...) setenv_is_leaky_dont_use() |
| 408 | struct variable { |
| 409 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 410 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 411 | #if ENABLE_HUSH_LOCAL |
| 412 | unsigned func_nest_level; |
| 413 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 414 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 415 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 416 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 417 | }; |
| 418 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 419 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 420 | BC_BREAK = 1, |
| 421 | BC_CONTINUE = 2, |
| 422 | }; |
| 423 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 424 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 425 | struct function { |
| 426 | struct function *next; |
| 427 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 428 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 429 | struct pipe *body; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 430 | #if !BB_MMU |
| 431 | char *body_as_string; |
| 432 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 433 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 434 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 435 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 436 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 437 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 438 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 439 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 440 | /* interactive_fd != 0 means we are an interactive shell. |
| 441 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 442 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 443 | * job control still works, but terminal signals |
| 444 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 445 | * process groups can only be created with "cmd &". |
| 446 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 447 | * to give tty to the foreground process group, |
| 448 | * and will take it back when the group is stopped (^Z) |
| 449 | * or killed (^C). |
| 450 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 451 | #if ENABLE_HUSH_INTERACTIVE |
| 452 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 453 | * _AND_ if we decided to act interactively */ |
| 454 | int interactive_fd; |
| 455 | const char *PS1; |
| 456 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 457 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 458 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 459 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 460 | #endif |
| 461 | #if ENABLE_FEATURE_EDITING |
| 462 | line_input_t *line_input_state; |
| 463 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 464 | pid_t root_pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 465 | pid_t last_bg_pid; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 466 | #if ENABLE_HUSH_JOB |
| 467 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 468 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 469 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 470 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 471 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 472 | #else |
| 473 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 474 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 475 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 476 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 477 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 478 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 479 | #if ENABLE_HUSH_FUNCTIONS |
| 480 | /* 0: outside of a function (or sourced file) |
| 481 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 482 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 483 | */ |
| 484 | smallint flag_return_in_progress; |
| 485 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 486 | smallint fake_mode; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 487 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 488 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 489 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 490 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 491 | smalluint global_args_malloced; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 492 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 493 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 494 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 495 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 496 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 497 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 498 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 499 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 500 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 501 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 502 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 503 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 504 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 505 | struct variable shell_ver; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 506 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 507 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 508 | # if ENABLE_HUSH_LOCAL |
| 509 | struct variable **shadowed_vars_pp; |
| 510 | unsigned func_nest_level; |
| 511 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 512 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 513 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 514 | #if ENABLE_HUSH_FAST |
| 515 | unsigned count_SIGCHLD; |
| 516 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 517 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 518 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 519 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 520 | unsigned non_DFL_mask; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 521 | char **traps; /* char *traps[NSIG] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 522 | sigset_t blocked_set; |
| 523 | sigset_t inherited_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 524 | #if HUSH_DEBUG |
| 525 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 526 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 527 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 528 | char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2]; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 529 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 530 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 531 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 532 | * (too many defines). Also, I actually prefer to see when a variable |
| 533 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 534 | #define INIT_G() do { \ |
| 535 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 536 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 537 | |
| 538 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 539 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 540 | static int builtin_cd(char **argv) FAST_FUNC; |
| 541 | static int builtin_echo(char **argv) FAST_FUNC; |
| 542 | static int builtin_eval(char **argv) FAST_FUNC; |
| 543 | static int builtin_exec(char **argv) FAST_FUNC; |
| 544 | static int builtin_exit(char **argv) FAST_FUNC; |
| 545 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 546 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 547 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 548 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 549 | #endif |
| 550 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 551 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 552 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 553 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 554 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 555 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 556 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 557 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 558 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 559 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 560 | static int builtin_read(char **argv) FAST_FUNC; |
| 561 | static int builtin_set(char **argv) FAST_FUNC; |
| 562 | static int builtin_shift(char **argv) FAST_FUNC; |
| 563 | static int builtin_source(char **argv) FAST_FUNC; |
| 564 | static int builtin_test(char **argv) FAST_FUNC; |
| 565 | static int builtin_trap(char **argv) FAST_FUNC; |
| 566 | static int builtin_type(char **argv) FAST_FUNC; |
| 567 | static int builtin_true(char **argv) FAST_FUNC; |
| 568 | static int builtin_umask(char **argv) FAST_FUNC; |
| 569 | static int builtin_unset(char **argv) FAST_FUNC; |
| 570 | static int builtin_wait(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 571 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 572 | static int builtin_break(char **argv) FAST_FUNC; |
| 573 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 574 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 575 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 576 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 577 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 578 | |
| 579 | /* Table of built-in functions. They can be forked or not, depending on |
| 580 | * context: within pipes, they fork. As simple commands, they do not. |
| 581 | * When used in non-forking context, they can change global variables |
| 582 | * in the parent shell process. If forked, of course they cannot. |
| 583 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 584 | * still be set at the end. */ |
| 585 | struct built_in_command { |
| 586 | const char *cmd; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 587 | int (*function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 588 | #if ENABLE_HUSH_HELP |
| 589 | const char *descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 590 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 591 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 592 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 593 | #endif |
| 594 | }; |
| 595 | |
| 596 | /* For now, echo and test are unconditionally enabled. |
| 597 | * Maybe make it configurable? */ |
| 598 | static const struct built_in_command bltins[] = { |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 599 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 600 | BLTIN(":" , builtin_true , "No-op"), |
| 601 | BLTIN("[" , builtin_test , "Test condition"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 602 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 603 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 604 | #endif |
| 605 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 606 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 607 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 608 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 609 | #if ENABLE_HUSH_LOOPS |
| 610 | BLTIN("continue", builtin_continue, "Start new loop iteration"), |
| 611 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 612 | BLTIN("echo" , builtin_echo , "Write to stdout"), |
| 613 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 614 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 615 | BLTIN("exit" , builtin_exit , "Exit"), |
| 616 | BLTIN("export" , builtin_export , "Set environment variable"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 617 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 618 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 619 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 620 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 621 | BLTIN("help" , builtin_help , "List shell built-in commands"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 622 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 623 | #if ENABLE_HUSH_JOB |
| 624 | BLTIN("jobs" , builtin_jobs , "List active jobs"), |
| 625 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 626 | #if ENABLE_HUSH_LOCAL |
| 627 | BLTIN("local" , builtin_local , "Set local variable"), |
| 628 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 629 | #if HUSH_DEBUG |
| 630 | BLTIN("memleak" , builtin_memleak , "Debug tool"), |
| 631 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 632 | BLTIN("pwd" , builtin_pwd , "Print current directory"), |
| 633 | BLTIN("read" , builtin_read , "Input environment variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 634 | #if ENABLE_HUSH_FUNCTIONS |
| 635 | BLTIN("return" , builtin_return , "Return from a function"), |
| 636 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 637 | BLTIN("set" , builtin_set , "Set/unset shell local variables"), |
| 638 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
| 639 | BLTIN("test" , builtin_test , "Test condition"), |
| 640 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 641 | BLTIN("type" , builtin_type , "Write a description of command type"), |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 642 | // BLTIN("ulimit" , builtin_ulimit , "Control resource limits"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 643 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 644 | BLTIN("unset" , builtin_unset , "Unset environment variable"), |
| 645 | BLTIN("wait" , builtin_wait , "Wait for process"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 646 | }; |
| 647 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 648 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 649 | /* Debug printouts. |
| 650 | */ |
| 651 | #if HUSH_DEBUG |
| 652 | /* prevent disasters with G.debug_indent < 0 */ |
| 653 | # define indent() fprintf(stderr, "%*s", (G.debug_indent * 2) & 0xff, "") |
| 654 | # define debug_enter() (G.debug_indent++) |
| 655 | # define debug_leave() (G.debug_indent--) |
| 656 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 657 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 658 | # define debug_enter() ((void)0) |
| 659 | # define debug_leave() ((void)0) |
| 660 | #endif |
| 661 | |
| 662 | #ifndef debug_printf |
| 663 | # define debug_printf(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 664 | #endif |
| 665 | |
| 666 | #ifndef debug_printf_parse |
| 667 | # define debug_printf_parse(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 668 | #endif |
| 669 | |
| 670 | #ifndef debug_printf_exec |
| 671 | #define debug_printf_exec(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 672 | #endif |
| 673 | |
| 674 | #ifndef debug_printf_env |
| 675 | # define debug_printf_env(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 676 | #endif |
| 677 | |
| 678 | #ifndef debug_printf_jobs |
| 679 | # define debug_printf_jobs(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 680 | # define DEBUG_JOBS 1 |
| 681 | #else |
| 682 | # define DEBUG_JOBS 0 |
| 683 | #endif |
| 684 | |
| 685 | #ifndef debug_printf_expand |
| 686 | # define debug_printf_expand(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 687 | # define DEBUG_EXPAND 1 |
| 688 | #else |
| 689 | # define DEBUG_EXPAND 0 |
| 690 | #endif |
| 691 | |
| 692 | #ifndef debug_printf_glob |
| 693 | # define debug_printf_glob(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 694 | # define DEBUG_GLOB 1 |
| 695 | #else |
| 696 | # define DEBUG_GLOB 0 |
| 697 | #endif |
| 698 | |
| 699 | #ifndef debug_printf_list |
| 700 | # define debug_printf_list(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 701 | #endif |
| 702 | |
| 703 | #ifndef debug_printf_subst |
| 704 | # define debug_printf_subst(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 705 | #endif |
| 706 | |
| 707 | #ifndef debug_printf_clean |
| 708 | # define debug_printf_clean(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 709 | # define DEBUG_CLEAN 1 |
| 710 | #else |
| 711 | # define DEBUG_CLEAN 0 |
| 712 | #endif |
| 713 | |
| 714 | #if DEBUG_EXPAND |
| 715 | static void debug_print_strings(const char *prefix, char **vv) |
| 716 | { |
| 717 | indent(); |
| 718 | fprintf(stderr, "%s:\n", prefix); |
| 719 | while (*vv) |
| 720 | fprintf(stderr, " '%s'\n", *vv++); |
| 721 | } |
| 722 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 723 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 724 | #endif |
| 725 | |
| 726 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 727 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 728 | */ |
| 729 | #if LEAK_HUNTING |
| 730 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 731 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 732 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 733 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 734 | return ptr; |
| 735 | } |
| 736 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 737 | { |
| 738 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 739 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 740 | return ptr; |
| 741 | } |
| 742 | static char *xxstrdup(int lineno, const char *str) |
| 743 | { |
| 744 | char *ptr = xstrdup(str); |
| 745 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 746 | return ptr; |
| 747 | } |
| 748 | static void xxfree(void *ptr) |
| 749 | { |
| 750 | fdprintf(2, "free %p\n", ptr); |
| 751 | free(ptr); |
| 752 | } |
| 753 | #define xmalloc(s) xxmalloc(__LINE__, s) |
| 754 | #define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 755 | #define xstrdup(s) xxstrdup(__LINE__, s) |
| 756 | #define free(p) xxfree(p) |
| 757 | #endif |
| 758 | |
| 759 | |
| 760 | /* Syntax and runtime errors. They always abort scripts. |
| 761 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 762 | * and return to the prompt. |
| 763 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 764 | */ |
| 765 | #if HUSH_DEBUG < 2 |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 766 | # define die_if_script(lineno, fmt...) die_if_script(fmt) |
| 767 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 768 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 769 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 770 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 771 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 772 | #endif |
| 773 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 774 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 775 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 776 | va_list p; |
| 777 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 778 | #if HUSH_DEBUG >= 2 |
| 779 | bb_error_msg("hush.c:%u", lineno); |
| 780 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 781 | va_start(p, fmt); |
| 782 | bb_verror_msg(fmt, p, NULL); |
| 783 | va_end(p); |
| 784 | if (!G_interactive_fd) |
| 785 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 786 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 787 | |
| 788 | static void syntax_error(unsigned lineno, const char *msg) |
| 789 | { |
| 790 | if (msg) |
| 791 | die_if_script(lineno, "syntax error: %s", msg); |
| 792 | else |
| 793 | die_if_script(lineno, "syntax error", NULL); |
| 794 | } |
| 795 | |
| 796 | static void syntax_error_at(unsigned lineno, const char *msg) |
| 797 | { |
| 798 | die_if_script(lineno, "syntax error at '%s'", msg); |
| 799 | } |
| 800 | |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 801 | static void syntax_error_unterm_str(unsigned lineno, const char *s) |
| 802 | { |
| 803 | die_if_script(lineno, "syntax error: unterminated %s", s); |
| 804 | } |
| 805 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 806 | /* It so happens that all such cases are totally fatal |
| 807 | * even if shell is interactive: EOF while looking for closing |
| 808 | * delimiter. There is nowhere to read stuff from after that, |
| 809 | * it's EOF! The only choice is to terminate. |
| 810 | */ |
| 811 | static void syntax_error_unterm_ch(unsigned lineno, char ch) NORETURN; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 812 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 813 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 814 | char msg[2] = { ch, '\0' }; |
| 815 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 816 | xfunc_die(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 819 | static void syntax_error_unexpected_ch(unsigned lineno, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 820 | { |
| 821 | char msg[2]; |
| 822 | msg[0] = ch; |
| 823 | msg[1] = '\0'; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 824 | die_if_script(lineno, "syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 827 | #if HUSH_DEBUG < 2 |
| 828 | # undef die_if_script |
| 829 | # undef syntax_error |
| 830 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 831 | # undef syntax_error_unterm_ch |
| 832 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 833 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 834 | #else |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 835 | # define die_if_script(fmt...) die_if_script(__LINE__, fmt) |
| 836 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 837 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 838 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 839 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 840 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 841 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 842 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 843 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 844 | #if ENABLE_HUSH_INTERACTIVE |
| 845 | static void cmdedit_update_prompt(void); |
| 846 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 847 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 848 | #endif |
| 849 | |
| 850 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 851 | /* Utility functions |
| 852 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 853 | static int glob_needed(const char *s) |
| 854 | { |
| 855 | while (*s) { |
| 856 | if (*s == '\\') |
| 857 | s++; |
| 858 | if (*s == '*' || *s == '[' || *s == '?') |
| 859 | return 1; |
| 860 | s++; |
| 861 | } |
| 862 | return 0; |
| 863 | } |
| 864 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 865 | static int is_well_formed_var_name(const char *s, char terminator) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 866 | { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 867 | if (!s || !(isalpha(*s) || *s == '_')) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 868 | return 0; |
| 869 | s++; |
| 870 | while (isalnum(*s) || *s == '_') |
| 871 | s++; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 872 | return *s == terminator; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 875 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 876 | static char *unbackslash(char *src) |
| 877 | { |
| 878 | char *dst = src; |
| 879 | while (1) { |
| 880 | if (*src == '\\') |
| 881 | src++; |
| 882 | if ((*dst++ = *src++) == '\0') |
| 883 | break; |
| 884 | } |
| 885 | return dst; |
| 886 | } |
| 887 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 888 | static char **add_strings_to_strings(char **strings, char **add, int need_to_dup) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 889 | { |
| 890 | int i; |
| 891 | unsigned count1; |
| 892 | unsigned count2; |
| 893 | char **v; |
| 894 | |
| 895 | v = strings; |
| 896 | count1 = 0; |
| 897 | if (v) { |
| 898 | while (*v) { |
| 899 | count1++; |
| 900 | v++; |
| 901 | } |
| 902 | } |
| 903 | count2 = 0; |
| 904 | v = add; |
| 905 | while (*v) { |
| 906 | count2++; |
| 907 | v++; |
| 908 | } |
| 909 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 910 | v[count1 + count2] = NULL; |
| 911 | i = count2; |
| 912 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 913 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 914 | return v; |
| 915 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 916 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 917 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 918 | { |
| 919 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 920 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 921 | return ptr; |
| 922 | } |
| 923 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 924 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 925 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 926 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 927 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 928 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 929 | { |
| 930 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 931 | v[0] = add; |
| 932 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 933 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 934 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 935 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 936 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 937 | { |
| 938 | char **ptr = add_string_to_strings(strings, add); |
| 939 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 940 | return ptr; |
| 941 | } |
| 942 | #define add_string_to_strings(strings, add) \ |
| 943 | xx_add_string_to_strings(__LINE__, strings, add) |
| 944 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 945 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 946 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 947 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 948 | char **v; |
| 949 | |
| 950 | if (!strings) |
| 951 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 952 | v = strings; |
| 953 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 954 | free(*v); |
| 955 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 956 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 957 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 960 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 961 | /* Helpers for setting new $n and restoring them back |
| 962 | */ |
| 963 | typedef struct save_arg_t { |
| 964 | char *sv_argv0; |
| 965 | char **sv_g_argv; |
| 966 | int sv_g_argc; |
| 967 | smallint sv_g_malloced; |
| 968 | } save_arg_t; |
| 969 | |
| 970 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 971 | { |
| 972 | int n; |
| 973 | |
| 974 | sv->sv_argv0 = argv[0]; |
| 975 | sv->sv_g_argv = G.global_argv; |
| 976 | sv->sv_g_argc = G.global_argc; |
| 977 | sv->sv_g_malloced = G.global_args_malloced; |
| 978 | |
| 979 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 980 | G.global_argv = argv; |
| 981 | G.global_args_malloced = 0; |
| 982 | |
| 983 | n = 1; |
| 984 | while (*++argv) |
| 985 | n++; |
| 986 | G.global_argc = n; |
| 987 | } |
| 988 | |
| 989 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 990 | { |
| 991 | char **pp; |
| 992 | |
| 993 | if (G.global_args_malloced) { |
| 994 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 995 | pp = G.global_argv; |
| 996 | while (*++pp) /* note: does not free $0 */ |
| 997 | free(*pp); |
| 998 | free(G.global_argv); |
| 999 | } |
| 1000 | argv[0] = sv->sv_argv0; |
| 1001 | G.global_argv = sv->sv_g_argv; |
| 1002 | G.global_argc = sv->sv_g_argc; |
| 1003 | G.global_args_malloced = sv->sv_g_malloced; |
| 1004 | } |
| 1005 | |
| 1006 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1007 | /* Basic theory of signal handling in shell |
| 1008 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1009 | * This does not describe what hush does, rather, it is current understanding |
| 1010 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1011 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1012 | * |
| 1013 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1014 | * is finished or backgrounded. It is the same in interactive and |
| 1015 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1016 | * a user trap handler is installed or a shell special one is in effect. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1017 | * ^C or ^Z from keyboard seem to execute "at once" because it usually |
| 1018 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1019 | * pipe. |
| 1020 | * |
| 1021 | * Wait builtin in interruptible by signals for which user trap is set |
| 1022 | * or by SIGINT in interactive shell. |
| 1023 | * |
| 1024 | * Trap handlers will execute even within trap handlers. (right?) |
| 1025 | * |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1026 | * User trap handlers are forgotten when subshell ("(cmd)") is entered. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1027 | * |
| 1028 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1029 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1030 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1031 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1032 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1033 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1034 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1035 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1036 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1037 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1038 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1039 | * |
| 1040 | * SIGQUIT: ignore |
| 1041 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1042 | * SIGHUP (interactive): |
| 1043 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1044 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1045 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1046 | * that all pipe members are stopped. Try this in bash: |
| 1047 | * while :; do :; done - ^Z does not background it |
| 1048 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1049 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1050 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1051 | * to interactive shell while shell is waiting for a pipe, |
| 1052 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1053 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1054 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1055 | * Example 2: this does not wait and does not execute ls: |
| 1056 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1057 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1058 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1059 | * |
| 1060 | * (What happens to signals which are IGN on shell start?) |
| 1061 | * (What happens with signal mask on shell start?) |
| 1062 | * |
| 1063 | * Implementation in hush |
| 1064 | * ====================== |
| 1065 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1066 | * We block all signals which we don't want to take action immediately, |
| 1067 | * i.e. we block all signals which need to have special handling as described |
| 1068 | * above, and all signals which have traps set. |
| 1069 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1070 | * and act on them. |
| 1071 | * |
| 1072 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 1073 | * sigset_t blocked_set: current blocked signal set |
| 1074 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1075 | * "trap - SIGxxx": |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1076 | * clear bit in blocked_set unless it is also in non_DFL_mask |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1077 | * "trap 'cmd' SIGxxx": |
| 1078 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1079 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1080 | * unblock signals with special interactive handling |
| 1081 | * (child shell is not interactive), |
| 1082 | * unset all traps (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1083 | * after [v]fork, if we plan to exec: |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1084 | * POSIX says pending signal mask is cleared in child - no need to clear it. |
| 1085 | * Restore blocked signal set to one inherited by shell just prior to exec. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1086 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1087 | * Note: as a result, we do not use signal handlers much. The only uses |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1088 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1089 | * and to restore tty pgrp on signal-induced exit. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1090 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1091 | enum { |
| 1092 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1093 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1094 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1095 | | (1 << SIGHUP) |
| 1096 | , |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1097 | SPECIAL_JOB_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1098 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1099 | | (1 << SIGTTIN) |
| 1100 | | (1 << SIGTTOU) |
| 1101 | | (1 << SIGTSTP) |
| 1102 | #endif |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1103 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1104 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1105 | #if ENABLE_HUSH_FAST |
| 1106 | static void SIGCHLD_handler(int sig UNUSED_PARAM) |
| 1107 | { |
| 1108 | G.count_SIGCHLD++; |
| 1109 | //bb_error_msg("[%d] SIGCHLD_handler: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1110 | } |
| 1111 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1112 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1113 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1114 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1115 | /* After [v]fork, in child: do not restore tty pgrp on xfunc death */ |
| 1116 | #define disable_restore_tty_pgrp_on_exit() (die_sleep = 0) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1117 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1118 | #define enable_restore_tty_pgrp_on_exit() (die_sleep = -1) |
| 1119 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1120 | /* Restores tty foreground process group, and exits. |
| 1121 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1122 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1123 | * or called directly with -EXITCODE. |
| 1124 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1125 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1126 | static void sigexit(int sig) |
| 1127 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1128 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 1129 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1130 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1131 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1132 | * tty pgrp then, only top-level shell process does that */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1133 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 1134 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1135 | |
| 1136 | /* Not a signal, just exit */ |
| 1137 | if (sig <= 0) |
| 1138 | _exit(- sig); |
| 1139 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1140 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1141 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1142 | #else |
| 1143 | |
| 1144 | #define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1145 | #define enable_restore_tty_pgrp_on_exit() ((void)0) |
| 1146 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1147 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1148 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1149 | /* Restores tty foreground process group, and exits. */ |
| 1150 | static void hush_exit(int exitcode) NORETURN; |
| 1151 | static void hush_exit(int exitcode) |
| 1152 | { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1153 | if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) { |
| 1154 | /* Prevent recursion: |
| 1155 | * trap "echo Hi; exit" EXIT; exit |
| 1156 | */ |
| 1157 | char *argv[] = { NULL, G.traps[0], NULL }; |
| 1158 | G.traps[0] = NULL; |
| 1159 | G.exiting = 1; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1160 | builtin_eval(argv); |
| 1161 | free(argv[1]); |
| 1162 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1163 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1164 | #if ENABLE_HUSH_JOB |
| 1165 | fflush(NULL); /* flush all streams */ |
| 1166 | sigexit(- (exitcode & 0xff)); |
| 1167 | #else |
| 1168 | exit(exitcode); |
| 1169 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1172 | static int check_and_run_traps(int sig) |
| 1173 | { |
| 1174 | static const struct timespec zero_timespec = { 0, 0 }; |
| 1175 | smalluint save_rcode; |
| 1176 | int last_sig = 0; |
| 1177 | |
| 1178 | if (sig) |
| 1179 | goto jump_in; |
| 1180 | while (1) { |
| 1181 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_timespec); |
| 1182 | if (sig <= 0) |
| 1183 | break; |
| 1184 | jump_in: |
| 1185 | last_sig = sig; |
| 1186 | if (G.traps && G.traps[sig]) { |
| 1187 | if (G.traps[sig][0]) { |
| 1188 | /* We have user-defined handler */ |
| 1189 | char *argv[] = { NULL, xstrdup(G.traps[sig]), NULL }; |
| 1190 | save_rcode = G.last_exitcode; |
| 1191 | builtin_eval(argv); |
| 1192 | free(argv[1]); |
| 1193 | G.last_exitcode = save_rcode; |
| 1194 | } /* else: "" trap, ignoring signal */ |
| 1195 | continue; |
| 1196 | } |
| 1197 | /* not a trap: special action */ |
| 1198 | switch (sig) { |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1199 | #if ENABLE_HUSH_FAST |
| 1200 | case SIGCHLD: |
| 1201 | G.count_SIGCHLD++; |
| 1202 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1203 | break; |
| 1204 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1205 | case SIGINT: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1206 | /* Builtin was ^C'ed, make it look prettier: */ |
| 1207 | bb_putchar('\n'); |
| 1208 | G.flag_SIGINT = 1; |
| 1209 | break; |
| 1210 | #if ENABLE_HUSH_JOB |
| 1211 | case SIGHUP: { |
| 1212 | struct pipe *job; |
| 1213 | /* bash is observed to signal whole process groups, |
| 1214 | * not individual processes */ |
| 1215 | for (job = G.job_list; job; job = job->next) { |
| 1216 | if (job->pgrp <= 0) |
| 1217 | continue; |
| 1218 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1219 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1220 | kill(- job->pgrp, SIGCONT); |
| 1221 | } |
| 1222 | sigexit(SIGHUP); |
| 1223 | } |
| 1224 | #endif |
| 1225 | default: /* ignored: */ |
| 1226 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
| 1227 | break; |
| 1228 | } |
| 1229 | } |
| 1230 | return last_sig; |
| 1231 | } |
| 1232 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1233 | |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1234 | static const char *set_cwd(void) |
| 1235 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1236 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1237 | * we must not try to free(bb_msg_unknown) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1238 | if (G.cwd == bb_msg_unknown) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1239 | G.cwd = NULL; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1240 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1241 | if (!G.cwd) |
| 1242 | G.cwd = bb_msg_unknown; |
| 1243 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1246 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1247 | /* |
| 1248 | * Shell and environment variable support |
| 1249 | */ |
| 1250 | static struct variable **get_ptr_to_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1251 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1252 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1253 | struct variable *cur; |
| 1254 | int len; |
| 1255 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1256 | len = strlen(name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1257 | pp = &G.top_var; |
| 1258 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1259 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1260 | return pp; |
| 1261 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1262 | } |
| 1263 | return NULL; |
| 1264 | } |
| 1265 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1266 | static struct variable *get_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1267 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1268 | struct variable **pp = get_ptr_to_local_var(name); |
| 1269 | if (pp) |
| 1270 | return *pp; |
| 1271 | return NULL; |
| 1272 | } |
| 1273 | |
| 1274 | static const char *get_local_var_value(const char *name) |
| 1275 | { |
| 1276 | struct variable **pp = get_ptr_to_local_var(name); |
| 1277 | if (pp) |
| 1278 | return strchr((*pp)->varstr, '=') + 1; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1279 | return NULL; |
| 1280 | } |
| 1281 | |
| 1282 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1283 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1284 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1285 | * 0: do not change export flag |
| 1286 | * (if creating new variable, flag will be 0) |
| 1287 | * 1: set export flag and putenv the variable |
| 1288 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1289 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1290 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1291 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1292 | /* all params are used */ |
| 1293 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1294 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1295 | set_local_var(str, flg_export, local_lvl) |
| 1296 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1297 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1298 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1299 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1300 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1301 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1302 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1303 | static int set_local_var(char *str, int flg_export, int local_lvl, int flg_read_only) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1304 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1305 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1306 | struct variable *cur; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1307 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1308 | int name_len; |
| 1309 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1310 | eq_sign = strchr(str, '='); |
| 1311 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1312 | free(str); |
| 1313 | return -1; |
| 1314 | } |
| 1315 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1316 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1317 | var_pp = &G.top_var; |
| 1318 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1319 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1320 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1321 | continue; |
| 1322 | } |
| 1323 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1324 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1325 | #if !BB_MMU |
| 1326 | if (!flg_read_only) |
| 1327 | #endif |
| 1328 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1329 | free(str); |
| 1330 | return -1; |
| 1331 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1332 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1333 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1334 | *eq_sign = '\0'; |
| 1335 | unsetenv(str); |
| 1336 | *eq_sign = '='; |
| 1337 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1338 | #if ENABLE_HUSH_LOCAL |
| 1339 | if (cur->func_nest_level < local_lvl) { |
| 1340 | /* New variable is declared as local, |
| 1341 | * and existing one is global, or local |
| 1342 | * from enclosing function. |
| 1343 | * Remove and save old one: */ |
| 1344 | *var_pp = cur->next; |
| 1345 | cur->next = *G.shadowed_vars_pp; |
| 1346 | *G.shadowed_vars_pp = cur; |
| 1347 | /* bash 3.2.33(1) and exported vars: |
| 1348 | * # export z=z |
| 1349 | * # f() { local z=a; env | grep ^z; } |
| 1350 | * # f |
| 1351 | * z=a |
| 1352 | * # env | grep ^z |
| 1353 | * z=z |
| 1354 | */ |
| 1355 | if (cur->flg_export) |
| 1356 | flg_export = 1; |
| 1357 | break; |
| 1358 | } |
| 1359 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1360 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1361 | free_and_exp: |
| 1362 | free(str); |
| 1363 | goto exp; |
| 1364 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1365 | if (cur->max_len != 0) { |
| 1366 | if (cur->max_len >= strlen(str)) { |
| 1367 | /* This one is from startup env, reuse space */ |
| 1368 | strcpy(cur->varstr, str); |
| 1369 | goto free_and_exp; |
| 1370 | } |
| 1371 | } else { |
| 1372 | /* max_len == 0 signifies "malloced" var, which we can |
| 1373 | * (and has to) free */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1374 | free(cur->varstr); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1375 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1376 | cur->max_len = 0; |
| 1377 | goto set_str_and_exp; |
| 1378 | } |
| 1379 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1380 | /* Not found - create new variable struct */ |
| 1381 | cur = xzalloc(sizeof(*cur)); |
| 1382 | #if ENABLE_HUSH_LOCAL |
| 1383 | cur->func_nest_level = local_lvl; |
| 1384 | #endif |
| 1385 | cur->next = *var_pp; |
| 1386 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1387 | |
| 1388 | set_str_and_exp: |
| 1389 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1390 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1391 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1392 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1393 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1394 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1395 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1396 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1397 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1398 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1399 | if (flg_export == -1) { |
| 1400 | cur->flg_export = 0; |
| 1401 | /* unsetenv was already done */ |
| 1402 | } else { |
| 1403 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1404 | return putenv(cur->varstr); |
| 1405 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1406 | } |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1410 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1411 | { |
| 1412 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1413 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1414 | |
| 1415 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1416 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1417 | var_pp = &G.top_var; |
| 1418 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1419 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1420 | if (cur->flg_read_only) { |
| 1421 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1422 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1423 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1424 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1425 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1426 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1427 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1428 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1429 | if (!cur->max_len) |
| 1430 | free(cur->varstr); |
| 1431 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1432 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1433 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1434 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1435 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1436 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1439 | static int unset_local_var(const char *name) |
| 1440 | { |
| 1441 | return unset_local_var_len(name, strlen(name)); |
| 1442 | } |
| 1443 | |
| 1444 | static void unset_vars(char **strings) |
| 1445 | { |
| 1446 | char **v; |
| 1447 | |
| 1448 | if (!strings) |
| 1449 | return; |
| 1450 | v = strings; |
| 1451 | while (*v) { |
| 1452 | const char *eq = strchrnul(*v, '='); |
| 1453 | unset_local_var_len(*v, (int)(eq - *v)); |
| 1454 | v++; |
| 1455 | } |
| 1456 | free(strings); |
| 1457 | } |
| 1458 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1459 | #if ENABLE_SH_MATH_SUPPORT |
| 1460 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1461 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1462 | static char *endofname(const char *name) |
| 1463 | { |
| 1464 | char *p; |
| 1465 | |
| 1466 | p = (char *) name; |
| 1467 | if (!is_name(*p)) |
| 1468 | return p; |
| 1469 | while (*++p) { |
| 1470 | if (!is_in_name(*p)) |
| 1471 | break; |
| 1472 | } |
| 1473 | return p; |
| 1474 | } |
| 1475 | |
| 1476 | static void arith_set_local_var(const char *name, const char *val, int flags) |
| 1477 | { |
| 1478 | /* arith code doesnt malloc space, so do it for it */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1479 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1480 | set_local_var(var, flags, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1481 | } |
| 1482 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1483 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1484 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1485 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1486 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 1487 | */ |
| 1488 | static void add_vars(struct variable *var) |
| 1489 | { |
| 1490 | struct variable *next; |
| 1491 | |
| 1492 | while (var) { |
| 1493 | next = var->next; |
| 1494 | var->next = G.top_var; |
| 1495 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1496 | if (var->flg_export) { |
| 1497 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1498 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1499 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1500 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1501 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1502 | var = next; |
| 1503 | } |
| 1504 | } |
| 1505 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1506 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1507 | { |
| 1508 | char **s; |
| 1509 | struct variable *old = NULL; |
| 1510 | |
| 1511 | if (!strings) |
| 1512 | return old; |
| 1513 | s = strings; |
| 1514 | while (*s) { |
| 1515 | struct variable *var_p; |
| 1516 | struct variable **var_pp; |
| 1517 | char *eq; |
| 1518 | |
| 1519 | eq = strchr(*s, '='); |
| 1520 | if (eq) { |
| 1521 | *eq = '\0'; |
| 1522 | var_pp = get_ptr_to_local_var(*s); |
| 1523 | *eq = '='; |
| 1524 | if (var_pp) { |
| 1525 | /* Remove variable from global linked list */ |
| 1526 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1527 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1528 | *var_pp = var_p->next; |
| 1529 | /* Add it to returned list */ |
| 1530 | var_p->next = old; |
| 1531 | old = var_p; |
| 1532 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1533 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1534 | } |
| 1535 | s++; |
| 1536 | } |
| 1537 | return old; |
| 1538 | } |
| 1539 | |
| 1540 | |
| 1541 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1542 | * in_str support |
| 1543 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 1544 | static int FAST_FUNC static_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1545 | { |
| 1546 | int ch = *i->p++; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1547 | if (ch != '\0') |
| 1548 | return ch; |
| 1549 | i->p--; |
| 1550 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 1553 | static int FAST_FUNC static_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1554 | { |
| 1555 | return *i->p; |
| 1556 | } |
| 1557 | |
| 1558 | #if ENABLE_HUSH_INTERACTIVE |
| 1559 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1560 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1561 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1562 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1563 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1564 | if (G.PS1 == NULL) |
| 1565 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1566 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1567 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1568 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1569 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1570 | if (G.PS2 == NULL) |
| 1571 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1572 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1573 | |
| 1574 | static const char* setup_prompt_string(int promptmode) |
| 1575 | { |
| 1576 | const char *prompt_str; |
| 1577 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1578 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1579 | /* Set up the prompt */ |
| 1580 | if (promptmode == 0) { /* PS1 */ |
| 1581 | free((char*)G.PS1); |
| 1582 | G.PS1 = xasprintf("%s %c ", G.cwd, (geteuid() != 0) ? '$' : '#'); |
| 1583 | prompt_str = G.PS1; |
| 1584 | } else |
| 1585 | prompt_str = G.PS2; |
| 1586 | } else |
| 1587 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1588 | debug_printf("result '%s'\n", prompt_str); |
| 1589 | return prompt_str; |
| 1590 | } |
| 1591 | |
| 1592 | static void get_user_input(struct in_str *i) |
| 1593 | { |
| 1594 | int r; |
| 1595 | const char *prompt_str; |
| 1596 | |
| 1597 | prompt_str = setup_prompt_string(i->promptmode); |
| 1598 | #if ENABLE_FEATURE_EDITING |
| 1599 | /* Enable command line editing only while a command line |
| 1600 | * is actually being read */ |
| 1601 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1602 | G.flag_SIGINT = 0; |
| 1603 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1604 | * only after <Enter>. (^C will work) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1605 | r = read_line_input(prompt_str, G.user_input_buf, BUFSIZ-1, G.line_input_state); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1606 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1607 | check_and_run_traps(0); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1608 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1609 | i->eof_flag = (r < 0); |
| 1610 | if (i->eof_flag) { /* EOF/error detected */ |
| 1611 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1612 | G.user_input_buf[1] = '\0'; |
| 1613 | } |
| 1614 | #else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1615 | do { |
| 1616 | G.flag_SIGINT = 0; |
| 1617 | fputs(prompt_str, stdout); |
| 1618 | fflush(stdout); |
| 1619 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1620 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1621 | //do we need check_and_run_traps(0)? (maybe only if stdin) |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1622 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1623 | i->eof_flag = (r == EOF); |
| 1624 | #endif |
| 1625 | i->p = G.user_input_buf; |
| 1626 | } |
| 1627 | |
| 1628 | #endif /* INTERACTIVE */ |
| 1629 | |
| 1630 | /* This is the magic location that prints prompts |
| 1631 | * and gets data back from the user */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 1632 | static int FAST_FUNC file_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1633 | { |
| 1634 | int ch; |
| 1635 | |
| 1636 | /* If there is data waiting, eat it up */ |
| 1637 | if (i->p && *i->p) { |
| 1638 | #if ENABLE_HUSH_INTERACTIVE |
| 1639 | take_cached: |
| 1640 | #endif |
| 1641 | ch = *i->p++; |
| 1642 | if (i->eof_flag && !*i->p) |
| 1643 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1644 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1645 | } else { |
| 1646 | /* need to double check i->file because we might be doing something |
| 1647 | * more complicated by now, like sourcing or substituting. */ |
| 1648 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 1649 | if (G_interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1650 | do { |
| 1651 | get_user_input(i); |
| 1652 | } while (!*i->p); /* need non-empty line */ |
| 1653 | i->promptmode = 1; /* PS2 */ |
| 1654 | i->promptme = 0; |
| 1655 | goto take_cached; |
| 1656 | } |
| 1657 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1658 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1659 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1660 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1661 | #if ENABLE_HUSH_INTERACTIVE |
| 1662 | if (ch == '\n') |
| 1663 | i->promptme = 1; |
| 1664 | #endif |
| 1665 | return ch; |
| 1666 | } |
| 1667 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1668 | /* All callers guarantee this routine will never |
| 1669 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1670 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 1671 | static int FAST_FUNC file_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1672 | { |
| 1673 | int ch; |
| 1674 | if (i->p && *i->p) { |
| 1675 | if (i->eof_flag && !i->p[1]) |
| 1676 | return EOF; |
| 1677 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1678 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1679 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1680 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1681 | i->eof_flag = (ch == EOF); |
| 1682 | i->peek_buf[0] = ch; |
| 1683 | i->peek_buf[1] = '\0'; |
| 1684 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1685 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1686 | return ch; |
| 1687 | } |
| 1688 | |
| 1689 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1690 | { |
| 1691 | i->peek = file_peek; |
| 1692 | i->get = file_get; |
| 1693 | #if ENABLE_HUSH_INTERACTIVE |
| 1694 | i->promptme = 1; |
| 1695 | i->promptmode = 0; /* PS1 */ |
| 1696 | #endif |
| 1697 | i->file = f; |
| 1698 | i->p = NULL; |
| 1699 | } |
| 1700 | |
| 1701 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1702 | { |
| 1703 | i->peek = static_peek; |
| 1704 | i->get = static_get; |
| 1705 | #if ENABLE_HUSH_INTERACTIVE |
| 1706 | i->promptme = 1; |
| 1707 | i->promptmode = 0; /* PS1 */ |
| 1708 | #endif |
| 1709 | i->p = s; |
| 1710 | i->eof_flag = 0; |
| 1711 | } |
| 1712 | |
| 1713 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1714 | /* |
| 1715 | * o_string support |
| 1716 | */ |
| 1717 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1718 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1719 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1720 | { |
| 1721 | o->length = 0; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 1722 | o->o_quoted = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1723 | if (o->data) |
| 1724 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1727 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1728 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1729 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1730 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1733 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 1734 | { |
| 1735 | free(o->data); |
| 1736 | } |
| 1737 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1738 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1739 | { |
| 1740 | if (o->length + len > o->maxlen) { |
| 1741 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 1742 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 1743 | } |
| 1744 | } |
| 1745 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1746 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1747 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1748 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1749 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1750 | o->data[o->length] = ch; |
| 1751 | o->length++; |
| 1752 | o->data[o->length] = '\0'; |
| 1753 | } |
| 1754 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1755 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1756 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1757 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1758 | memcpy(&o->data[o->length], str, len); |
| 1759 | o->length += len; |
| 1760 | o->data[o->length] = '\0'; |
| 1761 | } |
| 1762 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1763 | #if !BB_MMU |
| 1764 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1765 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1766 | o_addblock(o, str, strlen(str)); |
| 1767 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1768 | static void nommu_addchr(o_string *o, int ch) |
| 1769 | { |
| 1770 | if (o) |
| 1771 | o_addchr(o, ch); |
| 1772 | } |
| 1773 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1774 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1775 | #endif |
| 1776 | |
| 1777 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 1778 | { |
| 1779 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1782 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1783 | { |
| 1784 | while (len) { |
| 1785 | o_addchr(o, *str); |
| 1786 | if (*str++ == '\\' |
| 1787 | && (*str != '*' && *str != '?' && *str != '[') |
| 1788 | ) { |
| 1789 | o_addchr(o, '\\'); |
| 1790 | } |
| 1791 | len--; |
| 1792 | } |
| 1793 | } |
| 1794 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1795 | /* My analysis of quoting semantics tells me that state information |
| 1796 | * is associated with a destination, not a source. |
| 1797 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1798 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1799 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1800 | int sz = 1; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1801 | char *found = strchr("*?[\\", ch); |
| 1802 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1803 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1804 | o_grow_by(o, sz); |
| 1805 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1806 | o->data[o->length] = '\\'; |
| 1807 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1808 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1809 | o->data[o->length] = ch; |
| 1810 | o->length++; |
| 1811 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1814 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1815 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1816 | int sz = 1; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1817 | if (o->o_escape && strchr("*?[\\", ch)) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1818 | sz++; |
| 1819 | o->data[o->length] = '\\'; |
| 1820 | o->length++; |
| 1821 | } |
| 1822 | o_grow_by(o, sz); |
| 1823 | o->data[o->length] = ch; |
| 1824 | o->length++; |
| 1825 | o->data[o->length] = '\0'; |
| 1826 | } |
| 1827 | |
| 1828 | static void o_addQstr(o_string *o, const char *str, int len) |
| 1829 | { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1830 | if (!o->o_escape) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1831 | o_addblock(o, str, len); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1832 | return; |
| 1833 | } |
| 1834 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1835 | char ch; |
| 1836 | int sz; |
| 1837 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 1838 | if (ordinary_cnt > len) /* paranoia */ |
| 1839 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1840 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1841 | if (ordinary_cnt == len) |
| 1842 | return; |
| 1843 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1844 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1845 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1846 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1847 | sz = 1; |
| 1848 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 1849 | sz++; |
| 1850 | o->data[o->length] = '\\'; |
| 1851 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1852 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1853 | o_grow_by(o, sz); |
| 1854 | o->data[o->length] = ch; |
| 1855 | o->length++; |
| 1856 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1857 | } |
| 1858 | } |
| 1859 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1860 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1861 | * It contains char* list[] at the beginning, which is grown in 16 element |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1862 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1863 | * list[i] contains an INDEX (int!) into this string data. |
| 1864 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1865 | * but list[i]'s need not be modified. |
| 1866 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1867 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1868 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1869 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1870 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1871 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1872 | { |
| 1873 | char **list = (char**)o->data; |
| 1874 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1875 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1876 | |
| 1877 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1878 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1879 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1880 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1881 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1882 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1883 | o->data + (int)list[i] + string_start, |
| 1884 | o->data + (int)list[i] + string_start); |
| 1885 | i++; |
| 1886 | } |
| 1887 | if (n) { |
| 1888 | const char *p = o->data + (int)list[n - 1] + string_start; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1889 | indent(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1890 | fprintf(stderr, " total_sz:%ld\n", (long)((p + strlen(p) + 1) - o->data)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1891 | } |
| 1892 | } |
| 1893 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1894 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1895 | #endif |
| 1896 | |
| 1897 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1898 | * in list[n] so that it points past last stored byte so far. |
| 1899 | * It returns n+1. */ |
| 1900 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1901 | { |
| 1902 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1903 | int string_start; |
| 1904 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1905 | |
| 1906 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1907 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1908 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1909 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1910 | debug_printf_list("list[%d]=%d string_start=%d (growing)\n", n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1911 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1912 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1913 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1914 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1915 | memmove(list + n + 0x10, list + n, string_len); |
| 1916 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1917 | } else { |
| 1918 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 1919 | n, string_len, string_start); |
| 1920 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1921 | } else { |
| 1922 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1923 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1924 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1925 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 1926 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1927 | o->has_empty_slot = 0; |
| 1928 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1929 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1930 | return n + 1; |
| 1931 | } |
| 1932 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1933 | /* "What was our last o_save_ptr'ed position (byte offset relative o->data)?" */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1934 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1935 | { |
| 1936 | char **list = (char**)o->data; |
| 1937 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1938 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1939 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1942 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1943 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1944 | static int o_glob(o_string *o, int n) |
| 1945 | { |
| 1946 | glob_t globdata; |
| 1947 | int gr; |
| 1948 | char *pattern; |
| 1949 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1950 | debug_printf_glob("start o_glob: n:%d o->data:%p\n", n, o->data); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1951 | if (!o->data) |
| 1952 | return o_save_ptr_helper(o, n); |
| 1953 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1954 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1955 | if (!glob_needed(pattern)) { |
| 1956 | literal: |
| 1957 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1958 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1959 | return o_save_ptr_helper(o, n); |
| 1960 | } |
| 1961 | |
| 1962 | memset(&globdata, 0, sizeof(globdata)); |
| 1963 | gr = glob(pattern, 0, NULL, &globdata); |
| 1964 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 1965 | if (gr == GLOB_NOSPACE) |
| 1966 | bb_error_msg_and_die("out of memory during glob"); |
| 1967 | if (gr == GLOB_NOMATCH) { |
| 1968 | globfree(&globdata); |
| 1969 | goto literal; |
| 1970 | } |
| 1971 | if (gr != 0) { /* GLOB_ABORTED ? */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 1972 | /* TODO: testcase for bad glob pattern behavior */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1973 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 1974 | } |
| 1975 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 1976 | char **argv = globdata.gl_pathv; |
| 1977 | o->length = pattern - o->data; /* "forget" pattern */ |
| 1978 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1979 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1980 | n = o_save_ptr_helper(o, n); |
| 1981 | argv++; |
| 1982 | if (!*argv) |
| 1983 | break; |
| 1984 | } |
| 1985 | } |
| 1986 | globfree(&globdata); |
| 1987 | if (DEBUG_GLOB) |
| 1988 | debug_print_list("o_glob returning", o, n); |
| 1989 | return n; |
| 1990 | } |
| 1991 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1992 | /* If o->o_glob == 1, glob the string so far remembered. |
| 1993 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1994 | static int o_save_ptr(o_string *o, int n) |
| 1995 | { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 1996 | if (o->o_glob) { /* if globbing is requested */ |
| 1997 | /* If o->has_empty_slot, list[n] was already globbed |
| 1998 | * (if it was requested back then when it was filled) |
| 1999 | * so don't do that again! */ |
| 2000 | if (!o->has_empty_slot) |
| 2001 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 2002 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2003 | return o_save_ptr_helper(o, n); |
| 2004 | } |
| 2005 | |
| 2006 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2007 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2008 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2009 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2010 | int string_start; |
| 2011 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2012 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 2013 | if (DEBUG_EXPAND) |
| 2014 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2015 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2016 | list = (char**)o->data; |
| 2017 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2018 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2019 | while (n) { |
| 2020 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 2021 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2022 | } |
| 2023 | return list; |
| 2024 | } |
| 2025 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2026 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2027 | /* Expansion can recurse */ |
| 2028 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2029 | static int process_command_subs(o_string *dest, const char *s); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2030 | #endif |
| 2031 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2032 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2033 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2034 | parse_stream_dquoted(dest, input, dquote_end) |
| 2035 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2036 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2037 | o_string *dest, |
| 2038 | struct in_str *input, |
| 2039 | int dquote_end); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2040 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2041 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 2042 | * all variable references within and returns a pointer to |
| 2043 | * a list of expanded strings, possibly with larger number |
| 2044 | * of strings. (Think VAR="a b"; echo $VAR). |
| 2045 | * This new list is allocated as a single malloc block. |
| 2046 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 2047 | * followed by strings themself. |
| 2048 | * Caller can deallocate entire list by single free(list). */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2049 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2050 | /* Store given string, finalizing the word and starting new one whenever |
| 2051 | * we encounter IFS char(s). This is used for expanding variable values. |
| 2052 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 2053 | static int expand_on_ifs(o_string *output, int n, const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2054 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2055 | while (1) { |
| 2056 | int word_len = strcspn(str, G.ifs); |
| 2057 | if (word_len) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2058 | if (output->o_escape || !output->o_glob) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2059 | o_addQstr(output, str, word_len); |
| 2060 | else /* protect backslashes against globbing up :) */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2061 | o_addblock_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2062 | str += word_len; |
| 2063 | } |
| 2064 | if (!*str) /* EOL - do not finalize word */ |
| 2065 | break; |
| 2066 | o_addchr(output, '\0'); |
| 2067 | debug_print_list("expand_on_ifs", output, n); |
| 2068 | n = o_save_ptr(output, n); |
| 2069 | str += strspn(str, G.ifs); /* skip ifs chars */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2070 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2071 | debug_print_list("expand_on_ifs[1]", output, n); |
| 2072 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2075 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 2076 | * they are in double quotes, with the exception that they are not :). |
| 2077 | * Just the rules are similar: "expand only $var and `cmd`" |
| 2078 | * |
| 2079 | * Returns malloced string. |
| 2080 | * As an optimization, we return NULL if expansion is not needed. |
| 2081 | */ |
| 2082 | static char *expand_pseudo_dquoted(const char *str) |
| 2083 | { |
| 2084 | char *exp_str; |
| 2085 | struct in_str input; |
| 2086 | o_string dest = NULL_O_STRING; |
| 2087 | |
| 2088 | if (strchr(str, '$') == NULL |
| 2089 | #if ENABLE_HUSH_TICK |
| 2090 | && strchr(str, '`') == NULL |
| 2091 | #endif |
| 2092 | ) { |
| 2093 | return NULL; |
| 2094 | } |
| 2095 | |
| 2096 | /* We need to expand. Example: |
| 2097 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 2098 | */ |
| 2099 | setup_string_in_str(&input, str); |
| 2100 | parse_stream_dquoted(NULL, &dest, &input, EOF); |
| 2101 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
| 2102 | exp_str = expand_string_to_string(dest.data); |
| 2103 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 2104 | o_free_unsafe(&dest); |
| 2105 | return exp_str; |
| 2106 | } |
| 2107 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2108 | /* Expand all variable references in given string, adding words to list[] |
| 2109 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 2110 | * to be filled). This routine is extremely tricky: has to deal with |
| 2111 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 2112 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
| 2113 | static int expand_vars_to_list(o_string *output, int n, char *arg, char or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2114 | { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2115 | /* or_mask is either 0 (normal case) or 0x80 - |
| 2116 | * expansion of right-hand side of assignment == 1-element expand. |
| 2117 | * It will also do no globbing, and thus we must not backslash-quote! |
| 2118 | */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2119 | char ored_ch; |
| 2120 | char *p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2121 | |
| 2122 | ored_ch = 0; |
| 2123 | |
| 2124 | debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg); |
| 2125 | debug_print_list("expand_vars_to_list", output, n); |
| 2126 | n = o_save_ptr(output, n); |
| 2127 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 2128 | |
| 2129 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2130 | char first_ch; |
| 2131 | int i; |
| 2132 | char *dyn_val = NULL; |
| 2133 | const char *val = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2134 | #if ENABLE_HUSH_TICK |
| 2135 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2136 | #endif |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2137 | #if ENABLE_SH_MATH_SUPPORT |
| 2138 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 2139 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2140 | o_addblock(output, arg, p - arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2141 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 2142 | arg = ++p; |
| 2143 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2144 | |
| 2145 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
| 2146 | /* "$@" is special. Even if quoted, it can still |
| 2147 | * expand to nothing (not even an empty string) */ |
| 2148 | if ((first_ch & 0x7f) != '@') |
| 2149 | ored_ch |= first_ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2150 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2151 | switch (first_ch & 0x7f) { |
| 2152 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 2153 | case '$': /* pid */ |
| 2154 | val = utoa(G.root_pid); |
| 2155 | break; |
| 2156 | case '!': /* bg pid */ |
| 2157 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
| 2158 | break; |
| 2159 | case '?': /* exitcode */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 2160 | val = utoa(G.last_exitcode); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2161 | break; |
| 2162 | case '#': /* argc */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2163 | if (arg[1] != SPECIAL_VAR_SYMBOL) |
| 2164 | /* actually, it's a ${#var} */ |
| 2165 | goto case_default; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2166 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 2167 | break; |
| 2168 | case '*': |
| 2169 | case '@': |
| 2170 | i = 1; |
| 2171 | if (!G.global_argv[i]) |
| 2172 | break; |
| 2173 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
| 2174 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2175 | smallint sv = output->o_escape; |
| 2176 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2177 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2178 | while (G.global_argv[i]) { |
| 2179 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 2180 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 2181 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 2182 | /* this argv[] is not empty and not last: |
| 2183 | * put terminating NUL, start new word */ |
| 2184 | o_addchr(output, '\0'); |
| 2185 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 2186 | n = o_save_ptr(output, n); |
| 2187 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 2188 | } |
| 2189 | } |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2190 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2191 | } else |
| 2192 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
| 2193 | * and in this case should treat it like '$*' - see 'else...' below */ |
| 2194 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
| 2195 | while (1) { |
| 2196 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2197 | if (++i >= G.global_argc) |
| 2198 | break; |
| 2199 | o_addchr(output, '\0'); |
| 2200 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 2201 | n = o_save_ptr(output, n); |
| 2202 | } |
| 2203 | } else { /* quoted $*: add as one word */ |
| 2204 | while (1) { |
| 2205 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2206 | if (!G.global_argv[++i]) |
| 2207 | break; |
| 2208 | if (G.ifs[0]) |
| 2209 | o_addchr(output, G.ifs[0]); |
| 2210 | } |
| 2211 | } |
| 2212 | break; |
| 2213 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 2214 | /* "Empty variable", used to make "" etc to not disappear */ |
| 2215 | arg++; |
| 2216 | ored_ch = 0x80; |
| 2217 | break; |
| 2218 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2219 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2220 | *p = '\0'; |
| 2221 | arg++; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2222 | /* Can't just stuff it into output o_string, |
| 2223 | * expanded result may need to be globbed |
| 2224 | * and $IFS-splitted */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2225 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2226 | process_command_subs(&subst_result, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2227 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
| 2228 | val = subst_result.data; |
| 2229 | goto store_val; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2230 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2231 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2232 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2233 | arith_eval_hooks_t hooks; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2234 | arith_t res; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2235 | int errcode; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2236 | char *exp_str; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2237 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2238 | arg++; /* skip '+' */ |
| 2239 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2240 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2241 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2242 | exp_str = expand_pseudo_dquoted(arg); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2243 | hooks.lookupvar = get_local_var_value; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2244 | hooks.setvar = arith_set_local_var; |
| 2245 | hooks.endofname = endofname; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2246 | res = arith(exp_str ? exp_str : arg, &errcode, &hooks); |
| 2247 | free(exp_str); |
| 2248 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2249 | if (errcode < 0) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2250 | const char *msg = "error in arithmetic"; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2251 | switch (errcode) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2252 | case -3: |
| 2253 | msg = "exponent less than 0"; |
| 2254 | break; |
| 2255 | case -2: |
| 2256 | msg = "divide by 0"; |
| 2257 | break; |
| 2258 | case -5: |
| 2259 | msg = "expression recursion loop detected"; |
| 2260 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2261 | } |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2262 | die_if_script(msg); |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2263 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2264 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2265 | sprintf(arith_buf, arith_t_fmt, res); |
| 2266 | val = arith_buf; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2267 | break; |
| 2268 | } |
| 2269 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2270 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2271 | case_default: { |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2272 | bool exp_len = false; |
| 2273 | bool exp_null = false; |
| 2274 | char *var = arg; |
| 2275 | char exp_save = exp_save; /* for compiler */ |
| 2276 | char exp_op = exp_op; /* for compiler */ |
| 2277 | char *exp_word = exp_word; /* for compiler */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2278 | size_t exp_off = 0; |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2279 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2280 | *p = '\0'; |
| 2281 | arg[0] = first_ch & 0x7f; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2282 | |
| 2283 | /* prepare for expansions */ |
| 2284 | if (var[0] == '#') { |
| 2285 | /* handle length expansion ${#var} */ |
| 2286 | exp_len = true; |
| 2287 | ++var; |
| 2288 | } else { |
| 2289 | /* maybe handle parameter expansion */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2290 | exp_off = strcspn(var, ":-=+?%#"); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2291 | if (!var[exp_off]) |
| 2292 | exp_off = 0; |
| 2293 | if (exp_off) { |
| 2294 | exp_save = var[exp_off]; |
| 2295 | exp_null = exp_save == ':'; |
| 2296 | exp_word = var + exp_off; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2297 | if (exp_null) |
| 2298 | ++exp_word; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2299 | exp_op = *exp_word++; |
| 2300 | var[exp_off] = '\0'; |
| 2301 | } |
| 2302 | } |
| 2303 | |
| 2304 | /* lookup the variable in question */ |
| 2305 | if (isdigit(var[0])) { |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 2306 | /* handle_dollar() should have vetted var for us */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2307 | i = xatoi_u(var); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2308 | if (i < G.global_argc) |
| 2309 | val = G.global_argv[i]; |
| 2310 | /* else val remains NULL: $N with too big N */ |
| 2311 | } else |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 2312 | val = get_local_var_value(var); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2313 | |
| 2314 | /* handle any expansions */ |
| 2315 | if (exp_len) { |
| 2316 | debug_printf_expand("expand: length of '%s' = ", val); |
| 2317 | val = utoa(val ? strlen(val) : 0); |
| 2318 | debug_printf_expand("%s\n", val); |
| 2319 | } else if (exp_off) { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2320 | if (exp_op == '%' || exp_op == '#') { |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2321 | if (val) { |
| 2322 | /* we need to do a pattern match */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2323 | bool match_at_left; |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2324 | char *loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2325 | scan_t scan = pick_scan(exp_op, *exp_word, &match_at_left); |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2326 | if (exp_op == *exp_word) /* ## or %% */ |
| 2327 | ++exp_word; |
| 2328 | val = dyn_val = xstrdup(val); |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2329 | loc = scan(dyn_val, exp_word, match_at_left); |
| 2330 | if (match_at_left) /* # or ## */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2331 | val = loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2332 | else if (loc) /* % or %% and match was found */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2333 | *loc = '\0'; |
| 2334 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2335 | } else { |
| 2336 | /* we need to do an expansion */ |
| 2337 | int exp_test = (!val || (exp_null && !val[0])); |
| 2338 | if (exp_op == '+') |
| 2339 | exp_test = !exp_test; |
| 2340 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 2341 | exp_null ? "true" : "false", exp_test); |
| 2342 | if (exp_test) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2343 | if (exp_op == '?') { |
| 2344 | //TODO: how interactive bash aborts expansion mid-command? |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2345 | /* ${var?[error_msg_if_unset]} */ |
| 2346 | /* ${var:?[error_msg_if_unset_or_null]} */ |
| 2347 | /* mimic bash message */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2348 | die_if_script("%s: %s", |
| 2349 | var, |
| 2350 | exp_word[0] ? exp_word : "parameter null or not set" |
| 2351 | ); |
| 2352 | } else { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2353 | val = exp_word; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2354 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2355 | |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2356 | if (exp_op == '=') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2357 | /* ${var=[word]} or ${var:=[word]} */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2358 | if (isdigit(var[0]) || var[0] == '#') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2359 | /* mimic bash message */ |
| 2360 | die_if_script("$%s: cannot assign in this way", var); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2361 | val = NULL; |
| 2362 | } else { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2363 | char *new_var = xasprintf("%s=%s", var, val); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2364 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2365 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2366 | } |
| 2367 | } |
| 2368 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2369 | |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2370 | var[exp_off] = exp_save; |
| 2371 | } |
| 2372 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2373 | arg[0] = first_ch; |
| 2374 | #if ENABLE_HUSH_TICK |
| 2375 | store_val: |
| 2376 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2377 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2378 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, output->o_escape); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2379 | if (val) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2380 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2381 | smallint sv = output->o_escape; |
| 2382 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2383 | n = expand_on_ifs(output, n, val); |
| 2384 | val = NULL; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2385 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2386 | } |
| 2387 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2388 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, output->o_escape); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2389 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2390 | } /* default: */ |
| 2391 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2392 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2393 | if (val) { |
| 2394 | o_addQstr(output, val, strlen(val)); |
| 2395 | } |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2396 | free(dyn_val); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2397 | /* Do the check to avoid writing to a const string */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2398 | if (*p != SPECIAL_VAR_SYMBOL) |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2399 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2400 | |
| 2401 | #if ENABLE_HUSH_TICK |
| 2402 | o_free(&subst_result); |
| 2403 | #endif |
| 2404 | arg = ++p; |
| 2405 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 2406 | |
| 2407 | if (arg[0]) { |
| 2408 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 2409 | /* this part is literal, and it was already pre-quoted |
| 2410 | * if needed (much earlier), do not use o_addQstr here! */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2411 | o_addstr_with_NUL(output, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2412 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 2413 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 2414 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 2415 | ) { |
| 2416 | n--; |
| 2417 | /* allow to reuse list[n] later without re-growth */ |
| 2418 | output->has_empty_slot = 1; |
| 2419 | } else { |
| 2420 | o_addchr(output, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2421 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2422 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2425 | static char **expand_variables(char **argv, int or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2426 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2427 | int n; |
| 2428 | char **list; |
| 2429 | char **v; |
| 2430 | o_string output = NULL_O_STRING; |
| 2431 | |
| 2432 | if (or_mask & 0x100) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2433 | output.o_escape = 1; /* protect against globbing for "$var" */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2434 | /* (unquoted $var will temporarily switch it off) */ |
| 2435 | output.o_glob = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2436 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2437 | |
| 2438 | n = 0; |
| 2439 | v = argv; |
| 2440 | while (*v) { |
| 2441 | n = expand_vars_to_list(&output, n, *v, (char)or_mask); |
| 2442 | v++; |
| 2443 | } |
| 2444 | debug_print_list("expand_variables", &output, n); |
| 2445 | |
| 2446 | /* output.data (malloced in one block) gets returned in "list" */ |
| 2447 | list = o_finalize_list(&output, n); |
| 2448 | debug_print_strings("expand_variables[1]", list); |
| 2449 | return list; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2452 | static char **expand_strvec_to_strvec(char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2453 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2454 | return expand_variables(argv, 0x100); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2457 | /* Used for expansion of right hand of assignments */ |
| 2458 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 2459 | * "v=/bin/c*" */ |
| 2460 | static char *expand_string_to_string(const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2461 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2462 | char *argv[2], **list; |
| 2463 | |
| 2464 | argv[0] = (char*)str; |
| 2465 | argv[1] = NULL; |
| 2466 | list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */ |
| 2467 | if (HUSH_DEBUG) |
| 2468 | if (!list[0] || list[1]) |
| 2469 | bb_error_msg_and_die("BUG in varexp2"); |
| 2470 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 2471 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2472 | unbackslash((char*)list); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2473 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 2474 | return (char*)list; |
| 2475 | } |
| 2476 | |
| 2477 | /* Used for "eval" builtin */ |
| 2478 | static char* expand_strvec_to_string(char **argv) |
| 2479 | { |
| 2480 | char **list; |
| 2481 | |
| 2482 | list = expand_variables(argv, 0x80); |
| 2483 | /* Convert all NULs to spaces */ |
| 2484 | if (list[0]) { |
| 2485 | int n = 1; |
| 2486 | while (list[n]) { |
| 2487 | if (HUSH_DEBUG) |
| 2488 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 2489 | bb_error_msg_and_die("BUG in varexp3"); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 2490 | /* bash uses ' ' regardless of $IFS contents */ |
| 2491 | list[n][-1] = ' '; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2492 | n++; |
| 2493 | } |
| 2494 | } |
| 2495 | overlapping_strcpy((char*)list, list[0]); |
| 2496 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 2497 | return (char*)list; |
| 2498 | } |
| 2499 | |
| 2500 | static char **expand_assignments(char **argv, int count) |
| 2501 | { |
| 2502 | int i; |
| 2503 | char **p = NULL; |
| 2504 | /* Expand assignments into one string each */ |
| 2505 | for (i = 0; i < count; i++) { |
| 2506 | p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 2507 | } |
| 2508 | return p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2511 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2512 | #if BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2513 | /* never called */ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2514 | void re_execute_shell(char ***to_free, const char *s, char *argv0, char **argv); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2515 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2516 | static void reset_traps_to_defaults(void) |
| 2517 | { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2518 | /* This function is always called in a child shell |
| 2519 | * after fork (not vfork, NOMMU doesn't use this function). |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2520 | * Child shells are not interactive. |
| 2521 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 2522 | * Testcase: (while :; do :; done) + ^Z should background. |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2523 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2524 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2525 | unsigned sig; |
| 2526 | unsigned mask; |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2527 | |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2528 | if (!G.traps && !(G.non_DFL_mask & SPECIAL_INTERACTIVE_SIGS)) |
| 2529 | return; |
| 2530 | |
| 2531 | /* Stupid. It can be done with *single* &= op, but we can't use |
| 2532 | * the fact that G.blocked_set is implemented as a bitmask... */ |
| 2533 | mask = (SPECIAL_INTERACTIVE_SIGS >> 1); |
| 2534 | sig = 1; |
| 2535 | while (1) { |
| 2536 | if (mask & 1) |
| 2537 | sigdelset(&G.blocked_set, sig); |
| 2538 | mask >>= 1; |
| 2539 | if (!mask) |
| 2540 | break; |
| 2541 | sig++; |
| 2542 | } |
| 2543 | |
| 2544 | G.non_DFL_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
| 2545 | mask = G.non_DFL_mask; |
| 2546 | if (G.traps) for (sig = 0; sig < NSIG; sig++, mask >>= 1) { |
| 2547 | if (!G.traps[sig]) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2548 | continue; |
| 2549 | free(G.traps[sig]); |
| 2550 | G.traps[sig] = NULL; |
| 2551 | /* There is no signal for 0 (EXIT) */ |
| 2552 | if (sig == 0) |
| 2553 | continue; |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 2554 | /* There was a trap handler, we are removing it. |
| 2555 | * But if sig still has non-DFL handling, |
| 2556 | * we should not unblock it. */ |
| 2557 | if (mask & 1) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2558 | continue; |
| 2559 | sigdelset(&G.blocked_set, sig); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2560 | } |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 2561 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
| 2564 | #else /* !BB_MMU */ |
| 2565 | |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2566 | static void re_execute_shell(char ***to_free, const char *s, char *g_argv0, char **g_argv) NORETURN; |
| 2567 | static void re_execute_shell(char ***to_free, const char *s, char *g_argv0, char **g_argv) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2568 | { |
| 2569 | char param_buf[sizeof("-$%x:%x:%x:%x") + sizeof(unsigned) * 4]; |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2570 | char *heredoc_argv[4]; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2571 | struct variable *cur; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2572 | #if ENABLE_HUSH_FUNCTIONS |
| 2573 | struct function *funcp; |
| 2574 | #endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2575 | char **argv, **pp; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2576 | unsigned cnt; |
| 2577 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2578 | if (!g_argv0) { /* heredoc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2579 | argv = heredoc_argv; |
| 2580 | argv[0] = (char *) G.argv0_for_re_execing; |
| 2581 | argv[1] = (char *) "-<"; |
| 2582 | argv[2] = (char *) s; |
| 2583 | argv[3] = NULL; |
Denis Vlasenko | f50caac | 2009-04-09 01:40:15 +0000 | [diff] [blame] | 2584 | pp = &argv[3]; /* used as pointer to empty environment */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2585 | goto do_exec; |
| 2586 | } |
| 2587 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 2588 | sprintf(param_buf, "-$%x:%x:%x" IF_HUSH_LOOPS(":%x") |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2589 | , (unsigned) G.root_pid |
| 2590 | , (unsigned) G.last_bg_pid |
| 2591 | , (unsigned) G.last_exitcode |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 2592 | IF_HUSH_LOOPS(, G.depth_of_loop) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2593 | ); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2594 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<depth> <vars...> <funcs...> |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2595 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2596 | */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2597 | cnt = 6; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2598 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2599 | if (!cur->flg_export || cur->flg_read_only) |
| 2600 | cnt += 2; |
| 2601 | } |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2602 | #if ENABLE_HUSH_FUNCTIONS |
| 2603 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 2604 | cnt += 3; |
| 2605 | #endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2606 | pp = g_argv; |
| 2607 | while (*pp++) |
| 2608 | cnt++; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2609 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2610 | *pp++ = (char *) G.argv0_for_re_execing; |
| 2611 | *pp++ = param_buf; |
| 2612 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2613 | if (cur->varstr == hush_version_str) |
| 2614 | continue; |
| 2615 | if (cur->flg_read_only) { |
| 2616 | *pp++ = (char *) "-R"; |
| 2617 | *pp++ = cur->varstr; |
| 2618 | } else if (!cur->flg_export) { |
| 2619 | *pp++ = (char *) "-V"; |
| 2620 | *pp++ = cur->varstr; |
| 2621 | } |
| 2622 | } |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2623 | #if ENABLE_HUSH_FUNCTIONS |
| 2624 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 2625 | *pp++ = (char *) "-F"; |
| 2626 | *pp++ = funcp->name; |
| 2627 | *pp++ = funcp->body_as_string; |
| 2628 | } |
| 2629 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2630 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 2631 | * |
| 2632 | * However, POSIX says that subshells reset signals with traps |
| 2633 | * to SIG_DFL. |
| 2634 | * I tested bash-3.2 and it not only does that with true subshells |
| 2635 | * of the form ( list ), but with any forked children shells. |
| 2636 | * I set trap "echo W" WINCH; and then tried: |
| 2637 | * |
| 2638 | * { echo 1; sleep 20; echo 2; } & |
| 2639 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 2640 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 2641 | * |
| 2642 | * In all these cases sending SIGWINCH to the child shell |
| 2643 | * did not run the trap. If I add trap "echo V" WINCH; |
| 2644 | * _inside_ group (just before echo 1), it works. |
| 2645 | * |
| 2646 | * I conclude it means we don't need to pass active traps here. |
| 2647 | * exec syscall below resets them to SIG_DFL for us. |
| 2648 | */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2649 | *pp++ = (char *) "-c"; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2650 | *pp++ = (char *) s; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2651 | *pp++ = g_argv0; |
| 2652 | while (*g_argv) |
| 2653 | *pp++ = *g_argv++; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2654 | /* *pp = NULL; - is already there */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2655 | pp = environ; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2656 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2657 | do_exec: |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2658 | debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s); |
| 2659 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2660 | execve(bb_busybox_exec_path, argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2661 | /* Fallback. Useful for init=/bin/hush usage etc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2662 | if (argv[0][0] == '/') |
| 2663 | execve(argv[0], argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2664 | xfunc_error_retval = 127; |
| 2665 | bb_error_msg_and_die("can't re-execute the shell"); |
| 2666 | } |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2667 | #endif /* !BB_MMU */ |
| 2668 | |
| 2669 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2670 | static void setup_heredoc(struct redir_struct *redir) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2671 | { |
| 2672 | struct fd_pair pair; |
| 2673 | pid_t pid; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2674 | int len, written; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2675 | /* the _body_ of heredoc (misleading field name) */ |
| 2676 | const char *heredoc = redir->rd_filename; |
| 2677 | char *expanded; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2678 | #if !BB_MMU |
| 2679 | char **to_free; |
| 2680 | #endif |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2681 | |
| 2682 | expanded = NULL; |
| 2683 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
| 2684 | expanded = expand_pseudo_dquoted(heredoc); |
| 2685 | if (expanded) |
| 2686 | heredoc = expanded; |
| 2687 | } |
| 2688 | len = strlen(heredoc); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2689 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 2690 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2691 | xpiped_pair(pair); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2692 | xmove_fd(pair.rd, redir->rd_fd); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2693 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2694 | /* Try writing without forking. Newer kernels have |
| 2695 | * dynamically growing pipes. Must use non-blocking write! */ |
| 2696 | ndelay_on(pair.wr); |
| 2697 | while (1) { |
| 2698 | written = write(pair.wr, heredoc, len); |
| 2699 | if (written <= 0) |
| 2700 | break; |
| 2701 | len -= written; |
| 2702 | if (len == 0) { |
| 2703 | close(pair.wr); |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 2704 | free(expanded); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2705 | return; |
| 2706 | } |
| 2707 | heredoc += written; |
| 2708 | } |
| 2709 | ndelay_off(pair.wr); |
| 2710 | |
| 2711 | /* Okay, pipe buffer was not big enough */ |
| 2712 | /* Note: we must not create a stray child (bastard? :) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2713 | * for the unsuspecting parent process. Child creates a grandchild |
| 2714 | * and exits before parent execs the process which consumes heredoc |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2715 | * (that exec happens after we return from this function) */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 2716 | #if !BB_MMU |
| 2717 | to_free = NULL; |
| 2718 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2719 | pid = vfork(); |
| 2720 | if (pid < 0) |
| 2721 | bb_perror_msg_and_die("vfork"); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2722 | if (pid == 0) { |
| 2723 | /* child */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 2724 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2725 | pid = BB_MMU ? fork() : vfork(); |
| 2726 | if (pid < 0) |
| 2727 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
| 2728 | if (pid != 0) |
| 2729 | _exit(0); |
| 2730 | /* grandchild */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2731 | close(redir->rd_fd); /* read side of the pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2732 | #if BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2733 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2734 | _exit(0); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2735 | #else |
| 2736 | /* Delegate blocking writes to another process */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2737 | xmove_fd(pair.wr, STDOUT_FILENO); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2738 | re_execute_shell(&to_free, heredoc, NULL, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 2739 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2740 | } |
| 2741 | /* parent */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 2742 | #if ENABLE_HUSH_FAST |
| 2743 | G.count_SIGCHLD++; |
| 2744 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 2745 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2746 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 2747 | #if !BB_MMU |
| 2748 | free(to_free); |
| 2749 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2750 | close(pair.wr); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2751 | free(expanded); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2752 | wait(NULL); /* wait till child has died */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2755 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 2756 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2757 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2758 | { |
| 2759 | int openfd, mode; |
| 2760 | struct redir_struct *redir; |
| 2761 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2762 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2763 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2764 | /* rd_fd<<HERE case */ |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 2765 | if (squirrel && redir->rd_fd < 3 |
| 2766 | && squirrel[redir->rd_fd] < 0 |
| 2767 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2768 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2769 | } |
| 2770 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 2771 | * of the heredoc */ |
| 2772 | debug_printf_parse("set heredoc '%s'\n", |
| 2773 | redir->rd_filename); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2774 | setup_heredoc(redir); |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2775 | continue; |
| 2776 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2777 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2778 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
| 2779 | /* rd_fd<*>file case (<*> is <,>,>>,<>) */ |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2780 | char *p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2781 | if (redir->rd_filename == NULL) { |
| 2782 | /* Something went wrong in the parse. |
| 2783 | * Pretend it didn't happen */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2784 | bb_error_msg("bug in redirect parse"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2785 | continue; |
| 2786 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2787 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2788 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2789 | openfd = open_or_warn(p, mode); |
| 2790 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2791 | if (openfd < 0) { |
| 2792 | /* this could get lost if stderr has been redirected, but |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2793 | * bash and ash both lose it as well (though zsh doesn't!) */ |
| 2794 | //what the above comment tries to say? |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2795 | return 1; |
| 2796 | } |
| 2797 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2798 | /* rd_fd<*>rd_dup or rd_fd<*>- cases */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2799 | openfd = redir->rd_dup; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2802 | if (openfd != redir->rd_fd) { |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 2803 | if (squirrel && redir->rd_fd < 3 |
| 2804 | && squirrel[redir->rd_fd] < 0 |
| 2805 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2806 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2807 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2808 | if (openfd == REDIRFD_CLOSE) { |
| 2809 | /* "n>-" means "close me" */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2810 | close(redir->rd_fd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2811 | } else { |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2812 | xdup2(openfd, redir->rd_fd); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2813 | if (redir->rd_dup == REDIRFD_TO_FILE) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2814 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2815 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2816 | } |
| 2817 | } |
| 2818 | return 0; |
| 2819 | } |
| 2820 | |
| 2821 | static void restore_redirects(int squirrel[]) |
| 2822 | { |
| 2823 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2824 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2825 | fd = squirrel[i]; |
| 2826 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2827 | /* We simply die on error */ |
| 2828 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2829 | } |
| 2830 | } |
| 2831 | } |
| 2832 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2833 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2834 | static void free_pipe_list(struct pipe *head); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2835 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2836 | /* Return code is the exit status of the pipe */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2837 | static void free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2838 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2839 | char **p; |
| 2840 | struct command *command; |
| 2841 | struct redir_struct *r, *rnext; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2842 | int a, i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2843 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2844 | if (pi->stopped_cmds > 0) /* why? */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2845 | return; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2846 | debug_printf_clean("run pipe: (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2847 | for (i = 0; i < pi->num_cmds; i++) { |
| 2848 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2849 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2850 | if (command->argv) { |
| 2851 | for (a = 0, p = command->argv; *p; a++, p++) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2852 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2853 | } |
| 2854 | free_strings(command->argv); |
| 2855 | command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2856 | } |
| 2857 | /* not "else if": on syntax error, we may have both! */ |
| 2858 | if (command->group) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2859 | debug_printf_clean(" begin group (grp_type:%d)\n", |
| 2860 | command->grp_type); |
| 2861 | free_pipe_list(command->group); |
| 2862 | debug_printf_clean(" end group\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2863 | command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2864 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 2865 | /* else is crucial here. |
| 2866 | * If group != NULL, child_func is meaningless */ |
| 2867 | #if ENABLE_HUSH_FUNCTIONS |
| 2868 | else if (command->child_func) { |
| 2869 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 2870 | command->child_func->parent_cmd = NULL; |
| 2871 | } |
| 2872 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2873 | #if !BB_MMU |
| 2874 | free(command->group_as_string); |
| 2875 | command->group_as_string = NULL; |
| 2876 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2877 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2878 | debug_printf_clean(" redirect %d%s", |
| 2879 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2880 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 2881 | if (r->rd_filename) { |
| 2882 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 2883 | free(r->rd_filename); |
| 2884 | r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2885 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2886 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2887 | rnext = r->next; |
| 2888 | free(r); |
| 2889 | } |
| 2890 | command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2891 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2892 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 2893 | pi->cmds = NULL; |
| 2894 | #if ENABLE_HUSH_JOB |
| 2895 | free(pi->cmdtext); |
| 2896 | pi->cmdtext = NULL; |
| 2897 | #endif |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2898 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2899 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2900 | static void free_pipe_list(struct pipe *head) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2901 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2902 | struct pipe *pi, *next; |
| 2903 | |
| 2904 | for (pi = head; pi; pi = next) { |
| 2905 | #if HAS_KEYWORDS |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2906 | debug_printf_clean(" pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2907 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2908 | free_pipe(pi); |
| 2909 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2910 | next = pi->next; |
| 2911 | /*pi->next = NULL;*/ |
| 2912 | free(pi); |
| 2913 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
| 2916 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2917 | static int run_list(struct pipe *pi); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2918 | #if BB_MMU |
| 2919 | #define parse_stream(pstring, input, end_trigger) \ |
| 2920 | parse_stream(input, end_trigger) |
| 2921 | #endif |
| 2922 | static struct pipe *parse_stream(char **pstring, |
| 2923 | struct in_str *input, |
| 2924 | int end_trigger); |
| 2925 | static void parse_and_run_string(const char *s); |
| 2926 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2927 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 2928 | static char *find_in_path(const char *arg) |
| 2929 | { |
| 2930 | char *ret = NULL; |
| 2931 | const char *PATH = get_local_var_value("PATH"); |
| 2932 | |
| 2933 | if (!PATH) |
| 2934 | return NULL; |
| 2935 | |
| 2936 | while (1) { |
| 2937 | const char *end = strchrnul(PATH, ':'); |
| 2938 | int sz = end - PATH; /* must be int! */ |
| 2939 | |
| 2940 | free(ret); |
| 2941 | if (sz != 0) { |
| 2942 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 2943 | } else { |
| 2944 | /* We have xxx::yyyy in $PATH, |
| 2945 | * it means "use current dir" */ |
| 2946 | ret = xstrdup(arg); |
| 2947 | } |
| 2948 | if (access(ret, F_OK) == 0) |
| 2949 | break; |
| 2950 | |
| 2951 | if (*end == '\0') { |
| 2952 | free(ret); |
| 2953 | return NULL; |
| 2954 | } |
| 2955 | PATH = end + 1; |
| 2956 | } |
| 2957 | |
| 2958 | return ret; |
| 2959 | } |
| 2960 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2961 | static const struct built_in_command* find_builtin(const char *name) |
| 2962 | { |
| 2963 | const struct built_in_command *x; |
| 2964 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 2965 | if (strcmp(name, x->cmd) != 0) |
| 2966 | continue; |
| 2967 | debug_printf_exec("found builtin '%s'\n", name); |
| 2968 | return x; |
| 2969 | } |
| 2970 | return NULL; |
| 2971 | } |
| 2972 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2973 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2974 | static const struct function *find_function(const char *name) |
| 2975 | { |
| 2976 | const struct function *funcp = G.top_func; |
| 2977 | while (funcp) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2978 | if (strcmp(name, funcp->name) == 0) { |
| 2979 | break; |
| 2980 | } |
| 2981 | funcp = funcp->next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2982 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2983 | if (funcp) |
| 2984 | debug_printf_exec("found function '%s'\n", name); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 2985 | return funcp; |
| 2986 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 2987 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 2988 | /* Note: takes ownership on name ptr */ |
| 2989 | static struct function *new_function(char *name) |
| 2990 | { |
| 2991 | struct function *funcp; |
| 2992 | struct function **funcpp = &G.top_func; |
| 2993 | |
| 2994 | while ((funcp = *funcpp) != NULL) { |
| 2995 | struct command *cmd; |
| 2996 | |
| 2997 | if (strcmp(funcp->name, name) != 0) { |
| 2998 | funcpp = &funcp->next; |
| 2999 | continue; |
| 3000 | } |
| 3001 | |
| 3002 | cmd = funcp->parent_cmd; |
| 3003 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 3004 | if (!cmd) { |
| 3005 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 3006 | free(funcp->name); |
| 3007 | /* Note: if !funcp->body, do not free body_as_string! |
| 3008 | * This is a special case of "-F name body" function: |
| 3009 | * body_as_string was not malloced! */ |
| 3010 | if (funcp->body) { |
| 3011 | free_pipe_list(funcp->body); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3012 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3013 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3014 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3015 | } |
| 3016 | } else { |
| 3017 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 3018 | cmd->argv[0] = funcp->name; |
| 3019 | cmd->group = funcp->body; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3020 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3021 | cmd->group_as_string = funcp->body_as_string; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3022 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3023 | } |
| 3024 | goto skip; |
| 3025 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3026 | debug_printf_exec("remembering new function '%s'\n", name); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3027 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 3028 | /*funcp->next = NULL;*/ |
| 3029 | skip: |
| 3030 | funcp->name = name; |
| 3031 | return funcp; |
| 3032 | } |
| 3033 | |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3034 | static void unset_func(const char *name) |
| 3035 | { |
| 3036 | struct function *funcp; |
| 3037 | struct function **funcpp = &G.top_func; |
| 3038 | |
| 3039 | while ((funcp = *funcpp) != NULL) { |
| 3040 | if (strcmp(funcp->name, name) == 0) { |
| 3041 | *funcpp = funcp->next; |
root | 6245202 | 2009-05-04 12:00:19 +0200 | [diff] [blame] | 3042 | /* funcp is unlinked now, deleting it. |
| 3043 | * Note: if !funcp->body, the function was created by |
| 3044 | * "-F name body", do not free ->body_as_string |
| 3045 | * and ->name as they were not malloced. */ |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3046 | if (funcp->body) { |
| 3047 | free_pipe_list(funcp->body); |
root | 6245202 | 2009-05-04 12:00:19 +0200 | [diff] [blame] | 3048 | free(funcp->name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3049 | # if !BB_MMU |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3050 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3051 | # endif |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3052 | } |
| 3053 | free(funcp); |
| 3054 | break; |
| 3055 | } |
Denis Vlasenko | 7301067 | 2009-04-18 11:25:18 +0000 | [diff] [blame] | 3056 | funcpp = &funcp->next; |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3057 | } |
| 3058 | } |
| 3059 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3060 | # if BB_MMU |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3061 | #define exec_function(nommu_save, funcp, argv) \ |
| 3062 | exec_function(funcp, argv) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3063 | # endif |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3064 | static void exec_function(nommu_save_t *nommu_save, |
| 3065 | const struct function *funcp, |
| 3066 | char **argv) NORETURN; |
| 3067 | static void exec_function(nommu_save_t *nommu_save, |
| 3068 | const struct function *funcp, |
| 3069 | char **argv) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3070 | { |
| 3071 | # if BB_MMU |
| 3072 | int n = 1; |
| 3073 | |
| 3074 | argv[0] = G.global_argv[0]; |
| 3075 | G.global_argv = argv; |
| 3076 | while (*++argv) |
| 3077 | n++; |
| 3078 | G.global_argc = n; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3079 | /* On MMU, funcp->body is always non-NULL */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3080 | n = run_list(funcp->body); |
| 3081 | fflush(NULL); |
| 3082 | _exit(n); |
| 3083 | # else |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3084 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 3085 | funcp->body_as_string, |
| 3086 | G.global_argv[0], |
| 3087 | argv + 1); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3088 | # endif |
| 3089 | } |
| 3090 | |
| 3091 | static int run_function(const struct function *funcp, char **argv) |
| 3092 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3093 | int rc; |
| 3094 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 3095 | smallint sv_flg; |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3096 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3097 | save_and_replace_G_args(&sv, argv); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3098 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3099 | /* "we are in function, ok to use return" */ |
| 3100 | sv_flg = G.flag_return_in_progress; |
| 3101 | G.flag_return_in_progress = -1; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3102 | #if ENABLE_HUSH_LOCAL |
| 3103 | G.func_nest_level++; |
| 3104 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3105 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3106 | /* On MMU, funcp->body is always non-NULL */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3107 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3108 | if (!funcp->body) { |
| 3109 | /* Function defined by -F */ |
| 3110 | parse_and_run_string(funcp->body_as_string); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3111 | rc = G.last_exitcode; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3112 | } else |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3113 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3114 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3115 | rc = run_list(funcp->body); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3116 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3117 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3118 | #if ENABLE_HUSH_LOCAL |
| 3119 | { |
| 3120 | struct variable *var; |
| 3121 | struct variable **var_pp; |
| 3122 | |
| 3123 | var_pp = &G.top_var; |
| 3124 | while ((var = *var_pp) != NULL) { |
| 3125 | if (var->func_nest_level < G.func_nest_level) { |
| 3126 | var_pp = &var->next; |
| 3127 | continue; |
| 3128 | } |
| 3129 | /* Unexport */ |
| 3130 | if (var->flg_export) |
| 3131 | bb_unsetenv(var->varstr); |
| 3132 | /* Remove from global list */ |
| 3133 | *var_pp = var->next; |
| 3134 | /* Free */ |
| 3135 | if (!var->max_len) |
| 3136 | free(var->varstr); |
| 3137 | free(var); |
| 3138 | } |
| 3139 | G.func_nest_level--; |
| 3140 | } |
| 3141 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3142 | G.flag_return_in_progress = sv_flg; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3143 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3144 | restore_G_args(&sv, argv); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3145 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3146 | return rc; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3147 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3148 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3149 | |
| 3150 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 3151 | #if BB_MMU |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3152 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 3153 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 3154 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 3155 | pseudo_exec(command, argv_expanded) |
| 3156 | #endif |
| 3157 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3158 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3159 | * Never returns. |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3160 | * Don't exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3161 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3162 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3163 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 3164 | char **argv, int assignment_cnt, |
| 3165 | char **argv_expanded) NORETURN; |
| 3166 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 3167 | char **argv, int assignment_cnt, |
| 3168 | char **argv_expanded) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3169 | { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3170 | char **new_env; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3171 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3172 | /* Case when we are here: ... | var=val | ... */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3173 | if (!argv[assignment_cnt]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3174 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3175 | |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 3176 | new_env = expand_assignments(argv, assignment_cnt); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3177 | #if BB_MMU |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3178 | set_vars_and_save_old(new_env); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3179 | free(new_env); /* optional */ |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3180 | /* we can also destroy set_vars_and_save_old's return value, |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3181 | * to save memory */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3182 | #else |
| 3183 | nommu_save->new_env = new_env; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3184 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3185 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3186 | if (argv_expanded) { |
| 3187 | argv = argv_expanded; |
| 3188 | } else { |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 3189 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3190 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3191 | nommu_save->argv = argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3192 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3193 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 3194 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3195 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 3196 | if (strchr(argv[0], '/') != NULL) |
| 3197 | goto skip; |
| 3198 | #endif |
| 3199 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3200 | /* On NOMMU, we must never block! |
| 3201 | * Example: { sleep 99999 | read line } & echo Ok |
| 3202 | * read builtin will block on read syscall, leaving parent blocked |
| 3203 | * in vfork. Therefore we can't do this: |
| 3204 | */ |
| 3205 | #if BB_MMU |
| 3206 | /* Check if the command matches any of the builtins. |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3207 | * Depending on context, this might be redundant. But it's |
| 3208 | * easier to waste a few CPU cycles than it is to figure out |
| 3209 | * if this is one of those cases. |
| 3210 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3211 | { |
| 3212 | int rcode; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3213 | const struct built_in_command *x = find_builtin(argv[0]); |
| 3214 | if (x) { |
| 3215 | rcode = x->function(argv); |
| 3216 | fflush(NULL); |
| 3217 | _exit(rcode); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3218 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3219 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3220 | #endif |
| 3221 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3222 | /* Check if the command matches any functions */ |
| 3223 | { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3224 | const struct function *funcp = find_function(argv[0]); |
| 3225 | if (funcp) { |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3226 | exec_function(nommu_save, funcp, argv); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3227 | } |
| 3228 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3229 | #endif |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3230 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 3231 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3232 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3233 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3234 | int a = find_applet_by_name(argv[0]); |
| 3235 | if (a >= 0) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3236 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3237 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3238 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3239 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3240 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3241 | # endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3242 | /* Re-exec ourselves */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3243 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3244 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 3245 | execv(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3246 | /* If they called chroot or otherwise made the binary no longer |
| 3247 | * executable, fall through */ |
| 3248 | } |
| 3249 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3250 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3251 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3252 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3253 | skip: |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3254 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3255 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3256 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3257 | execvp(argv[0], argv); |
Denis Vlasenko | f9d4fc3 | 2009-04-21 20:40:51 +0000 | [diff] [blame] | 3258 | bb_perror_msg("can't execute '%s'", argv[0]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 3259 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3262 | /* Called after [v]fork() in run_pipe |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3263 | */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3264 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3265 | struct command *command, |
| 3266 | char **argv_expanded) NORETURN; |
| 3267 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3268 | struct command *command, |
| 3269 | char **argv_expanded) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3270 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3271 | if (command->argv) { |
| 3272 | pseudo_exec_argv(nommu_save, command->argv, |
| 3273 | command->assignment_cnt, argv_expanded); |
| 3274 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3275 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3276 | if (command->group) { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3277 | /* Cases when we are here: |
| 3278 | * ( list ) |
| 3279 | * { list } & |
| 3280 | * ... | ( list ) | ... |
| 3281 | * ... | { list } | ... |
| 3282 | */ |
| 3283 | #if BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 3284 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3285 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3286 | reset_traps_to_defaults(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3287 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 3288 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3289 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3290 | _exit(rcode); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3291 | #else |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3292 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 3293 | command->group_as_string, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3294 | G.global_argv[0], |
| 3295 | G.global_argv + 1); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3296 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3297 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3298 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3299 | /* Case when we are here: ... | >file */ |
| 3300 | debug_printf_exec("pseudo_exec'ed null command\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3301 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3302 | } |
| 3303 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3304 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3305 | static const char *get_cmdtext(struct pipe *pi) |
| 3306 | { |
| 3307 | char **argv; |
| 3308 | char *p; |
| 3309 | int len; |
| 3310 | |
| 3311 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 3312 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3313 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3314 | if (pi->cmdtext) |
| 3315 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3316 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3317 | if (!argv || !argv[0]) { |
| 3318 | pi->cmdtext = xzalloc(1); |
| 3319 | return pi->cmdtext; |
| 3320 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3321 | |
| 3322 | len = 0; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3323 | do { |
| 3324 | len += strlen(*argv) + 1; |
| 3325 | } while (*++argv); |
| 3326 | p = xmalloc(len); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3327 | pi->cmdtext = p; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3328 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3329 | do { |
| 3330 | len = strlen(*argv); |
| 3331 | memcpy(p, *argv, len); |
| 3332 | p += len; |
| 3333 | *p++ = ' '; |
| 3334 | } while (*++argv); |
| 3335 | p[-1] = '\0'; |
| 3336 | return pi->cmdtext; |
| 3337 | } |
| 3338 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3339 | static void insert_bg_job(struct pipe *pi) |
| 3340 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3341 | struct pipe *job, **jobp; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3342 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3343 | |
| 3344 | /* Linear search for the ID of the job to use */ |
| 3345 | pi->jobid = 1; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3346 | for (job = G.job_list; job; job = job->next) |
| 3347 | if (job->jobid >= pi->jobid) |
| 3348 | pi->jobid = job->jobid + 1; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3349 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3350 | /* Add job to the list of running jobs */ |
| 3351 | jobp = &G.job_list; |
| 3352 | while ((job = *jobp) != NULL) |
| 3353 | jobp = &job->next; |
| 3354 | job = *jobp = xmalloc(sizeof(*job)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3355 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3356 | *job = *pi; /* physical copy */ |
| 3357 | job->next = NULL; |
| 3358 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 3359 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3360 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3361 | job->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3362 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3363 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3364 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3365 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3366 | if (G_interactive_fd) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3367 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 3368 | /* Last command's pid goes to $! */ |
| 3369 | G.last_bg_pid = job->cmds[job->num_cmds - 1].pid; |
| 3370 | G.last_jobid = job->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3373 | static void remove_bg_job(struct pipe *pi) |
| 3374 | { |
| 3375 | struct pipe *prev_pipe; |
| 3376 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3377 | if (pi == G.job_list) { |
| 3378 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3379 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3380 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3381 | while (prev_pipe->next != pi) |
| 3382 | prev_pipe = prev_pipe->next; |
| 3383 | prev_pipe->next = pi->next; |
| 3384 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3385 | if (G.job_list) |
| 3386 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3387 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3388 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3389 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3390 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3391 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3392 | static void delete_finished_bg_job(struct pipe *pi) |
| 3393 | { |
| 3394 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3395 | pi->stopped_cmds = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3396 | free_pipe(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3397 | free(pi); |
| 3398 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3399 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3400 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3401 | /* Check to see if any processes have exited -- if they |
| 3402 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3403 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3404 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3405 | int attributes; |
| 3406 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3407 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3408 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3409 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3410 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3411 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3412 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3413 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 3414 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3415 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3416 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3417 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3418 | |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3419 | errno = 0; |
| 3420 | #if ENABLE_HUSH_FAST |
| 3421 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 3422 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 3423 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
Denys Vlasenko | 68759ed | 2009-05-26 14:39:41 +0200 | [diff] [blame] | 3424 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 3425 | /* Avoid doing waitpid syscall if possible */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3426 | if (!G.we_have_children) { |
| 3427 | errno = ECHILD; |
| 3428 | return -1; |
| 3429 | } |
| 3430 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 3431 | /* We have children, but they did not exit |
| 3432 | * or stop yet (we saw no SIGCHLD) */ |
| 3433 | return 0; |
| 3434 | } |
| 3435 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 3436 | } |
| 3437 | #endif |
| 3438 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3439 | /* Do we do this right? |
| 3440 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3441 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3442 | * [3]+ Stopped sleep 20 | false |
| 3443 | * bash-3.00# echo $? |
| 3444 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3445 | * [hush 1.14.0: yes we do it right] |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3446 | */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3447 | wait_more: |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3448 | while (1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3449 | int i; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3450 | int dead; |
| 3451 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3452 | #if ENABLE_HUSH_FAST |
| 3453 | i = G.count_SIGCHLD; |
| 3454 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3455 | childpid = waitpid(-1, &status, attributes); |
| 3456 | if (childpid <= 0) { |
| 3457 | if (childpid && errno != ECHILD) |
| 3458 | bb_perror_msg("waitpid"); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3459 | #if ENABLE_HUSH_FAST |
| 3460 | else { /* Until next SIGCHLD, waitpid's are useless */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 3461 | G.we_have_children = (childpid == 0); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3462 | G.handled_SIGCHLD = i; |
| 3463 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 3464 | } |
| 3465 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3466 | break; |
| 3467 | } |
| 3468 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 3469 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3470 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3471 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3472 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3473 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 3474 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3475 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3476 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 3477 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 3478 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3479 | childpid, WEXITSTATUS(status)); |
| 3480 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3481 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3482 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3483 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 3484 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 3485 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3486 | continue; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3487 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3488 | fg_pipe->cmds[i].pid = 0; |
| 3489 | fg_pipe->alive_cmds--; |
| 3490 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3491 | /* last process gives overall exitstatus */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3492 | /* Note: is WIFSIGNALED, WEXITSTATUS = sig + 128 */ |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3493 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3494 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3495 | /* bash prints killing signal's name for *last* |
| 3496 | * process in pipe (prints just newline for SIGINT). |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3497 | * Mimic this. Example: "sleep 5" + ^\ |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3498 | */ |
| 3499 | if (WIFSIGNALED(status)) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3500 | int sig = WTERMSIG(status); |
| 3501 | printf("%s\n", sig == SIGINT ? "" : get_signame(sig)); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3502 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3503 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3504 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3505 | fg_pipe->cmds[i].is_stopped = 1; |
| 3506 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3507 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3508 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 3509 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 3510 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3511 | /* All processes in fg pipe have exited or stopped */ |
| 3512 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 3513 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 3514 | * and "killall -STOP cat" */ |
| 3515 | if (G_interactive_fd) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3516 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3517 | if (fg_pipe->alive_cmds) |
| 3518 | insert_bg_job(fg_pipe); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3519 | #endif |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3520 | return rcode; |
| 3521 | } |
Denys Vlasenko | 6f22624 | 2009-06-03 14:37:30 +0200 | [diff] [blame] | 3522 | if (!fg_pipe->alive_cmds) |
| 3523 | return rcode; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3524 | } |
| 3525 | /* There are still running processes in the fg pipe */ |
| 3526 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3527 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3528 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3531 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3532 | /* We asked to wait for bg or orphaned children */ |
| 3533 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3534 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3535 | for (i = 0; i < pi->num_cmds; i++) { |
| 3536 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3537 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3538 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3539 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3540 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 3541 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3542 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 3543 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3544 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3545 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3546 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3547 | pi->cmds[i].pid = 0; |
| 3548 | pi->alive_cmds--; |
| 3549 | if (!pi->alive_cmds) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3550 | if (G_interactive_fd) |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 3551 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 3552 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3553 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3554 | } |
| 3555 | } else { |
| 3556 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3557 | pi->cmds[i].is_stopped = 1; |
| 3558 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3559 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3560 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 3561 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 3562 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3563 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3566 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3567 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 3568 | { |
| 3569 | pid_t p; |
| 3570 | int rcode = checkjobs(fg_pipe); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 3571 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3572 | /* Job finished, move the shell to the foreground */ |
| 3573 | p = getpgrp(); /* our process group id */ |
| 3574 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 3575 | tcsetpgrp(G_interactive_fd, p); |
| 3576 | } |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3577 | return rcode; |
| 3578 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3579 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 3580 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3581 | /* Start all the jobs, but don't wait for anything to finish. |
| 3582 | * See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3583 | * |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3584 | * Return code is normally -1, when the caller has to wait for children |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3585 | * to finish to determine the exit status of the pipe. If the pipe |
| 3586 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3587 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3588 | * return value. |
| 3589 | * |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3590 | * Returns -1 only if started some children. IOW: we have to |
| 3591 | * mask out retvals of builtins etc with 0xff! |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3592 | * |
| 3593 | * The only case when we do not need to [v]fork is when the pipe |
| 3594 | * is single, non-backgrounded, non-subshell command. Examples: |
| 3595 | * cmd ; ... { list } ; ... |
| 3596 | * cmd && ... { list } && ... |
| 3597 | * cmd || ... { list } || ... |
| 3598 | * If it is, then we can run cmd as a builtin, NOFORK [do we do this?], |
| 3599 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3600 | * with run_list. If it isn't one of these, we fork and exec cmd. |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3601 | * |
| 3602 | * Cases when we must fork: |
| 3603 | * non-single: cmd | cmd |
| 3604 | * backgrounded: cmd & { list } & |
| 3605 | * subshell: ( list ) [&] |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3606 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3607 | static int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3608 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3609 | static const char *const null_ptr = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3610 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3611 | int nextin; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3612 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3613 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3614 | char **argv; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3615 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3616 | /* it is not always needed, but we aim to smaller code */ |
| 3617 | int squirrel[] = { -1, -1, -1 }; |
| 3618 | int rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3619 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3620 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3621 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 3622 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 3623 | IF_HUSH_JOB(pi->pgrp = -1;) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3624 | pi->stopped_cmds = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3625 | command = &(pi->cmds[0]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3626 | argv_expanded = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3627 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3628 | if (pi->num_cmds != 1 |
| 3629 | || pi->followup == PIPE_BG |
| 3630 | || command->grp_type == GRP_SUBSHELL |
| 3631 | ) { |
| 3632 | goto must_fork; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3633 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3634 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3635 | pi->alive_cmds = 1; |
| 3636 | |
| 3637 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 3638 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 3639 | |
| 3640 | if (command->group) { |
| 3641 | #if ENABLE_HUSH_FUNCTIONS |
| 3642 | if (command->grp_type == GRP_FUNCTION) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3643 | /* "executing" func () { list } */ |
| 3644 | struct function *funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3645 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3646 | funcp = new_function(command->argv[0]); |
| 3647 | /* funcp->name is already set to argv[0] */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3648 | funcp->body = command->group; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3649 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3650 | funcp->body_as_string = command->group_as_string; |
| 3651 | command->group_as_string = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3652 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3653 | command->group = NULL; |
| 3654 | command->argv[0] = NULL; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3655 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 3656 | funcp->parent_cmd = command; |
| 3657 | command->child_func = funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3658 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3659 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 3660 | debug_leave(); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3661 | return EXIT_SUCCESS; |
| 3662 | } |
| 3663 | #endif |
| 3664 | /* { list } */ |
| 3665 | debug_printf("non-subshell group\n"); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3666 | rcode = 1; /* exitcode if redir failed */ |
| 3667 | if (setup_redirects(command, squirrel) == 0) { |
| 3668 | debug_printf_exec(": run_list\n"); |
| 3669 | rcode = run_list(command->group) & 0xff; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3670 | } |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 3671 | restore_redirects(squirrel); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3672 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3673 | debug_leave(); |
| 3674 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3675 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3678 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 3679 | { |
| 3680 | const struct built_in_command *x; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3681 | #if ENABLE_HUSH_FUNCTIONS |
| 3682 | const struct function *funcp; |
| 3683 | #else |
| 3684 | enum { funcp = 0 }; |
| 3685 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3686 | char **new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3687 | struct variable *old_vars = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3688 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3689 | if (argv[command->assignment_cnt] == NULL) { |
| 3690 | /* Assignments, but no command */ |
| 3691 | /* Ensure redirects take effect. Try "a=t >file" */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3692 | rcode = setup_redirects(command, squirrel); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3693 | restore_redirects(squirrel); |
| 3694 | /* Set shell variables */ |
| 3695 | while (*argv) { |
| 3696 | p = expand_string_to_string(*argv); |
| 3697 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 3698 | *argv, p); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3699 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3700 | argv++; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3701 | } |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3702 | /* Do we need to flag set_local_var() errors? |
| 3703 | * "assignment to readonly var" and "putenv error" |
| 3704 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3705 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3706 | debug_leave(); |
| 3707 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3708 | return rcode; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3709 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3710 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3711 | /* Expand the rest into (possibly) many strings each */ |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3712 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3713 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3714 | x = find_builtin(argv_expanded[0]); |
| 3715 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3716 | funcp = NULL; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3717 | if (!x) |
| 3718 | funcp = find_function(argv_expanded[0]); |
| 3719 | #endif |
| 3720 | if (x || funcp) { |
| 3721 | if (!funcp) { |
| 3722 | if (x->function == builtin_exec && argv_expanded[1] == NULL) { |
| 3723 | debug_printf("exec with redirects only\n"); |
| 3724 | rcode = setup_redirects(command, NULL); |
| 3725 | goto clean_up_and_ret1; |
| 3726 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3727 | } |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3728 | /* setup_redirects acts on file descriptors, not FILEs. |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3729 | * This is perfect for work that comes after exec(). |
| 3730 | * Is it really safe for inline use? Experimentally, |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3731 | * things seem to work. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3732 | rcode = setup_redirects(command, squirrel); |
| 3733 | if (rcode == 0) { |
| 3734 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3735 | old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3736 | if (!funcp) { |
| 3737 | debug_printf_exec(": builtin '%s' '%s'...\n", |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3738 | x->cmd, argv_expanded[1]); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3739 | rcode = x->function(argv_expanded) & 0xff; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 3740 | fflush(NULL); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3741 | } |
| 3742 | #if ENABLE_HUSH_FUNCTIONS |
| 3743 | else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3744 | # if ENABLE_HUSH_LOCAL |
| 3745 | struct variable **sv; |
| 3746 | sv = G.shadowed_vars_pp; |
| 3747 | G.shadowed_vars_pp = &old_vars; |
| 3748 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3749 | debug_printf_exec(": function '%s' '%s'...\n", |
| 3750 | funcp->name, argv_expanded[1]); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3751 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3752 | # if ENABLE_HUSH_LOCAL |
| 3753 | G.shadowed_vars_pp = sv; |
| 3754 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3755 | } |
| 3756 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3757 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3758 | #if ENABLE_FEATURE_SH_STANDALONE |
| 3759 | clean_up_and_ret: |
| 3760 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3761 | restore_redirects(squirrel); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3762 | unset_vars(new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3763 | add_vars(old_vars); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3764 | clean_up_and_ret1: |
| 3765 | free(argv_expanded); |
| 3766 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3767 | debug_leave(); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3768 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 3769 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3770 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3771 | |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3772 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3773 | i = find_applet_by_name(argv_expanded[0]); |
| 3774 | if (i >= 0 && APPLET_IS_NOFORK(i)) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3775 | rcode = setup_redirects(command, squirrel); |
| 3776 | if (rcode == 0) { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3777 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3778 | old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3779 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3780 | argv_expanded[0], argv_expanded[1]); |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 3781 | rcode = run_nofork_applet(i, argv_expanded); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3782 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3783 | goto clean_up_and_ret; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3784 | } |
| 3785 | #endif |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3786 | /* It is neither builtin nor applet. We must fork. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3789 | must_fork: |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3790 | /* NB: argv_expanded may already be created, and that |
| 3791 | * might include `cmd` runs! Do not rerun it! We *must* |
| 3792 | * use argv_expanded if it's non-NULL */ |
| 3793 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3794 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3795 | pi->alive_cmds = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3796 | nextin = 0; |
| 3797 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3798 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3799 | struct fd_pair pipefds; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3800 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3801 | volatile nommu_save_t nommu_save; |
| 3802 | nommu_save.new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3803 | nommu_save.old_vars = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3804 | nommu_save.argv = NULL; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3805 | nommu_save.argv_from_re_execing = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3806 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3807 | command = &(pi->cmds[i]); |
| 3808 | if (command->argv) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3809 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 3810 | command->argv[0], command->argv[1]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3811 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3812 | debug_printf_exec(": pipe member with no argv\n"); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3813 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3814 | |
| 3815 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3816 | pipefds.rd = 0; |
| 3817 | pipefds.wr = 1; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3818 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3819 | xpiped_pair(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3820 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3821 | command->pid = BB_MMU ? fork() : vfork(); |
| 3822 | if (!command->pid) { /* child */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3823 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3824 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3825 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3826 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3827 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3828 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3829 | pid_t pgrp; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3830 | pgrp = pi->pgrp; |
| 3831 | if (pgrp < 0) /* true for 1st process only */ |
| 3832 | pgrp = getpid(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3833 | if (setpgid(0, pgrp) == 0 |
| 3834 | && pi->followup != PIPE_BG |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 3835 | && G_saved_tty_pgrp /* we have ctty */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 3836 | ) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3837 | /* We do it in *every* child, not just first, |
| 3838 | * to avoid races */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3839 | tcsetpgrp(G_interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3840 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3841 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3842 | #endif |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3843 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 3844 | /* 1st cmd in backgrounded pipe |
| 3845 | * should have its stdin /dev/null'ed */ |
| 3846 | close(0); |
| 3847 | if (open(bb_dev_null, O_RDONLY)) |
| 3848 | xopen("/", O_RDONLY); |
| 3849 | } else { |
| 3850 | xmove_fd(nextin, 0); |
| 3851 | } |
| 3852 | xmove_fd(pipefds.wr, 1); |
| 3853 | if (pipefds.rd > 1) |
| 3854 | close(pipefds.rd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3855 | /* Like bash, explicit redirects override pipes, |
| 3856 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 3857 | if (setup_redirects(command, NULL)) |
| 3858 | _exit(1); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3859 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3860 | /* Restore default handlers just prior to exec */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 3861 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 3862 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3863 | /* Stores to nommu_save list of env vars putenv'ed |
| 3864 | * (NOMMU, on MMU we don't need that) */ |
| 3865 | /* cast away volatility... */ |
| 3866 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3867 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3868 | } |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 3869 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 3870 | /* parent or error */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3871 | #if ENABLE_HUSH_FAST |
| 3872 | G.count_SIGCHLD++; |
| 3873 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 3874 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3875 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3876 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3877 | /* Clean up after vforked child */ |
| 3878 | free(nommu_save.argv); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3879 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3880 | unset_vars(nommu_save.new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3881 | add_vars(nommu_save.old_vars); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3882 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3883 | free(argv_expanded); |
| 3884 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3885 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3886 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 3887 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3888 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3889 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3890 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3891 | /* Second and next children need to know pid of first one */ |
| 3892 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3893 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3894 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3895 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3896 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3897 | if (i) |
| 3898 | close(nextin); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3899 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3900 | close(pipefds.wr); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3901 | /* Pass read (output) pipe end to next iteration */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 3902 | nextin = pipefds.rd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3903 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 3904 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3905 | if (!pi->alive_cmds) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3906 | debug_leave(); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3907 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 3908 | return 1; |
| 3909 | } |
| 3910 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3911 | debug_leave(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3912 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3913 | return -1; |
| 3914 | } |
| 3915 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 3916 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3917 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3918 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3919 | static const char *const PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3920 | [PIPE_SEQ] = "SEQ", |
| 3921 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3922 | [PIPE_OR ] = "OR" , |
| 3923 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3924 | }; |
| 3925 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3926 | [RES_NONE ] = "NONE" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3927 | #if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3928 | [RES_IF ] = "IF" , |
| 3929 | [RES_THEN ] = "THEN" , |
| 3930 | [RES_ELIF ] = "ELIF" , |
| 3931 | [RES_ELSE ] = "ELSE" , |
| 3932 | [RES_FI ] = "FI" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3933 | #endif |
| 3934 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3935 | [RES_FOR ] = "FOR" , |
| 3936 | [RES_WHILE] = "WHILE", |
| 3937 | [RES_UNTIL] = "UNTIL", |
| 3938 | [RES_DO ] = "DO" , |
| 3939 | [RES_DONE ] = "DONE" , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 3940 | #endif |
| 3941 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3942 | [RES_IN ] = "IN" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3943 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3944 | #if ENABLE_HUSH_CASE |
| 3945 | [RES_CASE ] = "CASE" , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3946 | [RES_CASE_IN ] = "CASE_IN" , |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3947 | [RES_MATCH] = "MATCH", |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3948 | [RES_CASE_BODY] = "CASE_BODY", |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3949 | [RES_ESAC ] = "ESAC" , |
| 3950 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3951 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3952 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3953 | }; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3954 | static const char *const GRPTYPE[] = { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3955 | "{}", |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 3956 | "()", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3957 | #if ENABLE_HUSH_FUNCTIONS |
| 3958 | "func()", |
| 3959 | #endif |
| 3960 | }; |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3961 | |
| 3962 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3963 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3964 | pin = 0; |
| 3965 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3966 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 3967 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3968 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3969 | while (prn < pi->num_cmds) { |
| 3970 | struct command *command = &pi->cmds[prn]; |
| 3971 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3972 | |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3973 | fprintf(stderr, "%*s cmd %d assignment_cnt:%d", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3974 | lvl*2, "", prn, |
| 3975 | command->assignment_cnt); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3976 | if (command->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3977 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3978 | GRPTYPE[command->grp_type], |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3979 | argv); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3980 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3981 | prn++; |
| 3982 | continue; |
| 3983 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3984 | if (argv) while (*argv) { |
| 3985 | fprintf(stderr, " '%s'", *argv); |
| 3986 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 3987 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3988 | fprintf(stderr, "\n"); |
| 3989 | prn++; |
| 3990 | } |
| 3991 | pi = pi->next; |
| 3992 | pin++; |
| 3993 | } |
| 3994 | } |
| 3995 | #endif |
| 3996 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3997 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 3998 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3999 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4000 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4001 | #if ENABLE_HUSH_CASE |
| 4002 | char *case_word = NULL; |
| 4003 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4004 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4005 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4006 | char **for_lcur = NULL; |
| 4007 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4008 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4009 | smallint last_followup; |
| 4010 | smalluint rcode; |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 4011 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4012 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4013 | #else |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4014 | enum { cond_code = 0 }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4015 | #endif |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4016 | #if HAS_KEYWORDS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4017 | smallint rword; /* enum reserved_style */ |
| 4018 | smallint last_rword; /* ditto */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4019 | #endif |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4020 | |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4021 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4022 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4023 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4024 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4025 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4026 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 4027 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4028 | continue; |
| 4029 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4030 | if (cpipe->next == NULL) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4031 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4032 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4033 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4034 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4035 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4036 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4037 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4038 | continue; |
| 4039 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4040 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 4041 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4042 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4043 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4044 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4045 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4046 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4047 | } |
| 4048 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4049 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4050 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4051 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4052 | * in order to return, no direct "return" statements please. |
| 4053 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4054 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4055 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4056 | G.run_list_level++; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4057 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4058 | |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4059 | #if HAS_KEYWORDS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4060 | rword = RES_NONE; |
| 4061 | last_rword = RES_XXXX; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4062 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4063 | last_followup = PIPE_SEQ; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4064 | rcode = G.last_exitcode; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4065 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4066 | /* Go through list of pipes, (maybe) executing them. */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4067 | for (; pi; pi = IF_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 4068 | if (G.flag_SIGINT) |
| 4069 | break; |
| 4070 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4071 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4072 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 4073 | rword, cond_code, last_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4074 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4075 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4076 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4077 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4078 | /* start of a loop: remember where loop starts */ |
| 4079 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4080 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4081 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4082 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4083 | /* Still in the same "if...", "then..." or "do..." branch? */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4084 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4085 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 4086 | || (rcode != 0 && last_followup == PIPE_AND) |
| 4087 | ) { |
| 4088 | /* It is "<true> || CMD" or "<false> && CMD" |
| 4089 | * and we should not execute CMD */ |
| 4090 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 4091 | last_followup = pi->followup; |
| 4092 | continue; |
| 4093 | } |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4094 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4095 | last_followup = pi->followup; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4096 | IF_HAS_KEYWORDS(last_rword = rword;) |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4097 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4098 | if (cond_code) { |
| 4099 | if (rword == RES_THEN) { |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4100 | /* if false; then ... fi has exitcode 0! */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4101 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4102 | /* "if <false> THEN cmd": skip cmd */ |
| 4103 | continue; |
| 4104 | } |
| 4105 | } else { |
| 4106 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 4107 | /* "if <true> then ... ELSE/ELIF cmd": |
| 4108 | * skip cmd and all following ones */ |
| 4109 | break; |
| 4110 | } |
| 4111 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4112 | #endif |
| 4113 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4114 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4115 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 4116 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4117 | |
| 4118 | static const char encoded_dollar_at[] ALIGN1 = { |
| 4119 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 4120 | }; /* encoded representation of "$@" */ |
| 4121 | static const char *const encoded_dollar_at_argv[] = { |
| 4122 | encoded_dollar_at, NULL |
| 4123 | }; /* argv list with one element: "$@" */ |
| 4124 | char **vals; |
| 4125 | |
| 4126 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4127 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4128 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4129 | if (!pi->next->cmds[0].argv) { |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4130 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4131 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4132 | break; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4133 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4134 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4135 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4136 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4137 | debug_print_strings("for_list made from", vals); |
| 4138 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4139 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4140 | debug_print_strings("for_list", for_list); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4141 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4142 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4143 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4144 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4145 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4146 | for_lcur = NULL; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4147 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4148 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4149 | /* Insert next value from for_lcur */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4150 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4151 | set_local_var(xasprintf("%s=%s", pi->cmds[0].argv[0], *for_lcur++), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4152 | continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4153 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4154 | if (rword == RES_IN) { |
| 4155 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 4156 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4157 | if (rword == RES_DONE) { |
| 4158 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4159 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4160 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4161 | #if ENABLE_HUSH_CASE |
| 4162 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4163 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4164 | continue; |
| 4165 | } |
| 4166 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4167 | char **argv; |
| 4168 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4169 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 4170 | break; |
| 4171 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4172 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4173 | while (*argv) { |
| 4174 | char *pattern = expand_string_to_string(*argv); |
| 4175 | /* TODO: which FNM_xxx flags to use? */ |
| 4176 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 4177 | free(pattern); |
| 4178 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 4179 | free(case_word); /* make future "word)" stop */ |
| 4180 | case_word = NULL; |
| 4181 | break; |
| 4182 | } |
| 4183 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4184 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4185 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4186 | } |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4187 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4188 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4189 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4190 | } |
| 4191 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4192 | /* Just pressing <enter> in shell should check for jobs. |
| 4193 | * OTOH, in non-interactive shell this is useless |
| 4194 | * and only leads to extra job checks */ |
| 4195 | if (pi->num_cmds == 0) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4196 | if (G_interactive_fd) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4197 | goto check_jobs_and_continue; |
| 4198 | continue; |
| 4199 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4200 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4201 | /* After analyzing all keywords and conditions, we decided |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4202 | * to execute this pipe. NB: have to do checkjobs(NULL) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4203 | * after run_pipe to collect any background children, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4204 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4205 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4206 | { |
| 4207 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4208 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4209 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4210 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4211 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 4212 | if (r != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4213 | /* We ran a builtin, function, or group. |
| 4214 | * rcode is already known |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4215 | * and we don't need to wait for anything. */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4216 | G.last_exitcode = rcode; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4217 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4218 | check_and_run_traps(0); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4219 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4220 | /* Was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4221 | if (G.flag_break_continue) { |
| 4222 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4223 | /* We might fall into outer *loop*, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4224 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4225 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4226 | G.depth_break_continue--; |
| 4227 | if (G.depth_break_continue == 0) |
| 4228 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4229 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 4230 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4231 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4232 | goto check_jobs_and_break; |
| 4233 | /* "continue": simulate end of loop */ |
| 4234 | rword = RES_DONE; |
| 4235 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4236 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4237 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4238 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 673e945 | 2009-05-27 14:39:35 +0200 | [diff] [blame] | 4239 | if (G.flag_return_in_progress == 1) { |
| 4240 | /* same as "goto check_jobs_and_break" */ |
| 4241 | checkjobs(NULL); |
| 4242 | break; |
| 4243 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4244 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4245 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4246 | /* What does bash do with attempts to background builtins? */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4247 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4248 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 4249 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4250 | check_and_run_traps(0); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4251 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4252 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4253 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4254 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4255 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4256 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4257 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4258 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4259 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4260 | /* Waits for completion, then fg's main shell */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4261 | rcode = checkjobs_and_fg_shell(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4262 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4263 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4264 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4265 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4266 | { /* This one just waits for completion */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4267 | rcode = checkjobs(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4268 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4269 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4270 | } |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4271 | G.last_exitcode = rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4272 | } |
| 4273 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4274 | |
| 4275 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4276 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4277 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4278 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4279 | #endif |
| 4280 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4281 | /* Beware of "while false; true; do ..."! */ |
| 4282 | if (pi->next && pi->next->res_word == RES_DO) { |
| 4283 | if (rword == RES_WHILE) { |
| 4284 | if (rcode) { |
| 4285 | /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4286 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4287 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
| 4288 | goto check_jobs_and_break; |
| 4289 | } |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 4290 | } |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4291 | if (rword == RES_UNTIL) { |
| 4292 | if (!rcode) { |
| 4293 | debug_printf_exec(": until expr is true: breaking\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4294 | check_jobs_and_break: |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4295 | checkjobs(NULL); |
| 4296 | break; |
| 4297 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4298 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4299 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4300 | #endif |
Mike Frysinger | 8ec1c9d | 2009-03-29 00:45:26 +0000 | [diff] [blame] | 4301 | |
| 4302 | check_jobs_and_continue: |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 4303 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4304 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4305 | |
| 4306 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4307 | G.run_list_level--; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4308 | #endif |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4309 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4310 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4311 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4312 | free(for_list); |
| 4313 | #endif |
| 4314 | #if ENABLE_HUSH_CASE |
| 4315 | free(case_word); |
| 4316 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4317 | debug_leave(); |
| 4318 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4319 | return rcode; |
| 4320 | } |
| 4321 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4322 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4323 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4324 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4325 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4326 | debug_printf_exec("run_and_free_list entered\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4327 | if (!G.fake_mode) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4328 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4329 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4330 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4331 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4332 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4333 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4334 | free_pipe_list(pi); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4335 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4336 | return rcode; |
| 4337 | } |
| 4338 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4339 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 4340 | static struct pipe *new_pipe(void) |
| 4341 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4342 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4343 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4344 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4345 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4346 | return pi; |
| 4347 | } |
| 4348 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4349 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 4350 | * if ctx->command is NULL. |
| 4351 | * No errors possible here. |
| 4352 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4353 | static int done_command(struct parse_context *ctx) |
| 4354 | { |
| 4355 | /* The command is really already in the pipe structure, so |
| 4356 | * advance the pipe counter and make a new, null command. */ |
| 4357 | struct pipe *pi = ctx->pipe; |
| 4358 | struct command *command = ctx->command; |
| 4359 | |
| 4360 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4361 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4362 | debug_printf_parse("done_command: skipping null cmd, num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4363 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4364 | } |
| 4365 | pi->num_cmds++; |
| 4366 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4367 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4368 | } else { |
| 4369 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 4370 | } |
| 4371 | |
| 4372 | /* Only real trickiness here is that the uncommitted |
| 4373 | * command structure is not counted in pi->num_cmds. */ |
| 4374 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4375 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 4376 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4377 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4378 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 4379 | } |
| 4380 | |
| 4381 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 4382 | { |
| 4383 | int not_null; |
| 4384 | |
| 4385 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 4386 | /* Close previous command */ |
| 4387 | not_null = done_command(ctx); |
| 4388 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4389 | #if HAS_KEYWORDS |
| 4390 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 4391 | ctx->ctx_inverted = 0; |
| 4392 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 4393 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4394 | |
| 4395 | /* Without this check, even just <enter> on command line generates |
| 4396 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4397 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4398 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4399 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4400 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4401 | #endif |
| 4402 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4403 | || ctx->ctx_res_w == RES_DONE |
| 4404 | || ctx->ctx_res_w == RES_FOR |
| 4405 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 4406 | #endif |
| 4407 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4408 | || ctx->ctx_res_w == RES_ESAC |
| 4409 | #endif |
| 4410 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4411 | struct pipe *new_p; |
| 4412 | debug_printf_parse("done_pipe: adding new pipe: " |
| 4413 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 4414 | not_null, ctx->ctx_res_w); |
| 4415 | new_p = new_pipe(); |
| 4416 | ctx->pipe->next = new_p; |
| 4417 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4418 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4419 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4420 | * This is used to control execution. |
| 4421 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 4422 | * cases where variable or value happens to match a keyword): |
| 4423 | */ |
| 4424 | #if ENABLE_HUSH_LOOPS |
| 4425 | if (ctx->ctx_res_w == RES_FOR |
| 4426 | || ctx->ctx_res_w == RES_IN) |
| 4427 | ctx->ctx_res_w = RES_NONE; |
| 4428 | #endif |
| 4429 | #if ENABLE_HUSH_CASE |
| 4430 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4431 | ctx->ctx_res_w = RES_CASE_BODY; |
| 4432 | if (ctx->ctx_res_w == RES_CASE) |
| 4433 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4434 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4435 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4436 | /* Create the memory for command, roughly: |
| 4437 | * ctx->pipe->cmds = new struct command; |
| 4438 | * ctx->command = &ctx->pipe->cmds[0]; |
| 4439 | */ |
| 4440 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4441 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4442 | } |
| 4443 | debug_printf_parse("done_pipe return\n"); |
| 4444 | } |
| 4445 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4446 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4447 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4448 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4449 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4450 | /* Create the memory for command, roughly: |
| 4451 | * ctx->pipe->cmds = new struct command; |
| 4452 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4453 | */ |
| 4454 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4457 | /* If a reserved word is found and processed, parse context is modified |
| 4458 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4459 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4460 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4461 | struct reserved_combo { |
| 4462 | char literal[6]; |
| 4463 | unsigned char res; |
| 4464 | unsigned char assignment_flag; |
| 4465 | int flag; |
| 4466 | }; |
| 4467 | enum { |
| 4468 | FLAG_END = (1 << RES_NONE ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4469 | #if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4470 | FLAG_IF = (1 << RES_IF ), |
| 4471 | FLAG_THEN = (1 << RES_THEN ), |
| 4472 | FLAG_ELIF = (1 << RES_ELIF ), |
| 4473 | FLAG_ELSE = (1 << RES_ELSE ), |
| 4474 | FLAG_FI = (1 << RES_FI ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4475 | #endif |
| 4476 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4477 | FLAG_FOR = (1 << RES_FOR ), |
| 4478 | FLAG_WHILE = (1 << RES_WHILE), |
| 4479 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 4480 | FLAG_DO = (1 << RES_DO ), |
| 4481 | FLAG_DONE = (1 << RES_DONE ), |
| 4482 | FLAG_IN = (1 << RES_IN ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4483 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4484 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4485 | FLAG_MATCH = (1 << RES_MATCH), |
| 4486 | FLAG_ESAC = (1 << RES_ESAC ), |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4487 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4488 | FLAG_START = (1 << RES_XXXX ), |
| 4489 | }; |
| 4490 | |
| 4491 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 4492 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4493 | /* Mostly a list of accepted follow-up reserved words. |
| 4494 | * FLAG_END means we are done with the sequence, and are ready |
| 4495 | * to turn the compound list into a command. |
| 4496 | * FLAG_START means the word must start a new compound list. |
| 4497 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4498 | static const struct reserved_combo reserved_list[] = { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4499 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4500 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 4501 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 4502 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 4503 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 4504 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 4505 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4506 | #endif |
| 4507 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4508 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 4509 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 4510 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 4511 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 4512 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 4513 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4514 | #endif |
| 4515 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4516 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 4517 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4518 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4519 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4520 | const struct reserved_combo *r; |
| 4521 | |
| 4522 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 4523 | if (strcmp(word->data, r->literal) == 0) |
| 4524 | return r; |
| 4525 | } |
| 4526 | return NULL; |
| 4527 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4528 | /* Return 0: not a keyword, 1: keyword |
| 4529 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4530 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 4531 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4532 | #if ENABLE_HUSH_CASE |
| 4533 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4534 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4535 | }; |
| 4536 | #endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4537 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4538 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4539 | if (word->o_quoted) |
| 4540 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4541 | r = match_reserved_word(word); |
| 4542 | if (!r) |
| 4543 | return 0; |
| 4544 | |
| 4545 | debug_printf("found reserved word %s, res %d\n", r->literal, r->res); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4546 | #if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4547 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 4548 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4549 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4550 | } else |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4551 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4552 | if (r->flag == 0) { /* '!' */ |
| 4553 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4554 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4555 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4556 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4557 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4558 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4559 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4560 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4561 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4562 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4563 | old = xmalloc(sizeof(*old)); |
| 4564 | debug_printf_parse("push stack %p\n", old); |
| 4565 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4566 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4567 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4568 | } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4569 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4570 | ctx->ctx_res_w = RES_SNTX; |
| 4571 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4572 | } else { |
| 4573 | /* "{...} fi" is ok. "{...} if" is not |
| 4574 | * Example: |
| 4575 | * if { echo foo; } then { echo bar; } fi */ |
| 4576 | if (ctx->command->group) |
| 4577 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4578 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4579 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4580 | ctx->ctx_res_w = r->res; |
| 4581 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4582 | word->o_assignment = r->assignment_flag; |
| 4583 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4584 | if (ctx->old_flag & FLAG_END) { |
| 4585 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4586 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4587 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4588 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4589 | old = ctx->stack; |
| 4590 | old->command->group = ctx->list_head; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4591 | old->command->grp_type = GRP_NORMAL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4592 | #if !BB_MMU |
| 4593 | o_addstr(&old->as_string, ctx->as_string.data); |
| 4594 | o_free_unsafe(&ctx->as_string); |
| 4595 | old->command->group_as_string = xstrdup(old->as_string.data); |
| 4596 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 4597 | old->command->group_as_string); |
| 4598 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4599 | *ctx = *old; /* physical copy */ |
| 4600 | free(old); |
| 4601 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4602 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4603 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4604 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4605 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4606 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4607 | * Normal return is 0. Syntax errors return 1. |
| 4608 | * Note: on return, word is reset, but not o_free'd! |
| 4609 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4610 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4611 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4612 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4613 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4614 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4615 | if (word->length == 0 && word->o_quoted == 0) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4616 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 4617 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4618 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4619 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4620 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4621 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 4622 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4623 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4624 | * "2.7 Redirection |
| 4625 | * ...the word that follows the redirection operator |
| 4626 | * shall be subjected to tilde expansion, parameter expansion, |
| 4627 | * command substitution, arithmetic expansion, and quote |
| 4628 | * removal. Pathname expansion shall not be performed |
| 4629 | * on the word by a non-interactive shell; an interactive |
| 4630 | * shell may perform it, but shall do so only when |
| 4631 | * the expansion would result in one word." |
| 4632 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4633 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4634 | /* Cater for >\file case: |
| 4635 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 4636 | * Same with heredocs: |
| 4637 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 4638 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4639 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 4640 | unbackslash(ctx->pending_redirect->rd_filename); |
| 4641 | /* Is it <<"HEREDOC"? */ |
| 4642 | if (word->o_quoted) { |
| 4643 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 4644 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4645 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4646 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4647 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4648 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4649 | /* If this word wasn't an assignment, next ones definitely |
| 4650 | * can't be assignments. Even if they look like ones. */ |
| 4651 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 4652 | && word->o_assignment != WORD_IS_KEYWORD |
| 4653 | ) { |
| 4654 | word->o_assignment = NOT_ASSIGNMENT; |
| 4655 | } else { |
| 4656 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
| 4657 | command->assignment_cnt++; |
| 4658 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 4659 | } |
| 4660 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4661 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4662 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 4663 | if (ctx->ctx_dsemicolon |
| 4664 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 4665 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 4666 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4667 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 4668 | ctx->ctx_dsemicolon = 0; |
| 4669 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4670 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4671 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4672 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4673 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 4674 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4675 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4676 | # if ENABLE_HUSH_CASE |
| 4677 | && ctx->ctx_res_w != RES_CASE |
| 4678 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4679 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4680 | debug_printf_parse("checking '%s' for reserved-ness\n", word->data); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4681 | if (reserved_word(word, ctx)) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4682 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4683 | debug_printf_parse("done_word return %d\n", |
| 4684 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4685 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4686 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4687 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4688 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4689 | if (command->group) { |
| 4690 | /* "{ echo foo; } echo bar" - bad */ |
| 4691 | syntax_error_at(word->data); |
| 4692 | debug_printf_parse("done_word return 1: syntax error, " |
| 4693 | "groups and arglists don't mix\n"); |
| 4694 | return 1; |
| 4695 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4696 | if (word->o_quoted /* word had "xx" or 'xx' at least as part of it. */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4697 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 4698 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4699 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4700 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4701 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 4702 | char *p = word->data; |
| 4703 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 4704 | && (p[1] & 0x7f) == '@' |
| 4705 | && p[2] == SPECIAL_VAR_SYMBOL |
| 4706 | ) { |
| 4707 | p += 3; |
| 4708 | } |
| 4709 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4710 | /* saw no "$@", or not only "$@" but some |
| 4711 | * real text is there too */ |
| 4712 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 4713 | * e.g. "", $empty"" etc to not disappear */ |
| 4714 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 4715 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 4716 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 4717 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4718 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4719 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4720 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4721 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4722 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4723 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4724 | if (word->o_quoted |
| 4725 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 4726 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4727 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4728 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4729 | return 1; |
| 4730 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4731 | /* Force FOR to have just one word (variable name) */ |
| 4732 | /* NB: basically, this makes hush see "for v in ..." |
| 4733 | * syntax as if it is "for v; in ...". FOR and IN become |
| 4734 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4735 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4736 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4737 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4738 | #if ENABLE_HUSH_CASE |
| 4739 | /* Force CASE to have just one word */ |
| 4740 | if (ctx->ctx_res_w == RES_CASE) { |
| 4741 | done_pipe(ctx, PIPE_SEQ); |
| 4742 | } |
| 4743 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4744 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4745 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4746 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4747 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4748 | return 0; |
| 4749 | } |
| 4750 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4751 | |
| 4752 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 4753 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4754 | * Return: |
| 4755 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 4756 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 4757 | * REDIRFD_TO_FILE if no & was seen, |
| 4758 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4759 | */ |
| 4760 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4761 | #define parse_redir_right_fd(as_string, input) \ |
| 4762 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4763 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4764 | static int parse_redir_right_fd(o_string *as_string, struct in_str *input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4765 | { |
| 4766 | int ch, d, ok; |
| 4767 | |
| 4768 | ch = i_peek(input); |
| 4769 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4770 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4771 | |
| 4772 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4773 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4774 | ch = i_peek(input); |
| 4775 | if (ch == '-') { |
| 4776 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4777 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4778 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4779 | } |
| 4780 | d = 0; |
| 4781 | ok = 0; |
| 4782 | while (ch != EOF && isdigit(ch)) { |
| 4783 | d = d*10 + (ch-'0'); |
| 4784 | ok = 1; |
| 4785 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4786 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4787 | ch = i_peek(input); |
| 4788 | } |
| 4789 | if (ok) return d; |
| 4790 | |
| 4791 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 4792 | |
| 4793 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4794 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4795 | } |
| 4796 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4797 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4798 | */ |
| 4799 | static int parse_redirect(struct parse_context *ctx, |
| 4800 | int fd, |
| 4801 | redir_type style, |
| 4802 | struct in_str *input) |
| 4803 | { |
| 4804 | struct command *command = ctx->command; |
| 4805 | struct redir_struct *redir; |
| 4806 | struct redir_struct **redirp; |
| 4807 | int dup_num; |
| 4808 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4809 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4810 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4811 | /* Check for a '>&1' type redirect */ |
| 4812 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 4813 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 4814 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4815 | } else { |
| 4816 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4817 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4818 | if (dup_num) { /* <<-... */ |
| 4819 | ch = i_getch(input); |
| 4820 | nommu_addchr(&ctx->as_string, ch); |
| 4821 | ch = i_peek(input); |
| 4822 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4823 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4824 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4825 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4826 | int ch = i_peek(input); |
| 4827 | if (ch == '|') { |
| 4828 | /* >|FILE redirect ("clobbering" >). |
| 4829 | * Since we do not support "set -o noclobber" yet, |
| 4830 | * >| and > are the same for now. Just eat |. |
| 4831 | */ |
| 4832 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4833 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4834 | } |
| 4835 | } |
| 4836 | |
| 4837 | /* Create a new redir_struct and append it to the linked list */ |
| 4838 | redirp = &command->redirects; |
| 4839 | while ((redir = *redirp) != NULL) { |
| 4840 | redirp = &(redir->next); |
| 4841 | } |
| 4842 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 4843 | /* redir->next = NULL; */ |
| 4844 | /* redir->rd_filename = NULL; */ |
| 4845 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4846 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4847 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4848 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 4849 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4850 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4851 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4852 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4853 | /* Erik had a check here that the file descriptor in question |
| 4854 | * is legit; I postpone that to "run time" |
| 4855 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4856 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 4857 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4858 | } else { |
| 4859 | /* Set ctx->pending_redirect, so we know what to do at the |
| 4860 | * end of the next parsed word. */ |
| 4861 | ctx->pending_redirect = redir; |
| 4862 | } |
| 4863 | return 0; |
| 4864 | } |
| 4865 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4866 | /* If a redirect is immediately preceded by a number, that number is |
| 4867 | * supposed to tell which file descriptor to redirect. This routine |
| 4868 | * looks for such preceding numbers. In an ideal world this routine |
| 4869 | * needs to handle all the following classes of redirects... |
| 4870 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 4871 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 4872 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 4873 | * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4874 | * |
| 4875 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4876 | * "2.7 Redirection |
| 4877 | * ... If n is quoted, the number shall not be recognized as part of |
| 4878 | * the redirection expression. For example: |
| 4879 | * echo \2>a |
| 4880 | * writes the character 2 into file a" |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4881 | * We are getting it right by setting ->o_quoted on any \<char> |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4882 | * |
| 4883 | * A -1 return means no valid number was found, |
| 4884 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4885 | */ |
| 4886 | static int redirect_opt_num(o_string *o) |
| 4887 | { |
| 4888 | int num; |
| 4889 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4890 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4891 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4892 | num = bb_strtou(o->data, NULL, 10); |
| 4893 | if (errno || num < 0) |
| 4894 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4895 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4896 | return num; |
| 4897 | } |
| 4898 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4899 | #if BB_MMU |
| 4900 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 4901 | fetch_till_str(input, word, skip_tabs) |
| 4902 | #endif |
| 4903 | static char *fetch_till_str(o_string *as_string, |
| 4904 | struct in_str *input, |
| 4905 | const char *word, |
| 4906 | int skip_tabs) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4907 | { |
| 4908 | o_string heredoc = NULL_O_STRING; |
| 4909 | int past_EOL = 0; |
| 4910 | int ch; |
| 4911 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4912 | goto jump_in; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4913 | while (1) { |
| 4914 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4915 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4916 | if (ch == '\n') { |
| 4917 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 4918 | heredoc.data[past_EOL] = '\0'; |
| 4919 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 4920 | return heredoc.data; |
| 4921 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4922 | do { |
| 4923 | o_addchr(&heredoc, ch); |
| 4924 | past_EOL = heredoc.length; |
| 4925 | jump_in: |
| 4926 | do { |
| 4927 | ch = i_getch(input); |
| 4928 | nommu_addchr(as_string, ch); |
| 4929 | } while (skip_tabs && ch == '\t'); |
| 4930 | } while (ch == '\n'); |
| 4931 | } |
| 4932 | if (ch == EOF) { |
| 4933 | o_free_unsafe(&heredoc); |
| 4934 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4935 | } |
| 4936 | o_addchr(&heredoc, ch); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4937 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4938 | } |
| 4939 | } |
| 4940 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4941 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 4942 | * and load them all. There should be exactly heredoc_cnt of them. |
| 4943 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4944 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 4945 | { |
| 4946 | struct pipe *pi = ctx->list_head; |
| 4947 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4948 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4949 | int i; |
| 4950 | struct command *cmd = pi->cmds; |
| 4951 | |
| 4952 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 4953 | pi->num_cmds, |
| 4954 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 4955 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4956 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4957 | |
| 4958 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 4959 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4960 | while (redir) { |
| 4961 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4962 | char *p; |
| 4963 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4964 | redir->rd_type = REDIRECT_HEREDOC2; |
| 4965 | /* redir->dup is (ab)used to indicate <<- */ |
| 4966 | p = fetch_till_str(&ctx->as_string, input, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4967 | redir->rd_filename, redir->rd_dup & HEREDOC_SKIPTABS); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4968 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4969 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4970 | return 1; |
| 4971 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4972 | free(redir->rd_filename); |
| 4973 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4974 | heredoc_cnt--; |
| 4975 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4976 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4977 | } |
| 4978 | cmd++; |
| 4979 | } |
| 4980 | pi = pi->next; |
| 4981 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4982 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4983 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4984 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4985 | bb_error_msg_and_die("heredoc BUG 2"); |
| 4986 | #endif |
| 4987 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
| 4990 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4991 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 4992 | static FILE *generate_stream_from_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4993 | { |
| 4994 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4995 | int pid, channel[2]; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4996 | #if !BB_MMU |
| 4997 | char **to_free; |
| 4998 | #endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 4999 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 5000 | xpipe(channel); |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 5001 | pid = BB_MMU ? fork() : vfork(); |
| 5002 | if (pid < 0) |
| 5003 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5004 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5005 | if (pid == 0) { /* child */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5006 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 5007 | /* Process substitution is not considered to be usual |
| 5008 | * 'command execution'. |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 5009 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 5010 | */ |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 5011 | bb_signals(0 |
| 5012 | + (1 << SIGTSTP) |
| 5013 | + (1 << SIGTTIN) |
| 5014 | + (1 << SIGTTOU) |
| 5015 | , SIG_IGN); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5016 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 5017 | xmove_fd(channel[1], 1); |
| 5018 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 5019 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5020 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5021 | reset_traps_to_defaults(); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5022 | parse_and_run_string(s); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5023 | _exit(G.last_exitcode); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5024 | #else |
| 5025 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5026 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 5027 | * huge=`cat BIG` # was blocking here forever |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5028 | * echo OK |
| 5029 | */ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5030 | re_execute_shell(&to_free, |
| 5031 | s, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5032 | G.global_argv[0], |
| 5033 | G.global_argv + 1); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5034 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5035 | } |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5036 | |
| 5037 | /* parent */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 5038 | #if ENABLE_HUSH_FAST |
| 5039 | G.count_SIGCHLD++; |
| 5040 | //bb_error_msg("[%d] fork in generate_stream_from_string: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 5041 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5042 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5043 | #if !BB_MMU |
| 5044 | free(to_free); |
| 5045 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5046 | close(channel[1]); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 5047 | pf = fdopen(channel[0], "r"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5048 | return pf; |
| 5049 | } |
| 5050 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 5051 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5052 | static int process_command_subs(o_string *dest, const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5053 | { |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5054 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5055 | struct in_str pipe_str; |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5056 | int ch, eol_cnt; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5057 | |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5058 | pf = generate_stream_from_string(s); |
| 5059 | if (pf == NULL) |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5060 | return 1; |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5061 | close_on_exec_on(fileno(pf)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5062 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5063 | /* Now send results of command back into original context */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5064 | setup_file_in_str(&pipe_str, pf); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5065 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5066 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5067 | if (ch == '\n') { |
| 5068 | eol_cnt++; |
| 5069 | continue; |
| 5070 | } |
| 5071 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5072 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5073 | eol_cnt--; |
| 5074 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 5075 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
| 5078 | debug_printf("done reading from pipe, pclose()ing\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5079 | /* Note: we got EOF, and we just close the read end of the pipe. |
| 5080 | * We do not wait for the `cmd` child to terminate. bash and ash do. |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5081 | * Try these: |
| 5082 | * echo `echo Hi; exec 1>&-; sleep 2` - bash waits 2 sec |
| 5083 | * `false`; echo $? - bash outputs "1" |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5084 | */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5085 | fclose(pf); |
| 5086 | debug_printf("closed FILE from child. return 0\n"); |
| 5087 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5088 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5089 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5090 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5091 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5092 | struct in_str *input, int ch) |
| 5093 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5094 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5095 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5096 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5097 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5098 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5099 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5100 | |
| 5101 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5102 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5103 | if (ch == '(' && !dest->o_quoted) { |
| 5104 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5105 | if (done_word(dest, ctx)) |
| 5106 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5107 | if (!command->argv) |
| 5108 | goto skip; /* (... */ |
| 5109 | if (command->argv[1]) { /* word word ... (... */ |
| 5110 | syntax_error_unexpected_ch('('); |
| 5111 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5112 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5113 | /* it is "word(..." or "word (..." */ |
| 5114 | do |
| 5115 | ch = i_getch(input); |
| 5116 | while (ch == ' ' || ch == '\t'); |
| 5117 | if (ch != ')') { |
| 5118 | syntax_error_unexpected_ch(ch); |
| 5119 | return 1; |
| 5120 | } |
| 5121 | nommu_addchr(&ctx->as_string, ch); |
| 5122 | do |
| 5123 | ch = i_getch(input); |
| 5124 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 5125 | if (ch != '{') { |
| 5126 | syntax_error_unexpected_ch(ch); |
| 5127 | return 1; |
| 5128 | } |
| 5129 | nommu_addchr(&ctx->as_string, ch); |
| 5130 | command->grp_type = GRP_FUNCTION; |
| 5131 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5132 | } |
| 5133 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5134 | if (command->argv /* word [word]{... */ |
| 5135 | || dest->length /* word{... */ |
| 5136 | || dest->o_quoted /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5137 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5138 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5139 | debug_printf_parse("parse_group return 1: " |
| 5140 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5141 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5142 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5143 | |
| 5144 | #if ENABLE_HUSH_FUNCTIONS |
| 5145 | skip: |
| 5146 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5147 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 5148 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5149 | endch = ')'; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5150 | command->grp_type = GRP_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5151 | } else { |
| 5152 | /* bash does not allow "{echo...", requires whitespace */ |
| 5153 | ch = i_getch(input); |
| 5154 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 5155 | syntax_error_unexpected_ch(ch); |
| 5156 | return 1; |
| 5157 | } |
| 5158 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5159 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5160 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5161 | { |
| 5162 | #if !BB_MMU |
| 5163 | char *as_string = NULL; |
| 5164 | #endif |
| 5165 | pipe_list = parse_stream(&as_string, input, endch); |
| 5166 | #if !BB_MMU |
| 5167 | if (as_string) |
| 5168 | o_addstr(&ctx->as_string, as_string); |
| 5169 | #endif |
| 5170 | /* empty ()/{} or parse error? */ |
| 5171 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5172 | /* parse_stream already emitted error msg */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5173 | #if !BB_MMU |
| 5174 | free(as_string); |
| 5175 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5176 | debug_printf_parse("parse_group return 1: " |
| 5177 | "parse_stream returned %p\n", pipe_list); |
| 5178 | return 1; |
| 5179 | } |
| 5180 | command->group = pipe_list; |
| 5181 | #if !BB_MMU |
| 5182 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 5183 | command->group_as_string = as_string; |
| 5184 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 5185 | command->group_as_string); |
| 5186 | #endif |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5187 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5188 | debug_printf_parse("parse_group return 0\n"); |
| 5189 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5190 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5191 | } |
| 5192 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5193 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5194 | /* Subroutines for copying $(...) and `...` things */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5195 | static void add_till_backquote(o_string *dest, struct in_str *input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5196 | /* '...' */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5197 | static void add_till_single_quote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5198 | { |
| 5199 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5200 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5201 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5202 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5203 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5204 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5205 | if (ch == '\'') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5206 | return; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5207 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5208 | } |
| 5209 | } |
| 5210 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5211 | static void add_till_double_quote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5212 | { |
| 5213 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5214 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5215 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5216 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5217 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5218 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5219 | if (ch == '"') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5220 | return; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5221 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5222 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5223 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5224 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5225 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5226 | if (ch == '`') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5227 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5228 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5229 | continue; |
| 5230 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 5231 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5232 | } |
| 5233 | } |
| 5234 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 5235 | * \` quoting. |
| 5236 | * "Within the backquoted style of command substitution, backslash |
| 5237 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 5238 | * The search for the matching backquote shall be satisfied by the first |
| 5239 | * backquote found without a preceding backslash; during this search, |
| 5240 | * if a non-escaped backquote is encountered within a shell comment, |
| 5241 | * a here-document, an embedded command substitution of the $(command) |
| 5242 | * form, or a quoted string, undefined results occur. A single-quoted |
| 5243 | * or double-quoted string that begins, but does not end, within the |
| 5244 | * "`...`" sequence produces undefined results." |
| 5245 | * Example Output |
| 5246 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 5247 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5248 | static void add_till_backquote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5249 | { |
| 5250 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5251 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5252 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5253 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5254 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5255 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5256 | if (ch == '`') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5257 | return; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5258 | if (ch == '\\') { |
| 5259 | /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5260 | int ch2 = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5261 | if (ch2 == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5262 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5263 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5264 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5265 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5266 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5267 | ch = ch2; |
| 5268 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5269 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5270 | } |
| 5271 | } |
| 5272 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 5273 | * quoting and nested ()s. |
| 5274 | * "With the $(command) style of command substitution, all characters |
| 5275 | * following the open parenthesis to the matching closing parenthesis |
| 5276 | * constitute the command. Any valid shell script can be used for command, |
| 5277 | * except a script consisting solely of redirections which produces |
| 5278 | * unspecified results." |
| 5279 | * Example Output |
| 5280 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 5281 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 5282 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 5283 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5284 | static void add_till_closing_paren(o_string *dest, struct in_str *input, bool dbl) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5285 | { |
| 5286 | int count = 0; |
| 5287 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5288 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5289 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5290 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5291 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5292 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5293 | if (ch == '(') |
| 5294 | count++; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5295 | if (ch == ')') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5296 | if (--count < 0) { |
| 5297 | if (!dbl) |
| 5298 | break; |
| 5299 | if (i_peek(input) == ')') { |
| 5300 | i_getch(input); |
| 5301 | break; |
| 5302 | } |
| 5303 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5304 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5305 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5306 | if (ch == '\'') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5307 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5308 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5309 | continue; |
| 5310 | } |
| 5311 | if (ch == '"') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5312 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5313 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5314 | continue; |
| 5315 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5316 | if (ch == '\\') { |
| 5317 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5318 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5319 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5320 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5321 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5322 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 5323 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5324 | continue; |
| 5325 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5326 | } |
| 5327 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5328 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5329 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5330 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5331 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5332 | #define handle_dollar(as_string, dest, input) \ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5333 | handle_dollar(dest, input) |
| 5334 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5335 | static int handle_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5336 | o_string *dest, |
| 5337 | struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5338 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5339 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5340 | unsigned char quote_mask = dest->o_escape ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5341 | |
| 5342 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5343 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5344 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5345 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 5346 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5347 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5348 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5349 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5350 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5351 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5352 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5353 | if (!isalnum(ch) && ch != '_') |
| 5354 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5355 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5356 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5357 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5358 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5359 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5360 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5361 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5362 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5363 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5364 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5365 | o_addchr(dest, ch | quote_mask); |
| 5366 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5367 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5368 | case '$': /* pid */ |
| 5369 | case '!': /* last bg pid */ |
| 5370 | case '?': /* last exit code */ |
| 5371 | case '#': /* number of args */ |
| 5372 | case '*': /* args */ |
| 5373 | case '@': /* args */ |
| 5374 | goto make_one_char_var; |
| 5375 | case '{': { |
| 5376 | bool first_char, all_digits; |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 5377 | int expansion; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 5378 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5379 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5380 | nommu_addchr(as_string, ch); |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 5381 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5382 | |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 5383 | /* TODO: maybe someone will try to escape the '}' */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5384 | expansion = 0; |
| 5385 | first_char = true; |
| 5386 | all_digits = false; |
| 5387 | while (1) { |
| 5388 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5389 | nommu_addchr(as_string, ch); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5390 | if (ch == '}') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5391 | break; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5392 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5393 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5394 | if (first_char) { |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5395 | if (ch == '#') { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5396 | /* ${#var}: length of var contents */ |
| 5397 | goto char_ok; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5398 | } |
| 5399 | if (isdigit(ch)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5400 | all_digits = true; |
| 5401 | goto char_ok; |
| 5402 | } |
Mike Frysinger | dc3bc40 | 2009-06-01 14:09:09 -0400 | [diff] [blame] | 5403 | /* They're being verbose and doing ${?} */ |
| 5404 | if (i_peek(input) == '}' && strchr("$!?#*@_", ch)) |
| 5405 | goto char_ok; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5406 | } |
| 5407 | |
| 5408 | if (expansion < 2 |
| 5409 | && ( (all_digits && !isdigit(ch)) |
| 5410 | || (!all_digits && !isalnum(ch) && ch != '_') |
| 5411 | ) |
| 5412 | ) { |
| 5413 | /* handle parameter expansions |
| 5414 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 5415 | */ |
| 5416 | if (first_char) |
| 5417 | goto case_default; |
| 5418 | switch (ch) { |
| 5419 | case ':': /* null modifier */ |
| 5420 | if (expansion == 0) { |
| 5421 | debug_printf_parse(": null modifier\n"); |
| 5422 | ++expansion; |
| 5423 | break; |
| 5424 | } |
| 5425 | goto case_default; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5426 | case '#': /* remove prefix */ |
| 5427 | case '%': /* remove suffix */ |
| 5428 | if (expansion == 0) { |
| 5429 | debug_printf_parse(": remove suffix/prefix\n"); |
| 5430 | expansion = 2; |
| 5431 | break; |
| 5432 | } |
| 5433 | goto case_default; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5434 | case '-': /* default value */ |
| 5435 | case '=': /* assign default */ |
| 5436 | case '+': /* alternative */ |
| 5437 | case '?': /* error indicate */ |
| 5438 | debug_printf_parse(": parameter expansion\n"); |
| 5439 | expansion = 2; |
| 5440 | break; |
| 5441 | default: |
| 5442 | case_default: |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5443 | syntax_error_unterm_str("${name}"); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5444 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 5445 | return 1; |
| 5446 | } |
| 5447 | } |
| 5448 | char_ok: |
| 5449 | debug_printf_parse(": '%c'\n", ch); |
| 5450 | o_addchr(dest, ch | quote_mask); |
| 5451 | quote_mask = 0; |
| 5452 | first_char = false; |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5453 | } /* while (1) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5454 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5455 | break; |
| 5456 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5457 | #if (ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK) |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5458 | case '(': { |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5459 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5460 | int pos; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5461 | # endif |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5462 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5463 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5464 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5465 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5466 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5467 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5468 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5469 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5470 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5471 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5472 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5473 | add_till_closing_paren(dest, input, true); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5474 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5475 | if (as_string) { |
| 5476 | o_addstr(as_string, dest->data + pos); |
| 5477 | o_addchr(as_string, ')'); |
| 5478 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5479 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5480 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5481 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5482 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5483 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5484 | # endif |
| 5485 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5486 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5487 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5488 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5489 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5490 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5491 | add_till_closing_paren(dest, input, false); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5492 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5493 | if (as_string) { |
| 5494 | o_addstr(as_string, dest->data + pos); |
| 5495 | o_addchr(as_string, '`'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5496 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5497 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5498 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5499 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5500 | break; |
| 5501 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5502 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5503 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5504 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5505 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5506 | ch = i_peek(input); |
| 5507 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 5508 | ch = '_'; |
| 5509 | goto make_var; |
| 5510 | } |
| 5511 | /* else: it's $_ */ |
| 5512 | /* TODO: */ |
| 5513 | /* $_ Shell or shell script name; or last cmd name */ |
| 5514 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 5515 | default: |
| 5516 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5517 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5518 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5519 | return 0; |
| 5520 | } |
| 5521 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5522 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5523 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5524 | parse_stream_dquoted(dest, input, dquote_end) |
| 5525 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5526 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5527 | o_string *dest, |
| 5528 | struct in_str *input, |
| 5529 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5530 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5531 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5532 | int next; |
| 5533 | |
| 5534 | again: |
| 5535 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5536 | if (ch != EOF) |
| 5537 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5538 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5539 | if (dest->o_assignment == NOT_ASSIGNMENT) |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5540 | dest->o_escape ^= 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5541 | debug_printf_parse("parse_stream_dquoted return 0\n"); |
| 5542 | return 0; |
| 5543 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5544 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5545 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5546 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5547 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5548 | } |
| 5549 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5550 | if (ch != '\n') { |
| 5551 | next = i_peek(input); |
| 5552 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5553 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 5554 | ch, ch, dest->o_escape); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5555 | if (ch == '\\') { |
| 5556 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5557 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5558 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5559 | } |
| 5560 | /* bash: |
| 5561 | * "The backslash retains its special meaning [in "..."] |
| 5562 | * only when followed by one of the following characters: |
| 5563 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 5564 | * within double quotes by preceding it with a backslash." |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5565 | */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 5566 | if (strchr("$`\"\\\n", next) != NULL) { |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 5567 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 5568 | if (ch != '\n') { |
| 5569 | o_addqchr(dest, ch); |
| 5570 | nommu_addchr(as_string, ch); |
| 5571 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5572 | } else { |
| 5573 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 5574 | nommu_addchr(as_string, '\\'); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5575 | } |
| 5576 | goto again; |
| 5577 | } |
| 5578 | if (ch == '$') { |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5579 | if (handle_dollar(as_string, dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5580 | debug_printf_parse("parse_stream_dquoted return 1: " |
| 5581 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5582 | return 1; |
| 5583 | } |
| 5584 | goto again; |
| 5585 | } |
| 5586 | #if ENABLE_HUSH_TICK |
| 5587 | if (ch == '`') { |
| 5588 | //int pos = dest->length; |
| 5589 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5590 | o_addchr(dest, 0x80 | '`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5591 | add_till_backquote(dest, input); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5592 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5593 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5594 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5595 | } |
| 5596 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5597 | o_addQchr(dest, ch); |
| 5598 | if (ch == '=' |
| 5599 | && (dest->o_assignment == MAYBE_ASSIGNMENT |
| 5600 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5601 | && is_well_formed_var_name(dest->data, '=') |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5602 | ) { |
| 5603 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 5604 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5605 | goto again; |
| 5606 | } |
| 5607 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5608 | /* |
| 5609 | * Scan input until EOF or end_trigger char. |
| 5610 | * Return a list of pipes to execute, or NULL on EOF |
| 5611 | * or if end_trigger character is met. |
| 5612 | * On syntax error, exit is shell is not interactive, |
| 5613 | * reset parsing machinery and start parsing anew, |
| 5614 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5615 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5616 | static struct pipe *parse_stream(char **pstring, |
| 5617 | struct in_str *input, |
| 5618 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5619 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5620 | struct parse_context ctx; |
| 5621 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5622 | int is_in_dquote; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5623 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5624 | |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 5625 | /* Double-quote state is handled in the state variable is_in_dquote. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5626 | * A single-quote triggers a bypass of the main loop until its mate is |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5627 | * found. When recursing, quote state is passed in via dest->o_escape. |
| 5628 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5629 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
| 5630 | end_trigger ? : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5631 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5632 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5633 | G.ifs = get_local_var_value("IFS"); |
| 5634 | if (G.ifs == NULL) |
| 5635 | G.ifs = " \t\n"; |
| 5636 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5637 | reset: |
| 5638 | #if ENABLE_HUSH_INTERACTIVE |
| 5639 | input->promptmode = 0; /* PS1 */ |
| 5640 | #endif |
| 5641 | /* dest.o_assignment = MAYBE_ASSIGNMENT; - already is */ |
| 5642 | initialize_context(&ctx); |
| 5643 | is_in_dquote = 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5644 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5645 | while (1) { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5646 | const char *is_ifs; |
| 5647 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5648 | int ch; |
| 5649 | int next; |
| 5650 | int redir_fd; |
| 5651 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5652 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5653 | if (is_in_dquote) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5654 | /* dest.o_quoted = 1; - already is (see below) */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5655 | if (parse_stream_dquoted(&ctx.as_string, &dest, input, '"')) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5656 | goto parse_error; |
| 5657 | } |
| 5658 | /* We reached closing '"' */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5659 | is_in_dquote = 0; |
| 5660 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5661 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5662 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 5663 | ch, ch, dest.o_escape); |
| 5664 | if (ch == EOF) { |
| 5665 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5666 | |
| 5667 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5668 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5669 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5670 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5671 | /* end_trigger == '}' case errors out earlier, |
| 5672 | * checking only ')' */ |
| 5673 | if (end_trigger == ')') { |
| 5674 | syntax_error_unterm_ch('('); /* exits */ |
| 5675 | /* goto parse_error; */ |
| 5676 | } |
| 5677 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5678 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5679 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5680 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5681 | o_free(&dest); |
| 5682 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5683 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5684 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5685 | /* (this makes bare "&" cmd a no-op. |
| 5686 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5687 | if (pi->num_cmds == 0 |
| 5688 | IF_HAS_KEYWORDS( && pi->res_word == RES_NONE) |
| 5689 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5690 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5691 | pi = NULL; |
| 5692 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5693 | #if !BB_MMU |
| 5694 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 5695 | if (pstring) |
| 5696 | *pstring = ctx.as_string.data; |
| 5697 | else |
| 5698 | o_free_unsafe(&ctx.as_string); |
| 5699 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5700 | debug_leave(); |
| 5701 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5702 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5703 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5704 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5705 | is_ifs = strchr(G.ifs, ch); |
| 5706 | is_special = strchr("<>;&|(){}#'" /* special outside of "str" */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 5707 | "\\$\"" IF_HUSH_TICK("`") /* always special */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5708 | , ch); |
| 5709 | |
| 5710 | if (!is_special && !is_ifs) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5711 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5712 | o_addQchr(&dest, ch); |
| 5713 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 5714 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5715 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5716 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5717 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5718 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5719 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5720 | continue; |
| 5721 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5722 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5723 | if (is_ifs) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5724 | if (done_word(&dest, &ctx)) { |
| 5725 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 5726 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5727 | if (ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5728 | #if ENABLE_HUSH_CASE |
| 5729 | /* "case ... in <newline> word) ..." - |
| 5730 | * newlines are ignored (but ';' wouldn't be) */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5731 | if (ctx.command->argv == NULL |
| 5732 | && ctx.ctx_res_w == RES_MATCH |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5733 | ) { |
| 5734 | continue; |
| 5735 | } |
| 5736 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5737 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5738 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5739 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 5740 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5741 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5742 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5743 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5744 | heredoc_cnt = 0; |
| 5745 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5746 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5747 | ch = ';'; |
Denis Vlasenko | 7c98612 | 2009-04-04 12:15:42 +0000 | [diff] [blame] | 5748 | /* note: if (is_ifs) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5749 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5750 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5751 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5752 | |
| 5753 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 5754 | * } is an ordinary char in this case, even inside { cmd; } |
| 5755 | * Pathological example: { ""}; } should exec "}" cmd |
| 5756 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5757 | if (ch == '}') { |
| 5758 | if (!IS_NULL_CMD(ctx.command) /* cmd } */ |
| 5759 | || dest.length != 0 /* word} */ |
| 5760 | || dest.o_quoted /* ""} */ |
| 5761 | ) { |
| 5762 | goto ordinary_char; |
| 5763 | } |
| 5764 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
| 5765 | goto skip_end_trigger; |
| 5766 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5767 | } |
| 5768 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5769 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5770 | && (ch != ';' || heredoc_cnt == 0) |
| 5771 | #if ENABLE_HUSH_CASE |
| 5772 | && (ch != ')' |
| 5773 | || ctx.ctx_res_w != RES_MATCH |
| 5774 | || (!dest.o_quoted && strcmp(dest.data, "esac") == 0) |
| 5775 | ) |
| 5776 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5777 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5778 | if (heredoc_cnt) { |
| 5779 | /* This is technically valid: |
| 5780 | * { cat <<HERE; }; echo Ok |
| 5781 | * heredoc |
| 5782 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5783 | * HERE |
| 5784 | * but we don't support this. |
| 5785 | * We require heredoc to be in enclosing {}/(), |
| 5786 | * if any. |
| 5787 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5788 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5789 | goto parse_error; |
| 5790 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5791 | if (done_word(&dest, &ctx)) { |
| 5792 | goto parse_error; |
| 5793 | } |
| 5794 | done_pipe(&ctx, PIPE_SEQ); |
| 5795 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5796 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5797 | if (!HAS_KEYWORDS |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5798 | IF_HAS_KEYWORDS(|| (ctx.ctx_res_w == RES_NONE && ctx.old_flag == 0)) |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5799 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5800 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5801 | #if !BB_MMU |
| 5802 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 5803 | if (pstring) |
| 5804 | *pstring = ctx.as_string.data; |
| 5805 | else |
| 5806 | o_free_unsafe(&ctx.as_string); |
| 5807 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5808 | debug_leave(); |
| 5809 | debug_printf_parse("parse_stream return %p: " |
| 5810 | "end_trigger char found\n", |
| 5811 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5812 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5813 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5814 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5815 | skip_end_trigger: |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5816 | if (is_ifs) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5817 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5818 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5819 | next = '\0'; |
| 5820 | if (ch != '\n') { |
| 5821 | next = i_peek(input); |
| 5822 | } |
| 5823 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5824 | /* Catch <, > before deciding whether this word is |
| 5825 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 5826 | switch (ch) { |
| 5827 | case '>': |
| 5828 | redir_fd = redirect_opt_num(&dest); |
| 5829 | if (done_word(&dest, &ctx)) { |
| 5830 | goto parse_error; |
| 5831 | } |
| 5832 | redir_style = REDIRECT_OVERWRITE; |
| 5833 | if (next == '>') { |
| 5834 | redir_style = REDIRECT_APPEND; |
| 5835 | ch = i_getch(input); |
| 5836 | nommu_addchr(&ctx.as_string, ch); |
| 5837 | } |
| 5838 | #if 0 |
| 5839 | else if (next == '(') { |
| 5840 | syntax_error(">(process) not supported"); |
| 5841 | goto parse_error; |
| 5842 | } |
| 5843 | #endif |
| 5844 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5845 | goto parse_error; |
| 5846 | continue; /* back to top of while (1) */ |
| 5847 | case '<': |
| 5848 | redir_fd = redirect_opt_num(&dest); |
| 5849 | if (done_word(&dest, &ctx)) { |
| 5850 | goto parse_error; |
| 5851 | } |
| 5852 | redir_style = REDIRECT_INPUT; |
| 5853 | if (next == '<') { |
| 5854 | redir_style = REDIRECT_HEREDOC; |
| 5855 | heredoc_cnt++; |
| 5856 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 5857 | ch = i_getch(input); |
| 5858 | nommu_addchr(&ctx.as_string, ch); |
| 5859 | } else if (next == '>') { |
| 5860 | redir_style = REDIRECT_IO; |
| 5861 | ch = i_getch(input); |
| 5862 | nommu_addchr(&ctx.as_string, ch); |
| 5863 | } |
| 5864 | #if 0 |
| 5865 | else if (next == '(') { |
| 5866 | syntax_error("<(process) not supported"); |
| 5867 | goto parse_error; |
| 5868 | } |
| 5869 | #endif |
| 5870 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5871 | goto parse_error; |
| 5872 | continue; /* back to top of while (1) */ |
| 5873 | } |
| 5874 | |
| 5875 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 5876 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 5877 | && !ctx.pending_redirect |
| 5878 | ) { |
| 5879 | /* ch is a special char and thus this word |
| 5880 | * cannot be an assignment */ |
| 5881 | dest.o_assignment = NOT_ASSIGNMENT; |
| 5882 | } |
| 5883 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5884 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5885 | case '#': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5886 | if (dest.length == 0) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5887 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5888 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5889 | if (ch == EOF || ch == '\n') |
| 5890 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5891 | i_getch(input); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5892 | /* note: we do not add it to &ctx.as_string */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5893 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5894 | nommu_addchr(&ctx.as_string, '\n'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5895 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5896 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5897 | } |
| 5898 | break; |
| 5899 | case '\\': |
| 5900 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5901 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5902 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5903 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5904 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 5905 | if (ch != '\n') { |
| 5906 | o_addchr(&dest, '\\'); |
| 5907 | nommu_addchr(&ctx.as_string, '\\'); |
| 5908 | o_addchr(&dest, ch); |
| 5909 | nommu_addchr(&ctx.as_string, ch); |
| 5910 | /* Example: echo Hello \2>file |
| 5911 | * we need to know that word 2 is quoted */ |
| 5912 | dest.o_quoted = 1; |
| 5913 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5914 | break; |
| 5915 | case '$': |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5916 | if (handle_dollar(&ctx.as_string, &dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5917 | debug_printf_parse("parse_stream parse error: " |
| 5918 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5919 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5920 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5921 | break; |
| 5922 | case '\'': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5923 | dest.o_quoted = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5924 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5925 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5926 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5927 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5928 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5929 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5930 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5931 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5932 | break; |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 5933 | o_addqchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5934 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5935 | break; |
| 5936 | case '"': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5937 | dest.o_quoted = 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5938 | is_in_dquote ^= 1; /* invert */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5939 | if (dest.o_assignment == NOT_ASSIGNMENT) |
| 5940 | dest.o_escape ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5941 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5942 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5943 | case '`': { |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5944 | #if !BB_MMU |
| 5945 | int pos; |
| 5946 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5947 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5948 | o_addchr(&dest, '`'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5949 | #if !BB_MMU |
| 5950 | pos = dest.length; |
| 5951 | #endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5952 | add_till_backquote(&dest, input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5953 | #if !BB_MMU |
| 5954 | o_addstr(&ctx.as_string, dest.data + pos); |
| 5955 | o_addchr(&ctx.as_string, '`'); |
| 5956 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5957 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5958 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5959 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5960 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5961 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5962 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5963 | #if ENABLE_HUSH_CASE |
| 5964 | case_semi: |
| 5965 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5966 | if (done_word(&dest, &ctx)) { |
| 5967 | goto parse_error; |
| 5968 | } |
| 5969 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5970 | #if ENABLE_HUSH_CASE |
| 5971 | /* Eat multiple semicolons, detect |
| 5972 | * whether it means something special */ |
| 5973 | while (1) { |
| 5974 | ch = i_peek(input); |
| 5975 | if (ch != ';') |
| 5976 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5977 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5978 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5979 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5980 | ctx.ctx_dsemicolon = 1; |
| 5981 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5982 | break; |
| 5983 | } |
| 5984 | } |
| 5985 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5986 | new_cmd: |
| 5987 | /* We just finished a cmd. New one may start |
| 5988 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5989 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5990 | break; |
| 5991 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5992 | if (done_word(&dest, &ctx)) { |
| 5993 | goto parse_error; |
| 5994 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5995 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5996 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5997 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5998 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5999 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6000 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6001 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6002 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6003 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6004 | if (done_word(&dest, &ctx)) { |
| 6005 | goto parse_error; |
| 6006 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6007 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6008 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6009 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6010 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6011 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6012 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6013 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6014 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6015 | } else { |
| 6016 | /* we could pick up a file descriptor choice here |
| 6017 | * with redirect_opt_num(), but bash doesn't do it. |
| 6018 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6019 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6020 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6021 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6022 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6023 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6024 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6025 | if (ctx.ctx_res_w == RES_MATCH |
| 6026 | && ctx.command->argv == NULL /* not (word|(... */ |
| 6027 | && dest.length == 0 /* not word(... */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6028 | && dest.o_quoted == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6029 | ) { |
| 6030 | continue; |
| 6031 | } |
| 6032 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6033 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6034 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 6035 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6036 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6037 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6038 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6039 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6040 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6041 | goto case_semi; |
| 6042 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6043 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 6044 | /* proper use of this character is caught by end_trigger: |
| 6045 | * if we see {, we call parse_group(..., end_trigger='}') |
| 6046 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 6047 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6048 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6049 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 6050 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 6051 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6052 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6053 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6054 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6055 | parse_error: |
| 6056 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6057 | struct parse_context *pctx; |
| 6058 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6059 | |
| 6060 | /* Clean up allocated tree. |
| 6061 | * Samples for finding leaks on syntax error recovery path. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6062 | * Run them from interactive shell, watch pmap `pidof hush`. |
| 6063 | * while if false; then false; fi do break; done |
| 6064 | * (bash accepts it) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6065 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 6066 | * Samples to catch leaks at execution: |
| 6067 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 6068 | * while if (true | {true;}); then echo ok; fi; do (if echo ok; break; then :; fi) | cat; break; done |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6069 | */ |
| 6070 | pctx = &ctx; |
| 6071 | do { |
| 6072 | /* Update pipe/command counts, |
| 6073 | * otherwise freeing may miss some */ |
| 6074 | done_pipe(pctx, PIPE_SEQ); |
| 6075 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 6076 | pctx->list_head, pctx); |
| 6077 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6078 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6079 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6080 | #if !BB_MMU |
| 6081 | o_free_unsafe(&pctx->as_string); |
| 6082 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6083 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6084 | if (pctx != &ctx) { |
| 6085 | free(pctx); |
| 6086 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6087 | IF_HAS_KEYWORDS(pctx = p2;) |
| 6088 | } while (HAS_KEYWORDS && pctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6089 | /* Free text, clear all dest fields */ |
| 6090 | o_free(&dest); |
| 6091 | /* If we are not in top-level parse, we return, |
| 6092 | * our caller will propagate error. |
| 6093 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6094 | if (end_trigger != ';') { |
| 6095 | #if !BB_MMU |
| 6096 | if (pstring) |
| 6097 | *pstring = NULL; |
| 6098 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6099 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6100 | return ERR_PTR; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6101 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6102 | /* Discard cached input, force prompt */ |
| 6103 | input->p = NULL; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6104 | IF_HUSH_INTERACTIVE(input->promptme = 1;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6105 | goto reset; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6106 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6107 | } |
| 6108 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6109 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6110 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6111 | * end_trigger controls how often we stop parsing |
| 6112 | * NUL: parse all, execute, return |
| 6113 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6114 | */ |
| 6115 | static void parse_and_run_stream(struct in_str *inp, int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6116 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6117 | while (1) { |
| 6118 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6119 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6120 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6121 | if (!pipe_list) /* EOF */ |
| 6122 | break; |
| 6123 | debug_print_tree(pipe_list, 0); |
| 6124 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 6125 | run_and_free_list(pipe_list); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6126 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6127 | } |
| 6128 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6129 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6130 | { |
| 6131 | struct in_str input; |
| 6132 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6133 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6134 | } |
| 6135 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6136 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6137 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6138 | struct in_str input; |
| 6139 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6140 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6141 | } |
| 6142 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6143 | /* Called a few times only (or even once if "sh -c") */ |
| 6144 | static void block_signals(int second_time) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 6145 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6146 | unsigned sig; |
| 6147 | unsigned mask; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6148 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6149 | mask = (1 << SIGQUIT); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6150 | if (G_interactive_fd) { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 6151 | mask = (1 << SIGQUIT) | SPECIAL_INTERACTIVE_SIGS; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6152 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6153 | mask |= SPECIAL_JOB_SIGS; |
| 6154 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6155 | G.non_DFL_mask = mask; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 6156 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6157 | if (!second_time) |
| 6158 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 6159 | sig = 0; |
| 6160 | while (mask) { |
| 6161 | if (mask & 1) |
| 6162 | sigaddset(&G.blocked_set, sig); |
| 6163 | mask >>= 1; |
| 6164 | sig++; |
| 6165 | } |
| 6166 | sigdelset(&G.blocked_set, SIGCHLD); |
| 6167 | |
| 6168 | sigprocmask(SIG_SETMASK, &G.blocked_set, |
| 6169 | second_time ? NULL : &G.inherited_set); |
| 6170 | /* POSIX allows shell to re-enable SIGCHLD |
| 6171 | * even if it was SIG_IGN on entry */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 6172 | #if ENABLE_HUSH_FAST |
| 6173 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6174 | if (!second_time) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 6175 | signal(SIGCHLD, SIGCHLD_handler); |
| 6176 | #else |
| 6177 | if (!second_time) |
| 6178 | signal(SIGCHLD, SIG_DFL); |
| 6179 | #endif |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6180 | } |
| 6181 | |
| 6182 | #if ENABLE_HUSH_JOB |
| 6183 | /* helper */ |
| 6184 | static void maybe_set_to_sigexit(int sig) |
| 6185 | { |
| 6186 | void (*handler)(int); |
| 6187 | /* non_DFL_mask'ed signals are, well, masked, |
| 6188 | * no need to set handler for them. |
| 6189 | */ |
| 6190 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 6191 | handler = signal(sig, sigexit); |
| 6192 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
| 6193 | signal(sig, handler); |
| 6194 | } |
| 6195 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6196 | /* Set handlers to restore tty pgrp and exit */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6197 | static void set_fatal_handlers(void) |
| 6198 | { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 6199 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6200 | if (HUSH_DEBUG) { |
| 6201 | maybe_set_to_sigexit(SIGILL ); |
| 6202 | maybe_set_to_sigexit(SIGFPE ); |
| 6203 | maybe_set_to_sigexit(SIGBUS ); |
| 6204 | maybe_set_to_sigexit(SIGSEGV); |
| 6205 | maybe_set_to_sigexit(SIGTRAP); |
| 6206 | } /* else: hush is perfect. what SEGV? */ |
| 6207 | maybe_set_to_sigexit(SIGABRT); |
| 6208 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 6209 | maybe_set_to_sigexit(SIGPIPE); |
| 6210 | maybe_set_to_sigexit(SIGALRM); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6211 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are masked. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6212 | * if we aren't interactive... but in this case |
| 6213 | * we never want to restore pgrp on exit, and this fn is not called */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6214 | /*maybe_set_to_sigexit(SIGHUP );*/ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6215 | /*maybe_set_to_sigexit(SIGTERM);*/ |
| 6216 | /*maybe_set_to_sigexit(SIGINT );*/ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 6217 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 6218 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 6219 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6220 | static int set_mode(const char cstate, const char mode) |
| 6221 | { |
| 6222 | int state = (cstate == '-' ? 1 : 0); |
| 6223 | switch (mode) { |
| 6224 | case 'n': G.fake_mode = state; break; |
| 6225 | case 'x': /*G.debug_mode = state;*/ break; |
| 6226 | default: return EXIT_FAILURE; |
| 6227 | } |
| 6228 | return EXIT_SUCCESS; |
| 6229 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6230 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 6231 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 6232 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6233 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6234 | static const struct variable const_shell_ver = { |
| 6235 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 6236 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6237 | .max_len = 1, /* 0 can provoke free(name) */ |
| 6238 | .flg_export = 1, |
| 6239 | .flg_read_only = 1, |
| 6240 | }; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6241 | int signal_mask_is_inited = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6242 | int opt; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6243 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6244 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 6245 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 6246 | INIT_G(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6247 | if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6248 | G.last_exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6249 | #if !BB_MMU |
| 6250 | G.argv0_for_re_execing = argv[0]; |
| 6251 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6252 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6253 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 6254 | G.top_var = &G.shell_ver; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6255 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6256 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 6257 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6258 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6259 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 6260 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6261 | if (e) while (*e) { |
| 6262 | char *value = strchr(*e, '='); |
| 6263 | if (value) { /* paranoia */ |
| 6264 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 6265 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 6266 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6267 | cur_var->max_len = strlen(*e); |
| 6268 | cur_var->flg_export = 1; |
| 6269 | } |
| 6270 | e++; |
| 6271 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6272 | debug_printf_env("putenv '%s'\n", hush_version_str); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 6273 | putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */ |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6274 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6275 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 6276 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6277 | G.global_argc = argc; |
| 6278 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 6279 | /* Initialize some more globals to non-zero values */ |
| 6280 | set_cwd(); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 6281 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 6282 | |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 6283 | if (setjmp(die_jmp)) { |
| 6284 | /* xfunc has failed! die die die */ |
| 6285 | /* no EXIT traps, this is an escape hatch! */ |
| 6286 | G.exiting = 1; |
| 6287 | hush_exit(xfunc_error_retval); |
| 6288 | } |
| 6289 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6290 | /* Shell is non-interactive at first. We need to call |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6291 | * block_signals(0) if we are going to execute "sh <script>", |
| 6292 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6293 | * If we later decide that we are interactive, we run block_signals(0) |
| 6294 | * (or re-run block_signals(1) if we ran block_signals(0) before) |
| 6295 | * in order to intercept (more) signals. |
| 6296 | */ |
| 6297 | |
| 6298 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 6299 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6300 | while (1) { |
| 6301 | opt = getopt(argc, argv, "c:xins" |
| 6302 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 6303 | "<:$:R:V:" |
| 6304 | # if ENABLE_HUSH_FUNCTIONS |
| 6305 | "F:" |
| 6306 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6307 | #endif |
| 6308 | ); |
| 6309 | if (opt <= 0) |
| 6310 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6311 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6312 | case 'c': |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6313 | if (!G.root_pid) |
| 6314 | G.root_pid = getpid(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6315 | G.global_argv = argv + optind; |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 6316 | if (!argv[optind]) { |
| 6317 | /* -c 'script' (no params): prevent empty $0 */ |
| 6318 | *--G.global_argv = argv[0]; |
| 6319 | optind--; |
| 6320 | } /* else -c 'script' PAR0 PAR1: $0 is PAR0 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6321 | G.global_argc = argc - optind; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6322 | block_signals(0); /* 0: called 1st time */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6323 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6324 | goto final_return; |
| 6325 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 6326 | /* Well, we cannot just declare interactiveness, |
| 6327 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6328 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6329 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 6330 | case 's': |
| 6331 | /* "-s" means "read from stdin", but this is how we always |
| 6332 | * operate, so simply do nothing here. */ |
| 6333 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6334 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 6335 | case '<': /* "big heredoc" support */ |
| 6336 | full_write(STDOUT_FILENO, optarg, strlen(optarg)); |
| 6337 | _exit(0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6338 | case '$': |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6339 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 6340 | optarg++; |
| 6341 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 6342 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6343 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6344 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6345 | optarg++; |
| 6346 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6347 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 6348 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6349 | case 'R': |
| 6350 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6351 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6352 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 6353 | # if ENABLE_HUSH_FUNCTIONS |
| 6354 | case 'F': { |
| 6355 | struct function *funcp = new_function(optarg); |
| 6356 | /* funcp->name is already set to optarg */ |
| 6357 | /* funcp->body is set to NULL. It's a special case. */ |
| 6358 | funcp->body_as_string = argv[optind]; |
| 6359 | optind++; |
| 6360 | break; |
| 6361 | } |
| 6362 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6363 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6364 | case 'n': |
| 6365 | case 'x': |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 6366 | if (!set_mode('-', opt)) |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 6367 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6368 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6369 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6370 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 6371 | " or: sh -c command [args]...\n\n"); |
| 6372 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6373 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 6374 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 6375 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6376 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6377 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6378 | |
| 6379 | if (!G.root_pid) |
| 6380 | G.root_pid = getpid(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6381 | |
| 6382 | /* If we are login shell... */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6383 | if (argv[0] && argv[0][0] == '-') { |
| 6384 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6385 | debug_printf("sourcing /etc/profile\n"); |
| 6386 | input = fopen_for_read("/etc/profile"); |
| 6387 | if (input != NULL) { |
| 6388 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6389 | block_signals(0); /* 0: called 1st time */ |
| 6390 | signal_mask_is_inited = 1; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6391 | parse_and_run_file(input); |
| 6392 | fclose(input); |
| 6393 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6394 | /* bash: after sourcing /etc/profile, |
| 6395 | * tries to source (in the given order): |
| 6396 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 6397 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6398 | * bash also sources ~/.bash_logout on exit. |
| 6399 | * If called as sh, skips .bash_XXX files. |
| 6400 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 6401 | } |
| 6402 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6403 | if (argv[optind]) { |
| 6404 | FILE *input; |
| 6405 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 6406 | * "bash <script>" (which is never interactive (unless -i?)) |
| 6407 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6408 | * If called as sh, does the same but with $ENV. |
| 6409 | */ |
| 6410 | debug_printf("running script '%s'\n", argv[optind]); |
| 6411 | G.global_argv = argv + optind; |
| 6412 | G.global_argc = argc - optind; |
| 6413 | input = xfopen_for_read(argv[optind]); |
| 6414 | close_on_exec_on(fileno(input)); |
| 6415 | if (!signal_mask_is_inited) |
| 6416 | block_signals(0); /* 0: called 1st time */ |
| 6417 | parse_and_run_file(input); |
| 6418 | #if ENABLE_FEATURE_CLEAN_UP |
| 6419 | fclose(input); |
| 6420 | #endif |
| 6421 | goto final_return; |
| 6422 | } |
| 6423 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6424 | /* Up to here, shell was non-interactive. Now it may become one. |
| 6425 | * NB: don't forget to (re)run block_signals(0/1) as needed. |
| 6426 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6427 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 6428 | /* A shell is interactive if the '-i' flag was given, |
| 6429 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 6430 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6431 | * no arguments remaining or the -s flag given |
| 6432 | * standard input is a terminal |
| 6433 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6434 | * Refer to Posix.2, the description of the 'sh' utility. |
| 6435 | */ |
| 6436 | #if ENABLE_HUSH_JOB |
| 6437 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6438 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 6439 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 6440 | if (G_saved_tty_pgrp < 0) |
| 6441 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6442 | |
| 6443 | /* try to dup stdin to high fd#, >= 255 */ |
| 6444 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 6445 | if (G_interactive_fd < 0) { |
| 6446 | /* try to dup to any fd */ |
| 6447 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6448 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6449 | /* give up */ |
| 6450 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6451 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 6452 | } |
| 6453 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6454 | // TODO: track & disallow any attempts of user |
| 6455 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6456 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6457 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6458 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6459 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6460 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6461 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6462 | /* If we were run as 'hush &', sleep until we are |
| 6463 | * in the foreground (tty pgrp == our pgrp). |
| 6464 | * If we get started under a job aware app (like bash), |
| 6465 | * make sure we are now in charge so we don't fight over |
| 6466 | * who gets the foreground */ |
| 6467 | while (1) { |
| 6468 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6469 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 6470 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6471 | break; |
| 6472 | /* send TTIN to ourself (should stop us) */ |
| 6473 | kill(- shell_pgrp, SIGTTIN); |
| 6474 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6475 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6476 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6477 | /* Block some signals */ |
| 6478 | block_signals(signal_mask_is_inited); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6479 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6480 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6481 | /* Set other signals to restore saved_tty_pgrp */ |
| 6482 | set_fatal_handlers(); |
| 6483 | /* Put ourselves in our own process group |
| 6484 | * (bash, too, does this only if ctty is available) */ |
| 6485 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 6486 | /* Grab control of the terminal */ |
| 6487 | tcsetpgrp(G_interactive_fd, getpid()); |
| 6488 | } |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 6489 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 6490 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6491 | enable_restore_tty_pgrp_on_exit(); /* sets die_sleep = -1 */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6492 | } else if (!signal_mask_is_inited) { |
| 6493 | block_signals(0); /* 0: called 1st time */ |
| 6494 | } /* else: block_signals(0) was done before */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6495 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6496 | /* No job control compiled in, only prompt/line editing */ |
| 6497 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6498 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 6499 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6500 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6501 | G_interactive_fd = dup(STDIN_FILENO); |
| 6502 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6503 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6504 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 6505 | } |
| 6506 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6507 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6508 | close_on_exec_on(G_interactive_fd); |
| 6509 | block_signals(signal_mask_is_inited); |
| 6510 | } else if (!signal_mask_is_inited) { |
| 6511 | block_signals(0); |
| 6512 | } |
| 6513 | #else |
| 6514 | /* We have interactiveness code disabled */ |
| 6515 | if (!signal_mask_is_inited) { |
| 6516 | block_signals(0); |
| 6517 | } |
| 6518 | #endif |
| 6519 | /* bash: |
| 6520 | * if interactive but not a login shell, sources ~/.bashrc |
| 6521 | * (--norc turns this off, --rcfile <file> overrides) |
| 6522 | */ |
| 6523 | |
| 6524 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Mike Frysinger | 258275d | 2009-04-05 21:19:43 +0000 | [diff] [blame] | 6525 | printf("\n\n%s hush - the humble shell\n", bb_banner); |
Mike Frysinger | 26cf283 | 2009-04-24 06:40:30 +0000 | [diff] [blame] | 6526 | if (ENABLE_HUSH_HELP) |
| 6527 | puts("Enter 'help' for a list of built-in commands."); |
| 6528 | puts(""); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 6529 | } |
| 6530 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 6531 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6532 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6533 | final_return: |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 6534 | #if ENABLE_FEATURE_CLEAN_UP |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6535 | if (G.cwd != bb_msg_unknown) |
| 6536 | free((char*)G.cwd); |
| 6537 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6538 | while (cur_var) { |
| 6539 | struct variable *tmp = cur_var; |
| 6540 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 6541 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 6542 | cur_var = cur_var->next; |
| 6543 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 6544 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6545 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6546 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6547 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 6548 | |
| 6549 | |
| 6550 | #if ENABLE_LASH |
| 6551 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 6552 | int lash_main(int argc, char **argv) |
| 6553 | { |
| 6554 | //bb_error_msg("lash is deprecated, please use hush instead"); |
| 6555 | return hush_main(argc, argv); |
| 6556 | } |
| 6557 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6558 | |
| 6559 | |
| 6560 | /* |
| 6561 | * Built-ins |
| 6562 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 6563 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6564 | { |
| 6565 | return 0; |
| 6566 | } |
| 6567 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 6568 | static int FAST_FUNC builtin_test(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6569 | { |
| 6570 | int argc = 0; |
| 6571 | while (*argv) { |
| 6572 | argc++; |
| 6573 | argv++; |
| 6574 | } |
| 6575 | return test_main(argc, argv - argc); |
| 6576 | } |
| 6577 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 6578 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6579 | { |
| 6580 | int argc = 0; |
| 6581 | while (*argv) { |
| 6582 | argc++; |
| 6583 | argv++; |
| 6584 | } |
| 6585 | return echo_main(argc, argv - argc); |
| 6586 | } |
| 6587 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 6588 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6589 | { |
| 6590 | int rcode = EXIT_SUCCESS; |
| 6591 | |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6592 | if (*++argv) { |
| 6593 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6594 | /* bash: |
| 6595 | * eval "echo Hi; done" ("done" is syntax error): |
| 6596 | * "echo Hi" will not execute too. |
| 6597 | */ |
| 6598 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6599 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6600 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6601 | } |
| 6602 | return rcode; |
| 6603 | } |
| 6604 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 6605 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6606 | { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6607 | const char *newdir = argv[1]; |
| 6608 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6609 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6610 | * bash says "bash: cd: HOME not set" and does nothing |
| 6611 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6612 | */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 6613 | newdir = get_local_var_value("HOME") ? : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6614 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6615 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 6616 | /* Mimic bash message exactly */ |
| 6617 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6618 | return EXIT_FAILURE; |
| 6619 | } |
| 6620 | set_cwd(); |
| 6621 | return EXIT_SUCCESS; |
| 6622 | } |
| 6623 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 6624 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6625 | { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6626 | if (*++argv == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6627 | return EXIT_SUCCESS; /* bash does this */ |
| 6628 | { |
| 6629 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 6630 | nommu_save_t dummy; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6631 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6632 | /* TODO: if exec fails, bash does NOT exit! We do... */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6633 | pseudo_exec_argv(&dummy, argv, 0, NULL); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6634 | /* never returns */ |
| 6635 | } |
| 6636 | } |
| 6637 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 6638 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6639 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 6640 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 6641 | |
| 6642 | /* interactive bash: |
| 6643 | * # trap "echo EEE" EXIT |
| 6644 | * # exit |
| 6645 | * exit |
| 6646 | * There are stopped jobs. |
| 6647 | * (if there are _stopped_ jobs, running ones don't count) |
| 6648 | * # exit |
| 6649 | * exit |
| 6650 | # EEE (then bash exits) |
| 6651 | * |
| 6652 | * we can use G.exiting = -1 as indicator "last cmd was exit" |
| 6653 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 6654 | |
| 6655 | /* note: EXIT trap is run by hush_exit */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6656 | if (*++argv == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 6657 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6658 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 6659 | xfunc_error_retval = 255; |
| 6660 | /* bash: exit -2 == exit 254, no error msg */ |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 6661 | hush_exit(xatoi(*argv) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6662 | } |
| 6663 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6664 | static void print_escaped(const char *s) |
| 6665 | { |
| 6666 | do { |
| 6667 | if (*s != '\'') { |
| 6668 | const char *p; |
| 6669 | |
| 6670 | p = strchrnul(s, '\''); |
| 6671 | /* print 'xxxx', possibly just '' */ |
| 6672 | printf("'%.*s'", (int)(p - s), s); |
| 6673 | if (*p == '\0') |
| 6674 | break; |
| 6675 | s = p; |
| 6676 | } |
| 6677 | /* s points to '; print "'''...'''" */ |
| 6678 | putchar('"'); |
| 6679 | do putchar('\''); while (*++s == '\''); |
| 6680 | putchar('"'); |
| 6681 | } while (*s); |
| 6682 | } |
| 6683 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6684 | #if !ENABLE_HUSH_LOCAL |
| 6685 | #define helper_export_local(argv, exp, lvl) \ |
| 6686 | helper_export_local(argv, exp) |
| 6687 | #endif |
| 6688 | static void helper_export_local(char **argv, int exp, int lvl) |
| 6689 | { |
| 6690 | do { |
| 6691 | char *name = *argv; |
| 6692 | |
| 6693 | /* So far we do not check that name is valid (TODO?) */ |
| 6694 | |
| 6695 | if (strchr(name, '=') == NULL) { |
| 6696 | struct variable *var; |
| 6697 | |
| 6698 | var = get_local_var(name); |
| 6699 | if (exp == -1) { /* unexporting? */ |
| 6700 | /* export -n NAME (without =VALUE) */ |
| 6701 | if (var) { |
| 6702 | var->flg_export = 0; |
| 6703 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 6704 | unsetenv(name); |
| 6705 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 6706 | continue; |
| 6707 | } |
| 6708 | if (exp == 1) { /* exporting? */ |
| 6709 | /* export NAME (without =VALUE) */ |
| 6710 | if (var) { |
| 6711 | var->flg_export = 1; |
| 6712 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 6713 | putenv(var->varstr); |
| 6714 | continue; |
| 6715 | } |
| 6716 | } |
| 6717 | /* Exporting non-existing variable. |
| 6718 | * bash does not put it in environment, |
| 6719 | * but remembers that it is exported, |
| 6720 | * and does put it in env when it is set later. |
| 6721 | * We just set it to "" and export. */ |
| 6722 | /* Or, it's "local NAME" (without =VALUE). |
| 6723 | * bash sets the value to "". */ |
| 6724 | name = xasprintf("%s=", name); |
| 6725 | } else { |
| 6726 | /* (Un)exporting/making local NAME=VALUE */ |
| 6727 | name = xstrdup(name); |
| 6728 | } |
| 6729 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 6730 | } while (*++argv); |
| 6731 | } |
| 6732 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 6733 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6734 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 6735 | unsigned opt_unexport; |
| 6736 | |
| 6737 | if (argv[1] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6738 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6739 | if (e) { |
| 6740 | while (*e) { |
| 6741 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6742 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6743 | #else |
| 6744 | /* ash emits: export VAR='VAL' |
| 6745 | * bash: declare -x VAR="VAL" |
| 6746 | * we follow ash example */ |
| 6747 | const char *s = *e++; |
| 6748 | const char *p = strchr(s, '='); |
| 6749 | |
| 6750 | if (!p) /* wtf? take next variable */ |
| 6751 | continue; |
| 6752 | /* export var= */ |
| 6753 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6754 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6755 | putchar('\n'); |
| 6756 | #endif |
| 6757 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6758 | /*fflush(stdout); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6759 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6760 | return EXIT_SUCCESS; |
| 6761 | } |
| 6762 | |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 6763 | #if ENABLE_HUSH_EXPORT_N |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 6764 | /* "!": do not abort on errors */ |
| 6765 | /* "+": stop at 1st non-option */ |
| 6766 | opt_unexport = getopt32(argv, "!+n"); |
| 6767 | if (opt_unexport == (unsigned)-1) |
| 6768 | return EXIT_FAILURE; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 6769 | argv += optind; |
| 6770 | #else |
| 6771 | opt_unexport = 0; |
| 6772 | argv++; |
| 6773 | #endif |
| 6774 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6775 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6776 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6777 | return EXIT_SUCCESS; |
| 6778 | } |
| 6779 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6780 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 6781 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 6782 | { |
| 6783 | if (G.func_nest_level == 0) { |
| 6784 | bb_error_msg("%s: not in a function", argv[0]); |
| 6785 | return EXIT_FAILURE; /* bash compat */ |
| 6786 | } |
| 6787 | helper_export_local(argv, 0, G.func_nest_level); |
| 6788 | return EXIT_SUCCESS; |
| 6789 | } |
| 6790 | #endif |
| 6791 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 6792 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6793 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6794 | int sig; |
| 6795 | char *new_cmd; |
| 6796 | |
| 6797 | if (!G.traps) |
| 6798 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 6799 | |
| 6800 | argv++; |
| 6801 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 6802 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6803 | /* No args: print all trapped */ |
| 6804 | for (i = 0; i < NSIG; ++i) { |
| 6805 | if (G.traps[i]) { |
| 6806 | printf("trap -- "); |
| 6807 | print_escaped(G.traps[i]); |
| 6808 | printf(" %s\n", get_signame(i)); |
| 6809 | } |
| 6810 | } |
| 6811 | /*fflush(stdout); - done after each builtin anyway */ |
| 6812 | return EXIT_SUCCESS; |
| 6813 | } |
| 6814 | |
| 6815 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6816 | /* If first arg is a number: reset all specified signals */ |
| 6817 | sig = bb_strtou(*argv, NULL, 10); |
| 6818 | if (errno == 0) { |
| 6819 | int ret; |
| 6820 | process_sig_list: |
| 6821 | ret = EXIT_SUCCESS; |
| 6822 | while (*argv) { |
| 6823 | sig = get_signum(*argv++); |
| 6824 | if (sig < 0 || sig >= NSIG) { |
| 6825 | ret = EXIT_FAILURE; |
| 6826 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 6827 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6828 | continue; |
| 6829 | } |
| 6830 | |
| 6831 | free(G.traps[sig]); |
| 6832 | G.traps[sig] = xstrdup(new_cmd); |
| 6833 | |
| 6834 | debug_printf("trap: setting SIG%s (%i) to '%s'", |
| 6835 | get_signame(sig), sig, G.traps[sig]); |
| 6836 | |
| 6837 | /* There is no signal for 0 (EXIT) */ |
| 6838 | if (sig == 0) |
| 6839 | continue; |
| 6840 | |
| 6841 | if (new_cmd) { |
| 6842 | sigaddset(&G.blocked_set, sig); |
| 6843 | } else { |
| 6844 | /* There was a trap handler, we are removing it |
| 6845 | * (if sig has non-DFL handling, |
| 6846 | * we don't need to do anything) */ |
| 6847 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 6848 | continue; |
| 6849 | sigdelset(&G.blocked_set, sig); |
| 6850 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6851 | } |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 6852 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 6853 | return ret; |
| 6854 | } |
| 6855 | |
| 6856 | if (!argv[1]) { /* no second arg */ |
| 6857 | bb_error_msg("trap: invalid arguments"); |
| 6858 | return EXIT_FAILURE; |
| 6859 | } |
| 6860 | |
| 6861 | /* First arg is "-": reset all specified to default */ |
| 6862 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 6863 | /* Everything else: set arg as signal handler |
| 6864 | * (includes "" case, which ignores signal) */ |
| 6865 | if (argv[0][0] == '-') { |
| 6866 | if (argv[0][1] == '\0') { /* "-" */ |
| 6867 | /* new_cmd remains NULL: "reset these sigs" */ |
| 6868 | goto reset_traps; |
| 6869 | } |
| 6870 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 6871 | argv++; |
| 6872 | } |
| 6873 | /* else: "-something", no special meaning */ |
| 6874 | } |
| 6875 | new_cmd = *argv; |
| 6876 | reset_traps: |
| 6877 | argv++; |
| 6878 | goto process_sig_list; |
| 6879 | } |
| 6880 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6881 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 6882 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6883 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 6884 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6885 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 6886 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6887 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 6888 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6889 | |
| 6890 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 6891 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6892 | type = "an alias";*/ |
| 6893 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 6894 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6895 | type = "a function"; |
| 6896 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 6897 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6898 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 6899 | else if ((path = find_in_path(*argv)) != NULL) |
| 6900 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 6901 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 6902 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6903 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 6904 | continue; |
| 6905 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6906 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 6907 | printf("%s is %s\n", *argv, type); |
| 6908 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 6909 | } |
| 6910 | |
| 6911 | return ret; |
| 6912 | } |
| 6913 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6914 | #if ENABLE_HUSH_JOB |
| 6915 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 6916 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6917 | { |
| 6918 | int i, jobnum; |
| 6919 | struct pipe *pi; |
| 6920 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6921 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6922 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 6923 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6924 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 6925 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6926 | for (pi = G.job_list; pi; pi = pi->next) { |
| 6927 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6928 | goto found; |
| 6929 | } |
| 6930 | } |
| 6931 | bb_error_msg("%s: no current job", argv[0]); |
| 6932 | return EXIT_FAILURE; |
| 6933 | } |
| 6934 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 6935 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 6936 | return EXIT_FAILURE; |
| 6937 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 6938 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6939 | if (pi->jobid == jobnum) { |
| 6940 | goto found; |
| 6941 | } |
| 6942 | } |
| 6943 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 6944 | return EXIT_FAILURE; |
| 6945 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 6946 | /* TODO: bash prints a string representation |
| 6947 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 6948 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6949 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6950 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6951 | } |
| 6952 | |
| 6953 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 6954 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 6955 | for (i = 0; i < pi->num_cmds; i++) { |
| 6956 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 6957 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6958 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 6959 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6960 | |
| 6961 | i = kill(- pi->pgrp, SIGCONT); |
| 6962 | if (i < 0) { |
| 6963 | if (errno == ESRCH) { |
| 6964 | delete_finished_bg_job(pi); |
| 6965 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6966 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 6967 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6968 | } |
| 6969 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 6970 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6971 | remove_bg_job(pi); |
| 6972 | return checkjobs_and_fg_shell(pi); |
| 6973 | } |
| 6974 | return EXIT_SUCCESS; |
| 6975 | } |
| 6976 | #endif |
| 6977 | |
| 6978 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 6979 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6980 | { |
| 6981 | const struct built_in_command *x; |
| 6982 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 6983 | printf("\n" |
| 6984 | "Built-in commands:\n" |
| 6985 | "------------------\n"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6986 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 6987 | printf("%s\t%s\n", x->cmd, x->descr); |
| 6988 | } |
| 6989 | printf("\n\n"); |
| 6990 | return EXIT_SUCCESS; |
| 6991 | } |
| 6992 | #endif |
| 6993 | |
| 6994 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 6995 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6996 | { |
| 6997 | struct pipe *job; |
| 6998 | const char *status_string; |
| 6999 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7000 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7001 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7002 | status_string = "Stopped"; |
| 7003 | else |
| 7004 | status_string = "Running"; |
| 7005 | |
| 7006 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 7007 | } |
| 7008 | return EXIT_SUCCESS; |
| 7009 | } |
| 7010 | #endif |
| 7011 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7012 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 7013 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 7014 | { |
| 7015 | void *p; |
| 7016 | unsigned long l; |
| 7017 | |
| 7018 | /* Crude attempt to find where "free memory" starts, |
| 7019 | * sans fragmentation. */ |
| 7020 | p = malloc(240); |
| 7021 | l = (unsigned long)p; |
| 7022 | free(p); |
| 7023 | p = malloc(3400); |
| 7024 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 7025 | free(p); |
| 7026 | |
| 7027 | if (!G.memleak_value) |
| 7028 | G.memleak_value = l; |
| 7029 | |
| 7030 | l -= G.memleak_value; |
| 7031 | if ((long)l < 0) |
| 7032 | l = 0; |
| 7033 | l /= 1024; |
| 7034 | if (l > 127) |
| 7035 | l = 127; |
| 7036 | |
| 7037 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 7038 | return l; |
| 7039 | } |
| 7040 | #endif |
| 7041 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 7042 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7043 | { |
| 7044 | puts(set_cwd()); |
| 7045 | return EXIT_SUCCESS; |
| 7046 | } |
| 7047 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 7048 | static int FAST_FUNC builtin_read(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7049 | { |
| 7050 | char *string; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 7051 | const char *name = "REPLY"; |
| 7052 | |
| 7053 | if (argv[1]) { |
| 7054 | name = argv[1]; |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 7055 | /* bash (3.2.33(1)) bug: "read 0abcd" will execute, |
| 7056 | * and _after_ that_ it will complain */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 7057 | if (!is_well_formed_var_name(name, '\0')) { |
| 7058 | /* Mimic bash message */ |
| 7059 | bb_error_msg("read: '%s': not a valid identifier", name); |
| 7060 | return 1; |
| 7061 | } |
| 7062 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7063 | |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 7064 | //TODO: bash unbackslashes input, splits words and puts them in argv[i] |
| 7065 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7066 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7067 | return set_local_var(string, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7068 | } |
| 7069 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7070 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 7071 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7072 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7073 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 7074 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7075 | * set -- [argument...] |
| 7076 | * set -o |
| 7077 | * set +o |
| 7078 | * Implementations shall support the options in both their hyphen and |
| 7079 | * plus-sign forms. These options can also be specified as options to sh. |
| 7080 | * Examples: |
| 7081 | * Write out all variables and their values: set |
| 7082 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 7083 | * Turn on the -x and -v options: set -xv |
| 7084 | * Unset all positional parameters: set -- |
| 7085 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 7086 | * Set the positional parameters to the expansion of x, even if x expands |
| 7087 | * with a leading '-' or '+': set -- $x |
| 7088 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7089 | * So far, we only support "set -- [argument...]" and some of the short names. |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7090 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 7091 | static int FAST_FUNC builtin_set(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7092 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7093 | int n; |
| 7094 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7095 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7096 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7097 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7098 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7099 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7100 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7101 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 7102 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7103 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7104 | do { |
| 7105 | if (!strcmp(arg, "--")) { |
| 7106 | ++argv; |
| 7107 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7108 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 7109 | if (arg[0] != '+' && arg[0] != '-') |
| 7110 | break; |
| 7111 | for (n = 1; arg[n]; ++n) |
| 7112 | if (set_mode(arg[0], arg[n])) |
| 7113 | goto error; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7114 | } while ((arg = *++argv) != NULL); |
| 7115 | /* Now argv[0] is 1st argument */ |
| 7116 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7117 | if (arg == NULL) |
| 7118 | return EXIT_SUCCESS; |
| 7119 | set_argv: |
| 7120 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 7121 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 7122 | g_argv = G.global_argv; |
| 7123 | if (G.global_args_malloced) { |
| 7124 | pp = g_argv; |
| 7125 | while (*++pp) |
| 7126 | free(*pp); |
| 7127 | g_argv[1] = NULL; |
| 7128 | } else { |
| 7129 | G.global_args_malloced = 1; |
| 7130 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 7131 | pp[0] = g_argv[0]; /* retain $0 */ |
| 7132 | g_argv = pp; |
| 7133 | } |
| 7134 | /* This realloc's G.global_argv */ |
| 7135 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 7136 | |
| 7137 | n = 1; |
| 7138 | while (*++pp) |
| 7139 | n++; |
| 7140 | G.global_argc = n; |
| 7141 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7142 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7143 | |
| 7144 | /* Nothing known, so abort */ |
| 7145 | error: |
| 7146 | bb_error_msg("set: %s: invalid option", arg); |
| 7147 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7148 | } |
| 7149 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 7150 | static int FAST_FUNC builtin_shift(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7151 | { |
| 7152 | int n = 1; |
| 7153 | if (argv[1]) { |
| 7154 | n = atoi(argv[1]); |
| 7155 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7156 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 7157 | if (G.global_args_malloced) { |
| 7158 | int m = 1; |
| 7159 | while (m <= n) |
| 7160 | free(G.global_argv[m++]); |
| 7161 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7162 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 7163 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 7164 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7165 | return EXIT_SUCCESS; |
| 7166 | } |
| 7167 | return EXIT_FAILURE; |
| 7168 | } |
| 7169 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 7170 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7171 | { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7172 | char *arg_path; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7173 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7174 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7175 | #if ENABLE_HUSH_FUNCTIONS |
| 7176 | smallint sv_flg; |
| 7177 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7178 | |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7179 | if (*++argv == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7180 | return EXIT_FAILURE; |
| 7181 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7182 | if (strchr(*argv, '/') == NULL && (arg_path = find_in_path(*argv)) != NULL) { |
| 7183 | input = fopen_for_read(arg_path); |
| 7184 | free(arg_path); |
| 7185 | } else |
| 7186 | input = fopen_or_warn(*argv, "r"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7187 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7188 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7189 | return EXIT_FAILURE; |
| 7190 | } |
| 7191 | close_on_exec_on(fileno(input)); |
| 7192 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7193 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7194 | sv_flg = G.flag_return_in_progress; |
| 7195 | /* "we are inside sourced file, ok to use return" */ |
| 7196 | G.flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7197 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7198 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7199 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7200 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7201 | fclose(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 7202 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7203 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7204 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7205 | G.flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 7206 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7207 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7208 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7209 | } |
| 7210 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 7211 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7212 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7213 | int rc; |
| 7214 | mode_t mask; |
| 7215 | |
| 7216 | mask = umask(0); |
| 7217 | if (argv[1]) { |
| 7218 | mode_t old_mask = mask; |
| 7219 | |
| 7220 | mask ^= 0777; |
| 7221 | rc = bb_parse_mode(argv[1], &mask); |
| 7222 | mask ^= 0777; |
| 7223 | if (rc == 0) { |
| 7224 | mask = old_mask; |
| 7225 | /* bash messages: |
| 7226 | * bash: umask: 'q': invalid symbolic mode operator |
| 7227 | * bash: umask: 999: octal number out of range |
| 7228 | */ |
| 7229 | bb_error_msg("%s: '%s' invalid mode", argv[0], argv[1]); |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7230 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7231 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7232 | rc = 1; |
| 7233 | /* Mimic bash */ |
| 7234 | printf("%04o\n", (unsigned) mask); |
| 7235 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7236 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 7237 | umask(mask); |
| 7238 | |
| 7239 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7240 | } |
| 7241 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7242 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 7243 | static int FAST_FUNC builtin_unset(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7244 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7245 | int ret; |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7246 | unsigned opts; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7247 | |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7248 | /* "!": do not abort on errors */ |
| 7249 | /* "+": stop at 1st non-option */ |
| 7250 | opts = getopt32(argv, "!+vf"); |
| 7251 | if (opts == (unsigned)-1) |
| 7252 | return EXIT_FAILURE; |
| 7253 | if (opts == 3) { |
| 7254 | bb_error_msg("unset: -v and -f are exclusive"); |
| 7255 | return EXIT_FAILURE; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7256 | } |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7257 | argv += optind; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7258 | |
| 7259 | ret = EXIT_SUCCESS; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7260 | while (*argv) { |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 7261 | if (!(opts & 2)) { /* not -f */ |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7262 | if (unset_local_var(*argv)) { |
| 7263 | /* unset <nonexistent_var> doesn't fail. |
| 7264 | * Error is when one tries to unset RO var. |
| 7265 | * Message was printed by unset_local_var. */ |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7266 | ret = EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7267 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7268 | } |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 7269 | #if ENABLE_HUSH_FUNCTIONS |
| 7270 | else { |
| 7271 | unset_func(*argv); |
| 7272 | } |
| 7273 | #endif |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7274 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 7275 | } |
| 7276 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7277 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7278 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7279 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 7280 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7281 | { |
| 7282 | int ret = EXIT_SUCCESS; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7283 | int status, sig; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7284 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7285 | if (*++argv == NULL) { |
| 7286 | /* Don't care about wait results */ |
| 7287 | /* Note 1: must wait until there are no more children */ |
| 7288 | /* Note 2: must be interruptible */ |
| 7289 | /* Examples: |
| 7290 | * $ sleep 3 & sleep 6 & wait |
| 7291 | * [1] 30934 sleep 3 |
| 7292 | * [2] 30935 sleep 6 |
| 7293 | * [1] Done sleep 3 |
| 7294 | * [2] Done sleep 6 |
| 7295 | * $ sleep 3 & sleep 6 & wait |
| 7296 | * [1] 30936 sleep 3 |
| 7297 | * [2] 30937 sleep 6 |
| 7298 | * [1] Done sleep 3 |
| 7299 | * ^C <-- after ~4 sec from keyboard |
| 7300 | * $ |
| 7301 | */ |
| 7302 | sigaddset(&G.blocked_set, SIGCHLD); |
| 7303 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7304 | while (1) { |
| 7305 | checkjobs(NULL); |
| 7306 | if (errno == ECHILD) |
| 7307 | break; |
| 7308 | /* Wait for SIGCHLD or any other signal of interest */ |
| 7309 | /* sigtimedwait with infinite timeout: */ |
| 7310 | sig = sigwaitinfo(&G.blocked_set, NULL); |
| 7311 | if (sig > 0) { |
| 7312 | sig = check_and_run_traps(sig); |
| 7313 | if (sig && sig != SIGCHLD) { /* see note 2 */ |
| 7314 | ret = 128 + sig; |
| 7315 | break; |
| 7316 | } |
| 7317 | } |
| 7318 | } |
| 7319 | sigdelset(&G.blocked_set, SIGCHLD); |
| 7320 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7321 | return ret; |
| 7322 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7323 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 7324 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7325 | while (*argv) { |
| 7326 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 7327 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7328 | /* mimic bash message */ |
| 7329 | bb_error_msg("wait: '%s': not a pid or valid job spec", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7330 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7331 | } |
| 7332 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7333 | if (WIFSIGNALED(status)) |
| 7334 | ret = 128 + WTERMSIG(status); |
| 7335 | else if (WIFEXITED(status)) |
| 7336 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7337 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7338 | ret = EXIT_FAILURE; |
| 7339 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7340 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7341 | ret = 127; |
| 7342 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7343 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 7344 | } |
| 7345 | |
| 7346 | return ret; |
| 7347 | } |
| 7348 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7349 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 7350 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 7351 | { |
| 7352 | if (argv[1]) { |
| 7353 | def = bb_strtou(argv[1], NULL, 10); |
| 7354 | if (errno || def < def_min || argv[2]) { |
| 7355 | bb_error_msg("%s: bad arguments", argv[0]); |
| 7356 | def = UINT_MAX; |
| 7357 | } |
| 7358 | } |
| 7359 | return def; |
| 7360 | } |
| 7361 | #endif |
| 7362 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 7363 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 7364 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7365 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7366 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7367 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 7368 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 7369 | return EXIT_SUCCESS; /* bash compat */ |
| 7370 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7371 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7372 | |
| 7373 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 7374 | if (depth == UINT_MAX) |
| 7375 | G.flag_break_continue = BC_BREAK; |
| 7376 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7377 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7378 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7379 | return EXIT_SUCCESS; |
| 7380 | } |
| 7381 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 7382 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7383 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 7384 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 7385 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 7386 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 7387 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7388 | |
| 7389 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame^] | 7390 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 7391 | { |
| 7392 | int rc; |
| 7393 | |
| 7394 | if (G.flag_return_in_progress != -1) { |
| 7395 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 7396 | return EXIT_FAILURE; /* bash compat */ |
| 7397 | } |
| 7398 | |
| 7399 | G.flag_return_in_progress = 1; |
| 7400 | |
| 7401 | /* bash: |
| 7402 | * out of range: wraps around at 256, does not error out |
| 7403 | * non-numeric param: |
| 7404 | * f() { false; return qwe; }; f; echo $? |
| 7405 | * bash: return: qwe: numeric argument required <== we do this |
| 7406 | * 255 <== we also do this |
| 7407 | */ |
| 7408 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 7409 | return rc; |
| 7410 | } |
| 7411 | #endif |