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 |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 48 | * reserved words: case, esac, 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 | * reserved word execution woefully incomplete and buggy |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 54 | * to-do: |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 55 | * port selected bugfixes from post-0.49 busybox lash - done? |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 56 | * change { and } from special chars to reserved words |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 57 | * builtins: return, trap, ulimit |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 58 | * test magic exec |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 59 | * check setting of global_argc and global_argv |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 60 | * follow IFS rules more precisely, including update semantics |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 61 | * figure out what to do with backslash-newline |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 62 | * propagate syntax errors, die on resource errors? |
| 63 | * continuation lines, both explicit and implicit - done? |
| 64 | * memory leak finding and plugging - done? |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 65 | * maybe change charmap[] to use 2-bit entries |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 66 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 67 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 68 | */ |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 70 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 71 | #include <glob.h> |
| 72 | /* #include <dmalloc.h> */ |
| 73 | #if ENABLE_HUSH_CASE |
| 74 | #include <fnmatch.h> |
| 75 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 76 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 77 | #define HUSH_VER_STR "0.9" |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 78 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 79 | #if !BB_MMU && ENABLE_HUSH_TICK |
| 80 | //#undef ENABLE_HUSH_TICK |
| 81 | //#define ENABLE_HUSH_TICK 0 |
| 82 | #warning On NOMMU, hush command substitution is dangerous. |
| 83 | #warning Dont use it for commands which produce lots of output. |
| 84 | #warning For more info see shell/hush.c, generate_stream_from_list(). |
| 85 | #endif |
| 86 | |
| 87 | #if !BB_MMU && ENABLE_HUSH_JOB |
| 88 | #undef ENABLE_HUSH_JOB |
| 89 | #define ENABLE_HUSH_JOB 0 |
| 90 | #endif |
| 91 | |
| 92 | #if !ENABLE_HUSH_INTERACTIVE |
| 93 | #undef ENABLE_FEATURE_EDITING |
| 94 | #define ENABLE_FEATURE_EDITING 0 |
| 95 | #undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 96 | #define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 97 | #endif |
| 98 | |
| 99 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 100 | /* If you comment out one of these below, it will be #defined later |
| 101 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 102 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 103 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 104 | #define debug_printf_parse(...) do {} while (0) |
| 105 | #define debug_print_tree(a, b) do {} while (0) |
| 106 | #define debug_printf_exec(...) do {} while (0) |
| 107 | #define debug_printf_jobs(...) do {} while (0) |
| 108 | #define debug_printf_expand(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 109 | #define debug_printf_glob(...) do {} while (0) |
| 110 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 111 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 112 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 113 | |
| 114 | #ifndef debug_printf |
| 115 | #define debug_printf(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 116 | #endif |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 117 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 118 | #ifndef debug_printf_parse |
| 119 | #define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 120 | #endif |
| 121 | |
| 122 | #ifndef debug_printf_exec |
| 123 | #define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__) |
| 124 | #endif |
| 125 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 126 | #ifndef debug_printf_jobs |
| 127 | #define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 128 | #define DEBUG_JOBS 1 |
| 129 | #else |
| 130 | #define DEBUG_JOBS 0 |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 131 | #endif |
| 132 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 133 | #ifndef debug_printf_expand |
| 134 | #define debug_printf_expand(...) fprintf(stderr, __VA_ARGS__) |
| 135 | #define DEBUG_EXPAND 1 |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 136 | #else |
| 137 | #define DEBUG_EXPAND 0 |
| 138 | #endif |
| 139 | |
| 140 | #ifndef debug_printf_glob |
| 141 | #define debug_printf_glob(...) fprintf(stderr, __VA_ARGS__) |
| 142 | #define DEBUG_GLOB 1 |
| 143 | #else |
| 144 | #define DEBUG_GLOB 0 |
| 145 | #endif |
| 146 | |
| 147 | #ifndef debug_printf_list |
| 148 | #define debug_printf_list(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 149 | #endif |
| 150 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 151 | #ifndef debug_printf_subst |
| 152 | #define debug_printf_subst(...) fprintf(stderr, __VA_ARGS__) |
| 153 | #endif |
| 154 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 155 | #ifndef debug_printf_clean |
| 156 | /* broken, of course, but OK for testing */ |
| 157 | static const char *indenter(int i) |
| 158 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 159 | static const char blanks[] ALIGN1 = |
| 160 | " "; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 161 | return &blanks[sizeof(blanks) - i - 1]; |
| 162 | } |
| 163 | #define debug_printf_clean(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 164 | #define DEBUG_CLEAN 1 |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 165 | #endif |
| 166 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 167 | #if DEBUG_EXPAND |
| 168 | static void debug_print_strings(const char *prefix, char **vv) |
| 169 | { |
| 170 | fprintf(stderr, "%s:\n", prefix); |
| 171 | while (*vv) |
| 172 | fprintf(stderr, " '%s'\n", *vv++); |
| 173 | } |
| 174 | #else |
| 175 | #define debug_print_strings(prefix, vv) ((void)0) |
| 176 | #endif |
| 177 | |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 178 | /* |
| 179 | * Leak hunting. Use hush_leaktool.sh for post-processing. |
| 180 | */ |
| 181 | #ifdef FOR_HUSH_LEAKTOOL |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 182 | /* suppress "warning: no previous prototype..." */ |
| 183 | void *xxmalloc(int lineno, size_t size); |
| 184 | void *xxrealloc(int lineno, void *ptr, size_t size); |
| 185 | char *xxstrdup(int lineno, const char *str); |
| 186 | void xxfree(void *ptr); |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 187 | void *xxmalloc(int lineno, size_t size) |
| 188 | { |
| 189 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 190 | fprintf(stderr, "line %d: malloc %p\n", lineno, ptr); |
| 191 | return ptr; |
| 192 | } |
| 193 | void *xxrealloc(int lineno, void *ptr, size_t size) |
| 194 | { |
| 195 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 196 | fprintf(stderr, "line %d: realloc %p\n", lineno, ptr); |
| 197 | return ptr; |
| 198 | } |
| 199 | char *xxstrdup(int lineno, const char *str) |
| 200 | { |
| 201 | char *ptr = xstrdup(str); |
| 202 | fprintf(stderr, "line %d: strdup %p\n", lineno, ptr); |
| 203 | return ptr; |
| 204 | } |
| 205 | void xxfree(void *ptr) |
| 206 | { |
| 207 | fprintf(stderr, "free %p\n", ptr); |
| 208 | free(ptr); |
| 209 | } |
| 210 | #define xmalloc(s) xxmalloc(__LINE__, s) |
| 211 | #define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 212 | #define xstrdup(s) xxstrdup(__LINE__, s) |
| 213 | #define free(p) xxfree(p) |
| 214 | #endif |
| 215 | |
| 216 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 217 | /* Keep unconditionally on for now */ |
| 218 | #define HUSH_DEBUG 1 |
| 219 | /* Do we support ANY keywords? */ |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 220 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 221 | #define HAS_KEYWORDS 1 |
| 222 | #define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 223 | #define IF_HAS_NO_KEYWORDS(...) |
| 224 | #else |
| 225 | #define HAS_KEYWORDS 0 |
| 226 | #define IF_HAS_KEYWORDS(...) |
| 227 | #define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
| 228 | #endif |
| 229 | |
| 230 | |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 231 | #define SPECIAL_VAR_SYMBOL 3 |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 232 | #define PARSEFLAG_EXIT_FROM_LOOP 1 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 233 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 234 | typedef enum redir_type { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 235 | REDIRECT_INPUT = 1, |
| 236 | REDIRECT_OVERWRITE = 2, |
| 237 | REDIRECT_APPEND = 3, |
| 238 | REDIRECT_HEREIS = 4, |
| 239 | REDIRECT_IO = 5 |
| 240 | } redir_type; |
| 241 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 242 | /* The descrip member of this structure is only used to make |
| 243 | * debugging output pretty */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 244 | static const struct { |
| 245 | int mode; |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 246 | signed char default_fd; |
| 247 | char descrip[3]; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 248 | } redir_table[] = { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 249 | { 0, 0, "()" }, |
| 250 | { O_RDONLY, 0, "<" }, |
| 251 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 252 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 253 | { O_RDONLY, -1, "<<" }, |
| 254 | { O_RDWR, 1, "<>" } |
| 255 | }; |
| 256 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 257 | typedef enum pipe_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 258 | PIPE_SEQ = 1, |
| 259 | PIPE_AND = 2, |
| 260 | PIPE_OR = 3, |
| 261 | PIPE_BG = 4, |
| 262 | } pipe_style; |
| 263 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 264 | typedef enum reserved_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 265 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 266 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 267 | RES_IF , |
| 268 | RES_THEN , |
| 269 | RES_ELIF , |
| 270 | RES_ELSE , |
| 271 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 272 | #endif |
| 273 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 274 | RES_FOR , |
| 275 | RES_WHILE , |
| 276 | RES_UNTIL , |
| 277 | RES_DO , |
| 278 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 279 | #endif |
| 280 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 281 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 282 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 283 | #if ENABLE_HUSH_CASE |
| 284 | RES_CASE , |
| 285 | /* two pseudo-keywords support contrived "case" syntax: */ |
| 286 | RES_MATCH , /* "word)" */ |
| 287 | RES_CASEI , /* "this command is inside CASE" */ |
| 288 | RES_ESAC , |
| 289 | #endif |
| 290 | RES_XXXX , |
| 291 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 292 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 293 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 294 | /* This holds pointers to the various results of parsing */ |
| 295 | struct p_context { |
| 296 | struct child_prog *child; |
| 297 | struct pipe *list_head; |
| 298 | struct pipe *pipe; |
| 299 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 300 | #if HAS_KEYWORDS |
| 301 | smallint ctx_res_w; |
| 302 | smallint ctx_inverted; /* "! cmd | cmd" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 303 | #if ENABLE_HUSH_CASE |
| 304 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 305 | #endif |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 306 | int old_flag; /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 307 | struct p_context *stack; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 308 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 309 | }; |
| 310 | |
| 311 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 312 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 313 | char *rd_filename; /* filename */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 314 | int fd; /* file descriptor being redirected */ |
| 315 | int dup; /* -1, or file descriptor being duplicated */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 316 | smallint /*enum redir_type*/ rd_type; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 317 | }; |
| 318 | |
| 319 | struct child_prog { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 320 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 321 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 322 | smallint is_stopped; /* is the program currently running? */ |
| 323 | smallint subshell; /* flag, non-zero if group must be forked */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 324 | struct pipe *group; /* if non-NULL, this "prog" is {} group, |
| 325 | * subshell, or a compound statement */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 326 | char **argv; /* program name and arguments */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 327 | struct redir_struct *redirects; /* I/O redirections */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 328 | }; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 329 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 330 | * and on execution these are substituted with their values. |
| 331 | * Substitution can make _several_ words out of one argv[n]! |
| 332 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 333 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 334 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 335 | |
| 336 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 337 | struct pipe *next; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 338 | int num_progs; /* total number of programs in job */ |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 339 | int alive_progs; /* number of programs running (not exited) */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 340 | int stopped_progs; /* number of programs alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 341 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 342 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 343 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 344 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 345 | #endif |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 346 | struct child_prog *progs; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 347 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 348 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 349 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 350 | }; |
| 351 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 352 | /* On program start, environ points to initial environment. |
| 353 | * putenv adds new pointers into it, unsetenv removes them. |
| 354 | * Neither of these (de)allocates the strings. |
| 355 | * setenv allocates new strings in malloc space and does putenv, |
| 356 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 357 | #define setenv(...) setenv_is_leaky_dont_use() |
| 358 | struct variable { |
| 359 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 360 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 361 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 362 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 363 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 364 | }; |
| 365 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 366 | typedef struct o_string { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 367 | char *data; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 368 | int length; /* position where data is appended */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 369 | int maxlen; |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 370 | /* Misnomer! it's not "quoting", it's "protection against globbing"! |
| 371 | * (by prepending \ to *, ?, [ and to \ too) */ |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 372 | smallint o_quote; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 373 | smallint o_glob; |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 374 | smallint nonnull; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 375 | smallint has_empty_slot; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 376 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 377 | } o_string; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 378 | enum { |
| 379 | MAYBE_ASSIGNMENT = 0, |
| 380 | DEFINITELY_ASSIGNMENT = 1, |
| 381 | NOT_ASSIGNMENT = 2, |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 382 | 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] | 383 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 384 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 385 | #define NULL_O_STRING { NULL } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 386 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 387 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 388 | * available? Where is it documented? */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 389 | typedef struct in_str { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 390 | const char *p; |
| 391 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 392 | char eof_flag; /* meaningless if ->p == NULL */ |
| 393 | char peek_buf[2]; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 394 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 395 | smallint promptme; |
| 396 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 397 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 398 | FILE *file; |
| 399 | int (*get) (struct in_str *); |
| 400 | int (*peek) (struct in_str *); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 401 | } in_str; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 402 | #define i_getch(input) ((input)->get(input)) |
| 403 | #define i_peek(input) ((input)->peek(input)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 404 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 405 | enum { |
| 406 | CHAR_ORDINARY = 0, |
| 407 | CHAR_ORDINARY_IF_QUOTED = 1, /* example: *, # */ |
| 408 | CHAR_IFS = 2, /* treated as ordinary if quoted */ |
| 409 | CHAR_SPECIAL = 3, /* example: $ */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 410 | }; |
| 411 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 412 | enum { |
| 413 | BC_BREAK = 1, |
| 414 | BC_CONTINUE = 2, |
| 415 | }; |
| 416 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 417 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 418 | /* "Globals" within this file */ |
| 419 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 420 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 421 | struct globals { |
| 422 | #if ENABLE_HUSH_INTERACTIVE |
| 423 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 424 | * _AND_ if we decided to act interactively */ |
| 425 | int interactive_fd; |
| 426 | const char *PS1; |
| 427 | const char *PS2; |
| 428 | #endif |
| 429 | #if ENABLE_FEATURE_EDITING |
| 430 | line_input_t *line_input_state; |
| 431 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 432 | pid_t root_pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 433 | pid_t last_bg_pid; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 434 | #if ENABLE_HUSH_JOB |
| 435 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 436 | pid_t saved_tty_pgrp; |
| 437 | int last_jobid; |
| 438 | struct pipe *job_list; |
| 439 | struct pipe *toplevel_list; |
| 440 | smallint ctrl_z_flag; |
| 441 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 442 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 443 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 444 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 445 | smallint fake_mode; |
| 446 | /* these three support $?, $#, and $1 */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 447 | smalluint last_return_code; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 448 | char **global_argv; |
| 449 | int global_argc; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 450 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 451 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 452 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 453 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 454 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 455 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 456 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 457 | struct variable shell_ver; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 458 | #if ENABLE_FEATURE_SH_STANDALONE |
| 459 | struct nofork_save_area nofork_save; |
| 460 | #endif |
| 461 | #if ENABLE_HUSH_JOB |
| 462 | sigjmp_buf toplevel_jb; |
| 463 | #endif |
| 464 | unsigned char charmap[256]; |
| 465 | char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2]; |
| 466 | }; |
| 467 | |
| 468 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 469 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 470 | * (too many defines). Also, I actually prefer to see when a variable |
| 471 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 472 | #define INIT_G() do { \ |
| 473 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 474 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 475 | |
| 476 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 477 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
| 478 | |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 479 | #if 1 |
| 480 | /* Normal */ |
| 481 | static void syntax(const char *msg) |
| 482 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 483 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 484 | /* Was using fancy stuff: |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 485 | * (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...) |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 486 | * but it SEGVs. ?! Oh well... explicit temp ptr works around that */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 487 | void FAST_FUNC (*fp)(const char *s, ...); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 488 | fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die); |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 489 | fp(msg ? "%s: %s" : "syntax error", "syntax error", msg); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 490 | #else |
| 491 | bb_error_msg_and_die(msg ? "%s: %s" : "syntax error", "syntax error", msg); |
| 492 | #endif |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 493 | } |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 494 | |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 495 | #else |
| 496 | /* Debug */ |
| 497 | static void syntax_lineno(int line) |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 498 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 499 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 500 | void FAST_FUNC (*fp)(const char *s, ...); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 501 | fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die); |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 502 | fp("syntax error hush.c:%d", line); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 503 | #else |
| 504 | bb_error_msg_and_die("syntax error hush.c:%d", line); |
| 505 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 506 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 507 | #define syntax(str) syntax_lineno(__LINE__) |
| 508 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 509 | |
| 510 | /* Index of subroutines: */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 511 | /* in_str manipulations: */ |
| 512 | static int static_get(struct in_str *i); |
| 513 | static int static_peek(struct in_str *i); |
| 514 | static int file_get(struct in_str *i); |
| 515 | static int file_peek(struct in_str *i); |
| 516 | static void setup_file_in_str(struct in_str *i, FILE *f); |
| 517 | 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] | 518 | /* "run" the final data structures: */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 519 | #if !defined(DEBUG_CLEAN) |
| 520 | #define free_pipe_list(head, indent) free_pipe_list(head) |
| 521 | #define free_pipe(pi, indent) free_pipe(pi) |
| 522 | #endif |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 523 | static int free_pipe_list(struct pipe *head, int indent); |
| 524 | static int free_pipe(struct pipe *pi, int indent); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 525 | /* really run the final data structures: */ |
| 526 | static int setup_redirects(struct child_prog *prog, int squirrel[]); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 527 | static int run_list(struct pipe *pi); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 528 | #if BB_MMU |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 529 | #define pseudo_exec_argv(ptrs2free, argv, assignment_cnt, argv_expanded) \ |
| 530 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 531 | #define pseudo_exec(ptrs2free, child, argv_expanded) \ |
| 532 | pseudo_exec(child, argv_expanded) |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 533 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 534 | static void pseudo_exec_argv(char **ptrs2free, char **argv, int assignment_cnt, char **argv_expanded) NORETURN; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 535 | static void pseudo_exec(char **ptrs2free, struct child_prog *child, char **argv_expanded) NORETURN; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 536 | static int run_pipe(struct pipe *pi); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 537 | /* data structure manipulation: */ |
| 538 | static int setup_redirect(struct p_context *ctx, int fd, redir_type style, struct in_str *input); |
| 539 | static void initialize_context(struct p_context *ctx); |
| 540 | static int done_word(o_string *dest, struct p_context *ctx); |
| 541 | static int done_command(struct p_context *ctx); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 542 | static void done_pipe(struct p_context *ctx, pipe_style type); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 543 | /* primary string parsing: */ |
| 544 | static int redirect_dup_num(struct in_str *input); |
| 545 | static int redirect_opt_num(o_string *o); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 546 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 547 | static int process_command_subs(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 548 | struct in_str *input, const char *subst_end); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 549 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 550 | static int parse_group(o_string *dest, struct p_context *ctx, struct in_str *input, int ch); |
Denis Vlasenko | 15d78fb | 2007-01-30 22:28:21 +0000 | [diff] [blame] | 551 | static const char *lookup_param(const char *src); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 552 | static int handle_dollar(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 553 | struct in_str *input); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 554 | static int parse_stream(o_string *dest, struct p_context *ctx, struct in_str *input0, const char *end_trigger); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 555 | /* setup: */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 556 | static int parse_and_run_stream(struct in_str *inp, int parse_flag); |
| 557 | static int parse_and_run_string(const char *s, int parse_flag); |
| 558 | static int parse_and_run_file(FILE *f); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 559 | /* job management: */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 560 | static int checkjobs(struct pipe* fg_pipe); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 561 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 562 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 563 | static void insert_bg_job(struct pipe *pi); |
| 564 | static void remove_bg_job(struct pipe *pi); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 565 | static void delete_finished_bg_job(struct pipe *pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 566 | #else |
| 567 | int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */ |
| 568 | #endif |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 569 | /* local variable support */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 570 | static char **expand_strvec_to_strvec(char **argv); |
| 571 | /* used for eval */ |
| 572 | static char *expand_strvec_to_string(char **argv); |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 573 | /* used for expansion of right hand of assignments */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 574 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 575 | static struct variable *get_local_var(const char *name); |
| 576 | static int set_local_var(char *str, int flg_export); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 577 | static void unset_local_var(const char *name); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 578 | |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 579 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 580 | static int glob_needed(const char *s) |
| 581 | { |
| 582 | while (*s) { |
| 583 | if (*s == '\\') |
| 584 | s++; |
| 585 | if (*s == '*' || *s == '[' || *s == '?') |
| 586 | return 1; |
| 587 | s++; |
| 588 | } |
| 589 | return 0; |
| 590 | } |
| 591 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 592 | static int is_assignment(const char *s) |
| 593 | { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 594 | if (!s || !(isalpha(*s) || *s == '_')) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 595 | return 0; |
| 596 | s++; |
| 597 | while (isalnum(*s) || *s == '_') |
| 598 | s++; |
| 599 | return *s == '='; |
| 600 | } |
| 601 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 602 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 603 | static char *unbackslash(char *src) |
| 604 | { |
| 605 | char *dst = src; |
| 606 | while (1) { |
| 607 | if (*src == '\\') |
| 608 | src++; |
| 609 | if ((*dst++ = *src++) == '\0') |
| 610 | break; |
| 611 | } |
| 612 | return dst; |
| 613 | } |
| 614 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 615 | static char **add_malloced_strings_to_strings(char **strings, char **add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 616 | { |
| 617 | int i; |
| 618 | unsigned count1; |
| 619 | unsigned count2; |
| 620 | char **v; |
| 621 | |
| 622 | v = strings; |
| 623 | count1 = 0; |
| 624 | if (v) { |
| 625 | while (*v) { |
| 626 | count1++; |
| 627 | v++; |
| 628 | } |
| 629 | } |
| 630 | count2 = 0; |
| 631 | v = add; |
| 632 | while (*v) { |
| 633 | count2++; |
| 634 | v++; |
| 635 | } |
| 636 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 637 | v[count1 + count2] = NULL; |
| 638 | i = count2; |
| 639 | while (--i >= 0) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 640 | v[count1 + i] = add[i]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 641 | return v; |
| 642 | } |
| 643 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 644 | static char **add_malloced_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 645 | { |
| 646 | char *v[2]; |
| 647 | |
| 648 | v[0] = add; |
| 649 | v[1] = NULL; |
| 650 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 651 | return add_malloced_strings_to_strings(strings, v); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | static void free_strings(char **strings) |
| 655 | { |
| 656 | if (strings) { |
| 657 | char **v = strings; |
| 658 | while (*v) |
| 659 | free(*v++); |
| 660 | free(strings); |
| 661 | } |
| 662 | } |
| 663 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 664 | #if !BB_MMU |
| 665 | #define EXTRA_PTRS 5 /* 1 for NULL, 1 for args, 3 for paranoid reasons */ |
| 666 | static char **alloc_ptrs(char **argv) |
| 667 | { |
| 668 | char **v = argv; |
| 669 | while (*v) |
| 670 | v++; |
| 671 | return xzalloc((v - argv + EXTRA_PTRS) * sizeof(v[0])); |
| 672 | } |
| 673 | #endif |
| 674 | |
| 675 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 676 | /* Function prototypes for builtins */ |
| 677 | static int builtin_cd(char **argv); |
Denis Vlasenko | 5bc593c | 2007-11-23 21:20:21 +0000 | [diff] [blame] | 678 | static int builtin_echo(char **argv); |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 679 | static int builtin_eval(char **argv); |
| 680 | static int builtin_exec(char **argv); |
| 681 | static int builtin_exit(char **argv); |
| 682 | static int builtin_export(char **argv); |
| 683 | #if ENABLE_HUSH_JOB |
| 684 | static int builtin_fg_bg(char **argv); |
| 685 | static int builtin_jobs(char **argv); |
| 686 | #endif |
| 687 | #if ENABLE_HUSH_HELP |
| 688 | static int builtin_help(char **argv); |
| 689 | #endif |
| 690 | static int builtin_pwd(char **argv); |
| 691 | static int builtin_read(char **argv); |
| 692 | static int builtin_test(char **argv); |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 693 | static int builtin_true(char **argv); |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 694 | static int builtin_set(char **argv); |
| 695 | static int builtin_shift(char **argv); |
| 696 | static int builtin_source(char **argv); |
| 697 | static int builtin_umask(char **argv); |
| 698 | static int builtin_unset(char **argv); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 699 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 700 | static int builtin_break(char **argv); |
| 701 | static int builtin_continue(char **argv); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 702 | #endif |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 703 | //static int builtin_not_written(char **argv); |
| 704 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 705 | /* Table of built-in functions. They can be forked or not, depending on |
| 706 | * context: within pipes, they fork. As simple commands, they do not. |
| 707 | * When used in non-forking context, they can change global variables |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 708 | * in the parent shell process. If forked, of course they cannot. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 709 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 710 | * still be set at the end. */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 711 | struct built_in_command { |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 712 | const char *cmd; |
| 713 | int (*function)(char **argv); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 714 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 715 | const char *descr; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 716 | #define BLTIN(cmd, func, help) { cmd, func, help } |
| 717 | #else |
| 718 | #define BLTIN(cmd, func, help) { cmd, func } |
| 719 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 720 | }; |
| 721 | |
Denis Vlasenko | 5bc593c | 2007-11-23 21:20:21 +0000 | [diff] [blame] | 722 | /* For now, echo and test are unconditionally enabled. |
| 723 | * Maybe make it configurable? */ |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 724 | static const struct built_in_command bltins[] = { |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 725 | BLTIN("." , builtin_source, "Run commands in a file"), |
| 726 | BLTIN(":" , builtin_true, "No-op"), |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 727 | BLTIN("[" , builtin_test, "Test condition"), |
| 728 | BLTIN("[[" , builtin_test, "Test condition"), |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 729 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 730 | BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"), |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 731 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 732 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 733 | BLTIN("break" , builtin_break, "Exit from a loop"), |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 734 | #endif |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 735 | BLTIN("cd" , builtin_cd, "Change directory"), |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 736 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 737 | BLTIN("continue", builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 738 | #endif |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 739 | BLTIN("echo" , builtin_echo, "Write to stdout"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 740 | BLTIN("eval" , builtin_eval, "Construct and run shell command"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 741 | BLTIN("exec" , builtin_exec, "Execute command, don't return to shell"), |
| 742 | BLTIN("exit" , builtin_exit, "Exit"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 743 | BLTIN("export", builtin_export, "Set environment variable"), |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 744 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 745 | BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 746 | BLTIN("jobs" , builtin_jobs, "List active jobs"), |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 747 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 748 | BLTIN("pwd" , builtin_pwd, "Print current directory"), |
| 749 | BLTIN("read" , builtin_read, "Input environment variable"), |
| 750 | // BLTIN("return", builtin_not_written, "Return from a function"), |
| 751 | BLTIN("set" , builtin_set, "Set/unset shell local variables"), |
| 752 | BLTIN("shift" , builtin_shift, "Shift positional parameters"), |
| 753 | // BLTIN("trap" , builtin_not_written, "Trap signals"), |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 754 | BLTIN("test" , builtin_test, "Test condition"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 755 | // BLTIN("ulimit", builtin_not_written, "Control resource limits"), |
| 756 | BLTIN("umask" , builtin_umask, "Set file creation mask"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 757 | BLTIN("unset" , builtin_unset, "Unset environment variable"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 758 | #if ENABLE_HUSH_HELP |
| 759 | BLTIN("help" , builtin_help, "List shell built-in commands"), |
| 760 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 761 | }; |
| 762 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 763 | |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 764 | /* Signals are grouped, we handle them in batches */ |
| 765 | static void set_misc_sighandler(void (*handler)(int)) |
| 766 | { |
| 767 | bb_signals(0 |
| 768 | + (1 << SIGINT) |
| 769 | + (1 << SIGQUIT) |
| 770 | + (1 << SIGTERM) |
| 771 | , handler); |
| 772 | } |
| 773 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 774 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 775 | |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 776 | static void set_fatal_sighandler(void (*handler)(int)) |
| 777 | { |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 778 | bb_signals(0 |
| 779 | + (1 << SIGILL) |
| 780 | + (1 << SIGTRAP) |
| 781 | + (1 << SIGABRT) |
| 782 | + (1 << SIGFPE) |
| 783 | + (1 << SIGBUS) |
| 784 | + (1 << SIGSEGV) |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 785 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 786 | + (1 << SIGHUP) |
| 787 | + (1 << SIGPIPE) |
| 788 | + (1 << SIGALRM) |
| 789 | , handler); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 790 | } |
| 791 | static void set_jobctrl_sighandler(void (*handler)(int)) |
| 792 | { |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 793 | bb_signals(0 |
| 794 | + (1 << SIGTSTP) |
| 795 | + (1 << SIGTTIN) |
| 796 | + (1 << SIGTTOU) |
| 797 | , handler); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 798 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 799 | /* SIGCHLD is special and handled separately */ |
| 800 | |
| 801 | static void set_every_sighandler(void (*handler)(int)) |
| 802 | { |
| 803 | set_fatal_sighandler(handler); |
| 804 | set_jobctrl_sighandler(handler); |
| 805 | set_misc_sighandler(handler); |
| 806 | signal(SIGCHLD, handler); |
| 807 | } |
| 808 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 809 | static void handler_ctrl_c(int sig UNUSED_PARAM) |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 810 | { |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 811 | debug_printf_jobs("got sig %d\n", sig); |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 812 | // 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] | 813 | siglongjmp(G.toplevel_jb, 1); |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 816 | static void handler_ctrl_z(int sig UNUSED_PARAM) |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 817 | { |
| 818 | pid_t pid; |
| 819 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 820 | 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] | 821 | pid = fork(); |
Denis Vlasenko | c299032 | 2007-05-16 12:57:12 +0000 | [diff] [blame] | 822 | if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */ |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 823 | return; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 824 | G.ctrl_z_flag = 1; |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 825 | if (!pid) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 826 | if (ENABLE_HUSH_JOB) |
| 827 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 828 | setpgrp(); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 829 | debug_printf_jobs("set pgrp for child %d ok\n", getpid()); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 830 | set_every_sighandler(SIG_DFL); |
Denis Vlasenko | 18e19f2 | 2007-04-28 16:43:18 +0000 | [diff] [blame] | 831 | raise(SIGTSTP); /* resend TSTP so that child will be stopped */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 832 | debug_printf_jobs("returning in child\n"); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 833 | /* return to nofork, it will eventually exit now, |
| 834 | * not return back to shell */ |
| 835 | return; |
| 836 | } |
| 837 | /* parent */ |
| 838 | /* finish filling up pipe info */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 839 | G.toplevel_list->pgrp = pid; /* child is in its own pgrp */ |
| 840 | G.toplevel_list->progs[0].pid = pid; |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 841 | /* parent needs to longjmp out of running nofork. |
| 842 | * we will "return" exitcode 0, with child put in background */ |
| 843 | // 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] | 844 | debug_printf_jobs("siglongjmp in parent\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 845 | siglongjmp(G.toplevel_jb, 1); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 848 | /* Restores tty foreground process group, and exits. |
| 849 | * May be called as signal handler for fatal signal |
| 850 | * (will faithfully resend signal to itself, producing correct exit state) |
| 851 | * or called directly with -EXITCODE. |
| 852 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 853 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 854 | static void sigexit(int sig) |
| 855 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 856 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 857 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 858 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 859 | #if ENABLE_HUSH_INTERACTIVE |
| 860 | if (G.interactive_fd) |
| 861 | tcsetpgrp(G.interactive_fd, G.saved_tty_pgrp); |
| 862 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 863 | |
| 864 | /* Not a signal, just exit */ |
| 865 | if (sig <= 0) |
| 866 | _exit(- sig); |
| 867 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 868 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /* Restores tty foreground process group, and exits. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 872 | static void hush_exit(int exitcode) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 873 | static void hush_exit(int exitcode) |
| 874 | { |
| 875 | fflush(NULL); /* flush all streams */ |
| 876 | sigexit(- (exitcode & 0xff)); |
| 877 | } |
| 878 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 879 | #else /* !JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 880 | |
| 881 | #define set_fatal_sighandler(handler) ((void)0) |
| 882 | #define set_jobctrl_sighandler(handler) ((void)0) |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 883 | #define hush_exit(e) exit(e) |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 884 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 885 | #endif /* JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 886 | |
| 887 | |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 888 | static const char *set_cwd(void) |
| 889 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 890 | if (G.cwd == bb_msg_unknown) |
| 891 | G.cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */ |
| 892 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 893 | if (!G.cwd) |
| 894 | G.cwd = bb_msg_unknown; |
| 895 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 898 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 899 | /* |
| 900 | * o_string support |
| 901 | */ |
| 902 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 903 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 904 | static void o_reset(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 905 | { |
| 906 | o->length = 0; |
| 907 | o->nonnull = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 908 | if (o->data) |
| 909 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 912 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 913 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 914 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 915 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 918 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 919 | { |
| 920 | if (o->length + len > o->maxlen) { |
| 921 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 922 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 923 | } |
| 924 | } |
| 925 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 926 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 927 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 928 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 929 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 930 | o->data[o->length] = ch; |
| 931 | o->length++; |
| 932 | o->data[o->length] = '\0'; |
| 933 | } |
| 934 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 935 | static void o_addstr(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 936 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 937 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 938 | memcpy(&o->data[o->length], str, len); |
| 939 | o->length += len; |
| 940 | o->data[o->length] = '\0'; |
| 941 | } |
| 942 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 943 | static void o_addstr_duplicate_backslash(o_string *o, const char *str, int len) |
| 944 | { |
| 945 | while (len) { |
| 946 | o_addchr(o, *str); |
| 947 | if (*str++ == '\\' |
| 948 | && (*str != '*' && *str != '?' && *str != '[') |
| 949 | ) { |
| 950 | o_addchr(o, '\\'); |
| 951 | } |
| 952 | len--; |
| 953 | } |
| 954 | } |
| 955 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 956 | /* My analysis of quoting semantics tells me that state information |
| 957 | * is associated with a destination, not a source. |
| 958 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 959 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 960 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 961 | int sz = 1; |
| 962 | if (strchr("*?[\\", ch)) { |
| 963 | sz++; |
| 964 | o->data[o->length] = '\\'; |
| 965 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 966 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 967 | o_grow_by(o, sz); |
| 968 | o->data[o->length] = ch; |
| 969 | o->length++; |
| 970 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 973 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 974 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 975 | int sz = 1; |
| 976 | if (o->o_quote && strchr("*?[\\", ch)) { |
| 977 | sz++; |
| 978 | o->data[o->length] = '\\'; |
| 979 | o->length++; |
| 980 | } |
| 981 | o_grow_by(o, sz); |
| 982 | o->data[o->length] = ch; |
| 983 | o->length++; |
| 984 | o->data[o->length] = '\0'; |
| 985 | } |
| 986 | |
| 987 | static void o_addQstr(o_string *o, const char *str, int len) |
| 988 | { |
| 989 | if (!o->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 990 | o_addstr(o, str, len); |
| 991 | return; |
| 992 | } |
| 993 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 994 | char ch; |
| 995 | int sz; |
| 996 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 997 | if (ordinary_cnt > len) /* paranoia */ |
| 998 | ordinary_cnt = len; |
| 999 | o_addstr(o, str, ordinary_cnt); |
| 1000 | if (ordinary_cnt == len) |
| 1001 | return; |
| 1002 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1003 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1004 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1005 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1006 | sz = 1; |
| 1007 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 1008 | sz++; |
| 1009 | o->data[o->length] = '\\'; |
| 1010 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1011 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1012 | o_grow_by(o, sz); |
| 1013 | o->data[o->length] = ch; |
| 1014 | o->length++; |
| 1015 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1019 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1020 | * 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] | 1021 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1022 | * list[i] contains an INDEX (int!) into this string data. |
| 1023 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1024 | * but list[i]'s need not be modified. |
| 1025 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1026 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1027 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1028 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1029 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1030 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1031 | { |
| 1032 | char **list = (char**)o->data; |
| 1033 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1034 | int i = 0; |
| 1035 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1036 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1037 | while (i < n) { |
| 1038 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1039 | o->data + (int)list[i] + string_start, |
| 1040 | o->data + (int)list[i] + string_start); |
| 1041 | i++; |
| 1042 | } |
| 1043 | if (n) { |
| 1044 | const char *p = o->data + (int)list[n - 1] + string_start; |
| 1045 | fprintf(stderr, " total_sz:%d\n", (p + strlen(p) + 1) - o->data); |
| 1046 | } |
| 1047 | } |
| 1048 | #else |
| 1049 | #define debug_print_list(prefix, o, n) ((void)0) |
| 1050 | #endif |
| 1051 | |
| 1052 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1053 | * in list[n] so that it points past last stored byte so far. |
| 1054 | * It returns n+1. */ |
| 1055 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1056 | { |
| 1057 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1058 | int string_start; |
| 1059 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1060 | |
| 1061 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1062 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1063 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1064 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1065 | 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] | 1066 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1067 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1068 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1069 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1070 | memmove(list + n + 0x10, list + n, string_len); |
| 1071 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1072 | } else |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1073 | 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] | 1074 | } else { |
| 1075 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1076 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1077 | string_len = o->length - string_start; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1078 | 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] | 1079 | o->has_empty_slot = 0; |
| 1080 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1081 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1082 | return n + 1; |
| 1083 | } |
| 1084 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1085 | /* "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] | 1086 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1087 | { |
| 1088 | char **list = (char**)o->data; |
| 1089 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1090 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1091 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1094 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1095 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1096 | static int o_glob(o_string *o, int n) |
| 1097 | { |
| 1098 | glob_t globdata; |
| 1099 | int gr; |
| 1100 | char *pattern; |
| 1101 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1102 | 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] | 1103 | if (!o->data) |
| 1104 | return o_save_ptr_helper(o, n); |
| 1105 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1106 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1107 | if (!glob_needed(pattern)) { |
| 1108 | literal: |
| 1109 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1110 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1111 | return o_save_ptr_helper(o, n); |
| 1112 | } |
| 1113 | |
| 1114 | memset(&globdata, 0, sizeof(globdata)); |
| 1115 | gr = glob(pattern, 0, NULL, &globdata); |
| 1116 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 1117 | if (gr == GLOB_NOSPACE) |
| 1118 | bb_error_msg_and_die("out of memory during glob"); |
| 1119 | if (gr == GLOB_NOMATCH) { |
| 1120 | globfree(&globdata); |
| 1121 | goto literal; |
| 1122 | } |
| 1123 | if (gr != 0) { /* GLOB_ABORTED ? */ |
| 1124 | //TODO: testcase for bad glob pattern behavior |
| 1125 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 1126 | } |
| 1127 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 1128 | char **argv = globdata.gl_pathv; |
| 1129 | o->length = pattern - o->data; /* "forget" pattern */ |
| 1130 | while (1) { |
| 1131 | o_addstr(o, *argv, strlen(*argv) + 1); |
| 1132 | n = o_save_ptr_helper(o, n); |
| 1133 | argv++; |
| 1134 | if (!*argv) |
| 1135 | break; |
| 1136 | } |
| 1137 | } |
| 1138 | globfree(&globdata); |
| 1139 | if (DEBUG_GLOB) |
| 1140 | debug_print_list("o_glob returning", o, n); |
| 1141 | return n; |
| 1142 | } |
| 1143 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1144 | /* If o->o_glob == 1, glob the string so far remembered. |
| 1145 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1146 | static int o_save_ptr(o_string *o, int n) |
| 1147 | { |
| 1148 | if (o->o_glob) |
| 1149 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 1150 | return o_save_ptr_helper(o, n); |
| 1151 | } |
| 1152 | |
| 1153 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1154 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1155 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1156 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1157 | int string_start; |
| 1158 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1159 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 1160 | if (DEBUG_EXPAND) |
| 1161 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1162 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1163 | list = (char**)o->data; |
| 1164 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1165 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1166 | while (n) { |
| 1167 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1168 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1169 | } |
| 1170 | return list; |
| 1171 | } |
| 1172 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1173 | |
| 1174 | /* |
| 1175 | * in_str support |
| 1176 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1177 | static int static_get(struct in_str *i) |
| 1178 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1179 | int ch = *i->p++; |
| 1180 | if (ch == '\0') return EOF; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1181 | return ch; |
| 1182 | } |
| 1183 | |
| 1184 | static int static_peek(struct in_str *i) |
| 1185 | { |
| 1186 | return *i->p; |
| 1187 | } |
| 1188 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1189 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1190 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1191 | #if ENABLE_FEATURE_EDITING |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 1192 | static void cmdedit_set_initial_prompt(void) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1193 | { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1194 | #if !ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1195 | G.PS1 = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1196 | #else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1197 | G.PS1 = getenv("PS1"); |
| 1198 | if (G.PS1 == NULL) |
| 1199 | G.PS1 = "\\w \\$ "; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1200 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1201 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1202 | #endif /* EDITING */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1203 | |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1204 | static const char* setup_prompt_string(int promptmode) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1205 | { |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1206 | const char *prompt_str; |
| 1207 | debug_printf("setup_prompt_string %d ", promptmode); |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1208 | #if !ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1209 | /* Set up the prompt */ |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1210 | if (promptmode == 0) { /* PS1 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1211 | free((char*)G.PS1); |
| 1212 | G.PS1 = xasprintf("%s %c ", G.cwd, (geteuid() != 0) ? '$' : '#'); |
| 1213 | prompt_str = G.PS1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1214 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1215 | prompt_str = G.PS2; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1216 | } |
| 1217 | #else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1218 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Eric Andersen | af44a0e | 2001-04-27 07:26:12 +0000 | [diff] [blame] | 1219 | #endif |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1220 | debug_printf("result '%s'\n", prompt_str); |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1221 | return prompt_str; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1224 | static void get_user_input(struct in_str *i) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1225 | { |
Denis Vlasenko | 0937be5 | 2007-04-28 16:47:08 +0000 | [diff] [blame] | 1226 | int r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1227 | const char *prompt_str; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1228 | |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1229 | prompt_str = setup_prompt_string(i->promptmode); |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1230 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1231 | /* Enable command line editing only while a command line |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 1232 | * is actually being read */ |
| 1233 | do { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1234 | 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] | 1235 | } while (r == 0); /* repeat if Ctrl-C */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1236 | i->eof_flag = (r < 0); |
| 1237 | if (i->eof_flag) { /* EOF/error detected */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1238 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1239 | G.user_input_buf[1] = '\0'; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1240 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1241 | #else |
| 1242 | fputs(prompt_str, stdout); |
| 1243 | fflush(stdout); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1244 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1245 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1246 | i->eof_flag = (r == EOF); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1247 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1248 | i->p = G.user_input_buf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1249 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1250 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1251 | #endif /* INTERACTIVE */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1252 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1253 | /* This is the magic location that prints prompts |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1254 | * and gets data back from the user */ |
| 1255 | static int file_get(struct in_str *i) |
| 1256 | { |
| 1257 | int ch; |
| 1258 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1259 | /* If there is data waiting, eat it up */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1260 | if (i->p && *i->p) { |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1261 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1262 | take_cached: |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1263 | #endif |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1264 | ch = *i->p++; |
| 1265 | if (i->eof_flag && !*i->p) |
| 1266 | ch = EOF; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1267 | } else { |
| 1268 | /* need to double check i->file because we might be doing something |
| 1269 | * more complicated by now, like sourcing or substituting. */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1270 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1271 | if (G.interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1272 | do { |
| 1273 | get_user_input(i); |
| 1274 | } while (!*i->p); /* need non-empty line */ |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1275 | i->promptmode = 1; /* PS2 */ |
| 1276 | i->promptme = 0; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1277 | goto take_cached; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1278 | } |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1279 | #endif |
| 1280 | ch = fgetc(i->file); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1281 | } |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1282 | debug_printf("file_get: got a '%c' %d\n", ch, ch); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1283 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1284 | if (ch == '\n') |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1285 | i->promptme = 1; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1286 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1287 | return ch; |
| 1288 | } |
| 1289 | |
| 1290 | /* All the callers guarantee this routine will never be |
| 1291 | * used right after a newline, so prompting is not needed. |
| 1292 | */ |
| 1293 | static int file_peek(struct in_str *i) |
| 1294 | { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 1295 | int ch; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1296 | if (i->p && *i->p) { |
| 1297 | if (i->eof_flag && !i->p[1]) |
| 1298 | return EOF; |
| 1299 | return *i->p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1300 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 1301 | ch = fgetc(i->file); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1302 | i->eof_flag = (ch == EOF); |
| 1303 | i->peek_buf[0] = ch; |
| 1304 | i->peek_buf[1] = '\0'; |
| 1305 | i->p = i->peek_buf; |
| 1306 | 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] | 1307 | return ch; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1311 | { |
| 1312 | i->peek = file_peek; |
| 1313 | i->get = file_get; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1314 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1315 | i->promptme = 1; |
| 1316 | i->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1317 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1318 | i->file = f; |
| 1319 | i->p = NULL; |
| 1320 | } |
| 1321 | |
| 1322 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1323 | { |
| 1324 | i->peek = static_peek; |
| 1325 | i->get = static_get; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1326 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1327 | i->promptme = 1; |
| 1328 | i->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1329 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1330 | i->p = s; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1331 | i->eof_flag = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1334 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1335 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 1336 | * and stderr if they are redirected. */ |
| 1337 | static int setup_redirects(struct child_prog *prog, int squirrel[]) |
| 1338 | { |
| 1339 | int openfd, mode; |
| 1340 | struct redir_struct *redir; |
| 1341 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1342 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1343 | if (redir->dup == -1 && redir->rd_filename == NULL) { |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 1344 | /* something went wrong in the parse. Pretend it didn't happen */ |
| 1345 | continue; |
| 1346 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1347 | if (redir->dup == -1) { |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1348 | char *p; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1349 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 1350 | //TODO: check redir for names like '\\' |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1351 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1352 | openfd = open_or_warn(p, mode); |
| 1353 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1354 | if (openfd < 0) { |
| 1355 | /* this could get lost if stderr has been redirected, but |
| 1356 | bash and ash both lose it as well (though zsh doesn't!) */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1357 | return 1; |
| 1358 | } |
| 1359 | } else { |
| 1360 | openfd = redir->dup; |
| 1361 | } |
| 1362 | |
| 1363 | if (openfd != redir->fd) { |
| 1364 | if (squirrel && redir->fd < 3) { |
| 1365 | squirrel[redir->fd] = dup(redir->fd); |
| 1366 | } |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1367 | if (openfd == -3) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1368 | //close(openfd); // close(-3) ??! |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1369 | } else { |
| 1370 | dup2(openfd, redir->fd); |
Matt Kraai | c616e53 | 2001-06-05 16:50:08 +0000 | [diff] [blame] | 1371 | if (redir->dup == -1) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1372 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1373 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1374 | } |
| 1375 | } |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
| 1379 | static void restore_redirects(int squirrel[]) |
| 1380 | { |
| 1381 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1382 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1383 | fd = squirrel[i]; |
| 1384 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 1385 | /* We simply die on error */ |
| 1386 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1391 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1392 | /* Called after [v]fork() in run_pipe(), or from builtin_exec(). |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1393 | * Never returns. |
| 1394 | * XXX no exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1395 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1396 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 1397 | static void pseudo_exec_argv(char **ptrs2free, char **argv, int assignment_cnt, char **argv_expanded) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1398 | { |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1399 | int i, rcode; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 1400 | char *p; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 1401 | const struct built_in_command *x; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1402 | |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 1403 | for (i = 0; i < assignment_cnt; i++) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1404 | debug_printf_exec("pid %d environment modification: %s\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1405 | getpid(), argv[i]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1406 | p = expand_string_to_string(argv[i]); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1407 | #if !BB_MMU |
| 1408 | *ptrs2free++ = p; |
| 1409 | #endif |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1410 | putenv(p); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1411 | } |
| 1412 | argv += i; |
| 1413 | /* If a variable is assigned in a forest, and nobody listens, |
| 1414 | * was it ever really set? |
| 1415 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1416 | if (!argv[0]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1417 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1418 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1419 | if (argv_expanded) { |
| 1420 | argv = argv_expanded; |
| 1421 | } else { |
| 1422 | argv = expand_strvec_to_strvec(argv); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1423 | #if !BB_MMU |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1424 | *ptrs2free++ = (char*) argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1425 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1426 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 1427 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1428 | /* |
| 1429 | * Check if the command matches any of the builtins. |
| 1430 | * Depending on context, this might be redundant. But it's |
| 1431 | * easier to waste a few CPU cycles than it is to figure out |
| 1432 | * if this is one of those cases. |
| 1433 | */ |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 1434 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1435 | if (strcmp(argv[0], x->cmd) == 0) { |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 1436 | debug_printf_exec("running builtin '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1437 | rcode = x->function(argv); |
| 1438 | fflush(stdout); |
| 1439 | _exit(rcode); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1440 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1441 | } |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1442 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1443 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 1444 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1445 | if (strchr(argv[0], '/') == NULL) { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 1446 | int a = find_applet_by_name(argv[0]); |
| 1447 | if (a >= 0) { |
| 1448 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1449 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 1450 | // is it ok that run_applet_no_and_exit() does exit(), not _exit()? |
| 1451 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1452 | } |
| 1453 | /* re-exec ourselves with the new arguments */ |
| 1454 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | bdbbb7e | 2007-06-08 15:02:55 +0000 | [diff] [blame] | 1455 | execvp(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1456 | /* If they called chroot or otherwise made the binary no longer |
| 1457 | * executable, fall through */ |
| 1458 | } |
| 1459 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 1460 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1461 | |
| 1462 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1463 | execvp(argv[0], argv); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 1464 | bb_perror_msg("can't exec '%s'", argv[0]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 1465 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1468 | /* Called after [v]fork() in run_pipe() |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1469 | */ |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1470 | static void pseudo_exec(char **ptrs2free, struct child_prog *child, char **argv_expanded) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1471 | { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1472 | if (child->argv) |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 1473 | pseudo_exec_argv(ptrs2free, child->argv, child->assignment_cnt, argv_expanded); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1474 | |
| 1475 | if (child->group) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1476 | #if !BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 1477 | bb_error_msg_and_die("nested lists are not supported on NOMMU"); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1478 | #else |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 1479 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1480 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 1481 | rcode = run_list(child->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 1482 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1483 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1484 | _exit(rcode); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1485 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1486 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1487 | |
| 1488 | /* Can happen. See what bash does with ">foo" by itself. */ |
| 1489 | debug_printf("trying to pseudo_exec null command\n"); |
| 1490 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1493 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1494 | static const char *get_cmdtext(struct pipe *pi) |
| 1495 | { |
| 1496 | char **argv; |
| 1497 | char *p; |
| 1498 | int len; |
| 1499 | |
| 1500 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 1501 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1502 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1503 | if (pi->cmdtext) |
| 1504 | return pi->cmdtext; |
| 1505 | argv = pi->progs[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 1506 | if (!argv || !argv[0]) { |
| 1507 | pi->cmdtext = xzalloc(1); |
| 1508 | return pi->cmdtext; |
| 1509 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1510 | |
| 1511 | len = 0; |
| 1512 | do len += strlen(*argv) + 1; while (*++argv); |
| 1513 | pi->cmdtext = p = xmalloc(len); |
| 1514 | argv = pi->progs[0].argv; |
| 1515 | do { |
| 1516 | len = strlen(*argv); |
| 1517 | memcpy(p, *argv, len); |
| 1518 | p += len; |
| 1519 | *p++ = ' '; |
| 1520 | } while (*++argv); |
| 1521 | p[-1] = '\0'; |
| 1522 | return pi->cmdtext; |
| 1523 | } |
| 1524 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1525 | static void insert_bg_job(struct pipe *pi) |
| 1526 | { |
| 1527 | struct pipe *thejob; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1528 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1529 | |
| 1530 | /* Linear search for the ID of the job to use */ |
| 1531 | pi->jobid = 1; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1532 | for (thejob = G.job_list; thejob; thejob = thejob->next) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1533 | if (thejob->jobid >= pi->jobid) |
| 1534 | pi->jobid = thejob->jobid + 1; |
| 1535 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1536 | /* Add thejob to the list of running jobs */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1537 | if (!G.job_list) { |
| 1538 | thejob = G.job_list = xmalloc(sizeof(*thejob)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1539 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1540 | for (thejob = G.job_list; thejob->next; thejob = thejob->next) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1541 | continue; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1542 | thejob->next = xmalloc(sizeof(*thejob)); |
| 1543 | thejob = thejob->next; |
| 1544 | } |
| 1545 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1546 | /* Physically copy the struct job */ |
Eric Andersen | 0fcd447 | 2001-05-02 20:12:03 +0000 | [diff] [blame] | 1547 | memcpy(thejob, pi, sizeof(struct pipe)); |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1548 | thejob->progs = xzalloc(sizeof(pi->progs[0]) * pi->num_progs); |
| 1549 | /* We cannot copy entire pi->progs[] vector! Double free()s will happen */ |
| 1550 | for (i = 0; i < pi->num_progs; i++) { |
| 1551 | // TODO: do we really need to have so many fields which are just dead weight |
| 1552 | // at execution stage? |
| 1553 | thejob->progs[i].pid = pi->progs[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1554 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1555 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1556 | thejob->next = NULL; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1557 | thejob->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1558 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1559 | /* We don't wait for background thejobs to return -- append it |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1560 | to the list of backgrounded thejobs and leave it alone */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1561 | printf("[%d] %d %s\n", thejob->jobid, thejob->progs[0].pid, thejob->cmdtext); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1562 | G.last_bg_pid = thejob->progs[0].pid; |
| 1563 | G.last_jobid = thejob->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1566 | static void remove_bg_job(struct pipe *pi) |
| 1567 | { |
| 1568 | struct pipe *prev_pipe; |
| 1569 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1570 | if (pi == G.job_list) { |
| 1571 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1572 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1573 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1574 | while (prev_pipe->next != pi) |
| 1575 | prev_pipe = prev_pipe->next; |
| 1576 | prev_pipe->next = pi->next; |
| 1577 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1578 | if (G.job_list) |
| 1579 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 1580 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1581 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1582 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 1583 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1584 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1585 | static void delete_finished_bg_job(struct pipe *pi) |
| 1586 | { |
| 1587 | remove_bg_job(pi); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1588 | pi->stopped_progs = 0; |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 1589 | free_pipe(pi, 0); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1590 | free(pi); |
| 1591 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1592 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1593 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1594 | /* Check to see if any processes have exited -- if they |
| 1595 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1596 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1597 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1598 | int attributes; |
| 1599 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1600 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1601 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1602 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1603 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1604 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1605 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1606 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1607 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1608 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1609 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1610 | /* Do we do this right? |
| 1611 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1612 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1613 | * [3]+ Stopped sleep 20 | false |
| 1614 | * bash-3.00# echo $? |
| 1615 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 1616 | */ |
| 1617 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1618 | //FIXME: non-interactive bash does not continue even if all processes in fg pipe |
| 1619 | //are stopped. Testcase: "cat | cat" in a script (not on command line) |
| 1620 | // + killall -STOP cat |
| 1621 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1622 | wait_more: |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 1623 | // TODO: safe_waitpid? |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1624 | while ((childpid = waitpid(-1, &status, attributes)) > 0) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1625 | int i; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1626 | const int dead = WIFEXITED(status) || WIFSIGNALED(status); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1627 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1628 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1629 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1630 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 1631 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1632 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1633 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 1634 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1635 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1636 | childpid, WEXITSTATUS(status)); |
| 1637 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1638 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1639 | if (fg_pipe) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1640 | for (i = 0; i < fg_pipe->num_progs; i++) { |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1641 | debug_printf_jobs("check pid %d\n", fg_pipe->progs[i].pid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1642 | if (fg_pipe->progs[i].pid != childpid) |
| 1643 | continue; |
| 1644 | /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */ |
| 1645 | if (dead) { |
| 1646 | fg_pipe->progs[i].pid = 0; |
| 1647 | fg_pipe->alive_progs--; |
| 1648 | if (i == fg_pipe->num_progs - 1) { |
| 1649 | /* last process gives overall exitstatus */ |
| 1650 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 1651 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1652 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1653 | } else { |
| 1654 | fg_pipe->progs[i].is_stopped = 1; |
| 1655 | fg_pipe->stopped_progs++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1656 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1657 | debug_printf_jobs("fg_pipe: alive_progs %d stopped_progs %d\n", |
| 1658 | fg_pipe->alive_progs, fg_pipe->stopped_progs); |
| 1659 | if (fg_pipe->alive_progs - fg_pipe->stopped_progs <= 0) { |
| 1660 | /* All processes in fg pipe have exited/stopped */ |
| 1661 | #if ENABLE_HUSH_JOB |
| 1662 | if (fg_pipe->alive_progs) |
| 1663 | insert_bg_job(fg_pipe); |
| 1664 | #endif |
| 1665 | return rcode; |
| 1666 | } |
| 1667 | /* There are still running processes in the fg pipe */ |
| 1668 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1669 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1670 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1673 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1674 | /* We asked to wait for bg or orphaned children */ |
| 1675 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1676 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1677 | for (i = 0; i < pi->num_progs; i++) { |
| 1678 | if (pi->progs[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 1679 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1680 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1681 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 1682 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 1683 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1684 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 1685 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 1686 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1687 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1688 | /* child exited */ |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1689 | pi->progs[i].pid = 0; |
| 1690 | pi->alive_progs--; |
| 1691 | if (!pi->alive_progs) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1692 | printf(JOB_STATUS_FORMAT, pi->jobid, |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1693 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1694 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1695 | } |
| 1696 | } else { |
| 1697 | /* child stopped */ |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1698 | pi->progs[i].is_stopped = 1; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1699 | pi->stopped_progs++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1700 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1701 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1702 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1703 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1704 | /* wait found no children or failed */ |
| 1705 | |
| 1706 | if (childpid && errno != ECHILD) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1707 | bb_perror_msg("waitpid"); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1708 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1711 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1712 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 1713 | { |
| 1714 | pid_t p; |
| 1715 | int rcode = checkjobs(fg_pipe); |
| 1716 | /* Job finished, move the shell to the foreground */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1717 | p = getpgid(0); /* pgid of our process */ |
| 1718 | debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1719 | tcsetpgrp(G.interactive_fd, p); |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1720 | return rcode; |
| 1721 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1722 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1723 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1724 | /* run_pipe() starts all the jobs, but doesn't wait for anything |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1725 | * to finish. See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1726 | * |
| 1727 | * return code is normally -1, when the caller has to wait for children |
| 1728 | * to finish to determine the exit status of the pipe. If the pipe |
| 1729 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1730 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1731 | * return value. |
| 1732 | * |
| 1733 | * The input of the pipe is always stdin, the output is always |
| 1734 | * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus, |
| 1735 | * because it tries to avoid running the command substitution in |
| 1736 | * subshell, when that is in fact necessary. The subshell process |
| 1737 | * now has its stdout directed to the input of the appropriate pipe, |
| 1738 | * so this routine is noticeably simpler. |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1739 | * |
| 1740 | * Returns -1 only if started some children. IOW: we have to |
| 1741 | * mask out retvals of builtins etc with 0xff! |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1742 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1743 | static int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1744 | { |
| 1745 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1746 | int nextin; |
| 1747 | int pipefds[2]; /* pipefds[0] is for reading */ |
Rob Landley | 53702e5 | 2006-07-19 21:43:53 +0000 | [diff] [blame] | 1748 | struct child_prog *child; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1749 | char **argv_expanded = NULL; |
| 1750 | char **argv; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 1751 | const struct built_in_command *x; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 1752 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1753 | /* it is not always needed, but we aim to smaller code */ |
| 1754 | int squirrel[] = { -1, -1, -1 }; |
| 1755 | int rcode; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1756 | const int single_and_fg = (pi->num_progs == 1 && pi->followup != PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1757 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1758 | 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] | 1759 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1760 | #if ENABLE_HUSH_JOB |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 1761 | pi->pgrp = -1; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1762 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1763 | pi->alive_progs = 1; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1764 | pi->stopped_progs = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1765 | |
| 1766 | /* Check if this is a simple builtin (not part of a pipe). |
| 1767 | * Builtins within pipes have to fork anyway, and are handled in |
| 1768 | * pseudo_exec. "echo foo | read bar" doesn't work on bash, either. |
| 1769 | */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1770 | child = &(pi->progs[0]); |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1771 | if (single_and_fg && child->group && child->subshell == 0) { |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 1772 | debug_printf("non-subshell grouping\n"); |
| 1773 | setup_redirects(child, squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1774 | debug_printf_exec(": run_list\n"); |
| 1775 | rcode = run_list(child->group) & 0xff; |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 1776 | restore_redirects(squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1777 | debug_printf_exec("run_pipe return %d\n", rcode); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 1778 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1779 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1782 | argv = child->argv; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1783 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1784 | if (single_and_fg && argv != NULL) { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 1785 | i = child->assignment_cnt; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1786 | if (i != 0 && argv[i] == NULL) { |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1787 | /* assignments, but no command: set local environment */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1788 | for (i = 0; argv[i] != NULL; i++) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 1789 | debug_printf("local environment set: %s\n", argv[i]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1790 | p = expand_string_to_string(argv[i]); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 1791 | set_local_var(p, 0); |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1792 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1793 | 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] | 1794 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1795 | |
| 1796 | /* Expand assignments into one string each */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 1797 | for (i = 0; i < child->assignment_cnt; i++) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1798 | p = expand_string_to_string(argv[i]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1799 | putenv(p); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1800 | //FIXME: do we leak p?! |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 1801 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1802 | |
| 1803 | /* Expand the rest into (possibly) many strings each */ |
| 1804 | argv_expanded = expand_strvec_to_strvec(argv + i); |
| 1805 | |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 1806 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1807 | if (strcmp(argv_expanded[0], x->cmd) == 0) { |
| 1808 | if (x->function == builtin_exec && argv_expanded[1] == NULL) { |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1809 | debug_printf("magic exec\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1810 | setup_redirects(child, NULL); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1811 | return EXIT_SUCCESS; |
| 1812 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1813 | debug_printf("builtin inline %s\n", argv_expanded[0]); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1814 | /* XXX setup_redirects acts on file descriptors, not FILEs. |
| 1815 | * This is perfect for work that comes after exec(). |
| 1816 | * Is it really safe for inline use? Experimentally, |
| 1817 | * things seem to work with glibc. */ |
| 1818 | setup_redirects(child, squirrel); |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1819 | debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv_expanded[1]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1820 | rcode = x->function(argv_expanded) & 0xff; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1821 | free(argv_expanded); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1822 | restore_redirects(squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1823 | debug_printf_exec("run_pipe return %d\n", rcode); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 1824 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1825 | return rcode; |
| 1826 | } |
| 1827 | } |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1828 | #if ENABLE_FEATURE_SH_STANDALONE |
| 1829 | { |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1830 | int a = find_applet_by_name(argv_expanded[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 1831 | if (a >= 0 && APPLET_IS_NOFORK(a)) { |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1832 | setup_redirects(child, squirrel); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1833 | save_nofork_data(&G.nofork_save); |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 1834 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1835 | rcode = run_nofork_applet_prime(&G.nofork_save, a, argv_expanded); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1836 | free(argv_expanded); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1837 | restore_redirects(squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1838 | debug_printf_exec("run_pipe return %d\n", rcode); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 1839 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1840 | return rcode; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1841 | } |
| 1842 | } |
| 1843 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1846 | /* NB: argv_expanded may already be created, and that |
| 1847 | * might include `cmd` runs! Do not rerun it! We *must* |
| 1848 | * use argv_expanded if it's non-NULL */ |
| 1849 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1850 | /* Disable job control signals for shell (parent) and |
| 1851 | * for initial child code after fork */ |
| 1852 | set_jobctrl_sighandler(SIG_IGN); |
| 1853 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1854 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1855 | pi->alive_progs = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1856 | nextin = 0; |
| 1857 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1858 | for (i = 0; i < pi->num_progs; i++) { |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1859 | #if !BB_MMU |
| 1860 | char **ptrs2free = NULL; |
| 1861 | #endif |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1862 | child = &(pi->progs[i]); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1863 | if (child->argv) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1864 | debug_printf_exec(": pipe member '%s' '%s'...\n", child->argv[0], child->argv[1]); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1865 | #if !BB_MMU |
| 1866 | ptrs2free = alloc_ptrs(child->argv); |
| 1867 | #endif |
| 1868 | } else |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1869 | debug_printf_exec(": pipe member with no argv\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1870 | |
| 1871 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1872 | pipefds[0] = 0; |
| 1873 | pipefds[1] = 1; |
| 1874 | if ((i + 1) < pi->num_progs) |
| 1875 | xpipe(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1876 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1877 | child->pid = BB_MMU ? fork() : vfork(); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1878 | if (!child->pid) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 1879 | if (ENABLE_HUSH_JOB) |
| 1880 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1881 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1882 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1883 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1884 | if (G.run_list_level == 1 && G.interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1885 | pid_t pgrp; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1886 | /* Don't do pgrp restore anymore on fatal signals */ |
| 1887 | set_fatal_sighandler(SIG_DFL); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1888 | pgrp = pi->pgrp; |
| 1889 | if (pgrp < 0) /* true for 1st process only */ |
| 1890 | pgrp = getpid(); |
| 1891 | if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1892 | /* We do it in *every* child, not just first, |
| 1893 | * to avoid races */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1894 | tcsetpgrp(G.interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1895 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1896 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1897 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1898 | xmove_fd(nextin, 0); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1899 | xmove_fd(pipefds[1], 1); /* write end */ |
| 1900 | if (pipefds[0] > 1) |
| 1901 | close(pipefds[0]); /* read end */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1902 | /* Like bash, explicit redirects override pipes, |
| 1903 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1904 | setup_redirects(child, NULL); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1905 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1906 | /* Restore default handlers just prior to exec */ |
| 1907 | set_jobctrl_sighandler(SIG_DFL); |
| 1908 | set_misc_sighandler(SIG_DFL); |
| 1909 | signal(SIGCHLD, SIG_DFL); |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1910 | pseudo_exec(ptrs2free, child, argv_expanded); /* does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1911 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1912 | free(argv_expanded); |
| 1913 | argv_expanded = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1914 | #if !BB_MMU |
| 1915 | free_strings(ptrs2free); |
| 1916 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1917 | if (child->pid < 0) { /* [v]fork failed */ |
| 1918 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 1919 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1920 | } else { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1921 | pi->alive_progs++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1922 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1923 | /* Second and next children need to know pid of first one */ |
| 1924 | if (pi->pgrp < 0) |
| 1925 | pi->pgrp = child->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1926 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1927 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1928 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1929 | if (i) |
| 1930 | close(nextin); |
| 1931 | if ((i + 1) < pi->num_progs) |
| 1932 | close(pipefds[1]); /* write end */ |
| 1933 | /* Pass read (output) pipe end to next iteration */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1934 | nextin = pipefds[0]; |
| 1935 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1936 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1937 | if (!pi->alive_progs) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1938 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 1939 | return 1; |
| 1940 | } |
| 1941 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1942 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_progs); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1943 | return -1; |
| 1944 | } |
| 1945 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 1946 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1947 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 1948 | { |
| 1949 | static const char *PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1950 | [PIPE_SEQ] = "SEQ", |
| 1951 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1952 | [PIPE_OR ] = "OR" , |
| 1953 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1954 | }; |
| 1955 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1956 | [RES_NONE ] = "NONE" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 1957 | #if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1958 | [RES_IF ] = "IF" , |
| 1959 | [RES_THEN ] = "THEN" , |
| 1960 | [RES_ELIF ] = "ELIF" , |
| 1961 | [RES_ELSE ] = "ELSE" , |
| 1962 | [RES_FI ] = "FI" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 1963 | #endif |
| 1964 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1965 | [RES_FOR ] = "FOR" , |
| 1966 | [RES_WHILE] = "WHILE", |
| 1967 | [RES_UNTIL] = "UNTIL", |
| 1968 | [RES_DO ] = "DO" , |
| 1969 | [RES_DONE ] = "DONE" , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 1970 | #endif |
| 1971 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1972 | [RES_IN ] = "IN" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 1973 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 1974 | #if ENABLE_HUSH_CASE |
| 1975 | [RES_CASE ] = "CASE" , |
| 1976 | [RES_MATCH] = "MATCH", |
| 1977 | [RES_CASEI] = "CASEI", |
| 1978 | [RES_ESAC ] = "ESAC" , |
| 1979 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 1980 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1981 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1982 | }; |
| 1983 | |
| 1984 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 1985 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1986 | pin = 0; |
| 1987 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 1988 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 1989 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1990 | prn = 0; |
| 1991 | while (prn < pi->num_progs) { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 1992 | struct child_prog *child = &pi->progs[prn]; |
| 1993 | char **argv = child->argv; |
| 1994 | |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 1995 | fprintf(stderr, "%*s prog %d assignment_cnt:%d", lvl*2, "", prn, child->assignment_cnt); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 1996 | if (child->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1997 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 1998 | (child->subshell ? "()" : "{}"), |
| 1999 | argv); |
| 2000 | debug_print_tree(child->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2001 | prn++; |
| 2002 | continue; |
| 2003 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2004 | if (argv) while (*argv) { |
| 2005 | fprintf(stderr, " '%s'", *argv); |
| 2006 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 2007 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2008 | fprintf(stderr, "\n"); |
| 2009 | prn++; |
| 2010 | } |
| 2011 | pi = pi->next; |
| 2012 | pin++; |
| 2013 | } |
| 2014 | } |
| 2015 | #endif |
| 2016 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2017 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 2018 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2019 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2020 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2021 | #if ENABLE_HUSH_CASE |
| 2022 | char *case_word = NULL; |
| 2023 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2024 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2025 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2026 | char *for_varname = NULL; |
| 2027 | char **for_lcur = NULL; |
| 2028 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2029 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2030 | smallint flag_skip = 1; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2031 | smalluint rcode = 0; /* probably just for compiler */ |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 2032 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2033 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2034 | #else |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2035 | enum { cond_code = 0, }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2036 | #endif |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2037 | /*enum reserved_style*/ smallint rword = RES_NONE; |
| 2038 | /*enum reserved_style*/ smallint skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 2039 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2040 | 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] | 2041 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2042 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2043 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2044 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 2045 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2046 | continue; |
| 2047 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2048 | if (cpipe->next == NULL) { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2049 | syntax("malformed for"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2050 | 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] | 2051 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2052 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2053 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2054 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2055 | continue; |
| 2056 | /* 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] | 2057 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 2058 | || 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] | 2059 | ) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2060 | syntax("malformed for"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2061 | 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] | 2062 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2063 | } |
| 2064 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2065 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2066 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2067 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 2068 | * in order to return, no direct "return" statements please. |
| 2069 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2070 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2071 | #if ENABLE_HUSH_JOB |
| 2072 | /* Example of nested list: "while true; do { sleep 1 | exit 2; } done". |
| 2073 | * We are saving state before entering outermost list ("while...done") |
| 2074 | * so that ctrl-Z will correctly background _entire_ outermost list, |
| 2075 | * not just a part of it (like "sleep 1 | exit 2") */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2076 | if (++G.run_list_level == 1 && G.interactive_fd) { |
| 2077 | if (sigsetjmp(G.toplevel_jb, 1)) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2078 | /* ctrl-Z forked and we are parent; or ctrl-C. |
| 2079 | * Sighandler has longjmped us here */ |
| 2080 | signal(SIGINT, SIG_IGN); |
| 2081 | signal(SIGTSTP, SIG_IGN); |
| 2082 | /* Restore level (we can be coming from deep inside |
| 2083 | * nested levels) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2084 | G.run_list_level = 1; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2085 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2086 | if (G.nofork_save.saved) { /* if save area is valid */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2087 | debug_printf_jobs("exiting nofork early\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2088 | restore_nofork_data(&G.nofork_save); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2089 | } |
| 2090 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2091 | if (G.ctrl_z_flag) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2092 | /* ctrl-Z has forked and stored pid of the child in pi->pid. |
| 2093 | * Remember this child as background job */ |
| 2094 | insert_bg_job(pi); |
| 2095 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2096 | /* ctrl-C. We just stop doing whatever we were doing */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 2097 | bb_putchar('\n'); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2098 | } |
Denis Vlasenko | ff29b4f | 2008-07-29 13:57:59 +0000 | [diff] [blame] | 2099 | USE_HUSH_LOOPS(loop_top = NULL;) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2100 | USE_HUSH_LOOPS(G.depth_of_loop = 0;) |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2101 | rcode = 0; |
| 2102 | goto ret; |
| 2103 | } |
| 2104 | /* ctrl-Z handler will store pid etc in pi */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2105 | G.toplevel_list = pi; |
| 2106 | G.ctrl_z_flag = 0; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2107 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2108 | 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] | 2109 | #endif |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 2110 | signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2111 | signal(SIGINT, handler_ctrl_c); |
| 2112 | } |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2113 | #endif /* JOB */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2114 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2115 | /* Go through list of pipes, (maybe) executing them. */ |
| 2116 | 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] | 2117 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 2118 | IF_HAS_NO_KEYWORDS(rword = RES_NONE;) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2119 | debug_printf_exec(": rword=%d cond_code=%d skip_more=%d\n", |
| 2120 | rword, cond_code, skip_more_for_this_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2121 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 2122 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2123 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 2124 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2125 | /* start of a loop: remember where loop starts */ |
| 2126 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2127 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2128 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2129 | #endif |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2130 | if (rword == skip_more_for_this_rword && flag_skip) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2131 | if (pi->followup == PIPE_SEQ) |
| 2132 | flag_skip = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2133 | /* it is "<false> && CMD" or "<true> || CMD" |
| 2134 | * and we should not execute CMD */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2135 | continue; |
| 2136 | } |
| 2137 | flag_skip = 1; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2138 | skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2139 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2140 | if (cond_code) { |
| 2141 | if (rword == RES_THEN) { |
| 2142 | /* "if <false> THEN cmd": skip cmd */ |
| 2143 | continue; |
| 2144 | } |
| 2145 | } else { |
| 2146 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 2147 | /* "if <true> then ... ELSE/ELIF cmd": |
| 2148 | * skip cmd and all following ones */ |
| 2149 | break; |
| 2150 | } |
| 2151 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2152 | #endif |
| 2153 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2154 | if (rword == RES_FOR) { /* && pi->num_progs - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2155 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2156 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2157 | |
| 2158 | static const char encoded_dollar_at[] ALIGN1 = { |
| 2159 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 2160 | }; /* encoded representation of "$@" */ |
| 2161 | static const char *const encoded_dollar_at_argv[] = { |
| 2162 | encoded_dollar_at, NULL |
| 2163 | }; /* argv list with one element: "$@" */ |
| 2164 | char **vals; |
| 2165 | |
| 2166 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2167 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2168 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2169 | if (!pi->next->progs[0].argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2170 | break; |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2171 | vals = pi->next->progs[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2172 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2173 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2174 | debug_print_strings("for_list made from", vals); |
| 2175 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2176 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2177 | debug_print_strings("for_list", for_list); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2178 | for_varname = pi->progs[0].argv[0]; |
| 2179 | pi->progs[0].argv[0] = NULL; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2180 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2181 | free(pi->progs[0].argv[0]); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2182 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 2183 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2184 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2185 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2186 | for_lcur = NULL; |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2187 | pi->progs[0].argv[0] = for_varname; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2188 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2189 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2190 | /* insert next value from for_lcur */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2191 | //TODO: does it need escaping? |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2192 | pi->progs[0].argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++); |
| 2193 | pi->progs[0].assignment_cnt = 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2194 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2195 | 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] | 2196 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2197 | if (rword == RES_DONE) { |
| 2198 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2199 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2200 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2201 | #if ENABLE_HUSH_CASE |
| 2202 | if (rword == RES_CASE) { |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2203 | case_word = expand_strvec_to_string(pi->progs->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2204 | continue; |
| 2205 | } |
| 2206 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 2207 | char **argv; |
| 2208 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2209 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 2210 | break; |
| 2211 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 2212 | argv = pi->progs->argv; |
| 2213 | while (*argv) { |
| 2214 | char *pattern = expand_string_to_string(*argv); |
| 2215 | /* TODO: which FNM_xxx flags to use? */ |
| 2216 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 2217 | free(pattern); |
| 2218 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 2219 | free(case_word); /* make future "word)" stop */ |
| 2220 | case_word = NULL; |
| 2221 | break; |
| 2222 | } |
| 2223 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2224 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2225 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2226 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2227 | if (rword == RES_CASEI) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2228 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2229 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2230 | } |
| 2231 | #endif |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2232 | if (pi->num_progs == 0) |
| 2233 | continue; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2234 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2235 | /* After analyzing all keywords and conditions, we decided |
| 2236 | * to execute this pipe. NB: has to do checkjobs(NULL) |
| 2237 | * after run_pipe() to collect any background children, |
| 2238 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2239 | debug_printf_exec(": run_pipe with %d members\n", pi->num_progs); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2240 | { |
| 2241 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2242 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2243 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2244 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2245 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 2246 | if (r != -1) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2247 | /* we only ran a builtin: rcode is already known |
| 2248 | * and we don't need to wait for anything. */ |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2249 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2250 | /* was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2251 | if (G.flag_break_continue) { |
| 2252 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2253 | /* we might fall into outer *loop*, |
| 2254 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2255 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2256 | G.depth_break_continue--; |
| 2257 | if (G.depth_break_continue == 0) |
| 2258 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2259 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 2260 | } /* 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] | 2261 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2262 | goto check_jobs_and_break; |
| 2263 | /* "continue": simulate end of loop */ |
| 2264 | rword = RES_DONE; |
| 2265 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2266 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2267 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2268 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2269 | /* what does bash do with attempts to background builtins? */ |
| 2270 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2271 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 2272 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2273 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2274 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2275 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2276 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2277 | rcode = 0; /* EXIT_SUCCESS */ |
| 2278 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2279 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2280 | if (G.run_list_level == 1 && G.interactive_fd) { |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2281 | /* waits for completion, then fg's main shell */ |
| 2282 | rcode = checkjobs_and_fg_shell(pi); |
| 2283 | debug_printf_exec(": checkjobs_and_fg_shell returned %d\n", rcode); |
| 2284 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2285 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2286 | { /* this one just waits for completion */ |
| 2287 | rcode = checkjobs(pi); |
| 2288 | debug_printf_exec(": checkjobs returned %d\n", rcode); |
| 2289 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2290 | } |
| 2291 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2292 | debug_printf_exec(": setting last_return_code=%d\n", rcode); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2293 | G.last_return_code = rcode; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2294 | |
| 2295 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2296 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2297 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2298 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2299 | #endif |
| 2300 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2301 | if (rword == RES_WHILE) { |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 2302 | if (rcode) { |
| 2303 | rcode = 0; /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2304 | goto check_jobs_and_break; |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 2305 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2306 | } |
| 2307 | if (rword == RES_UNTIL) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2308 | if (!rcode) { |
| 2309 | check_jobs_and_break: |
| 2310 | checkjobs(NULL); |
| 2311 | break; |
| 2312 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2313 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2314 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2315 | if ((rcode == 0 && pi->followup == PIPE_OR) |
| 2316 | || (rcode != 0 && pi->followup == PIPE_AND) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2317 | ) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2318 | skip_more_for_this_rword = rword; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2319 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 2320 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2321 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2322 | |
| 2323 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2324 | if (G.ctrl_z_flag) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2325 | /* ctrl-Z forked somewhere in the past, we are the child, |
| 2326 | * and now we completed running the list. Exit. */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2327 | //TODO: _exit? |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2328 | exit(rcode); |
| 2329 | } |
| 2330 | ret: |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2331 | if (!--G.run_list_level && G.interactive_fd) { |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 2332 | signal(SIGTSTP, SIG_IGN); |
| 2333 | signal(SIGINT, SIG_IGN); |
| 2334 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2335 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2336 | 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] | 2337 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2338 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2339 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2340 | free(for_list); |
| 2341 | #endif |
| 2342 | #if ENABLE_HUSH_CASE |
| 2343 | free(case_word); |
| 2344 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2345 | return rcode; |
| 2346 | } |
| 2347 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2348 | /* return code is the exit status of the pipe */ |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2349 | static int free_pipe(struct pipe *pi, int indent) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2350 | { |
| 2351 | char **p; |
| 2352 | struct child_prog *child; |
| 2353 | struct redir_struct *r, *rnext; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2354 | int a, i, ret_code = 0; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 2355 | |
| 2356 | if (pi->stopped_progs > 0) |
| 2357 | return ret_code; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2358 | debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid()); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2359 | for (i = 0; i < pi->num_progs; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2360 | child = &pi->progs[i]; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2361 | debug_printf_clean("%s command %d:\n", indenter(indent), i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2362 | if (child->argv) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2363 | for (a = 0, p = child->argv; *p; a++, p++) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2364 | debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2365 | } |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 2366 | free_strings(child->argv); |
| 2367 | child->argv = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2368 | } else if (child->group) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2369 | debug_printf_clean("%s begin group (subshell:%d)\n", indenter(indent), child->subshell); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2370 | ret_code = free_pipe_list(child->group, indent+3); |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2371 | debug_printf_clean("%s end group\n", indenter(indent)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2372 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2373 | debug_printf_clean("%s (nil)\n", indenter(indent)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2374 | } |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2375 | for (r = child->redirects; r; r = rnext) { |
Denis Vlasenko | 211b59b | 2008-06-23 16:28:53 +0000 | [diff] [blame] | 2376 | 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] | 2377 | if (r->dup == -1) { |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2378 | /* guard against the case >$FOO, where foo is unset or blank */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2379 | if (r->rd_filename) { |
| 2380 | debug_printf_clean(" %s\n", r->rd_filename); |
| 2381 | free(r->rd_filename); |
| 2382 | r->rd_filename = NULL; |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2383 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2384 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2385 | debug_printf_clean("&%d\n", r->dup); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2386 | } |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2387 | rnext = r->next; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2388 | free(r); |
| 2389 | } |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2390 | child->redirects = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2391 | } |
| 2392 | free(pi->progs); /* children are an array, they get freed all at once */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2393 | pi->progs = NULL; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2394 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2395 | free(pi->cmdtext); |
| 2396 | pi->cmdtext = NULL; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2397 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2398 | return ret_code; |
| 2399 | } |
| 2400 | |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2401 | static int free_pipe_list(struct pipe *head, int indent) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2402 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2403 | int rcode = 0; /* if list has no members */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2404 | struct pipe *pi, *next; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2405 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2406 | for (pi = head; pi; pi = next) { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2407 | #if HAS_KEYWORDS |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2408 | 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] | 2409 | #endif |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2410 | rcode = free_pipe(pi, indent); |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2411 | 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] | 2412 | next = pi->next; |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2413 | /*pi->next = NULL;*/ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2414 | free(pi); |
| 2415 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2416 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2420 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2421 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2422 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2423 | debug_printf_exec("run_and_free_list entered\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2424 | if (!G.fake_mode) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2425 | debug_printf_exec(": run_list with %d members\n", pi->num_progs); |
| 2426 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2427 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2428 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2429 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2430 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2431 | free_pipe_list(pi, /* indent: */ 0); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2432 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2433 | return rcode; |
| 2434 | } |
| 2435 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2436 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2437 | /* expand_strvec_to_strvec() takes a list of strings, expands |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2438 | * all variable references within and returns a pointer to |
| 2439 | * a list of expanded strings, possibly with larger number |
| 2440 | * of strings. (Think VAR="a b"; echo $VAR). |
| 2441 | * This new list is allocated as a single malloc block. |
| 2442 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 2443 | * followed by strings themself. |
| 2444 | * Caller can deallocate entire list by single free(list). */ |
| 2445 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2446 | /* Store given string, finalizing the word and starting new one whenever |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2447 | * we encounter IFS char(s). This is used for expanding variable values. |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2448 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2449 | 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] | 2450 | { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2451 | while (1) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2452 | int word_len = strcspn(str, G.ifs); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2453 | if (word_len) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2454 | if (output->o_quote || !output->o_glob) |
| 2455 | o_addQstr(output, str, word_len); |
| 2456 | else /* protect backslashes against globbing up :) */ |
| 2457 | o_addstr_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2458 | str += word_len; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2459 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2460 | if (!*str) /* EOL - do not finalize word */ |
| 2461 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2462 | o_addchr(output, '\0'); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2463 | debug_print_list("expand_on_ifs", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2464 | n = o_save_ptr(output, n); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2465 | str += strspn(str, G.ifs); /* skip ifs chars */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2466 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2467 | debug_print_list("expand_on_ifs[1]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2468 | return n; |
| 2469 | } |
| 2470 | |
| 2471 | /* Expand all variable references in given string, adding words to list[] |
| 2472 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 2473 | * to be filled). This routine is extremely tricky: has to deal with |
| 2474 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 2475 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2476 | 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] | 2477 | { |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2478 | /* or_mask is either 0 (normal case) or 0x80 |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2479 | * (expansion of right-hand side of assignment == 1-element expand. |
| 2480 | * 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] | 2481 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2482 | char first_ch, ored_ch; |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2483 | int i; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2484 | const char *val; |
| 2485 | char *p; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2486 | |
| 2487 | ored_ch = 0; |
| 2488 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2489 | debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2490 | debug_print_list("expand_vars_to_list", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2491 | n = o_save_ptr(output, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2492 | debug_print_list("expand_vars_to_list[0]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2493 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2494 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2495 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2496 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2497 | #endif |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2498 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2499 | o_addstr(output, arg, p - arg); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2500 | debug_print_list("expand_vars_to_list[1]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2501 | arg = ++p; |
| 2502 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2503 | |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2504 | 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] | 2505 | /* "$@" is special. Even if quoted, it can still |
| 2506 | * expand to nothing (not even an empty string) */ |
| 2507 | if ((first_ch & 0x7f) != '@') |
| 2508 | ored_ch |= first_ch; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2509 | val = NULL; |
| 2510 | switch (first_ch & 0x7f) { |
| 2511 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 2512 | case '$': /* pid */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2513 | val = utoa(G.root_pid); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2514 | break; |
| 2515 | case '!': /* bg pid */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2516 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2517 | break; |
| 2518 | case '?': /* exitcode */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2519 | val = utoa(G.last_return_code); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2520 | break; |
| 2521 | case '#': /* argc */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2522 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2523 | break; |
| 2524 | case '*': |
| 2525 | case '@': |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2526 | i = 1; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2527 | if (!G.global_argv[i]) |
Denis Vlasenko | c3c6659 | 2007-11-24 00:22:42 +0000 | [diff] [blame] | 2528 | break; |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 2529 | 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] | 2530 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2531 | smallint sv = output->o_quote; |
| 2532 | /* unquoted var's contents should be globbed, so don't quote */ |
| 2533 | output->o_quote = 0; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2534 | while (G.global_argv[i]) { |
| 2535 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 2536 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 2537 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2538 | /* this argv[] is not empty and not last: |
| 2539 | * put terminating NUL, start new word */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2540 | o_addchr(output, '\0'); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2541 | debug_print_list("expand_vars_to_list[2]", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2542 | n = o_save_ptr(output, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2543 | debug_print_list("expand_vars_to_list[3]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2544 | } |
| 2545 | } |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2546 | output->o_quote = sv; |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2547 | } else |
| 2548 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2549 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2550 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2551 | while (1) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2552 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2553 | if (++i >= G.global_argc) |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2554 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2555 | o_addchr(output, '\0'); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2556 | debug_print_list("expand_vars_to_list[4]", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2557 | n = o_save_ptr(output, n); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2558 | } |
| 2559 | } else { /* quoted $*: add as one word */ |
Denis Vlasenko | c3c6659 | 2007-11-24 00:22:42 +0000 | [diff] [blame] | 2560 | while (1) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2561 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2562 | if (!G.global_argv[++i]) |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2563 | break; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2564 | if (G.ifs[0]) |
| 2565 | o_addchr(output, G.ifs[0]); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2566 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2567 | } |
| 2568 | break; |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2569 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 2570 | /* "Empty variable", used to make "" etc to not disappear */ |
| 2571 | arg++; |
| 2572 | ored_ch = 0x80; |
| 2573 | break; |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2574 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2575 | case '`': { /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2576 | struct in_str input; |
| 2577 | *p = '\0'; |
| 2578 | arg++; |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2579 | //TODO: can we just stuff it into "output" directly? |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2580 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2581 | setup_string_in_str(&input, arg); |
| 2582 | process_command_subs(&subst_result, &input, NULL); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2583 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2584 | val = subst_result.data; |
| 2585 | goto store_val; |
| 2586 | } |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2587 | #endif |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2588 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2589 | *p = '\0'; |
| 2590 | arg[0] = first_ch & 0x7f; |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2591 | if (isdigit(arg[0])) { |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2592 | i = xatoi_u(arg); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2593 | if (i < G.global_argc) |
| 2594 | val = G.global_argv[i]; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2595 | /* else val remains NULL: $N with too big N */ |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2596 | } else |
| 2597 | val = lookup_param(arg); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2598 | arg[0] = first_ch; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2599 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2600 | store_val: |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2601 | #endif |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2602 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2603 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2604 | 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] | 2605 | if (val) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2606 | /* unquoted var's contents should be globbed, so don't quote */ |
| 2607 | smallint sv = output->o_quote; |
| 2608 | output->o_quote = 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2609 | n = expand_on_ifs(output, n, val); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2610 | val = NULL; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2611 | output->o_quote = sv; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2612 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2613 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2614 | 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] | 2615 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2616 | } |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2617 | if (val) { |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2618 | o_addQstr(output, val, strlen(val)); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2619 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2620 | |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2621 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2622 | o_free(&subst_result); |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2623 | #endif |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2624 | arg = ++p; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2625 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 2626 | |
Denis Vlasenko | 2e76c3f | 2008-06-10 18:27:50 +0000 | [diff] [blame] | 2627 | if (arg[0]) { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2628 | debug_print_list("expand_vars_to_list[a]", output, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2629 | /* this part is literal, and it was already pre-quoted |
| 2630 | * if needed (much earlier), do not use o_addQstr here! */ |
| 2631 | o_addstr(output, arg, strlen(arg) + 1); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2632 | debug_print_list("expand_vars_to_list[b]", output, n); |
Denis Vlasenko | 2e76c3f | 2008-06-10 18:27:50 +0000 | [diff] [blame] | 2633 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 2634 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 2635 | ) { |
| 2636 | n--; |
| 2637 | /* allow to reuse list[n] later without re-growth */ |
| 2638 | output->has_empty_slot = 1; |
| 2639 | } else { |
| 2640 | o_addchr(output, '\0'); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2641 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2642 | return n; |
| 2643 | } |
| 2644 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2645 | static char **expand_variables(char **argv, int or_mask) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2646 | { |
| 2647 | int n; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2648 | char **list; |
| 2649 | char **v; |
| 2650 | o_string output = NULL_O_STRING; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2651 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2652 | if (or_mask & 0x100) { |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2653 | output.o_quote = 1; /* protect against globbing for "$var" */ |
| 2654 | /* (unquoted $var will temporarily switch it off) */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2655 | output.o_glob = 1; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2656 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2657 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2658 | n = 0; |
| 2659 | v = argv; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2660 | while (*v) { |
| 2661 | n = expand_vars_to_list(&output, n, *v, (char)or_mask); |
| 2662 | v++; |
| 2663 | } |
| 2664 | debug_print_list("expand_variables", &output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2665 | |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 2666 | /* output.data (malloced in one block) gets returned in "list" */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2667 | list = o_finalize_list(&output, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2668 | debug_print_strings("expand_variables[1]", list); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2669 | return list; |
| 2670 | } |
| 2671 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2672 | static char **expand_strvec_to_strvec(char **argv) |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2673 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2674 | return expand_variables(argv, 0x100); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2677 | /* Used for expansion of right hand of assignments */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 2678 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 2679 | * "v=/bin/c*" */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2680 | static char *expand_string_to_string(const char *str) |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2681 | { |
| 2682 | char *argv[2], **list; |
| 2683 | |
| 2684 | argv[0] = (char*)str; |
| 2685 | argv[1] = NULL; |
| 2686 | list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2687 | if (HUSH_DEBUG) |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2688 | if (!list[0] || list[1]) |
| 2689 | bb_error_msg_and_die("BUG in varexp2"); |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2690 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 2691 | strcpy((char*)list, list[0]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2692 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 2693 | return (char*)list; |
| 2694 | } |
| 2695 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2696 | /* Used for "eval" builtin */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2697 | static char* expand_strvec_to_string(char **argv) |
| 2698 | { |
| 2699 | char **list; |
| 2700 | |
| 2701 | list = expand_variables(argv, 0x80); |
| 2702 | /* Convert all NULs to spaces */ |
| 2703 | if (list[0]) { |
| 2704 | int n = 1; |
| 2705 | while (list[n]) { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2706 | if (HUSH_DEBUG) |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2707 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 2708 | bb_error_msg_and_die("BUG in varexp3"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2709 | list[n][-1] = ' '; /* TODO: or to G.ifs[0]? */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2710 | n++; |
| 2711 | } |
| 2712 | } |
| 2713 | strcpy((char*)list, list[0]); |
| 2714 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2715 | return (char*)list; |
| 2716 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2717 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2718 | |
| 2719 | /* Used to get/check local shell variables */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2720 | static struct variable *get_local_var(const char *name) |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2721 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2722 | struct variable *cur; |
| 2723 | int len; |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2724 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2725 | if (!name) |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2726 | return NULL; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2727 | len = strlen(name); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2728 | for (cur = G.top_var; cur; cur = cur->next) { |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2729 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2730 | return cur; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 2731 | } |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2732 | return NULL; |
| 2733 | } |
| 2734 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2735 | /* str holds "NAME=VAL" and is expected to be malloced. |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2736 | * We take ownership of it. */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2737 | static int set_local_var(char *str, int flg_export) |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2738 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2739 | struct variable *cur; |
| 2740 | char *value; |
| 2741 | int name_len; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2742 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2743 | value = strchr(str, '='); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2744 | if (!value) { /* not expected to ever happen? */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2745 | free(str); |
Eric Andersen | 9978576 | 2001-05-22 21:37:48 +0000 | [diff] [blame] | 2746 | return -1; |
| 2747 | } |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2748 | |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2749 | name_len = value - str + 1; /* including '=' */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2750 | 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] | 2751 | while (1) { |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2752 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2753 | if (!cur->next) { |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2754 | /* Bail out. Note that now cur points |
| 2755 | * to last var in linked list */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2756 | break; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2757 | } |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2758 | cur = cur->next; |
| 2759 | continue; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2760 | } |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2761 | /* We found an existing var with this name */ |
| 2762 | *value = '\0'; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2763 | if (cur->flg_read_only) { |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2764 | bb_error_msg("%s: readonly variable", str); |
| 2765 | free(str); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2766 | return -1; |
| 2767 | } |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2768 | unsetenv(str); /* just in case */ |
| 2769 | *value = '='; |
| 2770 | if (strcmp(cur->varstr, str) == 0) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2771 | free_and_exp: |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2772 | free(str); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2773 | goto exp; |
| 2774 | } |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2775 | if (cur->max_len >= strlen(str)) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2776 | /* This one is from startup env, reuse space */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2777 | strcpy(cur->varstr, str); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2778 | goto free_and_exp; |
| 2779 | } |
| 2780 | /* max_len == 0 signifies "malloced" var, which we can |
| 2781 | * (and has to) free */ |
| 2782 | if (!cur->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2783 | free(cur->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2784 | cur->max_len = 0; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2785 | goto set_str_and_exp; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2786 | } |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2787 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2788 | /* Not found - create next variable struct */ |
| 2789 | cur->next = xzalloc(sizeof(*cur)); |
| 2790 | cur = cur->next; |
| 2791 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2792 | set_str_and_exp: |
| 2793 | cur->varstr = str; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2794 | exp: |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2795 | if (flg_export) |
| 2796 | cur->flg_export = 1; |
| 2797 | if (cur->flg_export) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2798 | return putenv(cur->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2799 | return 0; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2802 | static void unset_local_var(const char *name) |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2803 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2804 | struct variable *cur; |
| 2805 | struct variable *prev = prev; /* for gcc */ |
| 2806 | int name_len; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2807 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2808 | if (!name) |
| 2809 | return; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2810 | name_len = strlen(name); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2811 | cur = G.top_var; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2812 | while (cur) { |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2813 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2814 | if (cur->flg_read_only) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 2815 | bb_error_msg("%s: readonly variable", name); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2816 | return; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2817 | } |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2818 | /* prev is ok to use here because 1st variable, HUSH_VERSION, |
| 2819 | * is ro, and we cannot reach this code on the 1st pass */ |
| 2820 | prev->next = cur->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2821 | unsetenv(cur->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2822 | if (!cur->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2823 | free(cur->varstr); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2824 | free(cur); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2825 | return; |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2826 | } |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2827 | prev = cur; |
| 2828 | cur = cur->next; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2829 | } |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2832 | /* 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] | 2833 | * for file descriptor duplication, e.g., "2>&1". |
| 2834 | * Return code is 0 normally, 1 if a syntax error is detected in src. |
| 2835 | * Resource errors (in xmalloc) cause the process to exit */ |
| 2836 | static int setup_redirect(struct p_context *ctx, int fd, redir_type style, |
| 2837 | struct in_str *input) |
| 2838 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2839 | struct child_prog *child = ctx->child; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2840 | struct redir_struct *redir = child->redirects; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2841 | struct redir_struct *last_redir = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2842 | |
| 2843 | /* 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] | 2844 | while (redir) { |
| 2845 | last_redir = redir; |
| 2846 | redir = redir->next; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2847 | } |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 2848 | redir = xzalloc(sizeof(struct redir_struct)); |
| 2849 | /* redir->next = NULL; */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2850 | /* redir->rd_filename = NULL; */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2851 | if (last_redir) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2852 | last_redir->next = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2853 | } else { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2854 | child->redirects = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2857 | redir->rd_type = style; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2858 | redir->fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2859 | |
| 2860 | debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip); |
| 2861 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2862 | /* Check for a '2>&1' type redirect */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2863 | redir->dup = redirect_dup_num(input); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2864 | if (redir->dup == -2) |
| 2865 | return 1; /* syntax error */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2866 | if (redir->dup != -1) { |
| 2867 | /* Erik had a check here that the file descriptor in question |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2868 | * is legit; I postpone that to "run time" |
| 2869 | * A "-" representation of "close me" shows up as a -3 here */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2870 | debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup); |
| 2871 | } else { |
| 2872 | /* We do _not_ try to open the file that src points to, |
| 2873 | * since we need to return and let src be expanded first. |
| 2874 | * Set ctx->pending_redirect, so we know what to do at the |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2875 | * end of the next parsed word. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2876 | ctx->pending_redirect = redir; |
| 2877 | } |
| 2878 | return 0; |
| 2879 | } |
| 2880 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 2881 | static struct pipe *new_pipe(void) |
| 2882 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2883 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2884 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2885 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2886 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2887 | return pi; |
| 2888 | } |
| 2889 | |
| 2890 | static void initialize_context(struct p_context *ctx) |
| 2891 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2892 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2893 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2894 | /* Create the memory for child, roughly: |
| 2895 | * ctx->pipe->progs = new struct child_prog; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2896 | * ctx->child = &ctx->pipe->progs[0]; |
| 2897 | */ |
| 2898 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2901 | /* If a reserved word is found and processed, parse context is modified |
| 2902 | * and 1 is returned. |
| 2903 | * Handles if, then, elif, else, fi, for, while, until, do, done. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2904 | * case, function, and select are obnoxious, save those for later. |
| 2905 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2906 | #if HAS_KEYWORDS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2907 | static int reserved_word(o_string *word, struct p_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2908 | { |
| 2909 | struct reserved_combo { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2910 | char literal[6]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2911 | unsigned char res; |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2912 | unsigned char assignment_flag; |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2913 | int flag; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2914 | }; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2915 | enum { |
| 2916 | FLAG_END = (1 << RES_NONE ), |
| 2917 | #if ENABLE_HUSH_IF |
| 2918 | FLAG_IF = (1 << RES_IF ), |
| 2919 | FLAG_THEN = (1 << RES_THEN ), |
| 2920 | FLAG_ELIF = (1 << RES_ELIF ), |
| 2921 | FLAG_ELSE = (1 << RES_ELSE ), |
| 2922 | FLAG_FI = (1 << RES_FI ), |
| 2923 | #endif |
| 2924 | #if ENABLE_HUSH_LOOPS |
| 2925 | FLAG_FOR = (1 << RES_FOR ), |
| 2926 | FLAG_WHILE = (1 << RES_WHILE), |
| 2927 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 2928 | FLAG_DO = (1 << RES_DO ), |
| 2929 | FLAG_DONE = (1 << RES_DONE ), |
| 2930 | FLAG_IN = (1 << RES_IN ), |
| 2931 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2932 | #if ENABLE_HUSH_CASE |
| 2933 | FLAG_MATCH = (1 << RES_MATCH), |
| 2934 | FLAG_ESAC = (1 << RES_ESAC ), |
| 2935 | #endif |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2936 | FLAG_START = (1 << RES_XXXX ), |
| 2937 | }; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2938 | /* Mostly a list of accepted follow-up reserved words. |
| 2939 | * FLAG_END means we are done with the sequence, and are ready |
| 2940 | * to turn the compound list into a command. |
| 2941 | * FLAG_START means the word must start a new compound list. |
| 2942 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2943 | static const struct reserved_combo reserved_list[] = { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2944 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2945 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 2946 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 2947 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 2948 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 2949 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 2950 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2951 | #endif |
| 2952 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2953 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 2954 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 2955 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 2956 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 2957 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 2958 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2959 | #endif |
| 2960 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2961 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 2962 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2963 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2964 | }; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2965 | #if ENABLE_HUSH_CASE |
| 2966 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2967 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2968 | }; |
| 2969 | #endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2970 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2971 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 2972 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2973 | if (strcmp(word->data, r->literal) != 0) |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2974 | continue; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2975 | 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] | 2976 | #if ENABLE_HUSH_CASE |
| 2977 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE) |
| 2978 | /* "case word IN ..." - IN part starts first match part */ |
| 2979 | r = &reserved_match; |
| 2980 | else |
| 2981 | #endif |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2982 | if (r->flag == 0) { /* '!' */ |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 2983 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2984 | syntax(NULL); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2985 | IF_HAS_KEYWORDS(ctx->ctx_res_w = RES_SNTX;) |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2986 | } |
| 2987 | ctx->ctx_inverted = 1; |
| 2988 | return 1; |
| 2989 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2990 | if (r->flag & FLAG_START) { |
| 2991 | struct p_context *new; |
| 2992 | debug_printf("push stack\n"); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2993 | new = xmalloc(sizeof(*new)); |
| 2994 | *new = *ctx; /* physical copy */ |
| 2995 | initialize_context(ctx); |
| 2996 | ctx->stack = new; |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 2997 | } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 2998 | syntax(NULL); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2999 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3000 | return 1; |
| 3001 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3002 | ctx->ctx_res_w = r->res; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3003 | ctx->old_flag = r->flag; |
| 3004 | if (ctx->old_flag & FLAG_END) { |
| 3005 | struct p_context *old; |
| 3006 | debug_printf("pop stack\n"); |
| 3007 | done_pipe(ctx, PIPE_SEQ); |
| 3008 | old = ctx->stack; |
| 3009 | old->child->group = ctx->list_head; |
| 3010 | old->child->subshell = 0; |
| 3011 | *ctx = *old; /* physical copy */ |
| 3012 | free(old); |
| 3013 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3014 | word->o_assignment = r->assignment_flag; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3015 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3016 | } |
| 3017 | return 0; |
| 3018 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3019 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3020 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3021 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3022 | * Normal return is 0. Syntax errors return 1. */ |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3023 | static int done_word(o_string *word, struct p_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3024 | { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3025 | struct child_prog *child = ctx->child; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3026 | |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3027 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, child); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3028 | if (word->length == 0 && word->nonnull == 0) { |
| 3029 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3030 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3031 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3032 | /* If this word wasn't an assignment, next ones definitely |
| 3033 | * can't be assignments. Even if they look like ones. */ |
| 3034 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3035 | && word->o_assignment != WORD_IS_KEYWORD |
| 3036 | ) { |
| 3037 | word->o_assignment = NOT_ASSIGNMENT; |
| 3038 | } else { |
| 3039 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
| 3040 | child->assignment_cnt++; |
| 3041 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3042 | } |
| 3043 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3044 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3045 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3046 | * only if run as "bash", not "sh" */ |
| 3047 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3048 | word->o_assignment = NOT_ASSIGNMENT; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3049 | debug_printf("word stored in rd_filename: '%s'\n", word->data); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3050 | } else { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3051 | /* "{ echo foo; } echo bar" - bad */ |
| 3052 | /* NB: bash allows e.g. "if true; then { echo foo; } fi". TODO? */ |
| 3053 | if (child->group) { |
| 3054 | syntax(NULL); |
| 3055 | debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n"); |
| 3056 | return 1; |
| 3057 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3058 | #if HAS_KEYWORDS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3059 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3060 | if (ctx->ctx_dsemicolon |
| 3061 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3062 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3063 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3064 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3065 | ctx->ctx_dsemicolon = 0; |
| 3066 | } else |
| 3067 | #endif |
| 3068 | |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3069 | if (!child->argv /* if it's the first word... */ |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3070 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3071 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3072 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3073 | #endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3074 | ) { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3075 | debug_printf_parse(": checking '%s' for reserved-ness\n", word->data); |
| 3076 | if (reserved_word(word, ctx)) { |
| 3077 | o_reset(word); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3078 | debug_printf_parse("done_word return %d\n", (ctx->ctx_res_w == RES_SNTX)); |
| 3079 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3080 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3081 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3082 | #endif |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3083 | 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] | 3084 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3085 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3086 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3087 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3088 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3089 | char *p = word->data; |
| 3090 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3091 | && (p[1] & 0x7f) == '@' |
| 3092 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3093 | ) { |
| 3094 | p += 3; |
| 3095 | } |
| 3096 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3097 | /* saw no "$@", or not only "$@" but some |
| 3098 | * real text is there too */ |
| 3099 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3100 | * e.g. "", $empty"" etc to not disappear */ |
| 3101 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3102 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3103 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3104 | } |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3105 | child->argv = add_malloced_string_to_strings(child->argv, xstrdup(word->data)); |
| 3106 | debug_print_strings("word appended to argv", child->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3107 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3108 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3109 | o_reset(word); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3110 | ctx->pending_redirect = NULL; |
| 3111 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3112 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3113 | /* Force FOR to have just one word (variable name) */ |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3114 | /* NB: basically, this makes hush see "for v in ..." syntax as if |
| 3115 | * as it is "for v; in ...". FOR and IN become two pipe structs |
| 3116 | * in parse tree. */ |
| 3117 | if (ctx->ctx_res_w == RES_FOR) { |
| 3118 | //TODO: check that child->argv[0] is a valid variable name! |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3119 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3120 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3121 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3122 | #if ENABLE_HUSH_CASE |
| 3123 | /* Force CASE to have just one word */ |
| 3124 | if (ctx->ctx_res_w == RES_CASE) { |
| 3125 | done_pipe(ctx, PIPE_SEQ); |
| 3126 | } |
| 3127 | #endif |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3128 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3129 | return 0; |
| 3130 | } |
| 3131 | |
Denis Vlasenko | 6eaf8de | 2008-06-17 12:09:21 +0000 | [diff] [blame] | 3132 | /* Command (member of a pipe) is complete. The only possible error here |
| 3133 | * is out of memory, in which case xmalloc exits. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3134 | static int done_command(struct p_context *ctx) |
| 3135 | { |
| 3136 | /* The child is really already in the pipe structure, so |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3137 | * advance the pipe counter and make a new, null child. */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3138 | struct pipe *pi = ctx->pipe; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3139 | struct child_prog *child = ctx->child; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3140 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3141 | if (child) { |
| 3142 | if (child->group == NULL |
| 3143 | && child->argv == NULL |
| 3144 | && child->redirects == NULL |
| 3145 | ) { |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3146 | debug_printf_parse("done_command: skipping null cmd, num_progs=%d\n", pi->num_progs); |
| 3147 | return pi->num_progs; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3148 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3149 | pi->num_progs++; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3150 | debug_printf_parse("done_command: ++num_progs=%d\n", pi->num_progs); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3151 | } else { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3152 | debug_printf_parse("done_command: initializing, num_progs=%d\n", pi->num_progs); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3153 | } |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3154 | |
| 3155 | /* Only real trickiness here is that the uncommitted |
| 3156 | * child structure is not counted in pi->num_progs. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3157 | pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1)); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3158 | child = &pi->progs[pi->num_progs]; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3159 | memset(child, 0, sizeof(*child)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3160 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3161 | ctx->child = child; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3162 | /* but ctx->pipe and ctx->list_head remain unchanged */ |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3163 | |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3164 | return pi->num_progs; /* used only for 0/nonzero check */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3167 | static void done_pipe(struct p_context *ctx, pipe_style type) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3168 | { |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3169 | int not_null; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3170 | |
| 3171 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3172 | /* Close previous command */ |
| 3173 | not_null = done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3174 | ctx->pipe->followup = type; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3175 | IF_HAS_KEYWORDS(ctx->pipe->pi_inverted = ctx->ctx_inverted;) |
| 3176 | IF_HAS_KEYWORDS(ctx->ctx_inverted = 0;) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3177 | IF_HAS_KEYWORDS(ctx->pipe->res_word = ctx->ctx_res_w;) |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3178 | |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3179 | /* Without this check, even just <enter> on command line generates |
| 3180 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | 6eaf8de | 2008-06-17 12:09:21 +0000 | [diff] [blame] | 3181 | * IOW: it is safe to do it unconditionally. |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3182 | * RES_NONE case is for "for a in; do ..." (empty IN set) |
| 3183 | * to work, possibly other cases too. */ |
| 3184 | if (not_null IF_HAS_KEYWORDS(|| ctx->ctx_res_w != RES_NONE)) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3185 | struct pipe *new_p; |
| 3186 | debug_printf_parse("done_pipe: adding new pipe: " |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3187 | "not_null:%d ctx->ctx_res_w:%d\n", |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3188 | not_null, ctx->ctx_res_w); |
| 3189 | new_p = new_pipe(); |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3190 | ctx->pipe->next = new_p; |
| 3191 | ctx->pipe = new_p; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3192 | ctx->child = NULL; /* needed! */ |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3193 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3194 | * they remain set for commands inside if/while. |
| 3195 | * This is used to control execution. |
| 3196 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3197 | * cases where variable or value happens to match a keyword): |
| 3198 | */ |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3199 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3200 | if (ctx->ctx_res_w == RES_FOR |
| 3201 | || ctx->ctx_res_w == RES_IN) |
| 3202 | ctx->ctx_res_w = RES_NONE; |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3203 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3204 | #if ENABLE_HUSH_CASE |
| 3205 | if (ctx->ctx_res_w == RES_MATCH) |
| 3206 | ctx->ctx_res_w = RES_CASEI; |
| 3207 | #endif |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3208 | /* Create the memory for child, roughly: |
| 3209 | * ctx->pipe->progs = new struct child_prog; |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 3210 | * ctx->child = &ctx->pipe->progs[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3211 | */ |
| 3212 | done_command(ctx); |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3213 | } |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3214 | debug_printf_parse("done_pipe return\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3217 | /* 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] | 3218 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3219 | * Return either -2 (syntax error), -1 (no &), or the number found. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3220 | */ |
| 3221 | static int redirect_dup_num(struct in_str *input) |
| 3222 | { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3223 | int ch, d = 0, ok = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3224 | ch = i_peek(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3225 | if (ch != '&') return -1; |
| 3226 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3227 | i_getch(input); /* get the & */ |
| 3228 | ch = i_peek(input); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3229 | if (ch == '-') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3230 | i_getch(input); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3231 | return -3; /* "-" represents "close me" */ |
| 3232 | } |
| 3233 | while (isdigit(ch)) { |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3234 | d = d*10 + (ch-'0'); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3235 | ok = 1; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3236 | i_getch(input); |
| 3237 | ch = i_peek(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3238 | } |
| 3239 | if (ok) return d; |
| 3240 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3241 | bb_error_msg("ambiguous redirect"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3242 | return -2; |
| 3243 | } |
| 3244 | |
| 3245 | /* If a redirect is immediately preceded by a number, that number is |
| 3246 | * supposed to tell which file descriptor to redirect. This routine |
| 3247 | * looks for such preceding numbers. In an ideal world this routine |
| 3248 | * needs to handle all the following classes of redirects... |
| 3249 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3250 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3251 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3252 | * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo |
| 3253 | * A -1 output from this program means no valid number was found, so the |
| 3254 | * caller should use the appropriate default for this redirection. |
| 3255 | */ |
| 3256 | static int redirect_opt_num(o_string *o) |
| 3257 | { |
| 3258 | int num; |
| 3259 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3260 | if (o->length == 0) |
| 3261 | return -1; |
| 3262 | for (num = 0; num < o->length; num++) { |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3263 | if (!isdigit(o->data[num])) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3264 | return -1; |
| 3265 | } |
| 3266 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3267 | num = atoi(o->data); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3268 | o_reset(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3269 | return num; |
| 3270 | } |
| 3271 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3272 | #if ENABLE_HUSH_TICK |
"Vladimir N. Oleynik" | 19c3701 | 2005-09-22 14:33:15 +0000 | [diff] [blame] | 3273 | static FILE *generate_stream_from_list(struct pipe *head) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3274 | { |
| 3275 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3276 | int pid, channel[2]; |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3277 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 3278 | xpipe(channel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3279 | /* *** NOMMU WARNING *** */ |
| 3280 | /* By using vfork here, we suspend parent till child exits or execs. |
| 3281 | * If child will not do it before it fills the pipe, it can block forever |
| 3282 | * in write(STDOUT_FILENO), and parent (shell) will be also stuck. |
Denis Vlasenko | 3fe4f98 | 2008-06-09 16:02:39 +0000 | [diff] [blame] | 3283 | * Try this script: |
| 3284 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >TESTFILE |
| 3285 | * huge=`cat TESTFILE` # will block here forever |
| 3286 | * echo OK |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3287 | */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 3288 | pid = BB_MMU ? fork() : vfork(); |
| 3289 | if (pid < 0) |
| 3290 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3291 | if (pid == 0) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 3292 | if (ENABLE_HUSH_JOB) |
| 3293 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 3294 | close(channel[0]); /* NB: close _first_, then move fd! */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3295 | xmove_fd(channel[1], 1); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3296 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3297 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3298 | G.run_list_level = 1; |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3299 | #endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3300 | /* Process substitution is not considered to be usual |
| 3301 | * 'command execution'. |
| 3302 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. */ |
| 3303 | /* Not needed, we are relying on it being disabled |
| 3304 | * everywhere outside actual command execution. */ |
| 3305 | /*set_jobctrl_sighandler(SIG_IGN);*/ |
| 3306 | set_misc_sighandler(SIG_DFL); |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3307 | /* Freeing 'head' here would break NOMMU. */ |
| 3308 | _exit(run_list(head)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3309 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3310 | close(channel[1]); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3311 | pf = fdopen(channel[0], "r"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3312 | return pf; |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3313 | /* 'head' is freed by the caller */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3316 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3317 | static int process_command_subs(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3318 | struct in_str *input, |
| 3319 | const char *subst_end) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3320 | { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3321 | int retcode, ch, eol_cnt; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3322 | o_string result = NULL_O_STRING; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3323 | struct p_context inner; |
| 3324 | FILE *p; |
| 3325 | struct in_str pipe_str; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3326 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3327 | initialize_context(&inner); |
| 3328 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3329 | /* Recursion to generate command */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3330 | retcode = parse_stream(&result, &inner, input, subst_end); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3331 | if (retcode != 0) |
| 3332 | return retcode; /* syntax error or EOF */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3333 | done_word(&result, &inner); |
| 3334 | done_pipe(&inner, PIPE_SEQ); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3335 | o_free(&result); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3336 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3337 | p = generate_stream_from_list(inner.list_head); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3338 | if (p == NULL) |
| 3339 | return 1; |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 3340 | close_on_exec_on(fileno(p)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3341 | setup_file_in_str(&pipe_str, p); |
| 3342 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3343 | /* Now send results of command back into original context */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3344 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3345 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3346 | if (ch == '\n') { |
| 3347 | eol_cnt++; |
| 3348 | continue; |
| 3349 | } |
| 3350 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3351 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3352 | eol_cnt--; |
| 3353 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3354 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
| 3357 | debug_printf("done reading from pipe, pclose()ing\n"); |
| 3358 | /* This is the step that wait()s for the child. Should be pretty |
| 3359 | * 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] | 3360 | * to do better, by using wait(), and keeping track of background jobs |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3361 | * at the same time. That would be a lot of work, and contrary |
| 3362 | * to the KISS philosophy of this program. */ |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3363 | retcode = fclose(p); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3364 | free_pipe_list(inner.list_head, /* indent: */ 0); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3365 | debug_printf("closed FILE from child, retcode=%d\n", retcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3366 | return retcode; |
| 3367 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3368 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3369 | |
| 3370 | static int parse_group(o_string *dest, struct p_context *ctx, |
| 3371 | struct in_str *input, int ch) |
| 3372 | { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3373 | /* NB: parse_group may create and use its own o_string, |
| 3374 | * without any code changes. It just so happens that code is smaller |
| 3375 | * if we (ab)use caller's one. */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3376 | int rcode; |
| 3377 | const char *endch = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3378 | struct p_context sub; |
| 3379 | struct child_prog *child = ctx->child; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3380 | |
| 3381 | debug_printf_parse("parse_group entered\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3382 | if (child->argv) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3383 | syntax(NULL); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3384 | debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n"); |
| 3385 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3386 | } |
| 3387 | initialize_context(&sub); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3388 | endch = "}"; |
| 3389 | if (ch == '(') { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3390 | endch = ")"; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3391 | child->subshell = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3392 | } |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3393 | rcode = parse_stream(dest, &sub, input, endch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3394 | if (rcode == 0) { |
| 3395 | done_word(dest, &sub); /* finish off the final word in the subcontext */ |
| 3396 | done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */ |
| 3397 | child->group = sub.list_head; |
| 3398 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3399 | debug_printf_parse("parse_group return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3400 | return rcode; |
| 3401 | /* child remains "open", available for possible redirects */ |
| 3402 | } |
| 3403 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3404 | /* Basically useful version until someone wants to get fancier, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3405 | * see the bash man page under "Parameter Expansion" */ |
Denis Vlasenko | 15d78fb | 2007-01-30 22:28:21 +0000 | [diff] [blame] | 3406 | static const char *lookup_param(const char *src) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3407 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3408 | struct variable *var = get_local_var(src); |
| 3409 | if (var) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 3410 | return strchr(var->varstr, '=') + 1; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3411 | return NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3414 | #if ENABLE_HUSH_TICK |
| 3415 | /* Subroutines for copying $(...) and `...` things */ |
| 3416 | static void add_till_backquote(o_string *dest, struct in_str *input); |
| 3417 | /* '...' */ |
| 3418 | static void add_till_single_quote(o_string *dest, struct in_str *input) |
| 3419 | { |
| 3420 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3421 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3422 | if (ch == EOF) |
| 3423 | break; |
| 3424 | if (ch == '\'') |
| 3425 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3426 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3427 | } |
| 3428 | } |
| 3429 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
| 3430 | static void add_till_double_quote(o_string *dest, struct in_str *input) |
| 3431 | { |
| 3432 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3433 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3434 | if (ch == '"') |
| 3435 | break; |
| 3436 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3437 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3438 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3439 | } |
| 3440 | if (ch == EOF) |
| 3441 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3442 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3443 | if (ch == '`') { |
| 3444 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3445 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3446 | continue; |
| 3447 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3448 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3449 | } |
| 3450 | } |
| 3451 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 3452 | * \` quoting. |
| 3453 | * "Within the backquoted style of command substitution, backslash |
| 3454 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 3455 | * The search for the matching backquote shall be satisfied by the first |
| 3456 | * backquote found without a preceding backslash; during this search, |
| 3457 | * if a non-escaped backquote is encountered within a shell comment, |
| 3458 | * a here-document, an embedded command substitution of the $(command) |
| 3459 | * form, or a quoted string, undefined results occur. A single-quoted |
| 3460 | * or double-quoted string that begins, but does not end, within the |
| 3461 | * "`...`" sequence produces undefined results." |
| 3462 | * Example Output |
| 3463 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 3464 | */ |
| 3465 | static void add_till_backquote(o_string *dest, struct in_str *input) |
| 3466 | { |
| 3467 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3468 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3469 | if (ch == '`') |
| 3470 | break; |
| 3471 | if (ch == '\\') { /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3472 | int ch2 = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3473 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3474 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3475 | ch = ch2; |
| 3476 | } |
| 3477 | if (ch == EOF) |
| 3478 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3479 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3480 | } |
| 3481 | } |
| 3482 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 3483 | * quoting and nested ()s. |
| 3484 | * "With the $(command) style of command substitution, all characters |
| 3485 | * following the open parenthesis to the matching closing parenthesis |
| 3486 | * constitute the command. Any valid shell script can be used for command, |
| 3487 | * except a script consisting solely of redirections which produces |
| 3488 | * unspecified results." |
| 3489 | * Example Output |
| 3490 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 3491 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 3492 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 3493 | */ |
| 3494 | static void add_till_closing_curly_brace(o_string *dest, struct in_str *input) |
| 3495 | { |
| 3496 | int count = 0; |
| 3497 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3498 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3499 | if (ch == EOF) |
| 3500 | break; |
| 3501 | if (ch == '(') |
| 3502 | count++; |
| 3503 | if (ch == ')') |
| 3504 | if (--count < 0) |
| 3505 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3506 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3507 | if (ch == '\'') { |
| 3508 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3509 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3510 | continue; |
| 3511 | } |
| 3512 | if (ch == '"') { |
| 3513 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3514 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3515 | continue; |
| 3516 | } |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3517 | if (ch == '\\') { /* \x. Copy verbatim. Important for \(, \) */ |
| 3518 | ch = i_getch(input); |
| 3519 | if (ch == EOF) |
| 3520 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3521 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3522 | continue; |
| 3523 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3524 | } |
| 3525 | } |
| 3526 | #endif /* ENABLE_HUSH_TICK */ |
| 3527 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3528 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3529 | static int handle_dollar(o_string *dest, struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3530 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3531 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3532 | unsigned char quote_mask = dest->o_quote ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3533 | |
| 3534 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3535 | if (isalpha(ch)) { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3536 | i_getch(input); |
| 3537 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3538 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3539 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3540 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3541 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3542 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3543 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3544 | if (!isalnum(ch) && ch != '_') |
| 3545 | break; |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3546 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3547 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3548 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3549 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3550 | make_one_char_var: |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3551 | i_getch(input); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3552 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3553 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3554 | o_addchr(dest, ch | quote_mask); |
| 3555 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3556 | } else switch (ch) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3557 | case '$': /* pid */ |
| 3558 | case '!': /* last bg pid */ |
| 3559 | case '?': /* last exit code */ |
| 3560 | case '#': /* number of args */ |
| 3561 | case '*': /* args */ |
| 3562 | case '@': /* args */ |
| 3563 | goto make_one_char_var; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3564 | case '{': |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3565 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3566 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3567 | /* XXX maybe someone will try to escape the '}' */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3568 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3569 | ch = i_getch(input); |
Denis Vlasenko | b055001 | 2007-05-24 12:18:16 +0000 | [diff] [blame] | 3570 | if (ch == '}') |
| 3571 | break; |
| 3572 | if (!isalnum(ch) && ch != '_') { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3573 | syntax("unterminated ${name}"); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3574 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 3575 | return 1; |
| 3576 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3577 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3578 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3579 | quote_mask = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3580 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3581 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3582 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3583 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3584 | case '(': { |
| 3585 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3586 | i_getch(input); |
| 3587 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3588 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3589 | add_till_closing_curly_brace(dest, input); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3590 | //debug_printf_subst("SUBST RES2 '%s'\n", dest->data + pos); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3591 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3592 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3593 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3594 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3595 | case '_': |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3596 | i_getch(input); |
| 3597 | ch = i_peek(input); |
| 3598 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 3599 | ch = '_'; |
| 3600 | goto make_var; |
| 3601 | } |
| 3602 | /* else: it's $_ */ |
| 3603 | case '-': |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3604 | /* still unhandled, but should be eventually */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3605 | bb_error_msg("unhandled syntax: $%c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3606 | return 1; |
| 3607 | break; |
| 3608 | default: |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3609 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3610 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3611 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3612 | return 0; |
| 3613 | } |
| 3614 | |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3615 | /* Scan input, call done_word() whenever full IFS delimited word was seen. |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3616 | * Call done_pipe if '\n' was seen (and end_trigger != NULL). |
| 3617 | * Return code is 0 if end_trigger char is met, |
| 3618 | * -1 on EOF (but if end_trigger == NULL then return 0), |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3619 | * 1 for syntax error */ |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3620 | static int parse_stream(o_string *dest, struct p_context *ctx, |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3621 | struct in_str *input, const char *end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3622 | { |
"Vladimir N. Oleynik" | 4ccd2b4 | 2006-01-31 09:27:48 +0000 | [diff] [blame] | 3623 | int ch, m; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3624 | int redir_fd; |
| 3625 | redir_type redir_style; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3626 | int shadow_quote = dest->o_quote; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3627 | int next; |
| 3628 | |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3629 | /* 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] | 3630 | * 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] | 3631 | * found. When recursing, quote state is passed in via dest->o_quote. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3632 | |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3633 | 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] | 3634 | |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3635 | while (1) { |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3636 | m = CHAR_IFS; |
| 3637 | next = '\0'; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3638 | ch = i_getch(input); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3639 | if (ch != EOF) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3640 | m = G.charmap[ch]; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3641 | if (ch != '\n') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3642 | next = i_peek(input); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3643 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3644 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3645 | debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n", |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3646 | ch, ch, m, dest->o_quote); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3647 | if (m == CHAR_ORDINARY |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3648 | || (m != CHAR_SPECIAL && shadow_quote) |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3649 | ) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3650 | if (ch == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3651 | syntax("unterminated \""); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3652 | debug_printf_parse("parse_stream return 1: unterminated \"\n"); |
| 3653 | return 1; |
| 3654 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3655 | o_addQchr(dest, ch); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3656 | if ((dest->o_assignment == MAYBE_ASSIGNMENT |
| 3657 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3658 | && ch == '=' |
| 3659 | && is_assignment(dest->data) |
| 3660 | ) { |
| 3661 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 3662 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3663 | continue; |
| 3664 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3665 | if (m == CHAR_IFS) { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3666 | if (done_word(dest, ctx)) { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3667 | debug_printf_parse("parse_stream return 1: done_word!=0\n"); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3668 | return 1; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3669 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3670 | if (ch == EOF) |
| 3671 | break; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3672 | /* If we aren't performing a substitution, treat |
| 3673 | * a newline as a command separator. |
| 3674 | * [why we don't handle it exactly like ';'? --vda] */ |
| 3675 | if (end_trigger && ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 3676 | #if ENABLE_HUSH_CASE |
| 3677 | /* "case ... in <newline> word) ..." - |
| 3678 | * newlines are ignored (but ';' wouldn't be) */ |
| 3679 | if (dest->length == 0 // && argv[0] == NULL |
| 3680 | && ctx->ctx_res_w == RES_MATCH |
| 3681 | ) { |
| 3682 | continue; |
| 3683 | } |
| 3684 | #endif |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3685 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3686 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3687 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3688 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3689 | if (end_trigger) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3690 | if (!shadow_quote && strchr(end_trigger, ch)) { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3691 | /* Special case: (...word) makes last word terminate, |
| 3692 | * as if ';' is seen */ |
| 3693 | if (ch == ')') { |
| 3694 | done_word(dest, ctx); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3695 | //err chk? |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3696 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3697 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3698 | } |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3699 | if (!HAS_KEYWORDS |
| 3700 | IF_HAS_KEYWORDS(|| (ctx->ctx_res_w == RES_NONE && ctx->old_flag == 0)) |
| 3701 | ) { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3702 | debug_printf_parse("parse_stream return 0: end_trigger char found\n"); |
| 3703 | return 0; |
| 3704 | } |
| 3705 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3706 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3707 | if (m == CHAR_IFS) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3708 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3709 | |
| 3710 | if (dest->o_assignment == MAYBE_ASSIGNMENT) { |
| 3711 | /* ch is a special char and thus this word |
| 3712 | * cannot be an assignment: */ |
| 3713 | dest->o_assignment = NOT_ASSIGNMENT; |
| 3714 | } |
| 3715 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3716 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3717 | case '#': |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3718 | if (dest->length == 0 && !shadow_quote) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3719 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3720 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3721 | if (ch == EOF || ch == '\n') |
| 3722 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3723 | i_getch(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3724 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3725 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3726 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3727 | } |
| 3728 | break; |
| 3729 | case '\\': |
| 3730 | if (next == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3731 | syntax("\\<eof>"); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3732 | debug_printf_parse("parse_stream return 1: \\<eof>\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3733 | return 1; |
| 3734 | } |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3735 | /* bash: |
| 3736 | * "The backslash retains its special meaning [in "..."] |
| 3737 | * only when followed by one of the following characters: |
| 3738 | * $, `, ", \, or <newline>. A double quote may be quoted |
| 3739 | * within double quotes by preceding it with a backslash. |
| 3740 | * If enabled, history expansion will be performed unless |
| 3741 | * an ! appearing in double quotes is escaped using |
| 3742 | * a backslash. The backslash preceding the ! is not removed." |
| 3743 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3744 | if (shadow_quote) { //NOT SURE dest->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3745 | if (strchr("$`\"\\", next) != NULL) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3746 | o_addqchr(dest, i_getch(input)); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3747 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3748 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3749 | } |
| 3750 | } else { |
| 3751 | o_addchr(dest, '\\'); |
| 3752 | o_addchr(dest, i_getch(input)); |
| 3753 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3754 | break; |
| 3755 | case '$': |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3756 | if (handle_dollar(dest, input) != 0) { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3757 | debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n"); |
| 3758 | return 1; |
| 3759 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3760 | break; |
| 3761 | case '\'': |
| 3762 | dest->nonnull = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3763 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3764 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3765 | if (ch == EOF) { |
| 3766 | syntax("unterminated '"); |
| 3767 | debug_printf_parse("parse_stream return 1: unterminated '\n"); |
| 3768 | return 1; |
| 3769 | } |
| 3770 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3771 | break; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3772 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 3773 | o_addqchr(dest, ch); |
| 3774 | else |
| 3775 | o_addchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3776 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3777 | break; |
| 3778 | case '"': |
| 3779 | dest->nonnull = 1; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3780 | shadow_quote ^= 1; /* invert */ |
| 3781 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 3782 | dest->o_quote ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3783 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3784 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3785 | case '`': { |
| 3786 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3787 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3788 | o_addchr(dest, shadow_quote /*or dest->o_quote??*/ ? 0x80 | '`' : '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3789 | add_till_backquote(dest, input); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3790 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3791 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3792 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3793 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3794 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3795 | case '>': |
| 3796 | redir_fd = redirect_opt_num(dest); |
| 3797 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3798 | redir_style = REDIRECT_OVERWRITE; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3799 | if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3800 | redir_style = REDIRECT_APPEND; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3801 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3802 | } |
| 3803 | #if 0 |
| 3804 | else if (next == '(') { |
| 3805 | syntax(">(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3806 | debug_printf_parse("parse_stream return 1: >(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3807 | return 1; |
| 3808 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3809 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3810 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 3811 | break; |
| 3812 | case '<': |
| 3813 | redir_fd = redirect_opt_num(dest); |
| 3814 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3815 | redir_style = REDIRECT_INPUT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3816 | if (next == '<') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3817 | redir_style = REDIRECT_HEREIS; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3818 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3819 | } else if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3820 | redir_style = REDIRECT_IO; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3821 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3822 | } |
| 3823 | #if 0 |
| 3824 | else if (next == '(') { |
| 3825 | syntax("<(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3826 | debug_printf_parse("parse_stream return 1: <(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3827 | return 1; |
| 3828 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3829 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3830 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 3831 | break; |
| 3832 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3833 | #if ENABLE_HUSH_CASE |
| 3834 | case_semi: |
| 3835 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3836 | done_word(dest, ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3837 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3838 | #if ENABLE_HUSH_CASE |
| 3839 | /* Eat multiple semicolons, detect |
| 3840 | * whether it means something special */ |
| 3841 | while (1) { |
| 3842 | ch = i_peek(input); |
| 3843 | if (ch != ';') |
| 3844 | break; |
| 3845 | i_getch(input); |
| 3846 | if (ctx->ctx_res_w == RES_CASEI) { |
| 3847 | ctx->ctx_dsemicolon = 1; |
| 3848 | ctx->ctx_res_w = RES_MATCH; |
| 3849 | break; |
| 3850 | } |
| 3851 | } |
| 3852 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3853 | new_cmd: |
| 3854 | /* We just finished a cmd. New one may start |
| 3855 | * with an assignment */ |
| 3856 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3857 | break; |
| 3858 | case '&': |
| 3859 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3860 | if (next == '&') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3861 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3862 | done_pipe(ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3863 | } else { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3864 | done_pipe(ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3865 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3866 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3867 | case '|': |
| 3868 | done_word(dest, ctx); |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 3869 | #if ENABLE_HUSH_CASE |
| 3870 | if (ctx->ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 3871 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 3872 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3873 | if (next == '|') { /* || */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3874 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3875 | done_pipe(ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3876 | } else { |
| 3877 | /* we could pick up a file descriptor choice here |
| 3878 | * with redirect_opt_num(), but bash doesn't do it. |
| 3879 | * "echo foo 2| cat" yields "foo 2". */ |
| 3880 | done_command(ctx); |
| 3881 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3882 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3883 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3884 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 3885 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3886 | if (dest->length == 0 // && argv[0] == NULL |
| 3887 | && ctx->ctx_res_w == RES_MATCH |
| 3888 | ) { |
| 3889 | continue; |
| 3890 | } |
| 3891 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3892 | case '{': |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3893 | if (parse_group(dest, ctx, input, ch) != 0) { |
| 3894 | 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] | 3895 | return 1; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3896 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3897 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3898 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3899 | #if ENABLE_HUSH_CASE |
| 3900 | if (ctx->ctx_res_w == RES_MATCH) |
| 3901 | goto case_semi; |
| 3902 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3903 | case '}': |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3904 | /* proper use of this character is caught by end_trigger */ |
| 3905 | syntax("unexpected } or )"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3906 | debug_printf_parse("parse_stream return 1: unexpected '}'\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3907 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3908 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3909 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3910 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3911 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3912 | } /* while (1) */ |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3913 | debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL)); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3914 | if (end_trigger) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3915 | return -1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3916 | return 0; |
| 3917 | } |
| 3918 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3919 | static void set_in_charmap(const char *set, int code) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3920 | { |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3921 | while (*set) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3922 | G.charmap[(unsigned char)*set++] = code; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3925 | static void update_charmap(void) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3926 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3927 | G.ifs = getenv("IFS"); |
| 3928 | if (G.ifs == NULL) |
| 3929 | G.ifs = " \t\n"; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3930 | /* Precompute a list of 'flow through' behavior so it can be treated |
| 3931 | * quickly up front. Computation is necessary because of IFS. |
| 3932 | * Special case handling of IFS == " \t\n" is not implemented. |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3933 | * The charmap[] array only really needs two bits each, |
| 3934 | * and on most machines that would be faster (reduced L1 cache use). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3935 | */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3936 | memset(G.charmap, CHAR_ORDINARY, sizeof(G.charmap)); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3937 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3938 | set_in_charmap("\\$\"`", CHAR_SPECIAL); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3939 | #else |
| 3940 | set_in_charmap("\\$\"", CHAR_SPECIAL); |
| 3941 | #endif |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3942 | set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3943 | set_in_charmap(G.ifs, CHAR_IFS); /* are ordinary if quoted */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3946 | /* Most recursion does not come through here, the exception is |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3947 | * from builtin_source() and builtin_eval() */ |
| 3948 | 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] | 3949 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3950 | struct p_context ctx; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3951 | o_string temp = NULL_O_STRING; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3952 | int rcode; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3953 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3954 | do { |
| 3955 | initialize_context(&ctx); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3956 | update_charmap(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3957 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 3958 | inp->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3959 | #endif |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3960 | /* We will stop & execute after each ';' or '\n'. |
| 3961 | * Example: "sleep 9999; echo TEST" + ctrl-C: |
| 3962 | * TEST should be printed */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3963 | temp.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3964 | rcode = parse_stream(&temp, &ctx, inp, ";\n"); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3965 | #if HAS_KEYWORDS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3966 | if (rcode != 1 && ctx.old_flag != 0) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3967 | syntax(NULL); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3968 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3969 | #endif |
| 3970 | if (rcode != 1 IF_HAS_KEYWORDS(&& ctx.old_flag == 0)) { |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3971 | done_word(&temp, &ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3972 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3973 | debug_print_tree(ctx.list_head, 0); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3974 | debug_printf_exec("parse_stream_outer: run_and_free_list\n"); |
| 3975 | run_and_free_list(ctx.list_head); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3976 | } else { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3977 | /* We arrive here also if rcode == 1 (error in parse_stream) */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3978 | #if HAS_KEYWORDS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3979 | if (ctx.old_flag != 0) { |
| 3980 | free(ctx.stack); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3981 | o_reset(&temp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3982 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3983 | #endif |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3984 | /*temp.nonnull = 0; - o_free does it below */ |
| 3985 | /*temp.o_quote = 0; - o_free does it below */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3986 | free_pipe_list(ctx.list_head, /* indent: */ 0); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3987 | /* Discard all unprocessed line input, force prompt on */ |
| 3988 | inp->p = NULL; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3989 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3990 | inp->promptme = 1; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3991 | #endif |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3992 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3993 | o_free(&temp); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3994 | /* loop on syntax errors, return on EOF: */ |
| 3995 | } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3996 | return 0; |
| 3997 | } |
| 3998 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3999 | static int parse_and_run_string(const char *s, int parse_flag) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4000 | { |
| 4001 | struct in_str input; |
| 4002 | setup_string_in_str(&input, s); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4003 | return parse_and_run_stream(&input, parse_flag); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4004 | } |
| 4005 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4006 | static int parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4007 | { |
| 4008 | int rcode; |
| 4009 | struct in_str input; |
| 4010 | setup_file_in_str(&input, f); |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4011 | rcode = parse_and_run_stream(&input, 0 /* parse_flag */); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4012 | return rcode; |
| 4013 | } |
| 4014 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4015 | #if ENABLE_HUSH_JOB |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4016 | /* Make sure we have a controlling tty. If we get started under a job |
| 4017 | * aware app (like bash for example), make sure we are now in charge so |
| 4018 | * we don't fight over who gets the foreground */ |
Eric Andersen | eaecbf3 | 2001-10-31 10:41:31 +0000 | [diff] [blame] | 4019 | static void setup_job_control(void) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4020 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4021 | pid_t shell_pgrp; |
| 4022 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4023 | shell_pgrp = getpgrp(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4024 | close_on_exec_on(G.interactive_fd); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4025 | |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 4026 | /* If we were ran as 'hush &', |
| 4027 | * sleep until we are in the foreground. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4028 | while (tcgetpgrp(G.interactive_fd) != shell_pgrp) { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4029 | /* Send TTIN to ourself (should stop us) */ |
Denis Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 4030 | kill(- shell_pgrp, SIGTTIN); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 4031 | shell_pgrp = getpgrp(); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4032 | } |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4033 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4034 | /* Ignore job-control and misc signals. */ |
| 4035 | set_jobctrl_sighandler(SIG_IGN); |
| 4036 | set_misc_sighandler(SIG_IGN); |
| 4037 | //huh? signal(SIGCHLD, SIG_IGN); |
| 4038 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4039 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4040 | set_fatal_sighandler(sigexit); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4041 | |
| 4042 | /* Put ourselves in our own process group. */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4043 | setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4044 | /* Grab control of the terminal. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4045 | tcsetpgrp(G.interactive_fd, getpid()); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4046 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4047 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4048 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4049 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 4050 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 4051 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4052 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 4053 | static const char version_str[] ALIGN1 = "HUSH_VERSION="HUSH_VER_STR; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4054 | static const struct variable const_shell_ver = { |
| 4055 | .next = NULL, |
| 4056 | .varstr = (char*)version_str, |
| 4057 | .max_len = 1, /* 0 can provoke free(name) */ |
| 4058 | .flg_export = 1, |
| 4059 | .flg_read_only = 1, |
| 4060 | }; |
| 4061 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4062 | int opt; |
| 4063 | FILE *input; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4064 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4065 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 4066 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 4067 | INIT_G(); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4068 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4069 | G.root_pid = getpid(); |
Denis Vlasenko | 16c2fea | 2008-06-17 12:28:44 +0000 | [diff] [blame] | 4070 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4071 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4072 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 4073 | G.top_var = &G.shell_ver; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4074 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 4075 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4076 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4077 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4078 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4079 | if (e) while (*e) { |
| 4080 | char *value = strchr(*e, '='); |
| 4081 | if (value) { /* paranoia */ |
| 4082 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 4083 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 4084 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4085 | cur_var->max_len = strlen(*e); |
| 4086 | cur_var->flg_export = 1; |
| 4087 | } |
| 4088 | e++; |
| 4089 | } |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4090 | putenv((char *)version_str); /* reinstate HUSH_VERSION */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 4091 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4092 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4093 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 4094 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4095 | /* XXX what should these be while sourcing /etc/profile? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4096 | G.global_argc = argc; |
| 4097 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 4098 | /* Initialize some more globals to non-zero values */ |
| 4099 | set_cwd(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4100 | #if ENABLE_HUSH_INTERACTIVE |
| 4101 | #if ENABLE_FEATURE_EDITING |
| 4102 | cmdedit_set_initial_prompt(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4103 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4104 | G.PS2 = "> "; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4105 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 4106 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4107 | if (EXIT_SUCCESS) /* otherwise is already done */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4108 | G.last_return_code = EXIT_SUCCESS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4109 | |
| 4110 | if (argv[0] && argv[0][0] == '-') { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4111 | debug_printf("sourcing /etc/profile\n"); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4112 | input = fopen_for_read("/etc/profile"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4113 | if (input != NULL) { |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 4114 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4115 | parse_and_run_file(input); |
Eric Andersen | a90f20b | 2001-06-26 23:00:21 +0000 | [diff] [blame] | 4116 | fclose(input); |
| 4117 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4118 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4119 | input = stdin; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4120 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4121 | while ((opt = getopt(argc, argv, "c:xif")) > 0) { |
| 4122 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4123 | case 'c': |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4124 | G.global_argv = argv + optind; |
| 4125 | G.global_argc = argc - optind; |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4126 | opt = parse_and_run_string(optarg, 0 /* parse_flag */); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4127 | goto final_return; |
| 4128 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4129 | /* Well, we cannot just declare interactiveness, |
| 4130 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4131 | /* G.interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4132 | break; |
| 4133 | case 'f': |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4134 | G.fake_mode = 1; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4135 | break; |
| 4136 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4137 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4138 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 4139 | " or: sh -c command [args]...\n\n"); |
| 4140 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4141 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4142 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4143 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4144 | } |
| 4145 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4146 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4147 | /* 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] | 4148 | * the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 4149 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4150 | * no arguments remaining or the -s flag given |
| 4151 | * standard input is a terminal |
| 4152 | * standard output is a terminal |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4153 | * Refer to Posix.2, the description of the 'sh' utility. */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4154 | if (argv[optind] == NULL && input == stdin |
| 4155 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 4156 | ) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4157 | G.saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 4158 | debug_printf("saved_tty_pgrp=%d\n", G.saved_tty_pgrp); |
| 4159 | if (G.saved_tty_pgrp >= 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4160 | /* try to dup to high fd#, >= 255 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4161 | G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 4162 | if (G.interactive_fd < 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4163 | /* try to dup to any fd */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4164 | G.interactive_fd = dup(STDIN_FILENO); |
| 4165 | if (G.interactive_fd < 0) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4166 | /* give up */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4167 | G.interactive_fd = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4168 | } |
Denis Vlasenko | 2f1bb36 | 2007-04-21 10:01:14 +0000 | [diff] [blame] | 4169 | // TODO: track & disallow any attempts of user |
| 4170 | // to (inadvertently) close/redirect it |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4171 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4172 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4173 | debug_printf("G.interactive_fd=%d\n", G.interactive_fd); |
| 4174 | if (G.interactive_fd) { |
| 4175 | fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4176 | /* Looks like they want an interactive shell */ |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4177 | setup_job_control(); |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 4178 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 4179 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
| 4180 | die_sleep = -1; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4181 | if (setjmp(die_jmp)) { |
| 4182 | /* xfunc has failed! die die die */ |
| 4183 | hush_exit(xfunc_error_retval); |
| 4184 | } |
Denis Vlasenko | 2f1bb36 | 2007-04-21 10:01:14 +0000 | [diff] [blame] | 4185 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 4186 | 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] | 4187 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
| 4188 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4189 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4190 | #elif ENABLE_HUSH_INTERACTIVE |
| 4191 | /* no job control compiled, only prompt/line editing */ |
| 4192 | if (argv[optind] == NULL && input == stdin |
| 4193 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 4194 | ) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4195 | G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 4196 | if (G.interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4197 | /* try to dup to any fd */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4198 | G.interactive_fd = dup(STDIN_FILENO); |
| 4199 | if (G.interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4200 | /* give up */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4201 | G.interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4202 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4203 | if (G.interactive_fd) { |
| 4204 | fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 4205 | set_misc_sighandler(SIG_IGN); |
| 4206 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4207 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4208 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4209 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4210 | if (argv[optind] == NULL) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4211 | opt = parse_and_run_file(stdin); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4212 | } else { |
| 4213 | debug_printf("\nrunning script '%s'\n", argv[optind]); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4214 | G.global_argv = argv + optind; |
| 4215 | G.global_argc = argc - optind; |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4216 | input = xfopen_for_read(argv[optind]); |
Denis Vlasenko | 459a5ad | 2008-02-11 08:35:03 +0000 | [diff] [blame] | 4217 | fcntl(fileno(input), F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4218 | opt = parse_and_run_file(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4219 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4220 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4221 | final_return: |
| 4222 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4223 | #if ENABLE_FEATURE_CLEAN_UP |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4224 | fclose(input); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4225 | if (G.cwd != bb_msg_unknown) |
| 4226 | free((char*)G.cwd); |
| 4227 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4228 | while (cur_var) { |
| 4229 | struct variable *tmp = cur_var; |
| 4230 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 4231 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4232 | cur_var = cur_var->next; |
| 4233 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4234 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4235 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4236 | hush_exit(opt ? opt : G.last_return_code); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4237 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 4238 | |
| 4239 | |
| 4240 | #if ENABLE_LASH |
| 4241 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 4242 | int lash_main(int argc, char **argv) |
| 4243 | { |
| 4244 | //bb_error_msg("lash is deprecated, please use hush instead"); |
| 4245 | return hush_main(argc, argv); |
| 4246 | } |
| 4247 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4248 | |
| 4249 | |
| 4250 | /* |
| 4251 | * Built-ins |
| 4252 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4253 | static int builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4254 | { |
| 4255 | return 0; |
| 4256 | } |
| 4257 | |
| 4258 | static int builtin_test(char **argv) |
| 4259 | { |
| 4260 | int argc = 0; |
| 4261 | while (*argv) { |
| 4262 | argc++; |
| 4263 | argv++; |
| 4264 | } |
| 4265 | return test_main(argc, argv - argc); |
| 4266 | } |
| 4267 | |
| 4268 | static int builtin_echo(char **argv) |
| 4269 | { |
| 4270 | int argc = 0; |
| 4271 | while (*argv) { |
| 4272 | argc++; |
| 4273 | argv++; |
| 4274 | } |
| 4275 | return echo_main(argc, argv - argc); |
| 4276 | } |
| 4277 | |
| 4278 | static int builtin_eval(char **argv) |
| 4279 | { |
| 4280 | int rcode = EXIT_SUCCESS; |
| 4281 | |
| 4282 | if (argv[1]) { |
| 4283 | char *str = expand_strvec_to_string(argv + 1); |
| 4284 | parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP); |
| 4285 | free(str); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4286 | rcode = G.last_return_code; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4287 | } |
| 4288 | return rcode; |
| 4289 | } |
| 4290 | |
| 4291 | static int builtin_cd(char **argv) |
| 4292 | { |
| 4293 | const char *newdir; |
| 4294 | if (argv[1] == NULL) { |
| 4295 | // bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
| 4296 | // bash says "bash: cd: HOME not set" and does nothing (exitcode 1) |
| 4297 | newdir = getenv("HOME") ? : "/"; |
| 4298 | } else |
| 4299 | newdir = argv[1]; |
| 4300 | if (chdir(newdir)) { |
| 4301 | printf("cd: %s: %s\n", newdir, strerror(errno)); |
| 4302 | return EXIT_FAILURE; |
| 4303 | } |
| 4304 | set_cwd(); |
| 4305 | return EXIT_SUCCESS; |
| 4306 | } |
| 4307 | |
| 4308 | static int builtin_exec(char **argv) |
| 4309 | { |
| 4310 | if (argv[1] == NULL) |
| 4311 | return EXIT_SUCCESS; /* bash does this */ |
| 4312 | { |
| 4313 | #if !BB_MMU |
| 4314 | char **ptrs2free = alloc_ptrs(argv); |
| 4315 | #endif |
| 4316 | // FIXME: if exec fails, bash does NOT exit! We do... |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4317 | pseudo_exec_argv(ptrs2free, argv + 1, 0, NULL); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4318 | /* never returns */ |
| 4319 | } |
| 4320 | } |
| 4321 | |
| 4322 | static int builtin_exit(char **argv) |
| 4323 | { |
| 4324 | // TODO: bash does it ONLY on top-level sh exit (+interacive only?) |
| 4325 | //puts("exit"); /* bash does it */ |
| 4326 | // TODO: warn if we have background jobs: "There are stopped jobs" |
| 4327 | // On second consecutive 'exit', exit anyway. |
| 4328 | if (argv[1] == NULL) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4329 | hush_exit(G.last_return_code); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4330 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 4331 | xfunc_error_retval = 255; |
| 4332 | /* bash: exit -2 == exit 254, no error msg */ |
| 4333 | hush_exit(xatoi(argv[1]) & 0xff); |
| 4334 | } |
| 4335 | |
| 4336 | static int builtin_export(char **argv) |
| 4337 | { |
| 4338 | const char *value; |
| 4339 | char *name = argv[1]; |
| 4340 | |
| 4341 | if (name == NULL) { |
| 4342 | // TODO: |
| 4343 | // ash emits: export VAR='VAL' |
| 4344 | // bash: declare -x VAR="VAL" |
| 4345 | // (both also escape as needed (quotes, $, etc)) |
| 4346 | char **e = environ; |
| 4347 | if (e) |
| 4348 | while (*e) |
| 4349 | puts(*e++); |
| 4350 | return EXIT_SUCCESS; |
| 4351 | } |
| 4352 | |
| 4353 | value = strchr(name, '='); |
| 4354 | if (!value) { |
| 4355 | /* They are exporting something without a =VALUE */ |
| 4356 | struct variable *var; |
| 4357 | |
| 4358 | var = get_local_var(name); |
| 4359 | if (var) { |
| 4360 | var->flg_export = 1; |
| 4361 | putenv(var->varstr); |
| 4362 | } |
| 4363 | /* bash does not return an error when trying to export |
| 4364 | * an undefined variable. Do likewise. */ |
| 4365 | return EXIT_SUCCESS; |
| 4366 | } |
| 4367 | |
| 4368 | set_local_var(xstrdup(name), 1); |
| 4369 | return EXIT_SUCCESS; |
| 4370 | } |
| 4371 | |
| 4372 | #if ENABLE_HUSH_JOB |
| 4373 | /* built-in 'fg' and 'bg' handler */ |
| 4374 | static int builtin_fg_bg(char **argv) |
| 4375 | { |
| 4376 | int i, jobnum; |
| 4377 | struct pipe *pi; |
| 4378 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4379 | if (!G.interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4380 | return EXIT_FAILURE; |
| 4381 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 4382 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4383 | for (pi = G.job_list; pi; pi = pi->next) { |
| 4384 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4385 | goto found; |
| 4386 | } |
| 4387 | } |
| 4388 | bb_error_msg("%s: no current job", argv[0]); |
| 4389 | return EXIT_FAILURE; |
| 4390 | } |
| 4391 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 4392 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 4393 | return EXIT_FAILURE; |
| 4394 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4395 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4396 | if (pi->jobid == jobnum) { |
| 4397 | goto found; |
| 4398 | } |
| 4399 | } |
| 4400 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 4401 | return EXIT_FAILURE; |
| 4402 | found: |
| 4403 | // TODO: bash prints a string representation |
| 4404 | // of job being foregrounded (like "sleep 1 | cat") |
| 4405 | if (*argv[0] == 'f') { |
| 4406 | /* Put the job into the foreground. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4407 | tcsetpgrp(G.interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4408 | } |
| 4409 | |
| 4410 | /* Restart the processes in the job */ |
| 4411 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_progs, pi->pgrp); |
| 4412 | for (i = 0; i < pi->num_progs; i++) { |
| 4413 | debug_printf_jobs("reviving pid %d\n", pi->progs[i].pid); |
| 4414 | pi->progs[i].is_stopped = 0; |
| 4415 | } |
| 4416 | pi->stopped_progs = 0; |
| 4417 | |
| 4418 | i = kill(- pi->pgrp, SIGCONT); |
| 4419 | if (i < 0) { |
| 4420 | if (errno == ESRCH) { |
| 4421 | delete_finished_bg_job(pi); |
| 4422 | return EXIT_SUCCESS; |
| 4423 | } else { |
| 4424 | bb_perror_msg("kill (SIGCONT)"); |
| 4425 | } |
| 4426 | } |
| 4427 | |
| 4428 | if (*argv[0] == 'f') { |
| 4429 | remove_bg_job(pi); |
| 4430 | return checkjobs_and_fg_shell(pi); |
| 4431 | } |
| 4432 | return EXIT_SUCCESS; |
| 4433 | } |
| 4434 | #endif |
| 4435 | |
| 4436 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4437 | static int builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4438 | { |
| 4439 | const struct built_in_command *x; |
| 4440 | |
| 4441 | printf("\nBuilt-in commands:\n"); |
| 4442 | printf("-------------------\n"); |
| 4443 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 4444 | printf("%s\t%s\n", x->cmd, x->descr); |
| 4445 | } |
| 4446 | printf("\n\n"); |
| 4447 | return EXIT_SUCCESS; |
| 4448 | } |
| 4449 | #endif |
| 4450 | |
| 4451 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4452 | static int builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4453 | { |
| 4454 | struct pipe *job; |
| 4455 | const char *status_string; |
| 4456 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4457 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4458 | if (job->alive_progs == job->stopped_progs) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4459 | status_string = "Stopped"; |
| 4460 | else |
| 4461 | status_string = "Running"; |
| 4462 | |
| 4463 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 4464 | } |
| 4465 | return EXIT_SUCCESS; |
| 4466 | } |
| 4467 | #endif |
| 4468 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4469 | static int builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4470 | { |
| 4471 | puts(set_cwd()); |
| 4472 | return EXIT_SUCCESS; |
| 4473 | } |
| 4474 | |
| 4475 | static int builtin_read(char **argv) |
| 4476 | { |
| 4477 | char *string; |
| 4478 | const char *name = argv[1] ? argv[1] : "REPLY"; |
| 4479 | |
| 4480 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
| 4481 | return set_local_var(string, 0); |
| 4482 | } |
| 4483 | |
| 4484 | /* built-in 'set [VAR=value]' handler */ |
| 4485 | static int builtin_set(char **argv) |
| 4486 | { |
| 4487 | char *temp = argv[1]; |
| 4488 | struct variable *e; |
| 4489 | |
| 4490 | if (temp == NULL) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4491 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4492 | puts(e->varstr); |
| 4493 | else |
| 4494 | set_local_var(xstrdup(temp), 0); |
| 4495 | |
| 4496 | return EXIT_SUCCESS; |
| 4497 | } |
| 4498 | |
| 4499 | static int builtin_shift(char **argv) |
| 4500 | { |
| 4501 | int n = 1; |
| 4502 | if (argv[1]) { |
| 4503 | n = atoi(argv[1]); |
| 4504 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4505 | if (n >= 0 && n < G.global_argc) { |
| 4506 | G.global_argv[n] = G.global_argv[0]; |
| 4507 | G.global_argc -= n; |
| 4508 | G.global_argv += n; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4509 | return EXIT_SUCCESS; |
| 4510 | } |
| 4511 | return EXIT_FAILURE; |
| 4512 | } |
| 4513 | |
| 4514 | static int builtin_source(char **argv) |
| 4515 | { |
| 4516 | FILE *input; |
| 4517 | int status; |
| 4518 | |
| 4519 | if (argv[1] == NULL) |
| 4520 | return EXIT_FAILURE; |
| 4521 | |
| 4522 | /* XXX search through $PATH is missing */ |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4523 | input = fopen_for_read(argv[1]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4524 | if (!input) { |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 4525 | bb_error_msg("can't open '%s'", argv[1]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4526 | return EXIT_FAILURE; |
| 4527 | } |
| 4528 | close_on_exec_on(fileno(input)); |
| 4529 | |
| 4530 | /* Now run the file */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4531 | /* 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] | 4532 | * (pointer only is OK!) on this stack frame, |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4533 | * set G.global_argv=argv+1, recurse, and restore. */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4534 | status = parse_and_run_file(input); |
| 4535 | fclose(input); |
| 4536 | return status; |
| 4537 | } |
| 4538 | |
| 4539 | static int builtin_umask(char **argv) |
| 4540 | { |
| 4541 | mode_t new_umask; |
| 4542 | const char *arg = argv[1]; |
| 4543 | char *end; |
| 4544 | if (arg) { |
| 4545 | new_umask = strtoul(arg, &end, 8); |
| 4546 | if (*end != '\0' || end == arg) { |
| 4547 | return EXIT_FAILURE; |
| 4548 | } |
| 4549 | } else { |
| 4550 | new_umask = umask(0); |
| 4551 | printf("%.3o\n", (unsigned) new_umask); |
| 4552 | } |
| 4553 | umask(new_umask); |
| 4554 | return EXIT_SUCCESS; |
| 4555 | } |
| 4556 | |
| 4557 | static int builtin_unset(char **argv) |
| 4558 | { |
| 4559 | /* bash always returns true */ |
| 4560 | unset_local_var(argv[1]); |
| 4561 | return EXIT_SUCCESS; |
| 4562 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4563 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4564 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4565 | static int builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4566 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4567 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 4568 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4569 | return EXIT_SUCCESS; /* bash compat */ |
| 4570 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4571 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
| 4572 | G.depth_break_continue = 1; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4573 | if (argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4574 | G.depth_break_continue = bb_strtou(argv[1], NULL, 10); |
| 4575 | if (errno || !G.depth_break_continue || argv[2]) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 4576 | bb_error_msg("%s: bad arguments", argv[0]); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4577 | G.flag_break_continue = BC_BREAK; |
| 4578 | G.depth_break_continue = UINT_MAX; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4579 | } |
| 4580 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4581 | if (G.depth_of_loop < G.depth_break_continue) |
| 4582 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4583 | return EXIT_SUCCESS; |
| 4584 | } |
| 4585 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4586 | static int builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4587 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 4588 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 4589 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4590 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4591 | #endif |