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; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 463 | //// smallint ctrl_z_flag; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 464 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 465 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 466 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 467 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 468 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 469 | smallint fake_mode; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 470 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 471 | smalluint last_return_code; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 472 | /* is global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 473 | smalluint global_args_malloced; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 474 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 475 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 476 | char **global_argv; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 477 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 478 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 479 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 480 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 481 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 482 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 483 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 484 | struct variable shell_ver; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 485 | #if ENABLE_FEATURE_SH_STANDALONE |
| 486 | struct nofork_save_area nofork_save; |
| 487 | #endif |
| 488 | #if ENABLE_HUSH_JOB |
| 489 | sigjmp_buf toplevel_jb; |
| 490 | #endif |
| 491 | unsigned char charmap[256]; |
| 492 | char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2]; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 493 | /* Signal and trap handling */ |
| 494 | char **traps; /* char *traps[NSIG] */ |
| 495 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 496 | unsigned non_DFL_mask; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 497 | sigset_t blocked_set; |
| 498 | sigset_t inherited_set; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 499 | }; |
| 500 | |
| 501 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 502 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 503 | * (too many defines). Also, I actually prefer to see when a variable |
| 504 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 505 | #define INIT_G() do { \ |
| 506 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 507 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 508 | |
| 509 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 510 | /* Function prototypes for builtins */ |
| 511 | static int builtin_cd(char **argv); |
| 512 | static int builtin_echo(char **argv); |
| 513 | static int builtin_eval(char **argv); |
| 514 | static int builtin_exec(char **argv); |
| 515 | static int builtin_exit(char **argv); |
| 516 | static int builtin_export(char **argv); |
| 517 | #if ENABLE_HUSH_JOB |
| 518 | static int builtin_fg_bg(char **argv); |
| 519 | static int builtin_jobs(char **argv); |
| 520 | #endif |
| 521 | #if ENABLE_HUSH_HELP |
| 522 | static int builtin_help(char **argv); |
| 523 | #endif |
| 524 | static int builtin_pwd(char **argv); |
| 525 | static int builtin_read(char **argv); |
| 526 | static int builtin_test(char **argv); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 527 | static int builtin_trap(char **argv); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 528 | static int builtin_true(char **argv); |
| 529 | static int builtin_set(char **argv); |
| 530 | static int builtin_shift(char **argv); |
| 531 | static int builtin_source(char **argv); |
| 532 | static int builtin_umask(char **argv); |
| 533 | static int builtin_unset(char **argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 534 | static int builtin_wait(char **argv); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 535 | #if ENABLE_HUSH_LOOPS |
| 536 | static int builtin_break(char **argv); |
| 537 | static int builtin_continue(char **argv); |
| 538 | #endif |
| 539 | //static int builtin_not_written(char **argv); |
| 540 | |
| 541 | /* Table of built-in functions. They can be forked or not, depending on |
| 542 | * context: within pipes, they fork. As simple commands, they do not. |
| 543 | * When used in non-forking context, they can change global variables |
| 544 | * in the parent shell process. If forked, of course they cannot. |
| 545 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 546 | * still be set at the end. */ |
| 547 | struct built_in_command { |
| 548 | const char *cmd; |
| 549 | int (*function)(char **argv); |
| 550 | #if ENABLE_HUSH_HELP |
| 551 | const char *descr; |
| 552 | #define BLTIN(cmd, func, help) { cmd, func, help } |
| 553 | #else |
| 554 | #define BLTIN(cmd, func, help) { cmd, func } |
| 555 | #endif |
| 556 | }; |
| 557 | |
| 558 | /* For now, echo and test are unconditionally enabled. |
| 559 | * Maybe make it configurable? */ |
| 560 | static const struct built_in_command bltins[] = { |
| 561 | BLTIN("." , builtin_source, "Run commands in a file"), |
| 562 | BLTIN(":" , builtin_true, "No-op"), |
| 563 | BLTIN("[" , builtin_test, "Test condition"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 564 | #if ENABLE_HUSH_JOB |
| 565 | BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"), |
| 566 | #endif |
| 567 | #if ENABLE_HUSH_LOOPS |
| 568 | BLTIN("break" , builtin_break, "Exit from a loop"), |
| 569 | #endif |
| 570 | BLTIN("cd" , builtin_cd, "Change directory"), |
| 571 | #if ENABLE_HUSH_LOOPS |
| 572 | BLTIN("continue", builtin_continue, "Start new loop iteration"), |
| 573 | #endif |
| 574 | BLTIN("echo" , builtin_echo, "Write to stdout"), |
| 575 | BLTIN("eval" , builtin_eval, "Construct and run shell command"), |
| 576 | BLTIN("exec" , builtin_exec, "Execute command, don't return to shell"), |
| 577 | BLTIN("exit" , builtin_exit, "Exit"), |
| 578 | BLTIN("export", builtin_export, "Set environment variable"), |
| 579 | #if ENABLE_HUSH_JOB |
| 580 | BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"), |
| 581 | BLTIN("jobs" , builtin_jobs, "List active jobs"), |
| 582 | #endif |
| 583 | BLTIN("pwd" , builtin_pwd, "Print current directory"), |
| 584 | BLTIN("read" , builtin_read, "Input environment variable"), |
| 585 | // BLTIN("return", builtin_not_written, "Return from a function"), |
| 586 | BLTIN("set" , builtin_set, "Set/unset shell local variables"), |
| 587 | BLTIN("shift" , builtin_shift, "Shift positional parameters"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 588 | BLTIN("test" , builtin_test, "Test condition"), |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 589 | BLTIN("trap" , builtin_trap, "Trap signals"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 590 | // BLTIN("ulimit", builtin_not_written, "Control resource limits"), |
| 591 | BLTIN("umask" , builtin_umask, "Set file creation mask"), |
| 592 | BLTIN("unset" , builtin_unset, "Unset environment variable"), |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 593 | BLTIN("wait" , builtin_wait, "Wait for process"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 594 | #if ENABLE_HUSH_HELP |
| 595 | BLTIN("help" , builtin_help, "List shell built-in commands"), |
| 596 | #endif |
| 597 | }; |
| 598 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 599 | |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 600 | /* Normal */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 601 | static void maybe_die(const char *notice, const char *msg) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 602 | { |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 603 | /* Was using fancy stuff: |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 604 | * (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...) |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 605 | * but it SEGVs. ?! Oh well... explicit temp ptr works around that */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 606 | void FAST_FUNC (*fp)(const char *s, ...) = bb_error_msg_and_die; |
| 607 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 608 | fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 609 | #endif |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 610 | fp(msg ? "%s: %s" : notice, notice, msg); |
| 611 | } |
Mike Frysinger | 5a82845 | 2009-03-28 19:09:04 +0000 | [diff] [blame] | 612 | #if 1 |
| 613 | #define syntax(msg) maybe_die("syntax error", msg); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 614 | #else |
Mike Frysinger | 5a82845 | 2009-03-28 19:09:04 +0000 | [diff] [blame] | 615 | /* Debug -- trick gcc to expand __LINE__ and convert to string */ |
| 616 | #define __syntax(msg, line) maybe_die("syntax error hush.c:" # line, msg) |
| 617 | #define _syntax(msg, line) __syntax(msg, line) |
| 618 | #define syntax(msg) _syntax(msg, __LINE__) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 619 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 620 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 621 | static int glob_needed(const char *s) |
| 622 | { |
| 623 | while (*s) { |
| 624 | if (*s == '\\') |
| 625 | s++; |
| 626 | if (*s == '*' || *s == '[' || *s == '?') |
| 627 | return 1; |
| 628 | s++; |
| 629 | } |
| 630 | return 0; |
| 631 | } |
| 632 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 633 | static int is_assignment(const char *s) |
| 634 | { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 635 | if (!s || !(isalpha(*s) || *s == '_')) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 636 | return 0; |
| 637 | s++; |
| 638 | while (isalnum(*s) || *s == '_') |
| 639 | s++; |
| 640 | return *s == '='; |
| 641 | } |
| 642 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 643 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 644 | static char *unbackslash(char *src) |
| 645 | { |
| 646 | char *dst = src; |
| 647 | while (1) { |
| 648 | if (*src == '\\') |
| 649 | src++; |
| 650 | if ((*dst++ = *src++) == '\0') |
| 651 | break; |
| 652 | } |
| 653 | return dst; |
| 654 | } |
| 655 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 656 | 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] | 657 | { |
| 658 | int i; |
| 659 | unsigned count1; |
| 660 | unsigned count2; |
| 661 | char **v; |
| 662 | |
| 663 | v = strings; |
| 664 | count1 = 0; |
| 665 | if (v) { |
| 666 | while (*v) { |
| 667 | count1++; |
| 668 | v++; |
| 669 | } |
| 670 | } |
| 671 | count2 = 0; |
| 672 | v = add; |
| 673 | while (*v) { |
| 674 | count2++; |
| 675 | v++; |
| 676 | } |
| 677 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 678 | v[count1 + count2] = NULL; |
| 679 | i = count2; |
| 680 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 681 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 682 | return v; |
| 683 | } |
| 684 | |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 685 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 686 | { |
| 687 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 688 | v[0] = add; |
| 689 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 690 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 691 | } |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 692 | |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 693 | static void putenv_all(char **strings) |
| 694 | { |
| 695 | if (!strings) |
| 696 | return; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 697 | while (*strings) { |
| 698 | debug_printf_env("putenv '%s'\n", *strings); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 699 | putenv(*strings++); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 700 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | static char **putenv_all_and_save_old(char **strings) |
| 704 | { |
| 705 | char **old = NULL; |
| 706 | char **s = strings; |
| 707 | |
| 708 | if (!strings) |
| 709 | return old; |
| 710 | while (*strings) { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 711 | char *v, *eq; |
| 712 | |
| 713 | eq = strchr(*strings, '='); |
| 714 | if (eq) { |
| 715 | *eq = '\0'; |
| 716 | v = getenv(*strings); |
| 717 | *eq = '='; |
| 718 | if (v) { |
| 719 | /* v points to VAL in VAR=VAL, go back to VAR */ |
| 720 | v -= (eq - *strings) + 1; |
| 721 | old = add_string_to_strings(old, v); |
| 722 | } |
| 723 | } |
| 724 | strings++; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 725 | } |
| 726 | putenv_all(s); |
| 727 | return old; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 730 | static void free_strings_and_unsetenv(char **strings, int unset) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 731 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 732 | char **v; |
| 733 | |
| 734 | if (!strings) |
| 735 | return; |
| 736 | |
| 737 | v = strings; |
| 738 | while (*v) { |
| 739 | if (unset) { |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 740 | debug_printf_env("unsetenv '%s'\n", *v); |
| 741 | bb_unsetenv(*v); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 742 | } |
| 743 | free(*v++); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 744 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 745 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 748 | static void free_strings(char **strings) |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 749 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 750 | free_strings_and_unsetenv(strings, 0); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 751 | } |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 752 | |
| 753 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 754 | /* Basic theory of signal handling in shell |
| 755 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 756 | * This does not describe what hush does, rather, it is current understanding |
| 757 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 758 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 759 | * |
| 760 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 761 | * is finished or backgrounded. It is the same in interactive and |
| 762 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 763 | * a user trap handler is installed or a shell special one is in effect. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 764 | * ^C or ^Z from keyboard seem to execute "at once" because it usually |
| 765 | * backgrounds (i.e. stops) or kills all members of currently running |
| 766 | * pipe. |
| 767 | * |
| 768 | * Wait builtin in interruptible by signals for which user trap is set |
| 769 | * or by SIGINT in interactive shell. |
| 770 | * |
| 771 | * Trap handlers will execute even within trap handlers. (right?) |
| 772 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 773 | * User trap handlers are forgotten when subshell ("(cmd)") is entered. [TODO] |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 774 | * |
| 775 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 776 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 777 | * |
| 778 | * Commands run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 779 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 780 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 781 | * Ordinary commands have signals set to SIG_IGN/DFL set as inherited |
| 782 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 783 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 784 | * Siganls which differ from SIG_DFL action |
| 785 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 786 | * |
| 787 | * SIGQUIT: ignore |
| 788 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 789 | * SIGHUP (interactive): |
| 790 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 791 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 792 | * (note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 793 | * that all pipe members are stopped) (right?) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 794 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 795 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 796 | * to interactive shell while shell is waiting for a pipe, |
| 797 | * since shell is bg'ed (is not in foreground process group). |
| 798 | * (check/expand this) |
| 799 | * Example 1: this waits 5 sec, but does not execute ls: |
| 800 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 801 | * Example 2: this does not wait and does not execute ls: |
| 802 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 803 | * Example 3: this does not wait 5 sec, but executes ls: |
| 804 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 805 | * |
| 806 | * (What happens to signals which are IGN on shell start?) |
| 807 | * (What happens with signal mask on shell start?) |
| 808 | * |
| 809 | * Implementation in hush |
| 810 | * ====================== |
| 811 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 812 | * We block all signals which we don't want to take action immediately, |
| 813 | * i.e. we block all signals which need to have special handling as described |
| 814 | * above, and all signals which have traps set. |
| 815 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 816 | * and act on them. |
| 817 | * |
| 818 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 819 | * sigset_t blocked_set: current blocked signal set |
| 820 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 821 | * "trap - SIGxxx": |
| 822 | * clear bit in blocked_set unless it is also in non_DFL |
| 823 | * "trap 'cmd' SIGxxx": |
| 824 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 825 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 826 | * nothing for {} child shell (say, "true | { true; true; } | true") |
| 827 | * unset all traps if () shell. [TODO] |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 828 | * after [v]fork, if we plan to exec: |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 829 | * POSIX says pending signal mask is cleared in child - no need to clear it. |
| 830 | * Restore blocked signal set to one inherited by shell just prior to exec. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 831 | * |
| 832 | * Note: as a result, we do not use signal handlers much. The only use |
| 833 | * is to restore terminal pgrp on exit. |
| 834 | * |
| 835 | * TODO: check/fix wait builtin to be interruptible. |
| 836 | */ |
| 837 | |
| 838 | /* called once at shell init */ |
| 839 | static void init_signal_mask(void) |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 840 | { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 841 | unsigned sig; |
| 842 | unsigned mask = (1 << SIGQUIT); |
| 843 | #if ENABLE_HUSH_INTERACTIVE |
| 844 | if (G.interactive_fd) { |
| 845 | mask = 0 |
| 846 | | (1 << SIGQUIT) |
| 847 | | (1 << SIGTERM) |
| 848 | | (1 << SIGHUP) |
| 849 | #if ENABLE_HUSH_JOB |
| 850 | | (1 << SIGTTIN) | (1 << SIGTTOU) | (1 << SIGTSTP) |
| 851 | #endif |
| 852 | | (1 << SIGINT) |
| 853 | ; |
| 854 | } |
| 855 | #endif |
| 856 | G.non_DFL_mask = mask; |
| 857 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 858 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 859 | sig = 0; |
| 860 | while (mask) { |
| 861 | if (mask & 1) |
| 862 | sigaddset(&G.blocked_set, sig); |
| 863 | mask >>= 1; |
| 864 | sig++; |
| 865 | } |
| 866 | sigprocmask(SIG_SETMASK, &G.blocked_set, &G.inherited_set); |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 867 | } |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 868 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 869 | static void check_and_run_traps(void) |
| 870 | { |
| 871 | static const struct timespec zero_ts = { 0, 0 }; |
| 872 | smalluint save_rcode; |
| 873 | int sig; |
| 874 | |
| 875 | while (1) { |
| 876 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_ts); |
| 877 | if (sig <= 0) |
| 878 | break; |
| 879 | |
| 880 | if (G.traps && G.traps[sig]) { |
| 881 | if (G.traps[sig][0]) { |
| 882 | /* We have user-defined handler */ |
| 883 | char *argv[] = { NULL, xstrdup(G.traps[sig]), NULL }; |
| 884 | save_rcode = G.last_return_code; |
| 885 | builtin_eval(argv); |
| 886 | free(argv[1]); |
| 887 | G.last_return_code = save_rcode; |
| 888 | } /* else: "" trap, ignoring signal */ |
| 889 | continue; |
| 890 | } |
| 891 | /* not a trap: special action */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 892 | switch (sig) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 893 | case SIGINT: |
| 894 | G.flag_SIGINT = 1; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 895 | break; |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 896 | //TODO |
| 897 | // case SIGHUP: ... |
| 898 | // break; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 899 | default: /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 900 | break; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 901 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 902 | } |
| 903 | } |
| 904 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 905 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 906 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 907 | /* Restores tty foreground process group, and exits. |
| 908 | * May be called as signal handler for fatal signal |
| 909 | * (will faithfully resend signal to itself, producing correct exit state) |
| 910 | * or called directly with -EXITCODE. |
| 911 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 912 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 913 | static void sigexit(int sig) |
| 914 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 915 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 916 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 917 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 918 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 919 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 920 | * tty pgrp then, only top-level shell process does that */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 921 | if (G.interactive_fd && getpid() == G.root_pid) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 922 | tcsetpgrp(G.interactive_fd, G.saved_tty_pgrp); |
| 923 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 924 | |
| 925 | /* Not a signal, just exit */ |
| 926 | if (sig <= 0) |
| 927 | _exit(- sig); |
| 928 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 929 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 932 | /* helper */ |
| 933 | static void maybe_set_sighandler(int sig) |
| 934 | { |
| 935 | void (*handler)(int); |
| 936 | /* non_DFL_mask'ed signals are, well, masked, |
| 937 | * no need to set handler for them. |
| 938 | */ |
| 939 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 940 | handler = signal(sig, sigexit); |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 941 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 942 | signal(sig, handler); |
| 943 | } |
| 944 | } |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame^] | 945 | /* Used only to set handler to restore pgrp on exit */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 946 | static void set_fatal_signals_to_sigexit(void) |
| 947 | { |
| 948 | if (HUSH_DEBUG) { |
| 949 | maybe_set_sighandler(SIGILL ); |
| 950 | maybe_set_sighandler(SIGFPE ); |
| 951 | maybe_set_sighandler(SIGBUS ); |
| 952 | maybe_set_sighandler(SIGSEGV); |
| 953 | maybe_set_sighandler(SIGTRAP); |
| 954 | } /* else: hush is perfect. what SEGV? */ |
| 955 | |
| 956 | maybe_set_sighandler(SIGABRT); |
| 957 | |
| 958 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 959 | maybe_set_sighandler(SIGPIPE); |
| 960 | maybe_set_sighandler(SIGALRM); |
| 961 | maybe_set_sighandler(SIGHUP ); |
| 962 | |
| 963 | /* if we aren't interactive... but in this case |
| 964 | * we never want to restore pgrp on exit, and this fn is not called */ |
| 965 | /*maybe_set_sighandler(SIGTERM);*/ |
| 966 | /*maybe_set_sighandler(SIGINT );*/ |
| 967 | } |
| 968 | /* Used only to suppress ^Z in `cmd` */ |
| 969 | static void set_jobctrl_signals_to_IGN(void) |
| 970 | { |
| 971 | bb_signals(0 |
| 972 | + (1 << SIGTSTP) |
| 973 | + (1 << SIGTTIN) |
| 974 | + (1 << SIGTTOU) |
| 975 | , SIG_IGN); |
| 976 | } |
| 977 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 978 | #else /* !JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 979 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 980 | #define set_fatal_signals_to_sigexit(handler) ((void)0) |
| 981 | #define set_jobctrl_signals_to_IGN(handler) ((void)0) |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 982 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 983 | #endif /* JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 984 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 985 | /* Restores tty foreground process group, and exits. */ |
| 986 | static void hush_exit(int exitcode) NORETURN; |
| 987 | static void hush_exit(int exitcode) |
| 988 | { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 989 | if (G.traps && G.traps[0] && G.traps[0][0]) { |
| 990 | char *argv[] = { NULL, xstrdup(G.traps[0]), NULL }; |
| 991 | builtin_eval(argv); |
| 992 | free(argv[1]); |
| 993 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 994 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 995 | #if ENABLE_HUSH_JOB |
| 996 | fflush(NULL); /* flush all streams */ |
| 997 | sigexit(- (exitcode & 0xff)); |
| 998 | #else |
| 999 | exit(exitcode); |
| 1000 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1003 | |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1004 | static const char *set_cwd(void) |
| 1005 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1006 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1007 | * we must not try to free(bb_msg_unknown) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1008 | if (G.cwd == bb_msg_unknown) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1009 | G.cwd = NULL; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1010 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1011 | if (!G.cwd) |
| 1012 | G.cwd = bb_msg_unknown; |
| 1013 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1016 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1017 | /* Get/check local shell variables */ |
| 1018 | static struct variable *get_local_var(const char *name) |
| 1019 | { |
| 1020 | struct variable *cur; |
| 1021 | int len; |
| 1022 | |
| 1023 | if (!name) |
| 1024 | return NULL; |
| 1025 | len = strlen(name); |
| 1026 | for (cur = G.top_var; cur; cur = cur->next) { |
| 1027 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
| 1028 | return cur; |
| 1029 | } |
| 1030 | return NULL; |
| 1031 | } |
| 1032 | |
| 1033 | /* Basically useful version until someone wants to get fancier, |
| 1034 | * see the bash man page under "Parameter Expansion" */ |
| 1035 | static const char *lookup_param(const char *src) |
| 1036 | { |
| 1037 | struct variable *var = get_local_var(src); |
| 1038 | if (var) |
| 1039 | return strchr(var->varstr, '=') + 1; |
| 1040 | return NULL; |
| 1041 | } |
| 1042 | |
| 1043 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1044 | * We take ownership of it. |
| 1045 | * flg_export is used by: |
| 1046 | * 0: do not export |
| 1047 | * 1: export |
| 1048 | * -1: if NAME is set, leave export status alone |
| 1049 | * if NAME is not set, do not export |
| 1050 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1051 | static int set_local_var(char *str, int flg_export) |
| 1052 | { |
| 1053 | struct variable *cur; |
| 1054 | char *value; |
| 1055 | int name_len; |
| 1056 | |
| 1057 | value = strchr(str, '='); |
| 1058 | if (!value) { /* not expected to ever happen? */ |
| 1059 | free(str); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | |
| 1063 | name_len = value - str + 1; /* including '=' */ |
| 1064 | cur = G.top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */ |
| 1065 | while (1) { |
| 1066 | if (strncmp(cur->varstr, str, name_len) != 0) { |
| 1067 | if (!cur->next) { |
| 1068 | /* Bail out. Note that now cur points |
| 1069 | * to last var in linked list */ |
| 1070 | break; |
| 1071 | } |
| 1072 | cur = cur->next; |
| 1073 | continue; |
| 1074 | } |
| 1075 | /* We found an existing var with this name */ |
| 1076 | *value = '\0'; |
| 1077 | if (cur->flg_read_only) { |
| 1078 | bb_error_msg("%s: readonly variable", str); |
| 1079 | free(str); |
| 1080 | return -1; |
| 1081 | } |
| 1082 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1083 | unsetenv(str); /* just in case */ |
| 1084 | *value = '='; |
| 1085 | if (strcmp(cur->varstr, str) == 0) { |
| 1086 | free_and_exp: |
| 1087 | free(str); |
| 1088 | goto exp; |
| 1089 | } |
| 1090 | if (cur->max_len >= strlen(str)) { |
| 1091 | /* This one is from startup env, reuse space */ |
| 1092 | strcpy(cur->varstr, str); |
| 1093 | goto free_and_exp; |
| 1094 | } |
| 1095 | /* max_len == 0 signifies "malloced" var, which we can |
| 1096 | * (and has to) free */ |
| 1097 | if (!cur->max_len) |
| 1098 | free(cur->varstr); |
| 1099 | cur->max_len = 0; |
| 1100 | goto set_str_and_exp; |
| 1101 | } |
| 1102 | |
| 1103 | /* Not found - create next variable struct */ |
| 1104 | cur->next = xzalloc(sizeof(*cur)); |
| 1105 | cur = cur->next; |
| 1106 | |
| 1107 | set_str_and_exp: |
| 1108 | cur->varstr = str; |
| 1109 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1110 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1111 | cur->flg_export = 1; |
| 1112 | if (cur->flg_export) { |
| 1113 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1114 | return putenv(cur->varstr); |
| 1115 | } |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1119 | static int unset_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1120 | { |
| 1121 | struct variable *cur; |
| 1122 | struct variable *prev = prev; /* for gcc */ |
| 1123 | int name_len; |
| 1124 | |
| 1125 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1126 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1127 | name_len = strlen(name); |
| 1128 | cur = G.top_var; |
| 1129 | while (cur) { |
| 1130 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1131 | if (cur->flg_read_only) { |
| 1132 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1133 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1134 | } |
| 1135 | /* prev is ok to use here because 1st variable, HUSH_VERSION, |
| 1136 | * is ro, and we cannot reach this code on the 1st pass */ |
| 1137 | prev->next = cur->next; |
| 1138 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1139 | bb_unsetenv(cur->varstr); |
| 1140 | if (!cur->max_len) |
| 1141 | free(cur->varstr); |
| 1142 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1143 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1144 | } |
| 1145 | prev = cur; |
| 1146 | cur = cur->next; |
| 1147 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1148 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | |
| 1152 | /* |
| 1153 | * in_str support |
| 1154 | */ |
| 1155 | static int static_get(struct in_str *i) |
| 1156 | { |
| 1157 | int ch = *i->p++; |
| 1158 | if (ch == '\0') return EOF; |
| 1159 | return ch; |
| 1160 | } |
| 1161 | |
| 1162 | static int static_peek(struct in_str *i) |
| 1163 | { |
| 1164 | return *i->p; |
| 1165 | } |
| 1166 | |
| 1167 | #if ENABLE_HUSH_INTERACTIVE |
| 1168 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1169 | static void cmdedit_set_initial_prompt(void) |
| 1170 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1171 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1172 | G.PS1 = getenv("PS1"); |
| 1173 | if (G.PS1 == NULL) |
| 1174 | G.PS1 = "\\w \\$ "; |
| 1175 | } else |
| 1176 | G.PS1 = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1177 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1178 | |
| 1179 | static const char* setup_prompt_string(int promptmode) |
| 1180 | { |
| 1181 | const char *prompt_str; |
| 1182 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1183 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1184 | /* Set up the prompt */ |
| 1185 | if (promptmode == 0) { /* PS1 */ |
| 1186 | free((char*)G.PS1); |
| 1187 | G.PS1 = xasprintf("%s %c ", G.cwd, (geteuid() != 0) ? '$' : '#'); |
| 1188 | prompt_str = G.PS1; |
| 1189 | } else |
| 1190 | prompt_str = G.PS2; |
| 1191 | } else |
| 1192 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1193 | debug_printf("result '%s'\n", prompt_str); |
| 1194 | return prompt_str; |
| 1195 | } |
| 1196 | |
| 1197 | static void get_user_input(struct in_str *i) |
| 1198 | { |
| 1199 | int r; |
| 1200 | const char *prompt_str; |
| 1201 | |
| 1202 | prompt_str = setup_prompt_string(i->promptmode); |
| 1203 | #if ENABLE_FEATURE_EDITING |
| 1204 | /* Enable command line editing only while a command line |
| 1205 | * is actually being read */ |
| 1206 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1207 | G.flag_SIGINT = 0; |
| 1208 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1209 | * only after <Enter>. (^C will work) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1210 | r = read_line_input(prompt_str, G.user_input_buf, BUFSIZ-1, G.line_input_state); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1211 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
| 1212 | check_and_run_traps(); |
| 1213 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1214 | i->eof_flag = (r < 0); |
| 1215 | if (i->eof_flag) { /* EOF/error detected */ |
| 1216 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1217 | G.user_input_buf[1] = '\0'; |
| 1218 | } |
| 1219 | #else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1220 | do { |
| 1221 | G.flag_SIGINT = 0; |
| 1222 | fputs(prompt_str, stdout); |
| 1223 | fflush(stdout); |
| 1224 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1225 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
| 1226 | //do we need check_and_run_traps()? (maybe only if stdin) |
| 1227 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1228 | i->eof_flag = (r == EOF); |
| 1229 | #endif |
| 1230 | i->p = G.user_input_buf; |
| 1231 | } |
| 1232 | |
| 1233 | #endif /* INTERACTIVE */ |
| 1234 | |
| 1235 | /* This is the magic location that prints prompts |
| 1236 | * and gets data back from the user */ |
| 1237 | static int file_get(struct in_str *i) |
| 1238 | { |
| 1239 | int ch; |
| 1240 | |
| 1241 | /* If there is data waiting, eat it up */ |
| 1242 | if (i->p && *i->p) { |
| 1243 | #if ENABLE_HUSH_INTERACTIVE |
| 1244 | take_cached: |
| 1245 | #endif |
| 1246 | ch = *i->p++; |
| 1247 | if (i->eof_flag && !*i->p) |
| 1248 | ch = EOF; |
| 1249 | } else { |
| 1250 | /* need to double check i->file because we might be doing something |
| 1251 | * more complicated by now, like sourcing or substituting. */ |
| 1252 | #if ENABLE_HUSH_INTERACTIVE |
| 1253 | if (G.interactive_fd && i->promptme && i->file == stdin) { |
| 1254 | do { |
| 1255 | get_user_input(i); |
| 1256 | } while (!*i->p); /* need non-empty line */ |
| 1257 | i->promptmode = 1; /* PS2 */ |
| 1258 | i->promptme = 0; |
| 1259 | goto take_cached; |
| 1260 | } |
| 1261 | #endif |
| 1262 | ch = fgetc(i->file); |
| 1263 | } |
| 1264 | debug_printf("file_get: got a '%c' %d\n", ch, ch); |
| 1265 | #if ENABLE_HUSH_INTERACTIVE |
| 1266 | if (ch == '\n') |
| 1267 | i->promptme = 1; |
| 1268 | #endif |
| 1269 | return ch; |
| 1270 | } |
| 1271 | |
| 1272 | /* All the callers guarantee this routine will never be |
| 1273 | * used right after a newline, so prompting is not needed. |
| 1274 | */ |
| 1275 | static int file_peek(struct in_str *i) |
| 1276 | { |
| 1277 | int ch; |
| 1278 | if (i->p && *i->p) { |
| 1279 | if (i->eof_flag && !i->p[1]) |
| 1280 | return EOF; |
| 1281 | return *i->p; |
| 1282 | } |
| 1283 | ch = fgetc(i->file); |
| 1284 | i->eof_flag = (ch == EOF); |
| 1285 | i->peek_buf[0] = ch; |
| 1286 | i->peek_buf[1] = '\0'; |
| 1287 | i->p = i->peek_buf; |
| 1288 | debug_printf("file_peek: got a '%c' %d\n", *i->p, *i->p); |
| 1289 | return ch; |
| 1290 | } |
| 1291 | |
| 1292 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1293 | { |
| 1294 | i->peek = file_peek; |
| 1295 | i->get = file_get; |
| 1296 | #if ENABLE_HUSH_INTERACTIVE |
| 1297 | i->promptme = 1; |
| 1298 | i->promptmode = 0; /* PS1 */ |
| 1299 | #endif |
| 1300 | i->file = f; |
| 1301 | i->p = NULL; |
| 1302 | } |
| 1303 | |
| 1304 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1305 | { |
| 1306 | i->peek = static_peek; |
| 1307 | i->get = static_get; |
| 1308 | #if ENABLE_HUSH_INTERACTIVE |
| 1309 | i->promptme = 1; |
| 1310 | i->promptmode = 0; /* PS1 */ |
| 1311 | #endif |
| 1312 | i->p = s; |
| 1313 | i->eof_flag = 0; |
| 1314 | } |
| 1315 | |
| 1316 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1317 | /* |
| 1318 | * o_string support |
| 1319 | */ |
| 1320 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1321 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1322 | static void o_reset(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1323 | { |
| 1324 | o->length = 0; |
| 1325 | o->nonnull = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1326 | if (o->data) |
| 1327 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1330 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1331 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1332 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1333 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1336 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1337 | { |
| 1338 | if (o->length + len > o->maxlen) { |
| 1339 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 1340 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 1341 | } |
| 1342 | } |
| 1343 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1344 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1345 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1346 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1347 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1348 | o->data[o->length] = ch; |
| 1349 | o->length++; |
| 1350 | o->data[o->length] = '\0'; |
| 1351 | } |
| 1352 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1353 | static void o_addstr(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1354 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1355 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1356 | memcpy(&o->data[o->length], str, len); |
| 1357 | o->length += len; |
| 1358 | o->data[o->length] = '\0'; |
| 1359 | } |
| 1360 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1361 | static void o_addstr_duplicate_backslash(o_string *o, const char *str, int len) |
| 1362 | { |
| 1363 | while (len) { |
| 1364 | o_addchr(o, *str); |
| 1365 | if (*str++ == '\\' |
| 1366 | && (*str != '*' && *str != '?' && *str != '[') |
| 1367 | ) { |
| 1368 | o_addchr(o, '\\'); |
| 1369 | } |
| 1370 | len--; |
| 1371 | } |
| 1372 | } |
| 1373 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1374 | /* My analysis of quoting semantics tells me that state information |
| 1375 | * is associated with a destination, not a source. |
| 1376 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1377 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1378 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1379 | int sz = 1; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1380 | char *found = strchr("*?[\\", ch); |
| 1381 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1382 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1383 | o_grow_by(o, sz); |
| 1384 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1385 | o->data[o->length] = '\\'; |
| 1386 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1387 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1388 | o->data[o->length] = ch; |
| 1389 | o->length++; |
| 1390 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1393 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1394 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1395 | int sz = 1; |
| 1396 | if (o->o_quote && strchr("*?[\\", ch)) { |
| 1397 | sz++; |
| 1398 | o->data[o->length] = '\\'; |
| 1399 | o->length++; |
| 1400 | } |
| 1401 | o_grow_by(o, sz); |
| 1402 | o->data[o->length] = ch; |
| 1403 | o->length++; |
| 1404 | o->data[o->length] = '\0'; |
| 1405 | } |
| 1406 | |
| 1407 | static void o_addQstr(o_string *o, const char *str, int len) |
| 1408 | { |
| 1409 | if (!o->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1410 | o_addstr(o, str, len); |
| 1411 | return; |
| 1412 | } |
| 1413 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1414 | char ch; |
| 1415 | int sz; |
| 1416 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 1417 | if (ordinary_cnt > len) /* paranoia */ |
| 1418 | ordinary_cnt = len; |
| 1419 | o_addstr(o, str, ordinary_cnt); |
| 1420 | if (ordinary_cnt == len) |
| 1421 | return; |
| 1422 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1423 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1424 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1425 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1426 | sz = 1; |
| 1427 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 1428 | sz++; |
| 1429 | o->data[o->length] = '\\'; |
| 1430 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1431 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1432 | o_grow_by(o, sz); |
| 1433 | o->data[o->length] = ch; |
| 1434 | o->length++; |
| 1435 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1436 | } |
| 1437 | } |
| 1438 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1439 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1440 | * 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] | 1441 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1442 | * list[i] contains an INDEX (int!) into this string data. |
| 1443 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1444 | * but list[i]'s need not be modified. |
| 1445 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1446 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1447 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1448 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1449 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1450 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1451 | { |
| 1452 | char **list = (char**)o->data; |
| 1453 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1454 | int i = 0; |
| 1455 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1456 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1457 | while (i < n) { |
| 1458 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1459 | o->data + (int)list[i] + string_start, |
| 1460 | o->data + (int)list[i] + string_start); |
| 1461 | i++; |
| 1462 | } |
| 1463 | if (n) { |
| 1464 | const char *p = o->data + (int)list[n - 1] + string_start; |
Mike Frysinger | d006edb | 2009-03-28 12:43:53 +0000 | [diff] [blame] | 1465 | fprintf(stderr, " total_sz:%ld\n", (p + strlen(p) + 1) - o->data); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1466 | } |
| 1467 | } |
| 1468 | #else |
| 1469 | #define debug_print_list(prefix, o, n) ((void)0) |
| 1470 | #endif |
| 1471 | |
| 1472 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1473 | * in list[n] so that it points past last stored byte so far. |
| 1474 | * It returns n+1. */ |
| 1475 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1476 | { |
| 1477 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1478 | int string_start; |
| 1479 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1480 | |
| 1481 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1482 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1483 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1484 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1485 | 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] | 1486 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1487 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1488 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1489 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1490 | memmove(list + n + 0x10, list + n, string_len); |
| 1491 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1492 | } else |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1493 | 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] | 1494 | } else { |
| 1495 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1496 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1497 | string_len = o->length - string_start; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1498 | 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] | 1499 | o->has_empty_slot = 0; |
| 1500 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1501 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1502 | return n + 1; |
| 1503 | } |
| 1504 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1505 | /* "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] | 1506 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1507 | { |
| 1508 | char **list = (char**)o->data; |
| 1509 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1510 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1511 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1514 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1515 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1516 | static int o_glob(o_string *o, int n) |
| 1517 | { |
| 1518 | glob_t globdata; |
| 1519 | int gr; |
| 1520 | char *pattern; |
| 1521 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1522 | 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] | 1523 | if (!o->data) |
| 1524 | return o_save_ptr_helper(o, n); |
| 1525 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1526 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1527 | if (!glob_needed(pattern)) { |
| 1528 | literal: |
| 1529 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1530 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1531 | return o_save_ptr_helper(o, n); |
| 1532 | } |
| 1533 | |
| 1534 | memset(&globdata, 0, sizeof(globdata)); |
| 1535 | gr = glob(pattern, 0, NULL, &globdata); |
| 1536 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 1537 | if (gr == GLOB_NOSPACE) |
| 1538 | bb_error_msg_and_die("out of memory during glob"); |
| 1539 | if (gr == GLOB_NOMATCH) { |
| 1540 | globfree(&globdata); |
| 1541 | goto literal; |
| 1542 | } |
| 1543 | if (gr != 0) { /* GLOB_ABORTED ? */ |
| 1544 | //TODO: testcase for bad glob pattern behavior |
| 1545 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 1546 | } |
| 1547 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 1548 | char **argv = globdata.gl_pathv; |
| 1549 | o->length = pattern - o->data; /* "forget" pattern */ |
| 1550 | while (1) { |
| 1551 | o_addstr(o, *argv, strlen(*argv) + 1); |
| 1552 | n = o_save_ptr_helper(o, n); |
| 1553 | argv++; |
| 1554 | if (!*argv) |
| 1555 | break; |
| 1556 | } |
| 1557 | } |
| 1558 | globfree(&globdata); |
| 1559 | if (DEBUG_GLOB) |
| 1560 | debug_print_list("o_glob returning", o, n); |
| 1561 | return n; |
| 1562 | } |
| 1563 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1564 | /* If o->o_glob == 1, glob the string so far remembered. |
| 1565 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1566 | static int o_save_ptr(o_string *o, int n) |
| 1567 | { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 1568 | if (o->o_glob) { /* if globbing is requested */ |
| 1569 | /* If o->has_empty_slot, list[n] was already globbed |
| 1570 | * (if it was requested back then when it was filled) |
| 1571 | * so don't do that again! */ |
| 1572 | if (!o->has_empty_slot) |
| 1573 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 1574 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1575 | return o_save_ptr_helper(o, n); |
| 1576 | } |
| 1577 | |
| 1578 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1579 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1580 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1581 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1582 | int string_start; |
| 1583 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1584 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 1585 | if (DEBUG_EXPAND) |
| 1586 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1587 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1588 | list = (char**)o->data; |
| 1589 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1590 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1591 | while (n) { |
| 1592 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1593 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1594 | } |
| 1595 | return list; |
| 1596 | } |
| 1597 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1598 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1599 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 1600 | * all variable references within and returns a pointer to |
| 1601 | * a list of expanded strings, possibly with larger number |
| 1602 | * of strings. (Think VAR="a b"; echo $VAR). |
| 1603 | * This new list is allocated as a single malloc block. |
| 1604 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 1605 | * followed by strings themself. |
| 1606 | * Caller can deallocate entire list by single free(list). */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1607 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1608 | /* Store given string, finalizing the word and starting new one whenever |
| 1609 | * we encounter IFS char(s). This is used for expanding variable values. |
| 1610 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 1611 | 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] | 1612 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1613 | while (1) { |
| 1614 | int word_len = strcspn(str, G.ifs); |
| 1615 | if (word_len) { |
| 1616 | if (output->o_quote || !output->o_glob) |
| 1617 | o_addQstr(output, str, word_len); |
| 1618 | else /* protect backslashes against globbing up :) */ |
| 1619 | o_addstr_duplicate_backslash(output, str, word_len); |
| 1620 | str += word_len; |
| 1621 | } |
| 1622 | if (!*str) /* EOL - do not finalize word */ |
| 1623 | break; |
| 1624 | o_addchr(output, '\0'); |
| 1625 | debug_print_list("expand_on_ifs", output, n); |
| 1626 | n = o_save_ptr(output, n); |
| 1627 | str += strspn(str, G.ifs); /* skip ifs chars */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1628 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1629 | debug_print_list("expand_on_ifs[1]", output, n); |
| 1630 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1633 | #if ENABLE_HUSH_TICK |
| 1634 | static int process_command_subs(o_string *dest, |
| 1635 | struct in_str *input, const char *subst_end); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1636 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1637 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1638 | /* Expand all variable references in given string, adding words to list[] |
| 1639 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 1640 | * to be filled). This routine is extremely tricky: has to deal with |
| 1641 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 1642 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
| 1643 | 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] | 1644 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1645 | /* or_mask is either 0 (normal case) or 0x80 |
| 1646 | * (expansion of right-hand side of assignment == 1-element expand. |
| 1647 | * 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] | 1648 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1649 | char first_ch, ored_ch; |
| 1650 | int i; |
| 1651 | const char *val; |
| 1652 | char *p; |
| 1653 | |
| 1654 | ored_ch = 0; |
| 1655 | |
| 1656 | debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg); |
| 1657 | debug_print_list("expand_vars_to_list", output, n); |
| 1658 | n = o_save_ptr(output, n); |
| 1659 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 1660 | |
| 1661 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 1662 | #if ENABLE_HUSH_TICK |
| 1663 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1664 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1665 | o_addstr(output, arg, p - arg); |
| 1666 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 1667 | arg = ++p; |
| 1668 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 1669 | |
| 1670 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
| 1671 | /* "$@" is special. Even if quoted, it can still |
| 1672 | * expand to nothing (not even an empty string) */ |
| 1673 | if ((first_ch & 0x7f) != '@') |
| 1674 | ored_ch |= first_ch; |
| 1675 | val = NULL; |
| 1676 | switch (first_ch & 0x7f) { |
| 1677 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 1678 | case '$': /* pid */ |
| 1679 | val = utoa(G.root_pid); |
| 1680 | break; |
| 1681 | case '!': /* bg pid */ |
| 1682 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
| 1683 | break; |
| 1684 | case '?': /* exitcode */ |
| 1685 | val = utoa(G.last_return_code); |
| 1686 | break; |
| 1687 | case '#': /* argc */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1688 | if (arg[1] != SPECIAL_VAR_SYMBOL) |
| 1689 | /* actually, it's a ${#var} */ |
| 1690 | goto case_default; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1691 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 1692 | break; |
| 1693 | case '*': |
| 1694 | case '@': |
| 1695 | i = 1; |
| 1696 | if (!G.global_argv[i]) |
| 1697 | break; |
| 1698 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
| 1699 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
| 1700 | smallint sv = output->o_quote; |
| 1701 | /* unquoted var's contents should be globbed, so don't quote */ |
| 1702 | output->o_quote = 0; |
| 1703 | while (G.global_argv[i]) { |
| 1704 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 1705 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 1706 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 1707 | /* this argv[] is not empty and not last: |
| 1708 | * put terminating NUL, start new word */ |
| 1709 | o_addchr(output, '\0'); |
| 1710 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 1711 | n = o_save_ptr(output, n); |
| 1712 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 1713 | } |
| 1714 | } |
| 1715 | output->o_quote = sv; |
| 1716 | } else |
| 1717 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
| 1718 | * and in this case should treat it like '$*' - see 'else...' below */ |
| 1719 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
| 1720 | while (1) { |
| 1721 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 1722 | if (++i >= G.global_argc) |
| 1723 | break; |
| 1724 | o_addchr(output, '\0'); |
| 1725 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 1726 | n = o_save_ptr(output, n); |
| 1727 | } |
| 1728 | } else { /* quoted $*: add as one word */ |
| 1729 | while (1) { |
| 1730 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 1731 | if (!G.global_argv[++i]) |
| 1732 | break; |
| 1733 | if (G.ifs[0]) |
| 1734 | o_addchr(output, G.ifs[0]); |
| 1735 | } |
| 1736 | } |
| 1737 | break; |
| 1738 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 1739 | /* "Empty variable", used to make "" etc to not disappear */ |
| 1740 | arg++; |
| 1741 | ored_ch = 0x80; |
| 1742 | break; |
| 1743 | #if ENABLE_HUSH_TICK |
| 1744 | case '`': { /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
| 1745 | struct in_str input; |
| 1746 | *p = '\0'; |
| 1747 | arg++; |
| 1748 | //TODO: can we just stuff it into "output" directly? |
| 1749 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 1750 | setup_string_in_str(&input, arg); |
| 1751 | process_command_subs(&subst_result, &input, NULL); |
| 1752 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
| 1753 | val = subst_result.data; |
| 1754 | goto store_val; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1755 | } |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1756 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1757 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1758 | case_default: { |
| 1759 | bool exp_len = false, exp_null = false; |
| 1760 | char *var = arg, exp_save, exp_op, *exp_word; |
| 1761 | size_t exp_off = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1762 | *p = '\0'; |
| 1763 | arg[0] = first_ch & 0x7f; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1764 | |
| 1765 | /* prepare for expansions */ |
| 1766 | if (var[0] == '#') { |
| 1767 | /* handle length expansion ${#var} */ |
| 1768 | exp_len = true; |
| 1769 | ++var; |
| 1770 | } else { |
| 1771 | /* maybe handle parameter expansion */ |
| 1772 | exp_off = strcspn(var, ":-=+?"); |
| 1773 | if (!var[exp_off]) |
| 1774 | exp_off = 0; |
| 1775 | if (exp_off) { |
| 1776 | exp_save = var[exp_off]; |
| 1777 | exp_null = exp_save == ':'; |
| 1778 | exp_word = var + exp_off; |
| 1779 | if (exp_null) ++exp_word; |
| 1780 | exp_op = *exp_word++; |
| 1781 | var[exp_off] = '\0'; |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | /* lookup the variable in question */ |
| 1786 | if (isdigit(var[0])) { |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 1787 | /* handle_dollar() should have vetted var for us */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1788 | i = xatoi_u(var); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1789 | if (i < G.global_argc) |
| 1790 | val = G.global_argv[i]; |
| 1791 | /* else val remains NULL: $N with too big N */ |
| 1792 | } else |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1793 | val = lookup_param(var); |
| 1794 | |
| 1795 | /* handle any expansions */ |
| 1796 | if (exp_len) { |
| 1797 | debug_printf_expand("expand: length of '%s' = ", val); |
| 1798 | val = utoa(val ? strlen(val) : 0); |
| 1799 | debug_printf_expand("%s\n", val); |
| 1800 | } else if (exp_off) { |
| 1801 | /* we need to do an expansion */ |
| 1802 | int exp_test = (!val || (exp_null && !val[0])); |
| 1803 | if (exp_op == '+') |
| 1804 | exp_test = !exp_test; |
| 1805 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 1806 | exp_null ? "true" : "false", exp_test); |
| 1807 | if (exp_test) { |
| 1808 | if (exp_op == '?') |
| 1809 | maybe_die(var, *exp_word ? exp_word : "parameter null or not set"); |
| 1810 | else |
| 1811 | val = exp_word; |
| 1812 | |
| 1813 | if (exp_op == '=') { |
| 1814 | if (isdigit(var[0]) || var[0] == '#') { |
| 1815 | maybe_die(var, "special vars cannot assign in this way"); |
| 1816 | val = NULL; |
| 1817 | } else { |
| 1818 | char *new_var = xmalloc(strlen(var) + strlen(val) + 2); |
| 1819 | sprintf(new_var, "%s=%s", var, val); |
| 1820 | set_local_var(new_var, -1); |
| 1821 | } |
| 1822 | } |
| 1823 | } |
| 1824 | var[exp_off] = exp_save; |
| 1825 | } |
| 1826 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1827 | arg[0] = first_ch; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1828 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1829 | #if ENABLE_HUSH_TICK |
| 1830 | store_val: |
| 1831 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1832 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
| 1833 | debug_printf_expand("unquoted '%s', output->o_quote:%d\n", val, output->o_quote); |
| 1834 | if (val) { |
| 1835 | /* unquoted var's contents should be globbed, so don't quote */ |
| 1836 | smallint sv = output->o_quote; |
| 1837 | output->o_quote = 0; |
| 1838 | n = expand_on_ifs(output, n, val); |
| 1839 | val = NULL; |
| 1840 | output->o_quote = sv; |
| 1841 | } |
| 1842 | } else { /* quoted $VAR, val will be appended below */ |
| 1843 | debug_printf_expand("quoted '%s', output->o_quote:%d\n", val, output->o_quote); |
| 1844 | } |
| 1845 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1846 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1847 | if (val) { |
| 1848 | o_addQstr(output, val, strlen(val)); |
| 1849 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1850 | /* Do the check to avoid writing to a const string */ |
| 1851 | if (p && *p != SPECIAL_VAR_SYMBOL) |
| 1852 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1853 | |
| 1854 | #if ENABLE_HUSH_TICK |
| 1855 | o_free(&subst_result); |
| 1856 | #endif |
| 1857 | arg = ++p; |
| 1858 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 1859 | |
| 1860 | if (arg[0]) { |
| 1861 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 1862 | /* this part is literal, and it was already pre-quoted |
| 1863 | * if needed (much earlier), do not use o_addQstr here! */ |
| 1864 | o_addstr(output, arg, strlen(arg) + 1); |
| 1865 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 1866 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 1867 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 1868 | ) { |
| 1869 | n--; |
| 1870 | /* allow to reuse list[n] later without re-growth */ |
| 1871 | output->has_empty_slot = 1; |
| 1872 | } else { |
| 1873 | o_addchr(output, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1874 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1875 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1878 | static char **expand_variables(char **argv, int or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1879 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1880 | int n; |
| 1881 | char **list; |
| 1882 | char **v; |
| 1883 | o_string output = NULL_O_STRING; |
| 1884 | |
| 1885 | if (or_mask & 0x100) { |
| 1886 | output.o_quote = 1; /* protect against globbing for "$var" */ |
| 1887 | /* (unquoted $var will temporarily switch it off) */ |
| 1888 | output.o_glob = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1889 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1890 | |
| 1891 | n = 0; |
| 1892 | v = argv; |
| 1893 | while (*v) { |
| 1894 | n = expand_vars_to_list(&output, n, *v, (char)or_mask); |
| 1895 | v++; |
| 1896 | } |
| 1897 | debug_print_list("expand_variables", &output, n); |
| 1898 | |
| 1899 | /* output.data (malloced in one block) gets returned in "list" */ |
| 1900 | list = o_finalize_list(&output, n); |
| 1901 | debug_print_strings("expand_variables[1]", list); |
| 1902 | return list; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1905 | static char **expand_strvec_to_strvec(char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1906 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1907 | return expand_variables(argv, 0x100); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1910 | /* Used for expansion of right hand of assignments */ |
| 1911 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 1912 | * "v=/bin/c*" */ |
| 1913 | static char *expand_string_to_string(const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1914 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1915 | char *argv[2], **list; |
| 1916 | |
| 1917 | argv[0] = (char*)str; |
| 1918 | argv[1] = NULL; |
| 1919 | list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */ |
| 1920 | if (HUSH_DEBUG) |
| 1921 | if (!list[0] || list[1]) |
| 1922 | bb_error_msg_and_die("BUG in varexp2"); |
| 1923 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 1924 | overlapping_strcpy((char*)list, list[0]); |
| 1925 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 1926 | return (char*)list; |
| 1927 | } |
| 1928 | |
| 1929 | /* Used for "eval" builtin */ |
| 1930 | static char* expand_strvec_to_string(char **argv) |
| 1931 | { |
| 1932 | char **list; |
| 1933 | |
| 1934 | list = expand_variables(argv, 0x80); |
| 1935 | /* Convert all NULs to spaces */ |
| 1936 | if (list[0]) { |
| 1937 | int n = 1; |
| 1938 | while (list[n]) { |
| 1939 | if (HUSH_DEBUG) |
| 1940 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 1941 | bb_error_msg_and_die("BUG in varexp3"); |
| 1942 | list[n][-1] = ' '; /* TODO: or to G.ifs[0]? */ |
| 1943 | n++; |
| 1944 | } |
| 1945 | } |
| 1946 | overlapping_strcpy((char*)list, list[0]); |
| 1947 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 1948 | return (char*)list; |
| 1949 | } |
| 1950 | |
| 1951 | static char **expand_assignments(char **argv, int count) |
| 1952 | { |
| 1953 | int i; |
| 1954 | char **p = NULL; |
| 1955 | /* Expand assignments into one string each */ |
| 1956 | for (i = 0; i < count; i++) { |
| 1957 | p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 1958 | } |
| 1959 | return p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1962 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1963 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 1964 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1965 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1966 | { |
| 1967 | int openfd, mode; |
| 1968 | struct redir_struct *redir; |
| 1969 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1970 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1971 | if (redir->dup == -1 && redir->rd_filename == NULL) { |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 1972 | /* something went wrong in the parse. Pretend it didn't happen */ |
| 1973 | continue; |
| 1974 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1975 | if (redir->dup == -1) { |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1976 | char *p; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1977 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 1978 | //TODO: check redir for names like '\\' |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1979 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1980 | openfd = open_or_warn(p, mode); |
| 1981 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1982 | if (openfd < 0) { |
| 1983 | /* this could get lost if stderr has been redirected, but |
| 1984 | bash and ash both lose it as well (though zsh doesn't!) */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1985 | return 1; |
| 1986 | } |
| 1987 | } else { |
| 1988 | openfd = redir->dup; |
| 1989 | } |
| 1990 | |
| 1991 | if (openfd != redir->fd) { |
| 1992 | if (squirrel && redir->fd < 3) { |
| 1993 | squirrel[redir->fd] = dup(redir->fd); |
| 1994 | } |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1995 | if (openfd == -3) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1996 | //close(openfd); // close(-3) ??! |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1997 | } else { |
| 1998 | dup2(openfd, redir->fd); |
Matt Kraai | c616e53 | 2001-06-05 16:50:08 +0000 | [diff] [blame] | 1999 | if (redir->dup == -1) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2000 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2001 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2002 | } |
| 2003 | } |
| 2004 | return 0; |
| 2005 | } |
| 2006 | |
| 2007 | static void restore_redirects(int squirrel[]) |
| 2008 | { |
| 2009 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2010 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2011 | fd = squirrel[i]; |
| 2012 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2013 | /* We simply die on error */ |
| 2014 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2015 | } |
| 2016 | } |
| 2017 | } |
| 2018 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2019 | |
| 2020 | #if !defined(DEBUG_CLEAN) |
| 2021 | #define free_pipe_list(head, indent) free_pipe_list(head) |
| 2022 | #define free_pipe(pi, indent) free_pipe(pi) |
| 2023 | #endif |
| 2024 | static int free_pipe_list(struct pipe *head, int indent); |
| 2025 | |
| 2026 | /* return code is the exit status of the pipe */ |
| 2027 | static int free_pipe(struct pipe *pi, int indent) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2028 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2029 | char **p; |
| 2030 | struct command *command; |
| 2031 | struct redir_struct *r, *rnext; |
| 2032 | int a, i, ret_code = 0; |
| 2033 | |
| 2034 | if (pi->stopped_cmds > 0) |
| 2035 | return ret_code; |
| 2036 | debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid()); |
| 2037 | for (i = 0; i < pi->num_cmds; i++) { |
| 2038 | command = &pi->cmds[i]; |
| 2039 | debug_printf_clean("%s command %d:\n", indenter(indent), i); |
| 2040 | if (command->argv) { |
| 2041 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 2042 | debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p); |
| 2043 | } |
| 2044 | free_strings(command->argv); |
| 2045 | command->argv = NULL; |
| 2046 | } else if (command->group) { |
| 2047 | debug_printf_clean("%s begin group (grp_type:%d)\n", indenter(indent), command->grp_type); |
| 2048 | ret_code = free_pipe_list(command->group, indent+3); |
| 2049 | debug_printf_clean("%s end group\n", indenter(indent)); |
| 2050 | } else { |
| 2051 | debug_printf_clean("%s (nil)\n", indenter(indent)); |
| 2052 | } |
| 2053 | for (r = command->redirects; r; r = rnext) { |
| 2054 | debug_printf_clean("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->rd_type].descrip); |
| 2055 | if (r->dup == -1) { |
| 2056 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 2057 | if (r->rd_filename) { |
| 2058 | debug_printf_clean(" %s\n", r->rd_filename); |
| 2059 | free(r->rd_filename); |
| 2060 | r->rd_filename = NULL; |
| 2061 | } |
| 2062 | } else { |
| 2063 | debug_printf_clean("&%d\n", r->dup); |
| 2064 | } |
| 2065 | rnext = r->next; |
| 2066 | free(r); |
| 2067 | } |
| 2068 | command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2069 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2070 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 2071 | pi->cmds = NULL; |
| 2072 | #if ENABLE_HUSH_JOB |
| 2073 | free(pi->cmdtext); |
| 2074 | pi->cmdtext = NULL; |
| 2075 | #endif |
| 2076 | return ret_code; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2077 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2078 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2079 | static int free_pipe_list(struct pipe *head, int indent) |
| 2080 | { |
| 2081 | int rcode = 0; /* if list has no members */ |
| 2082 | struct pipe *pi, *next; |
| 2083 | |
| 2084 | for (pi = head; pi; pi = next) { |
| 2085 | #if HAS_KEYWORDS |
| 2086 | debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word); |
| 2087 | #endif |
| 2088 | rcode = free_pipe(pi, indent); |
| 2089 | debug_printf_clean("%s pipe followup code %d\n", indenter(indent), pi->followup); |
| 2090 | next = pi->next; |
| 2091 | /*pi->next = NULL;*/ |
| 2092 | free(pi); |
| 2093 | } |
| 2094 | return rcode; |
| 2095 | } |
| 2096 | |
| 2097 | |
| 2098 | #if !BB_MMU |
| 2099 | typedef struct nommu_save_t { |
| 2100 | char **new_env; |
| 2101 | char **old_env; |
| 2102 | char **argv; |
| 2103 | } nommu_save_t; |
| 2104 | #else |
| 2105 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 2106 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 2107 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 2108 | pseudo_exec(command, argv_expanded) |
| 2109 | #endif |
| 2110 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2111 | /* Called after [v]fork() in run_pipe(), or from builtin_exec(). |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2112 | * Never returns. |
| 2113 | * XXX no exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2114 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2115 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2116 | 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] | 2117 | 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] | 2118 | { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2119 | int rcode; |
| 2120 | char **new_env; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 2121 | const struct built_in_command *x; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2122 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2123 | /* If a variable is assigned in a forest, and nobody listens, |
| 2124 | * was it ever really set? |
| 2125 | */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2126 | if (!argv[assignment_cnt]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2127 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2128 | |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 2129 | new_env = expand_assignments(argv, assignment_cnt); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2130 | #if BB_MMU |
| 2131 | putenv_all(new_env); |
| 2132 | free(new_env); /* optional */ |
| 2133 | #else |
| 2134 | nommu_save->new_env = new_env; |
| 2135 | nommu_save->old_env = putenv_all_and_save_old(new_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2136 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2137 | if (argv_expanded) { |
| 2138 | argv = argv_expanded; |
| 2139 | } else { |
| 2140 | argv = expand_strvec_to_strvec(argv); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2141 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2142 | nommu_save->argv = argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2143 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2144 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2145 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2146 | /* |
| 2147 | * Check if the command matches any of the builtins. |
| 2148 | * Depending on context, this might be redundant. But it's |
| 2149 | * easier to waste a few CPU cycles than it is to figure out |
| 2150 | * if this is one of those cases. |
| 2151 | */ |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 2152 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2153 | if (strcmp(argv[0], x->cmd) == 0) { |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2154 | debug_printf_exec("running builtin '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2155 | rcode = x->function(argv); |
| 2156 | fflush(stdout); |
| 2157 | _exit(rcode); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2158 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2159 | } |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2160 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2161 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 2162 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2163 | if (strchr(argv[0], '/') == NULL) { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 2164 | int a = find_applet_by_name(argv[0]); |
| 2165 | if (a >= 0) { |
| 2166 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2167 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 2168 | // is it ok that run_applet_no_and_exit() does exit(), not _exit()? |
| 2169 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2170 | } |
| 2171 | /* re-exec ourselves with the new arguments */ |
| 2172 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | bdbbb7e | 2007-06-08 15:02:55 +0000 | [diff] [blame] | 2173 | execvp(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2174 | /* If they called chroot or otherwise made the binary no longer |
| 2175 | * executable, fall through */ |
| 2176 | } |
| 2177 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 2178 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2179 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2180 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 2181 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2182 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2183 | execvp(argv[0], argv); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 2184 | bb_perror_msg("can't exec '%s'", argv[0]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 2185 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2188 | static int run_list(struct pipe *pi); |
| 2189 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2190 | /* Called after [v]fork() in run_pipe() |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2191 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2192 | 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] | 2193 | 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] | 2194 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2195 | if (command->argv) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2196 | pseudo_exec_argv(nommu_save, command->argv, command->assignment_cnt, argv_expanded); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2197 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2198 | if (command->group) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2199 | #if !BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 2200 | bb_error_msg_and_die("nested lists are not supported on NOMMU"); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2201 | #else |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 2202 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2203 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2204 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2205 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2206 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2207 | _exit(rcode); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2208 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2209 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2210 | |
| 2211 | /* Can happen. See what bash does with ">foo" by itself. */ |
| 2212 | debug_printf("trying to pseudo_exec null command\n"); |
| 2213 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2216 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2217 | static const char *get_cmdtext(struct pipe *pi) |
| 2218 | { |
| 2219 | char **argv; |
| 2220 | char *p; |
| 2221 | int len; |
| 2222 | |
| 2223 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 2224 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2225 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2226 | if (pi->cmdtext) |
| 2227 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2228 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2229 | if (!argv || !argv[0]) { |
| 2230 | pi->cmdtext = xzalloc(1); |
| 2231 | return pi->cmdtext; |
| 2232 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2233 | |
| 2234 | len = 0; |
| 2235 | do len += strlen(*argv) + 1; while (*++argv); |
| 2236 | pi->cmdtext = p = xmalloc(len); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2237 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2238 | do { |
| 2239 | len = strlen(*argv); |
| 2240 | memcpy(p, *argv, len); |
| 2241 | p += len; |
| 2242 | *p++ = ' '; |
| 2243 | } while (*++argv); |
| 2244 | p[-1] = '\0'; |
| 2245 | return pi->cmdtext; |
| 2246 | } |
| 2247 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2248 | static void insert_bg_job(struct pipe *pi) |
| 2249 | { |
| 2250 | struct pipe *thejob; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2251 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2252 | |
| 2253 | /* Linear search for the ID of the job to use */ |
| 2254 | pi->jobid = 1; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2255 | for (thejob = G.job_list; thejob; thejob = thejob->next) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2256 | if (thejob->jobid >= pi->jobid) |
| 2257 | pi->jobid = thejob->jobid + 1; |
| 2258 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2259 | /* Add thejob to the list of running jobs */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2260 | if (!G.job_list) { |
| 2261 | thejob = G.job_list = xmalloc(sizeof(*thejob)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2262 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2263 | for (thejob = G.job_list; thejob->next; thejob = thejob->next) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2264 | continue; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2265 | thejob->next = xmalloc(sizeof(*thejob)); |
| 2266 | thejob = thejob->next; |
| 2267 | } |
| 2268 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2269 | /* Physically copy the struct job */ |
Eric Andersen | 0fcd447 | 2001-05-02 20:12:03 +0000 | [diff] [blame] | 2270 | memcpy(thejob, pi, sizeof(struct pipe)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2271 | thejob->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 2272 | /* We cannot copy entire pi->cmds[] vector! Double free()s will happen */ |
| 2273 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2274 | // TODO: do we really need to have so many fields which are just dead weight |
| 2275 | // at execution stage? |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2276 | thejob->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2277 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2278 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2279 | thejob->next = NULL; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2280 | thejob->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2281 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2282 | /* We don't wait for background thejobs to return -- append it |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2283 | to the list of backgrounded thejobs and leave it alone */ |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 2284 | if (G.interactive_fd) |
| 2285 | 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] | 2286 | G.last_bg_pid = thejob->cmds[0].pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2287 | G.last_jobid = thejob->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2290 | static void remove_bg_job(struct pipe *pi) |
| 2291 | { |
| 2292 | struct pipe *prev_pipe; |
| 2293 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2294 | if (pi == G.job_list) { |
| 2295 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2296 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2297 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2298 | while (prev_pipe->next != pi) |
| 2299 | prev_pipe = prev_pipe->next; |
| 2300 | prev_pipe->next = pi->next; |
| 2301 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2302 | if (G.job_list) |
| 2303 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 2304 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2305 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2306 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 2307 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2308 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2309 | static void delete_finished_bg_job(struct pipe *pi) |
| 2310 | { |
| 2311 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2312 | pi->stopped_cmds = 0; |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2313 | free_pipe(pi, 0); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2314 | free(pi); |
| 2315 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2316 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2317 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2318 | /* Check to see if any processes have exited -- if they |
| 2319 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2320 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2321 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2322 | int attributes; |
| 2323 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2324 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2325 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2326 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2327 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2328 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2329 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2330 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 2331 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2332 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2333 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2334 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2335 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2336 | /* Do we do this right? |
| 2337 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2338 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2339 | * [3]+ Stopped sleep 20 | false |
| 2340 | * bash-3.00# echo $? |
| 2341 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 2342 | */ |
| 2343 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2344 | //FIXME: non-interactive bash does not continue even if all processes in fg pipe |
| 2345 | //are stopped. Testcase: "cat | cat" in a script (not on command line) |
| 2346 | // + killall -STOP cat |
| 2347 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2348 | wait_more: |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 2349 | // TODO: safe_waitpid? |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2350 | while ((childpid = waitpid(-1, &status, attributes)) > 0) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2351 | int i; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2352 | const int dead = WIFEXITED(status) || WIFSIGNALED(status); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2353 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2354 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 2355 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2356 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 2357 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 2358 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2359 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 2360 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 2361 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2362 | childpid, WEXITSTATUS(status)); |
| 2363 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2364 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2365 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2366 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 2367 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 2368 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2369 | continue; |
| 2370 | /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */ |
| 2371 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2372 | fg_pipe->cmds[i].pid = 0; |
| 2373 | fg_pipe->alive_cmds--; |
| 2374 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2375 | /* last process gives overall exitstatus */ |
| 2376 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2377 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2378 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2379 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2380 | fg_pipe->cmds[i].is_stopped = 1; |
| 2381 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2382 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2383 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 2384 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 2385 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2386 | /* All processes in fg pipe have exited/stopped */ |
| 2387 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2388 | if (fg_pipe->alive_cmds) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2389 | insert_bg_job(fg_pipe); |
| 2390 | #endif |
| 2391 | return rcode; |
| 2392 | } |
| 2393 | /* There are still running processes in the fg pipe */ |
| 2394 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2395 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2396 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2399 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2400 | /* We asked to wait for bg or orphaned children */ |
| 2401 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2402 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2403 | for (i = 0; i < pi->num_cmds; i++) { |
| 2404 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2405 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 2406 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2407 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2408 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 2409 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2410 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 2411 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2412 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2413 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2414 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2415 | pi->cmds[i].pid = 0; |
| 2416 | pi->alive_cmds--; |
| 2417 | if (!pi->alive_cmds) { |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 2418 | if (G.interactive_fd) |
| 2419 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 2420 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2421 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2422 | } |
| 2423 | } else { |
| 2424 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2425 | pi->cmds[i].is_stopped = 1; |
| 2426 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2427 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2428 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2429 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2430 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2431 | /* wait found no children or failed */ |
| 2432 | |
| 2433 | if (childpid && errno != ECHILD) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 2434 | bb_perror_msg("waitpid"); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2435 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2438 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 2439 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 2440 | { |
| 2441 | pid_t p; |
| 2442 | int rcode = checkjobs(fg_pipe); |
| 2443 | /* Job finished, move the shell to the foreground */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2444 | p = getpgid(0); /* pgid of our process */ |
| 2445 | debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2446 | tcsetpgrp(G.interactive_fd, p); |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 2447 | return rcode; |
| 2448 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2449 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 2450 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2451 | /* run_pipe() starts all the jobs, but doesn't wait for anything |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2452 | * to finish. See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2453 | * |
| 2454 | * return code is normally -1, when the caller has to wait for children |
| 2455 | * to finish to determine the exit status of the pipe. If the pipe |
| 2456 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2457 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2458 | * return value. |
| 2459 | * |
| 2460 | * The input of the pipe is always stdin, the output is always |
| 2461 | * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus, |
| 2462 | * because it tries to avoid running the command substitution in |
| 2463 | * subshell, when that is in fact necessary. The subshell process |
| 2464 | * now has its stdout directed to the input of the appropriate pipe, |
| 2465 | * so this routine is noticeably simpler. |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2466 | * |
| 2467 | * Returns -1 only if started some children. IOW: we have to |
| 2468 | * mask out retvals of builtins etc with 0xff! |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2469 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2470 | static int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2471 | { |
| 2472 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2473 | int nextin; |
| 2474 | int pipefds[2]; /* pipefds[0] is for reading */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2475 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2476 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2477 | char **argv; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 2478 | const struct built_in_command *x; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2479 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 2480 | /* it is not always needed, but we aim to smaller code */ |
| 2481 | int squirrel[] = { -1, -1, -1 }; |
| 2482 | int rcode; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2483 | 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] | 2484 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2485 | 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] | 2486 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2487 | #if ENABLE_HUSH_JOB |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 2488 | pi->pgrp = -1; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2489 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2490 | pi->alive_cmds = 1; |
| 2491 | pi->stopped_cmds = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2492 | |
| 2493 | /* Check if this is a simple builtin (not part of a pipe). |
| 2494 | * Builtins within pipes have to fork anyway, and are handled in |
| 2495 | * pseudo_exec. "echo foo | read bar" doesn't work on bash, either. |
| 2496 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2497 | command = &(pi->cmds[0]); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2498 | |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2499 | #if ENABLE_HUSH_FUNCTIONS |
| 2500 | if (single_and_fg && command->group && command->grp_type == GRP_FUNCTION) { |
| 2501 | /* We "execute" function definition */ |
| 2502 | bb_error_msg("here we ought to remember function definition, and go on"); |
| 2503 | return EXIT_SUCCESS; |
| 2504 | } |
| 2505 | #endif |
| 2506 | |
| 2507 | if (single_and_fg && command->group && command->grp_type == GRP_NORMAL) { |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 2508 | debug_printf("non-subshell grouping\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2509 | setup_redirects(command, squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2510 | debug_printf_exec(": run_list\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2511 | rcode = run_list(command->group) & 0xff; |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 2512 | restore_redirects(squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2513 | debug_printf_exec("run_pipe return %d\n", rcode); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2514 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2515 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2518 | argv = command->argv; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2519 | argv_expanded = NULL; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2520 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2521 | if (single_and_fg && argv != NULL) { |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2522 | char **new_env = NULL; |
| 2523 | char **old_env = NULL; |
| 2524 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2525 | i = command->assignment_cnt; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2526 | if (i != 0 && argv[i] == NULL) { |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2527 | /* assignments, but no command: set local environment */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2528 | for (i = 0; argv[i] != NULL; i++) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2529 | debug_printf("local environment set: %s\n", argv[i]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2530 | p = expand_string_to_string(argv[i]); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2531 | set_local_var(p, 0); |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2532 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2533 | 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] | 2534 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2535 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2536 | /* Expand the rest into (possibly) many strings each */ |
| 2537 | argv_expanded = expand_strvec_to_strvec(argv + i); |
| 2538 | |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 2539 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2540 | if (strcmp(argv_expanded[0], x->cmd) != 0) |
| 2541 | continue; |
| 2542 | if (x->function == builtin_exec && argv_expanded[1] == NULL) { |
| 2543 | debug_printf("exec with redirects only\n"); |
| 2544 | setup_redirects(command, NULL); |
| 2545 | rcode = EXIT_SUCCESS; |
| 2546 | goto clean_up_and_ret1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2547 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2548 | debug_printf("builtin inline %s\n", argv_expanded[0]); |
| 2549 | /* XXX setup_redirects acts on file descriptors, not FILEs. |
| 2550 | * This is perfect for work that comes after exec(). |
| 2551 | * Is it really safe for inline use? Experimentally, |
| 2552 | * things seem to work with glibc. */ |
| 2553 | setup_redirects(command, squirrel); |
| 2554 | new_env = expand_assignments(argv, command->assignment_cnt); |
| 2555 | old_env = putenv_all_and_save_old(new_env); |
| 2556 | debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv_expanded[1]); |
| 2557 | rcode = x->function(argv_expanded) & 0xff; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2558 | #if ENABLE_FEATURE_SH_STANDALONE |
| 2559 | clean_up_and_ret: |
| 2560 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2561 | restore_redirects(squirrel); |
| 2562 | free_strings_and_unsetenv(new_env, 1); |
| 2563 | putenv_all(old_env); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2564 | free(old_env); /* not free_strings()! */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2565 | clean_up_and_ret1: |
| 2566 | free(argv_expanded); |
| 2567 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 2568 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 2569 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2570 | } |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 2571 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2572 | i = find_applet_by_name(argv_expanded[0]); |
| 2573 | if (i >= 0 && APPLET_IS_NOFORK(i)) { |
| 2574 | setup_redirects(command, squirrel); |
| 2575 | save_nofork_data(&G.nofork_save); |
| 2576 | new_env = expand_assignments(argv, command->assignment_cnt); |
| 2577 | old_env = putenv_all_and_save_old(new_env); |
| 2578 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]); |
| 2579 | rcode = run_nofork_applet_prime(&G.nofork_save, i, argv_expanded); |
| 2580 | goto clean_up_and_ret; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 2581 | } |
| 2582 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2585 | /* NB: argv_expanded may already be created, and that |
| 2586 | * might include `cmd` runs! Do not rerun it! We *must* |
| 2587 | * use argv_expanded if it's non-NULL */ |
| 2588 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2589 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2590 | pi->alive_cmds = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2591 | nextin = 0; |
| 2592 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2593 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2594 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2595 | volatile nommu_save_t nommu_save; |
| 2596 | nommu_save.new_env = NULL; |
| 2597 | nommu_save.old_env = NULL; |
| 2598 | nommu_save.argv = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2599 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2600 | command = &(pi->cmds[i]); |
| 2601 | if (command->argv) { |
| 2602 | 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] | 2603 | } else |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2604 | debug_printf_exec(": pipe member with no argv\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2605 | |
| 2606 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2607 | pipefds[0] = 0; |
| 2608 | pipefds[1] = 1; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2609 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2610 | xpipe(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2611 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2612 | command->pid = BB_MMU ? fork() : vfork(); |
| 2613 | if (!command->pid) { /* child */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2614 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2615 | die_sleep = 0; /* let nofork's xfuncs die */ |
| 2616 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2617 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2618 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2619 | if (G.run_list_level == 1 && G.interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2620 | pid_t pgrp; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2621 | pgrp = pi->pgrp; |
| 2622 | if (pgrp < 0) /* true for 1st process only */ |
| 2623 | pgrp = getpid(); |
| 2624 | if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2625 | /* We do it in *every* child, not just first, |
| 2626 | * to avoid races */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2627 | tcsetpgrp(G.interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2628 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2629 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2630 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2631 | xmove_fd(nextin, 0); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2632 | xmove_fd(pipefds[1], 1); /* write end */ |
| 2633 | if (pipefds[0] > 1) |
| 2634 | close(pipefds[0]); /* read end */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2635 | /* Like bash, explicit redirects override pipes, |
| 2636 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2637 | setup_redirects(command, NULL); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 2638 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2639 | /* Restore default handlers just prior to exec */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2640 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 2641 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2642 | /* Stores to nommu_save list of env vars putenv'ed |
| 2643 | * (NOMMU, on MMU we don't need that) */ |
| 2644 | /* cast away volatility... */ |
| 2645 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2646 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2647 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2648 | /* parent */ |
| 2649 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2650 | /* Clean up after vforked child */ |
| 2651 | free(nommu_save.argv); |
| 2652 | free_strings_and_unsetenv(nommu_save.new_env, 1); |
| 2653 | putenv_all(nommu_save.old_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2654 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2655 | free(argv_expanded); |
| 2656 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2657 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2658 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 2659 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2660 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2661 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2662 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2663 | /* Second and next children need to know pid of first one */ |
| 2664 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2665 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2666 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2667 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2668 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2669 | if (i) |
| 2670 | close(nextin); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2671 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2672 | close(pipefds[1]); /* write end */ |
| 2673 | /* Pass read (output) pipe end to next iteration */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2674 | nextin = pipefds[0]; |
| 2675 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2676 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2677 | if (!pi->alive_cmds) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2678 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 2679 | return 1; |
| 2680 | } |
| 2681 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2682 | 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] | 2683 | return -1; |
| 2684 | } |
| 2685 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 2686 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2687 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 2688 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2689 | static const char *const PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2690 | [PIPE_SEQ] = "SEQ", |
| 2691 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2692 | [PIPE_OR ] = "OR" , |
| 2693 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2694 | }; |
| 2695 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2696 | [RES_NONE ] = "NONE" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2697 | #if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2698 | [RES_IF ] = "IF" , |
| 2699 | [RES_THEN ] = "THEN" , |
| 2700 | [RES_ELIF ] = "ELIF" , |
| 2701 | [RES_ELSE ] = "ELSE" , |
| 2702 | [RES_FI ] = "FI" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2703 | #endif |
| 2704 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2705 | [RES_FOR ] = "FOR" , |
| 2706 | [RES_WHILE] = "WHILE", |
| 2707 | [RES_UNTIL] = "UNTIL", |
| 2708 | [RES_DO ] = "DO" , |
| 2709 | [RES_DONE ] = "DONE" , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 2710 | #endif |
| 2711 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2712 | [RES_IN ] = "IN" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2713 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2714 | #if ENABLE_HUSH_CASE |
| 2715 | [RES_CASE ] = "CASE" , |
| 2716 | [RES_MATCH] = "MATCH", |
| 2717 | [RES_CASEI] = "CASEI", |
| 2718 | [RES_ESAC ] = "ESAC" , |
| 2719 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2720 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2721 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2722 | }; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2723 | static const char *const GRPTYPE[] = { |
| 2724 | "()", |
| 2725 | "{}", |
| 2726 | #if ENABLE_HUSH_FUNCTIONS |
| 2727 | "func()", |
| 2728 | #endif |
| 2729 | }; |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2730 | |
| 2731 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2732 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2733 | pin = 0; |
| 2734 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2735 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 2736 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2737 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2738 | while (prn < pi->num_cmds) { |
| 2739 | struct command *command = &pi->cmds[prn]; |
| 2740 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2741 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2742 | fprintf(stderr, "%*s prog %d assignment_cnt:%d", lvl*2, "", prn, command->assignment_cnt); |
| 2743 | if (command->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2744 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2745 | GRPTYPE[command->grp_type], |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2746 | argv); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2747 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2748 | prn++; |
| 2749 | continue; |
| 2750 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2751 | if (argv) while (*argv) { |
| 2752 | fprintf(stderr, " '%s'", *argv); |
| 2753 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 2754 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2755 | fprintf(stderr, "\n"); |
| 2756 | prn++; |
| 2757 | } |
| 2758 | pi = pi->next; |
| 2759 | pin++; |
| 2760 | } |
| 2761 | } |
| 2762 | #endif |
| 2763 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2764 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 2765 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2766 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2767 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2768 | #if ENABLE_HUSH_CASE |
| 2769 | char *case_word = NULL; |
| 2770 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2771 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2772 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2773 | char *for_varname = NULL; |
| 2774 | char **for_lcur = NULL; |
| 2775 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2776 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2777 | smallint flag_skip = 1; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2778 | smalluint rcode = 0; /* probably just for compiler */ |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 2779 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2780 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2781 | #else |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2782 | enum { cond_code = 0, }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2783 | #endif |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2784 | /*enum reserved_style*/ smallint rword = RES_NONE; |
| 2785 | /*enum reserved_style*/ smallint skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 2786 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2787 | 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] | 2788 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2789 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2790 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2791 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 2792 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2793 | continue; |
| 2794 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2795 | if (cpipe->next == NULL) { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2796 | syntax("malformed for"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2797 | 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] | 2798 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2799 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2800 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2801 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2802 | continue; |
| 2803 | /* 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] | 2804 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 2805 | || 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] | 2806 | ) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2807 | syntax("malformed for"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2808 | 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] | 2809 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2810 | } |
| 2811 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2812 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2813 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2814 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 2815 | * in order to return, no direct "return" statements please. |
| 2816 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2817 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2818 | ////TODO: ctrl-Z handling needs re-thinking and re-testing |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2819 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2820 | #if ENABLE_HUSH_JOB |
| 2821 | /* Example of nested list: "while true; do { sleep 1 | exit 2; } done". |
| 2822 | * We are saving state before entering outermost list ("while...done") |
| 2823 | * so that ctrl-Z will correctly background _entire_ outermost list, |
| 2824 | * not just a part of it (like "sleep 1 | exit 2") */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2825 | if (++G.run_list_level == 1 && G.interactive_fd) { |
| 2826 | if (sigsetjmp(G.toplevel_jb, 1)) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2827 | /* ctrl-Z forked and we are parent; or ctrl-C. |
| 2828 | * Sighandler has longjmped us here */ |
| 2829 | signal(SIGINT, SIG_IGN); |
| 2830 | signal(SIGTSTP, SIG_IGN); |
| 2831 | /* Restore level (we can be coming from deep inside |
| 2832 | * nested levels) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2833 | G.run_list_level = 1; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2834 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2835 | if (G.nofork_save.saved) { /* if save area is valid */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2836 | debug_printf_jobs("exiting nofork early\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2837 | restore_nofork_data(&G.nofork_save); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2838 | } |
| 2839 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2840 | //// if (G.ctrl_z_flag) { |
| 2841 | //// /* ctrl-Z has forked and stored pid of the child in pi->pid. |
| 2842 | //// * Remember this child as background job */ |
| 2843 | //// insert_bg_job(pi); |
| 2844 | //// } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2845 | /* ctrl-C. We just stop doing whatever we were doing */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 2846 | bb_putchar('\n'); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2847 | //// } |
Denis Vlasenko | ff29b4f | 2008-07-29 13:57:59 +0000 | [diff] [blame] | 2848 | USE_HUSH_LOOPS(loop_top = NULL;) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2849 | USE_HUSH_LOOPS(G.depth_of_loop = 0;) |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2850 | rcode = 0; |
| 2851 | goto ret; |
| 2852 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2853 | //// /* ctrl-Z handler will store pid etc in pi */ |
| 2854 | //// G.toplevel_list = pi; |
| 2855 | //// G.ctrl_z_flag = 0; |
| 2856 | ////#if ENABLE_FEATURE_SH_STANDALONE |
| 2857 | //// G.nofork_save.saved = 0; /* in case we will run a nofork later */ |
| 2858 | ////#endif |
| 2859 | //// signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z); |
| 2860 | //// signal(SIGINT, handler_ctrl_c); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2861 | } |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2862 | #endif /* JOB */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2863 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2864 | /* Go through list of pipes, (maybe) executing them. */ |
| 2865 | for (; pi; pi = USE_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2866 | if (G.flag_SIGINT) |
| 2867 | break; |
| 2868 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2869 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 2870 | IF_HAS_NO_KEYWORDS(rword = RES_NONE;) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2871 | debug_printf_exec(": rword=%d cond_code=%d skip_more=%d\n", |
| 2872 | rword, cond_code, skip_more_for_this_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2873 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 2874 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2875 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 2876 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2877 | /* start of a loop: remember where loop starts */ |
| 2878 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2879 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2880 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2881 | #endif |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2882 | if (rword == skip_more_for_this_rword && flag_skip) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2883 | if (pi->followup == PIPE_SEQ) |
| 2884 | flag_skip = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2885 | /* it is "<false> && CMD" or "<true> || CMD" |
| 2886 | * and we should not execute CMD */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2887 | continue; |
| 2888 | } |
| 2889 | flag_skip = 1; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2890 | skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2891 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2892 | if (cond_code) { |
| 2893 | if (rword == RES_THEN) { |
| 2894 | /* "if <false> THEN cmd": skip cmd */ |
| 2895 | continue; |
| 2896 | } |
| 2897 | } else { |
| 2898 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 2899 | /* "if <true> then ... ELSE/ELIF cmd": |
| 2900 | * skip cmd and all following ones */ |
| 2901 | break; |
| 2902 | } |
| 2903 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2904 | #endif |
| 2905 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2906 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2907 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2908 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2909 | |
| 2910 | static const char encoded_dollar_at[] ALIGN1 = { |
| 2911 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 2912 | }; /* encoded representation of "$@" */ |
| 2913 | static const char *const encoded_dollar_at_argv[] = { |
| 2914 | encoded_dollar_at, NULL |
| 2915 | }; /* argv list with one element: "$@" */ |
| 2916 | char **vals; |
| 2917 | |
| 2918 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2919 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2920 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2921 | if (!pi->next->cmds[0].argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2922 | break; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2923 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2924 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2925 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2926 | debug_print_strings("for_list made from", vals); |
| 2927 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2928 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2929 | debug_print_strings("for_list", for_list); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2930 | for_varname = pi->cmds[0].argv[0]; |
| 2931 | pi->cmds[0].argv[0] = NULL; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2932 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2933 | free(pi->cmds[0].argv[0]); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2934 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 2935 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2936 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2937 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2938 | for_lcur = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2939 | pi->cmds[0].argv[0] = for_varname; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2940 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2941 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2942 | /* insert next value from for_lcur */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2943 | //TODO: does it need escaping? |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2944 | pi->cmds[0].argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++); |
| 2945 | pi->cmds[0].assignment_cnt = 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2946 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2947 | 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] | 2948 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2949 | if (rword == RES_DONE) { |
| 2950 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2951 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2952 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2953 | #if ENABLE_HUSH_CASE |
| 2954 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2955 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2956 | continue; |
| 2957 | } |
| 2958 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 2959 | char **argv; |
| 2960 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2961 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 2962 | break; |
| 2963 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2964 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 2965 | while (*argv) { |
| 2966 | char *pattern = expand_string_to_string(*argv); |
| 2967 | /* TODO: which FNM_xxx flags to use? */ |
| 2968 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 2969 | free(pattern); |
| 2970 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 2971 | free(case_word); /* make future "word)" stop */ |
| 2972 | case_word = NULL; |
| 2973 | break; |
| 2974 | } |
| 2975 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2976 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2977 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2978 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2979 | if (rword == RES_CASEI) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2980 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2981 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2982 | } |
| 2983 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2984 | /* Just pressing <enter> in shell should check for jobs. |
| 2985 | * OTOH, in non-interactive shell this is useless |
| 2986 | * and only leads to extra job checks */ |
| 2987 | if (pi->num_cmds == 0) { |
| 2988 | if (G.interactive_fd) |
| 2989 | goto check_jobs_and_continue; |
| 2990 | continue; |
| 2991 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2992 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2993 | /* After analyzing all keywords and conditions, we decided |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2994 | * to execute this pipe. NB: have to do checkjobs(NULL) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2995 | * after run_pipe() to collect any background children, |
| 2996 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2997 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2998 | { |
| 2999 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3000 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3001 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3002 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3003 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 3004 | if (r != -1) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3005 | /* we only ran a builtin: rcode is already known |
| 3006 | * and we don't need to wait for anything. */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3007 | check_and_run_traps(); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3008 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3009 | /* was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3010 | if (G.flag_break_continue) { |
| 3011 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3012 | /* we might fall into outer *loop*, |
| 3013 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3014 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3015 | G.depth_break_continue--; |
| 3016 | if (G.depth_break_continue == 0) |
| 3017 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 3018 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 3019 | } /* 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] | 3020 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 3021 | goto check_jobs_and_break; |
| 3022 | /* "continue": simulate end of loop */ |
| 3023 | rword = RES_DONE; |
| 3024 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3025 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3026 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3027 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3028 | /* what does bash do with attempts to background builtins? */ |
| 3029 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3030 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 3031 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3032 | check_and_run_traps(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3033 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3034 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3035 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3036 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3037 | rcode = 0; /* EXIT_SUCCESS */ |
| 3038 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3039 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3040 | if (G.run_list_level == 1 && G.interactive_fd) { |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3041 | /* waits for completion, then fg's main shell */ |
| 3042 | rcode = checkjobs_and_fg_shell(pi); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3043 | check_and_run_traps(); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3044 | debug_printf_exec(": checkjobs_and_fg_shell returned %d\n", rcode); |
| 3045 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3046 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3047 | { /* this one just waits for completion */ |
| 3048 | rcode = checkjobs(pi); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3049 | check_and_run_traps(); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3050 | debug_printf_exec(": checkjobs returned %d\n", rcode); |
| 3051 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3052 | } |
| 3053 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3054 | debug_printf_exec(": setting last_return_code=%d\n", rcode); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3055 | G.last_return_code = rcode; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3056 | |
| 3057 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3058 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3059 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3060 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3061 | #endif |
| 3062 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3063 | if (rword == RES_WHILE) { |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 3064 | if (rcode) { |
| 3065 | rcode = 0; /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3066 | goto check_jobs_and_break; |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 3067 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3068 | } |
| 3069 | if (rword == RES_UNTIL) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3070 | if (!rcode) { |
| 3071 | check_jobs_and_break: |
| 3072 | checkjobs(NULL); |
| 3073 | break; |
| 3074 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3075 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3076 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3077 | if ((rcode == 0 && pi->followup == PIPE_OR) |
| 3078 | || (rcode != 0 && pi->followup == PIPE_AND) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3079 | ) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3080 | skip_more_for_this_rword = rword; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3081 | } |
Mike Frysinger | 8ec1c9d | 2009-03-29 00:45:26 +0000 | [diff] [blame] | 3082 | |
| 3083 | check_jobs_and_continue: |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3084 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3085 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3086 | |
| 3087 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3088 | //// if (G.ctrl_z_flag) { |
| 3089 | //// /* ctrl-Z forked somewhere in the past, we are the child, |
| 3090 | //// * and now we completed running the list. Exit. */ |
| 3091 | //////TODO: _exit? |
| 3092 | //// exit(rcode); |
| 3093 | //// } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3094 | ret: |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3095 | G.run_list_level--; |
| 3096 | //// if (!G.run_list_level && G.interactive_fd) { |
| 3097 | //// signal(SIGTSTP, SIG_IGN); |
| 3098 | //// signal(SIGINT, SIG_IGN); |
| 3099 | //// } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3100 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3101 | 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] | 3102 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 3103 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3104 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 3105 | free(for_list); |
| 3106 | #endif |
| 3107 | #if ENABLE_HUSH_CASE |
| 3108 | free(case_word); |
| 3109 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3110 | return rcode; |
| 3111 | } |
| 3112 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3113 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3114 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3115 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3116 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3117 | debug_printf_exec("run_and_free_list entered\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3118 | if (!G.fake_mode) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3119 | debug_printf_exec(": run_list with %d members\n", pi->num_cmds); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3120 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3121 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3122 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3123 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3124 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3125 | free_pipe_list(pi, /* indent: */ 0); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3126 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3127 | return rcode; |
| 3128 | } |
| 3129 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3130 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3131 | /* Peek ahead in the in_str to find out if we have a "&n" construct, |
| 3132 | * as in "2>&1", that represents duplicating a file descriptor. |
| 3133 | * Return either -2 (syntax error), -1 (no &), or the number found. |
| 3134 | */ |
| 3135 | static int redirect_dup_num(struct in_str *input) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3136 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3137 | int ch, d = 0, ok = 0; |
| 3138 | ch = i_peek(input); |
| 3139 | if (ch != '&') return -1; |
| 3140 | |
| 3141 | i_getch(input); /* get the & */ |
| 3142 | ch = i_peek(input); |
| 3143 | if (ch == '-') { |
| 3144 | i_getch(input); |
| 3145 | return -3; /* "-" represents "close me" */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3146 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3147 | while (isdigit(ch)) { |
| 3148 | d = d*10 + (ch-'0'); |
| 3149 | ok = 1; |
| 3150 | i_getch(input); |
| 3151 | ch = i_peek(input); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3152 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3153 | if (ok) return d; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3154 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3155 | bb_error_msg("ambiguous redirect"); |
| 3156 | return -2; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3157 | } |
| 3158 | |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3159 | /* 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] | 3160 | * for file descriptor duplication, e.g., "2>&1". |
| 3161 | * Return code is 0 normally, 1 if a syntax error is detected in src. |
| 3162 | * Resource errors (in xmalloc) cause the process to exit */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3163 | 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] | 3164 | struct in_str *input) |
| 3165 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3166 | struct command *command = ctx->command; |
| 3167 | struct redir_struct *redir = command->redirects; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3168 | struct redir_struct *last_redir = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3169 | |
| 3170 | /* 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] | 3171 | while (redir) { |
| 3172 | last_redir = redir; |
| 3173 | redir = redir->next; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3174 | } |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3175 | redir = xzalloc(sizeof(struct redir_struct)); |
| 3176 | /* redir->next = NULL; */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3177 | /* redir->rd_filename = NULL; */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3178 | if (last_redir) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3179 | last_redir->next = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3180 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3181 | command->redirects = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3184 | redir->rd_type = style; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3185 | redir->fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3186 | |
| 3187 | debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip); |
| 3188 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3189 | /* Check for a '2>&1' type redirect */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3190 | redir->dup = redirect_dup_num(input); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3191 | if (redir->dup == -2) |
| 3192 | return 1; /* syntax error */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3193 | if (redir->dup != -1) { |
| 3194 | /* Erik had a check here that the file descriptor in question |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3195 | * is legit; I postpone that to "run time" |
| 3196 | * A "-" representation of "close me" shows up as a -3 here */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3197 | debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup); |
| 3198 | } else { |
| 3199 | /* We do _not_ try to open the file that src points to, |
| 3200 | * since we need to return and let src be expanded first. |
| 3201 | * Set ctx->pending_redirect, so we know what to do at the |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 3202 | * end of the next parsed word. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3203 | ctx->pending_redirect = redir; |
| 3204 | } |
| 3205 | return 0; |
| 3206 | } |
| 3207 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3208 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3209 | static struct pipe *new_pipe(void) |
| 3210 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3211 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3212 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3213 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3214 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3215 | return pi; |
| 3216 | } |
| 3217 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3218 | /* Command (member of a pipe) is complete. The only possible error here |
| 3219 | * is out of memory, in which case xmalloc exits. */ |
| 3220 | static int done_command(struct parse_context *ctx) |
| 3221 | { |
| 3222 | /* The command is really already in the pipe structure, so |
| 3223 | * advance the pipe counter and make a new, null command. */ |
| 3224 | struct pipe *pi = ctx->pipe; |
| 3225 | struct command *command = ctx->command; |
| 3226 | |
| 3227 | if (command) { |
| 3228 | if (command->group == NULL |
| 3229 | && command->argv == NULL |
| 3230 | && command->redirects == NULL |
| 3231 | ) { |
| 3232 | debug_printf_parse("done_command: skipping null cmd, num_cmds=%d\n", pi->num_cmds); |
| 3233 | return pi->num_cmds; |
| 3234 | } |
| 3235 | pi->num_cmds++; |
| 3236 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
| 3237 | } else { |
| 3238 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3239 | } |
| 3240 | |
| 3241 | /* Only real trickiness here is that the uncommitted |
| 3242 | * command structure is not counted in pi->num_cmds. */ |
| 3243 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
| 3244 | command = &pi->cmds[pi->num_cmds]; |
| 3245 | memset(command, 0, sizeof(*command)); |
| 3246 | |
| 3247 | ctx->command = command; |
| 3248 | /* but ctx->pipe and ctx->list_head remain unchanged */ |
| 3249 | |
| 3250 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3251 | } |
| 3252 | |
| 3253 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3254 | { |
| 3255 | int not_null; |
| 3256 | |
| 3257 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3258 | /* Close previous command */ |
| 3259 | not_null = done_command(ctx); |
| 3260 | ctx->pipe->followup = type; |
| 3261 | IF_HAS_KEYWORDS(ctx->pipe->pi_inverted = ctx->ctx_inverted;) |
| 3262 | IF_HAS_KEYWORDS(ctx->ctx_inverted = 0;) |
| 3263 | IF_HAS_KEYWORDS(ctx->pipe->res_word = ctx->ctx_res_w;) |
| 3264 | |
| 3265 | /* Without this check, even just <enter> on command line generates |
| 3266 | * tree of three NOPs (!). Which is harmless but annoying. |
| 3267 | * IOW: it is safe to do it unconditionally. |
| 3268 | * RES_NONE case is for "for a in; do ..." (empty IN set) |
| 3269 | * to work, possibly other cases too. */ |
| 3270 | if (not_null IF_HAS_KEYWORDS(|| ctx->ctx_res_w != RES_NONE)) { |
| 3271 | struct pipe *new_p; |
| 3272 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3273 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3274 | not_null, ctx->ctx_res_w); |
| 3275 | new_p = new_pipe(); |
| 3276 | ctx->pipe->next = new_p; |
| 3277 | ctx->pipe = new_p; |
| 3278 | ctx->command = NULL; /* needed! */ |
| 3279 | /* RES_THEN, RES_DO etc are "sticky" - |
| 3280 | * they remain set for commands inside if/while. |
| 3281 | * This is used to control execution. |
| 3282 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3283 | * cases where variable or value happens to match a keyword): |
| 3284 | */ |
| 3285 | #if ENABLE_HUSH_LOOPS |
| 3286 | if (ctx->ctx_res_w == RES_FOR |
| 3287 | || ctx->ctx_res_w == RES_IN) |
| 3288 | ctx->ctx_res_w = RES_NONE; |
| 3289 | #endif |
| 3290 | #if ENABLE_HUSH_CASE |
| 3291 | if (ctx->ctx_res_w == RES_MATCH) |
| 3292 | ctx->ctx_res_w = RES_CASEI; |
| 3293 | #endif |
| 3294 | /* Create the memory for command, roughly: |
| 3295 | * ctx->pipe->cmds = new struct command; |
| 3296 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3297 | */ |
| 3298 | done_command(ctx); |
| 3299 | } |
| 3300 | debug_printf_parse("done_pipe return\n"); |
| 3301 | } |
| 3302 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3303 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3304 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3305 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3306 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3307 | /* Create the memory for command, roughly: |
| 3308 | * ctx->pipe->cmds = new struct command; |
| 3309 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3310 | */ |
| 3311 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3314 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3315 | /* If a reserved word is found and processed, parse context is modified |
| 3316 | * and 1 is returned. |
| 3317 | * Handles if, then, elif, else, fi, for, while, until, do, done. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3318 | * case, function, and select are obnoxious, save those for later. |
| 3319 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3320 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3321 | struct reserved_combo { |
| 3322 | char literal[6]; |
| 3323 | unsigned char res; |
| 3324 | unsigned char assignment_flag; |
| 3325 | int flag; |
| 3326 | }; |
| 3327 | enum { |
| 3328 | FLAG_END = (1 << RES_NONE ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3329 | #if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3330 | FLAG_IF = (1 << RES_IF ), |
| 3331 | FLAG_THEN = (1 << RES_THEN ), |
| 3332 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3333 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3334 | FLAG_FI = (1 << RES_FI ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3335 | #endif |
| 3336 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3337 | FLAG_FOR = (1 << RES_FOR ), |
| 3338 | FLAG_WHILE = (1 << RES_WHILE), |
| 3339 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3340 | FLAG_DO = (1 << RES_DO ), |
| 3341 | FLAG_DONE = (1 << RES_DONE ), |
| 3342 | FLAG_IN = (1 << RES_IN ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3343 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3344 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3345 | FLAG_MATCH = (1 << RES_MATCH), |
| 3346 | FLAG_ESAC = (1 << RES_ESAC ), |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3347 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3348 | FLAG_START = (1 << RES_XXXX ), |
| 3349 | }; |
| 3350 | |
| 3351 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3352 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3353 | /* Mostly a list of accepted follow-up reserved words. |
| 3354 | * FLAG_END means we are done with the sequence, and are ready |
| 3355 | * to turn the compound list into a command. |
| 3356 | * FLAG_START means the word must start a new compound list. |
| 3357 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3358 | static const struct reserved_combo reserved_list[] = { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3359 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3360 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3361 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 3362 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3363 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 3364 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 3365 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3366 | #endif |
| 3367 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3368 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3369 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 3370 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 3371 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3372 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 3373 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3374 | #endif |
| 3375 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3376 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3377 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3378 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3379 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3380 | const struct reserved_combo *r; |
| 3381 | |
| 3382 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 3383 | if (strcmp(word->data, r->literal) == 0) |
| 3384 | return r; |
| 3385 | } |
| 3386 | return NULL; |
| 3387 | } |
| 3388 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 3389 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3390 | #if ENABLE_HUSH_CASE |
| 3391 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3392 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3393 | }; |
| 3394 | #endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3395 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3396 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3397 | r = match_reserved_word(word); |
| 3398 | if (!r) |
| 3399 | return 0; |
| 3400 | |
| 3401 | 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] | 3402 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3403 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE) |
| 3404 | /* "case word IN ..." - IN part starts first match part */ |
| 3405 | r = &reserved_match; |
| 3406 | else |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3407 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3408 | if (r->flag == 0) { /* '!' */ |
| 3409 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3410 | syntax(NULL); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3411 | IF_HAS_KEYWORDS(ctx->ctx_res_w = RES_SNTX;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3412 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3413 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3414 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3415 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3416 | if (r->flag & FLAG_START) { |
| 3417 | struct parse_context *new; |
| 3418 | debug_printf("push stack\n"); |
| 3419 | new = xmalloc(sizeof(*new)); |
| 3420 | *new = *ctx; /* physical copy */ |
| 3421 | initialize_context(ctx); |
| 3422 | ctx->stack = new; |
| 3423 | } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) { |
| 3424 | syntax(NULL); |
| 3425 | ctx->ctx_res_w = RES_SNTX; |
| 3426 | return 1; |
| 3427 | } |
| 3428 | ctx->ctx_res_w = r->res; |
| 3429 | ctx->old_flag = r->flag; |
| 3430 | if (ctx->old_flag & FLAG_END) { |
| 3431 | struct parse_context *old; |
| 3432 | debug_printf("pop stack\n"); |
| 3433 | done_pipe(ctx, PIPE_SEQ); |
| 3434 | old = ctx->stack; |
| 3435 | old->command->group = ctx->list_head; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3436 | old->command->grp_type = GRP_NORMAL; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3437 | *ctx = *old; /* physical copy */ |
| 3438 | free(old); |
| 3439 | } |
| 3440 | word->o_assignment = r->assignment_flag; |
| 3441 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3442 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3443 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3444 | |
Denis Vlasenko | ddc8ae3 | 2008-10-14 12:50:34 +0000 | [diff] [blame] | 3445 | //TODO: many, many callers don't check error from done_word() |
| 3446 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3447 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3448 | * Normal return is 0. Syntax errors return 1. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3449 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3450 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3451 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3452 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3453 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3454 | if (word->length == 0 && word->nonnull == 0) { |
| 3455 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3456 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3457 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3458 | /* If this word wasn't an assignment, next ones definitely |
| 3459 | * can't be assignments. Even if they look like ones. */ |
| 3460 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3461 | && word->o_assignment != WORD_IS_KEYWORD |
| 3462 | ) { |
| 3463 | word->o_assignment = NOT_ASSIGNMENT; |
| 3464 | } else { |
| 3465 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3466 | command->assignment_cnt++; |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3467 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3468 | } |
| 3469 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3470 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3471 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3472 | * only if run as "bash", not "sh" */ |
| 3473 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3474 | word->o_assignment = NOT_ASSIGNMENT; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3475 | debug_printf("word stored in rd_filename: '%s'\n", word->data); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3476 | } else { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3477 | /* "{ echo foo; } echo bar" - bad */ |
| 3478 | /* NB: bash allows e.g. "if true; then { echo foo; } fi". TODO? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3479 | if (command->group) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3480 | syntax(NULL); |
| 3481 | debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n"); |
| 3482 | return 1; |
| 3483 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3484 | #if HAS_KEYWORDS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3485 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3486 | if (ctx->ctx_dsemicolon |
| 3487 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3488 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3489 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3490 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3491 | ctx->ctx_dsemicolon = 0; |
| 3492 | } else |
| 3493 | #endif |
| 3494 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3495 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3496 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3497 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3498 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3499 | #endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3500 | ) { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3501 | debug_printf_parse(": checking '%s' for reserved-ness\n", word->data); |
| 3502 | if (reserved_word(word, ctx)) { |
| 3503 | o_reset(word); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3504 | debug_printf_parse("done_word return %d\n", (ctx->ctx_res_w == RES_SNTX)); |
| 3505 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3506 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3507 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3508 | #endif |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3509 | 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] | 3510 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3511 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3512 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3513 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3514 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3515 | char *p = word->data; |
| 3516 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3517 | && (p[1] & 0x7f) == '@' |
| 3518 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3519 | ) { |
| 3520 | p += 3; |
| 3521 | } |
| 3522 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3523 | /* saw no "$@", or not only "$@" but some |
| 3524 | * real text is there too */ |
| 3525 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3526 | * e.g. "", $empty"" etc to not disappear */ |
| 3527 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3528 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3529 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3530 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3531 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3532 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3533 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3534 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3535 | o_reset(word); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3536 | ctx->pending_redirect = NULL; |
| 3537 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3538 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3539 | /* Force FOR to have just one word (variable name) */ |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3540 | /* NB: basically, this makes hush see "for v in ..." syntax as if |
| 3541 | * as it is "for v; in ...". FOR and IN become two pipe structs |
| 3542 | * in parse tree. */ |
| 3543 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3544 | //TODO: check that command->argv[0] is a valid variable name! |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3545 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3546 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3547 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3548 | #if ENABLE_HUSH_CASE |
| 3549 | /* Force CASE to have just one word */ |
| 3550 | if (ctx->ctx_res_w == RES_CASE) { |
| 3551 | done_pipe(ctx, PIPE_SEQ); |
| 3552 | } |
| 3553 | #endif |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3554 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3555 | return 0; |
| 3556 | } |
| 3557 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3558 | /* If a redirect is immediately preceded by a number, that number is |
| 3559 | * supposed to tell which file descriptor to redirect. This routine |
| 3560 | * looks for such preceding numbers. In an ideal world this routine |
| 3561 | * needs to handle all the following classes of redirects... |
| 3562 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3563 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3564 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3565 | * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo |
| 3566 | * A -1 output from this program means no valid number was found, so the |
| 3567 | * caller should use the appropriate default for this redirection. |
| 3568 | */ |
| 3569 | static int redirect_opt_num(o_string *o) |
| 3570 | { |
| 3571 | int num; |
| 3572 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3573 | if (o->length == 0) |
| 3574 | return -1; |
| 3575 | for (num = 0; num < o->length; num++) { |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3576 | if (!isdigit(o->data[num])) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3577 | return -1; |
| 3578 | } |
| 3579 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3580 | num = atoi(o->data); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3581 | o_reset(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3582 | return num; |
| 3583 | } |
| 3584 | |
Mike Frysinger | ddbee97 | 2009-03-22 22:48:41 +0000 | [diff] [blame] | 3585 | static int parse_stream(o_string *dest, struct parse_context *ctx, |
| 3586 | struct in_str *input0, const char *end_trigger); |
| 3587 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3588 | #if ENABLE_HUSH_TICK |
"Vladimir N. Oleynik" | 19c3701 | 2005-09-22 14:33:15 +0000 | [diff] [blame] | 3589 | static FILE *generate_stream_from_list(struct pipe *head) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3590 | { |
| 3591 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3592 | int pid, channel[2]; |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3593 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 3594 | xpipe(channel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3595 | /* *** NOMMU WARNING *** */ |
| 3596 | /* By using vfork here, we suspend parent till child exits or execs. |
| 3597 | * If child will not do it before it fills the pipe, it can block forever |
| 3598 | * in write(STDOUT_FILENO), and parent (shell) will be also stuck. |
Denis Vlasenko | 3fe4f98 | 2008-06-09 16:02:39 +0000 | [diff] [blame] | 3599 | * Try this script: |
| 3600 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >TESTFILE |
| 3601 | * huge=`cat TESTFILE` # will block here forever |
| 3602 | * echo OK |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3603 | */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 3604 | pid = BB_MMU ? fork() : vfork(); |
| 3605 | if (pid < 0) |
| 3606 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3607 | if (pid == 0) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 3608 | if (ENABLE_HUSH_JOB) |
| 3609 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 3610 | close(channel[0]); /* NB: close _first_, then move fd! */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3611 | xmove_fd(channel[1], 1); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3612 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3613 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3614 | G.run_list_level = 1; |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3615 | #endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3616 | /* Process substitution is not considered to be usual |
| 3617 | * 'command execution'. |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 3618 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 3619 | */ |
| 3620 | set_jobctrl_signals_to_IGN(); |
| 3621 | |
| 3622 | /* Note: freeing 'head' here would break NOMMU. */ |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3623 | _exit(run_list(head)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3624 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3625 | close(channel[1]); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3626 | pf = fdopen(channel[0], "r"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3627 | return pf; |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3628 | /* 'head' is freed by the caller */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3631 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3632 | static int process_command_subs(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3633 | struct in_str *input, |
| 3634 | const char *subst_end) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3635 | { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3636 | int retcode, ch, eol_cnt; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3637 | o_string result = NULL_O_STRING; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3638 | struct parse_context inner; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3639 | FILE *p; |
| 3640 | struct in_str pipe_str; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3641 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3642 | initialize_context(&inner); |
| 3643 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3644 | /* Recursion to generate command */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3645 | retcode = parse_stream(&result, &inner, input, subst_end); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3646 | if (retcode != 0) |
| 3647 | return retcode; /* syntax error or EOF */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3648 | done_word(&result, &inner); |
| 3649 | done_pipe(&inner, PIPE_SEQ); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3650 | o_free(&result); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3651 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3652 | p = generate_stream_from_list(inner.list_head); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3653 | if (p == NULL) |
| 3654 | return 1; |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 3655 | close_on_exec_on(fileno(p)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3656 | setup_file_in_str(&pipe_str, p); |
| 3657 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3658 | /* Now send results of command back into original context */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3659 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3660 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3661 | if (ch == '\n') { |
| 3662 | eol_cnt++; |
| 3663 | continue; |
| 3664 | } |
| 3665 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3666 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3667 | eol_cnt--; |
| 3668 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3669 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | debug_printf("done reading from pipe, pclose()ing\n"); |
| 3673 | /* This is the step that wait()s for the child. Should be pretty |
| 3674 | * 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] | 3675 | * to do better, by using wait(), and keeping track of background jobs |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3676 | * at the same time. That would be a lot of work, and contrary |
| 3677 | * to the KISS philosophy of this program. */ |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3678 | retcode = fclose(p); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3679 | free_pipe_list(inner.list_head, /* indent: */ 0); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3680 | debug_printf("closed FILE from child, retcode=%d\n", retcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3681 | return retcode; |
| 3682 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3683 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3684 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3685 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3686 | struct in_str *input, int ch) |
| 3687 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3688 | /* dest contains characters seen prior to ( or {. |
| 3689 | * Typically it's empty, but for functions defs, |
| 3690 | * it contains function name (without '()'). */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3691 | int rcode; |
| 3692 | const char *endch = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3693 | struct parse_context sub; |
| 3694 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3695 | |
| 3696 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3697 | #if ENABLE_HUSH_FUNCTIONS |
| 3698 | if (ch == 'F') { /* function definition? */ |
| 3699 | bb_error_msg("aha '%s' is a function, parsing it...", dest->data); |
| 3700 | //command->fname = dest->data; |
| 3701 | command->grp_type = GRP_FUNCTION; |
| 3702 | //TODO: review every o_reset() location... do they handle all o_string fields correctly? |
| 3703 | memset(dest, 0, sizeof(*dest)); |
| 3704 | } |
| 3705 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3706 | if (command->argv /* word [word](... */ |
| 3707 | || dest->length /* word(... */ |
| 3708 | || dest->nonnull /* ""(... */ |
| 3709 | ) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3710 | syntax(NULL); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3711 | debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n"); |
| 3712 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3713 | } |
| 3714 | initialize_context(&sub); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3715 | endch = "}"; |
| 3716 | if (ch == '(') { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3717 | endch = ")"; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3718 | command->grp_type = GRP_SUBSHELL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3719 | } |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3720 | rcode = parse_stream(dest, &sub, input, endch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3721 | if (rcode == 0) { |
| 3722 | done_word(dest, &sub); /* finish off the final word in the subcontext */ |
| 3723 | done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3724 | command->group = sub.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3725 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3726 | debug_printf_parse("parse_group return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3727 | return rcode; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3728 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3731 | #if ENABLE_HUSH_TICK |
| 3732 | /* Subroutines for copying $(...) and `...` things */ |
| 3733 | static void add_till_backquote(o_string *dest, struct in_str *input); |
| 3734 | /* '...' */ |
| 3735 | static void add_till_single_quote(o_string *dest, struct in_str *input) |
| 3736 | { |
| 3737 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3738 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3739 | if (ch == EOF) |
| 3740 | break; |
| 3741 | if (ch == '\'') |
| 3742 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3743 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3744 | } |
| 3745 | } |
| 3746 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
| 3747 | static void add_till_double_quote(o_string *dest, struct in_str *input) |
| 3748 | { |
| 3749 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3750 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3751 | if (ch == '"') |
| 3752 | break; |
| 3753 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3754 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3755 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3756 | } |
| 3757 | if (ch == EOF) |
| 3758 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3759 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3760 | if (ch == '`') { |
| 3761 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3762 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3763 | continue; |
| 3764 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3765 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3766 | } |
| 3767 | } |
| 3768 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 3769 | * \` quoting. |
| 3770 | * "Within the backquoted style of command substitution, backslash |
| 3771 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 3772 | * The search for the matching backquote shall be satisfied by the first |
| 3773 | * backquote found without a preceding backslash; during this search, |
| 3774 | * if a non-escaped backquote is encountered within a shell comment, |
| 3775 | * a here-document, an embedded command substitution of the $(command) |
| 3776 | * form, or a quoted string, undefined results occur. A single-quoted |
| 3777 | * or double-quoted string that begins, but does not end, within the |
| 3778 | * "`...`" sequence produces undefined results." |
| 3779 | * Example Output |
| 3780 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 3781 | */ |
| 3782 | static void add_till_backquote(o_string *dest, struct in_str *input) |
| 3783 | { |
| 3784 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3785 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3786 | if (ch == '`') |
| 3787 | break; |
| 3788 | if (ch == '\\') { /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3789 | int ch2 = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3790 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3791 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3792 | ch = ch2; |
| 3793 | } |
| 3794 | if (ch == EOF) |
| 3795 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3796 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3797 | } |
| 3798 | } |
| 3799 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 3800 | * quoting and nested ()s. |
| 3801 | * "With the $(command) style of command substitution, all characters |
| 3802 | * following the open parenthesis to the matching closing parenthesis |
| 3803 | * constitute the command. Any valid shell script can be used for command, |
| 3804 | * except a script consisting solely of redirections which produces |
| 3805 | * unspecified results." |
| 3806 | * Example Output |
| 3807 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 3808 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 3809 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 3810 | */ |
| 3811 | static void add_till_closing_curly_brace(o_string *dest, struct in_str *input) |
| 3812 | { |
| 3813 | int count = 0; |
| 3814 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3815 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3816 | if (ch == EOF) |
| 3817 | break; |
| 3818 | if (ch == '(') |
| 3819 | count++; |
| 3820 | if (ch == ')') |
| 3821 | if (--count < 0) |
| 3822 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3823 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3824 | if (ch == '\'') { |
| 3825 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3826 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3827 | continue; |
| 3828 | } |
| 3829 | if (ch == '"') { |
| 3830 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3831 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3832 | continue; |
| 3833 | } |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3834 | if (ch == '\\') { /* \x. Copy verbatim. Important for \(, \) */ |
| 3835 | ch = i_getch(input); |
| 3836 | if (ch == EOF) |
| 3837 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3838 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3839 | continue; |
| 3840 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3841 | } |
| 3842 | } |
| 3843 | #endif /* ENABLE_HUSH_TICK */ |
| 3844 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3845 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3846 | static int handle_dollar(o_string *dest, struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3847 | { |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3848 | int expansion; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3849 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3850 | unsigned char quote_mask = dest->o_quote ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3851 | |
| 3852 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3853 | if (isalpha(ch)) { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3854 | i_getch(input); |
| 3855 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3856 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3857 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3858 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3859 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3860 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3861 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3862 | if (!isalnum(ch) && ch != '_') |
| 3863 | break; |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3864 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3865 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3866 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3867 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3868 | make_one_char_var: |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3869 | i_getch(input); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3870 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3871 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3872 | o_addchr(dest, ch | quote_mask); |
| 3873 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3874 | } else switch (ch) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3875 | case '$': /* pid */ |
| 3876 | case '!': /* last bg pid */ |
| 3877 | case '?': /* last exit code */ |
| 3878 | case '#': /* number of args */ |
| 3879 | case '*': /* args */ |
| 3880 | case '@': /* args */ |
| 3881 | goto make_one_char_var; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3882 | case '{': { |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 3883 | bool first_char, all_digits; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3884 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3885 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3886 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3887 | /* XXX maybe someone will try to escape the '}' */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3888 | expansion = 0; |
| 3889 | first_char = true; |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 3890 | all_digits = false; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3891 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3892 | ch = i_getch(input); |
Denis Vlasenko | b055001 | 2007-05-24 12:18:16 +0000 | [diff] [blame] | 3893 | if (ch == '}') |
| 3894 | break; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3895 | |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 3896 | if (first_char) { |
| 3897 | if (ch == '#') |
| 3898 | /* ${#var}: length of var contents */ |
| 3899 | goto char_ok; |
| 3900 | else if (isdigit(ch)) { |
| 3901 | all_digits = true; |
| 3902 | goto char_ok; |
| 3903 | } |
| 3904 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3905 | |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 3906 | if (expansion < 2 && |
| 3907 | ((all_digits && !isdigit(ch)) || |
| 3908 | (!all_digits && !isalnum(ch) && ch != '_'))) |
| 3909 | { |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3910 | /* handle parameter expansions |
| 3911 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 3912 | */ |
| 3913 | if (first_char) |
| 3914 | goto case_default; |
| 3915 | switch (ch) { |
| 3916 | case ':': /* null modifier */ |
| 3917 | if (expansion == 0) { |
| 3918 | debug_printf_parse(": null modifier\n"); |
| 3919 | ++expansion; |
| 3920 | break; |
| 3921 | } |
| 3922 | goto case_default; |
| 3923 | |
| 3924 | #if 0 /* not implemented yet :( */ |
| 3925 | case '#': /* remove prefix */ |
| 3926 | case '%': /* remove suffix */ |
| 3927 | if (expansion == 0) { |
| 3928 | debug_printf_parse(": remove suffix/prefix\n"); |
| 3929 | expansion = 2; |
| 3930 | break; |
| 3931 | } |
| 3932 | goto case_default; |
| 3933 | #endif |
| 3934 | |
| 3935 | case '-': /* default value */ |
| 3936 | case '=': /* assign default */ |
| 3937 | case '+': /* alternative */ |
| 3938 | case '?': /* error indicate */ |
| 3939 | debug_printf_parse(": parameter expansion\n"); |
| 3940 | expansion = 2; |
| 3941 | break; |
| 3942 | |
| 3943 | default: |
| 3944 | case_default: |
| 3945 | syntax("unterminated ${name}"); |
| 3946 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 3947 | return 1; |
| 3948 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3949 | } |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 3950 | |
| 3951 | char_ok: |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3952 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3953 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3954 | quote_mask = 0; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3955 | first_char = false; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3956 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3957 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3958 | break; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3959 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3960 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3961 | case '(': { |
| 3962 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3963 | i_getch(input); |
| 3964 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3965 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3966 | add_till_closing_curly_brace(dest, input); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3967 | //debug_printf_subst("SUBST RES2 '%s'\n", dest->data + pos); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3968 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3969 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3970 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3971 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3972 | case '_': |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3973 | i_getch(input); |
| 3974 | ch = i_peek(input); |
| 3975 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 3976 | ch = '_'; |
| 3977 | goto make_var; |
| 3978 | } |
| 3979 | /* else: it's $_ */ |
| 3980 | case '-': |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3981 | /* still unhandled, but should be eventually */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3982 | bb_error_msg("unhandled syntax: $%c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3983 | return 1; |
| 3984 | break; |
| 3985 | default: |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3986 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3987 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3988 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3989 | return 0; |
| 3990 | } |
| 3991 | |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3992 | /* Scan input, call done_word() whenever full IFS delimited word was seen. |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3993 | * Call done_pipe if '\n' was seen (and end_trigger != NULL). |
| 3994 | * Return code is 0 if end_trigger char is met, |
| 3995 | * -1 on EOF (but if end_trigger == NULL then return 0), |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3996 | * 1 for syntax error */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3997 | static int parse_stream(o_string *dest, struct parse_context *ctx, |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3998 | struct in_str *input, const char *end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3999 | { |
"Vladimir N. Oleynik" | 4ccd2b4 | 2006-01-31 09:27:48 +0000 | [diff] [blame] | 4000 | int ch, m; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4001 | int redir_fd; |
| 4002 | redir_type redir_style; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4003 | int shadow_quote = dest->o_quote; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4004 | int next; |
| 4005 | |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 4006 | /* 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] | 4007 | * 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] | 4008 | * found. When recursing, quote state is passed in via dest->o_quote. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4009 | |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4010 | 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] | 4011 | |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4012 | while (1) { |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4013 | m = CHAR_IFS; |
| 4014 | next = '\0'; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4015 | ch = i_getch(input); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4016 | if (ch != EOF) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4017 | m = G.charmap[ch]; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4018 | if (ch != '\n') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4019 | next = i_peek(input); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4020 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4021 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4022 | debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n", |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 4023 | ch, ch, m, dest->o_quote); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4024 | if (m == CHAR_ORDINARY |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4025 | || (m != CHAR_SPECIAL && shadow_quote) |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4026 | ) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4027 | if (ch == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4028 | syntax("unterminated \""); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4029 | debug_printf_parse("parse_stream return 1: unterminated \"\n"); |
| 4030 | return 1; |
| 4031 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 4032 | o_addQchr(dest, ch); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4033 | if ((dest->o_assignment == MAYBE_ASSIGNMENT |
| 4034 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4035 | && ch == '=' |
| 4036 | && is_assignment(dest->data) |
| 4037 | ) { |
| 4038 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 4039 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4040 | continue; |
| 4041 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4042 | if (m == CHAR_IFS) { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4043 | if (done_word(dest, ctx)) { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4044 | debug_printf_parse("parse_stream return 1: done_word!=0\n"); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4045 | return 1; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 4046 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4047 | if (ch == EOF) |
| 4048 | break; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4049 | /* If we aren't performing a substitution, treat |
| 4050 | * a newline as a command separator. |
| 4051 | * [why we don't handle it exactly like ';'? --vda] */ |
| 4052 | if (end_trigger && ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4053 | #if ENABLE_HUSH_CASE |
| 4054 | /* "case ... in <newline> word) ..." - |
| 4055 | * newlines are ignored (but ';' wouldn't be) */ |
| 4056 | if (dest->length == 0 // && argv[0] == NULL |
| 4057 | && ctx->ctx_res_w == RES_MATCH |
| 4058 | ) { |
| 4059 | continue; |
| 4060 | } |
| 4061 | #endif |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4062 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4063 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4064 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4065 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4066 | if (end_trigger) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4067 | if (!shadow_quote && strchr(end_trigger, ch)) { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4068 | /* Special case: (...word) makes last word terminate, |
| 4069 | * as if ';' is seen */ |
| 4070 | if (ch == ')') { |
| 4071 | done_word(dest, ctx); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4072 | //err chk? |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4073 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4074 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4075 | } |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4076 | if (!HAS_KEYWORDS |
| 4077 | IF_HAS_KEYWORDS(|| (ctx->ctx_res_w == RES_NONE && ctx->old_flag == 0)) |
| 4078 | ) { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4079 | debug_printf_parse("parse_stream return 0: end_trigger char found\n"); |
| 4080 | return 0; |
| 4081 | } |
| 4082 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4083 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4084 | if (m == CHAR_IFS) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4085 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4086 | |
| 4087 | if (dest->o_assignment == MAYBE_ASSIGNMENT) { |
| 4088 | /* ch is a special char and thus this word |
| 4089 | * cannot be an assignment: */ |
| 4090 | dest->o_assignment = NOT_ASSIGNMENT; |
| 4091 | } |
| 4092 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4093 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4094 | case '#': |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4095 | if (dest->length == 0 && !shadow_quote) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4096 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4097 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4098 | if (ch == EOF || ch == '\n') |
| 4099 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4100 | i_getch(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4101 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4102 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 4103 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4104 | } |
| 4105 | break; |
| 4106 | case '\\': |
| 4107 | if (next == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4108 | syntax("\\<eof>"); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4109 | debug_printf_parse("parse_stream return 1: \\<eof>\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4110 | return 1; |
| 4111 | } |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4112 | /* bash: |
| 4113 | * "The backslash retains its special meaning [in "..."] |
| 4114 | * only when followed by one of the following characters: |
| 4115 | * $, `, ", \, or <newline>. A double quote may be quoted |
| 4116 | * within double quotes by preceding it with a backslash. |
| 4117 | * If enabled, history expansion will be performed unless |
| 4118 | * an ! appearing in double quotes is escaped using |
| 4119 | * a backslash. The backslash preceding the ! is not removed." |
| 4120 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4121 | if (shadow_quote) { //NOT SURE dest->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4122 | if (strchr("$`\"\\", next) != NULL) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 4123 | o_addqchr(dest, i_getch(input)); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4124 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 4125 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4126 | } |
| 4127 | } else { |
| 4128 | o_addchr(dest, '\\'); |
| 4129 | o_addchr(dest, i_getch(input)); |
| 4130 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4131 | break; |
| 4132 | case '$': |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4133 | if (handle_dollar(dest, input) != 0) { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4134 | debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n"); |
| 4135 | return 1; |
| 4136 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4137 | break; |
| 4138 | case '\'': |
| 4139 | dest->nonnull = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4140 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4141 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4142 | if (ch == EOF) { |
| 4143 | syntax("unterminated '"); |
| 4144 | debug_printf_parse("parse_stream return 1: unterminated '\n"); |
| 4145 | return 1; |
| 4146 | } |
| 4147 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4148 | break; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4149 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 4150 | o_addqchr(dest, ch); |
| 4151 | else |
| 4152 | o_addchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4153 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4154 | break; |
| 4155 | case '"': |
| 4156 | dest->nonnull = 1; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4157 | shadow_quote ^= 1; /* invert */ |
| 4158 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 4159 | dest->o_quote ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4160 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4161 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4162 | case '`': { |
| 4163 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4164 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4165 | o_addchr(dest, shadow_quote /*or dest->o_quote??*/ ? 0x80 | '`' : '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4166 | add_till_backquote(dest, input); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4167 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 4168 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4169 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4170 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4171 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4172 | case '>': |
| 4173 | redir_fd = redirect_opt_num(dest); |
| 4174 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4175 | redir_style = REDIRECT_OVERWRITE; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4176 | if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4177 | redir_style = REDIRECT_APPEND; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4178 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4179 | } |
| 4180 | #if 0 |
| 4181 | else if (next == '(') { |
| 4182 | syntax(">(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4183 | debug_printf_parse("parse_stream return 1: >(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4184 | return 1; |
| 4185 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4186 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4187 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 4188 | break; |
| 4189 | case '<': |
| 4190 | redir_fd = redirect_opt_num(dest); |
| 4191 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4192 | redir_style = REDIRECT_INPUT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4193 | if (next == '<') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4194 | redir_style = REDIRECT_HEREIS; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4195 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4196 | } else if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4197 | redir_style = REDIRECT_IO; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4198 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4199 | } |
| 4200 | #if 0 |
| 4201 | else if (next == '(') { |
| 4202 | syntax("<(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4203 | debug_printf_parse("parse_stream return 1: <(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4204 | return 1; |
| 4205 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4206 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4207 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 4208 | break; |
| 4209 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4210 | #if ENABLE_HUSH_CASE |
| 4211 | case_semi: |
| 4212 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4213 | done_word(dest, ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4214 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4215 | #if ENABLE_HUSH_CASE |
| 4216 | /* Eat multiple semicolons, detect |
| 4217 | * whether it means something special */ |
| 4218 | while (1) { |
| 4219 | ch = i_peek(input); |
| 4220 | if (ch != ';') |
| 4221 | break; |
| 4222 | i_getch(input); |
| 4223 | if (ctx->ctx_res_w == RES_CASEI) { |
| 4224 | ctx->ctx_dsemicolon = 1; |
| 4225 | ctx->ctx_res_w = RES_MATCH; |
| 4226 | break; |
| 4227 | } |
| 4228 | } |
| 4229 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4230 | new_cmd: |
| 4231 | /* We just finished a cmd. New one may start |
| 4232 | * with an assignment */ |
| 4233 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4234 | break; |
| 4235 | case '&': |
| 4236 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4237 | if (next == '&') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4238 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4239 | done_pipe(ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4240 | } else { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4241 | done_pipe(ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4242 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4243 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4244 | case '|': |
| 4245 | done_word(dest, ctx); |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4246 | #if ENABLE_HUSH_CASE |
| 4247 | if (ctx->ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4248 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4249 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4250 | if (next == '|') { /* || */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4251 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4252 | done_pipe(ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4253 | } else { |
| 4254 | /* we could pick up a file descriptor choice here |
| 4255 | * with redirect_opt_num(), but bash doesn't do it. |
| 4256 | * "echo foo 2| cat" yields "foo 2". */ |
| 4257 | done_command(ctx); |
| 4258 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4259 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4260 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4261 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4262 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4263 | if (ctx->ctx_res_w == RES_MATCH |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4264 | && ctx->command->argv == NULL /* not (word|(... */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4265 | && dest->length == 0 /* not word(... */ |
| 4266 | && dest->nonnull == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4267 | ) { |
| 4268 | continue; |
| 4269 | } |
| 4270 | #endif |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4271 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4272 | if (dest->length != 0 /* not just () but word() */ |
| 4273 | && dest->nonnull == 0 /* not a"b"c() */ |
| 4274 | && ctx->command->argv == NULL /* it's the first word */ |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4275 | //TODO: "func ( ) {...}" - note spaces - is valid format too in bash |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4276 | && i_peek(input) == ')' |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4277 | && !match_reserved_word(dest) |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4278 | ) { |
| 4279 | bb_error_msg("seems like a function definition"); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4280 | i_getch(input); |
| 4281 | do { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4282 | //TODO: do it properly. |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4283 | ch = i_getch(input); |
| 4284 | } while (ch == ' ' || ch == '\n'); |
| 4285 | if (ch != '{') { |
| 4286 | syntax("was expecting {"); |
| 4287 | debug_printf_parse("parse_stream return 1\n"); |
| 4288 | return 1; |
| 4289 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4290 | ch = 'F'; /* magic value */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4291 | } |
| 4292 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4293 | case '{': |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4294 | if (parse_group(dest, ctx, input, ch) != 0) { |
| 4295 | 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] | 4296 | return 1; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4297 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4298 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4299 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4300 | #if ENABLE_HUSH_CASE |
| 4301 | if (ctx->ctx_res_w == RES_MATCH) |
| 4302 | goto case_semi; |
| 4303 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4304 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4305 | /* proper use of this character is caught by end_trigger: |
| 4306 | * if we see {, we call parse_group(..., end_trigger='}') |
| 4307 | * and it will match } earlier (not here). */ |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4308 | syntax("unexpected } or )"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4309 | debug_printf_parse("parse_stream return 1: unexpected '}'\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4310 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4311 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4312 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4313 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4314 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4315 | } /* while (1) */ |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4316 | debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL)); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4317 | if (end_trigger) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4318 | return -1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4319 | return 0; |
| 4320 | } |
| 4321 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4322 | static void set_in_charmap(const char *set, int code) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4323 | { |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 4324 | while (*set) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4325 | G.charmap[(unsigned char)*set++] = code; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4326 | } |
| 4327 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4328 | static void update_charmap(void) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4329 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4330 | G.ifs = getenv("IFS"); |
| 4331 | if (G.ifs == NULL) |
| 4332 | G.ifs = " \t\n"; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4333 | /* Precompute a list of 'flow through' behavior so it can be treated |
| 4334 | * quickly up front. Computation is necessary because of IFS. |
| 4335 | * Special case handling of IFS == " \t\n" is not implemented. |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4336 | * The charmap[] array only really needs two bits each, |
| 4337 | * and on most machines that would be faster (reduced L1 cache use). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4338 | */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4339 | memset(G.charmap, CHAR_ORDINARY, sizeof(G.charmap)); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4340 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4341 | set_in_charmap("\\$\"`", CHAR_SPECIAL); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4342 | #else |
| 4343 | set_in_charmap("\\$\"", CHAR_SPECIAL); |
| 4344 | #endif |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4345 | set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4346 | set_in_charmap(G.ifs, CHAR_IFS); /* are ordinary if quoted */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4347 | } |
| 4348 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4349 | /* Most recursion does not come through here, the exception is |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4350 | * from builtin_source() and builtin_eval() */ |
| 4351 | 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] | 4352 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4353 | struct parse_context ctx; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4354 | o_string temp = NULL_O_STRING; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4355 | int rcode; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4356 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4357 | do { |
| 4358 | initialize_context(&ctx); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4359 | update_charmap(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4360 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 4361 | inp->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4362 | #endif |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4363 | /* We will stop & execute after each ';' or '\n'. |
| 4364 | * Example: "sleep 9999; echo TEST" + ctrl-C: |
| 4365 | * TEST should be printed */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4366 | temp.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4367 | rcode = parse_stream(&temp, &ctx, inp, ";\n"); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4368 | #if HAS_KEYWORDS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4369 | if (rcode != 1 && ctx.old_flag != 0) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4370 | syntax(NULL); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4371 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4372 | #endif |
| 4373 | if (rcode != 1 IF_HAS_KEYWORDS(&& ctx.old_flag == 0)) { |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4374 | done_word(&temp, &ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4375 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4376 | debug_print_tree(ctx.list_head, 0); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4377 | debug_printf_exec("parse_stream_outer: run_and_free_list\n"); |
| 4378 | run_and_free_list(ctx.list_head); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4379 | } else { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4380 | /* We arrive here also if rcode == 1 (error in parse_stream) */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4381 | #if HAS_KEYWORDS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4382 | if (ctx.old_flag != 0) { |
| 4383 | free(ctx.stack); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4384 | o_reset(&temp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4385 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4386 | #endif |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 4387 | /*temp.nonnull = 0; - o_free does it below */ |
| 4388 | /*temp.o_quote = 0; - o_free does it below */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4389 | free_pipe_list(ctx.list_head, /* indent: */ 0); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4390 | /* Discard all unprocessed line input, force prompt on */ |
| 4391 | inp->p = NULL; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4392 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4393 | inp->promptme = 1; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4394 | #endif |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4395 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4396 | o_free(&temp); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4397 | /* loop on syntax errors, return on EOF: */ |
| 4398 | } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4399 | return 0; |
| 4400 | } |
| 4401 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4402 | static int parse_and_run_string(const char *s, int parse_flag) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4403 | { |
| 4404 | struct in_str input; |
| 4405 | setup_string_in_str(&input, s); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4406 | return parse_and_run_stream(&input, parse_flag); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4409 | static int parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4410 | { |
| 4411 | int rcode; |
| 4412 | struct in_str input; |
| 4413 | setup_file_in_str(&input, f); |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4414 | rcode = parse_and_run_stream(&input, 0 /* parse_flag */); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4415 | return rcode; |
| 4416 | } |
| 4417 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4418 | #if ENABLE_HUSH_JOB |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4419 | /* Make sure we have a controlling tty. If we get started under a job |
| 4420 | * aware app (like bash for example), make sure we are now in charge so |
| 4421 | * we don't fight over who gets the foreground */ |
Eric Andersen | eaecbf3 | 2001-10-31 10:41:31 +0000 | [diff] [blame] | 4422 | static void setup_job_control(void) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4423 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4424 | pid_t shell_pgrp; |
| 4425 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4426 | shell_pgrp = getpgrp(); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4427 | |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 4428 | /* If we were ran as 'hush &', |
| 4429 | * sleep until we are in the foreground. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4430 | while (tcgetpgrp(G.interactive_fd) != shell_pgrp) { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4431 | /* Send TTIN to ourself (should stop us) */ |
Denis Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 4432 | kill(- shell_pgrp, SIGTTIN); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 4433 | shell_pgrp = getpgrp(); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4434 | } |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4435 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4436 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 4437 | set_fatal_signals_to_sigexit(); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4438 | |
| 4439 | /* Put ourselves in our own process group. */ |
Bernhard Reutner-Fischer | 864329d | 2008-09-25 10:55:05 +0000 | [diff] [blame] | 4440 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4441 | /* Grab control of the terminal. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4442 | tcsetpgrp(G.interactive_fd, getpid()); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4443 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4444 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4445 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4446 | static int set_mode(const char cstate, const char mode) |
| 4447 | { |
| 4448 | int state = (cstate == '-' ? 1 : 0); |
| 4449 | switch (mode) { |
| 4450 | case 'n': G.fake_mode = state; break; |
| 4451 | case 'x': /*G.debug_mode = state;*/ break; |
| 4452 | default: return EXIT_FAILURE; |
| 4453 | } |
| 4454 | return EXIT_SUCCESS; |
| 4455 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4456 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 4457 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 4458 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4459 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4460 | static const struct variable const_shell_ver = { |
| 4461 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4462 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4463 | .max_len = 1, /* 0 can provoke free(name) */ |
| 4464 | .flg_export = 1, |
| 4465 | .flg_read_only = 1, |
| 4466 | }; |
| 4467 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4468 | int opt; |
| 4469 | FILE *input; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4470 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4471 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 4472 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 4473 | INIT_G(); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4474 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4475 | G.root_pid = getpid(); |
Denis Vlasenko | 16c2fea | 2008-06-17 12:28:44 +0000 | [diff] [blame] | 4476 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4477 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4478 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 4479 | G.top_var = &G.shell_ver; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4480 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4481 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 4482 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4483 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4484 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4485 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4486 | if (e) while (*e) { |
| 4487 | char *value = strchr(*e, '='); |
| 4488 | if (value) { /* paranoia */ |
| 4489 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 4490 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 4491 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4492 | cur_var->max_len = strlen(*e); |
| 4493 | cur_var->flg_export = 1; |
| 4494 | } |
| 4495 | e++; |
| 4496 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4497 | debug_printf_env("putenv '%s'\n", hush_version_str); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4498 | putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 4499 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4500 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4501 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 4502 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4503 | /* XXX what should these be while sourcing /etc/profile? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4504 | G.global_argc = argc; |
| 4505 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 4506 | /* Initialize some more globals to non-zero values */ |
| 4507 | set_cwd(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4508 | #if ENABLE_HUSH_INTERACTIVE |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 4509 | if (ENABLE_FEATURE_EDITING) |
| 4510 | cmdedit_set_initial_prompt(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4511 | G.PS2 = "> "; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4512 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 4513 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4514 | if (EXIT_SUCCESS) /* otherwise is already done */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4515 | G.last_return_code = EXIT_SUCCESS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4516 | |
| 4517 | if (argv[0] && argv[0][0] == '-') { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4518 | debug_printf("sourcing /etc/profile\n"); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4519 | input = fopen_for_read("/etc/profile"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4520 | if (input != NULL) { |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 4521 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4522 | parse_and_run_file(input); |
Eric Andersen | a90f20b | 2001-06-26 23:00:21 +0000 | [diff] [blame] | 4523 | fclose(input); |
| 4524 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4525 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4526 | input = stdin; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4527 | |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 4528 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
| 4529 | while ((opt = getopt(argc, argv, "c:xins")) > 0) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4530 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4531 | case 'c': |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4532 | G.global_argv = argv + optind; |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 4533 | if (!argv[optind]) { |
| 4534 | /* -c 'script' (no params): prevent empty $0 */ |
| 4535 | *--G.global_argv = argv[0]; |
| 4536 | optind--; |
| 4537 | } /* else -c 'script' PAR0 PAR1: $0 is PAR0 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4538 | G.global_argc = argc - optind; |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4539 | opt = parse_and_run_string(optarg, 0 /* parse_flag */); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4540 | goto final_return; |
| 4541 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4542 | /* Well, we cannot just declare interactiveness, |
| 4543 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4544 | /* G.interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4545 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 4546 | case 's': |
| 4547 | /* "-s" means "read from stdin", but this is how we always |
| 4548 | * operate, so simply do nothing here. */ |
| 4549 | break; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 4550 | case 'n': |
| 4551 | case 'x': |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4552 | if (!set_mode('-', opt)) |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 4553 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4554 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4555 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4556 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 4557 | " or: sh -c command [args]...\n\n"); |
| 4558 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4559 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4560 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4561 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4562 | } |
| 4563 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4564 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4565 | /* 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] | 4566 | * the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 4567 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4568 | * no arguments remaining or the -s flag given |
| 4569 | * standard input is a terminal |
| 4570 | * standard output is a terminal |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4571 | * Refer to Posix.2, the description of the 'sh' utility. */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4572 | if (argv[optind] == NULL && input == stdin |
| 4573 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 4574 | ) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4575 | G.saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 4576 | debug_printf("saved_tty_pgrp=%d\n", G.saved_tty_pgrp); |
| 4577 | if (G.saved_tty_pgrp >= 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4578 | /* try to dup to high fd#, >= 255 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4579 | G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 4580 | if (G.interactive_fd < 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4581 | /* try to dup to any fd */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4582 | G.interactive_fd = dup(STDIN_FILENO); |
| 4583 | if (G.interactive_fd < 0) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4584 | /* give up */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4585 | G.interactive_fd = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4586 | } |
Denis Vlasenko | 2f1bb36 | 2007-04-21 10:01:14 +0000 | [diff] [blame] | 4587 | // TODO: track & disallow any attempts of user |
| 4588 | // to (inadvertently) close/redirect it |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4589 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4590 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4591 | init_signal_mask(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4592 | debug_printf("G.interactive_fd=%d\n", G.interactive_fd); |
| 4593 | if (G.interactive_fd) { |
| 4594 | fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4595 | /* Looks like they want an interactive shell */ |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4596 | setup_job_control(); |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 4597 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 4598 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
| 4599 | die_sleep = -1; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4600 | if (setjmp(die_jmp)) { |
| 4601 | /* xfunc has failed! die die die */ |
| 4602 | hush_exit(xfunc_error_retval); |
| 4603 | } |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4604 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4605 | #elif ENABLE_HUSH_INTERACTIVE |
| 4606 | /* no job control compiled, only prompt/line editing */ |
| 4607 | if (argv[optind] == NULL && input == stdin |
| 4608 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 4609 | ) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4610 | G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 4611 | if (G.interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4612 | /* try to dup to any fd */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4613 | G.interactive_fd = dup(STDIN_FILENO); |
| 4614 | if (G.interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4615 | /* give up */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4616 | G.interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4617 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4618 | if (G.interactive_fd) { |
| 4619 | fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 4620 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4621 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4622 | init_signal_mask(); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4623 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4624 | |
Denis Vlasenko | 701ac18 | 2009-03-28 19:22:08 +0000 | [diff] [blame] | 4625 | #if ENABLE_HUSH_INTERACTIVE && !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 4626 | if (G.interactive_fd) { |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 4627 | printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", bb_banner); |
| 4628 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
| 4629 | } |
Denis Vlasenko | 701ac18 | 2009-03-28 19:22:08 +0000 | [diff] [blame] | 4630 | #endif |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 4631 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4632 | if (argv[optind] == NULL) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4633 | opt = parse_and_run_file(stdin); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4634 | } else { |
| 4635 | debug_printf("\nrunning script '%s'\n", argv[optind]); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4636 | G.global_argv = argv + optind; |
| 4637 | G.global_argc = argc - optind; |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4638 | input = xfopen_for_read(argv[optind]); |
Denis Vlasenko | 459a5ad | 2008-02-11 08:35:03 +0000 | [diff] [blame] | 4639 | fcntl(fileno(input), F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4640 | opt = parse_and_run_file(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4641 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4642 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4643 | final_return: |
| 4644 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4645 | #if ENABLE_FEATURE_CLEAN_UP |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4646 | fclose(input); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4647 | if (G.cwd != bb_msg_unknown) |
| 4648 | free((char*)G.cwd); |
| 4649 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4650 | while (cur_var) { |
| 4651 | struct variable *tmp = cur_var; |
| 4652 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 4653 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4654 | cur_var = cur_var->next; |
| 4655 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4656 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4657 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4658 | hush_exit(opt ? opt : G.last_return_code); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4659 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 4660 | |
| 4661 | |
| 4662 | #if ENABLE_LASH |
| 4663 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 4664 | int lash_main(int argc, char **argv) |
| 4665 | { |
| 4666 | //bb_error_msg("lash is deprecated, please use hush instead"); |
| 4667 | return hush_main(argc, argv); |
| 4668 | } |
| 4669 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4670 | |
| 4671 | |
| 4672 | /* |
| 4673 | * Built-ins |
| 4674 | */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4675 | static int builtin_trap(char **argv) |
| 4676 | { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4677 | int i; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4678 | int sig; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4679 | char *new_cmd; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4680 | |
| 4681 | if (!G.traps) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4682 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4683 | |
| 4684 | if (!argv[1]) { |
| 4685 | /* No args: print all trapped. This isn't 100% correct as we should |
| 4686 | * be escaping the cmd so that it can be pasted back in ... |
| 4687 | */ |
| 4688 | for (i = 0; i < NSIG; ++i) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4689 | if (G.traps[i]) |
| 4690 | printf("trap -- '%s' %s\n", G.traps[i], get_signame(i)); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4691 | return EXIT_SUCCESS; |
| 4692 | } |
| 4693 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4694 | new_cmd = NULL; |
| 4695 | i = 0; |
| 4696 | /* if first arg is decimal: reset all specified */ |
| 4697 | sig = bb_strtou(*++argv, NULL, 10); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4698 | if (errno == 0) { |
| 4699 | int ret; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4700 | set_all: |
| 4701 | ret = EXIT_SUCCESS; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4702 | while (*argv) { |
| 4703 | sig = get_signum(*argv++); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4704 | if (sig < 0 || sig >= NSIG) { |
| 4705 | ret = EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4706 | /* mimic bash message exactly */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4707 | bb_perror_msg("trap: %s: invalid signal specification", argv[i]); |
| 4708 | continue; |
| 4709 | } |
| 4710 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4711 | free(G.traps[sig]); |
| 4712 | G.traps[sig] = xstrdup(new_cmd); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4713 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4714 | debug_printf("trap: setting SIG%s (%i) to '%s'", |
| 4715 | get_signame(sig), sig, G.traps[sig]); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4716 | |
| 4717 | /* There is no signal for 0 (EXIT) */ |
| 4718 | if (sig == 0) |
| 4719 | continue; |
| 4720 | |
| 4721 | if (new_cmd) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4722 | sigaddset(&G.blocked_set, sig); |
| 4723 | } else { |
| 4724 | /* there was a trap handler, we are removing it |
| 4725 | * (if sig has non-DFL handling, |
| 4726 | * we don't need to do anything) */ |
| 4727 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 4728 | continue; |
| 4729 | sigdelset(&G.blocked_set, sig); |
| 4730 | } |
| 4731 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4732 | } |
| 4733 | return ret; |
| 4734 | } |
| 4735 | |
| 4736 | /* first arg is "-": reset all specified to default */ |
| 4737 | /* first arg is "": ignore all specified */ |
| 4738 | /* everything else: execute first arg upon signal */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4739 | if (!argv[1]) { |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4740 | bb_error_msg("trap: invalid arguments"); |
| 4741 | return EXIT_FAILURE; |
| 4742 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4743 | if (LONE_DASH(*argv)) |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4744 | /* nothing! */; |
| 4745 | else |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4746 | new_cmd = *argv; |
| 4747 | argv++; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4748 | goto set_all; |
| 4749 | } |
| 4750 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4751 | static int builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4752 | { |
| 4753 | return 0; |
| 4754 | } |
| 4755 | |
| 4756 | static int builtin_test(char **argv) |
| 4757 | { |
| 4758 | int argc = 0; |
| 4759 | while (*argv) { |
| 4760 | argc++; |
| 4761 | argv++; |
| 4762 | } |
| 4763 | return test_main(argc, argv - argc); |
| 4764 | } |
| 4765 | |
| 4766 | static int builtin_echo(char **argv) |
| 4767 | { |
| 4768 | int argc = 0; |
| 4769 | while (*argv) { |
| 4770 | argc++; |
| 4771 | argv++; |
| 4772 | } |
| 4773 | return echo_main(argc, argv - argc); |
| 4774 | } |
| 4775 | |
| 4776 | static int builtin_eval(char **argv) |
| 4777 | { |
| 4778 | int rcode = EXIT_SUCCESS; |
| 4779 | |
| 4780 | if (argv[1]) { |
| 4781 | char *str = expand_strvec_to_string(argv + 1); |
| 4782 | parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP); |
| 4783 | free(str); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4784 | rcode = G.last_return_code; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4785 | } |
| 4786 | return rcode; |
| 4787 | } |
| 4788 | |
| 4789 | static int builtin_cd(char **argv) |
| 4790 | { |
| 4791 | const char *newdir; |
| 4792 | if (argv[1] == NULL) { |
| 4793 | // bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
| 4794 | // bash says "bash: cd: HOME not set" and does nothing (exitcode 1) |
| 4795 | newdir = getenv("HOME") ? : "/"; |
| 4796 | } else |
| 4797 | newdir = argv[1]; |
| 4798 | if (chdir(newdir)) { |
| 4799 | printf("cd: %s: %s\n", newdir, strerror(errno)); |
| 4800 | return EXIT_FAILURE; |
| 4801 | } |
| 4802 | set_cwd(); |
| 4803 | return EXIT_SUCCESS; |
| 4804 | } |
| 4805 | |
| 4806 | static int builtin_exec(char **argv) |
| 4807 | { |
| 4808 | if (argv[1] == NULL) |
| 4809 | return EXIT_SUCCESS; /* bash does this */ |
| 4810 | { |
| 4811 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4812 | nommu_save_t dummy; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4813 | #endif |
| 4814 | // FIXME: if exec fails, bash does NOT exit! We do... |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4815 | pseudo_exec_argv(&dummy, argv + 1, 0, NULL); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4816 | /* never returns */ |
| 4817 | } |
| 4818 | } |
| 4819 | |
| 4820 | static int builtin_exit(char **argv) |
| 4821 | { |
| 4822 | // TODO: bash does it ONLY on top-level sh exit (+interacive only?) |
| 4823 | //puts("exit"); /* bash does it */ |
| 4824 | // TODO: warn if we have background jobs: "There are stopped jobs" |
| 4825 | // On second consecutive 'exit', exit anyway. |
| 4826 | if (argv[1] == NULL) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4827 | hush_exit(G.last_return_code); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4828 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 4829 | xfunc_error_retval = 255; |
| 4830 | /* bash: exit -2 == exit 254, no error msg */ |
| 4831 | hush_exit(xatoi(argv[1]) & 0xff); |
| 4832 | } |
| 4833 | |
| 4834 | static int builtin_export(char **argv) |
| 4835 | { |
| 4836 | const char *value; |
| 4837 | char *name = argv[1]; |
| 4838 | |
| 4839 | if (name == NULL) { |
| 4840 | // TODO: |
| 4841 | // ash emits: export VAR='VAL' |
| 4842 | // bash: declare -x VAR="VAL" |
| 4843 | // (both also escape as needed (quotes, $, etc)) |
| 4844 | char **e = environ; |
| 4845 | if (e) |
| 4846 | while (*e) |
| 4847 | puts(*e++); |
| 4848 | return EXIT_SUCCESS; |
| 4849 | } |
| 4850 | |
| 4851 | value = strchr(name, '='); |
| 4852 | if (!value) { |
| 4853 | /* They are exporting something without a =VALUE */ |
| 4854 | struct variable *var; |
| 4855 | |
| 4856 | var = get_local_var(name); |
| 4857 | if (var) { |
| 4858 | var->flg_export = 1; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4859 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4860 | putenv(var->varstr); |
| 4861 | } |
| 4862 | /* bash does not return an error when trying to export |
| 4863 | * an undefined variable. Do likewise. */ |
| 4864 | return EXIT_SUCCESS; |
| 4865 | } |
| 4866 | |
| 4867 | set_local_var(xstrdup(name), 1); |
| 4868 | return EXIT_SUCCESS; |
| 4869 | } |
| 4870 | |
| 4871 | #if ENABLE_HUSH_JOB |
| 4872 | /* built-in 'fg' and 'bg' handler */ |
| 4873 | static int builtin_fg_bg(char **argv) |
| 4874 | { |
| 4875 | int i, jobnum; |
| 4876 | struct pipe *pi; |
| 4877 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4878 | if (!G.interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4879 | return EXIT_FAILURE; |
| 4880 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 4881 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4882 | for (pi = G.job_list; pi; pi = pi->next) { |
| 4883 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4884 | goto found; |
| 4885 | } |
| 4886 | } |
| 4887 | bb_error_msg("%s: no current job", argv[0]); |
| 4888 | return EXIT_FAILURE; |
| 4889 | } |
| 4890 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 4891 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 4892 | return EXIT_FAILURE; |
| 4893 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4894 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4895 | if (pi->jobid == jobnum) { |
| 4896 | goto found; |
| 4897 | } |
| 4898 | } |
| 4899 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 4900 | return EXIT_FAILURE; |
| 4901 | found: |
| 4902 | // TODO: bash prints a string representation |
| 4903 | // of job being foregrounded (like "sleep 1 | cat") |
| 4904 | if (*argv[0] == 'f') { |
| 4905 | /* Put the job into the foreground. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4906 | tcsetpgrp(G.interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4910 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 4911 | for (i = 0; i < pi->num_cmds; i++) { |
| 4912 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 4913 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4914 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4915 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4916 | |
| 4917 | i = kill(- pi->pgrp, SIGCONT); |
| 4918 | if (i < 0) { |
| 4919 | if (errno == ESRCH) { |
| 4920 | delete_finished_bg_job(pi); |
| 4921 | return EXIT_SUCCESS; |
| 4922 | } else { |
| 4923 | bb_perror_msg("kill (SIGCONT)"); |
| 4924 | } |
| 4925 | } |
| 4926 | |
| 4927 | if (*argv[0] == 'f') { |
| 4928 | remove_bg_job(pi); |
| 4929 | return checkjobs_and_fg_shell(pi); |
| 4930 | } |
| 4931 | return EXIT_SUCCESS; |
| 4932 | } |
| 4933 | #endif |
| 4934 | |
| 4935 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4936 | static int builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4937 | { |
| 4938 | const struct built_in_command *x; |
| 4939 | |
| 4940 | printf("\nBuilt-in commands:\n"); |
| 4941 | printf("-------------------\n"); |
| 4942 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 4943 | printf("%s\t%s\n", x->cmd, x->descr); |
| 4944 | } |
| 4945 | printf("\n\n"); |
| 4946 | return EXIT_SUCCESS; |
| 4947 | } |
| 4948 | #endif |
| 4949 | |
| 4950 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4951 | static int builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4952 | { |
| 4953 | struct pipe *job; |
| 4954 | const char *status_string; |
| 4955 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4956 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4957 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4958 | status_string = "Stopped"; |
| 4959 | else |
| 4960 | status_string = "Running"; |
| 4961 | |
| 4962 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 4963 | } |
| 4964 | return EXIT_SUCCESS; |
| 4965 | } |
| 4966 | #endif |
| 4967 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4968 | static int builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4969 | { |
| 4970 | puts(set_cwd()); |
| 4971 | return EXIT_SUCCESS; |
| 4972 | } |
| 4973 | |
| 4974 | static int builtin_read(char **argv) |
| 4975 | { |
| 4976 | char *string; |
| 4977 | const char *name = argv[1] ? argv[1] : "REPLY"; |
| 4978 | |
| 4979 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
| 4980 | return set_local_var(string, 0); |
| 4981 | } |
| 4982 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 4983 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 4984 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 4985 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 4986 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 4987 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 4988 | * set -- [argument...] |
| 4989 | * set -o |
| 4990 | * set +o |
| 4991 | * Implementations shall support the options in both their hyphen and |
| 4992 | * plus-sign forms. These options can also be specified as options to sh. |
| 4993 | * Examples: |
| 4994 | * Write out all variables and their values: set |
| 4995 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 4996 | * Turn on the -x and -v options: set -xv |
| 4997 | * Unset all positional parameters: set -- |
| 4998 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 4999 | * Set the positional parameters to the expansion of x, even if x expands |
| 5000 | * with a leading '-' or '+': set -- $x |
| 5001 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5002 | * 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] | 5003 | */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5004 | static int builtin_set(char **argv) |
| 5005 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5006 | int n; |
| 5007 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 5008 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5009 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 5010 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5011 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5012 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5013 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5014 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 5015 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5016 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5017 | do { |
| 5018 | if (!strcmp(arg, "--")) { |
| 5019 | ++argv; |
| 5020 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5021 | } |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5022 | |
| 5023 | if (arg[0] == '+' || arg[0] == '-') { |
| 5024 | for (n = 1; arg[n]; ++n) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5025 | if (set_mode(arg[0], arg[n])) |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5026 | goto error; |
| 5027 | continue; |
| 5028 | } |
| 5029 | |
| 5030 | break; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5031 | } while ((arg = *++argv) != NULL); |
| 5032 | /* Now argv[0] is 1st argument */ |
| 5033 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5034 | /* Only reset global_argv if we didn't process anything */ |
| 5035 | if (arg == NULL) |
| 5036 | return EXIT_SUCCESS; |
| 5037 | set_argv: |
| 5038 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5039 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 5040 | g_argv = G.global_argv; |
| 5041 | if (G.global_args_malloced) { |
| 5042 | pp = g_argv; |
| 5043 | while (*++pp) |
| 5044 | free(*pp); |
| 5045 | g_argv[1] = NULL; |
| 5046 | } else { |
| 5047 | G.global_args_malloced = 1; |
| 5048 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 5049 | pp[0] = g_argv[0]; /* retain $0 */ |
| 5050 | g_argv = pp; |
| 5051 | } |
| 5052 | /* This realloc's G.global_argv */ |
| 5053 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 5054 | |
| 5055 | n = 1; |
| 5056 | while (*++pp) |
| 5057 | n++; |
| 5058 | G.global_argc = n; |
| 5059 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5060 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5061 | |
| 5062 | /* Nothing known, so abort */ |
| 5063 | error: |
| 5064 | bb_error_msg("set: %s: invalid option", arg); |
| 5065 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | static int builtin_shift(char **argv) |
| 5069 | { |
| 5070 | int n = 1; |
| 5071 | if (argv[1]) { |
| 5072 | n = atoi(argv[1]); |
| 5073 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5074 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 5075 | if (G.global_args_malloced) { |
| 5076 | int m = 1; |
| 5077 | while (m <= n) |
| 5078 | free(G.global_argv[m++]); |
| 5079 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5080 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 5081 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 5082 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5083 | return EXIT_SUCCESS; |
| 5084 | } |
| 5085 | return EXIT_FAILURE; |
| 5086 | } |
| 5087 | |
| 5088 | static int builtin_source(char **argv) |
| 5089 | { |
| 5090 | FILE *input; |
| 5091 | int status; |
| 5092 | |
| 5093 | if (argv[1] == NULL) |
| 5094 | return EXIT_FAILURE; |
| 5095 | |
| 5096 | /* XXX search through $PATH is missing */ |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 5097 | input = fopen_for_read(argv[1]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5098 | if (!input) { |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5099 | bb_error_msg("can't open '%s'", argv[1]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5100 | return EXIT_FAILURE; |
| 5101 | } |
| 5102 | close_on_exec_on(fileno(input)); |
| 5103 | |
| 5104 | /* Now run the file */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5105 | /* 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] | 5106 | * (pointer only is OK!) on this stack frame, |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5107 | * set G.global_argv=argv+1, recurse, and restore. */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5108 | status = parse_and_run_file(input); |
| 5109 | fclose(input); |
| 5110 | return status; |
| 5111 | } |
| 5112 | |
| 5113 | static int builtin_umask(char **argv) |
| 5114 | { |
| 5115 | mode_t new_umask; |
| 5116 | const char *arg = argv[1]; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5117 | if (arg) { |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 5118 | new_umask = bb_strtou(arg, NULL, 8); |
| 5119 | if (errno) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5120 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5121 | } else { |
| 5122 | new_umask = umask(0); |
| 5123 | printf("%.3o\n", (unsigned) new_umask); |
| 5124 | } |
| 5125 | umask(new_umask); |
| 5126 | return EXIT_SUCCESS; |
| 5127 | } |
| 5128 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 5129 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5130 | static int builtin_unset(char **argv) |
| 5131 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 5132 | size_t i; |
| 5133 | int ret; |
| 5134 | bool var = true; |
| 5135 | |
| 5136 | if (!argv[1]) |
| 5137 | return EXIT_SUCCESS; |
| 5138 | |
| 5139 | i = 0; |
| 5140 | if (argv[1][0] == '-') { |
| 5141 | switch (argv[1][1]) { |
| 5142 | case 'v': break; |
| 5143 | case 'f': if (ENABLE_HUSH_FUNCTIONS) { var = false; break; } |
| 5144 | default: |
| 5145 | bb_error_msg("unset: %s: invalid option", argv[1]); |
| 5146 | return EXIT_FAILURE; |
| 5147 | } |
| 5148 | ++i; |
| 5149 | } |
| 5150 | |
| 5151 | ret = EXIT_SUCCESS; |
| 5152 | while (argv[++i]) { |
| 5153 | if (var) { |
| 5154 | if (unset_local_var(argv[i])) |
| 5155 | ret = EXIT_FAILURE; |
| 5156 | } |
| 5157 | #if ENABLE_HUSH_FUNCTIONS |
| 5158 | else |
| 5159 | unset_local_func(argv[i]); |
| 5160 | #endif |
| 5161 | } |
| 5162 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5163 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5164 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5165 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
| 5166 | static int builtin_wait(char **argv) |
| 5167 | { |
| 5168 | int ret = EXIT_SUCCESS; |
| 5169 | int status; |
| 5170 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5171 | if (*++argv == NULL) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5172 | /* don't care about exit status */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5173 | wait(NULL); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5174 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5175 | while (*argv) { |
| 5176 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 5177 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5178 | /* mimic bash message */ |
| 5179 | bb_error_msg("wait: '%s': not a pid or valid job spec", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5180 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5181 | } |
| 5182 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5183 | if (WIFSIGNALED(status)) |
| 5184 | ret = 128 + WTERMSIG(status); |
| 5185 | else if (WIFEXITED(status)) |
| 5186 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5187 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5188 | ret = EXIT_FAILURE; |
| 5189 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5190 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5191 | ret = 127; |
| 5192 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5193 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5194 | } |
| 5195 | |
| 5196 | return ret; |
| 5197 | } |
| 5198 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 5199 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5200 | static int builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5201 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5202 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 5203 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 5204 | return EXIT_SUCCESS; /* bash compat */ |
| 5205 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5206 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
| 5207 | G.depth_break_continue = 1; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5208 | if (argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5209 | G.depth_break_continue = bb_strtou(argv[1], NULL, 10); |
| 5210 | if (errno || !G.depth_break_continue || argv[2]) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 5211 | bb_error_msg("%s: bad arguments", argv[0]); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5212 | G.flag_break_continue = BC_BREAK; |
| 5213 | G.depth_break_continue = UINT_MAX; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5214 | } |
| 5215 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5216 | if (G.depth_of_loop < G.depth_break_continue) |
| 5217 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5218 | return EXIT_SUCCESS; |
| 5219 | } |
| 5220 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5221 | static int builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5222 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 5223 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 5224 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5225 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 5226 | #endif |