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