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 |
| 57 | * builtins: break, continue, eval, return, set, trap, ulimit |
| 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 | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 70 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 71 | #include <glob.h> /* glob, of course */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 72 | /* #include <dmalloc.h> */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 74 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 75 | |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 76 | // TEMP |
| 77 | #define ENABLE_HUSH_CASE 0 |
| 78 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 79 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 80 | #if !BB_MMU && ENABLE_HUSH_TICK |
| 81 | //#undef ENABLE_HUSH_TICK |
| 82 | //#define ENABLE_HUSH_TICK 0 |
| 83 | #warning On NOMMU, hush command substitution is dangerous. |
| 84 | #warning Dont use it for commands which produce lots of output. |
| 85 | #warning For more info see shell/hush.c, generate_stream_from_list(). |
| 86 | #endif |
| 87 | |
| 88 | #if !BB_MMU && ENABLE_HUSH_JOB |
| 89 | #undef ENABLE_HUSH_JOB |
| 90 | #define ENABLE_HUSH_JOB 0 |
| 91 | #endif |
| 92 | |
| 93 | #if !ENABLE_HUSH_INTERACTIVE |
| 94 | #undef ENABLE_FEATURE_EDITING |
| 95 | #define ENABLE_FEATURE_EDITING 0 |
| 96 | #undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 97 | #define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 98 | #endif |
| 99 | |
| 100 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 101 | /* If you comment out one of these below, it will be #defined later |
| 102 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 103 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 104 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 105 | #define debug_printf_parse(...) do {} while (0) |
| 106 | #define debug_print_tree(a, b) do {} while (0) |
| 107 | #define debug_printf_exec(...) do {} while (0) |
| 108 | #define debug_printf_jobs(...) do {} while (0) |
| 109 | #define debug_printf_expand(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 110 | #define debug_printf_glob(...) do {} while (0) |
| 111 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 112 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 113 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 114 | |
| 115 | #ifndef debug_printf |
| 116 | #define debug_printf(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 117 | #endif |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 118 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 119 | #ifndef debug_printf_parse |
| 120 | #define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 121 | #endif |
| 122 | |
| 123 | #ifndef debug_printf_exec |
| 124 | #define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__) |
| 125 | #endif |
| 126 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 127 | #ifndef debug_printf_jobs |
| 128 | #define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 129 | #define DEBUG_JOBS 1 |
| 130 | #else |
| 131 | #define DEBUG_JOBS 0 |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 132 | #endif |
| 133 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 134 | #ifndef debug_printf_expand |
| 135 | #define debug_printf_expand(...) fprintf(stderr, __VA_ARGS__) |
| 136 | #define DEBUG_EXPAND 1 |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 137 | #else |
| 138 | #define DEBUG_EXPAND 0 |
| 139 | #endif |
| 140 | |
| 141 | #ifndef debug_printf_glob |
| 142 | #define debug_printf_glob(...) fprintf(stderr, __VA_ARGS__) |
| 143 | #define DEBUG_GLOB 1 |
| 144 | #else |
| 145 | #define DEBUG_GLOB 0 |
| 146 | #endif |
| 147 | |
| 148 | #ifndef debug_printf_list |
| 149 | #define debug_printf_list(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 150 | #endif |
| 151 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 152 | #ifndef debug_printf_subst |
| 153 | #define debug_printf_subst(...) fprintf(stderr, __VA_ARGS__) |
| 154 | #endif |
| 155 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 156 | #ifndef debug_printf_clean |
| 157 | /* broken, of course, but OK for testing */ |
| 158 | static const char *indenter(int i) |
| 159 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 160 | static const char blanks[] ALIGN1 = |
| 161 | " "; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 162 | return &blanks[sizeof(blanks) - i - 1]; |
| 163 | } |
| 164 | #define debug_printf_clean(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 165 | #define DEBUG_CLEAN 1 |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 166 | #endif |
| 167 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 168 | #if DEBUG_EXPAND |
| 169 | static void debug_print_strings(const char *prefix, char **vv) |
| 170 | { |
| 171 | fprintf(stderr, "%s:\n", prefix); |
| 172 | while (*vv) |
| 173 | fprintf(stderr, " '%s'\n", *vv++); |
| 174 | } |
| 175 | #else |
| 176 | #define debug_print_strings(prefix, vv) ((void)0) |
| 177 | #endif |
| 178 | |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 179 | /* |
| 180 | * Leak hunting. Use hush_leaktool.sh for post-processing. |
| 181 | */ |
| 182 | #ifdef FOR_HUSH_LEAKTOOL |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 183 | /* suppress "warning: no previous prototype..." */ |
| 184 | void *xxmalloc(int lineno, size_t size); |
| 185 | void *xxrealloc(int lineno, void *ptr, size_t size); |
| 186 | char *xxstrdup(int lineno, const char *str); |
| 187 | void xxfree(void *ptr); |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 188 | void *xxmalloc(int lineno, size_t size) |
| 189 | { |
| 190 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 191 | fprintf(stderr, "line %d: malloc %p\n", lineno, ptr); |
| 192 | return ptr; |
| 193 | } |
| 194 | void *xxrealloc(int lineno, void *ptr, size_t size) |
| 195 | { |
| 196 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 197 | fprintf(stderr, "line %d: realloc %p\n", lineno, ptr); |
| 198 | return ptr; |
| 199 | } |
| 200 | char *xxstrdup(int lineno, const char *str) |
| 201 | { |
| 202 | char *ptr = xstrdup(str); |
| 203 | fprintf(stderr, "line %d: strdup %p\n", lineno, ptr); |
| 204 | return ptr; |
| 205 | } |
| 206 | void xxfree(void *ptr) |
| 207 | { |
| 208 | fprintf(stderr, "free %p\n", ptr); |
| 209 | free(ptr); |
| 210 | } |
| 211 | #define xmalloc(s) xxmalloc(__LINE__, s) |
| 212 | #define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 213 | #define xstrdup(s) xxstrdup(__LINE__, s) |
| 214 | #define free(p) xxfree(p) |
| 215 | #endif |
| 216 | |
| 217 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 218 | /* Keep unconditionally on for now */ |
| 219 | #define HUSH_DEBUG 1 |
| 220 | /* Do we support ANY keywords? */ |
| 221 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS |
| 222 | #define HAS_KEYWORDS 1 |
| 223 | #define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 224 | #define IF_HAS_NO_KEYWORDS(...) |
| 225 | #else |
| 226 | #define HAS_KEYWORDS 0 |
| 227 | #define IF_HAS_KEYWORDS(...) |
| 228 | #define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
| 229 | #endif |
| 230 | |
| 231 | |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 232 | #define SPECIAL_VAR_SYMBOL 3 |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 233 | #define PARSEFLAG_EXIT_FROM_LOOP 1 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 234 | |
| 235 | typedef enum { |
| 236 | REDIRECT_INPUT = 1, |
| 237 | REDIRECT_OVERWRITE = 2, |
| 238 | REDIRECT_APPEND = 3, |
| 239 | REDIRECT_HEREIS = 4, |
| 240 | REDIRECT_IO = 5 |
| 241 | } redir_type; |
| 242 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 243 | /* The descrip member of this structure is only used to make |
| 244 | * debugging output pretty */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 245 | static const struct { |
| 246 | int mode; |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 247 | signed char default_fd; |
| 248 | char descrip[3]; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 249 | } redir_table[] = { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 250 | { 0, 0, "()" }, |
| 251 | { O_RDONLY, 0, "<" }, |
| 252 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 253 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 254 | { O_RDONLY, -1, "<<" }, |
| 255 | { O_RDWR, 1, "<>" } |
| 256 | }; |
| 257 | |
| 258 | typedef enum { |
| 259 | PIPE_SEQ = 1, |
| 260 | PIPE_AND = 2, |
| 261 | PIPE_OR = 3, |
| 262 | PIPE_BG = 4, |
| 263 | } pipe_style; |
| 264 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 265 | typedef enum { |
| 266 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 267 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 268 | RES_IF , |
| 269 | RES_THEN , |
| 270 | RES_ELIF , |
| 271 | RES_ELSE , |
| 272 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 273 | #endif |
| 274 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 275 | RES_FOR , |
| 276 | RES_WHILE , |
| 277 | RES_UNTIL , |
| 278 | RES_DO , |
| 279 | RES_DONE , |
| 280 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 281 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 282 | #if ENABLE_HUSH_CASE |
| 283 | RES_CASE , |
| 284 | /* two pseudo-keywords support contrived "case" syntax: */ |
| 285 | RES_MATCH , /* "word)" */ |
| 286 | RES_CASEI , /* "this command is inside CASE" */ |
| 287 | RES_ESAC , |
| 288 | #endif |
| 289 | RES_XXXX , |
| 290 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 291 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 292 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 293 | /* This holds pointers to the various results of parsing */ |
| 294 | struct p_context { |
| 295 | struct child_prog *child; |
| 296 | struct pipe *list_head; |
| 297 | struct pipe *pipe; |
| 298 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 299 | #if HAS_KEYWORDS |
| 300 | smallint ctx_res_w; |
| 301 | smallint ctx_inverted; /* "! cmd | cmd" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 302 | #if ENABLE_HUSH_CASE |
| 303 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 304 | #endif |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 305 | 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] | 306 | struct p_context *stack; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 307 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 311 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 312 | char *rd_filename; /* filename */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 313 | int fd; /* file descriptor being redirected */ |
| 314 | int dup; /* -1, or file descriptor being duplicated */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 315 | smallint /*enum redir_type*/ rd_type; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 316 | }; |
| 317 | |
| 318 | struct child_prog { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 319 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 320 | smallint is_stopped; /* is the program currently running? */ |
| 321 | smallint subshell; /* flag, non-zero if group must be forked */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 322 | char **argv; /* program name and arguments */ |
| 323 | struct pipe *group; /* if non-NULL, first in group or subshell */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 324 | struct redir_struct *redirects; /* I/O redirections */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 325 | struct pipe *family; /* pointer back to the child's parent pipe */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 326 | }; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 327 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 328 | * and on execution these are substituted with their values. |
| 329 | * Substitution can make _several_ words out of one argv[n]! |
| 330 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 331 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 332 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 333 | |
| 334 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 335 | struct pipe *next; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 336 | int num_progs; /* total number of programs in job */ |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 337 | int alive_progs; /* number of programs running (not exited) */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 338 | int stopped_progs; /* number of programs alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 339 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 340 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 341 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 342 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 343 | #endif |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 344 | struct child_prog *progs; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 345 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 346 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 347 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 348 | }; |
| 349 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 350 | /* On program start, environ points to initial environment. |
| 351 | * putenv adds new pointers into it, unsetenv removes them. |
| 352 | * Neither of these (de)allocates the strings. |
| 353 | * setenv allocates new strings in malloc space and does putenv, |
| 354 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 355 | #define setenv(...) setenv_is_leaky_dont_use() |
| 356 | struct variable { |
| 357 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 358 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 359 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 360 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 361 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 362 | }; |
| 363 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 364 | typedef struct { |
| 365 | char *data; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 366 | int length; /* position where data is appended */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 367 | int maxlen; |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 368 | /* Misnomer! it's not "quoting", it's "protection against globbing"! |
| 369 | * (by prepending \ to *, ?, [ and to \ too) */ |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 370 | smallint o_quote; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 371 | smallint o_glob; |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 372 | smallint nonnull; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 373 | smallint has_empty_slot; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 374 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 375 | } o_string; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 376 | enum { |
| 377 | MAYBE_ASSIGNMENT = 0, |
| 378 | DEFINITELY_ASSIGNMENT = 1, |
| 379 | NOT_ASSIGNMENT = 2, |
| 380 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 381 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 382 | #define NULL_O_STRING { NULL } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 383 | |
| 384 | /* I can almost use ordinary FILE *. Is open_memstream() universally |
| 385 | * available? Where is it documented? */ |
| 386 | struct in_str { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 387 | const char *p; |
| 388 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 389 | char eof_flag; /* meaningless if ->p == NULL */ |
| 390 | char peek_buf[2]; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 391 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 392 | smallint promptme; |
| 393 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 394 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 395 | FILE *file; |
| 396 | int (*get) (struct in_str *); |
| 397 | int (*peek) (struct in_str *); |
| 398 | }; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 399 | #define i_getch(input) ((input)->get(input)) |
| 400 | #define i_peek(input) ((input)->peek(input)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 401 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 402 | enum { |
| 403 | CHAR_ORDINARY = 0, |
| 404 | CHAR_ORDINARY_IF_QUOTED = 1, /* example: *, # */ |
| 405 | CHAR_IFS = 2, /* treated as ordinary if quoted */ |
| 406 | CHAR_SPECIAL = 3, /* example: $ */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 407 | }; |
| 408 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 409 | #define HUSH_VER_STR "0.02" |
| 410 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 411 | /* "Globals" within this file */ |
| 412 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 413 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 414 | struct globals { |
| 415 | #if ENABLE_HUSH_INTERACTIVE |
| 416 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 417 | * _AND_ if we decided to act interactively */ |
| 418 | int interactive_fd; |
| 419 | const char *PS1; |
| 420 | const char *PS2; |
| 421 | #endif |
| 422 | #if ENABLE_FEATURE_EDITING |
| 423 | line_input_t *line_input_state; |
| 424 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 425 | pid_t root_pid; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 426 | #if ENABLE_HUSH_JOB |
| 427 | int run_list_level; |
| 428 | pid_t saved_task_pgrp; |
| 429 | pid_t saved_tty_pgrp; |
| 430 | int last_jobid; |
| 431 | struct pipe *job_list; |
| 432 | struct pipe *toplevel_list; |
| 433 | smallint ctrl_z_flag; |
| 434 | #endif |
| 435 | smallint fake_mode; |
| 436 | /* these three support $?, $#, and $1 */ |
| 437 | char **global_argv; |
| 438 | int global_argc; |
| 439 | int last_return_code; |
| 440 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 441 | const char *cwd; |
| 442 | unsigned last_bg_pid; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 443 | struct variable *top_var; /* = &shell_ver (set in main()) */ |
| 444 | struct variable shell_ver; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 445 | #if ENABLE_FEATURE_SH_STANDALONE |
| 446 | struct nofork_save_area nofork_save; |
| 447 | #endif |
| 448 | #if ENABLE_HUSH_JOB |
| 449 | sigjmp_buf toplevel_jb; |
| 450 | #endif |
| 451 | unsigned char charmap[256]; |
| 452 | char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2]; |
| 453 | }; |
| 454 | |
| 455 | #define G (*ptr_to_globals) |
| 456 | |
| 457 | #if !ENABLE_HUSH_INTERACTIVE |
| 458 | enum { interactive_fd = 0 }; |
| 459 | #endif |
| 460 | #if !ENABLE_HUSH_JOB |
| 461 | enum { run_list_level = 0 }; |
| 462 | #endif |
| 463 | |
| 464 | #if ENABLE_HUSH_INTERACTIVE |
| 465 | #define interactive_fd (G.interactive_fd ) |
| 466 | #define PS1 (G.PS1 ) |
| 467 | #define PS2 (G.PS2 ) |
| 468 | #endif |
| 469 | #if ENABLE_FEATURE_EDITING |
| 470 | #define line_input_state (G.line_input_state) |
| 471 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 472 | #define root_pid (G.root_pid ) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 473 | #if ENABLE_HUSH_JOB |
| 474 | #define run_list_level (G.run_list_level ) |
| 475 | #define saved_task_pgrp (G.saved_task_pgrp ) |
| 476 | #define saved_tty_pgrp (G.saved_tty_pgrp ) |
| 477 | #define last_jobid (G.last_jobid ) |
| 478 | #define job_list (G.job_list ) |
| 479 | #define toplevel_list (G.toplevel_list ) |
| 480 | #define toplevel_jb (G.toplevel_jb ) |
| 481 | #define ctrl_z_flag (G.ctrl_z_flag ) |
| 482 | #endif /* JOB */ |
| 483 | #define global_argv (G.global_argv ) |
| 484 | #define global_argc (G.global_argc ) |
| 485 | #define last_return_code (G.last_return_code) |
| 486 | #define ifs (G.ifs ) |
| 487 | #define fake_mode (G.fake_mode ) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 488 | #define cwd (G.cwd ) |
| 489 | #define last_bg_pid (G.last_bg_pid ) |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 490 | #define top_var (G.top_var ) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 491 | #define shell_ver (G.shell_ver ) |
| 492 | #if ENABLE_FEATURE_SH_STANDALONE |
| 493 | #define nofork_save (G.nofork_save ) |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 494 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 495 | #if ENABLE_HUSH_JOB |
| 496 | #define toplevel_jb (G.toplevel_jb ) |
| 497 | #endif |
| 498 | #define charmap (G.charmap ) |
| 499 | #define user_input_buf (G.user_input_buf ) |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 500 | #define INIT_G() do { \ |
| 501 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 502 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 503 | |
| 504 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 505 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
| 506 | |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 507 | #if 1 |
| 508 | /* Normal */ |
| 509 | static void syntax(const char *msg) |
| 510 | { |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 511 | /* Was using fancy stuff: |
| 512 | * (interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...) |
| 513 | * but it SEGVs. ?! Oh well... explicit temp ptr works around that */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 514 | void FAST_FUNC (*fp)(const char *s, ...); |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 515 | |
| 516 | fp = (interactive_fd ? bb_error_msg : bb_error_msg_and_die); |
| 517 | fp(msg ? "%s: %s" : "syntax error", "syntax error", msg); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 518 | } |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 519 | |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 520 | #else |
| 521 | /* Debug */ |
| 522 | static void syntax_lineno(int line) |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 523 | { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 524 | void FAST_FUNC (*fp)(const char *s, ...); |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 525 | |
| 526 | fp = (interactive_fd ? bb_error_msg : bb_error_msg_and_die); |
| 527 | fp("syntax error hush.c:%d", line); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 528 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 529 | #define syntax(str) syntax_lineno(__LINE__) |
| 530 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 531 | |
| 532 | /* Index of subroutines: */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 533 | /* in_str manipulations: */ |
| 534 | static int static_get(struct in_str *i); |
| 535 | static int static_peek(struct in_str *i); |
| 536 | static int file_get(struct in_str *i); |
| 537 | static int file_peek(struct in_str *i); |
| 538 | static void setup_file_in_str(struct in_str *i, FILE *f); |
| 539 | 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] | 540 | /* "run" the final data structures: */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 541 | #if !defined(DEBUG_CLEAN) |
| 542 | #define free_pipe_list(head, indent) free_pipe_list(head) |
| 543 | #define free_pipe(pi, indent) free_pipe(pi) |
| 544 | #endif |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 545 | static int free_pipe_list(struct pipe *head, int indent); |
| 546 | static int free_pipe(struct pipe *pi, int indent); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 547 | /* really run the final data structures: */ |
| 548 | static int setup_redirects(struct child_prog *prog, int squirrel[]); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 549 | static int run_list(struct pipe *pi); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 550 | #if BB_MMU |
| 551 | #define pseudo_exec_argv(ptrs2free, argv) pseudo_exec_argv(argv) |
| 552 | #define pseudo_exec(ptrs2free, child) pseudo_exec(child) |
| 553 | #endif |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 554 | static void pseudo_exec_argv(char **ptrs2free, char **argv) NORETURN; |
| 555 | static void pseudo_exec(char **ptrs2free, struct child_prog *child) NORETURN; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 556 | static int run_pipe(struct pipe *pi); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 557 | /* data structure manipulation: */ |
| 558 | static int setup_redirect(struct p_context *ctx, int fd, redir_type style, struct in_str *input); |
| 559 | static void initialize_context(struct p_context *ctx); |
| 560 | static int done_word(o_string *dest, struct p_context *ctx); |
| 561 | static int done_command(struct p_context *ctx); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 562 | static void done_pipe(struct p_context *ctx, pipe_style type); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 563 | /* primary string parsing: */ |
| 564 | static int redirect_dup_num(struct in_str *input); |
| 565 | static int redirect_opt_num(o_string *o); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 566 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 567 | static int process_command_subs(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 568 | struct in_str *input, const char *subst_end); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 569 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 570 | 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] | 571 | static const char *lookup_param(const char *src); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 572 | static int handle_dollar(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 573 | struct in_str *input); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 574 | 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] | 575 | /* setup: */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 576 | static int parse_and_run_stream(struct in_str *inp, int parse_flag); |
| 577 | static int parse_and_run_string(const char *s, int parse_flag); |
| 578 | static int parse_and_run_file(FILE *f); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 579 | /* job management: */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 580 | static int checkjobs(struct pipe* fg_pipe); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 581 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 582 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 583 | static void insert_bg_job(struct pipe *pi); |
| 584 | static void remove_bg_job(struct pipe *pi); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 585 | static void delete_finished_bg_job(struct pipe *pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 586 | #else |
| 587 | int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */ |
| 588 | #endif |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 589 | /* local variable support */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 590 | static char **expand_strvec_to_strvec(char **argv); |
| 591 | /* used for eval */ |
| 592 | static char *expand_strvec_to_string(char **argv); |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 593 | /* used for expansion of right hand of assignments */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 594 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 595 | static struct variable *get_local_var(const char *name); |
| 596 | static int set_local_var(char *str, int flg_export); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 597 | static void unset_local_var(const char *name); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 598 | |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 599 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 600 | static int glob_needed(const char *s) |
| 601 | { |
| 602 | while (*s) { |
| 603 | if (*s == '\\') |
| 604 | s++; |
| 605 | if (*s == '*' || *s == '[' || *s == '?') |
| 606 | return 1; |
| 607 | s++; |
| 608 | } |
| 609 | return 0; |
| 610 | } |
| 611 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 612 | static int is_assignment(const char *s) |
| 613 | { |
| 614 | if (!s || !isalpha(*s)) |
| 615 | return 0; |
| 616 | s++; |
| 617 | while (isalnum(*s) || *s == '_') |
| 618 | s++; |
| 619 | return *s == '='; |
| 620 | } |
| 621 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 622 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 623 | static char *unbackslash(char *src) |
| 624 | { |
| 625 | char *dst = src; |
| 626 | while (1) { |
| 627 | if (*src == '\\') |
| 628 | src++; |
| 629 | if ((*dst++ = *src++) == '\0') |
| 630 | break; |
| 631 | } |
| 632 | return dst; |
| 633 | } |
| 634 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 635 | static char **add_malloced_strings_to_strings(char **strings, char **add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 636 | { |
| 637 | int i; |
| 638 | unsigned count1; |
| 639 | unsigned count2; |
| 640 | char **v; |
| 641 | |
| 642 | v = strings; |
| 643 | count1 = 0; |
| 644 | if (v) { |
| 645 | while (*v) { |
| 646 | count1++; |
| 647 | v++; |
| 648 | } |
| 649 | } |
| 650 | count2 = 0; |
| 651 | v = add; |
| 652 | while (*v) { |
| 653 | count2++; |
| 654 | v++; |
| 655 | } |
| 656 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 657 | v[count1 + count2] = NULL; |
| 658 | i = count2; |
| 659 | while (--i >= 0) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 660 | v[count1 + i] = add[i]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 661 | return v; |
| 662 | } |
| 663 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 664 | static char **add_malloced_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 665 | { |
| 666 | char *v[2]; |
| 667 | |
| 668 | v[0] = add; |
| 669 | v[1] = NULL; |
| 670 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 671 | return add_malloced_strings_to_strings(strings, v); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | static void free_strings(char **strings) |
| 675 | { |
| 676 | if (strings) { |
| 677 | char **v = strings; |
| 678 | while (*v) |
| 679 | free(*v++); |
| 680 | free(strings); |
| 681 | } |
| 682 | } |
| 683 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 684 | #if !BB_MMU |
| 685 | #define EXTRA_PTRS 5 /* 1 for NULL, 1 for args, 3 for paranoid reasons */ |
| 686 | static char **alloc_ptrs(char **argv) |
| 687 | { |
| 688 | char **v = argv; |
| 689 | while (*v) |
| 690 | v++; |
| 691 | return xzalloc((v - argv + EXTRA_PTRS) * sizeof(v[0])); |
| 692 | } |
| 693 | #endif |
| 694 | |
| 695 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 696 | /* Function prototypes for builtins */ |
| 697 | static int builtin_cd(char **argv); |
Denis Vlasenko | 5bc593c | 2007-11-23 21:20:21 +0000 | [diff] [blame] | 698 | static int builtin_echo(char **argv); |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 699 | static int builtin_eval(char **argv); |
| 700 | static int builtin_exec(char **argv); |
| 701 | static int builtin_exit(char **argv); |
| 702 | static int builtin_export(char **argv); |
| 703 | #if ENABLE_HUSH_JOB |
| 704 | static int builtin_fg_bg(char **argv); |
| 705 | static int builtin_jobs(char **argv); |
| 706 | #endif |
| 707 | #if ENABLE_HUSH_HELP |
| 708 | static int builtin_help(char **argv); |
| 709 | #endif |
| 710 | static int builtin_pwd(char **argv); |
| 711 | static int builtin_read(char **argv); |
| 712 | static int builtin_test(char **argv); |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 713 | static int builtin_true(char **argv); |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 714 | static int builtin_set(char **argv); |
| 715 | static int builtin_shift(char **argv); |
| 716 | static int builtin_source(char **argv); |
| 717 | static int builtin_umask(char **argv); |
| 718 | static int builtin_unset(char **argv); |
| 719 | //static int builtin_not_written(char **argv); |
| 720 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 721 | /* Table of built-in functions. They can be forked or not, depending on |
| 722 | * context: within pipes, they fork. As simple commands, they do not. |
| 723 | * When used in non-forking context, they can change global variables |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 724 | * in the parent shell process. If forked, of course they cannot. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 725 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 726 | * still be set at the end. */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 727 | struct built_in_command { |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 728 | const char *cmd; |
| 729 | int (*function)(char **argv); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 730 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 731 | const char *descr; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 732 | #define BLTIN(cmd, func, help) { cmd, func, help } |
| 733 | #else |
| 734 | #define BLTIN(cmd, func, help) { cmd, func } |
| 735 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 736 | }; |
| 737 | |
Denis Vlasenko | 5bc593c | 2007-11-23 21:20:21 +0000 | [diff] [blame] | 738 | /* For now, echo and test are unconditionally enabled. |
| 739 | * Maybe make it configurable? */ |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 740 | static const struct built_in_command bltins[] = { |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 741 | BLTIN("." , builtin_source, "Run commands in a file"), |
| 742 | BLTIN(":" , builtin_true, "No-op"), |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 743 | BLTIN("[" , builtin_test, "Test condition"), |
| 744 | BLTIN("[[" , builtin_test, "Test condition"), |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 745 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 746 | BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"), |
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("break" , builtin_not_written, "Exit for, while or until loop"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 749 | BLTIN("cd" , builtin_cd, "Change directory"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 750 | // BLTIN("continue", builtin_not_written, "Continue for, while or until loop"), |
Denis Vlasenko | 5bc593c | 2007-11-23 21:20:21 +0000 | [diff] [blame] | 751 | BLTIN("echo" , builtin_echo, "Write strings to stdout"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 752 | BLTIN("eval" , builtin_eval, "Construct and run shell command"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 753 | BLTIN("exec" , builtin_exec, "Execute command, don't return to shell"), |
| 754 | BLTIN("exit" , builtin_exit, "Exit"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 755 | BLTIN("export", builtin_export, "Set environment variable"), |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 756 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 757 | BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 758 | BLTIN("jobs" , builtin_jobs, "List active jobs"), |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 759 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 760 | BLTIN("pwd" , builtin_pwd, "Print current directory"), |
| 761 | BLTIN("read" , builtin_read, "Input environment variable"), |
| 762 | // BLTIN("return", builtin_not_written, "Return from a function"), |
| 763 | BLTIN("set" , builtin_set, "Set/unset shell local variables"), |
| 764 | BLTIN("shift" , builtin_shift, "Shift positional parameters"), |
| 765 | // BLTIN("trap" , builtin_not_written, "Trap signals"), |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 766 | BLTIN("test" , builtin_test, "Test condition"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 767 | // BLTIN("ulimit", builtin_not_written, "Control resource limits"), |
| 768 | BLTIN("umask" , builtin_umask, "Set file creation mask"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 769 | BLTIN("unset" , builtin_unset, "Unset environment variable"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 770 | #if ENABLE_HUSH_HELP |
| 771 | BLTIN("help" , builtin_help, "List shell built-in commands"), |
| 772 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 773 | }; |
| 774 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 775 | |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 776 | /* Signals are grouped, we handle them in batches */ |
| 777 | static void set_misc_sighandler(void (*handler)(int)) |
| 778 | { |
| 779 | bb_signals(0 |
| 780 | + (1 << SIGINT) |
| 781 | + (1 << SIGQUIT) |
| 782 | + (1 << SIGTERM) |
| 783 | , handler); |
| 784 | } |
| 785 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 786 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 787 | |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 788 | static void set_fatal_sighandler(void (*handler)(int)) |
| 789 | { |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 790 | bb_signals(0 |
| 791 | + (1 << SIGILL) |
| 792 | + (1 << SIGTRAP) |
| 793 | + (1 << SIGABRT) |
| 794 | + (1 << SIGFPE) |
| 795 | + (1 << SIGBUS) |
| 796 | + (1 << SIGSEGV) |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 797 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 798 | + (1 << SIGHUP) |
| 799 | + (1 << SIGPIPE) |
| 800 | + (1 << SIGALRM) |
| 801 | , handler); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 802 | } |
| 803 | static void set_jobctrl_sighandler(void (*handler)(int)) |
| 804 | { |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 805 | bb_signals(0 |
| 806 | + (1 << SIGTSTP) |
| 807 | + (1 << SIGTTIN) |
| 808 | + (1 << SIGTTOU) |
| 809 | , handler); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 810 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 811 | /* SIGCHLD is special and handled separately */ |
| 812 | |
| 813 | static void set_every_sighandler(void (*handler)(int)) |
| 814 | { |
| 815 | set_fatal_sighandler(handler); |
| 816 | set_jobctrl_sighandler(handler); |
| 817 | set_misc_sighandler(handler); |
| 818 | signal(SIGCHLD, handler); |
| 819 | } |
| 820 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 821 | static void handler_ctrl_c(int sig UNUSED_PARAM) |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 822 | { |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 823 | debug_printf_jobs("got sig %d\n", sig); |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 824 | // 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] | 825 | siglongjmp(toplevel_jb, 1); |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 828 | static void handler_ctrl_z(int sig UNUSED_PARAM) |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 829 | { |
| 830 | pid_t pid; |
| 831 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 832 | 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] | 833 | pid = fork(); |
Denis Vlasenko | c299032 | 2007-05-16 12:57:12 +0000 | [diff] [blame] | 834 | if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */ |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 835 | return; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 836 | ctrl_z_flag = 1; |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 837 | if (!pid) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 838 | if (ENABLE_HUSH_JOB) |
| 839 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 840 | setpgrp(); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 841 | debug_printf_jobs("set pgrp for child %d ok\n", getpid()); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 842 | set_every_sighandler(SIG_DFL); |
Denis Vlasenko | 18e19f2 | 2007-04-28 16:43:18 +0000 | [diff] [blame] | 843 | raise(SIGTSTP); /* resend TSTP so that child will be stopped */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 844 | debug_printf_jobs("returning in child\n"); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 845 | /* return to nofork, it will eventually exit now, |
| 846 | * not return back to shell */ |
| 847 | return; |
| 848 | } |
| 849 | /* parent */ |
| 850 | /* finish filling up pipe info */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 851 | toplevel_list->pgrp = pid; /* child is in its own pgrp */ |
| 852 | toplevel_list->progs[0].pid = pid; |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 853 | /* parent needs to longjmp out of running nofork. |
| 854 | * we will "return" exitcode 0, with child put in background */ |
| 855 | // 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] | 856 | debug_printf_jobs("siglongjmp in parent\n"); |
| 857 | siglongjmp(toplevel_jb, 1); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 860 | /* Restores tty foreground process group, and exits. |
| 861 | * May be called as signal handler for fatal signal |
| 862 | * (will faithfully resend signal to itself, producing correct exit state) |
| 863 | * or called directly with -EXITCODE. |
| 864 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 865 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 866 | static void sigexit(int sig) |
| 867 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 868 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 869 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 870 | |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 871 | if (interactive_fd) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 872 | tcsetpgrp(interactive_fd, saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 873 | |
| 874 | /* Not a signal, just exit */ |
| 875 | if (sig <= 0) |
| 876 | _exit(- sig); |
| 877 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 878 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | /* Restores tty foreground process group, and exits. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 882 | static void hush_exit(int exitcode) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 883 | static void hush_exit(int exitcode) |
| 884 | { |
| 885 | fflush(NULL); /* flush all streams */ |
| 886 | sigexit(- (exitcode & 0xff)); |
| 887 | } |
| 888 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 889 | #else /* !JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 890 | |
| 891 | #define set_fatal_sighandler(handler) ((void)0) |
| 892 | #define set_jobctrl_sighandler(handler) ((void)0) |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 893 | #define hush_exit(e) exit(e) |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 894 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 895 | #endif /* JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 896 | |
| 897 | |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 898 | static const char *set_cwd(void) |
| 899 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 900 | if (cwd == bb_msg_unknown) |
Denis Vlasenko | 7cced6e | 2007-04-12 17:08:53 +0000 | [diff] [blame] | 901 | cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */ |
Denis Vlasenko | 6ca0444 | 2007-02-11 16:19:28 +0000 | [diff] [blame] | 902 | cwd = xrealloc_getcwd_or_warn((char *)cwd); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 903 | if (!cwd) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 904 | cwd = bb_msg_unknown; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 905 | return cwd; |
| 906 | } |
| 907 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 908 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 909 | /* |
| 910 | * o_string support |
| 911 | */ |
| 912 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 913 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 914 | static void o_reset(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 915 | { |
| 916 | o->length = 0; |
| 917 | o->nonnull = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 918 | if (o->data) |
| 919 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 922 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 923 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 924 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 925 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 928 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 929 | { |
| 930 | if (o->length + len > o->maxlen) { |
| 931 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 932 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 933 | } |
| 934 | } |
| 935 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 936 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 937 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 938 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 939 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 940 | o->data[o->length] = ch; |
| 941 | o->length++; |
| 942 | o->data[o->length] = '\0'; |
| 943 | } |
| 944 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 945 | static void o_addstr(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 946 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 947 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 948 | memcpy(&o->data[o->length], str, len); |
| 949 | o->length += len; |
| 950 | o->data[o->length] = '\0'; |
| 951 | } |
| 952 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 953 | static void o_addstr_duplicate_backslash(o_string *o, const char *str, int len) |
| 954 | { |
| 955 | while (len) { |
| 956 | o_addchr(o, *str); |
| 957 | if (*str++ == '\\' |
| 958 | && (*str != '*' && *str != '?' && *str != '[') |
| 959 | ) { |
| 960 | o_addchr(o, '\\'); |
| 961 | } |
| 962 | len--; |
| 963 | } |
| 964 | } |
| 965 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 966 | /* My analysis of quoting semantics tells me that state information |
| 967 | * is associated with a destination, not a source. |
| 968 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 969 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 970 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 971 | int sz = 1; |
| 972 | if (strchr("*?[\\", ch)) { |
| 973 | sz++; |
| 974 | o->data[o->length] = '\\'; |
| 975 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 976 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 977 | o_grow_by(o, sz); |
| 978 | o->data[o->length] = ch; |
| 979 | o->length++; |
| 980 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 983 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 984 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 985 | int sz = 1; |
| 986 | if (o->o_quote && strchr("*?[\\", ch)) { |
| 987 | sz++; |
| 988 | o->data[o->length] = '\\'; |
| 989 | o->length++; |
| 990 | } |
| 991 | o_grow_by(o, sz); |
| 992 | o->data[o->length] = ch; |
| 993 | o->length++; |
| 994 | o->data[o->length] = '\0'; |
| 995 | } |
| 996 | |
| 997 | static void o_addQstr(o_string *o, const char *str, int len) |
| 998 | { |
| 999 | if (!o->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1000 | o_addstr(o, str, len); |
| 1001 | return; |
| 1002 | } |
| 1003 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1004 | char ch; |
| 1005 | int sz; |
| 1006 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 1007 | if (ordinary_cnt > len) /* paranoia */ |
| 1008 | ordinary_cnt = len; |
| 1009 | o_addstr(o, str, ordinary_cnt); |
| 1010 | if (ordinary_cnt == len) |
| 1011 | return; |
| 1012 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1013 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1014 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1015 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1016 | sz = 1; |
| 1017 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 1018 | sz++; |
| 1019 | o->data[o->length] = '\\'; |
| 1020 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1021 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1022 | o_grow_by(o, sz); |
| 1023 | o->data[o->length] = ch; |
| 1024 | o->length++; |
| 1025 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1029 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1030 | * 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] | 1031 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1032 | * list[i] contains an INDEX (int!) into this string data. |
| 1033 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1034 | * but list[i]'s need not be modified. |
| 1035 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1036 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1037 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1038 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1039 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1040 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1041 | { |
| 1042 | char **list = (char**)o->data; |
| 1043 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1044 | int i = 0; |
| 1045 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1046 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1047 | while (i < n) { |
| 1048 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1049 | o->data + (int)list[i] + string_start, |
| 1050 | o->data + (int)list[i] + string_start); |
| 1051 | i++; |
| 1052 | } |
| 1053 | if (n) { |
| 1054 | const char *p = o->data + (int)list[n - 1] + string_start; |
| 1055 | fprintf(stderr, " total_sz:%d\n", (p + strlen(p) + 1) - o->data); |
| 1056 | } |
| 1057 | } |
| 1058 | #else |
| 1059 | #define debug_print_list(prefix, o, n) ((void)0) |
| 1060 | #endif |
| 1061 | |
| 1062 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1063 | * in list[n] so that it points past last stored byte so far. |
| 1064 | * It returns n+1. */ |
| 1065 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1066 | { |
| 1067 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1068 | int string_start; |
| 1069 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1070 | |
| 1071 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1072 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1073 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1074 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1075 | 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] | 1076 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1077 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1078 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1079 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1080 | memmove(list + n + 0x10, list + n, string_len); |
| 1081 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1082 | } else |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1083 | 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] | 1084 | } else { |
| 1085 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1086 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1087 | string_len = o->length - string_start; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1088 | 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] | 1089 | o->has_empty_slot = 0; |
| 1090 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1091 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1092 | return n + 1; |
| 1093 | } |
| 1094 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1095 | /* "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] | 1096 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1097 | { |
| 1098 | char **list = (char**)o->data; |
| 1099 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1100 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1101 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1104 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1105 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1106 | static int o_glob(o_string *o, int n) |
| 1107 | { |
| 1108 | glob_t globdata; |
| 1109 | int gr; |
| 1110 | char *pattern; |
| 1111 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1112 | 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] | 1113 | if (!o->data) |
| 1114 | return o_save_ptr_helper(o, n); |
| 1115 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1116 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1117 | if (!glob_needed(pattern)) { |
| 1118 | literal: |
| 1119 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1120 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1121 | return o_save_ptr_helper(o, n); |
| 1122 | } |
| 1123 | |
| 1124 | memset(&globdata, 0, sizeof(globdata)); |
| 1125 | gr = glob(pattern, 0, NULL, &globdata); |
| 1126 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 1127 | if (gr == GLOB_NOSPACE) |
| 1128 | bb_error_msg_and_die("out of memory during glob"); |
| 1129 | if (gr == GLOB_NOMATCH) { |
| 1130 | globfree(&globdata); |
| 1131 | goto literal; |
| 1132 | } |
| 1133 | if (gr != 0) { /* GLOB_ABORTED ? */ |
| 1134 | //TODO: testcase for bad glob pattern behavior |
| 1135 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 1136 | } |
| 1137 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 1138 | char **argv = globdata.gl_pathv; |
| 1139 | o->length = pattern - o->data; /* "forget" pattern */ |
| 1140 | while (1) { |
| 1141 | o_addstr(o, *argv, strlen(*argv) + 1); |
| 1142 | n = o_save_ptr_helper(o, n); |
| 1143 | argv++; |
| 1144 | if (!*argv) |
| 1145 | break; |
| 1146 | } |
| 1147 | } |
| 1148 | globfree(&globdata); |
| 1149 | if (DEBUG_GLOB) |
| 1150 | debug_print_list("o_glob returning", o, n); |
| 1151 | return n; |
| 1152 | } |
| 1153 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1154 | /* If o->o_glob == 1, glob the string so far remembered. |
| 1155 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1156 | static int o_save_ptr(o_string *o, int n) |
| 1157 | { |
| 1158 | if (o->o_glob) |
| 1159 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 1160 | return o_save_ptr_helper(o, n); |
| 1161 | } |
| 1162 | |
| 1163 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1164 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1165 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1166 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1167 | int string_start; |
| 1168 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1169 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 1170 | if (DEBUG_EXPAND) |
| 1171 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1172 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1173 | list = (char**)o->data; |
| 1174 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1175 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1176 | while (n) { |
| 1177 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1178 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1179 | } |
| 1180 | return list; |
| 1181 | } |
| 1182 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1183 | |
| 1184 | /* |
| 1185 | * in_str support |
| 1186 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1187 | static int static_get(struct in_str *i) |
| 1188 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1189 | int ch = *i->p++; |
| 1190 | if (ch == '\0') return EOF; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1191 | return ch; |
| 1192 | } |
| 1193 | |
| 1194 | static int static_peek(struct in_str *i) |
| 1195 | { |
| 1196 | return *i->p; |
| 1197 | } |
| 1198 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1199 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1200 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1201 | #if ENABLE_FEATURE_EDITING |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 1202 | static void cmdedit_set_initial_prompt(void) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1203 | { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1204 | #if !ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1205 | PS1 = NULL; |
| 1206 | #else |
| 1207 | PS1 = getenv("PS1"); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1208 | if (PS1 == NULL) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1209 | PS1 = "\\w \\$ "; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1210 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1211 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1212 | #endif /* EDITING */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1213 | |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1214 | static const char* setup_prompt_string(int promptmode) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1215 | { |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1216 | const char *prompt_str; |
| 1217 | debug_printf("setup_prompt_string %d ", promptmode); |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1218 | #if !ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1219 | /* Set up the prompt */ |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1220 | if (promptmode == 0) { /* PS1 */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1221 | free((char*)PS1); |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1222 | PS1 = xasprintf("%s %c ", cwd, (geteuid() != 0) ? '$' : '#'); |
| 1223 | prompt_str = PS1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1224 | } else { |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1225 | prompt_str = PS2; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1226 | } |
| 1227 | #else |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1228 | prompt_str = (promptmode == 0) ? PS1 : PS2; |
Eric Andersen | af44a0e | 2001-04-27 07:26:12 +0000 | [diff] [blame] | 1229 | #endif |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1230 | debug_printf("result '%s'\n", prompt_str); |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1231 | return prompt_str; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1234 | static void get_user_input(struct in_str *i) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1235 | { |
Denis Vlasenko | 0937be5 | 2007-04-28 16:47:08 +0000 | [diff] [blame] | 1236 | int r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1237 | const char *prompt_str; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1238 | |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1239 | prompt_str = setup_prompt_string(i->promptmode); |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1240 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1241 | /* Enable command line editing only while a command line |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 1242 | * is actually being read */ |
| 1243 | do { |
| 1244 | r = read_line_input(prompt_str, user_input_buf, BUFSIZ-1, line_input_state); |
| 1245 | } while (r == 0); /* repeat if Ctrl-C */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1246 | i->eof_flag = (r < 0); |
| 1247 | if (i->eof_flag) { /* EOF/error detected */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1248 | user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1249 | user_input_buf[1] = '\0'; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1250 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1251 | #else |
| 1252 | fputs(prompt_str, stdout); |
| 1253 | fflush(stdout); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1254 | user_input_buf[0] = r = fgetc(i->file); |
| 1255 | /*user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1256 | i->eof_flag = (r == EOF); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1257 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1258 | i->p = user_input_buf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1259 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1260 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1261 | #endif /* INTERACTIVE */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1262 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1263 | /* This is the magic location that prints prompts |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1264 | * and gets data back from the user */ |
| 1265 | static int file_get(struct in_str *i) |
| 1266 | { |
| 1267 | int ch; |
| 1268 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1269 | /* If there is data waiting, eat it up */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1270 | if (i->p && *i->p) { |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1271 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1272 | take_cached: |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1273 | #endif |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1274 | ch = *i->p++; |
| 1275 | if (i->eof_flag && !*i->p) |
| 1276 | ch = EOF; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1277 | } else { |
| 1278 | /* need to double check i->file because we might be doing something |
| 1279 | * more complicated by now, like sourcing or substituting. */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1280 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1281 | if (interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1282 | do { |
| 1283 | get_user_input(i); |
| 1284 | } while (!*i->p); /* need non-empty line */ |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1285 | i->promptmode = 1; /* PS2 */ |
| 1286 | i->promptme = 0; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1287 | goto take_cached; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1288 | } |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1289 | #endif |
| 1290 | ch = fgetc(i->file); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1291 | } |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1292 | debug_printf("file_get: got a '%c' %d\n", ch, ch); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1293 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1294 | if (ch == '\n') |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1295 | i->promptme = 1; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1296 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1297 | return ch; |
| 1298 | } |
| 1299 | |
| 1300 | /* All the callers guarantee this routine will never be |
| 1301 | * used right after a newline, so prompting is not needed. |
| 1302 | */ |
| 1303 | static int file_peek(struct in_str *i) |
| 1304 | { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 1305 | int ch; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1306 | if (i->p && *i->p) { |
| 1307 | if (i->eof_flag && !i->p[1]) |
| 1308 | return EOF; |
| 1309 | return *i->p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1310 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 1311 | ch = fgetc(i->file); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1312 | i->eof_flag = (ch == EOF); |
| 1313 | i->peek_buf[0] = ch; |
| 1314 | i->peek_buf[1] = '\0'; |
| 1315 | i->p = i->peek_buf; |
| 1316 | 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] | 1317 | return ch; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1321 | { |
| 1322 | i->peek = file_peek; |
| 1323 | i->get = file_get; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1324 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1325 | i->promptme = 1; |
| 1326 | i->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1327 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1328 | i->file = f; |
| 1329 | i->p = NULL; |
| 1330 | } |
| 1331 | |
| 1332 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1333 | { |
| 1334 | i->peek = static_peek; |
| 1335 | i->get = static_get; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1336 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1337 | i->promptme = 1; |
| 1338 | i->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1339 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1340 | i->p = s; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1341 | i->eof_flag = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1344 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1345 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 1346 | * and stderr if they are redirected. */ |
| 1347 | static int setup_redirects(struct child_prog *prog, int squirrel[]) |
| 1348 | { |
| 1349 | int openfd, mode; |
| 1350 | struct redir_struct *redir; |
| 1351 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1352 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1353 | if (redir->dup == -1 && redir->rd_filename == NULL) { |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 1354 | /* something went wrong in the parse. Pretend it didn't happen */ |
| 1355 | continue; |
| 1356 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1357 | if (redir->dup == -1) { |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1358 | char *p; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1359 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 1360 | //TODO: check redir for names like '\\' |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1361 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1362 | openfd = open_or_warn(p, mode); |
| 1363 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1364 | if (openfd < 0) { |
| 1365 | /* this could get lost if stderr has been redirected, but |
| 1366 | bash and ash both lose it as well (though zsh doesn't!) */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1367 | return 1; |
| 1368 | } |
| 1369 | } else { |
| 1370 | openfd = redir->dup; |
| 1371 | } |
| 1372 | |
| 1373 | if (openfd != redir->fd) { |
| 1374 | if (squirrel && redir->fd < 3) { |
| 1375 | squirrel[redir->fd] = dup(redir->fd); |
| 1376 | } |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1377 | if (openfd == -3) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1378 | //close(openfd); // close(-3) ??! |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1379 | } else { |
| 1380 | dup2(openfd, redir->fd); |
Matt Kraai | c616e53 | 2001-06-05 16:50:08 +0000 | [diff] [blame] | 1381 | if (redir->dup == -1) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1382 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1383 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
| 1389 | static void restore_redirects(int squirrel[]) |
| 1390 | { |
| 1391 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1392 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1393 | fd = squirrel[i]; |
| 1394 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 1395 | /* We simply die on error */ |
| 1396 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1401 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1402 | /* Called after [v]fork() in run_pipe(), or from builtin_exec(). |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1403 | * Never returns. |
| 1404 | * XXX no exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1405 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1406 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1407 | static void pseudo_exec_argv(char **ptrs2free, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1408 | { |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1409 | int i, rcode; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 1410 | char *p; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 1411 | const struct built_in_command *x; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1412 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1413 | for (i = 0; is_assignment(argv[i]); i++) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1414 | debug_printf_exec("pid %d environment modification: %s\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1415 | getpid(), argv[i]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1416 | p = expand_string_to_string(argv[i]); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1417 | #if !BB_MMU |
| 1418 | *ptrs2free++ = p; |
| 1419 | #endif |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1420 | putenv(p); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1421 | } |
| 1422 | argv += i; |
| 1423 | /* If a variable is assigned in a forest, and nobody listens, |
| 1424 | * was it ever really set? |
| 1425 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1426 | if (!argv[0]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1427 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1428 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1429 | argv = expand_strvec_to_strvec(argv); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1430 | #if !BB_MMU |
| 1431 | *ptrs2free++ = (char*) argv; |
| 1432 | #endif |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 1433 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1434 | /* |
| 1435 | * Check if the command matches any of the builtins. |
| 1436 | * Depending on context, this might be redundant. But it's |
| 1437 | * easier to waste a few CPU cycles than it is to figure out |
| 1438 | * if this is one of those cases. |
| 1439 | */ |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 1440 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1441 | if (strcmp(argv[0], x->cmd) == 0) { |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 1442 | debug_printf_exec("running builtin '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1443 | rcode = x->function(argv); |
| 1444 | fflush(stdout); |
| 1445 | _exit(rcode); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1446 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1447 | } |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1448 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1449 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 1450 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1451 | if (strchr(argv[0], '/') == NULL) { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 1452 | int a = find_applet_by_name(argv[0]); |
| 1453 | if (a >= 0) { |
| 1454 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1455 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 1456 | // is it ok that run_applet_no_and_exit() does exit(), not _exit()? |
| 1457 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1458 | } |
| 1459 | /* re-exec ourselves with the new arguments */ |
| 1460 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | bdbbb7e | 2007-06-08 15:02:55 +0000 | [diff] [blame] | 1461 | execvp(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1462 | /* If they called chroot or otherwise made the binary no longer |
| 1463 | * executable, fall through */ |
| 1464 | } |
| 1465 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 1466 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1467 | |
| 1468 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1469 | execvp(argv[0], argv); |
| 1470 | bb_perror_msg("cannot exec '%s'", argv[0]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 1471 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1474 | /* Called after [v]fork() in run_pipe() |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1475 | */ |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1476 | static void pseudo_exec(char **ptrs2free, struct child_prog *child) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1477 | { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1478 | if (child->argv) |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1479 | pseudo_exec_argv(ptrs2free, child->argv); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1480 | |
| 1481 | if (child->group) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1482 | #if !BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 1483 | bb_error_msg_and_die("nested lists are not supported on NOMMU"); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1484 | #else |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 1485 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1486 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 1487 | rcode = run_list(child->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 1488 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1489 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1490 | _exit(rcode); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1491 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1492 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1493 | |
| 1494 | /* Can happen. See what bash does with ">foo" by itself. */ |
| 1495 | debug_printf("trying to pseudo_exec null command\n"); |
| 1496 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1499 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1500 | static const char *get_cmdtext(struct pipe *pi) |
| 1501 | { |
| 1502 | char **argv; |
| 1503 | char *p; |
| 1504 | int len; |
| 1505 | |
| 1506 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 1507 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1508 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1509 | if (pi->cmdtext) |
| 1510 | return pi->cmdtext; |
| 1511 | argv = pi->progs[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 1512 | if (!argv || !argv[0]) { |
| 1513 | pi->cmdtext = xzalloc(1); |
| 1514 | return pi->cmdtext; |
| 1515 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1516 | |
| 1517 | len = 0; |
| 1518 | do len += strlen(*argv) + 1; while (*++argv); |
| 1519 | pi->cmdtext = p = xmalloc(len); |
| 1520 | argv = pi->progs[0].argv; |
| 1521 | do { |
| 1522 | len = strlen(*argv); |
| 1523 | memcpy(p, *argv, len); |
| 1524 | p += len; |
| 1525 | *p++ = ' '; |
| 1526 | } while (*++argv); |
| 1527 | p[-1] = '\0'; |
| 1528 | return pi->cmdtext; |
| 1529 | } |
| 1530 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1531 | static void insert_bg_job(struct pipe *pi) |
| 1532 | { |
| 1533 | struct pipe *thejob; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1534 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1535 | |
| 1536 | /* Linear search for the ID of the job to use */ |
| 1537 | pi->jobid = 1; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1538 | for (thejob = job_list; thejob; thejob = thejob->next) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1539 | if (thejob->jobid >= pi->jobid) |
| 1540 | pi->jobid = thejob->jobid + 1; |
| 1541 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1542 | /* Add thejob to the list of running jobs */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1543 | if (!job_list) { |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1544 | thejob = job_list = xmalloc(sizeof(*thejob)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1545 | } else { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1546 | for (thejob = job_list; thejob->next; thejob = thejob->next) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1547 | continue; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1548 | thejob->next = xmalloc(sizeof(*thejob)); |
| 1549 | thejob = thejob->next; |
| 1550 | } |
| 1551 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1552 | /* Physically copy the struct job */ |
Eric Andersen | 0fcd447 | 2001-05-02 20:12:03 +0000 | [diff] [blame] | 1553 | memcpy(thejob, pi, sizeof(struct pipe)); |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1554 | thejob->progs = xzalloc(sizeof(pi->progs[0]) * pi->num_progs); |
| 1555 | /* We cannot copy entire pi->progs[] vector! Double free()s will happen */ |
| 1556 | for (i = 0; i < pi->num_progs; i++) { |
| 1557 | // TODO: do we really need to have so many fields which are just dead weight |
| 1558 | // at execution stage? |
| 1559 | thejob->progs[i].pid = pi->progs[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1560 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1561 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1562 | thejob->next = NULL; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1563 | thejob->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1564 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1565 | /* We don't wait for background thejobs to return -- append it |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1566 | to the list of backgrounded thejobs and leave it alone */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1567 | printf("[%d] %d %s\n", thejob->jobid, thejob->progs[0].pid, thejob->cmdtext); |
Eric Andersen | 1a6d39b | 2001-05-08 05:11:54 +0000 | [diff] [blame] | 1568 | last_bg_pid = thejob->progs[0].pid; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1569 | last_jobid = thejob->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1572 | static void remove_bg_job(struct pipe *pi) |
| 1573 | { |
| 1574 | struct pipe *prev_pipe; |
| 1575 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1576 | if (pi == job_list) { |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1577 | job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1578 | } else { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1579 | prev_pipe = job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1580 | while (prev_pipe->next != pi) |
| 1581 | prev_pipe = prev_pipe->next; |
| 1582 | prev_pipe->next = pi->next; |
| 1583 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 1584 | if (job_list) |
| 1585 | last_jobid = job_list->jobid; |
| 1586 | else |
| 1587 | last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1588 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 1589 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1590 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1591 | static void delete_finished_bg_job(struct pipe *pi) |
| 1592 | { |
| 1593 | remove_bg_job(pi); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1594 | pi->stopped_progs = 0; |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 1595 | free_pipe(pi, 0); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1596 | free(pi); |
| 1597 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1598 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1599 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1600 | /* Check to see if any processes have exited -- if they |
| 1601 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1602 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1603 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1604 | int attributes; |
| 1605 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1606 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1607 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1608 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1609 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1610 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1611 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1612 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1613 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1614 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1615 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1616 | /* Do we do this right? |
| 1617 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1618 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1619 | * [3]+ Stopped sleep 20 | false |
| 1620 | * bash-3.00# echo $? |
| 1621 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 1622 | */ |
| 1623 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1624 | //FIXME: non-interactive bash does not continue even if all processes in fg pipe |
| 1625 | //are stopped. Testcase: "cat | cat" in a script (not on command line) |
| 1626 | // + killall -STOP cat |
| 1627 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1628 | wait_more: |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 1629 | // TODO: safe_waitpid? |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1630 | while ((childpid = waitpid(-1, &status, attributes)) > 0) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1631 | int i; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1632 | const int dead = WIFEXITED(status) || WIFSIGNALED(status); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1633 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1634 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1635 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1636 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 1637 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1638 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1639 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 1640 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1641 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1642 | childpid, WEXITSTATUS(status)); |
| 1643 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1644 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1645 | if (fg_pipe) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1646 | for (i = 0; i < fg_pipe->num_progs; i++) { |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1647 | debug_printf_jobs("check pid %d\n", fg_pipe->progs[i].pid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1648 | if (fg_pipe->progs[i].pid != childpid) |
| 1649 | continue; |
| 1650 | /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */ |
| 1651 | if (dead) { |
| 1652 | fg_pipe->progs[i].pid = 0; |
| 1653 | fg_pipe->alive_progs--; |
| 1654 | if (i == fg_pipe->num_progs - 1) { |
| 1655 | /* last process gives overall exitstatus */ |
| 1656 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 1657 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1658 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1659 | } else { |
| 1660 | fg_pipe->progs[i].is_stopped = 1; |
| 1661 | fg_pipe->stopped_progs++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1662 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1663 | debug_printf_jobs("fg_pipe: alive_progs %d stopped_progs %d\n", |
| 1664 | fg_pipe->alive_progs, fg_pipe->stopped_progs); |
| 1665 | if (fg_pipe->alive_progs - fg_pipe->stopped_progs <= 0) { |
| 1666 | /* All processes in fg pipe have exited/stopped */ |
| 1667 | #if ENABLE_HUSH_JOB |
| 1668 | if (fg_pipe->alive_progs) |
| 1669 | insert_bg_job(fg_pipe); |
| 1670 | #endif |
| 1671 | return rcode; |
| 1672 | } |
| 1673 | /* There are still running processes in the fg pipe */ |
| 1674 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1675 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1676 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1679 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1680 | /* We asked to wait for bg or orphaned children */ |
| 1681 | /* No need to remember exitcode in this case */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1682 | for (pi = job_list; pi; pi = pi->next) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1683 | for (i = 0; i < pi->num_progs; i++) { |
| 1684 | if (pi->progs[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 1685 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1686 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1687 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 1688 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 1689 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1690 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 1691 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 1692 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1693 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1694 | /* child exited */ |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1695 | pi->progs[i].pid = 0; |
| 1696 | pi->alive_progs--; |
| 1697 | if (!pi->alive_progs) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1698 | printf(JOB_STATUS_FORMAT, pi->jobid, |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1699 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1700 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1701 | } |
| 1702 | } else { |
| 1703 | /* child stopped */ |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1704 | pi->progs[i].is_stopped = 1; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1705 | pi->stopped_progs++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1706 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1707 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1708 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1709 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1710 | /* wait found no children or failed */ |
| 1711 | |
| 1712 | if (childpid && errno != ECHILD) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1713 | bb_perror_msg("waitpid"); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1714 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1717 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1718 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 1719 | { |
| 1720 | pid_t p; |
| 1721 | int rcode = checkjobs(fg_pipe); |
| 1722 | /* Job finished, move the shell to the foreground */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1723 | p = getpgid(0); /* pgid of our process */ |
| 1724 | debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1725 | tcsetpgrp(interactive_fd, p); |
| 1726 | // if (tcsetpgrp(interactive_fd, p) && errno != ENOTTY) |
| 1727 | // bb_perror_msg("tcsetpgrp-4a"); |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1728 | return rcode; |
| 1729 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1730 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1731 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1732 | /* run_pipe() starts all the jobs, but doesn't wait for anything |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1733 | * to finish. See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1734 | * |
| 1735 | * return code is normally -1, when the caller has to wait for children |
| 1736 | * to finish to determine the exit status of the pipe. If the pipe |
| 1737 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1738 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1739 | * return value. |
| 1740 | * |
| 1741 | * The input of the pipe is always stdin, the output is always |
| 1742 | * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus, |
| 1743 | * because it tries to avoid running the command substitution in |
| 1744 | * subshell, when that is in fact necessary. The subshell process |
| 1745 | * now has its stdout directed to the input of the appropriate pipe, |
| 1746 | * so this routine is noticeably simpler. |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1747 | * |
| 1748 | * Returns -1 only if started some children. IOW: we have to |
| 1749 | * mask out retvals of builtins etc with 0xff! |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1750 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1751 | static int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1752 | { |
| 1753 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1754 | int nextin; |
| 1755 | int pipefds[2]; /* pipefds[0] is for reading */ |
Rob Landley | 53702e5 | 2006-07-19 21:43:53 +0000 | [diff] [blame] | 1756 | struct child_prog *child; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 1757 | const struct built_in_command *x; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 1758 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1759 | /* it is not always needed, but we aim to smaller code */ |
| 1760 | int squirrel[] = { -1, -1, -1 }; |
| 1761 | int rcode; |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1762 | const int single_fg = (pi->num_progs == 1 && pi->followup != PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1763 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1764 | debug_printf_exec("run_pipe start: single_fg=%d\n", single_fg); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 1765 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1766 | #if ENABLE_HUSH_JOB |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 1767 | pi->pgrp = -1; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1768 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1769 | pi->alive_progs = 1; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1770 | pi->stopped_progs = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1771 | |
| 1772 | /* Check if this is a simple builtin (not part of a pipe). |
| 1773 | * Builtins within pipes have to fork anyway, and are handled in |
| 1774 | * pseudo_exec. "echo foo | read bar" doesn't work on bash, either. |
| 1775 | */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1776 | child = &(pi->progs[0]); |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1777 | if (single_fg && child->group && child->subshell == 0) { |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 1778 | debug_printf("non-subshell grouping\n"); |
| 1779 | setup_redirects(child, squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1780 | debug_printf_exec(": run_list\n"); |
| 1781 | rcode = run_list(child->group) & 0xff; |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 1782 | restore_redirects(squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1783 | debug_printf_exec("run_pipe return %d\n", rcode); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 1784 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1785 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1788 | if (single_fg && child->argv != NULL) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1789 | char **argv_expanded; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1790 | char **argv = child->argv; |
| 1791 | |
| 1792 | for (i = 0; is_assignment(argv[i]); i++) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1793 | continue; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1794 | if (i != 0 && argv[i] == NULL) { |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1795 | /* assignments, but no command: set the local environment */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1796 | for (i = 0; argv[i] != NULL; i++) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 1797 | debug_printf("local environment set: %s\n", argv[i]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1798 | p = expand_string_to_string(argv[i]); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 1799 | set_local_var(p, 0); |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1800 | } |
| 1801 | return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */ |
| 1802 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1803 | for (i = 0; is_assignment(argv[i]); i++) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1804 | p = expand_string_to_string(argv[i]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1805 | putenv(p); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1806 | //FIXME: do we leak p?! |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 1807 | } |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 1808 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1809 | if (strcmp(argv[i], x->cmd) == 0) { |
| 1810 | if (x->function == builtin_exec && argv[i+1] == NULL) { |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1811 | debug_printf("magic exec\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1812 | setup_redirects(child, NULL); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1813 | return EXIT_SUCCESS; |
| 1814 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1815 | debug_printf("builtin inline %s\n", argv[0]); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1816 | /* XXX setup_redirects acts on file descriptors, not FILEs. |
| 1817 | * This is perfect for work that comes after exec(). |
| 1818 | * Is it really safe for inline use? Experimentally, |
| 1819 | * things seem to work with glibc. */ |
| 1820 | setup_redirects(child, squirrel); |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1821 | debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv[i+1]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1822 | argv_expanded = expand_strvec_to_strvec(argv + i); |
| 1823 | rcode = x->function(argv_expanded) & 0xff; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1824 | free(argv_expanded); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1825 | restore_redirects(squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1826 | debug_printf_exec("run_pipe return %d\n", rcode); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 1827 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1828 | return rcode; |
| 1829 | } |
| 1830 | } |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1831 | #if ENABLE_FEATURE_SH_STANDALONE |
| 1832 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 1833 | int a = find_applet_by_name(argv[i]); |
| 1834 | if (a >= 0 && APPLET_IS_NOFORK(a)) { |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1835 | setup_redirects(child, squirrel); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1836 | save_nofork_data(&nofork_save); |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 1837 | argv_expanded = argv + i; |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1838 | argv_expanded = expand_strvec_to_strvec(argv + i); |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 1839 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]); |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 1840 | rcode = run_nofork_applet_prime(&nofork_save, a, argv_expanded); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1841 | free(argv_expanded); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1842 | restore_redirects(squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1843 | debug_printf_exec("run_pipe return %d\n", rcode); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 1844 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1845 | return rcode; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1846 | } |
| 1847 | } |
| 1848 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1851 | /* Disable job control signals for shell (parent) and |
| 1852 | * for initial child code after fork */ |
| 1853 | set_jobctrl_sighandler(SIG_IGN); |
| 1854 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1855 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1856 | pi->alive_progs = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1857 | nextin = 0; |
| 1858 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1859 | for (i = 0; i < pi->num_progs; i++) { |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1860 | #if !BB_MMU |
| 1861 | char **ptrs2free = NULL; |
| 1862 | #endif |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1863 | child = &(pi->progs[i]); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1864 | if (child->argv) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1865 | 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] | 1866 | #if !BB_MMU |
| 1867 | ptrs2free = alloc_ptrs(child->argv); |
| 1868 | #endif |
| 1869 | } else |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1870 | debug_printf_exec(": pipe member with no argv\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1871 | |
| 1872 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1873 | pipefds[0] = 0; |
| 1874 | pipefds[1] = 1; |
| 1875 | if ((i + 1) < pi->num_progs) |
| 1876 | xpipe(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1877 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1878 | child->pid = BB_MMU ? fork() : vfork(); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1879 | if (!child->pid) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 1880 | if (ENABLE_HUSH_JOB) |
| 1881 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1882 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1883 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1884 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1885 | if (run_list_level == 1 && interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1886 | pid_t pgrp; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1887 | /* Don't do pgrp restore anymore on fatal signals */ |
| 1888 | set_fatal_sighandler(SIG_DFL); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1889 | pgrp = pi->pgrp; |
| 1890 | if (pgrp < 0) /* true for 1st process only */ |
| 1891 | pgrp = getpid(); |
| 1892 | if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1893 | /* We do it in *every* child, not just first, |
| 1894 | * to avoid races */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1895 | tcsetpgrp(interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1896 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1897 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1898 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1899 | xmove_fd(nextin, 0); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1900 | xmove_fd(pipefds[1], 1); /* write end */ |
| 1901 | if (pipefds[0] > 1) |
| 1902 | close(pipefds[0]); /* read end */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1903 | /* Like bash, explicit redirects override pipes, |
| 1904 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1905 | setup_redirects(child, NULL); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1906 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1907 | /* Restore default handlers just prior to exec */ |
| 1908 | set_jobctrl_sighandler(SIG_DFL); |
| 1909 | set_misc_sighandler(SIG_DFL); |
| 1910 | signal(SIGCHLD, SIG_DFL); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1911 | pseudo_exec(ptrs2free, child); /* does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1912 | } |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1913 | #if !BB_MMU |
| 1914 | free_strings(ptrs2free); |
| 1915 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1916 | if (child->pid < 0) { /* [v]fork failed */ |
| 1917 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 1918 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1919 | } else { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1920 | pi->alive_progs++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1921 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1922 | /* Second and next children need to know pid of first one */ |
| 1923 | if (pi->pgrp < 0) |
| 1924 | pi->pgrp = child->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1925 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1926 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1927 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1928 | if (i) |
| 1929 | close(nextin); |
| 1930 | if ((i + 1) < pi->num_progs) |
| 1931 | close(pipefds[1]); /* write end */ |
| 1932 | /* Pass read (output) pipe end to next iteration */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1933 | nextin = pipefds[0]; |
| 1934 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1935 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1936 | if (!pi->alive_progs) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1937 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 1938 | return 1; |
| 1939 | } |
| 1940 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1941 | 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] | 1942 | return -1; |
| 1943 | } |
| 1944 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 1945 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1946 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 1947 | { |
| 1948 | static const char *PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1949 | [PIPE_SEQ] = "SEQ", |
| 1950 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1951 | [PIPE_OR ] = "OR" , |
| 1952 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1953 | }; |
| 1954 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1955 | [RES_NONE ] = "NONE" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 1956 | #if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1957 | [RES_IF ] = "IF" , |
| 1958 | [RES_THEN ] = "THEN" , |
| 1959 | [RES_ELIF ] = "ELIF" , |
| 1960 | [RES_ELSE ] = "ELSE" , |
| 1961 | [RES_FI ] = "FI" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 1962 | #endif |
| 1963 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1964 | [RES_FOR ] = "FOR" , |
| 1965 | [RES_WHILE] = "WHILE", |
| 1966 | [RES_UNTIL] = "UNTIL", |
| 1967 | [RES_DO ] = "DO" , |
| 1968 | [RES_DONE ] = "DONE" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1969 | [RES_IN ] = "IN" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 1970 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 1971 | #if ENABLE_HUSH_CASE |
| 1972 | [RES_CASE ] = "CASE" , |
| 1973 | [RES_MATCH] = "MATCH", |
| 1974 | [RES_CASEI] = "CASEI", |
| 1975 | [RES_ESAC ] = "ESAC" , |
| 1976 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 1977 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1978 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1979 | }; |
| 1980 | |
| 1981 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 1982 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1983 | pin = 0; |
| 1984 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 1985 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 1986 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1987 | prn = 0; |
| 1988 | while (prn < pi->num_progs) { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 1989 | struct child_prog *child = &pi->progs[prn]; |
| 1990 | char **argv = child->argv; |
| 1991 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1992 | fprintf(stderr, "%*s prog %d", lvl*2, "", prn); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 1993 | if (child->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1994 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 1995 | (child->subshell ? "()" : "{}"), |
| 1996 | argv); |
| 1997 | debug_print_tree(child->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1998 | prn++; |
| 1999 | continue; |
| 2000 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2001 | if (argv) while (*argv) { |
| 2002 | fprintf(stderr, " '%s'", *argv); |
| 2003 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 2004 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2005 | fprintf(stderr, "\n"); |
| 2006 | prn++; |
| 2007 | } |
| 2008 | pi = pi->next; |
| 2009 | pin++; |
| 2010 | } |
| 2011 | } |
| 2012 | #endif |
| 2013 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2014 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 2015 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2016 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2017 | { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2018 | struct pipe *rpipe; |
| 2019 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2020 | char *for_varname = NULL; |
| 2021 | char **for_lcur = NULL; |
| 2022 | char **for_list = NULL; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2023 | int flag_rep = 0; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2024 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 2025 | #if ENABLE_HUSH_CASE |
| 2026 | char *case_word = NULL; |
| 2027 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2028 | int flag_skip = 1; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2029 | int rcode = 0; /* probably for gcc only */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2030 | int flag_restore = 0; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2031 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2032 | int if_code = 0, next_if_code = 0; /* need double-buffer to handle elif */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2033 | #else |
| 2034 | enum { if_code = 0, next_if_code = 0 }; |
| 2035 | #endif |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2036 | reserved_style rword IF_HAS_NO_KEYWORDS(= RES_NONE); |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2037 | reserved_style skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 2038 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2039 | debug_printf_exec("run_list start lvl %d\n", run_list_level + 1); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 2040 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2041 | #if ENABLE_HUSH_LOOPS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2042 | /* check syntax for "for" */ |
| 2043 | for (rpipe = pi; rpipe; rpipe = rpipe->next) { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 2044 | if (rpipe->res_word != RES_FOR && rpipe->res_word != RES_IN) |
| 2045 | continue; |
| 2046 | /* current word is FOR or IN (BOLD in comments below) */ |
| 2047 | if (rpipe->next == NULL) { |
| 2048 | syntax("malformed for"); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2049 | debug_printf_exec("run_list lvl %d return 1\n", run_list_level); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2050 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2051 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 2052 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 2053 | if (rpipe->next->res_word == RES_DO) |
| 2054 | continue; |
| 2055 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 2056 | if (rpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 2057 | || rpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2058 | ) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2059 | syntax("malformed for"); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2060 | debug_printf_exec("run_list lvl %d return 1\n", run_list_level); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2061 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2062 | } |
| 2063 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2064 | #else |
| 2065 | rpipe = NULL; |
| 2066 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2067 | |
| 2068 | #if ENABLE_HUSH_JOB |
| 2069 | /* Example of nested list: "while true; do { sleep 1 | exit 2; } done". |
| 2070 | * We are saving state before entering outermost list ("while...done") |
| 2071 | * so that ctrl-Z will correctly background _entire_ outermost list, |
| 2072 | * not just a part of it (like "sleep 1 | exit 2") */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 2073 | if (++run_list_level == 1 && interactive_fd) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2074 | if (sigsetjmp(toplevel_jb, 1)) { |
| 2075 | /* ctrl-Z forked and we are parent; or ctrl-C. |
| 2076 | * Sighandler has longjmped us here */ |
| 2077 | signal(SIGINT, SIG_IGN); |
| 2078 | signal(SIGTSTP, SIG_IGN); |
| 2079 | /* Restore level (we can be coming from deep inside |
| 2080 | * nested levels) */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 2081 | run_list_level = 1; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2082 | #if ENABLE_FEATURE_SH_STANDALONE |
| 2083 | if (nofork_save.saved) { /* if save area is valid */ |
| 2084 | debug_printf_jobs("exiting nofork early\n"); |
| 2085 | restore_nofork_data(&nofork_save); |
| 2086 | } |
| 2087 | #endif |
| 2088 | if (ctrl_z_flag) { |
| 2089 | /* ctrl-Z has forked and stored pid of the child in pi->pid. |
| 2090 | * Remember this child as background job */ |
| 2091 | insert_bg_job(pi); |
| 2092 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2093 | /* ctrl-C. We just stop doing whatever we were doing */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 2094 | bb_putchar('\n'); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2095 | } |
| 2096 | rcode = 0; |
| 2097 | goto ret; |
| 2098 | } |
| 2099 | /* ctrl-Z handler will store pid etc in pi */ |
| 2100 | toplevel_list = pi; |
| 2101 | ctrl_z_flag = 0; |
| 2102 | #if ENABLE_FEATURE_SH_STANDALONE |
| 2103 | nofork_save.saved = 0; /* in case we will run a nofork later */ |
| 2104 | #endif |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 2105 | signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2106 | signal(SIGINT, handler_ctrl_c); |
| 2107 | } |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2108 | #endif /* JOB */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2109 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2110 | for (; pi; pi = flag_restore ? rpipe : pi->next) { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2111 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 2112 | IF_HAS_NO_KEYWORDS(rword = RES_NONE;) |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2113 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2114 | if (rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2115 | flag_restore = 0; |
| 2116 | if (!rpipe) { |
| 2117 | flag_rep = 0; |
| 2118 | rpipe = pi; |
| 2119 | } |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2120 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2121 | #endif |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2122 | debug_printf_exec(": rword=%d if_code=%d next_if_code=%d skip_more=%d\n", |
| 2123 | rword, if_code, next_if_code, skip_more_for_this_rword); |
| 2124 | if (rword == skip_more_for_this_rword && flag_skip) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2125 | if (pi->followup == PIPE_SEQ) |
| 2126 | flag_skip = 0; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2127 | continue; |
| 2128 | } |
| 2129 | flag_skip = 1; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2130 | skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2131 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2132 | if (rword == RES_THEN || rword == RES_ELSE) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2133 | if_code = next_if_code; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2134 | if (rword == RES_THEN && if_code) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2135 | continue; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2136 | if (rword == RES_ELSE && !if_code) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2137 | continue; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2138 | if (rword == RES_ELIF && !if_code) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2139 | break; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2140 | #endif |
| 2141 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2142 | if (rword == RES_FOR && pi->num_progs) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2143 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2144 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2145 | |
| 2146 | static const char encoded_dollar_at[] ALIGN1 = { |
| 2147 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 2148 | }; /* encoded representation of "$@" */ |
| 2149 | static const char *const encoded_dollar_at_argv[] = { |
| 2150 | encoded_dollar_at, NULL |
| 2151 | }; /* argv list with one element: "$@" */ |
| 2152 | char **vals; |
| 2153 | |
| 2154 | vals = (char**)encoded_dollar_at_argv; |
| 2155 | if (rpipe->next->res_word == RES_IN) { |
| 2156 | /* if no variable values after "in" we skip "for" */ |
| 2157 | if (!pi->next->progs->argv) |
| 2158 | continue; |
| 2159 | vals = pi->next->progs->argv; |
| 2160 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2161 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2162 | debug_print_strings("for_list made from", vals); |
| 2163 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2164 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 2165 | debug_print_strings("for_list", for_list); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2166 | for_varname = pi->progs->argv[0]; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2167 | pi->progs->argv[0] = NULL; |
| 2168 | flag_rep = 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2169 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2170 | free(pi->progs->argv[0]); |
| 2171 | if (!*for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2172 | /* for loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2173 | free(for_list); |
| 2174 | for_lcur = NULL; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2175 | flag_rep = 0; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2176 | pi->progs->argv[0] = for_varname; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2177 | continue; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2178 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2179 | /* insert next value from for_lcur */ |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2180 | /* vda: does it need escaping? */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2181 | pi->progs->argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2182 | } |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2183 | if (rword == RES_IN) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2184 | continue; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2185 | if (rword == RES_DO) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2186 | if (!flag_rep) |
| 2187 | continue; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 2188 | } |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2189 | if (rword == RES_DONE) { |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2190 | if (flag_rep) { |
| 2191 | flag_restore = 1; |
| 2192 | } else { |
| 2193 | rpipe = NULL; |
| 2194 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2195 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2196 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 2197 | #if ENABLE_HUSH_CASE |
| 2198 | if (rword == RES_CASE) { |
| 2199 | case_word = pi->progs->argv[0]; |
| 2200 | continue; |
| 2201 | } |
| 2202 | if (rword == RES_MATCH) { |
| 2203 | if (case_word) { |
| 2204 | next_if_code = strcmp(case_word, pi->progs->argv[0]); |
| 2205 | if (next_if_code == 0) |
| 2206 | case_word = NULL; |
| 2207 | continue; |
| 2208 | } |
| 2209 | break; |
| 2210 | } |
| 2211 | if (rword == RES_CASEI) { |
| 2212 | if (next_if_code != 0) |
| 2213 | continue; |
| 2214 | } |
| 2215 | #endif |
| 2216 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2217 | if (pi->num_progs == 0) |
| 2218 | continue; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2219 | debug_printf_exec(": run_pipe with %d members\n", pi->num_progs); |
| 2220 | rcode = run_pipe(pi); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2221 | if (rcode != -1) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2222 | /* We only ran a builtin: rcode was set by the return value |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2223 | * of run_pipe(), and we don't need to wait for anything. */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2224 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2225 | /* What does bash do with attempts to background builtins? */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2226 | /* Even bash 3.2 doesn't do that well with nested bg: |
| 2227 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2228 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2229 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 2230 | if (run_list_level == 1) |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2231 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2232 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2233 | rcode = EXIT_SUCCESS; |
| 2234 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2235 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 2236 | if (run_list_level == 1 && interactive_fd) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2237 | /* waits for completion, then fg's main shell */ |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 2238 | rcode = checkjobs_and_fg_shell(pi); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2239 | } else |
| 2240 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 2241 | { /* this one just waits for completion */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2242 | rcode = checkjobs(pi); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2243 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2244 | debug_printf_exec(": checkjobs returned %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2245 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2246 | debug_printf_exec(": setting last_return_code=%d\n", rcode); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2247 | last_return_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2248 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2249 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2250 | next_if_code = rcode; /* can be overwritten a number of times */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2251 | #endif |
| 2252 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2253 | if (rword == RES_WHILE) |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2254 | flag_rep = !last_return_code; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2255 | if (rword == RES_UNTIL) |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2256 | flag_rep = last_return_code; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2257 | #endif |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2258 | if ((rcode == EXIT_SUCCESS && pi->followup == PIPE_OR) |
| 2259 | || (rcode != EXIT_SUCCESS && pi->followup == PIPE_AND) |
| 2260 | ) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2261 | skip_more_for_this_rword = rword; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2262 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 2263 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 2264 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2265 | |
| 2266 | #if ENABLE_HUSH_JOB |
| 2267 | if (ctrl_z_flag) { |
| 2268 | /* ctrl-Z forked somewhere in the past, we are the child, |
| 2269 | * and now we completed running the list. Exit. */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 2270 | //TODO: _exit? |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2271 | exit(rcode); |
| 2272 | } |
| 2273 | ret: |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 2274 | if (!--run_list_level && interactive_fd) { |
| 2275 | signal(SIGTSTP, SIG_IGN); |
| 2276 | signal(SIGINT, SIG_IGN); |
| 2277 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2278 | #endif |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2279 | debug_printf_exec("run_list lvl %d return %d\n", run_list_level + 1, rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2280 | return rcode; |
| 2281 | } |
| 2282 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2283 | /* return code is the exit status of the pipe */ |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2284 | static int free_pipe(struct pipe *pi, int indent) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2285 | { |
| 2286 | char **p; |
| 2287 | struct child_prog *child; |
| 2288 | struct redir_struct *r, *rnext; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2289 | int a, i, ret_code = 0; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 2290 | |
| 2291 | if (pi->stopped_progs > 0) |
| 2292 | return ret_code; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2293 | debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid()); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2294 | for (i = 0; i < pi->num_progs; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2295 | child = &pi->progs[i]; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2296 | debug_printf_clean("%s command %d:\n", indenter(indent), i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2297 | if (child->argv) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2298 | for (a = 0, p = child->argv; *p; a++, p++) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2299 | debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2300 | } |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 2301 | free_strings(child->argv); |
| 2302 | child->argv = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2303 | } else if (child->group) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2304 | 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] | 2305 | ret_code = free_pipe_list(child->group, indent+3); |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2306 | debug_printf_clean("%s end group\n", indenter(indent)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2307 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2308 | debug_printf_clean("%s (nil)\n", indenter(indent)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2309 | } |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2310 | for (r = child->redirects; r; r = rnext) { |
Denis Vlasenko | 211b59b | 2008-06-23 16:28:53 +0000 | [diff] [blame] | 2311 | 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] | 2312 | if (r->dup == -1) { |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2313 | /* guard against the case >$FOO, where foo is unset or blank */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2314 | if (r->rd_filename) { |
| 2315 | debug_printf_clean(" %s\n", r->rd_filename); |
| 2316 | free(r->rd_filename); |
| 2317 | r->rd_filename = NULL; |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2318 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2319 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2320 | debug_printf_clean("&%d\n", r->dup); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2321 | } |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2322 | rnext = r->next; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2323 | free(r); |
| 2324 | } |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2325 | child->redirects = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2326 | } |
| 2327 | 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] | 2328 | pi->progs = NULL; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2329 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2330 | free(pi->cmdtext); |
| 2331 | pi->cmdtext = NULL; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2332 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2333 | return ret_code; |
| 2334 | } |
| 2335 | |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2336 | static int free_pipe_list(struct pipe *head, int indent) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2337 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2338 | int rcode = 0; /* if list has no members */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2339 | struct pipe *pi, *next; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2340 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2341 | for (pi = head; pi; pi = next) { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2342 | #if HAS_KEYWORDS |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2343 | 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] | 2344 | #endif |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2345 | rcode = free_pipe(pi, indent); |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2346 | 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] | 2347 | next = pi->next; |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2348 | /*pi->next = NULL;*/ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2349 | free(pi); |
| 2350 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2351 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2355 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2356 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2357 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2358 | debug_printf_exec("run_and_free_list entered\n"); |
| 2359 | if (!fake_mode) { |
| 2360 | debug_printf_exec(": run_list with %d members\n", pi->num_progs); |
| 2361 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2362 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2363 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2364 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2365 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2366 | free_pipe_list(pi, /* indent: */ 0); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2367 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2368 | return rcode; |
| 2369 | } |
| 2370 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2371 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2372 | /* expand_strvec_to_strvec() takes a list of strings, expands |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2373 | * all variable references within and returns a pointer to |
| 2374 | * a list of expanded strings, possibly with larger number |
| 2375 | * of strings. (Think VAR="a b"; echo $VAR). |
| 2376 | * This new list is allocated as a single malloc block. |
| 2377 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 2378 | * followed by strings themself. |
| 2379 | * Caller can deallocate entire list by single free(list). */ |
| 2380 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2381 | /* Store given string, finalizing the word and starting new one whenever |
| 2382 | * we encounter ifs char(s). This is used for expanding variable values. |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2383 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2384 | 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] | 2385 | { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2386 | while (1) { |
| 2387 | int word_len = strcspn(str, ifs); |
| 2388 | if (word_len) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2389 | if (output->o_quote || !output->o_glob) |
| 2390 | o_addQstr(output, str, word_len); |
| 2391 | else /* protect backslashes against globbing up :) */ |
| 2392 | o_addstr_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2393 | str += word_len; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2394 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2395 | if (!*str) /* EOL - do not finalize word */ |
| 2396 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2397 | o_addchr(output, '\0'); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2398 | debug_print_list("expand_on_ifs", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2399 | n = o_save_ptr(output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2400 | str += strspn(str, ifs); /* skip ifs chars */ |
| 2401 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2402 | debug_print_list("expand_on_ifs[1]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2403 | return n; |
| 2404 | } |
| 2405 | |
| 2406 | /* Expand all variable references in given string, adding words to list[] |
| 2407 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 2408 | * to be filled). This routine is extremely tricky: has to deal with |
| 2409 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 2410 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2411 | /* NB: another bug is that we cannot detect empty strings yet: |
| 2412 | * "" or $empty"" expands to zero words, has to expand to empty word */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2413 | 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] | 2414 | { |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2415 | /* or_mask is either 0 (normal case) or 0x80 |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2416 | * (expansion of right-hand side of assignment == 1-element expand. |
| 2417 | * 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] | 2418 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2419 | char first_ch, ored_ch; |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2420 | int i; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2421 | const char *val; |
| 2422 | char *p; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2423 | |
| 2424 | ored_ch = 0; |
| 2425 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2426 | debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2427 | debug_print_list("expand_vars_to_list", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2428 | n = o_save_ptr(output, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2429 | debug_print_list("expand_vars_to_list[0]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2430 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2431 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2432 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2433 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2434 | #endif |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2435 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2436 | o_addstr(output, arg, p - arg); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2437 | debug_print_list("expand_vars_to_list[1]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2438 | arg = ++p; |
| 2439 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2440 | |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2441 | 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] | 2442 | /* "$@" is special. Even if quoted, it can still |
| 2443 | * expand to nothing (not even an empty string) */ |
| 2444 | if ((first_ch & 0x7f) != '@') |
| 2445 | ored_ch |= first_ch; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2446 | val = NULL; |
| 2447 | switch (first_ch & 0x7f) { |
| 2448 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 2449 | case '$': /* pid */ |
Denis Vlasenko | 16c2fea | 2008-06-17 12:28:44 +0000 | [diff] [blame] | 2450 | val = utoa(root_pid); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2451 | break; |
| 2452 | case '!': /* bg pid */ |
| 2453 | val = last_bg_pid ? utoa(last_bg_pid) : (char*)""; |
| 2454 | break; |
| 2455 | case '?': /* exitcode */ |
| 2456 | val = utoa(last_return_code); |
| 2457 | break; |
| 2458 | case '#': /* argc */ |
| 2459 | val = utoa(global_argc ? global_argc-1 : 0); |
| 2460 | break; |
| 2461 | case '*': |
| 2462 | case '@': |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2463 | i = 1; |
Denis Vlasenko | c3c6659 | 2007-11-24 00:22:42 +0000 | [diff] [blame] | 2464 | if (!global_argv[i]) |
| 2465 | break; |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 2466 | 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] | 2467 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2468 | smallint sv = output->o_quote; |
| 2469 | /* unquoted var's contents should be globbed, so don't quote */ |
| 2470 | output->o_quote = 0; |
Denis Vlasenko | c3c6659 | 2007-11-24 00:22:42 +0000 | [diff] [blame] | 2471 | while (global_argv[i]) { |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2472 | n = expand_on_ifs(output, n, global_argv[i]); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2473 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, global_argc-1); |
Denis Vlasenko | c3c6659 | 2007-11-24 00:22:42 +0000 | [diff] [blame] | 2474 | if (global_argv[i++][0] && global_argv[i]) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2475 | /* this argv[] is not empty and not last: |
| 2476 | * put terminating NUL, start new word */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2477 | o_addchr(output, '\0'); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2478 | debug_print_list("expand_vars_to_list[2]", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2479 | n = o_save_ptr(output, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2480 | debug_print_list("expand_vars_to_list[3]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2481 | } |
| 2482 | } |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2483 | output->o_quote = sv; |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2484 | } else |
| 2485 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2486 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2487 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2488 | while (1) { |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2489 | o_addQstr(output, global_argv[i], strlen(global_argv[i])); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2490 | if (++i >= global_argc) |
| 2491 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2492 | o_addchr(output, '\0'); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2493 | debug_print_list("expand_vars_to_list[4]", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2494 | n = o_save_ptr(output, n); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2495 | } |
| 2496 | } else { /* quoted $*: add as one word */ |
Denis Vlasenko | c3c6659 | 2007-11-24 00:22:42 +0000 | [diff] [blame] | 2497 | while (1) { |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2498 | o_addQstr(output, global_argv[i], strlen(global_argv[i])); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2499 | if (!global_argv[++i]) |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2500 | break; |
| 2501 | if (ifs[0]) |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2502 | o_addchr(output, ifs[0]); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2503 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2504 | } |
| 2505 | break; |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2506 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 2507 | /* "Empty variable", used to make "" etc to not disappear */ |
| 2508 | arg++; |
| 2509 | ored_ch = 0x80; |
| 2510 | break; |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2511 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2512 | case '`': { /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2513 | struct in_str input; |
| 2514 | *p = '\0'; |
| 2515 | arg++; |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2516 | //TODO: can we just stuff it into "output" directly? |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2517 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2518 | setup_string_in_str(&input, arg); |
| 2519 | process_command_subs(&subst_result, &input, NULL); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2520 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2521 | val = subst_result.data; |
| 2522 | goto store_val; |
| 2523 | } |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2524 | #endif |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2525 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2526 | *p = '\0'; |
| 2527 | arg[0] = first_ch & 0x7f; |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2528 | if (isdigit(arg[0])) { |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2529 | i = xatoi_u(arg); |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2530 | if (i < global_argc) |
| 2531 | val = global_argv[i]; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2532 | /* else val remains NULL: $N with too big N */ |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2533 | } else |
| 2534 | val = lookup_param(arg); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2535 | arg[0] = first_ch; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2536 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2537 | store_val: |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2538 | #endif |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2539 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2540 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2541 | 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] | 2542 | if (val) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2543 | /* unquoted var's contents should be globbed, so don't quote */ |
| 2544 | smallint sv = output->o_quote; |
| 2545 | output->o_quote = 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2546 | n = expand_on_ifs(output, n, val); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2547 | val = NULL; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2548 | output->o_quote = sv; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2549 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2550 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2551 | 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] | 2552 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2553 | } |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2554 | if (val) { |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2555 | o_addQstr(output, val, strlen(val)); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2556 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2557 | |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2558 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2559 | o_free(&subst_result); |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2560 | #endif |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2561 | arg = ++p; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2562 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 2563 | |
Denis Vlasenko | 2e76c3f | 2008-06-10 18:27:50 +0000 | [diff] [blame] | 2564 | if (arg[0]) { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2565 | debug_print_list("expand_vars_to_list[a]", output, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2566 | /* this part is literal, and it was already pre-quoted |
| 2567 | * if needed (much earlier), do not use o_addQstr here! */ |
| 2568 | o_addstr(output, arg, strlen(arg) + 1); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2569 | debug_print_list("expand_vars_to_list[b]", output, n); |
Denis Vlasenko | 2e76c3f | 2008-06-10 18:27:50 +0000 | [diff] [blame] | 2570 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 2571 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 2572 | ) { |
| 2573 | n--; |
| 2574 | /* allow to reuse list[n] later without re-growth */ |
| 2575 | output->has_empty_slot = 1; |
| 2576 | } else { |
| 2577 | o_addchr(output, '\0'); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2578 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2579 | return n; |
| 2580 | } |
| 2581 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2582 | static char **expand_variables(char **argv, int or_mask) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2583 | { |
| 2584 | int n; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2585 | char **list; |
| 2586 | char **v; |
| 2587 | o_string output = NULL_O_STRING; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2588 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2589 | if (or_mask & 0x100) { |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2590 | output.o_quote = 1; /* protect against globbing for "$var" */ |
| 2591 | /* (unquoted $var will temporarily switch it off) */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2592 | output.o_glob = 1; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2593 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2594 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2595 | n = 0; |
| 2596 | v = argv; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2597 | while (*v) { |
| 2598 | n = expand_vars_to_list(&output, n, *v, (char)or_mask); |
| 2599 | v++; |
| 2600 | } |
| 2601 | debug_print_list("expand_variables", &output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2602 | |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 2603 | /* output.data (malloced in one block) gets returned in "list" */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2604 | list = o_finalize_list(&output, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2605 | debug_print_strings("expand_variables[1]", list); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2606 | return list; |
| 2607 | } |
| 2608 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2609 | static char **expand_strvec_to_strvec(char **argv) |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2610 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2611 | return expand_variables(argv, 0x100); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2614 | /* Used for expansion of right hand of assignments */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 2615 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 2616 | * "v=/bin/c*" */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2617 | static char *expand_string_to_string(const char *str) |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2618 | { |
| 2619 | char *argv[2], **list; |
| 2620 | |
| 2621 | argv[0] = (char*)str; |
| 2622 | argv[1] = NULL; |
| 2623 | list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2624 | if (HUSH_DEBUG) |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2625 | if (!list[0] || list[1]) |
| 2626 | bb_error_msg_and_die("BUG in varexp2"); |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2627 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 2628 | strcpy((char*)list, list[0]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2629 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 2630 | return (char*)list; |
| 2631 | } |
| 2632 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2633 | /* Used for "eval" builtin */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2634 | static char* expand_strvec_to_string(char **argv) |
| 2635 | { |
| 2636 | char **list; |
| 2637 | |
| 2638 | list = expand_variables(argv, 0x80); |
| 2639 | /* Convert all NULs to spaces */ |
| 2640 | if (list[0]) { |
| 2641 | int n = 1; |
| 2642 | while (list[n]) { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2643 | if (HUSH_DEBUG) |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2644 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 2645 | bb_error_msg_and_die("BUG in varexp3"); |
| 2646 | list[n][-1] = ' '; /* TODO: or to ifs[0]? */ |
| 2647 | n++; |
| 2648 | } |
| 2649 | } |
| 2650 | strcpy((char*)list, list[0]); |
| 2651 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2652 | return (char*)list; |
| 2653 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2654 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2655 | |
| 2656 | /* Used to get/check local shell variables */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2657 | static struct variable *get_local_var(const char *name) |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2658 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2659 | struct variable *cur; |
| 2660 | int len; |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2661 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2662 | if (!name) |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2663 | return NULL; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2664 | len = strlen(name); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2665 | for (cur = top_var; cur; cur = cur->next) { |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2666 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2667 | return cur; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 2668 | } |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2669 | return NULL; |
| 2670 | } |
| 2671 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2672 | /* str holds "NAME=VAL" and is expected to be malloced. |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2673 | * We take ownership of it. */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2674 | static int set_local_var(char *str, int flg_export) |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2675 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2676 | struct variable *cur; |
| 2677 | char *value; |
| 2678 | int name_len; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2679 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2680 | value = strchr(str, '='); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2681 | if (!value) { /* not expected to ever happen? */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2682 | free(str); |
Eric Andersen | 9978576 | 2001-05-22 21:37:48 +0000 | [diff] [blame] | 2683 | return -1; |
| 2684 | } |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2685 | |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2686 | name_len = value - str + 1; /* including '=' */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2687 | cur = top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */ |
| 2688 | while (1) { |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2689 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2690 | if (!cur->next) { |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2691 | /* Bail out. Note that now cur points |
| 2692 | * to last var in linked list */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2693 | break; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2694 | } |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2695 | cur = cur->next; |
| 2696 | continue; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2697 | } |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2698 | /* We found an existing var with this name */ |
| 2699 | *value = '\0'; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2700 | if (cur->flg_read_only) { |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2701 | bb_error_msg("%s: readonly variable", str); |
| 2702 | free(str); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2703 | return -1; |
| 2704 | } |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2705 | unsetenv(str); /* just in case */ |
| 2706 | *value = '='; |
| 2707 | if (strcmp(cur->varstr, str) == 0) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2708 | free_and_exp: |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2709 | free(str); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2710 | goto exp; |
| 2711 | } |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2712 | if (cur->max_len >= strlen(str)) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2713 | /* This one is from startup env, reuse space */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2714 | strcpy(cur->varstr, str); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2715 | goto free_and_exp; |
| 2716 | } |
| 2717 | /* max_len == 0 signifies "malloced" var, which we can |
| 2718 | * (and has to) free */ |
| 2719 | if (!cur->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2720 | free(cur->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2721 | cur->max_len = 0; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2722 | goto set_str_and_exp; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2723 | } |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2724 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2725 | /* Not found - create next variable struct */ |
| 2726 | cur->next = xzalloc(sizeof(*cur)); |
| 2727 | cur = cur->next; |
| 2728 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2729 | set_str_and_exp: |
| 2730 | cur->varstr = str; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2731 | exp: |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2732 | if (flg_export) |
| 2733 | cur->flg_export = 1; |
| 2734 | if (cur->flg_export) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2735 | return putenv(cur->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2736 | return 0; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2739 | static void unset_local_var(const char *name) |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2740 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2741 | struct variable *cur; |
| 2742 | struct variable *prev = prev; /* for gcc */ |
| 2743 | int name_len; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2744 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2745 | if (!name) |
| 2746 | return; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2747 | name_len = strlen(name); |
| 2748 | cur = top_var; |
| 2749 | while (cur) { |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2750 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2751 | if (cur->flg_read_only) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 2752 | bb_error_msg("%s: readonly variable", name); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2753 | return; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2754 | } |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2755 | /* prev is ok to use here because 1st variable, HUSH_VERSION, |
| 2756 | * is ro, and we cannot reach this code on the 1st pass */ |
| 2757 | prev->next = cur->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2758 | unsetenv(cur->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2759 | if (!cur->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2760 | free(cur->varstr); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2761 | free(cur); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2762 | return; |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2763 | } |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2764 | prev = cur; |
| 2765 | cur = cur->next; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2766 | } |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2769 | /* 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] | 2770 | * for file descriptor duplication, e.g., "2>&1". |
| 2771 | * Return code is 0 normally, 1 if a syntax error is detected in src. |
| 2772 | * Resource errors (in xmalloc) cause the process to exit */ |
| 2773 | static int setup_redirect(struct p_context *ctx, int fd, redir_type style, |
| 2774 | struct in_str *input) |
| 2775 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2776 | struct child_prog *child = ctx->child; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2777 | struct redir_struct *redir = child->redirects; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2778 | struct redir_struct *last_redir = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2779 | |
| 2780 | /* 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] | 2781 | while (redir) { |
| 2782 | last_redir = redir; |
| 2783 | redir = redir->next; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2784 | } |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 2785 | redir = xzalloc(sizeof(struct redir_struct)); |
| 2786 | /* redir->next = NULL; */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2787 | /* redir->rd_filename = NULL; */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2788 | if (last_redir) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2789 | last_redir->next = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2790 | } else { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2791 | child->redirects = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2794 | redir->rd_type = style; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2795 | redir->fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2796 | |
| 2797 | debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip); |
| 2798 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2799 | /* Check for a '2>&1' type redirect */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2800 | redir->dup = redirect_dup_num(input); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2801 | if (redir->dup == -2) |
| 2802 | return 1; /* syntax error */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2803 | if (redir->dup != -1) { |
| 2804 | /* Erik had a check here that the file descriptor in question |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2805 | * is legit; I postpone that to "run time" |
| 2806 | * A "-" representation of "close me" shows up as a -3 here */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2807 | debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup); |
| 2808 | } else { |
| 2809 | /* We do _not_ try to open the file that src points to, |
| 2810 | * since we need to return and let src be expanded first. |
| 2811 | * Set ctx->pending_redirect, so we know what to do at the |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2812 | * end of the next parsed word. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2813 | ctx->pending_redirect = redir; |
| 2814 | } |
| 2815 | return 0; |
| 2816 | } |
| 2817 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 2818 | static struct pipe *new_pipe(void) |
| 2819 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2820 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2821 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2822 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2823 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2824 | return pi; |
| 2825 | } |
| 2826 | |
| 2827 | static void initialize_context(struct p_context *ctx) |
| 2828 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2829 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2830 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2831 | /* Create the memory for child, roughly: |
| 2832 | * ctx->pipe->progs = new struct child_prog; |
| 2833 | * ctx->pipe->progs[0].family = ctx->pipe; |
| 2834 | * ctx->child = &ctx->pipe->progs[0]; |
| 2835 | */ |
| 2836 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2839 | /* If a reserved word is found and processed, parse context is modified |
| 2840 | * and 1 is returned. |
| 2841 | * Handles if, then, elif, else, fi, for, while, until, do, done. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2842 | * case, function, and select are obnoxious, save those for later. |
| 2843 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2844 | #if HAS_KEYWORDS |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2845 | static int reserved_word(const o_string *word, struct p_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2846 | { |
| 2847 | struct reserved_combo { |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2848 | char literal[7]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2849 | unsigned char res; |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2850 | int flag; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2851 | }; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2852 | enum { |
| 2853 | FLAG_END = (1 << RES_NONE ), |
| 2854 | #if ENABLE_HUSH_IF |
| 2855 | FLAG_IF = (1 << RES_IF ), |
| 2856 | FLAG_THEN = (1 << RES_THEN ), |
| 2857 | FLAG_ELIF = (1 << RES_ELIF ), |
| 2858 | FLAG_ELSE = (1 << RES_ELSE ), |
| 2859 | FLAG_FI = (1 << RES_FI ), |
| 2860 | #endif |
| 2861 | #if ENABLE_HUSH_LOOPS |
| 2862 | FLAG_FOR = (1 << RES_FOR ), |
| 2863 | FLAG_WHILE = (1 << RES_WHILE), |
| 2864 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 2865 | FLAG_DO = (1 << RES_DO ), |
| 2866 | FLAG_DONE = (1 << RES_DONE ), |
| 2867 | FLAG_IN = (1 << RES_IN ), |
| 2868 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 2869 | #if ENABLE_HUSH_CASE |
| 2870 | FLAG_MATCH = (1 << RES_MATCH), |
| 2871 | FLAG_ESAC = (1 << RES_ESAC ), |
| 2872 | #endif |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2873 | FLAG_START = (1 << RES_XXXX ), |
| 2874 | }; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2875 | /* Mostly a list of accepted follow-up reserved words. |
| 2876 | * FLAG_END means we are done with the sequence, and are ready |
| 2877 | * to turn the compound list into a command. |
| 2878 | * FLAG_START means the word must start a new compound list. |
| 2879 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2880 | static const struct reserved_combo reserved_list[] = { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2881 | #if ENABLE_HUSH_IF |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2882 | { "!", RES_NONE, 0 }, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2883 | { "if", RES_IF, FLAG_THEN | FLAG_START }, |
| 2884 | { "then", RES_THEN, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 2885 | { "elif", RES_ELIF, FLAG_THEN }, |
| 2886 | { "else", RES_ELSE, FLAG_FI }, |
| 2887 | { "fi", RES_FI, FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2888 | #endif |
| 2889 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2890 | { "for", RES_FOR, FLAG_IN | FLAG_DO | FLAG_START }, |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2891 | { "while", RES_WHILE, FLAG_DO | FLAG_START }, |
| 2892 | { "until", RES_UNTIL, FLAG_DO | FLAG_START }, |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2893 | { "in", RES_IN, FLAG_DO }, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2894 | { "do", RES_DO, FLAG_DONE }, |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 2895 | { "done", RES_DONE, FLAG_END }, |
| 2896 | #endif |
| 2897 | #if ENABLE_HUSH_CASE |
| 2898 | { "case", RES_CASE, FLAG_MATCH | FLAG_START }, |
| 2899 | { "esac", RES_ESAC, FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2900 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2901 | }; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 2902 | #if ENABLE_HUSH_CASE |
| 2903 | static const struct reserved_combo reserved_match = { |
| 2904 | "" /* "match" */, RES_MATCH, FLAG_MATCH | FLAG_ESAC |
| 2905 | }; |
| 2906 | #endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2907 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2908 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 2909 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2910 | if (strcmp(word->data, r->literal) != 0) |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2911 | continue; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2912 | 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^] | 2913 | #if ENABLE_HUSH_CASE |
| 2914 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE) |
| 2915 | /* "case word IN ..." - IN part starts first match part */ |
| 2916 | r = &reserved_match; |
| 2917 | else |
| 2918 | #endif |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2919 | if (r->flag == 0) { /* '!' */ |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 2920 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2921 | syntax(NULL); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2922 | IF_HAS_KEYWORDS(ctx->ctx_res_w = RES_SNTX;) |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2923 | } |
| 2924 | ctx->ctx_inverted = 1; |
| 2925 | return 1; |
| 2926 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2927 | if (r->flag & FLAG_START) { |
| 2928 | struct p_context *new; |
| 2929 | debug_printf("push stack\n"); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2930 | new = xmalloc(sizeof(*new)); |
| 2931 | *new = *ctx; /* physical copy */ |
| 2932 | initialize_context(ctx); |
| 2933 | ctx->stack = new; |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 2934 | } 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] | 2935 | syntax(NULL); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2936 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2937 | return 1; |
| 2938 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2939 | ctx->ctx_res_w = r->res; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2940 | ctx->old_flag = r->flag; |
| 2941 | if (ctx->old_flag & FLAG_END) { |
| 2942 | struct p_context *old; |
| 2943 | debug_printf("pop stack\n"); |
| 2944 | done_pipe(ctx, PIPE_SEQ); |
| 2945 | old = ctx->stack; |
| 2946 | old->child->group = ctx->list_head; |
| 2947 | old->child->subshell = 0; |
| 2948 | *ctx = *old; /* physical copy */ |
| 2949 | free(old); |
| 2950 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2951 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2952 | } |
| 2953 | return 0; |
| 2954 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2955 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2956 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2957 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2958 | * Normal return is 0. Syntax errors return 1. */ |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2959 | static int done_word(o_string *word, struct p_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2960 | { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 2961 | struct child_prog *child = ctx->child; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2962 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2963 | /* If this word wasn't an assignment, next ones definitely |
| 2964 | * can't be assignments. Even if they look like ones. */ |
| 2965 | if (word->o_assignment != DEFINITELY_ASSIGNMENT) { |
| 2966 | word->o_assignment = NOT_ASSIGNMENT; |
| 2967 | } else { |
| 2968 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 2969 | } |
| 2970 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2971 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, child); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2972 | if (word->length == 0 && word->nonnull == 0) { |
| 2973 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 2974 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2975 | } |
| 2976 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2977 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 2978 | * only if run as "bash", not "sh" */ |
| 2979 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2980 | word->o_assignment = NOT_ASSIGNMENT; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2981 | debug_printf("word stored in rd_filename: '%s'\n", word->data); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2982 | } else { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 2983 | // if (child->group) { /* TODO: example how to trigger? */ |
| 2984 | // syntax(NULL); |
| 2985 | // debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n"); |
| 2986 | // return 1; |
| 2987 | // } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2988 | #if HAS_KEYWORDS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 2989 | #if ENABLE_HUSH_CASE |
| 2990 | if (ctx->ctx_dsemicolon) { |
| 2991 | /* already done when ctx_dsemicolon was set to 1 */ |
| 2992 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 2993 | ctx->ctx_dsemicolon = 0; |
| 2994 | } else |
| 2995 | #endif |
| 2996 | |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 2997 | if (!child->argv /* if it's the first word... */ |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 2998 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 2999 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3000 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3001 | #endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3002 | ) { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3003 | debug_printf_parse(": checking '%s' for reserved-ness\n", word->data); |
| 3004 | if (reserved_word(word, ctx)) { |
| 3005 | o_reset(word); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3006 | word->o_assignment = NOT_ASSIGNMENT; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3007 | debug_printf_parse("done_word return %d\n", (ctx->ctx_res_w == RES_SNTX)); |
| 3008 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3009 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3010 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3011 | #endif |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3012 | 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] | 3013 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3014 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3015 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3016 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3017 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3018 | char *p = word->data; |
| 3019 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3020 | && (p[1] & 0x7f) == '@' |
| 3021 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3022 | ) { |
| 3023 | p += 3; |
| 3024 | } |
| 3025 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3026 | /* saw no "$@", or not only "$@" but some |
| 3027 | * real text is there too */ |
| 3028 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3029 | * e.g. "", $empty"" etc to not disappear */ |
| 3030 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3031 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3032 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3033 | } |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3034 | child->argv = add_malloced_string_to_strings(child->argv, xstrdup(word->data)); |
| 3035 | debug_print_strings("word appended to argv", child->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3036 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3037 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3038 | o_reset(word); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3039 | ctx->pending_redirect = NULL; |
| 3040 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3041 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3042 | /* Force FOR to have just one word (variable name) */ |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3043 | /* NB: basically, this makes hush see "for v in ..." syntax as if |
| 3044 | * as it is "for v; in ...". FOR and IN become two pipe structs |
| 3045 | * in parse tree. */ |
| 3046 | if (ctx->ctx_res_w == RES_FOR) { |
| 3047 | //TODO: check that child->argv[0] is a valid variable name! |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3048 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3049 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3050 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 3051 | #if ENABLE_HUSH_CASE |
| 3052 | /* Force CASE to have just one word */ |
| 3053 | if (ctx->ctx_res_w == RES_CASE) { |
| 3054 | done_pipe(ctx, PIPE_SEQ); |
| 3055 | } |
| 3056 | #endif |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3057 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3058 | return 0; |
| 3059 | } |
| 3060 | |
Denis Vlasenko | 6eaf8de | 2008-06-17 12:09:21 +0000 | [diff] [blame] | 3061 | /* Command (member of a pipe) is complete. The only possible error here |
| 3062 | * is out of memory, in which case xmalloc exits. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3063 | static int done_command(struct p_context *ctx) |
| 3064 | { |
| 3065 | /* The child is really already in the pipe structure, so |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3066 | * advance the pipe counter and make a new, null child. */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3067 | struct pipe *pi = ctx->pipe; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3068 | struct child_prog *child = ctx->child; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3069 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3070 | if (child) { |
| 3071 | if (child->group == NULL |
| 3072 | && child->argv == NULL |
| 3073 | && child->redirects == NULL |
| 3074 | ) { |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3075 | debug_printf_parse("done_command: skipping null cmd, num_progs=%d\n", pi->num_progs); |
| 3076 | return pi->num_progs; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3077 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3078 | pi->num_progs++; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3079 | debug_printf_parse("done_command: ++num_progs=%d\n", pi->num_progs); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3080 | } else { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3081 | 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] | 3082 | } |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3083 | |
| 3084 | /* Only real trickiness here is that the uncommitted |
| 3085 | * child structure is not counted in pi->num_progs. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3086 | pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1)); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3087 | child = &pi->progs[pi->num_progs]; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3088 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3089 | memset(child, 0, sizeof(*child)); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3090 | child->family = pi; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3091 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3092 | ctx->child = child; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3093 | /* but ctx->pipe and ctx->list_head remain unchanged */ |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3094 | |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3095 | return pi->num_progs; /* used only for 0/nonzero check */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3098 | static void done_pipe(struct p_context *ctx, pipe_style type) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3099 | { |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3100 | int not_null; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3101 | |
| 3102 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3103 | not_null = done_command(ctx); /* implicit closure of previous command */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3104 | ctx->pipe->followup = type; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3105 | IF_HAS_KEYWORDS(ctx->pipe->pi_inverted = ctx->ctx_inverted;) |
| 3106 | IF_HAS_KEYWORDS(ctx->ctx_inverted = 0;) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3107 | IF_HAS_KEYWORDS(ctx->pipe->res_word = ctx->ctx_res_w;) |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3108 | /* Without this check, even just <enter> on command line generates |
| 3109 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | 6eaf8de | 2008-06-17 12:09:21 +0000 | [diff] [blame] | 3110 | * IOW: it is safe to do it unconditionally. |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3111 | * RES_NONE case is for "for a in; do ..." (empty IN set) |
| 3112 | * to work, possibly other cases too. */ |
| 3113 | if (not_null IF_HAS_KEYWORDS(|| ctx->ctx_res_w != RES_NONE)) { |
Denis Vlasenko | 6eaf8de | 2008-06-17 12:09:21 +0000 | [diff] [blame] | 3114 | struct pipe *new_p = new_pipe(); |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3115 | ctx->pipe->next = new_p; |
| 3116 | ctx->pipe = new_p; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3117 | ctx->child = NULL; /* needed! */ |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3118 | /* RES_IF, RES_WHILE etc are "sticky" - |
| 3119 | * they remain set for commands inside if/while. |
| 3120 | * This is used to control execution. |
| 3121 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3122 | * cases where variable or value happens to match a keyword): |
| 3123 | */ |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3124 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3125 | if (ctx->ctx_res_w == RES_FOR |
| 3126 | || ctx->ctx_res_w == RES_IN) |
| 3127 | ctx->ctx_res_w = RES_NONE; |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3128 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 3129 | #if ENABLE_HUSH_CASE |
| 3130 | if (ctx->ctx_res_w == RES_MATCH) |
| 3131 | ctx->ctx_res_w = RES_CASEI; |
| 3132 | #endif |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3133 | /* Create the memory for child, roughly: |
| 3134 | * ctx->pipe->progs = new struct child_prog; |
| 3135 | * ctx->pipe->progs[0].family = ctx->pipe; |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 3136 | * ctx->child = &ctx->pipe->progs[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3137 | */ |
| 3138 | done_command(ctx); |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3139 | } |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3140 | debug_printf_parse("done_pipe return\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3143 | /* 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] | 3144 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3145 | * Return either -2 (syntax error), -1 (no &), or the number found. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3146 | */ |
| 3147 | static int redirect_dup_num(struct in_str *input) |
| 3148 | { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3149 | int ch, d = 0, ok = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3150 | ch = i_peek(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3151 | if (ch != '&') return -1; |
| 3152 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3153 | i_getch(input); /* get the & */ |
| 3154 | ch = i_peek(input); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3155 | if (ch == '-') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3156 | i_getch(input); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3157 | return -3; /* "-" represents "close me" */ |
| 3158 | } |
| 3159 | while (isdigit(ch)) { |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3160 | d = d*10 + (ch-'0'); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3161 | ok = 1; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3162 | i_getch(input); |
| 3163 | ch = i_peek(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3164 | } |
| 3165 | if (ok) return d; |
| 3166 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3167 | bb_error_msg("ambiguous redirect"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3168 | return -2; |
| 3169 | } |
| 3170 | |
| 3171 | /* If a redirect is immediately preceded by a number, that number is |
| 3172 | * supposed to tell which file descriptor to redirect. This routine |
| 3173 | * looks for such preceding numbers. In an ideal world this routine |
| 3174 | * needs to handle all the following classes of redirects... |
| 3175 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3176 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3177 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3178 | * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo |
| 3179 | * A -1 output from this program means no valid number was found, so the |
| 3180 | * caller should use the appropriate default for this redirection. |
| 3181 | */ |
| 3182 | static int redirect_opt_num(o_string *o) |
| 3183 | { |
| 3184 | int num; |
| 3185 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3186 | if (o->length == 0) |
| 3187 | return -1; |
| 3188 | for (num = 0; num < o->length; num++) { |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3189 | if (!isdigit(o->data[num])) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3190 | return -1; |
| 3191 | } |
| 3192 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3193 | num = atoi(o->data); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3194 | o_reset(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3195 | return num; |
| 3196 | } |
| 3197 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3198 | #if ENABLE_HUSH_TICK |
"Vladimir N. Oleynik" | 19c3701 | 2005-09-22 14:33:15 +0000 | [diff] [blame] | 3199 | static FILE *generate_stream_from_list(struct pipe *head) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3200 | { |
| 3201 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3202 | int pid, channel[2]; |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3203 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 3204 | xpipe(channel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3205 | /* *** NOMMU WARNING *** */ |
| 3206 | /* By using vfork here, we suspend parent till child exits or execs. |
| 3207 | * If child will not do it before it fills the pipe, it can block forever |
| 3208 | * in write(STDOUT_FILENO), and parent (shell) will be also stuck. |
Denis Vlasenko | 3fe4f98 | 2008-06-09 16:02:39 +0000 | [diff] [blame] | 3209 | * Try this script: |
| 3210 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >TESTFILE |
| 3211 | * huge=`cat TESTFILE` # will block here forever |
| 3212 | * echo OK |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3213 | */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 3214 | pid = BB_MMU ? fork() : vfork(); |
| 3215 | if (pid < 0) |
| 3216 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3217 | if (pid == 0) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 3218 | if (ENABLE_HUSH_JOB) |
| 3219 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 3220 | close(channel[0]); /* NB: close _first_, then move fd! */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3221 | xmove_fd(channel[1], 1); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3222 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3223 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3224 | run_list_level = 1; |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3225 | #endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3226 | /* Process substitution is not considered to be usual |
| 3227 | * 'command execution'. |
| 3228 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. */ |
| 3229 | /* Not needed, we are relying on it being disabled |
| 3230 | * everywhere outside actual command execution. */ |
| 3231 | /*set_jobctrl_sighandler(SIG_IGN);*/ |
| 3232 | set_misc_sighandler(SIG_DFL); |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3233 | /* Freeing 'head' here would break NOMMU. */ |
| 3234 | _exit(run_list(head)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3235 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3236 | close(channel[1]); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3237 | pf = fdopen(channel[0], "r"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3238 | return pf; |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3239 | /* 'head' is freed by the caller */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3242 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3243 | static int process_command_subs(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3244 | struct in_str *input, |
| 3245 | const char *subst_end) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3246 | { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3247 | int retcode, ch, eol_cnt; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3248 | o_string result = NULL_O_STRING; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3249 | struct p_context inner; |
| 3250 | FILE *p; |
| 3251 | struct in_str pipe_str; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3252 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3253 | initialize_context(&inner); |
| 3254 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3255 | /* Recursion to generate command */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3256 | retcode = parse_stream(&result, &inner, input, subst_end); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3257 | if (retcode != 0) |
| 3258 | return retcode; /* syntax error or EOF */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3259 | done_word(&result, &inner); |
| 3260 | done_pipe(&inner, PIPE_SEQ); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3261 | o_free(&result); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3262 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3263 | p = generate_stream_from_list(inner.list_head); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3264 | if (p == NULL) |
| 3265 | return 1; |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 3266 | close_on_exec_on(fileno(p)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3267 | setup_file_in_str(&pipe_str, p); |
| 3268 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3269 | /* Now send results of command back into original context */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3270 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3271 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3272 | if (ch == '\n') { |
| 3273 | eol_cnt++; |
| 3274 | continue; |
| 3275 | } |
| 3276 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3277 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3278 | eol_cnt--; |
| 3279 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3280 | // /* Even unquoted `echo '\'` results in two backslashes |
| 3281 | // * (which are converted into one by globbing later) */ |
| 3282 | // if (!dest->o_quote && ch == '\\') { |
| 3283 | // o_addchr(dest, ch); |
| 3284 | // } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3285 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | debug_printf("done reading from pipe, pclose()ing\n"); |
| 3289 | /* This is the step that wait()s for the child. Should be pretty |
| 3290 | * 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] | 3291 | * to do better, by using wait(), and keeping track of background jobs |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3292 | * at the same time. That would be a lot of work, and contrary |
| 3293 | * to the KISS philosophy of this program. */ |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3294 | retcode = fclose(p); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3295 | free_pipe_list(inner.list_head, /* indent: */ 0); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3296 | debug_printf("closed FILE from child, retcode=%d\n", retcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3297 | return retcode; |
| 3298 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3299 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3300 | |
| 3301 | static int parse_group(o_string *dest, struct p_context *ctx, |
| 3302 | struct in_str *input, int ch) |
| 3303 | { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3304 | int rcode; |
| 3305 | const char *endch = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3306 | struct p_context sub; |
| 3307 | struct child_prog *child = ctx->child; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3308 | |
| 3309 | debug_printf_parse("parse_group entered\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3310 | if (child->argv) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3311 | syntax(NULL); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3312 | debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n"); |
| 3313 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3314 | } |
| 3315 | initialize_context(&sub); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3316 | endch = "}"; |
| 3317 | if (ch == '(') { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3318 | endch = ")"; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3319 | child->subshell = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3320 | } |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3321 | rcode = parse_stream(dest, &sub, input, endch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3322 | if (rcode == 0) { |
| 3323 | done_word(dest, &sub); /* finish off the final word in the subcontext */ |
| 3324 | done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */ |
| 3325 | child->group = sub.list_head; |
| 3326 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3327 | debug_printf_parse("parse_group return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3328 | return rcode; |
| 3329 | /* child remains "open", available for possible redirects */ |
| 3330 | } |
| 3331 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3332 | /* Basically useful version until someone wants to get fancier, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3333 | * see the bash man page under "Parameter Expansion" */ |
Denis Vlasenko | 15d78fb | 2007-01-30 22:28:21 +0000 | [diff] [blame] | 3334 | static const char *lookup_param(const char *src) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3335 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3336 | struct variable *var = get_local_var(src); |
| 3337 | if (var) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 3338 | return strchr(var->varstr, '=') + 1; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3339 | return NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3342 | #if ENABLE_HUSH_TICK |
| 3343 | /* Subroutines for copying $(...) and `...` things */ |
| 3344 | static void add_till_backquote(o_string *dest, struct in_str *input); |
| 3345 | /* '...' */ |
| 3346 | static void add_till_single_quote(o_string *dest, struct in_str *input) |
| 3347 | { |
| 3348 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3349 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3350 | if (ch == EOF) |
| 3351 | break; |
| 3352 | if (ch == '\'') |
| 3353 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3354 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3355 | } |
| 3356 | } |
| 3357 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
| 3358 | static void add_till_double_quote(o_string *dest, struct in_str *input) |
| 3359 | { |
| 3360 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3361 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3362 | if (ch == '"') |
| 3363 | break; |
| 3364 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3365 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3366 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3367 | } |
| 3368 | if (ch == EOF) |
| 3369 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3370 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3371 | if (ch == '`') { |
| 3372 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3373 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3374 | continue; |
| 3375 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3376 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3377 | } |
| 3378 | } |
| 3379 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 3380 | * \` quoting. |
| 3381 | * "Within the backquoted style of command substitution, backslash |
| 3382 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 3383 | * The search for the matching backquote shall be satisfied by the first |
| 3384 | * backquote found without a preceding backslash; during this search, |
| 3385 | * if a non-escaped backquote is encountered within a shell comment, |
| 3386 | * a here-document, an embedded command substitution of the $(command) |
| 3387 | * form, or a quoted string, undefined results occur. A single-quoted |
| 3388 | * or double-quoted string that begins, but does not end, within the |
| 3389 | * "`...`" sequence produces undefined results." |
| 3390 | * Example Output |
| 3391 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 3392 | */ |
| 3393 | static void add_till_backquote(o_string *dest, struct in_str *input) |
| 3394 | { |
| 3395 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3396 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3397 | if (ch == '`') |
| 3398 | break; |
| 3399 | if (ch == '\\') { /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3400 | int ch2 = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3401 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3402 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3403 | ch = ch2; |
| 3404 | } |
| 3405 | if (ch == EOF) |
| 3406 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3407 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3408 | } |
| 3409 | } |
| 3410 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 3411 | * quoting and nested ()s. |
| 3412 | * "With the $(command) style of command substitution, all characters |
| 3413 | * following the open parenthesis to the matching closing parenthesis |
| 3414 | * constitute the command. Any valid shell script can be used for command, |
| 3415 | * except a script consisting solely of redirections which produces |
| 3416 | * unspecified results." |
| 3417 | * Example Output |
| 3418 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 3419 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 3420 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 3421 | */ |
| 3422 | static void add_till_closing_curly_brace(o_string *dest, struct in_str *input) |
| 3423 | { |
| 3424 | int count = 0; |
| 3425 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3426 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3427 | if (ch == EOF) |
| 3428 | break; |
| 3429 | if (ch == '(') |
| 3430 | count++; |
| 3431 | if (ch == ')') |
| 3432 | if (--count < 0) |
| 3433 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3434 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3435 | if (ch == '\'') { |
| 3436 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3437 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3438 | continue; |
| 3439 | } |
| 3440 | if (ch == '"') { |
| 3441 | add_till_double_quote(dest, input); |
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 | continue; |
| 3444 | } |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3445 | if (ch == '\\') { /* \x. Copy verbatim. Important for \(, \) */ |
| 3446 | ch = i_getch(input); |
| 3447 | if (ch == EOF) |
| 3448 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3449 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3450 | continue; |
| 3451 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3452 | } |
| 3453 | } |
| 3454 | #endif /* ENABLE_HUSH_TICK */ |
| 3455 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3456 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3457 | static int handle_dollar(o_string *dest, struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3458 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3459 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3460 | unsigned char quote_mask = dest->o_quote ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3461 | |
| 3462 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3463 | if (isalpha(ch)) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3464 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3465 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3466 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3467 | i_getch(input); |
| 3468 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3469 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3470 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3471 | if (!isalnum(ch) && ch != '_') |
| 3472 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3473 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3474 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3475 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3476 | make_one_char_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3477 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3478 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3479 | i_getch(input); |
| 3480 | o_addchr(dest, ch | quote_mask); |
| 3481 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3482 | } else switch (ch) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3483 | case '$': /* pid */ |
| 3484 | case '!': /* last bg pid */ |
| 3485 | case '?': /* last exit code */ |
| 3486 | case '#': /* number of args */ |
| 3487 | case '*': /* args */ |
| 3488 | case '@': /* args */ |
| 3489 | goto make_one_char_var; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3490 | case '{': |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3491 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3492 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3493 | /* XXX maybe someone will try to escape the '}' */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3494 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3495 | ch = i_getch(input); |
Denis Vlasenko | b055001 | 2007-05-24 12:18:16 +0000 | [diff] [blame] | 3496 | if (ch == '}') |
| 3497 | break; |
| 3498 | if (!isalnum(ch) && ch != '_') { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3499 | syntax("unterminated ${name}"); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3500 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 3501 | return 1; |
| 3502 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3503 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3504 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3505 | quote_mask = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3506 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3507 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3508 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3509 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3510 | case '(': { |
| 3511 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3512 | i_getch(input); |
| 3513 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3514 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3515 | add_till_closing_curly_brace(dest, input); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3516 | //debug_printf_subst("SUBST RES2 '%s'\n", dest->data + pos); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3517 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3518 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3519 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3520 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3521 | case '-': |
| 3522 | case '_': |
| 3523 | /* still unhandled, but should be eventually */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3524 | bb_error_msg("unhandled syntax: $%c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3525 | return 1; |
| 3526 | break; |
| 3527 | default: |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3528 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3529 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3530 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3531 | return 0; |
| 3532 | } |
| 3533 | |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3534 | /* Scan input, call done_word() whenever full IFS delimited word was seen. |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 3535 | * Call done_pipe if '\n' was seen (and end_trigger != NULL). |
| 3536 | * Return code is 0 if end_trigger char is met, |
| 3537 | * -1 on EOF (but if end_trigger == NULL then return 0), |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3538 | * 1 for syntax error */ |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3539 | static int parse_stream(o_string *dest, struct p_context *ctx, |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3540 | struct in_str *input, const char *end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3541 | { |
"Vladimir N. Oleynik" | 4ccd2b4 | 2006-01-31 09:27:48 +0000 | [diff] [blame] | 3542 | int ch, m; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3543 | int redir_fd; |
| 3544 | redir_type redir_style; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3545 | int shadow_quote = dest->o_quote; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3546 | int next; |
| 3547 | |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3548 | /* 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] | 3549 | * 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] | 3550 | * found. When recursing, quote state is passed in via dest->o_quote. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3551 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3552 | debug_printf_parse("parse_stream entered, end_trigger='%s'\n", end_trigger); |
| 3553 | |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3554 | while (1) { |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3555 | m = CHAR_IFS; |
| 3556 | next = '\0'; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3557 | ch = i_getch(input); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3558 | if (ch != EOF) { |
| 3559 | m = charmap[ch]; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3560 | if (ch != '\n') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3561 | next = i_peek(input); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3562 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3563 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3564 | debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n", |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3565 | ch, ch, m, dest->o_quote); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3566 | if (m == CHAR_ORDINARY |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3567 | || (m != CHAR_SPECIAL && shadow_quote) |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3568 | ) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3569 | if (ch == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3570 | syntax("unterminated \""); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3571 | debug_printf_parse("parse_stream return 1: unterminated \"\n"); |
| 3572 | return 1; |
| 3573 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3574 | o_addQchr(dest, ch); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3575 | if (dest->o_assignment == MAYBE_ASSIGNMENT |
| 3576 | && ch == '=' |
| 3577 | && is_assignment(dest->data) |
| 3578 | ) { |
| 3579 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 3580 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3581 | continue; |
| 3582 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3583 | if (m == CHAR_IFS) { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3584 | if (done_word(dest, ctx)) { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3585 | debug_printf_parse("parse_stream return 1: done_word!=0\n"); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3586 | return 1; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3587 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3588 | if (ch == EOF) |
| 3589 | break; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3590 | /* If we aren't performing a substitution, treat |
| 3591 | * a newline as a command separator. |
| 3592 | * [why we don't handle it exactly like ';'? --vda] */ |
| 3593 | if (end_trigger && ch == '\n') { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3594 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3595 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3596 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3597 | if (end_trigger) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3598 | if (!shadow_quote && strchr(end_trigger, ch)) { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3599 | /* Special case: (...word) makes last word terminate, |
| 3600 | * as if ';' is seen */ |
| 3601 | if (ch == ')') { |
| 3602 | done_word(dest, ctx); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3603 | //err chk? |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3604 | done_pipe(ctx, PIPE_SEQ); |
| 3605 | } |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3606 | if (!HAS_KEYWORDS |
| 3607 | IF_HAS_KEYWORDS(|| (ctx->ctx_res_w == RES_NONE && ctx->old_flag == 0)) |
| 3608 | ) { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3609 | debug_printf_parse("parse_stream return 0: end_trigger char found\n"); |
| 3610 | return 0; |
| 3611 | } |
| 3612 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3613 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3614 | if (m == CHAR_IFS) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3615 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3616 | |
| 3617 | if (dest->o_assignment == MAYBE_ASSIGNMENT) { |
| 3618 | /* ch is a special char and thus this word |
| 3619 | * cannot be an assignment: */ |
| 3620 | dest->o_assignment = NOT_ASSIGNMENT; |
| 3621 | } |
| 3622 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3623 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3624 | case '#': |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3625 | if (dest->length == 0 && !shadow_quote) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3626 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3627 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3628 | if (ch == EOF || ch == '\n') |
| 3629 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3630 | i_getch(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3631 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3632 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3633 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3634 | } |
| 3635 | break; |
| 3636 | case '\\': |
| 3637 | if (next == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3638 | syntax("\\<eof>"); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3639 | debug_printf_parse("parse_stream return 1: \\<eof>\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3640 | return 1; |
| 3641 | } |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3642 | /* bash: |
| 3643 | * "The backslash retains its special meaning [in "..."] |
| 3644 | * only when followed by one of the following characters: |
| 3645 | * $, `, ", \, or <newline>. A double quote may be quoted |
| 3646 | * within double quotes by preceding it with a backslash. |
| 3647 | * If enabled, history expansion will be performed unless |
| 3648 | * an ! appearing in double quotes is escaped using |
| 3649 | * a backslash. The backslash preceding the ! is not removed." |
| 3650 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3651 | if (shadow_quote) { //NOT SURE dest->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3652 | if (strchr("$`\"\\", next) != NULL) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3653 | o_addqchr(dest, i_getch(input)); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3654 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3655 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3656 | } |
| 3657 | } else { |
| 3658 | o_addchr(dest, '\\'); |
| 3659 | o_addchr(dest, i_getch(input)); |
| 3660 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3661 | break; |
| 3662 | case '$': |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3663 | if (handle_dollar(dest, input) != 0) { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3664 | debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n"); |
| 3665 | return 1; |
| 3666 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3667 | break; |
| 3668 | case '\'': |
| 3669 | dest->nonnull = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3670 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3671 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3672 | if (ch == EOF) { |
| 3673 | syntax("unterminated '"); |
| 3674 | debug_printf_parse("parse_stream return 1: unterminated '\n"); |
| 3675 | return 1; |
| 3676 | } |
| 3677 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3678 | break; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3679 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 3680 | o_addqchr(dest, ch); |
| 3681 | else |
| 3682 | o_addchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3683 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3684 | break; |
| 3685 | case '"': |
| 3686 | dest->nonnull = 1; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3687 | shadow_quote ^= 1; /* invert */ |
| 3688 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 3689 | dest->o_quote ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3690 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3691 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3692 | case '`': { |
| 3693 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3694 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3695 | o_addchr(dest, shadow_quote /*or dest->o_quote??*/ ? 0x80 | '`' : '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3696 | add_till_backquote(dest, input); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3697 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3698 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3699 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3700 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3701 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3702 | case '>': |
| 3703 | redir_fd = redirect_opt_num(dest); |
| 3704 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3705 | redir_style = REDIRECT_OVERWRITE; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3706 | if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3707 | redir_style = REDIRECT_APPEND; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3708 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3709 | } |
| 3710 | #if 0 |
| 3711 | else if (next == '(') { |
| 3712 | syntax(">(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3713 | debug_printf_parse("parse_stream return 1: >(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3714 | return 1; |
| 3715 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3716 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3717 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 3718 | break; |
| 3719 | case '<': |
| 3720 | redir_fd = redirect_opt_num(dest); |
| 3721 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3722 | redir_style = REDIRECT_INPUT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3723 | if (next == '<') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3724 | redir_style = REDIRECT_HEREIS; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3725 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3726 | } else if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3727 | redir_style = REDIRECT_IO; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3728 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3729 | } |
| 3730 | #if 0 |
| 3731 | else if (next == '(') { |
| 3732 | syntax("<(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3733 | debug_printf_parse("parse_stream return 1: <(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3734 | return 1; |
| 3735 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3736 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3737 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 3738 | break; |
| 3739 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 3740 | #if ENABLE_HUSH_CASE |
| 3741 | case_semi: |
| 3742 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3743 | done_word(dest, ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3744 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 3745 | #if ENABLE_HUSH_CASE |
| 3746 | /* Eat multiple semicolons, detect |
| 3747 | * whether it means something special */ |
| 3748 | while (1) { |
| 3749 | ch = i_peek(input); |
| 3750 | if (ch != ';') |
| 3751 | break; |
| 3752 | i_getch(input); |
| 3753 | if (ctx->ctx_res_w == RES_CASEI) { |
| 3754 | ctx->ctx_dsemicolon = 1; |
| 3755 | ctx->ctx_res_w = RES_MATCH; |
| 3756 | break; |
| 3757 | } |
| 3758 | } |
| 3759 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3760 | break; |
| 3761 | case '&': |
| 3762 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3763 | if (next == '&') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3764 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3765 | done_pipe(ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3766 | } else { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3767 | done_pipe(ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3768 | } |
| 3769 | break; |
| 3770 | case '|': |
| 3771 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3772 | if (next == '|') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3773 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3774 | done_pipe(ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3775 | } else { |
| 3776 | /* we could pick up a file descriptor choice here |
| 3777 | * with redirect_opt_num(), but bash doesn't do it. |
| 3778 | * "echo foo 2| cat" yields "foo 2". */ |
| 3779 | done_command(ctx); |
| 3780 | } |
| 3781 | break; |
| 3782 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 3783 | #if ENABLE_HUSH_CASE |
| 3784 | if (dest->length == 0 // && argv[0] == NULL |
| 3785 | && ctx->ctx_res_w == RES_MATCH |
| 3786 | ) { |
| 3787 | continue; |
| 3788 | } |
| 3789 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3790 | case '{': |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3791 | if (parse_group(dest, ctx, input, ch) != 0) { |
| 3792 | 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] | 3793 | return 1; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3794 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3795 | break; |
| 3796 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame^] | 3797 | #if ENABLE_HUSH_CASE |
| 3798 | if (ctx->ctx_res_w == RES_MATCH) |
| 3799 | goto case_semi; |
| 3800 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3801 | case '}': |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3802 | /* proper use of this character is caught by end_trigger */ |
| 3803 | syntax("unexpected } or )"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3804 | debug_printf_parse("parse_stream return 1: unexpected '}'\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3805 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3806 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3807 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3808 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3809 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3810 | } /* while (1) */ |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3811 | debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL)); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3812 | if (end_trigger) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3813 | return -1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3814 | return 0; |
| 3815 | } |
| 3816 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3817 | static void set_in_charmap(const char *set, int code) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3818 | { |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3819 | while (*set) |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3820 | charmap[(unsigned char)*set++] = code; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3823 | static void update_charmap(void) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3824 | { |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3825 | /* char *ifs and char charmap[256] are both globals. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3826 | ifs = getenv("IFS"); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3827 | if (ifs == NULL) |
| 3828 | ifs = " \t\n"; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3829 | /* Precompute a list of 'flow through' behavior so it can be treated |
| 3830 | * quickly up front. Computation is necessary because of IFS. |
| 3831 | * Special case handling of IFS == " \t\n" is not implemented. |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3832 | * The charmap[] array only really needs two bits each, |
| 3833 | * and on most machines that would be faster (reduced L1 cache use). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3834 | */ |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3835 | memset(charmap, CHAR_ORDINARY, sizeof(charmap)); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3836 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3837 | set_in_charmap("\\$\"`", CHAR_SPECIAL); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3838 | #else |
| 3839 | set_in_charmap("\\$\"", CHAR_SPECIAL); |
| 3840 | #endif |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3841 | set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3842 | set_in_charmap(ifs, CHAR_IFS); /* are ordinary if quoted */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3845 | /* Most recursion does not come through here, the exception is |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3846 | * from builtin_source() and builtin_eval() */ |
| 3847 | 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] | 3848 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3849 | struct p_context ctx; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3850 | o_string temp = NULL_O_STRING; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3851 | int rcode; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3852 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3853 | do { |
| 3854 | initialize_context(&ctx); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3855 | update_charmap(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3856 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 3857 | inp->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3858 | #endif |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3859 | /* We will stop & execute after each ';' or '\n'. |
| 3860 | * Example: "sleep 9999; echo TEST" + ctrl-C: |
| 3861 | * TEST should be printed */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3862 | rcode = parse_stream(&temp, &ctx, inp, ";\n"); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3863 | #if HAS_KEYWORDS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3864 | if (rcode != 1 && ctx.old_flag != 0) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3865 | syntax(NULL); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3866 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3867 | #endif |
| 3868 | if (rcode != 1 IF_HAS_KEYWORDS(&& ctx.old_flag == 0)) { |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3869 | done_word(&temp, &ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3870 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3871 | debug_print_tree(ctx.list_head, 0); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3872 | debug_printf_exec("parse_stream_outer: run_and_free_list\n"); |
| 3873 | run_and_free_list(ctx.list_head); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3874 | } else { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3875 | /* We arrive here also if rcode == 1 (error in parse_stream) */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3876 | #if HAS_KEYWORDS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3877 | if (ctx.old_flag != 0) { |
| 3878 | free(ctx.stack); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3879 | o_reset(&temp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3880 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3881 | #endif |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3882 | /*temp.nonnull = 0; - o_free does it below */ |
| 3883 | /*temp.o_quote = 0; - o_free does it below */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3884 | free_pipe_list(ctx.list_head, /* indent: */ 0); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3885 | /* Discard all unprocessed line input, force prompt on */ |
| 3886 | inp->p = NULL; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3887 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3888 | inp->promptme = 1; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3889 | #endif |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3890 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3891 | o_free(&temp); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3892 | /* loop on syntax errors, return on EOF: */ |
| 3893 | } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3894 | return 0; |
| 3895 | } |
| 3896 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3897 | static int parse_and_run_string(const char *s, int parse_flag) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3898 | { |
| 3899 | struct in_str input; |
| 3900 | setup_string_in_str(&input, s); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3901 | return parse_and_run_stream(&input, parse_flag); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3902 | } |
| 3903 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3904 | static int parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3905 | { |
| 3906 | int rcode; |
| 3907 | struct in_str input; |
| 3908 | setup_file_in_str(&input, f); |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3909 | rcode = parse_and_run_stream(&input, 0 /* parse_flag */); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3910 | return rcode; |
| 3911 | } |
| 3912 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3913 | #if ENABLE_HUSH_JOB |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 3914 | /* Make sure we have a controlling tty. If we get started under a job |
| 3915 | * aware app (like bash for example), make sure we are now in charge so |
| 3916 | * we don't fight over who gets the foreground */ |
Eric Andersen | eaecbf3 | 2001-10-31 10:41:31 +0000 | [diff] [blame] | 3917 | static void setup_job_control(void) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3918 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3919 | pid_t shell_pgrp; |
| 3920 | |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 3921 | saved_task_pgrp = shell_pgrp = getpgrp(); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3922 | debug_printf_jobs("saved_task_pgrp=%d\n", saved_task_pgrp); |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 3923 | close_on_exec_on(interactive_fd); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3924 | |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 3925 | /* If we were ran as 'hush &', |
| 3926 | * sleep until we are in the foreground. */ |
| 3927 | while (tcgetpgrp(interactive_fd) != shell_pgrp) { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3928 | /* Send TTIN to ourself (should stop us) */ |
Denis Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 3929 | kill(- shell_pgrp, SIGTTIN); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 3930 | shell_pgrp = getpgrp(); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3931 | } |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3932 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3933 | /* Ignore job-control and misc signals. */ |
| 3934 | set_jobctrl_sighandler(SIG_IGN); |
| 3935 | set_misc_sighandler(SIG_IGN); |
| 3936 | //huh? signal(SIGCHLD, SIG_IGN); |
| 3937 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3938 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3939 | set_fatal_sighandler(sigexit); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 3940 | |
| 3941 | /* Put ourselves in our own process group. */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3942 | setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 3943 | /* Grab control of the terminal. */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3944 | tcsetpgrp(interactive_fd, getpid()); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 3945 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3946 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 3947 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3948 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 3949 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 3950 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3951 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 3952 | static const char version_str[] ALIGN1 = "HUSH_VERSION="HUSH_VER_STR; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 3953 | static const struct variable const_shell_ver = { |
| 3954 | .next = NULL, |
| 3955 | .varstr = (char*)version_str, |
| 3956 | .max_len = 1, /* 0 can provoke free(name) */ |
| 3957 | .flg_export = 1, |
| 3958 | .flg_read_only = 1, |
| 3959 | }; |
| 3960 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3961 | int opt; |
| 3962 | FILE *input; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3963 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3964 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 3965 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 3966 | INIT_G(); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3967 | |
Denis Vlasenko | 16c2fea | 2008-06-17 12:28:44 +0000 | [diff] [blame] | 3968 | root_pid = getpid(); |
| 3969 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 3970 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 3971 | shell_ver = const_shell_ver; /* copying struct here */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3972 | top_var = &shell_ver; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 3973 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 3974 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3975 | * currently living in the environment */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3976 | cur_var = top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 3977 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3978 | if (e) while (*e) { |
| 3979 | char *value = strchr(*e, '='); |
| 3980 | if (value) { /* paranoia */ |
| 3981 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 3982 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 3983 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3984 | cur_var->max_len = strlen(*e); |
| 3985 | cur_var->flg_export = 1; |
| 3986 | } |
| 3987 | e++; |
| 3988 | } |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 3989 | putenv((char *)version_str); /* reinstate HUSH_VERSION */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 3990 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 3991 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 3992 | line_input_state = new_line_input_t(FOR_SHELL); |
| 3993 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3994 | /* XXX what should these be while sourcing /etc/profile? */ |
| 3995 | global_argc = argc; |
| 3996 | global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3997 | /* Initialize some more globals to non-zero values */ |
| 3998 | set_cwd(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3999 | #if ENABLE_HUSH_INTERACTIVE |
| 4000 | #if ENABLE_FEATURE_EDITING |
| 4001 | cmdedit_set_initial_prompt(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4002 | #endif |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 4003 | PS2 = "> "; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4004 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 4005 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4006 | if (EXIT_SUCCESS) /* otherwise is already done */ |
| 4007 | last_return_code = EXIT_SUCCESS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4008 | |
| 4009 | if (argv[0] && argv[0][0] == '-') { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4010 | debug_printf("sourcing /etc/profile\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4011 | input = fopen("/etc/profile", "r"); |
| 4012 | if (input != NULL) { |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 4013 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4014 | parse_and_run_file(input); |
Eric Andersen | a90f20b | 2001-06-26 23:00:21 +0000 | [diff] [blame] | 4015 | fclose(input); |
| 4016 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4017 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4018 | input = stdin; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4019 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4020 | while ((opt = getopt(argc, argv, "c:xif")) > 0) { |
| 4021 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4022 | case 'c': |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 4023 | global_argv = argv + optind; |
| 4024 | global_argc = argc - optind; |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4025 | opt = parse_and_run_string(optarg, 0 /* parse_flag */); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4026 | goto final_return; |
| 4027 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4028 | /* Well, we cannot just declare interactiveness, |
| 4029 | * we have to have some stuff (ctty, etc) */ |
| 4030 | /* interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4031 | break; |
| 4032 | case 'f': |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 4033 | fake_mode = 1; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4034 | break; |
| 4035 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4036 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4037 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 4038 | " or: sh -c command [args]...\n\n"); |
| 4039 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4040 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4041 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4042 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4043 | } |
| 4044 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4045 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4046 | /* 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] | 4047 | * the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 4048 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4049 | * no arguments remaining or the -s flag given |
| 4050 | * standard input is a terminal |
| 4051 | * standard output is a terminal |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4052 | * Refer to Posix.2, the description of the 'sh' utility. */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4053 | if (argv[optind] == NULL && input == stdin |
| 4054 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 4055 | ) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4056 | saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 4057 | debug_printf("saved_tty_pgrp=%d\n", saved_tty_pgrp); |
| 4058 | if (saved_tty_pgrp >= 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4059 | /* try to dup to high fd#, >= 255 */ |
| 4060 | interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 4061 | if (interactive_fd < 0) { |
| 4062 | /* try to dup to any fd */ |
| 4063 | interactive_fd = dup(STDIN_FILENO); |
| 4064 | if (interactive_fd < 0) |
| 4065 | /* give up */ |
| 4066 | interactive_fd = 0; |
| 4067 | } |
Denis Vlasenko | 2f1bb36 | 2007-04-21 10:01:14 +0000 | [diff] [blame] | 4068 | // TODO: track & disallow any attempts of user |
| 4069 | // to (inadvertently) close/redirect it |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4070 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4071 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4072 | debug_printf("interactive_fd=%d\n", interactive_fd); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4073 | if (interactive_fd) { |
Denis Vlasenko | 08126f6 | 2008-02-11 08:39:11 +0000 | [diff] [blame] | 4074 | fcntl(interactive_fd, F_SETFD, FD_CLOEXEC); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4075 | /* Looks like they want an interactive shell */ |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4076 | setup_job_control(); |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 4077 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 4078 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
| 4079 | die_sleep = -1; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4080 | if (setjmp(die_jmp)) { |
| 4081 | /* xfunc has failed! die die die */ |
| 4082 | hush_exit(xfunc_error_retval); |
| 4083 | } |
Denis Vlasenko | 2f1bb36 | 2007-04-21 10:01:14 +0000 | [diff] [blame] | 4084 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 4085 | 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] | 4086 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
| 4087 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4088 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4089 | #elif ENABLE_HUSH_INTERACTIVE |
| 4090 | /* no job control compiled, only prompt/line editing */ |
| 4091 | if (argv[optind] == NULL && input == stdin |
| 4092 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 4093 | ) { |
| 4094 | interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 4095 | if (interactive_fd < 0) { |
| 4096 | /* try to dup to any fd */ |
| 4097 | interactive_fd = dup(STDIN_FILENO); |
| 4098 | if (interactive_fd < 0) |
| 4099 | /* give up */ |
| 4100 | interactive_fd = 0; |
| 4101 | } |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 4102 | if (interactive_fd) { |
Denis Vlasenko | 08126f6 | 2008-02-11 08:39:11 +0000 | [diff] [blame] | 4103 | fcntl(interactive_fd, F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 4104 | set_misc_sighandler(SIG_IGN); |
| 4105 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4106 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4107 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4108 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4109 | if (argv[optind] == NULL) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4110 | opt = parse_and_run_file(stdin); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4111 | } else { |
| 4112 | debug_printf("\nrunning script '%s'\n", argv[optind]); |
| 4113 | global_argv = argv + optind; |
| 4114 | global_argc = argc - optind; |
| 4115 | input = xfopen(argv[optind], "r"); |
Denis Vlasenko | 459a5ad | 2008-02-11 08:35:03 +0000 | [diff] [blame] | 4116 | fcntl(fileno(input), F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4117 | opt = parse_and_run_file(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4118 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4119 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4120 | final_return: |
| 4121 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4122 | #if ENABLE_FEATURE_CLEAN_UP |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4123 | fclose(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4124 | if (cwd != bb_msg_unknown) |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4125 | free((char*)cwd); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4126 | cur_var = top_var->next; |
| 4127 | while (cur_var) { |
| 4128 | struct variable *tmp = cur_var; |
| 4129 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 4130 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4131 | cur_var = cur_var->next; |
| 4132 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4133 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4134 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4135 | hush_exit(opt ? opt : last_return_code); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4136 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 4137 | |
| 4138 | |
| 4139 | #if ENABLE_LASH |
| 4140 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 4141 | int lash_main(int argc, char **argv) |
| 4142 | { |
| 4143 | //bb_error_msg("lash is deprecated, please use hush instead"); |
| 4144 | return hush_main(argc, argv); |
| 4145 | } |
| 4146 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4147 | |
| 4148 | |
| 4149 | /* |
| 4150 | * Built-ins |
| 4151 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4152 | static int builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4153 | { |
| 4154 | return 0; |
| 4155 | } |
| 4156 | |
| 4157 | static int builtin_test(char **argv) |
| 4158 | { |
| 4159 | int argc = 0; |
| 4160 | while (*argv) { |
| 4161 | argc++; |
| 4162 | argv++; |
| 4163 | } |
| 4164 | return test_main(argc, argv - argc); |
| 4165 | } |
| 4166 | |
| 4167 | static int builtin_echo(char **argv) |
| 4168 | { |
| 4169 | int argc = 0; |
| 4170 | while (*argv) { |
| 4171 | argc++; |
| 4172 | argv++; |
| 4173 | } |
| 4174 | return echo_main(argc, argv - argc); |
| 4175 | } |
| 4176 | |
| 4177 | static int builtin_eval(char **argv) |
| 4178 | { |
| 4179 | int rcode = EXIT_SUCCESS; |
| 4180 | |
| 4181 | if (argv[1]) { |
| 4182 | char *str = expand_strvec_to_string(argv + 1); |
| 4183 | parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP); |
| 4184 | free(str); |
| 4185 | rcode = last_return_code; |
| 4186 | } |
| 4187 | return rcode; |
| 4188 | } |
| 4189 | |
| 4190 | static int builtin_cd(char **argv) |
| 4191 | { |
| 4192 | const char *newdir; |
| 4193 | if (argv[1] == NULL) { |
| 4194 | // bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
| 4195 | // bash says "bash: cd: HOME not set" and does nothing (exitcode 1) |
| 4196 | newdir = getenv("HOME") ? : "/"; |
| 4197 | } else |
| 4198 | newdir = argv[1]; |
| 4199 | if (chdir(newdir)) { |
| 4200 | printf("cd: %s: %s\n", newdir, strerror(errno)); |
| 4201 | return EXIT_FAILURE; |
| 4202 | } |
| 4203 | set_cwd(); |
| 4204 | return EXIT_SUCCESS; |
| 4205 | } |
| 4206 | |
| 4207 | static int builtin_exec(char **argv) |
| 4208 | { |
| 4209 | if (argv[1] == NULL) |
| 4210 | return EXIT_SUCCESS; /* bash does this */ |
| 4211 | { |
| 4212 | #if !BB_MMU |
| 4213 | char **ptrs2free = alloc_ptrs(argv); |
| 4214 | #endif |
| 4215 | // FIXME: if exec fails, bash does NOT exit! We do... |
| 4216 | pseudo_exec_argv(ptrs2free, argv + 1); |
| 4217 | /* never returns */ |
| 4218 | } |
| 4219 | } |
| 4220 | |
| 4221 | static int builtin_exit(char **argv) |
| 4222 | { |
| 4223 | // TODO: bash does it ONLY on top-level sh exit (+interacive only?) |
| 4224 | //puts("exit"); /* bash does it */ |
| 4225 | // TODO: warn if we have background jobs: "There are stopped jobs" |
| 4226 | // On second consecutive 'exit', exit anyway. |
| 4227 | if (argv[1] == NULL) |
| 4228 | hush_exit(last_return_code); |
| 4229 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 4230 | xfunc_error_retval = 255; |
| 4231 | /* bash: exit -2 == exit 254, no error msg */ |
| 4232 | hush_exit(xatoi(argv[1]) & 0xff); |
| 4233 | } |
| 4234 | |
| 4235 | static int builtin_export(char **argv) |
| 4236 | { |
| 4237 | const char *value; |
| 4238 | char *name = argv[1]; |
| 4239 | |
| 4240 | if (name == NULL) { |
| 4241 | // TODO: |
| 4242 | // ash emits: export VAR='VAL' |
| 4243 | // bash: declare -x VAR="VAL" |
| 4244 | // (both also escape as needed (quotes, $, etc)) |
| 4245 | char **e = environ; |
| 4246 | if (e) |
| 4247 | while (*e) |
| 4248 | puts(*e++); |
| 4249 | return EXIT_SUCCESS; |
| 4250 | } |
| 4251 | |
| 4252 | value = strchr(name, '='); |
| 4253 | if (!value) { |
| 4254 | /* They are exporting something without a =VALUE */ |
| 4255 | struct variable *var; |
| 4256 | |
| 4257 | var = get_local_var(name); |
| 4258 | if (var) { |
| 4259 | var->flg_export = 1; |
| 4260 | putenv(var->varstr); |
| 4261 | } |
| 4262 | /* bash does not return an error when trying to export |
| 4263 | * an undefined variable. Do likewise. */ |
| 4264 | return EXIT_SUCCESS; |
| 4265 | } |
| 4266 | |
| 4267 | set_local_var(xstrdup(name), 1); |
| 4268 | return EXIT_SUCCESS; |
| 4269 | } |
| 4270 | |
| 4271 | #if ENABLE_HUSH_JOB |
| 4272 | /* built-in 'fg' and 'bg' handler */ |
| 4273 | static int builtin_fg_bg(char **argv) |
| 4274 | { |
| 4275 | int i, jobnum; |
| 4276 | struct pipe *pi; |
| 4277 | |
| 4278 | if (!interactive_fd) |
| 4279 | return EXIT_FAILURE; |
| 4280 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 4281 | if (!argv[1]) { |
| 4282 | for (pi = job_list; pi; pi = pi->next) { |
| 4283 | if (pi->jobid == last_jobid) { |
| 4284 | goto found; |
| 4285 | } |
| 4286 | } |
| 4287 | bb_error_msg("%s: no current job", argv[0]); |
| 4288 | return EXIT_FAILURE; |
| 4289 | } |
| 4290 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 4291 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 4292 | return EXIT_FAILURE; |
| 4293 | } |
| 4294 | for (pi = job_list; pi; pi = pi->next) { |
| 4295 | if (pi->jobid == jobnum) { |
| 4296 | goto found; |
| 4297 | } |
| 4298 | } |
| 4299 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 4300 | return EXIT_FAILURE; |
| 4301 | found: |
| 4302 | // TODO: bash prints a string representation |
| 4303 | // of job being foregrounded (like "sleep 1 | cat") |
| 4304 | if (*argv[0] == 'f') { |
| 4305 | /* Put the job into the foreground. */ |
| 4306 | tcsetpgrp(interactive_fd, pi->pgrp); |
| 4307 | } |
| 4308 | |
| 4309 | /* Restart the processes in the job */ |
| 4310 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_progs, pi->pgrp); |
| 4311 | for (i = 0; i < pi->num_progs; i++) { |
| 4312 | debug_printf_jobs("reviving pid %d\n", pi->progs[i].pid); |
| 4313 | pi->progs[i].is_stopped = 0; |
| 4314 | } |
| 4315 | pi->stopped_progs = 0; |
| 4316 | |
| 4317 | i = kill(- pi->pgrp, SIGCONT); |
| 4318 | if (i < 0) { |
| 4319 | if (errno == ESRCH) { |
| 4320 | delete_finished_bg_job(pi); |
| 4321 | return EXIT_SUCCESS; |
| 4322 | } else { |
| 4323 | bb_perror_msg("kill (SIGCONT)"); |
| 4324 | } |
| 4325 | } |
| 4326 | |
| 4327 | if (*argv[0] == 'f') { |
| 4328 | remove_bg_job(pi); |
| 4329 | return checkjobs_and_fg_shell(pi); |
| 4330 | } |
| 4331 | return EXIT_SUCCESS; |
| 4332 | } |
| 4333 | #endif |
| 4334 | |
| 4335 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4336 | static int builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4337 | { |
| 4338 | const struct built_in_command *x; |
| 4339 | |
| 4340 | printf("\nBuilt-in commands:\n"); |
| 4341 | printf("-------------------\n"); |
| 4342 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 4343 | printf("%s\t%s\n", x->cmd, x->descr); |
| 4344 | } |
| 4345 | printf("\n\n"); |
| 4346 | return EXIT_SUCCESS; |
| 4347 | } |
| 4348 | #endif |
| 4349 | |
| 4350 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4351 | static int builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4352 | { |
| 4353 | struct pipe *job; |
| 4354 | const char *status_string; |
| 4355 | |
| 4356 | for (job = job_list; job; job = job->next) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4357 | if (job->alive_progs == job->stopped_progs) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4358 | status_string = "Stopped"; |
| 4359 | else |
| 4360 | status_string = "Running"; |
| 4361 | |
| 4362 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 4363 | } |
| 4364 | return EXIT_SUCCESS; |
| 4365 | } |
| 4366 | #endif |
| 4367 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4368 | static int builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4369 | { |
| 4370 | puts(set_cwd()); |
| 4371 | return EXIT_SUCCESS; |
| 4372 | } |
| 4373 | |
| 4374 | static int builtin_read(char **argv) |
| 4375 | { |
| 4376 | char *string; |
| 4377 | const char *name = argv[1] ? argv[1] : "REPLY"; |
| 4378 | |
| 4379 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
| 4380 | return set_local_var(string, 0); |
| 4381 | } |
| 4382 | |
| 4383 | /* built-in 'set [VAR=value]' handler */ |
| 4384 | static int builtin_set(char **argv) |
| 4385 | { |
| 4386 | char *temp = argv[1]; |
| 4387 | struct variable *e; |
| 4388 | |
| 4389 | if (temp == NULL) |
| 4390 | for (e = top_var; e; e = e->next) |
| 4391 | puts(e->varstr); |
| 4392 | else |
| 4393 | set_local_var(xstrdup(temp), 0); |
| 4394 | |
| 4395 | return EXIT_SUCCESS; |
| 4396 | } |
| 4397 | |
| 4398 | static int builtin_shift(char **argv) |
| 4399 | { |
| 4400 | int n = 1; |
| 4401 | if (argv[1]) { |
| 4402 | n = atoi(argv[1]); |
| 4403 | } |
| 4404 | if (n >= 0 && n < global_argc) { |
| 4405 | global_argv[n] = global_argv[0]; |
| 4406 | global_argc -= n; |
| 4407 | global_argv += n; |
| 4408 | return EXIT_SUCCESS; |
| 4409 | } |
| 4410 | return EXIT_FAILURE; |
| 4411 | } |
| 4412 | |
| 4413 | static int builtin_source(char **argv) |
| 4414 | { |
| 4415 | FILE *input; |
| 4416 | int status; |
| 4417 | |
| 4418 | if (argv[1] == NULL) |
| 4419 | return EXIT_FAILURE; |
| 4420 | |
| 4421 | /* XXX search through $PATH is missing */ |
| 4422 | input = fopen(argv[1], "r"); |
| 4423 | if (!input) { |
| 4424 | bb_error_msg("cannot open '%s'", argv[1]); |
| 4425 | return EXIT_FAILURE; |
| 4426 | } |
| 4427 | close_on_exec_on(fileno(input)); |
| 4428 | |
| 4429 | /* Now run the file */ |
| 4430 | /* XXX argv and argc are broken; need to save old global_argv |
| 4431 | * (pointer only is OK!) on this stack frame, |
| 4432 | * set global_argv=argv+1, recurse, and restore. */ |
| 4433 | status = parse_and_run_file(input); |
| 4434 | fclose(input); |
| 4435 | return status; |
| 4436 | } |
| 4437 | |
| 4438 | static int builtin_umask(char **argv) |
| 4439 | { |
| 4440 | mode_t new_umask; |
| 4441 | const char *arg = argv[1]; |
| 4442 | char *end; |
| 4443 | if (arg) { |
| 4444 | new_umask = strtoul(arg, &end, 8); |
| 4445 | if (*end != '\0' || end == arg) { |
| 4446 | return EXIT_FAILURE; |
| 4447 | } |
| 4448 | } else { |
| 4449 | new_umask = umask(0); |
| 4450 | printf("%.3o\n", (unsigned) new_umask); |
| 4451 | } |
| 4452 | umask(new_umask); |
| 4453 | return EXIT_SUCCESS; |
| 4454 | } |
| 4455 | |
| 4456 | static int builtin_unset(char **argv) |
| 4457 | { |
| 4458 | /* bash always returns true */ |
| 4459 | unset_local_var(argv[1]); |
| 4460 | return EXIT_SUCCESS; |
| 4461 | } |