Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * sh.c -- a prototype Bourne shell grammar parser |
| 4 | * Intended to follow the original Thompson and Ritchie |
| 5 | * "small and simple is beautiful" philosophy, which |
| 6 | * incidentally is a good match to today's BusyBox. |
| 7 | * |
| 8 | * Copyright (C) 2000,2001 Larry Doolittle <larry@doolittle.boa.org> |
| 9 | * |
| 10 | * Credits: |
| 11 | * The parser routines proper are all original material, first |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 12 | * written Dec 2000 and Jan 2001 by Larry Doolittle. The |
| 13 | * execution engine, the builtins, and much of the underlying |
| 14 | * support has been adapted from busybox-0.49pre's lash, which is |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 15 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 16 | * written by Erik Andersen <andersen@codepoet.org>. That, in turn, |
| 17 | * is based in part on ladsh.c, by Michael K. Johnson and Erik W. |
| 18 | * Troan, which they placed in the public domain. I don't know |
| 19 | * how much of the Johnson/Troan code has survived the repeated |
| 20 | * rewrites. |
| 21 | * |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 22 | * Other credits: |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 23 | * o_addchr() derived from similar w_addchar function in glibc-2.2. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 24 | * setup_redirect(), redirect_opt_num(), and big chunks of main() |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 25 | * and many builtins derived from contributions by Erik Andersen |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 26 | * miscellaneous bugfixes from Matt Kraai. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 27 | * |
| 28 | * There are two big (and related) architecture differences between |
| 29 | * this parser and the lash parser. One is that this version is |
| 30 | * actually designed from the ground up to understand nearly all |
| 31 | * of the Bourne grammar. The second, consequential change is that |
| 32 | * the parser and input reader have been turned inside out. Now, |
| 33 | * the parser is in control, and asks for input as needed. The old |
| 34 | * way had the input reader in control, and it asked for parsing to |
| 35 | * take place as needed. The new way makes it much easier to properly |
| 36 | * handle the recursion implicit in the various substitutions, especially |
| 37 | * across continuation lines. |
| 38 | * |
| 39 | * Bash grammar not implemented: (how many of these were in original sh?) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 40 | * $_ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 41 | * &> and >& redirection of stdout+stderr |
| 42 | * Brace Expansion |
| 43 | * Tilde Expansion |
| 44 | * fancy forms of Parameter Expansion |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 45 | * aliases |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 46 | * Arithmetic Expansion |
| 47 | * <(list) and >(list) Process Substitution |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 48 | * reserved words: select, function |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 49 | * Here Documents ( << word ) |
| 50 | * Functions |
| 51 | * Major bugs: |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 52 | * job handling woefully incomplete and buggy (improved --vda) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 53 | * to-do: |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 54 | * port selected bugfixes from post-0.49 busybox lash - done? |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 55 | * change { and } from special chars to reserved words |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 56 | * builtins: return, trap, ulimit |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 57 | * test magic exec with redirection only |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 58 | * check setting of global_argc and global_argv |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 59 | * follow IFS rules more precisely, including update semantics |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 60 | * figure out what to do with backslash-newline |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 61 | * propagate syntax errors, die on resource errors? |
| 62 | * continuation lines, both explicit and implicit - done? |
| 63 | * memory leak finding and plugging - done? |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 64 | * maybe change charmap[] to use 2-bit entries |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 65 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 66 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 67 | */ |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 69 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 70 | #include <glob.h> |
| 71 | /* #include <dmalloc.h> */ |
| 72 | #if ENABLE_HUSH_CASE |
| 73 | #include <fnmatch.h> |
| 74 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 75 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 76 | #define HUSH_VER_STR "0.91" |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 78 | #if !BB_MMU && ENABLE_HUSH_TICK |
| 79 | //#undef ENABLE_HUSH_TICK |
| 80 | //#define ENABLE_HUSH_TICK 0 |
| 81 | #warning On NOMMU, hush command substitution is dangerous. |
| 82 | #warning Dont use it for commands which produce lots of output. |
| 83 | #warning For more info see shell/hush.c, generate_stream_from_list(). |
| 84 | #endif |
| 85 | |
| 86 | #if !BB_MMU && ENABLE_HUSH_JOB |
| 87 | #undef ENABLE_HUSH_JOB |
| 88 | #define ENABLE_HUSH_JOB 0 |
| 89 | #endif |
| 90 | |
| 91 | #if !ENABLE_HUSH_INTERACTIVE |
| 92 | #undef ENABLE_FEATURE_EDITING |
| 93 | #define ENABLE_FEATURE_EDITING 0 |
| 94 | #undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 95 | #define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 96 | #endif |
| 97 | |
| 98 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 99 | /* If you comment out one of these below, it will be #defined later |
| 100 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 101 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 102 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 103 | #define debug_printf_parse(...) do {} while (0) |
| 104 | #define debug_print_tree(a, b) do {} while (0) |
| 105 | #define debug_printf_exec(...) do {} while (0) |
| 106 | #define debug_printf_jobs(...) do {} while (0) |
| 107 | #define debug_printf_expand(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 108 | #define debug_printf_glob(...) do {} while (0) |
| 109 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 110 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 111 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 112 | |
| 113 | #ifndef debug_printf |
| 114 | #define debug_printf(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 115 | #endif |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 116 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 117 | #ifndef debug_printf_parse |
| 118 | #define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 119 | #endif |
| 120 | |
| 121 | #ifndef debug_printf_exec |
| 122 | #define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__) |
| 123 | #endif |
| 124 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 125 | #ifndef debug_printf_jobs |
| 126 | #define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 127 | #define DEBUG_JOBS 1 |
| 128 | #else |
| 129 | #define DEBUG_JOBS 0 |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 130 | #endif |
| 131 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 132 | #ifndef debug_printf_expand |
| 133 | #define debug_printf_expand(...) fprintf(stderr, __VA_ARGS__) |
| 134 | #define DEBUG_EXPAND 1 |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 135 | #else |
| 136 | #define DEBUG_EXPAND 0 |
| 137 | #endif |
| 138 | |
| 139 | #ifndef debug_printf_glob |
| 140 | #define debug_printf_glob(...) fprintf(stderr, __VA_ARGS__) |
| 141 | #define DEBUG_GLOB 1 |
| 142 | #else |
| 143 | #define DEBUG_GLOB 0 |
| 144 | #endif |
| 145 | |
| 146 | #ifndef debug_printf_list |
| 147 | #define debug_printf_list(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 148 | #endif |
| 149 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 150 | #ifndef debug_printf_subst |
| 151 | #define debug_printf_subst(...) fprintf(stderr, __VA_ARGS__) |
| 152 | #endif |
| 153 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 154 | #ifndef debug_printf_clean |
| 155 | /* broken, of course, but OK for testing */ |
| 156 | static const char *indenter(int i) |
| 157 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 158 | static const char blanks[] ALIGN1 = |
| 159 | " "; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 160 | return &blanks[sizeof(blanks) - i - 1]; |
| 161 | } |
| 162 | #define debug_printf_clean(...) fprintf(stderr, __VA_ARGS__) |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 163 | #define DEBUG_CLEAN 1 |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 164 | #endif |
| 165 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 166 | #if DEBUG_EXPAND |
| 167 | static void debug_print_strings(const char *prefix, char **vv) |
| 168 | { |
| 169 | fprintf(stderr, "%s:\n", prefix); |
| 170 | while (*vv) |
| 171 | fprintf(stderr, " '%s'\n", *vv++); |
| 172 | } |
| 173 | #else |
| 174 | #define debug_print_strings(prefix, vv) ((void)0) |
| 175 | #endif |
| 176 | |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 177 | /* |
| 178 | * Leak hunting. Use hush_leaktool.sh for post-processing. |
| 179 | */ |
| 180 | #ifdef FOR_HUSH_LEAKTOOL |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 181 | /* suppress "warning: no previous prototype..." */ |
| 182 | void *xxmalloc(int lineno, size_t size); |
| 183 | void *xxrealloc(int lineno, void *ptr, size_t size); |
| 184 | char *xxstrdup(int lineno, const char *str); |
| 185 | void xxfree(void *ptr); |
Denis Vlasenko | f962a03 | 2007-11-23 12:50:54 +0000 | [diff] [blame] | 186 | void *xxmalloc(int lineno, size_t size) |
| 187 | { |
| 188 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 189 | fprintf(stderr, "line %d: malloc %p\n", lineno, ptr); |
| 190 | return ptr; |
| 191 | } |
| 192 | void *xxrealloc(int lineno, void *ptr, size_t size) |
| 193 | { |
| 194 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 195 | fprintf(stderr, "line %d: realloc %p\n", lineno, ptr); |
| 196 | return ptr; |
| 197 | } |
| 198 | char *xxstrdup(int lineno, const char *str) |
| 199 | { |
| 200 | char *ptr = xstrdup(str); |
| 201 | fprintf(stderr, "line %d: strdup %p\n", lineno, ptr); |
| 202 | return ptr; |
| 203 | } |
| 204 | void xxfree(void *ptr) |
| 205 | { |
| 206 | fprintf(stderr, "free %p\n", ptr); |
| 207 | free(ptr); |
| 208 | } |
| 209 | #define xmalloc(s) xxmalloc(__LINE__, s) |
| 210 | #define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 211 | #define xstrdup(s) xxstrdup(__LINE__, s) |
| 212 | #define free(p) xxfree(p) |
| 213 | #endif |
| 214 | |
| 215 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 216 | /* Keep unconditionally on for now */ |
| 217 | #define HUSH_DEBUG 1 |
| 218 | /* Do we support ANY keywords? */ |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 219 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 220 | #define HAS_KEYWORDS 1 |
| 221 | #define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 222 | #define IF_HAS_NO_KEYWORDS(...) |
| 223 | #else |
| 224 | #define HAS_KEYWORDS 0 |
| 225 | #define IF_HAS_KEYWORDS(...) |
| 226 | #define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
| 227 | #endif |
| 228 | |
| 229 | |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 230 | #define SPECIAL_VAR_SYMBOL 3 |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 231 | #define PARSEFLAG_EXIT_FROM_LOOP 1 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 232 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 233 | typedef enum redir_type { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 234 | REDIRECT_INPUT = 1, |
| 235 | REDIRECT_OVERWRITE = 2, |
| 236 | REDIRECT_APPEND = 3, |
| 237 | REDIRECT_HEREIS = 4, |
| 238 | REDIRECT_IO = 5 |
| 239 | } redir_type; |
| 240 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 241 | /* The descrip member of this structure is only used to make |
| 242 | * debugging output pretty */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 243 | static const struct { |
| 244 | int mode; |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 245 | signed char default_fd; |
| 246 | char descrip[3]; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 247 | } redir_table[] = { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 248 | { 0, 0, "()" }, |
| 249 | { O_RDONLY, 0, "<" }, |
| 250 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 251 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 252 | { O_RDONLY, -1, "<<" }, |
| 253 | { O_RDWR, 1, "<>" } |
| 254 | }; |
| 255 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 256 | typedef enum pipe_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 257 | PIPE_SEQ = 1, |
| 258 | PIPE_AND = 2, |
| 259 | PIPE_OR = 3, |
| 260 | PIPE_BG = 4, |
| 261 | } pipe_style; |
| 262 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 263 | typedef enum reserved_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 264 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 265 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 266 | RES_IF , |
| 267 | RES_THEN , |
| 268 | RES_ELIF , |
| 269 | RES_ELSE , |
| 270 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 271 | #endif |
| 272 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 273 | RES_FOR , |
| 274 | RES_WHILE , |
| 275 | RES_UNTIL , |
| 276 | RES_DO , |
| 277 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 278 | #endif |
| 279 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 280 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 281 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 282 | #if ENABLE_HUSH_CASE |
| 283 | RES_CASE , |
| 284 | /* two pseudo-keywords support contrived "case" syntax: */ |
| 285 | RES_MATCH , /* "word)" */ |
| 286 | RES_CASEI , /* "this command is inside CASE" */ |
| 287 | RES_ESAC , |
| 288 | #endif |
| 289 | RES_XXXX , |
| 290 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 291 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 292 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 293 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 294 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 295 | char *rd_filename; /* filename */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 296 | int fd; /* file descriptor being redirected */ |
| 297 | int dup; /* -1, or file descriptor being duplicated */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 298 | smallint /*enum redir_type*/ rd_type; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 299 | }; |
| 300 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 301 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 302 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 303 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 304 | smallint is_stopped; /* is the command currently running? */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 305 | smallint subshell; /* flag, non-zero if group must be forked */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 306 | struct pipe *group; /* if non-NULL, this "prog" is {} group, |
| 307 | * subshell, or a compound statement */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 308 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 309 | struct redir_struct *redirects; /* I/O redirections */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 310 | }; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 311 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 312 | * and on execution these are substituted with their values. |
| 313 | * Substitution can make _several_ words out of one argv[n]! |
| 314 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 315 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 316 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 317 | |
| 318 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 319 | struct pipe *next; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 320 | int num_cmds; /* total number of commands in job */ |
| 321 | int alive_cmds; /* number of commands running (not exited) */ |
| 322 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 323 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 324 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 325 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 326 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 327 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 328 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 329 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 330 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 331 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 332 | }; |
| 333 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 334 | /* This holds pointers to the various results of parsing */ |
| 335 | struct parse_context { |
| 336 | struct command *command; |
| 337 | struct pipe *list_head; |
| 338 | struct pipe *pipe; |
| 339 | struct redir_struct *pending_redirect; |
| 340 | #if HAS_KEYWORDS |
| 341 | smallint ctx_res_w; |
| 342 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 343 | #if ENABLE_HUSH_CASE |
| 344 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 345 | #endif |
| 346 | int old_flag; /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 347 | struct parse_context *stack; |
| 348 | #endif |
| 349 | }; |
| 350 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 351 | /* On program start, environ points to initial environment. |
| 352 | * putenv adds new pointers into it, unsetenv removes them. |
| 353 | * Neither of these (de)allocates the strings. |
| 354 | * setenv allocates new strings in malloc space and does putenv, |
| 355 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 356 | #define setenv(...) setenv_is_leaky_dont_use() |
| 357 | struct variable { |
| 358 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 359 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 360 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 361 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 362 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 363 | }; |
| 364 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 365 | typedef struct o_string { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 366 | char *data; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 367 | int length; /* position where data is appended */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 368 | int maxlen; |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 369 | /* Misnomer! it's not "quoting", it's "protection against globbing"! |
| 370 | * (by prepending \ to *, ?, [ and to \ too) */ |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 371 | smallint o_quote; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 372 | smallint o_glob; |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 373 | smallint nonnull; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 374 | smallint has_empty_slot; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 375 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 376 | } o_string; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 377 | enum { |
| 378 | MAYBE_ASSIGNMENT = 0, |
| 379 | DEFINITELY_ASSIGNMENT = 1, |
| 380 | NOT_ASSIGNMENT = 2, |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 381 | WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 382 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 383 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 384 | #define NULL_O_STRING { NULL } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 385 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 386 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 387 | * available? Where is it documented? */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 388 | typedef struct in_str { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 389 | const char *p; |
| 390 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 391 | char eof_flag; /* meaningless if ->p == NULL */ |
| 392 | char peek_buf[2]; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 393 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 394 | smallint promptme; |
| 395 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 396 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 397 | FILE *file; |
| 398 | int (*get) (struct in_str *); |
| 399 | int (*peek) (struct in_str *); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 400 | } in_str; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 401 | #define i_getch(input) ((input)->get(input)) |
| 402 | #define i_peek(input) ((input)->peek(input)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 403 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 404 | enum { |
| 405 | CHAR_ORDINARY = 0, |
| 406 | CHAR_ORDINARY_IF_QUOTED = 1, /* example: *, # */ |
| 407 | CHAR_IFS = 2, /* treated as ordinary if quoted */ |
| 408 | CHAR_SPECIAL = 3, /* example: $ */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 411 | enum { |
| 412 | BC_BREAK = 1, |
| 413 | BC_CONTINUE = 2, |
| 414 | }; |
| 415 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 416 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 417 | /* "Globals" within this file */ |
| 418 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 419 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 420 | struct globals { |
| 421 | #if ENABLE_HUSH_INTERACTIVE |
| 422 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 423 | * _AND_ if we decided to act interactively */ |
| 424 | int interactive_fd; |
| 425 | const char *PS1; |
| 426 | const char *PS2; |
| 427 | #endif |
| 428 | #if ENABLE_FEATURE_EDITING |
| 429 | line_input_t *line_input_state; |
| 430 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 431 | pid_t root_pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 432 | pid_t last_bg_pid; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 433 | #if ENABLE_HUSH_JOB |
| 434 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 435 | pid_t saved_tty_pgrp; |
| 436 | int last_jobid; |
| 437 | struct pipe *job_list; |
| 438 | struct pipe *toplevel_list; |
| 439 | smallint ctrl_z_flag; |
| 440 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 441 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 442 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 443 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 444 | smallint fake_mode; |
| 445 | /* these three support $?, $#, and $1 */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 446 | smalluint last_return_code; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 447 | char **global_argv; |
| 448 | int global_argc; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 449 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 450 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 451 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 452 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 453 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 454 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 455 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 456 | struct variable shell_ver; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 457 | #if ENABLE_FEATURE_SH_STANDALONE |
| 458 | struct nofork_save_area nofork_save; |
| 459 | #endif |
| 460 | #if ENABLE_HUSH_JOB |
| 461 | sigjmp_buf toplevel_jb; |
| 462 | #endif |
| 463 | unsigned char charmap[256]; |
| 464 | char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2]; |
| 465 | }; |
| 466 | |
| 467 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 468 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 469 | * (too many defines). Also, I actually prefer to see when a variable |
| 470 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 471 | #define INIT_G() do { \ |
| 472 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 473 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 474 | |
| 475 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 476 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
| 477 | |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 478 | #if 1 |
| 479 | /* Normal */ |
| 480 | static void syntax(const char *msg) |
| 481 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 482 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 483 | /* Was using fancy stuff: |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 484 | * (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...) |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 485 | * but it SEGVs. ?! Oh well... explicit temp ptr works around that */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 486 | void FAST_FUNC (*fp)(const char *s, ...); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 487 | fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die); |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 488 | fp(msg ? "%s: %s" : "syntax error", "syntax error", msg); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 489 | #else |
| 490 | bb_error_msg_and_die(msg ? "%s: %s" : "syntax error", "syntax error", msg); |
| 491 | #endif |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 492 | } |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 493 | |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 494 | #else |
| 495 | /* Debug */ |
| 496 | static void syntax_lineno(int line) |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 497 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 498 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 499 | void FAST_FUNC (*fp)(const char *s, ...); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 500 | fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die); |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 501 | fp("syntax error hush.c:%d", line); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 502 | #else |
| 503 | bb_error_msg_and_die("syntax error hush.c:%d", line); |
| 504 | #endif |
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: */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 525 | static int setup_redirects(struct command *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 |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 528 | #define pseudo_exec_argv(ptr_ptrs2free, argv, assignment_cnt, argv_expanded) \ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 529 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 530 | #define pseudo_exec(ptr_ptrs2free, command, argv_expanded) \ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 531 | pseudo_exec(command, argv_expanded) |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 532 | #endif |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 533 | static void pseudo_exec_argv(char ***ptr_ptrs2free, char **argv, int assignment_cnt, char **argv_expanded) NORETURN; |
| 534 | static void pseudo_exec(char ***ptr_ptrs2free, struct command *command, char **argv_expanded) NORETURN; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 535 | static int run_pipe(struct pipe *pi); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 536 | /* data structure manipulation: */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 537 | static int setup_redirect(struct parse_context *ctx, int fd, redir_type style, struct in_str *input); |
| 538 | static void initialize_context(struct parse_context *ctx); |
| 539 | static int done_word(o_string *dest, struct parse_context *ctx); |
| 540 | static int done_command(struct parse_context *ctx); |
| 541 | static void done_pipe(struct parse_context *ctx, pipe_style type); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 542 | /* primary string parsing: */ |
| 543 | static int redirect_dup_num(struct in_str *input); |
| 544 | static int redirect_opt_num(o_string *o); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 545 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 546 | static int process_command_subs(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 547 | struct in_str *input, const char *subst_end); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 548 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 549 | static int parse_group(o_string *dest, struct parse_context *ctx, struct in_str *input, int ch); |
Denis Vlasenko | 15d78fb | 2007-01-30 22:28:21 +0000 | [diff] [blame] | 550 | static const char *lookup_param(const char *src); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 551 | static int handle_dollar(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 552 | struct in_str *input); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 553 | static int parse_stream(o_string *dest, struct parse_context *ctx, struct in_str *input0, const char *end_trigger); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 554 | /* setup: */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 555 | static int parse_and_run_stream(struct in_str *inp, int parse_flag); |
| 556 | static int parse_and_run_string(const char *s, int parse_flag); |
| 557 | static int parse_and_run_file(FILE *f); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 558 | /* job management: */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 559 | static int checkjobs(struct pipe* fg_pipe); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 560 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 561 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 562 | static void insert_bg_job(struct pipe *pi); |
| 563 | static void remove_bg_job(struct pipe *pi); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 564 | static void delete_finished_bg_job(struct pipe *pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 565 | #else |
| 566 | int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */ |
| 567 | #endif |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 568 | /* local variable support */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 569 | static char **expand_strvec_to_strvec(char **argv); |
| 570 | /* used for eval */ |
| 571 | static char *expand_strvec_to_string(char **argv); |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 572 | /* used for expansion of right hand of assignments */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 573 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 574 | static struct variable *get_local_var(const char *name); |
| 575 | static int set_local_var(char *str, int flg_export); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 576 | static void unset_local_var(const char *name); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 577 | |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 578 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 579 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="HUSH_VER_STR; |
| 580 | |
| 581 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 582 | static int glob_needed(const char *s) |
| 583 | { |
| 584 | while (*s) { |
| 585 | if (*s == '\\') |
| 586 | s++; |
| 587 | if (*s == '*' || *s == '[' || *s == '?') |
| 588 | return 1; |
| 589 | s++; |
| 590 | } |
| 591 | return 0; |
| 592 | } |
| 593 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 594 | static int is_assignment(const char *s) |
| 595 | { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 596 | if (!s || !(isalpha(*s) || *s == '_')) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 597 | return 0; |
| 598 | s++; |
| 599 | while (isalnum(*s) || *s == '_') |
| 600 | s++; |
| 601 | return *s == '='; |
| 602 | } |
| 603 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 604 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 605 | static char *unbackslash(char *src) |
| 606 | { |
| 607 | char *dst = src; |
| 608 | while (1) { |
| 609 | if (*src == '\\') |
| 610 | src++; |
| 611 | if ((*dst++ = *src++) == '\0') |
| 612 | break; |
| 613 | } |
| 614 | return dst; |
| 615 | } |
| 616 | |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 617 | static char **add_strings_to_strings(char **strings, char **add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 618 | { |
| 619 | int i; |
| 620 | unsigned count1; |
| 621 | unsigned count2; |
| 622 | char **v; |
| 623 | |
| 624 | v = strings; |
| 625 | count1 = 0; |
| 626 | if (v) { |
| 627 | while (*v) { |
| 628 | count1++; |
| 629 | v++; |
| 630 | } |
| 631 | } |
| 632 | count2 = 0; |
| 633 | v = add; |
| 634 | while (*v) { |
| 635 | count2++; |
| 636 | v++; |
| 637 | } |
| 638 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 639 | v[count1 + count2] = NULL; |
| 640 | i = count2; |
| 641 | while (--i >= 0) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 642 | v[count1 + i] = add[i]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 643 | return v; |
| 644 | } |
| 645 | |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 646 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 647 | { |
| 648 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 649 | v[0] = add; |
| 650 | v[1] = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 651 | return add_strings_to_strings(strings, v); |
| 652 | } |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 653 | |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 654 | static void putenv_all(char **strings) |
| 655 | { |
| 656 | if (!strings) |
| 657 | return; |
| 658 | while (*strings) |
| 659 | putenv(*strings++); |
| 660 | } |
| 661 | |
| 662 | static char **putenv_all_and_save_old(char **strings) |
| 663 | { |
| 664 | char **old = NULL; |
| 665 | char **s = strings; |
| 666 | |
| 667 | if (!strings) |
| 668 | return old; |
| 669 | while (*strings) { |
| 670 | char *v = getenv(*strings++); |
| 671 | if (!v) |
| 672 | continue; |
| 673 | old = add_string_to_strings(old, v); |
| 674 | } |
| 675 | putenv_all(s); |
| 676 | return old; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 679 | static void free_strings_and_unsetenv(char **strings, int unset) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 680 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 681 | char **v; |
| 682 | |
| 683 | if (!strings) |
| 684 | return; |
| 685 | |
| 686 | v = strings; |
| 687 | while (*v) { |
| 688 | if (unset) { |
| 689 | char *copy; |
| 690 | #if !BB_MMU |
| 691 | /* If this is a magic guard pointer, do not free it, |
| 692 | * and stop unsetting */ |
| 693 | if (*v == hush_version_str) { |
| 694 | unset = 0; |
| 695 | v++; |
| 696 | continue; |
| 697 | } |
| 698 | #endif |
| 699 | /* *strchrnul(*v, '=') = '\0'; -- BAD |
| 700 | * In case *v was putenv'ed, we can't |
| 701 | * unsetenv(*v) after taking out '=': |
| 702 | * it won't work, env is modified by taking out! |
| 703 | * horror :( */ |
| 704 | copy = xstrndup(*v, strchrnul(*v, '=') - *v); |
| 705 | unsetenv(copy); |
| 706 | free(copy); |
| 707 | } |
| 708 | free(*v++); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 709 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 710 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 713 | static void free_strings(char **strings) |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 714 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 715 | free_strings_and_unsetenv(strings, 0); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 716 | } |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 717 | |
| 718 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 719 | /* Function prototypes for builtins */ |
| 720 | static int builtin_cd(char **argv); |
Denis Vlasenko | 5bc593c | 2007-11-23 21:20:21 +0000 | [diff] [blame] | 721 | static int builtin_echo(char **argv); |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 722 | static int builtin_eval(char **argv); |
| 723 | static int builtin_exec(char **argv); |
| 724 | static int builtin_exit(char **argv); |
| 725 | static int builtin_export(char **argv); |
| 726 | #if ENABLE_HUSH_JOB |
| 727 | static int builtin_fg_bg(char **argv); |
| 728 | static int builtin_jobs(char **argv); |
| 729 | #endif |
| 730 | #if ENABLE_HUSH_HELP |
| 731 | static int builtin_help(char **argv); |
| 732 | #endif |
| 733 | static int builtin_pwd(char **argv); |
| 734 | static int builtin_read(char **argv); |
| 735 | static int builtin_test(char **argv); |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 736 | static int builtin_true(char **argv); |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 737 | static int builtin_set(char **argv); |
| 738 | static int builtin_shift(char **argv); |
| 739 | static int builtin_source(char **argv); |
| 740 | static int builtin_umask(char **argv); |
| 741 | static int builtin_unset(char **argv); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 742 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 743 | static int builtin_break(char **argv); |
| 744 | static int builtin_continue(char **argv); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 745 | #endif |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 746 | //static int builtin_not_written(char **argv); |
| 747 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 748 | /* Table of built-in functions. They can be forked or not, depending on |
| 749 | * context: within pipes, they fork. As simple commands, they do not. |
| 750 | * When used in non-forking context, they can change global variables |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 751 | * in the parent shell process. If forked, of course they cannot. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 752 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 753 | * still be set at the end. */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 754 | struct built_in_command { |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 755 | const char *cmd; |
| 756 | int (*function)(char **argv); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 757 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 758 | const char *descr; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 759 | #define BLTIN(cmd, func, help) { cmd, func, help } |
| 760 | #else |
| 761 | #define BLTIN(cmd, func, help) { cmd, func } |
| 762 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 763 | }; |
| 764 | |
Denis Vlasenko | 5bc593c | 2007-11-23 21:20:21 +0000 | [diff] [blame] | 765 | /* For now, echo and test are unconditionally enabled. |
| 766 | * Maybe make it configurable? */ |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 767 | static const struct built_in_command bltins[] = { |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 768 | BLTIN("." , builtin_source, "Run commands in a file"), |
| 769 | BLTIN(":" , builtin_true, "No-op"), |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 770 | BLTIN("[" , builtin_test, "Test condition"), |
| 771 | BLTIN("[[" , builtin_test, "Test condition"), |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 772 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 773 | BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"), |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 774 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 775 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 776 | BLTIN("break" , builtin_break, "Exit from a loop"), |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 777 | #endif |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 778 | BLTIN("cd" , builtin_cd, "Change directory"), |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 779 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 780 | BLTIN("continue", builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 781 | #endif |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 782 | BLTIN("echo" , builtin_echo, "Write to stdout"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 783 | BLTIN("eval" , builtin_eval, "Construct and run shell command"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 784 | BLTIN("exec" , builtin_exec, "Execute command, don't return to shell"), |
| 785 | BLTIN("exit" , builtin_exit, "Exit"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 786 | BLTIN("export", builtin_export, "Set environment variable"), |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 787 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 788 | BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 789 | BLTIN("jobs" , builtin_jobs, "List active jobs"), |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 790 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 791 | BLTIN("pwd" , builtin_pwd, "Print current directory"), |
| 792 | BLTIN("read" , builtin_read, "Input environment variable"), |
| 793 | // BLTIN("return", builtin_not_written, "Return from a function"), |
| 794 | BLTIN("set" , builtin_set, "Set/unset shell local variables"), |
| 795 | BLTIN("shift" , builtin_shift, "Shift positional parameters"), |
| 796 | // BLTIN("trap" , builtin_not_written, "Trap signals"), |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 797 | BLTIN("test" , builtin_test, "Test condition"), |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 798 | // BLTIN("ulimit", builtin_not_written, "Control resource limits"), |
| 799 | BLTIN("umask" , builtin_umask, "Set file creation mask"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 800 | BLTIN("unset" , builtin_unset, "Unset environment variable"), |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 801 | #if ENABLE_HUSH_HELP |
| 802 | BLTIN("help" , builtin_help, "List shell built-in commands"), |
| 803 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 804 | }; |
| 805 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 806 | |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 807 | /* Signals are grouped, we handle them in batches */ |
| 808 | static void set_misc_sighandler(void (*handler)(int)) |
| 809 | { |
| 810 | bb_signals(0 |
| 811 | + (1 << SIGINT) |
| 812 | + (1 << SIGQUIT) |
| 813 | + (1 << SIGTERM) |
| 814 | , handler); |
| 815 | } |
| 816 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 817 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 818 | |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 819 | static void set_fatal_sighandler(void (*handler)(int)) |
| 820 | { |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 821 | bb_signals(0 |
| 822 | + (1 << SIGILL) |
| 823 | + (1 << SIGTRAP) |
| 824 | + (1 << SIGABRT) |
| 825 | + (1 << SIGFPE) |
| 826 | + (1 << SIGBUS) |
| 827 | + (1 << SIGSEGV) |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 828 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 829 | + (1 << SIGHUP) |
| 830 | + (1 << SIGPIPE) |
| 831 | + (1 << SIGALRM) |
| 832 | , handler); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 833 | } |
| 834 | static void set_jobctrl_sighandler(void (*handler)(int)) |
| 835 | { |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 836 | bb_signals(0 |
| 837 | + (1 << SIGTSTP) |
| 838 | + (1 << SIGTTIN) |
| 839 | + (1 << SIGTTOU) |
| 840 | , handler); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 841 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 842 | /* SIGCHLD is special and handled separately */ |
| 843 | |
| 844 | static void set_every_sighandler(void (*handler)(int)) |
| 845 | { |
| 846 | set_fatal_sighandler(handler); |
| 847 | set_jobctrl_sighandler(handler); |
| 848 | set_misc_sighandler(handler); |
| 849 | signal(SIGCHLD, handler); |
| 850 | } |
| 851 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 852 | static void handler_ctrl_c(int sig UNUSED_PARAM) |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 853 | { |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 854 | debug_printf_jobs("got sig %d\n", sig); |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 855 | // as usual we can have all kinds of nasty problems with leaked malloc data here |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 856 | siglongjmp(G.toplevel_jb, 1); |
Denis Vlasenko | b5eaabb | 2007-04-28 16:45:59 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 859 | static void handler_ctrl_z(int sig UNUSED_PARAM) |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 860 | { |
| 861 | pid_t pid; |
| 862 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 863 | 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] | 864 | pid = fork(); |
Denis Vlasenko | c299032 | 2007-05-16 12:57:12 +0000 | [diff] [blame] | 865 | if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */ |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 866 | return; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 867 | G.ctrl_z_flag = 1; |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 868 | if (!pid) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 869 | if (ENABLE_HUSH_JOB) |
| 870 | die_sleep = 0; /* let nofork's xfuncs die */ |
Bernhard Reutner-Fischer | 864329d | 2008-09-25 10:55:05 +0000 | [diff] [blame] | 871 | bb_setpgrp(); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 872 | debug_printf_jobs("set pgrp for child %d ok\n", getpid()); |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 873 | set_every_sighandler(SIG_DFL); |
Denis Vlasenko | 18e19f2 | 2007-04-28 16:43:18 +0000 | [diff] [blame] | 874 | raise(SIGTSTP); /* resend TSTP so that child will be stopped */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 875 | debug_printf_jobs("returning in child\n"); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 876 | /* return to nofork, it will eventually exit now, |
| 877 | * not return back to shell */ |
| 878 | return; |
| 879 | } |
| 880 | /* parent */ |
| 881 | /* finish filling up pipe info */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 882 | G.toplevel_list->pgrp = pid; /* child is in its own pgrp */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 883 | G.toplevel_list->cmds[0].pid = pid; |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 884 | /* parent needs to longjmp out of running nofork. |
| 885 | * we will "return" exitcode 0, with child put in background */ |
| 886 | // 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] | 887 | debug_printf_jobs("siglongjmp in parent\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 888 | siglongjmp(G.toplevel_jb, 1); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 891 | /* Restores tty foreground process group, and exits. |
| 892 | * May be called as signal handler for fatal signal |
| 893 | * (will faithfully resend signal to itself, producing correct exit state) |
| 894 | * or called directly with -EXITCODE. |
| 895 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 896 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 897 | static void sigexit(int sig) |
| 898 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 899 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 900 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 901 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 902 | #if ENABLE_HUSH_INTERACTIVE |
| 903 | if (G.interactive_fd) |
| 904 | tcsetpgrp(G.interactive_fd, G.saved_tty_pgrp); |
| 905 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 906 | |
| 907 | /* Not a signal, just exit */ |
| 908 | if (sig <= 0) |
| 909 | _exit(- sig); |
| 910 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 911 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | /* Restores tty foreground process group, and exits. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 915 | static void hush_exit(int exitcode) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 916 | static void hush_exit(int exitcode) |
| 917 | { |
| 918 | fflush(NULL); /* flush all streams */ |
| 919 | sigexit(- (exitcode & 0xff)); |
| 920 | } |
| 921 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 922 | #else /* !JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 923 | |
| 924 | #define set_fatal_sighandler(handler) ((void)0) |
| 925 | #define set_jobctrl_sighandler(handler) ((void)0) |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 926 | #define hush_exit(e) exit(e) |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 927 | |
Denis Vlasenko | ef36ead | 2007-05-02 15:34:47 +0000 | [diff] [blame] | 928 | #endif /* JOB */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 929 | |
| 930 | |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 931 | static const char *set_cwd(void) |
| 932 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 933 | if (G.cwd == bb_msg_unknown) |
| 934 | G.cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */ |
| 935 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 936 | if (!G.cwd) |
| 937 | G.cwd = bb_msg_unknown; |
| 938 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 941 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 942 | /* |
| 943 | * o_string support |
| 944 | */ |
| 945 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 946 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 947 | static void o_reset(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 948 | { |
| 949 | o->length = 0; |
| 950 | o->nonnull = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 951 | if (o->data) |
| 952 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 955 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 956 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 957 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 958 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 961 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 962 | { |
| 963 | if (o->length + len > o->maxlen) { |
| 964 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 965 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 966 | } |
| 967 | } |
| 968 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 969 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 970 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 971 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 972 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 973 | o->data[o->length] = ch; |
| 974 | o->length++; |
| 975 | o->data[o->length] = '\0'; |
| 976 | } |
| 977 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 978 | static void o_addstr(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 979 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 980 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 981 | memcpy(&o->data[o->length], str, len); |
| 982 | o->length += len; |
| 983 | o->data[o->length] = '\0'; |
| 984 | } |
| 985 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 986 | static void o_addstr_duplicate_backslash(o_string *o, const char *str, int len) |
| 987 | { |
| 988 | while (len) { |
| 989 | o_addchr(o, *str); |
| 990 | if (*str++ == '\\' |
| 991 | && (*str != '*' && *str != '?' && *str != '[') |
| 992 | ) { |
| 993 | o_addchr(o, '\\'); |
| 994 | } |
| 995 | len--; |
| 996 | } |
| 997 | } |
| 998 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 999 | /* My analysis of quoting semantics tells me that state information |
| 1000 | * is associated with a destination, not a source. |
| 1001 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1002 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1003 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1004 | int sz = 1; |
| 1005 | if (strchr("*?[\\", ch)) { |
| 1006 | sz++; |
| 1007 | o->data[o->length] = '\\'; |
| 1008 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1009 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1010 | o_grow_by(o, sz); |
| 1011 | o->data[o->length] = ch; |
| 1012 | o->length++; |
| 1013 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1016 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1017 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1018 | int sz = 1; |
| 1019 | if (o->o_quote && strchr("*?[\\", ch)) { |
| 1020 | sz++; |
| 1021 | o->data[o->length] = '\\'; |
| 1022 | o->length++; |
| 1023 | } |
| 1024 | o_grow_by(o, sz); |
| 1025 | o->data[o->length] = ch; |
| 1026 | o->length++; |
| 1027 | o->data[o->length] = '\0'; |
| 1028 | } |
| 1029 | |
| 1030 | static void o_addQstr(o_string *o, const char *str, int len) |
| 1031 | { |
| 1032 | if (!o->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1033 | o_addstr(o, str, len); |
| 1034 | return; |
| 1035 | } |
| 1036 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1037 | char ch; |
| 1038 | int sz; |
| 1039 | int ordinary_cnt = strcspn(str, "*?[\\"); |
| 1040 | if (ordinary_cnt > len) /* paranoia */ |
| 1041 | ordinary_cnt = len; |
| 1042 | o_addstr(o, str, ordinary_cnt); |
| 1043 | if (ordinary_cnt == len) |
| 1044 | return; |
| 1045 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1046 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1047 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1048 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1049 | sz = 1; |
| 1050 | if (ch) { /* it is necessarily one of "*?[\\" */ |
| 1051 | sz++; |
| 1052 | o->data[o->length] = '\\'; |
| 1053 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1054 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 1055 | o_grow_by(o, sz); |
| 1056 | o->data[o->length] = ch; |
| 1057 | o->length++; |
| 1058 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 1059 | } |
| 1060 | } |
| 1061 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1062 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 1063 | * 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] | 1064 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1065 | * list[i] contains an INDEX (int!) into this string data. |
| 1066 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 1067 | * but list[i]'s need not be modified. |
| 1068 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1069 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1070 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 1071 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1072 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 1073 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 1074 | { |
| 1075 | char **list = (char**)o->data; |
| 1076 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1077 | int i = 0; |
| 1078 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 1079 | prefix, list, n, string_start, o->length, o->maxlen); |
| 1080 | while (i < n) { |
| 1081 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 1082 | o->data + (int)list[i] + string_start, |
| 1083 | o->data + (int)list[i] + string_start); |
| 1084 | i++; |
| 1085 | } |
| 1086 | if (n) { |
| 1087 | const char *p = o->data + (int)list[n - 1] + string_start; |
| 1088 | fprintf(stderr, " total_sz:%d\n", (p + strlen(p) + 1) - o->data); |
| 1089 | } |
| 1090 | } |
| 1091 | #else |
| 1092 | #define debug_print_list(prefix, o, n) ((void)0) |
| 1093 | #endif |
| 1094 | |
| 1095 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 1096 | * in list[n] so that it points past last stored byte so far. |
| 1097 | * It returns n+1. */ |
| 1098 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1099 | { |
| 1100 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1101 | int string_start; |
| 1102 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1103 | |
| 1104 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1105 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1106 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1107 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1108 | 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] | 1109 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 1110 | o->maxlen += 0x10 * sizeof(list[0]); |
| 1111 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 1112 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1113 | memmove(list + n + 0x10, list + n, string_len); |
| 1114 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1115 | } else |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1116 | 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] | 1117 | } else { |
| 1118 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 1119 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 1120 | string_len = o->length - string_start; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1121 | 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] | 1122 | o->has_empty_slot = 0; |
| 1123 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1124 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1125 | return n + 1; |
| 1126 | } |
| 1127 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1128 | /* "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] | 1129 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1130 | { |
| 1131 | char **list = (char**)o->data; |
| 1132 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1133 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1134 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1137 | /* o_glob performs globbing on last list[], saving each result |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1138 | * as a new list[]. */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1139 | static int o_glob(o_string *o, int n) |
| 1140 | { |
| 1141 | glob_t globdata; |
| 1142 | int gr; |
| 1143 | char *pattern; |
| 1144 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1145 | 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] | 1146 | if (!o->data) |
| 1147 | return o_save_ptr_helper(o, n); |
| 1148 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1149 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1150 | if (!glob_needed(pattern)) { |
| 1151 | literal: |
| 1152 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1153 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1154 | return o_save_ptr_helper(o, n); |
| 1155 | } |
| 1156 | |
| 1157 | memset(&globdata, 0, sizeof(globdata)); |
| 1158 | gr = glob(pattern, 0, NULL, &globdata); |
| 1159 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 1160 | if (gr == GLOB_NOSPACE) |
| 1161 | bb_error_msg_and_die("out of memory during glob"); |
| 1162 | if (gr == GLOB_NOMATCH) { |
| 1163 | globfree(&globdata); |
| 1164 | goto literal; |
| 1165 | } |
| 1166 | if (gr != 0) { /* GLOB_ABORTED ? */ |
| 1167 | //TODO: testcase for bad glob pattern behavior |
| 1168 | bb_error_msg("glob(3) error %d on '%s'", gr, pattern); |
| 1169 | } |
| 1170 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 1171 | char **argv = globdata.gl_pathv; |
| 1172 | o->length = pattern - o->data; /* "forget" pattern */ |
| 1173 | while (1) { |
| 1174 | o_addstr(o, *argv, strlen(*argv) + 1); |
| 1175 | n = o_save_ptr_helper(o, n); |
| 1176 | argv++; |
| 1177 | if (!*argv) |
| 1178 | break; |
| 1179 | } |
| 1180 | } |
| 1181 | globfree(&globdata); |
| 1182 | if (DEBUG_GLOB) |
| 1183 | debug_print_list("o_glob returning", o, n); |
| 1184 | return n; |
| 1185 | } |
| 1186 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1187 | /* If o->o_glob == 1, glob the string so far remembered. |
| 1188 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1189 | static int o_save_ptr(o_string *o, int n) |
| 1190 | { |
| 1191 | if (o->o_glob) |
| 1192 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 1193 | return o_save_ptr_helper(o, n); |
| 1194 | } |
| 1195 | |
| 1196 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1197 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1198 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1199 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1200 | int string_start; |
| 1201 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1202 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 1203 | if (DEBUG_EXPAND) |
| 1204 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 1205 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1206 | list = (char**)o->data; |
| 1207 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 1208 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1209 | while (n) { |
| 1210 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 1211 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1212 | } |
| 1213 | return list; |
| 1214 | } |
| 1215 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1216 | |
| 1217 | /* |
| 1218 | * in_str support |
| 1219 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1220 | static int static_get(struct in_str *i) |
| 1221 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1222 | int ch = *i->p++; |
| 1223 | if (ch == '\0') return EOF; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1224 | return ch; |
| 1225 | } |
| 1226 | |
| 1227 | static int static_peek(struct in_str *i) |
| 1228 | { |
| 1229 | return *i->p; |
| 1230 | } |
| 1231 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1232 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1233 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1234 | #if ENABLE_FEATURE_EDITING |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 1235 | static void cmdedit_set_initial_prompt(void) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1236 | { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1237 | #if !ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1238 | G.PS1 = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1239 | #else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1240 | G.PS1 = getenv("PS1"); |
| 1241 | if (G.PS1 == NULL) |
| 1242 | G.PS1 = "\\w \\$ "; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1243 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1244 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1245 | #endif /* EDITING */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1246 | |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1247 | static const char* setup_prompt_string(int promptmode) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1248 | { |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1249 | const char *prompt_str; |
| 1250 | debug_printf("setup_prompt_string %d ", promptmode); |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1251 | #if !ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1252 | /* Set up the prompt */ |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1253 | if (promptmode == 0) { /* PS1 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1254 | free((char*)G.PS1); |
| 1255 | G.PS1 = xasprintf("%s %c ", G.cwd, (geteuid() != 0) ? '$' : '#'); |
| 1256 | prompt_str = G.PS1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1257 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1258 | prompt_str = G.PS2; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1259 | } |
| 1260 | #else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1261 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Eric Andersen | af44a0e | 2001-04-27 07:26:12 +0000 | [diff] [blame] | 1262 | #endif |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1263 | debug_printf("result '%s'\n", prompt_str); |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1264 | return prompt_str; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1267 | static void get_user_input(struct in_str *i) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1268 | { |
Denis Vlasenko | 0937be5 | 2007-04-28 16:47:08 +0000 | [diff] [blame] | 1269 | int r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1270 | const char *prompt_str; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1271 | |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 1272 | prompt_str = setup_prompt_string(i->promptmode); |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 1273 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1274 | /* Enable command line editing only while a command line |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 1275 | * is actually being read */ |
| 1276 | do { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1277 | r = read_line_input(prompt_str, G.user_input_buf, BUFSIZ-1, G.line_input_state); |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 1278 | } while (r == 0); /* repeat if Ctrl-C */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1279 | i->eof_flag = (r < 0); |
| 1280 | if (i->eof_flag) { /* EOF/error detected */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1281 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1282 | G.user_input_buf[1] = '\0'; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1283 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1284 | #else |
| 1285 | fputs(prompt_str, stdout); |
| 1286 | fflush(stdout); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1287 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1288 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1289 | i->eof_flag = (r == EOF); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1290 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1291 | i->p = G.user_input_buf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1292 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1293 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1294 | #endif /* INTERACTIVE */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1295 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1296 | /* This is the magic location that prints prompts |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1297 | * and gets data back from the user */ |
| 1298 | static int file_get(struct in_str *i) |
| 1299 | { |
| 1300 | int ch; |
| 1301 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1302 | /* If there is data waiting, eat it up */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1303 | if (i->p && *i->p) { |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1304 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1305 | take_cached: |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1306 | #endif |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1307 | ch = *i->p++; |
| 1308 | if (i->eof_flag && !*i->p) |
| 1309 | ch = EOF; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1310 | } else { |
| 1311 | /* need to double check i->file because we might be doing something |
| 1312 | * more complicated by now, like sourcing or substituting. */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1313 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1314 | if (G.interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1315 | do { |
| 1316 | get_user_input(i); |
| 1317 | } while (!*i->p); /* need non-empty line */ |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1318 | i->promptmode = 1; /* PS2 */ |
| 1319 | i->promptme = 0; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1320 | goto take_cached; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1321 | } |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 1322 | #endif |
| 1323 | ch = fgetc(i->file); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1324 | } |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1325 | debug_printf("file_get: got a '%c' %d\n", ch, ch); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1326 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1327 | if (ch == '\n') |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1328 | i->promptme = 1; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1329 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1330 | return ch; |
| 1331 | } |
| 1332 | |
| 1333 | /* All the callers guarantee this routine will never be |
| 1334 | * used right after a newline, so prompting is not needed. |
| 1335 | */ |
| 1336 | static int file_peek(struct in_str *i) |
| 1337 | { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 1338 | int ch; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1339 | if (i->p && *i->p) { |
| 1340 | if (i->eof_flag && !i->p[1]) |
| 1341 | return EOF; |
| 1342 | return *i->p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1343 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 1344 | ch = fgetc(i->file); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1345 | i->eof_flag = (ch == EOF); |
| 1346 | i->peek_buf[0] = ch; |
| 1347 | i->peek_buf[1] = '\0'; |
| 1348 | i->p = i->peek_buf; |
| 1349 | 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] | 1350 | return ch; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1354 | { |
| 1355 | i->peek = file_peek; |
| 1356 | i->get = file_get; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1357 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1358 | i->promptme = 1; |
| 1359 | i->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1360 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1361 | i->file = f; |
| 1362 | i->p = NULL; |
| 1363 | } |
| 1364 | |
| 1365 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1366 | { |
| 1367 | i->peek = static_peek; |
| 1368 | i->get = static_get; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1369 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 1370 | i->promptme = 1; |
| 1371 | i->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1372 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1373 | i->p = s; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 1374 | i->eof_flag = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1377 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1378 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 1379 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1380 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1381 | { |
| 1382 | int openfd, mode; |
| 1383 | struct redir_struct *redir; |
| 1384 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1385 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1386 | if (redir->dup == -1 && redir->rd_filename == NULL) { |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 1387 | /* something went wrong in the parse. Pretend it didn't happen */ |
| 1388 | continue; |
| 1389 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1390 | if (redir->dup == -1) { |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1391 | char *p; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1392 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 1393 | //TODO: check redir for names like '\\' |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 1394 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 1395 | openfd = open_or_warn(p, mode); |
| 1396 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1397 | if (openfd < 0) { |
| 1398 | /* this could get lost if stderr has been redirected, but |
| 1399 | bash and ash both lose it as well (though zsh doesn't!) */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1400 | return 1; |
| 1401 | } |
| 1402 | } else { |
| 1403 | openfd = redir->dup; |
| 1404 | } |
| 1405 | |
| 1406 | if (openfd != redir->fd) { |
| 1407 | if (squirrel && redir->fd < 3) { |
| 1408 | squirrel[redir->fd] = dup(redir->fd); |
| 1409 | } |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1410 | if (openfd == -3) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1411 | //close(openfd); // close(-3) ??! |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1412 | } else { |
| 1413 | dup2(openfd, redir->fd); |
Matt Kraai | c616e53 | 2001-06-05 16:50:08 +0000 | [diff] [blame] | 1414 | if (redir->dup == -1) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1415 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 1416 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1417 | } |
| 1418 | } |
| 1419 | return 0; |
| 1420 | } |
| 1421 | |
| 1422 | static void restore_redirects(int squirrel[]) |
| 1423 | { |
| 1424 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1425 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1426 | fd = squirrel[i]; |
| 1427 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 1428 | /* We simply die on error */ |
| 1429 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 1434 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1435 | /* Called after [v]fork() in run_pipe(), or from builtin_exec(). |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1436 | * Never returns. |
| 1437 | * XXX no exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1438 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1439 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1440 | static void pseudo_exec_argv(char ***ptr_ptrs2free, char **argv, int assignment_cnt, char **argv_expanded) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1441 | { |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1442 | int i, rcode; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 1443 | char *p; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 1444 | const struct built_in_command *x; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1445 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1446 | /* If a variable is assigned in a forest, and nobody listens, |
| 1447 | * was it ever really set? |
| 1448 | */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1449 | if (!argv[assignment_cnt]) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1450 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 1451 | |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1452 | for (i = 0; i < assignment_cnt; i++) { |
| 1453 | debug_printf_exec("pid %d environment modification: %s\n", |
| 1454 | getpid(), *argv); |
| 1455 | p = expand_string_to_string(*argv); |
| 1456 | putenv(p); |
| 1457 | #if !BB_MMU |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 1458 | *ptr_ptrs2free = add_string_to_strings(*ptr_ptrs2free, p); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1459 | #endif |
| 1460 | argv++; |
| 1461 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1462 | if (argv_expanded) { |
| 1463 | argv = argv_expanded; |
| 1464 | } else { |
| 1465 | argv = expand_strvec_to_strvec(argv); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1466 | #if !BB_MMU |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1467 | /* Inserting magic guard pointer to not unsetenv junk later */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 1468 | *ptr_ptrs2free = add_string_to_strings(*ptr_ptrs2free, (char*)hush_version_str); |
| 1469 | *ptr_ptrs2free = add_string_to_strings(*ptr_ptrs2free, (char*)argv); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1470 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1471 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 1472 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1473 | /* |
| 1474 | * Check if the command matches any of the builtins. |
| 1475 | * Depending on context, this might be redundant. But it's |
| 1476 | * easier to waste a few CPU cycles than it is to figure out |
| 1477 | * if this is one of those cases. |
| 1478 | */ |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 1479 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1480 | if (strcmp(argv[0], x->cmd) == 0) { |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 1481 | debug_printf_exec("running builtin '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1482 | rcode = x->function(argv); |
| 1483 | fflush(stdout); |
| 1484 | _exit(rcode); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1485 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1486 | } |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1487 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1488 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 1489 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1490 | if (strchr(argv[0], '/') == NULL) { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 1491 | int a = find_applet_by_name(argv[0]); |
| 1492 | if (a >= 0) { |
| 1493 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1494 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 1495 | // is it ok that run_applet_no_and_exit() does exit(), not _exit()? |
| 1496 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1497 | } |
| 1498 | /* re-exec ourselves with the new arguments */ |
| 1499 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | bdbbb7e | 2007-06-08 15:02:55 +0000 | [diff] [blame] | 1500 | execvp(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1501 | /* If they called chroot or otherwise made the binary no longer |
| 1502 | * executable, fall through */ |
| 1503 | } |
| 1504 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 1505 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1506 | |
| 1507 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1508 | execvp(argv[0], argv); |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 1509 | bb_perror_msg("can't exec '%s'", argv[0]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 1510 | _exit(EXIT_FAILURE); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1513 | /* Called after [v]fork() in run_pipe() |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1514 | */ |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1515 | static void pseudo_exec(char ***ptr_ptrs2free, struct command *command, char **argv_expanded) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1516 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1517 | if (command->argv) |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1518 | pseudo_exec_argv(ptr_ptrs2free, command->argv, command->assignment_cnt, argv_expanded); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1519 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1520 | if (command->group) { |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1521 | #if !BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 1522 | bb_error_msg_and_die("nested lists are not supported on NOMMU"); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1523 | #else |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 1524 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1525 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1526 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 1527 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1528 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 1529 | _exit(rcode); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1530 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1531 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1532 | |
| 1533 | /* Can happen. See what bash does with ">foo" by itself. */ |
| 1534 | debug_printf("trying to pseudo_exec null command\n"); |
| 1535 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1538 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1539 | static const char *get_cmdtext(struct pipe *pi) |
| 1540 | { |
| 1541 | char **argv; |
| 1542 | char *p; |
| 1543 | int len; |
| 1544 | |
| 1545 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 1546 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1547 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1548 | if (pi->cmdtext) |
| 1549 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1550 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 1551 | if (!argv || !argv[0]) { |
| 1552 | pi->cmdtext = xzalloc(1); |
| 1553 | return pi->cmdtext; |
| 1554 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1555 | |
| 1556 | len = 0; |
| 1557 | do len += strlen(*argv) + 1; while (*++argv); |
| 1558 | pi->cmdtext = p = xmalloc(len); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1559 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1560 | do { |
| 1561 | len = strlen(*argv); |
| 1562 | memcpy(p, *argv, len); |
| 1563 | p += len; |
| 1564 | *p++ = ' '; |
| 1565 | } while (*++argv); |
| 1566 | p[-1] = '\0'; |
| 1567 | return pi->cmdtext; |
| 1568 | } |
| 1569 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1570 | static void insert_bg_job(struct pipe *pi) |
| 1571 | { |
| 1572 | struct pipe *thejob; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1573 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1574 | |
| 1575 | /* Linear search for the ID of the job to use */ |
| 1576 | pi->jobid = 1; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1577 | for (thejob = G.job_list; thejob; thejob = thejob->next) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1578 | if (thejob->jobid >= pi->jobid) |
| 1579 | pi->jobid = thejob->jobid + 1; |
| 1580 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1581 | /* Add thejob to the list of running jobs */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1582 | if (!G.job_list) { |
| 1583 | thejob = G.job_list = xmalloc(sizeof(*thejob)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1584 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1585 | for (thejob = G.job_list; thejob->next; thejob = thejob->next) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1586 | continue; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1587 | thejob->next = xmalloc(sizeof(*thejob)); |
| 1588 | thejob = thejob->next; |
| 1589 | } |
| 1590 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1591 | /* Physically copy the struct job */ |
Eric Andersen | 0fcd447 | 2001-05-02 20:12:03 +0000 | [diff] [blame] | 1592 | memcpy(thejob, pi, sizeof(struct pipe)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1593 | thejob->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 1594 | /* We cannot copy entire pi->cmds[] vector! Double free()s will happen */ |
| 1595 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1596 | // TODO: do we really need to have so many fields which are just dead weight |
| 1597 | // at execution stage? |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1598 | thejob->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 1599 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1600 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1601 | thejob->next = NULL; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1602 | thejob->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1603 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1604 | /* We don't wait for background thejobs to return -- append it |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1605 | to the list of backgrounded thejobs and leave it alone */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1606 | printf("[%d] %d %s\n", thejob->jobid, thejob->cmds[0].pid, thejob->cmdtext); |
| 1607 | G.last_bg_pid = thejob->cmds[0].pid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1608 | G.last_jobid = thejob->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1611 | static void remove_bg_job(struct pipe *pi) |
| 1612 | { |
| 1613 | struct pipe *prev_pipe; |
| 1614 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1615 | if (pi == G.job_list) { |
| 1616 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1617 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1618 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1619 | while (prev_pipe->next != pi) |
| 1620 | prev_pipe = prev_pipe->next; |
| 1621 | prev_pipe->next = pi->next; |
| 1622 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1623 | if (G.job_list) |
| 1624 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 1625 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1626 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1627 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 1628 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1629 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1630 | static void delete_finished_bg_job(struct pipe *pi) |
| 1631 | { |
| 1632 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1633 | pi->stopped_cmds = 0; |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 1634 | free_pipe(pi, 0); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1635 | free(pi); |
| 1636 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1637 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1638 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1639 | /* Check to see if any processes have exited -- if they |
| 1640 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1641 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1642 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1643 | int attributes; |
| 1644 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1645 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1646 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1647 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1648 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1649 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1650 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1651 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1652 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1653 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1654 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1655 | /* Do we do this right? |
| 1656 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 1657 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1658 | * [3]+ Stopped sleep 20 | false |
| 1659 | * bash-3.00# echo $? |
| 1660 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 1661 | */ |
| 1662 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1663 | //FIXME: non-interactive bash does not continue even if all processes in fg pipe |
| 1664 | //are stopped. Testcase: "cat | cat" in a script (not on command line) |
| 1665 | // + killall -STOP cat |
| 1666 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1667 | wait_more: |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 1668 | // TODO: safe_waitpid? |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1669 | while ((childpid = waitpid(-1, &status, attributes)) > 0) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1670 | int i; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1671 | const int dead = WIFEXITED(status) || WIFSIGNALED(status); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 1672 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1673 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1674 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1675 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 1676 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1677 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1678 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 1679 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 1680 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1681 | childpid, WEXITSTATUS(status)); |
| 1682 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1683 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1684 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1685 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 1686 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 1687 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1688 | continue; |
| 1689 | /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */ |
| 1690 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1691 | fg_pipe->cmds[i].pid = 0; |
| 1692 | fg_pipe->alive_cmds--; |
| 1693 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1694 | /* last process gives overall exitstatus */ |
| 1695 | rcode = WEXITSTATUS(status); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 1696 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1697 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1698 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1699 | fg_pipe->cmds[i].is_stopped = 1; |
| 1700 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1701 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1702 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 1703 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 1704 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1705 | /* All processes in fg pipe have exited/stopped */ |
| 1706 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1707 | if (fg_pipe->alive_cmds) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1708 | insert_bg_job(fg_pipe); |
| 1709 | #endif |
| 1710 | return rcode; |
| 1711 | } |
| 1712 | /* There are still running processes in the fg pipe */ |
| 1713 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1714 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1715 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1718 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1719 | /* We asked to wait for bg or orphaned children */ |
| 1720 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1721 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1722 | for (i = 0; i < pi->num_cmds; i++) { |
| 1723 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 1724 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1725 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1726 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 1727 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 1728 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1729 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 1730 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 1731 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1732 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1733 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1734 | pi->cmds[i].pid = 0; |
| 1735 | pi->alive_cmds--; |
| 1736 | if (!pi->alive_cmds) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 1737 | printf(JOB_STATUS_FORMAT, pi->jobid, |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1738 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1739 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1740 | } |
| 1741 | } else { |
| 1742 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1743 | pi->cmds[i].is_stopped = 1; |
| 1744 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1745 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1746 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 1747 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 1748 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1749 | /* wait found no children or failed */ |
| 1750 | |
| 1751 | if (childpid && errno != ECHILD) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1752 | bb_perror_msg("waitpid"); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1753 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1756 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1757 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 1758 | { |
| 1759 | pid_t p; |
| 1760 | int rcode = checkjobs(fg_pipe); |
| 1761 | /* Job finished, move the shell to the foreground */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1762 | p = getpgid(0); /* pgid of our process */ |
| 1763 | debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1764 | tcsetpgrp(G.interactive_fd, p); |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1765 | return rcode; |
| 1766 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1767 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 1768 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1769 | /* run_pipe() starts all the jobs, but doesn't wait for anything |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 1770 | * to finish. See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1771 | * |
| 1772 | * return code is normally -1, when the caller has to wait for children |
| 1773 | * to finish to determine the exit status of the pipe. If the pipe |
| 1774 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1775 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1776 | * return value. |
| 1777 | * |
| 1778 | * The input of the pipe is always stdin, the output is always |
| 1779 | * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus, |
| 1780 | * because it tries to avoid running the command substitution in |
| 1781 | * subshell, when that is in fact necessary. The subshell process |
| 1782 | * now has its stdout directed to the input of the appropriate pipe, |
| 1783 | * so this routine is noticeably simpler. |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1784 | * |
| 1785 | * Returns -1 only if started some children. IOW: we have to |
| 1786 | * mask out retvals of builtins etc with 0xff! |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1787 | */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 1788 | /* A little helper first */ |
| 1789 | static char **expand_assignments(char **argv, int count) |
| 1790 | { |
| 1791 | int i; |
| 1792 | char **p = NULL; |
| 1793 | /* Expand assignments into one string each */ |
| 1794 | for (i = 0; i < count; i++) { |
| 1795 | p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 1796 | } |
| 1797 | return p; |
| 1798 | } |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1799 | static int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1800 | { |
| 1801 | int i; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1802 | int nextin; |
| 1803 | int pipefds[2]; /* pipefds[0] is for reading */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1804 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 1805 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1806 | char **argv; |
"Vladimir N. Oleynik" | 485d7cb | 2005-10-17 09:48:57 +0000 | [diff] [blame] | 1807 | const struct built_in_command *x; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 1808 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1809 | /* it is not always needed, but we aim to smaller code */ |
| 1810 | int squirrel[] = { -1, -1, -1 }; |
| 1811 | int rcode; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1812 | const int single_and_fg = (pi->num_cmds == 1 && pi->followup != PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1813 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1814 | debug_printf_exec("run_pipe start: single_and_fg=%d\n", single_and_fg); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 1815 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1816 | #if ENABLE_HUSH_JOB |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 1817 | pi->pgrp = -1; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1818 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1819 | pi->alive_cmds = 1; |
| 1820 | pi->stopped_cmds = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1821 | |
| 1822 | /* Check if this is a simple builtin (not part of a pipe). |
| 1823 | * Builtins within pipes have to fork anyway, and are handled in |
| 1824 | * pseudo_exec. "echo foo | read bar" doesn't work on bash, either. |
| 1825 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1826 | command = &(pi->cmds[0]); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 1827 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1828 | if (single_and_fg && command->group && command->subshell == 0) { |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 1829 | debug_printf("non-subshell grouping\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1830 | setup_redirects(command, squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1831 | debug_printf_exec(": run_list\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1832 | rcode = run_list(command->group) & 0xff; |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 1833 | restore_redirects(squirrel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1834 | debug_printf_exec("run_pipe return %d\n", rcode); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 1835 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1836 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1839 | argv = command->argv; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 1840 | argv_expanded = NULL; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1841 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1842 | if (single_and_fg && argv != NULL) { |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 1843 | char **new_env = NULL; |
| 1844 | char **old_env = NULL; |
| 1845 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1846 | i = command->assignment_cnt; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1847 | if (i != 0 && argv[i] == NULL) { |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1848 | /* assignments, but no command: set local environment */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 1849 | for (i = 0; argv[i] != NULL; i++) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 1850 | debug_printf("local environment set: %s\n", argv[i]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 1851 | p = expand_string_to_string(argv[i]); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 1852 | set_local_var(p, 0); |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1853 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1854 | return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */ |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 1855 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1856 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1857 | /* Expand the rest into (possibly) many strings each */ |
| 1858 | argv_expanded = expand_strvec_to_strvec(argv + i); |
| 1859 | |
Denis Vlasenko | dd316dd | 2008-06-14 15:50:55 +0000 | [diff] [blame] | 1860 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 1861 | if (strcmp(argv_expanded[0], x->cmd) != 0) |
| 1862 | continue; |
| 1863 | if (x->function == builtin_exec && argv_expanded[1] == NULL) { |
| 1864 | debug_printf("exec with redirects only\n"); |
| 1865 | setup_redirects(command, NULL); |
| 1866 | rcode = EXIT_SUCCESS; |
| 1867 | goto clean_up_and_ret1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1868 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 1869 | debug_printf("builtin inline %s\n", argv_expanded[0]); |
| 1870 | /* XXX setup_redirects acts on file descriptors, not FILEs. |
| 1871 | * This is perfect for work that comes after exec(). |
| 1872 | * Is it really safe for inline use? Experimentally, |
| 1873 | * things seem to work with glibc. */ |
| 1874 | setup_redirects(command, squirrel); |
| 1875 | new_env = expand_assignments(argv, command->assignment_cnt); |
| 1876 | old_env = putenv_all_and_save_old(new_env); |
| 1877 | debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv_expanded[1]); |
| 1878 | rcode = x->function(argv_expanded) & 0xff; |
| 1879 | USE_FEATURE_SH_STANDALONE(clean_up_and_ret:) |
| 1880 | restore_redirects(squirrel); |
| 1881 | free_strings_and_unsetenv(new_env, 1); |
| 1882 | putenv_all(old_env); |
| 1883 | free_strings(old_env); |
| 1884 | clean_up_and_ret1: |
| 1885 | free(argv_expanded); |
| 1886 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 1887 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 1888 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1889 | } |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1890 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 1891 | i = find_applet_by_name(argv_expanded[0]); |
| 1892 | if (i >= 0 && APPLET_IS_NOFORK(i)) { |
| 1893 | setup_redirects(command, squirrel); |
| 1894 | save_nofork_data(&G.nofork_save); |
| 1895 | new_env = expand_assignments(argv, command->assignment_cnt); |
| 1896 | old_env = putenv_all_and_save_old(new_env); |
| 1897 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]); |
| 1898 | rcode = run_nofork_applet_prime(&G.nofork_save, i, argv_expanded); |
| 1899 | goto clean_up_and_ret; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 1900 | } |
| 1901 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1904 | /* NB: argv_expanded may already be created, and that |
| 1905 | * might include `cmd` runs! Do not rerun it! We *must* |
| 1906 | * use argv_expanded if it's non-NULL */ |
| 1907 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1908 | /* Disable job control signals for shell (parent) and |
| 1909 | * for initial child code after fork */ |
| 1910 | set_jobctrl_sighandler(SIG_IGN); |
| 1911 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1912 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1913 | pi->alive_cmds = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1914 | nextin = 0; |
| 1915 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1916 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1917 | #if !BB_MMU |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1918 | /* Avoid confusion WHAT is volatile. Pointer is volatile, |
| 1919 | * not the stuff it points to. */ |
| 1920 | typedef char **ppchar_t; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 1921 | volatile ppchar_t shared_across_vfork = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1922 | #endif |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1923 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1924 | command = &(pi->cmds[i]); |
| 1925 | if (command->argv) { |
| 1926 | debug_printf_exec(": pipe member '%s' '%s'...\n", command->argv[0], command->argv[1]); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1927 | } else |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 1928 | debug_printf_exec(": pipe member with no argv\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1929 | |
| 1930 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1931 | pipefds[0] = 0; |
| 1932 | pipefds[1] = 1; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1933 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1934 | xpipe(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1935 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1936 | command->pid = BB_MMU ? fork() : vfork(); |
| 1937 | if (!command->pid) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 1938 | if (ENABLE_HUSH_JOB) |
| 1939 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1940 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1941 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1942 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1943 | if (G.run_list_level == 1 && G.interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1944 | pid_t pgrp; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 1945 | /* Don't do pgrp restore anymore on fatal signals */ |
| 1946 | set_fatal_sighandler(SIG_DFL); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 1947 | pgrp = pi->pgrp; |
| 1948 | if (pgrp < 0) /* true for 1st process only */ |
| 1949 | pgrp = getpid(); |
| 1950 | if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1951 | /* We do it in *every* child, not just first, |
| 1952 | * to avoid races */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1953 | tcsetpgrp(G.interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1954 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1955 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1956 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 1957 | xmove_fd(nextin, 0); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1958 | xmove_fd(pipefds[1], 1); /* write end */ |
| 1959 | if (pipefds[0] > 1) |
| 1960 | close(pipefds[0]); /* read end */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1961 | /* Like bash, explicit redirects override pipes, |
| 1962 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1963 | setup_redirects(command, NULL); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 1964 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1965 | /* Restore default handlers just prior to exec */ |
| 1966 | set_jobctrl_sighandler(SIG_DFL); |
| 1967 | set_misc_sighandler(SIG_DFL); |
| 1968 | signal(SIGCHLD, SIG_DFL); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1969 | /* comment how it sets env???? |
| 1970 | for single_and_fg, it's already set yes? */ |
| 1971 | pseudo_exec((char ***) &shared_across_vfork, command, argv_expanded); |
| 1972 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1973 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1974 | /* parent */ |
| 1975 | #if !BB_MMU |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 1976 | //BUG: does not restore OLD env var contents |
| 1977 | free_strings_and_unsetenv((char **)shared_across_vfork, 1); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1978 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 1979 | free(argv_expanded); |
| 1980 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1981 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1982 | /* Clearly indicate, was it fork or vfork */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 1983 | bb_perror_msg(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1984 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1985 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1986 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1987 | /* Second and next children need to know pid of first one */ |
| 1988 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1989 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1990 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1991 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1992 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1993 | if (i) |
| 1994 | close(nextin); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 1995 | if ((i + 1) < pi->num_cmds) |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 1996 | close(pipefds[1]); /* write end */ |
| 1997 | /* Pass read (output) pipe end to next iteration */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1998 | nextin = pipefds[0]; |
| 1999 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 2000 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2001 | if (!pi->alive_cmds) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2002 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 2003 | return 1; |
| 2004 | } |
| 2005 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2006 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2007 | return -1; |
| 2008 | } |
| 2009 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 2010 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2011 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 2012 | { |
| 2013 | static const char *PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2014 | [PIPE_SEQ] = "SEQ", |
| 2015 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2016 | [PIPE_OR ] = "OR" , |
| 2017 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2018 | }; |
| 2019 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2020 | [RES_NONE ] = "NONE" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2021 | #if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2022 | [RES_IF ] = "IF" , |
| 2023 | [RES_THEN ] = "THEN" , |
| 2024 | [RES_ELIF ] = "ELIF" , |
| 2025 | [RES_ELSE ] = "ELSE" , |
| 2026 | [RES_FI ] = "FI" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2027 | #endif |
| 2028 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2029 | [RES_FOR ] = "FOR" , |
| 2030 | [RES_WHILE] = "WHILE", |
| 2031 | [RES_UNTIL] = "UNTIL", |
| 2032 | [RES_DO ] = "DO" , |
| 2033 | [RES_DONE ] = "DONE" , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 2034 | #endif |
| 2035 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2036 | [RES_IN ] = "IN" , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2037 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2038 | #if ENABLE_HUSH_CASE |
| 2039 | [RES_CASE ] = "CASE" , |
| 2040 | [RES_MATCH] = "MATCH", |
| 2041 | [RES_CASEI] = "CASEI", |
| 2042 | [RES_ESAC ] = "ESAC" , |
| 2043 | #endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2044 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2045 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2046 | }; |
| 2047 | |
| 2048 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2049 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2050 | pin = 0; |
| 2051 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2052 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 2053 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2054 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2055 | while (prn < pi->num_cmds) { |
| 2056 | struct command *command = &pi->cmds[prn]; |
| 2057 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2058 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2059 | fprintf(stderr, "%*s prog %d assignment_cnt:%d", lvl*2, "", prn, command->assignment_cnt); |
| 2060 | if (command->group) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2061 | fprintf(stderr, " group %s: (argv=%p)\n", |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2062 | (command->subshell ? "()" : "{}"), |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 2063 | argv); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2064 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2065 | prn++; |
| 2066 | continue; |
| 2067 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2068 | if (argv) while (*argv) { |
| 2069 | fprintf(stderr, " '%s'", *argv); |
| 2070 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 2071 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 2072 | fprintf(stderr, "\n"); |
| 2073 | prn++; |
| 2074 | } |
| 2075 | pi = pi->next; |
| 2076 | pin++; |
| 2077 | } |
| 2078 | } |
| 2079 | #endif |
| 2080 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 2081 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 2082 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2083 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2084 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2085 | #if ENABLE_HUSH_CASE |
| 2086 | char *case_word = NULL; |
| 2087 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2088 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2089 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2090 | char *for_varname = NULL; |
| 2091 | char **for_lcur = NULL; |
| 2092 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2093 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2094 | smallint flag_skip = 1; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2095 | smalluint rcode = 0; /* probably just for compiler */ |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 2096 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2097 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2098 | #else |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2099 | enum { cond_code = 0, }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2100 | #endif |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2101 | /*enum reserved_style*/ smallint rword = RES_NONE; |
| 2102 | /*enum reserved_style*/ smallint skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 2103 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2104 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level + 1); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 2105 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2106 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2107 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2108 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 2109 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2110 | continue; |
| 2111 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2112 | if (cpipe->next == NULL) { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2113 | syntax("malformed for"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2114 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2115 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2116 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2117 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2118 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2119 | continue; |
| 2120 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2121 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 2122 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2123 | ) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2124 | syntax("malformed for"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2125 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2126 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2127 | } |
| 2128 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2129 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2130 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2131 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 2132 | * in order to return, no direct "return" statements please. |
| 2133 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2134 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2135 | #if ENABLE_HUSH_JOB |
| 2136 | /* Example of nested list: "while true; do { sleep 1 | exit 2; } done". |
| 2137 | * We are saving state before entering outermost list ("while...done") |
| 2138 | * so that ctrl-Z will correctly background _entire_ outermost list, |
| 2139 | * not just a part of it (like "sleep 1 | exit 2") */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2140 | if (++G.run_list_level == 1 && G.interactive_fd) { |
| 2141 | if (sigsetjmp(G.toplevel_jb, 1)) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2142 | /* ctrl-Z forked and we are parent; or ctrl-C. |
| 2143 | * Sighandler has longjmped us here */ |
| 2144 | signal(SIGINT, SIG_IGN); |
| 2145 | signal(SIGTSTP, SIG_IGN); |
| 2146 | /* Restore level (we can be coming from deep inside |
| 2147 | * nested levels) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2148 | G.run_list_level = 1; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2149 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2150 | if (G.nofork_save.saved) { /* if save area is valid */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2151 | debug_printf_jobs("exiting nofork early\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2152 | restore_nofork_data(&G.nofork_save); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2153 | } |
| 2154 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2155 | if (G.ctrl_z_flag) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2156 | /* ctrl-Z has forked and stored pid of the child in pi->pid. |
| 2157 | * Remember this child as background job */ |
| 2158 | insert_bg_job(pi); |
| 2159 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2160 | /* ctrl-C. We just stop doing whatever we were doing */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 2161 | bb_putchar('\n'); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2162 | } |
Denis Vlasenko | ff29b4f | 2008-07-29 13:57:59 +0000 | [diff] [blame] | 2163 | USE_HUSH_LOOPS(loop_top = NULL;) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2164 | USE_HUSH_LOOPS(G.depth_of_loop = 0;) |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2165 | rcode = 0; |
| 2166 | goto ret; |
| 2167 | } |
| 2168 | /* ctrl-Z handler will store pid etc in pi */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2169 | G.toplevel_list = pi; |
| 2170 | G.ctrl_z_flag = 0; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2171 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2172 | G.nofork_save.saved = 0; /* in case we will run a nofork later */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2173 | #endif |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 2174 | signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z); |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2175 | signal(SIGINT, handler_ctrl_c); |
| 2176 | } |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2177 | #endif /* JOB */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2178 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2179 | /* Go through list of pipes, (maybe) executing them. */ |
| 2180 | for (; pi; pi = USE_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2181 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 2182 | IF_HAS_NO_KEYWORDS(rword = RES_NONE;) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2183 | debug_printf_exec(": rword=%d cond_code=%d skip_more=%d\n", |
| 2184 | rword, cond_code, skip_more_for_this_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2185 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 2186 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2187 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 2188 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2189 | /* start of a loop: remember where loop starts */ |
| 2190 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2191 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2192 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2193 | #endif |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2194 | if (rword == skip_more_for_this_rword && flag_skip) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2195 | if (pi->followup == PIPE_SEQ) |
| 2196 | flag_skip = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2197 | /* it is "<false> && CMD" or "<true> || CMD" |
| 2198 | * and we should not execute CMD */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2199 | continue; |
| 2200 | } |
| 2201 | flag_skip = 1; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2202 | skip_more_for_this_rword = RES_XXXX; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2203 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2204 | if (cond_code) { |
| 2205 | if (rword == RES_THEN) { |
| 2206 | /* "if <false> THEN cmd": skip cmd */ |
| 2207 | continue; |
| 2208 | } |
| 2209 | } else { |
| 2210 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 2211 | /* "if <true> then ... ELSE/ELIF cmd": |
| 2212 | * skip cmd and all following ones */ |
| 2213 | break; |
| 2214 | } |
| 2215 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2216 | #endif |
| 2217 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2218 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2219 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2220 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2221 | |
| 2222 | static const char encoded_dollar_at[] ALIGN1 = { |
| 2223 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 2224 | }; /* encoded representation of "$@" */ |
| 2225 | static const char *const encoded_dollar_at_argv[] = { |
| 2226 | encoded_dollar_at, NULL |
| 2227 | }; /* argv list with one element: "$@" */ |
| 2228 | char **vals; |
| 2229 | |
| 2230 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 2231 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2232 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2233 | if (!pi->next->cmds[0].argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2234 | break; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2235 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2236 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2237 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2238 | debug_print_strings("for_list made from", vals); |
| 2239 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2240 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2241 | debug_print_strings("for_list", for_list); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2242 | for_varname = pi->cmds[0].argv[0]; |
| 2243 | pi->cmds[0].argv[0] = NULL; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2244 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2245 | free(pi->cmds[0].argv[0]); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2246 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 2247 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2248 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2249 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2250 | for_lcur = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2251 | pi->cmds[0].argv[0] = for_varname; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2252 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 2253 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2254 | /* insert next value from for_lcur */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2255 | //TODO: does it need escaping? |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2256 | pi->cmds[0].argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++); |
| 2257 | pi->cmds[0].assignment_cnt = 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2258 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2259 | if (rword == RES_IN) /* "for v IN list;..." - "in" has no cmds anyway */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2260 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2261 | if (rword == RES_DONE) { |
| 2262 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2263 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2264 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2265 | #if ENABLE_HUSH_CASE |
| 2266 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2267 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2268 | continue; |
| 2269 | } |
| 2270 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 2271 | char **argv; |
| 2272 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2273 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 2274 | break; |
| 2275 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2276 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 2277 | while (*argv) { |
| 2278 | char *pattern = expand_string_to_string(*argv); |
| 2279 | /* TODO: which FNM_xxx flags to use? */ |
| 2280 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 2281 | free(pattern); |
| 2282 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 2283 | free(case_word); /* make future "word)" stop */ |
| 2284 | case_word = NULL; |
| 2285 | break; |
| 2286 | } |
| 2287 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2288 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2289 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2290 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2291 | if (rword == RES_CASEI) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2292 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2293 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2294 | } |
| 2295 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2296 | if (pi->num_cmds == 0) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2297 | continue; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2298 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2299 | /* After analyzing all keywords and conditions, we decided |
| 2300 | * to execute this pipe. NB: has to do checkjobs(NULL) |
| 2301 | * after run_pipe() to collect any background children, |
| 2302 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2303 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2304 | { |
| 2305 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2306 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2307 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2308 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2309 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 2310 | if (r != -1) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2311 | /* we only ran a builtin: rcode is already known |
| 2312 | * and we don't need to wait for anything. */ |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2313 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2314 | /* was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2315 | if (G.flag_break_continue) { |
| 2316 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2317 | /* we might fall into outer *loop*, |
| 2318 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2319 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2320 | G.depth_break_continue--; |
| 2321 | if (G.depth_break_continue == 0) |
| 2322 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2323 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 2324 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2325 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2326 | goto check_jobs_and_break; |
| 2327 | /* "continue": simulate end of loop */ |
| 2328 | rword = RES_DONE; |
| 2329 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2330 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 2331 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2332 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2333 | /* what does bash do with attempts to background builtins? */ |
| 2334 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2335 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 2336 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2337 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2338 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2339 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2340 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2341 | rcode = 0; /* EXIT_SUCCESS */ |
| 2342 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2343 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2344 | if (G.run_list_level == 1 && G.interactive_fd) { |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2345 | /* waits for completion, then fg's main shell */ |
| 2346 | rcode = checkjobs_and_fg_shell(pi); |
| 2347 | debug_printf_exec(": checkjobs_and_fg_shell returned %d\n", rcode); |
| 2348 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2349 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2350 | { /* this one just waits for completion */ |
| 2351 | rcode = checkjobs(pi); |
| 2352 | debug_printf_exec(": checkjobs returned %d\n", rcode); |
| 2353 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2354 | } |
| 2355 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2356 | debug_printf_exec(": setting last_return_code=%d\n", rcode); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2357 | G.last_return_code = rcode; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2358 | |
| 2359 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2360 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2361 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2362 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2363 | #endif |
| 2364 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2365 | if (rword == RES_WHILE) { |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 2366 | if (rcode) { |
| 2367 | rcode = 0; /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2368 | goto check_jobs_and_break; |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 2369 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2370 | } |
| 2371 | if (rword == RES_UNTIL) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 2372 | if (!rcode) { |
| 2373 | check_jobs_and_break: |
| 2374 | checkjobs(NULL); |
| 2375 | break; |
| 2376 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2377 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2378 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 2379 | if ((rcode == 0 && pi->followup == PIPE_OR) |
| 2380 | || (rcode != 0 && pi->followup == PIPE_AND) |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2381 | ) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2382 | skip_more_for_this_rword = rword; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2383 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 2384 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2385 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2386 | |
| 2387 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2388 | if (G.ctrl_z_flag) { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2389 | /* ctrl-Z forked somewhere in the past, we are the child, |
| 2390 | * and now we completed running the list. Exit. */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2391 | //TODO: _exit? |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2392 | exit(rcode); |
| 2393 | } |
| 2394 | ret: |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2395 | if (!--G.run_list_level && G.interactive_fd) { |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 2396 | signal(SIGTSTP, SIG_IGN); |
| 2397 | signal(SIGINT, SIG_IGN); |
| 2398 | } |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 2399 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2400 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2401 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 2402 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2403 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 2404 | free(for_list); |
| 2405 | #endif |
| 2406 | #if ENABLE_HUSH_CASE |
| 2407 | free(case_word); |
| 2408 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2409 | return rcode; |
| 2410 | } |
| 2411 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2412 | /* return code is the exit status of the pipe */ |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2413 | static int free_pipe(struct pipe *pi, int indent) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2414 | { |
| 2415 | char **p; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2416 | struct command *command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2417 | struct redir_struct *r, *rnext; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2418 | int a, i, ret_code = 0; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 2419 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2420 | if (pi->stopped_cmds > 0) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 2421 | return ret_code; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2422 | debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid()); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2423 | for (i = 0; i < pi->num_cmds; i++) { |
| 2424 | command = &pi->cmds[i]; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2425 | debug_printf_clean("%s command %d:\n", indenter(indent), i); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2426 | if (command->argv) { |
| 2427 | for (a = 0, p = command->argv; *p; a++, p++) { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2428 | debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2429 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2430 | free_strings(command->argv); |
| 2431 | command->argv = NULL; |
| 2432 | } else if (command->group) { |
| 2433 | debug_printf_clean("%s begin group (subshell:%d)\n", indenter(indent), command->subshell); |
| 2434 | ret_code = free_pipe_list(command->group, indent+3); |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2435 | debug_printf_clean("%s end group\n", indenter(indent)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2436 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2437 | debug_printf_clean("%s (nil)\n", indenter(indent)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2438 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2439 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 211b59b | 2008-06-23 16:28:53 +0000 | [diff] [blame] | 2440 | 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] | 2441 | if (r->dup == -1) { |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2442 | /* guard against the case >$FOO, where foo is unset or blank */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2443 | if (r->rd_filename) { |
| 2444 | debug_printf_clean(" %s\n", r->rd_filename); |
| 2445 | free(r->rd_filename); |
| 2446 | r->rd_filename = NULL; |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 2447 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2448 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2449 | debug_printf_clean("&%d\n", r->dup); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2450 | } |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2451 | rnext = r->next; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2452 | free(r); |
| 2453 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2454 | command->redirects = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2455 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2456 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 2457 | pi->cmds = NULL; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 2458 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2459 | free(pi->cmdtext); |
| 2460 | pi->cmdtext = NULL; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2461 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2462 | return ret_code; |
| 2463 | } |
| 2464 | |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2465 | static int free_pipe_list(struct pipe *head, int indent) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2466 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2467 | int rcode = 0; /* if list has no members */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2468 | struct pipe *pi, *next; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2469 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2470 | for (pi = head; pi; pi = next) { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2471 | #if HAS_KEYWORDS |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 2472 | debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2473 | #endif |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 2474 | rcode = free_pipe(pi, indent); |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2475 | 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] | 2476 | next = pi->next; |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2477 | /*pi->next = NULL;*/ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2478 | free(pi); |
| 2479 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2480 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2484 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2485 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2486 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2487 | debug_printf_exec("run_and_free_list entered\n"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2488 | if (!G.fake_mode) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2489 | debug_printf_exec(": run_list with %d members\n", pi->num_cmds); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2490 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2491 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 2492 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2493 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2494 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 2495 | free_pipe_list(pi, /* indent: */ 0); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2496 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2497 | return rcode; |
| 2498 | } |
| 2499 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2500 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2501 | /* expand_strvec_to_strvec() takes a list of strings, expands |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2502 | * all variable references within and returns a pointer to |
| 2503 | * a list of expanded strings, possibly with larger number |
| 2504 | * of strings. (Think VAR="a b"; echo $VAR). |
| 2505 | * This new list is allocated as a single malloc block. |
| 2506 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 2507 | * followed by strings themself. |
| 2508 | * Caller can deallocate entire list by single free(list). */ |
| 2509 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2510 | /* Store given string, finalizing the word and starting new one whenever |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2511 | * we encounter IFS char(s). This is used for expanding variable values. |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2512 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2513 | 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] | 2514 | { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2515 | while (1) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2516 | int word_len = strcspn(str, G.ifs); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2517 | if (word_len) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2518 | if (output->o_quote || !output->o_glob) |
| 2519 | o_addQstr(output, str, word_len); |
| 2520 | else /* protect backslashes against globbing up :) */ |
| 2521 | o_addstr_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2522 | str += word_len; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2523 | } |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2524 | if (!*str) /* EOL - do not finalize word */ |
| 2525 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2526 | o_addchr(output, '\0'); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2527 | debug_print_list("expand_on_ifs", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2528 | n = o_save_ptr(output, n); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2529 | str += strspn(str, G.ifs); /* skip ifs chars */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2530 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2531 | debug_print_list("expand_on_ifs[1]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2532 | return n; |
| 2533 | } |
| 2534 | |
| 2535 | /* Expand all variable references in given string, adding words to list[] |
| 2536 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 2537 | * to be filled). This routine is extremely tricky: has to deal with |
| 2538 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 2539 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2540 | 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] | 2541 | { |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2542 | /* or_mask is either 0 (normal case) or 0x80 |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2543 | * (expansion of right-hand side of assignment == 1-element expand. |
| 2544 | * 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] | 2545 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2546 | char first_ch, ored_ch; |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2547 | int i; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2548 | const char *val; |
| 2549 | char *p; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2550 | |
| 2551 | ored_ch = 0; |
| 2552 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2553 | debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2554 | debug_print_list("expand_vars_to_list", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2555 | n = o_save_ptr(output, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2556 | debug_print_list("expand_vars_to_list[0]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2557 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2558 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2559 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2560 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2561 | #endif |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2562 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2563 | o_addstr(output, arg, p - arg); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2564 | debug_print_list("expand_vars_to_list[1]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2565 | arg = ++p; |
| 2566 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2567 | |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2568 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 2569 | /* "$@" is special. Even if quoted, it can still |
| 2570 | * expand to nothing (not even an empty string) */ |
| 2571 | if ((first_ch & 0x7f) != '@') |
| 2572 | ored_ch |= first_ch; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2573 | val = NULL; |
| 2574 | switch (first_ch & 0x7f) { |
| 2575 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 2576 | case '$': /* pid */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2577 | val = utoa(G.root_pid); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2578 | break; |
| 2579 | case '!': /* bg pid */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2580 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2581 | break; |
| 2582 | case '?': /* exitcode */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2583 | val = utoa(G.last_return_code); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2584 | break; |
| 2585 | case '#': /* argc */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2586 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2587 | break; |
| 2588 | case '*': |
| 2589 | case '@': |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2590 | i = 1; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2591 | if (!G.global_argv[i]) |
Denis Vlasenko | c3c6659 | 2007-11-24 00:22:42 +0000 | [diff] [blame] | 2592 | break; |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 2593 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2594 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2595 | smallint sv = output->o_quote; |
| 2596 | /* unquoted var's contents should be globbed, so don't quote */ |
| 2597 | output->o_quote = 0; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2598 | while (G.global_argv[i]) { |
| 2599 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 2600 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 2601 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2602 | /* this argv[] is not empty and not last: |
| 2603 | * put terminating NUL, start new word */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2604 | o_addchr(output, '\0'); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2605 | debug_print_list("expand_vars_to_list[2]", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2606 | n = o_save_ptr(output, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2607 | debug_print_list("expand_vars_to_list[3]", output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2608 | } |
| 2609 | } |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2610 | output->o_quote = sv; |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2611 | } else |
| 2612 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 2613 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2614 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2615 | while (1) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2616 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2617 | if (++i >= G.global_argc) |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2618 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2619 | o_addchr(output, '\0'); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2620 | debug_print_list("expand_vars_to_list[4]", output, n); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2621 | n = o_save_ptr(output, n); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2622 | } |
| 2623 | } else { /* quoted $*: add as one word */ |
Denis Vlasenko | c3c6659 | 2007-11-24 00:22:42 +0000 | [diff] [blame] | 2624 | while (1) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2625 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2626 | if (!G.global_argv[++i]) |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2627 | break; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2628 | if (G.ifs[0]) |
| 2629 | o_addchr(output, G.ifs[0]); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2630 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2631 | } |
| 2632 | break; |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2633 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 2634 | /* "Empty variable", used to make "" etc to not disappear */ |
| 2635 | arg++; |
| 2636 | ored_ch = 0x80; |
| 2637 | break; |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2638 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2639 | case '`': { /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2640 | struct in_str input; |
| 2641 | *p = '\0'; |
| 2642 | arg++; |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2643 | //TODO: can we just stuff it into "output" directly? |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2644 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2645 | setup_string_in_str(&input, arg); |
| 2646 | process_command_subs(&subst_result, &input, NULL); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2647 | debug_printf_subst("SUBST RES '%s'\n", subst_result.data); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2648 | val = subst_result.data; |
| 2649 | goto store_val; |
| 2650 | } |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2651 | #endif |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2652 | default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2653 | *p = '\0'; |
| 2654 | arg[0] = first_ch & 0x7f; |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2655 | if (isdigit(arg[0])) { |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2656 | i = xatoi_u(arg); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2657 | if (i < G.global_argc) |
| 2658 | val = G.global_argv[i]; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2659 | /* else val remains NULL: $N with too big N */ |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 2660 | } else |
| 2661 | val = lookup_param(arg); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2662 | arg[0] = first_ch; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2663 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2664 | store_val: |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2665 | #endif |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2666 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 2667 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2668 | 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] | 2669 | if (val) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2670 | /* unquoted var's contents should be globbed, so don't quote */ |
| 2671 | smallint sv = output->o_quote; |
| 2672 | output->o_quote = 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2673 | n = expand_on_ifs(output, n, val); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2674 | val = NULL; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2675 | output->o_quote = sv; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2676 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2677 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2678 | 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] | 2679 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2680 | } |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2681 | if (val) { |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2682 | o_addQstr(output, val, strlen(val)); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2683 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2684 | |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2685 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2686 | o_free(&subst_result); |
Denis Vlasenko | ccce59d | 2008-06-16 14:35:57 +0000 | [diff] [blame] | 2687 | #endif |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2688 | arg = ++p; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2689 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 2690 | |
Denis Vlasenko | 2e76c3f | 2008-06-10 18:27:50 +0000 | [diff] [blame] | 2691 | if (arg[0]) { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2692 | debug_print_list("expand_vars_to_list[a]", output, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2693 | /* this part is literal, and it was already pre-quoted |
| 2694 | * if needed (much earlier), do not use o_addQstr here! */ |
| 2695 | o_addstr(output, arg, strlen(arg) + 1); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2696 | debug_print_list("expand_vars_to_list[b]", output, n); |
Denis Vlasenko | 2e76c3f | 2008-06-10 18:27:50 +0000 | [diff] [blame] | 2697 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 2698 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 2699 | ) { |
| 2700 | n--; |
| 2701 | /* allow to reuse list[n] later without re-growth */ |
| 2702 | output->has_empty_slot = 1; |
| 2703 | } else { |
| 2704 | o_addchr(output, '\0'); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2705 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2706 | return n; |
| 2707 | } |
| 2708 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2709 | static char **expand_variables(char **argv, int or_mask) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2710 | { |
| 2711 | int n; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2712 | char **list; |
| 2713 | char **v; |
| 2714 | o_string output = NULL_O_STRING; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2715 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2716 | if (or_mask & 0x100) { |
Denis Vlasenko | 324a3fd | 2008-06-18 17:49:58 +0000 | [diff] [blame] | 2717 | output.o_quote = 1; /* protect against globbing for "$var" */ |
| 2718 | /* (unquoted $var will temporarily switch it off) */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2719 | output.o_glob = 1; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2720 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2721 | |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2722 | n = 0; |
| 2723 | v = argv; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2724 | while (*v) { |
| 2725 | n = expand_vars_to_list(&output, n, *v, (char)or_mask); |
| 2726 | v++; |
| 2727 | } |
| 2728 | debug_print_list("expand_variables", &output, n); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2729 | |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 2730 | /* output.data (malloced in one block) gets returned in "list" */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2731 | list = o_finalize_list(&output, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2732 | debug_print_strings("expand_variables[1]", list); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2733 | return list; |
| 2734 | } |
| 2735 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2736 | static char **expand_strvec_to_strvec(char **argv) |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2737 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2738 | return expand_variables(argv, 0x100); |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2741 | /* Used for expansion of right hand of assignments */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 2742 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 2743 | * "v=/bin/c*" */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2744 | static char *expand_string_to_string(const char *str) |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2745 | { |
| 2746 | char *argv[2], **list; |
| 2747 | |
| 2748 | argv[0] = (char*)str; |
| 2749 | argv[1] = NULL; |
| 2750 | list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2751 | if (HUSH_DEBUG) |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2752 | if (!list[0] || list[1]) |
| 2753 | bb_error_msg_and_die("BUG in varexp2"); |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2754 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 2755 | strcpy((char*)list, list[0]); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2756 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 2757 | return (char*)list; |
| 2758 | } |
| 2759 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2760 | /* Used for "eval" builtin */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2761 | static char* expand_strvec_to_string(char **argv) |
| 2762 | { |
| 2763 | char **list; |
| 2764 | |
| 2765 | list = expand_variables(argv, 0x80); |
| 2766 | /* Convert all NULs to spaces */ |
| 2767 | if (list[0]) { |
| 2768 | int n = 1; |
| 2769 | while (list[n]) { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2770 | if (HUSH_DEBUG) |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2771 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 2772 | bb_error_msg_and_die("BUG in varexp3"); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2773 | list[n][-1] = ' '; /* TODO: or to G.ifs[0]? */ |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 2774 | n++; |
| 2775 | } |
| 2776 | } |
| 2777 | strcpy((char*)list, list[0]); |
| 2778 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
Denis Vlasenko | b6a741f | 2007-05-17 14:38:17 +0000 | [diff] [blame] | 2779 | return (char*)list; |
| 2780 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 2781 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2782 | |
| 2783 | /* Used to get/check local shell variables */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2784 | static struct variable *get_local_var(const char *name) |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2785 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2786 | struct variable *cur; |
| 2787 | int len; |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2788 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2789 | if (!name) |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2790 | return NULL; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2791 | len = strlen(name); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2792 | for (cur = G.top_var; cur; cur = cur->next) { |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2793 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2794 | return cur; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 2795 | } |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2796 | return NULL; |
| 2797 | } |
| 2798 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2799 | /* str holds "NAME=VAL" and is expected to be malloced. |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2800 | * We take ownership of it. */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2801 | static int set_local_var(char *str, int flg_export) |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2802 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2803 | struct variable *cur; |
| 2804 | char *value; |
| 2805 | int name_len; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2806 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2807 | value = strchr(str, '='); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2808 | if (!value) { /* not expected to ever happen? */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2809 | free(str); |
Eric Andersen | 9978576 | 2001-05-22 21:37:48 +0000 | [diff] [blame] | 2810 | return -1; |
| 2811 | } |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2812 | |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2813 | name_len = value - str + 1; /* including '=' */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2814 | cur = G.top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2815 | while (1) { |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2816 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2817 | if (!cur->next) { |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2818 | /* Bail out. Note that now cur points |
| 2819 | * to last var in linked list */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2820 | break; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2821 | } |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2822 | cur = cur->next; |
| 2823 | continue; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2824 | } |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2825 | /* We found an existing var with this name */ |
| 2826 | *value = '\0'; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2827 | if (cur->flg_read_only) { |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2828 | bb_error_msg("%s: readonly variable", str); |
| 2829 | free(str); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2830 | return -1; |
| 2831 | } |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2832 | unsetenv(str); /* just in case */ |
| 2833 | *value = '='; |
| 2834 | if (strcmp(cur->varstr, str) == 0) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2835 | free_and_exp: |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2836 | free(str); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2837 | goto exp; |
| 2838 | } |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2839 | if (cur->max_len >= strlen(str)) { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2840 | /* This one is from startup env, reuse space */ |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2841 | strcpy(cur->varstr, str); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2842 | goto free_and_exp; |
| 2843 | } |
| 2844 | /* max_len == 0 signifies "malloced" var, which we can |
| 2845 | * (and has to) free */ |
| 2846 | if (!cur->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2847 | free(cur->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2848 | cur->max_len = 0; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2849 | goto set_str_and_exp; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2850 | } |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2851 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2852 | /* Not found - create next variable struct */ |
| 2853 | cur->next = xzalloc(sizeof(*cur)); |
| 2854 | cur = cur->next; |
| 2855 | |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2856 | set_str_and_exp: |
| 2857 | cur->varstr = str; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2858 | exp: |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2859 | if (flg_export) |
| 2860 | cur->flg_export = 1; |
| 2861 | if (cur->flg_export) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2862 | return putenv(cur->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2863 | return 0; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2866 | static void unset_local_var(const char *name) |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2867 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2868 | struct variable *cur; |
| 2869 | struct variable *prev = prev; /* for gcc */ |
| 2870 | int name_len; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2871 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2872 | if (!name) |
| 2873 | return; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2874 | name_len = strlen(name); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2875 | cur = G.top_var; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2876 | while (cur) { |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2877 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2878 | if (cur->flg_read_only) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 2879 | bb_error_msg("%s: readonly variable", name); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 2880 | return; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2881 | } |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2882 | /* prev is ok to use here because 1st variable, HUSH_VERSION, |
| 2883 | * is ro, and we cannot reach this code on the 1st pass */ |
| 2884 | prev->next = cur->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2885 | unsetenv(cur->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2886 | if (!cur->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 2887 | free(cur->varstr); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2888 | free(cur); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 2889 | return; |
Eric Andersen | f72f562 | 2001-05-15 23:21:41 +0000 | [diff] [blame] | 2890 | } |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 2891 | prev = cur; |
| 2892 | cur = cur->next; |
Eric Andersen | 20a69a7 | 2001-05-15 17:24:44 +0000 | [diff] [blame] | 2893 | } |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 2896 | /* The src parameter allows us to peek forward to a possible &n syntax |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2897 | * for file descriptor duplication, e.g., "2>&1". |
| 2898 | * Return code is 0 normally, 1 if a syntax error is detected in src. |
| 2899 | * Resource errors (in xmalloc) cause the process to exit */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2900 | static int setup_redirect(struct parse_context *ctx, int fd, redir_type style, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2901 | struct in_str *input) |
| 2902 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2903 | struct command *command = ctx->command; |
| 2904 | struct redir_struct *redir = command->redirects; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2905 | struct redir_struct *last_redir = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2906 | |
| 2907 | /* 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] | 2908 | while (redir) { |
| 2909 | last_redir = redir; |
| 2910 | redir = redir->next; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2911 | } |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 2912 | redir = xzalloc(sizeof(struct redir_struct)); |
| 2913 | /* redir->next = NULL; */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2914 | /* redir->rd_filename = NULL; */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2915 | if (last_redir) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2916 | last_redir->next = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2917 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2918 | command->redirects = redir; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2921 | redir->rd_type = style; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 2922 | redir->fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2923 | |
| 2924 | debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip); |
| 2925 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2926 | /* Check for a '2>&1' type redirect */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2927 | redir->dup = redirect_dup_num(input); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2928 | if (redir->dup == -2) |
| 2929 | return 1; /* syntax error */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2930 | if (redir->dup != -1) { |
| 2931 | /* Erik had a check here that the file descriptor in question |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 2932 | * is legit; I postpone that to "run time" |
| 2933 | * A "-" representation of "close me" shows up as a -3 here */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2934 | debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup); |
| 2935 | } else { |
| 2936 | /* We do _not_ try to open the file that src points to, |
| 2937 | * since we need to return and let src be expanded first. |
| 2938 | * Set ctx->pending_redirect, so we know what to do at the |
Denis Vlasenko | 201c72a | 2007-05-25 10:00:36 +0000 | [diff] [blame] | 2939 | * end of the next parsed word. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2940 | ctx->pending_redirect = redir; |
| 2941 | } |
| 2942 | return 0; |
| 2943 | } |
| 2944 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 2945 | static struct pipe *new_pipe(void) |
| 2946 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2947 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2948 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2949 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2950 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2951 | return pi; |
| 2952 | } |
| 2953 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2954 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2955 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2956 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2957 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2958 | /* Create the memory for command, roughly: |
| 2959 | * ctx->pipe->cmds = new struct command; |
| 2960 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2961 | */ |
| 2962 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2965 | /* If a reserved word is found and processed, parse context is modified |
| 2966 | * and 1 is returned. |
| 2967 | * Handles if, then, elif, else, fi, for, while, until, do, done. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2968 | * case, function, and select are obnoxious, save those for later. |
| 2969 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2970 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2971 | struct reserved_combo { |
| 2972 | char literal[6]; |
| 2973 | unsigned char res; |
| 2974 | unsigned char assignment_flag; |
| 2975 | int flag; |
| 2976 | }; |
| 2977 | enum { |
| 2978 | FLAG_END = (1 << RES_NONE ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2979 | #if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2980 | FLAG_IF = (1 << RES_IF ), |
| 2981 | FLAG_THEN = (1 << RES_THEN ), |
| 2982 | FLAG_ELIF = (1 << RES_ELIF ), |
| 2983 | FLAG_ELSE = (1 << RES_ELSE ), |
| 2984 | FLAG_FI = (1 << RES_FI ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2985 | #endif |
| 2986 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2987 | FLAG_FOR = (1 << RES_FOR ), |
| 2988 | FLAG_WHILE = (1 << RES_WHILE), |
| 2989 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 2990 | FLAG_DO = (1 << RES_DO ), |
| 2991 | FLAG_DONE = (1 << RES_DONE ), |
| 2992 | FLAG_IN = (1 << RES_IN ), |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2993 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2994 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2995 | FLAG_MATCH = (1 << RES_MATCH), |
| 2996 | FLAG_ESAC = (1 << RES_ESAC ), |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2997 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2998 | FLAG_START = (1 << RES_XXXX ), |
| 2999 | }; |
| 3000 | |
| 3001 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3002 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3003 | /* Mostly a list of accepted follow-up reserved words. |
| 3004 | * FLAG_END means we are done with the sequence, and are ready |
| 3005 | * to turn the compound list into a command. |
| 3006 | * FLAG_START means the word must start a new compound list. |
| 3007 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3008 | static const struct reserved_combo reserved_list[] = { |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3009 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3010 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3011 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 3012 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3013 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 3014 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 3015 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3016 | #endif |
| 3017 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3018 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3019 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 3020 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 3021 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3022 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 3023 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3024 | #endif |
| 3025 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3026 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3027 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3028 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3029 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3030 | const struct reserved_combo *r; |
| 3031 | |
| 3032 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 3033 | if (strcmp(word->data, r->literal) == 0) |
| 3034 | return r; |
| 3035 | } |
| 3036 | return NULL; |
| 3037 | } |
| 3038 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 3039 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3040 | #if ENABLE_HUSH_CASE |
| 3041 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3042 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3043 | }; |
| 3044 | #endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3045 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3046 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3047 | r = match_reserved_word(word); |
| 3048 | if (!r) |
| 3049 | return 0; |
| 3050 | |
| 3051 | debug_printf("found reserved word %s, res %d\n", r->literal, r->res); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3052 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3053 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE) |
| 3054 | /* "case word IN ..." - IN part starts first match part */ |
| 3055 | r = &reserved_match; |
| 3056 | else |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3057 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3058 | if (r->flag == 0) { /* '!' */ |
| 3059 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3060 | syntax(NULL); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3061 | IF_HAS_KEYWORDS(ctx->ctx_res_w = RES_SNTX;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3062 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3063 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3064 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3065 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3066 | if (r->flag & FLAG_START) { |
| 3067 | struct parse_context *new; |
| 3068 | debug_printf("push stack\n"); |
| 3069 | new = xmalloc(sizeof(*new)); |
| 3070 | *new = *ctx; /* physical copy */ |
| 3071 | initialize_context(ctx); |
| 3072 | ctx->stack = new; |
| 3073 | } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) { |
| 3074 | syntax(NULL); |
| 3075 | ctx->ctx_res_w = RES_SNTX; |
| 3076 | return 1; |
| 3077 | } |
| 3078 | ctx->ctx_res_w = r->res; |
| 3079 | ctx->old_flag = r->flag; |
| 3080 | if (ctx->old_flag & FLAG_END) { |
| 3081 | struct parse_context *old; |
| 3082 | debug_printf("pop stack\n"); |
| 3083 | done_pipe(ctx, PIPE_SEQ); |
| 3084 | old = ctx->stack; |
| 3085 | old->command->group = ctx->list_head; |
| 3086 | old->command->subshell = 0; |
| 3087 | *ctx = *old; /* physical copy */ |
| 3088 | free(old); |
| 3089 | } |
| 3090 | word->o_assignment = r->assignment_flag; |
| 3091 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3092 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3093 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3094 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3095 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3096 | * Normal return is 0. Syntax errors return 1. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3097 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3098 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3099 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3100 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3101 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3102 | if (word->length == 0 && word->nonnull == 0) { |
| 3103 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3104 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3105 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3106 | /* If this word wasn't an assignment, next ones definitely |
| 3107 | * can't be assignments. Even if they look like ones. */ |
| 3108 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3109 | && word->o_assignment != WORD_IS_KEYWORD |
| 3110 | ) { |
| 3111 | word->o_assignment = NOT_ASSIGNMENT; |
| 3112 | } else { |
| 3113 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3114 | command->assignment_cnt++; |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3115 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3116 | } |
| 3117 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3118 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3119 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3120 | * only if run as "bash", not "sh" */ |
| 3121 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3122 | word->o_assignment = NOT_ASSIGNMENT; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3123 | debug_printf("word stored in rd_filename: '%s'\n", word->data); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3124 | } else { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3125 | /* "{ echo foo; } echo bar" - bad */ |
| 3126 | /* NB: bash allows e.g. "if true; then { echo foo; } fi". TODO? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3127 | if (command->group) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3128 | syntax(NULL); |
| 3129 | debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n"); |
| 3130 | return 1; |
| 3131 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3132 | #if HAS_KEYWORDS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3133 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3134 | if (ctx->ctx_dsemicolon |
| 3135 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3136 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3137 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3138 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3139 | ctx->ctx_dsemicolon = 0; |
| 3140 | } else |
| 3141 | #endif |
| 3142 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3143 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3144 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3145 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3146 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3147 | #endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3148 | ) { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3149 | debug_printf_parse(": checking '%s' for reserved-ness\n", word->data); |
| 3150 | if (reserved_word(word, ctx)) { |
| 3151 | o_reset(word); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3152 | debug_printf_parse("done_word return %d\n", (ctx->ctx_res_w == RES_SNTX)); |
| 3153 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3154 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3155 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3156 | #endif |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 3157 | if (word->nonnull /* word had "xx" or 'xx' at least as part of it. */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3158 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3159 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3160 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3161 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3162 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3163 | char *p = word->data; |
| 3164 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3165 | && (p[1] & 0x7f) == '@' |
| 3166 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3167 | ) { |
| 3168 | p += 3; |
| 3169 | } |
| 3170 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3171 | /* saw no "$@", or not only "$@" but some |
| 3172 | * real text is there too */ |
| 3173 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3174 | * e.g. "", $empty"" etc to not disappear */ |
| 3175 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3176 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3177 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3178 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 3179 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3180 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3181 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3182 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3183 | o_reset(word); |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3184 | ctx->pending_redirect = NULL; |
| 3185 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3186 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3187 | /* Force FOR to have just one word (variable name) */ |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3188 | /* NB: basically, this makes hush see "for v in ..." syntax as if |
| 3189 | * as it is "for v; in ...". FOR and IN become two pipe structs |
| 3190 | * in parse tree. */ |
| 3191 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3192 | //TODO: check that command->argv[0] is a valid variable name! |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3193 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3194 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3195 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3196 | #if ENABLE_HUSH_CASE |
| 3197 | /* Force CASE to have just one word */ |
| 3198 | if (ctx->ctx_res_w == RES_CASE) { |
| 3199 | done_pipe(ctx, PIPE_SEQ); |
| 3200 | } |
| 3201 | #endif |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3202 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3203 | return 0; |
| 3204 | } |
| 3205 | |
Denis Vlasenko | 6eaf8de | 2008-06-17 12:09:21 +0000 | [diff] [blame] | 3206 | /* Command (member of a pipe) is complete. The only possible error here |
| 3207 | * is out of memory, in which case xmalloc exits. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3208 | static int done_command(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3209 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3210 | /* The command is really already in the pipe structure, so |
| 3211 | * advance the pipe counter and make a new, null command. */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3212 | struct pipe *pi = ctx->pipe; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3213 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3214 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3215 | if (command) { |
| 3216 | if (command->group == NULL |
| 3217 | && command->argv == NULL |
| 3218 | && command->redirects == NULL |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3219 | ) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3220 | debug_printf_parse("done_command: skipping null cmd, num_cmds=%d\n", pi->num_cmds); |
| 3221 | return pi->num_cmds; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3222 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3223 | pi->num_cmds++; |
| 3224 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3225 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3226 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3227 | } |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3228 | |
| 3229 | /* Only real trickiness here is that the uncommitted |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3230 | * command structure is not counted in pi->num_cmds. */ |
| 3231 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
| 3232 | command = &pi->cmds[pi->num_cmds]; |
| 3233 | memset(command, 0, sizeof(*command)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3234 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3235 | ctx->command = command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3236 | /* but ctx->pipe and ctx->list_head remain unchanged */ |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3237 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3238 | return pi->num_cmds; /* used only for 0/nonzero check */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3241 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3242 | { |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3243 | int not_null; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3244 | |
| 3245 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3246 | /* Close previous command */ |
| 3247 | not_null = done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3248 | ctx->pipe->followup = type; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3249 | IF_HAS_KEYWORDS(ctx->pipe->pi_inverted = ctx->ctx_inverted;) |
| 3250 | IF_HAS_KEYWORDS(ctx->ctx_inverted = 0;) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3251 | IF_HAS_KEYWORDS(ctx->pipe->res_word = ctx->ctx_res_w;) |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3252 | |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3253 | /* Without this check, even just <enter> on command line generates |
| 3254 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | 6eaf8de | 2008-06-17 12:09:21 +0000 | [diff] [blame] | 3255 | * IOW: it is safe to do it unconditionally. |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3256 | * RES_NONE case is for "for a in; do ..." (empty IN set) |
| 3257 | * to work, possibly other cases too. */ |
| 3258 | if (not_null IF_HAS_KEYWORDS(|| ctx->ctx_res_w != RES_NONE)) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3259 | struct pipe *new_p; |
| 3260 | debug_printf_parse("done_pipe: adding new pipe: " |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3261 | "not_null:%d ctx->ctx_res_w:%d\n", |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3262 | not_null, ctx->ctx_res_w); |
| 3263 | new_p = new_pipe(); |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3264 | ctx->pipe->next = new_p; |
| 3265 | ctx->pipe = new_p; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3266 | ctx->command = NULL; /* needed! */ |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3267 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3268 | * they remain set for commands inside if/while. |
| 3269 | * This is used to control execution. |
| 3270 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3271 | * cases where variable or value happens to match a keyword): |
| 3272 | */ |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3273 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3274 | if (ctx->ctx_res_w == RES_FOR |
| 3275 | || ctx->ctx_res_w == RES_IN) |
| 3276 | ctx->ctx_res_w = RES_NONE; |
Denis Vlasenko | 6bdff08 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 3277 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3278 | #if ENABLE_HUSH_CASE |
| 3279 | if (ctx->ctx_res_w == RES_MATCH) |
| 3280 | ctx->ctx_res_w = RES_CASEI; |
| 3281 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3282 | /* Create the memory for command, roughly: |
| 3283 | * ctx->pipe->cmds = new struct command; |
| 3284 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3285 | */ |
| 3286 | done_command(ctx); |
Denis Vlasenko | dd4cb2b | 2007-05-05 15:11:40 +0000 | [diff] [blame] | 3287 | } |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3288 | debug_printf_parse("done_pipe return\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3291 | /* 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] | 3292 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3293 | * Return either -2 (syntax error), -1 (no &), or the number found. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3294 | */ |
| 3295 | static int redirect_dup_num(struct in_str *input) |
| 3296 | { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3297 | int ch, d = 0, ok = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3298 | ch = i_peek(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3299 | if (ch != '&') return -1; |
| 3300 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3301 | i_getch(input); /* get the & */ |
| 3302 | ch = i_peek(input); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3303 | if (ch == '-') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3304 | i_getch(input); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3305 | return -3; /* "-" represents "close me" */ |
| 3306 | } |
| 3307 | while (isdigit(ch)) { |
Denis Vlasenko | 7d4c44e | 2007-04-16 22:34:39 +0000 | [diff] [blame] | 3308 | d = d*10 + (ch-'0'); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3309 | ok = 1; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3310 | i_getch(input); |
| 3311 | ch = i_peek(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3312 | } |
| 3313 | if (ok) return d; |
| 3314 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3315 | bb_error_msg("ambiguous redirect"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3316 | return -2; |
| 3317 | } |
| 3318 | |
| 3319 | /* If a redirect is immediately preceded by a number, that number is |
| 3320 | * supposed to tell which file descriptor to redirect. This routine |
| 3321 | * looks for such preceding numbers. In an ideal world this routine |
| 3322 | * needs to handle all the following classes of redirects... |
| 3323 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3324 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3325 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3326 | * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo |
| 3327 | * A -1 output from this program means no valid number was found, so the |
| 3328 | * caller should use the appropriate default for this redirection. |
| 3329 | */ |
| 3330 | static int redirect_opt_num(o_string *o) |
| 3331 | { |
| 3332 | int num; |
| 3333 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3334 | if (o->length == 0) |
| 3335 | return -1; |
| 3336 | for (num = 0; num < o->length; num++) { |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3337 | if (!isdigit(o->data[num])) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3338 | return -1; |
| 3339 | } |
| 3340 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3341 | num = atoi(o->data); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3342 | o_reset(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3343 | return num; |
| 3344 | } |
| 3345 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3346 | #if ENABLE_HUSH_TICK |
"Vladimir N. Oleynik" | 19c3701 | 2005-09-22 14:33:15 +0000 | [diff] [blame] | 3347 | static FILE *generate_stream_from_list(struct pipe *head) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3348 | { |
| 3349 | FILE *pf; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3350 | int pid, channel[2]; |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3351 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 3352 | xpipe(channel); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3353 | /* *** NOMMU WARNING *** */ |
| 3354 | /* By using vfork here, we suspend parent till child exits or execs. |
| 3355 | * If child will not do it before it fills the pipe, it can block forever |
| 3356 | * in write(STDOUT_FILENO), and parent (shell) will be also stuck. |
Denis Vlasenko | 3fe4f98 | 2008-06-09 16:02:39 +0000 | [diff] [blame] | 3357 | * Try this script: |
| 3358 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >TESTFILE |
| 3359 | * huge=`cat TESTFILE` # will block here forever |
| 3360 | * echo OK |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3361 | */ |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 3362 | pid = BB_MMU ? fork() : vfork(); |
| 3363 | if (pid < 0) |
| 3364 | bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork"); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3365 | if (pid == 0) { /* child */ |
Denis Vlasenko | 8317799 | 2008-02-11 08:44:36 +0000 | [diff] [blame] | 3366 | if (ENABLE_HUSH_JOB) |
| 3367 | die_sleep = 0; /* let nofork's xfuncs die */ |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 3368 | close(channel[0]); /* NB: close _first_, then move fd! */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3369 | xmove_fd(channel[1], 1); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3370 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3371 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3372 | G.run_list_level = 1; |
Denis Vlasenko | 27f79ff | 2007-05-30 00:55:52 +0000 | [diff] [blame] | 3373 | #endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3374 | /* Process substitution is not considered to be usual |
| 3375 | * 'command execution'. |
| 3376 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. */ |
| 3377 | /* Not needed, we are relying on it being disabled |
| 3378 | * everywhere outside actual command execution. */ |
| 3379 | /*set_jobctrl_sighandler(SIG_IGN);*/ |
| 3380 | set_misc_sighandler(SIG_DFL); |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3381 | /* Freeing 'head' here would break NOMMU. */ |
| 3382 | _exit(run_list(head)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3383 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3384 | close(channel[1]); |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 3385 | pf = fdopen(channel[0], "r"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3386 | return pf; |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 3387 | /* 'head' is freed by the caller */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3390 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3391 | static int process_command_subs(o_string *dest, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3392 | struct in_str *input, |
| 3393 | const char *subst_end) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3394 | { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3395 | int retcode, ch, eol_cnt; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3396 | o_string result = NULL_O_STRING; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3397 | struct parse_context inner; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3398 | FILE *p; |
| 3399 | struct in_str pipe_str; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3400 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3401 | initialize_context(&inner); |
| 3402 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3403 | /* Recursion to generate command */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3404 | retcode = parse_stream(&result, &inner, input, subst_end); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3405 | if (retcode != 0) |
| 3406 | return retcode; /* syntax error or EOF */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3407 | done_word(&result, &inner); |
| 3408 | done_pipe(&inner, PIPE_SEQ); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3409 | o_free(&result); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3410 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3411 | p = generate_stream_from_list(inner.list_head); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3412 | if (p == NULL) |
| 3413 | return 1; |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 3414 | close_on_exec_on(fileno(p)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3415 | setup_file_in_str(&pipe_str, p); |
| 3416 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3417 | /* Now send results of command back into original context */ |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3418 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3419 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3420 | if (ch == '\n') { |
| 3421 | eol_cnt++; |
| 3422 | continue; |
| 3423 | } |
| 3424 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3425 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 3426 | eol_cnt--; |
| 3427 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3428 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | debug_printf("done reading from pipe, pclose()ing\n"); |
| 3432 | /* This is the step that wait()s for the child. Should be pretty |
| 3433 | * 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] | 3434 | * to do better, by using wait(), and keeping track of background jobs |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3435 | * at the same time. That would be a lot of work, and contrary |
| 3436 | * to the KISS philosophy of this program. */ |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3437 | retcode = fclose(p); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3438 | free_pipe_list(inner.list_head, /* indent: */ 0); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3439 | debug_printf("closed FILE from child, retcode=%d\n", retcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3440 | return retcode; |
| 3441 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3442 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3443 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3444 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3445 | struct in_str *input, int ch) |
| 3446 | { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3447 | /* NB: parse_group may create and use its own o_string, |
| 3448 | * without any code changes. It just so happens that code is smaller |
| 3449 | * if we (ab)use caller's one. */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3450 | int rcode; |
| 3451 | const char *endch = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3452 | struct parse_context sub; |
| 3453 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3454 | |
| 3455 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3456 | if (command->argv /* word [word](... */ |
| 3457 | || dest->length /* word(... */ |
| 3458 | || dest->nonnull /* ""(... */ |
| 3459 | ) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3460 | syntax(NULL); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3461 | debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n"); |
| 3462 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3463 | } |
| 3464 | initialize_context(&sub); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3465 | endch = "}"; |
| 3466 | if (ch == '(') { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3467 | endch = ")"; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3468 | command->subshell = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3469 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 3470 | #if 0 /* TODO function support */ |
| 3471 | if (ch == 'F') { /* function definition */ |
| 3472 | command->subshell = 2; |
| 3473 | } |
| 3474 | #endif |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3475 | rcode = parse_stream(dest, &sub, input, endch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3476 | if (rcode == 0) { |
| 3477 | done_word(dest, &sub); /* finish off the final word in the subcontext */ |
| 3478 | done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3479 | command->group = sub.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3480 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3481 | debug_printf_parse("parse_group return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3482 | return rcode; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3483 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 3486 | /* Basically useful version until someone wants to get fancier, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3487 | * see the bash man page under "Parameter Expansion" */ |
Denis Vlasenko | 15d78fb | 2007-01-30 22:28:21 +0000 | [diff] [blame] | 3488 | static const char *lookup_param(const char *src) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3489 | { |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3490 | struct variable *var = get_local_var(src); |
| 3491 | if (var) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 3492 | return strchr(var->varstr, '=') + 1; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 3493 | return NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3496 | #if ENABLE_HUSH_TICK |
| 3497 | /* Subroutines for copying $(...) and `...` things */ |
| 3498 | static void add_till_backquote(o_string *dest, struct in_str *input); |
| 3499 | /* '...' */ |
| 3500 | static void add_till_single_quote(o_string *dest, struct in_str *input) |
| 3501 | { |
| 3502 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3503 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3504 | if (ch == EOF) |
| 3505 | break; |
| 3506 | if (ch == '\'') |
| 3507 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3508 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3509 | } |
| 3510 | } |
| 3511 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
| 3512 | static void add_till_double_quote(o_string *dest, struct in_str *input) |
| 3513 | { |
| 3514 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3515 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3516 | if (ch == '"') |
| 3517 | break; |
| 3518 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3519 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3520 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3521 | } |
| 3522 | if (ch == EOF) |
| 3523 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3524 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3525 | if (ch == '`') { |
| 3526 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3527 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3528 | continue; |
| 3529 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3530 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3531 | } |
| 3532 | } |
| 3533 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 3534 | * \` quoting. |
| 3535 | * "Within the backquoted style of command substitution, backslash |
| 3536 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 3537 | * The search for the matching backquote shall be satisfied by the first |
| 3538 | * backquote found without a preceding backslash; during this search, |
| 3539 | * if a non-escaped backquote is encountered within a shell comment, |
| 3540 | * a here-document, an embedded command substitution of the $(command) |
| 3541 | * form, or a quoted string, undefined results occur. A single-quoted |
| 3542 | * or double-quoted string that begins, but does not end, within the |
| 3543 | * "`...`" sequence produces undefined results." |
| 3544 | * Example Output |
| 3545 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 3546 | */ |
| 3547 | static void add_till_backquote(o_string *dest, struct in_str *input) |
| 3548 | { |
| 3549 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3550 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3551 | if (ch == '`') |
| 3552 | break; |
| 3553 | if (ch == '\\') { /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3554 | int ch2 = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3555 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3556 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3557 | ch = ch2; |
| 3558 | } |
| 3559 | if (ch == EOF) |
| 3560 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3561 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3562 | } |
| 3563 | } |
| 3564 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 3565 | * quoting and nested ()s. |
| 3566 | * "With the $(command) style of command substitution, all characters |
| 3567 | * following the open parenthesis to the matching closing parenthesis |
| 3568 | * constitute the command. Any valid shell script can be used for command, |
| 3569 | * except a script consisting solely of redirections which produces |
| 3570 | * unspecified results." |
| 3571 | * Example Output |
| 3572 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 3573 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 3574 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 3575 | */ |
| 3576 | static void add_till_closing_curly_brace(o_string *dest, struct in_str *input) |
| 3577 | { |
| 3578 | int count = 0; |
| 3579 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3580 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3581 | if (ch == EOF) |
| 3582 | break; |
| 3583 | if (ch == '(') |
| 3584 | count++; |
| 3585 | if (ch == ')') |
| 3586 | if (--count < 0) |
| 3587 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3588 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3589 | if (ch == '\'') { |
| 3590 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3591 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3592 | continue; |
| 3593 | } |
| 3594 | if (ch == '"') { |
| 3595 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3596 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3597 | continue; |
| 3598 | } |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3599 | if (ch == '\\') { /* \x. Copy verbatim. Important for \(, \) */ |
| 3600 | ch = i_getch(input); |
| 3601 | if (ch == EOF) |
| 3602 | break; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3603 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3604 | continue; |
| 3605 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3606 | } |
| 3607 | } |
| 3608 | #endif /* ENABLE_HUSH_TICK */ |
| 3609 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3610 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3611 | static int handle_dollar(o_string *dest, struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3612 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3613 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3614 | unsigned char quote_mask = dest->o_quote ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3615 | |
| 3616 | debug_printf_parse("handle_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3617 | if (isalpha(ch)) { |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3618 | i_getch(input); |
| 3619 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3620 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3621 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3622 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3623 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3624 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3625 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3626 | if (!isalnum(ch) && ch != '_') |
| 3627 | break; |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3628 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3629 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3630 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3631 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3632 | make_one_char_var: |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3633 | i_getch(input); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3634 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3635 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3636 | o_addchr(dest, ch | quote_mask); |
| 3637 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3638 | } else switch (ch) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3639 | case '$': /* pid */ |
| 3640 | case '!': /* last bg pid */ |
| 3641 | case '?': /* last exit code */ |
| 3642 | case '#': /* number of args */ |
| 3643 | case '*': /* args */ |
| 3644 | case '@': /* args */ |
| 3645 | goto make_one_char_var; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3646 | case '{': |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3647 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3648 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3649 | /* XXX maybe someone will try to escape the '}' */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3650 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3651 | ch = i_getch(input); |
Denis Vlasenko | b055001 | 2007-05-24 12:18:16 +0000 | [diff] [blame] | 3652 | if (ch == '}') |
| 3653 | break; |
| 3654 | if (!isalnum(ch) && ch != '_') { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3655 | syntax("unterminated ${name}"); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3656 | debug_printf_parse("handle_dollar return 1: unterminated ${name}\n"); |
| 3657 | return 1; |
| 3658 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3659 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3660 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3661 | quote_mask = 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3662 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3663 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3664 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3665 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3666 | case '(': { |
| 3667 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3668 | i_getch(input); |
| 3669 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3670 | o_addchr(dest, quote_mask | '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3671 | add_till_closing_curly_brace(dest, input); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3672 | //debug_printf_subst("SUBST RES2 '%s'\n", dest->data + pos); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3673 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3674 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3675 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3676 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3677 | case '_': |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3678 | i_getch(input); |
| 3679 | ch = i_peek(input); |
| 3680 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 3681 | ch = '_'; |
| 3682 | goto make_var; |
| 3683 | } |
| 3684 | /* else: it's $_ */ |
| 3685 | case '-': |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3686 | /* still unhandled, but should be eventually */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3687 | bb_error_msg("unhandled syntax: $%c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3688 | return 1; |
| 3689 | break; |
| 3690 | default: |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3691 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3692 | } |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3693 | debug_printf_parse("handle_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3694 | return 0; |
| 3695 | } |
| 3696 | |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3697 | /* Scan input, call done_word() whenever full IFS delimited word was seen. |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3698 | * Call done_pipe if '\n' was seen (and end_trigger != NULL). |
| 3699 | * Return code is 0 if end_trigger char is met, |
| 3700 | * -1 on EOF (but if end_trigger == NULL then return 0), |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3701 | * 1 for syntax error */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3702 | static int parse_stream(o_string *dest, struct parse_context *ctx, |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3703 | struct in_str *input, const char *end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3704 | { |
"Vladimir N. Oleynik" | 4ccd2b4 | 2006-01-31 09:27:48 +0000 | [diff] [blame] | 3705 | int ch, m; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3706 | int redir_fd; |
| 3707 | redir_type redir_style; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3708 | int shadow_quote = dest->o_quote; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3709 | int next; |
| 3710 | |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3711 | /* 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] | 3712 | * 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] | 3713 | * found. When recursing, quote state is passed in via dest->o_quote. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3714 | |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3715 | debug_printf_parse("parse_stream entered, end_trigger='%s' dest->o_assignment:%d\n", end_trigger, dest->o_assignment); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3716 | |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3717 | while (1) { |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3718 | m = CHAR_IFS; |
| 3719 | next = '\0'; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3720 | ch = i_getch(input); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3721 | if (ch != EOF) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 3722 | m = G.charmap[ch]; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3723 | if (ch != '\n') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3724 | next = i_peek(input); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3725 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3726 | } |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3727 | debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n", |
Denis Vlasenko | ff09762 | 2007-10-01 10:00:45 +0000 | [diff] [blame] | 3728 | ch, ch, m, dest->o_quote); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3729 | if (m == CHAR_ORDINARY |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3730 | || (m != CHAR_SPECIAL && shadow_quote) |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3731 | ) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3732 | if (ch == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3733 | syntax("unterminated \""); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 3734 | debug_printf_parse("parse_stream return 1: unterminated \"\n"); |
| 3735 | return 1; |
| 3736 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3737 | o_addQchr(dest, ch); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3738 | if ((dest->o_assignment == MAYBE_ASSIGNMENT |
| 3739 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3740 | && ch == '=' |
| 3741 | && is_assignment(dest->data) |
| 3742 | ) { |
| 3743 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 3744 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3745 | continue; |
| 3746 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3747 | if (m == CHAR_IFS) { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3748 | if (done_word(dest, ctx)) { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3749 | debug_printf_parse("parse_stream return 1: done_word!=0\n"); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3750 | return 1; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3751 | } |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3752 | if (ch == EOF) |
| 3753 | break; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3754 | /* If we aren't performing a substitution, treat |
| 3755 | * a newline as a command separator. |
| 3756 | * [why we don't handle it exactly like ';'? --vda] */ |
| 3757 | if (end_trigger && ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 3758 | #if ENABLE_HUSH_CASE |
| 3759 | /* "case ... in <newline> word) ..." - |
| 3760 | * newlines are ignored (but ';' wouldn't be) */ |
| 3761 | if (dest->length == 0 // && argv[0] == NULL |
| 3762 | && ctx->ctx_res_w == RES_MATCH |
| 3763 | ) { |
| 3764 | continue; |
| 3765 | } |
| 3766 | #endif |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3767 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3768 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3769 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3770 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3771 | if (end_trigger) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3772 | if (!shadow_quote && strchr(end_trigger, ch)) { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3773 | /* Special case: (...word) makes last word terminate, |
| 3774 | * as if ';' is seen */ |
| 3775 | if (ch == ')') { |
| 3776 | done_word(dest, ctx); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3777 | //err chk? |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3778 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3779 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3780 | } |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3781 | if (!HAS_KEYWORDS |
| 3782 | IF_HAS_KEYWORDS(|| (ctx->ctx_res_w == RES_NONE && ctx->old_flag == 0)) |
| 3783 | ) { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3784 | debug_printf_parse("parse_stream return 0: end_trigger char found\n"); |
| 3785 | return 0; |
| 3786 | } |
| 3787 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3788 | } |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 3789 | if (m == CHAR_IFS) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3790 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3791 | |
| 3792 | if (dest->o_assignment == MAYBE_ASSIGNMENT) { |
| 3793 | /* ch is a special char and thus this word |
| 3794 | * cannot be an assignment: */ |
| 3795 | dest->o_assignment = NOT_ASSIGNMENT; |
| 3796 | } |
| 3797 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3798 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3799 | case '#': |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3800 | if (dest->length == 0 && !shadow_quote) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3801 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3802 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3803 | if (ch == EOF || ch == '\n') |
| 3804 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3805 | i_getch(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3806 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3807 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3808 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3809 | } |
| 3810 | break; |
| 3811 | case '\\': |
| 3812 | if (next == EOF) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3813 | syntax("\\<eof>"); |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 3814 | debug_printf_parse("parse_stream return 1: \\<eof>\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3815 | return 1; |
| 3816 | } |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3817 | /* bash: |
| 3818 | * "The backslash retains its special meaning [in "..."] |
| 3819 | * only when followed by one of the following characters: |
| 3820 | * $, `, ", \, or <newline>. A double quote may be quoted |
| 3821 | * within double quotes by preceding it with a backslash. |
| 3822 | * If enabled, history expansion will be performed unless |
| 3823 | * an ! appearing in double quotes is escaped using |
| 3824 | * a backslash. The backslash preceding the ! is not removed." |
| 3825 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3826 | if (shadow_quote) { //NOT SURE dest->o_quote) { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3827 | if (strchr("$`\"\\", next) != NULL) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3828 | o_addqchr(dest, i_getch(input)); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3829 | } else { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3830 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3831 | } |
| 3832 | } else { |
| 3833 | o_addchr(dest, '\\'); |
| 3834 | o_addchr(dest, i_getch(input)); |
| 3835 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3836 | break; |
| 3837 | case '$': |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3838 | if (handle_dollar(dest, input) != 0) { |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3839 | debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n"); |
| 3840 | return 1; |
| 3841 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3842 | break; |
| 3843 | case '\'': |
| 3844 | dest->nonnull = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3845 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3846 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3847 | if (ch == EOF) { |
| 3848 | syntax("unterminated '"); |
| 3849 | debug_printf_parse("parse_stream return 1: unterminated '\n"); |
| 3850 | return 1; |
| 3851 | } |
| 3852 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3853 | break; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3854 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 3855 | o_addqchr(dest, ch); |
| 3856 | else |
| 3857 | o_addchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3858 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3859 | break; |
| 3860 | case '"': |
| 3861 | dest->nonnull = 1; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3862 | shadow_quote ^= 1; /* invert */ |
| 3863 | if (dest->o_assignment == NOT_ASSIGNMENT) |
| 3864 | dest->o_quote ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3865 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3866 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3867 | case '`': { |
| 3868 | //int pos = dest->length; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3869 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3870 | o_addchr(dest, shadow_quote /*or dest->o_quote??*/ ? 0x80 | '`' : '`'); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3871 | add_till_backquote(dest, input); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3872 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3873 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3874 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3875 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 3876 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3877 | case '>': |
| 3878 | redir_fd = redirect_opt_num(dest); |
| 3879 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3880 | redir_style = REDIRECT_OVERWRITE; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3881 | if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3882 | redir_style = REDIRECT_APPEND; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3883 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3884 | } |
| 3885 | #if 0 |
| 3886 | else if (next == '(') { |
| 3887 | syntax(">(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3888 | debug_printf_parse("parse_stream return 1: >(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3889 | return 1; |
| 3890 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3891 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3892 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 3893 | break; |
| 3894 | case '<': |
| 3895 | redir_fd = redirect_opt_num(dest); |
| 3896 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3897 | redir_style = REDIRECT_INPUT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3898 | if (next == '<') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3899 | redir_style = REDIRECT_HEREIS; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3900 | i_getch(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3901 | } else if (next == '>') { |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3902 | redir_style = REDIRECT_IO; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3903 | i_getch(input); |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3904 | } |
| 3905 | #if 0 |
| 3906 | else if (next == '(') { |
| 3907 | syntax("<(process) not supported"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3908 | debug_printf_parse("parse_stream return 1: <(process) not supported\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3909 | return 1; |
| 3910 | } |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3911 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3912 | setup_redirect(ctx, redir_fd, redir_style, input); |
| 3913 | break; |
| 3914 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3915 | #if ENABLE_HUSH_CASE |
| 3916 | case_semi: |
| 3917 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3918 | done_word(dest, ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3919 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3920 | #if ENABLE_HUSH_CASE |
| 3921 | /* Eat multiple semicolons, detect |
| 3922 | * whether it means something special */ |
| 3923 | while (1) { |
| 3924 | ch = i_peek(input); |
| 3925 | if (ch != ';') |
| 3926 | break; |
| 3927 | i_getch(input); |
| 3928 | if (ctx->ctx_res_w == RES_CASEI) { |
| 3929 | ctx->ctx_dsemicolon = 1; |
| 3930 | ctx->ctx_res_w = RES_MATCH; |
| 3931 | break; |
| 3932 | } |
| 3933 | } |
| 3934 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3935 | new_cmd: |
| 3936 | /* We just finished a cmd. New one may start |
| 3937 | * with an assignment */ |
| 3938 | dest->o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3939 | break; |
| 3940 | case '&': |
| 3941 | done_word(dest, ctx); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3942 | if (next == '&') { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3943 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3944 | done_pipe(ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3945 | } else { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3946 | done_pipe(ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3947 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3948 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3949 | case '|': |
| 3950 | done_word(dest, ctx); |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 3951 | #if ENABLE_HUSH_CASE |
| 3952 | if (ctx->ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 3953 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 3954 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3955 | if (next == '|') { /* || */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3956 | i_getch(input); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3957 | done_pipe(ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3958 | } else { |
| 3959 | /* we could pick up a file descriptor choice here |
| 3960 | * with redirect_opt_num(), but bash doesn't do it. |
| 3961 | * "echo foo 2| cat" yields "foo 2". */ |
| 3962 | done_command(ctx); |
| 3963 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3964 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3965 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3966 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 3967 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 3968 | if (ctx->ctx_res_w == RES_MATCH |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3969 | && ctx->command->argv == NULL /* not (word|(... */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 3970 | && dest->length == 0 /* not word(... */ |
| 3971 | && dest->nonnull == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3972 | ) { |
| 3973 | continue; |
| 3974 | } |
| 3975 | #endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3976 | #if 0 /* TODO: implement functions */ |
| 3977 | if (dest->length != 0 /* not just () but word() */ |
| 3978 | && dest->nonnull == 0 /* not a"b"c() */ |
| 3979 | && ctx->command->argv == NULL /* it's the first word */ |
| 3980 | && i_peek(input) == ')' |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 3981 | && !match_reserved_word(dest) |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3982 | ) { |
| 3983 | bb_error_msg("seems like a function definition"); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame^] | 3984 | i_getch(input); |
| 3985 | do { |
| 3986 | ch = i_getch(input); |
| 3987 | } while (ch == ' ' || ch == '\n'); |
| 3988 | if (ch != '{') { |
| 3989 | syntax("was expecting {"); |
| 3990 | debug_printf_parse("parse_stream return 1\n"); |
| 3991 | return 1; |
| 3992 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3993 | } |
| 3994 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3995 | case '{': |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3996 | if (parse_group(dest, ctx, input, ch) != 0) { |
| 3997 | 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] | 3998 | return 1; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3999 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4000 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4001 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4002 | #if ENABLE_HUSH_CASE |
| 4003 | if (ctx->ctx_res_w == RES_MATCH) |
| 4004 | goto case_semi; |
| 4005 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4006 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4007 | /* proper use of this character is caught by end_trigger: |
| 4008 | * if we see {, we call parse_group(..., end_trigger='}') |
| 4009 | * and it will match } earlier (not here). */ |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4010 | syntax("unexpected } or )"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4011 | debug_printf_parse("parse_stream return 1: unexpected '}'\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4012 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4013 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4014 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4015 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4016 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4017 | } /* while (1) */ |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4018 | debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL)); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4019 | if (end_trigger) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4020 | return -1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4021 | return 0; |
| 4022 | } |
| 4023 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4024 | static void set_in_charmap(const char *set, int code) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4025 | { |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 4026 | while (*set) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4027 | G.charmap[(unsigned char)*set++] = code; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4030 | static void update_charmap(void) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4031 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4032 | G.ifs = getenv("IFS"); |
| 4033 | if (G.ifs == NULL) |
| 4034 | G.ifs = " \t\n"; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4035 | /* Precompute a list of 'flow through' behavior so it can be treated |
| 4036 | * quickly up front. Computation is necessary because of IFS. |
| 4037 | * Special case handling of IFS == " \t\n" is not implemented. |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4038 | * The charmap[] array only really needs two bits each, |
| 4039 | * and on most machines that would be faster (reduced L1 cache use). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4040 | */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4041 | memset(G.charmap, CHAR_ORDINARY, sizeof(G.charmap)); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4042 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4043 | set_in_charmap("\\$\"`", CHAR_SPECIAL); |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4044 | #else |
| 4045 | set_in_charmap("\\$\"", CHAR_SPECIAL); |
| 4046 | #endif |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4047 | set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4048 | set_in_charmap(G.ifs, CHAR_IFS); /* are ordinary if quoted */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4049 | } |
| 4050 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4051 | /* Most recursion does not come through here, the exception is |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4052 | * from builtin_source() and builtin_eval() */ |
| 4053 | 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] | 4054 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4055 | struct parse_context ctx; |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4056 | o_string temp = NULL_O_STRING; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4057 | int rcode; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4058 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4059 | do { |
| 4060 | initialize_context(&ctx); |
Denis Vlasenko | 8f6bdb4 | 2007-05-16 09:36:55 +0000 | [diff] [blame] | 4061 | update_charmap(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4062 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 5a1437d | 2007-05-24 13:22:47 +0000 | [diff] [blame] | 4063 | inp->promptmode = 0; /* PS1 */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4064 | #endif |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4065 | /* We will stop & execute after each ';' or '\n'. |
| 4066 | * Example: "sleep 9999; echo TEST" + ctrl-C: |
| 4067 | * TEST should be printed */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4068 | temp.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4069 | rcode = parse_stream(&temp, &ctx, inp, ";\n"); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4070 | #if HAS_KEYWORDS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4071 | if (rcode != 1 && ctx.old_flag != 0) { |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4072 | syntax(NULL); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4073 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4074 | #endif |
| 4075 | if (rcode != 1 IF_HAS_KEYWORDS(&& ctx.old_flag == 0)) { |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4076 | done_word(&temp, &ctx); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4077 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4078 | debug_print_tree(ctx.list_head, 0); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4079 | debug_printf_exec("parse_stream_outer: run_and_free_list\n"); |
| 4080 | run_and_free_list(ctx.list_head); |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4081 | } else { |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4082 | /* We arrive here also if rcode == 1 (error in parse_stream) */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4083 | #if HAS_KEYWORDS |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4084 | if (ctx.old_flag != 0) { |
| 4085 | free(ctx.stack); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4086 | o_reset(&temp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4087 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4088 | #endif |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 4089 | /*temp.nonnull = 0; - o_free does it below */ |
| 4090 | /*temp.o_quote = 0; - o_free does it below */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4091 | free_pipe_list(ctx.list_head, /* indent: */ 0); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4092 | /* Discard all unprocessed line input, force prompt on */ |
| 4093 | inp->p = NULL; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4094 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4095 | inp->promptme = 1; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4096 | #endif |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4097 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4098 | o_free(&temp); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4099 | /* loop on syntax errors, return on EOF: */ |
| 4100 | } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4101 | return 0; |
| 4102 | } |
| 4103 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4104 | static int parse_and_run_string(const char *s, int parse_flag) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4105 | { |
| 4106 | struct in_str input; |
| 4107 | setup_string_in_str(&input, s); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4108 | return parse_and_run_stream(&input, parse_flag); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4111 | static int parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4112 | { |
| 4113 | int rcode; |
| 4114 | struct in_str input; |
| 4115 | setup_file_in_str(&input, f); |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4116 | rcode = parse_and_run_stream(&input, 0 /* parse_flag */); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4117 | return rcode; |
| 4118 | } |
| 4119 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4120 | #if ENABLE_HUSH_JOB |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4121 | /* Make sure we have a controlling tty. If we get started under a job |
| 4122 | * aware app (like bash for example), make sure we are now in charge so |
| 4123 | * we don't fight over who gets the foreground */ |
Eric Andersen | eaecbf3 | 2001-10-31 10:41:31 +0000 | [diff] [blame] | 4124 | static void setup_job_control(void) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4125 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4126 | pid_t shell_pgrp; |
| 4127 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4128 | shell_pgrp = getpgrp(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4129 | close_on_exec_on(G.interactive_fd); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4130 | |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 4131 | /* If we were ran as 'hush &', |
| 4132 | * sleep until we are in the foreground. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4133 | while (tcgetpgrp(G.interactive_fd) != shell_pgrp) { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4134 | /* Send TTIN to ourself (should stop us) */ |
Denis Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 4135 | kill(- shell_pgrp, SIGTTIN); |
Denis Vlasenko | a6a1785 | 2007-04-28 16:42:11 +0000 | [diff] [blame] | 4136 | shell_pgrp = getpgrp(); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4137 | } |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4138 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4139 | /* Ignore job-control and misc signals. */ |
| 4140 | set_jobctrl_sighandler(SIG_IGN); |
| 4141 | set_misc_sighandler(SIG_IGN); |
| 4142 | //huh? signal(SIGCHLD, SIG_IGN); |
| 4143 | |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4144 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4145 | set_fatal_sighandler(sigexit); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4146 | |
| 4147 | /* Put ourselves in our own process group. */ |
Bernhard Reutner-Fischer | 864329d | 2008-09-25 10:55:05 +0000 | [diff] [blame] | 4148 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4149 | /* Grab control of the terminal. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4150 | tcsetpgrp(G.interactive_fd, getpid()); |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 4151 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4152 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4153 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4154 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 4155 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 4156 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4157 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4158 | static const struct variable const_shell_ver = { |
| 4159 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4160 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4161 | .max_len = 1, /* 0 can provoke free(name) */ |
| 4162 | .flg_export = 1, |
| 4163 | .flg_read_only = 1, |
| 4164 | }; |
| 4165 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4166 | int opt; |
| 4167 | FILE *input; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4168 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4169 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 4170 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 4171 | INIT_G(); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4172 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4173 | G.root_pid = getpid(); |
Denis Vlasenko | 16c2fea | 2008-06-17 12:28:44 +0000 | [diff] [blame] | 4174 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4175 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4176 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 4177 | G.top_var = &G.shell_ver; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4178 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 4179 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4180 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4181 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 4182 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4183 | if (e) while (*e) { |
| 4184 | char *value = strchr(*e, '='); |
| 4185 | if (value) { /* paranoia */ |
| 4186 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 4187 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 4188 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4189 | cur_var->max_len = strlen(*e); |
| 4190 | cur_var->flg_export = 1; |
| 4191 | } |
| 4192 | e++; |
| 4193 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4194 | putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 4195 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4196 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4197 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 4198 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4199 | /* XXX what should these be while sourcing /etc/profile? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4200 | G.global_argc = argc; |
| 4201 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 4202 | /* Initialize some more globals to non-zero values */ |
| 4203 | set_cwd(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4204 | #if ENABLE_HUSH_INTERACTIVE |
| 4205 | #if ENABLE_FEATURE_EDITING |
| 4206 | cmdedit_set_initial_prompt(); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4207 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4208 | G.PS2 = "> "; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4209 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 4210 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4211 | if (EXIT_SUCCESS) /* otherwise is already done */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4212 | G.last_return_code = EXIT_SUCCESS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4213 | |
| 4214 | if (argv[0] && argv[0][0] == '-') { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4215 | debug_printf("sourcing /etc/profile\n"); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4216 | input = fopen_for_read("/etc/profile"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4217 | if (input != NULL) { |
Denis Vlasenko | a089817 | 2007-10-01 09:59:01 +0000 | [diff] [blame] | 4218 | close_on_exec_on(fileno(input)); |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4219 | parse_and_run_file(input); |
Eric Andersen | a90f20b | 2001-06-26 23:00:21 +0000 | [diff] [blame] | 4220 | fclose(input); |
| 4221 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4222 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4223 | input = stdin; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4224 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4225 | while ((opt = getopt(argc, argv, "c:xif")) > 0) { |
| 4226 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4227 | case 'c': |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4228 | G.global_argv = argv + optind; |
| 4229 | G.global_argc = argc - optind; |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4230 | opt = parse_and_run_string(optarg, 0 /* parse_flag */); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4231 | goto final_return; |
| 4232 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4233 | /* Well, we cannot just declare interactiveness, |
| 4234 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4235 | /* G.interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4236 | break; |
| 4237 | case 'f': |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4238 | G.fake_mode = 1; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4239 | break; |
| 4240 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4241 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4242 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 4243 | " or: sh -c command [args]...\n\n"); |
| 4244 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4245 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4246 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 4247 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4248 | } |
| 4249 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4250 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4251 | /* 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] | 4252 | * the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 4253 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4254 | * no arguments remaining or the -s flag given |
| 4255 | * standard input is a terminal |
| 4256 | * standard output is a terminal |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4257 | * Refer to Posix.2, the description of the 'sh' utility. */ |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4258 | if (argv[optind] == NULL && input == stdin |
| 4259 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 4260 | ) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4261 | G.saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 4262 | debug_printf("saved_tty_pgrp=%d\n", G.saved_tty_pgrp); |
| 4263 | if (G.saved_tty_pgrp >= 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4264 | /* try to dup to high fd#, >= 255 */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4265 | G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 4266 | if (G.interactive_fd < 0) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4267 | /* try to dup to any fd */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4268 | G.interactive_fd = dup(STDIN_FILENO); |
| 4269 | if (G.interactive_fd < 0) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4270 | /* give up */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4271 | G.interactive_fd = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4272 | } |
Denis Vlasenko | 2f1bb36 | 2007-04-21 10:01:14 +0000 | [diff] [blame] | 4273 | // TODO: track & disallow any attempts of user |
| 4274 | // to (inadvertently) close/redirect it |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4275 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4276 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4277 | debug_printf("G.interactive_fd=%d\n", G.interactive_fd); |
| 4278 | if (G.interactive_fd) { |
| 4279 | fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4280 | /* Looks like they want an interactive shell */ |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4281 | setup_job_control(); |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 4282 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 4283 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
| 4284 | die_sleep = -1; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4285 | if (setjmp(die_jmp)) { |
| 4286 | /* xfunc has failed! die die die */ |
| 4287 | hush_exit(xfunc_error_retval); |
| 4288 | } |
Denis Vlasenko | 2f1bb36 | 2007-04-21 10:01:14 +0000 | [diff] [blame] | 4289 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 4290 | 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] | 4291 | printf("Enter 'help' for a list of built-in commands.\n\n"); |
| 4292 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4293 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4294 | #elif ENABLE_HUSH_INTERACTIVE |
| 4295 | /* no job control compiled, only prompt/line editing */ |
| 4296 | if (argv[optind] == NULL && input == stdin |
| 4297 | && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) |
| 4298 | ) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4299 | G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 4300 | if (G.interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4301 | /* try to dup to any fd */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4302 | G.interactive_fd = dup(STDIN_FILENO); |
| 4303 | if (G.interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4304 | /* give up */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4305 | G.interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4306 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4307 | if (G.interactive_fd) { |
| 4308 | fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 4830fc5 | 2008-05-25 21:50:55 +0000 | [diff] [blame] | 4309 | set_misc_sighandler(SIG_IGN); |
| 4310 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4311 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4312 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4313 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4314 | if (argv[optind] == NULL) { |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4315 | opt = parse_and_run_file(stdin); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4316 | } else { |
| 4317 | debug_printf("\nrunning script '%s'\n", argv[optind]); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4318 | G.global_argv = argv + optind; |
| 4319 | G.global_argc = argc - optind; |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4320 | input = xfopen_for_read(argv[optind]); |
Denis Vlasenko | 459a5ad | 2008-02-11 08:35:03 +0000 | [diff] [blame] | 4321 | fcntl(fileno(input), F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4322 | opt = parse_and_run_file(input); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4323 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4324 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4325 | final_return: |
| 4326 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 4327 | #if ENABLE_FEATURE_CLEAN_UP |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4328 | fclose(input); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4329 | if (G.cwd != bb_msg_unknown) |
| 4330 | free((char*)G.cwd); |
| 4331 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4332 | while (cur_var) { |
| 4333 | struct variable *tmp = cur_var; |
| 4334 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 4335 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 4336 | cur_var = cur_var->next; |
| 4337 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4338 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4339 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4340 | hush_exit(opt ? opt : G.last_return_code); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4341 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 4342 | |
| 4343 | |
| 4344 | #if ENABLE_LASH |
| 4345 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 4346 | int lash_main(int argc, char **argv) |
| 4347 | { |
| 4348 | //bb_error_msg("lash is deprecated, please use hush instead"); |
| 4349 | return hush_main(argc, argv); |
| 4350 | } |
| 4351 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4352 | |
| 4353 | |
| 4354 | /* |
| 4355 | * Built-ins |
| 4356 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4357 | static int builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4358 | { |
| 4359 | return 0; |
| 4360 | } |
| 4361 | |
| 4362 | static int builtin_test(char **argv) |
| 4363 | { |
| 4364 | int argc = 0; |
| 4365 | while (*argv) { |
| 4366 | argc++; |
| 4367 | argv++; |
| 4368 | } |
| 4369 | return test_main(argc, argv - argc); |
| 4370 | } |
| 4371 | |
| 4372 | static int builtin_echo(char **argv) |
| 4373 | { |
| 4374 | int argc = 0; |
| 4375 | while (*argv) { |
| 4376 | argc++; |
| 4377 | argv++; |
| 4378 | } |
| 4379 | return echo_main(argc, argv - argc); |
| 4380 | } |
| 4381 | |
| 4382 | static int builtin_eval(char **argv) |
| 4383 | { |
| 4384 | int rcode = EXIT_SUCCESS; |
| 4385 | |
| 4386 | if (argv[1]) { |
| 4387 | char *str = expand_strvec_to_string(argv + 1); |
| 4388 | parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP); |
| 4389 | free(str); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4390 | rcode = G.last_return_code; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4391 | } |
| 4392 | return rcode; |
| 4393 | } |
| 4394 | |
| 4395 | static int builtin_cd(char **argv) |
| 4396 | { |
| 4397 | const char *newdir; |
| 4398 | if (argv[1] == NULL) { |
| 4399 | // bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
| 4400 | // bash says "bash: cd: HOME not set" and does nothing (exitcode 1) |
| 4401 | newdir = getenv("HOME") ? : "/"; |
| 4402 | } else |
| 4403 | newdir = argv[1]; |
| 4404 | if (chdir(newdir)) { |
| 4405 | printf("cd: %s: %s\n", newdir, strerror(errno)); |
| 4406 | return EXIT_FAILURE; |
| 4407 | } |
| 4408 | set_cwd(); |
| 4409 | return EXIT_SUCCESS; |
| 4410 | } |
| 4411 | |
| 4412 | static int builtin_exec(char **argv) |
| 4413 | { |
| 4414 | if (argv[1] == NULL) |
| 4415 | return EXIT_SUCCESS; /* bash does this */ |
| 4416 | { |
| 4417 | #if !BB_MMU |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4418 | char **ptrs2free = NULL; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4419 | #endif |
| 4420 | // FIXME: if exec fails, bash does NOT exit! We do... |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4421 | pseudo_exec_argv(&ptrs2free, argv + 1, 0, NULL); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4422 | /* never returns */ |
| 4423 | } |
| 4424 | } |
| 4425 | |
| 4426 | static int builtin_exit(char **argv) |
| 4427 | { |
| 4428 | // TODO: bash does it ONLY on top-level sh exit (+interacive only?) |
| 4429 | //puts("exit"); /* bash does it */ |
| 4430 | // TODO: warn if we have background jobs: "There are stopped jobs" |
| 4431 | // On second consecutive 'exit', exit anyway. |
| 4432 | if (argv[1] == NULL) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4433 | hush_exit(G.last_return_code); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4434 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 4435 | xfunc_error_retval = 255; |
| 4436 | /* bash: exit -2 == exit 254, no error msg */ |
| 4437 | hush_exit(xatoi(argv[1]) & 0xff); |
| 4438 | } |
| 4439 | |
| 4440 | static int builtin_export(char **argv) |
| 4441 | { |
| 4442 | const char *value; |
| 4443 | char *name = argv[1]; |
| 4444 | |
| 4445 | if (name == NULL) { |
| 4446 | // TODO: |
| 4447 | // ash emits: export VAR='VAL' |
| 4448 | // bash: declare -x VAR="VAL" |
| 4449 | // (both also escape as needed (quotes, $, etc)) |
| 4450 | char **e = environ; |
| 4451 | if (e) |
| 4452 | while (*e) |
| 4453 | puts(*e++); |
| 4454 | return EXIT_SUCCESS; |
| 4455 | } |
| 4456 | |
| 4457 | value = strchr(name, '='); |
| 4458 | if (!value) { |
| 4459 | /* They are exporting something without a =VALUE */ |
| 4460 | struct variable *var; |
| 4461 | |
| 4462 | var = get_local_var(name); |
| 4463 | if (var) { |
| 4464 | var->flg_export = 1; |
| 4465 | putenv(var->varstr); |
| 4466 | } |
| 4467 | /* bash does not return an error when trying to export |
| 4468 | * an undefined variable. Do likewise. */ |
| 4469 | return EXIT_SUCCESS; |
| 4470 | } |
| 4471 | |
| 4472 | set_local_var(xstrdup(name), 1); |
| 4473 | return EXIT_SUCCESS; |
| 4474 | } |
| 4475 | |
| 4476 | #if ENABLE_HUSH_JOB |
| 4477 | /* built-in 'fg' and 'bg' handler */ |
| 4478 | static int builtin_fg_bg(char **argv) |
| 4479 | { |
| 4480 | int i, jobnum; |
| 4481 | struct pipe *pi; |
| 4482 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4483 | if (!G.interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4484 | return EXIT_FAILURE; |
| 4485 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 4486 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4487 | for (pi = G.job_list; pi; pi = pi->next) { |
| 4488 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4489 | goto found; |
| 4490 | } |
| 4491 | } |
| 4492 | bb_error_msg("%s: no current job", argv[0]); |
| 4493 | return EXIT_FAILURE; |
| 4494 | } |
| 4495 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 4496 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 4497 | return EXIT_FAILURE; |
| 4498 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4499 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4500 | if (pi->jobid == jobnum) { |
| 4501 | goto found; |
| 4502 | } |
| 4503 | } |
| 4504 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 4505 | return EXIT_FAILURE; |
| 4506 | found: |
| 4507 | // TODO: bash prints a string representation |
| 4508 | // of job being foregrounded (like "sleep 1 | cat") |
| 4509 | if (*argv[0] == 'f') { |
| 4510 | /* Put the job into the foreground. */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4511 | tcsetpgrp(G.interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4515 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 4516 | for (i = 0; i < pi->num_cmds; i++) { |
| 4517 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 4518 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4519 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4520 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4521 | |
| 4522 | i = kill(- pi->pgrp, SIGCONT); |
| 4523 | if (i < 0) { |
| 4524 | if (errno == ESRCH) { |
| 4525 | delete_finished_bg_job(pi); |
| 4526 | return EXIT_SUCCESS; |
| 4527 | } else { |
| 4528 | bb_perror_msg("kill (SIGCONT)"); |
| 4529 | } |
| 4530 | } |
| 4531 | |
| 4532 | if (*argv[0] == 'f') { |
| 4533 | remove_bg_job(pi); |
| 4534 | return checkjobs_and_fg_shell(pi); |
| 4535 | } |
| 4536 | return EXIT_SUCCESS; |
| 4537 | } |
| 4538 | #endif |
| 4539 | |
| 4540 | #if ENABLE_HUSH_HELP |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4541 | static int builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4542 | { |
| 4543 | const struct built_in_command *x; |
| 4544 | |
| 4545 | printf("\nBuilt-in commands:\n"); |
| 4546 | printf("-------------------\n"); |
| 4547 | for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) { |
| 4548 | printf("%s\t%s\n", x->cmd, x->descr); |
| 4549 | } |
| 4550 | printf("\n\n"); |
| 4551 | return EXIT_SUCCESS; |
| 4552 | } |
| 4553 | #endif |
| 4554 | |
| 4555 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4556 | static int builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4557 | { |
| 4558 | struct pipe *job; |
| 4559 | const char *status_string; |
| 4560 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4561 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4562 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4563 | status_string = "Stopped"; |
| 4564 | else |
| 4565 | status_string = "Running"; |
| 4566 | |
| 4567 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 4568 | } |
| 4569 | return EXIT_SUCCESS; |
| 4570 | } |
| 4571 | #endif |
| 4572 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4573 | static int builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4574 | { |
| 4575 | puts(set_cwd()); |
| 4576 | return EXIT_SUCCESS; |
| 4577 | } |
| 4578 | |
| 4579 | static int builtin_read(char **argv) |
| 4580 | { |
| 4581 | char *string; |
| 4582 | const char *name = argv[1] ? argv[1] : "REPLY"; |
| 4583 | |
| 4584 | string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL); |
| 4585 | return set_local_var(string, 0); |
| 4586 | } |
| 4587 | |
| 4588 | /* built-in 'set [VAR=value]' handler */ |
| 4589 | static int builtin_set(char **argv) |
| 4590 | { |
| 4591 | char *temp = argv[1]; |
| 4592 | struct variable *e; |
| 4593 | |
| 4594 | if (temp == NULL) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4595 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4596 | puts(e->varstr); |
| 4597 | else |
| 4598 | set_local_var(xstrdup(temp), 0); |
| 4599 | |
| 4600 | return EXIT_SUCCESS; |
| 4601 | } |
| 4602 | |
| 4603 | static int builtin_shift(char **argv) |
| 4604 | { |
| 4605 | int n = 1; |
| 4606 | if (argv[1]) { |
| 4607 | n = atoi(argv[1]); |
| 4608 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4609 | if (n >= 0 && n < G.global_argc) { |
| 4610 | G.global_argv[n] = G.global_argv[0]; |
| 4611 | G.global_argc -= n; |
| 4612 | G.global_argv += n; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4613 | return EXIT_SUCCESS; |
| 4614 | } |
| 4615 | return EXIT_FAILURE; |
| 4616 | } |
| 4617 | |
| 4618 | static int builtin_source(char **argv) |
| 4619 | { |
| 4620 | FILE *input; |
| 4621 | int status; |
| 4622 | |
| 4623 | if (argv[1] == NULL) |
| 4624 | return EXIT_FAILURE; |
| 4625 | |
| 4626 | /* XXX search through $PATH is missing */ |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 4627 | input = fopen_for_read(argv[1]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4628 | if (!input) { |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 4629 | bb_error_msg("can't open '%s'", argv[1]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4630 | return EXIT_FAILURE; |
| 4631 | } |
| 4632 | close_on_exec_on(fileno(input)); |
| 4633 | |
| 4634 | /* Now run the file */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4635 | /* XXX argv and argc are broken; need to save old G.global_argv |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4636 | * (pointer only is OK!) on this stack frame, |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4637 | * set G.global_argv=argv+1, recurse, and restore. */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4638 | status = parse_and_run_file(input); |
| 4639 | fclose(input); |
| 4640 | return status; |
| 4641 | } |
| 4642 | |
| 4643 | static int builtin_umask(char **argv) |
| 4644 | { |
| 4645 | mode_t new_umask; |
| 4646 | const char *arg = argv[1]; |
| 4647 | char *end; |
| 4648 | if (arg) { |
| 4649 | new_umask = strtoul(arg, &end, 8); |
| 4650 | if (*end != '\0' || end == arg) { |
| 4651 | return EXIT_FAILURE; |
| 4652 | } |
| 4653 | } else { |
| 4654 | new_umask = umask(0); |
| 4655 | printf("%.3o\n", (unsigned) new_umask); |
| 4656 | } |
| 4657 | umask(new_umask); |
| 4658 | return EXIT_SUCCESS; |
| 4659 | } |
| 4660 | |
| 4661 | static int builtin_unset(char **argv) |
| 4662 | { |
| 4663 | /* bash always returns true */ |
| 4664 | unset_local_var(argv[1]); |
| 4665 | return EXIT_SUCCESS; |
| 4666 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4667 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4668 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4669 | static int builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4670 | { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4671 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 4672 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4673 | return EXIT_SUCCESS; /* bash compat */ |
| 4674 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4675 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
| 4676 | G.depth_break_continue = 1; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4677 | if (argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4678 | G.depth_break_continue = bb_strtou(argv[1], NULL, 10); |
| 4679 | if (errno || !G.depth_break_continue || argv[2]) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 4680 | bb_error_msg("%s: bad arguments", argv[0]); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4681 | G.flag_break_continue = BC_BREAK; |
| 4682 | G.depth_break_continue = UINT_MAX; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4683 | } |
| 4684 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4685 | if (G.depth_of_loop < G.depth_break_continue) |
| 4686 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4687 | return EXIT_SUCCESS; |
| 4688 | } |
| 4689 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4690 | static int builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4691 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 4692 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 4693 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4694 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4695 | #endif |