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 */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 494 | // unsigned count_SIGCHLD; |
| 495 | // unsigned handled_SIGCHLD; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 496 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 497 | unsigned non_DFL_mask; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 498 | char **traps; /* char *traps[NSIG] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 499 | sigset_t blocked_set; |
| 500 | sigset_t inherited_set; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 501 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 502 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 503 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 504 | * (too many defines). Also, I actually prefer to see when a variable |
| 505 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 506 | #define INIT_G() do { \ |
| 507 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 508 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 509 | |
| 510 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 511 | /* Function prototypes for builtins */ |
| 512 | static int builtin_cd(char **argv); |
| 513 | static int builtin_echo(char **argv); |
| 514 | static int builtin_eval(char **argv); |
| 515 | static int builtin_exec(char **argv); |
| 516 | static int builtin_exit(char **argv); |
| 517 | static int builtin_export(char **argv); |
| 518 | #if ENABLE_HUSH_JOB |
| 519 | static int builtin_fg_bg(char **argv); |
| 520 | static int builtin_jobs(char **argv); |
| 521 | #endif |
| 522 | #if ENABLE_HUSH_HELP |
| 523 | static int builtin_help(char **argv); |
| 524 | #endif |
| 525 | static int builtin_pwd(char **argv); |
| 526 | static int builtin_read(char **argv); |
| 527 | static int builtin_test(char **argv); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 528 | static int builtin_trap(char **argv); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 529 | static int builtin_true(char **argv); |
| 530 | static int builtin_set(char **argv); |
| 531 | static int builtin_shift(char **argv); |
| 532 | static int builtin_source(char **argv); |
| 533 | static int builtin_umask(char **argv); |
| 534 | static int builtin_unset(char **argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 535 | static int builtin_wait(char **argv); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 536 | #if ENABLE_HUSH_LOOPS |
| 537 | static int builtin_break(char **argv); |
| 538 | static int builtin_continue(char **argv); |
| 539 | #endif |
| 540 | //static int builtin_not_written(char **argv); |
| 541 | |
| 542 | /* Table of built-in functions. They can be forked or not, depending on |
| 543 | * context: within pipes, they fork. As simple commands, they do not. |
| 544 | * When used in non-forking context, they can change global variables |
| 545 | * in the parent shell process. If forked, of course they cannot. |
| 546 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 547 | * still be set at the end. */ |
| 548 | struct built_in_command { |
| 549 | const char *cmd; |
| 550 | int (*function)(char **argv); |
| 551 | #if ENABLE_HUSH_HELP |
| 552 | const char *descr; |
| 553 | #define BLTIN(cmd, func, help) { cmd, func, help } |
| 554 | #else |
| 555 | #define BLTIN(cmd, func, help) { cmd, func } |
| 556 | #endif |
| 557 | }; |
| 558 | |
| 559 | /* For now, echo and test are unconditionally enabled. |
| 560 | * Maybe make it configurable? */ |
| 561 | static const struct built_in_command bltins[] = { |
| 562 | BLTIN("." , builtin_source, "Run commands in a file"), |
| 563 | BLTIN(":" , builtin_true, "No-op"), |
| 564 | BLTIN("[" , builtin_test, "Test condition"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 565 | #if ENABLE_HUSH_JOB |
| 566 | BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"), |
| 567 | #endif |
| 568 | #if ENABLE_HUSH_LOOPS |
| 569 | BLTIN("break" , builtin_break, "Exit from a loop"), |
| 570 | #endif |
| 571 | BLTIN("cd" , builtin_cd, "Change directory"), |
| 572 | #if ENABLE_HUSH_LOOPS |
| 573 | BLTIN("continue", builtin_continue, "Start new loop iteration"), |
| 574 | #endif |
| 575 | BLTIN("echo" , builtin_echo, "Write to stdout"), |
| 576 | BLTIN("eval" , builtin_eval, "Construct and run shell command"), |
| 577 | BLTIN("exec" , builtin_exec, "Execute command, don't return to shell"), |
| 578 | BLTIN("exit" , builtin_exit, "Exit"), |
| 579 | BLTIN("export", builtin_export, "Set environment variable"), |
| 580 | #if ENABLE_HUSH_JOB |
| 581 | BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"), |
| 582 | BLTIN("jobs" , builtin_jobs, "List active jobs"), |
| 583 | #endif |
| 584 | BLTIN("pwd" , builtin_pwd, "Print current directory"), |
| 585 | BLTIN("read" , builtin_read, "Input environment variable"), |
| 586 | // BLTIN("return", builtin_not_written, "Return from a function"), |
| 587 | BLTIN("set" , builtin_set, "Set/unset shell local variables"), |
| 588 | BLTIN("shift" , builtin_shift, "Shift positional parameters"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 589 | BLTIN("test" , builtin_test, "Test condition"), |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 590 | BLTIN("trap" , builtin_trap, "Trap signals"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 591 | // BLTIN("ulimit", builtin_not_written, "Control resource limits"), |
| 592 | BLTIN("umask" , builtin_umask, "Set file creation mask"), |
| 593 | BLTIN("unset" , builtin_unset, "Unset environment variable"), |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 594 | BLTIN("wait" , builtin_wait, "Wait for process"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 595 | #if ENABLE_HUSH_HELP |
| 596 | BLTIN("help" , builtin_help, "List shell built-in commands"), |
| 597 | #endif |
| 598 | }; |
| 599 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 600 | |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 601 | /* Normal */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 602 | static void maybe_die(const char *notice, const char *msg) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 603 | { |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 604 | /* Was using fancy stuff: |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 605 | * (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...) |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 606 | * but it SEGVs. ?! Oh well... explicit temp ptr works around that */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 607 | void FAST_FUNC (*fp)(const char *s, ...) = bb_error_msg_and_die; |
| 608 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 609 | fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 610 | #endif |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 611 | fp(msg ? "%s: %s" : notice, notice, msg); |
| 612 | } |
Mike Frysinger | 5a82845 | 2009-03-28 19:09:04 +0000 | [diff] [blame] | 613 | #if 1 |
| 614 | #define syntax(msg) maybe_die("syntax error", msg); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 615 | #else |
Mike Frysinger | 5a82845 | 2009-03-28 19:09:04 +0000 | [diff] [blame] | 616 | /* Debug -- trick gcc to expand __LINE__ and convert to string */ |
| 617 | #define __syntax(msg, line) maybe_die("syntax error hush.c:" # line, msg) |
| 618 | #define _syntax(msg, line) __syntax(msg, line) |
| 619 | #define syntax(msg) _syntax(msg, __LINE__) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 620 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 621 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 622 | static int glob_needed(const char *s) |
| 623 | { |
| 624 | while (*s) { |
| 625 | if (*s == '\\') |
| 626 | s++; |
| 627 | if (*s == '*' || *s == '[' || *s == '?') |
| 628 | return 1; |
| 629 | s++; |
| 630 | } |
| 631 | return 0; |
| 632 | } |
| 633 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 634 | static int is_assignment(const char *s) |
| 635 | { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 636 | if (!s || !(isalpha(*s) || *s == '_')) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 637 | return 0; |
| 638 | s++; |
| 639 | while (isalnum(*s) || *s == '_') |
| 640 | s++; |
| 641 | return *s == '='; |
| 642 | } |
| 643 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 644 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 645 | static char *unbackslash(char *src) |
| 646 | { |
| 647 | char *dst = src; |
| 648 | while (1) { |
| 649 | if (*src == '\\') |
| 650 | src++; |
| 651 | if ((*dst++ = *src++) == '\0') |
| 652 | break; |
| 653 | } |
| 654 | return dst; |
| 655 | } |
| 656 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 657 | 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] | 658 | { |
| 659 | int i; |
| 660 | unsigned count1; |
| 661 | unsigned count2; |
| 662 | char **v; |
| 663 | |
| 664 | v = strings; |
| 665 | count1 = 0; |
| 666 | if (v) { |
| 667 | while (*v) { |
| 668 | count1++; |
| 669 | v++; |
| 670 | } |
| 671 | } |
| 672 | count2 = 0; |
| 673 | v = add; |
| 674 | while (*v) { |
| 675 | count2++; |
| 676 | v++; |
| 677 | } |
| 678 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 679 | v[count1 + count2] = NULL; |
| 680 | i = count2; |
| 681 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 682 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 683 | return v; |
| 684 | } |
| 685 | |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 686 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 687 | { |
| 688 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 689 | v[0] = add; |
| 690 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 691 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 692 | } |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 693 | |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 694 | static void putenv_all(char **strings) |
| 695 | { |
| 696 | if (!strings) |
| 697 | return; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 698 | while (*strings) { |
| 699 | debug_printf_env("putenv '%s'\n", *strings); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 700 | putenv(*strings++); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 701 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | static char **putenv_all_and_save_old(char **strings) |
| 705 | { |
| 706 | char **old = NULL; |
| 707 | char **s = strings; |
| 708 | |
| 709 | if (!strings) |
| 710 | return old; |
| 711 | while (*strings) { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 712 | char *v, *eq; |
| 713 | |
| 714 | eq = strchr(*strings, '='); |
| 715 | if (eq) { |
| 716 | *eq = '\0'; |
| 717 | v = getenv(*strings); |
| 718 | *eq = '='; |
| 719 | if (v) { |
| 720 | /* v points to VAL in VAR=VAL, go back to VAR */ |
| 721 | v -= (eq - *strings) + 1; |
| 722 | old = add_string_to_strings(old, v); |
| 723 | } |
| 724 | } |
| 725 | strings++; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 726 | } |
| 727 | putenv_all(s); |
| 728 | return old; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 731 | static void free_strings_and_unsetenv(char **strings, int unset) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 732 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 733 | char **v; |
| 734 | |
| 735 | if (!strings) |
| 736 | return; |
| 737 | |
| 738 | v = strings; |
| 739 | while (*v) { |
| 740 | if (unset) { |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 741 | debug_printf_env("unsetenv '%s'\n", *v); |
| 742 | bb_unsetenv(*v); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 743 | } |
| 744 | free(*v++); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 745 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 746 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 749 | static void free_strings(char **strings) |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 750 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 751 | free_strings_and_unsetenv(strings, 0); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 752 | } |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 753 | |
| 754 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 755 | /* Basic theory of signal handling in shell |
| 756 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 757 | * This does not describe what hush does, rather, it is current understanding |
| 758 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 759 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 760 | * |
| 761 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 762 | * is finished or backgrounded. It is the same in interactive and |
| 763 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 764 | * 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] | 765 | * ^C or ^Z from keyboard seem to execute "at once" because it usually |
| 766 | * backgrounds (i.e. stops) or kills all members of currently running |
| 767 | * pipe. |
| 768 | * |
| 769 | * Wait builtin in interruptible by signals for which user trap is set |
| 770 | * or by SIGINT in interactive shell. |
| 771 | * |
| 772 | * Trap handlers will execute even within trap handlers. (right?) |
| 773 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 774 | * User trap handlers are forgotten when subshell ("(cmd)") is entered. [TODO] |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 775 | * |
| 776 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 777 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 778 | * |
| 779 | * Commands run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 780 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 781 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 782 | * Ordinary commands have signals set to SIG_IGN/DFL set as inherited |
| 783 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 784 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 785 | * Siganls which differ from SIG_DFL action |
| 786 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 787 | * |
| 788 | * SIGQUIT: ignore |
| 789 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 790 | * SIGHUP (interactive): |
| 791 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 792 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 793 | * (note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 794 | * that all pipe members are stopped) (right?) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 795 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 796 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 797 | * to interactive shell while shell is waiting for a pipe, |
| 798 | * since shell is bg'ed (is not in foreground process group). |
| 799 | * (check/expand this) |
| 800 | * Example 1: this waits 5 sec, but does not execute ls: |
| 801 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 802 | * Example 2: this does not wait and does not execute ls: |
| 803 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 804 | * Example 3: this does not wait 5 sec, but executes ls: |
| 805 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 806 | * |
| 807 | * (What happens to signals which are IGN on shell start?) |
| 808 | * (What happens with signal mask on shell start?) |
| 809 | * |
| 810 | * Implementation in hush |
| 811 | * ====================== |
| 812 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 813 | * We block all signals which we don't want to take action immediately, |
| 814 | * i.e. we block all signals which need to have special handling as described |
| 815 | * above, and all signals which have traps set. |
| 816 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 817 | * and act on them. |
| 818 | * |
| 819 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 820 | * sigset_t blocked_set: current blocked signal set |
| 821 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 822 | * "trap - SIGxxx": |
| 823 | * clear bit in blocked_set unless it is also in non_DFL |
| 824 | * "trap 'cmd' SIGxxx": |
| 825 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 826 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 827 | * nothing for {} child shell (say, "true | { true; true; } | true") |
| 828 | * unset all traps if () shell. [TODO] |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 829 | * after [v]fork, if we plan to exec: |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 830 | * POSIX says pending signal mask is cleared in child - no need to clear it. |
| 831 | * 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] | 832 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 833 | * Note: as a result, we do not use signal handlers much. The only uses |
| 834 | * are to count SIGCHLDs [disabled - bug somewhere, + bloat] |
| 835 | * and to restore tty pgrp on signal-induced exit. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 836 | * |
| 837 | * TODO: check/fix wait builtin to be interruptible. |
| 838 | */ |
| 839 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 840 | //static void SIGCHLD_handler(int sig UNUSED_PARAM) |
| 841 | //{ |
| 842 | // G.count_SIGCHLD++; |
| 843 | //} |
| 844 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 845 | /* called once at shell init */ |
| 846 | static void init_signal_mask(void) |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 847 | { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 848 | unsigned sig; |
| 849 | unsigned mask = (1 << SIGQUIT); |
| 850 | #if ENABLE_HUSH_INTERACTIVE |
| 851 | if (G.interactive_fd) { |
| 852 | mask = 0 |
| 853 | | (1 << SIGQUIT) |
| 854 | | (1 << SIGTERM) |
| 855 | | (1 << SIGHUP) |
| 856 | #if ENABLE_HUSH_JOB |
| 857 | | (1 << SIGTTIN) | (1 << SIGTTOU) | (1 << SIGTSTP) |
| 858 | #endif |
| 859 | | (1 << SIGINT) |
| 860 | ; |
| 861 | } |
| 862 | #endif |
| 863 | G.non_DFL_mask = mask; |
| 864 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 865 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 866 | sig = 0; |
| 867 | while (mask) { |
| 868 | if (mask & 1) |
| 869 | sigaddset(&G.blocked_set, sig); |
| 870 | mask >>= 1; |
| 871 | sig++; |
| 872 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 873 | sigdelset(&G.blocked_set, SIGCHLD); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 874 | sigprocmask(SIG_SETMASK, &G.blocked_set, &G.inherited_set); |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 875 | } |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 876 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 877 | static int check_and_run_traps(int sig) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 878 | { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 879 | static const struct timespec zero_timespec = { 0, 0 }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 880 | smalluint save_rcode; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 881 | int last_sig = 0; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 882 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 883 | if (sig) |
| 884 | goto jump_in; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 885 | while (1) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 886 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_timespec); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 887 | if (sig <= 0) |
| 888 | break; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 889 | jump_in: |
| 890 | last_sig = sig; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 891 | if (G.traps && G.traps[sig]) { |
| 892 | if (G.traps[sig][0]) { |
| 893 | /* We have user-defined handler */ |
| 894 | char *argv[] = { NULL, xstrdup(G.traps[sig]), NULL }; |
| 895 | save_rcode = G.last_return_code; |
| 896 | builtin_eval(argv); |
| 897 | free(argv[1]); |
| 898 | G.last_return_code = save_rcode; |
| 899 | } /* else: "" trap, ignoring signal */ |
| 900 | continue; |
| 901 | } |
| 902 | /* not a trap: special action */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 903 | switch (sig) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 904 | // case SIGCHLD: |
| 905 | // G.count_SIGCHLD++; |
| 906 | // break; |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 907 | case SIGINT: |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 908 | bb_putchar('\n'); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 909 | G.flag_SIGINT = 1; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 910 | break; |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 911 | //TODO |
| 912 | // case SIGHUP: ... |
| 913 | // break; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 914 | default: /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 915 | break; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 916 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 917 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 918 | return last_sig; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 921 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 922 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 923 | /* Restores tty foreground process group, and exits. |
| 924 | * May be called as signal handler for fatal signal |
| 925 | * (will faithfully resend signal to itself, producing correct exit state) |
| 926 | * or called directly with -EXITCODE. |
| 927 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 928 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 929 | static void sigexit(int sig) |
| 930 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 931 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 932 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 933 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 934 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 935 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 936 | * tty pgrp then, only top-level shell process does that */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 937 | if (G.interactive_fd && getpid() == G.root_pid) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 938 | tcsetpgrp(G.interactive_fd, G.saved_tty_pgrp); |
| 939 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 940 | |
| 941 | /* Not a signal, just exit */ |
| 942 | if (sig <= 0) |
| 943 | _exit(- sig); |
| 944 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 945 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 948 | /* helper */ |
| 949 | static void maybe_set_sighandler(int sig) |
| 950 | { |
| 951 | void (*handler)(int); |
| 952 | /* non_DFL_mask'ed signals are, well, masked, |
| 953 | * no need to set handler for them. |
| 954 | */ |
| 955 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 956 | handler = signal(sig, sigexit); |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 957 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 958 | signal(sig, handler); |
| 959 | } |
| 960 | } |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 961 | /* Used only to set handler to restore pgrp on exit */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 962 | static void set_fatal_signals_to_sigexit(void) |
| 963 | { |
| 964 | if (HUSH_DEBUG) { |
| 965 | maybe_set_sighandler(SIGILL ); |
| 966 | maybe_set_sighandler(SIGFPE ); |
| 967 | maybe_set_sighandler(SIGBUS ); |
| 968 | maybe_set_sighandler(SIGSEGV); |
| 969 | maybe_set_sighandler(SIGTRAP); |
| 970 | } /* else: hush is perfect. what SEGV? */ |
| 971 | |
| 972 | maybe_set_sighandler(SIGABRT); |
| 973 | |
| 974 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 975 | maybe_set_sighandler(SIGPIPE); |
| 976 | maybe_set_sighandler(SIGALRM); |
| 977 | maybe_set_sighandler(SIGHUP ); |
| 978 | |
| 979 | /* if we aren't interactive... but in this case |
| 980 | * we never want to restore pgrp on exit, and this fn is not called */ |
| 981 | /*maybe_set_sighandler(SIGTERM);*/ |
| 982 | /*maybe_set_sighandler(SIGINT );*/ |
| 983 | } |
| 984 | /* Used only to suppress ^Z in `cmd` */ |
| 985 | static void set_jobctrl_signals_to_IGN(void) |
| 986 | { |
| 987 | bb_signals(0 |
| 988 | + (1 << SIGTSTP) |
| 989 | + (1 << SIGTTIN) |
| 990 | + (1 << SIGTTOU) |
| 991 | , SIG_IGN); |
| 992 | } |
| 993 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 994 | #else /* !JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 995 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 996 | #define set_fatal_signals_to_sigexit(handler) ((void)0) |
| 997 | #define set_jobctrl_signals_to_IGN(handler) ((void)0) |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 998 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 999 | #endif /* JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1000 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1001 | /* Restores tty foreground process group, and exits. */ |
| 1002 | static void hush_exit(int exitcode) NORETURN; |
| 1003 | static void hush_exit(int exitcode) |
| 1004 | { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1005 | if (G.traps && G.traps[0] && G.traps[0][0]) { |
| 1006 | char *argv[] = { NULL, xstrdup(G.traps[0]), NULL }; |
| 1007 | builtin_eval(argv); |
| 1008 | free(argv[1]); |
| 1009 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1010 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1011 | #if ENABLE_HUSH_JOB |
| 1012 | fflush(NULL); /* flush all streams */ |
| 1013 | sigexit(- (exitcode & 0xff)); |
| 1014 | #else |
| 1015 | exit(exitcode); |
| 1016 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1019 | |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1020 | static const char *set_cwd(void) |
| 1021 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1022 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1023 | * we must not try to free(bb_msg_unknown) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1024 | if (G.cwd == bb_msg_unknown) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1025 | G.cwd = NULL; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1026 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1027 | if (!G.cwd) |
| 1028 | G.cwd = bb_msg_unknown; |
| 1029 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1032 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1033 | /* Get/check local shell variables */ |
| 1034 | static struct variable *get_local_var(const char *name) |
| 1035 | { |
| 1036 | struct variable *cur; |
| 1037 | int len; |
| 1038 | |
| 1039 | if (!name) |
| 1040 | return NULL; |
| 1041 | len = strlen(name); |
| 1042 | for (cur = G.top_var; cur; cur = cur->next) { |
| 1043 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
| 1044 | return cur; |
| 1045 | } |
| 1046 | return NULL; |
| 1047 | } |
| 1048 | |
| 1049 | /* Basically useful version until someone wants to get fancier, |
| 1050 | * see the bash man page under "Parameter Expansion" */ |
| 1051 | static const char *lookup_param(const char *src) |
| 1052 | { |
| 1053 | struct variable *var = get_local_var(src); |
| 1054 | if (var) |
| 1055 | return strchr(var->varstr, '=') + 1; |
| 1056 | return NULL; |
| 1057 | } |
| 1058 | |
| 1059 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1060 | * We take ownership of it. |
| 1061 | * flg_export is used by: |
| 1062 | * 0: do not export |
| 1063 | * 1: export |
| 1064 | * -1: if NAME is set, leave export status alone |
| 1065 | * if NAME is not set, do not export |
| 1066 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1067 | static int set_local_var(char *str, int flg_export) |
| 1068 | { |
| 1069 | struct variable *cur; |
| 1070 | char *value; |
| 1071 | int name_len; |
| 1072 | |
| 1073 | value = strchr(str, '='); |
| 1074 | if (!value) { /* not expected to ever happen? */ |
| 1075 | free(str); |
| 1076 | return -1; |
| 1077 | } |
| 1078 | |
| 1079 | name_len = value - str + 1; /* including '=' */ |
| 1080 | cur = G.top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */ |
| 1081 | while (1) { |
| 1082 | if (strncmp(cur->varstr, str, name_len) != 0) { |
| 1083 | if (!cur->next) { |
| 1084 | /* Bail out. Note that now cur points |
| 1085 | * to last var in linked list */ |
| 1086 | break; |
| 1087 | } |
| 1088 | cur = cur->next; |
| 1089 | continue; |
| 1090 | } |
| 1091 | /* We found an existing var with this name */ |
| 1092 | *value = '\0'; |
| 1093 | if (cur->flg_read_only) { |
| 1094 | bb_error_msg("%s: readonly variable", str); |
| 1095 | free(str); |
| 1096 | return -1; |
| 1097 | } |
| 1098 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1099 | unsetenv(str); /* just in case */ |
| 1100 | *value = '='; |
| 1101 | if (strcmp(cur->varstr, str) == 0) { |
| 1102 | free_and_exp: |
| 1103 | free(str); |
| 1104 | goto exp; |
| 1105 | } |
| 1106 | if (cur->max_len >= strlen(str)) { |
| 1107 | /* This one is from startup env, reuse space */ |
| 1108 | strcpy(cur->varstr, str); |
| 1109 | goto free_and_exp; |
| 1110 | } |
| 1111 | /* max_len == 0 signifies "malloced" var, which we can |
| 1112 | * (and has to) free */ |
| 1113 | if (!cur->max_len) |
| 1114 | free(cur->varstr); |
| 1115 | cur->max_len = 0; |
| 1116 | goto set_str_and_exp; |
| 1117 | } |
| 1118 | |
| 1119 | /* Not found - create next variable struct */ |
| 1120 | cur->next = xzalloc(sizeof(*cur)); |
| 1121 | cur = cur->next; |
| 1122 | |
| 1123 | set_str_and_exp: |
| 1124 | cur->varstr = str; |
| 1125 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1126 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1127 | cur->flg_export = 1; |
| 1128 | if (cur->flg_export) { |
| 1129 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1130 | return putenv(cur->varstr); |
| 1131 | } |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1135 | static int unset_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1136 | { |
| 1137 | struct variable *cur; |
| 1138 | struct variable *prev = prev; /* for gcc */ |
| 1139 | int name_len; |
| 1140 | |
| 1141 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1142 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1143 | name_len = strlen(name); |
| 1144 | cur = G.top_var; |
| 1145 | while (cur) { |
| 1146 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1147 | if (cur->flg_read_only) { |
| 1148 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1149 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1150 | } |
| 1151 | /* prev is ok to use here because 1st variable, HUSH_VERSION, |
| 1152 | * is ro, and we cannot reach this code on the 1st pass */ |
| 1153 | prev->next = cur->next; |
| 1154 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1155 | bb_unsetenv(cur->varstr); |
| 1156 | if (!cur->max_len) |
| 1157 | free(cur->varstr); |
| 1158 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1159 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1160 | } |
| 1161 | prev = cur; |
| 1162 | cur = cur->next; |
| 1163 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1164 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | |
| 1168 | /* |
| 1169 | * in_str support |
| 1170 | */ |
| 1171 | static int static_get(struct in_str *i) |
| 1172 | { |
| 1173 | int ch = *i->p++; |
| 1174 | if (ch == '\0') return EOF; |
| 1175 | return ch; |
| 1176 | } |
| 1177 | |
| 1178 | static int static_peek(struct in_str *i) |
| 1179 | { |
| 1180 | return *i->p; |
| 1181 | } |
| 1182 | |
| 1183 | #if ENABLE_HUSH_INTERACTIVE |
| 1184 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1185 | static void cmdedit_set_initial_prompt(void) |
| 1186 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1187 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1188 | G.PS1 = getenv("PS1"); |
| 1189 | if (G.PS1 == NULL) |
| 1190 | G.PS1 = "\\w \\$ "; |
| 1191 | } else |
| 1192 | G.PS1 = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1193 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1194 | |
| 1195 | static const char* setup_prompt_string(int promptmode) |
| 1196 | { |
| 1197 | const char *prompt_str; |
| 1198 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1199 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1200 | /* Set up the prompt */ |
| 1201 | if (promptmode == 0) { /* PS1 */ |
| 1202 | free((char*)G.PS1); |
| 1203 | G.PS1 = xasprintf("%s %c ", G.cwd, (geteuid() != 0) ? '$' : '#'); |
| 1204 | prompt_str = G.PS1; |
| 1205 | } else |
| 1206 | prompt_str = G.PS2; |
| 1207 | } else |
| 1208 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1209 | debug_printf("result '%s'\n", prompt_str); |
| 1210 | return prompt_str; |
| 1211 | } |
| 1212 | |
| 1213 | static void get_user_input(struct in_str *i) |
| 1214 | { |
| 1215 | int r; |
| 1216 | const char *prompt_str; |
| 1217 | |
| 1218 | prompt_str = setup_prompt_string(i->promptmode); |
| 1219 | #if ENABLE_FEATURE_EDITING |
| 1220 | /* Enable command line editing only while a command line |
| 1221 | * is actually being read */ |
| 1222 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1223 | G.flag_SIGINT = 0; |
| 1224 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1225 | * only after <Enter>. (^C will work) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1226 | 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] | 1227 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1228 | check_and_run_traps(0); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1229 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1230 | i->eof_flag = (r < 0); |
| 1231 | if (i->eof_flag) { /* EOF/error detected */ |
| 1232 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1233 | G.user_input_buf[1] = '\0'; |
| 1234 | } |
| 1235 | #else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1236 | do { |
| 1237 | G.flag_SIGINT = 0; |
| 1238 | fputs(prompt_str, stdout); |
| 1239 | fflush(stdout); |
| 1240 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1241 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1242 | //do we need check_and_run_traps(0)? (maybe only if stdin) |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1243 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1244 | i->eof_flag = (r == EOF); |
| 1245 | #endif |
| 1246 | i->p = G.user_input_buf; |
| 1247 | } |
| 1248 | |
| 1249 | #endif /* INTERACTIVE */ |
| 1250 | |
| 1251 | /* This is the magic location that prints prompts |
| 1252 | * and gets data back from the user */ |
| 1253 | static int file_get(struct in_str *i) |
| 1254 | { |
| 1255 | int ch; |
| 1256 | |
| 1257 | /* If there is data waiting, eat it up */ |
| 1258 | if (i->p && *i->p) { |
| 1259 | #if ENABLE_HUSH_INTERACTIVE |
| 1260 | take_cached: |
| 1261 | #endif |
| 1262 | ch = *i->p++; |
| 1263 | if (i->eof_flag && !*i->p) |
| 1264 | ch = EOF; |
| 1265 | } else { |
| 1266 | /* need to double check i->file because we might be doing something |
| 1267 | * more complicated by now, like sourcing or substituting. */ |
| 1268 | #if ENABLE_HUSH_INTERACTIVE |
| 1269 | if (G.interactive_fd && i->promptme && i->file == stdin) { |
| 1270 | do { |
| 1271 | get_user_input(i); |
| 1272 | } while (!*i->p); /* need non-empty line */ |
| 1273 | i->promptmode = 1; /* PS2 */ |
| 1274 | i->promptme = 0; |
| 1275 | goto take_cached; |
| 1276 | } |
| 1277 | #endif |
| 1278 | ch = fgetc(i->file); |
| 1279 | } |
| 1280 | debug_printf("file_get: got a '%c' %d\n", ch, ch); |
| 1281 | #if ENABLE_HUSH_INTERACTIVE |
| 1282 | if (ch == '\n') |
| 1283 | i->promptme = 1; |
| 1284 | #endif |
| 1285 | return ch; |
| 1286 | } |
| 1287 | |
| 1288 | /* All the callers guarantee this routine will never be |
| 1289 | * used right after a newline, so prompting is not needed. |
| 1290 | */ |
| 1291 | static int file_peek(struct in_str *i) |
| 1292 | { |
| 1293 | int ch; |
| 1294 | if (i->p && *i->p) { |
| 1295 | if (i->eof_flag && !i->p[1]) |
| 1296 | return EOF; |
| 1297 | return *i->p; |
| 1298 | } |
| 1299 | ch = fgetc(i->file); |
| 1300 | i->eof_flag = (ch == EOF); |
| 1301 | i->peek_buf[0] = ch; |
| 1302 | i->peek_buf[1] = '\0'; |
| 1303 | i->p = i->peek_buf; |
| 1304 | debug_printf("file_peek: got a '%c' %d\n", *i->p, *i->p); |
| 1305 | return ch; |
| 1306 | } |
| 1307 | |
| 1308 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1309 | { |
| 1310 | i->peek = file_peek; |
| 1311 | i->get = file_get; |
| 1312 | #if ENABLE_HUSH_INTERACTIVE |
| 1313 | i->promptme = 1; |
| 1314 | i->promptmode = 0; /* PS1 */ |
| 1315 | #endif |
| 1316 | i->file = f; |
| 1317 | i->p = NULL; |
| 1318 | } |
| 1319 | |
| 1320 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1321 | { |
| 1322 | i->peek = static_peek; |
| 1323 | i->get = static_get; |
| 1324 | #if ENABLE_HUSH_INTERACTIVE |
| 1325 | i->promptme = 1; |
| 1326 | i->promptmode = 0; /* PS1 */ |
| 1327 | #endif |
| 1328 | i->p = s; |
| 1329 | i->eof_flag = 0; |
| 1330 | } |
| 1331 | |
| 1332 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1333 | /* |
| 1334 | * o_string support |
| 1335 | */ |
| 1336 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1337 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1338 | static void o_reset(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1339 | { |
| 1340 | o->length = 0; |
| 1341 | o->nonnull = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1342 | if (o->data) |
| 1343 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1346 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1347 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1348 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1349 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1352 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1353 | { |
| 1354 | if (o->length + len > o->maxlen) { |
| 1355 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 1356 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 1357 | } |
| 1358 | } |
| 1359 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1360 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1361 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1362 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1363 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1364 | o->data[o->length] = ch; |
| 1365 | o->length++; |
| 1366 | o->data[o->length] = '\0'; |
| 1367 | } |
| 1368 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1369 | static void o_addstr(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1370 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1371 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1372 | memcpy(&o->data[o->length], str, len); |
| 1373 | o->length += len; |
| 1374 | o->data[o->length] = '\0'; |
| 1375 | } |
| 1376 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1377 | static void o_addstr_duplicate_backslash(o_string *o, const char *str, int len) |
| 1378 | { |
| 1379 | while (len) { |
| 1380 | o_addchr(o, *str); |
| 1381 | if (*str++ == '\\' |
| 1382 | && (*str != '*' && *str != '?' && *str != '[') |
| 1383 | ) { |
| 1384 | o_addchr(o, '\\'); |
| 1385 | } |
| 1386 | len--; |
| 1387 | } |
| 1388 | } |
| 1389 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1390 | /* My analysis of quoting semantics tells me that state information |
| 1391 | * is associated with a destination, not a source. |
| 1392 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1393 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1394 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1395 | int sz = 1; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1396 | char *found = strchr("*?[\\", ch); |
| 1397 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1398 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1399 | o_grow_by(o, sz); |
| 1400 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1401 | o->data[o->length] = '\\'; |
| 1402 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1403 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1404 | o->data[o->length] = ch; |
| 1405 | o->length++; |
| 1406 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1409 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1410 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1411 | int sz = 1; |
| 1412 | if (o->o_quote && strchr("*?[\\", ch)) { |
| 1413 | sz++; |
| 1414 | o->data[o->length] = '\\'; |
| 1415 | o->length++; |
| 1416 | } |
| 1417 | o_grow_by(o, sz); |
| 1418 | o->data[o->length] = ch; |
| 1419 | o->length++; |
| 1420 | o->data[o->length] = '\0'; |
| 1421 | } |
| 1422 | |
| 1423 | static void o_addQstr(o_string *o, const char *str, int len) |
| 1424 | { |
| 1425 | if (!o->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1426 | o_addstr(o, str, len); |
| 1427 | return; |
| 1428 | } |
| 1429 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1430 | char ch; |
| 1431 | int sz; |
| 1432 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 1433 | if (ordinary_cnt > len) /* paranoia */ |
| 1434 | ordinary_cnt = len; |
| 1435 | o_addstr(o, str, ordinary_cnt); |
| 1436 | if (ordinary_cnt == len) |
| 1437 | return; |
| 1438 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1439 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1440 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1441 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1442 | sz = 1; |
| 1443 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 1444 | sz++; |
| 1445 | o->data[o->length] = '\\'; |
| 1446 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1447 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1448 | o_grow_by(o, sz); |
| 1449 | o->data[o->length] = ch; |
| 1450 | o->length++; |
| 1451 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1455 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1456 | * 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] | 1457 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1458 | * list[i] contains an INDEX (int!) into this string data. |
| 1459 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1460 | * but list[i]'s need not be modified. |
| 1461 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1462 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1463 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1464 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1465 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1466 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1467 | { |
| 1468 | char **list = (char**)o->data; |
| 1469 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1470 | int i = 0; |
| 1471 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1472 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1473 | while (i < n) { |
| 1474 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1475 | o->data + (int)list[i] + string_start, |
| 1476 | o->data + (int)list[i] + string_start); |
| 1477 | i++; |
| 1478 | } |
| 1479 | if (n) { |
| 1480 | const char *p = o->data + (int)list[n - 1] + string_start; |
Mike Frysinger | d006edb | 2009-03-28 12:43:53 +0000 | [diff] [blame] | 1481 | fprintf(stderr, " total_sz:%ld\n", (p + strlen(p) + 1) - o->data); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | #else |
| 1485 | #define debug_print_list(prefix, o, n) ((void)0) |
| 1486 | #endif |
| 1487 | |
| 1488 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1489 | * in list[n] so that it points past last stored byte so far. |
| 1490 | * It returns n+1. */ |
| 1491 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1492 | { |
| 1493 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1494 | int string_start; |
| 1495 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1496 | |
| 1497 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1498 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1499 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1500 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1501 | 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] | 1502 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1503 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1504 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1505 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1506 | memmove(list + n + 0x10, list + n, string_len); |
| 1507 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1508 | } else |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1509 | 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] | 1510 | } else { |
| 1511 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1512 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1513 | string_len = o->length - string_start; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1514 | 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] | 1515 | o->has_empty_slot = 0; |
| 1516 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1517 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1518 | return n + 1; |
| 1519 | } |
| 1520 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1521 | /* "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] | 1522 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1523 | { |
| 1524 | char **list = (char**)o->data; |
| 1525 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1526 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1527 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1530 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1531 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1532 | static int o_glob(o_string *o, int n) |
| 1533 | { |
| 1534 | glob_t globdata; |
| 1535 | int gr; |
| 1536 | char *pattern; |
| 1537 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1538 | 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] | 1539 | if (!o->data) |
| 1540 | return o_save_ptr_helper(o, n); |
| 1541 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1542 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1543 | if (!glob_needed(pattern)) { |
| 1544 | literal: |
| 1545 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1546 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1547 | return o_save_ptr_helper(o, n); |
| 1548 | } |
| 1549 | |
| 1550 | memset(&globdata, 0, sizeof(globdata)); |
| 1551 | gr = glob(pattern, 0, NULL, &globdata); |
| 1552 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 1553 | if (gr == GLOB_NOSPACE) |
| 1554 | bb_error_msg_and_die("out of memory during glob"); |
| 1555 | if (gr == GLOB_NOMATCH) { |
| 1556 | globfree(&globdata); |
| 1557 | goto literal; |
| 1558 | } |
| 1559 | if (gr != 0) { /* GLOB_ABORTED ? */ |
| 1560 | //TODO: testcase for bad glob pattern behavior |
| 1561 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 1562 | } |
| 1563 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 1564 | char **argv = globdata.gl_pathv; |
| 1565 | o->length = pattern - o->data; /* "forget" pattern */ |
| 1566 | while (1) { |
| 1567 | o_addstr(o, *argv, strlen(*argv) + 1); |
| 1568 | n = o_save_ptr_helper(o, n); |
| 1569 | argv++; |
| 1570 | if (!*argv) |
| 1571 | break; |
| 1572 | } |
| 1573 | } |
| 1574 | globfree(&globdata); |
| 1575 | if (DEBUG_GLOB) |
| 1576 | debug_print_list("o_glob returning", o, n); |
| 1577 | return n; |
| 1578 | } |
| 1579 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1580 | /* If o->o_glob == 1, glob the string so far remembered. |
| 1581 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1582 | static int o_save_ptr(o_string *o, int n) |
| 1583 | { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 1584 | if (o->o_glob) { /* if globbing is requested */ |
| 1585 | /* If o->has_empty_slot, list[n] was already globbed |
| 1586 | * (if it was requested back then when it was filled) |
| 1587 | * so don't do that again! */ |
| 1588 | if (!o->has_empty_slot) |
| 1589 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 1590 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1591 | return o_save_ptr_helper(o, n); |
| 1592 | } |
| 1593 | |
| 1594 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1595 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1596 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1597 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1598 | int string_start; |
| 1599 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1600 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 1601 | if (DEBUG_EXPAND) |
| 1602 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1603 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1604 | list = (char**)o->data; |
| 1605 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1606 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1607 | while (n) { |
| 1608 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1609 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1610 | } |
| 1611 | return list; |
| 1612 | } |
| 1613 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1614 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1615 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 1616 | * all variable references within and returns a pointer to |
| 1617 | * a list of expanded strings, possibly with larger number |
| 1618 | * of strings. (Think VAR="a b"; echo $VAR). |
| 1619 | * This new list is allocated as a single malloc block. |
| 1620 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 1621 | * followed by strings themself. |
| 1622 | * Caller can deallocate entire list by single free(list). */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1623 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1624 | /* Store given string, finalizing the word and starting new one whenever |
| 1625 | * we encounter IFS char(s). This is used for expanding variable values. |
| 1626 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 1627 | 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] | 1628 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1629 | while (1) { |
| 1630 | int word_len = strcspn(str, G.ifs); |
| 1631 | if (word_len) { |
| 1632 | if (output->o_quote || !output->o_glob) |
| 1633 | o_addQstr(output, str, word_len); |
| 1634 | else /* protect backslashes against globbing up :) */ |
| 1635 | o_addstr_duplicate_backslash(output, str, word_len); |
| 1636 | str += word_len; |
| 1637 | } |
| 1638 | if (!*str) /* EOL - do not finalize word */ |
| 1639 | break; |
| 1640 | o_addchr(output, '\0'); |
| 1641 | debug_print_list("expand_on_ifs", output, n); |
| 1642 | n = o_save_ptr(output, n); |
| 1643 | str += strspn(str, G.ifs); /* skip ifs chars */ |
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 | debug_print_list("expand_on_ifs[1]", output, n); |
| 1646 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1649 | #if ENABLE_HUSH_TICK |
| 1650 | static int process_command_subs(o_string *dest, |
| 1651 | struct in_str *input, const char *subst_end); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1652 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1653 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1654 | /* Expand all variable references in given string, adding words to list[] |
| 1655 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 1656 | * to be filled). This routine is extremely tricky: has to deal with |
| 1657 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 1658 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
| 1659 | 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] | 1660 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1661 | /* or_mask is either 0 (normal case) or 0x80 |
| 1662 | * (expansion of right-hand side of assignment == 1-element expand. |
| 1663 | * 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] | 1664 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1665 | char first_ch, ored_ch; |
| 1666 | int i; |
| 1667 | const char *val; |
| 1668 | char *p; |
| 1669 | |
| 1670 | ored_ch = 0; |
| 1671 | |
| 1672 | debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg); |
| 1673 | debug_print_list("expand_vars_to_list", output, n); |
| 1674 | n = o_save_ptr(output, n); |
| 1675 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 1676 | |
| 1677 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 1678 | #if ENABLE_HUSH_TICK |
| 1679 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1680 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1681 | o_addstr(output, arg, p - arg); |
| 1682 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 1683 | arg = ++p; |
| 1684 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 1685 | |
| 1686 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
| 1687 | /* "$@" is special. Even if quoted, it can still |
| 1688 | * expand to nothing (not even an empty string) */ |
| 1689 | if ((first_ch & 0x7f) != '@') |
| 1690 | ored_ch |= first_ch; |
| 1691 | val = NULL; |
| 1692 | switch (first_ch & 0x7f) { |
| 1693 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 1694 | case '$': /* pid */ |
| 1695 | val = utoa(G.root_pid); |
| 1696 | break; |
| 1697 | case '!': /* bg pid */ |
| 1698 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
| 1699 | break; |
| 1700 | case '?': /* exitcode */ |
| 1701 | val = utoa(G.last_return_code); |
| 1702 | break; |
| 1703 | case '#': /* argc */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1704 | if (arg[1] != SPECIAL_VAR_SYMBOL) |
| 1705 | /* actually, it's a ${#var} */ |
| 1706 | goto case_default; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1707 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 1708 | break; |
| 1709 | case '*': |
| 1710 | case '@': |
| 1711 | i = 1; |
| 1712 | if (!G.global_argv[i]) |
| 1713 | break; |
| 1714 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
| 1715 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
| 1716 | smallint sv = output->o_quote; |
| 1717 | /* unquoted var's contents should be globbed, so don't quote */ |
| 1718 | output->o_quote = 0; |
| 1719 | while (G.global_argv[i]) { |
| 1720 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 1721 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 1722 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 1723 | /* this argv[] is not empty and not last: |
| 1724 | * put terminating NUL, start new word */ |
| 1725 | o_addchr(output, '\0'); |
| 1726 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 1727 | n = o_save_ptr(output, n); |
| 1728 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 1729 | } |
| 1730 | } |
| 1731 | output->o_quote = sv; |
| 1732 | } else |
| 1733 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
| 1734 | * and in this case should treat it like '$*' - see 'else...' below */ |
| 1735 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
| 1736 | while (1) { |
| 1737 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 1738 | if (++i >= G.global_argc) |
| 1739 | break; |
| 1740 | o_addchr(output, '\0'); |
| 1741 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 1742 | n = o_save_ptr(output, n); |
| 1743 | } |
| 1744 | } else { /* quoted $*: add as one word */ |
| 1745 | while (1) { |
| 1746 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 1747 | if (!G.global_argv[++i]) |
| 1748 | break; |
| 1749 | if (G.ifs[0]) |
| 1750 | o_addchr(output, G.ifs[0]); |
| 1751 | } |
| 1752 | } |
| 1753 | break; |
| 1754 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 1755 | /* "Empty variable", used to make "" etc to not disappear */ |
| 1756 | arg++; |
| 1757 | ored_ch = 0x80; |
| 1758 | break; |
| 1759 | #if ENABLE_HUSH_TICK |
| 1760 | case '`': { /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
| 1761 | struct in_str input; |
| 1762 | *p = '\0'; |
| 1763 | arg++; |
| 1764 | //TODO: can we just stuff it into "output" directly? |
| 1765 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 1766 | setup_string_in_str(&input, arg); |
| 1767 | process_command_subs(&subst_result, &input, NULL); |
| 1768 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
| 1769 | val = subst_result.data; |
| 1770 | goto store_val; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1771 | } |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1772 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1773 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1774 | case_default: { |
| 1775 | bool exp_len = false, exp_null = false; |
| 1776 | char *var = arg, exp_save, exp_op, *exp_word; |
| 1777 | size_t exp_off = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1778 | *p = '\0'; |
| 1779 | arg[0] = first_ch & 0x7f; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1780 | |
| 1781 | /* prepare for expansions */ |
| 1782 | if (var[0] == '#') { |
| 1783 | /* handle length expansion ${#var} */ |
| 1784 | exp_len = true; |
| 1785 | ++var; |
| 1786 | } else { |
| 1787 | /* maybe handle parameter expansion */ |
| 1788 | exp_off = strcspn(var, ":-=+?"); |
| 1789 | if (!var[exp_off]) |
| 1790 | exp_off = 0; |
| 1791 | if (exp_off) { |
| 1792 | exp_save = var[exp_off]; |
| 1793 | exp_null = exp_save == ':'; |
| 1794 | exp_word = var + exp_off; |
| 1795 | if (exp_null) ++exp_word; |
| 1796 | exp_op = *exp_word++; |
| 1797 | var[exp_off] = '\0'; |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | /* lookup the variable in question */ |
| 1802 | if (isdigit(var[0])) { |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 1803 | /* handle_dollar() should have vetted var for us */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1804 | i = xatoi_u(var); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1805 | if (i < G.global_argc) |
| 1806 | val = G.global_argv[i]; |
| 1807 | /* else val remains NULL: $N with too big N */ |
| 1808 | } else |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1809 | val = lookup_param(var); |
| 1810 | |
| 1811 | /* handle any expansions */ |
| 1812 | if (exp_len) { |
| 1813 | debug_printf_expand("expand: length of '%s' = ", val); |
| 1814 | val = utoa(val ? strlen(val) : 0); |
| 1815 | debug_printf_expand("%s\n", val); |
| 1816 | } else if (exp_off) { |
| 1817 | /* we need to do an expansion */ |
| 1818 | int exp_test = (!val || (exp_null && !val[0])); |
| 1819 | if (exp_op == '+') |
| 1820 | exp_test = !exp_test; |
| 1821 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 1822 | exp_null ? "true" : "false", exp_test); |
| 1823 | if (exp_test) { |
| 1824 | if (exp_op == '?') |
| 1825 | maybe_die(var, *exp_word ? exp_word : "parameter null or not set"); |
| 1826 | else |
| 1827 | val = exp_word; |
| 1828 | |
| 1829 | if (exp_op == '=') { |
| 1830 | if (isdigit(var[0]) || var[0] == '#') { |
| 1831 | maybe_die(var, "special vars cannot assign in this way"); |
| 1832 | val = NULL; |
| 1833 | } else { |
| 1834 | char *new_var = xmalloc(strlen(var) + strlen(val) + 2); |
| 1835 | sprintf(new_var, "%s=%s", var, val); |
| 1836 | set_local_var(new_var, -1); |
| 1837 | } |
| 1838 | } |
| 1839 | } |
| 1840 | var[exp_off] = exp_save; |
| 1841 | } |
| 1842 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1843 | arg[0] = first_ch; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1844 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1845 | #if ENABLE_HUSH_TICK |
| 1846 | store_val: |
| 1847 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1848 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
| 1849 | debug_printf_expand("unquoted '%s', output->o_quote:%d\n", val, output->o_quote); |
| 1850 | if (val) { |
| 1851 | /* unquoted var's contents should be globbed, so don't quote */ |
| 1852 | smallint sv = output->o_quote; |
| 1853 | output->o_quote = 0; |
| 1854 | n = expand_on_ifs(output, n, val); |
| 1855 | val = NULL; |
| 1856 | output->o_quote = sv; |
| 1857 | } |
| 1858 | } else { /* quoted $VAR, val will be appended below */ |
| 1859 | debug_printf_expand("quoted '%s', output->o_quote:%d\n", val, output->o_quote); |
| 1860 | } |
| 1861 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1862 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1863 | if (val) { |
| 1864 | o_addQstr(output, val, strlen(val)); |
| 1865 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1866 | /* Do the check to avoid writing to a const string */ |
| 1867 | if (p && *p != SPECIAL_VAR_SYMBOL) |
| 1868 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1869 | |
| 1870 | #if ENABLE_HUSH_TICK |
| 1871 | o_free(&subst_result); |
| 1872 | #endif |
| 1873 | arg = ++p; |
| 1874 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 1875 | |
| 1876 | if (arg[0]) { |
| 1877 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 1878 | /* this part is literal, and it was already pre-quoted |
| 1879 | * if needed (much earlier), do not use o_addQstr here! */ |
| 1880 | o_addstr(output, arg, strlen(arg) + 1); |
| 1881 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 1882 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 1883 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 1884 | ) { |
| 1885 | n--; |
| 1886 | /* allow to reuse list[n] later without re-growth */ |
| 1887 | output->has_empty_slot = 1; |
| 1888 | } else { |
| 1889 | o_addchr(output, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1890 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1891 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1894 | static char **expand_variables(char **argv, int or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1895 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1896 | int n; |
| 1897 | char **list; |
| 1898 | char **v; |
| 1899 | o_string output = NULL_O_STRING; |
| 1900 | |
| 1901 | if (or_mask & 0x100) { |
| 1902 | output.o_quote = 1; /* protect against globbing for "$var" */ |
| 1903 | /* (unquoted $var will temporarily switch it off) */ |
| 1904 | output.o_glob = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1905 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1906 | |
| 1907 | n = 0; |
| 1908 | v = argv; |
| 1909 | while (*v) { |
| 1910 | n = expand_vars_to_list(&output, n, *v, (char)or_mask); |
| 1911 | v++; |
| 1912 | } |
| 1913 | debug_print_list("expand_variables", &output, n); |
| 1914 | |
| 1915 | /* output.data (malloced in one block) gets returned in "list" */ |
| 1916 | list = o_finalize_list(&output, n); |
| 1917 | debug_print_strings("expand_variables[1]", list); |
| 1918 | return list; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1921 | static char **expand_strvec_to_strvec(char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1922 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1923 | return expand_variables(argv, 0x100); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1926 | /* Used for expansion of right hand of assignments */ |
| 1927 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 1928 | * "v=/bin/c*" */ |
| 1929 | static char *expand_string_to_string(const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1930 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1931 | char *argv[2], **list; |
| 1932 | |
| 1933 | argv[0] = (char*)str; |
| 1934 | argv[1] = NULL; |
| 1935 | list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */ |
| 1936 | if (HUSH_DEBUG) |
| 1937 | if (!list[0] || list[1]) |
| 1938 | bb_error_msg_and_die("BUG in varexp2"); |
| 1939 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 1940 | overlapping_strcpy((char*)list, list[0]); |
| 1941 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 1942 | return (char*)list; |
| 1943 | } |
| 1944 | |
| 1945 | /* Used for "eval" builtin */ |
| 1946 | static char* expand_strvec_to_string(char **argv) |
| 1947 | { |
| 1948 | char **list; |
| 1949 | |
| 1950 | list = expand_variables(argv, 0x80); |
| 1951 | /* Convert all NULs to spaces */ |
| 1952 | if (list[0]) { |
| 1953 | int n = 1; |
| 1954 | while (list[n]) { |
| 1955 | if (HUSH_DEBUG) |
| 1956 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 1957 | bb_error_msg_and_die("BUG in varexp3"); |
| 1958 | list[n][-1] = ' '; /* TODO: or to G.ifs[0]? */ |
| 1959 | n++; |
| 1960 | } |
| 1961 | } |
| 1962 | overlapping_strcpy((char*)list, list[0]); |
| 1963 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 1964 | return (char*)list; |
| 1965 | } |
| 1966 | |
| 1967 | static char **expand_assignments(char **argv, int count) |
| 1968 | { |
| 1969 | int i; |
| 1970 | char **p = NULL; |
| 1971 | /* Expand assignments into one string each */ |
| 1972 | for (i = 0; i < count; i++) { |
| 1973 | p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 1974 | } |
| 1975 | return p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1978 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1979 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 1980 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1981 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1982 | { |
| 1983 | int openfd, mode; |
| 1984 | struct redir_struct *redir; |
| 1985 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1986 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1987 | if (redir->dup == -1 && redir->rd_filename == NULL) { |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 1988 | /* something went wrong in the parse. Pretend it didn't happen */ |
| 1989 | continue; |
| 1990 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1991 | if (redir->dup == -1) { |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1992 | char *p; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1993 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 1994 | //TODO: check redir for names like '\\' |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1995 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1996 | openfd = open_or_warn(p, mode); |
| 1997 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1998 | if (openfd < 0) { |
| 1999 | /* this could get lost if stderr has been redirected, but |
| 2000 | bash and ash both lose it as well (though zsh doesn't!) */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2001 | return 1; |
| 2002 | } |
| 2003 | } else { |
| 2004 | openfd = redir->dup; |
| 2005 | } |
| 2006 | |
| 2007 | if (openfd != redir->fd) { |
| 2008 | if (squirrel && redir->fd < 3) { |
| 2009 | squirrel[redir->fd] = dup(redir->fd); |
| 2010 | } |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2011 | if (openfd == -3) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2012 | //close(openfd); // close(-3) ??! |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2013 | } else { |
| 2014 | dup2(openfd, redir->fd); |
Matt Kraai | c616e53 | 2001-06-05 16:50:08 +0000 | [diff] [blame] | 2015 | if (redir->dup == -1) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2016 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2017 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | return 0; |
| 2021 | } |
| 2022 | |
| 2023 | static void restore_redirects(int squirrel[]) |
| 2024 | { |
| 2025 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2026 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2027 | fd = squirrel[i]; |
| 2028 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2029 | /* We simply die on error */ |
| 2030 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2031 | } |
| 2032 | } |
| 2033 | } |
| 2034 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2035 | |
| 2036 | #if !defined(DEBUG_CLEAN) |
| 2037 | #define free_pipe_list(head, indent) free_pipe_list(head) |
| 2038 | #define free_pipe(pi, indent) free_pipe(pi) |
| 2039 | #endif |
| 2040 | static int free_pipe_list(struct pipe *head, int indent); |
| 2041 | |
| 2042 | /* return code is the exit status of the pipe */ |
| 2043 | static int free_pipe(struct pipe *pi, int indent) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2044 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2045 | char **p; |
| 2046 | struct command *command; |
| 2047 | struct redir_struct *r, *rnext; |
| 2048 | int a, i, ret_code = 0; |
| 2049 | |
| 2050 | if (pi->stopped_cmds > 0) |
| 2051 | return ret_code; |
| 2052 | debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid()); |
| 2053 | for (i = 0; i < pi->num_cmds; i++) { |
| 2054 | command = &pi->cmds[i]; |
| 2055 | debug_printf_clean("%s command %d:\n", indenter(indent), i); |
| 2056 | if (command->argv) { |
| 2057 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 2058 | debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p); |
| 2059 | } |
| 2060 | free_strings(command->argv); |
| 2061 | command->argv = NULL; |
| 2062 | } else if (command->group) { |
| 2063 | debug_printf_clean("%s begin group (grp_type:%d)\n", indenter(indent), command->grp_type); |
| 2064 | ret_code = free_pipe_list(command->group, indent+3); |
| 2065 | debug_printf_clean("%s end group\n", indenter(indent)); |
| 2066 | } else { |
| 2067 | debug_printf_clean("%s (nil)\n", indenter(indent)); |
| 2068 | } |
| 2069 | for (r = command->redirects; r; r = rnext) { |
| 2070 | debug_printf_clean("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->rd_type].descrip); |
| 2071 | if (r->dup == -1) { |
| 2072 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 2073 | if (r->rd_filename) { |
| 2074 | debug_printf_clean(" %s\n", r->rd_filename); |
| 2075 | free(r->rd_filename); |
| 2076 | r->rd_filename = NULL; |
| 2077 | } |
| 2078 | } else { |
| 2079 | debug_printf_clean("&%d\n", r->dup); |
| 2080 | } |
| 2081 | rnext = r->next; |
| 2082 | free(r); |
| 2083 | } |
| 2084 | command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2085 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2086 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 2087 | pi->cmds = NULL; |
| 2088 | #if ENABLE_HUSH_JOB |
| 2089 | free(pi->cmdtext); |
| 2090 | pi->cmdtext = NULL; |
| 2091 | #endif |
| 2092 | return ret_code; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2093 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2094 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2095 | static int free_pipe_list(struct pipe *head, int indent) |
| 2096 | { |
| 2097 | int rcode = 0; /* if list has no members */ |
| 2098 | struct pipe *pi, *next; |
| 2099 | |
| 2100 | for (pi = head; pi; pi = next) { |
| 2101 | #if HAS_KEYWORDS |
| 2102 | debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word); |
| 2103 | #endif |
| 2104 | rcode = free_pipe(pi, indent); |
| 2105 | debug_printf_clean("%s pipe followup code %d\n", indenter(indent), pi->followup); |
| 2106 | next = pi->next; |
| 2107 | /*pi->next = NULL;*/ |
| 2108 | free(pi); |
| 2109 | } |
| 2110 | return rcode; |
| 2111 | } |
| 2112 | |
| 2113 | |
| 2114 | #if !BB_MMU |
| 2115 | typedef struct nommu_save_t { |
| 2116 | char **new_env; |
| 2117 | char **old_env; |
| 2118 | char **argv; |
| 2119 | } nommu_save_t; |
| 2120 | #else |
| 2121 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 2122 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 2123 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 2124 | pseudo_exec(command, argv_expanded) |
| 2125 | #endif |
| 2126 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2127 | /* Called after [v]fork() in run_pipe(), or from builtin_exec(). |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2128 | * Never returns. |
| 2129 | * XXX no exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2130 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2131 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2132 | 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] | 2133 | 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] | 2134 | { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2135 | int rcode; |
| 2136 | char **new_env; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 2137 | const struct built_in_command *x; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2138 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2139 | /* If a variable is assigned in a forest, and nobody listens, |
| 2140 | * was it ever really set? |
| 2141 | */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2142 | if (!argv[assignment_cnt]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2143 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2144 | |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 2145 | new_env = expand_assignments(argv, assignment_cnt); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2146 | #if BB_MMU |
| 2147 | putenv_all(new_env); |
| 2148 | free(new_env); /* optional */ |
| 2149 | #else |
| 2150 | nommu_save->new_env = new_env; |
| 2151 | nommu_save->old_env = putenv_all_and_save_old(new_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2152 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2153 | if (argv_expanded) { |
| 2154 | argv = argv_expanded; |
| 2155 | } else { |
| 2156 | argv = expand_strvec_to_strvec(argv); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2157 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2158 | nommu_save->argv = argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2159 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2160 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2161 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2162 | /* |
| 2163 | * Check if the command matches any of the builtins. |
| 2164 | * Depending on context, this might be redundant. But it's |
| 2165 | * easier to waste a few CPU cycles than it is to figure out |
| 2166 | * if this is one of those cases. |
| 2167 | */ |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 2168 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2169 | if (strcmp(argv[0], x->cmd) == 0) { |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2170 | debug_printf_exec("running builtin '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2171 | rcode = x->function(argv); |
| 2172 | fflush(stdout); |
| 2173 | _exit(rcode); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2174 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2175 | } |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2176 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2177 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 2178 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2179 | if (strchr(argv[0], '/') == NULL) { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 2180 | int a = find_applet_by_name(argv[0]); |
| 2181 | if (a >= 0) { |
| 2182 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2183 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 2184 | // is it ok that run_applet_no_and_exit() does exit(), not _exit()? |
| 2185 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2186 | } |
| 2187 | /* re-exec ourselves with the new arguments */ |
| 2188 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | bdbbb7e | 2007-06-08 15:02:55 +0000 | [diff] [blame] | 2189 | execvp(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2190 | /* If they called chroot or otherwise made the binary no longer |
| 2191 | * executable, fall through */ |
| 2192 | } |
| 2193 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 2194 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2195 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2196 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 2197 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2198 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2199 | execvp(argv[0], argv); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 2200 | bb_perror_msg("can't exec '%s'", argv[0]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 2201 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2204 | static int run_list(struct pipe *pi); |
| 2205 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2206 | /* Called after [v]fork() in run_pipe() |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2207 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2208 | 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] | 2209 | 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] | 2210 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2211 | if (command->argv) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2212 | pseudo_exec_argv(nommu_save, command->argv, command->assignment_cnt, argv_expanded); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2213 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2214 | if (command->group) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2215 | #if !BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 2216 | bb_error_msg_and_die("nested lists are not supported on NOMMU"); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2217 | #else |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 2218 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2219 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2220 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2221 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2222 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2223 | _exit(rcode); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2224 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2225 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2226 | |
| 2227 | /* Can happen. See what bash does with ">foo" by itself. */ |
| 2228 | debug_printf("trying to pseudo_exec null command\n"); |
| 2229 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2232 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2233 | static const char *get_cmdtext(struct pipe *pi) |
| 2234 | { |
| 2235 | char **argv; |
| 2236 | char *p; |
| 2237 | int len; |
| 2238 | |
| 2239 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 2240 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2241 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2242 | if (pi->cmdtext) |
| 2243 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2244 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2245 | if (!argv || !argv[0]) { |
| 2246 | pi->cmdtext = xzalloc(1); |
| 2247 | return pi->cmdtext; |
| 2248 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2249 | |
| 2250 | len = 0; |
| 2251 | do len += strlen(*argv) + 1; while (*++argv); |
| 2252 | pi->cmdtext = p = xmalloc(len); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2253 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2254 | do { |
| 2255 | len = strlen(*argv); |
| 2256 | memcpy(p, *argv, len); |
| 2257 | p += len; |
| 2258 | *p++ = ' '; |
| 2259 | } while (*++argv); |
| 2260 | p[-1] = '\0'; |
| 2261 | return pi->cmdtext; |
| 2262 | } |
| 2263 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2264 | static void insert_bg_job(struct pipe *pi) |
| 2265 | { |
| 2266 | struct pipe *thejob; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2267 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2268 | |
| 2269 | /* Linear search for the ID of the job to use */ |
| 2270 | pi->jobid = 1; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2271 | for (thejob = G.job_list; thejob; thejob = thejob->next) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2272 | if (thejob->jobid >= pi->jobid) |
| 2273 | pi->jobid = thejob->jobid + 1; |
| 2274 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2275 | /* Add thejob to the list of running jobs */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2276 | if (!G.job_list) { |
| 2277 | thejob = G.job_list = xmalloc(sizeof(*thejob)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2278 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2279 | for (thejob = G.job_list; thejob->next; thejob = thejob->next) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2280 | continue; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2281 | thejob->next = xmalloc(sizeof(*thejob)); |
| 2282 | thejob = thejob->next; |
| 2283 | } |
| 2284 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2285 | /* Physically copy the struct job */ |
Eric Andersen | 0fcd447 | 2001-05-02 20:12:03 +0000 | [diff] [blame] | 2286 | memcpy(thejob, pi, sizeof(struct pipe)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2287 | thejob->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 2288 | /* We cannot copy entire pi->cmds[] vector! Double free()s will happen */ |
| 2289 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2290 | // TODO: do we really need to have so many fields which are just dead weight |
| 2291 | // at execution stage? |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2292 | thejob->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2293 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2294 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2295 | thejob->next = NULL; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2296 | thejob->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2297 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2298 | /* We don't wait for background thejobs to return -- append it |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2299 | to the list of backgrounded thejobs and leave it alone */ |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 2300 | if (G.interactive_fd) |
| 2301 | 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] | 2302 | G.last_bg_pid = thejob->cmds[0].pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2303 | G.last_jobid = thejob->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2306 | static void remove_bg_job(struct pipe *pi) |
| 2307 | { |
| 2308 | struct pipe *prev_pipe; |
| 2309 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2310 | if (pi == G.job_list) { |
| 2311 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2312 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2313 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2314 | while (prev_pipe->next != pi) |
| 2315 | prev_pipe = prev_pipe->next; |
| 2316 | prev_pipe->next = pi->next; |
| 2317 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2318 | if (G.job_list) |
| 2319 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 2320 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2321 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2322 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 2323 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2324 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2325 | static void delete_finished_bg_job(struct pipe *pi) |
| 2326 | { |
| 2327 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2328 | pi->stopped_cmds = 0; |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2329 | free_pipe(pi, 0); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2330 | free(pi); |
| 2331 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2332 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2333 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2334 | /* Check to see if any processes have exited -- if they |
| 2335 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2336 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2337 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2338 | int attributes; |
| 2339 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2340 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2341 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2342 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2343 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2344 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2345 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2346 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 2347 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 2348 | errno = 0; |
| 2349 | // if (G.handled_SIGCHLD == G.count_SIGCHLD) |
| 2350 | // /* avoid doing syscall, nothing there anyway */ |
| 2351 | // return rcode; |
| 2352 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2353 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2354 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2355 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2356 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2357 | /* Do we do this right? |
| 2358 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2359 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2360 | * [3]+ Stopped sleep 20 | false |
| 2361 | * bash-3.00# echo $? |
| 2362 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 2363 | */ |
| 2364 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2365 | //FIXME: non-interactive bash does not continue even if all processes in fg pipe |
| 2366 | //are stopped. Testcase: "cat | cat" in a script (not on command line) |
| 2367 | // + killall -STOP cat |
| 2368 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2369 | wait_more: |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 2370 | while (1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2371 | int i; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 2372 | int dead; |
| 2373 | |
| 2374 | // i = G.count_SIGCHLD; |
| 2375 | childpid = waitpid(-1, &status, attributes); |
| 2376 | if (childpid <= 0) { |
| 2377 | if (childpid && errno != ECHILD) |
| 2378 | bb_perror_msg("waitpid"); |
| 2379 | // else /* Until next SIGCHLD, waitpid's are useless */ |
| 2380 | // G.handled_SIGCHLD = i; |
| 2381 | break; |
| 2382 | } |
| 2383 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 2384 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2385 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2386 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 2387 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2388 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 2389 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 2390 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2391 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 2392 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 2393 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2394 | childpid, WEXITSTATUS(status)); |
| 2395 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2396 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2397 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2398 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 2399 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 2400 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2401 | continue; |
| 2402 | /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */ |
| 2403 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2404 | fg_pipe->cmds[i].pid = 0; |
| 2405 | fg_pipe->alive_cmds--; |
| 2406 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2407 | /* last process gives overall exitstatus */ |
| 2408 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2409 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2410 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2411 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2412 | fg_pipe->cmds[i].is_stopped = 1; |
| 2413 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2414 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2415 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 2416 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 2417 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2418 | /* All processes in fg pipe have exited/stopped */ |
| 2419 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2420 | if (fg_pipe->alive_cmds) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2421 | insert_bg_job(fg_pipe); |
| 2422 | #endif |
| 2423 | return rcode; |
| 2424 | } |
| 2425 | /* There are still running processes in the fg pipe */ |
| 2426 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2427 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2428 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2431 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2432 | /* We asked to wait for bg or orphaned children */ |
| 2433 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2434 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2435 | for (i = 0; i < pi->num_cmds; i++) { |
| 2436 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2437 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 2438 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2439 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2440 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 2441 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2442 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 2443 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2444 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2445 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2446 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2447 | pi->cmds[i].pid = 0; |
| 2448 | pi->alive_cmds--; |
| 2449 | if (!pi->alive_cmds) { |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 2450 | if (G.interactive_fd) |
| 2451 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 2452 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2453 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2454 | } |
| 2455 | } else { |
| 2456 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2457 | pi->cmds[i].is_stopped = 1; |
| 2458 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2459 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2460 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 2461 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 2462 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2463 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2466 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 2467 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 2468 | { |
| 2469 | pid_t p; |
| 2470 | int rcode = checkjobs(fg_pipe); |
| 2471 | /* Job finished, move the shell to the foreground */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2472 | p = getpgid(0); /* pgid of our process */ |
| 2473 | debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2474 | tcsetpgrp(G.interactive_fd, p); |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 2475 | return rcode; |
| 2476 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2477 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 2478 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2479 | /* run_pipe() starts all the jobs, but doesn't wait for anything |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2480 | * to finish. See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2481 | * |
| 2482 | * return code is normally -1, when the caller has to wait for children |
| 2483 | * to finish to determine the exit status of the pipe. If the pipe |
| 2484 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2485 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2486 | * return value. |
| 2487 | * |
| 2488 | * The input of the pipe is always stdin, the output is always |
| 2489 | * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus, |
| 2490 | * because it tries to avoid running the command substitution in |
| 2491 | * subshell, when that is in fact necessary. The subshell process |
| 2492 | * now has its stdout directed to the input of the appropriate pipe, |
| 2493 | * so this routine is noticeably simpler. |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2494 | * |
| 2495 | * Returns -1 only if started some children. IOW: we have to |
| 2496 | * mask out retvals of builtins etc with 0xff! |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2497 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2498 | static int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2499 | { |
| 2500 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2501 | int nextin; |
| 2502 | int pipefds[2]; /* pipefds[0] is for reading */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2503 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2504 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2505 | char **argv; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 2506 | const struct built_in_command *x; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2507 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 2508 | /* it is not always needed, but we aim to smaller code */ |
| 2509 | int squirrel[] = { -1, -1, -1 }; |
| 2510 | int rcode; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2511 | 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] | 2512 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2513 | 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] | 2514 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2515 | #if ENABLE_HUSH_JOB |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 2516 | pi->pgrp = -1; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2517 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2518 | pi->alive_cmds = 1; |
| 2519 | pi->stopped_cmds = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2520 | |
| 2521 | /* Check if this is a simple builtin (not part of a pipe). |
| 2522 | * Builtins within pipes have to fork anyway, and are handled in |
| 2523 | * pseudo_exec. "echo foo | read bar" doesn't work on bash, either. |
| 2524 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2525 | command = &(pi->cmds[0]); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2526 | |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2527 | #if ENABLE_HUSH_FUNCTIONS |
| 2528 | if (single_and_fg && command->group && command->grp_type == GRP_FUNCTION) { |
| 2529 | /* We "execute" function definition */ |
| 2530 | bb_error_msg("here we ought to remember function definition, and go on"); |
| 2531 | return EXIT_SUCCESS; |
| 2532 | } |
| 2533 | #endif |
| 2534 | |
| 2535 | if (single_and_fg && command->group && command->grp_type == GRP_NORMAL) { |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 2536 | debug_printf("non-subshell grouping\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2537 | setup_redirects(command, squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2538 | debug_printf_exec(": run_list\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2539 | rcode = run_list(command->group) & 0xff; |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 2540 | restore_redirects(squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2541 | debug_printf_exec("run_pipe return %d\n", rcode); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2542 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2543 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2546 | argv = command->argv; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2547 | argv_expanded = NULL; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2548 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2549 | if (single_and_fg && argv != NULL) { |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2550 | char **new_env = NULL; |
| 2551 | char **old_env = NULL; |
| 2552 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2553 | i = command->assignment_cnt; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2554 | if (i != 0 && argv[i] == NULL) { |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2555 | /* assignments, but no command: set local environment */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 2556 | for (i = 0; argv[i] != NULL; i++) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2557 | debug_printf("local environment set: %s\n", argv[i]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2558 | p = expand_string_to_string(argv[i]); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2559 | set_local_var(p, 0); |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2560 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2561 | 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] | 2562 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2563 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2564 | /* Expand the rest into (possibly) many strings each */ |
| 2565 | argv_expanded = expand_strvec_to_strvec(argv + i); |
| 2566 | |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 2567 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2568 | if (strcmp(argv_expanded[0], x->cmd) != 0) |
| 2569 | continue; |
| 2570 | if (x->function == builtin_exec && argv_expanded[1] == NULL) { |
| 2571 | debug_printf("exec with redirects only\n"); |
| 2572 | setup_redirects(command, NULL); |
| 2573 | rcode = EXIT_SUCCESS; |
| 2574 | goto clean_up_and_ret1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2575 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2576 | debug_printf("builtin inline %s\n", argv_expanded[0]); |
| 2577 | /* XXX setup_redirects acts on file descriptors, not FILEs. |
| 2578 | * This is perfect for work that comes after exec(). |
| 2579 | * Is it really safe for inline use? Experimentally, |
| 2580 | * things seem to work with glibc. */ |
| 2581 | setup_redirects(command, squirrel); |
| 2582 | new_env = expand_assignments(argv, command->assignment_cnt); |
| 2583 | old_env = putenv_all_and_save_old(new_env); |
| 2584 | debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv_expanded[1]); |
| 2585 | rcode = x->function(argv_expanded) & 0xff; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2586 | #if ENABLE_FEATURE_SH_STANDALONE |
| 2587 | clean_up_and_ret: |
| 2588 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2589 | restore_redirects(squirrel); |
| 2590 | free_strings_and_unsetenv(new_env, 1); |
| 2591 | putenv_all(old_env); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2592 | free(old_env); /* not free_strings()! */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2593 | clean_up_and_ret1: |
| 2594 | free(argv_expanded); |
| 2595 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 2596 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 2597 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2598 | } |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 2599 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2600 | i = find_applet_by_name(argv_expanded[0]); |
| 2601 | if (i >= 0 && APPLET_IS_NOFORK(i)) { |
| 2602 | setup_redirects(command, squirrel); |
| 2603 | save_nofork_data(&G.nofork_save); |
| 2604 | new_env = expand_assignments(argv, command->assignment_cnt); |
| 2605 | old_env = putenv_all_and_save_old(new_env); |
| 2606 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]); |
| 2607 | rcode = run_nofork_applet_prime(&G.nofork_save, i, argv_expanded); |
| 2608 | goto clean_up_and_ret; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 2609 | } |
| 2610 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2613 | /* NB: argv_expanded may already be created, and that |
| 2614 | * might include `cmd` runs! Do not rerun it! We *must* |
| 2615 | * use argv_expanded if it's non-NULL */ |
| 2616 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2617 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2618 | pi->alive_cmds = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2619 | nextin = 0; |
| 2620 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2621 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2622 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2623 | volatile nommu_save_t nommu_save; |
| 2624 | nommu_save.new_env = NULL; |
| 2625 | nommu_save.old_env = NULL; |
| 2626 | nommu_save.argv = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 2627 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2628 | command = &(pi->cmds[i]); |
| 2629 | if (command->argv) { |
| 2630 | 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] | 2631 | } else |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2632 | debug_printf_exec(": pipe member with no argv\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2633 | |
| 2634 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2635 | pipefds[0] = 0; |
| 2636 | pipefds[1] = 1; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2637 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2638 | xpipe(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2639 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2640 | command->pid = BB_MMU ? fork() : vfork(); |
| 2641 | if (!command->pid) { /* child */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2642 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2643 | die_sleep = 0; /* let nofork's xfuncs die */ |
| 2644 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2645 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2646 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2647 | if (G.run_list_level == 1 && G.interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2648 | pid_t pgrp; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2649 | pgrp = pi->pgrp; |
| 2650 | if (pgrp < 0) /* true for 1st process only */ |
| 2651 | pgrp = getpid(); |
| 2652 | if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2653 | /* We do it in *every* child, not just first, |
| 2654 | * to avoid races */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2655 | tcsetpgrp(G.interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2656 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2657 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2658 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 2659 | xmove_fd(nextin, 0); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2660 | xmove_fd(pipefds[1], 1); /* write end */ |
| 2661 | if (pipefds[0] > 1) |
| 2662 | close(pipefds[0]); /* read end */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2663 | /* Like bash, explicit redirects override pipes, |
| 2664 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2665 | setup_redirects(command, NULL); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 2666 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2667 | /* Restore default handlers just prior to exec */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2668 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 2669 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2670 | /* Stores to nommu_save list of env vars putenv'ed |
| 2671 | * (NOMMU, on MMU we don't need that) */ |
| 2672 | /* cast away volatility... */ |
| 2673 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2674 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2675 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2676 | /* parent */ |
| 2677 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2678 | /* Clean up after vforked child */ |
| 2679 | free(nommu_save.argv); |
| 2680 | free_strings_and_unsetenv(nommu_save.new_env, 1); |
| 2681 | putenv_all(nommu_save.old_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2682 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2683 | free(argv_expanded); |
| 2684 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2685 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2686 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 2687 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2688 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2689 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2690 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2691 | /* Second and next children need to know pid of first one */ |
| 2692 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2693 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2694 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2695 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2696 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2697 | if (i) |
| 2698 | close(nextin); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2699 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2700 | close(pipefds[1]); /* write end */ |
| 2701 | /* Pass read (output) pipe end to next iteration */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2702 | nextin = pipefds[0]; |
| 2703 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2704 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2705 | if (!pi->alive_cmds) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2706 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 2707 | return 1; |
| 2708 | } |
| 2709 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2710 | 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] | 2711 | return -1; |
| 2712 | } |
| 2713 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 2714 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2715 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 2716 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2717 | static const char *const PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2718 | [PIPE_SEQ] = "SEQ", |
| 2719 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2720 | [PIPE_OR ] = "OR" , |
| 2721 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2722 | }; |
| 2723 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2724 | [RES_NONE ] = "NONE" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2725 | #if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2726 | [RES_IF ] = "IF" , |
| 2727 | [RES_THEN ] = "THEN" , |
| 2728 | [RES_ELIF ] = "ELIF" , |
| 2729 | [RES_ELSE ] = "ELSE" , |
| 2730 | [RES_FI ] = "FI" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2731 | #endif |
| 2732 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2733 | [RES_FOR ] = "FOR" , |
| 2734 | [RES_WHILE] = "WHILE", |
| 2735 | [RES_UNTIL] = "UNTIL", |
| 2736 | [RES_DO ] = "DO" , |
| 2737 | [RES_DONE ] = "DONE" , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 2738 | #endif |
| 2739 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2740 | [RES_IN ] = "IN" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2741 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2742 | #if ENABLE_HUSH_CASE |
| 2743 | [RES_CASE ] = "CASE" , |
| 2744 | [RES_MATCH] = "MATCH", |
| 2745 | [RES_CASEI] = "CASEI", |
| 2746 | [RES_ESAC ] = "ESAC" , |
| 2747 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2748 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2749 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2750 | }; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2751 | static const char *const GRPTYPE[] = { |
| 2752 | "()", |
| 2753 | "{}", |
| 2754 | #if ENABLE_HUSH_FUNCTIONS |
| 2755 | "func()", |
| 2756 | #endif |
| 2757 | }; |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2758 | |
| 2759 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2760 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2761 | pin = 0; |
| 2762 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2763 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 2764 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2765 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2766 | while (prn < pi->num_cmds) { |
| 2767 | struct command *command = &pi->cmds[prn]; |
| 2768 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2769 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2770 | fprintf(stderr, "%*s prog %d assignment_cnt:%d", lvl*2, "", prn, command->assignment_cnt); |
| 2771 | if (command->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2772 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2773 | GRPTYPE[command->grp_type], |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2774 | argv); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2775 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2776 | prn++; |
| 2777 | continue; |
| 2778 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2779 | if (argv) while (*argv) { |
| 2780 | fprintf(stderr, " '%s'", *argv); |
| 2781 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 2782 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2783 | fprintf(stderr, "\n"); |
| 2784 | prn++; |
| 2785 | } |
| 2786 | pi = pi->next; |
| 2787 | pin++; |
| 2788 | } |
| 2789 | } |
| 2790 | #endif |
| 2791 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2792 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 2793 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2794 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2795 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2796 | #if ENABLE_HUSH_CASE |
| 2797 | char *case_word = NULL; |
| 2798 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2799 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2800 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2801 | char *for_varname = NULL; |
| 2802 | char **for_lcur = NULL; |
| 2803 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2804 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2805 | smallint flag_skip = 1; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2806 | smalluint rcode = 0; /* probably just for compiler */ |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 2807 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2808 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2809 | #else |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2810 | enum { cond_code = 0, }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2811 | #endif |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2812 | /*enum reserved_style*/ smallint rword = RES_NONE; |
| 2813 | /*enum reserved_style*/ smallint skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 2814 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2815 | 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] | 2816 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2817 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2818 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2819 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 2820 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2821 | continue; |
| 2822 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2823 | if (cpipe->next == NULL) { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2824 | syntax("malformed for"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2825 | 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] | 2826 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2827 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2828 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2829 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2830 | continue; |
| 2831 | /* 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] | 2832 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 2833 | || 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] | 2834 | ) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2835 | syntax("malformed for"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2836 | 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] | 2837 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2838 | } |
| 2839 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2840 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2841 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2842 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 2843 | * in order to return, no direct "return" statements please. |
| 2844 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2845 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2846 | ////TODO: ctrl-Z handling needs re-thinking and re-testing |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2847 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2848 | #if ENABLE_HUSH_JOB |
| 2849 | /* Example of nested list: "while true; do { sleep 1 | exit 2; } done". |
| 2850 | * We are saving state before entering outermost list ("while...done") |
| 2851 | * so that ctrl-Z will correctly background _entire_ outermost list, |
| 2852 | * not just a part of it (like "sleep 1 | exit 2") */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2853 | if (++G.run_list_level == 1 && G.interactive_fd) { |
| 2854 | if (sigsetjmp(G.toplevel_jb, 1)) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2855 | /* ctrl-Z forked and we are parent; or ctrl-C. |
| 2856 | * Sighandler has longjmped us here */ |
| 2857 | signal(SIGINT, SIG_IGN); |
| 2858 | signal(SIGTSTP, SIG_IGN); |
| 2859 | /* Restore level (we can be coming from deep inside |
| 2860 | * nested levels) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2861 | G.run_list_level = 1; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2862 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2863 | if (G.nofork_save.saved) { /* if save area is valid */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2864 | debug_printf_jobs("exiting nofork early\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2865 | restore_nofork_data(&G.nofork_save); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2866 | } |
| 2867 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2868 | //// if (G.ctrl_z_flag) { |
| 2869 | //// /* ctrl-Z has forked and stored pid of the child in pi->pid. |
| 2870 | //// * Remember this child as background job */ |
| 2871 | //// insert_bg_job(pi); |
| 2872 | //// } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2873 | /* ctrl-C. We just stop doing whatever we were doing */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 2874 | bb_putchar('\n'); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2875 | //// } |
Denis Vlasenko | ff29b4f | 2008-07-29 13:57:59 +0000 | [diff] [blame] | 2876 | USE_HUSH_LOOPS(loop_top = NULL;) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2877 | USE_HUSH_LOOPS(G.depth_of_loop = 0;) |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2878 | rcode = 0; |
| 2879 | goto ret; |
| 2880 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2881 | //// /* ctrl-Z handler will store pid etc in pi */ |
| 2882 | //// G.toplevel_list = pi; |
| 2883 | //// G.ctrl_z_flag = 0; |
| 2884 | ////#if ENABLE_FEATURE_SH_STANDALONE |
| 2885 | //// G.nofork_save.saved = 0; /* in case we will run a nofork later */ |
| 2886 | ////#endif |
| 2887 | //// signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z); |
| 2888 | //// signal(SIGINT, handler_ctrl_c); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2889 | } |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2890 | #endif /* JOB */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2891 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2892 | /* Go through list of pipes, (maybe) executing them. */ |
| 2893 | 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] | 2894 | if (G.flag_SIGINT) |
| 2895 | break; |
| 2896 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2897 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 2898 | IF_HAS_NO_KEYWORDS(rword = RES_NONE;) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2899 | debug_printf_exec(": rword=%d cond_code=%d skip_more=%d\n", |
| 2900 | rword, cond_code, skip_more_for_this_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2901 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 2902 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2903 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 2904 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2905 | /* start of a loop: remember where loop starts */ |
| 2906 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2907 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2908 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2909 | #endif |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2910 | if (rword == skip_more_for_this_rword && flag_skip) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2911 | if (pi->followup == PIPE_SEQ) |
| 2912 | flag_skip = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2913 | /* it is "<false> && CMD" or "<true> || CMD" |
| 2914 | * and we should not execute CMD */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2915 | continue; |
| 2916 | } |
| 2917 | flag_skip = 1; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2918 | skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2919 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2920 | if (cond_code) { |
| 2921 | if (rword == RES_THEN) { |
| 2922 | /* "if <false> THEN cmd": skip cmd */ |
| 2923 | continue; |
| 2924 | } |
| 2925 | } else { |
| 2926 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 2927 | /* "if <true> then ... ELSE/ELIF cmd": |
| 2928 | * skip cmd and all following ones */ |
| 2929 | break; |
| 2930 | } |
| 2931 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2932 | #endif |
| 2933 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2934 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2935 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2936 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2937 | |
| 2938 | static const char encoded_dollar_at[] ALIGN1 = { |
| 2939 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 2940 | }; /* encoded representation of "$@" */ |
| 2941 | static const char *const encoded_dollar_at_argv[] = { |
| 2942 | encoded_dollar_at, NULL |
| 2943 | }; /* argv list with one element: "$@" */ |
| 2944 | char **vals; |
| 2945 | |
| 2946 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2947 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2948 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2949 | if (!pi->next->cmds[0].argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2950 | break; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2951 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2952 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2953 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2954 | debug_print_strings("for_list made from", vals); |
| 2955 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2956 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2957 | debug_print_strings("for_list", for_list); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2958 | for_varname = pi->cmds[0].argv[0]; |
| 2959 | pi->cmds[0].argv[0] = NULL; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2960 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2961 | free(pi->cmds[0].argv[0]); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2962 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 2963 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2964 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2965 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2966 | for_lcur = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2967 | pi->cmds[0].argv[0] = for_varname; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2968 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2969 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2970 | /* insert next value from for_lcur */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2971 | //TODO: does it need escaping? |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2972 | pi->cmds[0].argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++); |
| 2973 | pi->cmds[0].assignment_cnt = 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2974 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2975 | 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] | 2976 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2977 | if (rword == RES_DONE) { |
| 2978 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2979 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2980 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2981 | #if ENABLE_HUSH_CASE |
| 2982 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2983 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2984 | continue; |
| 2985 | } |
| 2986 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 2987 | char **argv; |
| 2988 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2989 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 2990 | break; |
| 2991 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2992 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 2993 | while (*argv) { |
| 2994 | char *pattern = expand_string_to_string(*argv); |
| 2995 | /* TODO: which FNM_xxx flags to use? */ |
| 2996 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 2997 | free(pattern); |
| 2998 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 2999 | free(case_word); /* make future "word)" stop */ |
| 3000 | case_word = NULL; |
| 3001 | break; |
| 3002 | } |
| 3003 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3004 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3005 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3006 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3007 | if (rword == RES_CASEI) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3008 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3009 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3010 | } |
| 3011 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3012 | /* Just pressing <enter> in shell should check for jobs. |
| 3013 | * OTOH, in non-interactive shell this is useless |
| 3014 | * and only leads to extra job checks */ |
| 3015 | if (pi->num_cmds == 0) { |
| 3016 | if (G.interactive_fd) |
| 3017 | goto check_jobs_and_continue; |
| 3018 | continue; |
| 3019 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3020 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3021 | /* After analyzing all keywords and conditions, we decided |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3022 | * to execute this pipe. NB: have to do checkjobs(NULL) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3023 | * after run_pipe() to collect any background children, |
| 3024 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3025 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3026 | { |
| 3027 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3028 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3029 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3030 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3031 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 3032 | if (r != -1) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3033 | /* we only ran a builtin: rcode is already known |
| 3034 | * and we don't need to wait for anything. */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3035 | check_and_run_traps(0); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3036 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3037 | /* was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3038 | if (G.flag_break_continue) { |
| 3039 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3040 | /* we might fall into outer *loop*, |
| 3041 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3042 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3043 | G.depth_break_continue--; |
| 3044 | if (G.depth_break_continue == 0) |
| 3045 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 3046 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 3047 | } /* 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] | 3048 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 3049 | goto check_jobs_and_break; |
| 3050 | /* "continue": simulate end of loop */ |
| 3051 | rword = RES_DONE; |
| 3052 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3053 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 3054 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3055 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3056 | /* what does bash do with attempts to background builtins? */ |
| 3057 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3058 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 3059 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3060 | check_and_run_traps(0); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3061 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3062 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3063 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3064 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3065 | rcode = 0; /* EXIT_SUCCESS */ |
| 3066 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3067 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3068 | if (G.run_list_level == 1 && G.interactive_fd) { |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3069 | /* waits for completion, then fg's main shell */ |
| 3070 | rcode = checkjobs_and_fg_shell(pi); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3071 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3072 | debug_printf_exec(": checkjobs_and_fg_shell returned %d\n", rcode); |
| 3073 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3074 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3075 | { /* this one just waits for completion */ |
| 3076 | rcode = checkjobs(pi); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3077 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3078 | debug_printf_exec(": checkjobs returned %d\n", rcode); |
| 3079 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3080 | } |
| 3081 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3082 | debug_printf_exec(": setting last_return_code=%d\n", rcode); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3083 | G.last_return_code = rcode; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3084 | |
| 3085 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3086 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3087 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3088 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3089 | #endif |
| 3090 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3091 | if (rword == RES_WHILE) { |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 3092 | if (rcode) { |
| 3093 | rcode = 0; /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3094 | goto check_jobs_and_break; |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 3095 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3096 | } |
| 3097 | if (rword == RES_UNTIL) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 3098 | if (!rcode) { |
| 3099 | check_jobs_and_break: |
| 3100 | checkjobs(NULL); |
| 3101 | break; |
| 3102 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3103 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3104 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 3105 | if ((rcode == 0 && pi->followup == PIPE_OR) |
| 3106 | || (rcode != 0 && pi->followup == PIPE_AND) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3107 | ) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3108 | skip_more_for_this_rword = rword; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3109 | } |
Mike Frysinger | 8ec1c9d | 2009-03-29 00:45:26 +0000 | [diff] [blame] | 3110 | |
| 3111 | check_jobs_and_continue: |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 3112 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3113 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3114 | |
| 3115 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3116 | //// if (G.ctrl_z_flag) { |
| 3117 | //// /* ctrl-Z forked somewhere in the past, we are the child, |
| 3118 | //// * and now we completed running the list. Exit. */ |
| 3119 | //////TODO: _exit? |
| 3120 | //// exit(rcode); |
| 3121 | //// } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3122 | ret: |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 3123 | G.run_list_level--; |
| 3124 | //// if (!G.run_list_level && G.interactive_fd) { |
| 3125 | //// signal(SIGTSTP, SIG_IGN); |
| 3126 | //// signal(SIGINT, SIG_IGN); |
| 3127 | //// } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 3128 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3129 | 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] | 3130 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 3131 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3132 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 3133 | free(for_list); |
| 3134 | #endif |
| 3135 | #if ENABLE_HUSH_CASE |
| 3136 | free(case_word); |
| 3137 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3138 | return rcode; |
| 3139 | } |
| 3140 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3141 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3142 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3143 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3144 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3145 | debug_printf_exec("run_and_free_list entered\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3146 | if (!G.fake_mode) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3147 | debug_printf_exec(": run_list with %d members\n", pi->num_cmds); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3148 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3149 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3150 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3151 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3152 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3153 | free_pipe_list(pi, /* indent: */ 0); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3154 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3155 | return rcode; |
| 3156 | } |
| 3157 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3158 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3159 | /* Peek ahead in the in_str to find out if we have a "&n" construct, |
| 3160 | * as in "2>&1", that represents duplicating a file descriptor. |
| 3161 | * Return either -2 (syntax error), -1 (no &), or the number found. |
| 3162 | */ |
| 3163 | static int redirect_dup_num(struct in_str *input) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3164 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3165 | int ch, d = 0, ok = 0; |
| 3166 | ch = i_peek(input); |
| 3167 | if (ch != '&') return -1; |
| 3168 | |
| 3169 | i_getch(input); /* get the & */ |
| 3170 | ch = i_peek(input); |
| 3171 | if (ch == '-') { |
| 3172 | i_getch(input); |
| 3173 | return -3; /* "-" represents "close me" */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3174 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3175 | while (isdigit(ch)) { |
| 3176 | d = d*10 + (ch-'0'); |
| 3177 | ok = 1; |
| 3178 | i_getch(input); |
| 3179 | ch = i_peek(input); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3180 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3181 | if (ok) return d; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3182 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3183 | bb_error_msg("ambiguous redirect"); |
| 3184 | return -2; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3187 | /* 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] | 3188 | * for file descriptor duplication, e.g., "2>&1". |
| 3189 | * Return code is 0 normally, 1 if a syntax error is detected in src. |
| 3190 | * Resource errors (in xmalloc) cause the process to exit */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3191 | 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] | 3192 | struct in_str *input) |
| 3193 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3194 | struct command *command = ctx->command; |
| 3195 | struct redir_struct *redir = command->redirects; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3196 | struct redir_struct *last_redir = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3197 | |
| 3198 | /* 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] | 3199 | while (redir) { |
| 3200 | last_redir = redir; |
| 3201 | redir = redir->next; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3202 | } |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3203 | redir = xzalloc(sizeof(struct redir_struct)); |
| 3204 | /* redir->next = NULL; */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3205 | /* redir->rd_filename = NULL; */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3206 | if (last_redir) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3207 | last_redir->next = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3208 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3209 | command->redirects = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3212 | redir->rd_type = style; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3213 | redir->fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3214 | |
| 3215 | debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip); |
| 3216 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3217 | /* Check for a '2>&1' type redirect */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3218 | redir->dup = redirect_dup_num(input); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3219 | if (redir->dup == -2) |
| 3220 | return 1; /* syntax error */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3221 | if (redir->dup != -1) { |
| 3222 | /* Erik had a check here that the file descriptor in question |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3223 | * is legit; I postpone that to "run time" |
| 3224 | * A "-" representation of "close me" shows up as a -3 here */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3225 | debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup); |
| 3226 | } else { |
| 3227 | /* We do _not_ try to open the file that src points to, |
| 3228 | * since we need to return and let src be expanded first. |
| 3229 | * Set ctx->pending_redirect, so we know what to do at the |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 3230 | * end of the next parsed word. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3231 | ctx->pending_redirect = redir; |
| 3232 | } |
| 3233 | return 0; |
| 3234 | } |
| 3235 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3236 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3237 | static struct pipe *new_pipe(void) |
| 3238 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3239 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3240 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3241 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3242 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3243 | return pi; |
| 3244 | } |
| 3245 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3246 | /* Command (member of a pipe) is complete. The only possible error here |
| 3247 | * is out of memory, in which case xmalloc exits. */ |
| 3248 | static int done_command(struct parse_context *ctx) |
| 3249 | { |
| 3250 | /* The command is really already in the pipe structure, so |
| 3251 | * advance the pipe counter and make a new, null command. */ |
| 3252 | struct pipe *pi = ctx->pipe; |
| 3253 | struct command *command = ctx->command; |
| 3254 | |
| 3255 | if (command) { |
| 3256 | if (command->group == NULL |
| 3257 | && command->argv == NULL |
| 3258 | && command->redirects == NULL |
| 3259 | ) { |
| 3260 | debug_printf_parse("done_command: skipping null cmd, num_cmds=%d\n", pi->num_cmds); |
| 3261 | return pi->num_cmds; |
| 3262 | } |
| 3263 | pi->num_cmds++; |
| 3264 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
| 3265 | } else { |
| 3266 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3267 | } |
| 3268 | |
| 3269 | /* Only real trickiness here is that the uncommitted |
| 3270 | * command structure is not counted in pi->num_cmds. */ |
| 3271 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
| 3272 | command = &pi->cmds[pi->num_cmds]; |
| 3273 | memset(command, 0, sizeof(*command)); |
| 3274 | |
| 3275 | ctx->command = command; |
| 3276 | /* but ctx->pipe and ctx->list_head remain unchanged */ |
| 3277 | |
| 3278 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3279 | } |
| 3280 | |
| 3281 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3282 | { |
| 3283 | int not_null; |
| 3284 | |
| 3285 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3286 | /* Close previous command */ |
| 3287 | not_null = done_command(ctx); |
| 3288 | ctx->pipe->followup = type; |
| 3289 | IF_HAS_KEYWORDS(ctx->pipe->pi_inverted = ctx->ctx_inverted;) |
| 3290 | IF_HAS_KEYWORDS(ctx->ctx_inverted = 0;) |
| 3291 | IF_HAS_KEYWORDS(ctx->pipe->res_word = ctx->ctx_res_w;) |
| 3292 | |
| 3293 | /* Without this check, even just <enter> on command line generates |
| 3294 | * tree of three NOPs (!). Which is harmless but annoying. |
| 3295 | * IOW: it is safe to do it unconditionally. |
| 3296 | * RES_NONE case is for "for a in; do ..." (empty IN set) |
| 3297 | * to work, possibly other cases too. */ |
| 3298 | if (not_null IF_HAS_KEYWORDS(|| ctx->ctx_res_w != RES_NONE)) { |
| 3299 | struct pipe *new_p; |
| 3300 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3301 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3302 | not_null, ctx->ctx_res_w); |
| 3303 | new_p = new_pipe(); |
| 3304 | ctx->pipe->next = new_p; |
| 3305 | ctx->pipe = new_p; |
| 3306 | ctx->command = NULL; /* needed! */ |
| 3307 | /* RES_THEN, RES_DO etc are "sticky" - |
| 3308 | * they remain set for commands inside if/while. |
| 3309 | * This is used to control execution. |
| 3310 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3311 | * cases where variable or value happens to match a keyword): |
| 3312 | */ |
| 3313 | #if ENABLE_HUSH_LOOPS |
| 3314 | if (ctx->ctx_res_w == RES_FOR |
| 3315 | || ctx->ctx_res_w == RES_IN) |
| 3316 | ctx->ctx_res_w = RES_NONE; |
| 3317 | #endif |
| 3318 | #if ENABLE_HUSH_CASE |
| 3319 | if (ctx->ctx_res_w == RES_MATCH) |
| 3320 | ctx->ctx_res_w = RES_CASEI; |
| 3321 | #endif |
| 3322 | /* Create the memory for command, roughly: |
| 3323 | * ctx->pipe->cmds = new struct command; |
| 3324 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3325 | */ |
| 3326 | done_command(ctx); |
| 3327 | } |
| 3328 | debug_printf_parse("done_pipe return\n"); |
| 3329 | } |
| 3330 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3331 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3332 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3333 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3334 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3335 | /* Create the memory for command, roughly: |
| 3336 | * ctx->pipe->cmds = new struct command; |
| 3337 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3338 | */ |
| 3339 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3342 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3343 | /* If a reserved word is found and processed, parse context is modified |
| 3344 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3345 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3346 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3347 | struct reserved_combo { |
| 3348 | char literal[6]; |
| 3349 | unsigned char res; |
| 3350 | unsigned char assignment_flag; |
| 3351 | int flag; |
| 3352 | }; |
| 3353 | enum { |
| 3354 | FLAG_END = (1 << RES_NONE ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3355 | #if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3356 | FLAG_IF = (1 << RES_IF ), |
| 3357 | FLAG_THEN = (1 << RES_THEN ), |
| 3358 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3359 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3360 | FLAG_FI = (1 << RES_FI ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3361 | #endif |
| 3362 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3363 | FLAG_FOR = (1 << RES_FOR ), |
| 3364 | FLAG_WHILE = (1 << RES_WHILE), |
| 3365 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3366 | FLAG_DO = (1 << RES_DO ), |
| 3367 | FLAG_DONE = (1 << RES_DONE ), |
| 3368 | FLAG_IN = (1 << RES_IN ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3369 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3370 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3371 | FLAG_MATCH = (1 << RES_MATCH), |
| 3372 | FLAG_ESAC = (1 << RES_ESAC ), |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3373 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3374 | FLAG_START = (1 << RES_XXXX ), |
| 3375 | }; |
| 3376 | |
| 3377 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3378 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3379 | /* Mostly a list of accepted follow-up reserved words. |
| 3380 | * FLAG_END means we are done with the sequence, and are ready |
| 3381 | * to turn the compound list into a command. |
| 3382 | * FLAG_START means the word must start a new compound list. |
| 3383 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3384 | static const struct reserved_combo reserved_list[] = { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3385 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3386 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3387 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 3388 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3389 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 3390 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 3391 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3392 | #endif |
| 3393 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3394 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3395 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 3396 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 3397 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3398 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 3399 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3400 | #endif |
| 3401 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3402 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3403 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3404 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3405 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3406 | const struct reserved_combo *r; |
| 3407 | |
| 3408 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 3409 | if (strcmp(word->data, r->literal) == 0) |
| 3410 | return r; |
| 3411 | } |
| 3412 | return NULL; |
| 3413 | } |
| 3414 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 3415 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3416 | #if ENABLE_HUSH_CASE |
| 3417 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3418 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3419 | }; |
| 3420 | #endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3421 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3422 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3423 | r = match_reserved_word(word); |
| 3424 | if (!r) |
| 3425 | return 0; |
| 3426 | |
| 3427 | 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] | 3428 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3429 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE) |
| 3430 | /* "case word IN ..." - IN part starts first match part */ |
| 3431 | r = &reserved_match; |
| 3432 | else |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3433 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3434 | if (r->flag == 0) { /* '!' */ |
| 3435 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3436 | syntax(NULL); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3437 | IF_HAS_KEYWORDS(ctx->ctx_res_w = RES_SNTX;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3438 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3439 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3440 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3441 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3442 | if (r->flag & FLAG_START) { |
| 3443 | struct parse_context *new; |
| 3444 | debug_printf("push stack\n"); |
| 3445 | new = xmalloc(sizeof(*new)); |
| 3446 | *new = *ctx; /* physical copy */ |
| 3447 | initialize_context(ctx); |
| 3448 | ctx->stack = new; |
| 3449 | } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) { |
| 3450 | syntax(NULL); |
| 3451 | ctx->ctx_res_w = RES_SNTX; |
| 3452 | return 1; |
| 3453 | } |
| 3454 | ctx->ctx_res_w = r->res; |
| 3455 | ctx->old_flag = r->flag; |
| 3456 | if (ctx->old_flag & FLAG_END) { |
| 3457 | struct parse_context *old; |
| 3458 | debug_printf("pop stack\n"); |
| 3459 | done_pipe(ctx, PIPE_SEQ); |
| 3460 | old = ctx->stack; |
| 3461 | old->command->group = ctx->list_head; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3462 | old->command->grp_type = GRP_NORMAL; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3463 | *ctx = *old; /* physical copy */ |
| 3464 | free(old); |
| 3465 | } |
| 3466 | word->o_assignment = r->assignment_flag; |
| 3467 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3468 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3469 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3470 | |
Denis Vlasenko | ddc8ae3 | 2008-10-14 12:50:34 +0000 | [diff] [blame] | 3471 | //TODO: many, many callers don't check error from done_word() |
| 3472 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3473 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3474 | * Normal return is 0. Syntax errors return 1. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3475 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3476 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3477 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3478 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3479 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3480 | if (word->length == 0 && word->nonnull == 0) { |
| 3481 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3482 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3483 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3484 | /* If this word wasn't an assignment, next ones definitely |
| 3485 | * can't be assignments. Even if they look like ones. */ |
| 3486 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3487 | && word->o_assignment != WORD_IS_KEYWORD |
| 3488 | ) { |
| 3489 | word->o_assignment = NOT_ASSIGNMENT; |
| 3490 | } else { |
| 3491 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3492 | command->assignment_cnt++; |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3493 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3494 | } |
| 3495 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3496 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3497 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3498 | * only if run as "bash", not "sh" */ |
| 3499 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3500 | word->o_assignment = NOT_ASSIGNMENT; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3501 | debug_printf("word stored in rd_filename: '%s'\n", word->data); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3502 | } else { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3503 | /* "{ echo foo; } echo bar" - bad */ |
| 3504 | /* NB: bash allows e.g. "if true; then { echo foo; } fi". TODO? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3505 | if (command->group) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3506 | syntax(NULL); |
| 3507 | debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n"); |
| 3508 | return 1; |
| 3509 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3510 | #if HAS_KEYWORDS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3511 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3512 | if (ctx->ctx_dsemicolon |
| 3513 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3514 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3515 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3516 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3517 | ctx->ctx_dsemicolon = 0; |
| 3518 | } else |
| 3519 | #endif |
| 3520 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3521 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3522 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3523 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3524 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3525 | #endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3526 | ) { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3527 | debug_printf_parse(": checking '%s' for reserved-ness\n", word->data); |
| 3528 | if (reserved_word(word, ctx)) { |
| 3529 | o_reset(word); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3530 | debug_printf_parse("done_word return %d\n", (ctx->ctx_res_w == RES_SNTX)); |
| 3531 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3532 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3533 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3534 | #endif |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3535 | 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] | 3536 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3537 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3538 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3539 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3540 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3541 | char *p = word->data; |
| 3542 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3543 | && (p[1] & 0x7f) == '@' |
| 3544 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3545 | ) { |
| 3546 | p += 3; |
| 3547 | } |
| 3548 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3549 | /* saw no "$@", or not only "$@" but some |
| 3550 | * real text is there too */ |
| 3551 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3552 | * e.g. "", $empty"" etc to not disappear */ |
| 3553 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3554 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3555 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3556 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3557 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3558 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3559 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3560 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3561 | o_reset(word); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3562 | ctx->pending_redirect = NULL; |
| 3563 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3564 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3565 | /* Force FOR to have just one word (variable name) */ |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3566 | /* NB: basically, this makes hush see "for v in ..." syntax as if |
| 3567 | * as it is "for v; in ...". FOR and IN become two pipe structs |
| 3568 | * in parse tree. */ |
| 3569 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3570 | //TODO: check that command->argv[0] is a valid variable name! |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3571 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3572 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3573 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3574 | #if ENABLE_HUSH_CASE |
| 3575 | /* Force CASE to have just one word */ |
| 3576 | if (ctx->ctx_res_w == RES_CASE) { |
| 3577 | done_pipe(ctx, PIPE_SEQ); |
| 3578 | } |
| 3579 | #endif |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3580 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3581 | return 0; |
| 3582 | } |
| 3583 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3584 | /* If a redirect is immediately preceded by a number, that number is |
| 3585 | * supposed to tell which file descriptor to redirect. This routine |
| 3586 | * looks for such preceding numbers. In an ideal world this routine |
| 3587 | * needs to handle all the following classes of redirects... |
| 3588 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3589 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3590 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3591 | * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo |
| 3592 | * A -1 output from this program means no valid number was found, so the |
| 3593 | * caller should use the appropriate default for this redirection. |
| 3594 | */ |
| 3595 | static int redirect_opt_num(o_string *o) |
| 3596 | { |
| 3597 | int num; |
| 3598 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3599 | if (o->length == 0) |
| 3600 | return -1; |
| 3601 | for (num = 0; num < o->length; num++) { |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3602 | if (!isdigit(o->data[num])) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3603 | return -1; |
| 3604 | } |
| 3605 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3606 | num = atoi(o->data); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3607 | o_reset(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3608 | return num; |
| 3609 | } |
| 3610 | |
Mike Frysinger | ddbee97 | 2009-03-22 22:48:41 +0000 | [diff] [blame] | 3611 | static int parse_stream(o_string *dest, struct parse_context *ctx, |
| 3612 | struct in_str *input0, const char *end_trigger); |
| 3613 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3614 | #if ENABLE_HUSH_TICK |
"Vladimir N. Oleynik" | 19c3701 | 2005-09-22 14:33:15 +0000 | [diff] [blame] | 3615 | static FILE *generate_stream_from_list(struct pipe *head) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3616 | { |
| 3617 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3618 | int pid, channel[2]; |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3619 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 3620 | xpipe(channel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3621 | /* *** NOMMU WARNING *** */ |
| 3622 | /* By using vfork here, we suspend parent till child exits or execs. |
| 3623 | * If child will not do it before it fills the pipe, it can block forever |
| 3624 | * in write(STDOUT_FILENO), and parent (shell) will be also stuck. |
Denis Vlasenko | 3fe4f98 | 2008-06-09 16:02:39 +0000 | [diff] [blame] | 3625 | * Try this script: |
| 3626 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >TESTFILE |
| 3627 | * huge=`cat TESTFILE` # will block here forever |
| 3628 | * echo OK |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3629 | */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 3630 | pid = BB_MMU ? fork() : vfork(); |
| 3631 | if (pid < 0) |
| 3632 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3633 | if (pid == 0) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 3634 | if (ENABLE_HUSH_JOB) |
| 3635 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 3636 | close(channel[0]); /* NB: close _first_, then move fd! */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3637 | xmove_fd(channel[1], 1); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3638 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3639 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3640 | G.run_list_level = 1; |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3641 | #endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3642 | /* Process substitution is not considered to be usual |
| 3643 | * 'command execution'. |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 3644 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 3645 | */ |
| 3646 | set_jobctrl_signals_to_IGN(); |
| 3647 | |
| 3648 | /* Note: freeing 'head' here would break NOMMU. */ |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3649 | _exit(run_list(head)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3650 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3651 | close(channel[1]); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3652 | pf = fdopen(channel[0], "r"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3653 | return pf; |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3654 | /* 'head' is freed by the caller */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3657 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3658 | static int process_command_subs(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3659 | struct in_str *input, |
| 3660 | const char *subst_end) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3661 | { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3662 | int retcode, ch, eol_cnt; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3663 | o_string result = NULL_O_STRING; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3664 | struct parse_context inner; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3665 | FILE *p; |
| 3666 | struct in_str pipe_str; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3667 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3668 | initialize_context(&inner); |
| 3669 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3670 | /* Recursion to generate command */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3671 | retcode = parse_stream(&result, &inner, input, subst_end); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3672 | if (retcode != 0) |
| 3673 | return retcode; /* syntax error or EOF */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3674 | done_word(&result, &inner); |
| 3675 | done_pipe(&inner, PIPE_SEQ); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3676 | o_free(&result); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3677 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3678 | p = generate_stream_from_list(inner.list_head); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3679 | if (p == NULL) |
| 3680 | return 1; |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 3681 | close_on_exec_on(fileno(p)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3682 | setup_file_in_str(&pipe_str, p); |
| 3683 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3684 | /* Now send results of command back into original context */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3685 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3686 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3687 | if (ch == '\n') { |
| 3688 | eol_cnt++; |
| 3689 | continue; |
| 3690 | } |
| 3691 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3692 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3693 | eol_cnt--; |
| 3694 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3695 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | debug_printf("done reading from pipe, pclose()ing\n"); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3699 | /* This is the step that waits for the child. Should be pretty |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3700 | * safe, since we just read an EOF from its stdout. We could try |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 3701 | * to do better, by using waitpid, and keeping track of background jobs |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3702 | * at the same time. That would be a lot of work, and contrary |
| 3703 | * to the KISS philosophy of this program. */ |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3704 | retcode = fclose(p); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3705 | free_pipe_list(inner.list_head, /* indent: */ 0); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3706 | debug_printf("closed FILE from child, retcode=%d\n", retcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3707 | return retcode; |
| 3708 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3709 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3710 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3711 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3712 | struct in_str *input, int ch) |
| 3713 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3714 | /* dest contains characters seen prior to ( or {. |
| 3715 | * Typically it's empty, but for functions defs, |
| 3716 | * it contains function name (without '()'). */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3717 | int rcode; |
| 3718 | const char *endch = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3719 | struct parse_context sub; |
| 3720 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3721 | |
| 3722 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3723 | #if ENABLE_HUSH_FUNCTIONS |
| 3724 | if (ch == 'F') { /* function definition? */ |
| 3725 | bb_error_msg("aha '%s' is a function, parsing it...", dest->data); |
| 3726 | //command->fname = dest->data; |
| 3727 | command->grp_type = GRP_FUNCTION; |
| 3728 | //TODO: review every o_reset() location... do they handle all o_string fields correctly? |
| 3729 | memset(dest, 0, sizeof(*dest)); |
| 3730 | } |
| 3731 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3732 | if (command->argv /* word [word](... */ |
| 3733 | || dest->length /* word(... */ |
| 3734 | || dest->nonnull /* ""(... */ |
| 3735 | ) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3736 | syntax(NULL); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3737 | debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n"); |
| 3738 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3739 | } |
| 3740 | initialize_context(&sub); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3741 | endch = "}"; |
| 3742 | if (ch == '(') { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3743 | endch = ")"; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3744 | command->grp_type = GRP_SUBSHELL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3745 | } |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3746 | rcode = parse_stream(dest, &sub, input, endch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3747 | if (rcode == 0) { |
| 3748 | done_word(dest, &sub); /* finish off the final word in the subcontext */ |
| 3749 | done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3750 | command->group = sub.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3751 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3752 | debug_printf_parse("parse_group return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3753 | return rcode; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3754 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3755 | } |
| 3756 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3757 | #if ENABLE_HUSH_TICK |
| 3758 | /* Subroutines for copying $(...) and `...` things */ |
| 3759 | static void add_till_backquote(o_string *dest, struct in_str *input); |
| 3760 | /* '...' */ |
| 3761 | static void add_till_single_quote(o_string *dest, struct in_str *input) |
| 3762 | { |
| 3763 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3764 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3765 | if (ch == EOF) |
| 3766 | break; |
| 3767 | if (ch == '\'') |
| 3768 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3769 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3770 | } |
| 3771 | } |
| 3772 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
| 3773 | static void add_till_double_quote(o_string *dest, struct in_str *input) |
| 3774 | { |
| 3775 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3776 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3777 | if (ch == '"') |
| 3778 | break; |
| 3779 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3780 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3781 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3782 | } |
| 3783 | if (ch == EOF) |
| 3784 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3785 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3786 | if (ch == '`') { |
| 3787 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3788 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3789 | continue; |
| 3790 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3791 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3792 | } |
| 3793 | } |
| 3794 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 3795 | * \` quoting. |
| 3796 | * "Within the backquoted style of command substitution, backslash |
| 3797 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 3798 | * The search for the matching backquote shall be satisfied by the first |
| 3799 | * backquote found without a preceding backslash; during this search, |
| 3800 | * if a non-escaped backquote is encountered within a shell comment, |
| 3801 | * a here-document, an embedded command substitution of the $(command) |
| 3802 | * form, or a quoted string, undefined results occur. A single-quoted |
| 3803 | * or double-quoted string that begins, but does not end, within the |
| 3804 | * "`...`" sequence produces undefined results." |
| 3805 | * Example Output |
| 3806 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 3807 | */ |
| 3808 | static void add_till_backquote(o_string *dest, struct in_str *input) |
| 3809 | { |
| 3810 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3811 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3812 | if (ch == '`') |
| 3813 | break; |
| 3814 | if (ch == '\\') { /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3815 | int ch2 = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3816 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3817 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3818 | ch = ch2; |
| 3819 | } |
| 3820 | if (ch == EOF) |
| 3821 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3822 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3823 | } |
| 3824 | } |
| 3825 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 3826 | * quoting and nested ()s. |
| 3827 | * "With the $(command) style of command substitution, all characters |
| 3828 | * following the open parenthesis to the matching closing parenthesis |
| 3829 | * constitute the command. Any valid shell script can be used for command, |
| 3830 | * except a script consisting solely of redirections which produces |
| 3831 | * unspecified results." |
| 3832 | * Example Output |
| 3833 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 3834 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 3835 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 3836 | */ |
| 3837 | static void add_till_closing_curly_brace(o_string *dest, struct in_str *input) |
| 3838 | { |
| 3839 | int count = 0; |
| 3840 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3841 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3842 | if (ch == EOF) |
| 3843 | break; |
| 3844 | if (ch == '(') |
| 3845 | count++; |
| 3846 | if (ch == ')') |
| 3847 | if (--count < 0) |
| 3848 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3849 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3850 | if (ch == '\'') { |
| 3851 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3852 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3853 | continue; |
| 3854 | } |
| 3855 | if (ch == '"') { |
| 3856 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3857 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3858 | continue; |
| 3859 | } |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3860 | if (ch == '\\') { /* \x. Copy verbatim. Important for \(, \) */ |
| 3861 | ch = i_getch(input); |
| 3862 | if (ch == EOF) |
| 3863 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3864 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3865 | continue; |
| 3866 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3867 | } |
| 3868 | } |
| 3869 | #endif /* ENABLE_HUSH_TICK */ |
| 3870 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3871 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3872 | static int handle_dollar(o_string *dest, struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3873 | { |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3874 | int expansion; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3875 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3876 | unsigned char quote_mask = dest->o_quote ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3877 | |
| 3878 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3879 | if (isalpha(ch)) { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3880 | i_getch(input); |
| 3881 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3882 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3883 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3884 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3885 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3886 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3887 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3888 | if (!isalnum(ch) && ch != '_') |
| 3889 | break; |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3890 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3891 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3892 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3893 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3894 | make_one_char_var: |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3895 | i_getch(input); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3896 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3897 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3898 | o_addchr(dest, ch | quote_mask); |
| 3899 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3900 | } else switch (ch) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3901 | case '$': /* pid */ |
| 3902 | case '!': /* last bg pid */ |
| 3903 | case '?': /* last exit code */ |
| 3904 | case '#': /* number of args */ |
| 3905 | case '*': /* args */ |
| 3906 | case '@': /* args */ |
| 3907 | goto make_one_char_var; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3908 | case '{': { |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 3909 | bool first_char, all_digits; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3910 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3911 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3912 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3913 | /* XXX maybe someone will try to escape the '}' */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3914 | expansion = 0; |
| 3915 | first_char = true; |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 3916 | all_digits = false; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3917 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3918 | ch = i_getch(input); |
Denis Vlasenko | b055001 | 2007-05-24 12:18:16 +0000 | [diff] [blame] | 3919 | if (ch == '}') |
| 3920 | break; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3921 | |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 3922 | if (first_char) { |
| 3923 | if (ch == '#') |
| 3924 | /* ${#var}: length of var contents */ |
| 3925 | goto char_ok; |
| 3926 | else if (isdigit(ch)) { |
| 3927 | all_digits = true; |
| 3928 | goto char_ok; |
| 3929 | } |
| 3930 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3931 | |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 3932 | if (expansion < 2 && |
| 3933 | ((all_digits && !isdigit(ch)) || |
| 3934 | (!all_digits && !isalnum(ch) && ch != '_'))) |
| 3935 | { |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3936 | /* handle parameter expansions |
| 3937 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 3938 | */ |
| 3939 | if (first_char) |
| 3940 | goto case_default; |
| 3941 | switch (ch) { |
| 3942 | case ':': /* null modifier */ |
| 3943 | if (expansion == 0) { |
| 3944 | debug_printf_parse(": null modifier\n"); |
| 3945 | ++expansion; |
| 3946 | break; |
| 3947 | } |
| 3948 | goto case_default; |
| 3949 | |
| 3950 | #if 0 /* not implemented yet :( */ |
| 3951 | case '#': /* remove prefix */ |
| 3952 | case '%': /* remove suffix */ |
| 3953 | if (expansion == 0) { |
| 3954 | debug_printf_parse(": remove suffix/prefix\n"); |
| 3955 | expansion = 2; |
| 3956 | break; |
| 3957 | } |
| 3958 | goto case_default; |
| 3959 | #endif |
| 3960 | |
| 3961 | case '-': /* default value */ |
| 3962 | case '=': /* assign default */ |
| 3963 | case '+': /* alternative */ |
| 3964 | case '?': /* error indicate */ |
| 3965 | debug_printf_parse(": parameter expansion\n"); |
| 3966 | expansion = 2; |
| 3967 | break; |
| 3968 | |
| 3969 | default: |
| 3970 | case_default: |
| 3971 | syntax("unterminated ${name}"); |
| 3972 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 3973 | return 1; |
| 3974 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3975 | } |
Mike Frysinger | 7c3e52c | 2009-03-28 21:06:22 +0000 | [diff] [blame] | 3976 | |
| 3977 | char_ok: |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3978 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3979 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3980 | quote_mask = 0; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3981 | first_char = false; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3982 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3983 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3984 | break; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 3985 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3986 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3987 | case '(': { |
| 3988 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3989 | i_getch(input); |
| 3990 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3991 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3992 | add_till_closing_curly_brace(dest, input); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3993 | //debug_printf_subst("SUBST RES2 '%s'\n", dest->data + pos); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3994 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3995 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3996 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3997 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3998 | case '_': |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3999 | i_getch(input); |
| 4000 | ch = i_peek(input); |
| 4001 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 4002 | ch = '_'; |
| 4003 | goto make_var; |
| 4004 | } |
| 4005 | /* else: it's $_ */ |
| 4006 | case '-': |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4007 | /* still unhandled, but should be eventually */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4008 | bb_error_msg("unhandled syntax: $%c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4009 | return 1; |
| 4010 | break; |
| 4011 | default: |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 4012 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4013 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4014 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4015 | return 0; |
| 4016 | } |
| 4017 | |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4018 | /* Scan input, call done_word() whenever full IFS delimited word was seen. |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4019 | * Call done_pipe if '\n' was seen (and end_trigger != NULL). |
| 4020 | * Return code is 0 if end_trigger char is met, |
| 4021 | * -1 on EOF (but if end_trigger == NULL then return 0), |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4022 | * 1 for syntax error */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4023 | static int parse_stream(o_string *dest, struct parse_context *ctx, |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 4024 | struct in_str *input, const char *end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4025 | { |
"Vladimir N. Oleynik" | 4ccd2b4 | 2006-01-31 09:27:48 +0000 | [diff] [blame] | 4026 | int ch, m; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4027 | int redir_fd; |
| 4028 | redir_type redir_style; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4029 | int shadow_quote = dest->o_quote; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4030 | int next; |
| 4031 | |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 4032 | /* 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] | 4033 | * 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] | 4034 | * found. When recursing, quote state is passed in via dest->o_quote. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4035 | |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4036 | 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] | 4037 | |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4038 | while (1) { |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4039 | m = CHAR_IFS; |
| 4040 | next = '\0'; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4041 | ch = i_getch(input); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4042 | if (ch != EOF) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4043 | m = G.charmap[ch]; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4044 | if (ch != '\n') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4045 | next = i_peek(input); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4046 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4047 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4048 | debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n", |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 4049 | ch, ch, m, dest->o_quote); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4050 | if (m == CHAR_ORDINARY |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4051 | || (m != CHAR_SPECIAL && shadow_quote) |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4052 | ) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4053 | if (ch == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4054 | syntax("unterminated \""); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4055 | debug_printf_parse("parse_stream return 1: unterminated \"\n"); |
| 4056 | return 1; |
| 4057 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 4058 | o_addQchr(dest, ch); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4059 | if ((dest->o_assignment == MAYBE_ASSIGNMENT |
| 4060 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4061 | && ch == '=' |
| 4062 | && is_assignment(dest->data) |
| 4063 | ) { |
| 4064 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 4065 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4066 | continue; |
| 4067 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4068 | if (m == CHAR_IFS) { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4069 | if (done_word(dest, ctx)) { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4070 | debug_printf_parse("parse_stream return 1: done_word!=0\n"); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4071 | return 1; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 4072 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4073 | if (ch == EOF) |
| 4074 | break; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4075 | /* If we aren't performing a substitution, treat |
| 4076 | * a newline as a command separator. |
| 4077 | * [why we don't handle it exactly like ';'? --vda] */ |
| 4078 | if (end_trigger && ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4079 | #if ENABLE_HUSH_CASE |
| 4080 | /* "case ... in <newline> word) ..." - |
| 4081 | * newlines are ignored (but ';' wouldn't be) */ |
| 4082 | if (dest->length == 0 // && argv[0] == NULL |
| 4083 | && ctx->ctx_res_w == RES_MATCH |
| 4084 | ) { |
| 4085 | continue; |
| 4086 | } |
| 4087 | #endif |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4088 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4089 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4090 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4091 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4092 | if (end_trigger) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4093 | if (!shadow_quote && strchr(end_trigger, ch)) { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4094 | /* Special case: (...word) makes last word terminate, |
| 4095 | * as if ';' is seen */ |
| 4096 | if (ch == ')') { |
| 4097 | done_word(dest, ctx); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4098 | //err chk? |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4099 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4100 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4101 | } |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4102 | if (!HAS_KEYWORDS |
| 4103 | IF_HAS_KEYWORDS(|| (ctx->ctx_res_w == RES_NONE && ctx->old_flag == 0)) |
| 4104 | ) { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4105 | debug_printf_parse("parse_stream return 0: end_trigger char found\n"); |
| 4106 | return 0; |
| 4107 | } |
| 4108 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4109 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4110 | if (m == CHAR_IFS) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4111 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4112 | |
| 4113 | if (dest->o_assignment == MAYBE_ASSIGNMENT) { |
| 4114 | /* ch is a special char and thus this word |
| 4115 | * cannot be an assignment: */ |
| 4116 | dest->o_assignment = NOT_ASSIGNMENT; |
| 4117 | } |
| 4118 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4119 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4120 | case '#': |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4121 | if (dest->length == 0 && !shadow_quote) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4122 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4123 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4124 | if (ch == EOF || ch == '\n') |
| 4125 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4126 | i_getch(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4127 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4128 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 4129 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4130 | } |
| 4131 | break; |
| 4132 | case '\\': |
| 4133 | if (next == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4134 | syntax("\\<eof>"); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4135 | debug_printf_parse("parse_stream return 1: \\<eof>\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4136 | return 1; |
| 4137 | } |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4138 | /* bash: |
| 4139 | * "The backslash retains its special meaning [in "..."] |
| 4140 | * only when followed by one of the following characters: |
| 4141 | * $, `, ", \, or <newline>. A double quote may be quoted |
| 4142 | * within double quotes by preceding it with a backslash. |
| 4143 | * If enabled, history expansion will be performed unless |
| 4144 | * an ! appearing in double quotes is escaped using |
| 4145 | * a backslash. The backslash preceding the ! is not removed." |
| 4146 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4147 | if (shadow_quote) { //NOT SURE dest->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4148 | if (strchr("$`\"\\", next) != NULL) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 4149 | o_addqchr(dest, i_getch(input)); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4150 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 4151 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 4152 | } |
| 4153 | } else { |
| 4154 | o_addchr(dest, '\\'); |
| 4155 | o_addchr(dest, i_getch(input)); |
| 4156 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4157 | break; |
| 4158 | case '$': |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4159 | if (handle_dollar(dest, input) != 0) { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4160 | debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n"); |
| 4161 | return 1; |
| 4162 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4163 | break; |
| 4164 | case '\'': |
| 4165 | dest->nonnull = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4166 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4167 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4168 | if (ch == EOF) { |
| 4169 | syntax("unterminated '"); |
| 4170 | debug_printf_parse("parse_stream return 1: unterminated '\n"); |
| 4171 | return 1; |
| 4172 | } |
| 4173 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4174 | break; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4175 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 4176 | o_addqchr(dest, ch); |
| 4177 | else |
| 4178 | o_addchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4179 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4180 | break; |
| 4181 | case '"': |
| 4182 | dest->nonnull = 1; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4183 | shadow_quote ^= 1; /* invert */ |
| 4184 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 4185 | dest->o_quote ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4186 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4187 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4188 | case '`': { |
| 4189 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4190 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4191 | o_addchr(dest, shadow_quote /*or dest->o_quote??*/ ? 0x80 | '`' : '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4192 | add_till_backquote(dest, input); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4193 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 4194 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4195 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4196 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4197 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4198 | case '>': |
| 4199 | redir_fd = redirect_opt_num(dest); |
| 4200 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4201 | redir_style = REDIRECT_OVERWRITE; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4202 | if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4203 | redir_style = REDIRECT_APPEND; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4204 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4205 | } |
| 4206 | #if 0 |
| 4207 | else if (next == '(') { |
| 4208 | syntax(">(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4209 | debug_printf_parse("parse_stream return 1: >(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4210 | return 1; |
| 4211 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4212 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4213 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 4214 | break; |
| 4215 | case '<': |
| 4216 | redir_fd = redirect_opt_num(dest); |
| 4217 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4218 | redir_style = REDIRECT_INPUT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4219 | if (next == '<') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4220 | redir_style = REDIRECT_HEREIS; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4221 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4222 | } else if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4223 | redir_style = REDIRECT_IO; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4224 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4225 | } |
| 4226 | #if 0 |
| 4227 | else if (next == '(') { |
| 4228 | syntax("<(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4229 | debug_printf_parse("parse_stream return 1: <(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4230 | return 1; |
| 4231 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4232 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4233 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 4234 | break; |
| 4235 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4236 | #if ENABLE_HUSH_CASE |
| 4237 | case_semi: |
| 4238 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4239 | done_word(dest, ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4240 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4241 | #if ENABLE_HUSH_CASE |
| 4242 | /* Eat multiple semicolons, detect |
| 4243 | * whether it means something special */ |
| 4244 | while (1) { |
| 4245 | ch = i_peek(input); |
| 4246 | if (ch != ';') |
| 4247 | break; |
| 4248 | i_getch(input); |
| 4249 | if (ctx->ctx_res_w == RES_CASEI) { |
| 4250 | ctx->ctx_dsemicolon = 1; |
| 4251 | ctx->ctx_res_w = RES_MATCH; |
| 4252 | break; |
| 4253 | } |
| 4254 | } |
| 4255 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4256 | new_cmd: |
| 4257 | /* We just finished a cmd. New one may start |
| 4258 | * with an assignment */ |
| 4259 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4260 | break; |
| 4261 | case '&': |
| 4262 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4263 | if (next == '&') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4264 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4265 | done_pipe(ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4266 | } else { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4267 | done_pipe(ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4268 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4269 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4270 | case '|': |
| 4271 | done_word(dest, ctx); |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4272 | #if ENABLE_HUSH_CASE |
| 4273 | if (ctx->ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4274 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4275 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4276 | if (next == '|') { /* || */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4277 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4278 | done_pipe(ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4279 | } else { |
| 4280 | /* we could pick up a file descriptor choice here |
| 4281 | * with redirect_opt_num(), but bash doesn't do it. |
| 4282 | * "echo foo 2| cat" yields "foo 2". */ |
| 4283 | done_command(ctx); |
| 4284 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4285 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4286 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4287 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4288 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4289 | if (ctx->ctx_res_w == RES_MATCH |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4290 | && ctx->command->argv == NULL /* not (word|(... */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4291 | && dest->length == 0 /* not word(... */ |
| 4292 | && dest->nonnull == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4293 | ) { |
| 4294 | continue; |
| 4295 | } |
| 4296 | #endif |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4297 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4298 | if (dest->length != 0 /* not just () but word() */ |
| 4299 | && dest->nonnull == 0 /* not a"b"c() */ |
| 4300 | && ctx->command->argv == NULL /* it's the first word */ |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4301 | //TODO: "func ( ) {...}" - note spaces - is valid format too in bash |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4302 | && i_peek(input) == ')' |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4303 | && !match_reserved_word(dest) |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4304 | ) { |
| 4305 | bb_error_msg("seems like a function definition"); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4306 | i_getch(input); |
| 4307 | do { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4308 | //TODO: do it properly. |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4309 | ch = i_getch(input); |
| 4310 | } while (ch == ' ' || ch == '\n'); |
| 4311 | if (ch != '{') { |
| 4312 | syntax("was expecting {"); |
| 4313 | debug_printf_parse("parse_stream return 1\n"); |
| 4314 | return 1; |
| 4315 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4316 | ch = 'F'; /* magic value */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4317 | } |
| 4318 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4319 | case '{': |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4320 | if (parse_group(dest, ctx, input, ch) != 0) { |
| 4321 | 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] | 4322 | return 1; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4323 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4324 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4325 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4326 | #if ENABLE_HUSH_CASE |
| 4327 | if (ctx->ctx_res_w == RES_MATCH) |
| 4328 | goto case_semi; |
| 4329 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4330 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4331 | /* proper use of this character is caught by end_trigger: |
| 4332 | * if we see {, we call parse_group(..., end_trigger='}') |
| 4333 | * and it will match } earlier (not here). */ |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4334 | syntax("unexpected } or )"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4335 | debug_printf_parse("parse_stream return 1: unexpected '}'\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4336 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4337 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4338 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4339 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4340 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4341 | } /* while (1) */ |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4342 | debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL)); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4343 | if (end_trigger) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4344 | return -1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4345 | return 0; |
| 4346 | } |
| 4347 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4348 | static void set_in_charmap(const char *set, int code) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4349 | { |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 4350 | while (*set) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4351 | G.charmap[(unsigned char)*set++] = code; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4352 | } |
| 4353 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4354 | static void update_charmap(void) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4355 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4356 | G.ifs = getenv("IFS"); |
| 4357 | if (G.ifs == NULL) |
| 4358 | G.ifs = " \t\n"; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4359 | /* Precompute a list of 'flow through' behavior so it can be treated |
| 4360 | * quickly up front. Computation is necessary because of IFS. |
| 4361 | * Special case handling of IFS == " \t\n" is not implemented. |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4362 | * The charmap[] array only really needs two bits each, |
| 4363 | * and on most machines that would be faster (reduced L1 cache use). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4364 | */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4365 | memset(G.charmap, CHAR_ORDINARY, sizeof(G.charmap)); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4366 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4367 | set_in_charmap("\\$\"`", CHAR_SPECIAL); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4368 | #else |
| 4369 | set_in_charmap("\\$\"", CHAR_SPECIAL); |
| 4370 | #endif |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4371 | set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4372 | set_in_charmap(G.ifs, CHAR_IFS); /* are ordinary if quoted */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4375 | /* Most recursion does not come through here, the exception is |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4376 | * from builtin_source() and builtin_eval() */ |
| 4377 | 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] | 4378 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4379 | struct parse_context ctx; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4380 | o_string temp = NULL_O_STRING; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4381 | int rcode; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4382 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4383 | do { |
| 4384 | initialize_context(&ctx); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4385 | update_charmap(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4386 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 4387 | inp->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4388 | #endif |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4389 | /* We will stop & execute after each ';' or '\n'. |
| 4390 | * Example: "sleep 9999; echo TEST" + ctrl-C: |
| 4391 | * TEST should be printed */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4392 | temp.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4393 | rcode = parse_stream(&temp, &ctx, inp, ";\n"); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4394 | #if HAS_KEYWORDS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4395 | if (rcode != 1 && ctx.old_flag != 0) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4396 | syntax(NULL); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4397 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4398 | #endif |
| 4399 | if (rcode != 1 IF_HAS_KEYWORDS(&& ctx.old_flag == 0)) { |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4400 | done_word(&temp, &ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4401 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4402 | debug_print_tree(ctx.list_head, 0); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4403 | debug_printf_exec("parse_stream_outer: run_and_free_list\n"); |
| 4404 | run_and_free_list(ctx.list_head); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4405 | } else { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4406 | /* We arrive here also if rcode == 1 (error in parse_stream) */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4407 | #if HAS_KEYWORDS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4408 | if (ctx.old_flag != 0) { |
| 4409 | free(ctx.stack); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4410 | o_reset(&temp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4411 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4412 | #endif |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 4413 | /*temp.nonnull = 0; - o_free does it below */ |
| 4414 | /*temp.o_quote = 0; - o_free does it below */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4415 | free_pipe_list(ctx.list_head, /* indent: */ 0); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4416 | /* Discard all unprocessed line input, force prompt on */ |
| 4417 | inp->p = NULL; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4418 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4419 | inp->promptme = 1; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4420 | #endif |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4421 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4422 | o_free(&temp); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4423 | /* loop on syntax errors, return on EOF: */ |
| 4424 | } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4425 | return 0; |
| 4426 | } |
| 4427 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4428 | static int parse_and_run_string(const char *s, int parse_flag) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4429 | { |
| 4430 | struct in_str input; |
| 4431 | setup_string_in_str(&input, s); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4432 | return parse_and_run_stream(&input, parse_flag); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4433 | } |
| 4434 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4435 | static int parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4436 | { |
| 4437 | int rcode; |
| 4438 | struct in_str input; |
| 4439 | setup_file_in_str(&input, f); |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4440 | rcode = parse_and_run_stream(&input, 0 /* parse_flag */); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4441 | return rcode; |
| 4442 | } |
| 4443 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4444 | #if ENABLE_HUSH_JOB |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4445 | /* Make sure we have a controlling tty. If we get started under a job |
| 4446 | * aware app (like bash for example), make sure we are now in charge so |
| 4447 | * we don't fight over who gets the foreground */ |
Eric Andersen | eaecbf3 | 2001-10-31 10:41:31 +0000 | [diff] [blame] | 4448 | static void setup_job_control(void) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4449 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4450 | pid_t shell_pgrp; |
| 4451 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4452 | shell_pgrp = getpgrp(); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4453 | |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 4454 | /* If we were ran as 'hush &', |
| 4455 | * sleep until we are in the foreground. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4456 | while (tcgetpgrp(G.interactive_fd) != shell_pgrp) { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4457 | /* Send TTIN to ourself (should stop us) */ |
Denis Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 4458 | kill(- shell_pgrp, SIGTTIN); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 4459 | shell_pgrp = getpgrp(); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4460 | } |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4461 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4462 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 4463 | set_fatal_signals_to_sigexit(); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4464 | |
| 4465 | /* Put ourselves in our own process group. */ |
Bernhard Reutner-Fischer | 864329d | 2008-09-25 10:55:05 +0000 | [diff] [blame] | 4466 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4467 | /* Grab control of the terminal. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4468 | tcsetpgrp(G.interactive_fd, getpid()); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4469 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4470 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4471 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4472 | static int set_mode(const char cstate, const char mode) |
| 4473 | { |
| 4474 | int state = (cstate == '-' ? 1 : 0); |
| 4475 | switch (mode) { |
| 4476 | case 'n': G.fake_mode = state; break; |
| 4477 | case 'x': /*G.debug_mode = state;*/ break; |
| 4478 | default: return EXIT_FAILURE; |
| 4479 | } |
| 4480 | return EXIT_SUCCESS; |
| 4481 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4482 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 4483 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 4484 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4485 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4486 | static const struct variable const_shell_ver = { |
| 4487 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4488 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4489 | .max_len = 1, /* 0 can provoke free(name) */ |
| 4490 | .flg_export = 1, |
| 4491 | .flg_read_only = 1, |
| 4492 | }; |
| 4493 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4494 | int opt; |
| 4495 | FILE *input; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4496 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4497 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 4498 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 4499 | INIT_G(); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4500 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4501 | G.root_pid = getpid(); |
Denis Vlasenko | 16c2fea | 2008-06-17 12:28:44 +0000 | [diff] [blame] | 4502 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4503 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4504 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 4505 | G.top_var = &G.shell_ver; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4506 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4507 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 4508 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4509 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4510 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4511 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4512 | if (e) while (*e) { |
| 4513 | char *value = strchr(*e, '='); |
| 4514 | if (value) { /* paranoia */ |
| 4515 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 4516 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 4517 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4518 | cur_var->max_len = strlen(*e); |
| 4519 | cur_var->flg_export = 1; |
| 4520 | } |
| 4521 | e++; |
| 4522 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4523 | debug_printf_env("putenv '%s'\n", hush_version_str); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4524 | putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 4525 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4526 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4527 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 4528 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4529 | /* XXX what should these be while sourcing /etc/profile? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4530 | G.global_argc = argc; |
| 4531 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 4532 | /* Initialize some more globals to non-zero values */ |
| 4533 | set_cwd(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4534 | #if ENABLE_HUSH_INTERACTIVE |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 4535 | if (ENABLE_FEATURE_EDITING) |
| 4536 | cmdedit_set_initial_prompt(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4537 | G.PS2 = "> "; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4538 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 4539 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4540 | if (EXIT_SUCCESS) /* otherwise is already done */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4541 | G.last_return_code = EXIT_SUCCESS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4542 | |
| 4543 | if (argv[0] && argv[0][0] == '-') { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4544 | debug_printf("sourcing /etc/profile\n"); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4545 | input = fopen_for_read("/etc/profile"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4546 | if (input != NULL) { |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 4547 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4548 | parse_and_run_file(input); |
Eric Andersen | a90f20b | 2001-06-26 23:00:21 +0000 | [diff] [blame] | 4549 | fclose(input); |
| 4550 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4551 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4552 | input = stdin; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4553 | |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 4554 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
| 4555 | while ((opt = getopt(argc, argv, "c:xins")) > 0) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4556 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4557 | case 'c': |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4558 | G.global_argv = argv + optind; |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 4559 | if (!argv[optind]) { |
| 4560 | /* -c 'script' (no params): prevent empty $0 */ |
| 4561 | *--G.global_argv = argv[0]; |
| 4562 | optind--; |
| 4563 | } /* else -c 'script' PAR0 PAR1: $0 is PAR0 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4564 | G.global_argc = argc - optind; |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4565 | opt = parse_and_run_string(optarg, 0 /* parse_flag */); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4566 | goto final_return; |
| 4567 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4568 | /* Well, we cannot just declare interactiveness, |
| 4569 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4570 | /* G.interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4571 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 4572 | case 's': |
| 4573 | /* "-s" means "read from stdin", but this is how we always |
| 4574 | * operate, so simply do nothing here. */ |
| 4575 | break; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 4576 | case 'n': |
| 4577 | case 'x': |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4578 | if (!set_mode('-', opt)) |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 4579 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4580 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4581 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4582 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 4583 | " or: sh -c command [args]...\n\n"); |
| 4584 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4585 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4586 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4587 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4588 | } |
| 4589 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4590 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4591 | /* 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] | 4592 | * the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 4593 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4594 | * no arguments remaining or the -s flag given |
| 4595 | * standard input is a terminal |
| 4596 | * standard output is a terminal |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4597 | * Refer to Posix.2, the description of the 'sh' utility. */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4598 | if (argv[optind] == NULL && input == stdin |
| 4599 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 4600 | ) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4601 | G.saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 4602 | debug_printf("saved_tty_pgrp=%d\n", G.saved_tty_pgrp); |
| 4603 | if (G.saved_tty_pgrp >= 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4604 | /* try to dup to high fd#, >= 255 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4605 | G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 4606 | if (G.interactive_fd < 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4607 | /* try to dup to any fd */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4608 | G.interactive_fd = dup(STDIN_FILENO); |
| 4609 | if (G.interactive_fd < 0) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4610 | /* give up */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4611 | G.interactive_fd = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4612 | } |
Denis Vlasenko | 2f1bb36 | 2007-04-21 10:01:14 +0000 | [diff] [blame] | 4613 | // TODO: track & disallow any attempts of user |
| 4614 | // to (inadvertently) close/redirect it |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4615 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4616 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4617 | init_signal_mask(); /* note: ensures SIGCHLD is not masked */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4618 | debug_printf("G.interactive_fd=%d\n", G.interactive_fd); |
| 4619 | if (G.interactive_fd) { |
| 4620 | fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4621 | /* Looks like they want an interactive shell */ |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4622 | setup_job_control(); |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 4623 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 4624 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
| 4625 | die_sleep = -1; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4626 | if (setjmp(die_jmp)) { |
| 4627 | /* xfunc has failed! die die die */ |
| 4628 | hush_exit(xfunc_error_retval); |
| 4629 | } |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4630 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4631 | #elif ENABLE_HUSH_INTERACTIVE |
| 4632 | /* no job control compiled, only prompt/line editing */ |
| 4633 | if (argv[optind] == NULL && input == stdin |
| 4634 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 4635 | ) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4636 | G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 4637 | if (G.interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4638 | /* try to dup to any fd */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4639 | G.interactive_fd = dup(STDIN_FILENO); |
| 4640 | if (G.interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4641 | /* give up */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4642 | G.interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4643 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4644 | if (G.interactive_fd) { |
| 4645 | fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 4646 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4647 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4648 | init_signal_mask(); /* note: ensures SIGCHLD is not masked */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4649 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4650 | /* POSIX allows shell to re-enable SIGCHLD |
| 4651 | * even if it was SIG_IGN on entry */ |
| 4652 | // G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 4653 | signal(SIGCHLD, SIG_DFL); // SIGCHLD_handler); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4654 | |
Denis Vlasenko | 701ac18 | 2009-03-28 19:22:08 +0000 | [diff] [blame] | 4655 | #if ENABLE_HUSH_INTERACTIVE && !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 4656 | if (G.interactive_fd) { |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 4657 | printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", bb_banner); |
| 4658 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
| 4659 | } |
Denis Vlasenko | 701ac18 | 2009-03-28 19:22:08 +0000 | [diff] [blame] | 4660 | #endif |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 4661 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4662 | if (argv[optind] == NULL) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4663 | opt = parse_and_run_file(stdin); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4664 | } else { |
| 4665 | debug_printf("\nrunning script '%s'\n", argv[optind]); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4666 | G.global_argv = argv + optind; |
| 4667 | G.global_argc = argc - optind; |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4668 | input = xfopen_for_read(argv[optind]); |
Denis Vlasenko | 459a5ad | 2008-02-11 08:35:03 +0000 | [diff] [blame] | 4669 | fcntl(fileno(input), F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4670 | opt = parse_and_run_file(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4671 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4672 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4673 | final_return: |
| 4674 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4675 | #if ENABLE_FEATURE_CLEAN_UP |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4676 | fclose(input); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4677 | if (G.cwd != bb_msg_unknown) |
| 4678 | free((char*)G.cwd); |
| 4679 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4680 | while (cur_var) { |
| 4681 | struct variable *tmp = cur_var; |
| 4682 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 4683 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4684 | cur_var = cur_var->next; |
| 4685 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4686 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4687 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4688 | hush_exit(opt ? opt : G.last_return_code); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4689 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 4690 | |
| 4691 | |
| 4692 | #if ENABLE_LASH |
| 4693 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 4694 | int lash_main(int argc, char **argv) |
| 4695 | { |
| 4696 | //bb_error_msg("lash is deprecated, please use hush instead"); |
| 4697 | return hush_main(argc, argv); |
| 4698 | } |
| 4699 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4700 | |
| 4701 | |
| 4702 | /* |
| 4703 | * Built-ins |
| 4704 | */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4705 | static int builtin_trap(char **argv) |
| 4706 | { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4707 | int i; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4708 | int sig; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4709 | char *new_cmd; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4710 | |
| 4711 | if (!G.traps) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4712 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4713 | |
| 4714 | if (!argv[1]) { |
| 4715 | /* No args: print all trapped. This isn't 100% correct as we should |
| 4716 | * be escaping the cmd so that it can be pasted back in ... |
| 4717 | */ |
| 4718 | for (i = 0; i < NSIG; ++i) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4719 | if (G.traps[i]) |
| 4720 | printf("trap -- '%s' %s\n", G.traps[i], get_signame(i)); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4721 | return EXIT_SUCCESS; |
| 4722 | } |
| 4723 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4724 | new_cmd = NULL; |
| 4725 | i = 0; |
| 4726 | /* if first arg is decimal: reset all specified */ |
| 4727 | sig = bb_strtou(*++argv, NULL, 10); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4728 | if (errno == 0) { |
| 4729 | int ret; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4730 | set_all: |
| 4731 | ret = EXIT_SUCCESS; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4732 | while (*argv) { |
| 4733 | sig = get_signum(*argv++); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4734 | if (sig < 0 || sig >= NSIG) { |
| 4735 | ret = EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4736 | /* mimic bash message exactly */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4737 | bb_perror_msg("trap: %s: invalid signal specification", argv[i]); |
| 4738 | continue; |
| 4739 | } |
| 4740 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4741 | free(G.traps[sig]); |
| 4742 | G.traps[sig] = xstrdup(new_cmd); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4743 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4744 | debug_printf("trap: setting SIG%s (%i) to '%s'", |
| 4745 | get_signame(sig), sig, G.traps[sig]); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4746 | |
| 4747 | /* There is no signal for 0 (EXIT) */ |
| 4748 | if (sig == 0) |
| 4749 | continue; |
| 4750 | |
| 4751 | if (new_cmd) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4752 | sigaddset(&G.blocked_set, sig); |
| 4753 | } else { |
| 4754 | /* there was a trap handler, we are removing it |
| 4755 | * (if sig has non-DFL handling, |
| 4756 | * we don't need to do anything) */ |
| 4757 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 4758 | continue; |
| 4759 | sigdelset(&G.blocked_set, sig); |
| 4760 | } |
| 4761 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4762 | } |
| 4763 | return ret; |
| 4764 | } |
| 4765 | |
| 4766 | /* first arg is "-": reset all specified to default */ |
| 4767 | /* first arg is "": ignore all specified */ |
| 4768 | /* everything else: execute first arg upon signal */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4769 | if (!argv[1]) { |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4770 | bb_error_msg("trap: invalid arguments"); |
| 4771 | return EXIT_FAILURE; |
| 4772 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4773 | if (LONE_DASH(*argv)) |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4774 | /* nothing! */; |
| 4775 | else |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4776 | new_cmd = *argv; |
| 4777 | argv++; |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 4778 | goto set_all; |
| 4779 | } |
| 4780 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4781 | static int builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4782 | { |
| 4783 | return 0; |
| 4784 | } |
| 4785 | |
| 4786 | static int builtin_test(char **argv) |
| 4787 | { |
| 4788 | int argc = 0; |
| 4789 | while (*argv) { |
| 4790 | argc++; |
| 4791 | argv++; |
| 4792 | } |
| 4793 | return test_main(argc, argv - argc); |
| 4794 | } |
| 4795 | |
| 4796 | static int builtin_echo(char **argv) |
| 4797 | { |
| 4798 | int argc = 0; |
| 4799 | while (*argv) { |
| 4800 | argc++; |
| 4801 | argv++; |
| 4802 | } |
| 4803 | return echo_main(argc, argv - argc); |
| 4804 | } |
| 4805 | |
| 4806 | static int builtin_eval(char **argv) |
| 4807 | { |
| 4808 | int rcode = EXIT_SUCCESS; |
| 4809 | |
| 4810 | if (argv[1]) { |
| 4811 | char *str = expand_strvec_to_string(argv + 1); |
| 4812 | parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP); |
| 4813 | free(str); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4814 | rcode = G.last_return_code; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4815 | } |
| 4816 | return rcode; |
| 4817 | } |
| 4818 | |
| 4819 | static int builtin_cd(char **argv) |
| 4820 | { |
| 4821 | const char *newdir; |
| 4822 | if (argv[1] == NULL) { |
| 4823 | // bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
| 4824 | // bash says "bash: cd: HOME not set" and does nothing (exitcode 1) |
| 4825 | newdir = getenv("HOME") ? : "/"; |
| 4826 | } else |
| 4827 | newdir = argv[1]; |
| 4828 | if (chdir(newdir)) { |
| 4829 | printf("cd: %s: %s\n", newdir, strerror(errno)); |
| 4830 | return EXIT_FAILURE; |
| 4831 | } |
| 4832 | set_cwd(); |
| 4833 | return EXIT_SUCCESS; |
| 4834 | } |
| 4835 | |
| 4836 | static int builtin_exec(char **argv) |
| 4837 | { |
| 4838 | if (argv[1] == NULL) |
| 4839 | return EXIT_SUCCESS; /* bash does this */ |
| 4840 | { |
| 4841 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4842 | nommu_save_t dummy; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4843 | #endif |
| 4844 | // FIXME: if exec fails, bash does NOT exit! We do... |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4845 | pseudo_exec_argv(&dummy, argv + 1, 0, NULL); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4846 | /* never returns */ |
| 4847 | } |
| 4848 | } |
| 4849 | |
| 4850 | static int builtin_exit(char **argv) |
| 4851 | { |
| 4852 | // TODO: bash does it ONLY on top-level sh exit (+interacive only?) |
| 4853 | //puts("exit"); /* bash does it */ |
| 4854 | // TODO: warn if we have background jobs: "There are stopped jobs" |
| 4855 | // On second consecutive 'exit', exit anyway. |
| 4856 | if (argv[1] == NULL) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4857 | hush_exit(G.last_return_code); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4858 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 4859 | xfunc_error_retval = 255; |
| 4860 | /* bash: exit -2 == exit 254, no error msg */ |
| 4861 | hush_exit(xatoi(argv[1]) & 0xff); |
| 4862 | } |
| 4863 | |
| 4864 | static int builtin_export(char **argv) |
| 4865 | { |
| 4866 | const char *value; |
| 4867 | char *name = argv[1]; |
| 4868 | |
| 4869 | if (name == NULL) { |
| 4870 | // TODO: |
| 4871 | // ash emits: export VAR='VAL' |
| 4872 | // bash: declare -x VAR="VAL" |
| 4873 | // (both also escape as needed (quotes, $, etc)) |
| 4874 | char **e = environ; |
| 4875 | if (e) |
| 4876 | while (*e) |
| 4877 | puts(*e++); |
| 4878 | return EXIT_SUCCESS; |
| 4879 | } |
| 4880 | |
| 4881 | value = strchr(name, '='); |
| 4882 | if (!value) { |
| 4883 | /* They are exporting something without a =VALUE */ |
| 4884 | struct variable *var; |
| 4885 | |
| 4886 | var = get_local_var(name); |
| 4887 | if (var) { |
| 4888 | var->flg_export = 1; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4889 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4890 | putenv(var->varstr); |
| 4891 | } |
| 4892 | /* bash does not return an error when trying to export |
| 4893 | * an undefined variable. Do likewise. */ |
| 4894 | return EXIT_SUCCESS; |
| 4895 | } |
| 4896 | |
| 4897 | set_local_var(xstrdup(name), 1); |
| 4898 | return EXIT_SUCCESS; |
| 4899 | } |
| 4900 | |
| 4901 | #if ENABLE_HUSH_JOB |
| 4902 | /* built-in 'fg' and 'bg' handler */ |
| 4903 | static int builtin_fg_bg(char **argv) |
| 4904 | { |
| 4905 | int i, jobnum; |
| 4906 | struct pipe *pi; |
| 4907 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4908 | if (!G.interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4909 | return EXIT_FAILURE; |
| 4910 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 4911 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4912 | for (pi = G.job_list; pi; pi = pi->next) { |
| 4913 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4914 | goto found; |
| 4915 | } |
| 4916 | } |
| 4917 | bb_error_msg("%s: no current job", argv[0]); |
| 4918 | return EXIT_FAILURE; |
| 4919 | } |
| 4920 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 4921 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 4922 | return EXIT_FAILURE; |
| 4923 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4924 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4925 | if (pi->jobid == jobnum) { |
| 4926 | goto found; |
| 4927 | } |
| 4928 | } |
| 4929 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 4930 | return EXIT_FAILURE; |
| 4931 | found: |
| 4932 | // TODO: bash prints a string representation |
| 4933 | // of job being foregrounded (like "sleep 1 | cat") |
| 4934 | if (*argv[0] == 'f') { |
| 4935 | /* Put the job into the foreground. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4936 | tcsetpgrp(G.interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
| 4939 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4940 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 4941 | for (i = 0; i < pi->num_cmds; i++) { |
| 4942 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 4943 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4944 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4945 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4946 | |
| 4947 | i = kill(- pi->pgrp, SIGCONT); |
| 4948 | if (i < 0) { |
| 4949 | if (errno == ESRCH) { |
| 4950 | delete_finished_bg_job(pi); |
| 4951 | return EXIT_SUCCESS; |
| 4952 | } else { |
| 4953 | bb_perror_msg("kill (SIGCONT)"); |
| 4954 | } |
| 4955 | } |
| 4956 | |
| 4957 | if (*argv[0] == 'f') { |
| 4958 | remove_bg_job(pi); |
| 4959 | return checkjobs_and_fg_shell(pi); |
| 4960 | } |
| 4961 | return EXIT_SUCCESS; |
| 4962 | } |
| 4963 | #endif |
| 4964 | |
| 4965 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4966 | static int builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4967 | { |
| 4968 | const struct built_in_command *x; |
| 4969 | |
| 4970 | printf("\nBuilt-in commands:\n"); |
| 4971 | printf("-------------------\n"); |
| 4972 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 4973 | printf("%s\t%s\n", x->cmd, x->descr); |
| 4974 | } |
| 4975 | printf("\n\n"); |
| 4976 | return EXIT_SUCCESS; |
| 4977 | } |
| 4978 | #endif |
| 4979 | |
| 4980 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4981 | static int builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4982 | { |
| 4983 | struct pipe *job; |
| 4984 | const char *status_string; |
| 4985 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4986 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4987 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4988 | status_string = "Stopped"; |
| 4989 | else |
| 4990 | status_string = "Running"; |
| 4991 | |
| 4992 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 4993 | } |
| 4994 | return EXIT_SUCCESS; |
| 4995 | } |
| 4996 | #endif |
| 4997 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4998 | static int builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4999 | { |
| 5000 | puts(set_cwd()); |
| 5001 | return EXIT_SUCCESS; |
| 5002 | } |
| 5003 | |
| 5004 | static int builtin_read(char **argv) |
| 5005 | { |
| 5006 | char *string; |
| 5007 | const char *name = argv[1] ? argv[1] : "REPLY"; |
| 5008 | |
| 5009 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
| 5010 | return set_local_var(string, 0); |
| 5011 | } |
| 5012 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5013 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 5014 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 5015 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5016 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 5017 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 5018 | * set -- [argument...] |
| 5019 | * set -o |
| 5020 | * set +o |
| 5021 | * Implementations shall support the options in both their hyphen and |
| 5022 | * plus-sign forms. These options can also be specified as options to sh. |
| 5023 | * Examples: |
| 5024 | * Write out all variables and their values: set |
| 5025 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 5026 | * Turn on the -x and -v options: set -xv |
| 5027 | * Unset all positional parameters: set -- |
| 5028 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 5029 | * Set the positional parameters to the expansion of x, even if x expands |
| 5030 | * with a leading '-' or '+': set -- $x |
| 5031 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5032 | * 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] | 5033 | */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5034 | static int builtin_set(char **argv) |
| 5035 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5036 | int n; |
| 5037 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 5038 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5039 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 5040 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5041 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5042 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5043 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5044 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 5045 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5046 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5047 | do { |
| 5048 | if (!strcmp(arg, "--")) { |
| 5049 | ++argv; |
| 5050 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5051 | } |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5052 | |
| 5053 | if (arg[0] == '+' || arg[0] == '-') { |
| 5054 | for (n = 1; arg[n]; ++n) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5055 | if (set_mode(arg[0], arg[n])) |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5056 | goto error; |
| 5057 | continue; |
| 5058 | } |
| 5059 | |
| 5060 | break; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5061 | } while ((arg = *++argv) != NULL); |
| 5062 | /* Now argv[0] is 1st argument */ |
| 5063 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5064 | /* Only reset global_argv if we didn't process anything */ |
| 5065 | if (arg == NULL) |
| 5066 | return EXIT_SUCCESS; |
| 5067 | set_argv: |
| 5068 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5069 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 5070 | g_argv = G.global_argv; |
| 5071 | if (G.global_args_malloced) { |
| 5072 | pp = g_argv; |
| 5073 | while (*++pp) |
| 5074 | free(*pp); |
| 5075 | g_argv[1] = NULL; |
| 5076 | } else { |
| 5077 | G.global_args_malloced = 1; |
| 5078 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 5079 | pp[0] = g_argv[0]; /* retain $0 */ |
| 5080 | g_argv = pp; |
| 5081 | } |
| 5082 | /* This realloc's G.global_argv */ |
| 5083 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 5084 | |
| 5085 | n = 1; |
| 5086 | while (*++pp) |
| 5087 | n++; |
| 5088 | G.global_argc = n; |
| 5089 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5090 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 5091 | |
| 5092 | /* Nothing known, so abort */ |
| 5093 | error: |
| 5094 | bb_error_msg("set: %s: invalid option", arg); |
| 5095 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5096 | } |
| 5097 | |
| 5098 | static int builtin_shift(char **argv) |
| 5099 | { |
| 5100 | int n = 1; |
| 5101 | if (argv[1]) { |
| 5102 | n = atoi(argv[1]); |
| 5103 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5104 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 5105 | if (G.global_args_malloced) { |
| 5106 | int m = 1; |
| 5107 | while (m <= n) |
| 5108 | free(G.global_argv[m++]); |
| 5109 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5110 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 5111 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 5112 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5113 | return EXIT_SUCCESS; |
| 5114 | } |
| 5115 | return EXIT_FAILURE; |
| 5116 | } |
| 5117 | |
| 5118 | static int builtin_source(char **argv) |
| 5119 | { |
| 5120 | FILE *input; |
| 5121 | int status; |
| 5122 | |
| 5123 | if (argv[1] == NULL) |
| 5124 | return EXIT_FAILURE; |
| 5125 | |
| 5126 | /* XXX search through $PATH is missing */ |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 5127 | input = fopen_for_read(argv[1]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5128 | if (!input) { |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5129 | bb_error_msg("can't open '%s'", argv[1]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5130 | return EXIT_FAILURE; |
| 5131 | } |
| 5132 | close_on_exec_on(fileno(input)); |
| 5133 | |
| 5134 | /* Now run the file */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5135 | /* 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] | 5136 | * (pointer only is OK!) on this stack frame, |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5137 | * set G.global_argv=argv+1, recurse, and restore. */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5138 | status = parse_and_run_file(input); |
| 5139 | fclose(input); |
| 5140 | return status; |
| 5141 | } |
| 5142 | |
| 5143 | static int builtin_umask(char **argv) |
| 5144 | { |
| 5145 | mode_t new_umask; |
| 5146 | const char *arg = argv[1]; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5147 | if (arg) { |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 5148 | new_umask = bb_strtou(arg, NULL, 8); |
| 5149 | if (errno) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5150 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5151 | } else { |
| 5152 | new_umask = umask(0); |
| 5153 | printf("%.3o\n", (unsigned) new_umask); |
| 5154 | } |
| 5155 | umask(new_umask); |
| 5156 | return EXIT_SUCCESS; |
| 5157 | } |
| 5158 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 5159 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5160 | static int builtin_unset(char **argv) |
| 5161 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 5162 | size_t i; |
| 5163 | int ret; |
| 5164 | bool var = true; |
| 5165 | |
| 5166 | if (!argv[1]) |
| 5167 | return EXIT_SUCCESS; |
| 5168 | |
| 5169 | i = 0; |
| 5170 | if (argv[1][0] == '-') { |
| 5171 | switch (argv[1][1]) { |
| 5172 | case 'v': break; |
| 5173 | case 'f': if (ENABLE_HUSH_FUNCTIONS) { var = false; break; } |
| 5174 | default: |
| 5175 | bb_error_msg("unset: %s: invalid option", argv[1]); |
| 5176 | return EXIT_FAILURE; |
| 5177 | } |
| 5178 | ++i; |
| 5179 | } |
| 5180 | |
| 5181 | ret = EXIT_SUCCESS; |
| 5182 | while (argv[++i]) { |
| 5183 | if (var) { |
| 5184 | if (unset_local_var(argv[i])) |
| 5185 | ret = EXIT_FAILURE; |
| 5186 | } |
| 5187 | #if ENABLE_HUSH_FUNCTIONS |
| 5188 | else |
| 5189 | unset_local_func(argv[i]); |
| 5190 | #endif |
| 5191 | } |
| 5192 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5193 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5194 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5195 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
| 5196 | static int builtin_wait(char **argv) |
| 5197 | { |
| 5198 | int ret = EXIT_SUCCESS; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 5199 | int status, sig; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5200 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 5201 | if (*++argv == NULL) { |
| 5202 | /* Don't care about wait results */ |
| 5203 | /* Note 1: must wait until there are no more children */ |
| 5204 | /* Note 2: must be interruptible */ |
| 5205 | /* Examples: |
| 5206 | * $ sleep 3 & sleep 6 & wait |
| 5207 | * [1] 30934 sleep 3 |
| 5208 | * [2] 30935 sleep 6 |
| 5209 | * [1] Done sleep 3 |
| 5210 | * [2] Done sleep 6 |
| 5211 | * $ sleep 3 & sleep 6 & wait |
| 5212 | * [1] 30936 sleep 3 |
| 5213 | * [2] 30937 sleep 6 |
| 5214 | * [1] Done sleep 3 |
| 5215 | * ^C <-- after ~4 sec from keyboard |
| 5216 | * $ |
| 5217 | */ |
| 5218 | sigaddset(&G.blocked_set, SIGCHLD); |
| 5219 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 5220 | while (1) { |
| 5221 | checkjobs(NULL); |
| 5222 | if (errno == ECHILD) |
| 5223 | break; |
| 5224 | /* Wait for SIGCHLD or any other signal of interest */ |
| 5225 | /* sigtimedwait with infinite timeout: */ |
| 5226 | sig = sigwaitinfo(&G.blocked_set, NULL); |
| 5227 | if (sig > 0) { |
| 5228 | sig = check_and_run_traps(sig); |
| 5229 | if (sig && sig != SIGCHLD) { /* see note 2 */ |
| 5230 | ret = 128 + sig; |
| 5231 | break; |
| 5232 | } |
| 5233 | } |
| 5234 | } |
| 5235 | sigdelset(&G.blocked_set, SIGCHLD); |
| 5236 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 5237 | return ret; |
| 5238 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5239 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 5240 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5241 | while (*argv) { |
| 5242 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 5243 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5244 | /* mimic bash message */ |
| 5245 | 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] | 5246 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5247 | } |
| 5248 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5249 | if (WIFSIGNALED(status)) |
| 5250 | ret = 128 + WTERMSIG(status); |
| 5251 | else if (WIFEXITED(status)) |
| 5252 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5253 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5254 | ret = EXIT_FAILURE; |
| 5255 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5256 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5257 | ret = 127; |
| 5258 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5259 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 5260 | } |
| 5261 | |
| 5262 | return ret; |
| 5263 | } |
| 5264 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 5265 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5266 | static int builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5267 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5268 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 5269 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 5270 | return EXIT_SUCCESS; /* bash compat */ |
| 5271 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5272 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
| 5273 | G.depth_break_continue = 1; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5274 | if (argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5275 | G.depth_break_continue = bb_strtou(argv[1], NULL, 10); |
| 5276 | if (errno || !G.depth_break_continue || argv[2]) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 5277 | bb_error_msg("%s: bad arguments", argv[0]); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5278 | G.flag_break_continue = BC_BREAK; |
| 5279 | G.depth_break_continue = UINT_MAX; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5280 | } |
| 5281 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5282 | if (G.depth_of_loop < G.depth_break_continue) |
| 5283 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5284 | return EXIT_SUCCESS; |
| 5285 | } |
| 5286 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 5287 | static int builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5288 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 5289 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 5290 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5291 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 5292 | #endif |