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