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 | * Arithmetic Expansion |
| 42 | * <(list) and >(list) Process Substitution |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 43 | * Here Documents ( << word ) |
| 44 | * Functions |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 45 | * Tilde Expansion |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 46 | * Parameter Expansion for substring processing ${var#word} ${var%word} |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 47 | * |
| 48 | * Bash stuff maybe optional enable: |
| 49 | * &> and >& redirection of stdout+stderr |
| 50 | * Brace expansion |
| 51 | * reserved words: [[ ]] function select |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 52 | * substrings ${var:1:5} |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 53 | * |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 54 | * Major bugs: |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 55 | * job handling woefully incomplete and buggy (improved --vda) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 56 | * to-do: |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 57 | * port selected bugfixes from post-0.49 busybox lash - done? |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 58 | * change { and } from special chars to reserved words |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 59 | * builtins: return, trap, ulimit |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 60 | * test magic exec with redirection only |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 61 | * follow IFS rules more precisely, including update semantics |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 62 | * figure out what to do with backslash-newline |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 63 | * propagate syntax errors, die on resource errors? |
| 64 | * continuation lines, both explicit and implicit - done? |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 65 | * maybe change charmap[] to use 2-bit entries |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 66 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 67 | * 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] | 68 | */ |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 70 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 71 | //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] | 72 | //#include "applet_tables.h" doesn't work |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 73 | #include <glob.h> |
| 74 | /* #include <dmalloc.h> */ |
| 75 | #if ENABLE_HUSH_CASE |
| 76 | #include <fnmatch.h> |
| 77 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 78 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 79 | #define HUSH_VER_STR "0.92" |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 81 | #if defined SINGLE_APPLET_MAIN |
| 82 | /* STANDALONE does not make sense, and won't compile */ |
| 83 | #undef CONFIG_FEATURE_SH_STANDALONE |
| 84 | #undef ENABLE_FEATURE_SH_STANDALONE |
| 85 | #undef USE_FEATURE_SH_STANDALONE |
| 86 | #define SKIP_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
| 87 | #define ENABLE_FEATURE_SH_STANDALONE 0 |
| 88 | #define USE_FEATURE_SH_STANDALONE(...) |
| 89 | #define SKIP_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
| 90 | #endif |
| 91 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 92 | #if !BB_MMU && ENABLE_HUSH_TICK |
| 93 | //#undef ENABLE_HUSH_TICK |
| 94 | //#define ENABLE_HUSH_TICK 0 |
| 95 | #warning On NOMMU, hush command substitution is dangerous. |
| 96 | #warning Dont use it for commands which produce lots of output. |
| 97 | #warning For more info see shell/hush.c, generate_stream_from_list(). |
| 98 | #endif |
| 99 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 100 | #if !ENABLE_HUSH_INTERACTIVE |
| 101 | #undef ENABLE_FEATURE_EDITING |
| 102 | #define ENABLE_FEATURE_EDITING 0 |
| 103 | #undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 104 | #define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 105 | #endif |
| 106 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 107 | /* Do we support ANY keywords? */ |
| 108 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 109 | #define HAS_KEYWORDS 1 |
| 110 | #define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 111 | #define IF_HAS_NO_KEYWORDS(...) |
| 112 | #else |
| 113 | #define HAS_KEYWORDS 0 |
| 114 | #define IF_HAS_KEYWORDS(...) |
| 115 | #define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
| 116 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 117 | |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 118 | /* Keep unconditionally on for now */ |
| 119 | #define HUSH_DEBUG 1 |
| 120 | /* In progress... */ |
| 121 | #define ENABLE_HUSH_FUNCTIONS 0 |
| 122 | |
| 123 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 124 | /* If you comment out one of these below, it will be #defined later |
| 125 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 126 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 127 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 128 | #define debug_printf_parse(...) do {} while (0) |
| 129 | #define debug_print_tree(a, b) do {} while (0) |
| 130 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 131 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 132 | #define debug_printf_jobs(...) do {} while (0) |
| 133 | #define debug_printf_expand(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 134 | #define debug_printf_glob(...) do {} while (0) |
| 135 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 136 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 137 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 138 | |
| 139 | #ifndef debug_printf |
| 140 | #define debug_printf(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 141 | #endif |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 142 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 143 | #ifndef debug_printf_parse |
| 144 | #define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 145 | #endif |
| 146 | |
| 147 | #ifndef debug_printf_exec |
| 148 | #define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__) |
| 149 | #endif |
| 150 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 151 | #ifndef debug_printf_env |
| 152 | #define debug_printf_env(...) fprintf(stderr, __VA_ARGS__) |
| 153 | #endif |
| 154 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 155 | #ifndef debug_printf_jobs |
| 156 | #define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 157 | #define DEBUG_JOBS 1 |
| 158 | #else |
| 159 | #define DEBUG_JOBS 0 |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 160 | #endif |
| 161 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 162 | #ifndef debug_printf_expand |
| 163 | #define debug_printf_expand(...) fprintf(stderr, __VA_ARGS__) |
| 164 | #define DEBUG_EXPAND 1 |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 165 | #else |
| 166 | #define DEBUG_EXPAND 0 |
| 167 | #endif |
| 168 | |
| 169 | #ifndef debug_printf_glob |
| 170 | #define debug_printf_glob(...) fprintf(stderr, __VA_ARGS__) |
| 171 | #define DEBUG_GLOB 1 |
| 172 | #else |
| 173 | #define DEBUG_GLOB 0 |
| 174 | #endif |
| 175 | |
| 176 | #ifndef debug_printf_list |
| 177 | #define debug_printf_list(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 178 | #endif |
| 179 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 180 | #ifndef debug_printf_subst |
| 181 | #define debug_printf_subst(...) fprintf(stderr, __VA_ARGS__) |
| 182 | #endif |
| 183 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 184 | #ifndef debug_printf_clean |
| 185 | /* broken, of course, but OK for testing */ |
| 186 | static const char *indenter(int i) |
| 187 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 188 | static const char blanks[] ALIGN1 = |
| 189 | " "; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 190 | return &blanks[sizeof(blanks) - i - 1]; |
| 191 | } |
| 192 | #define debug_printf_clean(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 193 | #define DEBUG_CLEAN 1 |
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 | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 211 | /* suppress "warning: no previous prototype..." */ |
| 212 | void *xxmalloc(int lineno, size_t size); |
| 213 | void *xxrealloc(int lineno, void *ptr, size_t size); |
| 214 | char *xxstrdup(int lineno, const char *str); |
| 215 | void xxfree(void *ptr); |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 216 | void *xxmalloc(int lineno, size_t size) |
| 217 | { |
| 218 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 219 | fprintf(stderr, "line %d: malloc %p\n", lineno, ptr); |
| 220 | return ptr; |
| 221 | } |
| 222 | void *xxrealloc(int lineno, void *ptr, size_t size) |
| 223 | { |
| 224 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 225 | fprintf(stderr, "line %d: realloc %p\n", lineno, ptr); |
| 226 | return ptr; |
| 227 | } |
| 228 | char *xxstrdup(int lineno, const char *str) |
| 229 | { |
| 230 | char *ptr = xstrdup(str); |
| 231 | fprintf(stderr, "line %d: strdup %p\n", lineno, ptr); |
| 232 | return ptr; |
| 233 | } |
| 234 | void xxfree(void *ptr) |
| 235 | { |
| 236 | fprintf(stderr, "free %p\n", ptr); |
| 237 | free(ptr); |
| 238 | } |
| 239 | #define xmalloc(s) xxmalloc(__LINE__, s) |
| 240 | #define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 241 | #define xstrdup(s) xxstrdup(__LINE__, s) |
| 242 | #define free(p) xxfree(p) |
| 243 | #endif |
| 244 | |
| 245 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 246 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="HUSH_VER_STR; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 247 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 248 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 249 | |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 250 | #define SPECIAL_VAR_SYMBOL 3 |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 251 | #define PARSEFLAG_EXIT_FROM_LOOP 1 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 252 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 253 | typedef enum redir_type { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 254 | REDIRECT_INPUT = 1, |
| 255 | REDIRECT_OVERWRITE = 2, |
| 256 | REDIRECT_APPEND = 3, |
| 257 | REDIRECT_HEREIS = 4, |
| 258 | REDIRECT_IO = 5 |
| 259 | } redir_type; |
| 260 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 261 | /* The descrip member of this structure is only used to make |
| 262 | * debugging output pretty */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 263 | static const struct { |
| 264 | int mode; |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 265 | signed char default_fd; |
| 266 | char descrip[3]; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 267 | } redir_table[] = { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 268 | { 0, 0, "()" }, |
| 269 | { O_RDONLY, 0, "<" }, |
| 270 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 271 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 272 | { O_RDONLY, -1, "<<" }, |
| 273 | { O_RDWR, 1, "<>" } |
| 274 | }; |
| 275 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 276 | typedef enum pipe_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 277 | PIPE_SEQ = 1, |
| 278 | PIPE_AND = 2, |
| 279 | PIPE_OR = 3, |
| 280 | PIPE_BG = 4, |
| 281 | } pipe_style; |
| 282 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 283 | typedef enum reserved_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 284 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 285 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 286 | RES_IF , |
| 287 | RES_THEN , |
| 288 | RES_ELIF , |
| 289 | RES_ELSE , |
| 290 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 291 | #endif |
| 292 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 293 | RES_FOR , |
| 294 | RES_WHILE , |
| 295 | RES_UNTIL , |
| 296 | RES_DO , |
| 297 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 298 | #endif |
| 299 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 300 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 301 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 302 | #if ENABLE_HUSH_CASE |
| 303 | RES_CASE , |
| 304 | /* two pseudo-keywords support contrived "case" syntax: */ |
| 305 | RES_MATCH , /* "word)" */ |
| 306 | RES_CASEI , /* "this command is inside CASE" */ |
| 307 | RES_ESAC , |
| 308 | #endif |
| 309 | RES_XXXX , |
| 310 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 311 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 312 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 313 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 314 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 315 | char *rd_filename; /* filename */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 316 | int fd; /* file descriptor being redirected */ |
| 317 | int dup; /* -1, or file descriptor being duplicated */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 318 | smallint /*enum redir_type*/ rd_type; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 319 | }; |
| 320 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 321 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 322 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 323 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 324 | smallint is_stopped; /* is the command currently running? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 325 | smallint grp_type; /* GRP_xxx */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 326 | struct pipe *group; /* if non-NULL, this "prog" is {} group, |
| 327 | * subshell, or a compound statement */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 328 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 329 | struct redir_struct *redirects; /* I/O redirections */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 330 | }; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 331 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 332 | * and on execution these are substituted with their values. |
| 333 | * Substitution can make _several_ words out of one argv[n]! |
| 334 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 335 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 336 | */ |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 337 | #define GRP_NORMAL 0 |
| 338 | #define GRP_SUBSHELL 1 |
| 339 | #if ENABLE_HUSH_FUNCTIONS |
| 340 | #define GRP_FUNCTION 2 |
| 341 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 342 | |
| 343 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 344 | struct pipe *next; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 345 | int num_cmds; /* total number of commands in job */ |
| 346 | int alive_cmds; /* number of commands running (not exited) */ |
| 347 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 348 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 349 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 350 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 351 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 352 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 353 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 354 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 355 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 356 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 357 | }; |
| 358 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 359 | /* This holds pointers to the various results of parsing */ |
| 360 | struct parse_context { |
| 361 | struct command *command; |
| 362 | struct pipe *list_head; |
| 363 | struct pipe *pipe; |
| 364 | struct redir_struct *pending_redirect; |
| 365 | #if HAS_KEYWORDS |
| 366 | smallint ctx_res_w; |
| 367 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 368 | #if ENABLE_HUSH_CASE |
| 369 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 370 | #endif |
| 371 | int old_flag; /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 372 | struct parse_context *stack; |
| 373 | #endif |
| 374 | }; |
| 375 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 376 | /* On program start, environ points to initial environment. |
| 377 | * putenv adds new pointers into it, unsetenv removes them. |
| 378 | * Neither of these (de)allocates the strings. |
| 379 | * setenv allocates new strings in malloc space and does putenv, |
| 380 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 381 | #define setenv(...) setenv_is_leaky_dont_use() |
| 382 | struct variable { |
| 383 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 384 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 385 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 386 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 387 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 388 | }; |
| 389 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 390 | typedef struct o_string { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 391 | char *data; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 392 | int length; /* position where data is appended */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 393 | int maxlen; |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 394 | /* Misnomer! it's not "quoting", it's "protection against globbing"! |
| 395 | * (by prepending \ to *, ?, [ and to \ too) */ |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 396 | smallint o_quote; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 397 | smallint o_glob; |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 398 | smallint nonnull; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 399 | smallint has_empty_slot; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 400 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 401 | } o_string; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 402 | enum { |
| 403 | MAYBE_ASSIGNMENT = 0, |
| 404 | DEFINITELY_ASSIGNMENT = 1, |
| 405 | NOT_ASSIGNMENT = 2, |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 406 | WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 407 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 408 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 409 | #define NULL_O_STRING { NULL } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 410 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 411 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 412 | * available? Where is it documented? */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 413 | typedef struct in_str { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 414 | const char *p; |
| 415 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 416 | char eof_flag; /* meaningless if ->p == NULL */ |
| 417 | char peek_buf[2]; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 418 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 419 | smallint promptme; |
| 420 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 421 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 422 | FILE *file; |
| 423 | int (*get) (struct in_str *); |
| 424 | int (*peek) (struct in_str *); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 425 | } in_str; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 426 | #define i_getch(input) ((input)->get(input)) |
| 427 | #define i_peek(input) ((input)->peek(input)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 428 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 429 | enum { |
| 430 | CHAR_ORDINARY = 0, |
| 431 | CHAR_ORDINARY_IF_QUOTED = 1, /* example: *, # */ |
| 432 | CHAR_IFS = 2, /* treated as ordinary if quoted */ |
| 433 | CHAR_SPECIAL = 3, /* example: $ */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 434 | }; |
| 435 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 436 | enum { |
| 437 | BC_BREAK = 1, |
| 438 | BC_CONTINUE = 2, |
| 439 | }; |
| 440 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 441 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 442 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 443 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 444 | struct globals { |
| 445 | #if ENABLE_HUSH_INTERACTIVE |
| 446 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 447 | * _AND_ if we decided to act interactively */ |
| 448 | int interactive_fd; |
| 449 | const char *PS1; |
| 450 | const char *PS2; |
| 451 | #endif |
| 452 | #if ENABLE_FEATURE_EDITING |
| 453 | line_input_t *line_input_state; |
| 454 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 455 | pid_t root_pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 456 | pid_t last_bg_pid; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 457 | #if ENABLE_HUSH_JOB |
| 458 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 459 | pid_t saved_tty_pgrp; |
| 460 | int last_jobid; |
| 461 | struct pipe *job_list; |
| 462 | struct pipe *toplevel_list; |
| 463 | smallint ctrl_z_flag; |
| 464 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 465 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 466 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 467 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 468 | smallint fake_mode; |
| 469 | /* these three support $?, $#, and $1 */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 470 | smalluint last_return_code; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 471 | /* is global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 472 | smalluint global_args_malloced; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 473 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 474 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 475 | char **global_argv; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 476 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 477 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 478 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 479 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 480 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 481 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 482 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 483 | struct variable shell_ver; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 484 | #if ENABLE_FEATURE_SH_STANDALONE |
| 485 | struct nofork_save_area nofork_save; |
| 486 | #endif |
| 487 | #if ENABLE_HUSH_JOB |
| 488 | sigjmp_buf toplevel_jb; |
| 489 | #endif |
| 490 | unsigned char charmap[256]; |
| 491 | char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2]; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 492 | struct { |
| 493 | char *cmd; |
| 494 | struct sigaction oact; |
| 495 | } *traps; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 496 | }; |
| 497 | |
| 498 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 499 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 500 | * (too many defines). Also, I actually prefer to see when a variable |
| 501 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 502 | #define INIT_G() do { \ |
| 503 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 504 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 505 | |
| 506 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 507 | /* Function prototypes for builtins */ |
| 508 | static int builtin_cd(char **argv); |
| 509 | static int builtin_echo(char **argv); |
| 510 | static int builtin_eval(char **argv); |
| 511 | static int builtin_exec(char **argv); |
| 512 | static int builtin_exit(char **argv); |
| 513 | static int builtin_export(char **argv); |
| 514 | #if ENABLE_HUSH_JOB |
| 515 | static int builtin_fg_bg(char **argv); |
| 516 | static int builtin_jobs(char **argv); |
| 517 | #endif |
| 518 | #if ENABLE_HUSH_HELP |
| 519 | static int builtin_help(char **argv); |
| 520 | #endif |
| 521 | static int builtin_pwd(char **argv); |
| 522 | static int builtin_read(char **argv); |
| 523 | static int builtin_test(char **argv); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 524 | static void handle_trap(int sig); |
| 525 | static int builtin_trap(char **argv); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 526 | static int builtin_true(char **argv); |
| 527 | static int builtin_set(char **argv); |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 528 | static int builtin_set_mode(const char, const char); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 529 | static int builtin_shift(char **argv); |
| 530 | static int builtin_source(char **argv); |
| 531 | static int builtin_umask(char **argv); |
| 532 | static int builtin_unset(char **argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 533 | static int builtin_wait(char **argv); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 534 | #if ENABLE_HUSH_LOOPS |
| 535 | static int builtin_break(char **argv); |
| 536 | static int builtin_continue(char **argv); |
| 537 | #endif |
| 538 | //static int builtin_not_written(char **argv); |
| 539 | |
| 540 | /* Table of built-in functions. They can be forked or not, depending on |
| 541 | * context: within pipes, they fork. As simple commands, they do not. |
| 542 | * When used in non-forking context, they can change global variables |
| 543 | * in the parent shell process. If forked, of course they cannot. |
| 544 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 545 | * still be set at the end. */ |
| 546 | struct built_in_command { |
| 547 | const char *cmd; |
| 548 | int (*function)(char **argv); |
| 549 | #if ENABLE_HUSH_HELP |
| 550 | const char *descr; |
| 551 | #define BLTIN(cmd, func, help) { cmd, func, help } |
| 552 | #else |
| 553 | #define BLTIN(cmd, func, help) { cmd, func } |
| 554 | #endif |
| 555 | }; |
| 556 | |
| 557 | /* For now, echo and test are unconditionally enabled. |
| 558 | * Maybe make it configurable? */ |
| 559 | static const struct built_in_command bltins[] = { |
| 560 | BLTIN("." , builtin_source, "Run commands in a file"), |
| 561 | BLTIN(":" , builtin_true, "No-op"), |
| 562 | BLTIN("[" , builtin_test, "Test condition"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 563 | #if ENABLE_HUSH_JOB |
| 564 | BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"), |
| 565 | #endif |
| 566 | #if ENABLE_HUSH_LOOPS |
| 567 | BLTIN("break" , builtin_break, "Exit from a loop"), |
| 568 | #endif |
| 569 | BLTIN("cd" , builtin_cd, "Change directory"), |
| 570 | #if ENABLE_HUSH_LOOPS |
| 571 | BLTIN("continue", builtin_continue, "Start new loop iteration"), |
| 572 | #endif |
| 573 | BLTIN("echo" , builtin_echo, "Write to stdout"), |
| 574 | BLTIN("eval" , builtin_eval, "Construct and run shell command"), |
| 575 | BLTIN("exec" , builtin_exec, "Execute command, don't return to shell"), |
| 576 | BLTIN("exit" , builtin_exit, "Exit"), |
| 577 | BLTIN("export", builtin_export, "Set environment variable"), |
| 578 | #if ENABLE_HUSH_JOB |
| 579 | BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"), |
| 580 | BLTIN("jobs" , builtin_jobs, "List active jobs"), |
| 581 | #endif |
| 582 | BLTIN("pwd" , builtin_pwd, "Print current directory"), |
| 583 | BLTIN("read" , builtin_read, "Input environment variable"), |
| 584 | // BLTIN("return", builtin_not_written, "Return from a function"), |
| 585 | BLTIN("set" , builtin_set, "Set/unset shell local variables"), |
| 586 | BLTIN("shift" , builtin_shift, "Shift positional parameters"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 587 | BLTIN("test" , builtin_test, "Test condition"), |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 588 | BLTIN("trap" , builtin_trap, "Trap signals"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 589 | // BLTIN("ulimit", builtin_not_written, "Control resource limits"), |
| 590 | BLTIN("umask" , builtin_umask, "Set file creation mask"), |
| 591 | BLTIN("unset" , builtin_unset, "Unset environment variable"), |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 592 | BLTIN("wait" , builtin_wait, "Wait for process"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 593 | #if ENABLE_HUSH_HELP |
| 594 | BLTIN("help" , builtin_help, "List shell built-in commands"), |
| 595 | #endif |
| 596 | }; |
| 597 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 598 | |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 599 | /* Normal */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 600 | static void maybe_die(const char *notice, const char *msg) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 601 | { |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 602 | /* Was using fancy stuff: |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 603 | * (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...) |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 604 | * but it SEGVs. ?! Oh well... explicit temp ptr works around that */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 605 | void FAST_FUNC (*fp)(const char *s, ...) = bb_error_msg_and_die; |
| 606 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 607 | fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 608 | #endif |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 609 | fp(msg ? "%s: %s" : notice, notice, msg); |
| 610 | } |
Mike Frysinger | 5a82845 | 2009-03-28 19:09:04 +0000 | [diff] [blame] | 611 | #if 1 |
| 612 | #define syntax(msg) maybe_die("syntax error", msg); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 613 | #else |
Mike Frysinger | 5a82845 | 2009-03-28 19:09:04 +0000 | [diff] [blame] | 614 | /* Debug -- trick gcc to expand __LINE__ and convert to string */ |
| 615 | #define __syntax(msg, line) maybe_die("syntax error hush.c:" # line, msg) |
| 616 | #define _syntax(msg, line) __syntax(msg, line) |
| 617 | #define syntax(msg) _syntax(msg, __LINE__) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 618 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 619 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 620 | static int glob_needed(const char *s) |
| 621 | { |
| 622 | while (*s) { |
| 623 | if (*s == '\\') |
| 624 | s++; |
| 625 | if (*s == '*' || *s == '[' || *s == '?') |
| 626 | return 1; |
| 627 | s++; |
| 628 | } |
| 629 | return 0; |
| 630 | } |
| 631 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 632 | static int is_assignment(const char *s) |
| 633 | { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 634 | if (!s || !(isalpha(*s) || *s == '_')) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 635 | return 0; |
| 636 | s++; |
| 637 | while (isalnum(*s) || *s == '_') |
| 638 | s++; |
| 639 | return *s == '='; |
| 640 | } |
| 641 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 642 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 643 | static char *unbackslash(char *src) |
| 644 | { |
| 645 | char *dst = src; |
| 646 | while (1) { |
| 647 | if (*src == '\\') |
| 648 | src++; |
| 649 | if ((*dst++ = *src++) == '\0') |
| 650 | break; |
| 651 | } |
| 652 | return dst; |
| 653 | } |
| 654 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 655 | 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] | 656 | { |
| 657 | int i; |
| 658 | unsigned count1; |
| 659 | unsigned count2; |
| 660 | char **v; |
| 661 | |
| 662 | v = strings; |
| 663 | count1 = 0; |
| 664 | if (v) { |
| 665 | while (*v) { |
| 666 | count1++; |
| 667 | v++; |
| 668 | } |
| 669 | } |
| 670 | count2 = 0; |
| 671 | v = add; |
| 672 | while (*v) { |
| 673 | count2++; |
| 674 | v++; |
| 675 | } |
| 676 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 677 | v[count1 + count2] = NULL; |
| 678 | i = count2; |
| 679 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 680 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 681 | return v; |
| 682 | } |
| 683 | |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 684 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 685 | { |
| 686 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 687 | v[0] = add; |
| 688 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 689 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 690 | } |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 691 | |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 692 | static void putenv_all(char **strings) |
| 693 | { |
| 694 | if (!strings) |
| 695 | return; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 696 | while (*strings) { |
| 697 | debug_printf_env("putenv '%s'\n", *strings); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 698 | putenv(*strings++); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 699 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | static char **putenv_all_and_save_old(char **strings) |
| 703 | { |
| 704 | char **old = NULL; |
| 705 | char **s = strings; |
| 706 | |
| 707 | if (!strings) |
| 708 | return old; |
| 709 | while (*strings) { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 710 | char *v, *eq; |
| 711 | |
| 712 | eq = strchr(*strings, '='); |
| 713 | if (eq) { |
| 714 | *eq = '\0'; |
| 715 | v = getenv(*strings); |
| 716 | *eq = '='; |
| 717 | if (v) { |
| 718 | /* v points to VAL in VAR=VAL, go back to VAR */ |
| 719 | v -= (eq - *strings) + 1; |
| 720 | old = add_string_to_strings(old, v); |
| 721 | } |
| 722 | } |
| 723 | strings++; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 724 | } |
| 725 | putenv_all(s); |
| 726 | return old; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 729 | static void free_strings_and_unsetenv(char **strings, int unset) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 730 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 731 | char **v; |
| 732 | |
| 733 | if (!strings) |
| 734 | return; |
| 735 | |
| 736 | v = strings; |
| 737 | while (*v) { |
| 738 | if (unset) { |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 739 | debug_printf_env("unsetenv '%s'\n", *v); |
| 740 | bb_unsetenv(*v); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 741 | } |
| 742 | free(*v++); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 743 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 744 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 747 | static void free_strings(char **strings) |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 748 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 749 | free_strings_and_unsetenv(strings, 0); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 750 | } |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 751 | |
| 752 | |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 753 | /* Signals are grouped, we handle them in batches */ |
| 754 | static void set_misc_sighandler(void (*handler)(int)) |
| 755 | { |
| 756 | bb_signals(0 |
| 757 | + (1 << SIGINT) |
| 758 | + (1 << SIGQUIT) |
| 759 | + (1 << SIGTERM) |
| 760 | , handler); |
| 761 | } |
| 762 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 763 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 764 | |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 765 | static void set_fatal_sighandler(void (*handler)(int)) |
| 766 | { |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 767 | bb_signals(0 |
| 768 | + (1 << SIGILL) |
| 769 | + (1 << SIGTRAP) |
| 770 | + (1 << SIGABRT) |
| 771 | + (1 << SIGFPE) |
| 772 | + (1 << SIGBUS) |
| 773 | + (1 << SIGSEGV) |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 774 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 775 | + (1 << SIGHUP) |
| 776 | + (1 << SIGPIPE) |
| 777 | + (1 << SIGALRM) |
| 778 | , handler); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 779 | } |
| 780 | static void set_jobctrl_sighandler(void (*handler)(int)) |
| 781 | { |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 782 | bb_signals(0 |
| 783 | + (1 << SIGTSTP) |
| 784 | + (1 << SIGTTIN) |
| 785 | + (1 << SIGTTOU) |
| 786 | , handler); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 787 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 788 | /* SIGCHLD is special and handled separately */ |
| 789 | |
| 790 | static void set_every_sighandler(void (*handler)(int)) |
| 791 | { |
| 792 | set_fatal_sighandler(handler); |
| 793 | set_jobctrl_sighandler(handler); |
| 794 | set_misc_sighandler(handler); |
| 795 | signal(SIGCHLD, handler); |
| 796 | } |
| 797 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 798 | static void handler_ctrl_c(int sig UNUSED_PARAM) |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 799 | { |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 800 | debug_printf_jobs("got sig %d\n", sig); |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 801 | // as usual we can have all kinds of nasty problems with leaked malloc data here |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 802 | siglongjmp(G.toplevel_jb, 1); |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 805 | static void handler_ctrl_z(int sig UNUSED_PARAM) |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 806 | { |
| 807 | pid_t pid; |
| 808 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 809 | debug_printf_jobs("got tty sig %d in pid %d\n", sig, getpid()); |
Mike Frysinger | bfc0fae | 2009-03-26 18:14:16 +0000 | [diff] [blame] | 810 | |
| 811 | if (!BB_MMU) { |
| 812 | fputs("Sorry, backgrounding (CTRL+Z) of foreground scripts not supported on nommu\n", stderr); |
| 813 | return; |
| 814 | } |
| 815 | |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 816 | pid = fork(); |
Denis Vlasenko | c299032 | 2007-05-16 12:57:12 +0000 | [diff] [blame] | 817 | if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */ |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 818 | return; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 819 | G.ctrl_z_flag = 1; |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 820 | if (!pid) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 821 | if (ENABLE_HUSH_JOB) |
| 822 | die_sleep = 0; /* let nofork's xfuncs die */ |
Bernhard Reutner-Fischer | 864329d | 2008-09-25 10:55:05 +0000 | [diff] [blame] | 823 | bb_setpgrp(); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 824 | debug_printf_jobs("set pgrp for child %d ok\n", getpid()); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 825 | set_every_sighandler(SIG_DFL); |
Denis Vlasenko | 18e19f2 | 2007-04-28 16:43:18 +0000 | [diff] [blame] | 826 | raise(SIGTSTP); /* resend TSTP so that child will be stopped */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 827 | debug_printf_jobs("returning in child\n"); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 828 | /* return to nofork, it will eventually exit now, |
| 829 | * not return back to shell */ |
| 830 | return; |
| 831 | } |
| 832 | /* parent */ |
| 833 | /* finish filling up pipe info */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 834 | G.toplevel_list->pgrp = pid; /* child is in its own pgrp */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 835 | G.toplevel_list->cmds[0].pid = pid; |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 836 | /* parent needs to longjmp out of running nofork. |
| 837 | * we will "return" exitcode 0, with child put in background */ |
| 838 | // as usual we can have all kinds of nasty problems with leaked malloc data here |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 839 | debug_printf_jobs("siglongjmp in parent\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 840 | siglongjmp(G.toplevel_jb, 1); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 843 | /* Restores tty foreground process group, and exits. |
| 844 | * May be called as signal handler for fatal signal |
| 845 | * (will faithfully resend signal to itself, producing correct exit state) |
| 846 | * or called directly with -EXITCODE. |
| 847 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 848 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 849 | static void sigexit(int sig) |
| 850 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 851 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 852 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 853 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 854 | #if ENABLE_HUSH_INTERACTIVE |
| 855 | if (G.interactive_fd) |
| 856 | tcsetpgrp(G.interactive_fd, G.saved_tty_pgrp); |
| 857 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 858 | |
| 859 | /* Not a signal, just exit */ |
| 860 | if (sig <= 0) |
| 861 | _exit(- sig); |
| 862 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 863 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 866 | #else /* !JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 867 | |
| 868 | #define set_fatal_sighandler(handler) ((void)0) |
| 869 | #define set_jobctrl_sighandler(handler) ((void)0) |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 870 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 871 | #endif /* JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 872 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 873 | /* Restores tty foreground process group, and exits. */ |
| 874 | static void hush_exit(int exitcode) NORETURN; |
| 875 | static void hush_exit(int exitcode) |
| 876 | { |
| 877 | if (G.traps && G.traps[0].cmd) |
| 878 | handle_trap(0); |
| 879 | |
| 880 | if (ENABLE_HUSH_JOB) { |
| 881 | fflush(NULL); /* flush all streams */ |
| 882 | sigexit(- (exitcode & 0xff)); |
| 883 | } else |
| 884 | exit(exitcode); |
| 885 | } |
| 886 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 887 | |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 888 | static const char *set_cwd(void) |
| 889 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 890 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 891 | * we must not try to free(bb_msg_unknown) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 892 | if (G.cwd == bb_msg_unknown) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 893 | G.cwd = NULL; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 894 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 895 | if (!G.cwd) |
| 896 | G.cwd = bb_msg_unknown; |
| 897 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 900 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 901 | /* Get/check local shell variables */ |
| 902 | static struct variable *get_local_var(const char *name) |
| 903 | { |
| 904 | struct variable *cur; |
| 905 | int len; |
| 906 | |
| 907 | if (!name) |
| 908 | return NULL; |
| 909 | len = strlen(name); |
| 910 | for (cur = G.top_var; cur; cur = cur->next) { |
| 911 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
| 912 | return cur; |
| 913 | } |
| 914 | return NULL; |
| 915 | } |
| 916 | |
| 917 | /* Basically useful version until someone wants to get fancier, |
| 918 | * see the bash man page under "Parameter Expansion" */ |
| 919 | static const char *lookup_param(const char *src) |
| 920 | { |
| 921 | struct variable *var = get_local_var(src); |
| 922 | if (var) |
| 923 | return strchr(var->varstr, '=') + 1; |
| 924 | return NULL; |
| 925 | } |
| 926 | |
| 927 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 928 | * We take ownership of it. |
| 929 | * flg_export is used by: |
| 930 | * 0: do not export |
| 931 | * 1: export |
| 932 | * -1: if NAME is set, leave export status alone |
| 933 | * if NAME is not set, do not export |
| 934 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 935 | static int set_local_var(char *str, int flg_export) |
| 936 | { |
| 937 | struct variable *cur; |
| 938 | char *value; |
| 939 | int name_len; |
| 940 | |
| 941 | value = strchr(str, '='); |
| 942 | if (!value) { /* not expected to ever happen? */ |
| 943 | free(str); |
| 944 | return -1; |
| 945 | } |
| 946 | |
| 947 | name_len = value - str + 1; /* including '=' */ |
| 948 | cur = G.top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */ |
| 949 | while (1) { |
| 950 | if (strncmp(cur->varstr, str, name_len) != 0) { |
| 951 | if (!cur->next) { |
| 952 | /* Bail out. Note that now cur points |
| 953 | * to last var in linked list */ |
| 954 | break; |
| 955 | } |
| 956 | cur = cur->next; |
| 957 | continue; |
| 958 | } |
| 959 | /* We found an existing var with this name */ |
| 960 | *value = '\0'; |
| 961 | if (cur->flg_read_only) { |
| 962 | bb_error_msg("%s: readonly variable", str); |
| 963 | free(str); |
| 964 | return -1; |
| 965 | } |
| 966 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 967 | unsetenv(str); /* just in case */ |
| 968 | *value = '='; |
| 969 | if (strcmp(cur->varstr, str) == 0) { |
| 970 | free_and_exp: |
| 971 | free(str); |
| 972 | goto exp; |
| 973 | } |
| 974 | if (cur->max_len >= strlen(str)) { |
| 975 | /* This one is from startup env, reuse space */ |
| 976 | strcpy(cur->varstr, str); |
| 977 | goto free_and_exp; |
| 978 | } |
| 979 | /* max_len == 0 signifies "malloced" var, which we can |
| 980 | * (and has to) free */ |
| 981 | if (!cur->max_len) |
| 982 | free(cur->varstr); |
| 983 | cur->max_len = 0; |
| 984 | goto set_str_and_exp; |
| 985 | } |
| 986 | |
| 987 | /* Not found - create next variable struct */ |
| 988 | cur->next = xzalloc(sizeof(*cur)); |
| 989 | cur = cur->next; |
| 990 | |
| 991 | set_str_and_exp: |
| 992 | cur->varstr = str; |
| 993 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 994 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 995 | cur->flg_export = 1; |
| 996 | if (cur->flg_export) { |
| 997 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 998 | return putenv(cur->varstr); |
| 999 | } |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame^] | 1003 | static int unset_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1004 | { |
| 1005 | struct variable *cur; |
| 1006 | struct variable *prev = prev; /* for gcc */ |
| 1007 | int name_len; |
| 1008 | |
| 1009 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame^] | 1010 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1011 | name_len = strlen(name); |
| 1012 | cur = G.top_var; |
| 1013 | while (cur) { |
| 1014 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1015 | if (cur->flg_read_only) { |
| 1016 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame^] | 1017 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1018 | } |
| 1019 | /* prev is ok to use here because 1st variable, HUSH_VERSION, |
| 1020 | * is ro, and we cannot reach this code on the 1st pass */ |
| 1021 | prev->next = cur->next; |
| 1022 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1023 | bb_unsetenv(cur->varstr); |
| 1024 | if (!cur->max_len) |
| 1025 | free(cur->varstr); |
| 1026 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame^] | 1027 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1028 | } |
| 1029 | prev = cur; |
| 1030 | cur = cur->next; |
| 1031 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame^] | 1032 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | |
| 1036 | /* |
| 1037 | * in_str support |
| 1038 | */ |
| 1039 | static int static_get(struct in_str *i) |
| 1040 | { |
| 1041 | int ch = *i->p++; |
| 1042 | if (ch == '\0') return EOF; |
| 1043 | return ch; |
| 1044 | } |
| 1045 | |
| 1046 | static int static_peek(struct in_str *i) |
| 1047 | { |
| 1048 | return *i->p; |
| 1049 | } |
| 1050 | |
| 1051 | #if ENABLE_HUSH_INTERACTIVE |
| 1052 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1053 | static void cmdedit_set_initial_prompt(void) |
| 1054 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1055 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1056 | G.PS1 = getenv("PS1"); |
| 1057 | if (G.PS1 == NULL) |
| 1058 | G.PS1 = "\\w \\$ "; |
| 1059 | } else |
| 1060 | G.PS1 = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1061 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1062 | |
| 1063 | static const char* setup_prompt_string(int promptmode) |
| 1064 | { |
| 1065 | const char *prompt_str; |
| 1066 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1067 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1068 | /* Set up the prompt */ |
| 1069 | if (promptmode == 0) { /* PS1 */ |
| 1070 | free((char*)G.PS1); |
| 1071 | G.PS1 = xasprintf("%s %c ", G.cwd, (geteuid() != 0) ? '$' : '#'); |
| 1072 | prompt_str = G.PS1; |
| 1073 | } else |
| 1074 | prompt_str = G.PS2; |
| 1075 | } else |
| 1076 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1077 | debug_printf("result '%s'\n", prompt_str); |
| 1078 | return prompt_str; |
| 1079 | } |
| 1080 | |
| 1081 | static void get_user_input(struct in_str *i) |
| 1082 | { |
| 1083 | int r; |
| 1084 | const char *prompt_str; |
| 1085 | |
| 1086 | prompt_str = setup_prompt_string(i->promptmode); |
| 1087 | #if ENABLE_FEATURE_EDITING |
| 1088 | /* Enable command line editing only while a command line |
| 1089 | * is actually being read */ |
| 1090 | do { |
| 1091 | r = read_line_input(prompt_str, G.user_input_buf, BUFSIZ-1, G.line_input_state); |
| 1092 | } while (r == 0); /* repeat if Ctrl-C */ |
| 1093 | i->eof_flag = (r < 0); |
| 1094 | if (i->eof_flag) { /* EOF/error detected */ |
| 1095 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1096 | G.user_input_buf[1] = '\0'; |
| 1097 | } |
| 1098 | #else |
| 1099 | fputs(prompt_str, stdout); |
| 1100 | fflush(stdout); |
| 1101 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1102 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
| 1103 | i->eof_flag = (r == EOF); |
| 1104 | #endif |
| 1105 | i->p = G.user_input_buf; |
| 1106 | } |
| 1107 | |
| 1108 | #endif /* INTERACTIVE */ |
| 1109 | |
| 1110 | /* This is the magic location that prints prompts |
| 1111 | * and gets data back from the user */ |
| 1112 | static int file_get(struct in_str *i) |
| 1113 | { |
| 1114 | int ch; |
| 1115 | |
| 1116 | /* If there is data waiting, eat it up */ |
| 1117 | if (i->p && *i->p) { |
| 1118 | #if ENABLE_HUSH_INTERACTIVE |
| 1119 | take_cached: |
| 1120 | #endif |
| 1121 | ch = *i->p++; |
| 1122 | if (i->eof_flag && !*i->p) |
| 1123 | ch = EOF; |
| 1124 | } else { |
| 1125 | /* need to double check i->file because we might be doing something |
| 1126 | * more complicated by now, like sourcing or substituting. */ |
| 1127 | #if ENABLE_HUSH_INTERACTIVE |
| 1128 | if (G.interactive_fd && i->promptme && i->file == stdin) { |
| 1129 | do { |
| 1130 | get_user_input(i); |
| 1131 | } while (!*i->p); /* need non-empty line */ |
| 1132 | i->promptmode = 1; /* PS2 */ |
| 1133 | i->promptme = 0; |
| 1134 | goto take_cached; |
| 1135 | } |
| 1136 | #endif |
| 1137 | ch = fgetc(i->file); |
| 1138 | } |
| 1139 | debug_printf("file_get: got a '%c' %d\n", ch, ch); |
| 1140 | #if ENABLE_HUSH_INTERACTIVE |
| 1141 | if (ch == '\n') |
| 1142 | i->promptme = 1; |
| 1143 | #endif |
| 1144 | return ch; |
| 1145 | } |
| 1146 | |
| 1147 | /* All the callers guarantee this routine will never be |
| 1148 | * used right after a newline, so prompting is not needed. |
| 1149 | */ |
| 1150 | static int file_peek(struct in_str *i) |
| 1151 | { |
| 1152 | int ch; |
| 1153 | if (i->p && *i->p) { |
| 1154 | if (i->eof_flag && !i->p[1]) |
| 1155 | return EOF; |
| 1156 | return *i->p; |
| 1157 | } |
| 1158 | ch = fgetc(i->file); |
| 1159 | i->eof_flag = (ch == EOF); |
| 1160 | i->peek_buf[0] = ch; |
| 1161 | i->peek_buf[1] = '\0'; |
| 1162 | i->p = i->peek_buf; |
| 1163 | debug_printf("file_peek: got a '%c' %d\n", *i->p, *i->p); |
| 1164 | return ch; |
| 1165 | } |
| 1166 | |
| 1167 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1168 | { |
| 1169 | i->peek = file_peek; |
| 1170 | i->get = file_get; |
| 1171 | #if ENABLE_HUSH_INTERACTIVE |
| 1172 | i->promptme = 1; |
| 1173 | i->promptmode = 0; /* PS1 */ |
| 1174 | #endif |
| 1175 | i->file = f; |
| 1176 | i->p = NULL; |
| 1177 | } |
| 1178 | |
| 1179 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1180 | { |
| 1181 | i->peek = static_peek; |
| 1182 | i->get = static_get; |
| 1183 | #if ENABLE_HUSH_INTERACTIVE |
| 1184 | i->promptme = 1; |
| 1185 | i->promptmode = 0; /* PS1 */ |
| 1186 | #endif |
| 1187 | i->p = s; |
| 1188 | i->eof_flag = 0; |
| 1189 | } |
| 1190 | |
| 1191 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1192 | /* |
| 1193 | * o_string support |
| 1194 | */ |
| 1195 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1196 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1197 | static void o_reset(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1198 | { |
| 1199 | o->length = 0; |
| 1200 | o->nonnull = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1201 | if (o->data) |
| 1202 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1205 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1206 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1207 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1208 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1211 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1212 | { |
| 1213 | if (o->length + len > o->maxlen) { |
| 1214 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 1215 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 1216 | } |
| 1217 | } |
| 1218 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1219 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1220 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1221 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1222 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1223 | o->data[o->length] = ch; |
| 1224 | o->length++; |
| 1225 | o->data[o->length] = '\0'; |
| 1226 | } |
| 1227 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1228 | static void o_addstr(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1229 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1230 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1231 | memcpy(&o->data[o->length], str, len); |
| 1232 | o->length += len; |
| 1233 | o->data[o->length] = '\0'; |
| 1234 | } |
| 1235 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1236 | static void o_addstr_duplicate_backslash(o_string *o, const char *str, int len) |
| 1237 | { |
| 1238 | while (len) { |
| 1239 | o_addchr(o, *str); |
| 1240 | if (*str++ == '\\' |
| 1241 | && (*str != '*' && *str != '?' && *str != '[') |
| 1242 | ) { |
| 1243 | o_addchr(o, '\\'); |
| 1244 | } |
| 1245 | len--; |
| 1246 | } |
| 1247 | } |
| 1248 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1249 | /* My analysis of quoting semantics tells me that state information |
| 1250 | * is associated with a destination, not a source. |
| 1251 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1252 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1253 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1254 | int sz = 1; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1255 | char *found = strchr("*?[\\", ch); |
| 1256 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1257 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1258 | o_grow_by(o, sz); |
| 1259 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1260 | o->data[o->length] = '\\'; |
| 1261 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1262 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1263 | o->data[o->length] = ch; |
| 1264 | o->length++; |
| 1265 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1268 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1269 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1270 | int sz = 1; |
| 1271 | if (o->o_quote && strchr("*?[\\", ch)) { |
| 1272 | sz++; |
| 1273 | o->data[o->length] = '\\'; |
| 1274 | o->length++; |
| 1275 | } |
| 1276 | o_grow_by(o, sz); |
| 1277 | o->data[o->length] = ch; |
| 1278 | o->length++; |
| 1279 | o->data[o->length] = '\0'; |
| 1280 | } |
| 1281 | |
| 1282 | static void o_addQstr(o_string *o, const char *str, int len) |
| 1283 | { |
| 1284 | if (!o->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1285 | o_addstr(o, str, len); |
| 1286 | return; |
| 1287 | } |
| 1288 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1289 | char ch; |
| 1290 | int sz; |
| 1291 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 1292 | if (ordinary_cnt > len) /* paranoia */ |
| 1293 | ordinary_cnt = len; |
| 1294 | o_addstr(o, str, ordinary_cnt); |
| 1295 | if (ordinary_cnt == len) |
| 1296 | return; |
| 1297 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1298 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1299 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1300 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1301 | sz = 1; |
| 1302 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 1303 | sz++; |
| 1304 | o->data[o->length] = '\\'; |
| 1305 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1306 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1307 | o_grow_by(o, sz); |
| 1308 | o->data[o->length] = ch; |
| 1309 | o->length++; |
| 1310 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1311 | } |
| 1312 | } |
| 1313 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1314 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1315 | * 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] | 1316 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1317 | * list[i] contains an INDEX (int!) into this string data. |
| 1318 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1319 | * but list[i]'s need not be modified. |
| 1320 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1321 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1322 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1323 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1324 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1325 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1326 | { |
| 1327 | char **list = (char**)o->data; |
| 1328 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1329 | int i = 0; |
| 1330 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1331 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1332 | while (i < n) { |
| 1333 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1334 | o->data + (int)list[i] + string_start, |
| 1335 | o->data + (int)list[i] + string_start); |
| 1336 | i++; |
| 1337 | } |
| 1338 | if (n) { |
| 1339 | const char *p = o->data + (int)list[n - 1] + string_start; |
Mike Frysinger | d006edb | 2009-03-28 12:43:53 +0000 | [diff] [blame] | 1340 | fprintf(stderr, " total_sz:%ld\n", (p + strlen(p) + 1) - o->data); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1341 | } |
| 1342 | } |
| 1343 | #else |
| 1344 | #define debug_print_list(prefix, o, n) ((void)0) |
| 1345 | #endif |
| 1346 | |
| 1347 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1348 | * in list[n] so that it points past last stored byte so far. |
| 1349 | * It returns n+1. */ |
| 1350 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1351 | { |
| 1352 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1353 | int string_start; |
| 1354 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1355 | |
| 1356 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1357 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1358 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1359 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1360 | 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] | 1361 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1362 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1363 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1364 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1365 | memmove(list + n + 0x10, list + n, string_len); |
| 1366 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1367 | } else |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1368 | debug_printf_list("list[%d]=%d string_start=%d\n", n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1369 | } else { |
| 1370 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1371 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1372 | string_len = o->length - string_start; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1373 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1374 | o->has_empty_slot = 0; |
| 1375 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1376 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1377 | return n + 1; |
| 1378 | } |
| 1379 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1380 | /* "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] | 1381 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1382 | { |
| 1383 | char **list = (char**)o->data; |
| 1384 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1385 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1386 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1389 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1390 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1391 | static int o_glob(o_string *o, int n) |
| 1392 | { |
| 1393 | glob_t globdata; |
| 1394 | int gr; |
| 1395 | char *pattern; |
| 1396 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1397 | 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] | 1398 | if (!o->data) |
| 1399 | return o_save_ptr_helper(o, n); |
| 1400 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1401 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1402 | if (!glob_needed(pattern)) { |
| 1403 | literal: |
| 1404 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1405 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1406 | return o_save_ptr_helper(o, n); |
| 1407 | } |
| 1408 | |
| 1409 | memset(&globdata, 0, sizeof(globdata)); |
| 1410 | gr = glob(pattern, 0, NULL, &globdata); |
| 1411 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 1412 | if (gr == GLOB_NOSPACE) |
| 1413 | bb_error_msg_and_die("out of memory during glob"); |
| 1414 | if (gr == GLOB_NOMATCH) { |
| 1415 | globfree(&globdata); |
| 1416 | goto literal; |
| 1417 | } |
| 1418 | if (gr != 0) { /* GLOB_ABORTED ? */ |
| 1419 | //TODO: testcase for bad glob pattern behavior |
| 1420 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 1421 | } |
| 1422 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 1423 | char **argv = globdata.gl_pathv; |
| 1424 | o->length = pattern - o->data; /* "forget" pattern */ |
| 1425 | while (1) { |
| 1426 | o_addstr(o, *argv, strlen(*argv) + 1); |
| 1427 | n = o_save_ptr_helper(o, n); |
| 1428 | argv++; |
| 1429 | if (!*argv) |
| 1430 | break; |
| 1431 | } |
| 1432 | } |
| 1433 | globfree(&globdata); |
| 1434 | if (DEBUG_GLOB) |
| 1435 | debug_print_list("o_glob returning", o, n); |
| 1436 | return n; |
| 1437 | } |
| 1438 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1439 | /* If o->o_glob == 1, glob the string so far remembered. |
| 1440 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1441 | static int o_save_ptr(o_string *o, int n) |
| 1442 | { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 1443 | if (o->o_glob) { /* if globbing is requested */ |
| 1444 | /* If o->has_empty_slot, list[n] was already globbed |
| 1445 | * (if it was requested back then when it was filled) |
| 1446 | * so don't do that again! */ |
| 1447 | if (!o->has_empty_slot) |
| 1448 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 1449 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1450 | return o_save_ptr_helper(o, n); |
| 1451 | } |
| 1452 | |
| 1453 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1454 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1455 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1456 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1457 | int string_start; |
| 1458 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1459 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 1460 | if (DEBUG_EXPAND) |
| 1461 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1462 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1463 | list = (char**)o->data; |
| 1464 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1465 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1466 | while (n) { |
| 1467 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1468 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1469 | } |
| 1470 | return list; |
| 1471 | } |
| 1472 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1473 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1474 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 1475 | * all variable references within and returns a pointer to |
| 1476 | * a list of expanded strings, possibly with larger number |
| 1477 | * of strings. (Think VAR="a b"; echo $VAR). |
| 1478 | * This new list is allocated as a single malloc block. |
| 1479 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 1480 | * followed by strings themself. |
| 1481 | * Caller can deallocate entire list by single free(list). */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1482 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1483 | /* Store given string, finalizing the word and starting new one whenever |
| 1484 | * we encounter IFS char(s). This is used for expanding variable values. |
| 1485 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 1486 | 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] | 1487 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1488 | while (1) { |
| 1489 | int word_len = strcspn(str, G.ifs); |
| 1490 | if (word_len) { |
| 1491 | if (output->o_quote || !output->o_glob) |
| 1492 | o_addQstr(output, str, word_len); |
| 1493 | else /* protect backslashes against globbing up :) */ |
| 1494 | o_addstr_duplicate_backslash(output, str, word_len); |
| 1495 | str += word_len; |
| 1496 | } |
| 1497 | if (!*str) /* EOL - do not finalize word */ |
| 1498 | break; |
| 1499 | o_addchr(output, '\0'); |
| 1500 | debug_print_list("expand_on_ifs", output, n); |
| 1501 | n = o_save_ptr(output, n); |
| 1502 | str += strspn(str, G.ifs); /* skip ifs chars */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1503 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1504 | debug_print_list("expand_on_ifs[1]", output, n); |
| 1505 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1508 | #if ENABLE_HUSH_TICK |
| 1509 | static int process_command_subs(o_string *dest, |
| 1510 | struct in_str *input, const char *subst_end); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1511 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1512 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1513 | /* Expand all variable references in given string, adding words to list[] |
| 1514 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 1515 | * to be filled). This routine is extremely tricky: has to deal with |
| 1516 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 1517 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
| 1518 | 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] | 1519 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1520 | /* or_mask is either 0 (normal case) or 0x80 |
| 1521 | * (expansion of right-hand side of assignment == 1-element expand. |
| 1522 | * 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] | 1523 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1524 | char first_ch, ored_ch; |
| 1525 | int i; |
| 1526 | const char *val; |
| 1527 | char *p; |
| 1528 | |
| 1529 | ored_ch = 0; |
| 1530 | |
| 1531 | debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg); |
| 1532 | debug_print_list("expand_vars_to_list", output, n); |
| 1533 | n = o_save_ptr(output, n); |
| 1534 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 1535 | |
| 1536 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 1537 | #if ENABLE_HUSH_TICK |
| 1538 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1539 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1540 | o_addstr(output, arg, p - arg); |
| 1541 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 1542 | arg = ++p; |
| 1543 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 1544 | |
| 1545 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
| 1546 | /* "$@" is special. Even if quoted, it can still |
| 1547 | * expand to nothing (not even an empty string) */ |
| 1548 | if ((first_ch & 0x7f) != '@') |
| 1549 | ored_ch |= first_ch; |
| 1550 | val = NULL; |
| 1551 | switch (first_ch & 0x7f) { |
| 1552 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 1553 | case '$': /* pid */ |
| 1554 | val = utoa(G.root_pid); |
| 1555 | break; |
| 1556 | case '!': /* bg pid */ |
| 1557 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
| 1558 | break; |
| 1559 | case '?': /* exitcode */ |
| 1560 | val = utoa(G.last_return_code); |
| 1561 | break; |
| 1562 | case '#': /* argc */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1563 | if (arg[1] != SPECIAL_VAR_SYMBOL) |
| 1564 | /* actually, it's a ${#var} */ |
| 1565 | goto case_default; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1566 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 1567 | break; |
| 1568 | case '*': |
| 1569 | case '@': |
| 1570 | i = 1; |
| 1571 | if (!G.global_argv[i]) |
| 1572 | break; |
| 1573 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
| 1574 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
| 1575 | smallint sv = output->o_quote; |
| 1576 | /* unquoted var's contents should be globbed, so don't quote */ |
| 1577 | output->o_quote = 0; |
| 1578 | while (G.global_argv[i]) { |
| 1579 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 1580 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 1581 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 1582 | /* this argv[] is not empty and not last: |
| 1583 | * put terminating NUL, start new word */ |
| 1584 | o_addchr(output, '\0'); |
| 1585 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 1586 | n = o_save_ptr(output, n); |
| 1587 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 1588 | } |
| 1589 | } |
| 1590 | output->o_quote = sv; |
| 1591 | } else |
| 1592 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
| 1593 | * and in this case should treat it like '$*' - see 'else...' below */ |
| 1594 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
| 1595 | while (1) { |
| 1596 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 1597 | if (++i >= G.global_argc) |
| 1598 | break; |
| 1599 | o_addchr(output, '\0'); |
| 1600 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 1601 | n = o_save_ptr(output, n); |
| 1602 | } |
| 1603 | } else { /* quoted $*: add as one word */ |
| 1604 | while (1) { |
| 1605 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 1606 | if (!G.global_argv[++i]) |
| 1607 | break; |
| 1608 | if (G.ifs[0]) |
| 1609 | o_addchr(output, G.ifs[0]); |
| 1610 | } |
| 1611 | } |
| 1612 | break; |
| 1613 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 1614 | /* "Empty variable", used to make "" etc to not disappear */ |
| 1615 | arg++; |
| 1616 | ored_ch = 0x80; |
| 1617 | break; |
| 1618 | #if ENABLE_HUSH_TICK |
| 1619 | case '`': { /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
| 1620 | struct in_str input; |
| 1621 | *p = '\0'; |
| 1622 | arg++; |
| 1623 | //TODO: can we just stuff it into "output" directly? |
| 1624 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 1625 | setup_string_in_str(&input, arg); |
| 1626 | process_command_subs(&subst_result, &input, NULL); |
| 1627 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
| 1628 | val = subst_result.data; |
| 1629 | goto store_val; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1630 | } |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1631 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1632 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1633 | case_default: { |
| 1634 | bool exp_len = false, exp_null = false; |
| 1635 | char *var = arg, exp_save, exp_op, *exp_word; |
| 1636 | size_t exp_off = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1637 | *p = '\0'; |
| 1638 | arg[0] = first_ch & 0x7f; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1639 | |
| 1640 | /* prepare for expansions */ |
| 1641 | if (var[0] == '#') { |
| 1642 | /* handle length expansion ${#var} */ |
| 1643 | exp_len = true; |
| 1644 | ++var; |
| 1645 | } else { |
| 1646 | /* maybe handle parameter expansion */ |
| 1647 | exp_off = strcspn(var, ":-=+?"); |
| 1648 | if (!var[exp_off]) |
| 1649 | exp_off = 0; |
| 1650 | if (exp_off) { |
| 1651 | exp_save = var[exp_off]; |
| 1652 | exp_null = exp_save == ':'; |
| 1653 | exp_word = var + exp_off; |
| 1654 | if (exp_null) ++exp_word; |
| 1655 | exp_op = *exp_word++; |
| 1656 | var[exp_off] = '\0'; |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | /* lookup the variable in question */ |
| 1661 | if (isdigit(var[0])) { |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 1662 | /* handle_dollar() should have vetted var for us */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1663 | i = xatoi_u(var); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1664 | if (i < G.global_argc) |
| 1665 | val = G.global_argv[i]; |
| 1666 | /* else val remains NULL: $N with too big N */ |
| 1667 | } else |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1668 | val = lookup_param(var); |
| 1669 | |
| 1670 | /* handle any expansions */ |
| 1671 | if (exp_len) { |
| 1672 | debug_printf_expand("expand: length of '%s' = ", val); |
| 1673 | val = utoa(val ? strlen(val) : 0); |
| 1674 | debug_printf_expand("%s\n", val); |
| 1675 | } else if (exp_off) { |
| 1676 | /* we need to do an expansion */ |
| 1677 | int exp_test = (!val || (exp_null && !val[0])); |
| 1678 | if (exp_op == '+') |
| 1679 | exp_test = !exp_test; |
| 1680 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 1681 | exp_null ? "true" : "false", exp_test); |
| 1682 | if (exp_test) { |
| 1683 | if (exp_op == '?') |
| 1684 | maybe_die(var, *exp_word ? exp_word : "parameter null or not set"); |
| 1685 | else |
| 1686 | val = exp_word; |
| 1687 | |
| 1688 | if (exp_op == '=') { |
| 1689 | if (isdigit(var[0]) || var[0] == '#') { |
| 1690 | maybe_die(var, "special vars cannot assign in this way"); |
| 1691 | val = NULL; |
| 1692 | } else { |
| 1693 | char *new_var = xmalloc(strlen(var) + strlen(val) + 2); |
| 1694 | sprintf(new_var, "%s=%s", var, val); |
| 1695 | set_local_var(new_var, -1); |
| 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | var[exp_off] = exp_save; |
| 1700 | } |
| 1701 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1702 | arg[0] = first_ch; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1703 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1704 | #if ENABLE_HUSH_TICK |
| 1705 | store_val: |
| 1706 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1707 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
| 1708 | debug_printf_expand("unquoted '%s', output->o_quote:%d\n", val, output->o_quote); |
| 1709 | if (val) { |
| 1710 | /* unquoted var's contents should be globbed, so don't quote */ |
| 1711 | smallint sv = output->o_quote; |
| 1712 | output->o_quote = 0; |
| 1713 | n = expand_on_ifs(output, n, val); |
| 1714 | val = NULL; |
| 1715 | output->o_quote = sv; |
| 1716 | } |
| 1717 | } else { /* quoted $VAR, val will be appended below */ |
| 1718 | debug_printf_expand("quoted '%s', output->o_quote:%d\n", val, output->o_quote); |
| 1719 | } |
| 1720 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1721 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1722 | if (val) { |
| 1723 | o_addQstr(output, val, strlen(val)); |
| 1724 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1725 | /* Do the check to avoid writing to a const string */ |
| 1726 | if (p && *p != SPECIAL_VAR_SYMBOL) |
| 1727 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1728 | |
| 1729 | #if ENABLE_HUSH_TICK |
| 1730 | o_free(&subst_result); |
| 1731 | #endif |
| 1732 | arg = ++p; |
| 1733 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 1734 | |
| 1735 | if (arg[0]) { |
| 1736 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 1737 | /* this part is literal, and it was already pre-quoted |
| 1738 | * if needed (much earlier), do not use o_addQstr here! */ |
| 1739 | o_addstr(output, arg, strlen(arg) + 1); |
| 1740 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 1741 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 1742 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 1743 | ) { |
| 1744 | n--; |
| 1745 | /* allow to reuse list[n] later without re-growth */ |
| 1746 | output->has_empty_slot = 1; |
| 1747 | } else { |
| 1748 | o_addchr(output, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1749 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1750 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1753 | static char **expand_variables(char **argv, int or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1754 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1755 | int n; |
| 1756 | char **list; |
| 1757 | char **v; |
| 1758 | o_string output = NULL_O_STRING; |
| 1759 | |
| 1760 | if (or_mask & 0x100) { |
| 1761 | output.o_quote = 1; /* protect against globbing for "$var" */ |
| 1762 | /* (unquoted $var will temporarily switch it off) */ |
| 1763 | output.o_glob = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1764 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1765 | |
| 1766 | n = 0; |
| 1767 | v = argv; |
| 1768 | while (*v) { |
| 1769 | n = expand_vars_to_list(&output, n, *v, (char)or_mask); |
| 1770 | v++; |
| 1771 | } |
| 1772 | debug_print_list("expand_variables", &output, n); |
| 1773 | |
| 1774 | /* output.data (malloced in one block) gets returned in "list" */ |
| 1775 | list = o_finalize_list(&output, n); |
| 1776 | debug_print_strings("expand_variables[1]", list); |
| 1777 | return list; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1780 | static char **expand_strvec_to_strvec(char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1781 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1782 | return expand_variables(argv, 0x100); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1785 | /* Used for expansion of right hand of assignments */ |
| 1786 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 1787 | * "v=/bin/c*" */ |
| 1788 | static char *expand_string_to_string(const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1789 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1790 | char *argv[2], **list; |
| 1791 | |
| 1792 | argv[0] = (char*)str; |
| 1793 | argv[1] = NULL; |
| 1794 | list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */ |
| 1795 | if (HUSH_DEBUG) |
| 1796 | if (!list[0] || list[1]) |
| 1797 | bb_error_msg_and_die("BUG in varexp2"); |
| 1798 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 1799 | overlapping_strcpy((char*)list, list[0]); |
| 1800 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 1801 | return (char*)list; |
| 1802 | } |
| 1803 | |
| 1804 | /* Used for "eval" builtin */ |
| 1805 | static char* expand_strvec_to_string(char **argv) |
| 1806 | { |
| 1807 | char **list; |
| 1808 | |
| 1809 | list = expand_variables(argv, 0x80); |
| 1810 | /* Convert all NULs to spaces */ |
| 1811 | if (list[0]) { |
| 1812 | int n = 1; |
| 1813 | while (list[n]) { |
| 1814 | if (HUSH_DEBUG) |
| 1815 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 1816 | bb_error_msg_and_die("BUG in varexp3"); |
| 1817 | list[n][-1] = ' '; /* TODO: or to G.ifs[0]? */ |
| 1818 | n++; |
| 1819 | } |
| 1820 | } |
| 1821 | overlapping_strcpy((char*)list, list[0]); |
| 1822 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 1823 | return (char*)list; |
| 1824 | } |
| 1825 | |
| 1826 | static char **expand_assignments(char **argv, int count) |
| 1827 | { |
| 1828 | int i; |
| 1829 | char **p = NULL; |
| 1830 | /* Expand assignments into one string each */ |
| 1831 | for (i = 0; i < count; i++) { |
| 1832 | p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 1833 | } |
| 1834 | return p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1837 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1838 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 1839 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1840 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1841 | { |
| 1842 | int openfd, mode; |
| 1843 | struct redir_struct *redir; |
| 1844 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1845 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1846 | if (redir->dup == -1 && redir->rd_filename == NULL) { |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 1847 | /* something went wrong in the parse. Pretend it didn't happen */ |
| 1848 | continue; |
| 1849 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1850 | if (redir->dup == -1) { |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1851 | char *p; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1852 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 1853 | //TODO: check redir for names like '\\' |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1854 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1855 | openfd = open_or_warn(p, mode); |
| 1856 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1857 | if (openfd < 0) { |
| 1858 | /* this could get lost if stderr has been redirected, but |
| 1859 | bash and ash both lose it as well (though zsh doesn't!) */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1860 | return 1; |
| 1861 | } |
| 1862 | } else { |
| 1863 | openfd = redir->dup; |
| 1864 | } |
| 1865 | |
| 1866 | if (openfd != redir->fd) { |
| 1867 | if (squirrel && redir->fd < 3) { |
| 1868 | squirrel[redir->fd] = dup(redir->fd); |
| 1869 | } |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1870 | if (openfd == -3) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1871 | //close(openfd); // close(-3) ??! |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1872 | } else { |
| 1873 | dup2(openfd, redir->fd); |
Matt Kraai | c616e53 | 2001-06-05 16:50:08 +0000 | [diff] [blame] | 1874 | if (redir->dup == -1) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1875 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1876 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1877 | } |
| 1878 | } |
| 1879 | return 0; |
| 1880 | } |
| 1881 | |
| 1882 | static void restore_redirects(int squirrel[]) |
| 1883 | { |
| 1884 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1885 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1886 | fd = squirrel[i]; |
| 1887 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 1888 | /* We simply die on error */ |
| 1889 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1890 | } |
| 1891 | } |
| 1892 | } |
| 1893 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1894 | |
| 1895 | #if !defined(DEBUG_CLEAN) |
| 1896 | #define free_pipe_list(head, indent) free_pipe_list(head) |
| 1897 | #define free_pipe(pi, indent) free_pipe(pi) |
| 1898 | #endif |
| 1899 | static int free_pipe_list(struct pipe *head, int indent); |
| 1900 | |
| 1901 | /* return code is the exit status of the pipe */ |
| 1902 | static int free_pipe(struct pipe *pi, int indent) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 1903 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1904 | char **p; |
| 1905 | struct command *command; |
| 1906 | struct redir_struct *r, *rnext; |
| 1907 | int a, i, ret_code = 0; |
| 1908 | |
| 1909 | if (pi->stopped_cmds > 0) |
| 1910 | return ret_code; |
| 1911 | debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid()); |
| 1912 | for (i = 0; i < pi->num_cmds; i++) { |
| 1913 | command = &pi->cmds[i]; |
| 1914 | debug_printf_clean("%s command %d:\n", indenter(indent), i); |
| 1915 | if (command->argv) { |
| 1916 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 1917 | debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p); |
| 1918 | } |
| 1919 | free_strings(command->argv); |
| 1920 | command->argv = NULL; |
| 1921 | } else if (command->group) { |
| 1922 | debug_printf_clean("%s begin group (grp_type:%d)\n", indenter(indent), command->grp_type); |
| 1923 | ret_code = free_pipe_list(command->group, indent+3); |
| 1924 | debug_printf_clean("%s end group\n", indenter(indent)); |
| 1925 | } else { |
| 1926 | debug_printf_clean("%s (nil)\n", indenter(indent)); |
| 1927 | } |
| 1928 | for (r = command->redirects; r; r = rnext) { |
| 1929 | debug_printf_clean("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->rd_type].descrip); |
| 1930 | if (r->dup == -1) { |
| 1931 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 1932 | if (r->rd_filename) { |
| 1933 | debug_printf_clean(" %s\n", r->rd_filename); |
| 1934 | free(r->rd_filename); |
| 1935 | r->rd_filename = NULL; |
| 1936 | } |
| 1937 | } else { |
| 1938 | debug_printf_clean("&%d\n", r->dup); |
| 1939 | } |
| 1940 | rnext = r->next; |
| 1941 | free(r); |
| 1942 | } |
| 1943 | command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 1944 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1945 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 1946 | pi->cmds = NULL; |
| 1947 | #if ENABLE_HUSH_JOB |
| 1948 | free(pi->cmdtext); |
| 1949 | pi->cmdtext = NULL; |
| 1950 | #endif |
| 1951 | return ret_code; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 1952 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1953 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1954 | static int free_pipe_list(struct pipe *head, int indent) |
| 1955 | { |
| 1956 | int rcode = 0; /* if list has no members */ |
| 1957 | struct pipe *pi, *next; |
| 1958 | |
| 1959 | for (pi = head; pi; pi = next) { |
| 1960 | #if HAS_KEYWORDS |
| 1961 | debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word); |
| 1962 | #endif |
| 1963 | rcode = free_pipe(pi, indent); |
| 1964 | debug_printf_clean("%s pipe followup code %d\n", indenter(indent), pi->followup); |
| 1965 | next = pi->next; |
| 1966 | /*pi->next = NULL;*/ |
| 1967 | free(pi); |
| 1968 | } |
| 1969 | return rcode; |
| 1970 | } |
| 1971 | |
| 1972 | |
| 1973 | #if !BB_MMU |
| 1974 | typedef struct nommu_save_t { |
| 1975 | char **new_env; |
| 1976 | char **old_env; |
| 1977 | char **argv; |
| 1978 | } nommu_save_t; |
| 1979 | #else |
| 1980 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 1981 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 1982 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 1983 | pseudo_exec(command, argv_expanded) |
| 1984 | #endif |
| 1985 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1986 | /* Called after [v]fork() in run_pipe(), or from builtin_exec(). |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1987 | * Never returns. |
| 1988 | * XXX no exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1989 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1990 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1991 | static void pseudo_exec_argv(nommu_save_t *nommu_save, char **argv, int assignment_cnt, char **argv_expanded) NORETURN; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 1992 | static void pseudo_exec_argv(nommu_save_t *nommu_save, char **argv, int assignment_cnt, char **argv_expanded) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1993 | { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 1994 | int rcode; |
| 1995 | char **new_env; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 1996 | const struct built_in_command *x; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1997 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1998 | /* If a variable is assigned in a forest, and nobody listens, |
| 1999 | * was it ever really set? |
| 2000 | */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2001 | if (!argv[assignment_cnt]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2002 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2003 | |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 2004 | new_env = expand_assignments(argv, assignment_cnt); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2005 | #if BB_MMU |
| 2006 | putenv_all(new_env); |
| 2007 | free(new_env); /* optional */ |
| 2008 | #else |
| 2009 | nommu_save->new_env = new_env; |
| 2010 | nommu_save->old_env = putenv_all_and_save_old(new_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2011 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2012 | if (argv_expanded) { |
| 2013 | argv = argv_expanded; |
| 2014 | } else { |
| 2015 | argv = expand_strvec_to_strvec(argv); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2016 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2017 | nommu_save->argv = argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2018 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2019 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2020 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2021 | /* |
| 2022 | * Check if the command matches any of the builtins. |
| 2023 | * Depending on context, this might be redundant. But it's |
| 2024 | * easier to waste a few CPU cycles than it is to figure out |
| 2025 | * if this is one of those cases. |
| 2026 | */ |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 2027 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2028 | if (strcmp(argv[0], x->cmd) == 0) { |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2029 | debug_printf_exec("running builtin '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2030 | rcode = x->function(argv); |
| 2031 | fflush(stdout); |
| 2032 | _exit(rcode); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2033 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2034 | } |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2035 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2036 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 2037 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2038 | if (strchr(argv[0], '/') == NULL) { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 2039 | int a = find_applet_by_name(argv[0]); |
| 2040 | if (a >= 0) { |
| 2041 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2042 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 2043 | // is it ok that run_applet_no_and_exit() does exit(), not _exit()? |
| 2044 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2045 | } |
| 2046 | /* re-exec ourselves with the new arguments */ |
| 2047 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | bdbbb7e | 2007-06-08 15:02:55 +0000 | [diff] [blame] | 2048 | execvp(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2049 | /* If they called chroot or otherwise made the binary no longer |
| 2050 | * executable, fall through */ |
| 2051 | } |
| 2052 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 2053 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2054 | |
| 2055 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2056 | execvp(argv[0], argv); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 2057 | bb_perror_msg("can't exec '%s'", argv[0]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 2058 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2061 | static int run_list(struct pipe *pi); |
| 2062 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2063 | /* Called after [v]fork() in run_pipe() |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2064 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2065 | static void pseudo_exec(nommu_save_t *nommu_save, struct command *command, char **argv_expanded) NORETURN; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2066 | static void pseudo_exec(nommu_save_t *nommu_save, struct command *command, char **argv_expanded) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2067 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2068 | if (command->argv) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2069 | pseudo_exec_argv(nommu_save, command->argv, command->assignment_cnt, argv_expanded); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2070 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2071 | if (command->group) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2072 | #if !BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 2073 | bb_error_msg_and_die("nested lists are not supported on NOMMU"); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2074 | #else |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 2075 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2076 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2077 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2078 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2079 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2080 | _exit(rcode); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2081 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2082 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2083 | |
| 2084 | /* Can happen. See what bash does with ">foo" by itself. */ |
| 2085 | debug_printf("trying to pseudo_exec null command\n"); |
| 2086 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2087 | } |
| 2088 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2089 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2090 | static const char *get_cmdtext(struct pipe *pi) |
| 2091 | { |
| 2092 | char **argv; |
| 2093 | char *p; |
| 2094 | int len; |
| 2095 | |
| 2096 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 2097 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2098 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2099 | if (pi->cmdtext) |
| 2100 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2101 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2102 | if (!argv || !argv[0]) { |
| 2103 | pi->cmdtext = xzalloc(1); |
| 2104 | return pi->cmdtext; |
| 2105 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2106 | |
| 2107 | len = 0; |
| 2108 | do len += strlen(*argv) + 1; while (*++argv); |
| 2109 | pi->cmdtext = p = xmalloc(len); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2110 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2111 | do { |
| 2112 | len = strlen(*argv); |
| 2113 | memcpy(p, *argv, len); |
| 2114 | p += len; |
| 2115 | *p++ = ' '; |
| 2116 | } while (*++argv); |
| 2117 | p[-1] = '\0'; |
| 2118 | return pi->cmdtext; |
| 2119 | } |
| 2120 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2121 | static void insert_bg_job(struct pipe *pi) |
| 2122 | { |
| 2123 | struct pipe *thejob; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2124 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2125 | |
| 2126 | /* Linear search for the ID of the job to use */ |
| 2127 | pi->jobid = 1; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2128 | for (thejob = G.job_list; thejob; thejob = thejob->next) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2129 | if (thejob->jobid >= pi->jobid) |
| 2130 | pi->jobid = thejob->jobid + 1; |
| 2131 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2132 | /* Add thejob to the list of running jobs */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2133 | if (!G.job_list) { |
| 2134 | thejob = G.job_list = xmalloc(sizeof(*thejob)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2135 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2136 | for (thejob = G.job_list; thejob->next; thejob = thejob->next) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2137 | continue; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2138 | thejob->next = xmalloc(sizeof(*thejob)); |
| 2139 | thejob = thejob->next; |
| 2140 | } |
| 2141 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2142 | /* Physically copy the struct job */ |
Eric Andersen | 0fcd447 | 2001-05-02 20:12:03 +0000 | [diff] [blame] | 2143 | memcpy(thejob, pi, sizeof(struct pipe)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2144 | thejob->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 2145 | /* We cannot copy entire pi->cmds[] vector! Double free()s will happen */ |
| 2146 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2147 | // TODO: do we really need to have so many fields which are just dead weight |
| 2148 | // at execution stage? |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2149 | thejob->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2150 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2151 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2152 | thejob->next = NULL; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2153 | thejob->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2154 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2155 | /* We don't wait for background thejobs to return -- append it |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2156 | to the list of backgrounded thejobs and leave it alone */ |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 2157 | if (G.interactive_fd) |
| 2158 | 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] | 2159 | G.last_bg_pid = thejob->cmds[0].pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2160 | G.last_jobid = thejob->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2163 | static void remove_bg_job(struct pipe *pi) |
| 2164 | { |
| 2165 | struct pipe *prev_pipe; |
| 2166 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2167 | if (pi == G.job_list) { |
| 2168 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2169 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2170 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2171 | while (prev_pipe->next != pi) |
| 2172 | prev_pipe = prev_pipe->next; |
| 2173 | prev_pipe->next = pi->next; |
| 2174 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2175 | if (G.job_list) |
| 2176 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 2177 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2178 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2179 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 2180 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2181 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2182 | static void delete_finished_bg_job(struct pipe *pi) |
| 2183 | { |
| 2184 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2185 | pi->stopped_cmds = 0; |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2186 | free_pipe(pi, 0); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2187 | free(pi); |
| 2188 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2189 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2190 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2191 | /* Check to see if any processes have exited -- if they |
| 2192 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2193 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2194 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2195 | int attributes; |
| 2196 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2197 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2198 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2199 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2200 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2201 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2202 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2203 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2204 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2205 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2206 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2207 | /* Do we do this right? |
| 2208 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2209 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2210 | * [3]+ Stopped sleep 20 | false |
| 2211 | * bash-3.00# echo $? |
| 2212 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 2213 | */ |
| 2214 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2215 | //FIXME: non-interactive bash does not continue even if all processes in fg pipe |
| 2216 | //are stopped. Testcase: "cat | cat" in a script (not on command line) |
| 2217 | // + killall -STOP cat |
| 2218 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2219 | wait_more: |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 2220 | // TODO: safe_waitpid? |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2221 | while ((childpid = waitpid(-1, &status, attributes)) > 0) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2222 | int i; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2223 | const int dead = WIFEXITED(status) || WIFSIGNALED(status); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2224 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2225 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 2226 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2227 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 2228 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 2229 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2230 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 2231 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 2232 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2233 | childpid, WEXITSTATUS(status)); |
| 2234 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2235 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2236 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2237 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 2238 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 2239 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2240 | continue; |
| 2241 | /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */ |
| 2242 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2243 | fg_pipe->cmds[i].pid = 0; |
| 2244 | fg_pipe->alive_cmds--; |
| 2245 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2246 | /* last process gives overall exitstatus */ |
| 2247 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2248 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2249 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2250 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2251 | fg_pipe->cmds[i].is_stopped = 1; |
| 2252 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2253 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2254 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 2255 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 2256 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2257 | /* All processes in fg pipe have exited/stopped */ |
| 2258 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2259 | if (fg_pipe->alive_cmds) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2260 | insert_bg_job(fg_pipe); |
| 2261 | #endif |
| 2262 | return rcode; |
| 2263 | } |
| 2264 | /* There are still running processes in the fg pipe */ |
| 2265 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2266 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2267 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2270 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2271 | /* We asked to wait for bg or orphaned children */ |
| 2272 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2273 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2274 | for (i = 0; i < pi->num_cmds; i++) { |
| 2275 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2276 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 2277 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2278 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2279 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 2280 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2281 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 2282 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2283 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2284 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2285 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2286 | pi->cmds[i].pid = 0; |
| 2287 | pi->alive_cmds--; |
| 2288 | if (!pi->alive_cmds) { |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 2289 | if (G.interactive_fd) |
| 2290 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 2291 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2292 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2293 | } |
| 2294 | } else { |
| 2295 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2296 | pi->cmds[i].is_stopped = 1; |
| 2297 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2298 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2299 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2300 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2301 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2302 | /* wait found no children or failed */ |
| 2303 | |
| 2304 | if (childpid && errno != ECHILD) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 2305 | bb_perror_msg("waitpid"); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2306 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2309 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 2310 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 2311 | { |
| 2312 | pid_t p; |
| 2313 | int rcode = checkjobs(fg_pipe); |
| 2314 | /* Job finished, move the shell to the foreground */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2315 | p = getpgid(0); /* pgid of our process */ |
| 2316 | debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2317 | tcsetpgrp(G.interactive_fd, p); |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 2318 | return rcode; |
| 2319 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2320 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 2321 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2322 | /* run_pipe() starts all the jobs, but doesn't wait for anything |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2323 | * to finish. See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2324 | * |
| 2325 | * return code is normally -1, when the caller has to wait for children |
| 2326 | * to finish to determine the exit status of the pipe. If the pipe |
| 2327 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2328 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2329 | * return value. |
| 2330 | * |
| 2331 | * The input of the pipe is always stdin, the output is always |
| 2332 | * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus, |
| 2333 | * because it tries to avoid running the command substitution in |
| 2334 | * subshell, when that is in fact necessary. The subshell process |
| 2335 | * now has its stdout directed to the input of the appropriate pipe, |
| 2336 | * so this routine is noticeably simpler. |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2337 | * |
| 2338 | * Returns -1 only if started some children. IOW: we have to |
| 2339 | * mask out retvals of builtins etc with 0xff! |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2340 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2341 | static int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2342 | { |
| 2343 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2344 | int nextin; |
| 2345 | int pipefds[2]; /* pipefds[0] is for reading */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2346 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2347 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2348 | char **argv; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 2349 | const struct built_in_command *x; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2350 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 2351 | /* it is not always needed, but we aim to smaller code */ |
| 2352 | int squirrel[] = { -1, -1, -1 }; |
| 2353 | int rcode; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2354 | const int single_and_fg = (pi->num_cmds == 1 && pi->followup != PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2355 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2356 | debug_printf_exec("run_pipe start: single_and_fg=%d\n", single_and_fg); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 2357 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2358 | #if ENABLE_HUSH_JOB |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 2359 | pi->pgrp = -1; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2360 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2361 | pi->alive_cmds = 1; |
| 2362 | pi->stopped_cmds = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2363 | |
| 2364 | /* Check if this is a simple builtin (not part of a pipe). |
| 2365 | * Builtins within pipes have to fork anyway, and are handled in |
| 2366 | * pseudo_exec. "echo foo | read bar" doesn't work on bash, either. |
| 2367 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2368 | command = &(pi->cmds[0]); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2369 | |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2370 | #if ENABLE_HUSH_FUNCTIONS |
| 2371 | if (single_and_fg && command->group && command->grp_type == GRP_FUNCTION) { |
| 2372 | /* We "execute" function definition */ |
| 2373 | bb_error_msg("here we ought to remember function definition, and go on"); |
| 2374 | return EXIT_SUCCESS; |
| 2375 | } |
| 2376 | #endif |
| 2377 | |
| 2378 | if (single_and_fg && command->group && command->grp_type == GRP_NORMAL) { |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 2379 | debug_printf("non-subshell grouping\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2380 | setup_redirects(command, squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2381 | debug_printf_exec(": run_list\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2382 | rcode = run_list(command->group) & 0xff; |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 2383 | restore_redirects(squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2384 | debug_printf_exec("run_pipe return %d\n", rcode); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2385 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2386 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2389 | argv = command->argv; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2390 | argv_expanded = NULL; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2391 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2392 | if (single_and_fg && argv != NULL) { |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2393 | char **new_env = NULL; |
| 2394 | char **old_env = NULL; |
| 2395 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2396 | i = command->assignment_cnt; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2397 | if (i != 0 && argv[i] == NULL) { |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2398 | /* assignments, but no command: set local environment */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2399 | for (i = 0; argv[i] != NULL; i++) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2400 | debug_printf("local environment set: %s\n", argv[i]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2401 | p = expand_string_to_string(argv[i]); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2402 | set_local_var(p, 0); |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2403 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2404 | return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */ |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2405 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2406 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2407 | /* Expand the rest into (possibly) many strings each */ |
| 2408 | argv_expanded = expand_strvec_to_strvec(argv + i); |
| 2409 | |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 2410 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2411 | if (strcmp(argv_expanded[0], x->cmd) != 0) |
| 2412 | continue; |
| 2413 | if (x->function == builtin_exec && argv_expanded[1] == NULL) { |
| 2414 | debug_printf("exec with redirects only\n"); |
| 2415 | setup_redirects(command, NULL); |
| 2416 | rcode = EXIT_SUCCESS; |
| 2417 | goto clean_up_and_ret1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2418 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2419 | debug_printf("builtin inline %s\n", argv_expanded[0]); |
| 2420 | /* XXX setup_redirects acts on file descriptors, not FILEs. |
| 2421 | * This is perfect for work that comes after exec(). |
| 2422 | * Is it really safe for inline use? Experimentally, |
| 2423 | * things seem to work with glibc. */ |
| 2424 | setup_redirects(command, squirrel); |
| 2425 | new_env = expand_assignments(argv, command->assignment_cnt); |
| 2426 | old_env = putenv_all_and_save_old(new_env); |
| 2427 | debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv_expanded[1]); |
| 2428 | rcode = x->function(argv_expanded) & 0xff; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2429 | #if ENABLE_FEATURE_SH_STANDALONE |
| 2430 | clean_up_and_ret: |
| 2431 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2432 | restore_redirects(squirrel); |
| 2433 | free_strings_and_unsetenv(new_env, 1); |
| 2434 | putenv_all(old_env); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2435 | free(old_env); /* not free_strings()! */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2436 | clean_up_and_ret1: |
| 2437 | free(argv_expanded); |
| 2438 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 2439 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 2440 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2441 | } |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 2442 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2443 | i = find_applet_by_name(argv_expanded[0]); |
| 2444 | if (i >= 0 && APPLET_IS_NOFORK(i)) { |
| 2445 | setup_redirects(command, squirrel); |
| 2446 | save_nofork_data(&G.nofork_save); |
| 2447 | new_env = expand_assignments(argv, command->assignment_cnt); |
| 2448 | old_env = putenv_all_and_save_old(new_env); |
| 2449 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]); |
| 2450 | rcode = run_nofork_applet_prime(&G.nofork_save, i, argv_expanded); |
| 2451 | goto clean_up_and_ret; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 2452 | } |
| 2453 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2456 | /* NB: argv_expanded may already be created, and that |
| 2457 | * might include `cmd` runs! Do not rerun it! We *must* |
| 2458 | * use argv_expanded if it's non-NULL */ |
| 2459 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2460 | /* Disable job control signals for shell (parent) and |
| 2461 | * for initial child code after fork */ |
| 2462 | set_jobctrl_sighandler(SIG_IGN); |
| 2463 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2464 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2465 | pi->alive_cmds = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2466 | nextin = 0; |
| 2467 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2468 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2469 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2470 | volatile nommu_save_t nommu_save; |
| 2471 | nommu_save.new_env = NULL; |
| 2472 | nommu_save.old_env = NULL; |
| 2473 | nommu_save.argv = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2474 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2475 | command = &(pi->cmds[i]); |
| 2476 | if (command->argv) { |
| 2477 | debug_printf_exec(": pipe member '%s' '%s'...\n", command->argv[0], command->argv[1]); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2478 | } else |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2479 | debug_printf_exec(": pipe member with no argv\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2480 | |
| 2481 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2482 | pipefds[0] = 0; |
| 2483 | pipefds[1] = 1; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2484 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2485 | xpipe(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2486 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2487 | command->pid = BB_MMU ? fork() : vfork(); |
| 2488 | if (!command->pid) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 2489 | if (ENABLE_HUSH_JOB) |
| 2490 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2491 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2492 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2493 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2494 | if (G.run_list_level == 1 && G.interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2495 | pid_t pgrp; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2496 | /* Don't do pgrp restore anymore on fatal signals */ |
| 2497 | set_fatal_sighandler(SIG_DFL); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2498 | pgrp = pi->pgrp; |
| 2499 | if (pgrp < 0) /* true for 1st process only */ |
| 2500 | pgrp = getpid(); |
| 2501 | if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2502 | /* We do it in *every* child, not just first, |
| 2503 | * to avoid races */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2504 | tcsetpgrp(G.interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2505 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2506 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2507 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2508 | xmove_fd(nextin, 0); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2509 | xmove_fd(pipefds[1], 1); /* write end */ |
| 2510 | if (pipefds[0] > 1) |
| 2511 | close(pipefds[0]); /* read end */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2512 | /* Like bash, explicit redirects override pipes, |
| 2513 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2514 | setup_redirects(command, NULL); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 2515 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2516 | /* Restore default handlers just prior to exec */ |
| 2517 | set_jobctrl_sighandler(SIG_DFL); |
| 2518 | set_misc_sighandler(SIG_DFL); |
| 2519 | signal(SIGCHLD, SIG_DFL); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2520 | /* Stores to nommu_save list of env vars putenv'ed |
| 2521 | * (NOMMU, on MMU we don't need that) */ |
| 2522 | /* cast away volatility... */ |
| 2523 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2524 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2525 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2526 | /* parent */ |
| 2527 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2528 | /* Clean up after vforked child */ |
| 2529 | free(nommu_save.argv); |
| 2530 | free_strings_and_unsetenv(nommu_save.new_env, 1); |
| 2531 | putenv_all(nommu_save.old_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2532 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2533 | free(argv_expanded); |
| 2534 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2535 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2536 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 2537 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2538 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2539 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2540 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2541 | /* Second and next children need to know pid of first one */ |
| 2542 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2543 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2544 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2545 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2546 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2547 | if (i) |
| 2548 | close(nextin); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2549 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2550 | close(pipefds[1]); /* write end */ |
| 2551 | /* Pass read (output) pipe end to next iteration */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2552 | nextin = pipefds[0]; |
| 2553 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2554 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2555 | if (!pi->alive_cmds) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2556 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 2557 | return 1; |
| 2558 | } |
| 2559 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2560 | 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] | 2561 | return -1; |
| 2562 | } |
| 2563 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 2564 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2565 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 2566 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2567 | static const char *const PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2568 | [PIPE_SEQ] = "SEQ", |
| 2569 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2570 | [PIPE_OR ] = "OR" , |
| 2571 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2572 | }; |
| 2573 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2574 | [RES_NONE ] = "NONE" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2575 | #if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2576 | [RES_IF ] = "IF" , |
| 2577 | [RES_THEN ] = "THEN" , |
| 2578 | [RES_ELIF ] = "ELIF" , |
| 2579 | [RES_ELSE ] = "ELSE" , |
| 2580 | [RES_FI ] = "FI" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2581 | #endif |
| 2582 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2583 | [RES_FOR ] = "FOR" , |
| 2584 | [RES_WHILE] = "WHILE", |
| 2585 | [RES_UNTIL] = "UNTIL", |
| 2586 | [RES_DO ] = "DO" , |
| 2587 | [RES_DONE ] = "DONE" , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 2588 | #endif |
| 2589 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2590 | [RES_IN ] = "IN" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2591 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2592 | #if ENABLE_HUSH_CASE |
| 2593 | [RES_CASE ] = "CASE" , |
| 2594 | [RES_MATCH] = "MATCH", |
| 2595 | [RES_CASEI] = "CASEI", |
| 2596 | [RES_ESAC ] = "ESAC" , |
| 2597 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2598 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2599 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2600 | }; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2601 | static const char *const GRPTYPE[] = { |
| 2602 | "()", |
| 2603 | "{}", |
| 2604 | #if ENABLE_HUSH_FUNCTIONS |
| 2605 | "func()", |
| 2606 | #endif |
| 2607 | }; |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2608 | |
| 2609 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2610 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2611 | pin = 0; |
| 2612 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2613 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 2614 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2615 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2616 | while (prn < pi->num_cmds) { |
| 2617 | struct command *command = &pi->cmds[prn]; |
| 2618 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2619 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2620 | fprintf(stderr, "%*s prog %d assignment_cnt:%d", lvl*2, "", prn, command->assignment_cnt); |
| 2621 | if (command->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2622 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2623 | GRPTYPE[command->grp_type], |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2624 | argv); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2625 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2626 | prn++; |
| 2627 | continue; |
| 2628 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2629 | if (argv) while (*argv) { |
| 2630 | fprintf(stderr, " '%s'", *argv); |
| 2631 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 2632 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2633 | fprintf(stderr, "\n"); |
| 2634 | prn++; |
| 2635 | } |
| 2636 | pi = pi->next; |
| 2637 | pin++; |
| 2638 | } |
| 2639 | } |
| 2640 | #endif |
| 2641 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2642 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 2643 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2644 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2645 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2646 | #if ENABLE_HUSH_CASE |
| 2647 | char *case_word = NULL; |
| 2648 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2649 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2650 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2651 | char *for_varname = NULL; |
| 2652 | char **for_lcur = NULL; |
| 2653 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2654 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2655 | smallint flag_skip = 1; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2656 | smalluint rcode = 0; /* probably just for compiler */ |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 2657 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2658 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2659 | #else |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2660 | enum { cond_code = 0, }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2661 | #endif |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2662 | /*enum reserved_style*/ smallint rword = RES_NONE; |
| 2663 | /*enum reserved_style*/ smallint skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 2664 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2665 | 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] | 2666 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2667 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2668 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2669 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 2670 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2671 | continue; |
| 2672 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2673 | if (cpipe->next == NULL) { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2674 | syntax("malformed for"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2675 | 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] | 2676 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2677 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2678 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2679 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2680 | continue; |
| 2681 | /* 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] | 2682 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 2683 | || 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] | 2684 | ) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2685 | syntax("malformed for"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2686 | 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] | 2687 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2688 | } |
| 2689 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2690 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2691 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2692 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 2693 | * in order to return, no direct "return" statements please. |
| 2694 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2695 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2696 | #if ENABLE_HUSH_JOB |
| 2697 | /* Example of nested list: "while true; do { sleep 1 | exit 2; } done". |
| 2698 | * We are saving state before entering outermost list ("while...done") |
| 2699 | * so that ctrl-Z will correctly background _entire_ outermost list, |
| 2700 | * not just a part of it (like "sleep 1 | exit 2") */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2701 | if (++G.run_list_level == 1 && G.interactive_fd) { |
| 2702 | if (sigsetjmp(G.toplevel_jb, 1)) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2703 | /* ctrl-Z forked and we are parent; or ctrl-C. |
| 2704 | * Sighandler has longjmped us here */ |
| 2705 | signal(SIGINT, SIG_IGN); |
| 2706 | signal(SIGTSTP, SIG_IGN); |
| 2707 | /* Restore level (we can be coming from deep inside |
| 2708 | * nested levels) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2709 | G.run_list_level = 1; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2710 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2711 | if (G.nofork_save.saved) { /* if save area is valid */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2712 | debug_printf_jobs("exiting nofork early\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2713 | restore_nofork_data(&G.nofork_save); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2714 | } |
| 2715 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2716 | if (G.ctrl_z_flag) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2717 | /* ctrl-Z has forked and stored pid of the child in pi->pid. |
| 2718 | * Remember this child as background job */ |
| 2719 | insert_bg_job(pi); |
| 2720 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2721 | /* ctrl-C. We just stop doing whatever we were doing */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 2722 | bb_putchar('\n'); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2723 | } |
Denis Vlasenko | ff29b4f | 2008-07-29 13:57:59 +0000 | [diff] [blame] | 2724 | USE_HUSH_LOOPS(loop_top = NULL;) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2725 | USE_HUSH_LOOPS(G.depth_of_loop = 0;) |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2726 | rcode = 0; |
| 2727 | goto ret; |
| 2728 | } |
| 2729 | /* ctrl-Z handler will store pid etc in pi */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2730 | G.toplevel_list = pi; |
| 2731 | G.ctrl_z_flag = 0; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2732 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2733 | G.nofork_save.saved = 0; /* in case we will run a nofork later */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2734 | #endif |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 2735 | signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2736 | signal(SIGINT, handler_ctrl_c); |
| 2737 | } |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2738 | #endif /* JOB */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2739 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2740 | /* Go through list of pipes, (maybe) executing them. */ |
| 2741 | for (; pi; pi = USE_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2742 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 2743 | IF_HAS_NO_KEYWORDS(rword = RES_NONE;) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2744 | debug_printf_exec(": rword=%d cond_code=%d skip_more=%d\n", |
| 2745 | rword, cond_code, skip_more_for_this_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2746 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 2747 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2748 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 2749 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2750 | /* start of a loop: remember where loop starts */ |
| 2751 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2752 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2753 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2754 | #endif |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2755 | if (rword == skip_more_for_this_rword && flag_skip) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2756 | if (pi->followup == PIPE_SEQ) |
| 2757 | flag_skip = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2758 | /* it is "<false> && CMD" or "<true> || CMD" |
| 2759 | * and we should not execute CMD */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2760 | continue; |
| 2761 | } |
| 2762 | flag_skip = 1; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2763 | skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2764 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2765 | if (cond_code) { |
| 2766 | if (rword == RES_THEN) { |
| 2767 | /* "if <false> THEN cmd": skip cmd */ |
| 2768 | continue; |
| 2769 | } |
| 2770 | } else { |
| 2771 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 2772 | /* "if <true> then ... ELSE/ELIF cmd": |
| 2773 | * skip cmd and all following ones */ |
| 2774 | break; |
| 2775 | } |
| 2776 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2777 | #endif |
| 2778 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2779 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2780 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2781 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2782 | |
| 2783 | static const char encoded_dollar_at[] ALIGN1 = { |
| 2784 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 2785 | }; /* encoded representation of "$@" */ |
| 2786 | static const char *const encoded_dollar_at_argv[] = { |
| 2787 | encoded_dollar_at, NULL |
| 2788 | }; /* argv list with one element: "$@" */ |
| 2789 | char **vals; |
| 2790 | |
| 2791 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2792 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2793 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2794 | if (!pi->next->cmds[0].argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2795 | break; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2796 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2797 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2798 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2799 | debug_print_strings("for_list made from", vals); |
| 2800 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2801 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2802 | debug_print_strings("for_list", for_list); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2803 | for_varname = pi->cmds[0].argv[0]; |
| 2804 | pi->cmds[0].argv[0] = NULL; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2805 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2806 | free(pi->cmds[0].argv[0]); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2807 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 2808 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2809 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2810 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2811 | for_lcur = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2812 | pi->cmds[0].argv[0] = for_varname; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2813 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2814 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2815 | /* insert next value from for_lcur */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2816 | //TODO: does it need escaping? |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2817 | pi->cmds[0].argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++); |
| 2818 | pi->cmds[0].assignment_cnt = 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2819 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2820 | if (rword == RES_IN) /* "for v IN list;..." - "in" has no cmds anyway */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2821 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2822 | if (rword == RES_DONE) { |
| 2823 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2824 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2825 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2826 | #if ENABLE_HUSH_CASE |
| 2827 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2828 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2829 | continue; |
| 2830 | } |
| 2831 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 2832 | char **argv; |
| 2833 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2834 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 2835 | break; |
| 2836 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2837 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 2838 | while (*argv) { |
| 2839 | char *pattern = expand_string_to_string(*argv); |
| 2840 | /* TODO: which FNM_xxx flags to use? */ |
| 2841 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 2842 | free(pattern); |
| 2843 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 2844 | free(case_word); /* make future "word)" stop */ |
| 2845 | case_word = NULL; |
| 2846 | break; |
| 2847 | } |
| 2848 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2849 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2850 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2851 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2852 | if (rword == RES_CASEI) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2853 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2854 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2855 | } |
| 2856 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2857 | if (pi->num_cmds == 0) |
Mike Frysinger | 8ec1c9d | 2009-03-29 00:45:26 +0000 | [diff] [blame] | 2858 | goto check_jobs_and_continue; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2859 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2860 | /* After analyzing all keywords and conditions, we decided |
| 2861 | * to execute this pipe. NB: has to do checkjobs(NULL) |
| 2862 | * after run_pipe() to collect any background children, |
| 2863 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2864 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2865 | { |
| 2866 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2867 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2868 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2869 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2870 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 2871 | if (r != -1) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2872 | /* we only ran a builtin: rcode is already known |
| 2873 | * and we don't need to wait for anything. */ |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2874 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2875 | /* was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2876 | if (G.flag_break_continue) { |
| 2877 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2878 | /* we might fall into outer *loop*, |
| 2879 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2880 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2881 | G.depth_break_continue--; |
| 2882 | if (G.depth_break_continue == 0) |
| 2883 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2884 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 2885 | } /* 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] | 2886 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2887 | goto check_jobs_and_break; |
| 2888 | /* "continue": simulate end of loop */ |
| 2889 | rword = RES_DONE; |
| 2890 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2891 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2892 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2893 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2894 | /* what does bash do with attempts to background builtins? */ |
| 2895 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2896 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 2897 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2898 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2899 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2900 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2901 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2902 | rcode = 0; /* EXIT_SUCCESS */ |
| 2903 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2904 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2905 | if (G.run_list_level == 1 && G.interactive_fd) { |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2906 | /* waits for completion, then fg's main shell */ |
| 2907 | rcode = checkjobs_and_fg_shell(pi); |
| 2908 | debug_printf_exec(": checkjobs_and_fg_shell returned %d\n", rcode); |
| 2909 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2910 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2911 | { /* this one just waits for completion */ |
| 2912 | rcode = checkjobs(pi); |
| 2913 | debug_printf_exec(": checkjobs returned %d\n", rcode); |
| 2914 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2915 | } |
| 2916 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2917 | debug_printf_exec(": setting last_return_code=%d\n", rcode); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2918 | G.last_return_code = rcode; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2919 | |
| 2920 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2921 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2922 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2923 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2924 | #endif |
| 2925 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2926 | if (rword == RES_WHILE) { |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 2927 | if (rcode) { |
| 2928 | rcode = 0; /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2929 | goto check_jobs_and_break; |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 2930 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2931 | } |
| 2932 | if (rword == RES_UNTIL) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2933 | if (!rcode) { |
| 2934 | check_jobs_and_break: |
| 2935 | checkjobs(NULL); |
| 2936 | break; |
| 2937 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2938 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2939 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2940 | if ((rcode == 0 && pi->followup == PIPE_OR) |
| 2941 | || (rcode != 0 && pi->followup == PIPE_AND) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2942 | ) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2943 | skip_more_for_this_rword = rword; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2944 | } |
Mike Frysinger | 8ec1c9d | 2009-03-29 00:45:26 +0000 | [diff] [blame] | 2945 | |
| 2946 | check_jobs_and_continue: |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 2947 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2948 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2949 | |
| 2950 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2951 | if (G.ctrl_z_flag) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2952 | /* ctrl-Z forked somewhere in the past, we are the child, |
| 2953 | * and now we completed running the list. Exit. */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2954 | //TODO: _exit? |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2955 | exit(rcode); |
| 2956 | } |
| 2957 | ret: |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2958 | if (!--G.run_list_level && G.interactive_fd) { |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 2959 | signal(SIGTSTP, SIG_IGN); |
| 2960 | signal(SIGINT, SIG_IGN); |
| 2961 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2962 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2963 | 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] | 2964 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2965 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2966 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2967 | free(for_list); |
| 2968 | #endif |
| 2969 | #if ENABLE_HUSH_CASE |
| 2970 | free(case_word); |
| 2971 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2972 | return rcode; |
| 2973 | } |
| 2974 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2975 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2976 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2977 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2978 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2979 | debug_printf_exec("run_and_free_list entered\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2980 | if (!G.fake_mode) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2981 | debug_printf_exec(": run_list with %d members\n", pi->num_cmds); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2982 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2983 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2984 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2985 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2986 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2987 | free_pipe_list(pi, /* indent: */ 0); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2988 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2989 | return rcode; |
| 2990 | } |
| 2991 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2992 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2993 | /* Peek ahead in the in_str to find out if we have a "&n" construct, |
| 2994 | * as in "2>&1", that represents duplicating a file descriptor. |
| 2995 | * Return either -2 (syntax error), -1 (no &), or the number found. |
| 2996 | */ |
| 2997 | static int redirect_dup_num(struct in_str *input) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2998 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2999 | int ch, d = 0, ok = 0; |
| 3000 | ch = i_peek(input); |
| 3001 | if (ch != '&') return -1; |
| 3002 | |
| 3003 | i_getch(input); /* get the & */ |
| 3004 | ch = i_peek(input); |
| 3005 | if (ch == '-') { |
| 3006 | i_getch(input); |
| 3007 | return -3; /* "-" represents "close me" */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3008 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3009 | while (isdigit(ch)) { |
| 3010 | d = d*10 + (ch-'0'); |
| 3011 | ok = 1; |
| 3012 | i_getch(input); |
| 3013 | ch = i_peek(input); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3014 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3015 | if (ok) return d; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3016 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3017 | bb_error_msg("ambiguous redirect"); |
| 3018 | return -2; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3021 | /* 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] | 3022 | * for file descriptor duplication, e.g., "2>&1". |
| 3023 | * Return code is 0 normally, 1 if a syntax error is detected in src. |
| 3024 | * Resource errors (in xmalloc) cause the process to exit */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3025 | static int setup_redirect(struct parse_context *ctx, int fd, redir_type style, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3026 | struct in_str *input) |
| 3027 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3028 | struct command *command = ctx->command; |
| 3029 | struct redir_struct *redir = command->redirects; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3030 | struct redir_struct *last_redir = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3031 | |
| 3032 | /* Create a new redir_struct and drop it onto the end of the linked list */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3033 | while (redir) { |
| 3034 | last_redir = redir; |
| 3035 | redir = redir->next; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3036 | } |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3037 | redir = xzalloc(sizeof(struct redir_struct)); |
| 3038 | /* redir->next = NULL; */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3039 | /* redir->rd_filename = NULL; */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3040 | if (last_redir) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3041 | last_redir->next = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3042 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3043 | command->redirects = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3046 | redir->rd_type = style; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3047 | redir->fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3048 | |
| 3049 | debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip); |
| 3050 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3051 | /* Check for a '2>&1' type redirect */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3052 | redir->dup = redirect_dup_num(input); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3053 | if (redir->dup == -2) |
| 3054 | return 1; /* syntax error */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3055 | if (redir->dup != -1) { |
| 3056 | /* Erik had a check here that the file descriptor in question |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3057 | * is legit; I postpone that to "run time" |
| 3058 | * A "-" representation of "close me" shows up as a -3 here */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3059 | debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup); |
| 3060 | } else { |
| 3061 | /* We do _not_ try to open the file that src points to, |
| 3062 | * since we need to return and let src be expanded first. |
| 3063 | * Set ctx->pending_redirect, so we know what to do at the |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 3064 | * end of the next parsed word. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3065 | ctx->pending_redirect = redir; |
| 3066 | } |
| 3067 | return 0; |
| 3068 | } |
| 3069 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3070 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3071 | static struct pipe *new_pipe(void) |
| 3072 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3073 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3074 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3075 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3076 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3077 | return pi; |
| 3078 | } |
| 3079 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3080 | /* Command (member of a pipe) is complete. The only possible error here |
| 3081 | * is out of memory, in which case xmalloc exits. */ |
| 3082 | static int done_command(struct parse_context *ctx) |
| 3083 | { |
| 3084 | /* The command is really already in the pipe structure, so |
| 3085 | * advance the pipe counter and make a new, null command. */ |
| 3086 | struct pipe *pi = ctx->pipe; |
| 3087 | struct command *command = ctx->command; |
| 3088 | |
| 3089 | if (command) { |
| 3090 | if (command->group == NULL |
| 3091 | && command->argv == NULL |
| 3092 | && command->redirects == NULL |
| 3093 | ) { |
| 3094 | debug_printf_parse("done_command: skipping null cmd, num_cmds=%d\n", pi->num_cmds); |
| 3095 | return pi->num_cmds; |
| 3096 | } |
| 3097 | pi->num_cmds++; |
| 3098 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
| 3099 | } else { |
| 3100 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3101 | } |
| 3102 | |
| 3103 | /* Only real trickiness here is that the uncommitted |
| 3104 | * command structure is not counted in pi->num_cmds. */ |
| 3105 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
| 3106 | command = &pi->cmds[pi->num_cmds]; |
| 3107 | memset(command, 0, sizeof(*command)); |
| 3108 | |
| 3109 | ctx->command = command; |
| 3110 | /* but ctx->pipe and ctx->list_head remain unchanged */ |
| 3111 | |
| 3112 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3113 | } |
| 3114 | |
| 3115 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3116 | { |
| 3117 | int not_null; |
| 3118 | |
| 3119 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3120 | /* Close previous command */ |
| 3121 | not_null = done_command(ctx); |
| 3122 | ctx->pipe->followup = type; |
| 3123 | IF_HAS_KEYWORDS(ctx->pipe->pi_inverted = ctx->ctx_inverted;) |
| 3124 | IF_HAS_KEYWORDS(ctx->ctx_inverted = 0;) |
| 3125 | IF_HAS_KEYWORDS(ctx->pipe->res_word = ctx->ctx_res_w;) |
| 3126 | |
| 3127 | /* Without this check, even just <enter> on command line generates |
| 3128 | * tree of three NOPs (!). Which is harmless but annoying. |
| 3129 | * IOW: it is safe to do it unconditionally. |
| 3130 | * RES_NONE case is for "for a in; do ..." (empty IN set) |
| 3131 | * to work, possibly other cases too. */ |
| 3132 | if (not_null IF_HAS_KEYWORDS(|| ctx->ctx_res_w != RES_NONE)) { |
| 3133 | struct pipe *new_p; |
| 3134 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3135 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3136 | not_null, ctx->ctx_res_w); |
| 3137 | new_p = new_pipe(); |
| 3138 | ctx->pipe->next = new_p; |
| 3139 | ctx->pipe = new_p; |
| 3140 | ctx->command = NULL; /* needed! */ |
| 3141 | /* RES_THEN, RES_DO etc are "sticky" - |
| 3142 | * they remain set for commands inside if/while. |
| 3143 | * This is used to control execution. |
| 3144 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3145 | * cases where variable or value happens to match a keyword): |
| 3146 | */ |
| 3147 | #if ENABLE_HUSH_LOOPS |
| 3148 | if (ctx->ctx_res_w == RES_FOR |
| 3149 | || ctx->ctx_res_w == RES_IN) |
| 3150 | ctx->ctx_res_w = RES_NONE; |
| 3151 | #endif |
| 3152 | #if ENABLE_HUSH_CASE |
| 3153 | if (ctx->ctx_res_w == RES_MATCH) |
| 3154 | ctx->ctx_res_w = RES_CASEI; |
| 3155 | #endif |
| 3156 | /* Create the memory for command, roughly: |
| 3157 | * ctx->pipe->cmds = new struct command; |
| 3158 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3159 | */ |
| 3160 | done_command(ctx); |
| 3161 | } |
| 3162 | debug_printf_parse("done_pipe return\n"); |
| 3163 | } |
| 3164 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3165 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3166 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3167 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3168 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3169 | /* Create the memory for command, roughly: |
| 3170 | * ctx->pipe->cmds = new struct command; |
| 3171 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3172 | */ |
| 3173 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3176 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3177 | /* If a reserved word is found and processed, parse context is modified |
| 3178 | * and 1 is returned. |
| 3179 | * Handles if, then, elif, else, fi, for, while, until, do, done. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3180 | * case, function, and select are obnoxious, save those for later. |
| 3181 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3182 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3183 | struct reserved_combo { |
| 3184 | char literal[6]; |
| 3185 | unsigned char res; |
| 3186 | unsigned char assignment_flag; |
| 3187 | int flag; |
| 3188 | }; |
| 3189 | enum { |
| 3190 | FLAG_END = (1 << RES_NONE ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3191 | #if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3192 | FLAG_IF = (1 << RES_IF ), |
| 3193 | FLAG_THEN = (1 << RES_THEN ), |
| 3194 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3195 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3196 | FLAG_FI = (1 << RES_FI ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3197 | #endif |
| 3198 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3199 | FLAG_FOR = (1 << RES_FOR ), |
| 3200 | FLAG_WHILE = (1 << RES_WHILE), |
| 3201 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3202 | FLAG_DO = (1 << RES_DO ), |
| 3203 | FLAG_DONE = (1 << RES_DONE ), |
| 3204 | FLAG_IN = (1 << RES_IN ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3205 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3206 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3207 | FLAG_MATCH = (1 << RES_MATCH), |
| 3208 | FLAG_ESAC = (1 << RES_ESAC ), |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3209 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3210 | FLAG_START = (1 << RES_XXXX ), |
| 3211 | }; |
| 3212 | |
| 3213 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3214 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3215 | /* Mostly a list of accepted follow-up reserved words. |
| 3216 | * FLAG_END means we are done with the sequence, and are ready |
| 3217 | * to turn the compound list into a command. |
| 3218 | * FLAG_START means the word must start a new compound list. |
| 3219 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3220 | static const struct reserved_combo reserved_list[] = { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3221 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3222 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3223 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 3224 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3225 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 3226 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 3227 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3228 | #endif |
| 3229 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3230 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3231 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 3232 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 3233 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3234 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 3235 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3236 | #endif |
| 3237 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3238 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3239 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3240 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3241 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3242 | const struct reserved_combo *r; |
| 3243 | |
| 3244 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 3245 | if (strcmp(word->data, r->literal) == 0) |
| 3246 | return r; |
| 3247 | } |
| 3248 | return NULL; |
| 3249 | } |
| 3250 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 3251 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3252 | #if ENABLE_HUSH_CASE |
| 3253 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3254 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3255 | }; |
| 3256 | #endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3257 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3258 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3259 | r = match_reserved_word(word); |
| 3260 | if (!r) |
| 3261 | return 0; |
| 3262 | |
| 3263 | 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] | 3264 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3265 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE) |
| 3266 | /* "case word IN ..." - IN part starts first match part */ |
| 3267 | r = &reserved_match; |
| 3268 | else |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3269 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3270 | if (r->flag == 0) { /* '!' */ |
| 3271 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3272 | syntax(NULL); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3273 | IF_HAS_KEYWORDS(ctx->ctx_res_w = RES_SNTX;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3274 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3275 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3276 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3277 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3278 | if (r->flag & FLAG_START) { |
| 3279 | struct parse_context *new; |
| 3280 | debug_printf("push stack\n"); |
| 3281 | new = xmalloc(sizeof(*new)); |
| 3282 | *new = *ctx; /* physical copy */ |
| 3283 | initialize_context(ctx); |
| 3284 | ctx->stack = new; |
| 3285 | } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) { |
| 3286 | syntax(NULL); |
| 3287 | ctx->ctx_res_w = RES_SNTX; |
| 3288 | return 1; |
| 3289 | } |
| 3290 | ctx->ctx_res_w = r->res; |
| 3291 | ctx->old_flag = r->flag; |
| 3292 | if (ctx->old_flag & FLAG_END) { |
| 3293 | struct parse_context *old; |
| 3294 | debug_printf("pop stack\n"); |
| 3295 | done_pipe(ctx, PIPE_SEQ); |
| 3296 | old = ctx->stack; |
| 3297 | old->command->group = ctx->list_head; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3298 | old->command->grp_type = GRP_NORMAL; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3299 | *ctx = *old; /* physical copy */ |
| 3300 | free(old); |
| 3301 | } |
| 3302 | word->o_assignment = r->assignment_flag; |
| 3303 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3304 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3305 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3306 | |
Denis Vlasenko | ddc8ae3 | 2008-10-14 12:50:34 +0000 | [diff] [blame] | 3307 | //TODO: many, many callers don't check error from done_word() |
| 3308 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3309 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3310 | * Normal return is 0. Syntax errors return 1. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3311 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3312 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3313 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3314 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3315 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3316 | if (word->length == 0 && word->nonnull == 0) { |
| 3317 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3318 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3319 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3320 | /* If this word wasn't an assignment, next ones definitely |
| 3321 | * can't be assignments. Even if they look like ones. */ |
| 3322 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3323 | && word->o_assignment != WORD_IS_KEYWORD |
| 3324 | ) { |
| 3325 | word->o_assignment = NOT_ASSIGNMENT; |
| 3326 | } else { |
| 3327 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3328 | command->assignment_cnt++; |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3329 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3330 | } |
| 3331 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3332 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3333 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3334 | * only if run as "bash", not "sh" */ |
| 3335 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3336 | word->o_assignment = NOT_ASSIGNMENT; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3337 | debug_printf("word stored in rd_filename: '%s'\n", word->data); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3338 | } else { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3339 | /* "{ echo foo; } echo bar" - bad */ |
| 3340 | /* NB: bash allows e.g. "if true; then { echo foo; } fi". TODO? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3341 | if (command->group) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3342 | syntax(NULL); |
| 3343 | debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n"); |
| 3344 | return 1; |
| 3345 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3346 | #if HAS_KEYWORDS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3347 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3348 | if (ctx->ctx_dsemicolon |
| 3349 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3350 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3351 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3352 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3353 | ctx->ctx_dsemicolon = 0; |
| 3354 | } else |
| 3355 | #endif |
| 3356 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3357 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3358 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3359 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3360 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3361 | #endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3362 | ) { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3363 | debug_printf_parse(": checking '%s' for reserved-ness\n", word->data); |
| 3364 | if (reserved_word(word, ctx)) { |
| 3365 | o_reset(word); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3366 | debug_printf_parse("done_word return %d\n", (ctx->ctx_res_w == RES_SNTX)); |
| 3367 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3368 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3369 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3370 | #endif |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3371 | 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] | 3372 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3373 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3374 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3375 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3376 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3377 | char *p = word->data; |
| 3378 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3379 | && (p[1] & 0x7f) == '@' |
| 3380 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3381 | ) { |
| 3382 | p += 3; |
| 3383 | } |
| 3384 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3385 | /* saw no "$@", or not only "$@" but some |
| 3386 | * real text is there too */ |
| 3387 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3388 | * e.g. "", $empty"" etc to not disappear */ |
| 3389 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3390 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3391 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3392 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3393 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3394 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3395 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3396 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3397 | o_reset(word); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3398 | ctx->pending_redirect = NULL; |
| 3399 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3400 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3401 | /* Force FOR to have just one word (variable name) */ |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3402 | /* NB: basically, this makes hush see "for v in ..." syntax as if |
| 3403 | * as it is "for v; in ...". FOR and IN become two pipe structs |
| 3404 | * in parse tree. */ |
| 3405 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3406 | //TODO: check that command->argv[0] is a valid variable name! |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3407 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3408 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3409 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3410 | #if ENABLE_HUSH_CASE |
| 3411 | /* Force CASE to have just one word */ |
| 3412 | if (ctx->ctx_res_w == RES_CASE) { |
| 3413 | done_pipe(ctx, PIPE_SEQ); |
| 3414 | } |
| 3415 | #endif |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3416 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3417 | return 0; |
| 3418 | } |
| 3419 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3420 | /* If a redirect is immediately preceded by a number, that number is |
| 3421 | * supposed to tell which file descriptor to redirect. This routine |
| 3422 | * looks for such preceding numbers. In an ideal world this routine |
| 3423 | * needs to handle all the following classes of redirects... |
| 3424 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3425 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3426 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3427 | * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo |
| 3428 | * A -1 output from this program means no valid number was found, so the |
| 3429 | * caller should use the appropriate default for this redirection. |
| 3430 | */ |
| 3431 | static int redirect_opt_num(o_string *o) |
| 3432 | { |
| 3433 | int num; |
| 3434 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3435 | if (o->length == 0) |
| 3436 | return -1; |
| 3437 | for (num = 0; num < o->length; num++) { |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3438 | if (!isdigit(o->data[num])) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3439 | return -1; |
| 3440 | } |
| 3441 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3442 | num = atoi(o->data); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3443 | o_reset(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3444 | return num; |
| 3445 | } |
| 3446 | |
Mike Frysinger | ddbee97 | 2009-03-22 22:48:41 +0000 | [diff] [blame] | 3447 | static int parse_stream(o_string *dest, struct parse_context *ctx, |
| 3448 | struct in_str *input0, const char *end_trigger); |
| 3449 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3450 | #if ENABLE_HUSH_TICK |
"Vladimir N. Oleynik" | 19c3701 | 2005-09-22 14:33:15 +0000 | [diff] [blame] | 3451 | static FILE *generate_stream_from_list(struct pipe *head) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3452 | { |
| 3453 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3454 | int pid, channel[2]; |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3455 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 3456 | xpipe(channel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3457 | /* *** NOMMU WARNING *** */ |
| 3458 | /* By using vfork here, we suspend parent till child exits or execs. |
| 3459 | * If child will not do it before it fills the pipe, it can block forever |
| 3460 | * in write(STDOUT_FILENO), and parent (shell) will be also stuck. |
Denis Vlasenko | 3fe4f98 | 2008-06-09 16:02:39 +0000 | [diff] [blame] | 3461 | * Try this script: |
| 3462 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >TESTFILE |
| 3463 | * huge=`cat TESTFILE` # will block here forever |
| 3464 | * echo OK |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3465 | */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 3466 | pid = BB_MMU ? fork() : vfork(); |
| 3467 | if (pid < 0) |
| 3468 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3469 | if (pid == 0) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 3470 | if (ENABLE_HUSH_JOB) |
| 3471 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 3472 | close(channel[0]); /* NB: close _first_, then move fd! */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3473 | xmove_fd(channel[1], 1); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3474 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3475 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3476 | G.run_list_level = 1; |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3477 | #endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3478 | /* Process substitution is not considered to be usual |
| 3479 | * 'command execution'. |
| 3480 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. */ |
| 3481 | /* Not needed, we are relying on it being disabled |
| 3482 | * everywhere outside actual command execution. */ |
| 3483 | /*set_jobctrl_sighandler(SIG_IGN);*/ |
| 3484 | set_misc_sighandler(SIG_DFL); |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3485 | /* Freeing 'head' here would break NOMMU. */ |
| 3486 | _exit(run_list(head)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3487 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3488 | close(channel[1]); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3489 | pf = fdopen(channel[0], "r"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3490 | return pf; |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3491 | /* 'head' is freed by the caller */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3494 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3495 | static int process_command_subs(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3496 | struct in_str *input, |
| 3497 | const char *subst_end) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3498 | { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3499 | int retcode, ch, eol_cnt; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3500 | o_string result = NULL_O_STRING; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3501 | struct parse_context inner; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3502 | FILE *p; |
| 3503 | struct in_str pipe_str; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3504 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3505 | initialize_context(&inner); |
| 3506 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3507 | /* Recursion to generate command */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3508 | retcode = parse_stream(&result, &inner, input, subst_end); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3509 | if (retcode != 0) |
| 3510 | return retcode; /* syntax error or EOF */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3511 | done_word(&result, &inner); |
| 3512 | done_pipe(&inner, PIPE_SEQ); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3513 | o_free(&result); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3514 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3515 | p = generate_stream_from_list(inner.list_head); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3516 | if (p == NULL) |
| 3517 | return 1; |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 3518 | close_on_exec_on(fileno(p)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3519 | setup_file_in_str(&pipe_str, p); |
| 3520 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3521 | /* Now send results of command back into original context */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3522 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3523 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3524 | if (ch == '\n') { |
| 3525 | eol_cnt++; |
| 3526 | continue; |
| 3527 | } |
| 3528 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3529 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3530 | eol_cnt--; |
| 3531 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3532 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | debug_printf("done reading from pipe, pclose()ing\n"); |
| 3536 | /* This is the step that wait()s for the child. Should be pretty |
| 3537 | * safe, since we just read an EOF from its stdout. We could try |
Denis Vlasenko | 262d765 | 2007-05-20 21:52:49 +0000 | [diff] [blame] | 3538 | * to do better, by using wait(), and keeping track of background jobs |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3539 | * at the same time. That would be a lot of work, and contrary |
| 3540 | * to the KISS philosophy of this program. */ |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3541 | retcode = fclose(p); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3542 | free_pipe_list(inner.list_head, /* indent: */ 0); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3543 | debug_printf("closed FILE from child, retcode=%d\n", retcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3544 | return retcode; |
| 3545 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3546 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3547 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3548 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3549 | struct in_str *input, int ch) |
| 3550 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3551 | /* dest contains characters seen prior to ( or {. |
| 3552 | * Typically it's empty, but for functions defs, |
| 3553 | * it contains function name (without '()'). */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3554 | int rcode; |
| 3555 | const char *endch = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3556 | struct parse_context sub; |
| 3557 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3558 | |
| 3559 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3560 | #if ENABLE_HUSH_FUNCTIONS |
| 3561 | if (ch == 'F') { /* function definition? */ |
| 3562 | bb_error_msg("aha '%s' is a function, parsing it...", dest->data); |
| 3563 | //command->fname = dest->data; |
| 3564 | command->grp_type = GRP_FUNCTION; |
| 3565 | //TODO: review every o_reset() location... do they handle all o_string fields correctly? |
| 3566 | memset(dest, 0, sizeof(*dest)); |
| 3567 | } |
| 3568 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3569 | if (command->argv /* word [word](... */ |
| 3570 | || dest->length /* word(... */ |
| 3571 | || dest->nonnull /* ""(... */ |
| 3572 | ) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3573 | syntax(NULL); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3574 | debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n"); |
| 3575 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3576 | } |
| 3577 | initialize_context(&sub); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3578 | endch = "}"; |
| 3579 | if (ch == '(') { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3580 | endch = ")"; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3581 | command->grp_type = GRP_SUBSHELL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3582 | } |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3583 | rcode = parse_stream(dest, &sub, input, endch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3584 | if (rcode == 0) { |
| 3585 | done_word(dest, &sub); /* finish off the final word in the subcontext */ |
| 3586 | done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3587 | command->group = sub.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3588 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3589 | debug_printf_parse("parse_group return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3590 | return rcode; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3591 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3594 | #if ENABLE_HUSH_TICK |
| 3595 | /* Subroutines for copying $(...) and `...` things */ |
| 3596 | static void add_till_backquote(o_string *dest, struct in_str *input); |
| 3597 | /* '...' */ |
| 3598 | static void add_till_single_quote(o_string *dest, struct in_str *input) |
| 3599 | { |
| 3600 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3601 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3602 | if (ch == EOF) |
| 3603 | break; |
| 3604 | if (ch == '\'') |
| 3605 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3606 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3607 | } |
| 3608 | } |
| 3609 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
| 3610 | static void add_till_double_quote(o_string *dest, struct in_str *input) |
| 3611 | { |
| 3612 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3613 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3614 | if (ch == '"') |
| 3615 | break; |
| 3616 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3617 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3618 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3619 | } |
| 3620 | if (ch == EOF) |
| 3621 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3622 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3623 | if (ch == '`') { |
| 3624 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3625 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3626 | continue; |
| 3627 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3628 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3629 | } |
| 3630 | } |
| 3631 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 3632 | * \` quoting. |
| 3633 | * "Within the backquoted style of command substitution, backslash |
| 3634 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 3635 | * The search for the matching backquote shall be satisfied by the first |
| 3636 | * backquote found without a preceding backslash; during this search, |
| 3637 | * if a non-escaped backquote is encountered within a shell comment, |
| 3638 | * a here-document, an embedded command substitution of the $(command) |
| 3639 | * form, or a quoted string, undefined results occur. A single-quoted |
| 3640 | * or double-quoted string that begins, but does not end, within the |
| 3641 | * "`...`" sequence produces undefined results." |
| 3642 | * Example Output |
| 3643 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 3644 | */ |
| 3645 | static void add_till_backquote(o_string *dest, struct in_str *input) |
| 3646 | { |
| 3647 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3648 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3649 | if (ch == '`') |
| 3650 | break; |
| 3651 | if (ch == '\\') { /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3652 | int ch2 = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3653 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3654 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3655 | ch = ch2; |
| 3656 | } |
| 3657 | if (ch == EOF) |
| 3658 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3659 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3660 | } |
| 3661 | } |
| 3662 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 3663 | * quoting and nested ()s. |
| 3664 | * "With the $(command) style of command substitution, all characters |
| 3665 | * following the open parenthesis to the matching closing parenthesis |
| 3666 | * constitute the command. Any valid shell script can be used for command, |
| 3667 | * except a script consisting solely of redirections which produces |
| 3668 | * unspecified results." |
| 3669 | * Example Output |
| 3670 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 3671 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 3672 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 3673 | */ |
| 3674 | static void add_till_closing_curly_brace(o_string *dest, struct in_str *input) |
| 3675 | { |
| 3676 | int count = 0; |
| 3677 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3678 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3679 | if (ch == EOF) |
| 3680 | break; |
| 3681 | if (ch == '(') |
| 3682 | count++; |
| 3683 | if (ch == ')') |
| 3684 | if (--count < 0) |
| 3685 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3686 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3687 | if (ch == '\'') { |
| 3688 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3689 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3690 | continue; |
| 3691 | } |
| 3692 | if (ch == '"') { |
| 3693 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3694 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3695 | continue; |
| 3696 | } |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3697 | if (ch == '\\') { /* \x. Copy verbatim. Important for \(, \) */ |
| 3698 | ch = i_getch(input); |
| 3699 | if (ch == EOF) |
| 3700 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3701 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3702 | continue; |
| 3703 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3704 | } |
| 3705 | } |
| 3706 | #endif /* ENABLE_HUSH_TICK */ |
| 3707 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3708 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3709 | static int handle_dollar(o_string *dest, struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3710 | { |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3711 | int expansion; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3712 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3713 | unsigned char quote_mask = dest->o_quote ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3714 | |
| 3715 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3716 | if (isalpha(ch)) { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3717 | i_getch(input); |
| 3718 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3719 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3720 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3721 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3722 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3723 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3724 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3725 | if (!isalnum(ch) && ch != '_') |
| 3726 | break; |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3727 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3728 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3729 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3730 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3731 | make_one_char_var: |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3732 | i_getch(input); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3733 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3734 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3735 | o_addchr(dest, ch | quote_mask); |
| 3736 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3737 | } else switch (ch) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3738 | case '$': /* pid */ |
| 3739 | case '!': /* last bg pid */ |
| 3740 | case '?': /* last exit code */ |
| 3741 | case '#': /* number of args */ |
| 3742 | case '*': /* args */ |
| 3743 | case '@': /* args */ |
| 3744 | goto make_one_char_var; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3745 | case '{': { |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 3746 | bool first_char, all_digits; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3747 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3748 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3749 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3750 | /* XXX maybe someone will try to escape the '}' */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3751 | expansion = 0; |
| 3752 | first_char = true; |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 3753 | all_digits = false; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3754 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3755 | ch = i_getch(input); |
Denis Vlasenko | b055001 | 2007-05-24 12:18:16 +0000 | [diff] [blame] | 3756 | if (ch == '}') |
| 3757 | break; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3758 | |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 3759 | if (first_char) { |
| 3760 | if (ch == '#') |
| 3761 | /* ${#var}: length of var contents */ |
| 3762 | goto char_ok; |
| 3763 | else if (isdigit(ch)) { |
| 3764 | all_digits = true; |
| 3765 | goto char_ok; |
| 3766 | } |
| 3767 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3768 | |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 3769 | if (expansion < 2 && |
| 3770 | ((all_digits && !isdigit(ch)) || |
| 3771 | (!all_digits && !isalnum(ch) && ch != '_'))) |
| 3772 | { |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3773 | /* handle parameter expansions |
| 3774 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 3775 | */ |
| 3776 | if (first_char) |
| 3777 | goto case_default; |
| 3778 | switch (ch) { |
| 3779 | case ':': /* null modifier */ |
| 3780 | if (expansion == 0) { |
| 3781 | debug_printf_parse(": null modifier\n"); |
| 3782 | ++expansion; |
| 3783 | break; |
| 3784 | } |
| 3785 | goto case_default; |
| 3786 | |
| 3787 | #if 0 /* not implemented yet :( */ |
| 3788 | case '#': /* remove prefix */ |
| 3789 | case '%': /* remove suffix */ |
| 3790 | if (expansion == 0) { |
| 3791 | debug_printf_parse(": remove suffix/prefix\n"); |
| 3792 | expansion = 2; |
| 3793 | break; |
| 3794 | } |
| 3795 | goto case_default; |
| 3796 | #endif |
| 3797 | |
| 3798 | case '-': /* default value */ |
| 3799 | case '=': /* assign default */ |
| 3800 | case '+': /* alternative */ |
| 3801 | case '?': /* error indicate */ |
| 3802 | debug_printf_parse(": parameter expansion\n"); |
| 3803 | expansion = 2; |
| 3804 | break; |
| 3805 | |
| 3806 | default: |
| 3807 | case_default: |
| 3808 | syntax("unterminated ${name}"); |
| 3809 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 3810 | return 1; |
| 3811 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3812 | } |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 3813 | |
| 3814 | char_ok: |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3815 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3816 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3817 | quote_mask = 0; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3818 | first_char = false; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3819 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3820 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3821 | break; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3822 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3823 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3824 | case '(': { |
| 3825 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3826 | i_getch(input); |
| 3827 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3828 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3829 | add_till_closing_curly_brace(dest, input); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3830 | //debug_printf_subst("SUBST RES2 '%s'\n", dest->data + pos); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3831 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3832 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3833 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3834 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3835 | case '_': |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3836 | i_getch(input); |
| 3837 | ch = i_peek(input); |
| 3838 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 3839 | ch = '_'; |
| 3840 | goto make_var; |
| 3841 | } |
| 3842 | /* else: it's $_ */ |
| 3843 | case '-': |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3844 | /* still unhandled, but should be eventually */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3845 | bb_error_msg("unhandled syntax: $%c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3846 | return 1; |
| 3847 | break; |
| 3848 | default: |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3849 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3850 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3851 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3852 | return 0; |
| 3853 | } |
| 3854 | |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3855 | /* Scan input, call done_word() whenever full IFS delimited word was seen. |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3856 | * Call done_pipe if '\n' was seen (and end_trigger != NULL). |
| 3857 | * Return code is 0 if end_trigger char is met, |
| 3858 | * -1 on EOF (but if end_trigger == NULL then return 0), |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3859 | * 1 for syntax error */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3860 | static int parse_stream(o_string *dest, struct parse_context *ctx, |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3861 | struct in_str *input, const char *end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3862 | { |
"Vladimir N. Oleynik" | 4ccd2b4 | 2006-01-31 09:27:48 +0000 | [diff] [blame] | 3863 | int ch, m; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3864 | int redir_fd; |
| 3865 | redir_type redir_style; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3866 | int shadow_quote = dest->o_quote; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3867 | int next; |
| 3868 | |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3869 | /* Only double-quote state is handled in the state variable dest->o_quote. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3870 | * A single-quote triggers a bypass of the main loop until its mate is |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3871 | * found. When recursing, quote state is passed in via dest->o_quote. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3872 | |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3873 | debug_printf_parse("parse_stream entered, end_trigger='%s' dest->o_assignment:%d\n", end_trigger, dest->o_assignment); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3874 | |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3875 | while (1) { |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3876 | m = CHAR_IFS; |
| 3877 | next = '\0'; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3878 | ch = i_getch(input); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3879 | if (ch != EOF) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3880 | m = G.charmap[ch]; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3881 | if (ch != '\n') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3882 | next = i_peek(input); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3883 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3884 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3885 | debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n", |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3886 | ch, ch, m, dest->o_quote); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3887 | if (m == CHAR_ORDINARY |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3888 | || (m != CHAR_SPECIAL && shadow_quote) |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3889 | ) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3890 | if (ch == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3891 | syntax("unterminated \""); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3892 | debug_printf_parse("parse_stream return 1: unterminated \"\n"); |
| 3893 | return 1; |
| 3894 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3895 | o_addQchr(dest, ch); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3896 | if ((dest->o_assignment == MAYBE_ASSIGNMENT |
| 3897 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3898 | && ch == '=' |
| 3899 | && is_assignment(dest->data) |
| 3900 | ) { |
| 3901 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 3902 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3903 | continue; |
| 3904 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3905 | if (m == CHAR_IFS) { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3906 | if (done_word(dest, ctx)) { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3907 | debug_printf_parse("parse_stream return 1: done_word!=0\n"); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3908 | return 1; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3909 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3910 | if (ch == EOF) |
| 3911 | break; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3912 | /* If we aren't performing a substitution, treat |
| 3913 | * a newline as a command separator. |
| 3914 | * [why we don't handle it exactly like ';'? --vda] */ |
| 3915 | if (end_trigger && ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 3916 | #if ENABLE_HUSH_CASE |
| 3917 | /* "case ... in <newline> word) ..." - |
| 3918 | * newlines are ignored (but ';' wouldn't be) */ |
| 3919 | if (dest->length == 0 // && argv[0] == NULL |
| 3920 | && ctx->ctx_res_w == RES_MATCH |
| 3921 | ) { |
| 3922 | continue; |
| 3923 | } |
| 3924 | #endif |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3925 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3926 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3927 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3928 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3929 | if (end_trigger) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3930 | if (!shadow_quote && strchr(end_trigger, ch)) { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3931 | /* Special case: (...word) makes last word terminate, |
| 3932 | * as if ';' is seen */ |
| 3933 | if (ch == ')') { |
| 3934 | done_word(dest, ctx); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3935 | //err chk? |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3936 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3937 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3938 | } |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3939 | if (!HAS_KEYWORDS |
| 3940 | IF_HAS_KEYWORDS(|| (ctx->ctx_res_w == RES_NONE && ctx->old_flag == 0)) |
| 3941 | ) { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3942 | debug_printf_parse("parse_stream return 0: end_trigger char found\n"); |
| 3943 | return 0; |
| 3944 | } |
| 3945 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3946 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3947 | if (m == CHAR_IFS) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3948 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3949 | |
| 3950 | if (dest->o_assignment == MAYBE_ASSIGNMENT) { |
| 3951 | /* ch is a special char and thus this word |
| 3952 | * cannot be an assignment: */ |
| 3953 | dest->o_assignment = NOT_ASSIGNMENT; |
| 3954 | } |
| 3955 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3956 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3957 | case '#': |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3958 | if (dest->length == 0 && !shadow_quote) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3959 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3960 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3961 | if (ch == EOF || ch == '\n') |
| 3962 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3963 | i_getch(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3964 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3965 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3966 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3967 | } |
| 3968 | break; |
| 3969 | case '\\': |
| 3970 | if (next == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3971 | syntax("\\<eof>"); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3972 | debug_printf_parse("parse_stream return 1: \\<eof>\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3973 | return 1; |
| 3974 | } |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3975 | /* bash: |
| 3976 | * "The backslash retains its special meaning [in "..."] |
| 3977 | * only when followed by one of the following characters: |
| 3978 | * $, `, ", \, or <newline>. A double quote may be quoted |
| 3979 | * within double quotes by preceding it with a backslash. |
| 3980 | * If enabled, history expansion will be performed unless |
| 3981 | * an ! appearing in double quotes is escaped using |
| 3982 | * a backslash. The backslash preceding the ! is not removed." |
| 3983 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3984 | if (shadow_quote) { //NOT SURE dest->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3985 | if (strchr("$`\"\\", next) != NULL) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3986 | o_addqchr(dest, i_getch(input)); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3987 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3988 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3989 | } |
| 3990 | } else { |
| 3991 | o_addchr(dest, '\\'); |
| 3992 | o_addchr(dest, i_getch(input)); |
| 3993 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3994 | break; |
| 3995 | case '$': |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3996 | if (handle_dollar(dest, input) != 0) { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3997 | debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n"); |
| 3998 | return 1; |
| 3999 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4000 | break; |
| 4001 | case '\'': |
| 4002 | dest->nonnull = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4003 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4004 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4005 | if (ch == EOF) { |
| 4006 | syntax("unterminated '"); |
| 4007 | debug_printf_parse("parse_stream return 1: unterminated '\n"); |
| 4008 | return 1; |
| 4009 | } |
| 4010 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4011 | break; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4012 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 4013 | o_addqchr(dest, ch); |
| 4014 | else |
| 4015 | o_addchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4016 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4017 | break; |
| 4018 | case '"': |
| 4019 | dest->nonnull = 1; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4020 | shadow_quote ^= 1; /* invert */ |
| 4021 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 4022 | dest->o_quote ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4023 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4024 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4025 | case '`': { |
| 4026 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4027 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4028 | o_addchr(dest, shadow_quote /*or dest->o_quote??*/ ? 0x80 | '`' : '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4029 | add_till_backquote(dest, input); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4030 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 4031 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4032 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4033 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4034 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4035 | case '>': |
| 4036 | redir_fd = redirect_opt_num(dest); |
| 4037 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4038 | redir_style = REDIRECT_OVERWRITE; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4039 | if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4040 | redir_style = REDIRECT_APPEND; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4041 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4042 | } |
| 4043 | #if 0 |
| 4044 | else if (next == '(') { |
| 4045 | syntax(">(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4046 | debug_printf_parse("parse_stream return 1: >(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4047 | return 1; |
| 4048 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4049 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4050 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 4051 | break; |
| 4052 | case '<': |
| 4053 | redir_fd = redirect_opt_num(dest); |
| 4054 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4055 | redir_style = REDIRECT_INPUT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4056 | if (next == '<') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4057 | redir_style = REDIRECT_HEREIS; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4058 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4059 | } else if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4060 | redir_style = REDIRECT_IO; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4061 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4062 | } |
| 4063 | #if 0 |
| 4064 | else if (next == '(') { |
| 4065 | syntax("<(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4066 | debug_printf_parse("parse_stream return 1: <(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4067 | return 1; |
| 4068 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4069 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4070 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 4071 | break; |
| 4072 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4073 | #if ENABLE_HUSH_CASE |
| 4074 | case_semi: |
| 4075 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4076 | done_word(dest, ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4077 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4078 | #if ENABLE_HUSH_CASE |
| 4079 | /* Eat multiple semicolons, detect |
| 4080 | * whether it means something special */ |
| 4081 | while (1) { |
| 4082 | ch = i_peek(input); |
| 4083 | if (ch != ';') |
| 4084 | break; |
| 4085 | i_getch(input); |
| 4086 | if (ctx->ctx_res_w == RES_CASEI) { |
| 4087 | ctx->ctx_dsemicolon = 1; |
| 4088 | ctx->ctx_res_w = RES_MATCH; |
| 4089 | break; |
| 4090 | } |
| 4091 | } |
| 4092 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4093 | new_cmd: |
| 4094 | /* We just finished a cmd. New one may start |
| 4095 | * with an assignment */ |
| 4096 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4097 | break; |
| 4098 | case '&': |
| 4099 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4100 | if (next == '&') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4101 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4102 | done_pipe(ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4103 | } else { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4104 | done_pipe(ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4105 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4106 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4107 | case '|': |
| 4108 | done_word(dest, ctx); |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4109 | #if ENABLE_HUSH_CASE |
| 4110 | if (ctx->ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4111 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4112 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4113 | if (next == '|') { /* || */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4114 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4115 | done_pipe(ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4116 | } else { |
| 4117 | /* we could pick up a file descriptor choice here |
| 4118 | * with redirect_opt_num(), but bash doesn't do it. |
| 4119 | * "echo foo 2| cat" yields "foo 2". */ |
| 4120 | done_command(ctx); |
| 4121 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4122 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4123 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4124 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4125 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4126 | if (ctx->ctx_res_w == RES_MATCH |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4127 | && ctx->command->argv == NULL /* not (word|(... */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4128 | && dest->length == 0 /* not word(... */ |
| 4129 | && dest->nonnull == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4130 | ) { |
| 4131 | continue; |
| 4132 | } |
| 4133 | #endif |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4134 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4135 | if (dest->length != 0 /* not just () but word() */ |
| 4136 | && dest->nonnull == 0 /* not a"b"c() */ |
| 4137 | && ctx->command->argv == NULL /* it's the first word */ |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4138 | //TODO: "func ( ) {...}" - note spaces - is valid format too in bash |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4139 | && i_peek(input) == ')' |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4140 | && !match_reserved_word(dest) |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4141 | ) { |
| 4142 | bb_error_msg("seems like a function definition"); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4143 | i_getch(input); |
| 4144 | do { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4145 | //TODO: do it properly. |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4146 | ch = i_getch(input); |
| 4147 | } while (ch == ' ' || ch == '\n'); |
| 4148 | if (ch != '{') { |
| 4149 | syntax("was expecting {"); |
| 4150 | debug_printf_parse("parse_stream return 1\n"); |
| 4151 | return 1; |
| 4152 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4153 | ch = 'F'; /* magic value */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4154 | } |
| 4155 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4156 | case '{': |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4157 | if (parse_group(dest, ctx, input, ch) != 0) { |
| 4158 | debug_printf_parse("parse_stream return 1: parse_group returned non-0\n"); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4159 | return 1; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4160 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4161 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4162 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4163 | #if ENABLE_HUSH_CASE |
| 4164 | if (ctx->ctx_res_w == RES_MATCH) |
| 4165 | goto case_semi; |
| 4166 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4167 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4168 | /* proper use of this character is caught by end_trigger: |
| 4169 | * if we see {, we call parse_group(..., end_trigger='}') |
| 4170 | * and it will match } earlier (not here). */ |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4171 | syntax("unexpected } or )"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4172 | debug_printf_parse("parse_stream return 1: unexpected '}'\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4173 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4174 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4175 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4176 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4177 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4178 | } /* while (1) */ |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4179 | debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL)); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4180 | if (end_trigger) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4181 | return -1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4182 | return 0; |
| 4183 | } |
| 4184 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4185 | static void set_in_charmap(const char *set, int code) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4186 | { |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 4187 | while (*set) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4188 | G.charmap[(unsigned char)*set++] = code; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4191 | static void update_charmap(void) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4192 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4193 | G.ifs = getenv("IFS"); |
| 4194 | if (G.ifs == NULL) |
| 4195 | G.ifs = " \t\n"; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4196 | /* Precompute a list of 'flow through' behavior so it can be treated |
| 4197 | * quickly up front. Computation is necessary because of IFS. |
| 4198 | * Special case handling of IFS == " \t\n" is not implemented. |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4199 | * The charmap[] array only really needs two bits each, |
| 4200 | * and on most machines that would be faster (reduced L1 cache use). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4201 | */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4202 | memset(G.charmap, CHAR_ORDINARY, sizeof(G.charmap)); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4203 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4204 | set_in_charmap("\\$\"`", CHAR_SPECIAL); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4205 | #else |
| 4206 | set_in_charmap("\\$\"", CHAR_SPECIAL); |
| 4207 | #endif |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4208 | set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4209 | set_in_charmap(G.ifs, CHAR_IFS); /* are ordinary if quoted */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4210 | } |
| 4211 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4212 | /* Most recursion does not come through here, the exception is |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4213 | * from builtin_source() and builtin_eval() */ |
| 4214 | static int parse_and_run_stream(struct in_str *inp, int parse_flag) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4215 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4216 | struct parse_context ctx; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4217 | o_string temp = NULL_O_STRING; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4218 | int rcode; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4219 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4220 | do { |
| 4221 | initialize_context(&ctx); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4222 | update_charmap(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4223 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 4224 | inp->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4225 | #endif |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4226 | /* We will stop & execute after each ';' or '\n'. |
| 4227 | * Example: "sleep 9999; echo TEST" + ctrl-C: |
| 4228 | * TEST should be printed */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4229 | temp.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4230 | rcode = parse_stream(&temp, &ctx, inp, ";\n"); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4231 | #if HAS_KEYWORDS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4232 | if (rcode != 1 && ctx.old_flag != 0) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4233 | syntax(NULL); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4234 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4235 | #endif |
| 4236 | if (rcode != 1 IF_HAS_KEYWORDS(&& ctx.old_flag == 0)) { |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4237 | done_word(&temp, &ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4238 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4239 | debug_print_tree(ctx.list_head, 0); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4240 | debug_printf_exec("parse_stream_outer: run_and_free_list\n"); |
| 4241 | run_and_free_list(ctx.list_head); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4242 | } else { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4243 | /* We arrive here also if rcode == 1 (error in parse_stream) */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4244 | #if HAS_KEYWORDS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4245 | if (ctx.old_flag != 0) { |
| 4246 | free(ctx.stack); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4247 | o_reset(&temp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4248 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4249 | #endif |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 4250 | /*temp.nonnull = 0; - o_free does it below */ |
| 4251 | /*temp.o_quote = 0; - o_free does it below */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4252 | free_pipe_list(ctx.list_head, /* indent: */ 0); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4253 | /* Discard all unprocessed line input, force prompt on */ |
| 4254 | inp->p = NULL; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4255 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4256 | inp->promptme = 1; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4257 | #endif |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4258 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4259 | o_free(&temp); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4260 | /* loop on syntax errors, return on EOF: */ |
| 4261 | } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4262 | return 0; |
| 4263 | } |
| 4264 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4265 | static int parse_and_run_string(const char *s, int parse_flag) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4266 | { |
| 4267 | struct in_str input; |
| 4268 | setup_string_in_str(&input, s); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4269 | return parse_and_run_stream(&input, parse_flag); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4272 | static int parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4273 | { |
| 4274 | int rcode; |
| 4275 | struct in_str input; |
| 4276 | setup_file_in_str(&input, f); |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4277 | rcode = parse_and_run_stream(&input, 0 /* parse_flag */); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4278 | return rcode; |
| 4279 | } |
| 4280 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4281 | #if ENABLE_HUSH_JOB |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4282 | /* Make sure we have a controlling tty. If we get started under a job |
| 4283 | * aware app (like bash for example), make sure we are now in charge so |
| 4284 | * we don't fight over who gets the foreground */ |
Eric Andersen | eaecbf3 | 2001-10-31 10:41:31 +0000 | [diff] [blame] | 4285 | static void setup_job_control(void) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4286 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4287 | pid_t shell_pgrp; |
| 4288 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4289 | shell_pgrp = getpgrp(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4290 | close_on_exec_on(G.interactive_fd); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4291 | |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 4292 | /* If we were ran as 'hush &', |
| 4293 | * sleep until we are in the foreground. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4294 | while (tcgetpgrp(G.interactive_fd) != shell_pgrp) { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4295 | /* Send TTIN to ourself (should stop us) */ |
Denis Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 4296 | kill(- shell_pgrp, SIGTTIN); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 4297 | shell_pgrp = getpgrp(); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4298 | } |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4299 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4300 | /* Ignore job-control and misc signals. */ |
| 4301 | set_jobctrl_sighandler(SIG_IGN); |
| 4302 | set_misc_sighandler(SIG_IGN); |
| 4303 | //huh? signal(SIGCHLD, SIG_IGN); |
| 4304 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4305 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4306 | set_fatal_sighandler(sigexit); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4307 | |
| 4308 | /* Put ourselves in our own process group. */ |
Bernhard Reutner-Fischer | 864329d | 2008-09-25 10:55:05 +0000 | [diff] [blame] | 4309 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4310 | /* Grab control of the terminal. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4311 | tcsetpgrp(G.interactive_fd, getpid()); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4312 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4313 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4314 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4315 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 4316 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 4317 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4318 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4319 | static const struct variable const_shell_ver = { |
| 4320 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4321 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4322 | .max_len = 1, /* 0 can provoke free(name) */ |
| 4323 | .flg_export = 1, |
| 4324 | .flg_read_only = 1, |
| 4325 | }; |
| 4326 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4327 | int opt; |
| 4328 | FILE *input; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4329 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4330 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 4331 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 4332 | INIT_G(); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4333 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4334 | G.root_pid = getpid(); |
Denis Vlasenko | 16c2fea | 2008-06-17 12:28:44 +0000 | [diff] [blame] | 4335 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4336 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4337 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 4338 | G.top_var = &G.shell_ver; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4339 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4340 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 4341 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4342 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4343 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4344 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4345 | if (e) while (*e) { |
| 4346 | char *value = strchr(*e, '='); |
| 4347 | if (value) { /* paranoia */ |
| 4348 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 4349 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 4350 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4351 | cur_var->max_len = strlen(*e); |
| 4352 | cur_var->flg_export = 1; |
| 4353 | } |
| 4354 | e++; |
| 4355 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4356 | debug_printf_env("putenv '%s'\n", hush_version_str); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4357 | putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 4358 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4359 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4360 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 4361 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4362 | /* XXX what should these be while sourcing /etc/profile? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4363 | G.global_argc = argc; |
| 4364 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 4365 | /* Initialize some more globals to non-zero values */ |
| 4366 | set_cwd(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4367 | #if ENABLE_HUSH_INTERACTIVE |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 4368 | if (ENABLE_FEATURE_EDITING) |
| 4369 | cmdedit_set_initial_prompt(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4370 | G.PS2 = "> "; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4371 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 4372 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4373 | if (EXIT_SUCCESS) /* otherwise is already done */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4374 | G.last_return_code = EXIT_SUCCESS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4375 | |
| 4376 | if (argv[0] && argv[0][0] == '-') { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4377 | debug_printf("sourcing /etc/profile\n"); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4378 | input = fopen_for_read("/etc/profile"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4379 | if (input != NULL) { |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 4380 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4381 | parse_and_run_file(input); |
Eric Andersen | a90f20b | 2001-06-26 23:00:21 +0000 | [diff] [blame] | 4382 | fclose(input); |
| 4383 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4384 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4385 | input = stdin; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4386 | |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 4387 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
| 4388 | while ((opt = getopt(argc, argv, "c:xins")) > 0) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4389 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4390 | case 'c': |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4391 | G.global_argv = argv + optind; |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 4392 | if (!argv[optind]) { |
| 4393 | /* -c 'script' (no params): prevent empty $0 */ |
| 4394 | *--G.global_argv = argv[0]; |
| 4395 | optind--; |
| 4396 | } /* else -c 'script' PAR0 PAR1: $0 is PAR0 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4397 | G.global_argc = argc - optind; |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4398 | opt = parse_and_run_string(optarg, 0 /* parse_flag */); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4399 | goto final_return; |
| 4400 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4401 | /* Well, we cannot just declare interactiveness, |
| 4402 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4403 | /* G.interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4404 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 4405 | case 's': |
| 4406 | /* "-s" means "read from stdin", but this is how we always |
| 4407 | * operate, so simply do nothing here. */ |
| 4408 | break; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 4409 | case 'n': |
| 4410 | case 'x': |
| 4411 | if (!builtin_set_mode('-', opt)) |
| 4412 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4413 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4414 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4415 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 4416 | " or: sh -c command [args]...\n\n"); |
| 4417 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4418 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4419 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4420 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4421 | } |
| 4422 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4423 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4424 | /* 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] | 4425 | * the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 4426 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4427 | * no arguments remaining or the -s flag given |
| 4428 | * standard input is a terminal |
| 4429 | * standard output is a terminal |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4430 | * Refer to Posix.2, the description of the 'sh' utility. */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4431 | if (argv[optind] == NULL && input == stdin |
| 4432 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 4433 | ) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4434 | G.saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 4435 | debug_printf("saved_tty_pgrp=%d\n", G.saved_tty_pgrp); |
| 4436 | if (G.saved_tty_pgrp >= 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4437 | /* try to dup to high fd#, >= 255 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4438 | G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 4439 | if (G.interactive_fd < 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4440 | /* try to dup to any fd */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4441 | G.interactive_fd = dup(STDIN_FILENO); |
| 4442 | if (G.interactive_fd < 0) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4443 | /* give up */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4444 | G.interactive_fd = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4445 | } |
Denis Vlasenko | 2f1bb36 | 2007-04-21 10:01:14 +0000 | [diff] [blame] | 4446 | // TODO: track & disallow any attempts of user |
| 4447 | // to (inadvertently) close/redirect it |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4448 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4449 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4450 | debug_printf("G.interactive_fd=%d\n", G.interactive_fd); |
| 4451 | if (G.interactive_fd) { |
| 4452 | fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4453 | /* Looks like they want an interactive shell */ |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4454 | setup_job_control(); |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 4455 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 4456 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
| 4457 | die_sleep = -1; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4458 | if (setjmp(die_jmp)) { |
| 4459 | /* xfunc has failed! die die die */ |
| 4460 | hush_exit(xfunc_error_retval); |
| 4461 | } |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4462 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4463 | #elif ENABLE_HUSH_INTERACTIVE |
| 4464 | /* no job control compiled, only prompt/line editing */ |
| 4465 | if (argv[optind] == NULL && input == stdin |
| 4466 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 4467 | ) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4468 | G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 4469 | if (G.interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4470 | /* try to dup to any fd */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4471 | G.interactive_fd = dup(STDIN_FILENO); |
| 4472 | if (G.interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4473 | /* give up */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4474 | G.interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4475 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4476 | if (G.interactive_fd) { |
| 4477 | fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 4478 | set_misc_sighandler(SIG_IGN); |
| 4479 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4480 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4481 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4482 | |
Denis Vlasenko | 701ac18 | 2009-03-28 19:22:08 +0000 | [diff] [blame] | 4483 | #if ENABLE_HUSH_INTERACTIVE && !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 4484 | if (G.interactive_fd) { |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 4485 | printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", bb_banner); |
| 4486 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
| 4487 | } |
Denis Vlasenko | 701ac18 | 2009-03-28 19:22:08 +0000 | [diff] [blame] | 4488 | #endif |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 4489 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4490 | if (argv[optind] == NULL) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4491 | opt = parse_and_run_file(stdin); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4492 | } else { |
| 4493 | debug_printf("\nrunning script '%s'\n", argv[optind]); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4494 | G.global_argv = argv + optind; |
| 4495 | G.global_argc = argc - optind; |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4496 | input = xfopen_for_read(argv[optind]); |
Denis Vlasenko | 459a5ad | 2008-02-11 08:35:03 +0000 | [diff] [blame] | 4497 | fcntl(fileno(input), F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4498 | opt = parse_and_run_file(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4499 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4500 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4501 | final_return: |
| 4502 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4503 | #if ENABLE_FEATURE_CLEAN_UP |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4504 | fclose(input); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4505 | if (G.cwd != bb_msg_unknown) |
| 4506 | free((char*)G.cwd); |
| 4507 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4508 | while (cur_var) { |
| 4509 | struct variable *tmp = cur_var; |
| 4510 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 4511 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4512 | cur_var = cur_var->next; |
| 4513 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4514 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4515 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4516 | hush_exit(opt ? opt : G.last_return_code); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4517 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 4518 | |
| 4519 | |
| 4520 | #if ENABLE_LASH |
| 4521 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 4522 | int lash_main(int argc, char **argv) |
| 4523 | { |
| 4524 | //bb_error_msg("lash is deprecated, please use hush instead"); |
| 4525 | return hush_main(argc, argv); |
| 4526 | } |
| 4527 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4528 | |
| 4529 | |
| 4530 | /* |
| 4531 | * Built-ins |
| 4532 | */ |
Mike Frysinger | 0080ea7 | 2009-03-30 03:50:07 +0000 | [diff] [blame] | 4533 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4534 | * |
| 4535 | * Traps are also evaluated immediately instead of being delayed properly: |
| 4536 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_11 |
| 4537 | * Example: hush -c 'trap "echo hi" 31; sleep 10; echo moo' & sleep 1; kill -31 $! |
| 4538 | * "hi" should not be displayed until the sleep finishes |
| 4539 | * This will have to get fixed ... |
| 4540 | */ |
| 4541 | static void handle_trap(int sig) |
| 4542 | { |
| 4543 | int save_errno, save_rcode; |
| 4544 | char *argv[] = { NULL, G.traps[sig].cmd, NULL }; |
| 4545 | /* Race! We transitioned from handled to ignore/default, but |
| 4546 | * the signal came in after updating .cmd but before we could |
| 4547 | * register the new signal handler. |
| 4548 | */ |
| 4549 | if (!argv[1] || argv[1][0] == '\0') |
| 4550 | return; |
| 4551 | /* need to save/restore errno/$? across traps */ |
| 4552 | save_errno = errno; |
| 4553 | save_rcode = G.last_return_code; |
| 4554 | builtin_eval(argv); |
| 4555 | errno = save_errno; |
| 4556 | G.last_return_code = save_rcode; |
| 4557 | } |
| 4558 | static int builtin_trap(char **argv) |
| 4559 | { |
| 4560 | size_t i; |
| 4561 | int sig; |
| 4562 | bool ign = false; |
| 4563 | char *new_cmd = NULL; |
| 4564 | |
| 4565 | if (!G.traps) |
| 4566 | G.traps = xzalloc(sizeof(*G.traps) * NSIG); |
| 4567 | |
| 4568 | if (!argv[1]) { |
| 4569 | /* No args: print all trapped. This isn't 100% correct as we should |
| 4570 | * be escaping the cmd so that it can be pasted back in ... |
| 4571 | */ |
| 4572 | for (i = 0; i < NSIG; ++i) |
| 4573 | if (G.traps[i].cmd) |
| 4574 | printf("trap -- '%s' %s\n", G.traps[i].cmd, get_signame(i)); |
| 4575 | return EXIT_SUCCESS; |
| 4576 | } |
| 4577 | |
| 4578 | /* first arg is decimal: reset all specified */ |
| 4579 | sig = bb_strtou(argv[1], NULL, 10); |
| 4580 | if (errno == 0) { |
| 4581 | int ret; |
| 4582 | i = 0; |
| 4583 | set_all: |
| 4584 | ret = EXIT_SUCCESS; |
| 4585 | while (argv[++i]) { |
| 4586 | char *old_cmd; |
| 4587 | |
| 4588 | sig = get_signum(argv[i]); |
| 4589 | if (sig < 0 || sig >= NSIG) { |
| 4590 | ret = EXIT_FAILURE; |
| 4591 | bb_perror_msg("trap: %s: invalid signal specification", argv[i]); |
| 4592 | continue; |
| 4593 | } |
| 4594 | |
| 4595 | /* Make sure .cmd is always a valid command list since |
| 4596 | * signals can occur at any time ... |
| 4597 | */ |
| 4598 | old_cmd = G.traps[sig].cmd; |
| 4599 | G.traps[sig].cmd = xstrdup(new_cmd); |
| 4600 | free(old_cmd); |
| 4601 | |
| 4602 | debug_printf("trap: setting SIG%s (%i) to: %s", |
| 4603 | get_signame(sig), sig, G.traps[sig].cmd); |
| 4604 | |
| 4605 | /* There is no signal for 0 (EXIT) */ |
| 4606 | if (sig == 0) |
| 4607 | continue; |
| 4608 | |
| 4609 | if (new_cmd) { |
| 4610 | /* add/update a handler */ |
| 4611 | struct sigaction act = { |
| 4612 | .sa_handler = ign ? SIG_IGN : handle_trap, |
| 4613 | .sa_flags = SA_RESTART, |
| 4614 | }; |
| 4615 | sigemptyset(&act.sa_mask); |
| 4616 | sigaction(sig, &act, old_cmd ? NULL : &G.traps[sig].oact); |
| 4617 | } else if (old_cmd && !new_cmd) |
| 4618 | /* there was a handler, and we are removing it */ |
| 4619 | sigaction_set(sig, &G.traps[sig].oact); |
| 4620 | } |
| 4621 | return ret; |
| 4622 | } |
| 4623 | |
| 4624 | /* first arg is "-": reset all specified to default */ |
| 4625 | /* first arg is "": ignore all specified */ |
| 4626 | /* everything else: execute first arg upon signal */ |
| 4627 | if (!argv[2]) { |
| 4628 | bb_error_msg("trap: invalid arguments"); |
| 4629 | return EXIT_FAILURE; |
| 4630 | } |
| 4631 | if (LONE_DASH(argv[1])) |
| 4632 | /* nothing! */; |
| 4633 | else |
| 4634 | new_cmd = argv[1]; |
| 4635 | if (argv[1][0] == '\0') |
| 4636 | ign = true; |
| 4637 | i = 1; |
| 4638 | goto set_all; |
| 4639 | } |
| 4640 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4641 | static int builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4642 | { |
| 4643 | return 0; |
| 4644 | } |
| 4645 | |
| 4646 | static int builtin_test(char **argv) |
| 4647 | { |
| 4648 | int argc = 0; |
| 4649 | while (*argv) { |
| 4650 | argc++; |
| 4651 | argv++; |
| 4652 | } |
| 4653 | return test_main(argc, argv - argc); |
| 4654 | } |
| 4655 | |
| 4656 | static int builtin_echo(char **argv) |
| 4657 | { |
| 4658 | int argc = 0; |
| 4659 | while (*argv) { |
| 4660 | argc++; |
| 4661 | argv++; |
| 4662 | } |
| 4663 | return echo_main(argc, argv - argc); |
| 4664 | } |
| 4665 | |
| 4666 | static int builtin_eval(char **argv) |
| 4667 | { |
| 4668 | int rcode = EXIT_SUCCESS; |
| 4669 | |
| 4670 | if (argv[1]) { |
| 4671 | char *str = expand_strvec_to_string(argv + 1); |
| 4672 | parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP); |
| 4673 | free(str); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4674 | rcode = G.last_return_code; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4675 | } |
| 4676 | return rcode; |
| 4677 | } |
| 4678 | |
| 4679 | static int builtin_cd(char **argv) |
| 4680 | { |
| 4681 | const char *newdir; |
| 4682 | if (argv[1] == NULL) { |
| 4683 | // bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
| 4684 | // bash says "bash: cd: HOME not set" and does nothing (exitcode 1) |
| 4685 | newdir = getenv("HOME") ? : "/"; |
| 4686 | } else |
| 4687 | newdir = argv[1]; |
| 4688 | if (chdir(newdir)) { |
| 4689 | printf("cd: %s: %s\n", newdir, strerror(errno)); |
| 4690 | return EXIT_FAILURE; |
| 4691 | } |
| 4692 | set_cwd(); |
| 4693 | return EXIT_SUCCESS; |
| 4694 | } |
| 4695 | |
| 4696 | static int builtin_exec(char **argv) |
| 4697 | { |
| 4698 | if (argv[1] == NULL) |
| 4699 | return EXIT_SUCCESS; /* bash does this */ |
| 4700 | { |
| 4701 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4702 | nommu_save_t dummy; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4703 | #endif |
| 4704 | // FIXME: if exec fails, bash does NOT exit! We do... |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4705 | pseudo_exec_argv(&dummy, argv + 1, 0, NULL); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4706 | /* never returns */ |
| 4707 | } |
| 4708 | } |
| 4709 | |
| 4710 | static int builtin_exit(char **argv) |
| 4711 | { |
| 4712 | // TODO: bash does it ONLY on top-level sh exit (+interacive only?) |
| 4713 | //puts("exit"); /* bash does it */ |
| 4714 | // TODO: warn if we have background jobs: "There are stopped jobs" |
| 4715 | // On second consecutive 'exit', exit anyway. |
| 4716 | if (argv[1] == NULL) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4717 | hush_exit(G.last_return_code); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4718 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 4719 | xfunc_error_retval = 255; |
| 4720 | /* bash: exit -2 == exit 254, no error msg */ |
| 4721 | hush_exit(xatoi(argv[1]) & 0xff); |
| 4722 | } |
| 4723 | |
| 4724 | static int builtin_export(char **argv) |
| 4725 | { |
| 4726 | const char *value; |
| 4727 | char *name = argv[1]; |
| 4728 | |
| 4729 | if (name == NULL) { |
| 4730 | // TODO: |
| 4731 | // ash emits: export VAR='VAL' |
| 4732 | // bash: declare -x VAR="VAL" |
| 4733 | // (both also escape as needed (quotes, $, etc)) |
| 4734 | char **e = environ; |
| 4735 | if (e) |
| 4736 | while (*e) |
| 4737 | puts(*e++); |
| 4738 | return EXIT_SUCCESS; |
| 4739 | } |
| 4740 | |
| 4741 | value = strchr(name, '='); |
| 4742 | if (!value) { |
| 4743 | /* They are exporting something without a =VALUE */ |
| 4744 | struct variable *var; |
| 4745 | |
| 4746 | var = get_local_var(name); |
| 4747 | if (var) { |
| 4748 | var->flg_export = 1; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4749 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4750 | putenv(var->varstr); |
| 4751 | } |
| 4752 | /* bash does not return an error when trying to export |
| 4753 | * an undefined variable. Do likewise. */ |
| 4754 | return EXIT_SUCCESS; |
| 4755 | } |
| 4756 | |
| 4757 | set_local_var(xstrdup(name), 1); |
| 4758 | return EXIT_SUCCESS; |
| 4759 | } |
| 4760 | |
| 4761 | #if ENABLE_HUSH_JOB |
| 4762 | /* built-in 'fg' and 'bg' handler */ |
| 4763 | static int builtin_fg_bg(char **argv) |
| 4764 | { |
| 4765 | int i, jobnum; |
| 4766 | struct pipe *pi; |
| 4767 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4768 | if (!G.interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4769 | return EXIT_FAILURE; |
| 4770 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 4771 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4772 | for (pi = G.job_list; pi; pi = pi->next) { |
| 4773 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4774 | goto found; |
| 4775 | } |
| 4776 | } |
| 4777 | bb_error_msg("%s: no current job", argv[0]); |
| 4778 | return EXIT_FAILURE; |
| 4779 | } |
| 4780 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 4781 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 4782 | return EXIT_FAILURE; |
| 4783 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4784 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4785 | if (pi->jobid == jobnum) { |
| 4786 | goto found; |
| 4787 | } |
| 4788 | } |
| 4789 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 4790 | return EXIT_FAILURE; |
| 4791 | found: |
| 4792 | // TODO: bash prints a string representation |
| 4793 | // of job being foregrounded (like "sleep 1 | cat") |
| 4794 | if (*argv[0] == 'f') { |
| 4795 | /* Put the job into the foreground. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4796 | tcsetpgrp(G.interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4800 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 4801 | for (i = 0; i < pi->num_cmds; i++) { |
| 4802 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 4803 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4804 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4805 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4806 | |
| 4807 | i = kill(- pi->pgrp, SIGCONT); |
| 4808 | if (i < 0) { |
| 4809 | if (errno == ESRCH) { |
| 4810 | delete_finished_bg_job(pi); |
| 4811 | return EXIT_SUCCESS; |
| 4812 | } else { |
| 4813 | bb_perror_msg("kill (SIGCONT)"); |
| 4814 | } |
| 4815 | } |
| 4816 | |
| 4817 | if (*argv[0] == 'f') { |
| 4818 | remove_bg_job(pi); |
| 4819 | return checkjobs_and_fg_shell(pi); |
| 4820 | } |
| 4821 | return EXIT_SUCCESS; |
| 4822 | } |
| 4823 | #endif |
| 4824 | |
| 4825 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4826 | static int builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4827 | { |
| 4828 | const struct built_in_command *x; |
| 4829 | |
| 4830 | printf("\nBuilt-in commands:\n"); |
| 4831 | printf("-------------------\n"); |
| 4832 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 4833 | printf("%s\t%s\n", x->cmd, x->descr); |
| 4834 | } |
| 4835 | printf("\n\n"); |
| 4836 | return EXIT_SUCCESS; |
| 4837 | } |
| 4838 | #endif |
| 4839 | |
| 4840 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4841 | static int builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4842 | { |
| 4843 | struct pipe *job; |
| 4844 | const char *status_string; |
| 4845 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4846 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4847 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4848 | status_string = "Stopped"; |
| 4849 | else |
| 4850 | status_string = "Running"; |
| 4851 | |
| 4852 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 4853 | } |
| 4854 | return EXIT_SUCCESS; |
| 4855 | } |
| 4856 | #endif |
| 4857 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4858 | static int builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4859 | { |
| 4860 | puts(set_cwd()); |
| 4861 | return EXIT_SUCCESS; |
| 4862 | } |
| 4863 | |
| 4864 | static int builtin_read(char **argv) |
| 4865 | { |
| 4866 | char *string; |
| 4867 | const char *name = argv[1] ? argv[1] : "REPLY"; |
| 4868 | |
| 4869 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
| 4870 | return set_local_var(string, 0); |
| 4871 | } |
| 4872 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 4873 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 4874 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 4875 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 4876 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 4877 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 4878 | * set -- [argument...] |
| 4879 | * set -o |
| 4880 | * set +o |
| 4881 | * Implementations shall support the options in both their hyphen and |
| 4882 | * plus-sign forms. These options can also be specified as options to sh. |
| 4883 | * Examples: |
| 4884 | * Write out all variables and their values: set |
| 4885 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 4886 | * Turn on the -x and -v options: set -xv |
| 4887 | * Unset all positional parameters: set -- |
| 4888 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 4889 | * Set the positional parameters to the expansion of x, even if x expands |
| 4890 | * with a leading '-' or '+': set -- $x |
| 4891 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 4892 | * 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] | 4893 | */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 4894 | static int builtin_set_mode(const char cstate, const char mode) |
| 4895 | { |
| 4896 | int state = (cstate == '-' ? 1 : 0); |
| 4897 | switch (mode) { |
| 4898 | case 'n': G.fake_mode = state; break; |
| 4899 | case 'x': /*G.debug_mode = state;*/ break; |
| 4900 | default: return EXIT_FAILURE; |
| 4901 | } |
| 4902 | return EXIT_SUCCESS; |
| 4903 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4904 | static int builtin_set(char **argv) |
| 4905 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4906 | int n; |
| 4907 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 4908 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4909 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 4910 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4911 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4912 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4913 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4914 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 4915 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4916 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 4917 | do { |
| 4918 | if (!strcmp(arg, "--")) { |
| 4919 | ++argv; |
| 4920 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4921 | } |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 4922 | |
| 4923 | if (arg[0] == '+' || arg[0] == '-') { |
| 4924 | for (n = 1; arg[n]; ++n) |
| 4925 | if (builtin_set_mode(arg[0], arg[n])) |
| 4926 | goto error; |
| 4927 | continue; |
| 4928 | } |
| 4929 | |
| 4930 | break; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4931 | } while ((arg = *++argv) != NULL); |
| 4932 | /* Now argv[0] is 1st argument */ |
| 4933 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 4934 | /* Only reset global_argv if we didn't process anything */ |
| 4935 | if (arg == NULL) |
| 4936 | return EXIT_SUCCESS; |
| 4937 | set_argv: |
| 4938 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4939 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 4940 | g_argv = G.global_argv; |
| 4941 | if (G.global_args_malloced) { |
| 4942 | pp = g_argv; |
| 4943 | while (*++pp) |
| 4944 | free(*pp); |
| 4945 | g_argv[1] = NULL; |
| 4946 | } else { |
| 4947 | G.global_args_malloced = 1; |
| 4948 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 4949 | pp[0] = g_argv[0]; /* retain $0 */ |
| 4950 | g_argv = pp; |
| 4951 | } |
| 4952 | /* This realloc's G.global_argv */ |
| 4953 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 4954 | |
| 4955 | n = 1; |
| 4956 | while (*++pp) |
| 4957 | n++; |
| 4958 | G.global_argc = n; |
| 4959 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4960 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 4961 | |
| 4962 | /* Nothing known, so abort */ |
| 4963 | error: |
| 4964 | bb_error_msg("set: %s: invalid option", arg); |
| 4965 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4966 | } |
| 4967 | |
| 4968 | static int builtin_shift(char **argv) |
| 4969 | { |
| 4970 | int n = 1; |
| 4971 | if (argv[1]) { |
| 4972 | n = atoi(argv[1]); |
| 4973 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4974 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 4975 | if (G.global_args_malloced) { |
| 4976 | int m = 1; |
| 4977 | while (m <= n) |
| 4978 | free(G.global_argv[m++]); |
| 4979 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4980 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 4981 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 4982 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4983 | return EXIT_SUCCESS; |
| 4984 | } |
| 4985 | return EXIT_FAILURE; |
| 4986 | } |
| 4987 | |
| 4988 | static int builtin_source(char **argv) |
| 4989 | { |
| 4990 | FILE *input; |
| 4991 | int status; |
| 4992 | |
| 4993 | if (argv[1] == NULL) |
| 4994 | return EXIT_FAILURE; |
| 4995 | |
| 4996 | /* XXX search through $PATH is missing */ |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4997 | input = fopen_for_read(argv[1]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4998 | if (!input) { |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 4999 | bb_error_msg("can't open '%s'", argv[1]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5000 | return EXIT_FAILURE; |
| 5001 | } |
| 5002 | close_on_exec_on(fileno(input)); |
| 5003 | |
| 5004 | /* Now run the file */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5005 | /* 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] | 5006 | * (pointer only is OK!) on this stack frame, |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5007 | * set G.global_argv=argv+1, recurse, and restore. */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5008 | status = parse_and_run_file(input); |
| 5009 | fclose(input); |
| 5010 | return status; |
| 5011 | } |
| 5012 | |
| 5013 | static int builtin_umask(char **argv) |
| 5014 | { |
| 5015 | mode_t new_umask; |
| 5016 | const char *arg = argv[1]; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5017 | if (arg) { |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 5018 | new_umask = bb_strtou(arg, NULL, 8); |
| 5019 | if (errno) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5020 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5021 | } else { |
| 5022 | new_umask = umask(0); |
| 5023 | printf("%.3o\n", (unsigned) new_umask); |
| 5024 | } |
| 5025 | umask(new_umask); |
| 5026 | return EXIT_SUCCESS; |
| 5027 | } |
| 5028 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame^] | 5029 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5030 | static int builtin_unset(char **argv) |
| 5031 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame^] | 5032 | size_t i; |
| 5033 | int ret; |
| 5034 | bool var = true; |
| 5035 | |
| 5036 | if (!argv[1]) |
| 5037 | return EXIT_SUCCESS; |
| 5038 | |
| 5039 | i = 0; |
| 5040 | if (argv[1][0] == '-') { |
| 5041 | switch (argv[1][1]) { |
| 5042 | case 'v': break; |
| 5043 | case 'f': if (ENABLE_HUSH_FUNCTIONS) { var = false; break; } |
| 5044 | default: |
| 5045 | bb_error_msg("unset: %s: invalid option", argv[1]); |
| 5046 | return EXIT_FAILURE; |
| 5047 | } |
| 5048 | ++i; |
| 5049 | } |
| 5050 | |
| 5051 | ret = EXIT_SUCCESS; |
| 5052 | while (argv[++i]) { |
| 5053 | if (var) { |
| 5054 | if (unset_local_var(argv[i])) |
| 5055 | ret = EXIT_FAILURE; |
| 5056 | } |
| 5057 | #if ENABLE_HUSH_FUNCTIONS |
| 5058 | else |
| 5059 | unset_local_func(argv[i]); |
| 5060 | #endif |
| 5061 | } |
| 5062 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5063 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5064 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5065 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
| 5066 | static int builtin_wait(char **argv) |
| 5067 | { |
| 5068 | int ret = EXIT_SUCCESS; |
| 5069 | int status; |
| 5070 | |
| 5071 | if (argv[1] == NULL) |
| 5072 | /* don't care about exit status */ |
| 5073 | wait(&status); |
| 5074 | |
| 5075 | while (argv[1]) { |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 5076 | pid_t pid = bb_strtou(argv[1], NULL, 10); |
| 5077 | if (errno) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5078 | bb_perror_msg("wait %s", argv[1]); |
| 5079 | return EXIT_FAILURE; |
| 5080 | } else if (waitpid(pid, &status, 0) == pid) { |
| 5081 | if (WIFSIGNALED(status)) |
| 5082 | ret = 128 + WTERMSIG(status); |
| 5083 | else if (WIFEXITED(status)) |
| 5084 | ret = WEXITSTATUS(status); |
| 5085 | else |
| 5086 | ret = EXIT_FAILURE; |
| 5087 | } else { |
| 5088 | bb_perror_msg("wait %s", argv[1]); |
| 5089 | ret = 127; |
| 5090 | } |
| 5091 | ++argv; |
| 5092 | } |
| 5093 | |
| 5094 | return ret; |
| 5095 | } |
| 5096 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 5097 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5098 | static int builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5099 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5100 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 5101 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 5102 | return EXIT_SUCCESS; /* bash compat */ |
| 5103 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5104 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
| 5105 | G.depth_break_continue = 1; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5106 | if (argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5107 | G.depth_break_continue = bb_strtou(argv[1], NULL, 10); |
| 5108 | if (errno || !G.depth_break_continue || argv[2]) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 5109 | bb_error_msg("%s: bad arguments", argv[0]); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5110 | G.flag_break_continue = BC_BREAK; |
| 5111 | G.depth_break_continue = UINT_MAX; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5112 | } |
| 5113 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5114 | if (G.depth_of_loop < G.depth_break_continue) |
| 5115 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5116 | return EXIT_SUCCESS; |
| 5117 | } |
| 5118 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5119 | static int builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5120 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 5121 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 5122 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5123 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 5124 | #endif |