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 | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 76 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 77 | #if !BB_MMU && ENABLE_HUSH_TICK |
| 78 | //#undef ENABLE_HUSH_TICK |
| 79 | //#define ENABLE_HUSH_TICK 0 |
| 80 | #warning On NOMMU, hush command substitution is dangerous. |
| 81 | #warning Dont use it for commands which produce lots of output. |
| 82 | #warning For more info see shell/hush.c, generate_stream_from_list(). |
| 83 | #endif |
| 84 | |
| 85 | #if !BB_MMU && ENABLE_HUSH_JOB |
| 86 | #undef ENABLE_HUSH_JOB |
| 87 | #define ENABLE_HUSH_JOB 0 |
| 88 | #endif |
| 89 | |
| 90 | #if !ENABLE_HUSH_INTERACTIVE |
| 91 | #undef ENABLE_FEATURE_EDITING |
| 92 | #define ENABLE_FEATURE_EDITING 0 |
| 93 | #undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 94 | #define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 95 | #endif |
| 96 | |
| 97 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 98 | /* If you comment out one of these below, it will be #defined later |
| 99 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 100 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 101 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 102 | #define debug_printf_parse(...) do {} while (0) |
| 103 | #define debug_print_tree(a, b) do {} while (0) |
| 104 | #define debug_printf_exec(...) do {} while (0) |
| 105 | #define debug_printf_jobs(...) do {} while (0) |
| 106 | #define debug_printf_expand(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 107 | #define debug_printf_glob(...) do {} while (0) |
| 108 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 109 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 110 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 111 | |
| 112 | #ifndef debug_printf |
| 113 | #define debug_printf(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 114 | #endif |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 115 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 116 | #ifndef debug_printf_parse |
| 117 | #define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 118 | #endif |
| 119 | |
| 120 | #ifndef debug_printf_exec |
| 121 | #define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__) |
| 122 | #endif |
| 123 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 124 | #ifndef debug_printf_jobs |
| 125 | #define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 126 | #define DEBUG_JOBS 1 |
| 127 | #else |
| 128 | #define DEBUG_JOBS 0 |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 129 | #endif |
| 130 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 131 | #ifndef debug_printf_expand |
| 132 | #define debug_printf_expand(...) fprintf(stderr, __VA_ARGS__) |
| 133 | #define DEBUG_EXPAND 1 |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 134 | #else |
| 135 | #define DEBUG_EXPAND 0 |
| 136 | #endif |
| 137 | |
| 138 | #ifndef debug_printf_glob |
| 139 | #define debug_printf_glob(...) fprintf(stderr, __VA_ARGS__) |
| 140 | #define DEBUG_GLOB 1 |
| 141 | #else |
| 142 | #define DEBUG_GLOB 0 |
| 143 | #endif |
| 144 | |
| 145 | #ifndef debug_printf_list |
| 146 | #define debug_printf_list(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 147 | #endif |
| 148 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 149 | #ifndef debug_printf_subst |
| 150 | #define debug_printf_subst(...) fprintf(stderr, __VA_ARGS__) |
| 151 | #endif |
| 152 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 153 | /* Keep unconditionally on for now */ |
| 154 | #define ENABLE_HUSH_DEBUG 1 |
| 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 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 179 | |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 180 | /* |
| 181 | * Leak hunting. Use hush_leaktool.sh for post-processing. |
| 182 | */ |
| 183 | #ifdef FOR_HUSH_LEAKTOOL |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 184 | /* suppress "warning: no previous prototype..." */ |
| 185 | void *xxmalloc(int lineno, size_t size); |
| 186 | void *xxrealloc(int lineno, void *ptr, size_t size); |
| 187 | char *xxstrdup(int lineno, const char *str); |
| 188 | void xxfree(void *ptr); |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 189 | void *xxmalloc(int lineno, size_t size) |
| 190 | { |
| 191 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 192 | fprintf(stderr, "line %d: malloc %p\n", lineno, ptr); |
| 193 | return ptr; |
| 194 | } |
| 195 | void *xxrealloc(int lineno, void *ptr, size_t size) |
| 196 | { |
| 197 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 198 | fprintf(stderr, "line %d: realloc %p\n", lineno, ptr); |
| 199 | return ptr; |
| 200 | } |
| 201 | char *xxstrdup(int lineno, const char *str) |
| 202 | { |
| 203 | char *ptr = xstrdup(str); |
| 204 | fprintf(stderr, "line %d: strdup %p\n", lineno, ptr); |
| 205 | return ptr; |
| 206 | } |
| 207 | void xxfree(void *ptr) |
| 208 | { |
| 209 | fprintf(stderr, "free %p\n", ptr); |
| 210 | free(ptr); |
| 211 | } |
| 212 | #define xmalloc(s) xxmalloc(__LINE__, s) |
| 213 | #define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 214 | #define xstrdup(s) xxstrdup(__LINE__, s) |
| 215 | #define free(p) xxfree(p) |
| 216 | #endif |
| 217 | |
| 218 | |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 219 | #define SPECIAL_VAR_SYMBOL 3 |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 220 | #define PARSEFLAG_EXIT_FROM_LOOP 1 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 221 | |
| 222 | typedef enum { |
| 223 | REDIRECT_INPUT = 1, |
| 224 | REDIRECT_OVERWRITE = 2, |
| 225 | REDIRECT_APPEND = 3, |
| 226 | REDIRECT_HEREIS = 4, |
| 227 | REDIRECT_IO = 5 |
| 228 | } redir_type; |
| 229 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 230 | /* The descrip member of this structure is only used to make |
| 231 | * debugging output pretty */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 232 | static const struct { |
| 233 | int mode; |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 234 | signed char default_fd; |
| 235 | char descrip[3]; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 236 | } redir_table[] = { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 237 | { 0, 0, "()" }, |
| 238 | { O_RDONLY, 0, "<" }, |
| 239 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 240 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 241 | { O_RDONLY, -1, "<<" }, |
| 242 | { O_RDWR, 1, "<>" } |
| 243 | }; |
| 244 | |
| 245 | typedef enum { |
| 246 | PIPE_SEQ = 1, |
| 247 | PIPE_AND = 2, |
| 248 | PIPE_OR = 3, |
| 249 | PIPE_BG = 4, |
| 250 | } pipe_style; |
| 251 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 252 | typedef enum { |
| 253 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 254 | #if ENABLE_HUSH_IF |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 255 | RES_IF = 1, |
| 256 | RES_THEN = 2, |
| 257 | RES_ELIF = 3, |
| 258 | RES_ELSE = 4, |
| 259 | RES_FI = 5, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 260 | #endif |
| 261 | #if ENABLE_HUSH_LOOPS |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 262 | RES_FOR = 6, |
| 263 | RES_WHILE = 7, |
| 264 | RES_UNTIL = 8, |
| 265 | RES_DO = 9, |
| 266 | RES_DONE = 10, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 267 | RES_IN = 11, |
| 268 | #endif |
| 269 | RES_XXXX = 12, |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 270 | RES_SNTX = 13 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 271 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 272 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 273 | /* This holds pointers to the various results of parsing */ |
| 274 | struct p_context { |
| 275 | struct child_prog *child; |
| 276 | struct pipe *list_head; |
| 277 | struct pipe *pipe; |
| 278 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 279 | smallint res_w; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 280 | smallint ctx_inverted; /* "! cmd | cmd" */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 281 | 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] | 282 | struct p_context *stack; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 286 | struct redir_struct *next; |
| 287 | smallint /*redir_type*/ rd_type; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 288 | int fd; /* file descriptor being redirected */ |
| 289 | int dup; /* -1, or file descriptor being duplicated */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 290 | char *rd_filename; /* filename */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | struct child_prog { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 294 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 295 | smallint is_stopped; /* is the program currently running? */ |
| 296 | smallint subshell; /* flag, non-zero if group must be forked */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 297 | char **argv; /* program name and arguments */ |
| 298 | struct pipe *group; /* if non-NULL, first in group or subshell */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 299 | struct redir_struct *redirects; /* I/O redirections */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 300 | struct pipe *family; /* pointer back to the child's parent pipe */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 301 | }; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 302 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 303 | * and on execution these are substituted with their values. |
| 304 | * Substitution can make _several_ words out of one argv[n]! |
| 305 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 306 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 307 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 308 | |
| 309 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 310 | struct pipe *next; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 311 | int num_progs; /* total number of programs in job */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 312 | int running_progs; /* number of programs running (not exited) */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 313 | int stopped_progs; /* number of programs alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 314 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 315 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 316 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 317 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 318 | #endif |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 319 | char *cmdbuf; /* buffer various argv's point into */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 320 | struct child_prog *progs; /* array of commands in pipe */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 321 | int job_context; /* bitmask defining current context */ |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 322 | smallint pi_inverted; /* "! cmd | cmd" */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 323 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
| 324 | smallint res_word; /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 327 | /* On program start, environ points to initial environment. |
| 328 | * putenv adds new pointers into it, unsetenv removes them. |
| 329 | * Neither of these (de)allocates the strings. |
| 330 | * setenv allocates new strings in malloc space and does putenv, |
| 331 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 332 | #define setenv(...) setenv_is_leaky_dont_use() |
| 333 | struct variable { |
| 334 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 335 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 336 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 337 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 338 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 339 | }; |
| 340 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 341 | typedef struct { |
| 342 | char *data; |
| 343 | int length; |
| 344 | int maxlen; |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 345 | /* Misnomer! it's not "quoting", it's "protection against globbing"! |
| 346 | * (by prepending \ to *, ?, [ and to \ too) */ |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 347 | smallint o_quote; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 348 | smallint o_glob; |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 349 | smallint nonnull; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 350 | smallint has_empty_slot; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 351 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 352 | } o_string; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 353 | enum { |
| 354 | MAYBE_ASSIGNMENT = 0, |
| 355 | DEFINITELY_ASSIGNMENT = 1, |
| 356 | NOT_ASSIGNMENT = 2, |
| 357 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 358 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 359 | #define NULL_O_STRING { NULL } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 360 | |
| 361 | /* I can almost use ordinary FILE *. Is open_memstream() universally |
| 362 | * available? Where is it documented? */ |
| 363 | struct in_str { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 364 | const char *p; |
| 365 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 366 | char eof_flag; /* meaningless if ->p == NULL */ |
| 367 | char peek_buf[2]; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 368 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 369 | smallint promptme; |
| 370 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 371 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 372 | FILE *file; |
| 373 | int (*get) (struct in_str *); |
| 374 | int (*peek) (struct in_str *); |
| 375 | }; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 376 | #define i_getch(input) ((input)->get(input)) |
| 377 | #define i_peek(input) ((input)->peek(input)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 378 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 379 | enum { |
| 380 | CHAR_ORDINARY = 0, |
| 381 | CHAR_ORDINARY_IF_QUOTED = 1, /* example: *, # */ |
| 382 | CHAR_IFS = 2, /* treated as ordinary if quoted */ |
| 383 | CHAR_SPECIAL = 3, /* example: $ */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 384 | }; |
| 385 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 386 | #define HUSH_VER_STR "0.02" |
| 387 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 388 | /* "Globals" within this file */ |
| 389 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 390 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 391 | struct globals { |
| 392 | #if ENABLE_HUSH_INTERACTIVE |
| 393 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 394 | * _AND_ if we decided to act interactively */ |
| 395 | int interactive_fd; |
| 396 | const char *PS1; |
| 397 | const char *PS2; |
| 398 | #endif |
| 399 | #if ENABLE_FEATURE_EDITING |
| 400 | line_input_t *line_input_state; |
| 401 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame^] | 402 | pid_t root_pid; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 403 | #if ENABLE_HUSH_JOB |
| 404 | int run_list_level; |
| 405 | pid_t saved_task_pgrp; |
| 406 | pid_t saved_tty_pgrp; |
| 407 | int last_jobid; |
| 408 | struct pipe *job_list; |
| 409 | struct pipe *toplevel_list; |
| 410 | smallint ctrl_z_flag; |
| 411 | #endif |
| 412 | smallint fake_mode; |
| 413 | /* these three support $?, $#, and $1 */ |
| 414 | char **global_argv; |
| 415 | int global_argc; |
| 416 | int last_return_code; |
| 417 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 418 | const char *cwd; |
| 419 | unsigned last_bg_pid; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 420 | struct variable *top_var; /* = &shell_ver (set in main()) */ |
| 421 | struct variable shell_ver; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 422 | #if ENABLE_FEATURE_SH_STANDALONE |
| 423 | struct nofork_save_area nofork_save; |
| 424 | #endif |
| 425 | #if ENABLE_HUSH_JOB |
| 426 | sigjmp_buf toplevel_jb; |
| 427 | #endif |
| 428 | unsigned char charmap[256]; |
| 429 | char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2]; |
| 430 | }; |
| 431 | |
| 432 | #define G (*ptr_to_globals) |
| 433 | |
| 434 | #if !ENABLE_HUSH_INTERACTIVE |
| 435 | enum { interactive_fd = 0 }; |
| 436 | #endif |
| 437 | #if !ENABLE_HUSH_JOB |
| 438 | enum { run_list_level = 0 }; |
| 439 | #endif |
| 440 | |
| 441 | #if ENABLE_HUSH_INTERACTIVE |
| 442 | #define interactive_fd (G.interactive_fd ) |
| 443 | #define PS1 (G.PS1 ) |
| 444 | #define PS2 (G.PS2 ) |
| 445 | #endif |
| 446 | #if ENABLE_FEATURE_EDITING |
| 447 | #define line_input_state (G.line_input_state) |
| 448 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame^] | 449 | #define root_pid (G.root_pid ) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 450 | #if ENABLE_HUSH_JOB |
| 451 | #define run_list_level (G.run_list_level ) |
| 452 | #define saved_task_pgrp (G.saved_task_pgrp ) |
| 453 | #define saved_tty_pgrp (G.saved_tty_pgrp ) |
| 454 | #define last_jobid (G.last_jobid ) |
| 455 | #define job_list (G.job_list ) |
| 456 | #define toplevel_list (G.toplevel_list ) |
| 457 | #define toplevel_jb (G.toplevel_jb ) |
| 458 | #define ctrl_z_flag (G.ctrl_z_flag ) |
| 459 | #endif /* JOB */ |
| 460 | #define global_argv (G.global_argv ) |
| 461 | #define global_argc (G.global_argc ) |
| 462 | #define last_return_code (G.last_return_code) |
| 463 | #define ifs (G.ifs ) |
| 464 | #define fake_mode (G.fake_mode ) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 465 | #define cwd (G.cwd ) |
| 466 | #define last_bg_pid (G.last_bg_pid ) |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 467 | #define top_var (G.top_var ) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 468 | #define shell_ver (G.shell_ver ) |
| 469 | #if ENABLE_FEATURE_SH_STANDALONE |
| 470 | #define nofork_save (G.nofork_save ) |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 471 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 472 | #if ENABLE_HUSH_JOB |
| 473 | #define toplevel_jb (G.toplevel_jb ) |
| 474 | #endif |
| 475 | #define charmap (G.charmap ) |
| 476 | #define user_input_buf (G.user_input_buf ) |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 477 | #define INIT_G() do { \ |
| 478 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 479 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 480 | |
| 481 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 482 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
| 483 | |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 484 | #if 1 |
| 485 | /* Normal */ |
| 486 | static void syntax(const char *msg) |
| 487 | { |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 488 | /* Was using fancy stuff: |
| 489 | * (interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...) |
| 490 | * but it SEGVs. ?! Oh well... explicit temp ptr works around that */ |
| 491 | void (*fp)(const char *s, ...); |
| 492 | |
| 493 | fp = (interactive_fd ? bb_error_msg : bb_error_msg_and_die); |
| 494 | fp(msg ? "%s: %s" : "syntax error", "syntax error", msg); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 495 | } |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 496 | |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 497 | #else |
| 498 | /* Debug */ |
| 499 | static void syntax_lineno(int line) |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 500 | { |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 501 | void (*fp)(const char *s, ...); |
| 502 | |
| 503 | fp = (interactive_fd ? bb_error_msg : bb_error_msg_and_die); |
| 504 | fp("syntax error hush.c:%d", line); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 505 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 506 | #define syntax(str) syntax_lineno(__LINE__) |
| 507 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 508 | |
| 509 | /* Index of subroutines: */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 510 | /* in_str manipulations: */ |
| 511 | static int static_get(struct in_str *i); |
| 512 | static int static_peek(struct in_str *i); |
| 513 | static int file_get(struct in_str *i); |
| 514 | static int file_peek(struct in_str *i); |
| 515 | static void setup_file_in_str(struct in_str *i, FILE *f); |
| 516 | 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] | 517 | /* "run" the final data structures: */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 518 | #if !defined(DEBUG_CLEAN) |
| 519 | #define free_pipe_list(head, indent) free_pipe_list(head) |
| 520 | #define free_pipe(pi, indent) free_pipe(pi) |
| 521 | #endif |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 522 | static int free_pipe_list(struct pipe *head, int indent); |
| 523 | static int free_pipe(struct pipe *pi, int indent); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 524 | /* really run the final data structures: */ |
| 525 | static int setup_redirects(struct child_prog *prog, int squirrel[]); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 526 | static int run_list(struct pipe *pi); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 527 | #if BB_MMU |
| 528 | #define pseudo_exec_argv(ptrs2free, argv) pseudo_exec_argv(argv) |
| 529 | #define pseudo_exec(ptrs2free, child) pseudo_exec(child) |
| 530 | #endif |
| 531 | static void pseudo_exec_argv(char **ptrs2free, char **argv) ATTRIBUTE_NORETURN; |
| 532 | static void pseudo_exec(char **ptrs2free, struct child_prog *child) ATTRIBUTE_NORETURN; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 533 | static int run_pipe(struct pipe *pi); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 534 | /* data structure manipulation: */ |
| 535 | static int setup_redirect(struct p_context *ctx, int fd, redir_type style, struct in_str *input); |
| 536 | static void initialize_context(struct p_context *ctx); |
| 537 | static int done_word(o_string *dest, struct p_context *ctx); |
| 538 | static int done_command(struct p_context *ctx); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 539 | static void done_pipe(struct p_context *ctx, pipe_style type); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 540 | /* primary string parsing: */ |
| 541 | static int redirect_dup_num(struct in_str *input); |
| 542 | static int redirect_opt_num(o_string *o); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 543 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 544 | static int process_command_subs(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 545 | struct in_str *input, const char *subst_end); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 546 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 547 | 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] | 548 | static const char *lookup_param(const char *src); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 549 | static int handle_dollar(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 550 | struct in_str *input); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 551 | 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] | 552 | /* setup: */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 553 | static int parse_and_run_stream(struct in_str *inp, int parse_flag); |
| 554 | static int parse_and_run_string(const char *s, int parse_flag); |
| 555 | static int parse_and_run_file(FILE *f); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 556 | /* job management: */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 557 | static int checkjobs(struct pipe* fg_pipe); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 558 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 559 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 560 | static void insert_bg_job(struct pipe *pi); |
| 561 | static void remove_bg_job(struct pipe *pi); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 562 | static void delete_finished_bg_job(struct pipe *pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 563 | #else |
| 564 | int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */ |
| 565 | #endif |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 566 | /* local variable support */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 567 | static char **expand_strvec_to_strvec(char **argv); |
| 568 | /* used for eval */ |
| 569 | static char *expand_strvec_to_string(char **argv); |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 570 | /* used for expansion of right hand of assignments */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 571 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 572 | static struct variable *get_local_var(const char *name); |
| 573 | static int set_local_var(char *str, int flg_export); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 574 | static void unset_local_var(const char *name); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 575 | |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 576 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 577 | static int glob_needed(const char *s) |
| 578 | { |
| 579 | while (*s) { |
| 580 | if (*s == '\\') |
| 581 | s++; |
| 582 | if (*s == '*' || *s == '[' || *s == '?') |
| 583 | return 1; |
| 584 | s++; |
| 585 | } |
| 586 | return 0; |
| 587 | } |
| 588 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 589 | static int is_assignment(const char *s) |
| 590 | { |
| 591 | if (!s || !isalpha(*s)) |
| 592 | return 0; |
| 593 | s++; |
| 594 | while (isalnum(*s) || *s == '_') |
| 595 | s++; |
| 596 | return *s == '='; |
| 597 | } |
| 598 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 599 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 600 | static char *unbackslash(char *src) |
| 601 | { |
| 602 | char *dst = src; |
| 603 | while (1) { |
| 604 | if (*src == '\\') |
| 605 | src++; |
| 606 | if ((*dst++ = *src++) == '\0') |
| 607 | break; |
| 608 | } |
| 609 | return dst; |
| 610 | } |
| 611 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 612 | static char **add_malloced_strings_to_strings(char **strings, char **add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 613 | { |
| 614 | int i; |
| 615 | unsigned count1; |
| 616 | unsigned count2; |
| 617 | char **v; |
| 618 | |
| 619 | v = strings; |
| 620 | count1 = 0; |
| 621 | if (v) { |
| 622 | while (*v) { |
| 623 | count1++; |
| 624 | v++; |
| 625 | } |
| 626 | } |
| 627 | count2 = 0; |
| 628 | v = add; |
| 629 | while (*v) { |
| 630 | count2++; |
| 631 | v++; |
| 632 | } |
| 633 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 634 | v[count1 + count2] = NULL; |
| 635 | i = count2; |
| 636 | while (--i >= 0) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 637 | v[count1 + i] = add[i]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 638 | return v; |
| 639 | } |
| 640 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 641 | static char **add_malloced_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 642 | { |
| 643 | char *v[2]; |
| 644 | |
| 645 | v[0] = add; |
| 646 | v[1] = NULL; |
| 647 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 648 | return add_malloced_strings_to_strings(strings, v); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | static void free_strings(char **strings) |
| 652 | { |
| 653 | if (strings) { |
| 654 | char **v = strings; |
| 655 | while (*v) |
| 656 | free(*v++); |
| 657 | free(strings); |
| 658 | } |
| 659 | } |
| 660 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 661 | #if !BB_MMU |
| 662 | #define EXTRA_PTRS 5 /* 1 for NULL, 1 for args, 3 for paranoid reasons */ |
| 663 | static char **alloc_ptrs(char **argv) |
| 664 | { |
| 665 | char **v = argv; |
| 666 | while (*v) |
| 667 | v++; |
| 668 | return xzalloc((v - argv + EXTRA_PTRS) * sizeof(v[0])); |
| 669 | } |
| 670 | #endif |
| 671 | |
| 672 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 673 | /* Function prototypes for builtins */ |
| 674 | static int builtin_cd(char **argv); |
Denis Vlasenko | 5bc593c | 2007-11-23 21:20:21 +0000 | [diff] [blame] | 675 | static int builtin_echo(char **argv); |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 676 | static int builtin_eval(char **argv); |
| 677 | static int builtin_exec(char **argv); |
| 678 | static int builtin_exit(char **argv); |
| 679 | static int builtin_export(char **argv); |
| 680 | #if ENABLE_HUSH_JOB |
| 681 | static int builtin_fg_bg(char **argv); |
| 682 | static int builtin_jobs(char **argv); |
| 683 | #endif |
| 684 | #if ENABLE_HUSH_HELP |
| 685 | static int builtin_help(char **argv); |
| 686 | #endif |
| 687 | static int builtin_pwd(char **argv); |
| 688 | static int builtin_read(char **argv); |
| 689 | static int builtin_test(char **argv); |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 690 | static int builtin_true(char **argv); |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 691 | static int builtin_set(char **argv); |
| 692 | static int builtin_shift(char **argv); |
| 693 | static int builtin_source(char **argv); |
| 694 | static int builtin_umask(char **argv); |
| 695 | static int builtin_unset(char **argv); |
| 696 | //static int builtin_not_written(char **argv); |
| 697 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 698 | /* Table of built-in functions. They can be forked or not, depending on |
| 699 | * context: within pipes, they fork. As simple commands, they do not. |
| 700 | * When used in non-forking context, they can change global variables |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 701 | * in the parent shell process. If forked, of course they cannot. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 702 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 703 | * still be set at the end. */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 704 | struct built_in_command { |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 705 | const char *cmd; |
| 706 | int (*function)(char **argv); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 707 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 708 | const char *descr; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 709 | #define BLTIN(cmd, func, help) { cmd, func, help } |
| 710 | #else |
| 711 | #define BLTIN(cmd, func, help) { cmd, func } |
| 712 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 713 | }; |
| 714 | |
Denis Vlasenko | 5bc593c | 2007-11-23 21:20:21 +0000 | [diff] [blame] | 715 | /* For now, echo and test are unconditionally enabled. |
| 716 | * Maybe make it configurable? */ |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 717 | static const struct built_in_command bltins[] = { |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 718 | BLTIN("." , builtin_source, "Run commands in a file"), |
| 719 | BLTIN(":" , builtin_true, "No-op"), |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 720 | BLTIN("[" , builtin_test, "Test condition"), |
| 721 | BLTIN("[[" , builtin_test, "Test condition"), |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 722 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 723 | BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"), |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 724 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 725 | // BLTIN("break" , builtin_not_written, "Exit for, while or until loop"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 726 | BLTIN("cd" , builtin_cd, "Change directory"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 727 | // BLTIN("continue", builtin_not_written, "Continue for, while or until loop"), |
Denis Vlasenko | 5bc593c | 2007-11-23 21:20:21 +0000 | [diff] [blame] | 728 | BLTIN("echo" , builtin_echo, "Write strings to stdout"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 729 | BLTIN("eval" , builtin_eval, "Construct and run shell command"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 730 | BLTIN("exec" , builtin_exec, "Execute command, don't return to shell"), |
| 731 | BLTIN("exit" , builtin_exit, "Exit"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 732 | BLTIN("export", builtin_export, "Set environment variable"), |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 733 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 734 | BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 735 | BLTIN("jobs" , builtin_jobs, "List active jobs"), |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 736 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 737 | BLTIN("pwd" , builtin_pwd, "Print current directory"), |
| 738 | BLTIN("read" , builtin_read, "Input environment variable"), |
| 739 | // BLTIN("return", builtin_not_written, "Return from a function"), |
| 740 | BLTIN("set" , builtin_set, "Set/unset shell local variables"), |
| 741 | BLTIN("shift" , builtin_shift, "Shift positional parameters"), |
| 742 | // BLTIN("trap" , builtin_not_written, "Trap signals"), |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 743 | BLTIN("test" , builtin_test, "Test condition"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 744 | // BLTIN("ulimit", builtin_not_written, "Control resource limits"), |
| 745 | BLTIN("umask" , builtin_umask, "Set file creation mask"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 746 | BLTIN("unset" , builtin_unset, "Unset environment variable"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 747 | #if ENABLE_HUSH_HELP |
| 748 | BLTIN("help" , builtin_help, "List shell built-in commands"), |
| 749 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 750 | }; |
| 751 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 752 | |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 753 | /* Signals are grouped, we handle them in batches */ |
| 754 | static void set_misc_sighandler(void (*handler)(int)) |
| 755 | { |
| 756 | bb_signals(0 |
| 757 | + (1 << SIGINT) |
| 758 | + (1 << SIGQUIT) |
| 759 | + (1 << SIGTERM) |
| 760 | , handler); |
| 761 | } |
| 762 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 763 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 764 | |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 765 | static void set_fatal_sighandler(void (*handler)(int)) |
| 766 | { |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 767 | bb_signals(0 |
| 768 | + (1 << SIGILL) |
| 769 | + (1 << SIGTRAP) |
| 770 | + (1 << SIGABRT) |
| 771 | + (1 << SIGFPE) |
| 772 | + (1 << SIGBUS) |
| 773 | + (1 << SIGSEGV) |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 774 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 775 | + (1 << SIGHUP) |
| 776 | + (1 << SIGPIPE) |
| 777 | + (1 << SIGALRM) |
| 778 | , handler); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 779 | } |
| 780 | static void set_jobctrl_sighandler(void (*handler)(int)) |
| 781 | { |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 782 | bb_signals(0 |
| 783 | + (1 << SIGTSTP) |
| 784 | + (1 << SIGTTIN) |
| 785 | + (1 << SIGTTOU) |
| 786 | , handler); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 787 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 788 | /* SIGCHLD is special and handled separately */ |
| 789 | |
| 790 | static void set_every_sighandler(void (*handler)(int)) |
| 791 | { |
| 792 | set_fatal_sighandler(handler); |
| 793 | set_jobctrl_sighandler(handler); |
| 794 | set_misc_sighandler(handler); |
| 795 | signal(SIGCHLD, handler); |
| 796 | } |
| 797 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 798 | static void handler_ctrl_c(int sig ATTRIBUTE_UNUSED) |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 799 | { |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 800 | debug_printf_jobs("got sig %d\n", sig); |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 801 | // 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] | 802 | siglongjmp(toplevel_jb, 1); |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 805 | static void handler_ctrl_z(int sig ATTRIBUTE_UNUSED) |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 806 | { |
| 807 | pid_t pid; |
| 808 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 809 | 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] | 810 | pid = fork(); |
Denis Vlasenko | c299032 | 2007-05-16 12:57:12 +0000 | [diff] [blame] | 811 | if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */ |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 812 | return; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 813 | ctrl_z_flag = 1; |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 814 | if (!pid) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 815 | if (ENABLE_HUSH_JOB) |
| 816 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 817 | setpgrp(); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 818 | debug_printf_jobs("set pgrp for child %d ok\n", getpid()); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 819 | set_every_sighandler(SIG_DFL); |
Denis Vlasenko | 18e19f2 | 2007-04-28 16:43:18 +0000 | [diff] [blame] | 820 | raise(SIGTSTP); /* resend TSTP so that child will be stopped */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 821 | debug_printf_jobs("returning in child\n"); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 822 | /* return to nofork, it will eventually exit now, |
| 823 | * not return back to shell */ |
| 824 | return; |
| 825 | } |
| 826 | /* parent */ |
| 827 | /* finish filling up pipe info */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 828 | toplevel_list->pgrp = pid; /* child is in its own pgrp */ |
| 829 | toplevel_list->progs[0].pid = pid; |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 830 | /* parent needs to longjmp out of running nofork. |
| 831 | * we will "return" exitcode 0, with child put in background */ |
| 832 | // 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] | 833 | debug_printf_jobs("siglongjmp in parent\n"); |
| 834 | siglongjmp(toplevel_jb, 1); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 837 | /* Restores tty foreground process group, and exits. |
| 838 | * May be called as signal handler for fatal signal |
| 839 | * (will faithfully resend signal to itself, producing correct exit state) |
| 840 | * or called directly with -EXITCODE. |
| 841 | * We also call it if xfunc is exiting. */ |
| 842 | static void sigexit(int sig) ATTRIBUTE_NORETURN; |
| 843 | static void sigexit(int sig) |
| 844 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 845 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 846 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 847 | |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 848 | if (interactive_fd) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 849 | tcsetpgrp(interactive_fd, saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 850 | |
| 851 | /* Not a signal, just exit */ |
| 852 | if (sig <= 0) |
| 853 | _exit(- sig); |
| 854 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 855 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /* Restores tty foreground process group, and exits. */ |
| 859 | static void hush_exit(int exitcode) ATTRIBUTE_NORETURN; |
| 860 | static void hush_exit(int exitcode) |
| 861 | { |
| 862 | fflush(NULL); /* flush all streams */ |
| 863 | sigexit(- (exitcode & 0xff)); |
| 864 | } |
| 865 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 866 | #else /* !JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 867 | |
| 868 | #define set_fatal_sighandler(handler) ((void)0) |
| 869 | #define set_jobctrl_sighandler(handler) ((void)0) |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 870 | #define hush_exit(e) exit(e) |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 871 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 872 | #endif /* JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 873 | |
| 874 | |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 875 | static const char *set_cwd(void) |
| 876 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 877 | if (cwd == bb_msg_unknown) |
Denis Vlasenko | 7cced6e | 2007-04-12 17:08:53 +0000 | [diff] [blame] | 878 | cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */ |
Denis Vlasenko | 6ca0444 | 2007-02-11 16:19:28 +0000 | [diff] [blame] | 879 | cwd = xrealloc_getcwd_or_warn((char *)cwd); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 880 | if (!cwd) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 881 | cwd = bb_msg_unknown; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 882 | return cwd; |
| 883 | } |
| 884 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 885 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 886 | /* |
| 887 | * o_string support |
| 888 | */ |
| 889 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 890 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 891 | static void o_reset(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 892 | { |
| 893 | o->length = 0; |
| 894 | o->nonnull = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 895 | if (o->data) |
| 896 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 899 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 900 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 901 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 902 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 905 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 906 | { |
| 907 | if (o->length + len > o->maxlen) { |
| 908 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 909 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 910 | } |
| 911 | } |
| 912 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 913 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 914 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 915 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 916 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 917 | o->data[o->length] = ch; |
| 918 | o->length++; |
| 919 | o->data[o->length] = '\0'; |
| 920 | } |
| 921 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 922 | static void o_addstr(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 923 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 924 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 925 | memcpy(&o->data[o->length], str, len); |
| 926 | o->length += len; |
| 927 | o->data[o->length] = '\0'; |
| 928 | } |
| 929 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 930 | static void o_addstr_duplicate_backslash(o_string *o, const char *str, int len) |
| 931 | { |
| 932 | while (len) { |
| 933 | o_addchr(o, *str); |
| 934 | if (*str++ == '\\' |
| 935 | && (*str != '*' && *str != '?' && *str != '[') |
| 936 | ) { |
| 937 | o_addchr(o, '\\'); |
| 938 | } |
| 939 | len--; |
| 940 | } |
| 941 | } |
| 942 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 943 | /* My analysis of quoting semantics tells me that state information |
| 944 | * is associated with a destination, not a source. |
| 945 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 946 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 947 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 948 | int sz = 1; |
| 949 | if (strchr("*?[\\", ch)) { |
| 950 | sz++; |
| 951 | o->data[o->length] = '\\'; |
| 952 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 953 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 954 | o_grow_by(o, sz); |
| 955 | o->data[o->length] = ch; |
| 956 | o->length++; |
| 957 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 960 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 961 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 962 | int sz = 1; |
| 963 | if (o->o_quote && strchr("*?[\\", ch)) { |
| 964 | sz++; |
| 965 | o->data[o->length] = '\\'; |
| 966 | o->length++; |
| 967 | } |
| 968 | o_grow_by(o, sz); |
| 969 | o->data[o->length] = ch; |
| 970 | o->length++; |
| 971 | o->data[o->length] = '\0'; |
| 972 | } |
| 973 | |
| 974 | static void o_addQstr(o_string *o, const char *str, int len) |
| 975 | { |
| 976 | if (!o->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 977 | o_addstr(o, str, len); |
| 978 | return; |
| 979 | } |
| 980 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 981 | char ch; |
| 982 | int sz; |
| 983 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 984 | if (ordinary_cnt > len) /* paranoia */ |
| 985 | ordinary_cnt = len; |
| 986 | o_addstr(o, str, ordinary_cnt); |
| 987 | if (ordinary_cnt == len) |
| 988 | return; |
| 989 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 990 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 991 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 992 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 993 | sz = 1; |
| 994 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 995 | sz++; |
| 996 | o->data[o->length] = '\\'; |
| 997 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 998 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 999 | o_grow_by(o, sz); |
| 1000 | o->data[o->length] = ch; |
| 1001 | o->length++; |
| 1002 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1006 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1007 | * 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] | 1008 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1009 | * list[i] contains an INDEX (int!) into this string data. |
| 1010 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1011 | * but list[i]'s need not be modified. |
| 1012 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1013 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1014 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1015 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1016 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1017 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1018 | { |
| 1019 | char **list = (char**)o->data; |
| 1020 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1021 | int i = 0; |
| 1022 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1023 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1024 | while (i < n) { |
| 1025 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1026 | o->data + (int)list[i] + string_start, |
| 1027 | o->data + (int)list[i] + string_start); |
| 1028 | i++; |
| 1029 | } |
| 1030 | if (n) { |
| 1031 | const char *p = o->data + (int)list[n - 1] + string_start; |
| 1032 | fprintf(stderr, " total_sz:%d\n", (p + strlen(p) + 1) - o->data); |
| 1033 | } |
| 1034 | } |
| 1035 | #else |
| 1036 | #define debug_print_list(prefix, o, n) ((void)0) |
| 1037 | #endif |
| 1038 | |
| 1039 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1040 | * in list[n] so that it points past last stored byte so far. |
| 1041 | * It returns n+1. */ |
| 1042 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1043 | { |
| 1044 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1045 | int string_start; |
| 1046 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1047 | |
| 1048 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1049 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1050 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1051 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1052 | 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] | 1053 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1054 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1055 | o->data = xrealloc(o->data, o->maxlen + 1); |
| 1056 | list = (char**)o->data; |
| 1057 | memmove(list + n + 0x10, list + n, string_len); |
| 1058 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1059 | } else |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1060 | 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] | 1061 | } else { |
| 1062 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1063 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1064 | string_len = o->length - string_start; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1065 | 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] | 1066 | o->has_empty_slot = 0; |
| 1067 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame^] | 1068 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1069 | return n + 1; |
| 1070 | } |
| 1071 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1072 | /* "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] | 1073 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1074 | { |
| 1075 | char **list = (char**)o->data; |
| 1076 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1077 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame^] | 1078 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1081 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1082 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1083 | static int o_glob(o_string *o, int n) |
| 1084 | { |
| 1085 | glob_t globdata; |
| 1086 | int gr; |
| 1087 | char *pattern; |
| 1088 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1089 | 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] | 1090 | if (!o->data) |
| 1091 | return o_save_ptr_helper(o, n); |
| 1092 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1093 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1094 | if (!glob_needed(pattern)) { |
| 1095 | literal: |
| 1096 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1097 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1098 | return o_save_ptr_helper(o, n); |
| 1099 | } |
| 1100 | |
| 1101 | memset(&globdata, 0, sizeof(globdata)); |
| 1102 | gr = glob(pattern, 0, NULL, &globdata); |
| 1103 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 1104 | if (gr == GLOB_NOSPACE) |
| 1105 | bb_error_msg_and_die("out of memory during glob"); |
| 1106 | if (gr == GLOB_NOMATCH) { |
| 1107 | globfree(&globdata); |
| 1108 | goto literal; |
| 1109 | } |
| 1110 | if (gr != 0) { /* GLOB_ABORTED ? */ |
| 1111 | //TODO: testcase for bad glob pattern behavior |
| 1112 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 1113 | } |
| 1114 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 1115 | char **argv = globdata.gl_pathv; |
| 1116 | o->length = pattern - o->data; /* "forget" pattern */ |
| 1117 | while (1) { |
| 1118 | o_addstr(o, *argv, strlen(*argv) + 1); |
| 1119 | n = o_save_ptr_helper(o, n); |
| 1120 | argv++; |
| 1121 | if (!*argv) |
| 1122 | break; |
| 1123 | } |
| 1124 | } |
| 1125 | globfree(&globdata); |
| 1126 | if (DEBUG_GLOB) |
| 1127 | debug_print_list("o_glob returning", o, n); |
| 1128 | return n; |
| 1129 | } |
| 1130 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1131 | /* If o->o_glob == 1, glob the string so far remembered. |
| 1132 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1133 | static int o_save_ptr(o_string *o, int n) |
| 1134 | { |
| 1135 | if (o->o_glob) |
| 1136 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 1137 | return o_save_ptr_helper(o, n); |
| 1138 | } |
| 1139 | |
| 1140 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1141 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1142 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1143 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1144 | int string_start; |
| 1145 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1146 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 1147 | if (DEBUG_EXPAND) |
| 1148 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1149 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1150 | list = (char**)o->data; |
| 1151 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1152 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1153 | while (n) { |
| 1154 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame^] | 1155 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1156 | } |
| 1157 | return list; |
| 1158 | } |
| 1159 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1160 | |
| 1161 | /* |
| 1162 | * in_str support |
| 1163 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1164 | static int static_get(struct in_str *i) |
| 1165 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1166 | int ch = *i->p++; |
| 1167 | if (ch == '\0') return EOF; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1168 | return ch; |
| 1169 | } |
| 1170 | |
| 1171 | static int static_peek(struct in_str *i) |
| 1172 | { |
| 1173 | return *i->p; |
| 1174 | } |
| 1175 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1176 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1177 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1178 | #if ENABLE_FEATURE_EDITING |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 1179 | static void cmdedit_set_initial_prompt(void) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1180 | { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1181 | #if !ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1182 | PS1 = NULL; |
| 1183 | #else |
| 1184 | PS1 = getenv("PS1"); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1185 | if (PS1 == NULL) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1186 | PS1 = "\\w \\$ "; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1187 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1188 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1189 | #endif /* EDITING */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1190 | |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1191 | static const char* setup_prompt_string(int promptmode) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1192 | { |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1193 | const char *prompt_str; |
| 1194 | debug_printf("setup_prompt_string %d ", promptmode); |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1195 | #if !ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1196 | /* Set up the prompt */ |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1197 | if (promptmode == 0) { /* PS1 */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1198 | free((char*)PS1); |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1199 | PS1 = xasprintf("%s %c ", cwd, (geteuid() != 0) ? '$' : '#'); |
| 1200 | prompt_str = PS1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1201 | } else { |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1202 | prompt_str = PS2; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1203 | } |
| 1204 | #else |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1205 | prompt_str = (promptmode == 0) ? PS1 : PS2; |
Eric Andersen | af44a0e | 2001-04-27 07:26:12 +0000 | [diff] [blame] | 1206 | #endif |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1207 | debug_printf("result '%s'\n", prompt_str); |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1208 | return prompt_str; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1211 | static void get_user_input(struct in_str *i) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1212 | { |
Denis Vlasenko | 0937be5 | 2007-04-28 16:47:08 +0000 | [diff] [blame] | 1213 | int r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1214 | const char *prompt_str; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1215 | |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1216 | prompt_str = setup_prompt_string(i->promptmode); |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1217 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1218 | /* Enable command line editing only while a command line |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 1219 | * is actually being read */ |
| 1220 | do { |
| 1221 | r = read_line_input(prompt_str, user_input_buf, BUFSIZ-1, line_input_state); |
| 1222 | } while (r == 0); /* repeat if Ctrl-C */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1223 | i->eof_flag = (r < 0); |
| 1224 | if (i->eof_flag) { /* EOF/error detected */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1225 | user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1226 | user_input_buf[1] = '\0'; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1227 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1228 | #else |
| 1229 | fputs(prompt_str, stdout); |
| 1230 | fflush(stdout); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1231 | user_input_buf[0] = r = fgetc(i->file); |
| 1232 | /*user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1233 | i->eof_flag = (r == EOF); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1234 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1235 | i->p = user_input_buf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1236 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1237 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1238 | #endif /* INTERACTIVE */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1239 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1240 | /* This is the magic location that prints prompts |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1241 | * and gets data back from the user */ |
| 1242 | static int file_get(struct in_str *i) |
| 1243 | { |
| 1244 | int ch; |
| 1245 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1246 | /* If there is data waiting, eat it up */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1247 | if (i->p && *i->p) { |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1248 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1249 | take_cached: |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1250 | #endif |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1251 | ch = *i->p++; |
| 1252 | if (i->eof_flag && !*i->p) |
| 1253 | ch = EOF; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1254 | } else { |
| 1255 | /* need to double check i->file because we might be doing something |
| 1256 | * more complicated by now, like sourcing or substituting. */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1257 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1258 | if (interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1259 | do { |
| 1260 | get_user_input(i); |
| 1261 | } while (!*i->p); /* need non-empty line */ |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1262 | i->promptmode = 1; /* PS2 */ |
| 1263 | i->promptme = 0; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1264 | goto take_cached; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1265 | } |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1266 | #endif |
| 1267 | ch = fgetc(i->file); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1268 | } |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1269 | debug_printf("file_get: got a '%c' %d\n", ch, ch); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1270 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1271 | if (ch == '\n') |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1272 | i->promptme = 1; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1273 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1274 | return ch; |
| 1275 | } |
| 1276 | |
| 1277 | /* All the callers guarantee this routine will never be |
| 1278 | * used right after a newline, so prompting is not needed. |
| 1279 | */ |
| 1280 | static int file_peek(struct in_str *i) |
| 1281 | { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 1282 | int ch; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1283 | if (i->p && *i->p) { |
| 1284 | if (i->eof_flag && !i->p[1]) |
| 1285 | return EOF; |
| 1286 | return *i->p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1287 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 1288 | ch = fgetc(i->file); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1289 | i->eof_flag = (ch == EOF); |
| 1290 | i->peek_buf[0] = ch; |
| 1291 | i->peek_buf[1] = '\0'; |
| 1292 | i->p = i->peek_buf; |
| 1293 | 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] | 1294 | return ch; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1298 | { |
| 1299 | i->peek = file_peek; |
| 1300 | i->get = file_get; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1301 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1302 | i->promptme = 1; |
| 1303 | i->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1304 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1305 | i->file = f; |
| 1306 | i->p = NULL; |
| 1307 | } |
| 1308 | |
| 1309 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1310 | { |
| 1311 | i->peek = static_peek; |
| 1312 | i->get = static_get; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1313 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1314 | i->promptme = 1; |
| 1315 | i->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1316 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1317 | i->p = s; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1318 | i->eof_flag = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1321 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1322 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 1323 | * and stderr if they are redirected. */ |
| 1324 | static int setup_redirects(struct child_prog *prog, int squirrel[]) |
| 1325 | { |
| 1326 | int openfd, mode; |
| 1327 | struct redir_struct *redir; |
| 1328 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1329 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1330 | if (redir->dup == -1 && redir->rd_filename == NULL) { |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 1331 | /* something went wrong in the parse. Pretend it didn't happen */ |
| 1332 | continue; |
| 1333 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1334 | if (redir->dup == -1) { |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1335 | char *p; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1336 | mode = redir_table[redir->rd_type].mode; |
| 1337 | //TODO: check redir to names like '\\' |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1338 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1339 | openfd = open_or_warn(p, mode); |
| 1340 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1341 | if (openfd < 0) { |
| 1342 | /* this could get lost if stderr has been redirected, but |
| 1343 | bash and ash both lose it as well (though zsh doesn't!) */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1344 | return 1; |
| 1345 | } |
| 1346 | } else { |
| 1347 | openfd = redir->dup; |
| 1348 | } |
| 1349 | |
| 1350 | if (openfd != redir->fd) { |
| 1351 | if (squirrel && redir->fd < 3) { |
| 1352 | squirrel[redir->fd] = dup(redir->fd); |
| 1353 | } |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1354 | if (openfd == -3) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1355 | //close(openfd); // close(-3) ??! |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1356 | } else { |
| 1357 | dup2(openfd, redir->fd); |
Matt Kraai | c616e53 | 2001-06-05 16:50:08 +0000 | [diff] [blame] | 1358 | if (redir->dup == -1) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1359 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1360 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | return 0; |
| 1364 | } |
| 1365 | |
| 1366 | static void restore_redirects(int squirrel[]) |
| 1367 | { |
| 1368 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1369 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1370 | fd = squirrel[i]; |
| 1371 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 1372 | /* We simply die on error */ |
| 1373 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1378 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1379 | /* Called after [v]fork() in run_pipe(), or from builtin_exec(). |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1380 | * Never returns. |
| 1381 | * XXX no exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1382 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1383 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1384 | static void pseudo_exec_argv(char **ptrs2free, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1385 | { |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1386 | int i, rcode; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 1387 | char *p; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 1388 | const struct built_in_command *x; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1389 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1390 | for (i = 0; is_assignment(argv[i]); i++) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1391 | debug_printf_exec("pid %d environment modification: %s\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1392 | getpid(), argv[i]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1393 | p = expand_string_to_string(argv[i]); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1394 | #if !BB_MMU |
| 1395 | *ptrs2free++ = p; |
| 1396 | #endif |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1397 | putenv(p); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1398 | } |
| 1399 | argv += i; |
| 1400 | /* If a variable is assigned in a forest, and nobody listens, |
| 1401 | * was it ever really set? |
| 1402 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1403 | if (!argv[0]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1404 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1405 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1406 | argv = expand_strvec_to_strvec(argv); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1407 | #if !BB_MMU |
| 1408 | *ptrs2free++ = (char*) argv; |
| 1409 | #endif |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 1410 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1411 | /* |
| 1412 | * Check if the command matches any of the builtins. |
| 1413 | * Depending on context, this might be redundant. But it's |
| 1414 | * easier to waste a few CPU cycles than it is to figure out |
| 1415 | * if this is one of those cases. |
| 1416 | */ |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 1417 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1418 | if (strcmp(argv[0], x->cmd) == 0) { |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 1419 | debug_printf_exec("running builtin '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1420 | rcode = x->function(argv); |
| 1421 | fflush(stdout); |
| 1422 | _exit(rcode); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1423 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1424 | } |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1425 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1426 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 1427 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1428 | if (strchr(argv[0], '/') == NULL) { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 1429 | int a = find_applet_by_name(argv[0]); |
| 1430 | if (a >= 0) { |
| 1431 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1432 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 1433 | // is it ok that run_applet_no_and_exit() does exit(), not _exit()? |
| 1434 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1435 | } |
| 1436 | /* re-exec ourselves with the new arguments */ |
| 1437 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | bdbbb7e | 2007-06-08 15:02:55 +0000 | [diff] [blame] | 1438 | execvp(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1439 | /* If they called chroot or otherwise made the binary no longer |
| 1440 | * executable, fall through */ |
| 1441 | } |
| 1442 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 1443 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1444 | |
| 1445 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1446 | execvp(argv[0], argv); |
| 1447 | bb_perror_msg("cannot exec '%s'", argv[0]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 1448 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1451 | /* Called after [v]fork() in run_pipe() |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1452 | */ |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1453 | static void pseudo_exec(char **ptrs2free, struct child_prog *child) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1454 | { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1455 | if (child->argv) |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1456 | pseudo_exec_argv(ptrs2free, child->argv); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1457 | |
| 1458 | if (child->group) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1459 | #if !BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 1460 | bb_error_msg_and_die("nested lists are not supported on NOMMU"); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1461 | #else |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 1462 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1463 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 1464 | rcode = run_list(child->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 1465 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1466 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1467 | _exit(rcode); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1468 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1469 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1470 | |
| 1471 | /* Can happen. See what bash does with ">foo" by itself. */ |
| 1472 | debug_printf("trying to pseudo_exec null command\n"); |
| 1473 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1476 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1477 | static const char *get_cmdtext(struct pipe *pi) |
| 1478 | { |
| 1479 | char **argv; |
| 1480 | char *p; |
| 1481 | int len; |
| 1482 | |
| 1483 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 1484 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1485 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1486 | if (pi->cmdtext) |
| 1487 | return pi->cmdtext; |
| 1488 | argv = pi->progs[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 1489 | if (!argv || !argv[0]) { |
| 1490 | pi->cmdtext = xzalloc(1); |
| 1491 | return pi->cmdtext; |
| 1492 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1493 | |
| 1494 | len = 0; |
| 1495 | do len += strlen(*argv) + 1; while (*++argv); |
| 1496 | pi->cmdtext = p = xmalloc(len); |
| 1497 | argv = pi->progs[0].argv; |
| 1498 | do { |
| 1499 | len = strlen(*argv); |
| 1500 | memcpy(p, *argv, len); |
| 1501 | p += len; |
| 1502 | *p++ = ' '; |
| 1503 | } while (*++argv); |
| 1504 | p[-1] = '\0'; |
| 1505 | return pi->cmdtext; |
| 1506 | } |
| 1507 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1508 | static void insert_bg_job(struct pipe *pi) |
| 1509 | { |
| 1510 | struct pipe *thejob; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1511 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1512 | |
| 1513 | /* Linear search for the ID of the job to use */ |
| 1514 | pi->jobid = 1; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1515 | for (thejob = job_list; thejob; thejob = thejob->next) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1516 | if (thejob->jobid >= pi->jobid) |
| 1517 | pi->jobid = thejob->jobid + 1; |
| 1518 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1519 | /* Add thejob to the list of running jobs */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1520 | if (!job_list) { |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1521 | thejob = job_list = xmalloc(sizeof(*thejob)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1522 | } else { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1523 | for (thejob = job_list; thejob->next; thejob = thejob->next) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1524 | continue; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1525 | thejob->next = xmalloc(sizeof(*thejob)); |
| 1526 | thejob = thejob->next; |
| 1527 | } |
| 1528 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1529 | /* Physically copy the struct job */ |
Eric Andersen | 0fcd447 | 2001-05-02 20:12:03 +0000 | [diff] [blame] | 1530 | memcpy(thejob, pi, sizeof(struct pipe)); |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1531 | thejob->progs = xzalloc(sizeof(pi->progs[0]) * pi->num_progs); |
| 1532 | /* We cannot copy entire pi->progs[] vector! Double free()s will happen */ |
| 1533 | for (i = 0; i < pi->num_progs; i++) { |
| 1534 | // TODO: do we really need to have so many fields which are just dead weight |
| 1535 | // at execution stage? |
| 1536 | thejob->progs[i].pid = pi->progs[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1537 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1538 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1539 | thejob->next = NULL; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1540 | thejob->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1541 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1542 | /* We don't wait for background thejobs to return -- append it |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1543 | to the list of backgrounded thejobs and leave it alone */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1544 | 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] | 1545 | last_bg_pid = thejob->progs[0].pid; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1546 | last_jobid = thejob->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1549 | static void remove_bg_job(struct pipe *pi) |
| 1550 | { |
| 1551 | struct pipe *prev_pipe; |
| 1552 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1553 | if (pi == job_list) { |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1554 | job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1555 | } else { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1556 | prev_pipe = job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1557 | while (prev_pipe->next != pi) |
| 1558 | prev_pipe = prev_pipe->next; |
| 1559 | prev_pipe->next = pi->next; |
| 1560 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 1561 | if (job_list) |
| 1562 | last_jobid = job_list->jobid; |
| 1563 | else |
| 1564 | last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1565 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 1566 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1567 | /* remove a backgrounded job */ |
| 1568 | static void delete_finished_bg_job(struct pipe *pi) |
| 1569 | { |
| 1570 | remove_bg_job(pi); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1571 | pi->stopped_progs = 0; |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 1572 | free_pipe(pi, 0); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1573 | free(pi); |
| 1574 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1575 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1576 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1577 | /* Checks to see if any processes have exited -- if they |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1578 | have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1579 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1580 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1581 | int attributes; |
| 1582 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1583 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1584 | int prognum = 0; |
| 1585 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1586 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1587 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1588 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1589 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1590 | attributes = WUNTRACED; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1591 | if (fg_pipe == NULL) { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1592 | attributes |= WNOHANG; |
| 1593 | } |
| 1594 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1595 | /* Do we do this right? |
| 1596 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1597 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1598 | * [3]+ Stopped sleep 20 | false |
| 1599 | * bash-3.00# echo $? |
| 1600 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 1601 | */ |
| 1602 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1603 | //FIXME: non-interactive bash does not continue even if all processes in fg pipe |
| 1604 | //are stopped. Testcase: "cat | cat" in a script (not on command line) |
| 1605 | // + killall -STOP cat |
| 1606 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1607 | wait_more: |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 1608 | // TODO: safe_waitpid? |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1609 | while ((childpid = waitpid(-1, &status, attributes)) > 0) { |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1610 | const int dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 1611 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1612 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1613 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1614 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1615 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 1616 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1617 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1618 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 1619 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1620 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1621 | childpid, WEXITSTATUS(status)); |
| 1622 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1623 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1624 | if (fg_pipe) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1625 | int i; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1626 | for (i = 0; i < fg_pipe->num_progs; i++) { |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1627 | debug_printf_jobs("check pid %d\n", fg_pipe->progs[i].pid); |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1628 | if (fg_pipe->progs[i].pid == childpid) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1629 | /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1630 | if (dead) { |
| 1631 | fg_pipe->progs[i].pid = 0; |
| 1632 | fg_pipe->running_progs--; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 1633 | if (i == fg_pipe->num_progs - 1) { |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1634 | /* last process gives overall exitstatus */ |
| 1635 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 1636 | if (fg_pipe->pi_inverted) |
| 1637 | rcode = !rcode; |
| 1638 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1639 | } else { |
| 1640 | fg_pipe->progs[i].is_stopped = 1; |
| 1641 | fg_pipe->stopped_progs++; |
| 1642 | } |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1643 | debug_printf_jobs("fg_pipe: running_progs %d stopped_progs %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1644 | fg_pipe->running_progs, fg_pipe->stopped_progs); |
| 1645 | if (fg_pipe->running_progs - fg_pipe->stopped_progs <= 0) { |
| 1646 | /* All processes in fg pipe have exited/stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1647 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1648 | if (fg_pipe->running_progs) |
| 1649 | insert_bg_job(fg_pipe); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1650 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1651 | return rcode; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1652 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1653 | /* There are still running processes in the fg pipe */ |
| 1654 | goto wait_more; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1655 | } |
| 1656 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1657 | /* fall through to searching process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1660 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1661 | /* We asked to wait for bg or orphaned children */ |
| 1662 | /* No need to remember exitcode in this case */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1663 | for (pi = job_list; pi; pi = pi->next) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1664 | prognum = 0; |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 1665 | while (prognum < pi->num_progs) { |
| 1666 | if (pi->progs[prognum].pid == childpid) |
| 1667 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1668 | prognum++; |
| 1669 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1670 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1671 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1672 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 1673 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 1674 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1675 | goto wait_more; |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 1676 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1677 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 1678 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1679 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1680 | /* child exited */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1681 | pi->progs[prognum].pid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1682 | pi->running_progs--; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1683 | if (!pi->running_progs) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1684 | printf(JOB_STATUS_FORMAT, pi->jobid, |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1685 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1686 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1687 | } |
| 1688 | } else { |
| 1689 | /* child stopped */ |
| 1690 | pi->stopped_progs++; |
| 1691 | pi->progs[prognum].is_stopped = 1; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1692 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1693 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1696 | /* wait found no children or failed */ |
| 1697 | |
| 1698 | if (childpid && errno != ECHILD) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1699 | bb_perror_msg("waitpid"); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1700 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1703 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1704 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 1705 | { |
| 1706 | pid_t p; |
| 1707 | int rcode = checkjobs(fg_pipe); |
| 1708 | /* Job finished, move the shell to the foreground */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1709 | p = getpgid(0); /* pgid of our process */ |
| 1710 | debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p); |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1711 | if (tcsetpgrp(interactive_fd, p) && errno != ENOTTY) |
| 1712 | bb_perror_msg("tcsetpgrp-4a"); |
| 1713 | return rcode; |
| 1714 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1715 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1716 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1717 | /* run_pipe() starts all the jobs, but doesn't wait for anything |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1718 | * to finish. See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1719 | * |
| 1720 | * return code is normally -1, when the caller has to wait for children |
| 1721 | * to finish to determine the exit status of the pipe. If the pipe |
| 1722 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1723 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1724 | * return value. |
| 1725 | * |
| 1726 | * The input of the pipe is always stdin, the output is always |
| 1727 | * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus, |
| 1728 | * because it tries to avoid running the command substitution in |
| 1729 | * subshell, when that is in fact necessary. The subshell process |
| 1730 | * now has its stdout directed to the input of the appropriate pipe, |
| 1731 | * so this routine is noticeably simpler. |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1732 | * |
| 1733 | * Returns -1 only if started some children. IOW: we have to |
| 1734 | * mask out retvals of builtins etc with 0xff! |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1735 | */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1736 | static int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1737 | { |
| 1738 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1739 | int nextin; |
| 1740 | int pipefds[2]; /* pipefds[0] is for reading */ |
Rob Landley | 53702e5 | 2006-07-19 21:43:53 +0000 | [diff] [blame] | 1741 | struct child_prog *child; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 1742 | const struct built_in_command *x; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 1743 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1744 | /* it is not always needed, but we aim to smaller code */ |
| 1745 | int squirrel[] = { -1, -1, -1 }; |
| 1746 | int rcode; |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1747 | const int single_fg = (pi->num_progs == 1 && pi->followup != PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1748 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1749 | debug_printf_exec("run_pipe start: single_fg=%d\n", single_fg); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 1750 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1751 | #if ENABLE_HUSH_JOB |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 1752 | pi->pgrp = -1; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1753 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1754 | pi->running_progs = 1; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1755 | pi->stopped_progs = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1756 | |
| 1757 | /* Check if this is a simple builtin (not part of a pipe). |
| 1758 | * Builtins within pipes have to fork anyway, and are handled in |
| 1759 | * pseudo_exec. "echo foo | read bar" doesn't work on bash, either. |
| 1760 | */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1761 | child = &(pi->progs[0]); |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1762 | if (single_fg && child->group && child->subshell == 0) { |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 1763 | debug_printf("non-subshell grouping\n"); |
| 1764 | setup_redirects(child, squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1765 | debug_printf_exec(": run_list\n"); |
| 1766 | rcode = run_list(child->group) & 0xff; |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 1767 | restore_redirects(squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1768 | debug_printf_exec("run_pipe return %d\n", rcode); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 1769 | if (pi->pi_inverted) |
| 1770 | rcode = !rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1771 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1774 | if (single_fg && child->argv != NULL) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1775 | char **argv_expanded; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1776 | char **argv = child->argv; |
| 1777 | |
| 1778 | for (i = 0; is_assignment(argv[i]); i++) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1779 | continue; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1780 | if (i != 0 && argv[i] == NULL) { |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1781 | /* assignments, but no command: set the local environment */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1782 | for (i = 0; argv[i] != NULL; i++) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 1783 | debug_printf("local environment set: %s\n", argv[i]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1784 | p = expand_string_to_string(argv[i]); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 1785 | set_local_var(p, 0); |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1786 | } |
| 1787 | return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */ |
| 1788 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1789 | for (i = 0; is_assignment(argv[i]); i++) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1790 | p = expand_string_to_string(argv[i]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1791 | putenv(p); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1792 | //FIXME: do we leak p?! |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 1793 | } |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 1794 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1795 | if (strcmp(argv[i], x->cmd) == 0) { |
| 1796 | if (x->function == builtin_exec && argv[i+1] == NULL) { |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1797 | debug_printf("magic exec\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1798 | setup_redirects(child, NULL); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1799 | return EXIT_SUCCESS; |
| 1800 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1801 | debug_printf("builtin inline %s\n", argv[0]); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1802 | /* XXX setup_redirects acts on file descriptors, not FILEs. |
| 1803 | * This is perfect for work that comes after exec(). |
| 1804 | * Is it really safe for inline use? Experimentally, |
| 1805 | * things seem to work with glibc. */ |
| 1806 | setup_redirects(child, squirrel); |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1807 | debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv[i+1]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1808 | argv_expanded = expand_strvec_to_strvec(argv + i); |
| 1809 | rcode = x->function(argv_expanded) & 0xff; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1810 | free(argv_expanded); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1811 | restore_redirects(squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1812 | debug_printf_exec("run_pipe return %d\n", rcode); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 1813 | if (pi->pi_inverted) |
| 1814 | rcode = !rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1815 | return rcode; |
| 1816 | } |
| 1817 | } |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1818 | #if ENABLE_FEATURE_SH_STANDALONE |
| 1819 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 1820 | int a = find_applet_by_name(argv[i]); |
| 1821 | if (a >= 0 && APPLET_IS_NOFORK(a)) { |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1822 | setup_redirects(child, squirrel); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1823 | save_nofork_data(&nofork_save); |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 1824 | argv_expanded = argv + i; |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1825 | argv_expanded = expand_strvec_to_strvec(argv + i); |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 1826 | 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] | 1827 | rcode = run_nofork_applet_prime(&nofork_save, a, argv_expanded); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1828 | free(argv_expanded); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1829 | restore_redirects(squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1830 | debug_printf_exec("run_pipe return %d\n", rcode); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 1831 | if (pi->pi_inverted) |
| 1832 | rcode = !rcode; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1833 | return rcode; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1834 | } |
| 1835 | } |
| 1836 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1839 | /* Disable job control signals for shell (parent) and |
| 1840 | * for initial child code after fork */ |
| 1841 | set_jobctrl_sighandler(SIG_IGN); |
| 1842 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1843 | /* Going to fork a child per each pipe member */ |
| 1844 | pi->running_progs = 0; |
| 1845 | nextin = 0; |
| 1846 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1847 | for (i = 0; i < pi->num_progs; i++) { |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1848 | #if !BB_MMU |
| 1849 | char **ptrs2free = NULL; |
| 1850 | #endif |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1851 | child = &(pi->progs[i]); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1852 | if (child->argv) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1853 | 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] | 1854 | #if !BB_MMU |
| 1855 | ptrs2free = alloc_ptrs(child->argv); |
| 1856 | #endif |
| 1857 | } else |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1858 | debug_printf_exec(": pipe member with no argv\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1859 | |
| 1860 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1861 | pipefds[0] = 0; |
| 1862 | pipefds[1] = 1; |
| 1863 | if ((i + 1) < pi->num_progs) |
| 1864 | xpipe(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1865 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1866 | child->pid = BB_MMU ? fork() : vfork(); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1867 | if (!child->pid) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 1868 | if (ENABLE_HUSH_JOB) |
| 1869 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1870 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1871 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1872 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1873 | if (run_list_level == 1 && interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1874 | pid_t pgrp; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1875 | /* Don't do pgrp restore anymore on fatal signals */ |
| 1876 | set_fatal_sighandler(SIG_DFL); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1877 | pgrp = pi->pgrp; |
| 1878 | if (pgrp < 0) /* true for 1st process only */ |
| 1879 | pgrp = getpid(); |
| 1880 | if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1881 | /* We do it in *every* child, not just first, |
| 1882 | * to avoid races */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1883 | tcsetpgrp(interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1884 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1885 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1886 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1887 | xmove_fd(nextin, 0); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1888 | xmove_fd(pipefds[1], 1); /* write end */ |
| 1889 | if (pipefds[0] > 1) |
| 1890 | close(pipefds[0]); /* read end */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1891 | /* Like bash, explicit redirects override pipes, |
| 1892 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1893 | setup_redirects(child, NULL); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1894 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1895 | /* Restore default handlers just prior to exec */ |
| 1896 | set_jobctrl_sighandler(SIG_DFL); |
| 1897 | set_misc_sighandler(SIG_DFL); |
| 1898 | signal(SIGCHLD, SIG_DFL); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1899 | pseudo_exec(ptrs2free, child); /* does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1900 | } |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1901 | #if !BB_MMU |
| 1902 | free_strings(ptrs2free); |
| 1903 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1904 | if (child->pid < 0) { /* [v]fork failed */ |
| 1905 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1906 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1907 | } else { |
| 1908 | pi->running_progs++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1909 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1910 | /* Second and next children need to know pid of first one */ |
| 1911 | if (pi->pgrp < 0) |
| 1912 | pi->pgrp = child->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1913 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1914 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1915 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1916 | if (i) |
| 1917 | close(nextin); |
| 1918 | if ((i + 1) < pi->num_progs) |
| 1919 | close(pipefds[1]); /* write end */ |
| 1920 | /* Pass read (output) pipe end to next iteration */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1921 | nextin = pipefds[0]; |
| 1922 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1923 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1924 | if (!pi->running_progs) { |
| 1925 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 1926 | return 1; |
| 1927 | } |
| 1928 | |
| 1929 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->running_progs); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1930 | return -1; |
| 1931 | } |
| 1932 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 1933 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1934 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 1935 | { |
| 1936 | static const char *PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1937 | [PIPE_SEQ] = "SEQ", |
| 1938 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1939 | [PIPE_OR ] = "OR" , |
| 1940 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1941 | }; |
| 1942 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1943 | [RES_NONE ] = "NONE" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 1944 | #if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1945 | [RES_IF ] = "IF" , |
| 1946 | [RES_THEN ] = "THEN" , |
| 1947 | [RES_ELIF ] = "ELIF" , |
| 1948 | [RES_ELSE ] = "ELSE" , |
| 1949 | [RES_FI ] = "FI" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 1950 | #endif |
| 1951 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1952 | [RES_FOR ] = "FOR" , |
| 1953 | [RES_WHILE] = "WHILE", |
| 1954 | [RES_UNTIL] = "UNTIL", |
| 1955 | [RES_DO ] = "DO" , |
| 1956 | [RES_DONE ] = "DONE" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1957 | [RES_IN ] = "IN" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 1958 | #endif |
| 1959 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1960 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1961 | }; |
| 1962 | |
| 1963 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 1964 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1965 | pin = 0; |
| 1966 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 1967 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 1968 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1969 | prn = 0; |
| 1970 | while (prn < pi->num_progs) { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 1971 | struct child_prog *child = &pi->progs[prn]; |
| 1972 | char **argv = child->argv; |
| 1973 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1974 | fprintf(stderr, "%*s prog %d", lvl*2, "", prn); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 1975 | if (child->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1976 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 1977 | (child->subshell ? "()" : "{}"), |
| 1978 | argv); |
| 1979 | debug_print_tree(child->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1980 | prn++; |
| 1981 | continue; |
| 1982 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1983 | if (argv) while (*argv) { |
| 1984 | fprintf(stderr, " '%s'", *argv); |
| 1985 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 1986 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 1987 | fprintf(stderr, "\n"); |
| 1988 | prn++; |
| 1989 | } |
| 1990 | pi = pi->next; |
| 1991 | pin++; |
| 1992 | } |
| 1993 | } |
| 1994 | #endif |
| 1995 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1996 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 1997 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1998 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1999 | { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2000 | struct pipe *rpipe; |
| 2001 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2002 | char *for_varname = NULL; |
| 2003 | char **for_lcur = NULL; |
| 2004 | char **for_list = NULL; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2005 | int flag_rep = 0; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2006 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2007 | int flag_skip = 1; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2008 | int rcode = 0; /* probably for gcc only */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2009 | int flag_restore = 0; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2010 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2011 | 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] | 2012 | #else |
| 2013 | enum { if_code = 0, next_if_code = 0 }; |
| 2014 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame^] | 2015 | // TODO: rword and ->res_word are not needed if !LOOPS and !IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2016 | reserved_style rword; |
| 2017 | reserved_style skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 2018 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2019 | 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] | 2020 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2021 | #if ENABLE_HUSH_LOOPS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2022 | /* check syntax for "for" */ |
| 2023 | for (rpipe = pi; rpipe; rpipe = rpipe->next) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2024 | if ((rpipe->res_word == RES_IN || rpipe->res_word == RES_FOR) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2025 | && (rpipe->next == NULL) |
| 2026 | ) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 2027 | syntax("malformed for"); /* no IN or no commands after IN */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2028 | 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] | 2029 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2030 | } |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2031 | if ((rpipe->res_word == RES_IN && rpipe->next->res_word == RES_IN && rpipe->next->progs[0].argv != NULL) |
| 2032 | || (rpipe->res_word == RES_FOR && rpipe->next->res_word != RES_IN) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2033 | ) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2034 | /* TODO: what is tested in the first condition? */ |
Denis Vlasenko | 6eaf8de | 2008-06-17 12:09:21 +0000 | [diff] [blame] | 2035 | syntax("malformed for"); /* 2nd condition: FOR not followed by IN */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2036 | 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] | 2037 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2038 | } |
| 2039 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2040 | #else |
| 2041 | rpipe = NULL; |
| 2042 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2043 | |
| 2044 | #if ENABLE_HUSH_JOB |
| 2045 | /* Example of nested list: "while true; do { sleep 1 | exit 2; } done". |
| 2046 | * We are saving state before entering outermost list ("while...done") |
| 2047 | * so that ctrl-Z will correctly background _entire_ outermost list, |
| 2048 | * not just a part of it (like "sleep 1 | exit 2") */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 2049 | if (++run_list_level == 1 && interactive_fd) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2050 | if (sigsetjmp(toplevel_jb, 1)) { |
| 2051 | /* ctrl-Z forked and we are parent; or ctrl-C. |
| 2052 | * Sighandler has longjmped us here */ |
| 2053 | signal(SIGINT, SIG_IGN); |
| 2054 | signal(SIGTSTP, SIG_IGN); |
| 2055 | /* Restore level (we can be coming from deep inside |
| 2056 | * nested levels) */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 2057 | run_list_level = 1; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2058 | #if ENABLE_FEATURE_SH_STANDALONE |
| 2059 | if (nofork_save.saved) { /* if save area is valid */ |
| 2060 | debug_printf_jobs("exiting nofork early\n"); |
| 2061 | restore_nofork_data(&nofork_save); |
| 2062 | } |
| 2063 | #endif |
| 2064 | if (ctrl_z_flag) { |
| 2065 | /* ctrl-Z has forked and stored pid of the child in pi->pid. |
| 2066 | * Remember this child as background job */ |
| 2067 | insert_bg_job(pi); |
| 2068 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2069 | /* ctrl-C. We just stop doing whatever we were doing */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 2070 | bb_putchar('\n'); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2071 | } |
| 2072 | rcode = 0; |
| 2073 | goto ret; |
| 2074 | } |
| 2075 | /* ctrl-Z handler will store pid etc in pi */ |
| 2076 | toplevel_list = pi; |
| 2077 | ctrl_z_flag = 0; |
| 2078 | #if ENABLE_FEATURE_SH_STANDALONE |
| 2079 | nofork_save.saved = 0; /* in case we will run a nofork later */ |
| 2080 | #endif |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 2081 | signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2082 | signal(SIGINT, handler_ctrl_c); |
| 2083 | } |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2084 | #endif /* JOB */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2085 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2086 | for (; pi; pi = flag_restore ? rpipe : pi->next) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2087 | rword = pi->res_word; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2088 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2089 | if (rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2090 | flag_restore = 0; |
| 2091 | if (!rpipe) { |
| 2092 | flag_rep = 0; |
| 2093 | rpipe = pi; |
| 2094 | } |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2095 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2096 | #endif |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2097 | debug_printf_exec(": rword=%d if_code=%d next_if_code=%d skip_more=%d\n", |
| 2098 | rword, if_code, next_if_code, skip_more_for_this_rword); |
| 2099 | if (rword == skip_more_for_this_rword && flag_skip) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2100 | if (pi->followup == PIPE_SEQ) |
| 2101 | flag_skip = 0; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2102 | continue; |
| 2103 | } |
| 2104 | flag_skip = 1; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2105 | skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2106 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2107 | if (rword == RES_THEN || rword == RES_ELSE) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2108 | if_code = next_if_code; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2109 | if (rword == RES_THEN && if_code) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2110 | continue; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2111 | if (rword == RES_ELSE && !if_code) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2112 | continue; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2113 | if (rword == RES_ELIF && !if_code) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2114 | break; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2115 | #endif |
| 2116 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2117 | if (rword == RES_FOR && pi->num_progs) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2118 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2119 | /* first loop through for */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2120 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2121 | if (!pi->next->progs->argv) |
| 2122 | continue; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2123 | /* create list of variable values */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 2124 | debug_print_strings("for_list made from", pi->next->progs->argv); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2125 | for_list = expand_strvec_to_strvec(pi->next->progs->argv); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 2126 | debug_print_strings("for_list", for_list); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2127 | for_lcur = for_list; |
| 2128 | for_varname = pi->progs->argv[0]; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2129 | pi->progs->argv[0] = NULL; |
| 2130 | flag_rep = 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2131 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2132 | free(pi->progs->argv[0]); |
| 2133 | if (!*for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2134 | /* for loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2135 | free(for_list); |
| 2136 | for_lcur = NULL; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2137 | flag_rep = 0; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2138 | pi->progs->argv[0] = for_varname; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2139 | continue; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2140 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2141 | /* insert next value from for_lcur */ |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2142 | /* vda: does it need escaping? */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2143 | pi->progs->argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2144 | } |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2145 | if (rword == RES_IN) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2146 | continue; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2147 | if (rword == RES_DO) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2148 | if (!flag_rep) |
| 2149 | continue; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 2150 | } |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2151 | if (rword == RES_DONE) { |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2152 | if (flag_rep) { |
| 2153 | flag_restore = 1; |
| 2154 | } else { |
| 2155 | rpipe = NULL; |
| 2156 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2157 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2158 | #endif |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2159 | if (pi->num_progs == 0) |
| 2160 | continue; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2161 | debug_printf_exec(": run_pipe with %d members\n", pi->num_progs); |
| 2162 | rcode = run_pipe(pi); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2163 | if (rcode != -1) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2164 | /* We only ran a builtin: rcode was set by the return value |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2165 | * of run_pipe(), and we don't need to wait for anything. */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2166 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2167 | /* What does bash do with attempts to background builtins? */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2168 | /* Even bash 3.2 doesn't do that well with nested bg: |
| 2169 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2170 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2171 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 2172 | if (run_list_level == 1) |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2173 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2174 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2175 | rcode = EXIT_SUCCESS; |
| 2176 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2177 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 2178 | if (run_list_level == 1 && interactive_fd) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2179 | /* waits for completion, then fg's main shell */ |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 2180 | rcode = checkjobs_and_fg_shell(pi); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2181 | } else |
| 2182 | #endif |
| 2183 | { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2184 | /* this one just waits for completion */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 2185 | rcode = checkjobs(pi); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2186 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2187 | debug_printf_exec(": checkjobs returned %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2188 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2189 | debug_printf_exec(": setting last_return_code=%d\n", rcode); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2190 | last_return_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2191 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2192 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2193 | next_if_code = rcode; /* can be overwritten a number of times */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2194 | #endif |
| 2195 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2196 | if (rword == RES_WHILE) |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2197 | flag_rep = !last_return_code; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2198 | if (rword == RES_UNTIL) |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2199 | flag_rep = last_return_code; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2200 | #endif |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2201 | if ((rcode == EXIT_SUCCESS && pi->followup == PIPE_OR) |
| 2202 | || (rcode != EXIT_SUCCESS && pi->followup == PIPE_AND) |
| 2203 | ) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2204 | skip_more_for_this_rword = rword; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2205 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 2206 | checkjobs(NULL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2207 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2208 | |
| 2209 | #if ENABLE_HUSH_JOB |
| 2210 | if (ctrl_z_flag) { |
| 2211 | /* ctrl-Z forked somewhere in the past, we are the child, |
| 2212 | * and now we completed running the list. Exit. */ |
| 2213 | exit(rcode); |
| 2214 | } |
| 2215 | ret: |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 2216 | if (!--run_list_level && interactive_fd) { |
| 2217 | signal(SIGTSTP, SIG_IGN); |
| 2218 | signal(SIGINT, SIG_IGN); |
| 2219 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2220 | #endif |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2221 | 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] | 2222 | return rcode; |
| 2223 | } |
| 2224 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2225 | /* return code is the exit status of the pipe */ |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2226 | static int free_pipe(struct pipe *pi, int indent) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2227 | { |
| 2228 | char **p; |
| 2229 | struct child_prog *child; |
| 2230 | struct redir_struct *r, *rnext; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2231 | int a, i, ret_code = 0; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 2232 | |
| 2233 | if (pi->stopped_progs > 0) |
| 2234 | return ret_code; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2235 | debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid()); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2236 | for (i = 0; i < pi->num_progs; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2237 | child = &pi->progs[i]; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2238 | debug_printf_clean("%s command %d:\n", indenter(indent), i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2239 | if (child->argv) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2240 | for (a = 0, p = child->argv; *p; a++, p++) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2241 | debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2242 | } |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 2243 | free_strings(child->argv); |
| 2244 | child->argv = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2245 | } else if (child->group) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2246 | 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] | 2247 | ret_code = free_pipe_list(child->group, indent+3); |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2248 | debug_printf_clean("%s end group\n", indenter(indent)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2249 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2250 | debug_printf_clean("%s (nil)\n", indenter(indent)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2251 | } |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2252 | for (r = child->redirects; r; r = rnext) { |
Denis Vlasenko | 211b59b | 2008-06-23 16:28:53 +0000 | [diff] [blame] | 2253 | 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] | 2254 | if (r->dup == -1) { |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2255 | /* guard against the case >$FOO, where foo is unset or blank */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2256 | if (r->rd_filename) { |
| 2257 | debug_printf_clean(" %s\n", r->rd_filename); |
| 2258 | free(r->rd_filename); |
| 2259 | r->rd_filename = NULL; |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2260 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2261 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2262 | debug_printf_clean("&%d\n", r->dup); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2263 | } |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2264 | rnext = r->next; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2265 | free(r); |
| 2266 | } |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2267 | child->redirects = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2268 | } |
| 2269 | 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] | 2270 | pi->progs = NULL; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2271 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2272 | free(pi->cmdtext); |
| 2273 | pi->cmdtext = NULL; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2274 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2275 | return ret_code; |
| 2276 | } |
| 2277 | |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2278 | static int free_pipe_list(struct pipe *head, int indent) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2279 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2280 | int rcode = 0; /* if list has no members */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2281 | struct pipe *pi, *next; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2282 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2283 | for (pi = head; pi; pi = next) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2284 | debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2285 | rcode = free_pipe(pi, indent); |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2286 | 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] | 2287 | next = pi->next; |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2288 | /*pi->next = NULL;*/ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2289 | free(pi); |
| 2290 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2291 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2295 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2296 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2297 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2298 | debug_printf_exec("run_and_free_list entered\n"); |
| 2299 | if (!fake_mode) { |
| 2300 | debug_printf_exec(": run_list with %d members\n", pi->num_progs); |
| 2301 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2302 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2303 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2304 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2305 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2306 | free_pipe_list(pi, /* indent: */ 0); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2307 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2308 | return rcode; |
| 2309 | } |
| 2310 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2311 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2312 | /* expand_strvec_to_strvec() takes a list of strings, expands |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2313 | * all variable references within and returns a pointer to |
| 2314 | * a list of expanded strings, possibly with larger number |
| 2315 | * of strings. (Think VAR="a b"; echo $VAR). |
| 2316 | * This new list is allocated as a single malloc block. |
| 2317 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 2318 | * followed by strings themself. |
| 2319 | * Caller can deallocate entire list by single free(list). */ |
| 2320 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2321 | /* Store given string, finalizing the word and starting new one whenever |
| 2322 | * we encounter ifs char(s). This is used for expanding variable values. |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2323 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2324 | 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] | 2325 | { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2326 | while (1) { |
| 2327 | int word_len = strcspn(str, ifs); |
| 2328 | if (word_len) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2329 | if (output->o_quote || !output->o_glob) |
| 2330 | o_addQstr(output, str, word_len); |
| 2331 | else /* protect backslashes against globbing up :) */ |
| 2332 | o_addstr_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2333 | str += word_len; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2334 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2335 | if (!*str) /* EOL - do not finalize word */ |
| 2336 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2337 | o_addchr(output, '\0'); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2338 | debug_print_list("expand_on_ifs", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2339 | n = o_save_ptr(output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2340 | str += strspn(str, ifs); /* skip ifs chars */ |
| 2341 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2342 | debug_print_list("expand_on_ifs[1]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2343 | return n; |
| 2344 | } |
| 2345 | |
| 2346 | /* Expand all variable references in given string, adding words to list[] |
| 2347 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 2348 | * to be filled). This routine is extremely tricky: has to deal with |
| 2349 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 2350 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2351 | /* NB: another bug is that we cannot detect empty strings yet: |
| 2352 | * "" or $empty"" expands to zero words, has to expand to empty word */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2353 | 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] | 2354 | { |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2355 | /* or_mask is either 0 (normal case) or 0x80 |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2356 | * (expansion of right-hand side of assignment == 1-element expand. |
| 2357 | * 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] | 2358 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2359 | char first_ch, ored_ch; |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2360 | int i; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2361 | const char *val; |
| 2362 | char *p; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2363 | |
| 2364 | ored_ch = 0; |
| 2365 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2366 | debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2367 | debug_print_list("expand_vars_to_list", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2368 | n = o_save_ptr(output, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2369 | debug_print_list("expand_vars_to_list[0]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2370 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2371 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2372 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2373 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2374 | #endif |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2375 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2376 | o_addstr(output, arg, p - arg); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2377 | debug_print_list("expand_vars_to_list[1]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2378 | arg = ++p; |
| 2379 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2380 | |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2381 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2382 | ored_ch |= first_ch; |
| 2383 | val = NULL; |
| 2384 | switch (first_ch & 0x7f) { |
| 2385 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 2386 | case '$': /* pid */ |
Denis Vlasenko | 16c2fea | 2008-06-17 12:28:44 +0000 | [diff] [blame] | 2387 | val = utoa(root_pid); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2388 | break; |
| 2389 | case '!': /* bg pid */ |
| 2390 | val = last_bg_pid ? utoa(last_bg_pid) : (char*)""; |
| 2391 | break; |
| 2392 | case '?': /* exitcode */ |
| 2393 | val = utoa(last_return_code); |
| 2394 | break; |
| 2395 | case '#': /* argc */ |
| 2396 | val = utoa(global_argc ? global_argc-1 : 0); |
| 2397 | break; |
| 2398 | case '*': |
| 2399 | case '@': |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2400 | i = 1; |
Denis Vlasenko | c3c6659 | 2007-11-24 00:22:42 +0000 | [diff] [blame] | 2401 | if (!global_argv[i]) |
| 2402 | break; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2403 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2404 | smallint sv = output->o_quote; |
| 2405 | /* unquoted var's contents should be globbed, so don't quote */ |
| 2406 | output->o_quote = 0; |
Denis Vlasenko | c3c6659 | 2007-11-24 00:22:42 +0000 | [diff] [blame] | 2407 | while (global_argv[i]) { |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2408 | n = expand_on_ifs(output, n, global_argv[i]); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2409 | 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] | 2410 | if (global_argv[i++][0] && global_argv[i]) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2411 | /* this argv[] is not empty and not last: |
| 2412 | * put terminating NUL, start new word */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2413 | o_addchr(output, '\0'); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2414 | debug_print_list("expand_vars_to_list[2]", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2415 | n = o_save_ptr(output, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2416 | debug_print_list("expand_vars_to_list[3]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2417 | } |
| 2418 | } |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2419 | output->o_quote = sv; |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2420 | } else |
| 2421 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2422 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2423 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2424 | while (1) { |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2425 | o_addQstr(output, global_argv[i], strlen(global_argv[i])); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2426 | if (++i >= global_argc) |
| 2427 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2428 | o_addchr(output, '\0'); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2429 | debug_print_list("expand_vars_to_list[4]", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2430 | n = o_save_ptr(output, n); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2431 | } |
| 2432 | } else { /* quoted $*: add as one word */ |
Denis Vlasenko | c3c6659 | 2007-11-24 00:22:42 +0000 | [diff] [blame] | 2433 | while (1) { |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2434 | o_addQstr(output, global_argv[i], strlen(global_argv[i])); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2435 | if (!global_argv[++i]) |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2436 | break; |
| 2437 | if (ifs[0]) |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2438 | o_addchr(output, ifs[0]); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2439 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2440 | } |
| 2441 | break; |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2442 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 2443 | /* "Empty variable", used to make "" etc to not disappear */ |
| 2444 | arg++; |
| 2445 | ored_ch = 0x80; |
| 2446 | break; |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2447 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2448 | case '`': { /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2449 | struct in_str input; |
| 2450 | *p = '\0'; |
| 2451 | arg++; |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2452 | //TODO: can we just stuff it into "output" directly? |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2453 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2454 | setup_string_in_str(&input, arg); |
| 2455 | process_command_subs(&subst_result, &input, NULL); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2456 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2457 | val = subst_result.data; |
| 2458 | goto store_val; |
| 2459 | } |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2460 | #endif |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2461 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2462 | *p = '\0'; |
| 2463 | arg[0] = first_ch & 0x7f; |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2464 | if (isdigit(arg[0])) { |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2465 | i = xatoi_u(arg); |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2466 | if (i < global_argc) |
| 2467 | val = global_argv[i]; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2468 | /* else val remains NULL: $N with too big N */ |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2469 | } else |
| 2470 | val = lookup_param(arg); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2471 | arg[0] = first_ch; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2472 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2473 | store_val: |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2474 | #endif |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2475 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2476 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2477 | 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] | 2478 | if (val) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2479 | /* unquoted var's contents should be globbed, so don't quote */ |
| 2480 | smallint sv = output->o_quote; |
| 2481 | output->o_quote = 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2482 | n = expand_on_ifs(output, n, val); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2483 | val = NULL; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2484 | output->o_quote = sv; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2485 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2486 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2487 | 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] | 2488 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2489 | } |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2490 | if (val) { |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2491 | o_addQstr(output, val, strlen(val)); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2492 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2493 | |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2494 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2495 | o_free(&subst_result); |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2496 | #endif |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2497 | arg = ++p; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2498 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 2499 | |
Denis Vlasenko | 2e76c3f | 2008-06-10 18:27:50 +0000 | [diff] [blame] | 2500 | if (arg[0]) { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2501 | debug_print_list("expand_vars_to_list[a]", output, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2502 | /* this part is literal, and it was already pre-quoted |
| 2503 | * if needed (much earlier), do not use o_addQstr here! */ |
| 2504 | o_addstr(output, arg, strlen(arg) + 1); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2505 | debug_print_list("expand_vars_to_list[b]", output, n); |
Denis Vlasenko | 2e76c3f | 2008-06-10 18:27:50 +0000 | [diff] [blame] | 2506 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 2507 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 2508 | ) { |
| 2509 | n--; |
| 2510 | /* allow to reuse list[n] later without re-growth */ |
| 2511 | output->has_empty_slot = 1; |
| 2512 | } else { |
| 2513 | o_addchr(output, '\0'); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2514 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2515 | return n; |
| 2516 | } |
| 2517 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2518 | static char **expand_variables(char **argv, int or_mask) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2519 | { |
| 2520 | int n; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2521 | char **list; |
| 2522 | char **v; |
| 2523 | o_string output = NULL_O_STRING; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2524 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2525 | if (or_mask & 0x100) { |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2526 | output.o_quote = 1; /* protect against globbing for "$var" */ |
| 2527 | /* (unquoted $var will temporarily switch it off) */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2528 | output.o_glob = 1; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2529 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2530 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2531 | n = 0; |
| 2532 | v = argv; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2533 | while (*v) { |
| 2534 | n = expand_vars_to_list(&output, n, *v, (char)or_mask); |
| 2535 | v++; |
| 2536 | } |
| 2537 | debug_print_list("expand_variables", &output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2538 | |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 2539 | /* output.data (malloced in one block) gets returned in "list" */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2540 | list = o_finalize_list(&output, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2541 | debug_print_strings("expand_variables[1]", list); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2542 | return list; |
| 2543 | } |
| 2544 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2545 | static char **expand_strvec_to_strvec(char **argv) |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2546 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2547 | return expand_variables(argv, 0x100); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2550 | /* Used for expansion of right hand of assignments */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 2551 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 2552 | * "v=/bin/c*" */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2553 | static char *expand_string_to_string(const char *str) |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2554 | { |
| 2555 | char *argv[2], **list; |
| 2556 | |
| 2557 | argv[0] = (char*)str; |
| 2558 | argv[1] = NULL; |
| 2559 | list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2560 | if (ENABLE_HUSH_DEBUG) |
| 2561 | if (!list[0] || list[1]) |
| 2562 | bb_error_msg_and_die("BUG in varexp2"); |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2563 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 2564 | strcpy((char*)list, list[0]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2565 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 2566 | return (char*)list; |
| 2567 | } |
| 2568 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2569 | /* Used for "eval" builtin */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2570 | static char* expand_strvec_to_string(char **argv) |
| 2571 | { |
| 2572 | char **list; |
| 2573 | |
| 2574 | list = expand_variables(argv, 0x80); |
| 2575 | /* Convert all NULs to spaces */ |
| 2576 | if (list[0]) { |
| 2577 | int n = 1; |
| 2578 | while (list[n]) { |
| 2579 | if (ENABLE_HUSH_DEBUG) |
| 2580 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 2581 | bb_error_msg_and_die("BUG in varexp3"); |
| 2582 | list[n][-1] = ' '; /* TODO: or to ifs[0]? */ |
| 2583 | n++; |
| 2584 | } |
| 2585 | } |
| 2586 | strcpy((char*)list, list[0]); |
| 2587 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2588 | return (char*)list; |
| 2589 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2590 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2591 | |
| 2592 | /* Used to get/check local shell variables */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2593 | static struct variable *get_local_var(const char *name) |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2594 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2595 | struct variable *cur; |
| 2596 | int len; |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2597 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2598 | if (!name) |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2599 | return NULL; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2600 | len = strlen(name); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2601 | for (cur = top_var; cur; cur = cur->next) { |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2602 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2603 | return cur; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 2604 | } |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2605 | return NULL; |
| 2606 | } |
| 2607 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2608 | /* str holds "NAME=VAL" and is expected to be malloced. |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2609 | * We take ownership of it. */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2610 | static int set_local_var(char *str, int flg_export) |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2611 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2612 | struct variable *cur; |
| 2613 | char *value; |
| 2614 | int name_len; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2615 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2616 | value = strchr(str, '='); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2617 | if (!value) { /* not expected to ever happen? */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2618 | free(str); |
Eric Andersen | 9978576 | 2001-05-22 21:37:48 +0000 | [diff] [blame] | 2619 | return -1; |
| 2620 | } |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2621 | |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2622 | name_len = value - str + 1; /* including '=' */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2623 | cur = top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */ |
| 2624 | while (1) { |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2625 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2626 | if (!cur->next) { |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2627 | /* Bail out. Note that now cur points |
| 2628 | * to last var in linked list */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2629 | break; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2630 | } |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2631 | cur = cur->next; |
| 2632 | continue; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2633 | } |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2634 | /* We found an existing var with this name */ |
| 2635 | *value = '\0'; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2636 | if (cur->flg_read_only) { |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2637 | bb_error_msg("%s: readonly variable", str); |
| 2638 | free(str); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2639 | return -1; |
| 2640 | } |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2641 | unsetenv(str); /* just in case */ |
| 2642 | *value = '='; |
| 2643 | if (strcmp(cur->varstr, str) == 0) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2644 | free_and_exp: |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2645 | free(str); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2646 | goto exp; |
| 2647 | } |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2648 | if (cur->max_len >= strlen(str)) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2649 | /* This one is from startup env, reuse space */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2650 | strcpy(cur->varstr, str); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2651 | goto free_and_exp; |
| 2652 | } |
| 2653 | /* max_len == 0 signifies "malloced" var, which we can |
| 2654 | * (and has to) free */ |
| 2655 | if (!cur->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2656 | free(cur->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2657 | cur->max_len = 0; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2658 | goto set_str_and_exp; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2659 | } |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2660 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2661 | /* Not found - create next variable struct */ |
| 2662 | cur->next = xzalloc(sizeof(*cur)); |
| 2663 | cur = cur->next; |
| 2664 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2665 | set_str_and_exp: |
| 2666 | cur->varstr = str; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2667 | exp: |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2668 | if (flg_export) |
| 2669 | cur->flg_export = 1; |
| 2670 | if (cur->flg_export) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2671 | return putenv(cur->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2672 | return 0; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2675 | static void unset_local_var(const char *name) |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2676 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2677 | struct variable *cur; |
| 2678 | struct variable *prev = prev; /* for gcc */ |
| 2679 | int name_len; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2680 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2681 | if (!name) |
| 2682 | return; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2683 | name_len = strlen(name); |
| 2684 | cur = top_var; |
| 2685 | while (cur) { |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2686 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2687 | if (cur->flg_read_only) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 2688 | bb_error_msg("%s: readonly variable", name); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2689 | return; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2690 | } |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2691 | /* prev is ok to use here because 1st variable, HUSH_VERSION, |
| 2692 | * is ro, and we cannot reach this code on the 1st pass */ |
| 2693 | prev->next = cur->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2694 | unsetenv(cur->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2695 | if (!cur->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2696 | free(cur->varstr); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2697 | free(cur); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2698 | return; |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2699 | } |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2700 | prev = cur; |
| 2701 | cur = cur->next; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2702 | } |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2705 | /* the src parameter allows us to peek forward to a possible &n syntax |
| 2706 | * for file descriptor duplication, e.g., "2>&1". |
| 2707 | * Return code is 0 normally, 1 if a syntax error is detected in src. |
| 2708 | * Resource errors (in xmalloc) cause the process to exit */ |
| 2709 | static int setup_redirect(struct p_context *ctx, int fd, redir_type style, |
| 2710 | struct in_str *input) |
| 2711 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2712 | struct child_prog *child = ctx->child; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2713 | struct redir_struct *redir = child->redirects; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2714 | struct redir_struct *last_redir = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2715 | |
| 2716 | /* 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] | 2717 | while (redir) { |
| 2718 | last_redir = redir; |
| 2719 | redir = redir->next; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2720 | } |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 2721 | redir = xzalloc(sizeof(struct redir_struct)); |
| 2722 | /* redir->next = NULL; */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2723 | /* redir->rd_filename = NULL; */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2724 | if (last_redir) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2725 | last_redir->next = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2726 | } else { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2727 | child->redirects = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2730 | redir->rd_type = style; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2731 | redir->fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2732 | |
| 2733 | debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip); |
| 2734 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2735 | /* Check for a '2>&1' type redirect */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2736 | redir->dup = redirect_dup_num(input); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2737 | if (redir->dup == -2) |
| 2738 | return 1; /* syntax error */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2739 | if (redir->dup != -1) { |
| 2740 | /* Erik had a check here that the file descriptor in question |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2741 | * is legit; I postpone that to "run time" |
| 2742 | * A "-" representation of "close me" shows up as a -3 here */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2743 | debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup); |
| 2744 | } else { |
| 2745 | /* We do _not_ try to open the file that src points to, |
| 2746 | * since we need to return and let src be expanded first. |
| 2747 | * Set ctx->pending_redirect, so we know what to do at the |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2748 | * end of the next parsed word. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2749 | ctx->pending_redirect = redir; |
| 2750 | } |
| 2751 | return 0; |
| 2752 | } |
| 2753 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 2754 | static struct pipe *new_pipe(void) |
| 2755 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2756 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2757 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2758 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2759 | if (RES_NONE) |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2760 | pi->res_word = RES_NONE; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2761 | return pi; |
| 2762 | } |
| 2763 | |
| 2764 | static void initialize_context(struct p_context *ctx) |
| 2765 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2766 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2767 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2768 | /* Create the memory for child, roughly: |
| 2769 | * ctx->pipe->progs = new struct child_prog; |
| 2770 | * ctx->pipe->progs[0].family = ctx->pipe; |
| 2771 | * ctx->child = &ctx->pipe->progs[0]; |
| 2772 | */ |
| 2773 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2776 | /* If a reserved word is found and processed, parse context is modified |
| 2777 | * and 1 is returned. |
| 2778 | * Handles if, then, elif, else, fi, for, while, until, do, done. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2779 | * case, function, and select are obnoxious, save those for later. |
| 2780 | */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2781 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2782 | static int reserved_word(const o_string *word, struct p_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2783 | { |
| 2784 | struct reserved_combo { |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2785 | char literal[7]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2786 | unsigned char res; |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2787 | int flag; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2788 | }; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2789 | enum { |
| 2790 | FLAG_END = (1 << RES_NONE ), |
| 2791 | #if ENABLE_HUSH_IF |
| 2792 | FLAG_IF = (1 << RES_IF ), |
| 2793 | FLAG_THEN = (1 << RES_THEN ), |
| 2794 | FLAG_ELIF = (1 << RES_ELIF ), |
| 2795 | FLAG_ELSE = (1 << RES_ELSE ), |
| 2796 | FLAG_FI = (1 << RES_FI ), |
| 2797 | #endif |
| 2798 | #if ENABLE_HUSH_LOOPS |
| 2799 | FLAG_FOR = (1 << RES_FOR ), |
| 2800 | FLAG_WHILE = (1 << RES_WHILE), |
| 2801 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 2802 | FLAG_DO = (1 << RES_DO ), |
| 2803 | FLAG_DONE = (1 << RES_DONE ), |
| 2804 | FLAG_IN = (1 << RES_IN ), |
| 2805 | #endif |
| 2806 | FLAG_START = (1 << RES_XXXX ), |
| 2807 | }; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2808 | /* Mostly a list of accepted follow-up reserved words. |
| 2809 | * FLAG_END means we are done with the sequence, and are ready |
| 2810 | * to turn the compound list into a command. |
| 2811 | * FLAG_START means the word must start a new compound list. |
| 2812 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2813 | static const struct reserved_combo reserved_list[] = { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2814 | #if ENABLE_HUSH_IF |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2815 | { "!", RES_NONE, 0 }, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2816 | { "if", RES_IF, FLAG_THEN | FLAG_START }, |
| 2817 | { "then", RES_THEN, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 2818 | { "elif", RES_ELIF, FLAG_THEN }, |
| 2819 | { "else", RES_ELSE, FLAG_FI }, |
| 2820 | { "fi", RES_FI, FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2821 | #endif |
| 2822 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2823 | { "for", RES_FOR, FLAG_IN | FLAG_START }, |
| 2824 | { "while", RES_WHILE, FLAG_DO | FLAG_START }, |
| 2825 | { "until", RES_UNTIL, FLAG_DO | FLAG_START }, |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2826 | { "in", RES_IN, FLAG_DO }, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2827 | { "do", RES_DO, FLAG_DONE }, |
| 2828 | { "done", RES_DONE, FLAG_END } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2829 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2830 | }; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 2831 | |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2832 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2833 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 2834 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2835 | if (strcmp(word->data, r->literal) != 0) |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2836 | continue; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2837 | debug_printf("found reserved word %s, res %d\n", r->literal, r->res); |
| 2838 | if (r->flag == 0) { /* '!' */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2839 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2840 | if (ctx->res_w == RES_IN) { |
| 2841 | /* 'for a in ! a b c; ...' - ! isn't a keyword here */ |
| 2842 | break; |
| 2843 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2844 | #endif |
| 2845 | if (ctx->ctx_inverted /* bash doesn't accept '! ! true' */ |
| 2846 | #if ENABLE_HUSH_LOOPS |
| 2847 | || ctx->res_w == RES_FOR /* example: 'for ! a' */ |
| 2848 | #endif |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2849 | ) { |
| 2850 | syntax(NULL); |
| 2851 | ctx->res_w = RES_SNTX; |
| 2852 | } |
| 2853 | ctx->ctx_inverted = 1; |
| 2854 | return 1; |
| 2855 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2856 | if (r->flag & FLAG_START) { |
| 2857 | struct p_context *new; |
| 2858 | debug_printf("push stack\n"); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2859 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2860 | if (ctx->res_w == RES_IN || ctx->res_w == RES_FOR) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 2861 | syntax("malformed for"); /* example: 'for if' */ |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2862 | ctx->res_w = RES_SNTX; |
Eric Andersen | af44a0e | 2001-04-27 07:26:12 +0000 | [diff] [blame] | 2863 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2864 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2865 | #endif |
| 2866 | new = xmalloc(sizeof(*new)); |
| 2867 | *new = *ctx; /* physical copy */ |
| 2868 | initialize_context(ctx); |
| 2869 | ctx->stack = new; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2870 | } else if (ctx->res_w == RES_NONE || !(ctx->old_flag & (1 << r->res))) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 2871 | syntax(NULL); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2872 | ctx->res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2873 | return 1; |
| 2874 | } |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2875 | ctx->res_w = r->res; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2876 | ctx->old_flag = r->flag; |
| 2877 | if (ctx->old_flag & FLAG_END) { |
| 2878 | struct p_context *old; |
| 2879 | debug_printf("pop stack\n"); |
| 2880 | done_pipe(ctx, PIPE_SEQ); |
| 2881 | old = ctx->stack; |
| 2882 | old->child->group = ctx->list_head; |
| 2883 | old->child->subshell = 0; |
| 2884 | *ctx = *old; /* physical copy */ |
| 2885 | free(old); |
| 2886 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2887 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2888 | } |
| 2889 | return 0; |
| 2890 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2891 | #else |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2892 | #define reserved_word(word, ctx) ((int)0) |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2893 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2894 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2895 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2896 | * Normal return is 0. Syntax errors return 1. */ |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2897 | static int done_word(o_string *word, struct p_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2898 | { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 2899 | struct child_prog *child = ctx->child; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2900 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2901 | /* If this word wasn't an assignment, next ones definitely |
| 2902 | * can't be assignments. Even if they look like ones. */ |
| 2903 | if (word->o_assignment != DEFINITELY_ASSIGNMENT) { |
| 2904 | word->o_assignment = NOT_ASSIGNMENT; |
| 2905 | } else { |
| 2906 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 2907 | } |
| 2908 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2909 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, child); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2910 | if (word->length == 0 && word->nonnull == 0) { |
| 2911 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 2912 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2913 | } |
| 2914 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2915 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 2916 | * only if run as "bash", not "sh" */ |
| 2917 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2918 | word->o_assignment = NOT_ASSIGNMENT; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2919 | debug_printf("word stored in rd_filename: '%s'\n", word->data); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2920 | } else { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2921 | if (child->group) { /* TODO: example how to trigger? */ |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 2922 | syntax(NULL); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 2923 | debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n"); |
| 2924 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2925 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2926 | if (!child->argv) { /* if it's the first word... */ |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2927 | debug_printf_parse(": checking '%s' for reserved-ness\n", word->data); |
| 2928 | if (reserved_word(word, ctx)) { |
| 2929 | o_reset(word); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2930 | word->o_assignment = NOT_ASSIGNMENT; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2931 | debug_printf_parse("done_word return %d\n", (ctx->res_w == RES_SNTX)); |
| 2932 | return (ctx->res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 2933 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2934 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2935 | if (word->nonnull /* we saw "xx" or 'xx' */ |
| 2936 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 2937 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
| 2938 | /* (otherwise it's "abc".... and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2939 | ) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2940 | /* Insert "empty variable" reference, this makes |
| 2941 | * e.g. "", $empty"" etc to not disappear */ |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2942 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 2943 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 2944 | } |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2945 | child->argv = add_malloced_string_to_strings(child->argv, xstrdup(word->data)); |
| 2946 | debug_print_strings("word appended to argv", child->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 2947 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2948 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2949 | o_reset(word); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2950 | ctx->pending_redirect = NULL; |
| 2951 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2952 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2953 | /* Force FOR to have just one word (variable name) */ |
| 2954 | if (ctx->res_w == RES_FOR) |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 2955 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2956 | #endif |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 2957 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2958 | return 0; |
| 2959 | } |
| 2960 | |
Denis Vlasenko | 6eaf8de | 2008-06-17 12:09:21 +0000 | [diff] [blame] | 2961 | /* Command (member of a pipe) is complete. The only possible error here |
| 2962 | * is out of memory, in which case xmalloc exits. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2963 | static int done_command(struct p_context *ctx) |
| 2964 | { |
| 2965 | /* The child is really already in the pipe structure, so |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2966 | * advance the pipe counter and make a new, null child. */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 2967 | struct pipe *pi = ctx->pipe; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2968 | struct child_prog *child = ctx->child; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2969 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2970 | if (child) { |
| 2971 | if (child->group == NULL |
| 2972 | && child->argv == NULL |
| 2973 | && child->redirects == NULL |
| 2974 | ) { |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 2975 | debug_printf_parse("done_command: skipping null cmd, num_progs=%d\n", pi->num_progs); |
| 2976 | return pi->num_progs; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2977 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2978 | pi->num_progs++; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2979 | debug_printf_parse("done_command: ++num_progs=%d\n", pi->num_progs); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2980 | } else { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2981 | 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] | 2982 | } |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2983 | |
| 2984 | /* Only real trickiness here is that the uncommitted |
| 2985 | * child structure is not counted in pi->num_progs. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2986 | pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1)); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2987 | child = &pi->progs[pi->num_progs]; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2988 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2989 | memset(child, 0, sizeof(*child)); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2990 | child->family = pi; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2991 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2992 | ctx->child = child; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2993 | /* but ctx->pipe and ctx->list_head remain unchanged */ |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2994 | |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 2995 | return pi->num_progs; /* used only for 0/nonzero check */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2998 | static void done_pipe(struct p_context *ctx, pipe_style type) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2999 | { |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3000 | int not_null; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3001 | |
| 3002 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3003 | not_null = done_command(ctx); /* implicit closure of previous command */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3004 | ctx->pipe->followup = type; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 3005 | ctx->pipe->res_word = ctx->res_w; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3006 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 3007 | ctx->ctx_inverted = 0; |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3008 | /* Without this check, even just <enter> on command line generates |
| 3009 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | 6eaf8de | 2008-06-17 12:09:21 +0000 | [diff] [blame] | 3010 | * IOW: it is safe to do it unconditionally. |
| 3011 | * RES_IN case is for "for a in; do ..." (empty IN set) |
| 3012 | * to work. */ |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame^] | 3013 | if (not_null USE_HUSH_LOOPS(|| ctx->pipe->res_word == RES_IN)) { |
Denis Vlasenko | 6eaf8de | 2008-06-17 12:09:21 +0000 | [diff] [blame] | 3014 | struct pipe *new_p = new_pipe(); |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3015 | ctx->pipe->next = new_p; |
| 3016 | ctx->pipe = new_p; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3017 | ctx->child = NULL; /* needed! */ |
| 3018 | /* Create the memory for child, roughly: |
| 3019 | * ctx->pipe->progs = new struct child_prog; |
| 3020 | * ctx->pipe->progs[0].family = ctx->pipe; |
| 3021 | * ctx->child = &ctx->pipe->progs[0]; |
| 3022 | */ |
| 3023 | done_command(ctx); |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3024 | } |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3025 | debug_printf_parse("done_pipe return\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3028 | /* 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] | 3029 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3030 | * Return either -2 (syntax error), -1 (no &), or the number found. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3031 | */ |
| 3032 | static int redirect_dup_num(struct in_str *input) |
| 3033 | { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3034 | int ch, d = 0, ok = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3035 | ch = i_peek(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3036 | if (ch != '&') return -1; |
| 3037 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3038 | i_getch(input); /* get the & */ |
| 3039 | ch = i_peek(input); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3040 | if (ch == '-') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3041 | i_getch(input); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3042 | return -3; /* "-" represents "close me" */ |
| 3043 | } |
| 3044 | while (isdigit(ch)) { |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3045 | d = d*10 + (ch-'0'); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3046 | ok = 1; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3047 | i_getch(input); |
| 3048 | ch = i_peek(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3049 | } |
| 3050 | if (ok) return d; |
| 3051 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3052 | bb_error_msg("ambiguous redirect"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3053 | return -2; |
| 3054 | } |
| 3055 | |
| 3056 | /* If a redirect is immediately preceded by a number, that number is |
| 3057 | * supposed to tell which file descriptor to redirect. This routine |
| 3058 | * looks for such preceding numbers. In an ideal world this routine |
| 3059 | * needs to handle all the following classes of redirects... |
| 3060 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3061 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3062 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3063 | * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo |
| 3064 | * A -1 output from this program means no valid number was found, so the |
| 3065 | * caller should use the appropriate default for this redirection. |
| 3066 | */ |
| 3067 | static int redirect_opt_num(o_string *o) |
| 3068 | { |
| 3069 | int num; |
| 3070 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3071 | if (o->length == 0) |
| 3072 | return -1; |
| 3073 | for (num = 0; num < o->length; num++) { |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3074 | if (!isdigit(o->data[num])) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3075 | return -1; |
| 3076 | } |
| 3077 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3078 | num = atoi(o->data); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3079 | o_reset(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3080 | return num; |
| 3081 | } |
| 3082 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3083 | #if ENABLE_HUSH_TICK |
"Vladimir N. Oleynik" | 19c3701 | 2005-09-22 14:33:15 +0000 | [diff] [blame] | 3084 | static FILE *generate_stream_from_list(struct pipe *head) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3085 | { |
| 3086 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3087 | int pid, channel[2]; |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3088 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 3089 | xpipe(channel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3090 | /* *** NOMMU WARNING *** */ |
| 3091 | /* By using vfork here, we suspend parent till child exits or execs. |
| 3092 | * If child will not do it before it fills the pipe, it can block forever |
| 3093 | * in write(STDOUT_FILENO), and parent (shell) will be also stuck. |
Denis Vlasenko | 3fe4f98 | 2008-06-09 16:02:39 +0000 | [diff] [blame] | 3094 | * Try this script: |
| 3095 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >TESTFILE |
| 3096 | * huge=`cat TESTFILE` # will block here forever |
| 3097 | * echo OK |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3098 | */ |
| 3099 | pid = BB_MMU ? fork() : vfork(); |
| 3100 | if (pid < 0) |
| 3101 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
| 3102 | if (pid == 0) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 3103 | if (ENABLE_HUSH_JOB) |
| 3104 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 3105 | close(channel[0]); /* NB: close _first_, then move fd! */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3106 | xmove_fd(channel[1], 1); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3107 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3108 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3109 | run_list_level = 1; |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3110 | #endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3111 | /* Process substitution is not considered to be usual |
| 3112 | * 'command execution'. |
| 3113 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. */ |
| 3114 | /* Not needed, we are relying on it being disabled |
| 3115 | * everywhere outside actual command execution. */ |
| 3116 | /*set_jobctrl_sighandler(SIG_IGN);*/ |
| 3117 | set_misc_sighandler(SIG_DFL); |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3118 | /* Freeing 'head' here would break NOMMU. */ |
| 3119 | _exit(run_list(head)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3120 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3121 | close(channel[1]); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3122 | pf = fdopen(channel[0], "r"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3123 | return pf; |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3124 | /* 'head' is freed by the caller */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3125 | } |
| 3126 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3127 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3128 | static int process_command_subs(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3129 | struct in_str *input, |
| 3130 | const char *subst_end) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3131 | { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3132 | int retcode, ch, eol_cnt; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3133 | o_string result = NULL_O_STRING; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3134 | struct p_context inner; |
| 3135 | FILE *p; |
| 3136 | struct in_str pipe_str; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3137 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3138 | initialize_context(&inner); |
| 3139 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3140 | /* Recursion to generate command */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3141 | retcode = parse_stream(&result, &inner, input, subst_end); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3142 | if (retcode != 0) |
| 3143 | return retcode; /* syntax error or EOF */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3144 | done_word(&result, &inner); |
| 3145 | done_pipe(&inner, PIPE_SEQ); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3146 | o_free(&result); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3147 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3148 | p = generate_stream_from_list(inner.list_head); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3149 | if (p == NULL) |
| 3150 | return 1; |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 3151 | close_on_exec_on(fileno(p)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3152 | setup_file_in_str(&pipe_str, p); |
| 3153 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3154 | /* Now send results of command back into original context */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3155 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3156 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3157 | if (ch == '\n') { |
| 3158 | eol_cnt++; |
| 3159 | continue; |
| 3160 | } |
| 3161 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3162 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3163 | eol_cnt--; |
| 3164 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3165 | // /* Even unquoted `echo '\'` results in two backslashes |
| 3166 | // * (which are converted into one by globbing later) */ |
| 3167 | // if (!dest->o_quote && ch == '\\') { |
| 3168 | // o_addchr(dest, ch); |
| 3169 | // } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3170 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | debug_printf("done reading from pipe, pclose()ing\n"); |
| 3174 | /* This is the step that wait()s for the child. Should be pretty |
| 3175 | * 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] | 3176 | * to do better, by using wait(), and keeping track of background jobs |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3177 | * at the same time. That would be a lot of work, and contrary |
| 3178 | * to the KISS philosophy of this program. */ |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3179 | retcode = fclose(p); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3180 | free_pipe_list(inner.list_head, /* indent: */ 0); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3181 | debug_printf("closed FILE from child, retcode=%d\n", retcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3182 | return retcode; |
| 3183 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3184 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3185 | |
| 3186 | static int parse_group(o_string *dest, struct p_context *ctx, |
| 3187 | struct in_str *input, int ch) |
| 3188 | { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3189 | int rcode; |
| 3190 | const char *endch = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3191 | struct p_context sub; |
| 3192 | struct child_prog *child = ctx->child; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3193 | |
| 3194 | debug_printf_parse("parse_group entered\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3195 | if (child->argv) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3196 | syntax(NULL); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3197 | debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n"); |
| 3198 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3199 | } |
| 3200 | initialize_context(&sub); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3201 | endch = "}"; |
| 3202 | if (ch == '(') { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3203 | endch = ")"; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3204 | child->subshell = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3205 | } |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3206 | rcode = parse_stream(dest, &sub, input, endch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3207 | if (rcode == 0) { |
| 3208 | done_word(dest, &sub); /* finish off the final word in the subcontext */ |
| 3209 | done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */ |
| 3210 | child->group = sub.list_head; |
| 3211 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3212 | debug_printf_parse("parse_group return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3213 | return rcode; |
| 3214 | /* child remains "open", available for possible redirects */ |
| 3215 | } |
| 3216 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3217 | /* Basically useful version until someone wants to get fancier, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3218 | * see the bash man page under "Parameter Expansion" */ |
Denis Vlasenko | 15d78fb | 2007-01-30 22:28:21 +0000 | [diff] [blame] | 3219 | static const char *lookup_param(const char *src) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3220 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3221 | struct variable *var = get_local_var(src); |
| 3222 | if (var) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 3223 | return strchr(var->varstr, '=') + 1; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3224 | return NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3227 | #if ENABLE_HUSH_TICK |
| 3228 | /* Subroutines for copying $(...) and `...` things */ |
| 3229 | static void add_till_backquote(o_string *dest, struct in_str *input); |
| 3230 | /* '...' */ |
| 3231 | static void add_till_single_quote(o_string *dest, struct in_str *input) |
| 3232 | { |
| 3233 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3234 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3235 | if (ch == EOF) |
| 3236 | break; |
| 3237 | if (ch == '\'') |
| 3238 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3239 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3240 | } |
| 3241 | } |
| 3242 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
| 3243 | static void add_till_double_quote(o_string *dest, struct in_str *input) |
| 3244 | { |
| 3245 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3246 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3247 | if (ch == '"') |
| 3248 | break; |
| 3249 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3250 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3251 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3252 | } |
| 3253 | if (ch == EOF) |
| 3254 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3255 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3256 | if (ch == '`') { |
| 3257 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3258 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3259 | continue; |
| 3260 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3261 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3262 | } |
| 3263 | } |
| 3264 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 3265 | * \` quoting. |
| 3266 | * "Within the backquoted style of command substitution, backslash |
| 3267 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 3268 | * The search for the matching backquote shall be satisfied by the first |
| 3269 | * backquote found without a preceding backslash; during this search, |
| 3270 | * if a non-escaped backquote is encountered within a shell comment, |
| 3271 | * a here-document, an embedded command substitution of the $(command) |
| 3272 | * form, or a quoted string, undefined results occur. A single-quoted |
| 3273 | * or double-quoted string that begins, but does not end, within the |
| 3274 | * "`...`" sequence produces undefined results." |
| 3275 | * Example Output |
| 3276 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 3277 | */ |
| 3278 | static void add_till_backquote(o_string *dest, struct in_str *input) |
| 3279 | { |
| 3280 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3281 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3282 | if (ch == '`') |
| 3283 | break; |
| 3284 | if (ch == '\\') { /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3285 | int ch2 = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3286 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3287 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3288 | ch = ch2; |
| 3289 | } |
| 3290 | if (ch == EOF) |
| 3291 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3292 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3293 | } |
| 3294 | } |
| 3295 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 3296 | * quoting and nested ()s. |
| 3297 | * "With the $(command) style of command substitution, all characters |
| 3298 | * following the open parenthesis to the matching closing parenthesis |
| 3299 | * constitute the command. Any valid shell script can be used for command, |
| 3300 | * except a script consisting solely of redirections which produces |
| 3301 | * unspecified results." |
| 3302 | * Example Output |
| 3303 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 3304 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 3305 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 3306 | */ |
| 3307 | static void add_till_closing_curly_brace(o_string *dest, struct in_str *input) |
| 3308 | { |
| 3309 | int count = 0; |
| 3310 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3311 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3312 | if (ch == EOF) |
| 3313 | break; |
| 3314 | if (ch == '(') |
| 3315 | count++; |
| 3316 | if (ch == ')') |
| 3317 | if (--count < 0) |
| 3318 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3319 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3320 | if (ch == '\'') { |
| 3321 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3322 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3323 | continue; |
| 3324 | } |
| 3325 | if (ch == '"') { |
| 3326 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3327 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3328 | continue; |
| 3329 | } |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3330 | if (ch == '\\') { /* \x. Copy verbatim. Important for \(, \) */ |
| 3331 | ch = i_getch(input); |
| 3332 | if (ch == EOF) |
| 3333 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3334 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3335 | continue; |
| 3336 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3337 | } |
| 3338 | } |
| 3339 | #endif /* ENABLE_HUSH_TICK */ |
| 3340 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3341 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3342 | static int handle_dollar(o_string *dest, struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3343 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3344 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3345 | unsigned char quote_mask = dest->o_quote ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3346 | |
| 3347 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3348 | if (isalpha(ch)) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3349 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3350 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3351 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3352 | i_getch(input); |
| 3353 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3354 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3355 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3356 | if (!isalnum(ch) && ch != '_') |
| 3357 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3358 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3359 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3360 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3361 | make_one_char_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3362 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3363 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3364 | i_getch(input); |
| 3365 | o_addchr(dest, ch | quote_mask); |
| 3366 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3367 | } else switch (ch) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3368 | case '$': /* pid */ |
| 3369 | case '!': /* last bg pid */ |
| 3370 | case '?': /* last exit code */ |
| 3371 | case '#': /* number of args */ |
| 3372 | case '*': /* args */ |
| 3373 | case '@': /* args */ |
| 3374 | goto make_one_char_var; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3375 | case '{': |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3376 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3377 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3378 | /* XXX maybe someone will try to escape the '}' */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3379 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3380 | ch = i_getch(input); |
Denis Vlasenko | b055001 | 2007-05-24 12:18:16 +0000 | [diff] [blame] | 3381 | if (ch == '}') |
| 3382 | break; |
| 3383 | if (!isalnum(ch) && ch != '_') { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3384 | syntax("unterminated ${name}"); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3385 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 3386 | return 1; |
| 3387 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3388 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3389 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3390 | quote_mask = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3391 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3392 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3393 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3394 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3395 | case '(': { |
| 3396 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3397 | i_getch(input); |
| 3398 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3399 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3400 | add_till_closing_curly_brace(dest, input); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3401 | //debug_printf_subst("SUBST RES2 '%s'\n", dest->data + pos); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3402 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3403 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3404 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3405 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3406 | case '-': |
| 3407 | case '_': |
| 3408 | /* still unhandled, but should be eventually */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3409 | bb_error_msg("unhandled syntax: $%c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3410 | return 1; |
| 3411 | break; |
| 3412 | default: |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3413 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3414 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3415 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3416 | return 0; |
| 3417 | } |
| 3418 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3419 | /* Scan input, call done_word() whenever full IFS delemited word was seen. |
| 3420 | * call done_pipe if '\n' was seen (and end_trigger != NULL) |
| 3421 | * Return if (non-quoted) char in end_trigger was seen; or on parse error. */ |
| 3422 | /* Return code is 0 if end_trigger char is met, |
| 3423 | * -1 on EOF (but if end_trigger == NULL then return 0) |
| 3424 | * 1 for syntax error */ |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3425 | static int parse_stream(o_string *dest, struct p_context *ctx, |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3426 | struct in_str *input, const char *end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3427 | { |
"Vladimir N. Oleynik" | 4ccd2b4 | 2006-01-31 09:27:48 +0000 | [diff] [blame] | 3428 | int ch, m; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3429 | int redir_fd; |
| 3430 | redir_type redir_style; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3431 | int shadow_quote = dest->o_quote; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3432 | int next; |
| 3433 | |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3434 | /* 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] | 3435 | * 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] | 3436 | * found. When recursing, quote state is passed in via dest->o_quote. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3437 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3438 | debug_printf_parse("parse_stream entered, end_trigger='%s'\n", end_trigger); |
| 3439 | |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3440 | while (1) { |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3441 | m = CHAR_IFS; |
| 3442 | next = '\0'; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3443 | ch = i_getch(input); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3444 | if (ch != EOF) { |
| 3445 | m = charmap[ch]; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3446 | if (ch != '\n') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3447 | next = i_peek(input); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3448 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3449 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3450 | debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n", |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3451 | ch, ch, m, dest->o_quote); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3452 | if (m == CHAR_ORDINARY |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3453 | || (m != CHAR_SPECIAL && shadow_quote) |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3454 | ) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3455 | if (ch == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3456 | syntax("unterminated \""); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3457 | debug_printf_parse("parse_stream return 1: unterminated \"\n"); |
| 3458 | return 1; |
| 3459 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3460 | o_addQchr(dest, ch); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3461 | if (dest->o_assignment == MAYBE_ASSIGNMENT |
| 3462 | && ch == '=' |
| 3463 | && is_assignment(dest->data) |
| 3464 | ) { |
| 3465 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 3466 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3467 | continue; |
| 3468 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3469 | if (m == CHAR_IFS) { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3470 | if (done_word(dest, ctx)) { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3471 | debug_printf_parse("parse_stream return 1: done_word!=0\n"); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3472 | return 1; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3473 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3474 | if (ch == EOF) |
| 3475 | break; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3476 | /* If we aren't performing a substitution, treat |
| 3477 | * a newline as a command separator. |
| 3478 | * [why we don't handle it exactly like ';'? --vda] */ |
| 3479 | if (end_trigger && ch == '\n') { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3480 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3481 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3482 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3483 | if (end_trigger) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3484 | if (!shadow_quote && strchr(end_trigger, ch)) { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3485 | /* Special case: (...word) makes last word terminate, |
| 3486 | * as if ';' is seen */ |
| 3487 | if (ch == ')') { |
| 3488 | done_word(dest, ctx); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3489 | //err chk? |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3490 | done_pipe(ctx, PIPE_SEQ); |
| 3491 | } |
| 3492 | if (ctx->res_w == RES_NONE) { |
| 3493 | debug_printf_parse("parse_stream return 0: end_trigger char found\n"); |
| 3494 | return 0; |
| 3495 | } |
| 3496 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3497 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3498 | if (m == CHAR_IFS) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3499 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3500 | |
| 3501 | if (dest->o_assignment == MAYBE_ASSIGNMENT) { |
| 3502 | /* ch is a special char and thus this word |
| 3503 | * cannot be an assignment: */ |
| 3504 | dest->o_assignment = NOT_ASSIGNMENT; |
| 3505 | } |
| 3506 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3507 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3508 | case '#': |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3509 | if (dest->length == 0 && !shadow_quote) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3510 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3511 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3512 | if (ch == EOF || ch == '\n') |
| 3513 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3514 | i_getch(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3515 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3516 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3517 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3518 | } |
| 3519 | break; |
| 3520 | case '\\': |
| 3521 | if (next == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3522 | syntax("\\<eof>"); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3523 | debug_printf_parse("parse_stream return 1: \\<eof>\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3524 | return 1; |
| 3525 | } |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3526 | /* bash: |
| 3527 | * "The backslash retains its special meaning [in "..."] |
| 3528 | * only when followed by one of the following characters: |
| 3529 | * $, `, ", \, or <newline>. A double quote may be quoted |
| 3530 | * within double quotes by preceding it with a backslash. |
| 3531 | * If enabled, history expansion will be performed unless |
| 3532 | * an ! appearing in double quotes is escaped using |
| 3533 | * a backslash. The backslash preceding the ! is not removed." |
| 3534 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3535 | if (shadow_quote) { //NOT SURE dest->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3536 | if (strchr("$`\"\\", next) != NULL) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3537 | o_addqchr(dest, i_getch(input)); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3538 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3539 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3540 | } |
| 3541 | } else { |
| 3542 | o_addchr(dest, '\\'); |
| 3543 | o_addchr(dest, i_getch(input)); |
| 3544 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3545 | break; |
| 3546 | case '$': |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3547 | if (handle_dollar(dest, input) != 0) { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3548 | debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n"); |
| 3549 | return 1; |
| 3550 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3551 | break; |
| 3552 | case '\'': |
| 3553 | dest->nonnull = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3554 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3555 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3556 | if (ch == EOF) { |
| 3557 | syntax("unterminated '"); |
| 3558 | debug_printf_parse("parse_stream return 1: unterminated '\n"); |
| 3559 | return 1; |
| 3560 | } |
| 3561 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3562 | break; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3563 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 3564 | o_addqchr(dest, ch); |
| 3565 | else |
| 3566 | o_addchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3567 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3568 | break; |
| 3569 | case '"': |
| 3570 | dest->nonnull = 1; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3571 | shadow_quote ^= 1; /* invert */ |
| 3572 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 3573 | dest->o_quote ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3574 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3575 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3576 | case '`': { |
| 3577 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3578 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3579 | o_addchr(dest, shadow_quote /*or dest->o_quote??*/ ? 0x80 | '`' : '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3580 | add_till_backquote(dest, input); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3581 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3582 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3583 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3584 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3585 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3586 | case '>': |
| 3587 | redir_fd = redirect_opt_num(dest); |
| 3588 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3589 | redir_style = REDIRECT_OVERWRITE; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3590 | if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3591 | redir_style = REDIRECT_APPEND; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3592 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3593 | } |
| 3594 | #if 0 |
| 3595 | else if (next == '(') { |
| 3596 | syntax(">(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3597 | debug_printf_parse("parse_stream return 1: >(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3598 | return 1; |
| 3599 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3600 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3601 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 3602 | break; |
| 3603 | case '<': |
| 3604 | redir_fd = redirect_opt_num(dest); |
| 3605 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3606 | redir_style = REDIRECT_INPUT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3607 | if (next == '<') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3608 | redir_style = REDIRECT_HEREIS; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3609 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3610 | } else if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3611 | redir_style = REDIRECT_IO; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3612 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3613 | } |
| 3614 | #if 0 |
| 3615 | else if (next == '(') { |
| 3616 | syntax("<(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3617 | debug_printf_parse("parse_stream return 1: <(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3618 | return 1; |
| 3619 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3620 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3621 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 3622 | break; |
| 3623 | case ';': |
| 3624 | done_word(dest, ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3625 | done_pipe(ctx, PIPE_SEQ); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3626 | break; |
| 3627 | case '&': |
| 3628 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3629 | if (next == '&') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3630 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3631 | done_pipe(ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3632 | } else { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3633 | done_pipe(ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3634 | } |
| 3635 | break; |
| 3636 | case '|': |
| 3637 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3638 | if (next == '|') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3639 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3640 | done_pipe(ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3641 | } else { |
| 3642 | /* we could pick up a file descriptor choice here |
| 3643 | * with redirect_opt_num(), but bash doesn't do it. |
| 3644 | * "echo foo 2| cat" yields "foo 2". */ |
| 3645 | done_command(ctx); |
| 3646 | } |
| 3647 | break; |
| 3648 | case '(': |
| 3649 | case '{': |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3650 | if (parse_group(dest, ctx, input, ch) != 0) { |
| 3651 | 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] | 3652 | return 1; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3653 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3654 | break; |
| 3655 | case ')': |
| 3656 | case '}': |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3657 | /* proper use of this character is caught by end_trigger */ |
| 3658 | syntax("unexpected } or )"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3659 | debug_printf_parse("parse_stream return 1: unexpected '}'\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3660 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3661 | default: |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3662 | if (ENABLE_HUSH_DEBUG) |
| 3663 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3664 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3665 | } /* while (1) */ |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3666 | debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL)); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3667 | if (end_trigger) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3668 | return -1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3669 | return 0; |
| 3670 | } |
| 3671 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3672 | static void set_in_charmap(const char *set, int code) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3673 | { |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 3674 | while (*set) |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3675 | charmap[(unsigned char)*set++] = code; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3678 | static void update_charmap(void) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3679 | { |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3680 | /* char *ifs and char charmap[256] are both globals. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3681 | ifs = getenv("IFS"); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3682 | if (ifs == NULL) |
| 3683 | ifs = " \t\n"; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3684 | /* Precompute a list of 'flow through' behavior so it can be treated |
| 3685 | * quickly up front. Computation is necessary because of IFS. |
| 3686 | * Special case handling of IFS == " \t\n" is not implemented. |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3687 | * The charmap[] array only really needs two bits each, |
| 3688 | * and on most machines that would be faster (reduced L1 cache use). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3689 | */ |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3690 | memset(charmap, CHAR_ORDINARY, sizeof(charmap)); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3691 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3692 | set_in_charmap("\\$\"`", CHAR_SPECIAL); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3693 | #else |
| 3694 | set_in_charmap("\\$\"", CHAR_SPECIAL); |
| 3695 | #endif |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3696 | set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3697 | set_in_charmap(ifs, CHAR_IFS); /* are ordinary if quoted */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3700 | /* Most recursion does not come through here, the exception is |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3701 | * from builtin_source() and builtin_eval() */ |
| 3702 | 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] | 3703 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3704 | struct p_context ctx; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3705 | o_string temp = NULL_O_STRING; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3706 | int rcode; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3707 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3708 | do { |
| 3709 | initialize_context(&ctx); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3710 | update_charmap(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3711 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 3712 | inp->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3713 | #endif |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3714 | /* We will stop & execute after each ';' or '\n'. |
| 3715 | * Example: "sleep 9999; echo TEST" + ctrl-C: |
| 3716 | * TEST should be printed */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3717 | rcode = parse_stream(&temp, &ctx, inp, ";\n"); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3718 | if (rcode != 1 && ctx.old_flag != 0) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3719 | syntax(NULL); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3720 | } |
| 3721 | if (rcode != 1 && ctx.old_flag == 0) { |
| 3722 | done_word(&temp, &ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3723 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 3724 | debug_print_tree(ctx.list_head, 0); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3725 | debug_printf_exec("parse_stream_outer: run_and_free_list\n"); |
| 3726 | run_and_free_list(ctx.list_head); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3727 | } else { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3728 | /* We arrive here also if rcode == 1 (error in parse_stream) */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3729 | if (ctx.old_flag != 0) { |
| 3730 | free(ctx.stack); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3731 | o_reset(&temp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3732 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3733 | /*temp.nonnull = 0; - o_free does it below */ |
| 3734 | /*temp.o_quote = 0; - o_free does it below */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3735 | free_pipe_list(ctx.list_head, /* indent: */ 0); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3736 | /* Discard all unprocessed line input, force prompt on */ |
| 3737 | inp->p = NULL; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3738 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3739 | inp->promptme = 1; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3740 | #endif |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 3741 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3742 | o_free(&temp); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3743 | /* loop on syntax errors, return on EOF: */ |
| 3744 | } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3745 | return 0; |
| 3746 | } |
| 3747 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3748 | static int parse_and_run_string(const char *s, int parse_flag) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3749 | { |
| 3750 | struct in_str input; |
| 3751 | setup_string_in_str(&input, s); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3752 | return parse_and_run_stream(&input, parse_flag); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3753 | } |
| 3754 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3755 | static int parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3756 | { |
| 3757 | int rcode; |
| 3758 | struct in_str input; |
| 3759 | setup_file_in_str(&input, f); |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3760 | rcode = parse_and_run_stream(&input, 0 /* parse_flag */); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3761 | return rcode; |
| 3762 | } |
| 3763 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3764 | #if ENABLE_HUSH_JOB |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 3765 | /* Make sure we have a controlling tty. If we get started under a job |
| 3766 | * aware app (like bash for example), make sure we are now in charge so |
| 3767 | * we don't fight over who gets the foreground */ |
Eric Andersen | eaecbf3 | 2001-10-31 10:41:31 +0000 | [diff] [blame] | 3768 | static void setup_job_control(void) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3769 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3770 | pid_t shell_pgrp; |
| 3771 | |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 3772 | saved_task_pgrp = shell_pgrp = getpgrp(); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3773 | debug_printf_jobs("saved_task_pgrp=%d\n", saved_task_pgrp); |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 3774 | close_on_exec_on(interactive_fd); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3775 | |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 3776 | /* If we were ran as 'hush &', |
| 3777 | * sleep until we are in the foreground. */ |
| 3778 | while (tcgetpgrp(interactive_fd) != shell_pgrp) { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3779 | /* Send TTIN to ourself (should stop us) */ |
Denis Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 3780 | kill(- shell_pgrp, SIGTTIN); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 3781 | shell_pgrp = getpgrp(); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3782 | } |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3783 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3784 | /* Ignore job-control and misc signals. */ |
| 3785 | set_jobctrl_sighandler(SIG_IGN); |
| 3786 | set_misc_sighandler(SIG_IGN); |
| 3787 | //huh? signal(SIGCHLD, SIG_IGN); |
| 3788 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3789 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3790 | set_fatal_sighandler(sigexit); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 3791 | |
| 3792 | /* Put ourselves in our own process group. */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3793 | setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 3794 | /* Grab control of the terminal. */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3795 | tcsetpgrp(interactive_fd, getpid()); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 3796 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3797 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 3798 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3799 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 3800 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 3801 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3802 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 3803 | static const char version_str[] ALIGN1 = "HUSH_VERSION="HUSH_VER_STR; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 3804 | static const struct variable const_shell_ver = { |
| 3805 | .next = NULL, |
| 3806 | .varstr = (char*)version_str, |
| 3807 | .max_len = 1, /* 0 can provoke free(name) */ |
| 3808 | .flg_export = 1, |
| 3809 | .flg_read_only = 1, |
| 3810 | }; |
| 3811 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3812 | int opt; |
| 3813 | FILE *input; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3814 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3815 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 3816 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 3817 | INIT_G(); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3818 | |
Denis Vlasenko | 16c2fea | 2008-06-17 12:28:44 +0000 | [diff] [blame] | 3819 | root_pid = getpid(); |
| 3820 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 3821 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 3822 | shell_ver = const_shell_ver; /* copying struct here */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3823 | top_var = &shell_ver; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 3824 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 3825 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3826 | * currently living in the environment */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3827 | cur_var = top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 3828 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3829 | if (e) while (*e) { |
| 3830 | char *value = strchr(*e, '='); |
| 3831 | if (value) { /* paranoia */ |
| 3832 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 3833 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 3834 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3835 | cur_var->max_len = strlen(*e); |
| 3836 | cur_var->flg_export = 1; |
| 3837 | } |
| 3838 | e++; |
| 3839 | } |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 3840 | putenv((char *)version_str); /* reinstate HUSH_VERSION */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 3841 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 3842 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 3843 | line_input_state = new_line_input_t(FOR_SHELL); |
| 3844 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3845 | /* XXX what should these be while sourcing /etc/profile? */ |
| 3846 | global_argc = argc; |
| 3847 | global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3848 | /* Initialize some more globals to non-zero values */ |
| 3849 | set_cwd(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3850 | #if ENABLE_HUSH_INTERACTIVE |
| 3851 | #if ENABLE_FEATURE_EDITING |
| 3852 | cmdedit_set_initial_prompt(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3853 | #endif |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3854 | PS2 = "> "; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3855 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 3856 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3857 | if (EXIT_SUCCESS) /* otherwise is already done */ |
| 3858 | last_return_code = EXIT_SUCCESS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3859 | |
| 3860 | if (argv[0] && argv[0][0] == '-') { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3861 | debug_printf("sourcing /etc/profile\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3862 | input = fopen("/etc/profile", "r"); |
| 3863 | if (input != NULL) { |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 3864 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3865 | parse_and_run_file(input); |
Eric Andersen | a90f20b | 2001-06-26 23:00:21 +0000 | [diff] [blame] | 3866 | fclose(input); |
| 3867 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3868 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3869 | input = stdin; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3870 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3871 | while ((opt = getopt(argc, argv, "c:xif")) > 0) { |
| 3872 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3873 | case 'c': |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3874 | global_argv = argv + optind; |
| 3875 | global_argc = argc - optind; |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3876 | opt = parse_and_run_string(optarg, 0 /* parse_flag */); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3877 | goto final_return; |
| 3878 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3879 | /* Well, we cannot just declare interactiveness, |
| 3880 | * we have to have some stuff (ctty, etc) */ |
| 3881 | /* interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3882 | break; |
| 3883 | case 'f': |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 3884 | fake_mode = 1; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3885 | break; |
| 3886 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 3887 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3888 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 3889 | " or: sh -c command [args]...\n\n"); |
| 3890 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 3891 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3892 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 3893 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3894 | } |
| 3895 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3896 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3897 | /* 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] | 3898 | * the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 3899 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3900 | * no arguments remaining or the -s flag given |
| 3901 | * standard input is a terminal |
| 3902 | * standard output is a terminal |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3903 | * Refer to Posix.2, the description of the 'sh' utility. */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3904 | if (argv[optind] == NULL && input == stdin |
| 3905 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 3906 | ) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3907 | saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 3908 | debug_printf("saved_tty_pgrp=%d\n", saved_tty_pgrp); |
| 3909 | if (saved_tty_pgrp >= 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3910 | /* try to dup to high fd#, >= 255 */ |
| 3911 | interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 3912 | if (interactive_fd < 0) { |
| 3913 | /* try to dup to any fd */ |
| 3914 | interactive_fd = dup(STDIN_FILENO); |
| 3915 | if (interactive_fd < 0) |
| 3916 | /* give up */ |
| 3917 | interactive_fd = 0; |
| 3918 | } |
Denis Vlasenko | 2f1bb36 | 2007-04-21 10:01:14 +0000 | [diff] [blame] | 3919 | // TODO: track & disallow any attempts of user |
| 3920 | // to (inadvertently) close/redirect it |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3921 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3922 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3923 | debug_printf("interactive_fd=%d\n", interactive_fd); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3924 | if (interactive_fd) { |
Denis Vlasenko | 08126f6 | 2008-02-11 08:39:11 +0000 | [diff] [blame] | 3925 | fcntl(interactive_fd, F_SETFD, FD_CLOEXEC); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3926 | /* Looks like they want an interactive shell */ |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 3927 | setup_job_control(); |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 3928 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3929 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
| 3930 | die_sleep = -1; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3931 | if (setjmp(die_jmp)) { |
| 3932 | /* xfunc has failed! die die die */ |
| 3933 | hush_exit(xfunc_error_retval); |
| 3934 | } |
Denis Vlasenko | 2f1bb36 | 2007-04-21 10:01:14 +0000 | [diff] [blame] | 3935 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 3936 | 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] | 3937 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
| 3938 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 3939 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3940 | #elif ENABLE_HUSH_INTERACTIVE |
| 3941 | /* no job control compiled, only prompt/line editing */ |
| 3942 | if (argv[optind] == NULL && input == stdin |
| 3943 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 3944 | ) { |
| 3945 | interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 3946 | if (interactive_fd < 0) { |
| 3947 | /* try to dup to any fd */ |
| 3948 | interactive_fd = dup(STDIN_FILENO); |
| 3949 | if (interactive_fd < 0) |
| 3950 | /* give up */ |
| 3951 | interactive_fd = 0; |
| 3952 | } |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 3953 | if (interactive_fd) { |
Denis Vlasenko | 08126f6 | 2008-02-11 08:39:11 +0000 | [diff] [blame] | 3954 | fcntl(interactive_fd, F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 3955 | set_misc_sighandler(SIG_IGN); |
| 3956 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 3957 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 3958 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3959 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3960 | if (argv[optind] == NULL) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3961 | opt = parse_and_run_file(stdin); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3962 | } else { |
| 3963 | debug_printf("\nrunning script '%s'\n", argv[optind]); |
| 3964 | global_argv = argv + optind; |
| 3965 | global_argc = argc - optind; |
| 3966 | input = xfopen(argv[optind], "r"); |
Denis Vlasenko | 459a5ad | 2008-02-11 08:35:03 +0000 | [diff] [blame] | 3967 | fcntl(fileno(input), F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3968 | opt = parse_and_run_file(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3969 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3970 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3971 | final_return: |
| 3972 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 3973 | #if ENABLE_FEATURE_CLEAN_UP |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 3974 | fclose(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3975 | if (cwd != bb_msg_unknown) |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 3976 | free((char*)cwd); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3977 | cur_var = top_var->next; |
| 3978 | while (cur_var) { |
| 3979 | struct variable *tmp = cur_var; |
| 3980 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 3981 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3982 | cur_var = cur_var->next; |
| 3983 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 3984 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3985 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 3986 | hush_exit(opt ? opt : last_return_code); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3987 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 3988 | |
| 3989 | |
| 3990 | #if ENABLE_LASH |
| 3991 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 3992 | int lash_main(int argc, char **argv) |
| 3993 | { |
| 3994 | //bb_error_msg("lash is deprecated, please use hush instead"); |
| 3995 | return hush_main(argc, argv); |
| 3996 | } |
| 3997 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3998 | |
| 3999 | |
| 4000 | /* |
| 4001 | * Built-ins |
| 4002 | */ |
| 4003 | static int builtin_true(char **argv ATTRIBUTE_UNUSED) |
| 4004 | { |
| 4005 | return 0; |
| 4006 | } |
| 4007 | |
| 4008 | static int builtin_test(char **argv) |
| 4009 | { |
| 4010 | int argc = 0; |
| 4011 | while (*argv) { |
| 4012 | argc++; |
| 4013 | argv++; |
| 4014 | } |
| 4015 | return test_main(argc, argv - argc); |
| 4016 | } |
| 4017 | |
| 4018 | static int builtin_echo(char **argv) |
| 4019 | { |
| 4020 | int argc = 0; |
| 4021 | while (*argv) { |
| 4022 | argc++; |
| 4023 | argv++; |
| 4024 | } |
| 4025 | return echo_main(argc, argv - argc); |
| 4026 | } |
| 4027 | |
| 4028 | static int builtin_eval(char **argv) |
| 4029 | { |
| 4030 | int rcode = EXIT_SUCCESS; |
| 4031 | |
| 4032 | if (argv[1]) { |
| 4033 | char *str = expand_strvec_to_string(argv + 1); |
| 4034 | parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP); |
| 4035 | free(str); |
| 4036 | rcode = last_return_code; |
| 4037 | } |
| 4038 | return rcode; |
| 4039 | } |
| 4040 | |
| 4041 | static int builtin_cd(char **argv) |
| 4042 | { |
| 4043 | const char *newdir; |
| 4044 | if (argv[1] == NULL) { |
| 4045 | // bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
| 4046 | // bash says "bash: cd: HOME not set" and does nothing (exitcode 1) |
| 4047 | newdir = getenv("HOME") ? : "/"; |
| 4048 | } else |
| 4049 | newdir = argv[1]; |
| 4050 | if (chdir(newdir)) { |
| 4051 | printf("cd: %s: %s\n", newdir, strerror(errno)); |
| 4052 | return EXIT_FAILURE; |
| 4053 | } |
| 4054 | set_cwd(); |
| 4055 | return EXIT_SUCCESS; |
| 4056 | } |
| 4057 | |
| 4058 | static int builtin_exec(char **argv) |
| 4059 | { |
| 4060 | if (argv[1] == NULL) |
| 4061 | return EXIT_SUCCESS; /* bash does this */ |
| 4062 | { |
| 4063 | #if !BB_MMU |
| 4064 | char **ptrs2free = alloc_ptrs(argv); |
| 4065 | #endif |
| 4066 | // FIXME: if exec fails, bash does NOT exit! We do... |
| 4067 | pseudo_exec_argv(ptrs2free, argv + 1); |
| 4068 | /* never returns */ |
| 4069 | } |
| 4070 | } |
| 4071 | |
| 4072 | static int builtin_exit(char **argv) |
| 4073 | { |
| 4074 | // TODO: bash does it ONLY on top-level sh exit (+interacive only?) |
| 4075 | //puts("exit"); /* bash does it */ |
| 4076 | // TODO: warn if we have background jobs: "There are stopped jobs" |
| 4077 | // On second consecutive 'exit', exit anyway. |
| 4078 | if (argv[1] == NULL) |
| 4079 | hush_exit(last_return_code); |
| 4080 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 4081 | xfunc_error_retval = 255; |
| 4082 | /* bash: exit -2 == exit 254, no error msg */ |
| 4083 | hush_exit(xatoi(argv[1]) & 0xff); |
| 4084 | } |
| 4085 | |
| 4086 | static int builtin_export(char **argv) |
| 4087 | { |
| 4088 | const char *value; |
| 4089 | char *name = argv[1]; |
| 4090 | |
| 4091 | if (name == NULL) { |
| 4092 | // TODO: |
| 4093 | // ash emits: export VAR='VAL' |
| 4094 | // bash: declare -x VAR="VAL" |
| 4095 | // (both also escape as needed (quotes, $, etc)) |
| 4096 | char **e = environ; |
| 4097 | if (e) |
| 4098 | while (*e) |
| 4099 | puts(*e++); |
| 4100 | return EXIT_SUCCESS; |
| 4101 | } |
| 4102 | |
| 4103 | value = strchr(name, '='); |
| 4104 | if (!value) { |
| 4105 | /* They are exporting something without a =VALUE */ |
| 4106 | struct variable *var; |
| 4107 | |
| 4108 | var = get_local_var(name); |
| 4109 | if (var) { |
| 4110 | var->flg_export = 1; |
| 4111 | putenv(var->varstr); |
| 4112 | } |
| 4113 | /* bash does not return an error when trying to export |
| 4114 | * an undefined variable. Do likewise. */ |
| 4115 | return EXIT_SUCCESS; |
| 4116 | } |
| 4117 | |
| 4118 | set_local_var(xstrdup(name), 1); |
| 4119 | return EXIT_SUCCESS; |
| 4120 | } |
| 4121 | |
| 4122 | #if ENABLE_HUSH_JOB |
| 4123 | /* built-in 'fg' and 'bg' handler */ |
| 4124 | static int builtin_fg_bg(char **argv) |
| 4125 | { |
| 4126 | int i, jobnum; |
| 4127 | struct pipe *pi; |
| 4128 | |
| 4129 | if (!interactive_fd) |
| 4130 | return EXIT_FAILURE; |
| 4131 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 4132 | if (!argv[1]) { |
| 4133 | for (pi = job_list; pi; pi = pi->next) { |
| 4134 | if (pi->jobid == last_jobid) { |
| 4135 | goto found; |
| 4136 | } |
| 4137 | } |
| 4138 | bb_error_msg("%s: no current job", argv[0]); |
| 4139 | return EXIT_FAILURE; |
| 4140 | } |
| 4141 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 4142 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 4143 | return EXIT_FAILURE; |
| 4144 | } |
| 4145 | for (pi = job_list; pi; pi = pi->next) { |
| 4146 | if (pi->jobid == jobnum) { |
| 4147 | goto found; |
| 4148 | } |
| 4149 | } |
| 4150 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 4151 | return EXIT_FAILURE; |
| 4152 | found: |
| 4153 | // TODO: bash prints a string representation |
| 4154 | // of job being foregrounded (like "sleep 1 | cat") |
| 4155 | if (*argv[0] == 'f') { |
| 4156 | /* Put the job into the foreground. */ |
| 4157 | tcsetpgrp(interactive_fd, pi->pgrp); |
| 4158 | } |
| 4159 | |
| 4160 | /* Restart the processes in the job */ |
| 4161 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_progs, pi->pgrp); |
| 4162 | for (i = 0; i < pi->num_progs; i++) { |
| 4163 | debug_printf_jobs("reviving pid %d\n", pi->progs[i].pid); |
| 4164 | pi->progs[i].is_stopped = 0; |
| 4165 | } |
| 4166 | pi->stopped_progs = 0; |
| 4167 | |
| 4168 | i = kill(- pi->pgrp, SIGCONT); |
| 4169 | if (i < 0) { |
| 4170 | if (errno == ESRCH) { |
| 4171 | delete_finished_bg_job(pi); |
| 4172 | return EXIT_SUCCESS; |
| 4173 | } else { |
| 4174 | bb_perror_msg("kill (SIGCONT)"); |
| 4175 | } |
| 4176 | } |
| 4177 | |
| 4178 | if (*argv[0] == 'f') { |
| 4179 | remove_bg_job(pi); |
| 4180 | return checkjobs_and_fg_shell(pi); |
| 4181 | } |
| 4182 | return EXIT_SUCCESS; |
| 4183 | } |
| 4184 | #endif |
| 4185 | |
| 4186 | #if ENABLE_HUSH_HELP |
| 4187 | static int builtin_help(char **argv ATTRIBUTE_UNUSED) |
| 4188 | { |
| 4189 | const struct built_in_command *x; |
| 4190 | |
| 4191 | printf("\nBuilt-in commands:\n"); |
| 4192 | printf("-------------------\n"); |
| 4193 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 4194 | printf("%s\t%s\n", x->cmd, x->descr); |
| 4195 | } |
| 4196 | printf("\n\n"); |
| 4197 | return EXIT_SUCCESS; |
| 4198 | } |
| 4199 | #endif |
| 4200 | |
| 4201 | #if ENABLE_HUSH_JOB |
| 4202 | static int builtin_jobs(char **argv ATTRIBUTE_UNUSED) |
| 4203 | { |
| 4204 | struct pipe *job; |
| 4205 | const char *status_string; |
| 4206 | |
| 4207 | for (job = job_list; job; job = job->next) { |
| 4208 | if (job->running_progs == job->stopped_progs) |
| 4209 | status_string = "Stopped"; |
| 4210 | else |
| 4211 | status_string = "Running"; |
| 4212 | |
| 4213 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 4214 | } |
| 4215 | return EXIT_SUCCESS; |
| 4216 | } |
| 4217 | #endif |
| 4218 | |
| 4219 | static int builtin_pwd(char **argv ATTRIBUTE_UNUSED) |
| 4220 | { |
| 4221 | puts(set_cwd()); |
| 4222 | return EXIT_SUCCESS; |
| 4223 | } |
| 4224 | |
| 4225 | static int builtin_read(char **argv) |
| 4226 | { |
| 4227 | char *string; |
| 4228 | const char *name = argv[1] ? argv[1] : "REPLY"; |
| 4229 | |
| 4230 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
| 4231 | return set_local_var(string, 0); |
| 4232 | } |
| 4233 | |
| 4234 | /* built-in 'set [VAR=value]' handler */ |
| 4235 | static int builtin_set(char **argv) |
| 4236 | { |
| 4237 | char *temp = argv[1]; |
| 4238 | struct variable *e; |
| 4239 | |
| 4240 | if (temp == NULL) |
| 4241 | for (e = top_var; e; e = e->next) |
| 4242 | puts(e->varstr); |
| 4243 | else |
| 4244 | set_local_var(xstrdup(temp), 0); |
| 4245 | |
| 4246 | return EXIT_SUCCESS; |
| 4247 | } |
| 4248 | |
| 4249 | static int builtin_shift(char **argv) |
| 4250 | { |
| 4251 | int n = 1; |
| 4252 | if (argv[1]) { |
| 4253 | n = atoi(argv[1]); |
| 4254 | } |
| 4255 | if (n >= 0 && n < global_argc) { |
| 4256 | global_argv[n] = global_argv[0]; |
| 4257 | global_argc -= n; |
| 4258 | global_argv += n; |
| 4259 | return EXIT_SUCCESS; |
| 4260 | } |
| 4261 | return EXIT_FAILURE; |
| 4262 | } |
| 4263 | |
| 4264 | static int builtin_source(char **argv) |
| 4265 | { |
| 4266 | FILE *input; |
| 4267 | int status; |
| 4268 | |
| 4269 | if (argv[1] == NULL) |
| 4270 | return EXIT_FAILURE; |
| 4271 | |
| 4272 | /* XXX search through $PATH is missing */ |
| 4273 | input = fopen(argv[1], "r"); |
| 4274 | if (!input) { |
| 4275 | bb_error_msg("cannot open '%s'", argv[1]); |
| 4276 | return EXIT_FAILURE; |
| 4277 | } |
| 4278 | close_on_exec_on(fileno(input)); |
| 4279 | |
| 4280 | /* Now run the file */ |
| 4281 | /* XXX argv and argc are broken; need to save old global_argv |
| 4282 | * (pointer only is OK!) on this stack frame, |
| 4283 | * set global_argv=argv+1, recurse, and restore. */ |
| 4284 | status = parse_and_run_file(input); |
| 4285 | fclose(input); |
| 4286 | return status; |
| 4287 | } |
| 4288 | |
| 4289 | static int builtin_umask(char **argv) |
| 4290 | { |
| 4291 | mode_t new_umask; |
| 4292 | const char *arg = argv[1]; |
| 4293 | char *end; |
| 4294 | if (arg) { |
| 4295 | new_umask = strtoul(arg, &end, 8); |
| 4296 | if (*end != '\0' || end == arg) { |
| 4297 | return EXIT_FAILURE; |
| 4298 | } |
| 4299 | } else { |
| 4300 | new_umask = umask(0); |
| 4301 | printf("%.3o\n", (unsigned) new_umask); |
| 4302 | } |
| 4303 | umask(new_umask); |
| 4304 | return EXIT_SUCCESS; |
| 4305 | } |
| 4306 | |
| 4307 | static int builtin_unset(char **argv) |
| 4308 | { |
| 4309 | /* bash always returns true */ |
| 4310 | unset_local_var(argv[1]); |
| 4311 | return EXIT_SUCCESS; |
| 4312 | } |