Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * sh.c -- 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. |
| 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 | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 25 | * and many builtins derived from contributions by Erik Andersen |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 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 | * |
| 39 | * Bash grammar not implemented: (how many of these were in original sh?) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 40 | * $_ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 41 | * &> and >& redirection of stdout+stderr |
| 42 | * Brace Expansion |
| 43 | * Tilde Expansion |
| 44 | * fancy forms of Parameter Expansion |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 45 | * aliases |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 46 | * Arithmetic Expansion |
| 47 | * <(list) and >(list) Process Substitution |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 48 | * reserved words: select, function |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 49 | * Here Documents ( << word ) |
| 50 | * Functions |
| 51 | * Major bugs: |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 52 | * job handling woefully incomplete and buggy (improved --vda) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 53 | * to-do: |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 54 | * port selected bugfixes from post-0.49 busybox lash - done? |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 55 | * change { and } from special chars to reserved words |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 56 | * builtins: return, trap, ulimit |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 57 | * test magic exec with redirection only |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 58 | * check setting of global_argc and global_argv |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 59 | * follow IFS rules more precisely, including update semantics |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 60 | * figure out what to do with backslash-newline |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 61 | * propagate syntax errors, die on resource errors? |
| 62 | * continuation lines, both explicit and implicit - done? |
| 63 | * memory leak finding and plugging - done? |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 64 | * maybe change charmap[] to use 2-bit entries |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 65 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 66 | * 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] | 67 | */ |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 69 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 70 | //TODO: pull in some .h and find out do we have SINGLE_APPLET_MAIN? |
| 71 | //#include "applet_tables.h" doesn't work |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 72 | #include <glob.h> |
| 73 | /* #include <dmalloc.h> */ |
| 74 | #if ENABLE_HUSH_CASE |
| 75 | #include <fnmatch.h> |
| 76 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 78 | #define HUSH_VER_STR "0.91" |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 79 | |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 80 | #if defined SINGLE_APPLET_MAIN |
| 81 | /* STANDALONE does not make sense, and won't compile */ |
| 82 | #undef CONFIG_FEATURE_SH_STANDALONE |
| 83 | #undef ENABLE_FEATURE_SH_STANDALONE |
| 84 | #undef USE_FEATURE_SH_STANDALONE |
| 85 | #define SKIP_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
| 86 | #define ENABLE_FEATURE_SH_STANDALONE 0 |
| 87 | #define USE_FEATURE_SH_STANDALONE(...) |
| 88 | #define SKIP_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
| 89 | #endif |
| 90 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 91 | #if !BB_MMU && ENABLE_HUSH_TICK |
| 92 | //#undef ENABLE_HUSH_TICK |
| 93 | //#define ENABLE_HUSH_TICK 0 |
| 94 | #warning On NOMMU, hush command substitution is dangerous. |
| 95 | #warning Dont use it for commands which produce lots of output. |
| 96 | #warning For more info see shell/hush.c, generate_stream_from_list(). |
| 97 | #endif |
| 98 | |
| 99 | #if !BB_MMU && ENABLE_HUSH_JOB |
| 100 | #undef ENABLE_HUSH_JOB |
| 101 | #define ENABLE_HUSH_JOB 0 |
| 102 | #endif |
| 103 | |
| 104 | #if !ENABLE_HUSH_INTERACTIVE |
| 105 | #undef ENABLE_FEATURE_EDITING |
| 106 | #define ENABLE_FEATURE_EDITING 0 |
| 107 | #undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 108 | #define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 109 | #endif |
| 110 | |
| 111 | |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 112 | /* Keep unconditionally on for now */ |
| 113 | #define HUSH_DEBUG 1 |
| 114 | /* In progress... */ |
| 115 | #define ENABLE_HUSH_FUNCTIONS 0 |
| 116 | |
| 117 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 118 | /* If you comment out one of these below, it will be #defined later |
| 119 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 120 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 121 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 122 | #define debug_printf_parse(...) do {} while (0) |
| 123 | #define debug_print_tree(a, b) do {} while (0) |
| 124 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 125 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 126 | #define debug_printf_jobs(...) do {} while (0) |
| 127 | #define debug_printf_expand(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 128 | #define debug_printf_glob(...) do {} while (0) |
| 129 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 130 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 131 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 132 | |
| 133 | #ifndef debug_printf |
| 134 | #define debug_printf(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 135 | #endif |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 136 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 137 | #ifndef debug_printf_parse |
| 138 | #define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 139 | #endif |
| 140 | |
| 141 | #ifndef debug_printf_exec |
| 142 | #define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__) |
| 143 | #endif |
| 144 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 145 | #ifndef debug_printf_env |
| 146 | #define debug_printf_env(...) fprintf(stderr, __VA_ARGS__) |
| 147 | #endif |
| 148 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 149 | #ifndef debug_printf_jobs |
| 150 | #define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 151 | #define DEBUG_JOBS 1 |
| 152 | #else |
| 153 | #define DEBUG_JOBS 0 |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 154 | #endif |
| 155 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 156 | #ifndef debug_printf_expand |
| 157 | #define debug_printf_expand(...) fprintf(stderr, __VA_ARGS__) |
| 158 | #define DEBUG_EXPAND 1 |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 159 | #else |
| 160 | #define DEBUG_EXPAND 0 |
| 161 | #endif |
| 162 | |
| 163 | #ifndef debug_printf_glob |
| 164 | #define debug_printf_glob(...) fprintf(stderr, __VA_ARGS__) |
| 165 | #define DEBUG_GLOB 1 |
| 166 | #else |
| 167 | #define DEBUG_GLOB 0 |
| 168 | #endif |
| 169 | |
| 170 | #ifndef debug_printf_list |
| 171 | #define debug_printf_list(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 172 | #endif |
| 173 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 174 | #ifndef debug_printf_subst |
| 175 | #define debug_printf_subst(...) fprintf(stderr, __VA_ARGS__) |
| 176 | #endif |
| 177 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 178 | #ifndef debug_printf_clean |
| 179 | /* broken, of course, but OK for testing */ |
| 180 | static const char *indenter(int i) |
| 181 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 182 | static const char blanks[] ALIGN1 = |
| 183 | " "; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 184 | return &blanks[sizeof(blanks) - i - 1]; |
| 185 | } |
| 186 | #define debug_printf_clean(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 187 | #define DEBUG_CLEAN 1 |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 188 | #endif |
| 189 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 190 | #if DEBUG_EXPAND |
| 191 | static void debug_print_strings(const char *prefix, char **vv) |
| 192 | { |
| 193 | fprintf(stderr, "%s:\n", prefix); |
| 194 | while (*vv) |
| 195 | fprintf(stderr, " '%s'\n", *vv++); |
| 196 | } |
| 197 | #else |
| 198 | #define debug_print_strings(prefix, vv) ((void)0) |
| 199 | #endif |
| 200 | |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 201 | /* |
| 202 | * Leak hunting. Use hush_leaktool.sh for post-processing. |
| 203 | */ |
| 204 | #ifdef FOR_HUSH_LEAKTOOL |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 205 | /* suppress "warning: no previous prototype..." */ |
| 206 | void *xxmalloc(int lineno, size_t size); |
| 207 | void *xxrealloc(int lineno, void *ptr, size_t size); |
| 208 | char *xxstrdup(int lineno, const char *str); |
| 209 | void xxfree(void *ptr); |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 210 | void *xxmalloc(int lineno, size_t size) |
| 211 | { |
| 212 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 213 | fprintf(stderr, "line %d: malloc %p\n", lineno, ptr); |
| 214 | return ptr; |
| 215 | } |
| 216 | void *xxrealloc(int lineno, void *ptr, size_t size) |
| 217 | { |
| 218 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 219 | fprintf(stderr, "line %d: realloc %p\n", lineno, ptr); |
| 220 | return ptr; |
| 221 | } |
| 222 | char *xxstrdup(int lineno, const char *str) |
| 223 | { |
| 224 | char *ptr = xstrdup(str); |
| 225 | fprintf(stderr, "line %d: strdup %p\n", lineno, ptr); |
| 226 | return ptr; |
| 227 | } |
| 228 | void xxfree(void *ptr) |
| 229 | { |
| 230 | fprintf(stderr, "free %p\n", ptr); |
| 231 | free(ptr); |
| 232 | } |
| 233 | #define xmalloc(s) xxmalloc(__LINE__, s) |
| 234 | #define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 235 | #define xstrdup(s) xxstrdup(__LINE__, s) |
| 236 | #define free(p) xxfree(p) |
| 237 | #endif |
| 238 | |
| 239 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 240 | /* Do we support ANY keywords? */ |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 241 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 242 | #define HAS_KEYWORDS 1 |
| 243 | #define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 244 | #define IF_HAS_NO_KEYWORDS(...) |
| 245 | #else |
| 246 | #define HAS_KEYWORDS 0 |
| 247 | #define IF_HAS_KEYWORDS(...) |
| 248 | #define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
| 249 | #endif |
| 250 | |
| 251 | |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 252 | #define SPECIAL_VAR_SYMBOL 3 |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 253 | #define PARSEFLAG_EXIT_FROM_LOOP 1 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 254 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 255 | typedef enum redir_type { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 256 | REDIRECT_INPUT = 1, |
| 257 | REDIRECT_OVERWRITE = 2, |
| 258 | REDIRECT_APPEND = 3, |
| 259 | REDIRECT_HEREIS = 4, |
| 260 | REDIRECT_IO = 5 |
| 261 | } redir_type; |
| 262 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 263 | /* The descrip member of this structure is only used to make |
| 264 | * debugging output pretty */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 265 | static const struct { |
| 266 | int mode; |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 267 | signed char default_fd; |
| 268 | char descrip[3]; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 269 | } redir_table[] = { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 270 | { 0, 0, "()" }, |
| 271 | { O_RDONLY, 0, "<" }, |
| 272 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 273 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 274 | { O_RDONLY, -1, "<<" }, |
| 275 | { O_RDWR, 1, "<>" } |
| 276 | }; |
| 277 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 278 | typedef enum pipe_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 279 | PIPE_SEQ = 1, |
| 280 | PIPE_AND = 2, |
| 281 | PIPE_OR = 3, |
| 282 | PIPE_BG = 4, |
| 283 | } pipe_style; |
| 284 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 285 | typedef enum reserved_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 286 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 287 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 288 | RES_IF , |
| 289 | RES_THEN , |
| 290 | RES_ELIF , |
| 291 | RES_ELSE , |
| 292 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 293 | #endif |
| 294 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 295 | RES_FOR , |
| 296 | RES_WHILE , |
| 297 | RES_UNTIL , |
| 298 | RES_DO , |
| 299 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 300 | #endif |
| 301 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 302 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 303 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 304 | #if ENABLE_HUSH_CASE |
| 305 | RES_CASE , |
| 306 | /* two pseudo-keywords support contrived "case" syntax: */ |
| 307 | RES_MATCH , /* "word)" */ |
| 308 | RES_CASEI , /* "this command is inside CASE" */ |
| 309 | RES_ESAC , |
| 310 | #endif |
| 311 | RES_XXXX , |
| 312 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 313 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 314 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 315 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 316 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 317 | char *rd_filename; /* filename */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 318 | int fd; /* file descriptor being redirected */ |
| 319 | int dup; /* -1, or file descriptor being duplicated */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 320 | smallint /*enum redir_type*/ rd_type; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 321 | }; |
| 322 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 323 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 324 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 325 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 326 | smallint is_stopped; /* is the command currently running? */ |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 327 | smallint grp_type; |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 328 | struct pipe *group; /* if non-NULL, this "prog" is {} group, |
| 329 | * subshell, or a compound statement */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 330 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 331 | struct redir_struct *redirects; /* I/O redirections */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 332 | }; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 333 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 334 | * and on execution these are substituted with their values. |
| 335 | * Substitution can make _several_ words out of one argv[n]! |
| 336 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 337 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 338 | */ |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 339 | #define GRP_NORMAL 0 |
| 340 | #define GRP_SUBSHELL 1 |
| 341 | #if ENABLE_HUSH_FUNCTIONS |
| 342 | #define GRP_FUNCTION 2 |
| 343 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 344 | |
| 345 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 346 | struct pipe *next; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 347 | int num_cmds; /* total number of commands in job */ |
| 348 | int alive_cmds; /* number of commands running (not exited) */ |
| 349 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 350 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 351 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 352 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 353 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 354 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 355 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 356 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 357 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 358 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 359 | }; |
| 360 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 361 | /* This holds pointers to the various results of parsing */ |
| 362 | struct parse_context { |
| 363 | struct command *command; |
| 364 | struct pipe *list_head; |
| 365 | struct pipe *pipe; |
| 366 | struct redir_struct *pending_redirect; |
| 367 | #if HAS_KEYWORDS |
| 368 | smallint ctx_res_w; |
| 369 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 370 | #if ENABLE_HUSH_CASE |
| 371 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 372 | #endif |
| 373 | int old_flag; /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 374 | struct parse_context *stack; |
| 375 | #endif |
| 376 | }; |
| 377 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 378 | /* On program start, environ points to initial environment. |
| 379 | * putenv adds new pointers into it, unsetenv removes them. |
| 380 | * Neither of these (de)allocates the strings. |
| 381 | * setenv allocates new strings in malloc space and does putenv, |
| 382 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 383 | #define setenv(...) setenv_is_leaky_dont_use() |
| 384 | struct variable { |
| 385 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 386 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 387 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 388 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 389 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 390 | }; |
| 391 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 392 | typedef struct o_string { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 393 | char *data; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 394 | int length; /* position where data is appended */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 395 | int maxlen; |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 396 | /* Misnomer! it's not "quoting", it's "protection against globbing"! |
| 397 | * (by prepending \ to *, ?, [ and to \ too) */ |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 398 | smallint o_quote; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 399 | smallint o_glob; |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 400 | smallint nonnull; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 401 | smallint has_empty_slot; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 402 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 403 | } o_string; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 404 | enum { |
| 405 | MAYBE_ASSIGNMENT = 0, |
| 406 | DEFINITELY_ASSIGNMENT = 1, |
| 407 | NOT_ASSIGNMENT = 2, |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 408 | 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] | 409 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 410 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 411 | #define NULL_O_STRING { NULL } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 412 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 413 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 414 | * available? Where is it documented? */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 415 | typedef struct in_str { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 416 | const char *p; |
| 417 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 418 | char eof_flag; /* meaningless if ->p == NULL */ |
| 419 | char peek_buf[2]; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 420 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 421 | smallint promptme; |
| 422 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 423 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 424 | FILE *file; |
| 425 | int (*get) (struct in_str *); |
| 426 | int (*peek) (struct in_str *); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 427 | } in_str; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 428 | #define i_getch(input) ((input)->get(input)) |
| 429 | #define i_peek(input) ((input)->peek(input)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 430 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 431 | enum { |
| 432 | CHAR_ORDINARY = 0, |
| 433 | CHAR_ORDINARY_IF_QUOTED = 1, /* example: *, # */ |
| 434 | CHAR_IFS = 2, /* treated as ordinary if quoted */ |
| 435 | CHAR_SPECIAL = 3, /* example: $ */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 436 | }; |
| 437 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 438 | enum { |
| 439 | BC_BREAK = 1, |
| 440 | BC_CONTINUE = 2, |
| 441 | }; |
| 442 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 443 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 444 | /* "Globals" within this file */ |
| 445 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 446 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 447 | struct globals { |
| 448 | #if ENABLE_HUSH_INTERACTIVE |
| 449 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 450 | * _AND_ if we decided to act interactively */ |
| 451 | int interactive_fd; |
| 452 | const char *PS1; |
| 453 | const char *PS2; |
| 454 | #endif |
| 455 | #if ENABLE_FEATURE_EDITING |
| 456 | line_input_t *line_input_state; |
| 457 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 458 | pid_t root_pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 459 | pid_t last_bg_pid; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 460 | #if ENABLE_HUSH_JOB |
| 461 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 462 | pid_t saved_tty_pgrp; |
| 463 | int last_jobid; |
| 464 | struct pipe *job_list; |
| 465 | struct pipe *toplevel_list; |
| 466 | smallint ctrl_z_flag; |
| 467 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 468 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 469 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 470 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 471 | smallint fake_mode; |
| 472 | /* these three support $?, $#, and $1 */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 473 | smalluint last_return_code; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 474 | smalluint global_args_malloced; |
| 475 | int global_argc; /* NB: $# + 1 */ |
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]; |
| 493 | }; |
| 494 | |
| 495 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 496 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 497 | * (too many defines). Also, I actually prefer to see when a variable |
| 498 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 499 | #define INIT_G() do { \ |
| 500 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 501 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 502 | |
| 503 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 504 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
| 505 | |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 506 | #if 1 |
| 507 | /* Normal */ |
| 508 | static void syntax(const char *msg) |
| 509 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 510 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 511 | /* Was using fancy stuff: |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 512 | * (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...) |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 513 | * but it SEGVs. ?! Oh well... explicit temp ptr works around that */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 514 | void FAST_FUNC (*fp)(const char *s, ...); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 515 | fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die); |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 516 | fp(msg ? "%s: %s" : "syntax error", "syntax error", msg); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 517 | #else |
| 518 | bb_error_msg_and_die(msg ? "%s: %s" : "syntax error", "syntax error", msg); |
| 519 | #endif |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 520 | } |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 521 | |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 522 | #else |
| 523 | /* Debug */ |
| 524 | static void syntax_lineno(int line) |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 525 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 526 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 527 | void FAST_FUNC (*fp)(const char *s, ...); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 528 | fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die); |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 529 | fp("syntax error hush.c:%d", line); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 530 | #else |
| 531 | bb_error_msg_and_die("syntax error hush.c:%d", line); |
| 532 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 533 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 534 | #define syntax(str) syntax_lineno(__LINE__) |
| 535 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 536 | |
| 537 | /* Index of subroutines: */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 538 | /* in_str manipulations: */ |
| 539 | static int static_get(struct in_str *i); |
| 540 | static int static_peek(struct in_str *i); |
| 541 | static int file_get(struct in_str *i); |
| 542 | static int file_peek(struct in_str *i); |
| 543 | static void setup_file_in_str(struct in_str *i, FILE *f); |
| 544 | static void setup_string_in_str(struct in_str *i, const char *s); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 545 | /* "run" the final data structures: */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 546 | #if !defined(DEBUG_CLEAN) |
| 547 | #define free_pipe_list(head, indent) free_pipe_list(head) |
| 548 | #define free_pipe(pi, indent) free_pipe(pi) |
| 549 | #endif |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 550 | static int free_pipe_list(struct pipe *head, int indent); |
| 551 | static int free_pipe(struct pipe *pi, int indent); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 552 | /* really run the final data structures: */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 553 | typedef struct nommu_save_t { |
| 554 | char **new_env; |
| 555 | char **old_env; |
| 556 | char **argv; |
| 557 | } nommu_save_t; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 558 | #if BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 559 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 560 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 561 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 562 | pseudo_exec(command, argv_expanded) |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 563 | #endif |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 564 | static void pseudo_exec_argv(nommu_save_t *nommu_save, char **argv, int assignment_cnt, char **argv_expanded) NORETURN; |
| 565 | static void pseudo_exec(nommu_save_t *nommu_save, struct command *command, char **argv_expanded) NORETURN; |
| 566 | static int setup_redirects(struct command *prog, int squirrel[]); |
| 567 | static int run_list(struct pipe *pi); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 568 | static int run_pipe(struct pipe *pi); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 569 | /* data structure manipulation: */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 570 | static int setup_redirect(struct parse_context *ctx, int fd, redir_type style, struct in_str *input); |
| 571 | static void initialize_context(struct parse_context *ctx); |
| 572 | static int done_word(o_string *dest, struct parse_context *ctx); |
| 573 | static int done_command(struct parse_context *ctx); |
| 574 | static void done_pipe(struct parse_context *ctx, pipe_style type); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 575 | /* primary string parsing: */ |
| 576 | static int redirect_dup_num(struct in_str *input); |
| 577 | static int redirect_opt_num(o_string *o); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 578 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 579 | static int process_command_subs(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 580 | struct in_str *input, const char *subst_end); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 581 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 582 | static int parse_group(o_string *dest, struct parse_context *ctx, struct in_str *input, int ch); |
Denis Vlasenko | 15d78fb | 2007-01-30 22:28:21 +0000 | [diff] [blame] | 583 | static const char *lookup_param(const char *src); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 584 | static int handle_dollar(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 585 | struct in_str *input); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 586 | static int parse_stream(o_string *dest, struct parse_context *ctx, struct in_str *input0, const char *end_trigger); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 587 | /* setup: */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 588 | static int parse_and_run_stream(struct in_str *inp, int parse_flag); |
| 589 | static int parse_and_run_string(const char *s, int parse_flag); |
| 590 | static int parse_and_run_file(FILE *f); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 591 | /* job management: */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 592 | static int checkjobs(struct pipe* fg_pipe); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 593 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 594 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 595 | static void insert_bg_job(struct pipe *pi); |
| 596 | static void remove_bg_job(struct pipe *pi); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 597 | static void delete_finished_bg_job(struct pipe *pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 598 | #else |
| 599 | int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */ |
| 600 | #endif |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 601 | /* local variable support */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 602 | static char **expand_strvec_to_strvec(char **argv); |
| 603 | /* used for eval */ |
| 604 | static char *expand_strvec_to_string(char **argv); |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 605 | /* used for expansion of right hand of assignments */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 606 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 607 | static struct variable *get_local_var(const char *name); |
| 608 | static int set_local_var(char *str, int flg_export); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 609 | static void unset_local_var(const char *name); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 610 | |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 611 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 612 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="HUSH_VER_STR; |
| 613 | |
| 614 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 615 | static int glob_needed(const char *s) |
| 616 | { |
| 617 | while (*s) { |
| 618 | if (*s == '\\') |
| 619 | s++; |
| 620 | if (*s == '*' || *s == '[' || *s == '?') |
| 621 | return 1; |
| 622 | s++; |
| 623 | } |
| 624 | return 0; |
| 625 | } |
| 626 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 627 | static int is_assignment(const char *s) |
| 628 | { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 629 | if (!s || !(isalpha(*s) || *s == '_')) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 630 | return 0; |
| 631 | s++; |
| 632 | while (isalnum(*s) || *s == '_') |
| 633 | s++; |
| 634 | return *s == '='; |
| 635 | } |
| 636 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 637 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 638 | static char *unbackslash(char *src) |
| 639 | { |
| 640 | char *dst = src; |
| 641 | while (1) { |
| 642 | if (*src == '\\') |
| 643 | src++; |
| 644 | if ((*dst++ = *src++) == '\0') |
| 645 | break; |
| 646 | } |
| 647 | return dst; |
| 648 | } |
| 649 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 650 | 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] | 651 | { |
| 652 | int i; |
| 653 | unsigned count1; |
| 654 | unsigned count2; |
| 655 | char **v; |
| 656 | |
| 657 | v = strings; |
| 658 | count1 = 0; |
| 659 | if (v) { |
| 660 | while (*v) { |
| 661 | count1++; |
| 662 | v++; |
| 663 | } |
| 664 | } |
| 665 | count2 = 0; |
| 666 | v = add; |
| 667 | while (*v) { |
| 668 | count2++; |
| 669 | v++; |
| 670 | } |
| 671 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 672 | v[count1 + count2] = NULL; |
| 673 | i = count2; |
| 674 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 675 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 676 | return v; |
| 677 | } |
| 678 | |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 679 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 680 | { |
| 681 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 682 | v[0] = add; |
| 683 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 684 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 685 | } |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 686 | |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 687 | static void putenv_all(char **strings) |
| 688 | { |
| 689 | if (!strings) |
| 690 | return; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 691 | while (*strings) { |
| 692 | debug_printf_env("putenv '%s'\n", *strings); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 693 | putenv(*strings++); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 694 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | static char **putenv_all_and_save_old(char **strings) |
| 698 | { |
| 699 | char **old = NULL; |
| 700 | char **s = strings; |
| 701 | |
| 702 | if (!strings) |
| 703 | return old; |
| 704 | while (*strings) { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 705 | char *v, *eq; |
| 706 | |
| 707 | eq = strchr(*strings, '='); |
| 708 | if (eq) { |
| 709 | *eq = '\0'; |
| 710 | v = getenv(*strings); |
| 711 | *eq = '='; |
| 712 | if (v) { |
| 713 | /* v points to VAL in VAR=VAL, go back to VAR */ |
| 714 | v -= (eq - *strings) + 1; |
| 715 | old = add_string_to_strings(old, v); |
| 716 | } |
| 717 | } |
| 718 | strings++; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 719 | } |
| 720 | putenv_all(s); |
| 721 | return old; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 724 | static void free_strings_and_unsetenv(char **strings, int unset) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 725 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 726 | char **v; |
| 727 | |
| 728 | if (!strings) |
| 729 | return; |
| 730 | |
| 731 | v = strings; |
| 732 | while (*v) { |
| 733 | if (unset) { |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 734 | debug_printf_env("unsetenv '%s'\n", *v); |
| 735 | bb_unsetenv(*v); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 736 | } |
| 737 | free(*v++); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 738 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 739 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 742 | static void free_strings(char **strings) |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 743 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 744 | free_strings_and_unsetenv(strings, 0); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 745 | } |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 746 | |
| 747 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 748 | /* Function prototypes for builtins */ |
| 749 | static int builtin_cd(char **argv); |
Denis Vlasenko | 5bc593c | 2007-11-23 21:20:21 +0000 | [diff] [blame] | 750 | static int builtin_echo(char **argv); |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 751 | static int builtin_eval(char **argv); |
| 752 | static int builtin_exec(char **argv); |
| 753 | static int builtin_exit(char **argv); |
| 754 | static int builtin_export(char **argv); |
| 755 | #if ENABLE_HUSH_JOB |
| 756 | static int builtin_fg_bg(char **argv); |
| 757 | static int builtin_jobs(char **argv); |
| 758 | #endif |
| 759 | #if ENABLE_HUSH_HELP |
| 760 | static int builtin_help(char **argv); |
| 761 | #endif |
| 762 | static int builtin_pwd(char **argv); |
| 763 | static int builtin_read(char **argv); |
| 764 | static int builtin_test(char **argv); |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 765 | static int builtin_true(char **argv); |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 766 | static int builtin_set(char **argv); |
| 767 | static int builtin_shift(char **argv); |
| 768 | static int builtin_source(char **argv); |
| 769 | static int builtin_umask(char **argv); |
| 770 | static int builtin_unset(char **argv); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 771 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 772 | static int builtin_break(char **argv); |
| 773 | static int builtin_continue(char **argv); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 774 | #endif |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 775 | //static int builtin_not_written(char **argv); |
| 776 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 777 | /* Table of built-in functions. They can be forked or not, depending on |
| 778 | * context: within pipes, they fork. As simple commands, they do not. |
| 779 | * When used in non-forking context, they can change global variables |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 780 | * in the parent shell process. If forked, of course they cannot. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 781 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 782 | * still be set at the end. */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 783 | struct built_in_command { |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 784 | const char *cmd; |
| 785 | int (*function)(char **argv); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 786 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 787 | const char *descr; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 788 | #define BLTIN(cmd, func, help) { cmd, func, help } |
| 789 | #else |
| 790 | #define BLTIN(cmd, func, help) { cmd, func } |
| 791 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 792 | }; |
| 793 | |
Denis Vlasenko | 5bc593c | 2007-11-23 21:20:21 +0000 | [diff] [blame] | 794 | /* For now, echo and test are unconditionally enabled. |
| 795 | * Maybe make it configurable? */ |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 796 | static const struct built_in_command bltins[] = { |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 797 | BLTIN("." , builtin_source, "Run commands in a file"), |
| 798 | BLTIN(":" , builtin_true, "No-op"), |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 799 | BLTIN("[" , builtin_test, "Test condition"), |
| 800 | BLTIN("[[" , builtin_test, "Test condition"), |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 801 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 802 | BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"), |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 803 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 804 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 805 | BLTIN("break" , builtin_break, "Exit from a loop"), |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 806 | #endif |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 807 | BLTIN("cd" , builtin_cd, "Change directory"), |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 808 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 809 | BLTIN("continue", builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 810 | #endif |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 811 | BLTIN("echo" , builtin_echo, "Write to stdout"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 812 | BLTIN("eval" , builtin_eval, "Construct and run shell command"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 813 | BLTIN("exec" , builtin_exec, "Execute command, don't return to shell"), |
| 814 | BLTIN("exit" , builtin_exit, "Exit"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 815 | BLTIN("export", builtin_export, "Set environment variable"), |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 816 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 817 | BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 818 | BLTIN("jobs" , builtin_jobs, "List active jobs"), |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 819 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 820 | BLTIN("pwd" , builtin_pwd, "Print current directory"), |
| 821 | BLTIN("read" , builtin_read, "Input environment variable"), |
| 822 | // BLTIN("return", builtin_not_written, "Return from a function"), |
| 823 | BLTIN("set" , builtin_set, "Set/unset shell local variables"), |
| 824 | BLTIN("shift" , builtin_shift, "Shift positional parameters"), |
| 825 | // BLTIN("trap" , builtin_not_written, "Trap signals"), |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 826 | BLTIN("test" , builtin_test, "Test condition"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 827 | // BLTIN("ulimit", builtin_not_written, "Control resource limits"), |
| 828 | BLTIN("umask" , builtin_umask, "Set file creation mask"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 829 | BLTIN("unset" , builtin_unset, "Unset environment variable"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 830 | #if ENABLE_HUSH_HELP |
| 831 | BLTIN("help" , builtin_help, "List shell built-in commands"), |
| 832 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 833 | }; |
| 834 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 835 | |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 836 | /* Signals are grouped, we handle them in batches */ |
| 837 | static void set_misc_sighandler(void (*handler)(int)) |
| 838 | { |
| 839 | bb_signals(0 |
| 840 | + (1 << SIGINT) |
| 841 | + (1 << SIGQUIT) |
| 842 | + (1 << SIGTERM) |
| 843 | , handler); |
| 844 | } |
| 845 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 846 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 847 | |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 848 | static void set_fatal_sighandler(void (*handler)(int)) |
| 849 | { |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 850 | bb_signals(0 |
| 851 | + (1 << SIGILL) |
| 852 | + (1 << SIGTRAP) |
| 853 | + (1 << SIGABRT) |
| 854 | + (1 << SIGFPE) |
| 855 | + (1 << SIGBUS) |
| 856 | + (1 << SIGSEGV) |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 857 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 858 | + (1 << SIGHUP) |
| 859 | + (1 << SIGPIPE) |
| 860 | + (1 << SIGALRM) |
| 861 | , handler); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 862 | } |
| 863 | static void set_jobctrl_sighandler(void (*handler)(int)) |
| 864 | { |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 865 | bb_signals(0 |
| 866 | + (1 << SIGTSTP) |
| 867 | + (1 << SIGTTIN) |
| 868 | + (1 << SIGTTOU) |
| 869 | , handler); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 870 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 871 | /* SIGCHLD is special and handled separately */ |
| 872 | |
| 873 | static void set_every_sighandler(void (*handler)(int)) |
| 874 | { |
| 875 | set_fatal_sighandler(handler); |
| 876 | set_jobctrl_sighandler(handler); |
| 877 | set_misc_sighandler(handler); |
| 878 | signal(SIGCHLD, handler); |
| 879 | } |
| 880 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 881 | static void handler_ctrl_c(int sig UNUSED_PARAM) |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 882 | { |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 883 | debug_printf_jobs("got sig %d\n", sig); |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 884 | // as usual we can have all kinds of nasty problems with leaked malloc data here |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 885 | siglongjmp(G.toplevel_jb, 1); |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 888 | static void handler_ctrl_z(int sig UNUSED_PARAM) |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 889 | { |
| 890 | pid_t pid; |
| 891 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 892 | debug_printf_jobs("got tty sig %d in pid %d\n", sig, getpid()); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 893 | pid = fork(); |
Denis Vlasenko | c299032 | 2007-05-16 12:57:12 +0000 | [diff] [blame] | 894 | if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */ |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 895 | return; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 896 | G.ctrl_z_flag = 1; |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 897 | if (!pid) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 898 | if (ENABLE_HUSH_JOB) |
| 899 | die_sleep = 0; /* let nofork's xfuncs die */ |
Bernhard Reutner-Fischer | 864329d | 2008-09-25 10:55:05 +0000 | [diff] [blame] | 900 | bb_setpgrp(); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 901 | debug_printf_jobs("set pgrp for child %d ok\n", getpid()); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 902 | set_every_sighandler(SIG_DFL); |
Denis Vlasenko | 18e19f2 | 2007-04-28 16:43:18 +0000 | [diff] [blame] | 903 | raise(SIGTSTP); /* resend TSTP so that child will be stopped */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 904 | debug_printf_jobs("returning in child\n"); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 905 | /* return to nofork, it will eventually exit now, |
| 906 | * not return back to shell */ |
| 907 | return; |
| 908 | } |
| 909 | /* parent */ |
| 910 | /* finish filling up pipe info */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 911 | G.toplevel_list->pgrp = pid; /* child is in its own pgrp */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 912 | G.toplevel_list->cmds[0].pid = pid; |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 913 | /* parent needs to longjmp out of running nofork. |
| 914 | * we will "return" exitcode 0, with child put in background */ |
| 915 | // as usual we can have all kinds of nasty problems with leaked malloc data here |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 916 | debug_printf_jobs("siglongjmp in parent\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 917 | siglongjmp(G.toplevel_jb, 1); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 920 | /* Restores tty foreground process group, and exits. |
| 921 | * May be called as signal handler for fatal signal |
| 922 | * (will faithfully resend signal to itself, producing correct exit state) |
| 923 | * or called directly with -EXITCODE. |
| 924 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 925 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 926 | static void sigexit(int sig) |
| 927 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 928 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 929 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 930 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 931 | #if ENABLE_HUSH_INTERACTIVE |
| 932 | if (G.interactive_fd) |
| 933 | tcsetpgrp(G.interactive_fd, G.saved_tty_pgrp); |
| 934 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 935 | |
| 936 | /* Not a signal, just exit */ |
| 937 | if (sig <= 0) |
| 938 | _exit(- sig); |
| 939 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 940 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /* Restores tty foreground process group, and exits. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 944 | static void hush_exit(int exitcode) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 945 | static void hush_exit(int exitcode) |
| 946 | { |
| 947 | fflush(NULL); /* flush all streams */ |
| 948 | sigexit(- (exitcode & 0xff)); |
| 949 | } |
| 950 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 951 | #else /* !JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 952 | |
| 953 | #define set_fatal_sighandler(handler) ((void)0) |
| 954 | #define set_jobctrl_sighandler(handler) ((void)0) |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 955 | #define hush_exit(e) exit(e) |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 956 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 957 | #endif /* JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 958 | |
| 959 | |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 960 | static const char *set_cwd(void) |
| 961 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 962 | if (G.cwd == bb_msg_unknown) |
| 963 | G.cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */ |
| 964 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 965 | if (!G.cwd) |
| 966 | G.cwd = bb_msg_unknown; |
| 967 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 970 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 971 | /* |
| 972 | * o_string support |
| 973 | */ |
| 974 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 975 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 976 | static void o_reset(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 977 | { |
| 978 | o->length = 0; |
| 979 | o->nonnull = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 980 | if (o->data) |
| 981 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 984 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 985 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 986 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 987 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 990 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 991 | { |
| 992 | if (o->length + len > o->maxlen) { |
| 993 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 994 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 995 | } |
| 996 | } |
| 997 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 998 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 999 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1000 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1001 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1002 | o->data[o->length] = ch; |
| 1003 | o->length++; |
| 1004 | o->data[o->length] = '\0'; |
| 1005 | } |
| 1006 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1007 | static void o_addstr(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1008 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1009 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1010 | memcpy(&o->data[o->length], str, len); |
| 1011 | o->length += len; |
| 1012 | o->data[o->length] = '\0'; |
| 1013 | } |
| 1014 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1015 | static void o_addstr_duplicate_backslash(o_string *o, const char *str, int len) |
| 1016 | { |
| 1017 | while (len) { |
| 1018 | o_addchr(o, *str); |
| 1019 | if (*str++ == '\\' |
| 1020 | && (*str != '*' && *str != '?' && *str != '[') |
| 1021 | ) { |
| 1022 | o_addchr(o, '\\'); |
| 1023 | } |
| 1024 | len--; |
| 1025 | } |
| 1026 | } |
| 1027 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1028 | /* My analysis of quoting semantics tells me that state information |
| 1029 | * is associated with a destination, not a source. |
| 1030 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1031 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1032 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1033 | int sz = 1; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1034 | char *found = strchr("*?[\\", ch); |
| 1035 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1036 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1037 | o_grow_by(o, sz); |
| 1038 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1039 | o->data[o->length] = '\\'; |
| 1040 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1041 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1042 | o->data[o->length] = ch; |
| 1043 | o->length++; |
| 1044 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1047 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1048 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1049 | int sz = 1; |
| 1050 | if (o->o_quote && strchr("*?[\\", ch)) { |
| 1051 | sz++; |
| 1052 | o->data[o->length] = '\\'; |
| 1053 | o->length++; |
| 1054 | } |
| 1055 | o_grow_by(o, sz); |
| 1056 | o->data[o->length] = ch; |
| 1057 | o->length++; |
| 1058 | o->data[o->length] = '\0'; |
| 1059 | } |
| 1060 | |
| 1061 | static void o_addQstr(o_string *o, const char *str, int len) |
| 1062 | { |
| 1063 | if (!o->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1064 | o_addstr(o, str, len); |
| 1065 | return; |
| 1066 | } |
| 1067 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1068 | char ch; |
| 1069 | int sz; |
| 1070 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 1071 | if (ordinary_cnt > len) /* paranoia */ |
| 1072 | ordinary_cnt = len; |
| 1073 | o_addstr(o, str, ordinary_cnt); |
| 1074 | if (ordinary_cnt == len) |
| 1075 | return; |
| 1076 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1077 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1078 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1079 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1080 | sz = 1; |
| 1081 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 1082 | sz++; |
| 1083 | o->data[o->length] = '\\'; |
| 1084 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1085 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1086 | o_grow_by(o, sz); |
| 1087 | o->data[o->length] = ch; |
| 1088 | o->length++; |
| 1089 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1093 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1094 | * 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] | 1095 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1096 | * list[i] contains an INDEX (int!) into this string data. |
| 1097 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1098 | * but list[i]'s need not be modified. |
| 1099 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1100 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1101 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1102 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1103 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1104 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1105 | { |
| 1106 | char **list = (char**)o->data; |
| 1107 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1108 | int i = 0; |
| 1109 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1110 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1111 | while (i < n) { |
| 1112 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1113 | o->data + (int)list[i] + string_start, |
| 1114 | o->data + (int)list[i] + string_start); |
| 1115 | i++; |
| 1116 | } |
| 1117 | if (n) { |
| 1118 | const char *p = o->data + (int)list[n - 1] + string_start; |
| 1119 | fprintf(stderr, " total_sz:%d\n", (p + strlen(p) + 1) - o->data); |
| 1120 | } |
| 1121 | } |
| 1122 | #else |
| 1123 | #define debug_print_list(prefix, o, n) ((void)0) |
| 1124 | #endif |
| 1125 | |
| 1126 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1127 | * in list[n] so that it points past last stored byte so far. |
| 1128 | * It returns n+1. */ |
| 1129 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1130 | { |
| 1131 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1132 | int string_start; |
| 1133 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1134 | |
| 1135 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1136 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1137 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1138 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1139 | 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] | 1140 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1141 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1142 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1143 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1144 | memmove(list + n + 0x10, list + n, string_len); |
| 1145 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1146 | } else |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1147 | 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] | 1148 | } else { |
| 1149 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1150 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1151 | string_len = o->length - string_start; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1152 | 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] | 1153 | o->has_empty_slot = 0; |
| 1154 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1155 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1156 | return n + 1; |
| 1157 | } |
| 1158 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1159 | /* "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] | 1160 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1161 | { |
| 1162 | char **list = (char**)o->data; |
| 1163 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1164 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1165 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1168 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1169 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1170 | static int o_glob(o_string *o, int n) |
| 1171 | { |
| 1172 | glob_t globdata; |
| 1173 | int gr; |
| 1174 | char *pattern; |
| 1175 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1176 | 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] | 1177 | if (!o->data) |
| 1178 | return o_save_ptr_helper(o, n); |
| 1179 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1180 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1181 | if (!glob_needed(pattern)) { |
| 1182 | literal: |
| 1183 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1184 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1185 | return o_save_ptr_helper(o, n); |
| 1186 | } |
| 1187 | |
| 1188 | memset(&globdata, 0, sizeof(globdata)); |
| 1189 | gr = glob(pattern, 0, NULL, &globdata); |
| 1190 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 1191 | if (gr == GLOB_NOSPACE) |
| 1192 | bb_error_msg_and_die("out of memory during glob"); |
| 1193 | if (gr == GLOB_NOMATCH) { |
| 1194 | globfree(&globdata); |
| 1195 | goto literal; |
| 1196 | } |
| 1197 | if (gr != 0) { /* GLOB_ABORTED ? */ |
| 1198 | //TODO: testcase for bad glob pattern behavior |
| 1199 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 1200 | } |
| 1201 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 1202 | char **argv = globdata.gl_pathv; |
| 1203 | o->length = pattern - o->data; /* "forget" pattern */ |
| 1204 | while (1) { |
| 1205 | o_addstr(o, *argv, strlen(*argv) + 1); |
| 1206 | n = o_save_ptr_helper(o, n); |
| 1207 | argv++; |
| 1208 | if (!*argv) |
| 1209 | break; |
| 1210 | } |
| 1211 | } |
| 1212 | globfree(&globdata); |
| 1213 | if (DEBUG_GLOB) |
| 1214 | debug_print_list("o_glob returning", o, n); |
| 1215 | return n; |
| 1216 | } |
| 1217 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1218 | /* If o->o_glob == 1, glob the string so far remembered. |
| 1219 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1220 | static int o_save_ptr(o_string *o, int n) |
| 1221 | { |
| 1222 | if (o->o_glob) |
| 1223 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 1224 | return o_save_ptr_helper(o, n); |
| 1225 | } |
| 1226 | |
| 1227 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1228 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1229 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1230 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1231 | int string_start; |
| 1232 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1233 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 1234 | if (DEBUG_EXPAND) |
| 1235 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1236 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1237 | list = (char**)o->data; |
| 1238 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1239 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1240 | while (n) { |
| 1241 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1242 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1243 | } |
| 1244 | return list; |
| 1245 | } |
| 1246 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1247 | |
| 1248 | /* |
| 1249 | * in_str support |
| 1250 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1251 | static int static_get(struct in_str *i) |
| 1252 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1253 | int ch = *i->p++; |
| 1254 | if (ch == '\0') return EOF; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1255 | return ch; |
| 1256 | } |
| 1257 | |
| 1258 | static int static_peek(struct in_str *i) |
| 1259 | { |
| 1260 | return *i->p; |
| 1261 | } |
| 1262 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1263 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1264 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1265 | #if ENABLE_FEATURE_EDITING |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 1266 | static void cmdedit_set_initial_prompt(void) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1267 | { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1268 | #if !ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1269 | G.PS1 = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1270 | #else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1271 | G.PS1 = getenv("PS1"); |
| 1272 | if (G.PS1 == NULL) |
| 1273 | G.PS1 = "\\w \\$ "; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1274 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1275 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1276 | #endif /* EDITING */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1277 | |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1278 | static const char* setup_prompt_string(int promptmode) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1279 | { |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1280 | const char *prompt_str; |
| 1281 | debug_printf("setup_prompt_string %d ", promptmode); |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1282 | #if !ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1283 | /* Set up the prompt */ |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1284 | if (promptmode == 0) { /* PS1 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1285 | free((char*)G.PS1); |
| 1286 | G.PS1 = xasprintf("%s %c ", G.cwd, (geteuid() != 0) ? '$' : '#'); |
| 1287 | prompt_str = G.PS1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1288 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1289 | prompt_str = G.PS2; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1290 | } |
| 1291 | #else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1292 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Eric Andersen | af44a0e | 2001-04-27 07:26:12 +0000 | [diff] [blame] | 1293 | #endif |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1294 | debug_printf("result '%s'\n", prompt_str); |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1295 | return prompt_str; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1298 | static void get_user_input(struct in_str *i) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1299 | { |
Denis Vlasenko | 0937be5 | 2007-04-28 16:47:08 +0000 | [diff] [blame] | 1300 | int r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1301 | const char *prompt_str; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1302 | |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1303 | prompt_str = setup_prompt_string(i->promptmode); |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1304 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1305 | /* Enable command line editing only while a command line |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 1306 | * is actually being read */ |
| 1307 | do { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1308 | r = read_line_input(prompt_str, G.user_input_buf, BUFSIZ-1, G.line_input_state); |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 1309 | } while (r == 0); /* repeat if Ctrl-C */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1310 | i->eof_flag = (r < 0); |
| 1311 | if (i->eof_flag) { /* EOF/error detected */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1312 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1313 | G.user_input_buf[1] = '\0'; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1314 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1315 | #else |
| 1316 | fputs(prompt_str, stdout); |
| 1317 | fflush(stdout); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1318 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1319 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1320 | i->eof_flag = (r == EOF); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1321 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1322 | i->p = G.user_input_buf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1323 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1324 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1325 | #endif /* INTERACTIVE */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1326 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1327 | /* This is the magic location that prints prompts |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1328 | * and gets data back from the user */ |
| 1329 | static int file_get(struct in_str *i) |
| 1330 | { |
| 1331 | int ch; |
| 1332 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1333 | /* If there is data waiting, eat it up */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1334 | if (i->p && *i->p) { |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1335 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1336 | take_cached: |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1337 | #endif |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1338 | ch = *i->p++; |
| 1339 | if (i->eof_flag && !*i->p) |
| 1340 | ch = EOF; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1341 | } else { |
| 1342 | /* need to double check i->file because we might be doing something |
| 1343 | * more complicated by now, like sourcing or substituting. */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1344 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1345 | if (G.interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1346 | do { |
| 1347 | get_user_input(i); |
| 1348 | } while (!*i->p); /* need non-empty line */ |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1349 | i->promptmode = 1; /* PS2 */ |
| 1350 | i->promptme = 0; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1351 | goto take_cached; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1352 | } |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1353 | #endif |
| 1354 | ch = fgetc(i->file); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1355 | } |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1356 | debug_printf("file_get: got a '%c' %d\n", ch, ch); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1357 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1358 | if (ch == '\n') |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1359 | i->promptme = 1; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1360 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1361 | return ch; |
| 1362 | } |
| 1363 | |
| 1364 | /* All the callers guarantee this routine will never be |
| 1365 | * used right after a newline, so prompting is not needed. |
| 1366 | */ |
| 1367 | static int file_peek(struct in_str *i) |
| 1368 | { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 1369 | int ch; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1370 | if (i->p && *i->p) { |
| 1371 | if (i->eof_flag && !i->p[1]) |
| 1372 | return EOF; |
| 1373 | return *i->p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1374 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 1375 | ch = fgetc(i->file); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1376 | i->eof_flag = (ch == EOF); |
| 1377 | i->peek_buf[0] = ch; |
| 1378 | i->peek_buf[1] = '\0'; |
| 1379 | i->p = i->peek_buf; |
| 1380 | debug_printf("file_peek: got a '%c' %d\n", *i->p, *i->p); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 1381 | return ch; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1385 | { |
| 1386 | i->peek = file_peek; |
| 1387 | i->get = file_get; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1388 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1389 | i->promptme = 1; |
| 1390 | i->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1391 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1392 | i->file = f; |
| 1393 | i->p = NULL; |
| 1394 | } |
| 1395 | |
| 1396 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1397 | { |
| 1398 | i->peek = static_peek; |
| 1399 | i->get = static_get; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1400 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1401 | i->promptme = 1; |
| 1402 | i->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1403 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1404 | i->p = s; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1405 | i->eof_flag = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1408 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1409 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 1410 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1411 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1412 | { |
| 1413 | int openfd, mode; |
| 1414 | struct redir_struct *redir; |
| 1415 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1416 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1417 | if (redir->dup == -1 && redir->rd_filename == NULL) { |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 1418 | /* something went wrong in the parse. Pretend it didn't happen */ |
| 1419 | continue; |
| 1420 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1421 | if (redir->dup == -1) { |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1422 | char *p; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1423 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 1424 | //TODO: check redir for names like '\\' |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1425 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1426 | openfd = open_or_warn(p, mode); |
| 1427 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1428 | if (openfd < 0) { |
| 1429 | /* this could get lost if stderr has been redirected, but |
| 1430 | bash and ash both lose it as well (though zsh doesn't!) */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1431 | return 1; |
| 1432 | } |
| 1433 | } else { |
| 1434 | openfd = redir->dup; |
| 1435 | } |
| 1436 | |
| 1437 | if (openfd != redir->fd) { |
| 1438 | if (squirrel && redir->fd < 3) { |
| 1439 | squirrel[redir->fd] = dup(redir->fd); |
| 1440 | } |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1441 | if (openfd == -3) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1442 | //close(openfd); // close(-3) ??! |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1443 | } else { |
| 1444 | dup2(openfd, redir->fd); |
Matt Kraai | c616e53 | 2001-06-05 16:50:08 +0000 | [diff] [blame] | 1445 | if (redir->dup == -1) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1446 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1447 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1448 | } |
| 1449 | } |
| 1450 | return 0; |
| 1451 | } |
| 1452 | |
| 1453 | static void restore_redirects(int squirrel[]) |
| 1454 | { |
| 1455 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1456 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1457 | fd = squirrel[i]; |
| 1458 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 1459 | /* We simply die on error */ |
| 1460 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 1465 | static char **expand_assignments(char **argv, int count) |
| 1466 | { |
| 1467 | int i; |
| 1468 | char **p = NULL; |
| 1469 | /* Expand assignments into one string each */ |
| 1470 | for (i = 0; i < count; i++) { |
| 1471 | p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 1472 | } |
| 1473 | return p; |
| 1474 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1475 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1476 | /* Called after [v]fork() in run_pipe(), or from builtin_exec(). |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1477 | * Never returns. |
| 1478 | * XXX no exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1479 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1480 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 1481 | 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] | 1482 | { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 1483 | int rcode; |
| 1484 | char **new_env; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 1485 | const struct built_in_command *x; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1486 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1487 | /* If a variable is assigned in a forest, and nobody listens, |
| 1488 | * was it ever really set? |
| 1489 | */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1490 | if (!argv[assignment_cnt]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1491 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1492 | |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 1493 | new_env = expand_assignments(argv, assignment_cnt); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 1494 | #if BB_MMU |
| 1495 | putenv_all(new_env); |
| 1496 | free(new_env); /* optional */ |
| 1497 | #else |
| 1498 | nommu_save->new_env = new_env; |
| 1499 | nommu_save->old_env = putenv_all_and_save_old(new_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1500 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1501 | if (argv_expanded) { |
| 1502 | argv = argv_expanded; |
| 1503 | } else { |
| 1504 | argv = expand_strvec_to_strvec(argv); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1505 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 1506 | nommu_save->argv = argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1507 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1508 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 1509 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1510 | /* |
| 1511 | * Check if the command matches any of the builtins. |
| 1512 | * Depending on context, this might be redundant. But it's |
| 1513 | * easier to waste a few CPU cycles than it is to figure out |
| 1514 | * if this is one of those cases. |
| 1515 | */ |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 1516 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1517 | if (strcmp(argv[0], x->cmd) == 0) { |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 1518 | debug_printf_exec("running builtin '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1519 | rcode = x->function(argv); |
| 1520 | fflush(stdout); |
| 1521 | _exit(rcode); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1522 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1523 | } |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1524 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1525 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 1526 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1527 | if (strchr(argv[0], '/') == NULL) { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 1528 | int a = find_applet_by_name(argv[0]); |
| 1529 | if (a >= 0) { |
| 1530 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1531 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 1532 | // is it ok that run_applet_no_and_exit() does exit(), not _exit()? |
| 1533 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1534 | } |
| 1535 | /* re-exec ourselves with the new arguments */ |
| 1536 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | bdbbb7e | 2007-06-08 15:02:55 +0000 | [diff] [blame] | 1537 | execvp(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1538 | /* If they called chroot or otherwise made the binary no longer |
| 1539 | * executable, fall through */ |
| 1540 | } |
| 1541 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 1542 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1543 | |
| 1544 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1545 | execvp(argv[0], argv); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 1546 | bb_perror_msg("can't exec '%s'", argv[0]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 1547 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1550 | /* Called after [v]fork() in run_pipe() |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1551 | */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 1552 | 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] | 1553 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1554 | if (command->argv) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 1555 | pseudo_exec_argv(nommu_save, command->argv, command->assignment_cnt, argv_expanded); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1556 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1557 | if (command->group) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1558 | #if !BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 1559 | bb_error_msg_and_die("nested lists are not supported on NOMMU"); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1560 | #else |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 1561 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1562 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1563 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 1564 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1565 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1566 | _exit(rcode); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1567 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1568 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1569 | |
| 1570 | /* Can happen. See what bash does with ">foo" by itself. */ |
| 1571 | debug_printf("trying to pseudo_exec null command\n"); |
| 1572 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1575 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1576 | static const char *get_cmdtext(struct pipe *pi) |
| 1577 | { |
| 1578 | char **argv; |
| 1579 | char *p; |
| 1580 | int len; |
| 1581 | |
| 1582 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 1583 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1584 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1585 | if (pi->cmdtext) |
| 1586 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1587 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 1588 | if (!argv || !argv[0]) { |
| 1589 | pi->cmdtext = xzalloc(1); |
| 1590 | return pi->cmdtext; |
| 1591 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1592 | |
| 1593 | len = 0; |
| 1594 | do len += strlen(*argv) + 1; while (*++argv); |
| 1595 | pi->cmdtext = p = xmalloc(len); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1596 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1597 | do { |
| 1598 | len = strlen(*argv); |
| 1599 | memcpy(p, *argv, len); |
| 1600 | p += len; |
| 1601 | *p++ = ' '; |
| 1602 | } while (*++argv); |
| 1603 | p[-1] = '\0'; |
| 1604 | return pi->cmdtext; |
| 1605 | } |
| 1606 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1607 | static void insert_bg_job(struct pipe *pi) |
| 1608 | { |
| 1609 | struct pipe *thejob; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1610 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1611 | |
| 1612 | /* Linear search for the ID of the job to use */ |
| 1613 | pi->jobid = 1; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1614 | for (thejob = G.job_list; thejob; thejob = thejob->next) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1615 | if (thejob->jobid >= pi->jobid) |
| 1616 | pi->jobid = thejob->jobid + 1; |
| 1617 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1618 | /* Add thejob to the list of running jobs */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1619 | if (!G.job_list) { |
| 1620 | thejob = G.job_list = xmalloc(sizeof(*thejob)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1621 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1622 | for (thejob = G.job_list; thejob->next; thejob = thejob->next) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1623 | continue; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1624 | thejob->next = xmalloc(sizeof(*thejob)); |
| 1625 | thejob = thejob->next; |
| 1626 | } |
| 1627 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1628 | /* Physically copy the struct job */ |
Eric Andersen | 0fcd447 | 2001-05-02 20:12:03 +0000 | [diff] [blame] | 1629 | memcpy(thejob, pi, sizeof(struct pipe)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1630 | thejob->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 1631 | /* We cannot copy entire pi->cmds[] vector! Double free()s will happen */ |
| 1632 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1633 | // TODO: do we really need to have so many fields which are just dead weight |
| 1634 | // at execution stage? |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1635 | thejob->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1636 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1637 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1638 | thejob->next = NULL; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1639 | thejob->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1640 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1641 | /* We don't wait for background thejobs to return -- append it |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1642 | to the list of backgrounded thejobs and leave it alone */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1643 | printf("[%d] %d %s\n", thejob->jobid, thejob->cmds[0].pid, thejob->cmdtext); |
| 1644 | G.last_bg_pid = thejob->cmds[0].pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1645 | G.last_jobid = thejob->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1648 | static void remove_bg_job(struct pipe *pi) |
| 1649 | { |
| 1650 | struct pipe *prev_pipe; |
| 1651 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1652 | if (pi == G.job_list) { |
| 1653 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1654 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1655 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1656 | while (prev_pipe->next != pi) |
| 1657 | prev_pipe = prev_pipe->next; |
| 1658 | prev_pipe->next = pi->next; |
| 1659 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1660 | if (G.job_list) |
| 1661 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 1662 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1663 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1664 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 1665 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1666 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1667 | static void delete_finished_bg_job(struct pipe *pi) |
| 1668 | { |
| 1669 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1670 | pi->stopped_cmds = 0; |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 1671 | free_pipe(pi, 0); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1672 | free(pi); |
| 1673 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1674 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1675 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1676 | /* Check to see if any processes have exited -- if they |
| 1677 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1678 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1679 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1680 | int attributes; |
| 1681 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1682 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1683 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1684 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1685 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1686 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1687 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1688 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1689 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1690 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1691 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1692 | /* Do we do this right? |
| 1693 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1694 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1695 | * [3]+ Stopped sleep 20 | false |
| 1696 | * bash-3.00# echo $? |
| 1697 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 1698 | */ |
| 1699 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1700 | //FIXME: non-interactive bash does not continue even if all processes in fg pipe |
| 1701 | //are stopped. Testcase: "cat | cat" in a script (not on command line) |
| 1702 | // + killall -STOP cat |
| 1703 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1704 | wait_more: |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 1705 | // TODO: safe_waitpid? |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1706 | while ((childpid = waitpid(-1, &status, attributes)) > 0) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1707 | int i; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1708 | const int dead = WIFEXITED(status) || WIFSIGNALED(status); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1709 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1710 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1711 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1712 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 1713 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1714 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1715 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 1716 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1717 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1718 | childpid, WEXITSTATUS(status)); |
| 1719 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1720 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1721 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1722 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 1723 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 1724 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1725 | continue; |
| 1726 | /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */ |
| 1727 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1728 | fg_pipe->cmds[i].pid = 0; |
| 1729 | fg_pipe->alive_cmds--; |
| 1730 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1731 | /* last process gives overall exitstatus */ |
| 1732 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 1733 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1734 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1735 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1736 | fg_pipe->cmds[i].is_stopped = 1; |
| 1737 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1738 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1739 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 1740 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 1741 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1742 | /* All processes in fg pipe have exited/stopped */ |
| 1743 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1744 | if (fg_pipe->alive_cmds) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1745 | insert_bg_job(fg_pipe); |
| 1746 | #endif |
| 1747 | return rcode; |
| 1748 | } |
| 1749 | /* There are still running processes in the fg pipe */ |
| 1750 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1751 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1752 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1755 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1756 | /* We asked to wait for bg or orphaned children */ |
| 1757 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1758 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1759 | for (i = 0; i < pi->num_cmds; i++) { |
| 1760 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 1761 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1762 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1763 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 1764 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 1765 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1766 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 1767 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 1768 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1769 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1770 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1771 | pi->cmds[i].pid = 0; |
| 1772 | pi->alive_cmds--; |
| 1773 | if (!pi->alive_cmds) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1774 | printf(JOB_STATUS_FORMAT, pi->jobid, |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1775 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1776 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1777 | } |
| 1778 | } else { |
| 1779 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1780 | pi->cmds[i].is_stopped = 1; |
| 1781 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1782 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1783 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1784 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1785 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1786 | /* wait found no children or failed */ |
| 1787 | |
| 1788 | if (childpid && errno != ECHILD) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1789 | bb_perror_msg("waitpid"); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1790 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1793 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1794 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 1795 | { |
| 1796 | pid_t p; |
| 1797 | int rcode = checkjobs(fg_pipe); |
| 1798 | /* Job finished, move the shell to the foreground */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1799 | p = getpgid(0); /* pgid of our process */ |
| 1800 | debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1801 | tcsetpgrp(G.interactive_fd, p); |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1802 | return rcode; |
| 1803 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1804 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1805 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1806 | /* run_pipe() starts all the jobs, but doesn't wait for anything |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1807 | * to finish. See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1808 | * |
| 1809 | * return code is normally -1, when the caller has to wait for children |
| 1810 | * to finish to determine the exit status of the pipe. If the pipe |
| 1811 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1812 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1813 | * return value. |
| 1814 | * |
| 1815 | * The input of the pipe is always stdin, the output is always |
| 1816 | * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus, |
| 1817 | * because it tries to avoid running the command substitution in |
| 1818 | * subshell, when that is in fact necessary. The subshell process |
| 1819 | * now has its stdout directed to the input of the appropriate pipe, |
| 1820 | * so this routine is noticeably simpler. |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1821 | * |
| 1822 | * Returns -1 only if started some children. IOW: we have to |
| 1823 | * mask out retvals of builtins etc with 0xff! |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1824 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1825 | static int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1826 | { |
| 1827 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1828 | int nextin; |
| 1829 | int pipefds[2]; /* pipefds[0] is for reading */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1830 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1831 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1832 | char **argv; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 1833 | const struct built_in_command *x; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 1834 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1835 | /* it is not always needed, but we aim to smaller code */ |
| 1836 | int squirrel[] = { -1, -1, -1 }; |
| 1837 | int rcode; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1838 | 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] | 1839 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1840 | 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] | 1841 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1842 | #if ENABLE_HUSH_JOB |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 1843 | pi->pgrp = -1; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1844 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1845 | pi->alive_cmds = 1; |
| 1846 | pi->stopped_cmds = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1847 | |
| 1848 | /* Check if this is a simple builtin (not part of a pipe). |
| 1849 | * Builtins within pipes have to fork anyway, and are handled in |
| 1850 | * pseudo_exec. "echo foo | read bar" doesn't work on bash, either. |
| 1851 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1852 | command = &(pi->cmds[0]); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1853 | |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 1854 | #if ENABLE_HUSH_FUNCTIONS |
| 1855 | if (single_and_fg && command->group && command->grp_type == GRP_FUNCTION) { |
| 1856 | /* We "execute" function definition */ |
| 1857 | bb_error_msg("here we ought to remember function definition, and go on"); |
| 1858 | return EXIT_SUCCESS; |
| 1859 | } |
| 1860 | #endif |
| 1861 | |
| 1862 | if (single_and_fg && command->group && command->grp_type == GRP_NORMAL) { |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 1863 | debug_printf("non-subshell grouping\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1864 | setup_redirects(command, squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1865 | debug_printf_exec(": run_list\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1866 | rcode = run_list(command->group) & 0xff; |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 1867 | restore_redirects(squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1868 | debug_printf_exec("run_pipe return %d\n", rcode); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 1869 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1870 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1873 | argv = command->argv; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1874 | argv_expanded = NULL; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1875 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1876 | if (single_and_fg && argv != NULL) { |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1877 | char **new_env = NULL; |
| 1878 | char **old_env = NULL; |
| 1879 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1880 | i = command->assignment_cnt; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1881 | if (i != 0 && argv[i] == NULL) { |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1882 | /* assignments, but no command: set local environment */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1883 | for (i = 0; argv[i] != NULL; i++) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 1884 | debug_printf("local environment set: %s\n", argv[i]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1885 | p = expand_string_to_string(argv[i]); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 1886 | set_local_var(p, 0); |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1887 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1888 | 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] | 1889 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1890 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1891 | /* Expand the rest into (possibly) many strings each */ |
| 1892 | argv_expanded = expand_strvec_to_strvec(argv + i); |
| 1893 | |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 1894 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1895 | if (strcmp(argv_expanded[0], x->cmd) != 0) |
| 1896 | continue; |
| 1897 | if (x->function == builtin_exec && argv_expanded[1] == NULL) { |
| 1898 | debug_printf("exec with redirects only\n"); |
| 1899 | setup_redirects(command, NULL); |
| 1900 | rcode = EXIT_SUCCESS; |
| 1901 | goto clean_up_and_ret1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1902 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1903 | debug_printf("builtin inline %s\n", argv_expanded[0]); |
| 1904 | /* XXX setup_redirects acts on file descriptors, not FILEs. |
| 1905 | * This is perfect for work that comes after exec(). |
| 1906 | * Is it really safe for inline use? Experimentally, |
| 1907 | * things seem to work with glibc. */ |
| 1908 | setup_redirects(command, squirrel); |
| 1909 | new_env = expand_assignments(argv, command->assignment_cnt); |
| 1910 | old_env = putenv_all_and_save_old(new_env); |
| 1911 | debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv_expanded[1]); |
| 1912 | rcode = x->function(argv_expanded) & 0xff; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 1913 | #if ENABLE_FEATURE_SH_STANDALONE |
| 1914 | clean_up_and_ret: |
| 1915 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1916 | restore_redirects(squirrel); |
| 1917 | free_strings_and_unsetenv(new_env, 1); |
| 1918 | putenv_all(old_env); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 1919 | free(old_env); /* not free_strings()! */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1920 | clean_up_and_ret1: |
| 1921 | free(argv_expanded); |
| 1922 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 1923 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 1924 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1925 | } |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1926 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1927 | i = find_applet_by_name(argv_expanded[0]); |
| 1928 | if (i >= 0 && APPLET_IS_NOFORK(i)) { |
| 1929 | setup_redirects(command, squirrel); |
| 1930 | save_nofork_data(&G.nofork_save); |
| 1931 | new_env = expand_assignments(argv, command->assignment_cnt); |
| 1932 | old_env = putenv_all_and_save_old(new_env); |
| 1933 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]); |
| 1934 | rcode = run_nofork_applet_prime(&G.nofork_save, i, argv_expanded); |
| 1935 | goto clean_up_and_ret; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1936 | } |
| 1937 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1940 | /* NB: argv_expanded may already be created, and that |
| 1941 | * might include `cmd` runs! Do not rerun it! We *must* |
| 1942 | * use argv_expanded if it's non-NULL */ |
| 1943 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1944 | /* Disable job control signals for shell (parent) and |
| 1945 | * for initial child code after fork */ |
| 1946 | set_jobctrl_sighandler(SIG_IGN); |
| 1947 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1948 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1949 | pi->alive_cmds = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1950 | nextin = 0; |
| 1951 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1952 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1953 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 1954 | volatile nommu_save_t nommu_save; |
| 1955 | nommu_save.new_env = NULL; |
| 1956 | nommu_save.old_env = NULL; |
| 1957 | nommu_save.argv = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1958 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1959 | command = &(pi->cmds[i]); |
| 1960 | if (command->argv) { |
| 1961 | 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] | 1962 | } else |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1963 | debug_printf_exec(": pipe member with no argv\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1964 | |
| 1965 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1966 | pipefds[0] = 0; |
| 1967 | pipefds[1] = 1; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1968 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1969 | xpipe(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1970 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1971 | command->pid = BB_MMU ? fork() : vfork(); |
| 1972 | if (!command->pid) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 1973 | if (ENABLE_HUSH_JOB) |
| 1974 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1975 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1976 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1977 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1978 | if (G.run_list_level == 1 && G.interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1979 | pid_t pgrp; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1980 | /* Don't do pgrp restore anymore on fatal signals */ |
| 1981 | set_fatal_sighandler(SIG_DFL); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1982 | pgrp = pi->pgrp; |
| 1983 | if (pgrp < 0) /* true for 1st process only */ |
| 1984 | pgrp = getpid(); |
| 1985 | if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1986 | /* We do it in *every* child, not just first, |
| 1987 | * to avoid races */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1988 | tcsetpgrp(G.interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1989 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1990 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1991 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1992 | xmove_fd(nextin, 0); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1993 | xmove_fd(pipefds[1], 1); /* write end */ |
| 1994 | if (pipefds[0] > 1) |
| 1995 | close(pipefds[0]); /* read end */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1996 | /* Like bash, explicit redirects override pipes, |
| 1997 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1998 | setup_redirects(command, NULL); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1999 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2000 | /* Restore default handlers just prior to exec */ |
| 2001 | set_jobctrl_sighandler(SIG_DFL); |
| 2002 | set_misc_sighandler(SIG_DFL); |
| 2003 | signal(SIGCHLD, SIG_DFL); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2004 | /* Stores to nommu_save list of env vars putenv'ed |
| 2005 | * (NOMMU, on MMU we don't need that) */ |
| 2006 | /* cast away volatility... */ |
| 2007 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2008 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2009 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2010 | /* parent */ |
| 2011 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2012 | /* Clean up after vforked child */ |
| 2013 | free(nommu_save.argv); |
| 2014 | free_strings_and_unsetenv(nommu_save.new_env, 1); |
| 2015 | putenv_all(nommu_save.old_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 2016 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 2017 | free(argv_expanded); |
| 2018 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2019 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2020 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 2021 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2022 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2023 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2024 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2025 | /* Second and next children need to know pid of first one */ |
| 2026 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2027 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2028 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2029 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2030 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2031 | if (i) |
| 2032 | close(nextin); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2033 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2034 | close(pipefds[1]); /* write end */ |
| 2035 | /* Pass read (output) pipe end to next iteration */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2036 | nextin = pipefds[0]; |
| 2037 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2038 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2039 | if (!pi->alive_cmds) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2040 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 2041 | return 1; |
| 2042 | } |
| 2043 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2044 | 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] | 2045 | return -1; |
| 2046 | } |
| 2047 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 2048 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2049 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 2050 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2051 | static const char *const PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2052 | [PIPE_SEQ] = "SEQ", |
| 2053 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2054 | [PIPE_OR ] = "OR" , |
| 2055 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2056 | }; |
| 2057 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2058 | [RES_NONE ] = "NONE" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2059 | #if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2060 | [RES_IF ] = "IF" , |
| 2061 | [RES_THEN ] = "THEN" , |
| 2062 | [RES_ELIF ] = "ELIF" , |
| 2063 | [RES_ELSE ] = "ELSE" , |
| 2064 | [RES_FI ] = "FI" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2065 | #endif |
| 2066 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2067 | [RES_FOR ] = "FOR" , |
| 2068 | [RES_WHILE] = "WHILE", |
| 2069 | [RES_UNTIL] = "UNTIL", |
| 2070 | [RES_DO ] = "DO" , |
| 2071 | [RES_DONE ] = "DONE" , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 2072 | #endif |
| 2073 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2074 | [RES_IN ] = "IN" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2075 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2076 | #if ENABLE_HUSH_CASE |
| 2077 | [RES_CASE ] = "CASE" , |
| 2078 | [RES_MATCH] = "MATCH", |
| 2079 | [RES_CASEI] = "CASEI", |
| 2080 | [RES_ESAC ] = "ESAC" , |
| 2081 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2082 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2083 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2084 | }; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2085 | static const char *const GRPTYPE[] = { |
| 2086 | "()", |
| 2087 | "{}", |
| 2088 | #if ENABLE_HUSH_FUNCTIONS |
| 2089 | "func()", |
| 2090 | #endif |
| 2091 | }; |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2092 | |
| 2093 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2094 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2095 | pin = 0; |
| 2096 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2097 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 2098 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2099 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2100 | while (prn < pi->num_cmds) { |
| 2101 | struct command *command = &pi->cmds[prn]; |
| 2102 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2103 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2104 | fprintf(stderr, "%*s prog %d assignment_cnt:%d", lvl*2, "", prn, command->assignment_cnt); |
| 2105 | if (command->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2106 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2107 | GRPTYPE[command->grp_type], |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2108 | argv); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2109 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2110 | prn++; |
| 2111 | continue; |
| 2112 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2113 | if (argv) while (*argv) { |
| 2114 | fprintf(stderr, " '%s'", *argv); |
| 2115 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 2116 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2117 | fprintf(stderr, "\n"); |
| 2118 | prn++; |
| 2119 | } |
| 2120 | pi = pi->next; |
| 2121 | pin++; |
| 2122 | } |
| 2123 | } |
| 2124 | #endif |
| 2125 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2126 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 2127 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2128 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2129 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2130 | #if ENABLE_HUSH_CASE |
| 2131 | char *case_word = NULL; |
| 2132 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2133 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2134 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2135 | char *for_varname = NULL; |
| 2136 | char **for_lcur = NULL; |
| 2137 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2138 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2139 | smallint flag_skip = 1; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2140 | smalluint rcode = 0; /* probably just for compiler */ |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 2141 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2142 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2143 | #else |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2144 | enum { cond_code = 0, }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2145 | #endif |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2146 | /*enum reserved_style*/ smallint rword = RES_NONE; |
| 2147 | /*enum reserved_style*/ smallint skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 2148 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2149 | 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] | 2150 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2151 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2152 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2153 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 2154 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2155 | continue; |
| 2156 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2157 | if (cpipe->next == NULL) { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2158 | syntax("malformed for"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2159 | 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] | 2160 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2161 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2162 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2163 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2164 | continue; |
| 2165 | /* 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] | 2166 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 2167 | || 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] | 2168 | ) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2169 | syntax("malformed for"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2170 | 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] | 2171 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2172 | } |
| 2173 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2174 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2175 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2176 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 2177 | * in order to return, no direct "return" statements please. |
| 2178 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2179 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2180 | #if ENABLE_HUSH_JOB |
| 2181 | /* Example of nested list: "while true; do { sleep 1 | exit 2; } done". |
| 2182 | * We are saving state before entering outermost list ("while...done") |
| 2183 | * so that ctrl-Z will correctly background _entire_ outermost list, |
| 2184 | * not just a part of it (like "sleep 1 | exit 2") */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2185 | if (++G.run_list_level == 1 && G.interactive_fd) { |
| 2186 | if (sigsetjmp(G.toplevel_jb, 1)) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2187 | /* ctrl-Z forked and we are parent; or ctrl-C. |
| 2188 | * Sighandler has longjmped us here */ |
| 2189 | signal(SIGINT, SIG_IGN); |
| 2190 | signal(SIGTSTP, SIG_IGN); |
| 2191 | /* Restore level (we can be coming from deep inside |
| 2192 | * nested levels) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2193 | G.run_list_level = 1; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2194 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2195 | if (G.nofork_save.saved) { /* if save area is valid */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2196 | debug_printf_jobs("exiting nofork early\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2197 | restore_nofork_data(&G.nofork_save); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2198 | } |
| 2199 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2200 | if (G.ctrl_z_flag) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2201 | /* ctrl-Z has forked and stored pid of the child in pi->pid. |
| 2202 | * Remember this child as background job */ |
| 2203 | insert_bg_job(pi); |
| 2204 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2205 | /* ctrl-C. We just stop doing whatever we were doing */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 2206 | bb_putchar('\n'); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2207 | } |
Denis Vlasenko | ff29b4f | 2008-07-29 13:57:59 +0000 | [diff] [blame] | 2208 | USE_HUSH_LOOPS(loop_top = NULL;) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2209 | USE_HUSH_LOOPS(G.depth_of_loop = 0;) |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2210 | rcode = 0; |
| 2211 | goto ret; |
| 2212 | } |
| 2213 | /* ctrl-Z handler will store pid etc in pi */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2214 | G.toplevel_list = pi; |
| 2215 | G.ctrl_z_flag = 0; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2216 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2217 | G.nofork_save.saved = 0; /* in case we will run a nofork later */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2218 | #endif |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 2219 | signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2220 | signal(SIGINT, handler_ctrl_c); |
| 2221 | } |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2222 | #endif /* JOB */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2223 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2224 | /* Go through list of pipes, (maybe) executing them. */ |
| 2225 | for (; pi; pi = USE_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2226 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 2227 | IF_HAS_NO_KEYWORDS(rword = RES_NONE;) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2228 | debug_printf_exec(": rword=%d cond_code=%d skip_more=%d\n", |
| 2229 | rword, cond_code, skip_more_for_this_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2230 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 2231 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2232 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 2233 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2234 | /* start of a loop: remember where loop starts */ |
| 2235 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2236 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2237 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2238 | #endif |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2239 | if (rword == skip_more_for_this_rword && flag_skip) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2240 | if (pi->followup == PIPE_SEQ) |
| 2241 | flag_skip = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2242 | /* it is "<false> && CMD" or "<true> || CMD" |
| 2243 | * and we should not execute CMD */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2244 | continue; |
| 2245 | } |
| 2246 | flag_skip = 1; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2247 | skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2248 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2249 | if (cond_code) { |
| 2250 | if (rword == RES_THEN) { |
| 2251 | /* "if <false> THEN cmd": skip cmd */ |
| 2252 | continue; |
| 2253 | } |
| 2254 | } else { |
| 2255 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 2256 | /* "if <true> then ... ELSE/ELIF cmd": |
| 2257 | * skip cmd and all following ones */ |
| 2258 | break; |
| 2259 | } |
| 2260 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2261 | #endif |
| 2262 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2263 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2264 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2265 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2266 | |
| 2267 | static const char encoded_dollar_at[] ALIGN1 = { |
| 2268 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 2269 | }; /* encoded representation of "$@" */ |
| 2270 | static const char *const encoded_dollar_at_argv[] = { |
| 2271 | encoded_dollar_at, NULL |
| 2272 | }; /* argv list with one element: "$@" */ |
| 2273 | char **vals; |
| 2274 | |
| 2275 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2276 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2277 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2278 | if (!pi->next->cmds[0].argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2279 | break; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2280 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2281 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2282 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2283 | debug_print_strings("for_list made from", vals); |
| 2284 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2285 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2286 | debug_print_strings("for_list", for_list); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2287 | for_varname = pi->cmds[0].argv[0]; |
| 2288 | pi->cmds[0].argv[0] = NULL; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2289 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2290 | free(pi->cmds[0].argv[0]); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2291 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 2292 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2293 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2294 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2295 | for_lcur = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2296 | pi->cmds[0].argv[0] = for_varname; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2297 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2298 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2299 | /* insert next value from for_lcur */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2300 | //TODO: does it need escaping? |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2301 | pi->cmds[0].argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++); |
| 2302 | pi->cmds[0].assignment_cnt = 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2303 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2304 | 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] | 2305 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2306 | if (rword == RES_DONE) { |
| 2307 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2308 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2309 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2310 | #if ENABLE_HUSH_CASE |
| 2311 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2312 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2313 | continue; |
| 2314 | } |
| 2315 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 2316 | char **argv; |
| 2317 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2318 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 2319 | break; |
| 2320 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2321 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 2322 | while (*argv) { |
| 2323 | char *pattern = expand_string_to_string(*argv); |
| 2324 | /* TODO: which FNM_xxx flags to use? */ |
| 2325 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 2326 | free(pattern); |
| 2327 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 2328 | free(case_word); /* make future "word)" stop */ |
| 2329 | case_word = NULL; |
| 2330 | break; |
| 2331 | } |
| 2332 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2333 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2334 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2335 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2336 | if (rword == RES_CASEI) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2337 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2338 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2339 | } |
| 2340 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2341 | if (pi->num_cmds == 0) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2342 | continue; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2343 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2344 | /* After analyzing all keywords and conditions, we decided |
| 2345 | * to execute this pipe. NB: has to do checkjobs(NULL) |
| 2346 | * after run_pipe() to collect any background children, |
| 2347 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2348 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2349 | { |
| 2350 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2351 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2352 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2353 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2354 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 2355 | if (r != -1) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2356 | /* we only ran a builtin: rcode is already known |
| 2357 | * and we don't need to wait for anything. */ |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2358 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2359 | /* was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2360 | if (G.flag_break_continue) { |
| 2361 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2362 | /* we might fall into outer *loop*, |
| 2363 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2364 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2365 | G.depth_break_continue--; |
| 2366 | if (G.depth_break_continue == 0) |
| 2367 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2368 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 2369 | } /* 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] | 2370 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2371 | goto check_jobs_and_break; |
| 2372 | /* "continue": simulate end of loop */ |
| 2373 | rword = RES_DONE; |
| 2374 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2375 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2376 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2377 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2378 | /* what does bash do with attempts to background builtins? */ |
| 2379 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2380 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 2381 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2382 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2383 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2384 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2385 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2386 | rcode = 0; /* EXIT_SUCCESS */ |
| 2387 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2388 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2389 | if (G.run_list_level == 1 && G.interactive_fd) { |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2390 | /* waits for completion, then fg's main shell */ |
| 2391 | rcode = checkjobs_and_fg_shell(pi); |
| 2392 | debug_printf_exec(": checkjobs_and_fg_shell returned %d\n", rcode); |
| 2393 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2394 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2395 | { /* this one just waits for completion */ |
| 2396 | rcode = checkjobs(pi); |
| 2397 | debug_printf_exec(": checkjobs returned %d\n", rcode); |
| 2398 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2399 | } |
| 2400 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2401 | debug_printf_exec(": setting last_return_code=%d\n", rcode); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2402 | G.last_return_code = rcode; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2403 | |
| 2404 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2405 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2406 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2407 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2408 | #endif |
| 2409 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2410 | if (rword == RES_WHILE) { |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 2411 | if (rcode) { |
| 2412 | rcode = 0; /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2413 | goto check_jobs_and_break; |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 2414 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2415 | } |
| 2416 | if (rword == RES_UNTIL) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2417 | if (!rcode) { |
| 2418 | check_jobs_and_break: |
| 2419 | checkjobs(NULL); |
| 2420 | break; |
| 2421 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2422 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2423 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2424 | if ((rcode == 0 && pi->followup == PIPE_OR) |
| 2425 | || (rcode != 0 && pi->followup == PIPE_AND) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2426 | ) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2427 | skip_more_for_this_rword = rword; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2428 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 2429 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2430 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2431 | |
| 2432 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2433 | if (G.ctrl_z_flag) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2434 | /* ctrl-Z forked somewhere in the past, we are the child, |
| 2435 | * and now we completed running the list. Exit. */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2436 | //TODO: _exit? |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2437 | exit(rcode); |
| 2438 | } |
| 2439 | ret: |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2440 | if (!--G.run_list_level && G.interactive_fd) { |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 2441 | signal(SIGTSTP, SIG_IGN); |
| 2442 | signal(SIGINT, SIG_IGN); |
| 2443 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2444 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2445 | 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] | 2446 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2447 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2448 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2449 | free(for_list); |
| 2450 | #endif |
| 2451 | #if ENABLE_HUSH_CASE |
| 2452 | free(case_word); |
| 2453 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2454 | return rcode; |
| 2455 | } |
| 2456 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2457 | /* return code is the exit status of the pipe */ |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2458 | static int free_pipe(struct pipe *pi, int indent) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2459 | { |
| 2460 | char **p; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2461 | struct command *command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2462 | struct redir_struct *r, *rnext; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2463 | int a, i, ret_code = 0; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 2464 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2465 | if (pi->stopped_cmds > 0) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 2466 | return ret_code; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2467 | debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid()); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2468 | for (i = 0; i < pi->num_cmds; i++) { |
| 2469 | command = &pi->cmds[i]; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2470 | debug_printf_clean("%s command %d:\n", indenter(indent), i); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2471 | if (command->argv) { |
| 2472 | for (a = 0, p = command->argv; *p; a++, p++) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2473 | debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2474 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2475 | free_strings(command->argv); |
| 2476 | command->argv = NULL; |
| 2477 | } else if (command->group) { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2478 | debug_printf_clean("%s begin group (grp_type:%d)\n", indenter(indent), command->grp_type); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2479 | ret_code = free_pipe_list(command->group, indent+3); |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2480 | debug_printf_clean("%s end group\n", indenter(indent)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2481 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2482 | debug_printf_clean("%s (nil)\n", indenter(indent)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2483 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2484 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 211b59b | 2008-06-23 16:28:53 +0000 | [diff] [blame] | 2485 | debug_printf_clean("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->rd_type].descrip); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2486 | if (r->dup == -1) { |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2487 | /* guard against the case >$FOO, where foo is unset or blank */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2488 | if (r->rd_filename) { |
| 2489 | debug_printf_clean(" %s\n", r->rd_filename); |
| 2490 | free(r->rd_filename); |
| 2491 | r->rd_filename = NULL; |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2492 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2493 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2494 | debug_printf_clean("&%d\n", r->dup); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2495 | } |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2496 | rnext = r->next; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2497 | free(r); |
| 2498 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2499 | command->redirects = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2500 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2501 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 2502 | pi->cmds = NULL; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2503 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2504 | free(pi->cmdtext); |
| 2505 | pi->cmdtext = NULL; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2506 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2507 | return ret_code; |
| 2508 | } |
| 2509 | |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2510 | static int free_pipe_list(struct pipe *head, int indent) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2511 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2512 | int rcode = 0; /* if list has no members */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2513 | struct pipe *pi, *next; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2514 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2515 | for (pi = head; pi; pi = next) { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2516 | #if HAS_KEYWORDS |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2517 | debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2518 | #endif |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2519 | rcode = free_pipe(pi, indent); |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2520 | debug_printf_clean("%s pipe followup code %d\n", indenter(indent), pi->followup); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2521 | next = pi->next; |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2522 | /*pi->next = NULL;*/ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2523 | free(pi); |
| 2524 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2525 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
| 2528 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2529 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2530 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2531 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2532 | debug_printf_exec("run_and_free_list entered\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2533 | if (!G.fake_mode) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2534 | debug_printf_exec(": run_list with %d members\n", pi->num_cmds); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2535 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2536 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2537 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2538 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2539 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2540 | free_pipe_list(pi, /* indent: */ 0); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2541 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2542 | return rcode; |
| 2543 | } |
| 2544 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2545 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2546 | /* expand_strvec_to_strvec() takes a list of strings, expands |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2547 | * all variable references within and returns a pointer to |
| 2548 | * a list of expanded strings, possibly with larger number |
| 2549 | * of strings. (Think VAR="a b"; echo $VAR). |
| 2550 | * This new list is allocated as a single malloc block. |
| 2551 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 2552 | * followed by strings themself. |
| 2553 | * Caller can deallocate entire list by single free(list). */ |
| 2554 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2555 | /* Store given string, finalizing the word and starting new one whenever |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2556 | * we encounter IFS char(s). This is used for expanding variable values. |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2557 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2558 | static int expand_on_ifs(o_string *output, int n, const char *str) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2559 | { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2560 | while (1) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2561 | int word_len = strcspn(str, G.ifs); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2562 | if (word_len) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2563 | if (output->o_quote || !output->o_glob) |
| 2564 | o_addQstr(output, str, word_len); |
| 2565 | else /* protect backslashes against globbing up :) */ |
| 2566 | o_addstr_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2567 | str += word_len; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2568 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2569 | if (!*str) /* EOL - do not finalize word */ |
| 2570 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2571 | o_addchr(output, '\0'); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2572 | debug_print_list("expand_on_ifs", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2573 | n = o_save_ptr(output, n); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2574 | str += strspn(str, G.ifs); /* skip ifs chars */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2575 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2576 | debug_print_list("expand_on_ifs[1]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2577 | return n; |
| 2578 | } |
| 2579 | |
| 2580 | /* Expand all variable references in given string, adding words to list[] |
| 2581 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 2582 | * to be filled). This routine is extremely tricky: has to deal with |
| 2583 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 2584 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2585 | static int expand_vars_to_list(o_string *output, int n, char *arg, char or_mask) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2586 | { |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2587 | /* or_mask is either 0 (normal case) or 0x80 |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2588 | * (expansion of right-hand side of assignment == 1-element expand. |
| 2589 | * It will also do no globbing, and thus we must not backslash-quote!) */ |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2590 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2591 | char first_ch, ored_ch; |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2592 | int i; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2593 | const char *val; |
| 2594 | char *p; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2595 | |
| 2596 | ored_ch = 0; |
| 2597 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2598 | debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2599 | debug_print_list("expand_vars_to_list", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2600 | n = o_save_ptr(output, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2601 | debug_print_list("expand_vars_to_list[0]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2602 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2603 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2604 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2605 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2606 | #endif |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2607 | o_addstr(output, arg, p - arg); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2608 | debug_print_list("expand_vars_to_list[1]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2609 | arg = ++p; |
| 2610 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2611 | |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2612 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 2613 | /* "$@" is special. Even if quoted, it can still |
| 2614 | * expand to nothing (not even an empty string) */ |
| 2615 | if ((first_ch & 0x7f) != '@') |
| 2616 | ored_ch |= first_ch; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2617 | val = NULL; |
| 2618 | switch (first_ch & 0x7f) { |
| 2619 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 2620 | case '$': /* pid */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2621 | val = utoa(G.root_pid); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2622 | break; |
| 2623 | case '!': /* bg pid */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2624 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2625 | break; |
| 2626 | case '?': /* exitcode */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2627 | val = utoa(G.last_return_code); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2628 | break; |
| 2629 | case '#': /* argc */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2630 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2631 | break; |
| 2632 | case '*': |
| 2633 | case '@': |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2634 | i = 1; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2635 | if (!G.global_argv[i]) |
Denis Vlasenko | c3c6659 | 2007-11-24 00:22:42 +0000 | [diff] [blame] | 2636 | break; |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 2637 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2638 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2639 | smallint sv = output->o_quote; |
| 2640 | /* unquoted var's contents should be globbed, so don't quote */ |
| 2641 | output->o_quote = 0; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2642 | while (G.global_argv[i]) { |
| 2643 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 2644 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 2645 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2646 | /* this argv[] is not empty and not last: |
| 2647 | * put terminating NUL, start new word */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2648 | o_addchr(output, '\0'); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2649 | debug_print_list("expand_vars_to_list[2]", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2650 | n = o_save_ptr(output, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2651 | debug_print_list("expand_vars_to_list[3]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2652 | } |
| 2653 | } |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2654 | output->o_quote = sv; |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2655 | } else |
| 2656 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2657 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2658 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2659 | while (1) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2660 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2661 | if (++i >= G.global_argc) |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2662 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2663 | o_addchr(output, '\0'); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2664 | debug_print_list("expand_vars_to_list[4]", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2665 | n = o_save_ptr(output, n); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2666 | } |
| 2667 | } else { /* quoted $*: add as one word */ |
Denis Vlasenko | c3c6659 | 2007-11-24 00:22:42 +0000 | [diff] [blame] | 2668 | while (1) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2669 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2670 | if (!G.global_argv[++i]) |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2671 | break; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2672 | if (G.ifs[0]) |
| 2673 | o_addchr(output, G.ifs[0]); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2674 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2675 | } |
| 2676 | break; |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2677 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 2678 | /* "Empty variable", used to make "" etc to not disappear */ |
| 2679 | arg++; |
| 2680 | ored_ch = 0x80; |
| 2681 | break; |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2682 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2683 | case '`': { /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2684 | struct in_str input; |
| 2685 | *p = '\0'; |
| 2686 | arg++; |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2687 | //TODO: can we just stuff it into "output" directly? |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2688 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2689 | setup_string_in_str(&input, arg); |
| 2690 | process_command_subs(&subst_result, &input, NULL); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2691 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2692 | val = subst_result.data; |
| 2693 | goto store_val; |
| 2694 | } |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2695 | #endif |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2696 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2697 | *p = '\0'; |
| 2698 | arg[0] = first_ch & 0x7f; |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2699 | if (isdigit(arg[0])) { |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2700 | i = xatoi_u(arg); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2701 | if (i < G.global_argc) |
| 2702 | val = G.global_argv[i]; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2703 | /* else val remains NULL: $N with too big N */ |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2704 | } else |
| 2705 | val = lookup_param(arg); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2706 | arg[0] = first_ch; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2707 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2708 | store_val: |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2709 | #endif |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2710 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2711 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2712 | debug_printf_expand("unquoted '%s', output->o_quote:%d\n", val, output->o_quote); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2713 | if (val) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2714 | /* unquoted var's contents should be globbed, so don't quote */ |
| 2715 | smallint sv = output->o_quote; |
| 2716 | output->o_quote = 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2717 | n = expand_on_ifs(output, n, val); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2718 | val = NULL; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2719 | output->o_quote = sv; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2720 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2721 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2722 | debug_printf_expand("quoted '%s', output->o_quote:%d\n", val, output->o_quote); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2723 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2724 | } |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2725 | if (val) { |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2726 | o_addQstr(output, val, strlen(val)); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2727 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2728 | |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2729 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2730 | o_free(&subst_result); |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2731 | #endif |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2732 | arg = ++p; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2733 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 2734 | |
Denis Vlasenko | 2e76c3f | 2008-06-10 18:27:50 +0000 | [diff] [blame] | 2735 | if (arg[0]) { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2736 | debug_print_list("expand_vars_to_list[a]", output, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2737 | /* this part is literal, and it was already pre-quoted |
| 2738 | * if needed (much earlier), do not use o_addQstr here! */ |
| 2739 | o_addstr(output, arg, strlen(arg) + 1); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2740 | debug_print_list("expand_vars_to_list[b]", output, n); |
Denis Vlasenko | 2e76c3f | 2008-06-10 18:27:50 +0000 | [diff] [blame] | 2741 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 2742 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 2743 | ) { |
| 2744 | n--; |
| 2745 | /* allow to reuse list[n] later without re-growth */ |
| 2746 | output->has_empty_slot = 1; |
| 2747 | } else { |
| 2748 | o_addchr(output, '\0'); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2749 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2750 | return n; |
| 2751 | } |
| 2752 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2753 | static char **expand_variables(char **argv, int or_mask) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2754 | { |
| 2755 | int n; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2756 | char **list; |
| 2757 | char **v; |
| 2758 | o_string output = NULL_O_STRING; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2759 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2760 | if (or_mask & 0x100) { |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2761 | output.o_quote = 1; /* protect against globbing for "$var" */ |
| 2762 | /* (unquoted $var will temporarily switch it off) */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2763 | output.o_glob = 1; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2764 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2765 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2766 | n = 0; |
| 2767 | v = argv; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2768 | while (*v) { |
| 2769 | n = expand_vars_to_list(&output, n, *v, (char)or_mask); |
| 2770 | v++; |
| 2771 | } |
| 2772 | debug_print_list("expand_variables", &output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2773 | |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 2774 | /* output.data (malloced in one block) gets returned in "list" */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2775 | list = o_finalize_list(&output, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2776 | debug_print_strings("expand_variables[1]", list); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2777 | return list; |
| 2778 | } |
| 2779 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2780 | static char **expand_strvec_to_strvec(char **argv) |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2781 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2782 | return expand_variables(argv, 0x100); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2783 | } |
| 2784 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2785 | /* Used for expansion of right hand of assignments */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 2786 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 2787 | * "v=/bin/c*" */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2788 | static char *expand_string_to_string(const char *str) |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2789 | { |
| 2790 | char *argv[2], **list; |
| 2791 | |
| 2792 | argv[0] = (char*)str; |
| 2793 | argv[1] = NULL; |
| 2794 | list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2795 | if (HUSH_DEBUG) |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2796 | if (!list[0] || list[1]) |
| 2797 | bb_error_msg_and_die("BUG in varexp2"); |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2798 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 2799 | strcpy((char*)list, list[0]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2800 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 2801 | return (char*)list; |
| 2802 | } |
| 2803 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2804 | /* Used for "eval" builtin */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2805 | static char* expand_strvec_to_string(char **argv) |
| 2806 | { |
| 2807 | char **list; |
| 2808 | |
| 2809 | list = expand_variables(argv, 0x80); |
| 2810 | /* Convert all NULs to spaces */ |
| 2811 | if (list[0]) { |
| 2812 | int n = 1; |
| 2813 | while (list[n]) { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2814 | if (HUSH_DEBUG) |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2815 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 2816 | bb_error_msg_and_die("BUG in varexp3"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2817 | list[n][-1] = ' '; /* TODO: or to G.ifs[0]? */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2818 | n++; |
| 2819 | } |
| 2820 | } |
| 2821 | strcpy((char*)list, list[0]); |
| 2822 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2823 | return (char*)list; |
| 2824 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2825 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2826 | |
| 2827 | /* Used to get/check local shell variables */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2828 | static struct variable *get_local_var(const char *name) |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2829 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2830 | struct variable *cur; |
| 2831 | int len; |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2832 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2833 | if (!name) |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2834 | return NULL; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2835 | len = strlen(name); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2836 | for (cur = G.top_var; cur; cur = cur->next) { |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2837 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2838 | return cur; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 2839 | } |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2840 | return NULL; |
| 2841 | } |
| 2842 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2843 | /* str holds "NAME=VAL" and is expected to be malloced. |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2844 | * We take ownership of it. */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2845 | static int set_local_var(char *str, int flg_export) |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2846 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2847 | struct variable *cur; |
| 2848 | char *value; |
| 2849 | int name_len; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2850 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2851 | value = strchr(str, '='); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2852 | if (!value) { /* not expected to ever happen? */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2853 | free(str); |
Eric Andersen | 9978576 | 2001-05-22 21:37:48 +0000 | [diff] [blame] | 2854 | return -1; |
| 2855 | } |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2856 | |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2857 | name_len = value - str + 1; /* including '=' */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2858 | cur = G.top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2859 | while (1) { |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2860 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2861 | if (!cur->next) { |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2862 | /* Bail out. Note that now cur points |
| 2863 | * to last var in linked list */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2864 | break; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2865 | } |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2866 | cur = cur->next; |
| 2867 | continue; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2868 | } |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2869 | /* We found an existing var with this name */ |
| 2870 | *value = '\0'; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2871 | if (cur->flg_read_only) { |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2872 | bb_error_msg("%s: readonly variable", str); |
| 2873 | free(str); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2874 | return -1; |
| 2875 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2876 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2877 | unsetenv(str); /* just in case */ |
| 2878 | *value = '='; |
| 2879 | if (strcmp(cur->varstr, str) == 0) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2880 | free_and_exp: |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2881 | free(str); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2882 | goto exp; |
| 2883 | } |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2884 | if (cur->max_len >= strlen(str)) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2885 | /* This one is from startup env, reuse space */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2886 | strcpy(cur->varstr, str); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2887 | goto free_and_exp; |
| 2888 | } |
| 2889 | /* max_len == 0 signifies "malloced" var, which we can |
| 2890 | * (and has to) free */ |
| 2891 | if (!cur->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2892 | free(cur->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2893 | cur->max_len = 0; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2894 | goto set_str_and_exp; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2895 | } |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2896 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2897 | /* Not found - create next variable struct */ |
| 2898 | cur->next = xzalloc(sizeof(*cur)); |
| 2899 | cur = cur->next; |
| 2900 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2901 | set_str_and_exp: |
| 2902 | cur->varstr = str; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2903 | exp: |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2904 | if (flg_export) |
| 2905 | cur->flg_export = 1; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2906 | if (cur->flg_export) { |
| 2907 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2908 | return putenv(cur->varstr); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2909 | } |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2910 | return 0; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2913 | static void unset_local_var(const char *name) |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2914 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2915 | struct variable *cur; |
| 2916 | struct variable *prev = prev; /* for gcc */ |
| 2917 | int name_len; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2918 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2919 | if (!name) |
| 2920 | return; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2921 | name_len = strlen(name); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2922 | cur = G.top_var; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2923 | while (cur) { |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2924 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2925 | if (cur->flg_read_only) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 2926 | bb_error_msg("%s: readonly variable", name); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2927 | return; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2928 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2929 | /* prev is ok to use here because 1st variable, HUSH_VERSION, |
| 2930 | * is ro, and we cannot reach this code on the 1st pass */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2931 | prev->next = cur->next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2932 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 2933 | bb_unsetenv(cur->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2934 | if (!cur->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2935 | free(cur->varstr); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2936 | free(cur); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2937 | return; |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2938 | } |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2939 | prev = cur; |
| 2940 | cur = cur->next; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2941 | } |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2944 | /* 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] | 2945 | * for file descriptor duplication, e.g., "2>&1". |
| 2946 | * Return code is 0 normally, 1 if a syntax error is detected in src. |
| 2947 | * Resource errors (in xmalloc) cause the process to exit */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2948 | 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] | 2949 | struct in_str *input) |
| 2950 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2951 | struct command *command = ctx->command; |
| 2952 | struct redir_struct *redir = command->redirects; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2953 | struct redir_struct *last_redir = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2954 | |
| 2955 | /* 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] | 2956 | while (redir) { |
| 2957 | last_redir = redir; |
| 2958 | redir = redir->next; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2959 | } |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 2960 | redir = xzalloc(sizeof(struct redir_struct)); |
| 2961 | /* redir->next = NULL; */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2962 | /* redir->rd_filename = NULL; */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2963 | if (last_redir) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2964 | last_redir->next = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2965 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2966 | command->redirects = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2967 | } |
| 2968 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2969 | redir->rd_type = style; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2970 | redir->fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2971 | |
| 2972 | debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip); |
| 2973 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2974 | /* Check for a '2>&1' type redirect */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2975 | redir->dup = redirect_dup_num(input); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2976 | if (redir->dup == -2) |
| 2977 | return 1; /* syntax error */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2978 | if (redir->dup != -1) { |
| 2979 | /* Erik had a check here that the file descriptor in question |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2980 | * is legit; I postpone that to "run time" |
| 2981 | * A "-" representation of "close me" shows up as a -3 here */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2982 | debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup); |
| 2983 | } else { |
| 2984 | /* We do _not_ try to open the file that src points to, |
| 2985 | * since we need to return and let src be expanded first. |
| 2986 | * Set ctx->pending_redirect, so we know what to do at the |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2987 | * end of the next parsed word. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2988 | ctx->pending_redirect = redir; |
| 2989 | } |
| 2990 | return 0; |
| 2991 | } |
| 2992 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 2993 | static struct pipe *new_pipe(void) |
| 2994 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2995 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2996 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2997 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2998 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2999 | return pi; |
| 3000 | } |
| 3001 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3002 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3003 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3004 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3005 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3006 | /* Create the memory for command, roughly: |
| 3007 | * ctx->pipe->cmds = new struct command; |
| 3008 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3009 | */ |
| 3010 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3013 | /* If a reserved word is found and processed, parse context is modified |
| 3014 | * and 1 is returned. |
| 3015 | * Handles if, then, elif, else, fi, for, while, until, do, done. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3016 | * case, function, and select are obnoxious, save those for later. |
| 3017 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3018 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3019 | struct reserved_combo { |
| 3020 | char literal[6]; |
| 3021 | unsigned char res; |
| 3022 | unsigned char assignment_flag; |
| 3023 | int flag; |
| 3024 | }; |
| 3025 | enum { |
| 3026 | FLAG_END = (1 << RES_NONE ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3027 | #if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3028 | FLAG_IF = (1 << RES_IF ), |
| 3029 | FLAG_THEN = (1 << RES_THEN ), |
| 3030 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3031 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3032 | FLAG_FI = (1 << RES_FI ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3033 | #endif |
| 3034 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3035 | FLAG_FOR = (1 << RES_FOR ), |
| 3036 | FLAG_WHILE = (1 << RES_WHILE), |
| 3037 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3038 | FLAG_DO = (1 << RES_DO ), |
| 3039 | FLAG_DONE = (1 << RES_DONE ), |
| 3040 | FLAG_IN = (1 << RES_IN ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3041 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3042 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3043 | FLAG_MATCH = (1 << RES_MATCH), |
| 3044 | FLAG_ESAC = (1 << RES_ESAC ), |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3045 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3046 | FLAG_START = (1 << RES_XXXX ), |
| 3047 | }; |
| 3048 | |
| 3049 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3050 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3051 | /* Mostly a list of accepted follow-up reserved words. |
| 3052 | * FLAG_END means we are done with the sequence, and are ready |
| 3053 | * to turn the compound list into a command. |
| 3054 | * FLAG_START means the word must start a new compound list. |
| 3055 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3056 | static const struct reserved_combo reserved_list[] = { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3057 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3058 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3059 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 3060 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3061 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 3062 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 3063 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3064 | #endif |
| 3065 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3066 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3067 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 3068 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 3069 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3070 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 3071 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3072 | #endif |
| 3073 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3074 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3075 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3076 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3077 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3078 | const struct reserved_combo *r; |
| 3079 | |
| 3080 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 3081 | if (strcmp(word->data, r->literal) == 0) |
| 3082 | return r; |
| 3083 | } |
| 3084 | return NULL; |
| 3085 | } |
| 3086 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 3087 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3088 | #if ENABLE_HUSH_CASE |
| 3089 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3090 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3091 | }; |
| 3092 | #endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3093 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3094 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3095 | r = match_reserved_word(word); |
| 3096 | if (!r) |
| 3097 | return 0; |
| 3098 | |
| 3099 | 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] | 3100 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3101 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE) |
| 3102 | /* "case word IN ..." - IN part starts first match part */ |
| 3103 | r = &reserved_match; |
| 3104 | else |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3105 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3106 | if (r->flag == 0) { /* '!' */ |
| 3107 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3108 | syntax(NULL); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3109 | IF_HAS_KEYWORDS(ctx->ctx_res_w = RES_SNTX;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3110 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3111 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3112 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3113 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3114 | if (r->flag & FLAG_START) { |
| 3115 | struct parse_context *new; |
| 3116 | debug_printf("push stack\n"); |
| 3117 | new = xmalloc(sizeof(*new)); |
| 3118 | *new = *ctx; /* physical copy */ |
| 3119 | initialize_context(ctx); |
| 3120 | ctx->stack = new; |
| 3121 | } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) { |
| 3122 | syntax(NULL); |
| 3123 | ctx->ctx_res_w = RES_SNTX; |
| 3124 | return 1; |
| 3125 | } |
| 3126 | ctx->ctx_res_w = r->res; |
| 3127 | ctx->old_flag = r->flag; |
| 3128 | if (ctx->old_flag & FLAG_END) { |
| 3129 | struct parse_context *old; |
| 3130 | debug_printf("pop stack\n"); |
| 3131 | done_pipe(ctx, PIPE_SEQ); |
| 3132 | old = ctx->stack; |
| 3133 | old->command->group = ctx->list_head; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3134 | old->command->grp_type = GRP_NORMAL; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3135 | *ctx = *old; /* physical copy */ |
| 3136 | free(old); |
| 3137 | } |
| 3138 | word->o_assignment = r->assignment_flag; |
| 3139 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3140 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3141 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3142 | |
Denis Vlasenko | ddc8ae3 | 2008-10-14 12:50:34 +0000 | [diff] [blame] | 3143 | //TODO: many, many callers don't check error from done_word() |
| 3144 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3145 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3146 | * Normal return is 0. Syntax errors return 1. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3147 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3148 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3149 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3150 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3151 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3152 | if (word->length == 0 && word->nonnull == 0) { |
| 3153 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3154 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3155 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3156 | /* If this word wasn't an assignment, next ones definitely |
| 3157 | * can't be assignments. Even if they look like ones. */ |
| 3158 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3159 | && word->o_assignment != WORD_IS_KEYWORD |
| 3160 | ) { |
| 3161 | word->o_assignment = NOT_ASSIGNMENT; |
| 3162 | } else { |
| 3163 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3164 | command->assignment_cnt++; |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3165 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3166 | } |
| 3167 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3168 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3169 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3170 | * only if run as "bash", not "sh" */ |
| 3171 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3172 | word->o_assignment = NOT_ASSIGNMENT; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3173 | debug_printf("word stored in rd_filename: '%s'\n", word->data); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3174 | } else { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3175 | /* "{ echo foo; } echo bar" - bad */ |
| 3176 | /* NB: bash allows e.g. "if true; then { echo foo; } fi". TODO? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3177 | if (command->group) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3178 | syntax(NULL); |
| 3179 | debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n"); |
| 3180 | return 1; |
| 3181 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3182 | #if HAS_KEYWORDS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3183 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3184 | if (ctx->ctx_dsemicolon |
| 3185 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3186 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3187 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3188 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3189 | ctx->ctx_dsemicolon = 0; |
| 3190 | } else |
| 3191 | #endif |
| 3192 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3193 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3194 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3195 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3196 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3197 | #endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3198 | ) { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3199 | debug_printf_parse(": checking '%s' for reserved-ness\n", word->data); |
| 3200 | if (reserved_word(word, ctx)) { |
| 3201 | o_reset(word); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3202 | debug_printf_parse("done_word return %d\n", (ctx->ctx_res_w == RES_SNTX)); |
| 3203 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3204 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3205 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3206 | #endif |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3207 | 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] | 3208 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3209 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3210 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3211 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3212 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3213 | char *p = word->data; |
| 3214 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3215 | && (p[1] & 0x7f) == '@' |
| 3216 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3217 | ) { |
| 3218 | p += 3; |
| 3219 | } |
| 3220 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3221 | /* saw no "$@", or not only "$@" but some |
| 3222 | * real text is there too */ |
| 3223 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3224 | * e.g. "", $empty"" etc to not disappear */ |
| 3225 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3226 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3227 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3228 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3229 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3230 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3231 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3232 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3233 | o_reset(word); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3234 | ctx->pending_redirect = NULL; |
| 3235 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3236 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3237 | /* Force FOR to have just one word (variable name) */ |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3238 | /* NB: basically, this makes hush see "for v in ..." syntax as if |
| 3239 | * as it is "for v; in ...". FOR and IN become two pipe structs |
| 3240 | * in parse tree. */ |
| 3241 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3242 | //TODO: check that command->argv[0] is a valid variable name! |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3243 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3244 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3245 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3246 | #if ENABLE_HUSH_CASE |
| 3247 | /* Force CASE to have just one word */ |
| 3248 | if (ctx->ctx_res_w == RES_CASE) { |
| 3249 | done_pipe(ctx, PIPE_SEQ); |
| 3250 | } |
| 3251 | #endif |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3252 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3253 | return 0; |
| 3254 | } |
| 3255 | |
Denis Vlasenko | 6eaf8de | 2008-06-17 12:09:21 +0000 | [diff] [blame] | 3256 | /* Command (member of a pipe) is complete. The only possible error here |
| 3257 | * is out of memory, in which case xmalloc exits. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3258 | static int done_command(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3259 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3260 | /* The command is really already in the pipe structure, so |
| 3261 | * advance the pipe counter and make a new, null command. */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3262 | struct pipe *pi = ctx->pipe; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3263 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3264 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3265 | if (command) { |
| 3266 | if (command->group == NULL |
| 3267 | && command->argv == NULL |
| 3268 | && command->redirects == NULL |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3269 | ) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3270 | debug_printf_parse("done_command: skipping null cmd, num_cmds=%d\n", pi->num_cmds); |
| 3271 | return pi->num_cmds; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3272 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3273 | pi->num_cmds++; |
| 3274 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3275 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3276 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3277 | } |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3278 | |
| 3279 | /* Only real trickiness here is that the uncommitted |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3280 | * command structure is not counted in pi->num_cmds. */ |
| 3281 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
| 3282 | command = &pi->cmds[pi->num_cmds]; |
| 3283 | memset(command, 0, sizeof(*command)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3284 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3285 | ctx->command = command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3286 | /* but ctx->pipe and ctx->list_head remain unchanged */ |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3287 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3288 | return pi->num_cmds; /* used only for 0/nonzero check */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3291 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3292 | { |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3293 | int not_null; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3294 | |
| 3295 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3296 | /* Close previous command */ |
| 3297 | not_null = done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3298 | ctx->pipe->followup = type; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3299 | IF_HAS_KEYWORDS(ctx->pipe->pi_inverted = ctx->ctx_inverted;) |
| 3300 | IF_HAS_KEYWORDS(ctx->ctx_inverted = 0;) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3301 | IF_HAS_KEYWORDS(ctx->pipe->res_word = ctx->ctx_res_w;) |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3302 | |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3303 | /* Without this check, even just <enter> on command line generates |
| 3304 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | 6eaf8de | 2008-06-17 12:09:21 +0000 | [diff] [blame] | 3305 | * IOW: it is safe to do it unconditionally. |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3306 | * RES_NONE case is for "for a in; do ..." (empty IN set) |
| 3307 | * to work, possibly other cases too. */ |
| 3308 | if (not_null IF_HAS_KEYWORDS(|| ctx->ctx_res_w != RES_NONE)) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3309 | struct pipe *new_p; |
| 3310 | debug_printf_parse("done_pipe: adding new pipe: " |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3311 | "not_null:%d ctx->ctx_res_w:%d\n", |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3312 | not_null, ctx->ctx_res_w); |
| 3313 | new_p = new_pipe(); |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3314 | ctx->pipe->next = new_p; |
| 3315 | ctx->pipe = new_p; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3316 | ctx->command = NULL; /* needed! */ |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3317 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3318 | * they remain set for commands inside if/while. |
| 3319 | * This is used to control execution. |
| 3320 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3321 | * cases where variable or value happens to match a keyword): |
| 3322 | */ |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3323 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3324 | if (ctx->ctx_res_w == RES_FOR |
| 3325 | || ctx->ctx_res_w == RES_IN) |
| 3326 | ctx->ctx_res_w = RES_NONE; |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3327 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3328 | #if ENABLE_HUSH_CASE |
| 3329 | if (ctx->ctx_res_w == RES_MATCH) |
| 3330 | ctx->ctx_res_w = RES_CASEI; |
| 3331 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3332 | /* Create the memory for command, roughly: |
| 3333 | * ctx->pipe->cmds = new struct command; |
| 3334 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3335 | */ |
| 3336 | done_command(ctx); |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3337 | } |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3338 | debug_printf_parse("done_pipe return\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3339 | } |
| 3340 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3341 | /* Peek ahead in the in_str to find out if we have a "&n" construct, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3342 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3343 | * Return either -2 (syntax error), -1 (no &), or the number found. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3344 | */ |
| 3345 | static int redirect_dup_num(struct in_str *input) |
| 3346 | { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3347 | int ch, d = 0, ok = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3348 | ch = i_peek(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3349 | if (ch != '&') return -1; |
| 3350 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3351 | i_getch(input); /* get the & */ |
| 3352 | ch = i_peek(input); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3353 | if (ch == '-') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3354 | i_getch(input); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3355 | return -3; /* "-" represents "close me" */ |
| 3356 | } |
| 3357 | while (isdigit(ch)) { |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3358 | d = d*10 + (ch-'0'); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3359 | ok = 1; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3360 | i_getch(input); |
| 3361 | ch = i_peek(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3362 | } |
| 3363 | if (ok) return d; |
| 3364 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3365 | bb_error_msg("ambiguous redirect"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3366 | return -2; |
| 3367 | } |
| 3368 | |
| 3369 | /* If a redirect is immediately preceded by a number, that number is |
| 3370 | * supposed to tell which file descriptor to redirect. This routine |
| 3371 | * looks for such preceding numbers. In an ideal world this routine |
| 3372 | * needs to handle all the following classes of redirects... |
| 3373 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3374 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3375 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3376 | * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo |
| 3377 | * A -1 output from this program means no valid number was found, so the |
| 3378 | * caller should use the appropriate default for this redirection. |
| 3379 | */ |
| 3380 | static int redirect_opt_num(o_string *o) |
| 3381 | { |
| 3382 | int num; |
| 3383 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3384 | if (o->length == 0) |
| 3385 | return -1; |
| 3386 | for (num = 0; num < o->length; num++) { |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3387 | if (!isdigit(o->data[num])) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3388 | return -1; |
| 3389 | } |
| 3390 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3391 | num = atoi(o->data); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3392 | o_reset(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3393 | return num; |
| 3394 | } |
| 3395 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3396 | #if ENABLE_HUSH_TICK |
"Vladimir N. Oleynik" | 19c3701 | 2005-09-22 14:33:15 +0000 | [diff] [blame] | 3397 | static FILE *generate_stream_from_list(struct pipe *head) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3398 | { |
| 3399 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3400 | int pid, channel[2]; |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3401 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 3402 | xpipe(channel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3403 | /* *** NOMMU WARNING *** */ |
| 3404 | /* By using vfork here, we suspend parent till child exits or execs. |
| 3405 | * If child will not do it before it fills the pipe, it can block forever |
| 3406 | * in write(STDOUT_FILENO), and parent (shell) will be also stuck. |
Denis Vlasenko | 3fe4f98 | 2008-06-09 16:02:39 +0000 | [diff] [blame] | 3407 | * Try this script: |
| 3408 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >TESTFILE |
| 3409 | * huge=`cat TESTFILE` # will block here forever |
| 3410 | * echo OK |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3411 | */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 3412 | pid = BB_MMU ? fork() : vfork(); |
| 3413 | if (pid < 0) |
| 3414 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3415 | if (pid == 0) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 3416 | if (ENABLE_HUSH_JOB) |
| 3417 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 3418 | close(channel[0]); /* NB: close _first_, then move fd! */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3419 | xmove_fd(channel[1], 1); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3420 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3421 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3422 | G.run_list_level = 1; |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3423 | #endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3424 | /* Process substitution is not considered to be usual |
| 3425 | * 'command execution'. |
| 3426 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. */ |
| 3427 | /* Not needed, we are relying on it being disabled |
| 3428 | * everywhere outside actual command execution. */ |
| 3429 | /*set_jobctrl_sighandler(SIG_IGN);*/ |
| 3430 | set_misc_sighandler(SIG_DFL); |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3431 | /* Freeing 'head' here would break NOMMU. */ |
| 3432 | _exit(run_list(head)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3433 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3434 | close(channel[1]); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3435 | pf = fdopen(channel[0], "r"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3436 | return pf; |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3437 | /* 'head' is freed by the caller */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3440 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3441 | static int process_command_subs(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3442 | struct in_str *input, |
| 3443 | const char *subst_end) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3444 | { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3445 | int retcode, ch, eol_cnt; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3446 | o_string result = NULL_O_STRING; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3447 | struct parse_context inner; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3448 | FILE *p; |
| 3449 | struct in_str pipe_str; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3450 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3451 | initialize_context(&inner); |
| 3452 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3453 | /* Recursion to generate command */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3454 | retcode = parse_stream(&result, &inner, input, subst_end); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3455 | if (retcode != 0) |
| 3456 | return retcode; /* syntax error or EOF */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3457 | done_word(&result, &inner); |
| 3458 | done_pipe(&inner, PIPE_SEQ); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3459 | o_free(&result); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3460 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3461 | p = generate_stream_from_list(inner.list_head); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3462 | if (p == NULL) |
| 3463 | return 1; |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 3464 | close_on_exec_on(fileno(p)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3465 | setup_file_in_str(&pipe_str, p); |
| 3466 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3467 | /* Now send results of command back into original context */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3468 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3469 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3470 | if (ch == '\n') { |
| 3471 | eol_cnt++; |
| 3472 | continue; |
| 3473 | } |
| 3474 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3475 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3476 | eol_cnt--; |
| 3477 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3478 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | debug_printf("done reading from pipe, pclose()ing\n"); |
| 3482 | /* This is the step that wait()s for the child. Should be pretty |
| 3483 | * safe, since we just read an EOF from its stdout. We could try |
Denis Vlasenko | 262d765 | 2007-05-20 21:52:49 +0000 | [diff] [blame] | 3484 | * to do better, by using wait(), and keeping track of background jobs |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3485 | * at the same time. That would be a lot of work, and contrary |
| 3486 | * to the KISS philosophy of this program. */ |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3487 | retcode = fclose(p); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3488 | free_pipe_list(inner.list_head, /* indent: */ 0); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3489 | debug_printf("closed FILE from child, retcode=%d\n", retcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3490 | return retcode; |
| 3491 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3492 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3493 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3494 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3495 | struct in_str *input, int ch) |
| 3496 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3497 | /* dest contains characters seen prior to ( or {. |
| 3498 | * Typically it's empty, but for functions defs, |
| 3499 | * it contains function name (without '()'). */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3500 | int rcode; |
| 3501 | const char *endch = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3502 | struct parse_context sub; |
| 3503 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3504 | |
| 3505 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3506 | #if ENABLE_HUSH_FUNCTIONS |
| 3507 | if (ch == 'F') { /* function definition? */ |
| 3508 | bb_error_msg("aha '%s' is a function, parsing it...", dest->data); |
| 3509 | //command->fname = dest->data; |
| 3510 | command->grp_type = GRP_FUNCTION; |
| 3511 | //TODO: review every o_reset() location... do they handle all o_string fields correctly? |
| 3512 | memset(dest, 0, sizeof(*dest)); |
| 3513 | } |
| 3514 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3515 | if (command->argv /* word [word](... */ |
| 3516 | || dest->length /* word(... */ |
| 3517 | || dest->nonnull /* ""(... */ |
| 3518 | ) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3519 | syntax(NULL); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3520 | debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n"); |
| 3521 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3522 | } |
| 3523 | initialize_context(&sub); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3524 | endch = "}"; |
| 3525 | if (ch == '(') { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3526 | endch = ")"; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3527 | command->grp_type = GRP_SUBSHELL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3528 | } |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3529 | rcode = parse_stream(dest, &sub, input, endch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3530 | if (rcode == 0) { |
| 3531 | done_word(dest, &sub); /* finish off the final word in the subcontext */ |
| 3532 | done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3533 | command->group = sub.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3534 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3535 | debug_printf_parse("parse_group return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3536 | return rcode; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3537 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3538 | } |
| 3539 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3540 | /* Basically useful version until someone wants to get fancier, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3541 | * see the bash man page under "Parameter Expansion" */ |
Denis Vlasenko | 15d78fb | 2007-01-30 22:28:21 +0000 | [diff] [blame] | 3542 | static const char *lookup_param(const char *src) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3543 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3544 | struct variable *var = get_local_var(src); |
| 3545 | if (var) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 3546 | return strchr(var->varstr, '=') + 1; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3547 | return NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3550 | #if ENABLE_HUSH_TICK |
| 3551 | /* Subroutines for copying $(...) and `...` things */ |
| 3552 | static void add_till_backquote(o_string *dest, struct in_str *input); |
| 3553 | /* '...' */ |
| 3554 | static void add_till_single_quote(o_string *dest, struct in_str *input) |
| 3555 | { |
| 3556 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3557 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3558 | if (ch == EOF) |
| 3559 | break; |
| 3560 | if (ch == '\'') |
| 3561 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3562 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3563 | } |
| 3564 | } |
| 3565 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
| 3566 | static void add_till_double_quote(o_string *dest, struct in_str *input) |
| 3567 | { |
| 3568 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3569 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3570 | if (ch == '"') |
| 3571 | break; |
| 3572 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3573 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3574 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3575 | } |
| 3576 | if (ch == EOF) |
| 3577 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3578 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3579 | if (ch == '`') { |
| 3580 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3581 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3582 | continue; |
| 3583 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3584 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3585 | } |
| 3586 | } |
| 3587 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 3588 | * \` quoting. |
| 3589 | * "Within the backquoted style of command substitution, backslash |
| 3590 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 3591 | * The search for the matching backquote shall be satisfied by the first |
| 3592 | * backquote found without a preceding backslash; during this search, |
| 3593 | * if a non-escaped backquote is encountered within a shell comment, |
| 3594 | * a here-document, an embedded command substitution of the $(command) |
| 3595 | * form, or a quoted string, undefined results occur. A single-quoted |
| 3596 | * or double-quoted string that begins, but does not end, within the |
| 3597 | * "`...`" sequence produces undefined results." |
| 3598 | * Example Output |
| 3599 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 3600 | */ |
| 3601 | static void add_till_backquote(o_string *dest, struct in_str *input) |
| 3602 | { |
| 3603 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3604 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3605 | if (ch == '`') |
| 3606 | break; |
| 3607 | if (ch == '\\') { /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3608 | int ch2 = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3609 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3610 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3611 | ch = ch2; |
| 3612 | } |
| 3613 | if (ch == EOF) |
| 3614 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3615 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3616 | } |
| 3617 | } |
| 3618 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 3619 | * quoting and nested ()s. |
| 3620 | * "With the $(command) style of command substitution, all characters |
| 3621 | * following the open parenthesis to the matching closing parenthesis |
| 3622 | * constitute the command. Any valid shell script can be used for command, |
| 3623 | * except a script consisting solely of redirections which produces |
| 3624 | * unspecified results." |
| 3625 | * Example Output |
| 3626 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 3627 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 3628 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 3629 | */ |
| 3630 | static void add_till_closing_curly_brace(o_string *dest, struct in_str *input) |
| 3631 | { |
| 3632 | int count = 0; |
| 3633 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3634 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3635 | if (ch == EOF) |
| 3636 | break; |
| 3637 | if (ch == '(') |
| 3638 | count++; |
| 3639 | if (ch == ')') |
| 3640 | if (--count < 0) |
| 3641 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3642 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3643 | if (ch == '\'') { |
| 3644 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3645 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3646 | continue; |
| 3647 | } |
| 3648 | if (ch == '"') { |
| 3649 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3650 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3651 | continue; |
| 3652 | } |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3653 | if (ch == '\\') { /* \x. Copy verbatim. Important for \(, \) */ |
| 3654 | ch = i_getch(input); |
| 3655 | if (ch == EOF) |
| 3656 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3657 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3658 | continue; |
| 3659 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3660 | } |
| 3661 | } |
| 3662 | #endif /* ENABLE_HUSH_TICK */ |
| 3663 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3664 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3665 | static int handle_dollar(o_string *dest, struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3666 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3667 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3668 | unsigned char quote_mask = dest->o_quote ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3669 | |
| 3670 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3671 | if (isalpha(ch)) { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3672 | i_getch(input); |
| 3673 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3674 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3675 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3676 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3677 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3678 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3679 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3680 | if (!isalnum(ch) && ch != '_') |
| 3681 | break; |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3682 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3683 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3684 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3685 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3686 | make_one_char_var: |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3687 | i_getch(input); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3688 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3689 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3690 | o_addchr(dest, ch | quote_mask); |
| 3691 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3692 | } else switch (ch) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3693 | case '$': /* pid */ |
| 3694 | case '!': /* last bg pid */ |
| 3695 | case '?': /* last exit code */ |
| 3696 | case '#': /* number of args */ |
| 3697 | case '*': /* args */ |
| 3698 | case '@': /* args */ |
| 3699 | goto make_one_char_var; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3700 | case '{': |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3701 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3702 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3703 | /* XXX maybe someone will try to escape the '}' */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3704 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3705 | ch = i_getch(input); |
Denis Vlasenko | b055001 | 2007-05-24 12:18:16 +0000 | [diff] [blame] | 3706 | if (ch == '}') |
| 3707 | break; |
| 3708 | if (!isalnum(ch) && ch != '_') { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3709 | syntax("unterminated ${name}"); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3710 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 3711 | return 1; |
| 3712 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3713 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3714 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3715 | quote_mask = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3716 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3717 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3718 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3719 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3720 | case '(': { |
| 3721 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3722 | i_getch(input); |
| 3723 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3724 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3725 | add_till_closing_curly_brace(dest, input); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3726 | //debug_printf_subst("SUBST RES2 '%s'\n", dest->data + pos); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3727 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3728 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3729 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3730 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3731 | case '_': |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3732 | i_getch(input); |
| 3733 | ch = i_peek(input); |
| 3734 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 3735 | ch = '_'; |
| 3736 | goto make_var; |
| 3737 | } |
| 3738 | /* else: it's $_ */ |
| 3739 | case '-': |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3740 | /* still unhandled, but should be eventually */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3741 | bb_error_msg("unhandled syntax: $%c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3742 | return 1; |
| 3743 | break; |
| 3744 | default: |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3745 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3746 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3747 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3748 | return 0; |
| 3749 | } |
| 3750 | |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3751 | /* Scan input, call done_word() whenever full IFS delimited word was seen. |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3752 | * Call done_pipe if '\n' was seen (and end_trigger != NULL). |
| 3753 | * Return code is 0 if end_trigger char is met, |
| 3754 | * -1 on EOF (but if end_trigger == NULL then return 0), |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3755 | * 1 for syntax error */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3756 | static int parse_stream(o_string *dest, struct parse_context *ctx, |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3757 | struct in_str *input, const char *end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3758 | { |
"Vladimir N. Oleynik" | 4ccd2b4 | 2006-01-31 09:27:48 +0000 | [diff] [blame] | 3759 | int ch, m; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3760 | int redir_fd; |
| 3761 | redir_type redir_style; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3762 | int shadow_quote = dest->o_quote; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3763 | int next; |
| 3764 | |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3765 | /* 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] | 3766 | * 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] | 3767 | * found. When recursing, quote state is passed in via dest->o_quote. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3768 | |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3769 | 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] | 3770 | |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3771 | while (1) { |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3772 | m = CHAR_IFS; |
| 3773 | next = '\0'; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3774 | ch = i_getch(input); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3775 | if (ch != EOF) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3776 | m = G.charmap[ch]; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3777 | if (ch != '\n') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3778 | next = i_peek(input); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3779 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3780 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3781 | debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n", |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3782 | ch, ch, m, dest->o_quote); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3783 | if (m == CHAR_ORDINARY |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3784 | || (m != CHAR_SPECIAL && shadow_quote) |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3785 | ) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3786 | if (ch == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3787 | syntax("unterminated \""); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3788 | debug_printf_parse("parse_stream return 1: unterminated \"\n"); |
| 3789 | return 1; |
| 3790 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3791 | o_addQchr(dest, ch); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3792 | if ((dest->o_assignment == MAYBE_ASSIGNMENT |
| 3793 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3794 | && ch == '=' |
| 3795 | && is_assignment(dest->data) |
| 3796 | ) { |
| 3797 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 3798 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3799 | continue; |
| 3800 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3801 | if (m == CHAR_IFS) { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3802 | if (done_word(dest, ctx)) { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3803 | debug_printf_parse("parse_stream return 1: done_word!=0\n"); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3804 | return 1; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3805 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3806 | if (ch == EOF) |
| 3807 | break; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3808 | /* If we aren't performing a substitution, treat |
| 3809 | * a newline as a command separator. |
| 3810 | * [why we don't handle it exactly like ';'? --vda] */ |
| 3811 | if (end_trigger && ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 3812 | #if ENABLE_HUSH_CASE |
| 3813 | /* "case ... in <newline> word) ..." - |
| 3814 | * newlines are ignored (but ';' wouldn't be) */ |
| 3815 | if (dest->length == 0 // && argv[0] == NULL |
| 3816 | && ctx->ctx_res_w == RES_MATCH |
| 3817 | ) { |
| 3818 | continue; |
| 3819 | } |
| 3820 | #endif |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3821 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3822 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3823 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3824 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3825 | if (end_trigger) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3826 | if (!shadow_quote && strchr(end_trigger, ch)) { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3827 | /* Special case: (...word) makes last word terminate, |
| 3828 | * as if ';' is seen */ |
| 3829 | if (ch == ')') { |
| 3830 | done_word(dest, ctx); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3831 | //err chk? |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3832 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3833 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3834 | } |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3835 | if (!HAS_KEYWORDS |
| 3836 | IF_HAS_KEYWORDS(|| (ctx->ctx_res_w == RES_NONE && ctx->old_flag == 0)) |
| 3837 | ) { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3838 | debug_printf_parse("parse_stream return 0: end_trigger char found\n"); |
| 3839 | return 0; |
| 3840 | } |
| 3841 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3842 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3843 | if (m == CHAR_IFS) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3844 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3845 | |
| 3846 | if (dest->o_assignment == MAYBE_ASSIGNMENT) { |
| 3847 | /* ch is a special char and thus this word |
| 3848 | * cannot be an assignment: */ |
| 3849 | dest->o_assignment = NOT_ASSIGNMENT; |
| 3850 | } |
| 3851 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3852 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3853 | case '#': |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3854 | if (dest->length == 0 && !shadow_quote) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3855 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3856 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3857 | if (ch == EOF || ch == '\n') |
| 3858 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3859 | i_getch(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3860 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3861 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3862 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3863 | } |
| 3864 | break; |
| 3865 | case '\\': |
| 3866 | if (next == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3867 | syntax("\\<eof>"); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3868 | debug_printf_parse("parse_stream return 1: \\<eof>\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3869 | return 1; |
| 3870 | } |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3871 | /* bash: |
| 3872 | * "The backslash retains its special meaning [in "..."] |
| 3873 | * only when followed by one of the following characters: |
| 3874 | * $, `, ", \, or <newline>. A double quote may be quoted |
| 3875 | * within double quotes by preceding it with a backslash. |
| 3876 | * If enabled, history expansion will be performed unless |
| 3877 | * an ! appearing in double quotes is escaped using |
| 3878 | * a backslash. The backslash preceding the ! is not removed." |
| 3879 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3880 | if (shadow_quote) { //NOT SURE dest->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3881 | if (strchr("$`\"\\", next) != NULL) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3882 | o_addqchr(dest, i_getch(input)); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3883 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3884 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3885 | } |
| 3886 | } else { |
| 3887 | o_addchr(dest, '\\'); |
| 3888 | o_addchr(dest, i_getch(input)); |
| 3889 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3890 | break; |
| 3891 | case '$': |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3892 | if (handle_dollar(dest, input) != 0) { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3893 | debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n"); |
| 3894 | return 1; |
| 3895 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3896 | break; |
| 3897 | case '\'': |
| 3898 | dest->nonnull = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3899 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3900 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3901 | if (ch == EOF) { |
| 3902 | syntax("unterminated '"); |
| 3903 | debug_printf_parse("parse_stream return 1: unterminated '\n"); |
| 3904 | return 1; |
| 3905 | } |
| 3906 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3907 | break; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3908 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 3909 | o_addqchr(dest, ch); |
| 3910 | else |
| 3911 | o_addchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3912 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3913 | break; |
| 3914 | case '"': |
| 3915 | dest->nonnull = 1; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3916 | shadow_quote ^= 1; /* invert */ |
| 3917 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 3918 | dest->o_quote ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3919 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3920 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3921 | case '`': { |
| 3922 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3923 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3924 | o_addchr(dest, shadow_quote /*or dest->o_quote??*/ ? 0x80 | '`' : '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3925 | add_till_backquote(dest, input); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3926 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3927 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3928 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3929 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3930 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3931 | case '>': |
| 3932 | redir_fd = redirect_opt_num(dest); |
| 3933 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3934 | redir_style = REDIRECT_OVERWRITE; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3935 | if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3936 | redir_style = REDIRECT_APPEND; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3937 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3938 | } |
| 3939 | #if 0 |
| 3940 | else if (next == '(') { |
| 3941 | syntax(">(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3942 | debug_printf_parse("parse_stream return 1: >(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3943 | return 1; |
| 3944 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3945 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3946 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 3947 | break; |
| 3948 | case '<': |
| 3949 | redir_fd = redirect_opt_num(dest); |
| 3950 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3951 | redir_style = REDIRECT_INPUT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3952 | if (next == '<') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3953 | redir_style = REDIRECT_HEREIS; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3954 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3955 | } else if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3956 | redir_style = REDIRECT_IO; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3957 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3958 | } |
| 3959 | #if 0 |
| 3960 | else if (next == '(') { |
| 3961 | syntax("<(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3962 | debug_printf_parse("parse_stream return 1: <(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3963 | return 1; |
| 3964 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3965 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3966 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 3967 | break; |
| 3968 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3969 | #if ENABLE_HUSH_CASE |
| 3970 | case_semi: |
| 3971 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3972 | done_word(dest, ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3973 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3974 | #if ENABLE_HUSH_CASE |
| 3975 | /* Eat multiple semicolons, detect |
| 3976 | * whether it means something special */ |
| 3977 | while (1) { |
| 3978 | ch = i_peek(input); |
| 3979 | if (ch != ';') |
| 3980 | break; |
| 3981 | i_getch(input); |
| 3982 | if (ctx->ctx_res_w == RES_CASEI) { |
| 3983 | ctx->ctx_dsemicolon = 1; |
| 3984 | ctx->ctx_res_w = RES_MATCH; |
| 3985 | break; |
| 3986 | } |
| 3987 | } |
| 3988 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3989 | new_cmd: |
| 3990 | /* We just finished a cmd. New one may start |
| 3991 | * with an assignment */ |
| 3992 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3993 | break; |
| 3994 | case '&': |
| 3995 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3996 | if (next == '&') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3997 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3998 | done_pipe(ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3999 | } else { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4000 | done_pipe(ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4001 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4002 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4003 | case '|': |
| 4004 | done_word(dest, ctx); |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4005 | #if ENABLE_HUSH_CASE |
| 4006 | if (ctx->ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4007 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4008 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4009 | if (next == '|') { /* || */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4010 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4011 | done_pipe(ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4012 | } else { |
| 4013 | /* we could pick up a file descriptor choice here |
| 4014 | * with redirect_opt_num(), but bash doesn't do it. |
| 4015 | * "echo foo 2| cat" yields "foo 2". */ |
| 4016 | done_command(ctx); |
| 4017 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4018 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4019 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4020 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4021 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4022 | if (ctx->ctx_res_w == RES_MATCH |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4023 | && ctx->command->argv == NULL /* not (word|(... */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4024 | && dest->length == 0 /* not word(... */ |
| 4025 | && dest->nonnull == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4026 | ) { |
| 4027 | continue; |
| 4028 | } |
| 4029 | #endif |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4030 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4031 | if (dest->length != 0 /* not just () but word() */ |
| 4032 | && dest->nonnull == 0 /* not a"b"c() */ |
| 4033 | && ctx->command->argv == NULL /* it's the first word */ |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4034 | //TODO: "func ( ) {...}" - note spaces - is valid format too in bash |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4035 | && i_peek(input) == ')' |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4036 | && !match_reserved_word(dest) |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4037 | ) { |
| 4038 | bb_error_msg("seems like a function definition"); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4039 | i_getch(input); |
| 4040 | do { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4041 | //TODO: do it properly. |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4042 | ch = i_getch(input); |
| 4043 | } while (ch == ' ' || ch == '\n'); |
| 4044 | if (ch != '{') { |
| 4045 | syntax("was expecting {"); |
| 4046 | debug_printf_parse("parse_stream return 1\n"); |
| 4047 | return 1; |
| 4048 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4049 | ch = 'F'; /* magic value */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4050 | } |
| 4051 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4052 | case '{': |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4053 | if (parse_group(dest, ctx, input, ch) != 0) { |
| 4054 | 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] | 4055 | return 1; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4056 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4057 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4058 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4059 | #if ENABLE_HUSH_CASE |
| 4060 | if (ctx->ctx_res_w == RES_MATCH) |
| 4061 | goto case_semi; |
| 4062 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4063 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4064 | /* proper use of this character is caught by end_trigger: |
| 4065 | * if we see {, we call parse_group(..., end_trigger='}') |
| 4066 | * and it will match } earlier (not here). */ |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4067 | syntax("unexpected } or )"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4068 | debug_printf_parse("parse_stream return 1: unexpected '}'\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4069 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4070 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4071 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4072 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4073 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4074 | } /* while (1) */ |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4075 | debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL)); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4076 | if (end_trigger) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4077 | return -1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4078 | return 0; |
| 4079 | } |
| 4080 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4081 | static void set_in_charmap(const char *set, int code) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4082 | { |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 4083 | while (*set) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4084 | G.charmap[(unsigned char)*set++] = code; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4085 | } |
| 4086 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4087 | static void update_charmap(void) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4088 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4089 | G.ifs = getenv("IFS"); |
| 4090 | if (G.ifs == NULL) |
| 4091 | G.ifs = " \t\n"; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4092 | /* Precompute a list of 'flow through' behavior so it can be treated |
| 4093 | * quickly up front. Computation is necessary because of IFS. |
| 4094 | * Special case handling of IFS == " \t\n" is not implemented. |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4095 | * The charmap[] array only really needs two bits each, |
| 4096 | * and on most machines that would be faster (reduced L1 cache use). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4097 | */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4098 | memset(G.charmap, CHAR_ORDINARY, sizeof(G.charmap)); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4099 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4100 | set_in_charmap("\\$\"`", CHAR_SPECIAL); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4101 | #else |
| 4102 | set_in_charmap("\\$\"", CHAR_SPECIAL); |
| 4103 | #endif |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4104 | set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4105 | set_in_charmap(G.ifs, CHAR_IFS); /* are ordinary if quoted */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4108 | /* Most recursion does not come through here, the exception is |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4109 | * from builtin_source() and builtin_eval() */ |
| 4110 | 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] | 4111 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4112 | struct parse_context ctx; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4113 | o_string temp = NULL_O_STRING; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4114 | int rcode; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4115 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4116 | do { |
| 4117 | initialize_context(&ctx); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4118 | update_charmap(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4119 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 4120 | inp->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4121 | #endif |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4122 | /* We will stop & execute after each ';' or '\n'. |
| 4123 | * Example: "sleep 9999; echo TEST" + ctrl-C: |
| 4124 | * TEST should be printed */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4125 | temp.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4126 | rcode = parse_stream(&temp, &ctx, inp, ";\n"); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4127 | #if HAS_KEYWORDS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4128 | if (rcode != 1 && ctx.old_flag != 0) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4129 | syntax(NULL); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4130 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4131 | #endif |
| 4132 | if (rcode != 1 IF_HAS_KEYWORDS(&& ctx.old_flag == 0)) { |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4133 | done_word(&temp, &ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4134 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4135 | debug_print_tree(ctx.list_head, 0); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4136 | debug_printf_exec("parse_stream_outer: run_and_free_list\n"); |
| 4137 | run_and_free_list(ctx.list_head); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4138 | } else { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4139 | /* We arrive here also if rcode == 1 (error in parse_stream) */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4140 | #if HAS_KEYWORDS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4141 | if (ctx.old_flag != 0) { |
| 4142 | free(ctx.stack); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4143 | o_reset(&temp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4144 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4145 | #endif |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 4146 | /*temp.nonnull = 0; - o_free does it below */ |
| 4147 | /*temp.o_quote = 0; - o_free does it below */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4148 | free_pipe_list(ctx.list_head, /* indent: */ 0); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4149 | /* Discard all unprocessed line input, force prompt on */ |
| 4150 | inp->p = NULL; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4151 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4152 | inp->promptme = 1; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4153 | #endif |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4154 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4155 | o_free(&temp); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4156 | /* loop on syntax errors, return on EOF: */ |
| 4157 | } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4158 | return 0; |
| 4159 | } |
| 4160 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4161 | static int parse_and_run_string(const char *s, int parse_flag) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4162 | { |
| 4163 | struct in_str input; |
| 4164 | setup_string_in_str(&input, s); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4165 | return parse_and_run_stream(&input, parse_flag); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4168 | static int parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4169 | { |
| 4170 | int rcode; |
| 4171 | struct in_str input; |
| 4172 | setup_file_in_str(&input, f); |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4173 | rcode = parse_and_run_stream(&input, 0 /* parse_flag */); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4174 | return rcode; |
| 4175 | } |
| 4176 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4177 | #if ENABLE_HUSH_JOB |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4178 | /* Make sure we have a controlling tty. If we get started under a job |
| 4179 | * aware app (like bash for example), make sure we are now in charge so |
| 4180 | * we don't fight over who gets the foreground */ |
Eric Andersen | eaecbf3 | 2001-10-31 10:41:31 +0000 | [diff] [blame] | 4181 | static void setup_job_control(void) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4182 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4183 | pid_t shell_pgrp; |
| 4184 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4185 | shell_pgrp = getpgrp(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4186 | close_on_exec_on(G.interactive_fd); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4187 | |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 4188 | /* If we were ran as 'hush &', |
| 4189 | * sleep until we are in the foreground. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4190 | while (tcgetpgrp(G.interactive_fd) != shell_pgrp) { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4191 | /* Send TTIN to ourself (should stop us) */ |
Denis Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 4192 | kill(- shell_pgrp, SIGTTIN); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 4193 | shell_pgrp = getpgrp(); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4194 | } |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4195 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4196 | /* Ignore job-control and misc signals. */ |
| 4197 | set_jobctrl_sighandler(SIG_IGN); |
| 4198 | set_misc_sighandler(SIG_IGN); |
| 4199 | //huh? signal(SIGCHLD, SIG_IGN); |
| 4200 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4201 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4202 | set_fatal_sighandler(sigexit); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4203 | |
| 4204 | /* Put ourselves in our own process group. */ |
Bernhard Reutner-Fischer | 864329d | 2008-09-25 10:55:05 +0000 | [diff] [blame] | 4205 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4206 | /* Grab control of the terminal. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4207 | tcsetpgrp(G.interactive_fd, getpid()); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4208 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4209 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4210 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4211 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 4212 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 4213 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4214 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4215 | static const struct variable const_shell_ver = { |
| 4216 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4217 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4218 | .max_len = 1, /* 0 can provoke free(name) */ |
| 4219 | .flg_export = 1, |
| 4220 | .flg_read_only = 1, |
| 4221 | }; |
| 4222 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4223 | int opt; |
| 4224 | FILE *input; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4225 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4226 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 4227 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 4228 | INIT_G(); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4229 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4230 | G.root_pid = getpid(); |
Denis Vlasenko | 16c2fea | 2008-06-17 12:28:44 +0000 | [diff] [blame] | 4231 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4232 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4233 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 4234 | G.top_var = &G.shell_ver; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4235 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4236 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 4237 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4238 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4239 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4240 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4241 | if (e) while (*e) { |
| 4242 | char *value = strchr(*e, '='); |
| 4243 | if (value) { /* paranoia */ |
| 4244 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 4245 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 4246 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4247 | cur_var->max_len = strlen(*e); |
| 4248 | cur_var->flg_export = 1; |
| 4249 | } |
| 4250 | e++; |
| 4251 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4252 | debug_printf_env("putenv '%s'\n", hush_version_str); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4253 | putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 4254 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4255 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4256 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 4257 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4258 | /* XXX what should these be while sourcing /etc/profile? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4259 | G.global_argc = argc; |
| 4260 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 4261 | /* Initialize some more globals to non-zero values */ |
| 4262 | set_cwd(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4263 | #if ENABLE_HUSH_INTERACTIVE |
| 4264 | #if ENABLE_FEATURE_EDITING |
| 4265 | cmdedit_set_initial_prompt(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4266 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4267 | G.PS2 = "> "; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4268 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 4269 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4270 | if (EXIT_SUCCESS) /* otherwise is already done */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4271 | G.last_return_code = EXIT_SUCCESS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4272 | |
| 4273 | if (argv[0] && argv[0][0] == '-') { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4274 | debug_printf("sourcing /etc/profile\n"); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4275 | input = fopen_for_read("/etc/profile"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4276 | if (input != NULL) { |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 4277 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4278 | parse_and_run_file(input); |
Eric Andersen | a90f20b | 2001-06-26 23:00:21 +0000 | [diff] [blame] | 4279 | fclose(input); |
| 4280 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4281 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4282 | input = stdin; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4283 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4284 | while ((opt = getopt(argc, argv, "c:xif")) > 0) { |
| 4285 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4286 | case 'c': |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4287 | G.global_argv = argv + optind; |
| 4288 | G.global_argc = argc - optind; |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4289 | opt = parse_and_run_string(optarg, 0 /* parse_flag */); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4290 | goto final_return; |
| 4291 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4292 | /* Well, we cannot just declare interactiveness, |
| 4293 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4294 | /* G.interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4295 | break; |
| 4296 | case 'f': |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4297 | G.fake_mode = 1; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4298 | break; |
| 4299 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4300 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4301 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 4302 | " or: sh -c command [args]...\n\n"); |
| 4303 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4304 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4305 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4306 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4307 | } |
| 4308 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4309 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4310 | /* 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] | 4311 | * the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 4312 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4313 | * no arguments remaining or the -s flag given |
| 4314 | * standard input is a terminal |
| 4315 | * standard output is a terminal |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4316 | * Refer to Posix.2, the description of the 'sh' utility. */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4317 | if (argv[optind] == NULL && input == stdin |
| 4318 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 4319 | ) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4320 | G.saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 4321 | debug_printf("saved_tty_pgrp=%d\n", G.saved_tty_pgrp); |
| 4322 | if (G.saved_tty_pgrp >= 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4323 | /* try to dup to high fd#, >= 255 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4324 | G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 4325 | if (G.interactive_fd < 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4326 | /* try to dup to any fd */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4327 | G.interactive_fd = dup(STDIN_FILENO); |
| 4328 | if (G.interactive_fd < 0) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4329 | /* give up */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4330 | G.interactive_fd = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4331 | } |
Denis Vlasenko | 2f1bb36 | 2007-04-21 10:01:14 +0000 | [diff] [blame] | 4332 | // TODO: track & disallow any attempts of user |
| 4333 | // to (inadvertently) close/redirect it |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4334 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4335 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4336 | debug_printf("G.interactive_fd=%d\n", G.interactive_fd); |
| 4337 | if (G.interactive_fd) { |
| 4338 | fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4339 | /* Looks like they want an interactive shell */ |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4340 | setup_job_control(); |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 4341 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 4342 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
| 4343 | die_sleep = -1; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4344 | if (setjmp(die_jmp)) { |
| 4345 | /* xfunc has failed! die die die */ |
| 4346 | hush_exit(xfunc_error_retval); |
| 4347 | } |
Denis Vlasenko | 2f1bb36 | 2007-04-21 10:01:14 +0000 | [diff] [blame] | 4348 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 4349 | printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", bb_banner); |
Denis Vlasenko | 2f1bb36 | 2007-04-21 10:01:14 +0000 | [diff] [blame] | 4350 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
| 4351 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4352 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4353 | #elif ENABLE_HUSH_INTERACTIVE |
| 4354 | /* no job control compiled, only prompt/line editing */ |
| 4355 | if (argv[optind] == NULL && input == stdin |
| 4356 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 4357 | ) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4358 | G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 4359 | if (G.interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4360 | /* try to dup to any fd */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4361 | G.interactive_fd = dup(STDIN_FILENO); |
| 4362 | if (G.interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4363 | /* give up */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4364 | G.interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4365 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4366 | if (G.interactive_fd) { |
| 4367 | fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 4368 | set_misc_sighandler(SIG_IGN); |
| 4369 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4370 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4371 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4372 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4373 | if (argv[optind] == NULL) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4374 | opt = parse_and_run_file(stdin); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4375 | } else { |
| 4376 | debug_printf("\nrunning script '%s'\n", argv[optind]); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4377 | G.global_argv = argv + optind; |
| 4378 | G.global_argc = argc - optind; |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4379 | input = xfopen_for_read(argv[optind]); |
Denis Vlasenko | 459a5ad | 2008-02-11 08:35:03 +0000 | [diff] [blame] | 4380 | fcntl(fileno(input), F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4381 | opt = parse_and_run_file(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4382 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4383 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4384 | final_return: |
| 4385 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4386 | #if ENABLE_FEATURE_CLEAN_UP |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4387 | fclose(input); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4388 | if (G.cwd != bb_msg_unknown) |
| 4389 | free((char*)G.cwd); |
| 4390 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4391 | while (cur_var) { |
| 4392 | struct variable *tmp = cur_var; |
| 4393 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 4394 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4395 | cur_var = cur_var->next; |
| 4396 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4397 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4398 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4399 | hush_exit(opt ? opt : G.last_return_code); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4400 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 4401 | |
| 4402 | |
| 4403 | #if ENABLE_LASH |
| 4404 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 4405 | int lash_main(int argc, char **argv) |
| 4406 | { |
| 4407 | //bb_error_msg("lash is deprecated, please use hush instead"); |
| 4408 | return hush_main(argc, argv); |
| 4409 | } |
| 4410 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4411 | |
| 4412 | |
| 4413 | /* |
| 4414 | * Built-ins |
| 4415 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4416 | static int builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4417 | { |
| 4418 | return 0; |
| 4419 | } |
| 4420 | |
| 4421 | static int builtin_test(char **argv) |
| 4422 | { |
| 4423 | int argc = 0; |
| 4424 | while (*argv) { |
| 4425 | argc++; |
| 4426 | argv++; |
| 4427 | } |
| 4428 | return test_main(argc, argv - argc); |
| 4429 | } |
| 4430 | |
| 4431 | static int builtin_echo(char **argv) |
| 4432 | { |
| 4433 | int argc = 0; |
| 4434 | while (*argv) { |
| 4435 | argc++; |
| 4436 | argv++; |
| 4437 | } |
| 4438 | return echo_main(argc, argv - argc); |
| 4439 | } |
| 4440 | |
| 4441 | static int builtin_eval(char **argv) |
| 4442 | { |
| 4443 | int rcode = EXIT_SUCCESS; |
| 4444 | |
| 4445 | if (argv[1]) { |
| 4446 | char *str = expand_strvec_to_string(argv + 1); |
| 4447 | parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP); |
| 4448 | free(str); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4449 | rcode = G.last_return_code; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4450 | } |
| 4451 | return rcode; |
| 4452 | } |
| 4453 | |
| 4454 | static int builtin_cd(char **argv) |
| 4455 | { |
| 4456 | const char *newdir; |
| 4457 | if (argv[1] == NULL) { |
| 4458 | // bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
| 4459 | // bash says "bash: cd: HOME not set" and does nothing (exitcode 1) |
| 4460 | newdir = getenv("HOME") ? : "/"; |
| 4461 | } else |
| 4462 | newdir = argv[1]; |
| 4463 | if (chdir(newdir)) { |
| 4464 | printf("cd: %s: %s\n", newdir, strerror(errno)); |
| 4465 | return EXIT_FAILURE; |
| 4466 | } |
| 4467 | set_cwd(); |
| 4468 | return EXIT_SUCCESS; |
| 4469 | } |
| 4470 | |
| 4471 | static int builtin_exec(char **argv) |
| 4472 | { |
| 4473 | if (argv[1] == NULL) |
| 4474 | return EXIT_SUCCESS; /* bash does this */ |
| 4475 | { |
| 4476 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4477 | nommu_save_t dummy; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4478 | #endif |
| 4479 | // FIXME: if exec fails, bash does NOT exit! We do... |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4480 | pseudo_exec_argv(&dummy, argv + 1, 0, NULL); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4481 | /* never returns */ |
| 4482 | } |
| 4483 | } |
| 4484 | |
| 4485 | static int builtin_exit(char **argv) |
| 4486 | { |
| 4487 | // TODO: bash does it ONLY on top-level sh exit (+interacive only?) |
| 4488 | //puts("exit"); /* bash does it */ |
| 4489 | // TODO: warn if we have background jobs: "There are stopped jobs" |
| 4490 | // On second consecutive 'exit', exit anyway. |
| 4491 | if (argv[1] == NULL) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4492 | hush_exit(G.last_return_code); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4493 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 4494 | xfunc_error_retval = 255; |
| 4495 | /* bash: exit -2 == exit 254, no error msg */ |
| 4496 | hush_exit(xatoi(argv[1]) & 0xff); |
| 4497 | } |
| 4498 | |
| 4499 | static int builtin_export(char **argv) |
| 4500 | { |
| 4501 | const char *value; |
| 4502 | char *name = argv[1]; |
| 4503 | |
| 4504 | if (name == NULL) { |
| 4505 | // TODO: |
| 4506 | // ash emits: export VAR='VAL' |
| 4507 | // bash: declare -x VAR="VAL" |
| 4508 | // (both also escape as needed (quotes, $, etc)) |
| 4509 | char **e = environ; |
| 4510 | if (e) |
| 4511 | while (*e) |
| 4512 | puts(*e++); |
| 4513 | return EXIT_SUCCESS; |
| 4514 | } |
| 4515 | |
| 4516 | value = strchr(name, '='); |
| 4517 | if (!value) { |
| 4518 | /* They are exporting something without a =VALUE */ |
| 4519 | struct variable *var; |
| 4520 | |
| 4521 | var = get_local_var(name); |
| 4522 | if (var) { |
| 4523 | var->flg_export = 1; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4524 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4525 | putenv(var->varstr); |
| 4526 | } |
| 4527 | /* bash does not return an error when trying to export |
| 4528 | * an undefined variable. Do likewise. */ |
| 4529 | return EXIT_SUCCESS; |
| 4530 | } |
| 4531 | |
| 4532 | set_local_var(xstrdup(name), 1); |
| 4533 | return EXIT_SUCCESS; |
| 4534 | } |
| 4535 | |
| 4536 | #if ENABLE_HUSH_JOB |
| 4537 | /* built-in 'fg' and 'bg' handler */ |
| 4538 | static int builtin_fg_bg(char **argv) |
| 4539 | { |
| 4540 | int i, jobnum; |
| 4541 | struct pipe *pi; |
| 4542 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4543 | if (!G.interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4544 | return EXIT_FAILURE; |
| 4545 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 4546 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4547 | for (pi = G.job_list; pi; pi = pi->next) { |
| 4548 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4549 | goto found; |
| 4550 | } |
| 4551 | } |
| 4552 | bb_error_msg("%s: no current job", argv[0]); |
| 4553 | return EXIT_FAILURE; |
| 4554 | } |
| 4555 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 4556 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 4557 | return EXIT_FAILURE; |
| 4558 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4559 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4560 | if (pi->jobid == jobnum) { |
| 4561 | goto found; |
| 4562 | } |
| 4563 | } |
| 4564 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 4565 | return EXIT_FAILURE; |
| 4566 | found: |
| 4567 | // TODO: bash prints a string representation |
| 4568 | // of job being foregrounded (like "sleep 1 | cat") |
| 4569 | if (*argv[0] == 'f') { |
| 4570 | /* Put the job into the foreground. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4571 | tcsetpgrp(G.interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
| 4574 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4575 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 4576 | for (i = 0; i < pi->num_cmds; i++) { |
| 4577 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 4578 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4579 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4580 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4581 | |
| 4582 | i = kill(- pi->pgrp, SIGCONT); |
| 4583 | if (i < 0) { |
| 4584 | if (errno == ESRCH) { |
| 4585 | delete_finished_bg_job(pi); |
| 4586 | return EXIT_SUCCESS; |
| 4587 | } else { |
| 4588 | bb_perror_msg("kill (SIGCONT)"); |
| 4589 | } |
| 4590 | } |
| 4591 | |
| 4592 | if (*argv[0] == 'f') { |
| 4593 | remove_bg_job(pi); |
| 4594 | return checkjobs_and_fg_shell(pi); |
| 4595 | } |
| 4596 | return EXIT_SUCCESS; |
| 4597 | } |
| 4598 | #endif |
| 4599 | |
| 4600 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4601 | static int builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4602 | { |
| 4603 | const struct built_in_command *x; |
| 4604 | |
| 4605 | printf("\nBuilt-in commands:\n"); |
| 4606 | printf("-------------------\n"); |
| 4607 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 4608 | printf("%s\t%s\n", x->cmd, x->descr); |
| 4609 | } |
| 4610 | printf("\n\n"); |
| 4611 | return EXIT_SUCCESS; |
| 4612 | } |
| 4613 | #endif |
| 4614 | |
| 4615 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4616 | static int builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4617 | { |
| 4618 | struct pipe *job; |
| 4619 | const char *status_string; |
| 4620 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4621 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4622 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4623 | status_string = "Stopped"; |
| 4624 | else |
| 4625 | status_string = "Running"; |
| 4626 | |
| 4627 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 4628 | } |
| 4629 | return EXIT_SUCCESS; |
| 4630 | } |
| 4631 | #endif |
| 4632 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4633 | static int builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4634 | { |
| 4635 | puts(set_cwd()); |
| 4636 | return EXIT_SUCCESS; |
| 4637 | } |
| 4638 | |
| 4639 | static int builtin_read(char **argv) |
| 4640 | { |
| 4641 | char *string; |
| 4642 | const char *name = argv[1] ? argv[1] : "REPLY"; |
| 4643 | |
| 4644 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
| 4645 | return set_local_var(string, 0); |
| 4646 | } |
| 4647 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 4648 | /* built-in 'set' handler |
| 4649 | * SUSv3 says: |
| 4650 | * set [-abCefmnuvx] [-h] [-o option] [argument...] |
| 4651 | * set [+abCefmnuvx] [+h] [+o option] [argument...] |
| 4652 | * set -- [argument...] |
| 4653 | * set -o |
| 4654 | * set +o |
| 4655 | * Implementations shall support the options in both their hyphen and |
| 4656 | * plus-sign forms. These options can also be specified as options to sh. |
| 4657 | * Examples: |
| 4658 | * Write out all variables and their values: set |
| 4659 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 4660 | * Turn on the -x and -v options: set -xv |
| 4661 | * Unset all positional parameters: set -- |
| 4662 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 4663 | * Set the positional parameters to the expansion of x, even if x expands |
| 4664 | * with a leading '-' or '+': set -- $x |
| 4665 | * |
| 4666 | * So far, we only support "set -- [argument...]" by ignoring all options |
| 4667 | * (also, "-o option" will be mishandled by taking "option" as parameter #1). |
| 4668 | */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4669 | static int builtin_set(char **argv) |
| 4670 | { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4671 | struct variable *e; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 4672 | char **pp; |
| 4673 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4674 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 4675 | if (arg == NULL) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4676 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4677 | puts(e->varstr); |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 4678 | } else { |
| 4679 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 4680 | |
| 4681 | if (G.global_args_malloced) { |
| 4682 | pp = G.global_argv; |
| 4683 | while (*++pp) |
| 4684 | free(*pp); |
| 4685 | G.global_argv[1] = NULL; |
| 4686 | } else { |
| 4687 | G.global_args_malloced = 1; |
| 4688 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 4689 | pp[0] = G.global_argv[0]; /* retain $0 */ |
| 4690 | G.global_argv = pp; |
| 4691 | } |
| 4692 | do { |
| 4693 | if (arg[0] == '+') |
| 4694 | continue; |
| 4695 | if (arg[0] != '-') |
| 4696 | break; |
| 4697 | if (arg[1] == '-' && arg[2] == '\0') { |
| 4698 | argv++; |
| 4699 | break; |
| 4700 | } |
| 4701 | } while ((arg = *++argv) != NULL); |
| 4702 | /* Now argv[0] is 1st argument */ |
| 4703 | |
| 4704 | /* This realloc's G.global_argv */ |
| 4705 | G.global_argv = pp = add_strings_to_strings(G.global_argv, argv, /*dup:*/ 1); |
| 4706 | G.global_argc = 1; |
| 4707 | while (*++pp) |
| 4708 | G.global_argc++; |
| 4709 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4710 | |
| 4711 | return EXIT_SUCCESS; |
| 4712 | } |
| 4713 | |
| 4714 | static int builtin_shift(char **argv) |
| 4715 | { |
| 4716 | int n = 1; |
| 4717 | if (argv[1]) { |
| 4718 | n = atoi(argv[1]); |
| 4719 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4720 | if (n >= 0 && n < G.global_argc) { |
| 4721 | G.global_argv[n] = G.global_argv[0]; |
| 4722 | G.global_argc -= n; |
| 4723 | G.global_argv += n; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4724 | return EXIT_SUCCESS; |
| 4725 | } |
| 4726 | return EXIT_FAILURE; |
| 4727 | } |
| 4728 | |
| 4729 | static int builtin_source(char **argv) |
| 4730 | { |
| 4731 | FILE *input; |
| 4732 | int status; |
| 4733 | |
| 4734 | if (argv[1] == NULL) |
| 4735 | return EXIT_FAILURE; |
| 4736 | |
| 4737 | /* XXX search through $PATH is missing */ |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4738 | input = fopen_for_read(argv[1]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4739 | if (!input) { |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 4740 | bb_error_msg("can't open '%s'", argv[1]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4741 | return EXIT_FAILURE; |
| 4742 | } |
| 4743 | close_on_exec_on(fileno(input)); |
| 4744 | |
| 4745 | /* Now run the file */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4746 | /* 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] | 4747 | * (pointer only is OK!) on this stack frame, |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4748 | * set G.global_argv=argv+1, recurse, and restore. */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4749 | status = parse_and_run_file(input); |
| 4750 | fclose(input); |
| 4751 | return status; |
| 4752 | } |
| 4753 | |
| 4754 | static int builtin_umask(char **argv) |
| 4755 | { |
| 4756 | mode_t new_umask; |
| 4757 | const char *arg = argv[1]; |
| 4758 | char *end; |
| 4759 | if (arg) { |
| 4760 | new_umask = strtoul(arg, &end, 8); |
| 4761 | if (*end != '\0' || end == arg) { |
| 4762 | return EXIT_FAILURE; |
| 4763 | } |
| 4764 | } else { |
| 4765 | new_umask = umask(0); |
| 4766 | printf("%.3o\n", (unsigned) new_umask); |
| 4767 | } |
| 4768 | umask(new_umask); |
| 4769 | return EXIT_SUCCESS; |
| 4770 | } |
| 4771 | |
| 4772 | static int builtin_unset(char **argv) |
| 4773 | { |
| 4774 | /* bash always returns true */ |
| 4775 | unset_local_var(argv[1]); |
| 4776 | return EXIT_SUCCESS; |
| 4777 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4778 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4779 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4780 | static int builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4781 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4782 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 4783 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4784 | return EXIT_SUCCESS; /* bash compat */ |
| 4785 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4786 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
| 4787 | G.depth_break_continue = 1; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4788 | if (argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4789 | G.depth_break_continue = bb_strtou(argv[1], NULL, 10); |
| 4790 | if (errno || !G.depth_break_continue || argv[2]) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 4791 | bb_error_msg("%s: bad arguments", argv[0]); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4792 | G.flag_break_continue = BC_BREAK; |
| 4793 | G.depth_break_continue = UINT_MAX; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4794 | } |
| 4795 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4796 | if (G.depth_of_loop < G.depth_break_continue) |
| 4797 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4798 | return EXIT_SUCCESS; |
| 4799 | } |
| 4800 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4801 | static int builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4802 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 4803 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 4804 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4805 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4806 | #endif |