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 | * |
| 8 | * Copyright (C) 2000,2001 Larry Doolittle <larry@doolittle.boa.org> |
| 9 | * |
| 10 | * Credits: |
| 11 | * The parser routines proper are all original material, first |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 12 | * written Dec 2000 and Jan 2001 by Larry Doolittle. The |
| 13 | * execution engine, the builtins, and much of the underlying |
| 14 | * support has been adapted from busybox-0.49pre's lash, which is |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 15 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 16 | * written by Erik Andersen <andersen@codepoet.org>. That, in turn, |
| 17 | * is based in part on ladsh.c, by Michael K. Johnson and Erik W. |
| 18 | * Troan, which they placed in the public domain. I don't know |
| 19 | * how much of the Johnson/Troan code has survived the repeated |
| 20 | * rewrites. |
| 21 | * |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 22 | * Other credits: |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 23 | * o_addchr() derived from similar w_addchar function in glibc-2.2. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 24 | * setup_redirect(), redirect_opt_num(), and big chunks of main() |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 25 | * and many builtins derived from contributions by Erik Andersen. |
| 26 | * Miscellaneous bugfixes from Matt Kraai. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 27 | * |
| 28 | * There are two big (and related) architecture differences between |
| 29 | * this parser and the lash parser. One is that this version is |
| 30 | * actually designed from the ground up to understand nearly all |
| 31 | * of the Bourne grammar. The second, consequential change is that |
| 32 | * the parser and input reader have been turned inside out. Now, |
| 33 | * the parser is in control, and asks for input as needed. The old |
| 34 | * way had the input reader in control, and it asked for parsing to |
| 35 | * take place as needed. The new way makes it much easier to properly |
| 36 | * handle the recursion implicit in the various substitutions, especially |
| 37 | * across continuation lines. |
| 38 | * |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 39 | * POSIX syntax not implemented: |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 40 | * aliases |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 41 | * <(list) and >(list) Process Substitution |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 42 | * Here Documents ( << word ) |
| 43 | * Functions |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 44 | * Tilde Expansion |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 45 | * Parameter Expansion for substring processing ${var#word} ${var%word} |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 46 | * |
| 47 | * Bash stuff maybe optional enable: |
| 48 | * &> and >& redirection of stdout+stderr |
| 49 | * Brace expansion |
| 50 | * reserved words: [[ ]] function select |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 51 | * substrings ${var:1:5} |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 52 | * |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 53 | * Major bugs: |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 54 | * job handling woefully incomplete and buggy (improved --vda) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 55 | * to-do: |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 56 | * port selected bugfixes from post-0.49 busybox lash - done? |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 57 | * change { and } from special chars to reserved words |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 58 | * builtins: return, trap, ulimit |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 59 | * test magic exec with redirection only |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 60 | * follow IFS rules more precisely, including update semantics |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 61 | * figure out what to do with backslash-newline |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 62 | * propagate syntax errors, die on resource errors? |
| 63 | * continuation lines, both explicit and implicit - done? |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 64 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 65 | * 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] | 66 | */ |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 67 | |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 68 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 69 | //TODO: pull in some .h and find out whether we have SINGLE_APPLET_MAIN? |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 70 | //#include "applet_tables.h" doesn't work |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 71 | #include <glob.h> |
| 72 | /* #include <dmalloc.h> */ |
| 73 | #if ENABLE_HUSH_CASE |
| 74 | #include <fnmatch.h> |
| 75 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 76 | #include "math.h" |
| 77 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 78 | #ifdef WANT_TO_TEST_NOMMU |
| 79 | # undef BB_MMU |
| 80 | # undef USE_FOR_NOMMU |
| 81 | # undef USE_FOR_MMU |
| 82 | # define BB_MMU 0 |
| 83 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 84 | # define USE_FOR_MMU(...) |
| 85 | #endif |
| 86 | |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 87 | #if defined SINGLE_APPLET_MAIN |
| 88 | /* STANDALONE does not make sense, and won't compile */ |
| 89 | #undef CONFIG_FEATURE_SH_STANDALONE |
| 90 | #undef ENABLE_FEATURE_SH_STANDALONE |
| 91 | #undef USE_FEATURE_SH_STANDALONE |
| 92 | #define SKIP_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
| 93 | #define ENABLE_FEATURE_SH_STANDALONE 0 |
| 94 | #define USE_FEATURE_SH_STANDALONE(...) |
| 95 | #define SKIP_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
| 96 | #endif |
| 97 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 98 | #if !ENABLE_HUSH_INTERACTIVE |
| 99 | #undef ENABLE_FEATURE_EDITING |
| 100 | #define ENABLE_FEATURE_EDITING 0 |
| 101 | #undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 102 | #define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 103 | #endif |
| 104 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 105 | /* Do we support ANY keywords? */ |
| 106 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 107 | #define HAS_KEYWORDS 1 |
| 108 | #define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 109 | #define IF_HAS_NO_KEYWORDS(...) |
| 110 | #else |
| 111 | #define HAS_KEYWORDS 0 |
| 112 | #define IF_HAS_KEYWORDS(...) |
| 113 | #define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
| 114 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 115 | |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 116 | /* Keep unconditionally on for now */ |
| 117 | #define HUSH_DEBUG 1 |
| 118 | /* In progress... */ |
| 119 | #define ENABLE_HUSH_FUNCTIONS 0 |
| 120 | |
| 121 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 122 | /* If you comment out one of these below, it will be #defined later |
| 123 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 124 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 125 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 126 | #define debug_printf_parse(...) do {} while (0) |
| 127 | #define debug_print_tree(a, b) do {} while (0) |
| 128 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 129 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 130 | #define debug_printf_jobs(...) do {} while (0) |
| 131 | #define debug_printf_expand(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 132 | #define debug_printf_glob(...) do {} while (0) |
| 133 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 134 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 135 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 136 | |
| 137 | #ifndef debug_printf |
| 138 | #define debug_printf(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 139 | #endif |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 140 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 141 | #ifndef debug_printf_parse |
| 142 | #define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 143 | #endif |
| 144 | |
| 145 | #ifndef debug_printf_exec |
| 146 | #define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__) |
| 147 | #endif |
| 148 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 149 | #ifndef debug_printf_env |
| 150 | #define debug_printf_env(...) fprintf(stderr, __VA_ARGS__) |
| 151 | #endif |
| 152 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 153 | #ifndef debug_printf_jobs |
| 154 | #define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 155 | #define DEBUG_JOBS 1 |
| 156 | #else |
| 157 | #define DEBUG_JOBS 0 |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 158 | #endif |
| 159 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 160 | #ifndef debug_printf_expand |
| 161 | #define debug_printf_expand(...) fprintf(stderr, __VA_ARGS__) |
| 162 | #define DEBUG_EXPAND 1 |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 163 | #else |
| 164 | #define DEBUG_EXPAND 0 |
| 165 | #endif |
| 166 | |
| 167 | #ifndef debug_printf_glob |
| 168 | #define debug_printf_glob(...) fprintf(stderr, __VA_ARGS__) |
| 169 | #define DEBUG_GLOB 1 |
| 170 | #else |
| 171 | #define DEBUG_GLOB 0 |
| 172 | #endif |
| 173 | |
| 174 | #ifndef debug_printf_list |
| 175 | #define debug_printf_list(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 176 | #endif |
| 177 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 178 | #ifndef debug_printf_subst |
| 179 | #define debug_printf_subst(...) fprintf(stderr, __VA_ARGS__) |
| 180 | #endif |
| 181 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 182 | #ifndef debug_printf_clean |
| 183 | /* broken, of course, but OK for testing */ |
| 184 | static const char *indenter(int i) |
| 185 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 186 | static const char blanks[] ALIGN1 = |
| 187 | " "; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 188 | return &blanks[sizeof(blanks) - i - 1]; |
| 189 | } |
| 190 | #define debug_printf_clean(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 191 | #define DEBUG_CLEAN 1 |
Denis Vlasenko | a0e6512 | 2009-04-05 23:39:14 +0000 | [diff] [blame] | 192 | #else |
| 193 | #define DEBUG_CLEAN 0 |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 194 | #endif |
| 195 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 196 | #if DEBUG_EXPAND |
| 197 | static void debug_print_strings(const char *prefix, char **vv) |
| 198 | { |
| 199 | fprintf(stderr, "%s:\n", prefix); |
| 200 | while (*vv) |
| 201 | fprintf(stderr, " '%s'\n", *vv++); |
| 202 | } |
| 203 | #else |
| 204 | #define debug_print_strings(prefix, vv) ((void)0) |
| 205 | #endif |
| 206 | |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 207 | /* |
| 208 | * Leak hunting. Use hush_leaktool.sh for post-processing. |
| 209 | */ |
| 210 | #ifdef FOR_HUSH_LEAKTOOL |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 211 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 212 | { |
| 213 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 214 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 215 | return ptr; |
| 216 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 217 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 218 | { |
| 219 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 220 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 221 | return ptr; |
| 222 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 223 | static char *xxstrdup(int lineno, const char *str) |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 224 | { |
| 225 | char *ptr = xstrdup(str); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 226 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 227 | return ptr; |
| 228 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 229 | static void xxfree(void *ptr) |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 230 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 231 | fdprintf(2, "free %p\n", ptr); |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 232 | free(ptr); |
| 233 | } |
| 234 | #define xmalloc(s) xxmalloc(__LINE__, s) |
| 235 | #define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 236 | #define xstrdup(s) xxstrdup(__LINE__, s) |
| 237 | #define free(p) xxfree(p) |
| 238 | #endif |
| 239 | |
| 240 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 241 | #define ERR_PTR ((void*)(long)1) |
| 242 | |
Mike Frysinger | 258275d | 2009-04-05 21:19:43 +0000 | [diff] [blame] | 243 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 244 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 245 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 246 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 247 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 248 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 249 | typedef enum redir_type { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 250 | REDIRECT_INPUT = 1, |
| 251 | REDIRECT_OVERWRITE = 2, |
| 252 | REDIRECT_APPEND = 3, |
| 253 | REDIRECT_HEREIS = 4, |
| 254 | REDIRECT_IO = 5 |
| 255 | } redir_type; |
| 256 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 257 | /* The descrip member of this structure is only used to make |
| 258 | * debugging output pretty */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 259 | static const struct { |
| 260 | int mode; |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 261 | signed char default_fd; |
| 262 | char descrip[3]; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 263 | } redir_table[] = { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 264 | { 0, 0, "()" }, |
| 265 | { O_RDONLY, 0, "<" }, |
| 266 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 267 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 268 | { O_RDONLY, -1, "<<" }, |
| 269 | { O_RDWR, 1, "<>" } |
| 270 | }; |
| 271 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 272 | typedef enum pipe_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 273 | PIPE_SEQ = 1, |
| 274 | PIPE_AND = 2, |
| 275 | PIPE_OR = 3, |
| 276 | PIPE_BG = 4, |
| 277 | } pipe_style; |
| 278 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 279 | typedef enum reserved_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 280 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 281 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 282 | RES_IF , |
| 283 | RES_THEN , |
| 284 | RES_ELIF , |
| 285 | RES_ELSE , |
| 286 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 287 | #endif |
| 288 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 289 | RES_FOR , |
| 290 | RES_WHILE , |
| 291 | RES_UNTIL , |
| 292 | RES_DO , |
| 293 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 294 | #endif |
| 295 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 296 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 297 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 298 | #if ENABLE_HUSH_CASE |
| 299 | RES_CASE , |
| 300 | /* two pseudo-keywords support contrived "case" syntax: */ |
| 301 | RES_MATCH , /* "word)" */ |
| 302 | RES_CASEI , /* "this command is inside CASE" */ |
| 303 | RES_ESAC , |
| 304 | #endif |
| 305 | RES_XXXX , |
| 306 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 307 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 308 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 309 | typedef struct o_string { |
| 310 | char *data; |
| 311 | int length; /* position where data is appended */ |
| 312 | int maxlen; |
| 313 | /* Protect newly added chars against globbing |
| 314 | * (by prepending \ to *, ?, [, \) */ |
| 315 | smallint o_escape; |
| 316 | smallint o_glob; |
| 317 | smallint nonnull; |
| 318 | smallint has_empty_slot; |
| 319 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 320 | } o_string; |
| 321 | enum { |
| 322 | MAYBE_ASSIGNMENT = 0, |
| 323 | DEFINITELY_ASSIGNMENT = 1, |
| 324 | NOT_ASSIGNMENT = 2, |
| 325 | WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */ |
| 326 | }; |
| 327 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 328 | #define NULL_O_STRING { NULL } |
| 329 | |
| 330 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
| 331 | * available? Where is it documented? */ |
| 332 | typedef struct in_str { |
| 333 | const char *p; |
| 334 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 335 | char eof_flag; /* meaningless if ->p == NULL */ |
| 336 | char peek_buf[2]; |
| 337 | #if ENABLE_HUSH_INTERACTIVE |
| 338 | smallint promptme; |
| 339 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 340 | #endif |
| 341 | FILE *file; |
| 342 | int (*get) (struct in_str *); |
| 343 | int (*peek) (struct in_str *); |
| 344 | } in_str; |
| 345 | #define i_getch(input) ((input)->get(input)) |
| 346 | #define i_peek(input) ((input)->peek(input)) |
| 347 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 348 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 349 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 350 | char *rd_filename; /* filename */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 351 | int fd; /* file descriptor being redirected */ |
| 352 | int dup; /* -1, or file descriptor being duplicated */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 353 | smallint /*enum redir_type*/ rd_type; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 354 | }; |
| 355 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 356 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 357 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 358 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 359 | smallint is_stopped; /* is the command currently running? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 360 | smallint grp_type; /* GRP_xxx */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 361 | struct pipe *group; /* if non-NULL, this "command" is { list }, |
| 362 | * ( list ), or a compound statement */ |
| 363 | #if !BB_MMU |
| 364 | char *group_as_string; |
| 365 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 366 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 367 | struct redir_struct *redirects; /* I/O redirections */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 368 | }; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 369 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 370 | * and on execution these are substituted with their values. |
| 371 | * Substitution can make _several_ words out of one argv[n]! |
| 372 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 373 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 374 | */ |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 375 | #define GRP_NORMAL 0 |
| 376 | #define GRP_SUBSHELL 1 |
| 377 | #if ENABLE_HUSH_FUNCTIONS |
| 378 | #define GRP_FUNCTION 2 |
| 379 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 380 | |
| 381 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 382 | struct pipe *next; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 383 | int num_cmds; /* total number of commands in job */ |
| 384 | int alive_cmds; /* number of commands running (not exited) */ |
| 385 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 386 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 387 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 388 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 389 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 390 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 391 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 392 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 393 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 394 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 395 | }; |
| 396 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 397 | /* This holds pointers to the various results of parsing */ |
| 398 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 399 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 400 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 401 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 402 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 403 | /* last command in pipe (being constructed right now) */ |
| 404 | struct command *command; |
| 405 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 406 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 407 | #if !BB_MMU |
| 408 | o_string as_string; |
| 409 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 410 | #if HAS_KEYWORDS |
| 411 | smallint ctx_res_w; |
| 412 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 413 | #if ENABLE_HUSH_CASE |
| 414 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 415 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 416 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 417 | int old_flag; |
| 418 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 419 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 420 | * when we see "if" or "then", we malloc and copy current context, |
| 421 | * and make ->stack point to it. then we parse pipeN. |
| 422 | * when closing "then" / fi" / whatever is found, |
| 423 | * we move list_head into ->stack->command->group, |
| 424 | * copy ->stack into current context, and delete ->stack. |
| 425 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 426 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 427 | struct parse_context *stack; |
| 428 | #endif |
| 429 | }; |
| 430 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 431 | /* On program start, environ points to initial environment. |
| 432 | * putenv adds new pointers into it, unsetenv removes them. |
| 433 | * Neither of these (de)allocates the strings. |
| 434 | * setenv allocates new strings in malloc space and does putenv, |
| 435 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 436 | #define setenv(...) setenv_is_leaky_dont_use() |
| 437 | struct variable { |
| 438 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 439 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 440 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 441 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 442 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 443 | }; |
| 444 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 445 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 446 | BC_BREAK = 1, |
| 447 | BC_CONTINUE = 2, |
| 448 | }; |
| 449 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 450 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 451 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 452 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 453 | struct globals { |
| 454 | #if ENABLE_HUSH_INTERACTIVE |
| 455 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 456 | * _AND_ if we decided to act interactively */ |
| 457 | int interactive_fd; |
| 458 | const char *PS1; |
| 459 | const char *PS2; |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 460 | #define G_interactive_fd (G.interactive_fd) |
| 461 | #else |
| 462 | #define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 463 | #endif |
| 464 | #if ENABLE_FEATURE_EDITING |
| 465 | line_input_t *line_input_state; |
| 466 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 467 | pid_t root_pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 468 | pid_t last_bg_pid; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 469 | #if ENABLE_HUSH_JOB |
| 470 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 471 | pid_t saved_tty_pgrp; |
| 472 | int last_jobid; |
| 473 | struct pipe *job_list; |
| 474 | struct pipe *toplevel_list; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 475 | //// smallint ctrl_z_flag; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 476 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 477 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 478 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 479 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 480 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 481 | smallint fake_mode; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 482 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 483 | smalluint last_return_code; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 484 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 485 | smalluint global_args_malloced; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 486 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 487 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 488 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 489 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 490 | char *argv0_for_re_execing; |
| 491 | char **argv_from_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 492 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 493 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 494 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 495 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 496 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 497 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 498 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 499 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 500 | struct variable shell_ver; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 501 | /* Signal and trap handling */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 502 | // unsigned count_SIGCHLD; |
| 503 | // unsigned handled_SIGCHLD; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 504 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 505 | unsigned non_DFL_mask; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 506 | char **traps; /* char *traps[NSIG] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 507 | sigset_t blocked_set; |
| 508 | sigset_t inherited_set; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 509 | char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2]; |
| 510 | #if ENABLE_FEATURE_SH_STANDALONE |
| 511 | struct nofork_save_area nofork_save; |
| 512 | #endif |
| 513 | #if ENABLE_HUSH_JOB |
| 514 | sigjmp_buf toplevel_jb; |
| 515 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 516 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 517 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 518 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 519 | * (too many defines). Also, I actually prefer to see when a variable |
| 520 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 521 | #define INIT_G() do { \ |
| 522 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 523 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 524 | |
| 525 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 526 | /* Function prototypes for builtins */ |
| 527 | static int builtin_cd(char **argv); |
| 528 | static int builtin_echo(char **argv); |
| 529 | static int builtin_eval(char **argv); |
| 530 | static int builtin_exec(char **argv); |
| 531 | static int builtin_exit(char **argv); |
| 532 | static int builtin_export(char **argv); |
| 533 | #if ENABLE_HUSH_JOB |
| 534 | static int builtin_fg_bg(char **argv); |
| 535 | static int builtin_jobs(char **argv); |
| 536 | #endif |
| 537 | #if ENABLE_HUSH_HELP |
| 538 | static int builtin_help(char **argv); |
| 539 | #endif |
| 540 | static int builtin_pwd(char **argv); |
| 541 | static int builtin_read(char **argv); |
| 542 | static int builtin_test(char **argv); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 543 | static int builtin_trap(char **argv); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 544 | static int builtin_true(char **argv); |
| 545 | static int builtin_set(char **argv); |
| 546 | static int builtin_shift(char **argv); |
| 547 | static int builtin_source(char **argv); |
| 548 | static int builtin_umask(char **argv); |
| 549 | static int builtin_unset(char **argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 550 | static int builtin_wait(char **argv); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 551 | #if ENABLE_HUSH_LOOPS |
| 552 | static int builtin_break(char **argv); |
| 553 | static int builtin_continue(char **argv); |
| 554 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 555 | |
| 556 | /* Table of built-in functions. They can be forked or not, depending on |
| 557 | * context: within pipes, they fork. As simple commands, they do not. |
| 558 | * When used in non-forking context, they can change global variables |
| 559 | * in the parent shell process. If forked, of course they cannot. |
| 560 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 561 | * still be set at the end. */ |
| 562 | struct built_in_command { |
| 563 | const char *cmd; |
| 564 | int (*function)(char **argv); |
| 565 | #if ENABLE_HUSH_HELP |
| 566 | const char *descr; |
| 567 | #define BLTIN(cmd, func, help) { cmd, func, help } |
| 568 | #else |
| 569 | #define BLTIN(cmd, func, help) { cmd, func } |
| 570 | #endif |
| 571 | }; |
| 572 | |
| 573 | /* For now, echo and test are unconditionally enabled. |
| 574 | * Maybe make it configurable? */ |
| 575 | static const struct built_in_command bltins[] = { |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 576 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 577 | BLTIN(":" , builtin_true , "No-op"), |
| 578 | BLTIN("[" , builtin_test , "Test condition"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 579 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 580 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 581 | #endif |
| 582 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 583 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 584 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 585 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 586 | #if ENABLE_HUSH_LOOPS |
| 587 | BLTIN("continue", builtin_continue, "Start new loop iteration"), |
| 588 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 589 | BLTIN("echo" , builtin_echo , "Write to stdout"), |
| 590 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 591 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 592 | BLTIN("exit" , builtin_exit , "Exit"), |
| 593 | BLTIN("export" , builtin_export , "Set environment variable"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 594 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 595 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 596 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 597 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 598 | BLTIN("help" , builtin_help , "List shell built-in commands"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 599 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 600 | #if ENABLE_HUSH_JOB |
| 601 | BLTIN("jobs" , builtin_jobs , "List active jobs"), |
| 602 | #endif |
| 603 | BLTIN("pwd" , builtin_pwd , "Print current directory"), |
| 604 | BLTIN("read" , builtin_read , "Input environment variable"), |
| 605 | // BLTIN("return" , builtin_return , "Return from a function"), |
| 606 | BLTIN("set" , builtin_set , "Set/unset shell local variables"), |
| 607 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
| 608 | BLTIN("test" , builtin_test , "Test condition"), |
| 609 | BLTIN("trap" , builtin_trap , "Trap signals"), |
| 610 | // BLTIN("ulimit" , builtin_return , "Control resource limits"), |
| 611 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 612 | BLTIN("unset" , builtin_unset , "Unset environment variable"), |
| 613 | BLTIN("wait" , builtin_wait , "Wait for process"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 614 | }; |
| 615 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 616 | |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 617 | static void maybe_die(const char *notice, const char *msg) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 618 | { |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 619 | /* Was using fancy stuff: |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 620 | * (G_interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...) |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 621 | * but it SEGVs. ?! Oh well... explicit temp ptr works around that */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 622 | void FAST_FUNC (*fp)(const char *s, ...) = bb_error_msg_and_die; |
| 623 | #if ENABLE_HUSH_INTERACTIVE |
Mike Frysinger | dfa9de7 | 2009-04-03 22:48:10 +0000 | [diff] [blame] | 624 | if (G_interactive_fd) |
| 625 | fp = bb_error_msg; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 626 | #endif |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 627 | fp(msg ? "%s: %s" : notice, notice, msg); |
| 628 | } |
Mike Frysinger | 5a82845 | 2009-03-28 19:09:04 +0000 | [diff] [blame] | 629 | #if 1 |
| 630 | #define syntax(msg) maybe_die("syntax error", msg); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 631 | #else |
Mike Frysinger | 5a82845 | 2009-03-28 19:09:04 +0000 | [diff] [blame] | 632 | /* Debug -- trick gcc to expand __LINE__ and convert to string */ |
| 633 | #define __syntax(msg, line) maybe_die("syntax error hush.c:" # line, msg) |
| 634 | #define _syntax(msg, line) __syntax(msg, line) |
| 635 | #define syntax(msg) _syntax(msg, __LINE__) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 636 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 637 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 638 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 639 | static int glob_needed(const char *s) |
| 640 | { |
| 641 | while (*s) { |
| 642 | if (*s == '\\') |
| 643 | s++; |
| 644 | if (*s == '*' || *s == '[' || *s == '?') |
| 645 | return 1; |
| 646 | s++; |
| 647 | } |
| 648 | return 0; |
| 649 | } |
| 650 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 651 | static int is_assignment(const char *s) |
| 652 | { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 653 | if (!s || !(isalpha(*s) || *s == '_')) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 654 | return 0; |
| 655 | s++; |
| 656 | while (isalnum(*s) || *s == '_') |
| 657 | s++; |
| 658 | return *s == '='; |
| 659 | } |
| 660 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 661 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 662 | static char *unbackslash(char *src) |
| 663 | { |
| 664 | char *dst = src; |
| 665 | while (1) { |
| 666 | if (*src == '\\') |
| 667 | src++; |
| 668 | if ((*dst++ = *src++) == '\0') |
| 669 | break; |
| 670 | } |
| 671 | return dst; |
| 672 | } |
| 673 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 674 | 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] | 675 | { |
| 676 | int i; |
| 677 | unsigned count1; |
| 678 | unsigned count2; |
| 679 | char **v; |
| 680 | |
| 681 | v = strings; |
| 682 | count1 = 0; |
| 683 | if (v) { |
| 684 | while (*v) { |
| 685 | count1++; |
| 686 | v++; |
| 687 | } |
| 688 | } |
| 689 | count2 = 0; |
| 690 | v = add; |
| 691 | while (*v) { |
| 692 | count2++; |
| 693 | v++; |
| 694 | } |
| 695 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 696 | v[count1 + count2] = NULL; |
| 697 | i = count2; |
| 698 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 699 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 700 | return v; |
| 701 | } |
| 702 | |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 703 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 704 | { |
| 705 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 706 | v[0] = add; |
| 707 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 708 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 709 | } |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 710 | |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 711 | static void putenv_all(char **strings) |
| 712 | { |
| 713 | if (!strings) |
| 714 | return; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 715 | while (*strings) { |
| 716 | debug_printf_env("putenv '%s'\n", *strings); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 717 | putenv(*strings++); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 718 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | static char **putenv_all_and_save_old(char **strings) |
| 722 | { |
| 723 | char **old = NULL; |
| 724 | char **s = strings; |
| 725 | |
| 726 | if (!strings) |
| 727 | return old; |
| 728 | while (*strings) { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 729 | char *v, *eq; |
| 730 | |
| 731 | eq = strchr(*strings, '='); |
| 732 | if (eq) { |
| 733 | *eq = '\0'; |
| 734 | v = getenv(*strings); |
| 735 | *eq = '='; |
| 736 | if (v) { |
| 737 | /* v points to VAL in VAR=VAL, go back to VAR */ |
| 738 | v -= (eq - *strings) + 1; |
| 739 | old = add_string_to_strings(old, v); |
| 740 | } |
| 741 | } |
| 742 | strings++; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 743 | } |
| 744 | putenv_all(s); |
| 745 | return old; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 748 | static void free_strings_and_unsetenv(char **strings, int unset) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 749 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 750 | char **v; |
| 751 | |
| 752 | if (!strings) |
| 753 | return; |
| 754 | |
| 755 | v = strings; |
| 756 | while (*v) { |
| 757 | if (unset) { |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 758 | debug_printf_env("unsetenv '%s'\n", *v); |
| 759 | bb_unsetenv(*v); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 760 | } |
| 761 | free(*v++); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 762 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 763 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 766 | static void free_strings(char **strings) |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 767 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 768 | free_strings_and_unsetenv(strings, 0); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 769 | } |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 770 | |
| 771 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 772 | /* Basic theory of signal handling in shell |
| 773 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 774 | * This does not describe what hush does, rather, it is current understanding |
| 775 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 776 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 777 | * |
| 778 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 779 | * is finished or backgrounded. It is the same in interactive and |
| 780 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 781 | * 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] | 782 | * ^C or ^Z from keyboard seem to execute "at once" because it usually |
| 783 | * backgrounds (i.e. stops) or kills all members of currently running |
| 784 | * pipe. |
| 785 | * |
| 786 | * Wait builtin in interruptible by signals for which user trap is set |
| 787 | * or by SIGINT in interactive shell. |
| 788 | * |
| 789 | * Trap handlers will execute even within trap handlers. (right?) |
| 790 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 791 | * User trap handlers are forgotten when subshell ("(cmd)") is entered. [TODO] |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 792 | * |
| 793 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 794 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 795 | * |
| 796 | * Commands run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 797 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 798 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 799 | * Ordinary commands have signals set to SIG_IGN/DFL set as inherited |
| 800 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 801 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 802 | * Siganls which differ from SIG_DFL action |
| 803 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 804 | * |
| 805 | * SIGQUIT: ignore |
| 806 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 807 | * SIGHUP (interactive): |
| 808 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 809 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 810 | * (note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 811 | * that all pipe members are stopped) (right?) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 812 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 813 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 814 | * to interactive shell while shell is waiting for a pipe, |
| 815 | * since shell is bg'ed (is not in foreground process group). |
| 816 | * (check/expand this) |
| 817 | * Example 1: this waits 5 sec, but does not execute ls: |
| 818 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 819 | * Example 2: this does not wait and does not execute ls: |
| 820 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 821 | * Example 3: this does not wait 5 sec, but executes ls: |
| 822 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 823 | * |
| 824 | * (What happens to signals which are IGN on shell start?) |
| 825 | * (What happens with signal mask on shell start?) |
| 826 | * |
| 827 | * Implementation in hush |
| 828 | * ====================== |
| 829 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 830 | * We block all signals which we don't want to take action immediately, |
| 831 | * i.e. we block all signals which need to have special handling as described |
| 832 | * above, and all signals which have traps set. |
| 833 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 834 | * and act on them. |
| 835 | * |
| 836 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 837 | * sigset_t blocked_set: current blocked signal set |
| 838 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 839 | * "trap - SIGxxx": |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 840 | * 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] | 841 | * "trap 'cmd' SIGxxx": |
| 842 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 843 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 844 | * nothing for {} child shell (say, "true | { true; true; } | true") |
| 845 | * unset all traps if () shell. [TODO] |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 846 | * after [v]fork, if we plan to exec: |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 847 | * POSIX says pending signal mask is cleared in child - no need to clear it. |
| 848 | * 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] | 849 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 850 | * Note: as a result, we do not use signal handlers much. The only uses |
| 851 | * are to count SIGCHLDs [disabled - bug somewhere, + bloat] |
| 852 | * and to restore tty pgrp on signal-induced exit. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 853 | */ |
| 854 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 855 | //static void SIGCHLD_handler(int sig UNUSED_PARAM) |
| 856 | //{ |
| 857 | // G.count_SIGCHLD++; |
| 858 | //} |
| 859 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 860 | static int check_and_run_traps(int sig) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 861 | { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 862 | static const struct timespec zero_timespec = { 0, 0 }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 863 | smalluint save_rcode; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 864 | int last_sig = 0; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 865 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 866 | if (sig) |
| 867 | goto jump_in; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 868 | while (1) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 869 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_timespec); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 870 | if (sig <= 0) |
| 871 | break; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 872 | jump_in: |
| 873 | last_sig = sig; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 874 | if (G.traps && G.traps[sig]) { |
| 875 | if (G.traps[sig][0]) { |
| 876 | /* We have user-defined handler */ |
| 877 | char *argv[] = { NULL, xstrdup(G.traps[sig]), NULL }; |
| 878 | save_rcode = G.last_return_code; |
| 879 | builtin_eval(argv); |
| 880 | free(argv[1]); |
| 881 | G.last_return_code = save_rcode; |
| 882 | } /* else: "" trap, ignoring signal */ |
| 883 | continue; |
| 884 | } |
| 885 | /* not a trap: special action */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 886 | switch (sig) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 887 | // case SIGCHLD: |
| 888 | // G.count_SIGCHLD++; |
| 889 | // break; |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 890 | case SIGINT: |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 891 | bb_putchar('\n'); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 892 | G.flag_SIGINT = 1; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 893 | break; |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 894 | //TODO |
| 895 | // case SIGHUP: ... |
| 896 | // break; |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame^] | 897 | default: /* ignored: */ |
| 898 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 899 | break; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 900 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 901 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 902 | return last_sig; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 905 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 906 | /* Restores tty foreground process group, and exits. |
| 907 | * May be called as signal handler for fatal signal |
| 908 | * (will faithfully resend signal to itself, producing correct exit state) |
| 909 | * or called directly with -EXITCODE. |
| 910 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 911 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 912 | static void sigexit(int sig) |
| 913 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 914 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 915 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 916 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 917 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 918 | * tty pgrp then, only top-level shell process does that */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 919 | if (G_interactive_fd && getpid() == G.root_pid) |
| 920 | tcsetpgrp(G_interactive_fd, G.saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 921 | |
| 922 | /* Not a signal, just exit */ |
| 923 | if (sig <= 0) |
| 924 | _exit(- sig); |
| 925 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 926 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 927 | } |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 928 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 929 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 930 | /* Restores tty foreground process group, and exits. */ |
| 931 | static void hush_exit(int exitcode) NORETURN; |
| 932 | static void hush_exit(int exitcode) |
| 933 | { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 934 | if (G.traps && G.traps[0] && G.traps[0][0]) { |
| 935 | char *argv[] = { NULL, xstrdup(G.traps[0]), NULL }; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 936 | //TODO: do we need to prevent recursion? |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 937 | builtin_eval(argv); |
| 938 | free(argv[1]); |
| 939 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 940 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 941 | #if ENABLE_HUSH_JOB |
| 942 | fflush(NULL); /* flush all streams */ |
| 943 | sigexit(- (exitcode & 0xff)); |
| 944 | #else |
| 945 | exit(exitcode); |
| 946 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 949 | |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 950 | static const char *set_cwd(void) |
| 951 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 952 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 953 | * we must not try to free(bb_msg_unknown) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 954 | if (G.cwd == bb_msg_unknown) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 955 | G.cwd = NULL; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 956 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 957 | if (!G.cwd) |
| 958 | G.cwd = bb_msg_unknown; |
| 959 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 962 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 963 | /* Get/check local shell variables */ |
| 964 | static struct variable *get_local_var(const char *name) |
| 965 | { |
| 966 | struct variable *cur; |
| 967 | int len; |
| 968 | |
| 969 | if (!name) |
| 970 | return NULL; |
| 971 | len = strlen(name); |
| 972 | for (cur = G.top_var; cur; cur = cur->next) { |
| 973 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
| 974 | return cur; |
| 975 | } |
| 976 | return NULL; |
| 977 | } |
| 978 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 979 | static const char *get_local_var_value(const char *src) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 980 | { |
| 981 | struct variable *var = get_local_var(src); |
| 982 | if (var) |
| 983 | return strchr(var->varstr, '=') + 1; |
| 984 | return NULL; |
| 985 | } |
| 986 | |
| 987 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 988 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 989 | * flg_export: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 990 | * 0: do not export |
| 991 | * 1: export |
| 992 | * -1: if NAME is set, leave export status alone |
| 993 | * if NAME is not set, do not export |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 994 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 995 | */ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 996 | #if BB_MMU |
| 997 | #define set_local_var(str, flg_export, flg_read_only) \ |
| 998 | set_local_var(str, flg_export) |
| 999 | #endif |
| 1000 | static int set_local_var(char *str, int flg_export, int flg_read_only) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1001 | { |
| 1002 | struct variable *cur; |
| 1003 | char *value; |
| 1004 | int name_len; |
| 1005 | |
| 1006 | value = strchr(str, '='); |
| 1007 | if (!value) { /* not expected to ever happen? */ |
| 1008 | free(str); |
| 1009 | return -1; |
| 1010 | } |
| 1011 | |
| 1012 | name_len = value - str + 1; /* including '=' */ |
| 1013 | cur = G.top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */ |
| 1014 | while (1) { |
| 1015 | if (strncmp(cur->varstr, str, name_len) != 0) { |
| 1016 | if (!cur->next) { |
| 1017 | /* Bail out. Note that now cur points |
| 1018 | * to last var in linked list */ |
| 1019 | break; |
| 1020 | } |
| 1021 | cur = cur->next; |
| 1022 | continue; |
| 1023 | } |
| 1024 | /* We found an existing var with this name */ |
| 1025 | *value = '\0'; |
| 1026 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1027 | #if !BB_MMU |
| 1028 | if (!flg_read_only) |
| 1029 | #endif |
| 1030 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1031 | free(str); |
| 1032 | return -1; |
| 1033 | } |
| 1034 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1035 | unsetenv(str); /* just in case */ |
| 1036 | *value = '='; |
| 1037 | if (strcmp(cur->varstr, str) == 0) { |
| 1038 | free_and_exp: |
| 1039 | free(str); |
| 1040 | goto exp; |
| 1041 | } |
| 1042 | if (cur->max_len >= strlen(str)) { |
| 1043 | /* This one is from startup env, reuse space */ |
| 1044 | strcpy(cur->varstr, str); |
| 1045 | goto free_and_exp; |
| 1046 | } |
| 1047 | /* max_len == 0 signifies "malloced" var, which we can |
| 1048 | * (and has to) free */ |
| 1049 | if (!cur->max_len) |
| 1050 | free(cur->varstr); |
| 1051 | cur->max_len = 0; |
| 1052 | goto set_str_and_exp; |
| 1053 | } |
| 1054 | |
| 1055 | /* Not found - create next variable struct */ |
| 1056 | cur->next = xzalloc(sizeof(*cur)); |
| 1057 | cur = cur->next; |
| 1058 | |
| 1059 | set_str_and_exp: |
| 1060 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1061 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1062 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1063 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1064 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1065 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1066 | cur->flg_export = 1; |
| 1067 | if (cur->flg_export) { |
| 1068 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1069 | return putenv(cur->varstr); |
| 1070 | } |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1074 | static int unset_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1075 | { |
| 1076 | struct variable *cur; |
| 1077 | struct variable *prev = prev; /* for gcc */ |
| 1078 | int name_len; |
| 1079 | |
| 1080 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1081 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1082 | name_len = strlen(name); |
| 1083 | cur = G.top_var; |
| 1084 | while (cur) { |
| 1085 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1086 | if (cur->flg_read_only) { |
| 1087 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1088 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1089 | } |
| 1090 | /* prev is ok to use here because 1st variable, HUSH_VERSION, |
| 1091 | * is ro, and we cannot reach this code on the 1st pass */ |
| 1092 | prev->next = cur->next; |
| 1093 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1094 | bb_unsetenv(cur->varstr); |
| 1095 | if (!cur->max_len) |
| 1096 | free(cur->varstr); |
| 1097 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1098 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1099 | } |
| 1100 | prev = cur; |
| 1101 | cur = cur->next; |
| 1102 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1103 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1106 | #if ENABLE_SH_MATH_SUPPORT |
| 1107 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1108 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1109 | static char *endofname(const char *name) |
| 1110 | { |
| 1111 | char *p; |
| 1112 | |
| 1113 | p = (char *) name; |
| 1114 | if (!is_name(*p)) |
| 1115 | return p; |
| 1116 | while (*++p) { |
| 1117 | if (!is_in_name(*p)) |
| 1118 | break; |
| 1119 | } |
| 1120 | return p; |
| 1121 | } |
| 1122 | |
| 1123 | static void arith_set_local_var(const char *name, const char *val, int flags) |
| 1124 | { |
| 1125 | /* arith code doesnt malloc space, so do it for it */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1126 | char *var = xasprintf("%s=%s", name, val); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1127 | set_local_var(var, flags, 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1128 | } |
| 1129 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1130 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1131 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1132 | /* |
| 1133 | * in_str support |
| 1134 | */ |
| 1135 | static int static_get(struct in_str *i) |
| 1136 | { |
| 1137 | int ch = *i->p++; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1138 | if (ch != '\0') |
| 1139 | return ch; |
| 1140 | i->p--; |
| 1141 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | static int static_peek(struct in_str *i) |
| 1145 | { |
| 1146 | return *i->p; |
| 1147 | } |
| 1148 | |
| 1149 | #if ENABLE_HUSH_INTERACTIVE |
| 1150 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1151 | static void cmdedit_set_initial_prompt(void) |
| 1152 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1153 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1154 | G.PS1 = getenv("PS1"); |
| 1155 | if (G.PS1 == NULL) |
| 1156 | G.PS1 = "\\w \\$ "; |
| 1157 | } else |
| 1158 | G.PS1 = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1159 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1160 | |
| 1161 | static const char* setup_prompt_string(int promptmode) |
| 1162 | { |
| 1163 | const char *prompt_str; |
| 1164 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1165 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1166 | /* Set up the prompt */ |
| 1167 | if (promptmode == 0) { /* PS1 */ |
| 1168 | free((char*)G.PS1); |
| 1169 | G.PS1 = xasprintf("%s %c ", G.cwd, (geteuid() != 0) ? '$' : '#'); |
| 1170 | prompt_str = G.PS1; |
| 1171 | } else |
| 1172 | prompt_str = G.PS2; |
| 1173 | } else |
| 1174 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1175 | debug_printf("result '%s'\n", prompt_str); |
| 1176 | return prompt_str; |
| 1177 | } |
| 1178 | |
| 1179 | static void get_user_input(struct in_str *i) |
| 1180 | { |
| 1181 | int r; |
| 1182 | const char *prompt_str; |
| 1183 | |
| 1184 | prompt_str = setup_prompt_string(i->promptmode); |
| 1185 | #if ENABLE_FEATURE_EDITING |
| 1186 | /* Enable command line editing only while a command line |
| 1187 | * is actually being read */ |
| 1188 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1189 | G.flag_SIGINT = 0; |
| 1190 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1191 | * only after <Enter>. (^C will work) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1192 | 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] | 1193 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1194 | check_and_run_traps(0); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1195 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1196 | i->eof_flag = (r < 0); |
| 1197 | if (i->eof_flag) { /* EOF/error detected */ |
| 1198 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1199 | G.user_input_buf[1] = '\0'; |
| 1200 | } |
| 1201 | #else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1202 | do { |
| 1203 | G.flag_SIGINT = 0; |
| 1204 | fputs(prompt_str, stdout); |
| 1205 | fflush(stdout); |
| 1206 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1207 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1208 | //do we need check_and_run_traps(0)? (maybe only if stdin) |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1209 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1210 | i->eof_flag = (r == EOF); |
| 1211 | #endif |
| 1212 | i->p = G.user_input_buf; |
| 1213 | } |
| 1214 | |
| 1215 | #endif /* INTERACTIVE */ |
| 1216 | |
| 1217 | /* This is the magic location that prints prompts |
| 1218 | * and gets data back from the user */ |
| 1219 | static int file_get(struct in_str *i) |
| 1220 | { |
| 1221 | int ch; |
| 1222 | |
| 1223 | /* If there is data waiting, eat it up */ |
| 1224 | if (i->p && *i->p) { |
| 1225 | #if ENABLE_HUSH_INTERACTIVE |
| 1226 | take_cached: |
| 1227 | #endif |
| 1228 | ch = *i->p++; |
| 1229 | if (i->eof_flag && !*i->p) |
| 1230 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1231 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1232 | } else { |
| 1233 | /* need to double check i->file because we might be doing something |
| 1234 | * more complicated by now, like sourcing or substituting. */ |
| 1235 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 1236 | if (G_interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1237 | do { |
| 1238 | get_user_input(i); |
| 1239 | } while (!*i->p); /* need non-empty line */ |
| 1240 | i->promptmode = 1; /* PS2 */ |
| 1241 | i->promptme = 0; |
| 1242 | goto take_cached; |
| 1243 | } |
| 1244 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1245 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1246 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1247 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1248 | #if ENABLE_HUSH_INTERACTIVE |
| 1249 | if (ch == '\n') |
| 1250 | i->promptme = 1; |
| 1251 | #endif |
| 1252 | return ch; |
| 1253 | } |
| 1254 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1255 | /* All callers guarantee this routine will never |
| 1256 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1257 | */ |
| 1258 | static int file_peek(struct in_str *i) |
| 1259 | { |
| 1260 | int ch; |
| 1261 | if (i->p && *i->p) { |
| 1262 | if (i->eof_flag && !i->p[1]) |
| 1263 | return EOF; |
| 1264 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1265 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1266 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1267 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1268 | i->eof_flag = (ch == EOF); |
| 1269 | i->peek_buf[0] = ch; |
| 1270 | i->peek_buf[1] = '\0'; |
| 1271 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1272 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1273 | return ch; |
| 1274 | } |
| 1275 | |
| 1276 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1277 | { |
| 1278 | i->peek = file_peek; |
| 1279 | i->get = file_get; |
| 1280 | #if ENABLE_HUSH_INTERACTIVE |
| 1281 | i->promptme = 1; |
| 1282 | i->promptmode = 0; /* PS1 */ |
| 1283 | #endif |
| 1284 | i->file = f; |
| 1285 | i->p = NULL; |
| 1286 | } |
| 1287 | |
| 1288 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1289 | { |
| 1290 | i->peek = static_peek; |
| 1291 | i->get = static_get; |
| 1292 | #if ENABLE_HUSH_INTERACTIVE |
| 1293 | i->promptme = 1; |
| 1294 | i->promptmode = 0; /* PS1 */ |
| 1295 | #endif |
| 1296 | i->p = s; |
| 1297 | i->eof_flag = 0; |
| 1298 | } |
| 1299 | |
| 1300 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1301 | /* |
| 1302 | * o_string support |
| 1303 | */ |
| 1304 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1305 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1306 | static void o_reset(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1307 | { |
| 1308 | o->length = 0; |
| 1309 | o->nonnull = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1310 | if (o->data) |
| 1311 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1314 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1315 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1316 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1317 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1320 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 1321 | { |
| 1322 | free(o->data); |
| 1323 | } |
| 1324 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1325 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1326 | { |
| 1327 | if (o->length + len > o->maxlen) { |
| 1328 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 1329 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 1330 | } |
| 1331 | } |
| 1332 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1333 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1334 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1335 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1336 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1337 | o->data[o->length] = ch; |
| 1338 | o->length++; |
| 1339 | o->data[o->length] = '\0'; |
| 1340 | } |
| 1341 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1342 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1343 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1344 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1345 | memcpy(&o->data[o->length], str, len); |
| 1346 | o->length += len; |
| 1347 | o->data[o->length] = '\0'; |
| 1348 | } |
| 1349 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1350 | #if !BB_MMU |
| 1351 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1352 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1353 | o_addblock(o, str, strlen(str)); |
| 1354 | } |
| 1355 | #endif |
| 1356 | |
| 1357 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 1358 | { |
| 1359 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1362 | 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] | 1363 | { |
| 1364 | while (len) { |
| 1365 | o_addchr(o, *str); |
| 1366 | if (*str++ == '\\' |
| 1367 | && (*str != '*' && *str != '?' && *str != '[') |
| 1368 | ) { |
| 1369 | o_addchr(o, '\\'); |
| 1370 | } |
| 1371 | len--; |
| 1372 | } |
| 1373 | } |
| 1374 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1375 | /* My analysis of quoting semantics tells me that state information |
| 1376 | * is associated with a destination, not a source. |
| 1377 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1378 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1379 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1380 | int sz = 1; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1381 | char *found = strchr("*?[\\", ch); |
| 1382 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1383 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1384 | o_grow_by(o, sz); |
| 1385 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1386 | o->data[o->length] = '\\'; |
| 1387 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1388 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1389 | o->data[o->length] = ch; |
| 1390 | o->length++; |
| 1391 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1394 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1395 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1396 | int sz = 1; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1397 | if (o->o_escape && strchr("*?[\\", ch)) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1398 | sz++; |
| 1399 | o->data[o->length] = '\\'; |
| 1400 | o->length++; |
| 1401 | } |
| 1402 | o_grow_by(o, sz); |
| 1403 | o->data[o->length] = ch; |
| 1404 | o->length++; |
| 1405 | o->data[o->length] = '\0'; |
| 1406 | } |
| 1407 | |
| 1408 | static void o_addQstr(o_string *o, const char *str, int len) |
| 1409 | { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1410 | if (!o->o_escape) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1411 | o_addblock(o, str, len); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1412 | return; |
| 1413 | } |
| 1414 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1415 | char ch; |
| 1416 | int sz; |
| 1417 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 1418 | if (ordinary_cnt > len) /* paranoia */ |
| 1419 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1420 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1421 | if (ordinary_cnt == len) |
| 1422 | return; |
| 1423 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1424 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1425 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1426 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1427 | sz = 1; |
| 1428 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 1429 | sz++; |
| 1430 | o->data[o->length] = '\\'; |
| 1431 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1432 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1433 | o_grow_by(o, sz); |
| 1434 | o->data[o->length] = ch; |
| 1435 | o->length++; |
| 1436 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1437 | } |
| 1438 | } |
| 1439 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1440 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1441 | * 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] | 1442 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1443 | * list[i] contains an INDEX (int!) into this string data. |
| 1444 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1445 | * but list[i]'s need not be modified. |
| 1446 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1447 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1448 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1449 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1450 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1451 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1452 | { |
| 1453 | char **list = (char**)o->data; |
| 1454 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1455 | int i = 0; |
| 1456 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1457 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1458 | while (i < n) { |
| 1459 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1460 | o->data + (int)list[i] + string_start, |
| 1461 | o->data + (int)list[i] + string_start); |
| 1462 | i++; |
| 1463 | } |
| 1464 | if (n) { |
| 1465 | const char *p = o->data + (int)list[n - 1] + string_start; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1466 | 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] | 1467 | } |
| 1468 | } |
| 1469 | #else |
| 1470 | #define debug_print_list(prefix, o, n) ((void)0) |
| 1471 | #endif |
| 1472 | |
| 1473 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1474 | * in list[n] so that it points past last stored byte so far. |
| 1475 | * It returns n+1. */ |
| 1476 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1477 | { |
| 1478 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1479 | int string_start; |
| 1480 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1481 | |
| 1482 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1483 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1484 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1485 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1486 | 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] | 1487 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1488 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1489 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1490 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1491 | memmove(list + n + 0x10, list + n, string_len); |
| 1492 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1493 | } else { |
| 1494 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 1495 | n, string_len, string_start); |
| 1496 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1497 | } else { |
| 1498 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1499 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1500 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 1501 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 1502 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1503 | o->has_empty_slot = 0; |
| 1504 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1505 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1506 | return n + 1; |
| 1507 | } |
| 1508 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1509 | /* "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] | 1510 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1511 | { |
| 1512 | char **list = (char**)o->data; |
| 1513 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1514 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1515 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1518 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1519 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1520 | static int o_glob(o_string *o, int n) |
| 1521 | { |
| 1522 | glob_t globdata; |
| 1523 | int gr; |
| 1524 | char *pattern; |
| 1525 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1526 | 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] | 1527 | if (!o->data) |
| 1528 | return o_save_ptr_helper(o, n); |
| 1529 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1530 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1531 | if (!glob_needed(pattern)) { |
| 1532 | literal: |
| 1533 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1534 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1535 | return o_save_ptr_helper(o, n); |
| 1536 | } |
| 1537 | |
| 1538 | memset(&globdata, 0, sizeof(globdata)); |
| 1539 | gr = glob(pattern, 0, NULL, &globdata); |
| 1540 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 1541 | if (gr == GLOB_NOSPACE) |
| 1542 | bb_error_msg_and_die("out of memory during glob"); |
| 1543 | if (gr == GLOB_NOMATCH) { |
| 1544 | globfree(&globdata); |
| 1545 | goto literal; |
| 1546 | } |
| 1547 | if (gr != 0) { /* GLOB_ABORTED ? */ |
| 1548 | //TODO: testcase for bad glob pattern behavior |
| 1549 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 1550 | } |
| 1551 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 1552 | char **argv = globdata.gl_pathv; |
| 1553 | o->length = pattern - o->data; /* "forget" pattern */ |
| 1554 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1555 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1556 | n = o_save_ptr_helper(o, n); |
| 1557 | argv++; |
| 1558 | if (!*argv) |
| 1559 | break; |
| 1560 | } |
| 1561 | } |
| 1562 | globfree(&globdata); |
| 1563 | if (DEBUG_GLOB) |
| 1564 | debug_print_list("o_glob returning", o, n); |
| 1565 | return n; |
| 1566 | } |
| 1567 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1568 | /* If o->o_glob == 1, glob the string so far remembered. |
| 1569 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1570 | static int o_save_ptr(o_string *o, int n) |
| 1571 | { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 1572 | if (o->o_glob) { /* if globbing is requested */ |
| 1573 | /* If o->has_empty_slot, list[n] was already globbed |
| 1574 | * (if it was requested back then when it was filled) |
| 1575 | * so don't do that again! */ |
| 1576 | if (!o->has_empty_slot) |
| 1577 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 1578 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1579 | return o_save_ptr_helper(o, n); |
| 1580 | } |
| 1581 | |
| 1582 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1583 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1584 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1585 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1586 | int string_start; |
| 1587 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1588 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 1589 | if (DEBUG_EXPAND) |
| 1590 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1591 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1592 | list = (char**)o->data; |
| 1593 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1594 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1595 | while (n) { |
| 1596 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1597 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1598 | } |
| 1599 | return list; |
| 1600 | } |
| 1601 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1602 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1603 | /* Expansion can recurse */ |
| 1604 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 1605 | static int process_command_subs(o_string *dest, const char *s); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1606 | #endif |
| 1607 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1608 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 1609 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1610 | parse_stream_dquoted(dest, input, dquote_end) |
| 1611 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 1612 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1613 | o_string *dest, |
| 1614 | struct in_str *input, |
| 1615 | int dquote_end); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1616 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1617 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 1618 | * all variable references within and returns a pointer to |
| 1619 | * a list of expanded strings, possibly with larger number |
| 1620 | * of strings. (Think VAR="a b"; echo $VAR). |
| 1621 | * This new list is allocated as a single malloc block. |
| 1622 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 1623 | * followed by strings themself. |
| 1624 | * Caller can deallocate entire list by single free(list). */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1625 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1626 | /* Store given string, finalizing the word and starting new one whenever |
| 1627 | * we encounter IFS char(s). This is used for expanding variable values. |
| 1628 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 1629 | 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] | 1630 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1631 | while (1) { |
| 1632 | int word_len = strcspn(str, G.ifs); |
| 1633 | if (word_len) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1634 | if (output->o_escape || !output->o_glob) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1635 | o_addQstr(output, str, word_len); |
| 1636 | else /* protect backslashes against globbing up :) */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1637 | o_addblock_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1638 | str += word_len; |
| 1639 | } |
| 1640 | if (!*str) /* EOL - do not finalize word */ |
| 1641 | break; |
| 1642 | o_addchr(output, '\0'); |
| 1643 | debug_print_list("expand_on_ifs", output, n); |
| 1644 | n = o_save_ptr(output, n); |
| 1645 | str += strspn(str, G.ifs); /* skip ifs chars */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1646 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1647 | debug_print_list("expand_on_ifs[1]", output, n); |
| 1648 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1651 | /* Expand all variable references in given string, adding words to list[] |
| 1652 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 1653 | * to be filled). This routine is extremely tricky: has to deal with |
| 1654 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 1655 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
| 1656 | 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] | 1657 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1658 | /* or_mask is either 0 (normal case) or 0x80 |
| 1659 | * (expansion of right-hand side of assignment == 1-element expand. |
| 1660 | * It will also do no globbing, and thus we must not backslash-quote!) */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1661 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1662 | char first_ch, ored_ch; |
| 1663 | int i; |
| 1664 | const char *val; |
| 1665 | char *p; |
| 1666 | |
| 1667 | ored_ch = 0; |
| 1668 | |
| 1669 | debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg); |
| 1670 | debug_print_list("expand_vars_to_list", output, n); |
| 1671 | n = o_save_ptr(output, n); |
| 1672 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 1673 | |
| 1674 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 1675 | #if ENABLE_HUSH_TICK |
| 1676 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1677 | #endif |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1678 | #if ENABLE_SH_MATH_SUPPORT |
| 1679 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 1680 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1681 | o_addblock(output, arg, p - arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1682 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 1683 | arg = ++p; |
| 1684 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 1685 | |
| 1686 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
| 1687 | /* "$@" is special. Even if quoted, it can still |
| 1688 | * expand to nothing (not even an empty string) */ |
| 1689 | if ((first_ch & 0x7f) != '@') |
| 1690 | ored_ch |= first_ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1691 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1692 | val = NULL; |
| 1693 | switch (first_ch & 0x7f) { |
| 1694 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 1695 | case '$': /* pid */ |
| 1696 | val = utoa(G.root_pid); |
| 1697 | break; |
| 1698 | case '!': /* bg pid */ |
| 1699 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
| 1700 | break; |
| 1701 | case '?': /* exitcode */ |
| 1702 | val = utoa(G.last_return_code); |
| 1703 | break; |
| 1704 | case '#': /* argc */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1705 | if (arg[1] != SPECIAL_VAR_SYMBOL) |
| 1706 | /* actually, it's a ${#var} */ |
| 1707 | goto case_default; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1708 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 1709 | break; |
| 1710 | case '*': |
| 1711 | case '@': |
| 1712 | i = 1; |
| 1713 | if (!G.global_argv[i]) |
| 1714 | break; |
| 1715 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
| 1716 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1717 | smallint sv = output->o_escape; |
| 1718 | /* unquoted var's contents should be globbed, so don't escape */ |
| 1719 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1720 | while (G.global_argv[i]) { |
| 1721 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 1722 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 1723 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 1724 | /* this argv[] is not empty and not last: |
| 1725 | * put terminating NUL, start new word */ |
| 1726 | o_addchr(output, '\0'); |
| 1727 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 1728 | n = o_save_ptr(output, n); |
| 1729 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 1730 | } |
| 1731 | } |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1732 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1733 | } else |
| 1734 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
| 1735 | * and in this case should treat it like '$*' - see 'else...' below */ |
| 1736 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
| 1737 | while (1) { |
| 1738 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 1739 | if (++i >= G.global_argc) |
| 1740 | break; |
| 1741 | o_addchr(output, '\0'); |
| 1742 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 1743 | n = o_save_ptr(output, n); |
| 1744 | } |
| 1745 | } else { /* quoted $*: add as one word */ |
| 1746 | while (1) { |
| 1747 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 1748 | if (!G.global_argv[++i]) |
| 1749 | break; |
| 1750 | if (G.ifs[0]) |
| 1751 | o_addchr(output, G.ifs[0]); |
| 1752 | } |
| 1753 | } |
| 1754 | break; |
| 1755 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 1756 | /* "Empty variable", used to make "" etc to not disappear */ |
| 1757 | arg++; |
| 1758 | ored_ch = 0x80; |
| 1759 | break; |
| 1760 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 1761 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1762 | *p = '\0'; |
| 1763 | arg++; |
| 1764 | //TODO: can we just stuff it into "output" directly? |
| 1765 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 1766 | process_command_subs(&subst_result, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1767 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
| 1768 | val = subst_result.data; |
| 1769 | goto store_val; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1770 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1771 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1772 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1773 | arith_eval_hooks_t hooks; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1774 | arith_t res; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1775 | int errcode; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1776 | char *exp_str; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1777 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1778 | arg++; /* skip '+' */ |
| 1779 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1780 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1781 | |
| 1782 | /* Optional: skip expansion if expr is simple ("a + 3", "i++" etc) */ |
| 1783 | exp_str = arg; |
| 1784 | while (1) { |
| 1785 | unsigned char c = *exp_str++; |
| 1786 | if (c == '\0') { |
| 1787 | exp_str = NULL; |
| 1788 | goto skip_expand; |
| 1789 | } |
| 1790 | if (isdigit(c)) |
| 1791 | continue; |
| 1792 | if (strchr(" \t+-*/%_", c) != NULL) |
| 1793 | continue; |
| 1794 | c |= 0x20; /* tolower */ |
| 1795 | if (c >= 'a' && c <= 'z') |
| 1796 | continue; |
| 1797 | break; |
| 1798 | } |
| 1799 | /* We need to expand. Example: "echo $(($a + 1)) $((1 + $((2)) ))" */ |
| 1800 | { |
| 1801 | struct in_str input; |
| 1802 | o_string dest = NULL_O_STRING; |
| 1803 | |
| 1804 | setup_string_in_str(&input, arg); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1805 | parse_stream_dquoted(NULL, &dest, &input, EOF); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1806 | //bb_error_msg("'%s' -> '%s'", arg, dest.data); |
| 1807 | exp_str = expand_string_to_string(dest.data); |
| 1808 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 1809 | o_free(&dest); |
| 1810 | } |
| 1811 | skip_expand: |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 1812 | hooks.lookupvar = get_local_var_value; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1813 | hooks.setvar = arith_set_local_var; |
| 1814 | hooks.endofname = endofname; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1815 | res = arith(exp_str ? exp_str : arg, &errcode, &hooks); |
| 1816 | free(exp_str); |
| 1817 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1818 | if (errcode < 0) { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1819 | switch (errcode) { |
| 1820 | case -3: maybe_die("arith", "exponent less than 0"); break; |
| 1821 | case -2: maybe_die("arith", "divide by zero"); break; |
| 1822 | case -5: maybe_die("arith", "expression recursion loop detected"); break; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1823 | default: maybe_die("arith", "syntax error"); break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1824 | } |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1825 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1826 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1827 | sprintf(arith_buf, arith_t_fmt, res); |
| 1828 | val = arith_buf; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1829 | break; |
| 1830 | } |
| 1831 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1832 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1833 | case_default: { |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 1834 | bool exp_len = false; |
| 1835 | bool exp_null = false; |
| 1836 | char *var = arg; |
| 1837 | char exp_save = exp_save; /* for compiler */ |
| 1838 | char exp_op = exp_op; /* for compiler */ |
| 1839 | char *exp_word = exp_word; /* for compiler */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1840 | size_t exp_off = 0; |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 1841 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1842 | *p = '\0'; |
| 1843 | arg[0] = first_ch & 0x7f; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1844 | |
| 1845 | /* prepare for expansions */ |
| 1846 | if (var[0] == '#') { |
| 1847 | /* handle length expansion ${#var} */ |
| 1848 | exp_len = true; |
| 1849 | ++var; |
| 1850 | } else { |
| 1851 | /* maybe handle parameter expansion */ |
| 1852 | exp_off = strcspn(var, ":-=+?"); |
| 1853 | if (!var[exp_off]) |
| 1854 | exp_off = 0; |
| 1855 | if (exp_off) { |
| 1856 | exp_save = var[exp_off]; |
| 1857 | exp_null = exp_save == ':'; |
| 1858 | exp_word = var + exp_off; |
| 1859 | if (exp_null) ++exp_word; |
| 1860 | exp_op = *exp_word++; |
| 1861 | var[exp_off] = '\0'; |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | /* lookup the variable in question */ |
| 1866 | if (isdigit(var[0])) { |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 1867 | /* handle_dollar() should have vetted var for us */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1868 | i = xatoi_u(var); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1869 | if (i < G.global_argc) |
| 1870 | val = G.global_argv[i]; |
| 1871 | /* else val remains NULL: $N with too big N */ |
| 1872 | } else |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 1873 | val = get_local_var_value(var); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1874 | |
| 1875 | /* handle any expansions */ |
| 1876 | if (exp_len) { |
| 1877 | debug_printf_expand("expand: length of '%s' = ", val); |
| 1878 | val = utoa(val ? strlen(val) : 0); |
| 1879 | debug_printf_expand("%s\n", val); |
| 1880 | } else if (exp_off) { |
| 1881 | /* we need to do an expansion */ |
| 1882 | int exp_test = (!val || (exp_null && !val[0])); |
| 1883 | if (exp_op == '+') |
| 1884 | exp_test = !exp_test; |
| 1885 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 1886 | exp_null ? "true" : "false", exp_test); |
| 1887 | if (exp_test) { |
| 1888 | if (exp_op == '?') |
| 1889 | maybe_die(var, *exp_word ? exp_word : "parameter null or not set"); |
| 1890 | else |
| 1891 | val = exp_word; |
| 1892 | |
| 1893 | if (exp_op == '=') { |
| 1894 | if (isdigit(var[0]) || var[0] == '#') { |
| 1895 | maybe_die(var, "special vars cannot assign in this way"); |
| 1896 | val = NULL; |
| 1897 | } else { |
| 1898 | char *new_var = xmalloc(strlen(var) + strlen(val) + 2); |
| 1899 | sprintf(new_var, "%s=%s", var, val); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1900 | set_local_var(new_var, -1, 0); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1901 | } |
| 1902 | } |
| 1903 | } |
| 1904 | var[exp_off] = exp_save; |
| 1905 | } |
| 1906 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1907 | arg[0] = first_ch; |
| 1908 | #if ENABLE_HUSH_TICK |
| 1909 | store_val: |
| 1910 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1911 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1912 | 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] | 1913 | if (val) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1914 | /* unquoted var's contents should be globbed, so don't escape */ |
| 1915 | smallint sv = output->o_escape; |
| 1916 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1917 | n = expand_on_ifs(output, n, val); |
| 1918 | val = NULL; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1919 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1920 | } |
| 1921 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1922 | 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] | 1923 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1924 | } /* default: */ |
| 1925 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1926 | if (val) { |
| 1927 | o_addQstr(output, val, strlen(val)); |
| 1928 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1929 | /* Do the check to avoid writing to a const string */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 1930 | if (*p != SPECIAL_VAR_SYMBOL) |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1931 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1932 | |
| 1933 | #if ENABLE_HUSH_TICK |
| 1934 | o_free(&subst_result); |
| 1935 | #endif |
| 1936 | arg = ++p; |
| 1937 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 1938 | |
| 1939 | if (arg[0]) { |
| 1940 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 1941 | /* this part is literal, and it was already pre-quoted |
| 1942 | * if needed (much earlier), do not use o_addQstr here! */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1943 | o_addstr_with_NUL(output, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1944 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 1945 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 1946 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 1947 | ) { |
| 1948 | n--; |
| 1949 | /* allow to reuse list[n] later without re-growth */ |
| 1950 | output->has_empty_slot = 1; |
| 1951 | } else { |
| 1952 | o_addchr(output, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1953 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1954 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1957 | static char **expand_variables(char **argv, int or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1958 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1959 | int n; |
| 1960 | char **list; |
| 1961 | char **v; |
| 1962 | o_string output = NULL_O_STRING; |
| 1963 | |
| 1964 | if (or_mask & 0x100) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 1965 | output.o_escape = 1; /* protect against globbing for "$var" */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1966 | /* (unquoted $var will temporarily switch it off) */ |
| 1967 | output.o_glob = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1968 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1969 | |
| 1970 | n = 0; |
| 1971 | v = argv; |
| 1972 | while (*v) { |
| 1973 | n = expand_vars_to_list(&output, n, *v, (char)or_mask); |
| 1974 | v++; |
| 1975 | } |
| 1976 | debug_print_list("expand_variables", &output, n); |
| 1977 | |
| 1978 | /* output.data (malloced in one block) gets returned in "list" */ |
| 1979 | list = o_finalize_list(&output, n); |
| 1980 | debug_print_strings("expand_variables[1]", list); |
| 1981 | return list; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1984 | static char **expand_strvec_to_strvec(char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1985 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1986 | return expand_variables(argv, 0x100); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1989 | /* Used for expansion of right hand of assignments */ |
| 1990 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 1991 | * "v=/bin/c*" */ |
| 1992 | static char *expand_string_to_string(const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1993 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1994 | char *argv[2], **list; |
| 1995 | |
| 1996 | argv[0] = (char*)str; |
| 1997 | argv[1] = NULL; |
| 1998 | list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */ |
| 1999 | if (HUSH_DEBUG) |
| 2000 | if (!list[0] || list[1]) |
| 2001 | bb_error_msg_and_die("BUG in varexp2"); |
| 2002 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 2003 | overlapping_strcpy((char*)list, list[0]); |
| 2004 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 2005 | return (char*)list; |
| 2006 | } |
| 2007 | |
| 2008 | /* Used for "eval" builtin */ |
| 2009 | static char* expand_strvec_to_string(char **argv) |
| 2010 | { |
| 2011 | char **list; |
| 2012 | |
| 2013 | list = expand_variables(argv, 0x80); |
| 2014 | /* Convert all NULs to spaces */ |
| 2015 | if (list[0]) { |
| 2016 | int n = 1; |
| 2017 | while (list[n]) { |
| 2018 | if (HUSH_DEBUG) |
| 2019 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 2020 | bb_error_msg_and_die("BUG in varexp3"); |
| 2021 | list[n][-1] = ' '; /* TODO: or to G.ifs[0]? */ |
| 2022 | n++; |
| 2023 | } |
| 2024 | } |
| 2025 | overlapping_strcpy((char*)list, list[0]); |
| 2026 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 2027 | return (char*)list; |
| 2028 | } |
| 2029 | |
| 2030 | static char **expand_assignments(char **argv, int count) |
| 2031 | { |
| 2032 | int i; |
| 2033 | char **p = NULL; |
| 2034 | /* Expand assignments into one string each */ |
| 2035 | for (i = 0; i < count; i++) { |
| 2036 | p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 2037 | } |
| 2038 | return p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2041 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2042 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 2043 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2044 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2045 | { |
| 2046 | int openfd, mode; |
| 2047 | struct redir_struct *redir; |
| 2048 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2049 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2050 | if (redir->dup == -1 && redir->rd_filename == NULL) { |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2051 | /* something went wrong in the parse. Pretend it didn't happen */ |
| 2052 | continue; |
| 2053 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2054 | if (redir->dup == -1) { |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2055 | char *p; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2056 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 2057 | //TODO: check redir for names like '\\' |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2058 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2059 | openfd = open_or_warn(p, mode); |
| 2060 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2061 | if (openfd < 0) { |
| 2062 | /* this could get lost if stderr has been redirected, but |
| 2063 | bash and ash both lose it as well (though zsh doesn't!) */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2064 | return 1; |
| 2065 | } |
| 2066 | } else { |
| 2067 | openfd = redir->dup; |
| 2068 | } |
| 2069 | |
| 2070 | if (openfd != redir->fd) { |
| 2071 | if (squirrel && redir->fd < 3) { |
| 2072 | squirrel[redir->fd] = dup(redir->fd); |
| 2073 | } |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2074 | if (openfd == -3) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2075 | //close(openfd); // close(-3) ??! |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2076 | } else { |
| 2077 | dup2(openfd, redir->fd); |
Matt Kraai | c616e53 | 2001-06-05 16:50:08 +0000 | [diff] [blame] | 2078 | if (redir->dup == -1) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2079 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2080 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2081 | } |
| 2082 | } |
| 2083 | return 0; |
| 2084 | } |
| 2085 | |
| 2086 | static void restore_redirects(int squirrel[]) |
| 2087 | { |
| 2088 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2089 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2090 | fd = squirrel[i]; |
| 2091 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2092 | /* We simply die on error */ |
| 2093 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2094 | } |
| 2095 | } |
| 2096 | } |
| 2097 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2098 | |
Denis Vlasenko | a0e6512 | 2009-04-05 23:39:14 +0000 | [diff] [blame] | 2099 | #if !DEBUG_CLEAN |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2100 | #define free_pipe_list(head, indent) free_pipe_list(head) |
| 2101 | #define free_pipe(pi, indent) free_pipe(pi) |
| 2102 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2103 | static void free_pipe_list(struct pipe *head, int indent); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2104 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2105 | /* Return code is the exit status of the pipe */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2106 | static void free_pipe(struct pipe *pi, int indent) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2107 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2108 | char **p; |
| 2109 | struct command *command; |
| 2110 | struct redir_struct *r, *rnext; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2111 | int a, i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2112 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2113 | if (pi->stopped_cmds > 0) /* why? */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2114 | return; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2115 | debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid()); |
| 2116 | for (i = 0; i < pi->num_cmds; i++) { |
| 2117 | command = &pi->cmds[i]; |
| 2118 | debug_printf_clean("%s command %d:\n", indenter(indent), i); |
| 2119 | if (command->argv) { |
| 2120 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 2121 | debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p); |
| 2122 | } |
| 2123 | free_strings(command->argv); |
| 2124 | command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2125 | } |
| 2126 | /* not "else if": on syntax error, we may have both! */ |
| 2127 | if (command->group) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2128 | debug_printf_clean("%s begin group (grp_type:%d)\n", indenter(indent), command->grp_type); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2129 | free_pipe_list(command->group, indent+3); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2130 | debug_printf_clean("%s end group\n", indenter(indent)); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2131 | command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2132 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2133 | #if !BB_MMU |
| 2134 | free(command->group_as_string); |
| 2135 | command->group_as_string = NULL; |
| 2136 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2137 | for (r = command->redirects; r; r = rnext) { |
| 2138 | debug_printf_clean("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->rd_type].descrip); |
| 2139 | if (r->dup == -1) { |
| 2140 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 2141 | if (r->rd_filename) { |
| 2142 | debug_printf_clean(" %s\n", r->rd_filename); |
| 2143 | free(r->rd_filename); |
| 2144 | r->rd_filename = NULL; |
| 2145 | } |
| 2146 | } else { |
| 2147 | debug_printf_clean("&%d\n", r->dup); |
| 2148 | } |
| 2149 | rnext = r->next; |
| 2150 | free(r); |
| 2151 | } |
| 2152 | command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2153 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2154 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 2155 | pi->cmds = NULL; |
| 2156 | #if ENABLE_HUSH_JOB |
| 2157 | free(pi->cmdtext); |
| 2158 | pi->cmdtext = NULL; |
| 2159 | #endif |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2160 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2161 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2162 | static void free_pipe_list(struct pipe *head, int indent) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2163 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2164 | struct pipe *pi, *next; |
| 2165 | |
| 2166 | for (pi = head; pi; pi = next) { |
| 2167 | #if HAS_KEYWORDS |
| 2168 | debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word); |
| 2169 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2170 | free_pipe(pi, indent); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2171 | debug_printf_clean("%s pipe followup code %d\n", indenter(indent), pi->followup); |
| 2172 | next = pi->next; |
| 2173 | /*pi->next = NULL;*/ |
| 2174 | free(pi); |
| 2175 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | |
| 2179 | #if !BB_MMU |
| 2180 | typedef struct nommu_save_t { |
| 2181 | char **new_env; |
| 2182 | char **old_env; |
| 2183 | char **argv; |
| 2184 | } nommu_save_t; |
| 2185 | #else |
| 2186 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 2187 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 2188 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 2189 | pseudo_exec(command, argv_expanded) |
| 2190 | #endif |
| 2191 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2192 | /* Called after [v]fork() in run_pipe(), or from builtin_exec(). |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2193 | * Never returns. |
| 2194 | * XXX no exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2195 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2196 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2197 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 2198 | char **argv, int assignment_cnt, |
| 2199 | char **argv_expanded) NORETURN; |
| 2200 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 2201 | char **argv, int assignment_cnt, |
| 2202 | char **argv_expanded) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2203 | { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2204 | char **new_env; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2205 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2206 | /* Case when we are here: ... | var=val | ... */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2207 | if (!argv[assignment_cnt]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2208 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2209 | |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 2210 | new_env = expand_assignments(argv, assignment_cnt); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2211 | #if BB_MMU |
| 2212 | putenv_all(new_env); |
| 2213 | free(new_env); /* optional */ |
| 2214 | #else |
| 2215 | nommu_save->new_env = new_env; |
| 2216 | nommu_save->old_env = putenv_all_and_save_old(new_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2217 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2218 | if (argv_expanded) { |
| 2219 | argv = argv_expanded; |
| 2220 | } else { |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 2221 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2222 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2223 | nommu_save->argv = argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2224 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2225 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2226 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2227 | /* On NOMMU, we must never block! |
| 2228 | * Example: { sleep 99999 | read line } & echo Ok |
| 2229 | * read builtin will block on read syscall, leaving parent blocked |
| 2230 | * in vfork. Therefore we can't do this: |
| 2231 | */ |
| 2232 | #if BB_MMU |
| 2233 | /* Check if the command matches any of the builtins. |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2234 | * Depending on context, this might be redundant. But it's |
| 2235 | * easier to waste a few CPU cycles than it is to figure out |
| 2236 | * if this is one of those cases. |
| 2237 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2238 | { |
| 2239 | int rcode; |
| 2240 | const struct built_in_command *x; |
| 2241 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 2242 | if (strcmp(argv[0], x->cmd) == 0) { |
| 2243 | debug_printf_exec("running builtin '%s'\n", |
| 2244 | argv[0]); |
| 2245 | rcode = x->function(argv); |
| 2246 | fflush(NULL); |
| 2247 | _exit(rcode); |
| 2248 | } |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2249 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2250 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2251 | #endif |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2252 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 2253 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2254 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2255 | if (strchr(argv[0], '/') == NULL) { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 2256 | int a = find_applet_by_name(argv[0]); |
| 2257 | if (a >= 0) { |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2258 | #if BB_MMU /* see above why on NOMMU it is not allowed */ |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 2259 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2260 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 2261 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2262 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2263 | #endif |
| 2264 | /* Re-exec ourselves */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2265 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2266 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 2267 | execv(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2268 | /* If they called chroot or otherwise made the binary no longer |
| 2269 | * executable, fall through */ |
| 2270 | } |
| 2271 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 2272 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2273 | |
| 2274 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2275 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2276 | execvp(argv[0], argv); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 2277 | bb_perror_msg("can't exec '%s'", argv[0]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 2278 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2281 | #if BB_MMU |
| 2282 | static void reset_traps_to_defaults(void) |
| 2283 | { |
| 2284 | unsigned sig; |
| 2285 | int dirty; |
| 2286 | |
| 2287 | if (!G.traps) |
| 2288 | return; |
| 2289 | dirty = 0; |
| 2290 | for (sig = 0; sig < NSIG; sig++) { |
| 2291 | if (!G.traps[sig]) |
| 2292 | continue; |
| 2293 | free(G.traps[sig]); |
| 2294 | G.traps[sig] = NULL; |
| 2295 | /* There is no signal for 0 (EXIT) */ |
| 2296 | if (sig == 0) |
| 2297 | continue; |
| 2298 | /* there was a trap handler, we are removing it |
| 2299 | * (if sig has non-DFL handling, |
| 2300 | * we don't need to do anything) */ |
| 2301 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 2302 | continue; |
| 2303 | sigdelset(&G.blocked_set, sig); |
| 2304 | dirty = 1; |
| 2305 | } |
| 2306 | if (dirty) |
| 2307 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 2308 | } |
| 2309 | #define clean_up_after_re_execute() ((void)0) |
| 2310 | |
| 2311 | #else /* !BB_MMU */ |
| 2312 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 2313 | static void re_execute_shell(const char *s) NORETURN; |
| 2314 | static void re_execute_shell(const char *s) |
| 2315 | { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 2316 | struct variable *cur; |
Denis Vlasenko | 30db43b | 2009-04-05 02:10:39 +0000 | [diff] [blame] | 2317 | char **argv, **pp, **pp2; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 2318 | unsigned cnt; |
| 2319 | |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 2320 | /* 1:hush 2:-$<pid> 3:-!<pid> 4:-?<exitcode> 5:-D<depth> <vars...> |
| 2321 | * 6:-c 7:<cmd> <argN...> 8:NULL |
Denis Vlasenko | 30db43b | 2009-04-05 02:10:39 +0000 | [diff] [blame] | 2322 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 2323 | cnt = 8 + G.global_argc; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 2324 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2325 | if (!cur->flg_export || cur->flg_read_only) |
| 2326 | cnt += 2; |
| 2327 | } |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 2328 | G.argv_from_re_execing = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 2329 | *pp++ = (char *) G.argv0_for_re_execing; |
| 2330 | *pp++ = xasprintf("-$%u", (unsigned) G.root_pid); |
| 2331 | *pp++ = xasprintf("-!%u", (unsigned) G.last_bg_pid); |
| 2332 | *pp++ = xasprintf("-?%u", (unsigned) G.last_return_code); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 2333 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 16a0c74 | 2009-04-05 01:46:59 +0000 | [diff] [blame] | 2334 | *pp++ = xasprintf("-D%u", G.depth_of_loop); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 2335 | #endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 2336 | for (cur = G.top_var; cur; cur = cur->next) { |
| 2337 | if (cur->varstr == hush_version_str) |
| 2338 | continue; |
| 2339 | if (cur->flg_read_only) { |
| 2340 | *pp++ = (char *) "-R"; |
| 2341 | *pp++ = cur->varstr; |
| 2342 | } else if (!cur->flg_export) { |
| 2343 | *pp++ = (char *) "-V"; |
| 2344 | *pp++ = cur->varstr; |
| 2345 | } |
| 2346 | } |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2347 | //TODO: pass functions |
| 2348 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 2349 | * |
| 2350 | * However, POSIX says that subshells reset signals with traps |
| 2351 | * to SIG_DFL. |
| 2352 | * I tested bash-3.2 and it not only does that with true subshells |
| 2353 | * of the form ( list ), but with any forked children shells. |
| 2354 | * I set trap "echo W" WINCH; and then tried: |
| 2355 | * |
| 2356 | * { echo 1; sleep 20; echo 2; } & |
| 2357 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 2358 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 2359 | * |
| 2360 | * In all these cases sending SIGWINCH to the child shell |
| 2361 | * did not run the trap. If I add trap "echo V" WINCH; |
| 2362 | * _inside_ group (just before echo 1), it works. |
| 2363 | * |
| 2364 | * I conclude it means we don't need to pass active traps here. |
| 2365 | * exec syscall below resets them to SIG_DFL for us. |
| 2366 | */ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 2367 | *pp++ = (char *) "-c"; |
| 2368 | *pp++ = (char *) s; |
Denis Vlasenko | 30db43b | 2009-04-05 02:10:39 +0000 | [diff] [blame] | 2369 | pp2 = G.global_argv; |
| 2370 | while (*pp2) |
| 2371 | *pp++ = *pp2++; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 2372 | /* *pp = NULL; - is already there */ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 2373 | |
| 2374 | debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s); |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 2375 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 2376 | execv(bb_busybox_exec_path, G.argv_from_re_execing); |
| 2377 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 2378 | if (G.argv0_for_re_execing[0] == '/') |
| 2379 | execv(G.argv0_for_re_execing, G.argv_from_re_execing); |
| 2380 | xfunc_error_retval = 127; |
| 2381 | bb_error_msg_and_die("can't re-execute the shell"); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 2384 | static void clean_up_after_re_execute(void) |
| 2385 | { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 2386 | char **pp = G.argv_from_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 2387 | if (pp) { |
| 2388 | /* Must match re_execute_shell's allocations */ |
| 2389 | free(pp[1]); |
| 2390 | free(pp[2]); |
| 2391 | free(pp[3]); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 2392 | #if ENABLE_HUSH_LOOPS |
| 2393 | free(pp[4]); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 2394 | #endif |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 2395 | free(pp); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 2396 | G.argv_from_re_execing = NULL; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 2397 | } |
| 2398 | } |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 2399 | #endif |
| 2400 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2401 | static int run_list(struct pipe *pi); |
| 2402 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2403 | /* Called after [v]fork() in run_pipe() |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2404 | */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2405 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 2406 | struct command *command, |
| 2407 | char **argv_expanded) NORETURN; |
| 2408 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 2409 | struct command *command, |
| 2410 | char **argv_expanded) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2411 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2412 | if (command->argv) { |
| 2413 | pseudo_exec_argv(nommu_save, command->argv, |
| 2414 | command->assignment_cnt, argv_expanded); |
| 2415 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2416 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2417 | if (command->group) { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2418 | /* Cases when we are here: |
| 2419 | * ( list ) |
| 2420 | * { list } & |
| 2421 | * ... | ( list ) | ... |
| 2422 | * ... | { list } | ... |
| 2423 | */ |
| 2424 | #if BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 2425 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2426 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2427 | reset_traps_to_defaults(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2428 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2429 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2430 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2431 | _exit(rcode); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2432 | #else |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 2433 | re_execute_shell(command->group_as_string); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2434 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2435 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2436 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2437 | /* Case when we are here: ... | >file */ |
| 2438 | debug_printf_exec("pseudo_exec'ed null command\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2439 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2442 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2443 | static const char *get_cmdtext(struct pipe *pi) |
| 2444 | { |
| 2445 | char **argv; |
| 2446 | char *p; |
| 2447 | int len; |
| 2448 | |
| 2449 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 2450 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2451 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2452 | if (pi->cmdtext) |
| 2453 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2454 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2455 | if (!argv || !argv[0]) { |
| 2456 | pi->cmdtext = xzalloc(1); |
| 2457 | return pi->cmdtext; |
| 2458 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2459 | |
| 2460 | len = 0; |
| 2461 | do len += strlen(*argv) + 1; while (*++argv); |
| 2462 | pi->cmdtext = p = xmalloc(len); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2463 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2464 | do { |
| 2465 | len = strlen(*argv); |
| 2466 | memcpy(p, *argv, len); |
| 2467 | p += len; |
| 2468 | *p++ = ' '; |
| 2469 | } while (*++argv); |
| 2470 | p[-1] = '\0'; |
| 2471 | return pi->cmdtext; |
| 2472 | } |
| 2473 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2474 | static void insert_bg_job(struct pipe *pi) |
| 2475 | { |
| 2476 | struct pipe *thejob; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2477 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2478 | |
| 2479 | /* Linear search for the ID of the job to use */ |
| 2480 | pi->jobid = 1; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2481 | for (thejob = G.job_list; thejob; thejob = thejob->next) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2482 | if (thejob->jobid >= pi->jobid) |
| 2483 | pi->jobid = thejob->jobid + 1; |
| 2484 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2485 | /* Add thejob to the list of running jobs */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2486 | if (!G.job_list) { |
| 2487 | thejob = G.job_list = xmalloc(sizeof(*thejob)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2488 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2489 | for (thejob = G.job_list; thejob->next; thejob = thejob->next) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2490 | continue; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2491 | thejob->next = xmalloc(sizeof(*thejob)); |
| 2492 | thejob = thejob->next; |
| 2493 | } |
| 2494 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2495 | /* Physically copy the struct job */ |
Eric Andersen | 0fcd447 | 2001-05-02 20:12:03 +0000 | [diff] [blame] | 2496 | memcpy(thejob, pi, sizeof(struct pipe)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2497 | thejob->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 2498 | /* We cannot copy entire pi->cmds[] vector! Double free()s will happen */ |
| 2499 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2500 | // TODO: do we really need to have so many fields which are just dead weight |
| 2501 | // at execution stage? |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2502 | thejob->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2503 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2504 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2505 | thejob->next = NULL; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2506 | thejob->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2507 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2508 | /* We don't wait for background thejobs to return -- append it |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2509 | to the list of backgrounded thejobs and leave it alone */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 2510 | if (G_interactive_fd) |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 2511 | printf("[%d] %d %s\n", thejob->jobid, thejob->cmds[0].pid, thejob->cmdtext); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2512 | G.last_bg_pid = thejob->cmds[0].pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2513 | G.last_jobid = thejob->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2514 | } |
| 2515 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2516 | static void remove_bg_job(struct pipe *pi) |
| 2517 | { |
| 2518 | struct pipe *prev_pipe; |
| 2519 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2520 | if (pi == G.job_list) { |
| 2521 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2522 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2523 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2524 | while (prev_pipe->next != pi) |
| 2525 | prev_pipe = prev_pipe->next; |
| 2526 | prev_pipe->next = pi->next; |
| 2527 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2528 | if (G.job_list) |
| 2529 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 2530 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2531 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2532 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 2533 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2534 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2535 | static void delete_finished_bg_job(struct pipe *pi) |
| 2536 | { |
| 2537 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2538 | pi->stopped_cmds = 0; |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2539 | free_pipe(pi, 0); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2540 | free(pi); |
| 2541 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2542 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2543 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2544 | /* Check to see if any processes have exited -- if they |
| 2545 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2546 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2547 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2548 | int attributes; |
| 2549 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2550 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2551 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2552 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2553 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2554 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2555 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2556 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 2557 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 2558 | errno = 0; |
| 2559 | // if (G.handled_SIGCHLD == G.count_SIGCHLD) |
| 2560 | // /* avoid doing syscall, nothing there anyway */ |
| 2561 | // return rcode; |
| 2562 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2563 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2564 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2565 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2566 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2567 | /* Do we do this right? |
| 2568 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2569 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2570 | * [3]+ Stopped sleep 20 | false |
| 2571 | * bash-3.00# echo $? |
| 2572 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 2573 | */ |
| 2574 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2575 | //FIXME: non-interactive bash does not continue even if all processes in fg pipe |
| 2576 | //are stopped. Testcase: "cat | cat" in a script (not on command line) |
| 2577 | // + killall -STOP cat |
| 2578 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2579 | wait_more: |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 2580 | while (1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2581 | int i; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 2582 | int dead; |
| 2583 | |
| 2584 | // i = G.count_SIGCHLD; |
| 2585 | childpid = waitpid(-1, &status, attributes); |
| 2586 | if (childpid <= 0) { |
| 2587 | if (childpid && errno != ECHILD) |
| 2588 | bb_perror_msg("waitpid"); |
| 2589 | // else /* Until next SIGCHLD, waitpid's are useless */ |
| 2590 | // G.handled_SIGCHLD = i; |
| 2591 | break; |
| 2592 | } |
| 2593 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 2594 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2595 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2596 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 2597 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2598 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 2599 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 2600 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2601 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 2602 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 2603 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2604 | childpid, WEXITSTATUS(status)); |
| 2605 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2606 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2607 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2608 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 2609 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 2610 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2611 | continue; |
| 2612 | /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */ |
| 2613 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2614 | fg_pipe->cmds[i].pid = 0; |
| 2615 | fg_pipe->alive_cmds--; |
| 2616 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2617 | /* last process gives overall exitstatus */ |
| 2618 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2619 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2620 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2621 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2622 | fg_pipe->cmds[i].is_stopped = 1; |
| 2623 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2624 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2625 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 2626 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 2627 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2628 | /* All processes in fg pipe have exited/stopped */ |
| 2629 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2630 | if (fg_pipe->alive_cmds) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2631 | insert_bg_job(fg_pipe); |
| 2632 | #endif |
| 2633 | return rcode; |
| 2634 | } |
| 2635 | /* There are still running processes in the fg pipe */ |
| 2636 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2637 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2638 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2641 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2642 | /* We asked to wait for bg or orphaned children */ |
| 2643 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2644 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2645 | for (i = 0; i < pi->num_cmds; i++) { |
| 2646 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2647 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 2648 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2649 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2650 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 2651 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2652 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 2653 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2654 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2655 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2656 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2657 | pi->cmds[i].pid = 0; |
| 2658 | pi->alive_cmds--; |
| 2659 | if (!pi->alive_cmds) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 2660 | if (G_interactive_fd) |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 2661 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 2662 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2663 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2664 | } |
| 2665 | } else { |
| 2666 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2667 | pi->cmds[i].is_stopped = 1; |
| 2668 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2669 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2670 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2671 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2672 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2673 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2676 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 2677 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 2678 | { |
| 2679 | pid_t p; |
| 2680 | int rcode = checkjobs(fg_pipe); |
| 2681 | /* Job finished, move the shell to the foreground */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2682 | p = getpgid(0); /* pgid of our process */ |
| 2683 | debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 2684 | tcsetpgrp(G_interactive_fd, p); |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 2685 | return rcode; |
| 2686 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2687 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 2688 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2689 | /* Start all the jobs, but don't wait for anything to finish. |
| 2690 | * See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2691 | * |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2692 | * 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] | 2693 | * to finish to determine the exit status of the pipe. If the pipe |
| 2694 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2695 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2696 | * return value. |
| 2697 | * |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2698 | * Returns -1 only if started some children. IOW: we have to |
| 2699 | * mask out retvals of builtins etc with 0xff! |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2700 | * |
| 2701 | * The only case when we do not need to [v]fork is when the pipe |
| 2702 | * is single, non-backgrounded, non-subshell command. Examples: |
| 2703 | * cmd ; ... { list } ; ... |
| 2704 | * cmd && ... { list } && ... |
| 2705 | * cmd || ... { list } || ... |
| 2706 | * If it is, then we can run cmd as a builtin, NOFORK [do we do this?], |
| 2707 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 2708 | * 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] | 2709 | * |
| 2710 | * Cases when we must fork: |
| 2711 | * non-single: cmd | cmd |
| 2712 | * backgrounded: cmd & { list } & |
| 2713 | * subshell: ( list ) [&] |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2714 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2715 | static int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2716 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2717 | static const char *const null_ptr = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2718 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2719 | int nextin; |
| 2720 | int pipefds[2]; /* pipefds[0] is for reading */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2721 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2722 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2723 | char **argv; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2724 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 2725 | /* it is not always needed, but we aim to smaller code */ |
| 2726 | int squirrel[] = { -1, -1, -1 }; |
| 2727 | int rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2728 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2729 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 2730 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2731 | USE_HUSH_JOB(pi->pgrp = -1;) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2732 | pi->stopped_cmds = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2733 | command = &(pi->cmds[0]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2734 | argv_expanded = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2735 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2736 | if (pi->num_cmds != 1 |
| 2737 | || pi->followup == PIPE_BG |
| 2738 | || command->grp_type == GRP_SUBSHELL |
| 2739 | ) { |
| 2740 | goto must_fork; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2741 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2742 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2743 | pi->alive_cmds = 1; |
| 2744 | |
| 2745 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 2746 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 2747 | |
| 2748 | if (command->group) { |
| 2749 | #if ENABLE_HUSH_FUNCTIONS |
| 2750 | if (command->grp_type == GRP_FUNCTION) { |
| 2751 | /* func () { list } */ |
| 2752 | bb_error_msg("here we ought to remember function definition, and go on"); |
| 2753 | return EXIT_SUCCESS; |
| 2754 | } |
| 2755 | #endif |
| 2756 | /* { list } */ |
| 2757 | debug_printf("non-subshell group\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2758 | setup_redirects(command, squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2759 | debug_printf_exec(": run_list\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2760 | rcode = run_list(command->group) & 0xff; |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 2761 | restore_redirects(squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2762 | debug_printf_exec("run_pipe return %d\n", rcode); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2763 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2764 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2767 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 2768 | { |
| 2769 | const struct built_in_command *x; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2770 | char **new_env = NULL; |
| 2771 | char **old_env = NULL; |
| 2772 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2773 | if (argv[command->assignment_cnt] == NULL) { |
| 2774 | /* Assignments, but no command */ |
| 2775 | /* Ensure redirects take effect. Try "a=t >file" */ |
| 2776 | setup_redirects(command, squirrel); |
| 2777 | restore_redirects(squirrel); |
| 2778 | /* Set shell variables */ |
| 2779 | while (*argv) { |
| 2780 | p = expand_string_to_string(*argv); |
| 2781 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 2782 | *argv, p); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 2783 | set_local_var(p, 0, 0); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2784 | argv++; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2785 | } |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2786 | /* Do we need to flag set_local_var() errors? |
| 2787 | * "assignment to readonly var" and "putenv error" |
| 2788 | */ |
| 2789 | return EXIT_SUCCESS; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2790 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2791 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2792 | /* Expand the rest into (possibly) many strings each */ |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2793 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2794 | |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 2795 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2796 | if (strcmp(argv_expanded[0], x->cmd) != 0) |
| 2797 | continue; |
| 2798 | if (x->function == builtin_exec && argv_expanded[1] == NULL) { |
| 2799 | debug_printf("exec with redirects only\n"); |
| 2800 | setup_redirects(command, NULL); |
| 2801 | rcode = EXIT_SUCCESS; |
| 2802 | goto clean_up_and_ret1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2803 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2804 | debug_printf("builtin inline %s\n", argv_expanded[0]); |
| 2805 | /* XXX setup_redirects acts on file descriptors, not FILEs. |
| 2806 | * This is perfect for work that comes after exec(). |
| 2807 | * Is it really safe for inline use? Experimentally, |
| 2808 | * things seem to work with glibc. */ |
| 2809 | setup_redirects(command, squirrel); |
| 2810 | new_env = expand_assignments(argv, command->assignment_cnt); |
| 2811 | old_env = putenv_all_and_save_old(new_env); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2812 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 2813 | x->cmd, argv_expanded[1]); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2814 | rcode = x->function(argv_expanded) & 0xff; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2815 | #if ENABLE_FEATURE_SH_STANDALONE |
| 2816 | clean_up_and_ret: |
| 2817 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2818 | restore_redirects(squirrel); |
| 2819 | free_strings_and_unsetenv(new_env, 1); |
| 2820 | putenv_all(old_env); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2821 | free(old_env); /* not free_strings()! */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2822 | clean_up_and_ret1: |
| 2823 | free(argv_expanded); |
| 2824 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 2825 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 2826 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2827 | } |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 2828 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2829 | i = find_applet_by_name(argv_expanded[0]); |
| 2830 | if (i >= 0 && APPLET_IS_NOFORK(i)) { |
| 2831 | setup_redirects(command, squirrel); |
| 2832 | save_nofork_data(&G.nofork_save); |
| 2833 | new_env = expand_assignments(argv, command->assignment_cnt); |
| 2834 | old_env = putenv_all_and_save_old(new_env); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2835 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 2836 | argv_expanded[0], argv_expanded[1]); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2837 | rcode = run_nofork_applet_prime(&G.nofork_save, i, argv_expanded); |
| 2838 | goto clean_up_and_ret; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 2839 | } |
| 2840 | #endif |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2841 | /* It is neither builtin nor applet. We must fork. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2844 | must_fork: |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2845 | /* NB: argv_expanded may already be created, and that |
| 2846 | * might include `cmd` runs! Do not rerun it! We *must* |
| 2847 | * use argv_expanded if it's non-NULL */ |
| 2848 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2849 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2850 | pi->alive_cmds = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2851 | nextin = 0; |
| 2852 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2853 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2854 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2855 | volatile nommu_save_t nommu_save; |
| 2856 | nommu_save.new_env = NULL; |
| 2857 | nommu_save.old_env = NULL; |
| 2858 | nommu_save.argv = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2859 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2860 | command = &(pi->cmds[i]); |
| 2861 | if (command->argv) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2862 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 2863 | command->argv[0], command->argv[1]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2864 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2865 | debug_printf_exec(": pipe member with no argv\n"); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 2866 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2867 | |
| 2868 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2869 | pipefds[0] = 0; |
| 2870 | pipefds[1] = 1; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2871 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2872 | xpipe(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2873 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2874 | command->pid = BB_MMU ? fork() : vfork(); |
| 2875 | if (!command->pid) { /* child */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2876 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 2877 | die_sleep = 0; /* do not restore tty pgrp on xfunc death */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2878 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2879 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2880 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 2881 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2882 | pid_t pgrp; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2883 | pgrp = pi->pgrp; |
| 2884 | if (pgrp < 0) /* true for 1st process only */ |
| 2885 | pgrp = getpid(); |
| 2886 | if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2887 | /* We do it in *every* child, not just first, |
| 2888 | * to avoid races */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 2889 | tcsetpgrp(G_interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2890 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2891 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2892 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2893 | xmove_fd(nextin, 0); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2894 | xmove_fd(pipefds[1], 1); /* write end */ |
| 2895 | if (pipefds[0] > 1) |
| 2896 | close(pipefds[0]); /* read end */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2897 | /* Like bash, explicit redirects override pipes, |
| 2898 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2899 | setup_redirects(command, NULL); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 2900 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2901 | /* Restore default handlers just prior to exec */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2902 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 2903 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2904 | /* Stores to nommu_save list of env vars putenv'ed |
| 2905 | * (NOMMU, on MMU we don't need that) */ |
| 2906 | /* cast away volatility... */ |
| 2907 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2908 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2909 | } |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 2910 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 2911 | /* parent or error */ |
| 2912 | #if ENABLE_HUSH_JOB |
| 2913 | die_sleep = -1; /* restore tty pgrp on xfunc death */ |
| 2914 | #endif |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2915 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2916 | /* Clean up after vforked child */ |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 2917 | clean_up_after_re_execute(); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2918 | free(nommu_save.argv); |
| 2919 | free_strings_and_unsetenv(nommu_save.new_env, 1); |
| 2920 | putenv_all(nommu_save.old_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2921 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2922 | free(argv_expanded); |
| 2923 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2924 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2925 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 2926 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2927 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2928 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2929 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2930 | /* Second and next children need to know pid of first one */ |
| 2931 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2932 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2933 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2934 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2935 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2936 | if (i) |
| 2937 | close(nextin); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2938 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2939 | close(pipefds[1]); /* write end */ |
| 2940 | /* Pass read (output) pipe end to next iteration */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2941 | nextin = pipefds[0]; |
| 2942 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2943 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2944 | if (!pi->alive_cmds) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2945 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 2946 | return 1; |
| 2947 | } |
| 2948 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2949 | 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] | 2950 | return -1; |
| 2951 | } |
| 2952 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 2953 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2954 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 2955 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2956 | static const char *const PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2957 | [PIPE_SEQ] = "SEQ", |
| 2958 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2959 | [PIPE_OR ] = "OR" , |
| 2960 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2961 | }; |
| 2962 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2963 | [RES_NONE ] = "NONE" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2964 | #if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2965 | [RES_IF ] = "IF" , |
| 2966 | [RES_THEN ] = "THEN" , |
| 2967 | [RES_ELIF ] = "ELIF" , |
| 2968 | [RES_ELSE ] = "ELSE" , |
| 2969 | [RES_FI ] = "FI" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2970 | #endif |
| 2971 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2972 | [RES_FOR ] = "FOR" , |
| 2973 | [RES_WHILE] = "WHILE", |
| 2974 | [RES_UNTIL] = "UNTIL", |
| 2975 | [RES_DO ] = "DO" , |
| 2976 | [RES_DONE ] = "DONE" , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 2977 | #endif |
| 2978 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2979 | [RES_IN ] = "IN" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2980 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2981 | #if ENABLE_HUSH_CASE |
| 2982 | [RES_CASE ] = "CASE" , |
| 2983 | [RES_MATCH] = "MATCH", |
| 2984 | [RES_CASEI] = "CASEI", |
| 2985 | [RES_ESAC ] = "ESAC" , |
| 2986 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2987 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2988 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2989 | }; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2990 | static const char *const GRPTYPE[] = { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2991 | "{}", |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 2992 | "()", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2993 | #if ENABLE_HUSH_FUNCTIONS |
| 2994 | "func()", |
| 2995 | #endif |
| 2996 | }; |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2997 | |
| 2998 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2999 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3000 | pin = 0; |
| 3001 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3002 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 3003 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3004 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3005 | while (prn < pi->num_cmds) { |
| 3006 | struct command *command = &pi->cmds[prn]; |
| 3007 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3008 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3009 | fprintf(stderr, "%*s prog %d assignment_cnt:%d", |
| 3010 | lvl*2, "", prn, |
| 3011 | command->assignment_cnt); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3012 | if (command->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3013 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3014 | GRPTYPE[command->grp_type], |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3015 | argv); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3016 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3017 | prn++; |
| 3018 | continue; |
| 3019 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3020 | if (argv) while (*argv) { |
| 3021 | fprintf(stderr, " '%s'", *argv); |
| 3022 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 3023 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3024 | fprintf(stderr, "\n"); |
| 3025 | prn++; |
| 3026 | } |
| 3027 | pi = pi->next; |
| 3028 | pin++; |
| 3029 | } |
| 3030 | } |
| 3031 | #endif |
| 3032 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3033 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 3034 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3035 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3036 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3037 | #if ENABLE_HUSH_CASE |
| 3038 | char *case_word = NULL; |
| 3039 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3040 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3041 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3042 | char *for_varname = NULL; |
| 3043 | char **for_lcur = NULL; |
| 3044 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3045 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3046 | smallint flag_skip = 1; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 3047 | smalluint rcode = 0; /* probably just for compiler */ |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 3048 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3049 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3050 | #else |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3051 | enum { cond_code = 0, }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3052 | #endif |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3053 | /*enum reserved_style*/ smallint rword = RES_NONE; |
| 3054 | /*enum reserved_style*/ smallint skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 3055 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3056 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level + 1); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 3057 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3058 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3059 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 3060 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 3061 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3062 | continue; |
| 3063 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 3064 | if (cpipe->next == NULL) { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3065 | syntax("malformed for"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3066 | 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] | 3067 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3068 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3069 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 3070 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3071 | continue; |
| 3072 | /* 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] | 3073 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 3074 | || 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] | 3075 | ) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3076 | syntax("malformed for"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3077 | 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] | 3078 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3079 | } |
| 3080 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3081 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3082 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 3083 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 3084 | * in order to return, no direct "return" statements please. |
| 3085 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 3086 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 3087 | ////TODO: ctrl-Z handling needs re-thinking and re-testing |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3088 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3089 | #if ENABLE_HUSH_JOB |
| 3090 | /* Example of nested list: "while true; do { sleep 1 | exit 2; } done". |
| 3091 | * We are saving state before entering outermost list ("while...done") |
| 3092 | * so that ctrl-Z will correctly background _entire_ outermost list, |
| 3093 | * not just a part of it (like "sleep 1 | exit 2") */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3094 | if (++G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3095 | if (sigsetjmp(G.toplevel_jb, 1)) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3096 | /* ctrl-Z forked and we are parent; or ctrl-C. |
| 3097 | * Sighandler has longjmped us here */ |
| 3098 | signal(SIGINT, SIG_IGN); |
| 3099 | signal(SIGTSTP, SIG_IGN); |
| 3100 | /* Restore level (we can be coming from deep inside |
| 3101 | * nested levels) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3102 | G.run_list_level = 1; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3103 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3104 | if (G.nofork_save.saved) { /* if save area is valid */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3105 | debug_printf_jobs("exiting nofork early\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3106 | restore_nofork_data(&G.nofork_save); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3107 | } |
| 3108 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3109 | //// if (G.ctrl_z_flag) { |
| 3110 | //// /* ctrl-Z has forked and stored pid of the child in pi->pid. |
| 3111 | //// * Remember this child as background job */ |
| 3112 | //// insert_bg_job(pi); |
| 3113 | //// } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3114 | /* ctrl-C. We just stop doing whatever we were doing */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 3115 | bb_putchar('\n'); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3116 | //// } |
Denis Vlasenko | ff29b4f | 2008-07-29 13:57:59 +0000 | [diff] [blame] | 3117 | USE_HUSH_LOOPS(loop_top = NULL;) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3118 | USE_HUSH_LOOPS(G.depth_of_loop = 0;) |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3119 | rcode = 0; |
| 3120 | goto ret; |
| 3121 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3122 | //// /* ctrl-Z handler will store pid etc in pi */ |
| 3123 | //// G.toplevel_list = pi; |
| 3124 | //// G.ctrl_z_flag = 0; |
| 3125 | ////#if ENABLE_FEATURE_SH_STANDALONE |
| 3126 | //// G.nofork_save.saved = 0; /* in case we will run a nofork later */ |
| 3127 | ////#endif |
| 3128 | //// signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z); |
| 3129 | //// signal(SIGINT, handler_ctrl_c); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3130 | } |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3131 | #endif /* JOB */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3132 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3133 | /* Go through list of pipes, (maybe) executing them. */ |
| 3134 | for (; pi; pi = USE_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 3135 | if (G.flag_SIGINT) |
| 3136 | break; |
| 3137 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3138 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 3139 | IF_HAS_NO_KEYWORDS(rword = RES_NONE;) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3140 | debug_printf_exec(": rword=%d cond_code=%d skip_more=%d\n", |
| 3141 | rword, cond_code, skip_more_for_this_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3142 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 3143 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3144 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 3145 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3146 | /* start of a loop: remember where loop starts */ |
| 3147 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3148 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3149 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3150 | #endif |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3151 | if (rword == skip_more_for_this_rword && flag_skip) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3152 | if (pi->followup == PIPE_SEQ) |
| 3153 | flag_skip = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 3154 | /* it is "<false> && CMD" or "<true> || CMD" |
| 3155 | * and we should not execute CMD */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3156 | continue; |
| 3157 | } |
| 3158 | flag_skip = 1; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3159 | skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3160 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3161 | if (cond_code) { |
| 3162 | if (rword == RES_THEN) { |
| 3163 | /* "if <false> THEN cmd": skip cmd */ |
| 3164 | continue; |
| 3165 | } |
| 3166 | } else { |
| 3167 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 3168 | /* "if <true> then ... ELSE/ELIF cmd": |
| 3169 | * skip cmd and all following ones */ |
| 3170 | break; |
| 3171 | } |
| 3172 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3173 | #endif |
| 3174 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3175 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3176 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 3177 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3178 | |
| 3179 | static const char encoded_dollar_at[] ALIGN1 = { |
| 3180 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 3181 | }; /* encoded representation of "$@" */ |
| 3182 | static const char *const encoded_dollar_at_argv[] = { |
| 3183 | encoded_dollar_at, NULL |
| 3184 | }; /* argv list with one element: "$@" */ |
| 3185 | char **vals; |
| 3186 | |
| 3187 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 3188 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3189 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3190 | if (!pi->next->cmds[0].argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3191 | break; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3192 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3193 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3194 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3195 | debug_print_strings("for_list made from", vals); |
| 3196 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3197 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3198 | debug_print_strings("for_list", for_list); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3199 | for_varname = pi->cmds[0].argv[0]; |
| 3200 | pi->cmds[0].argv[0] = NULL; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3201 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3202 | free(pi->cmds[0].argv[0]); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3203 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 3204 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3205 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 3206 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3207 | for_lcur = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3208 | pi->cmds[0].argv[0] = for_varname; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3209 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3210 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3211 | /* insert next value from for_lcur */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3212 | //TODO: does it need escaping? |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3213 | pi->cmds[0].argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++); |
| 3214 | pi->cmds[0].assignment_cnt = 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3215 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3216 | if (rword == RES_IN) { |
| 3217 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 3218 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3219 | if (rword == RES_DONE) { |
| 3220 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3221 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3222 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3223 | #if ENABLE_HUSH_CASE |
| 3224 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3225 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3226 | continue; |
| 3227 | } |
| 3228 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 3229 | char **argv; |
| 3230 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3231 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 3232 | break; |
| 3233 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3234 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 3235 | while (*argv) { |
| 3236 | char *pattern = expand_string_to_string(*argv); |
| 3237 | /* TODO: which FNM_xxx flags to use? */ |
| 3238 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 3239 | free(pattern); |
| 3240 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 3241 | free(case_word); /* make future "word)" stop */ |
| 3242 | case_word = NULL; |
| 3243 | break; |
| 3244 | } |
| 3245 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3246 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3247 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3248 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3249 | if (rword == RES_CASEI) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3250 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3251 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3252 | } |
| 3253 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3254 | /* Just pressing <enter> in shell should check for jobs. |
| 3255 | * OTOH, in non-interactive shell this is useless |
| 3256 | * and only leads to extra job checks */ |
| 3257 | if (pi->num_cmds == 0) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3258 | if (G_interactive_fd) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3259 | goto check_jobs_and_continue; |
| 3260 | continue; |
| 3261 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3262 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3263 | /* After analyzing all keywords and conditions, we decided |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3264 | * to execute this pipe. NB: have to do checkjobs(NULL) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3265 | * after run_pipe() to collect any background children, |
| 3266 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3267 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3268 | { |
| 3269 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3270 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3271 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3272 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3273 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 3274 | if (r != -1) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3275 | /* we only ran a builtin: rcode is already known |
| 3276 | * and we don't need to wait for anything. */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3277 | check_and_run_traps(0); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3278 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3279 | /* was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3280 | if (G.flag_break_continue) { |
| 3281 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3282 | /* we might fall into outer *loop*, |
| 3283 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3284 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3285 | G.depth_break_continue--; |
| 3286 | if (G.depth_break_continue == 0) |
| 3287 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 3288 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 3289 | } /* 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] | 3290 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 3291 | goto check_jobs_and_break; |
| 3292 | /* "continue": simulate end of loop */ |
| 3293 | rword = RES_DONE; |
| 3294 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3295 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3296 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3297 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3298 | /* what does bash do with attempts to background builtins? */ |
| 3299 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3300 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 3301 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3302 | check_and_run_traps(0); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3303 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3304 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3305 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3306 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3307 | rcode = 0; /* EXIT_SUCCESS */ |
| 3308 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3309 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3310 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3311 | /* waits for completion, then fg's main shell */ |
| 3312 | rcode = checkjobs_and_fg_shell(pi); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3313 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3314 | debug_printf_exec(": checkjobs_and_fg_shell returned %d\n", rcode); |
| 3315 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3316 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3317 | { /* this one just waits for completion */ |
| 3318 | rcode = checkjobs(pi); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3319 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3320 | debug_printf_exec(": checkjobs returned %d\n", rcode); |
| 3321 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3322 | } |
| 3323 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3324 | debug_printf_exec(": setting last_return_code=%d\n", rcode); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3325 | G.last_return_code = rcode; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3326 | |
| 3327 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3328 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3329 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3330 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3331 | #endif |
| 3332 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3333 | if (rword == RES_WHILE) { |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 3334 | if (rcode) { |
| 3335 | rcode = 0; /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3336 | goto check_jobs_and_break; |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 3337 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3338 | } |
| 3339 | if (rword == RES_UNTIL) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3340 | if (!rcode) { |
| 3341 | check_jobs_and_break: |
| 3342 | checkjobs(NULL); |
| 3343 | break; |
| 3344 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3345 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3346 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3347 | if ((rcode == 0 && pi->followup == PIPE_OR) |
| 3348 | || (rcode != 0 && pi->followup == PIPE_AND) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3349 | ) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3350 | skip_more_for_this_rword = rword; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3351 | } |
Mike Frysinger | 8ec1c9d | 2009-03-29 00:45:26 +0000 | [diff] [blame] | 3352 | |
| 3353 | check_jobs_and_continue: |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3354 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3355 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3356 | |
| 3357 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3358 | //// if (G.ctrl_z_flag) { |
| 3359 | //// /* ctrl-Z forked somewhere in the past, we are the child, |
| 3360 | //// * and now we completed running the list. Exit. */ |
| 3361 | //////TODO: _exit? |
| 3362 | //// exit(rcode); |
| 3363 | //// } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3364 | ret: |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3365 | G.run_list_level--; |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 3366 | //// if (!G.run_list_level && G_interactive_fd) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3367 | //// signal(SIGTSTP, SIG_IGN); |
| 3368 | //// signal(SIGINT, SIG_IGN); |
| 3369 | //// } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3370 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3371 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 3372 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 3373 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3374 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 3375 | free(for_list); |
| 3376 | #endif |
| 3377 | #if ENABLE_HUSH_CASE |
| 3378 | free(case_word); |
| 3379 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3380 | return rcode; |
| 3381 | } |
| 3382 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3383 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3384 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3385 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3386 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3387 | debug_printf_exec("run_and_free_list entered\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3388 | if (!G.fake_mode) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3389 | debug_printf_exec(": run_list with %d members\n", pi->num_cmds); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3390 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3391 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3392 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3393 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3394 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3395 | free_pipe_list(pi, /* indent: */ 0); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3396 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3397 | return rcode; |
| 3398 | } |
| 3399 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3400 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3401 | /* Peek ahead in the in_str to find out if we have a "&n" construct, |
| 3402 | * as in "2>&1", that represents duplicating a file descriptor. |
| 3403 | * Return either -2 (syntax error), -1 (no &), or the number found. |
| 3404 | */ |
| 3405 | static int redirect_dup_num(struct in_str *input) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3406 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3407 | int ch, d = 0, ok = 0; |
| 3408 | ch = i_peek(input); |
| 3409 | if (ch != '&') return -1; |
| 3410 | |
| 3411 | i_getch(input); /* get the & */ |
| 3412 | ch = i_peek(input); |
| 3413 | if (ch == '-') { |
| 3414 | i_getch(input); |
| 3415 | return -3; /* "-" represents "close me" */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3416 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3417 | while (isdigit(ch)) { |
| 3418 | d = d*10 + (ch-'0'); |
| 3419 | ok = 1; |
| 3420 | i_getch(input); |
| 3421 | ch = i_peek(input); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3422 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3423 | if (ok) return d; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3424 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3425 | bb_error_msg("ambiguous redirect"); |
| 3426 | return -2; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3427 | } |
| 3428 | |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3429 | /* The src parameter allows us to peek forward to a possible &n syntax |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3430 | * for file descriptor duplication, e.g., "2>&1". |
| 3431 | * Return code is 0 normally, 1 if a syntax error is detected in src. |
| 3432 | * Resource errors (in xmalloc) cause the process to exit */ |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 3433 | static int setup_redirect(struct parse_context *ctx, |
| 3434 | int fd, |
| 3435 | redir_type style, |
| 3436 | struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3437 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3438 | struct command *command = ctx->command; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 3439 | struct redir_struct *redir; |
| 3440 | struct redir_struct **redirp; |
| 3441 | int dup_num; |
| 3442 | |
| 3443 | /* Check for a '2>&1' type redirect */ |
| 3444 | dup_num = redirect_dup_num(input); |
| 3445 | if (dup_num == -2) |
| 3446 | return 1; /* syntax error */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3447 | |
| 3448 | /* Create a new redir_struct and drop it onto the end of the linked list */ |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 3449 | redirp = &command->redirects; |
| 3450 | while ((redir = *redirp) != NULL) { |
| 3451 | redirp = &(redir->next); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3452 | } |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 3453 | *redirp = redir = xzalloc(sizeof(*redir)); |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3454 | /* redir->next = NULL; */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3455 | /* redir->rd_filename = NULL; */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3456 | redir->rd_type = style; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3457 | redir->fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3458 | |
| 3459 | debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip); |
| 3460 | |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 3461 | redir->dup = dup_num; |
| 3462 | if (dup_num != -1) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3463 | /* Erik had a check here that the file descriptor in question |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3464 | * is legit; I postpone that to "run time" |
| 3465 | * A "-" representation of "close me" shows up as a -3 here */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3466 | debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup); |
| 3467 | } else { |
| 3468 | /* We do _not_ try to open the file that src points to, |
| 3469 | * since we need to return and let src be expanded first. |
| 3470 | * Set ctx->pending_redirect, so we know what to do at the |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 3471 | * end of the next parsed word. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3472 | ctx->pending_redirect = redir; |
| 3473 | } |
| 3474 | return 0; |
| 3475 | } |
| 3476 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3477 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3478 | static struct pipe *new_pipe(void) |
| 3479 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3480 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3481 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3482 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3483 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3484 | return pi; |
| 3485 | } |
| 3486 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3487 | /* Command (member of a pipe) is complete. The only possible error here |
| 3488 | * is out of memory, in which case xmalloc exits. */ |
| 3489 | static int done_command(struct parse_context *ctx) |
| 3490 | { |
| 3491 | /* The command is really already in the pipe structure, so |
| 3492 | * advance the pipe counter and make a new, null command. */ |
| 3493 | struct pipe *pi = ctx->pipe; |
| 3494 | struct command *command = ctx->command; |
| 3495 | |
| 3496 | if (command) { |
| 3497 | if (command->group == NULL |
| 3498 | && command->argv == NULL |
| 3499 | && command->redirects == NULL |
| 3500 | ) { |
| 3501 | debug_printf_parse("done_command: skipping null cmd, num_cmds=%d\n", pi->num_cmds); |
| 3502 | return pi->num_cmds; |
| 3503 | } |
| 3504 | pi->num_cmds++; |
| 3505 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
| 3506 | } else { |
| 3507 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3508 | } |
| 3509 | |
| 3510 | /* Only real trickiness here is that the uncommitted |
| 3511 | * command structure is not counted in pi->num_cmds. */ |
| 3512 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
| 3513 | command = &pi->cmds[pi->num_cmds]; |
| 3514 | memset(command, 0, sizeof(*command)); |
| 3515 | |
| 3516 | ctx->command = command; |
| 3517 | /* but ctx->pipe and ctx->list_head remain unchanged */ |
| 3518 | |
| 3519 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3520 | } |
| 3521 | |
| 3522 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3523 | { |
| 3524 | int not_null; |
| 3525 | |
| 3526 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3527 | /* Close previous command */ |
| 3528 | not_null = done_command(ctx); |
| 3529 | ctx->pipe->followup = type; |
| 3530 | IF_HAS_KEYWORDS(ctx->pipe->pi_inverted = ctx->ctx_inverted;) |
| 3531 | IF_HAS_KEYWORDS(ctx->ctx_inverted = 0;) |
| 3532 | IF_HAS_KEYWORDS(ctx->pipe->res_word = ctx->ctx_res_w;) |
| 3533 | |
| 3534 | /* Without this check, even just <enter> on command line generates |
| 3535 | * tree of three NOPs (!). Which is harmless but annoying. |
| 3536 | * IOW: it is safe to do it unconditionally. |
| 3537 | * RES_NONE case is for "for a in; do ..." (empty IN set) |
| 3538 | * to work, possibly other cases too. */ |
| 3539 | if (not_null IF_HAS_KEYWORDS(|| ctx->ctx_res_w != RES_NONE)) { |
| 3540 | struct pipe *new_p; |
| 3541 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3542 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3543 | not_null, ctx->ctx_res_w); |
| 3544 | new_p = new_pipe(); |
| 3545 | ctx->pipe->next = new_p; |
| 3546 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3547 | /* RES_THEN, RES_DO etc are "sticky" - |
| 3548 | * they remain set for commands inside if/while. |
| 3549 | * This is used to control execution. |
| 3550 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3551 | * cases where variable or value happens to match a keyword): |
| 3552 | */ |
| 3553 | #if ENABLE_HUSH_LOOPS |
| 3554 | if (ctx->ctx_res_w == RES_FOR |
| 3555 | || ctx->ctx_res_w == RES_IN) |
| 3556 | ctx->ctx_res_w = RES_NONE; |
| 3557 | #endif |
| 3558 | #if ENABLE_HUSH_CASE |
| 3559 | if (ctx->ctx_res_w == RES_MATCH) |
| 3560 | ctx->ctx_res_w = RES_CASEI; |
| 3561 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3562 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3563 | /* Create the memory for command, roughly: |
| 3564 | * ctx->pipe->cmds = new struct command; |
| 3565 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3566 | */ |
| 3567 | done_command(ctx); |
| 3568 | } |
| 3569 | debug_printf_parse("done_pipe return\n"); |
| 3570 | } |
| 3571 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3572 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3573 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3574 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3575 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3576 | /* Create the memory for command, roughly: |
| 3577 | * ctx->pipe->cmds = new struct command; |
| 3578 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3579 | */ |
| 3580 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3583 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3584 | /* If a reserved word is found and processed, parse context is modified |
| 3585 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3586 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3587 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3588 | struct reserved_combo { |
| 3589 | char literal[6]; |
| 3590 | unsigned char res; |
| 3591 | unsigned char assignment_flag; |
| 3592 | int flag; |
| 3593 | }; |
| 3594 | enum { |
| 3595 | FLAG_END = (1 << RES_NONE ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3596 | #if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3597 | FLAG_IF = (1 << RES_IF ), |
| 3598 | FLAG_THEN = (1 << RES_THEN ), |
| 3599 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3600 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3601 | FLAG_FI = (1 << RES_FI ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3602 | #endif |
| 3603 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3604 | FLAG_FOR = (1 << RES_FOR ), |
| 3605 | FLAG_WHILE = (1 << RES_WHILE), |
| 3606 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3607 | FLAG_DO = (1 << RES_DO ), |
| 3608 | FLAG_DONE = (1 << RES_DONE ), |
| 3609 | FLAG_IN = (1 << RES_IN ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3610 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3611 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3612 | FLAG_MATCH = (1 << RES_MATCH), |
| 3613 | FLAG_ESAC = (1 << RES_ESAC ), |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3614 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3615 | FLAG_START = (1 << RES_XXXX ), |
| 3616 | }; |
| 3617 | |
| 3618 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3619 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3620 | /* Mostly a list of accepted follow-up reserved words. |
| 3621 | * FLAG_END means we are done with the sequence, and are ready |
| 3622 | * to turn the compound list into a command. |
| 3623 | * FLAG_START means the word must start a new compound list. |
| 3624 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3625 | static const struct reserved_combo reserved_list[] = { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3626 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3627 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3628 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 3629 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3630 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 3631 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 3632 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3633 | #endif |
| 3634 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3635 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3636 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 3637 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 3638 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3639 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 3640 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3641 | #endif |
| 3642 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3643 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3644 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3645 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3646 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3647 | const struct reserved_combo *r; |
| 3648 | |
| 3649 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 3650 | if (strcmp(word->data, r->literal) == 0) |
| 3651 | return r; |
| 3652 | } |
| 3653 | return NULL; |
| 3654 | } |
| 3655 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 3656 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3657 | #if ENABLE_HUSH_CASE |
| 3658 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3659 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3660 | }; |
| 3661 | #endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3662 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3663 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3664 | r = match_reserved_word(word); |
| 3665 | if (!r) |
| 3666 | return 0; |
| 3667 | |
| 3668 | 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] | 3669 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3670 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE) |
| 3671 | /* "case word IN ..." - IN part starts first match part */ |
| 3672 | r = &reserved_match; |
| 3673 | else |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3674 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3675 | if (r->flag == 0) { /* '!' */ |
| 3676 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3677 | syntax("! ! command"); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3678 | IF_HAS_KEYWORDS(ctx->ctx_res_w = RES_SNTX;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3679 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3680 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3681 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3682 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3683 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3684 | struct parse_context *old; |
| 3685 | old = xmalloc(sizeof(*old)); |
| 3686 | debug_printf_parse("push stack %p\n", old); |
| 3687 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3688 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3689 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3690 | } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3691 | syntax(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3692 | ctx->ctx_res_w = RES_SNTX; |
| 3693 | return 1; |
| 3694 | } |
| 3695 | ctx->ctx_res_w = r->res; |
| 3696 | ctx->old_flag = r->flag; |
| 3697 | if (ctx->old_flag & FLAG_END) { |
| 3698 | struct parse_context *old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3699 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3700 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3701 | old = ctx->stack; |
| 3702 | old->command->group = ctx->list_head; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3703 | old->command->grp_type = GRP_NORMAL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3704 | #if !BB_MMU |
| 3705 | o_addstr(&old->as_string, ctx->as_string.data); |
| 3706 | o_free_unsafe(&ctx->as_string); |
| 3707 | old->command->group_as_string = xstrdup(old->as_string.data); |
| 3708 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 3709 | old->command->group_as_string); |
| 3710 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3711 | *ctx = *old; /* physical copy */ |
| 3712 | free(old); |
| 3713 | } |
| 3714 | word->o_assignment = r->assignment_flag; |
| 3715 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3716 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3717 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3718 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3719 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3720 | * Normal return is 0. Syntax errors return 1. |
| 3721 | * Note: on return, word is reset, but not o_free'd! |
| 3722 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3723 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3724 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3725 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3726 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3727 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3728 | if (word->length == 0 && word->nonnull == 0) { |
| 3729 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3730 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3731 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3732 | /* If this word wasn't an assignment, next ones definitely |
| 3733 | * can't be assignments. Even if they look like ones. */ |
| 3734 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3735 | && word->o_assignment != WORD_IS_KEYWORD |
| 3736 | ) { |
| 3737 | word->o_assignment = NOT_ASSIGNMENT; |
| 3738 | } else { |
| 3739 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3740 | command->assignment_cnt++; |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3741 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3742 | } |
| 3743 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3744 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3745 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3746 | * only if run as "bash", not "sh" */ |
| 3747 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3748 | word->o_assignment = NOT_ASSIGNMENT; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3749 | debug_printf("word stored in rd_filename: '%s'\n", word->data); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3750 | } else { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3751 | /* "{ echo foo; } echo bar" - bad */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3752 | /* NB: bash allows e.g.: |
| 3753 | * if true; then { echo foo; } fi |
| 3754 | * while if false; then false; fi do break; done |
| 3755 | * TODO? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3756 | if (command->group) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3757 | syntax(word->data); |
| 3758 | debug_printf_parse("done_word return 1: syntax error, " |
| 3759 | "groups and arglists don't mix\n"); |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3760 | return 1; |
| 3761 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3762 | #if HAS_KEYWORDS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3763 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3764 | if (ctx->ctx_dsemicolon |
| 3765 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3766 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3767 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3768 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3769 | ctx->ctx_dsemicolon = 0; |
| 3770 | } else |
| 3771 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3772 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3773 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3774 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3775 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3776 | #endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3777 | ) { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3778 | debug_printf_parse(": checking '%s' for reserved-ness\n", word->data); |
| 3779 | if (reserved_word(word, ctx)) { |
| 3780 | o_reset(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3781 | debug_printf_parse("done_word return %d\n", |
| 3782 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3783 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3784 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3785 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3786 | #endif |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3787 | if (word->nonnull /* word had "xx" or 'xx' at least as part of it. */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3788 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3789 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3790 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3791 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3792 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3793 | char *p = word->data; |
| 3794 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3795 | && (p[1] & 0x7f) == '@' |
| 3796 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3797 | ) { |
| 3798 | p += 3; |
| 3799 | } |
| 3800 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3801 | /* saw no "$@", or not only "$@" but some |
| 3802 | * real text is there too */ |
| 3803 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3804 | * e.g. "", $empty"" etc to not disappear */ |
| 3805 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3806 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3807 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3808 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3809 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3810 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3811 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3812 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3813 | o_reset(word); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3814 | ctx->pending_redirect = NULL; |
| 3815 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3816 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3817 | /* Force FOR to have just one word (variable name) */ |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3818 | /* NB: basically, this makes hush see "for v in ..." syntax as if |
| 3819 | * as it is "for v; in ...". FOR and IN become two pipe structs |
| 3820 | * in parse tree. */ |
| 3821 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3822 | //TODO: check that command->argv[0] is a valid variable name! |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3823 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3824 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3825 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3826 | #if ENABLE_HUSH_CASE |
| 3827 | /* Force CASE to have just one word */ |
| 3828 | if (ctx->ctx_res_w == RES_CASE) { |
| 3829 | done_pipe(ctx, PIPE_SEQ); |
| 3830 | } |
| 3831 | #endif |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3832 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3833 | return 0; |
| 3834 | } |
| 3835 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3836 | /* If a redirect is immediately preceded by a number, that number is |
| 3837 | * supposed to tell which file descriptor to redirect. This routine |
| 3838 | * looks for such preceding numbers. In an ideal world this routine |
| 3839 | * needs to handle all the following classes of redirects... |
| 3840 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3841 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3842 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3843 | * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo |
| 3844 | * A -1 output from this program means no valid number was found, so the |
| 3845 | * caller should use the appropriate default for this redirection. |
| 3846 | */ |
| 3847 | static int redirect_opt_num(o_string *o) |
| 3848 | { |
| 3849 | int num; |
| 3850 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3851 | if (o->length == 0) |
| 3852 | return -1; |
| 3853 | for (num = 0; num < o->length; num++) { |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3854 | if (!isdigit(o->data[num])) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3855 | return -1; |
| 3856 | } |
| 3857 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3858 | num = atoi(o->data); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3859 | o_reset(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3860 | return num; |
| 3861 | } |
| 3862 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3863 | #if BB_MMU |
| 3864 | #define parse_stream(pstring, input, end_trigger) \ |
| 3865 | parse_stream(input, end_trigger) |
| 3866 | #endif |
| 3867 | static struct pipe *parse_stream(char **pstring, |
| 3868 | struct in_str *input, |
| 3869 | int end_trigger); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 3870 | static void parse_and_run_string(const char *s); |
Mike Frysinger | ddbee97 | 2009-03-22 22:48:41 +0000 | [diff] [blame] | 3871 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3872 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 3873 | static FILE *generate_stream_from_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3874 | { |
| 3875 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3876 | int pid, channel[2]; |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3877 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 3878 | xpipe(channel); |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 3879 | pid = BB_MMU ? fork() : vfork(); |
| 3880 | if (pid < 0) |
| 3881 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 3882 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3883 | if (pid == 0) { /* child */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 3884 | #if ENABLE_HUSH_JOB |
| 3885 | die_sleep = 0; /* do not restore tty pgrp on xfunc death */ |
| 3886 | #endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3887 | /* Process substitution is not considered to be usual |
| 3888 | * 'command execution'. |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 3889 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 3890 | */ |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 3891 | bb_signals(0 |
| 3892 | + (1 << SIGTSTP) |
| 3893 | + (1 << SIGTTIN) |
| 3894 | + (1 << SIGTTOU) |
| 3895 | , SIG_IGN); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 3896 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 3897 | xmove_fd(channel[1], 1); |
| 3898 | /* Prevent it from trying to handle ctrl-z etc */ |
| 3899 | USE_HUSH_JOB(G.run_list_level = 1;) |
| 3900 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3901 | reset_traps_to_defaults(); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 3902 | parse_and_run_string(s); |
| 3903 | _exit(G.last_return_code); |
| 3904 | #else |
| 3905 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 3906 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 3907 | * huge=`cat BIG` # was blocking here forever |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 3908 | * echo OK |
| 3909 | */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3910 | re_execute_shell(s); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 3911 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3912 | } |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 3913 | |
| 3914 | /* parent */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 3915 | #if ENABLE_HUSH_JOB |
| 3916 | die_sleep = -1; /* restore tty pgrp on xfunc death */ |
| 3917 | #endif |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 3918 | clean_up_after_re_execute(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3919 | close(channel[1]); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3920 | pf = fdopen(channel[0], "r"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3921 | return pf; |
| 3922 | } |
| 3923 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3924 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 3925 | static int process_command_subs(o_string *dest, const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3926 | { |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 3927 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3928 | struct in_str pipe_str; |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 3929 | int ch, eol_cnt; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3930 | |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 3931 | pf = generate_stream_from_string(s); |
| 3932 | if (pf == NULL) |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3933 | return 1; |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 3934 | close_on_exec_on(fileno(pf)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3935 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3936 | /* Now send results of command back into original context */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 3937 | setup_file_in_str(&pipe_str, pf); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3938 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3939 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3940 | if (ch == '\n') { |
| 3941 | eol_cnt++; |
| 3942 | continue; |
| 3943 | } |
| 3944 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3945 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3946 | eol_cnt--; |
| 3947 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3948 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3949 | } |
| 3950 | |
| 3951 | debug_printf("done reading from pipe, pclose()ing\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3952 | /* Note: we got EOF, and we just close the read end of the pipe. |
| 3953 | * 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] | 3954 | * Try these: |
| 3955 | * echo `echo Hi; exec 1>&-; sleep 2` - bash waits 2 sec |
| 3956 | * `false`; echo $? - bash outputs "1" |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3957 | */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 3958 | fclose(pf); |
| 3959 | debug_printf("closed FILE from child. return 0\n"); |
| 3960 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3961 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3962 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3963 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3964 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3965 | struct in_str *input, int ch) |
| 3966 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3967 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 3968 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3969 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3970 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3971 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3972 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3973 | |
| 3974 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3975 | #if ENABLE_HUSH_FUNCTIONS |
| 3976 | if (ch == 'F') { /* function definition? */ |
| 3977 | bb_error_msg("aha '%s' is a function, parsing it...", dest->data); |
| 3978 | //command->fname = dest->data; |
| 3979 | command->grp_type = GRP_FUNCTION; |
| 3980 | //TODO: review every o_reset() location... do they handle all o_string fields correctly? |
| 3981 | memset(dest, 0, sizeof(*dest)); |
| 3982 | } |
| 3983 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3984 | if (command->argv /* word [word](... */ |
| 3985 | || dest->length /* word(... */ |
| 3986 | || dest->nonnull /* ""(... */ |
| 3987 | ) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3988 | syntax(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3989 | debug_printf_parse("parse_group return 1: " |
| 3990 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3991 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3992 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3993 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3994 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3995 | endch = ')'; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3996 | command->grp_type = GRP_SUBSHELL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3997 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3998 | { |
| 3999 | #if !BB_MMU |
| 4000 | char *as_string = NULL; |
| 4001 | #endif |
| 4002 | pipe_list = parse_stream(&as_string, input, endch); |
| 4003 | #if !BB_MMU |
| 4004 | if (as_string) |
| 4005 | o_addstr(&ctx->as_string, as_string); |
| 4006 | #endif |
| 4007 | /* empty ()/{} or parse error? */ |
| 4008 | if (!pipe_list || pipe_list == ERR_PTR) { |
| 4009 | #if !BB_MMU |
| 4010 | free(as_string); |
| 4011 | #endif |
| 4012 | syntax(NULL); |
| 4013 | debug_printf_parse("parse_group return 1: " |
| 4014 | "parse_stream returned %p\n", pipe_list); |
| 4015 | return 1; |
| 4016 | } |
| 4017 | command->group = pipe_list; |
| 4018 | #if !BB_MMU |
| 4019 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 4020 | command->group_as_string = as_string; |
| 4021 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 4022 | command->group_as_string); |
| 4023 | #endif |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4024 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4025 | debug_printf_parse("parse_group return 0\n"); |
| 4026 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4027 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4030 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4031 | /* Subroutines for copying $(...) and `...` things */ |
| 4032 | static void add_till_backquote(o_string *dest, struct in_str *input); |
| 4033 | /* '...' */ |
| 4034 | static void add_till_single_quote(o_string *dest, struct in_str *input) |
| 4035 | { |
| 4036 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4037 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4038 | if (ch == EOF) |
| 4039 | break; |
| 4040 | if (ch == '\'') |
| 4041 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4042 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4043 | } |
| 4044 | } |
| 4045 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
| 4046 | static void add_till_double_quote(o_string *dest, struct in_str *input) |
| 4047 | { |
| 4048 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4049 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4050 | if (ch == '"') |
| 4051 | break; |
| 4052 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4053 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4054 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4055 | } |
| 4056 | if (ch == EOF) |
| 4057 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4058 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4059 | if (ch == '`') { |
| 4060 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4061 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4062 | continue; |
| 4063 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4064 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4065 | } |
| 4066 | } |
| 4067 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 4068 | * \` quoting. |
| 4069 | * "Within the backquoted style of command substitution, backslash |
| 4070 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 4071 | * The search for the matching backquote shall be satisfied by the first |
| 4072 | * backquote found without a preceding backslash; during this search, |
| 4073 | * if a non-escaped backquote is encountered within a shell comment, |
| 4074 | * a here-document, an embedded command substitution of the $(command) |
| 4075 | * form, or a quoted string, undefined results occur. A single-quoted |
| 4076 | * or double-quoted string that begins, but does not end, within the |
| 4077 | * "`...`" sequence produces undefined results." |
| 4078 | * Example Output |
| 4079 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 4080 | */ |
| 4081 | static void add_till_backquote(o_string *dest, struct in_str *input) |
| 4082 | { |
| 4083 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4084 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4085 | if (ch == '`') |
| 4086 | break; |
| 4087 | if (ch == '\\') { /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4088 | int ch2 = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4089 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4090 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4091 | ch = ch2; |
| 4092 | } |
| 4093 | if (ch == EOF) |
| 4094 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4095 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4096 | } |
| 4097 | } |
| 4098 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 4099 | * quoting and nested ()s. |
| 4100 | * "With the $(command) style of command substitution, all characters |
| 4101 | * following the open parenthesis to the matching closing parenthesis |
| 4102 | * constitute the command. Any valid shell script can be used for command, |
| 4103 | * except a script consisting solely of redirections which produces |
| 4104 | * unspecified results." |
| 4105 | * Example Output |
| 4106 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 4107 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 4108 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 4109 | */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4110 | 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] | 4111 | { |
| 4112 | int count = 0; |
| 4113 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4114 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4115 | if (ch == EOF) |
| 4116 | break; |
| 4117 | if (ch == '(') |
| 4118 | count++; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4119 | if (ch == ')') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4120 | if (--count < 0) { |
| 4121 | if (!dbl) |
| 4122 | break; |
| 4123 | if (i_peek(input) == ')') { |
| 4124 | i_getch(input); |
| 4125 | break; |
| 4126 | } |
| 4127 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4128 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4129 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4130 | if (ch == '\'') { |
| 4131 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4132 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4133 | continue; |
| 4134 | } |
| 4135 | if (ch == '"') { |
| 4136 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4137 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4138 | continue; |
| 4139 | } |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4140 | if (ch == '\\') { /* \x. Copy verbatim. Important for \(, \) */ |
| 4141 | ch = i_getch(input); |
| 4142 | if (ch == EOF) |
| 4143 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4144 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4145 | continue; |
| 4146 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4147 | } |
| 4148 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4149 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4150 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4151 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4152 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4153 | #define handle_dollar(as_string, dest, input) \ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4154 | handle_dollar(dest, input) |
| 4155 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4156 | static int handle_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4157 | o_string *dest, |
| 4158 | struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4159 | { |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 4160 | int expansion; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4161 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 4162 | unsigned char quote_mask = dest->o_escape ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4163 | |
| 4164 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4165 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4166 | ch = i_getch(input); |
| 4167 | #if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4168 | if (as_string) o_addchr(as_string, ch); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4169 | #endif |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 4170 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4171 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4172 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4173 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4174 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4175 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4176 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4177 | if (!isalnum(ch) && ch != '_') |
| 4178 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4179 | ch = i_getch(input); |
| 4180 | #if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4181 | if (as_string) o_addchr(as_string, ch); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4182 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4183 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4184 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4185 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4186 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4187 | ch = i_getch(input); |
| 4188 | #if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4189 | if (as_string) o_addchr(as_string, ch); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4190 | #endif |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4191 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4192 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4193 | o_addchr(dest, ch | quote_mask); |
| 4194 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4195 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4196 | case '$': /* pid */ |
| 4197 | case '!': /* last bg pid */ |
| 4198 | case '?': /* last exit code */ |
| 4199 | case '#': /* number of args */ |
| 4200 | case '*': /* args */ |
| 4201 | case '@': /* args */ |
| 4202 | goto make_one_char_var; |
| 4203 | case '{': { |
| 4204 | bool first_char, all_digits; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 4205 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4206 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4207 | ch = i_getch(input); |
| 4208 | #if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4209 | if (as_string) o_addchr(as_string, ch); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4210 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4211 | /* XXX maybe someone will try to escape the '}' */ |
| 4212 | expansion = 0; |
| 4213 | first_char = true; |
| 4214 | all_digits = false; |
| 4215 | while (1) { |
| 4216 | ch = i_getch(input); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4217 | #if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4218 | if (as_string) o_addchr(as_string, ch); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4219 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4220 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4221 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4222 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4223 | if (first_char) { |
| 4224 | if (ch == '#') |
| 4225 | /* ${#var}: length of var contents */ |
| 4226 | goto char_ok; |
| 4227 | else if (isdigit(ch)) { |
| 4228 | all_digits = true; |
| 4229 | goto char_ok; |
| 4230 | } |
| 4231 | } |
| 4232 | |
| 4233 | if (expansion < 2 |
| 4234 | && ( (all_digits && !isdigit(ch)) |
| 4235 | || (!all_digits && !isalnum(ch) && ch != '_') |
| 4236 | ) |
| 4237 | ) { |
| 4238 | /* handle parameter expansions |
| 4239 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 4240 | */ |
| 4241 | if (first_char) |
| 4242 | goto case_default; |
| 4243 | switch (ch) { |
| 4244 | case ':': /* null modifier */ |
| 4245 | if (expansion == 0) { |
| 4246 | debug_printf_parse(": null modifier\n"); |
| 4247 | ++expansion; |
| 4248 | break; |
| 4249 | } |
| 4250 | goto case_default; |
| 4251 | #if 0 /* not implemented yet :( */ |
| 4252 | case '#': /* remove prefix */ |
| 4253 | case '%': /* remove suffix */ |
| 4254 | if (expansion == 0) { |
| 4255 | debug_printf_parse(": remove suffix/prefix\n"); |
| 4256 | expansion = 2; |
| 4257 | break; |
| 4258 | } |
| 4259 | goto case_default; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4260 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4261 | case '-': /* default value */ |
| 4262 | case '=': /* assign default */ |
| 4263 | case '+': /* alternative */ |
| 4264 | case '?': /* error indicate */ |
| 4265 | debug_printf_parse(": parameter expansion\n"); |
| 4266 | expansion = 2; |
| 4267 | break; |
| 4268 | default: |
| 4269 | case_default: |
| 4270 | syntax("unterminated ${name}"); |
| 4271 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 4272 | return 1; |
| 4273 | } |
| 4274 | } |
| 4275 | char_ok: |
| 4276 | debug_printf_parse(": '%c'\n", ch); |
| 4277 | o_addchr(dest, ch | quote_mask); |
| 4278 | quote_mask = 0; |
| 4279 | first_char = false; |
| 4280 | } |
| 4281 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4282 | break; |
| 4283 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4284 | #if (ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK) |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4285 | case '(': { |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4286 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4287 | int pos; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4288 | # endif |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4289 | ch = i_getch(input); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4290 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4291 | if (as_string) o_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4292 | # endif |
| 4293 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4294 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4295 | ch = i_getch(input); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4296 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4297 | if (as_string) o_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4298 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4299 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4300 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4301 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4302 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4303 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4304 | add_till_closing_paren(dest, input, true); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4305 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4306 | if (as_string) { |
| 4307 | o_addstr(as_string, dest->data + pos); |
| 4308 | o_addchr(as_string, ')'); |
| 4309 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4310 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4311 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4312 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4313 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4314 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4315 | # endif |
| 4316 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4317 | //int pos = dest->length; |
| 4318 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4319 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4320 | # if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4321 | pos = dest->length; |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4322 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4323 | add_till_closing_paren(dest, input, false); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4324 | # if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4325 | if (as_string) { |
| 4326 | o_addstr(as_string, dest->data + pos); |
| 4327 | o_addchr(as_string, '`'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4328 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4329 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4330 | //debug_printf_subst("SUBST RES2 '%s'\n", dest->data + pos); |
| 4331 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4332 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4333 | break; |
| 4334 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4335 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4336 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4337 | ch = i_getch(input); |
| 4338 | #if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4339 | if (as_string) o_addchr(as_string, ch); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4340 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4341 | ch = i_peek(input); |
| 4342 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 4343 | ch = '_'; |
| 4344 | goto make_var; |
| 4345 | } |
| 4346 | /* else: it's $_ */ |
| 4347 | /* TODO: */ |
| 4348 | /* $_ Shell or shell script name; or last cmd name */ |
| 4349 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 4350 | default: |
| 4351 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4352 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4353 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4354 | return 0; |
| 4355 | } |
| 4356 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4357 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4358 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4359 | parse_stream_dquoted(dest, input, dquote_end) |
| 4360 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4361 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4362 | o_string *dest, |
| 4363 | struct in_str *input, |
| 4364 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4365 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4366 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4367 | int next; |
| 4368 | |
| 4369 | again: |
| 4370 | ch = i_getch(input); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4371 | #if !BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4372 | if (as_string && ch != EOF) |
| 4373 | o_addchr(as_string, ch); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4374 | #endif |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4375 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
| 4376 | dest->nonnull = 1; |
| 4377 | if (dest->o_assignment == NOT_ASSIGNMENT) |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 4378 | dest->o_escape ^= 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4379 | debug_printf_parse("parse_stream_dquoted return 0\n"); |
| 4380 | return 0; |
| 4381 | } |
| 4382 | if (ch == EOF) { |
| 4383 | syntax("unterminated \""); |
| 4384 | debug_printf_parse("parse_stream_dquoted return 1: unterminated \"\n"); |
| 4385 | return 1; |
| 4386 | } |
| 4387 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4388 | if (ch != '\n') { |
| 4389 | next = i_peek(input); |
| 4390 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4391 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 4392 | ch, ch, dest->o_escape); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4393 | if (ch == '\\') { |
| 4394 | if (next == EOF) { |
| 4395 | syntax("\\<eof>"); |
| 4396 | debug_printf_parse("parse_stream_dquoted return 1: \\<eof>\n"); |
| 4397 | return 1; |
| 4398 | } |
| 4399 | /* bash: |
| 4400 | * "The backslash retains its special meaning [in "..."] |
| 4401 | * only when followed by one of the following characters: |
| 4402 | * $, `, ", \, or <newline>. A double quote may be quoted |
| 4403 | * within double quotes by preceding it with a backslash. |
| 4404 | * If enabled, history expansion will be performed unless |
| 4405 | * an ! appearing in double quotes is escaped using |
| 4406 | * a backslash. The backslash preceding the ! is not removed." |
| 4407 | */ |
| 4408 | if (strchr("$`\"\\", next) != NULL) { |
| 4409 | o_addqchr(dest, i_getch(input)); |
| 4410 | } else { |
| 4411 | o_addqchr(dest, '\\'); |
| 4412 | } |
| 4413 | goto again; |
| 4414 | } |
| 4415 | if (ch == '$') { |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4416 | if (handle_dollar(as_string, dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4417 | debug_printf_parse("parse_stream_dquoted return 1: " |
| 4418 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4419 | return 1; |
| 4420 | } |
| 4421 | goto again; |
| 4422 | } |
| 4423 | #if ENABLE_HUSH_TICK |
| 4424 | if (ch == '`') { |
| 4425 | //int pos = dest->length; |
| 4426 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4427 | o_addchr(dest, 0x80 | '`'); |
| 4428 | add_till_backquote(dest, input); |
| 4429 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4430 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4431 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4432 | } |
| 4433 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4434 | o_addQchr(dest, ch); |
| 4435 | if (ch == '=' |
| 4436 | && (dest->o_assignment == MAYBE_ASSIGNMENT |
| 4437 | || dest->o_assignment == WORD_IS_KEYWORD) |
| 4438 | && is_assignment(dest->data) |
| 4439 | ) { |
| 4440 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 4441 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4442 | goto again; |
| 4443 | } |
| 4444 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4445 | /* |
| 4446 | * Scan input until EOF or end_trigger char. |
| 4447 | * Return a list of pipes to execute, or NULL on EOF |
| 4448 | * or if end_trigger character is met. |
| 4449 | * On syntax error, exit is shell is not interactive, |
| 4450 | * reset parsing machinery and start parsing anew, |
| 4451 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4452 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4453 | static struct pipe *parse_stream(char **pstring, |
| 4454 | struct in_str *input, |
| 4455 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4456 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4457 | struct parse_context ctx; |
| 4458 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4459 | int is_in_dquote; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4460 | |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 4461 | /* Double-quote state is handled in the state variable is_in_dquote. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4462 | * 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] | 4463 | * found. When recursing, quote state is passed in via dest->o_escape. |
| 4464 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4465 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
| 4466 | end_trigger ? : 'X'); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4467 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4468 | G.ifs = get_local_var_value("IFS"); |
| 4469 | if (G.ifs == NULL) |
| 4470 | G.ifs = " \t\n"; |
| 4471 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4472 | reset: |
| 4473 | #if ENABLE_HUSH_INTERACTIVE |
| 4474 | input->promptmode = 0; /* PS1 */ |
| 4475 | #endif |
| 4476 | /* dest.o_assignment = MAYBE_ASSIGNMENT; - already is */ |
| 4477 | initialize_context(&ctx); |
| 4478 | is_in_dquote = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4479 | while (1) { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4480 | const char *is_ifs; |
| 4481 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4482 | int ch; |
| 4483 | int next; |
| 4484 | int redir_fd; |
| 4485 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4486 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4487 | if (is_in_dquote) { |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4488 | if (parse_stream_dquoted(&ctx.as_string, &dest, input, '"')) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4489 | goto parse_error; |
| 4490 | } |
| 4491 | /* We reached closing '"' */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4492 | is_in_dquote = 0; |
| 4493 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4494 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4495 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 4496 | ch, ch, dest.o_escape); |
| 4497 | if (ch == EOF) { |
| 4498 | struct pipe *pi; |
| 4499 | if (done_word(&dest, &ctx)) { |
| 4500 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4501 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4502 | o_free(&dest); |
| 4503 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4504 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4505 | /* If we got nothing... */ |
| 4506 | // TODO: test script consisting of just "&" |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4507 | if (pi->num_cmds == 0 |
| 4508 | IF_HAS_KEYWORDS( && pi->res_word == RES_NONE) |
| 4509 | ) { |
| 4510 | free_pipe_list(pi, 0); |
| 4511 | pi = NULL; |
| 4512 | } |
| 4513 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4514 | #if !BB_MMU |
| 4515 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 4516 | if (pstring) |
| 4517 | *pstring = ctx.as_string.data; |
| 4518 | else |
| 4519 | o_free_unsafe(&ctx.as_string); |
| 4520 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4521 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4522 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4523 | #if !BB_MMU |
| 4524 | o_addchr(&ctx.as_string, ch); |
| 4525 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4526 | is_ifs = strchr(G.ifs, ch); |
| 4527 | is_special = strchr("<>;&|(){}#'" /* special outside of "str" */ |
| 4528 | "\\$\"" USE_HUSH_TICK("`") /* always special */ |
| 4529 | , ch); |
| 4530 | |
| 4531 | if (!is_special && !is_ifs) { /* ordinary char */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4532 | o_addQchr(&dest, ch); |
| 4533 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 4534 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4535 | && ch == '=' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4536 | && is_assignment(dest.data) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4537 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4538 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4539 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4540 | continue; |
| 4541 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4542 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4543 | if (is_ifs) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4544 | if (done_word(&dest, &ctx)) { |
| 4545 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 4546 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4547 | if (ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4548 | #if ENABLE_HUSH_CASE |
| 4549 | /* "case ... in <newline> word) ..." - |
| 4550 | * newlines are ignored (but ';' wouldn't be) */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4551 | if (ctx.command->argv == NULL |
| 4552 | && ctx.ctx_res_w == RES_MATCH |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4553 | ) { |
| 4554 | continue; |
| 4555 | } |
| 4556 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4557 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4558 | done_pipe(&ctx, PIPE_SEQ); |
| 4559 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4560 | ch = ';'; |
Denis Vlasenko | 7c98612 | 2009-04-04 12:15:42 +0000 | [diff] [blame] | 4561 | /* note: if (is_ifs) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4562 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4563 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4564 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4565 | if (end_trigger && end_trigger == ch) { |
| 4566 | //TODO: disallow "{ cmd }" without semicolon |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4567 | if (done_word(&dest, &ctx)) { |
| 4568 | goto parse_error; |
| 4569 | } |
| 4570 | done_pipe(&ctx, PIPE_SEQ); |
| 4571 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4572 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4573 | if (!HAS_KEYWORDS |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4574 | 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] | 4575 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4576 | debug_printf_parse("parse_stream return %p: " |
| 4577 | "end_trigger char found\n", |
| 4578 | ctx.list_head); |
| 4579 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4580 | #if !BB_MMU |
| 4581 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 4582 | if (pstring) |
| 4583 | *pstring = ctx.as_string.data; |
| 4584 | else |
| 4585 | o_free_unsafe(&ctx.as_string); |
| 4586 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4587 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4588 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4589 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4590 | if (is_ifs) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4591 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4592 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4593 | if (dest.o_assignment == MAYBE_ASSIGNMENT) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4594 | /* ch is a special char and thus this word |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4595 | * cannot be an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4596 | dest.o_assignment = NOT_ASSIGNMENT; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4599 | next = '\0'; |
| 4600 | if (ch != '\n') { |
| 4601 | next = i_peek(input); |
| 4602 | } |
| 4603 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4604 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4605 | case '#': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4606 | if (dest.length == 0) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4607 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4608 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4609 | if (ch == EOF || ch == '\n') |
| 4610 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4611 | i_getch(input); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4612 | /* note: we do not add it to &ctx.as_string */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4613 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4614 | #if !BB_MMU |
| 4615 | //TODO: go back one char? |
| 4616 | o_addchr(&ctx.as_string, '\n'); |
| 4617 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4618 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4619 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4620 | } |
| 4621 | break; |
| 4622 | case '\\': |
| 4623 | if (next == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4624 | syntax("\\<eof>"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4625 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4626 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4627 | o_addchr(&dest, '\\'); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4628 | ch = i_getch(input); |
| 4629 | o_addchr(&dest, ch); |
| 4630 | #if !BB_MMU |
| 4631 | o_addchr(&ctx.as_string, ch); |
| 4632 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4633 | break; |
| 4634 | case '$': |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4635 | if (handle_dollar(&ctx.as_string, &dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4636 | debug_printf_parse("parse_stream parse error: " |
| 4637 | "handle_dollar returned non-0\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4638 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4639 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4640 | break; |
| 4641 | case '\'': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4642 | dest.nonnull = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4643 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4644 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4645 | if (ch == EOF) { |
| 4646 | syntax("unterminated '"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4647 | goto parse_error; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4648 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4649 | #if !BB_MMU |
| 4650 | o_addchr(&ctx.as_string, ch); |
| 4651 | #endif |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4652 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4653 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4654 | if (dest.o_assignment == NOT_ASSIGNMENT) |
| 4655 | o_addqchr(&dest, ch); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4656 | else |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4657 | o_addchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4658 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4659 | break; |
| 4660 | case '"': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4661 | dest.nonnull = 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4662 | is_in_dquote ^= 1; /* invert */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4663 | if (dest.o_assignment == NOT_ASSIGNMENT) |
| 4664 | dest.o_escape ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4665 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4666 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4667 | case '`': { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4668 | //int pos = dest.length; |
| 4669 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4670 | o_addchr(&dest, '`'); |
| 4671 | add_till_backquote(&dest, input); |
| 4672 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4673 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4674 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4675 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4676 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4677 | case '>': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4678 | redir_fd = redirect_opt_num(&dest); |
| 4679 | if (done_word(&dest, &ctx)) { |
| 4680 | goto parse_error; |
| 4681 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4682 | redir_style = REDIRECT_OVERWRITE; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4683 | if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4684 | redir_style = REDIRECT_APPEND; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4685 | ch = i_getch(input); |
| 4686 | #if !BB_MMU |
| 4687 | o_addchr(&ctx.as_string, ch); |
| 4688 | #endif |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4689 | } |
| 4690 | #if 0 |
| 4691 | else if (next == '(') { |
| 4692 | syntax(">(process) not supported"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4693 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4694 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4695 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4696 | setup_redirect(&ctx, redir_fd, redir_style, input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4697 | break; |
| 4698 | case '<': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4699 | redir_fd = redirect_opt_num(&dest); |
| 4700 | if (done_word(&dest, &ctx)) { |
| 4701 | goto parse_error; |
| 4702 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4703 | redir_style = REDIRECT_INPUT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4704 | if (next == '<') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4705 | redir_style = REDIRECT_HEREIS; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4706 | ch = i_getch(input); |
| 4707 | #if !BB_MMU |
| 4708 | o_addchr(&ctx.as_string, ch); |
| 4709 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4710 | } else if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4711 | redir_style = REDIRECT_IO; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4712 | ch = i_getch(input); |
| 4713 | #if !BB_MMU |
| 4714 | o_addchr(&ctx.as_string, ch); |
| 4715 | #endif |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4716 | } |
| 4717 | #if 0 |
| 4718 | else if (next == '(') { |
| 4719 | syntax("<(process) not supported"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4720 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4721 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4722 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4723 | setup_redirect(&ctx, redir_fd, redir_style, input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4724 | break; |
| 4725 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4726 | #if ENABLE_HUSH_CASE |
| 4727 | case_semi: |
| 4728 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4729 | if (done_word(&dest, &ctx)) { |
| 4730 | goto parse_error; |
| 4731 | } |
| 4732 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4733 | #if ENABLE_HUSH_CASE |
| 4734 | /* Eat multiple semicolons, detect |
| 4735 | * whether it means something special */ |
| 4736 | while (1) { |
| 4737 | ch = i_peek(input); |
| 4738 | if (ch != ';') |
| 4739 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4740 | ch = i_getch(input); |
| 4741 | #if !BB_MMU |
| 4742 | o_addchr(&ctx.as_string, ch); |
| 4743 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4744 | if (ctx.ctx_res_w == RES_CASEI) { |
| 4745 | ctx.ctx_dsemicolon = 1; |
| 4746 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4747 | break; |
| 4748 | } |
| 4749 | } |
| 4750 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4751 | new_cmd: |
| 4752 | /* We just finished a cmd. New one may start |
| 4753 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4754 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4755 | break; |
| 4756 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4757 | if (done_word(&dest, &ctx)) { |
| 4758 | goto parse_error; |
| 4759 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4760 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4761 | ch = i_getch(input); |
| 4762 | #if !BB_MMU |
| 4763 | o_addchr(&ctx.as_string, ch); |
| 4764 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4765 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4766 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4767 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4768 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4769 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4770 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4771 | if (done_word(&dest, &ctx)) { |
| 4772 | goto parse_error; |
| 4773 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4774 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4775 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4776 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4777 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4778 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4779 | ch = i_getch(input); |
| 4780 | #if !BB_MMU |
| 4781 | o_addchr(&ctx.as_string, ch); |
| 4782 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4783 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4784 | } else { |
| 4785 | /* we could pick up a file descriptor choice here |
| 4786 | * with redirect_opt_num(), but bash doesn't do it. |
| 4787 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4788 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4789 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4790 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4791 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4792 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4793 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4794 | if (ctx.ctx_res_w == RES_MATCH |
| 4795 | && ctx.command->argv == NULL /* not (word|(... */ |
| 4796 | && dest.length == 0 /* not word(... */ |
| 4797 | && dest.nonnull == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4798 | ) { |
| 4799 | continue; |
| 4800 | } |
| 4801 | #endif |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4802 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4803 | if (dest.length != 0 /* not just () but word() */ |
| 4804 | && dest.nonnull == 0 /* not a"b"c() */ |
| 4805 | && ctx.command->argv == NULL /* it's the first word */ |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4806 | //TODO: "func ( ) {...}" - note spaces - is valid format too in bash |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4807 | && i_peek(input) == ')' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4808 | && !match_reserved_word(&dest) |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4809 | ) { |
| 4810 | bb_error_msg("seems like a function definition"); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4811 | i_getch(input); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4812 | //if !BB_MMU o_addchr(&ctx.as_string... |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4813 | do { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4814 | //TODO: do it properly. |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4815 | ch = i_getch(input); |
| 4816 | } while (ch == ' ' || ch == '\n'); |
| 4817 | if (ch != '{') { |
| 4818 | syntax("was expecting {"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4819 | goto parse_error; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4820 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4821 | ch = 'F'; /* magic value */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4822 | } |
| 4823 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4824 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4825 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 4826 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4827 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4828 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4829 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4830 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4831 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4832 | goto case_semi; |
| 4833 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4834 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4835 | /* proper use of this character is caught by end_trigger: |
| 4836 | * if we see {, we call parse_group(..., end_trigger='}') |
| 4837 | * and it will match } earlier (not here). */ |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4838 | syntax("unexpected } or )"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4839 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4840 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4841 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4842 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4843 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4844 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4845 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4846 | parse_error: |
| 4847 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4848 | struct parse_context *pctx; |
| 4849 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4850 | |
| 4851 | /* Clean up allocated tree. |
| 4852 | * Samples for finding leaks on syntax error recovery path. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4853 | * Run them from interactive shell, watch pmap `pidof hush`. |
| 4854 | * while if false; then false; fi do break; done |
| 4855 | * (bash accepts it) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4856 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 4857 | * Samples to catch leaks at execution: |
| 4858 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 4859 | * 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] | 4860 | */ |
| 4861 | pctx = &ctx; |
| 4862 | do { |
| 4863 | /* Update pipe/command counts, |
| 4864 | * otherwise freeing may miss some */ |
| 4865 | done_pipe(pctx, PIPE_SEQ); |
| 4866 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 4867 | pctx->list_head, pctx); |
| 4868 | debug_print_tree(pctx->list_head, 0); |
| 4869 | free_pipe_list(pctx->list_head, 0); |
| 4870 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4871 | #if !BB_MMU |
| 4872 | o_free_unsafe(&pctx->as_string); |
| 4873 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4874 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4875 | if (pctx != &ctx) { |
| 4876 | free(pctx); |
| 4877 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4878 | IF_HAS_KEYWORDS(pctx = p2;) |
| 4879 | } while (HAS_KEYWORDS && pctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4880 | /* Free text, clear all dest fields */ |
| 4881 | o_free(&dest); |
| 4882 | /* If we are not in top-level parse, we return, |
| 4883 | * our caller will propagate error. |
| 4884 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4885 | if (end_trigger != ';') { |
| 4886 | #if !BB_MMU |
| 4887 | if (pstring) |
| 4888 | *pstring = NULL; |
| 4889 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4890 | return ERR_PTR; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4891 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4892 | /* Discard cached input, force prompt */ |
| 4893 | input->p = NULL; |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4894 | USE_HUSH_INTERACTIVE(input->promptme = 1;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4895 | goto reset; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4896 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4897 | } |
| 4898 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4899 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4900 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 4901 | * end_trigger controls how often we stop parsing |
| 4902 | * NUL: parse all, execute, return |
| 4903 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 4904 | */ |
| 4905 | 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] | 4906 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4907 | while (1) { |
| 4908 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4909 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4910 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4911 | if (!pipe_list) /* EOF */ |
| 4912 | break; |
| 4913 | debug_print_tree(pipe_list, 0); |
| 4914 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 4915 | run_and_free_list(pipe_list); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4916 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4917 | } |
| 4918 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4919 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4920 | { |
| 4921 | struct in_str input; |
| 4922 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4923 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4926 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4927 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4928 | struct in_str input; |
| 4929 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4930 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4931 | } |
| 4932 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4933 | /* Called a few times only (or even once if "sh -c") */ |
| 4934 | static void block_signals(int second_time) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4935 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4936 | unsigned sig; |
| 4937 | unsigned mask; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4938 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4939 | mask = (1 << SIGQUIT); |
| 4940 | if (G_interactive_fd) { |
| 4941 | mask = 0 |
| 4942 | | (1 << SIGQUIT) |
| 4943 | | (1 << SIGTERM) |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame^] | 4944 | //TODO | (1 << SIGHUP) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4945 | #if ENABLE_HUSH_JOB |
| 4946 | | (1 << SIGTTIN) | (1 << SIGTTOU) | (1 << SIGTSTP) |
| 4947 | #endif |
| 4948 | | (1 << SIGINT) |
| 4949 | ; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4950 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4951 | G.non_DFL_mask = mask; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4952 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4953 | if (!second_time) |
| 4954 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 4955 | sig = 0; |
| 4956 | while (mask) { |
| 4957 | if (mask & 1) |
| 4958 | sigaddset(&G.blocked_set, sig); |
| 4959 | mask >>= 1; |
| 4960 | sig++; |
| 4961 | } |
| 4962 | sigdelset(&G.blocked_set, SIGCHLD); |
| 4963 | |
| 4964 | sigprocmask(SIG_SETMASK, &G.blocked_set, |
| 4965 | second_time ? NULL : &G.inherited_set); |
| 4966 | /* POSIX allows shell to re-enable SIGCHLD |
| 4967 | * even if it was SIG_IGN on entry */ |
| 4968 | // G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 4969 | if (!second_time) |
| 4970 | signal(SIGCHLD, SIG_DFL); // SIGCHLD_handler); |
| 4971 | } |
| 4972 | |
| 4973 | #if ENABLE_HUSH_JOB |
| 4974 | /* helper */ |
| 4975 | static void maybe_set_to_sigexit(int sig) |
| 4976 | { |
| 4977 | void (*handler)(int); |
| 4978 | /* non_DFL_mask'ed signals are, well, masked, |
| 4979 | * no need to set handler for them. |
| 4980 | */ |
| 4981 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 4982 | handler = signal(sig, sigexit); |
| 4983 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
| 4984 | signal(sig, handler); |
| 4985 | } |
| 4986 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame^] | 4987 | /* Set handlers to restore tty pgrp and exit */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4988 | static void set_fatal_handlers(void) |
| 4989 | { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4990 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4991 | if (HUSH_DEBUG) { |
| 4992 | maybe_set_to_sigexit(SIGILL ); |
| 4993 | maybe_set_to_sigexit(SIGFPE ); |
| 4994 | maybe_set_to_sigexit(SIGBUS ); |
| 4995 | maybe_set_to_sigexit(SIGSEGV); |
| 4996 | maybe_set_to_sigexit(SIGTRAP); |
| 4997 | } /* else: hush is perfect. what SEGV? */ |
| 4998 | maybe_set_to_sigexit(SIGABRT); |
| 4999 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 5000 | maybe_set_to_sigexit(SIGPIPE); |
| 5001 | maybe_set_to_sigexit(SIGALRM); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame^] | 5002 | //TODO: disable and move down when proper SIGHUP handling is added |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5003 | maybe_set_to_sigexit(SIGHUP ); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame^] | 5004 | /* if we are interactive, [SIGHUP,] SIGTERM and SIGINT are masked. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5005 | * if we aren't interactive... but in this case |
| 5006 | * we never want to restore pgrp on exit, and this fn is not called */ |
| 5007 | /*maybe_set_to_sigexit(SIGTERM);*/ |
| 5008 | /*maybe_set_to_sigexit(SIGINT );*/ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 5009 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 5010 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 5011 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5012 | static int set_mode(const char cstate, const char mode) |
| 5013 | { |
| 5014 | int state = (cstate == '-' ? 1 : 0); |
| 5015 | switch (mode) { |
| 5016 | case 'n': G.fake_mode = state; break; |
| 5017 | case 'x': /*G.debug_mode = state;*/ break; |
| 5018 | default: return EXIT_FAILURE; |
| 5019 | } |
| 5020 | return EXIT_SUCCESS; |
| 5021 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5022 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 5023 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 5024 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5025 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 5026 | static const struct variable const_shell_ver = { |
| 5027 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 5028 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 5029 | .max_len = 1, /* 0 can provoke free(name) */ |
| 5030 | .flg_export = 1, |
| 5031 | .flg_read_only = 1, |
| 5032 | }; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5033 | int signal_mask_is_inited = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5034 | int opt; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5035 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 5036 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 5037 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 5038 | INIT_G(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 5039 | if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, is already done */ |
| 5040 | G.last_return_code = EXIT_SUCCESS; |
| 5041 | #if !BB_MMU |
| 5042 | G.argv0_for_re_execing = argv[0]; |
| 5043 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 5044 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5045 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 5046 | G.top_var = &G.shell_ver; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 5047 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 5048 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 5049 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 5050 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5051 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 5052 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 5053 | if (e) while (*e) { |
| 5054 | char *value = strchr(*e, '='); |
| 5055 | if (value) { /* paranoia */ |
| 5056 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 5057 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 5058 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 5059 | cur_var->max_len = strlen(*e); |
| 5060 | cur_var->flg_export = 1; |
| 5061 | } |
| 5062 | e++; |
| 5063 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 5064 | debug_printf_env("putenv '%s'\n", hush_version_str); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 5065 | putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */ |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 5066 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5067 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 5068 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5069 | G.global_argc = argc; |
| 5070 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 5071 | /* Initialize some more globals to non-zero values */ |
| 5072 | set_cwd(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 5073 | #if ENABLE_HUSH_INTERACTIVE |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 5074 | if (ENABLE_FEATURE_EDITING) |
| 5075 | cmdedit_set_initial_prompt(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5076 | G.PS2 = "> "; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 5077 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 5078 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5079 | /* Shell is non-interactive at first. We need to call |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame^] | 5080 | * block_signals(0) if we are going to execute "sh <script>", |
| 5081 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5082 | * If we later decide that we are interactive, we run block_signals(0) |
| 5083 | * (or re-run block_signals(1) if we ran block_signals(0) before) |
| 5084 | * in order to intercept (more) signals. |
| 5085 | */ |
| 5086 | |
| 5087 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 5088 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5089 | while (1) { |
| 5090 | opt = getopt(argc, argv, "c:xins" |
| 5091 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 5092 | "$:!:?:D:R:V:" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5093 | #endif |
| 5094 | ); |
| 5095 | if (opt <= 0) |
| 5096 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5097 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5098 | case 'c': |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 5099 | if (!G.root_pid) |
| 5100 | G.root_pid = getpid(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5101 | G.global_argv = argv + optind; |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 5102 | if (!argv[optind]) { |
| 5103 | /* -c 'script' (no params): prevent empty $0 */ |
| 5104 | *--G.global_argv = argv[0]; |
| 5105 | optind--; |
| 5106 | } /* else -c 'script' PAR0 PAR1: $0 is PAR0 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5107 | G.global_argc = argc - optind; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5108 | block_signals(0); /* 0: called 1st time */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5109 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5110 | goto final_return; |
| 5111 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 5112 | /* Well, we cannot just declare interactiveness, |
| 5113 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5114 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5115 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 5116 | case 's': |
| 5117 | /* "-s" means "read from stdin", but this is how we always |
| 5118 | * operate, so simply do nothing here. */ |
| 5119 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5120 | #if !BB_MMU |
| 5121 | case '$': |
| 5122 | G.root_pid = xatoi_u(optarg); |
| 5123 | break; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 5124 | case '!': |
| 5125 | G.last_bg_pid = xatoi_u(optarg); |
| 5126 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5127 | case '?': |
| 5128 | G.last_return_code = xatoi_u(optarg); |
| 5129 | break; |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame^] | 5130 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 16a0c74 | 2009-04-05 01:46:59 +0000 | [diff] [blame] | 5131 | case 'D': |
| 5132 | G.depth_of_loop = xatoi_u(optarg); |
| 5133 | break; |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame^] | 5134 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5135 | case 'R': |
| 5136 | case 'V': |
| 5137 | set_local_var(xstrdup(optarg), 0, opt == 'R'); |
| 5138 | break; |
| 5139 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5140 | case 'n': |
| 5141 | case 'x': |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5142 | if (!set_mode('-', opt)) |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5143 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5144 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 5145 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5146 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 5147 | " or: sh -c command [args]...\n\n"); |
| 5148 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 5149 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5150 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 5151 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5152 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5153 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 5154 | |
| 5155 | if (!G.root_pid) |
| 5156 | G.root_pid = getpid(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5157 | |
| 5158 | /* If we are login shell... */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 5159 | if (argv[0] && argv[0][0] == '-') { |
| 5160 | FILE *input; |
| 5161 | /* XXX what should argv be while sourcing /etc/profile? */ |
| 5162 | debug_printf("sourcing /etc/profile\n"); |
| 5163 | input = fopen_for_read("/etc/profile"); |
| 5164 | if (input != NULL) { |
| 5165 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5166 | block_signals(0); /* 0: called 1st time */ |
| 5167 | signal_mask_is_inited = 1; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 5168 | parse_and_run_file(input); |
| 5169 | fclose(input); |
| 5170 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5171 | /* bash: after sourcing /etc/profile, |
| 5172 | * tries to source (in the given order): |
| 5173 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
| 5174 | * stopping of first found. --noprofile turns this off. |
| 5175 | * bash also sources ~/.bash_logout on exit. |
| 5176 | * If called as sh, skips .bash_XXX files. |
| 5177 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 5178 | } |
| 5179 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5180 | if (argv[optind]) { |
| 5181 | FILE *input; |
| 5182 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame^] | 5183 | * "bash <script>" (which is never interactive (unless -i?)) |
| 5184 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5185 | * If called as sh, does the same but with $ENV. |
| 5186 | */ |
| 5187 | debug_printf("running script '%s'\n", argv[optind]); |
| 5188 | G.global_argv = argv + optind; |
| 5189 | G.global_argc = argc - optind; |
| 5190 | input = xfopen_for_read(argv[optind]); |
| 5191 | close_on_exec_on(fileno(input)); |
| 5192 | if (!signal_mask_is_inited) |
| 5193 | block_signals(0); /* 0: called 1st time */ |
| 5194 | parse_and_run_file(input); |
| 5195 | #if ENABLE_FEATURE_CLEAN_UP |
| 5196 | fclose(input); |
| 5197 | #endif |
| 5198 | goto final_return; |
| 5199 | } |
| 5200 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5201 | /* Up to here, shell was non-interactive. Now it may become one. |
| 5202 | * NB: don't forget to (re)run block_signals(0/1) as needed. |
| 5203 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5204 | |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5205 | /* A shell is interactive if the '-i' flag was given, or if all of |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5206 | * the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 5207 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5208 | * no arguments remaining or the -s flag given |
| 5209 | * standard input is a terminal |
| 5210 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5211 | * Refer to Posix.2, the description of the 'sh' utility. |
| 5212 | */ |
| 5213 | #if ENABLE_HUSH_JOB |
| 5214 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5215 | G.saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5216 | debug_printf("saved_tty_pgrp:%d\n", G.saved_tty_pgrp); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame^] | 5217 | //TODO: "interactive" and "have job control" are two different things. |
| 5218 | //If tcgetpgrp fails here, "have job control" is false, but "interactive" |
| 5219 | //should stay on! Currently, we mix these into one. |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5220 | if (G.saved_tty_pgrp >= 0) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5221 | /* try to dup stdin to high fd#, >= 255 */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5222 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 5223 | if (G_interactive_fd < 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 5224 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5225 | G_interactive_fd = dup(STDIN_FILENO); |
| 5226 | if (G_interactive_fd < 0) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 5227 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5228 | G_interactive_fd = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 5229 | } |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 5230 | // TODO: track & disallow any attempts of user |
| 5231 | // to (inadvertently) close/redirect it |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 5232 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5233 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5234 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5235 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5236 | pid_t shell_pgrp; |
| 5237 | |
| 5238 | /* We are indeed interactive shell, and we will perform |
| 5239 | * job control. Setting up for that. */ |
| 5240 | |
| 5241 | close_on_exec_on(G_interactive_fd); |
| 5242 | /* If we were run as 'hush &', sleep until we are |
| 5243 | * in the foreground (tty pgrp == our pgrp). |
| 5244 | * If we get started under a job aware app (like bash), |
| 5245 | * make sure we are now in charge so we don't fight over |
| 5246 | * who gets the foreground */ |
| 5247 | while (1) { |
| 5248 | shell_pgrp = getpgrp(); |
| 5249 | G.saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 5250 | if (G.saved_tty_pgrp == shell_pgrp) |
| 5251 | break; |
| 5252 | /* send TTIN to ourself (should stop us) */ |
| 5253 | kill(- shell_pgrp, SIGTTIN); |
| 5254 | } |
| 5255 | /* Block some signals */ |
| 5256 | block_signals(signal_mask_is_inited); |
| 5257 | /* Set other signals to restore saved_tty_pgrp */ |
| 5258 | set_fatal_handlers(); |
| 5259 | /* Put ourselves in our own process group */ |
| 5260 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 5261 | /* Grab control of the terminal */ |
| 5262 | tcsetpgrp(G_interactive_fd, getpid()); |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 5263 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 5264 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
| 5265 | die_sleep = -1; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 5266 | if (setjmp(die_jmp)) { |
| 5267 | /* xfunc has failed! die die die */ |
| 5268 | hush_exit(xfunc_error_retval); |
| 5269 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5270 | } else if (!signal_mask_is_inited) { |
| 5271 | block_signals(0); /* 0: called 1st time */ |
| 5272 | } /* else: block_signals(0) was done before */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 5273 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5274 | /* No job control compiled in, only prompt/line editing */ |
| 5275 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5276 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 5277 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 5278 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5279 | G_interactive_fd = dup(STDIN_FILENO); |
| 5280 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 5281 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5282 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 5283 | } |
| 5284 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5285 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5286 | close_on_exec_on(G_interactive_fd); |
| 5287 | block_signals(signal_mask_is_inited); |
| 5288 | } else if (!signal_mask_is_inited) { |
| 5289 | block_signals(0); |
| 5290 | } |
| 5291 | #else |
| 5292 | /* We have interactiveness code disabled */ |
| 5293 | if (!signal_mask_is_inited) { |
| 5294 | block_signals(0); |
| 5295 | } |
| 5296 | #endif |
| 5297 | /* bash: |
| 5298 | * if interactive but not a login shell, sources ~/.bashrc |
| 5299 | * (--norc turns this off, --rcfile <file> overrides) |
| 5300 | */ |
| 5301 | |
| 5302 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Mike Frysinger | 258275d | 2009-04-05 21:19:43 +0000 | [diff] [blame] | 5303 | printf("\n\n%s hush - the humble shell\n", bb_banner); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 5304 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
| 5305 | } |
| 5306 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5307 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5308 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 5309 | final_return: |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 5310 | #if ENABLE_FEATURE_CLEAN_UP |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5311 | if (G.cwd != bb_msg_unknown) |
| 5312 | free((char*)G.cwd); |
| 5313 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 5314 | while (cur_var) { |
| 5315 | struct variable *tmp = cur_var; |
| 5316 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 5317 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 5318 | cur_var = cur_var->next; |
| 5319 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 5320 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5321 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5322 | hush_exit(G.last_return_code); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5323 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 5324 | |
| 5325 | |
| 5326 | #if ENABLE_LASH |
| 5327 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 5328 | int lash_main(int argc, char **argv) |
| 5329 | { |
| 5330 | //bb_error_msg("lash is deprecated, please use hush instead"); |
| 5331 | return hush_main(argc, argv); |
| 5332 | } |
| 5333 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5334 | |
| 5335 | |
| 5336 | /* |
| 5337 | * Built-ins |
| 5338 | */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 5339 | static int builtin_trap(char **argv) |
| 5340 | { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5341 | int i; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 5342 | int sig; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5343 | char *new_cmd; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 5344 | |
| 5345 | if (!G.traps) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5346 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 5347 | |
| 5348 | if (!argv[1]) { |
| 5349 | /* No args: print all trapped. This isn't 100% correct as we should |
| 5350 | * be escaping the cmd so that it can be pasted back in ... |
| 5351 | */ |
| 5352 | for (i = 0; i < NSIG; ++i) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5353 | if (G.traps[i]) |
| 5354 | printf("trap -- '%s' %s\n", G.traps[i], get_signame(i)); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 5355 | return EXIT_SUCCESS; |
| 5356 | } |
| 5357 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5358 | new_cmd = NULL; |
| 5359 | i = 0; |
| 5360 | /* if first arg is decimal: reset all specified */ |
| 5361 | sig = bb_strtou(*++argv, NULL, 10); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 5362 | if (errno == 0) { |
| 5363 | int ret; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 5364 | set_all: |
| 5365 | ret = EXIT_SUCCESS; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5366 | while (*argv) { |
| 5367 | sig = get_signum(*argv++); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 5368 | if (sig < 0 || sig >= NSIG) { |
| 5369 | ret = EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5370 | /* mimic bash message exactly */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 5371 | bb_perror_msg("trap: %s: invalid signal specification", argv[i]); |
| 5372 | continue; |
| 5373 | } |
| 5374 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5375 | free(G.traps[sig]); |
| 5376 | G.traps[sig] = xstrdup(new_cmd); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 5377 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5378 | debug_printf("trap: setting SIG%s (%i) to '%s'", |
| 5379 | get_signame(sig), sig, G.traps[sig]); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 5380 | |
| 5381 | /* There is no signal for 0 (EXIT) */ |
| 5382 | if (sig == 0) |
| 5383 | continue; |
| 5384 | |
| 5385 | if (new_cmd) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5386 | sigaddset(&G.blocked_set, sig); |
| 5387 | } else { |
| 5388 | /* there was a trap handler, we are removing it |
| 5389 | * (if sig has non-DFL handling, |
| 5390 | * we don't need to do anything) */ |
| 5391 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 5392 | continue; |
| 5393 | sigdelset(&G.blocked_set, sig); |
| 5394 | } |
| 5395 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 5396 | } |
| 5397 | return ret; |
| 5398 | } |
| 5399 | |
| 5400 | /* first arg is "-": reset all specified to default */ |
| 5401 | /* first arg is "": ignore all specified */ |
| 5402 | /* everything else: execute first arg upon signal */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5403 | if (!argv[1]) { |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 5404 | bb_error_msg("trap: invalid arguments"); |
| 5405 | return EXIT_FAILURE; |
| 5406 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5407 | if (LONE_DASH(*argv)) |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 5408 | /* nothing! */; |
| 5409 | else |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5410 | new_cmd = *argv; |
| 5411 | argv++; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 5412 | goto set_all; |
| 5413 | } |
| 5414 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 5415 | static int builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5416 | { |
| 5417 | return 0; |
| 5418 | } |
| 5419 | |
| 5420 | static int builtin_test(char **argv) |
| 5421 | { |
| 5422 | int argc = 0; |
| 5423 | while (*argv) { |
| 5424 | argc++; |
| 5425 | argv++; |
| 5426 | } |
| 5427 | return test_main(argc, argv - argc); |
| 5428 | } |
| 5429 | |
| 5430 | static int builtin_echo(char **argv) |
| 5431 | { |
| 5432 | int argc = 0; |
| 5433 | while (*argv) { |
| 5434 | argc++; |
| 5435 | argv++; |
| 5436 | } |
| 5437 | return echo_main(argc, argv - argc); |
| 5438 | } |
| 5439 | |
| 5440 | static int builtin_eval(char **argv) |
| 5441 | { |
| 5442 | int rcode = EXIT_SUCCESS; |
| 5443 | |
| 5444 | if (argv[1]) { |
| 5445 | char *str = expand_strvec_to_string(argv + 1); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5446 | /* bash: |
| 5447 | * eval "echo Hi; done" ("done" is syntax error): |
| 5448 | * "echo Hi" will not execute too. |
| 5449 | */ |
| 5450 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5451 | free(str); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5452 | rcode = G.last_return_code; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5453 | } |
| 5454 | return rcode; |
| 5455 | } |
| 5456 | |
| 5457 | static int builtin_cd(char **argv) |
| 5458 | { |
| 5459 | const char *newdir; |
| 5460 | if (argv[1] == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5461 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
| 5462 | * bash says "bash: cd: HOME not set" and does nothing (exitcode 1) |
| 5463 | */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5464 | newdir = getenv("HOME") ? : "/"; |
| 5465 | } else |
| 5466 | newdir = argv[1]; |
| 5467 | if (chdir(newdir)) { |
| 5468 | printf("cd: %s: %s\n", newdir, strerror(errno)); |
| 5469 | return EXIT_FAILURE; |
| 5470 | } |
| 5471 | set_cwd(); |
| 5472 | return EXIT_SUCCESS; |
| 5473 | } |
| 5474 | |
| 5475 | static int builtin_exec(char **argv) |
| 5476 | { |
| 5477 | if (argv[1] == NULL) |
| 5478 | return EXIT_SUCCESS; /* bash does this */ |
| 5479 | { |
| 5480 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 5481 | nommu_save_t dummy; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5482 | #endif |
| 5483 | // FIXME: if exec fails, bash does NOT exit! We do... |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 5484 | pseudo_exec_argv(&dummy, argv + 1, 0, NULL); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5485 | /* never returns */ |
| 5486 | } |
| 5487 | } |
| 5488 | |
| 5489 | static int builtin_exit(char **argv) |
| 5490 | { |
| 5491 | // TODO: bash does it ONLY on top-level sh exit (+interacive only?) |
| 5492 | //puts("exit"); /* bash does it */ |
| 5493 | // TODO: warn if we have background jobs: "There are stopped jobs" |
| 5494 | // On second consecutive 'exit', exit anyway. |
| 5495 | if (argv[1] == NULL) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5496 | hush_exit(G.last_return_code); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5497 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 5498 | xfunc_error_retval = 255; |
| 5499 | /* bash: exit -2 == exit 254, no error msg */ |
| 5500 | hush_exit(xatoi(argv[1]) & 0xff); |
| 5501 | } |
| 5502 | |
| 5503 | static int builtin_export(char **argv) |
| 5504 | { |
| 5505 | const char *value; |
| 5506 | char *name = argv[1]; |
| 5507 | |
| 5508 | if (name == NULL) { |
| 5509 | // TODO: |
| 5510 | // ash emits: export VAR='VAL' |
| 5511 | // bash: declare -x VAR="VAL" |
| 5512 | // (both also escape as needed (quotes, $, etc)) |
| 5513 | char **e = environ; |
| 5514 | if (e) |
| 5515 | while (*e) |
| 5516 | puts(*e++); |
| 5517 | return EXIT_SUCCESS; |
| 5518 | } |
| 5519 | |
| 5520 | value = strchr(name, '='); |
| 5521 | if (!value) { |
| 5522 | /* They are exporting something without a =VALUE */ |
| 5523 | struct variable *var; |
| 5524 | |
| 5525 | var = get_local_var(name); |
| 5526 | if (var) { |
| 5527 | var->flg_export = 1; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 5528 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5529 | putenv(var->varstr); |
| 5530 | } |
| 5531 | /* bash does not return an error when trying to export |
| 5532 | * an undefined variable. Do likewise. */ |
| 5533 | return EXIT_SUCCESS; |
| 5534 | } |
| 5535 | |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5536 | set_local_var(xstrdup(name), 1, 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5537 | return EXIT_SUCCESS; |
| 5538 | } |
| 5539 | |
| 5540 | #if ENABLE_HUSH_JOB |
| 5541 | /* built-in 'fg' and 'bg' handler */ |
| 5542 | static int builtin_fg_bg(char **argv) |
| 5543 | { |
| 5544 | int i, jobnum; |
| 5545 | struct pipe *pi; |
| 5546 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5547 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5548 | return EXIT_FAILURE; |
| 5549 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 5550 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5551 | for (pi = G.job_list; pi; pi = pi->next) { |
| 5552 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5553 | goto found; |
| 5554 | } |
| 5555 | } |
| 5556 | bb_error_msg("%s: no current job", argv[0]); |
| 5557 | return EXIT_FAILURE; |
| 5558 | } |
| 5559 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 5560 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 5561 | return EXIT_FAILURE; |
| 5562 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5563 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5564 | if (pi->jobid == jobnum) { |
| 5565 | goto found; |
| 5566 | } |
| 5567 | } |
| 5568 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 5569 | return EXIT_FAILURE; |
| 5570 | found: |
| 5571 | // TODO: bash prints a string representation |
| 5572 | // of job being foregrounded (like "sleep 1 | cat") |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 5573 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5574 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5575 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5576 | } |
| 5577 | |
| 5578 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5579 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 5580 | for (i = 0; i < pi->num_cmds; i++) { |
| 5581 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 5582 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5583 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5584 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5585 | |
| 5586 | i = kill(- pi->pgrp, SIGCONT); |
| 5587 | if (i < 0) { |
| 5588 | if (errno == ESRCH) { |
| 5589 | delete_finished_bg_job(pi); |
| 5590 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5591 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 5592 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5593 | } |
| 5594 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 5595 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5596 | remove_bg_job(pi); |
| 5597 | return checkjobs_and_fg_shell(pi); |
| 5598 | } |
| 5599 | return EXIT_SUCCESS; |
| 5600 | } |
| 5601 | #endif |
| 5602 | |
| 5603 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 5604 | static int builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5605 | { |
| 5606 | const struct built_in_command *x; |
| 5607 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 5608 | printf("\n" |
| 5609 | "Built-in commands:\n" |
| 5610 | "------------------\n"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5611 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 5612 | printf("%s\t%s\n", x->cmd, x->descr); |
| 5613 | } |
| 5614 | printf("\n\n"); |
| 5615 | return EXIT_SUCCESS; |
| 5616 | } |
| 5617 | #endif |
| 5618 | |
| 5619 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 5620 | static int builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5621 | { |
| 5622 | struct pipe *job; |
| 5623 | const char *status_string; |
| 5624 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5625 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5626 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5627 | status_string = "Stopped"; |
| 5628 | else |
| 5629 | status_string = "Running"; |
| 5630 | |
| 5631 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 5632 | } |
| 5633 | return EXIT_SUCCESS; |
| 5634 | } |
| 5635 | #endif |
| 5636 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 5637 | static int builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5638 | { |
| 5639 | puts(set_cwd()); |
| 5640 | return EXIT_SUCCESS; |
| 5641 | } |
| 5642 | |
| 5643 | static int builtin_read(char **argv) |
| 5644 | { |
| 5645 | char *string; |
| 5646 | const char *name = argv[1] ? argv[1] : "REPLY"; |
Denis Vlasenko | a0e6512 | 2009-04-05 23:39:14 +0000 | [diff] [blame] | 5647 | //TODO: check that argv[1] is a valid variable name |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5648 | |
| 5649 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5650 | return set_local_var(string, 0, 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5651 | } |
| 5652 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5653 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 5654 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 5655 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5656 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 5657 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 5658 | * set -- [argument...] |
| 5659 | * set -o |
| 5660 | * set +o |
| 5661 | * Implementations shall support the options in both their hyphen and |
| 5662 | * plus-sign forms. These options can also be specified as options to sh. |
| 5663 | * Examples: |
| 5664 | * Write out all variables and their values: set |
| 5665 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 5666 | * Turn on the -x and -v options: set -xv |
| 5667 | * Unset all positional parameters: set -- |
| 5668 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 5669 | * Set the positional parameters to the expansion of x, even if x expands |
| 5670 | * with a leading '-' or '+': set -- $x |
| 5671 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5672 | * 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] | 5673 | */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5674 | static int builtin_set(char **argv) |
| 5675 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5676 | int n; |
| 5677 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 5678 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5679 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 5680 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5681 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5682 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5683 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5684 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 5685 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5686 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5687 | do { |
| 5688 | if (!strcmp(arg, "--")) { |
| 5689 | ++argv; |
| 5690 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5691 | } |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5692 | |
| 5693 | if (arg[0] == '+' || arg[0] == '-') { |
| 5694 | for (n = 1; arg[n]; ++n) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5695 | if (set_mode(arg[0], arg[n])) |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5696 | goto error; |
| 5697 | continue; |
| 5698 | } |
| 5699 | |
| 5700 | break; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5701 | } while ((arg = *++argv) != NULL); |
| 5702 | /* Now argv[0] is 1st argument */ |
| 5703 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5704 | /* Only reset global_argv if we didn't process anything */ |
| 5705 | if (arg == NULL) |
| 5706 | return EXIT_SUCCESS; |
| 5707 | set_argv: |
| 5708 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5709 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 5710 | g_argv = G.global_argv; |
| 5711 | if (G.global_args_malloced) { |
| 5712 | pp = g_argv; |
| 5713 | while (*++pp) |
| 5714 | free(*pp); |
| 5715 | g_argv[1] = NULL; |
| 5716 | } else { |
| 5717 | G.global_args_malloced = 1; |
| 5718 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 5719 | pp[0] = g_argv[0]; /* retain $0 */ |
| 5720 | g_argv = pp; |
| 5721 | } |
| 5722 | /* This realloc's G.global_argv */ |
| 5723 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 5724 | |
| 5725 | n = 1; |
| 5726 | while (*++pp) |
| 5727 | n++; |
| 5728 | G.global_argc = n; |
| 5729 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5730 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5731 | |
| 5732 | /* Nothing known, so abort */ |
| 5733 | error: |
| 5734 | bb_error_msg("set: %s: invalid option", arg); |
| 5735 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5736 | } |
| 5737 | |
| 5738 | static int builtin_shift(char **argv) |
| 5739 | { |
| 5740 | int n = 1; |
| 5741 | if (argv[1]) { |
| 5742 | n = atoi(argv[1]); |
| 5743 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5744 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 5745 | if (G.global_args_malloced) { |
| 5746 | int m = 1; |
| 5747 | while (m <= n) |
| 5748 | free(G.global_argv[m++]); |
| 5749 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5750 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 5751 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 5752 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5753 | return EXIT_SUCCESS; |
| 5754 | } |
| 5755 | return EXIT_FAILURE; |
| 5756 | } |
| 5757 | |
| 5758 | static int builtin_source(char **argv) |
| 5759 | { |
| 5760 | FILE *input; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5761 | |
| 5762 | if (argv[1] == NULL) |
| 5763 | return EXIT_FAILURE; |
| 5764 | |
| 5765 | /* XXX search through $PATH is missing */ |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 5766 | input = fopen_for_read(argv[1]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5767 | if (!input) { |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5768 | bb_error_msg("can't open '%s'", argv[1]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5769 | return EXIT_FAILURE; |
| 5770 | } |
| 5771 | close_on_exec_on(fileno(input)); |
| 5772 | |
| 5773 | /* Now run the file */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5774 | /* XXX argv and argc are broken; need to save old G.global_argv |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5775 | * (pointer only is OK!) on this stack frame, |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5776 | * set G.global_argv=argv+1, recurse, and restore. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5777 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5778 | fclose(input); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5779 | return G.last_return_code; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5780 | } |
| 5781 | |
| 5782 | static int builtin_umask(char **argv) |
| 5783 | { |
| 5784 | mode_t new_umask; |
| 5785 | const char *arg = argv[1]; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5786 | if (arg) { |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 5787 | new_umask = bb_strtou(arg, NULL, 8); |
| 5788 | if (errno) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5789 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5790 | } else { |
| 5791 | new_umask = umask(0); |
| 5792 | printf("%.3o\n", (unsigned) new_umask); |
| 5793 | } |
| 5794 | umask(new_umask); |
| 5795 | return EXIT_SUCCESS; |
| 5796 | } |
| 5797 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 5798 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5799 | static int builtin_unset(char **argv) |
| 5800 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 5801 | size_t i; |
| 5802 | int ret; |
| 5803 | bool var = true; |
| 5804 | |
| 5805 | if (!argv[1]) |
| 5806 | return EXIT_SUCCESS; |
| 5807 | |
| 5808 | i = 0; |
| 5809 | if (argv[1][0] == '-') { |
| 5810 | switch (argv[1][1]) { |
| 5811 | case 'v': break; |
| 5812 | case 'f': if (ENABLE_HUSH_FUNCTIONS) { var = false; break; } |
| 5813 | default: |
| 5814 | bb_error_msg("unset: %s: invalid option", argv[1]); |
| 5815 | return EXIT_FAILURE; |
| 5816 | } |
| 5817 | ++i; |
| 5818 | } |
| 5819 | |
| 5820 | ret = EXIT_SUCCESS; |
| 5821 | while (argv[++i]) { |
| 5822 | if (var) { |
| 5823 | if (unset_local_var(argv[i])) |
| 5824 | ret = EXIT_FAILURE; |
| 5825 | } |
| 5826 | #if ENABLE_HUSH_FUNCTIONS |
| 5827 | else |
| 5828 | unset_local_func(argv[i]); |
| 5829 | #endif |
| 5830 | } |
| 5831 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5832 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5833 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5834 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
| 5835 | static int builtin_wait(char **argv) |
| 5836 | { |
| 5837 | int ret = EXIT_SUCCESS; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 5838 | int status, sig; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5839 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 5840 | if (*++argv == NULL) { |
| 5841 | /* Don't care about wait results */ |
| 5842 | /* Note 1: must wait until there are no more children */ |
| 5843 | /* Note 2: must be interruptible */ |
| 5844 | /* Examples: |
| 5845 | * $ sleep 3 & sleep 6 & wait |
| 5846 | * [1] 30934 sleep 3 |
| 5847 | * [2] 30935 sleep 6 |
| 5848 | * [1] Done sleep 3 |
| 5849 | * [2] Done sleep 6 |
| 5850 | * $ sleep 3 & sleep 6 & wait |
| 5851 | * [1] 30936 sleep 3 |
| 5852 | * [2] 30937 sleep 6 |
| 5853 | * [1] Done sleep 3 |
| 5854 | * ^C <-- after ~4 sec from keyboard |
| 5855 | * $ |
| 5856 | */ |
| 5857 | sigaddset(&G.blocked_set, SIGCHLD); |
| 5858 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 5859 | while (1) { |
| 5860 | checkjobs(NULL); |
| 5861 | if (errno == ECHILD) |
| 5862 | break; |
| 5863 | /* Wait for SIGCHLD or any other signal of interest */ |
| 5864 | /* sigtimedwait with infinite timeout: */ |
| 5865 | sig = sigwaitinfo(&G.blocked_set, NULL); |
| 5866 | if (sig > 0) { |
| 5867 | sig = check_and_run_traps(sig); |
| 5868 | if (sig && sig != SIGCHLD) { /* see note 2 */ |
| 5869 | ret = 128 + sig; |
| 5870 | break; |
| 5871 | } |
| 5872 | } |
| 5873 | } |
| 5874 | sigdelset(&G.blocked_set, SIGCHLD); |
| 5875 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 5876 | return ret; |
| 5877 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5878 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 5879 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5880 | while (*argv) { |
| 5881 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 5882 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5883 | /* mimic bash message */ |
| 5884 | 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] | 5885 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5886 | } |
| 5887 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5888 | if (WIFSIGNALED(status)) |
| 5889 | ret = 128 + WTERMSIG(status); |
| 5890 | else if (WIFEXITED(status)) |
| 5891 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5892 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5893 | ret = EXIT_FAILURE; |
| 5894 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5895 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5896 | ret = 127; |
| 5897 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5898 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5899 | } |
| 5900 | |
| 5901 | return ret; |
| 5902 | } |
| 5903 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 5904 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5905 | static int builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5906 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5907 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 5908 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 5909 | return EXIT_SUCCESS; /* bash compat */ |
| 5910 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5911 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
| 5912 | G.depth_break_continue = 1; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5913 | if (argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5914 | G.depth_break_continue = bb_strtou(argv[1], NULL, 10); |
| 5915 | if (errno || !G.depth_break_continue || argv[2]) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 5916 | bb_error_msg("%s: bad arguments", argv[0]); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5917 | G.flag_break_continue = BC_BREAK; |
| 5918 | G.depth_break_continue = UINT_MAX; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5919 | } |
| 5920 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5921 | if (G.depth_of_loop < G.depth_break_continue) |
| 5922 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5923 | return EXIT_SUCCESS; |
| 5924 | } |
| 5925 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5926 | static int builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5927 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 5928 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 5929 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5930 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 5931 | #endif |