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