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