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