blob: a462090c63a9570d0a45ce63fb69d0b74bba88c8 [file] [log] [blame]
Eric Andersen25f27032001-04-26 23:22:31 +00001/* 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 Andersencb81e642003-07-14 21:21:08 +000012 * 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 Andersenc7bda1c2004-03-15 08:29:22 +000015 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersencb81e642003-07-14 21:21:08 +000016 * 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 Andersen25f27032001-04-26 23:22:31 +000022 * Other credits:
Eric Andersen25f27032001-04-26 23:22:31 +000023 * b_addchr() derived from similar w_addchar function in glibc-2.2
24 * setup_redirect(), redirect_opt_num(), and big chunks of main()
Denis Vlasenko55b2de72007-04-18 17:21:28 +000025 * and many builtins derived from contributions by Erik Andersen
Eric Andersen25f27032001-04-26 23:22:31 +000026 * 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 Andersen78a7c992001-05-15 16:30:25 +000047 * aliases
Eric Andersen25f27032001-04-26 23:22:31 +000048 * Arithmetic Expansion
49 * <(list) and >(list) Process Substitution
Eric Andersen83a2ae22001-05-07 17:59:25 +000050 * reserved words: case, esac, select, function
Eric Andersen25f27032001-04-26 23:22:31 +000051 * Here Documents ( << word )
52 * Functions
53 * Major bugs:
Denis Vlasenkob81b3df2007-04-28 16:48:04 +000054 * job handling woefully incomplete and buggy (improved --vda)
Eric Andersen25f27032001-04-26 23:22:31 +000055 * reserved word execution woefully incomplete and buggy
Eric Andersen25f27032001-04-26 23:22:31 +000056 * to-do:
Eric Andersen83a2ae22001-05-07 17:59:25 +000057 * 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 Andersen25f27032001-04-26 23:22:31 +000062 * handle children going into background
63 * clean up recognition of null pipes
Eric Andersen25f27032001-04-26 23:22:31 +000064 * check setting of global_argc and global_argv
65 * control-C handling, probably with longjmp
Eric Andersen25f27032001-04-26 23:22:31 +000066 * follow IFS rules more precisely, including update semantics
Eric Andersen25f27032001-04-26 23:22:31 +000067 * 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 Andersen78a7c992001-05-15 16:30:25 +000073 * document how quoting rules not precisely followed for variable assignments
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +000074 * maybe change charmap[] to use 2-bit entries
Eric Andersen25f27032001-04-26 23:22:31 +000075 * (eventually) remove all the printf's
Eric Andersen25f27032001-04-26 23:22:31 +000076 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000077 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen25f27032001-04-26 23:22:31 +000078 */
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000079
80#include "busybox.h"
Eric Andersen25f27032001-04-26 23:22:31 +000081#include <glob.h> /* glob, of course */
Eric Andersen25f27032001-04-26 23:22:31 +000082#include <getopt.h> /* should be pretty obvious */
Eric Andersen25f27032001-04-26 23:22:31 +000083/* #include <dmalloc.h> */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +000084extern char **environ; /* This is in <unistd.h>, but protected with __USE_GNU */
Denis Vlasenkod01ff132007-05-02 21:40:23 +000085
86
87/* If you comment out one of these below, it will be #defined later
88 * to perform debug printfs to stderr: */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +000089#define debug_printf(...) do {} while (0)
Denis Vlasenko400c5b62007-05-04 13:07:27 +000090/* Finer-grained debug switches */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +000091#define debug_printf_parse(...) do {} while (0)
92#define debug_print_tree(a, b) do {} while (0)
93#define debug_printf_exec(...) do {} while (0)
94#define debug_printf_jobs(...) do {} while (0)
95#define debug_printf_expand(...) do {} while (0)
96#define debug_printf_clean(...) do {} while (0)
Denis Vlasenkod01ff132007-05-02 21:40:23 +000097
98#ifndef debug_printf
99#define debug_printf(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000100#endif
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000101
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000102#ifndef debug_printf_parse
103#define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000104#endif
105
106#ifndef debug_printf_exec
107#define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__)
108#endif
109
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000110#ifndef debug_printf_jobs
111#define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__)
112#define DEBUG_SHELL_JOBS 1
113#endif
114
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000115#ifndef debug_printf_expand
116#define debug_printf_expand(...) fprintf(stderr, __VA_ARGS__)
117#define DEBUG_EXPAND 1
118#endif
119
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000120#ifndef debug_printf_clean
121/* broken, of course, but OK for testing */
122static const char *indenter(int i)
123{
124 static const char blanks[] = " ";
125 return &blanks[sizeof(blanks) - i - 1];
126}
127#define debug_printf_clean(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000128#define DEBUG_CLEAN 1
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000129#endif
130
Eric Andersen25f27032001-04-26 23:22:31 +0000131
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000132#if !ENABLE_HUSH_INTERACTIVE
133#undef ENABLE_FEATURE_EDITING
134#define ENABLE_FEATURE_EDITING 0
135#undef ENABLE_FEATURE_EDITING_FANCY_PROMPT
136#define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0
137#endif
"Robert P. J. Day"f3501602006-07-01 12:19:39 +0000138
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +0000139#define SPECIAL_VAR_SYMBOL 3
140#define FLAG_EXIT_FROM_LOOP 1
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000141#define FLAG_PARSE_SEMICOLON (1 << 1) /* symbol ';' is special for parser */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +0000142#define FLAG_REPARSING (1 << 2) /* >= 2nd pass */
Eric Andersen25f27032001-04-26 23:22:31 +0000143
144typedef enum {
145 REDIRECT_INPUT = 1,
146 REDIRECT_OVERWRITE = 2,
147 REDIRECT_APPEND = 3,
148 REDIRECT_HEREIS = 4,
149 REDIRECT_IO = 5
150} redir_type;
151
152/* The descrip member of this structure is only used to make debugging
153 * output pretty */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000154static const struct {
155 int mode;
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000156 signed char default_fd;
157 char descrip[3];
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000158} redir_table[] = {
Eric Andersen25f27032001-04-26 23:22:31 +0000159 { 0, 0, "()" },
160 { O_RDONLY, 0, "<" },
161 { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
162 { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" },
163 { O_RDONLY, -1, "<<" },
164 { O_RDWR, 1, "<>" }
165};
166
167typedef enum {
168 PIPE_SEQ = 1,
169 PIPE_AND = 2,
170 PIPE_OR = 3,
171 PIPE_BG = 4,
172} pipe_style;
173
174/* might eventually control execution */
175typedef enum {
176 RES_NONE = 0,
177 RES_IF = 1,
178 RES_THEN = 2,
179 RES_ELIF = 3,
180 RES_ELSE = 4,
181 RES_FI = 5,
182 RES_FOR = 6,
183 RES_WHILE = 7,
184 RES_UNTIL = 8,
185 RES_DO = 9,
186 RES_DONE = 10,
Eric Andersenaf44a0e2001-04-27 07:26:12 +0000187 RES_XXXX = 11,
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000188 RES_IN = 12,
189 RES_SNTX = 13
Eric Andersen25f27032001-04-26 23:22:31 +0000190} reserved_style;
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000191enum {
192 FLAG_END = (1 << RES_NONE ),
193 FLAG_IF = (1 << RES_IF ),
194 FLAG_THEN = (1 << RES_THEN ),
195 FLAG_ELIF = (1 << RES_ELIF ),
196 FLAG_ELSE = (1 << RES_ELSE ),
197 FLAG_FI = (1 << RES_FI ),
198 FLAG_FOR = (1 << RES_FOR ),
199 FLAG_WHILE = (1 << RES_WHILE),
200 FLAG_UNTIL = (1 << RES_UNTIL),
201 FLAG_DO = (1 << RES_DO ),
202 FLAG_DONE = (1 << RES_DONE ),
203 FLAG_IN = (1 << RES_IN ),
204 FLAG_START = (1 << RES_XXXX ),
205};
Eric Andersen25f27032001-04-26 23:22:31 +0000206
207/* This holds pointers to the various results of parsing */
208struct p_context {
209 struct child_prog *child;
210 struct pipe *list_head;
211 struct pipe *pipe;
212 struct redir_struct *pending_redirect;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +0000213 reserved_style res_w;
214 int old_flag; /* for figuring out valid reserved words */
Eric Andersen25f27032001-04-26 23:22:31 +0000215 struct p_context *stack;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +0000216 int parse_type; /* define type of parser : ";$" common or special symbol */
Eric Andersen25f27032001-04-26 23:22:31 +0000217 /* How about quoting status? */
218};
219
220struct redir_struct {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000221 struct redir_struct *next; /* pointer to the next redirect in the list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000222 redir_type type; /* type of redirection */
223 int fd; /* file descriptor being redirected */
224 int dup; /* -1, or file descriptor being duplicated */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000225 glob_t word; /* *word.gl_pathv is the filename */
Eric Andersen25f27032001-04-26 23:22:31 +0000226};
227
228struct child_prog {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000229 pid_t pid; /* 0 if exited */
230 char **argv; /* program name and arguments */
231 struct pipe *group; /* if non-NULL, first in group or subshell */
232 int subshell; /* flag, non-zero if group must be forked */
233 struct redir_struct *redirects; /* I/O redirections */
234 glob_t glob_result; /* result of parameter globbing */
235 int is_stopped; /* is the program currently running? */
236 struct pipe *family; /* pointer back to the child's parent pipe */
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000237 //sp counting seems to be broken... so commented out, grep for '//sp:'
238 //sp: int sp; /* number of SPECIAL_VAR_SYMBOL */
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000239 int type;
Eric Andersen25f27032001-04-26 23:22:31 +0000240};
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000241/* argv vector may contain variable references (^Cvar^C, ^C0^C etc)
242 * and on execution these are substituted with their values.
243 * Substitution can make _several_ words out of one argv[n]!
244 * Example: argv[0]=='.^C*^C.' here: echo .$*.
245 */
Eric Andersen25f27032001-04-26 23:22:31 +0000246
247struct pipe {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000248 struct pipe *next;
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000249 int num_progs; /* total number of programs in job */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000250 int running_progs; /* number of programs running (not exited) */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000251 char *cmdbuf; /* buffer various argv's point into */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000252#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000253 int jobid; /* job number */
254 char *cmdtext; /* name of job */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000255 pid_t pgrp; /* process group ID for the job */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000256#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000257 struct child_prog *progs; /* array of commands in pipe */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000258 int stopped_progs; /* number of programs alive, but stopped */
259 int job_context; /* bitmask defining current context */
260 pipe_style followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
261 reserved_style r_mode; /* supports if, for, while, until */
Eric Andersen25f27032001-04-26 23:22:31 +0000262};
263
Eric Andersen25f27032001-04-26 23:22:31 +0000264struct close_me {
Eric Andersen25f27032001-04-26 23:22:31 +0000265 struct close_me *next;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000266 int fd;
Eric Andersen25f27032001-04-26 23:22:31 +0000267};
268
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000269struct variables {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000270 struct variables *next;
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000271 const char *name;
272 const char *value;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000273 int flg_export;
274 int flg_read_only;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000275};
276
Eric Andersen25f27032001-04-26 23:22:31 +0000277typedef struct {
278 char *data;
279 int length;
280 int maxlen;
281 int quote;
282 int nonnull;
283} o_string;
284#define NULL_O_STRING {NULL,0,0,0,0}
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000285/* used for initialization: o_string foo = NULL_O_STRING; */
Eric Andersen25f27032001-04-26 23:22:31 +0000286
287/* I can almost use ordinary FILE *. Is open_memstream() universally
288 * available? Where is it documented? */
289struct in_str {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +0000290 const char *p;
291 /* eof_flag=1: last char in ->p is really an EOF */
292 char eof_flag; /* meaningless if ->p == NULL */
293 char peek_buf[2];
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000294#if ENABLE_HUSH_INTERACTIVE
Eric Andersen25f27032001-04-26 23:22:31 +0000295 int __promptme;
296 int promptmode;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000297#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000298 FILE *file;
299 int (*get) (struct in_str *);
300 int (*peek) (struct in_str *);
301};
302#define b_getch(input) ((input)->get(input))
303#define b_peek(input) ((input)->peek(input))
304
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000305enum {
306 CHAR_ORDINARY = 0,
307 CHAR_ORDINARY_IF_QUOTED = 1, /* example: *, # */
308 CHAR_IFS = 2, /* treated as ordinary if quoted */
309 CHAR_SPECIAL = 3, /* example: $ */
Eric Andersen25f27032001-04-26 23:22:31 +0000310};
311
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000312
313/* "Globals" within this file */
314
315#define HUSH_VER_STR "0.02"
316static const struct variables const_shell_ver = {
317 NULL, "HUSH_VERSION", HUSH_VER_STR, 1, 1
318};
319
320/* Sorted roughly by size (smaller offsets == smaller code) */
321struct globals {
322#if ENABLE_HUSH_INTERACTIVE
323 /* 'interactive_fd' is a fd# open to ctty, if we have one
324 * _AND_ if we decided to act interactively */
325 int interactive_fd;
326 const char *PS1;
327 const char *PS2;
328#endif
329#if ENABLE_FEATURE_EDITING
330 line_input_t *line_input_state;
331#endif
332#if ENABLE_HUSH_JOB
333 int run_list_level;
334 pid_t saved_task_pgrp;
335 pid_t saved_tty_pgrp;
336 int last_jobid;
337 struct pipe *job_list;
338 struct pipe *toplevel_list;
339 smallint ctrl_z_flag;
340#endif
341 smallint fake_mode;
342 /* these three support $?, $#, and $1 */
343 char **global_argv;
344 int global_argc;
345 int last_return_code;
346 const char *ifs;
347 struct close_me *close_me_head;
348 const char *cwd;
349 unsigned last_bg_pid;
350 struct variables *top_vars; /* = &shell_ver (both are set in main()) */
351 struct variables shell_ver; /* = const_shell_ver */
352#if ENABLE_FEATURE_SH_STANDALONE
353 struct nofork_save_area nofork_save;
354#endif
355#if ENABLE_HUSH_JOB
356 sigjmp_buf toplevel_jb;
357#endif
358 unsigned char charmap[256];
359 char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2];
360};
361
362#define G (*ptr_to_globals)
363
364#if !ENABLE_HUSH_INTERACTIVE
365enum { interactive_fd = 0 };
366#endif
367#if !ENABLE_HUSH_JOB
368enum { run_list_level = 0 };
369#endif
370
371#if ENABLE_HUSH_INTERACTIVE
372#define interactive_fd (G.interactive_fd )
373#define PS1 (G.PS1 )
374#define PS2 (G.PS2 )
375#endif
376#if ENABLE_FEATURE_EDITING
377#define line_input_state (G.line_input_state)
378#endif
379#if ENABLE_HUSH_JOB
380#define run_list_level (G.run_list_level )
381#define saved_task_pgrp (G.saved_task_pgrp )
382#define saved_tty_pgrp (G.saved_tty_pgrp )
383#define last_jobid (G.last_jobid )
384#define job_list (G.job_list )
385#define toplevel_list (G.toplevel_list )
386#define toplevel_jb (G.toplevel_jb )
387#define ctrl_z_flag (G.ctrl_z_flag )
388#endif /* JOB */
389#define global_argv (G.global_argv )
390#define global_argc (G.global_argc )
391#define last_return_code (G.last_return_code)
392#define ifs (G.ifs )
393#define fake_mode (G.fake_mode )
394#define close_me_head (G.close_me_head )
395#define cwd (G.cwd )
396#define last_bg_pid (G.last_bg_pid )
397#define top_vars (G.top_vars )
398#define shell_ver (G.shell_ver )
399#if ENABLE_FEATURE_SH_STANDALONE
400#define nofork_save (G.nofork_save )
401#endif
402#if ENABLE_HUSH_JOB
403#define toplevel_jb (G.toplevel_jb )
404#endif
405#define charmap (G.charmap )
406#define user_input_buf (G.user_input_buf )
407
408
409#define B_CHUNK 100
410#define B_NOSPAC 1
411#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
412
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000413static void __syntax(int line)
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000414{
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000415 bb_error_msg("syntax error hush.c:%d", line);
Eric Andersen25f27032001-04-26 23:22:31 +0000416}
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000417#define syntax() __syntax(__LINE__)
Eric Andersen25f27032001-04-26 23:22:31 +0000418
419/* Index of subroutines: */
420/* function prototypes for builtins */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000421static int builtin_cd(char **argv);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000422static int builtin_eval(char **argv);
423static int builtin_exec(char **argv);
424static int builtin_exit(char **argv);
425static int builtin_export(char **argv);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000426#if ENABLE_HUSH_JOB
Denis Vlasenko1359da62007-04-21 23:27:30 +0000427static int builtin_fg_bg(char **argv);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000428static int builtin_jobs(char **argv);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000429#endif
430static int builtin_help(char **argv);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000431static int builtin_pwd(char **argv);
432static int builtin_read(char **argv);
433static int builtin_set(char **argv);
434static int builtin_shift(char **argv);
435static int builtin_source(char **argv);
436static int builtin_umask(char **argv);
437static int builtin_unset(char **argv);
438static int builtin_not_written(char **argv);
Eric Andersen25f27032001-04-26 23:22:31 +0000439/* o_string manipulation: */
440static int b_check_space(o_string *o, int len);
441static int b_addchr(o_string *o, int ch);
442static void b_reset(o_string *o);
443static int b_addqchr(o_string *o, int ch, int quote);
Eric Andersen25f27032001-04-26 23:22:31 +0000444/* in_str manipulations: */
445static int static_get(struct in_str *i);
446static int static_peek(struct in_str *i);
447static int file_get(struct in_str *i);
448static int file_peek(struct in_str *i);
449static void setup_file_in_str(struct in_str *i, FILE *f);
450static void setup_string_in_str(struct in_str *i, const char *s);
451/* close_me manipulations: */
452static void mark_open(int fd);
453static void mark_closed(int fd);
Eric Anderseneaecbf32001-10-31 10:41:31 +0000454static void close_all(void);
Eric Andersen25f27032001-04-26 23:22:31 +0000455/* "run" the final data structures: */
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000456#if !defined(DEBUG_CLEAN)
457#define free_pipe_list(head, indent) free_pipe_list(head)
458#define free_pipe(pi, indent) free_pipe(pi)
459#endif
Eric Andersenbf7df042001-05-23 22:18:35 +0000460static int free_pipe_list(struct pipe *head, int indent);
461static int free_pipe(struct pipe *pi, int indent);
Eric Andersen25f27032001-04-26 23:22:31 +0000462/* really run the final data structures: */
463static int setup_redirects(struct child_prog *prog, int squirrel[]);
Eric Andersen25f27032001-04-26 23:22:31 +0000464static int run_list_real(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000465static void pseudo_exec_argv(char **argv) ATTRIBUTE_NORETURN;
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000466static void pseudo_exec(struct child_prog *child) ATTRIBUTE_NORETURN;
Eric Andersen25f27032001-04-26 23:22:31 +0000467static int run_pipe_real(struct pipe *pi);
468/* extended glob support: */
469static int globhack(const char *src, int flags, glob_t *pglob);
470static int glob_needed(const char *s);
471static int xglob(o_string *dest, int flags, glob_t *pglob);
Eric Andersen78a7c992001-05-15 16:30:25 +0000472/* variable assignment: */
Eric Andersen78a7c992001-05-15 16:30:25 +0000473static int is_assignment(const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000474/* data structure manipulation: */
475static int setup_redirect(struct p_context *ctx, int fd, redir_type style, struct in_str *input);
476static void initialize_context(struct p_context *ctx);
477static int done_word(o_string *dest, struct p_context *ctx);
478static int done_command(struct p_context *ctx);
479static int done_pipe(struct p_context *ctx, pipe_style type);
480/* primary string parsing: */
481static int redirect_dup_num(struct in_str *input);
482static int redirect_opt_num(o_string *o);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000483static int process_command_subs(o_string *dest, struct p_context *ctx, struct in_str *input, const char *subst_end);
Eric Andersen25f27032001-04-26 23:22:31 +0000484static int parse_group(o_string *dest, struct p_context *ctx, struct in_str *input, int ch);
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000485static const char *lookup_param(const char *src);
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000486static char *make_string(char **inp);
Eric Andersen25f27032001-04-26 23:22:31 +0000487static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000488static int parse_stream(o_string *dest, struct p_context *ctx, struct in_str *input0, const char *end_trigger);
Eric Andersen25f27032001-04-26 23:22:31 +0000489/* setup: */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +0000490static int parse_stream_outer(struct in_str *inp, int parse_flag);
491static int parse_string_outer(const char *s, int parse_flag);
Eric Andersen25f27032001-04-26 23:22:31 +0000492static int parse_file_outer(FILE *f);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000493/* job management: */
Eric Andersenc798b072001-06-22 06:23:03 +0000494static int checkjobs(struct pipe* fg_pipe);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000495#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +0000496static int checkjobs_and_fg_shell(struct pipe* fg_pipe);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000497static void insert_bg_job(struct pipe *pi);
498static void remove_bg_job(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000499static void delete_finished_bg_job(struct pipe *pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000500#else
501int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */
502#endif
Eric Andersenf72f5622001-05-15 23:21:41 +0000503/* local variable support */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +0000504static char **expand_variables_to_list(char **argv);
505/* used for expansion of right hand of assignments */
506static char *expand_variables_to_string(const char *str);
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000507static const char *get_local_var(const char *var);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000508static int set_local_var(const char *s, int flg_export);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000509static void unset_local_var(const char *name);
Eric Andersen25f27032001-04-26 23:22:31 +0000510
511/* Table of built-in functions. They can be forked or not, depending on
512 * context: within pipes, they fork. As simple commands, they do not.
513 * When used in non-forking context, they can change global variables
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000514 * in the parent shell process. If forked, of course they cannot.
Eric Andersen25f27032001-04-26 23:22:31 +0000515 * For example, 'unset foo | whatever' will parse and run, but foo will
516 * still be set at the end. */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000517struct built_in_command {
518 const char *cmd; /* name */
519 const char *descr; /* description */
520 int (*function) (char **argv); /* function ptr */
521};
522
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +0000523static const struct built_in_command bltins[] = {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000524#if ENABLE_HUSH_JOB
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000525 { "bg", "Resume a job in the background", builtin_fg_bg },
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000526#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000527 { "break", "Exit for, while or until loop", builtin_not_written },
528 { "cd", "Change working directory", builtin_cd },
529 { "continue", "Continue for, while or until loop", builtin_not_written },
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000530 { "eval", "Construct and run shell command", builtin_eval },
531 { "exec", "Exec command, replacing this shell with the exec'd process",
532 builtin_exec },
533 { "exit", "Exit from shell()", builtin_exit },
534 { "export", "Set environment variable", builtin_export },
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000535#if ENABLE_HUSH_JOB
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000536 { "fg", "Bring job into the foreground", builtin_fg_bg },
537 { "jobs", "Lists the active jobs", builtin_jobs },
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000538#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000539 { "pwd", "Print current directory", builtin_pwd },
540 { "read", "Input environment variable", builtin_read },
541 { "return", "Return from a function", builtin_not_written },
542 { "set", "Set/unset shell local variables", builtin_set },
543 { "shift", "Shift positional parameters", builtin_shift },
544 { "trap", "Trap signals", builtin_not_written },
545 { "ulimit","Controls resource limits", builtin_not_written },
546 { "umask","Sets file creation mask", builtin_umask },
547 { "unset", "Unset environment variable", builtin_unset },
548 { ".", "Source-in and run commands in a file", builtin_source },
549 { "help", "List shell built-in commands", builtin_help },
550 { NULL, NULL, NULL }
Eric Andersen25f27032001-04-26 23:22:31 +0000551};
552
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000553#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000554
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000555/* move to libbb? */
556static void signal_SA_RESTART(int sig, void (*handler)(int))
557{
558 struct sigaction sa;
559 sa.sa_handler = handler;
560 sa.sa_flags = SA_RESTART;
561 sigemptyset(&sa.sa_mask);
562 sigaction(sig, &sa, NULL);
563}
564
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000565/* Signals are grouped, we handle them in batches */
566static void set_fatal_sighandler(void (*handler)(int))
567{
568 signal(SIGILL , handler);
569 signal(SIGTRAP, handler);
570 signal(SIGABRT, handler);
571 signal(SIGFPE , handler);
572 signal(SIGBUS , handler);
573 signal(SIGSEGV, handler);
574 /* bash 3.2 seems to handle these just like 'fatal' ones */
575 signal(SIGHUP , handler);
576 signal(SIGPIPE, handler);
577 signal(SIGALRM, handler);
578}
579static void set_jobctrl_sighandler(void (*handler)(int))
580{
581 signal(SIGTSTP, handler);
582 signal(SIGTTIN, handler);
583 signal(SIGTTOU, handler);
584}
585static void set_misc_sighandler(void (*handler)(int))
586{
587 signal(SIGINT , handler);
588 signal(SIGQUIT, handler);
589 signal(SIGTERM, handler);
590}
591/* SIGCHLD is special and handled separately */
592
593static void set_every_sighandler(void (*handler)(int))
594{
595 set_fatal_sighandler(handler);
596 set_jobctrl_sighandler(handler);
597 set_misc_sighandler(handler);
598 signal(SIGCHLD, handler);
599}
600
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000601static void handler_ctrl_c(int sig)
602{
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000603 debug_printf_jobs("got sig %d\n", sig);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000604// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000605 siglongjmp(toplevel_jb, 1);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000606}
607
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000608static void handler_ctrl_z(int sig)
609{
610 pid_t pid;
611
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000612 debug_printf_jobs("got tty sig %d in pid %d\n", sig, getpid());
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000613 pid = fork();
Denis Vlasenkoc2990322007-05-16 12:57:12 +0000614 if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000615 return;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000616 ctrl_z_flag = 1;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000617 if (!pid) { /* child */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000618 setpgrp();
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000619 debug_printf_jobs("set pgrp for child %d ok\n", getpid());
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000620 set_every_sighandler(SIG_DFL);
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000621 raise(SIGTSTP); /* resend TSTP so that child will be stopped */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000622 debug_printf_jobs("returning in child\n");
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000623 /* return to nofork, it will eventually exit now,
624 * not return back to shell */
625 return;
626 }
627 /* parent */
628 /* finish filling up pipe info */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000629 toplevel_list->pgrp = pid; /* child is in its own pgrp */
630 toplevel_list->progs[0].pid = pid;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000631 /* parent needs to longjmp out of running nofork.
632 * we will "return" exitcode 0, with child put in background */
633// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000634 debug_printf_jobs("siglongjmp in parent\n");
635 siglongjmp(toplevel_jb, 1);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000636}
637
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000638/* Restores tty foreground process group, and exits.
639 * May be called as signal handler for fatal signal
640 * (will faithfully resend signal to itself, producing correct exit state)
641 * or called directly with -EXITCODE.
642 * We also call it if xfunc is exiting. */
643static void sigexit(int sig) ATTRIBUTE_NORETURN;
644static void sigexit(int sig)
645{
646 sigset_t block_all;
647
648 /* Disable all signals: job control, SIGPIPE, etc. */
649 sigfillset(&block_all);
650 sigprocmask(SIG_SETMASK, &block_all, NULL);
651
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000652 if (interactive_fd)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000653 tcsetpgrp(interactive_fd, saved_tty_pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000654
655 /* Not a signal, just exit */
656 if (sig <= 0)
657 _exit(- sig);
658
659 /* Enable only this sig and kill ourself with it */
660 signal(sig, SIG_DFL);
661 sigdelset(&block_all, sig);
662 sigprocmask(SIG_SETMASK, &block_all, NULL);
663 raise(sig);
664 _exit(1); /* Should not reach it */
665}
666
667/* Restores tty foreground process group, and exits. */
668static void hush_exit(int exitcode) ATTRIBUTE_NORETURN;
669static void hush_exit(int exitcode)
670{
671 fflush(NULL); /* flush all streams */
672 sigexit(- (exitcode & 0xff));
673}
674
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000675#else /* !JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000676
677#define set_fatal_sighandler(handler) ((void)0)
678#define set_jobctrl_sighandler(handler) ((void)0)
679#define set_misc_sighandler(handler) ((void)0)
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000680#define hush_exit(e) exit(e)
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000681
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000682#endif /* JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000683
684
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000685static const char *set_cwd(void)
686{
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000687 if (cwd == bb_msg_unknown)
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000688 cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000689 cwd = xrealloc_getcwd_or_warn((char *)cwd);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000690 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000691 cwd = bb_msg_unknown;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000692 return cwd;
693}
694
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000695/* built-in 'eval' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000696static int builtin_eval(char **argv)
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000697{
698 char *str = NULL;
699 int rcode = EXIT_SUCCESS;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000700
Denis Vlasenko1359da62007-04-21 23:27:30 +0000701 if (argv[1]) {
702 str = make_string(argv + 1);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000703 parse_string_outer(str, FLAG_EXIT_FROM_LOOP |
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000704 FLAG_PARSE_SEMICOLON);
705 free(str);
706 rcode = last_return_code;
707 }
708 return rcode;
709}
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000710
Eric Andersen25f27032001-04-26 23:22:31 +0000711/* built-in 'cd <path>' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000712static int builtin_cd(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000713{
714 char *newdir;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000715 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000716 newdir = getenv("HOME");
717 else
Denis Vlasenko1359da62007-04-21 23:27:30 +0000718 newdir = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000719 if (chdir(newdir)) {
720 printf("cd: %s: %s\n", newdir, strerror(errno));
721 return EXIT_FAILURE;
722 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000723 set_cwd();
Eric Andersen25f27032001-04-26 23:22:31 +0000724 return EXIT_SUCCESS;
725}
726
Eric Andersen25f27032001-04-26 23:22:31 +0000727/* built-in 'exec' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000728static int builtin_exec(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000729{
Denis Vlasenko1359da62007-04-21 23:27:30 +0000730 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000731 return EXIT_SUCCESS; /* Really? */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000732 pseudo_exec_argv(argv + 1);
Eric Andersen25f27032001-04-26 23:22:31 +0000733 /* never returns */
734}
735
736/* built-in 'exit' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000737static int builtin_exit(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000738{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000739// TODO: bash does it ONLY on top-level sh exit (+interacive only?)
740 //puts("exit"); /* bash does it */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000741// TODO: warn if we have background jobs: "There are stopped jobs"
742// On second consecutive 'exit', exit anyway.
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000743
Denis Vlasenko1359da62007-04-21 23:27:30 +0000744 if (argv[1] == NULL)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000745 hush_exit(last_return_code);
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000746 /* mimic bash: exit 123abc == exit 255 + error msg */
747 xfunc_error_retval = 255;
748 /* bash: exit -2 == exit 254, no error msg */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000749 hush_exit(xatoi(argv[1]) & 0xff);
Eric Andersen25f27032001-04-26 23:22:31 +0000750}
751
752/* built-in 'export VAR=value' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000753static int builtin_export(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000754{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000755 int res = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000756 char *name = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000757
Eric Andersenf72f5622001-05-15 23:21:41 +0000758 if (name == NULL) {
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000759 // TODO:
760 // ash emits: export VAR='VAL'
761 // bash: declare -x VAR="VAL"
762 // (both also escape as needed (quotes, $, etc))
763 char **e = environ;
764 if (e)
765 while (*e)
766 puts(*e++);
767 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000768 }
Eric Andersenf72f5622001-05-15 23:21:41 +0000769
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000770 name = xstrdup(name);
771 {
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000772 const char *value = strchr(name, '=');
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000773
Eric Andersen94ac2442001-05-22 19:05:18 +0000774 if (!value) {
775 char *tmp;
776 /* They are exporting something without an =VALUE */
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000777
Eric Andersen94ac2442001-05-22 19:05:18 +0000778 value = get_local_var(name);
779 if (value) {
780 size_t ln = strlen(name);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000781
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000782 tmp = xrealloc(name, ln+strlen(value)+2);
783 sprintf(tmp+ln, "=%s", value);
784 name = tmp;
Eric Andersen94ac2442001-05-22 19:05:18 +0000785 } else {
786 /* bash does not return an error when trying to export
787 * an undefined variable. Do likewise. */
788 res = 1;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000789 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000790 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000791 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000792 if (res < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000793 bb_perror_msg("export");
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000794 else if (res == 0)
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000795 res = set_local_var(name, 1);
796 else
797 res = 0;
798 free(name);
799 return res;
Eric Andersen25f27032001-04-26 23:22:31 +0000800}
801
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000802#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +0000803/* built-in 'fg' and 'bg' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000804static int builtin_fg_bg(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000805{
Eric Andersen0fcd4472001-05-02 20:12:03 +0000806 int i, jobnum;
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000807 struct pipe *pi;
Eric Andersen25f27032001-04-26 23:22:31 +0000808
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000809 if (!interactive_fd)
Eric Andersenc798b072001-06-22 06:23:03 +0000810 return EXIT_FAILURE;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000811 /* If they gave us no args, assume they want the last backgrounded task */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000812 if (!argv[1]) {
Eric Andersenc798b072001-06-22 06:23:03 +0000813 for (pi = job_list; pi; pi = pi->next) {
814 if (pi->jobid == last_jobid) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000815 goto found;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000816 }
817 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000818 bb_error_msg("%s: no current job", argv[0]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000819 return EXIT_FAILURE;
820 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000821 if (sscanf(argv[1], "%%%d", &jobnum) != 1) {
822 bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000823 return EXIT_FAILURE;
824 }
825 for (pi = job_list; pi; pi = pi->next) {
826 if (pi->jobid == jobnum) {
827 goto found;
Eric Andersen25f27032001-04-26 23:22:31 +0000828 }
829 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000830 bb_error_msg("%s: %d: no such job", argv[0], jobnum);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000831 return EXIT_FAILURE;
832 found:
Denis Vlasenko52881e92007-04-21 13:42:52 +0000833 // TODO: bash prints a string representation
834 // of job being foregrounded (like "sleep 1 | cat")
Denis Vlasenko1359da62007-04-21 23:27:30 +0000835 if (*argv[0] == 'f') {
Eric Andersen028b65b2001-06-28 01:10:11 +0000836 /* Put the job into the foreground. */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000837 tcsetpgrp(interactive_fd, pi->pgrp);
Eric Andersen25f27032001-04-26 23:22:31 +0000838 }
839
840 /* Restart the processes in the job */
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000841 debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_progs, pi->pgrp);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000842 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000843 debug_printf_jobs("reviving pid %d\n", pi->progs[i].pid);
Eric Andersen0fcd4472001-05-02 20:12:03 +0000844 pi->progs[i].is_stopped = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000845 }
846 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +0000847
Denis Vlasenkobb81c582007-01-30 22:32:09 +0000848 i = kill(- pi->pgrp, SIGCONT);
849 if (i < 0) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000850 if (errno == ESRCH) {
Denis Vlasenko1359da62007-04-21 23:27:30 +0000851 delete_finished_bg_job(pi);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000852 return EXIT_SUCCESS;
Eric Andersen028b65b2001-06-28 01:10:11 +0000853 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000854 bb_perror_msg("kill (SIGCONT)");
Eric Andersen028b65b2001-06-28 01:10:11 +0000855 }
856 }
Eric Andersen25f27032001-04-26 23:22:31 +0000857
Denis Vlasenko1359da62007-04-21 23:27:30 +0000858 if (*argv[0] == 'f') {
859 remove_bg_job(pi);
Denis Vlasenko52881e92007-04-21 13:42:52 +0000860 return checkjobs_and_fg_shell(pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000861 }
Eric Andersen25f27032001-04-26 23:22:31 +0000862 return EXIT_SUCCESS;
863}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000864#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000865
866/* built-in 'help' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000867static int builtin_help(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +0000868{
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +0000869 const struct built_in_command *x;
Eric Andersen25f27032001-04-26 23:22:31 +0000870
871 printf("\nBuilt-in commands:\n");
872 printf("-------------------\n");
873 for (x = bltins; x->cmd; x++) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000874 if (x->descr == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000875 continue;
876 printf("%s\t%s\n", x->cmd, x->descr);
877 }
878 printf("\n\n");
879 return EXIT_SUCCESS;
880}
881
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000882#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +0000883/* built-in 'jobs' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000884static int builtin_jobs(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +0000885{
886 struct pipe *job;
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000887 const char *status_string;
Eric Andersen25f27032001-04-26 23:22:31 +0000888
Eric Andersenc798b072001-06-22 06:23:03 +0000889 for (job = job_list; job; job = job->next) {
Eric Andersen25f27032001-04-26 23:22:31 +0000890 if (job->running_progs == job->stopped_progs)
891 status_string = "Stopped";
892 else
893 status_string = "Running";
Eric Andersen52a97ca2001-06-22 06:49:26 +0000894
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000895 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
Eric Andersen25f27032001-04-26 23:22:31 +0000896 }
897 return EXIT_SUCCESS;
898}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000899#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000900
Eric Andersen25f27032001-04-26 23:22:31 +0000901/* built-in 'pwd' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000902static int builtin_pwd(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +0000903{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000904 puts(set_cwd());
Eric Andersen25f27032001-04-26 23:22:31 +0000905 return EXIT_SUCCESS;
906}
907
908/* built-in 'read VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000909static int builtin_read(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000910{
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +0000911 char string[BUFSIZ];
912 char *p;
913 const char *name = argv[1] ? argv[1] : "REPLY";
914 int name_len = strlen(name);
Eric Andersen25f27032001-04-26 23:22:31 +0000915
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +0000916 if (name_len >= sizeof(string) - 2)
917 return EXIT_FAILURE;
918 strcpy(string, name);
919 p = string + name_len;
920 *p++ = '=';
921 *p = '\0'; /* In case stdin has only EOF */
922 /* read string. name_len+1 chars are already used by 'name=' */
923 fgets(p, sizeof(string) - 1 - name_len, stdin);
924 chomp(p);
925 return set_local_var(string, 0);
Eric Andersen25f27032001-04-26 23:22:31 +0000926}
927
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +0000928/* built-in 'set [VAR=value]' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000929static int builtin_set(char **argv)
Eric Andersenf72f5622001-05-15 23:21:41 +0000930{
Denis Vlasenko1359da62007-04-21 23:27:30 +0000931 char *temp = argv[1];
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000932 struct variables *e;
Eric Andersenf72f5622001-05-15 23:21:41 +0000933
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000934 if (temp == NULL)
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000935 for (e = top_vars; e; e = e->next)
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000936 printf("%s=%s\n", e->name, e->value);
937 else
938 set_local_var(temp, 0);
939
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000940 return EXIT_SUCCESS;
Eric Andersenf72f5622001-05-15 23:21:41 +0000941}
942
943
Eric Andersen25f27032001-04-26 23:22:31 +0000944/* Built-in 'shift' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000945static int builtin_shift(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000946{
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000947 int n = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000948 if (argv[1]) {
949 n = atoi(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +0000950 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000951 if (n >= 0 && n < global_argc) {
Eric Andersen25f27032001-04-26 23:22:31 +0000952 /* XXX This probably breaks $0 */
953 global_argc -= n;
954 global_argv += n;
955 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000956 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +0000957 return EXIT_FAILURE;
Eric Andersen25f27032001-04-26 23:22:31 +0000958}
959
960/* Built-in '.' handler (read-in and execute commands from file) */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000961static int builtin_source(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000962{
963 FILE *input;
964 int status;
965
Denis Vlasenko1359da62007-04-21 23:27:30 +0000966 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000967 return EXIT_FAILURE;
968
969 /* XXX search through $PATH is missing */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000970 input = fopen(argv[1], "r");
Eric Andersen25f27032001-04-26 23:22:31 +0000971 if (!input) {
Denis Vlasenko1359da62007-04-21 23:27:30 +0000972 bb_error_msg("cannot open '%s'", argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +0000973 return EXIT_FAILURE;
974 }
975
976 /* Now run the file */
977 /* XXX argv and argc are broken; need to save old global_argv
978 * (pointer only is OK!) on this stack frame,
Denis Vlasenko1359da62007-04-21 23:27:30 +0000979 * set global_argv=argv+1, recurse, and restore. */
Eric Andersen25f27032001-04-26 23:22:31 +0000980 mark_open(fileno(input));
981 status = parse_file_outer(input);
982 mark_closed(fileno(input));
983 fclose(input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000984 return status;
Eric Andersen25f27032001-04-26 23:22:31 +0000985}
986
Denis Vlasenko1359da62007-04-21 23:27:30 +0000987static int builtin_umask(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000988{
Eric Andersen83a2ae22001-05-07 17:59:25 +0000989 mode_t new_umask;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000990 const char *arg = argv[1];
Eric Andersen83a2ae22001-05-07 17:59:25 +0000991 char *end;
992 if (arg) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000993 new_umask = strtoul(arg, &end, 8);
994 if (*end != '\0' || end == arg) {
Eric Andersen83a2ae22001-05-07 17:59:25 +0000995 return EXIT_FAILURE;
996 }
997 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000998 new_umask = umask(0);
999 printf("%.3o\n", (unsigned) new_umask);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001000 }
1001 umask(new_umask);
1002 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00001003}
1004
1005/* built-in 'unset VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001006static int builtin_unset(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001007{
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001008 /* bash always returns true */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001009 unset_local_var(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001010 return EXIT_SUCCESS;
1011}
1012
Denis Vlasenko1359da62007-04-21 23:27:30 +00001013static int builtin_not_written(char **argv)
Eric Andersen83a2ae22001-05-07 17:59:25 +00001014{
Denis Vlasenko1359da62007-04-21 23:27:30 +00001015 printf("builtin_%s not written\n", argv[0]);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001016 return EXIT_FAILURE;
1017}
1018
Eric Andersen25f27032001-04-26 23:22:31 +00001019static int b_check_space(o_string *o, int len)
1020{
1021 /* It would be easy to drop a more restrictive policy
1022 * in here, such as setting a maximum string length */
1023 if (o->length + len > o->maxlen) {
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001024 /* assert(data == NULL || o->maxlen != 0); */
Denis Vlasenkoef36ead2007-05-02 15:34:47 +00001025 o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001026 o->data = xrealloc(o->data, 1 + o->maxlen);
Eric Andersen25f27032001-04-26 23:22:31 +00001027 }
1028 return o->data == NULL;
1029}
1030
1031static int b_addchr(o_string *o, int ch)
1032{
Denis Vlasenko831dcc42007-05-16 15:05:36 +00001033 debug_printf("b_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001034 if (b_check_space(o, 1))
1035 return B_NOSPAC;
Eric Andersen25f27032001-04-26 23:22:31 +00001036 o->data[o->length] = ch;
1037 o->length++;
1038 o->data[o->length] = '\0';
1039 return 0;
1040}
1041
1042static void b_reset(o_string *o)
1043{
1044 o->length = 0;
1045 o->nonnull = 0;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001046 if (o->data != NULL)
1047 *o->data = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00001048}
1049
1050static void b_free(o_string *o)
1051{
1052 b_reset(o);
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001053 free(o->data);
Eric Andersen25f27032001-04-26 23:22:31 +00001054 o->data = NULL;
1055 o->maxlen = 0;
1056}
1057
1058/* My analysis of quoting semantics tells me that state information
1059 * is associated with a destination, not a source.
1060 */
1061static int b_addqchr(o_string *o, int ch, int quote)
1062{
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00001063 if (quote && strchr("*?[\\", ch)) {
Eric Andersen25f27032001-04-26 23:22:31 +00001064 int rc;
1065 rc = b_addchr(o, '\\');
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001066 if (rc)
1067 return rc;
Eric Andersen25f27032001-04-26 23:22:31 +00001068 }
1069 return b_addchr(o, ch);
1070}
1071
Eric Andersen25f27032001-04-26 23:22:31 +00001072static int static_get(struct in_str *i)
1073{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001074 int ch = *i->p++;
1075 if (ch == '\0') return EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001076 return ch;
1077}
1078
1079static int static_peek(struct in_str *i)
1080{
1081 return *i->p;
1082}
1083
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001084#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001085#if ENABLE_FEATURE_EDITING
Rob Landley88621d72006-08-29 19:41:06 +00001086static void cmdedit_set_initial_prompt(void)
Eric Andersen25f27032001-04-26 23:22:31 +00001087{
Denis Vlasenko38f63192007-01-22 09:03:07 +00001088#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001089 PS1 = NULL;
1090#else
1091 PS1 = getenv("PS1");
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001092 if (PS1 == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +00001093 PS1 = "\\w \\$ ";
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001094#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001095}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001096#endif /* EDITING */
Eric Andersen25f27032001-04-26 23:22:31 +00001097
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001098static const char* setup_prompt_string(int promptmode)
Eric Andersen25f27032001-04-26 23:22:31 +00001099{
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001100 const char *prompt_str;
1101 debug_printf("setup_prompt_string %d ", promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001102#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001103 /* Set up the prompt */
1104 if (promptmode == 1) {
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001105 char *ns;
1106 free((char*)PS1);
1107 ns = xmalloc(strlen(cwd)+4);
1108 sprintf(ns, "%s %s", cwd, (geteuid() != 0) ? "$ " : "# ");
1109 prompt_str = ns;
1110 PS1 = ns;
Eric Andersen25f27032001-04-26 23:22:31 +00001111 } else {
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001112 prompt_str = PS2;
Eric Andersen25f27032001-04-26 23:22:31 +00001113 }
1114#else
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001115 prompt_str = (promptmode == 1) ? PS1 : PS2;
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001116#endif
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001117 debug_printf("result %s\n", prompt_str);
1118 return prompt_str;
Eric Andersen25f27032001-04-26 23:22:31 +00001119}
1120
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001121static void get_user_input(struct in_str *i)
Eric Andersen25f27032001-04-26 23:22:31 +00001122{
Denis Vlasenko0937be52007-04-28 16:47:08 +00001123 int r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001124 const char *prompt_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001125
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001126 prompt_str = setup_prompt_string(i->promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001127#if ENABLE_FEATURE_EDITING
Eric Andersen25f27032001-04-26 23:22:31 +00001128 /*
1129 ** enable command line editing only while a command line
1130 ** is actually being read; otherwise, we'll end up bequeathing
1131 ** atexit() handlers and other unwanted stuff to our
1132 ** child processes (rob@sysgo.de)
1133 */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001134 r = read_line_input(prompt_str, user_input_buf, BUFSIZ-1, line_input_state);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001135 i->eof_flag = (r < 0);
1136 if (i->eof_flag) { /* EOF/error detected */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001137 user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */
1138 user_input_buf[1] = '\0';
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001139 }
Eric Andersen25f27032001-04-26 23:22:31 +00001140#else
1141 fputs(prompt_str, stdout);
1142 fflush(stdout);
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001143 user_input_buf[0] = r = fgetc(i->file);
1144 /*user_input_buf[1] = '\0'; - already is and never changed */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001145 i->eof_flag = (r == EOF);
Eric Andersen25f27032001-04-26 23:22:31 +00001146#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001147 i->p = user_input_buf;
Eric Andersen25f27032001-04-26 23:22:31 +00001148}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001149#endif /* INTERACTIVE */
Eric Andersen25f27032001-04-26 23:22:31 +00001150
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001151/* This is the magic location that prints prompts
Eric Andersen25f27032001-04-26 23:22:31 +00001152 * and gets data back from the user */
1153static int file_get(struct in_str *i)
1154{
1155 int ch;
1156
Eric Andersen25f27032001-04-26 23:22:31 +00001157 /* If there is data waiting, eat it up */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001158 if (i->p && *i->p) {
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001159#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001160 take_cached:
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001161#endif
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001162 ch = *i->p++;
1163 if (i->eof_flag && !*i->p)
1164 ch = EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001165 } else {
1166 /* need to double check i->file because we might be doing something
1167 * more complicated by now, like sourcing or substituting. */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001168#if ENABLE_HUSH_INTERACTIVE
1169 if (interactive_fd && i->__promptme && i->file == stdin) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001170 do {
1171 get_user_input(i);
1172 } while (!*i->p); /* need non-empty line */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001173 i->promptmode = 2;
Eric Andersene67c3ce2001-05-02 02:09:36 +00001174 i->__promptme = 0;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001175 goto take_cached;
Eric Andersen25f27032001-04-26 23:22:31 +00001176 }
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001177#endif
1178 ch = fgetc(i->file);
Eric Andersen25f27032001-04-26 23:22:31 +00001179 }
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001180 debug_printf("file_get: got a '%c' %d\n", ch, ch);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001181#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001182 if (ch == '\n')
1183 i->__promptme = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001184#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001185 return ch;
1186}
1187
1188/* All the callers guarantee this routine will never be
1189 * used right after a newline, so prompting is not needed.
1190 */
1191static int file_peek(struct in_str *i)
1192{
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001193 int ch;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001194 if (i->p && *i->p) {
1195 if (i->eof_flag && !i->p[1])
1196 return EOF;
1197 return *i->p;
Eric Andersen25f27032001-04-26 23:22:31 +00001198 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001199 ch = fgetc(i->file);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001200 i->eof_flag = (ch == EOF);
1201 i->peek_buf[0] = ch;
1202 i->peek_buf[1] = '\0';
1203 i->p = i->peek_buf;
1204 debug_printf("file_peek: got a '%c' %d\n", *i->p, *i->p);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001205 return ch;
Eric Andersen25f27032001-04-26 23:22:31 +00001206}
1207
1208static void setup_file_in_str(struct in_str *i, FILE *f)
1209{
1210 i->peek = file_peek;
1211 i->get = file_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001212#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001213 i->__promptme = 1;
1214 i->promptmode = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001215#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001216 i->file = f;
1217 i->p = NULL;
1218}
1219
1220static void setup_string_in_str(struct in_str *i, const char *s)
1221{
1222 i->peek = static_peek;
1223 i->get = static_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001224#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkobb81c582007-01-30 22:32:09 +00001225 i->__promptme = 1;
1226 i->promptmode = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001227#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001228 i->p = s;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001229 i->eof_flag = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001230}
1231
1232static void mark_open(int fd)
1233{
1234 struct close_me *new = xmalloc(sizeof(struct close_me));
1235 new->fd = fd;
1236 new->next = close_me_head;
1237 close_me_head = new;
1238}
1239
1240static void mark_closed(int fd)
1241{
1242 struct close_me *tmp;
1243 if (close_me_head == NULL || close_me_head->fd != fd)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001244 bb_error_msg_and_die("corrupt close_me");
Eric Andersen25f27032001-04-26 23:22:31 +00001245 tmp = close_me_head;
1246 close_me_head = close_me_head->next;
1247 free(tmp);
1248}
1249
Eric Anderseneaecbf32001-10-31 10:41:31 +00001250static void close_all(void)
Eric Andersen25f27032001-04-26 23:22:31 +00001251{
1252 struct close_me *c;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001253 for (c = close_me_head; c; c = c->next) {
Eric Andersen25f27032001-04-26 23:22:31 +00001254 close(c->fd);
1255 }
1256 close_me_head = NULL;
1257}
1258
1259/* squirrel != NULL means we squirrel away copies of stdin, stdout,
1260 * and stderr if they are redirected. */
1261static int setup_redirects(struct child_prog *prog, int squirrel[])
1262{
1263 int openfd, mode;
1264 struct redir_struct *redir;
1265
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001266 for (redir = prog->redirects; redir; redir = redir->next) {
Eric Andersen817e73c2001-06-06 17:56:09 +00001267 if (redir->dup == -1 && redir->word.gl_pathv == NULL) {
1268 /* something went wrong in the parse. Pretend it didn't happen */
1269 continue;
1270 }
Eric Andersen25f27032001-04-26 23:22:31 +00001271 if (redir->dup == -1) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001272 mode = redir_table[redir->type].mode;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001273 openfd = open_or_warn(redir->word.gl_pathv[0], mode);
Eric Andersen25f27032001-04-26 23:22:31 +00001274 if (openfd < 0) {
1275 /* this could get lost if stderr has been redirected, but
1276 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersen25f27032001-04-26 23:22:31 +00001277 return 1;
1278 }
1279 } else {
1280 openfd = redir->dup;
1281 }
1282
1283 if (openfd != redir->fd) {
1284 if (squirrel && redir->fd < 3) {
1285 squirrel[redir->fd] = dup(redir->fd);
1286 }
Eric Andersen83a2ae22001-05-07 17:59:25 +00001287 if (openfd == -3) {
1288 close(openfd);
1289 } else {
1290 dup2(openfd, redir->fd);
Matt Kraaic616e532001-06-05 16:50:08 +00001291 if (redir->dup == -1)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001292 close(openfd);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001293 }
Eric Andersen25f27032001-04-26 23:22:31 +00001294 }
1295 }
1296 return 0;
1297}
1298
1299static void restore_redirects(int squirrel[])
1300{
1301 int i, fd;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001302 for (i = 0; i < 3; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001303 fd = squirrel[i];
1304 if (fd != -1) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00001305 /* We simply die on error */
1306 xmove_fd(fd, i);
Eric Andersen25f27032001-04-26 23:22:31 +00001307 }
1308 }
1309}
1310
Eric Andersenada18ff2001-05-21 16:18:22 +00001311/* never returns */
Eric Andersen94ac2442001-05-22 19:05:18 +00001312/* XXX no exit() here. If you don't exec, use _exit instead.
1313 * The at_exit handlers apparently confuse the calling process,
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001314 * in particular stdin handling. Not sure why? -- because of vfork! (vda) */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001315static void pseudo_exec_argv(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001316{
Eric Andersen78a7c992001-05-15 16:30:25 +00001317 int i, rcode;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001318 char *p;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001319 const struct built_in_command *x;
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001320
Denis Vlasenko1359da62007-04-21 23:27:30 +00001321 for (i = 0; is_assignment(argv[i]); i++) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001322 debug_printf_exec("pid %d environment modification: %s\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001323 getpid(), argv[i]);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001324// FIXME: vfork case??
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00001325 p = expand_variables_to_string(argv[i]);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00001326 putenv(p == argv[i] ? xstrdup(p) : p);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001327 }
1328 argv += i;
1329 /* If a variable is assigned in a forest, and nobody listens,
1330 * was it ever really set?
1331 */
1332 if (argv[0] == NULL) {
1333 _exit(EXIT_SUCCESS);
1334 }
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001335
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00001336 argv = expand_variables_to_list(argv);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001337
Denis Vlasenko1359da62007-04-21 23:27:30 +00001338 /*
1339 * Check if the command matches any of the builtins.
1340 * Depending on context, this might be redundant. But it's
1341 * easier to waste a few CPU cycles than it is to figure out
1342 * if this is one of those cases.
1343 */
1344 for (x = bltins; x->cmd; x++) {
1345 if (strcmp(argv[0], x->cmd) == 0) {
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001346 debug_printf_exec("running builtin '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001347 rcode = x->function(argv);
1348 fflush(stdout);
1349 _exit(rcode);
Eric Andersen94ac2442001-05-22 19:05:18 +00001350 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001351 }
Eric Andersen78a7c992001-05-15 16:30:25 +00001352
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001353 /* Check if the command matches any busybox applets */
Denis Vlasenko80d14be2007-04-10 23:03:30 +00001354#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001355 if (strchr(argv[0], '/') == NULL) {
1356 const struct bb_applet *a = find_applet_by_name(argv[0]);
1357 if (a) {
1358 if (a->noexec) {
1359 current_applet = a;
1360 debug_printf_exec("running applet '%s'\n", argv[0]);
1361// is it ok that run_current_applet_and_exit() does exit(), not _exit()?
1362 run_current_applet_and_exit(argv);
1363 }
1364 /* re-exec ourselves with the new arguments */
1365 debug_printf_exec("re-execing applet '%s'\n", argv[0]);
1366 execvp(CONFIG_BUSYBOX_EXEC_PATH, argv);
1367 /* If they called chroot or otherwise made the binary no longer
1368 * executable, fall through */
1369 }
1370 }
Eric Andersenaac75e52001-04-30 18:18:45 +00001371#endif
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001372
1373 debug_printf_exec("execing '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001374 execvp(argv[0], argv);
1375 bb_perror_msg("cannot exec '%s'", argv[0]);
1376 _exit(1);
1377}
1378
1379static void pseudo_exec(struct child_prog *child)
1380{
Denis Vlasenkof2fffd02007-05-02 23:39:04 +00001381// FIXME: buggy wrt NOMMU! Must not modify any global data
1382// until it does exec/_exit, but currently it does.
Denis Vlasenko1359da62007-04-21 23:27:30 +00001383 int rcode;
1384
1385 if (child->argv) {
1386 pseudo_exec_argv(child->argv);
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001387 }
1388
1389 if (child->group) {
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001390 // FIXME: do not modify globals! Think vfork!
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001391#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001392 debug_printf_exec("pseudo_exec: setting interactive_fd=0\n");
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001393 interactive_fd = 0; /* crucial!!!! */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001394#endif
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001395 debug_printf_exec("pseudo_exec: run_list_real\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001396 rcode = run_list_real(child->group);
Eric Andersenbf7df042001-05-23 22:18:35 +00001397 /* OK to leak memory by not calling free_pipe_list,
Eric Andersen25f27032001-04-26 23:22:31 +00001398 * since this process is about to exit */
Eric Andersen94ac2442001-05-22 19:05:18 +00001399 _exit(rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00001400 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001401
1402 /* Can happen. See what bash does with ">foo" by itself. */
1403 debug_printf("trying to pseudo_exec null command\n");
1404 _exit(EXIT_SUCCESS);
Eric Andersen25f27032001-04-26 23:22:31 +00001405}
1406
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001407#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001408static const char *get_cmdtext(struct pipe *pi)
1409{
1410 char **argv;
1411 char *p;
1412 int len;
1413
1414 /* This is subtle. ->cmdtext is created only on first backgrounding.
1415 * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...)
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001416 * On subsequent bg argv is trashed, but we won't use it */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001417 if (pi->cmdtext)
1418 return pi->cmdtext;
1419 argv = pi->progs[0].argv;
1420 if (!argv || !argv[0])
1421 return (pi->cmdtext = xzalloc(1));
1422
1423 len = 0;
1424 do len += strlen(*argv) + 1; while (*++argv);
1425 pi->cmdtext = p = xmalloc(len);
1426 argv = pi->progs[0].argv;
1427 do {
1428 len = strlen(*argv);
1429 memcpy(p, *argv, len);
1430 p += len;
1431 *p++ = ' ';
1432 } while (*++argv);
1433 p[-1] = '\0';
1434 return pi->cmdtext;
1435}
1436
Eric Andersenbafd94f2001-05-02 16:11:59 +00001437static void insert_bg_job(struct pipe *pi)
1438{
1439 struct pipe *thejob;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001440 int i;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001441
1442 /* Linear search for the ID of the job to use */
1443 pi->jobid = 1;
Eric Andersenc798b072001-06-22 06:23:03 +00001444 for (thejob = job_list; thejob; thejob = thejob->next)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001445 if (thejob->jobid >= pi->jobid)
1446 pi->jobid = thejob->jobid + 1;
1447
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001448 /* Add thejob to the list of running jobs */
Eric Andersenc798b072001-06-22 06:23:03 +00001449 if (!job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001450 thejob = job_list = xmalloc(sizeof(*thejob));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001451 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001452 for (thejob = job_list; thejob->next; thejob = thejob->next)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001453 continue;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001454 thejob->next = xmalloc(sizeof(*thejob));
1455 thejob = thejob->next;
1456 }
1457
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001458 /* Physically copy the struct job */
Eric Andersen0fcd4472001-05-02 20:12:03 +00001459 memcpy(thejob, pi, sizeof(struct pipe));
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001460 thejob->progs = xzalloc(sizeof(pi->progs[0]) * pi->num_progs);
1461 /* We cannot copy entire pi->progs[] vector! Double free()s will happen */
1462 for (i = 0; i < pi->num_progs; i++) {
1463// TODO: do we really need to have so many fields which are just dead weight
1464// at execution stage?
1465 thejob->progs[i].pid = pi->progs[i].pid;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001466 /* all other fields are not used and stay zero */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001467 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001468 thejob->next = NULL;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001469 thejob->cmdtext = xstrdup(get_cmdtext(pi));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001470
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001471 /* We don't wait for background thejobs to return -- append it
Eric Andersenbafd94f2001-05-02 16:11:59 +00001472 to the list of backgrounded thejobs and leave it alone */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001473 printf("[%d] %d %s\n", thejob->jobid, thejob->progs[0].pid, thejob->cmdtext);
Eric Andersen1a6d39b2001-05-08 05:11:54 +00001474 last_bg_pid = thejob->progs[0].pid;
Eric Andersenc798b072001-06-22 06:23:03 +00001475 last_jobid = thejob->jobid;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001476}
1477
Eric Andersenbafd94f2001-05-02 16:11:59 +00001478static void remove_bg_job(struct pipe *pi)
1479{
1480 struct pipe *prev_pipe;
1481
Eric Andersenc798b072001-06-22 06:23:03 +00001482 if (pi == job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001483 job_list = pi->next;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001484 } else {
Eric Andersenc798b072001-06-22 06:23:03 +00001485 prev_pipe = job_list;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001486 while (prev_pipe->next != pi)
1487 prev_pipe = prev_pipe->next;
1488 prev_pipe->next = pi->next;
1489 }
Eric Andersen028b65b2001-06-28 01:10:11 +00001490 if (job_list)
1491 last_jobid = job_list->jobid;
1492 else
1493 last_jobid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001494}
Eric Andersen028b65b2001-06-28 01:10:11 +00001495
Denis Vlasenko1359da62007-04-21 23:27:30 +00001496/* remove a backgrounded job */
1497static void delete_finished_bg_job(struct pipe *pi)
1498{
1499 remove_bg_job(pi);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001500 pi->stopped_progs = 0;
Eric Andersenbf7df042001-05-23 22:18:35 +00001501 free_pipe(pi, 0);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001502 free(pi);
1503}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001504#endif /* JOB */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001505
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001506/* Checks to see if any processes have exited -- if they
Eric Andersenbafd94f2001-05-02 16:11:59 +00001507 have, figure out why and see if a job has completed */
Eric Andersenc798b072001-06-22 06:23:03 +00001508static int checkjobs(struct pipe* fg_pipe)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001509{
Eric Andersenc798b072001-06-22 06:23:03 +00001510 int attributes;
1511 int status;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001512#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00001513 int prognum = 0;
1514 struct pipe *pi;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001515#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001516 pid_t childpid;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001517 int rcode = 0;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001518
Eric Andersenc798b072001-06-22 06:23:03 +00001519 attributes = WUNTRACED;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001520 if (fg_pipe == NULL) {
Eric Andersenc798b072001-06-22 06:23:03 +00001521 attributes |= WNOHANG;
1522 }
1523
Denis Vlasenko1359da62007-04-21 23:27:30 +00001524/* Do we do this right?
1525 * bash-3.00# sleep 20 | false
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001526 * <ctrl-Z pressed>
Denis Vlasenko1359da62007-04-21 23:27:30 +00001527 * [3]+ Stopped sleep 20 | false
1528 * bash-3.00# echo $?
1529 * 1 <========== bg pipe is not fully done, but exitcode is already known!
1530 */
1531
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001532//FIXME: non-interactive bash does not continue even if all processes in fg pipe
1533//are stopped. Testcase: "cat | cat" in a script (not on command line)
1534// + killall -STOP cat
1535
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001536 wait_more:
Eric Andersenc798b072001-06-22 06:23:03 +00001537 while ((childpid = waitpid(-1, &status, attributes)) > 0) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001538 const int dead = WIFEXITED(status) || WIFSIGNALED(status);
1539
1540#ifdef DEBUG_SHELL_JOBS
1541 if (WIFSTOPPED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001542 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001543 childpid, WSTOPSIG(status), WEXITSTATUS(status));
1544 if (WIFSIGNALED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001545 debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001546 childpid, WTERMSIG(status), WEXITSTATUS(status));
1547 if (WIFEXITED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001548 debug_printf_jobs("pid %d exited, exitcode %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001549 childpid, WEXITSTATUS(status));
1550#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001551 /* Were we asked to wait for fg pipe? */
Eric Andersenc798b072001-06-22 06:23:03 +00001552 if (fg_pipe) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001553 int i;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001554 for (i = 0; i < fg_pipe->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001555 debug_printf_jobs("check pid %d\n", fg_pipe->progs[i].pid);
Eric Andersenc798b072001-06-22 06:23:03 +00001556 if (fg_pipe->progs[i].pid == childpid) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001557 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001558 if (dead) {
1559 fg_pipe->progs[i].pid = 0;
1560 fg_pipe->running_progs--;
1561 if (i == fg_pipe->num_progs-1)
1562 /* last process gives overall exitstatus */
1563 rcode = WEXITSTATUS(status);
1564 } else {
1565 fg_pipe->progs[i].is_stopped = 1;
1566 fg_pipe->stopped_progs++;
1567 }
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001568 debug_printf_jobs("fg_pipe: running_progs %d stopped_progs %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001569 fg_pipe->running_progs, fg_pipe->stopped_progs);
1570 if (fg_pipe->running_progs - fg_pipe->stopped_progs <= 0) {
1571 /* All processes in fg pipe have exited/stopped */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001572#if ENABLE_HUSH_JOB
Denis Vlasenko1359da62007-04-21 23:27:30 +00001573 if (fg_pipe->running_progs)
1574 insert_bg_job(fg_pipe);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001575#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001576 return rcode;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001577 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001578 /* There are still running processes in the fg pipe */
1579 goto wait_more;
Eric Andersenc798b072001-06-22 06:23:03 +00001580 }
1581 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001582 /* fall through to searching process in bg pipes */
Eric Andersenc798b072001-06-22 06:23:03 +00001583 }
1584
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001585#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001586 /* We asked to wait for bg or orphaned children */
1587 /* No need to remember exitcode in this case */
Eric Andersenc798b072001-06-22 06:23:03 +00001588 for (pi = job_list; pi; pi = pi->next) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001589 prognum = 0;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001590 while (prognum < pi->num_progs) {
1591 if (pi->progs[prognum].pid == childpid)
1592 goto found_pi_and_prognum;
Eric Andersen52a97ca2001-06-22 06:49:26 +00001593 prognum++;
1594 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001595 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001596#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001597
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001598 /* Happens when shell is used as init process (init=/bin/sh) */
1599 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001600 goto wait_more;
Eric Andersenaeb44c42001-05-22 20:29:00 +00001601
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001602#if ENABLE_HUSH_JOB
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001603 found_pi_and_prognum:
Denis Vlasenko1359da62007-04-21 23:27:30 +00001604 if (dead) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001605 /* child exited */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001606 pi->progs[prognum].pid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001607 pi->running_progs--;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001608 if (!pi->running_progs) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001609 printf(JOB_STATUS_FORMAT, pi->jobid,
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001610 "Done", pi->cmdtext);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001611 delete_finished_bg_job(pi);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001612 }
1613 } else {
1614 /* child stopped */
1615 pi->stopped_progs++;
1616 pi->progs[prognum].is_stopped = 1;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001617 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001618#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001619 }
1620
Denis Vlasenko1359da62007-04-21 23:27:30 +00001621 /* wait found no children or failed */
1622
1623 if (childpid && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001624 bb_perror_msg("waitpid");
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001625 return rcode;
Eric Andersenada18ff2001-05-21 16:18:22 +00001626}
1627
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001628#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +00001629static int checkjobs_and_fg_shell(struct pipe* fg_pipe)
1630{
1631 pid_t p;
1632 int rcode = checkjobs(fg_pipe);
1633 /* Job finished, move the shell to the foreground */
1634 p = getpgid(0);
1635 debug_printf("fg'ing ourself: getpgid(0)=%d\n", (int)p);
1636 if (tcsetpgrp(interactive_fd, p) && errno != ENOTTY)
1637 bb_perror_msg("tcsetpgrp-4a");
1638 return rcode;
1639}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001640#endif
Denis Vlasenko52881e92007-04-21 13:42:52 +00001641
Eric Andersen25f27032001-04-26 23:22:31 +00001642/* run_pipe_real() starts all the jobs, but doesn't wait for anything
Eric Andersenc798b072001-06-22 06:23:03 +00001643 * to finish. See checkjobs().
Eric Andersen25f27032001-04-26 23:22:31 +00001644 *
1645 * return code is normally -1, when the caller has to wait for children
1646 * to finish to determine the exit status of the pipe. If the pipe
1647 * is a simple builtin command, however, the action is done by the
1648 * time run_pipe_real returns, and the exit code is provided as the
1649 * return value.
1650 *
1651 * The input of the pipe is always stdin, the output is always
1652 * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
1653 * because it tries to avoid running the command substitution in
1654 * subshell, when that is in fact necessary. The subshell process
1655 * now has its stdout directed to the input of the appropriate pipe,
1656 * so this routine is noticeably simpler.
1657 */
1658static int run_pipe_real(struct pipe *pi)
1659{
1660 int i;
1661 int nextin, nextout;
1662 int pipefds[2]; /* pipefds[0] is for reading */
Rob Landley53702e52006-07-19 21:43:53 +00001663 struct child_prog *child;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001664 const struct built_in_command *x;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001665 char *p;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001666 /* it is not always needed, but we aim to smaller code */
1667 int squirrel[] = { -1, -1, -1 };
1668 int rcode;
Denis Vlasenko52881e92007-04-21 13:42:52 +00001669 const int single_fg = (pi->num_progs == 1 && pi->followup != PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00001670
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001671 debug_printf_exec("run_pipe_real start: single_fg=%d\n", single_fg);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001672
Eric Andersen25f27032001-04-26 23:22:31 +00001673 nextin = 0;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001674#if ENABLE_HUSH_JOB
Eric Andersenada18ff2001-05-21 16:18:22 +00001675 pi->pgrp = -1;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001676#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001677 pi->running_progs = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001678 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001679
1680 /* Check if this is a simple builtin (not part of a pipe).
1681 * Builtins within pipes have to fork anyway, and are handled in
1682 * pseudo_exec. "echo foo | read bar" doesn't work on bash, either.
1683 */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001684 child = &(pi->progs[0]);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001685 if (single_fg && child->group && child->subshell == 0) {
Eric Andersen04407e52001-06-07 16:42:05 +00001686 debug_printf("non-subshell grouping\n");
1687 setup_redirects(child, squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001688 debug_printf_exec(": run_list_real\n");
Eric Andersen04407e52001-06-07 16:42:05 +00001689 rcode = run_list_real(child->group);
1690 restore_redirects(squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001691 debug_printf_exec("run_pipe_real return %d\n", rcode);
Eric Andersen04407e52001-06-07 16:42:05 +00001692 return rcode;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001693 }
1694
Denis Vlasenko1359da62007-04-21 23:27:30 +00001695 if (single_fg && child->argv != NULL) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001696 char **argv_expanded;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001697 char **argv = child->argv;
1698
1699 for (i = 0; is_assignment(argv[i]); i++)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001700 continue;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001701 if (i != 0 && argv[i] == NULL) {
Eric Andersen78a7c992001-05-15 16:30:25 +00001702 /* assignments, but no command: set the local environment */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001703 for (i = 0; argv[i] != NULL; i++) {
Eric Andersen99785762001-05-22 21:37:48 +00001704 /* Ok, this case is tricky. We have to decide if this is a
1705 * local variable, or an already exported variable. If it is
1706 * already exported, we have to export the new value. If it is
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001707 * not exported, we need only set this as a local variable.
Eric Andersen99785762001-05-22 21:37:48 +00001708 * This junk is all to decide whether or not to export this
1709 * variable. */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001710 int export_me = 0;
Eric Andersen99785762001-05-22 21:37:48 +00001711 char *name, *value;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001712 name = xstrdup(argv[i]);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001713 debug_printf("local environment set: %s\n", name);
Eric Andersen99785762001-05-22 21:37:48 +00001714 value = strchr(name, '=');
1715 if (value)
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001716 *value = '\0';
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001717 if (get_local_var(name)) {
1718 export_me = 1;
Eric Andersen99785762001-05-22 21:37:48 +00001719 }
1720 free(name);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00001721 p = expand_variables_to_string(argv[i]);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001722 set_local_var(p, export_me);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001723 if (p != argv[i])
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001724 free(p);
Eric Andersen78a7c992001-05-15 16:30:25 +00001725 }
1726 return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */
1727 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001728 for (i = 0; is_assignment(argv[i]); i++) {
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00001729 p = expand_variables_to_string(argv[i]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001730 if (p != argv[i]) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001731 //sp: child->sp--;
Denis Vlasenko602d13c2007-05-13 18:34:53 +00001732 putenv(p);
1733 } else {
1734 putenv(xstrdup(p));
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001735 }
1736 }
Eric Andersen25f27032001-04-26 23:22:31 +00001737 for (x = bltins; x->cmd; x++) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001738 if (strcmp(argv[i], x->cmd) == 0) {
1739 if (x->function == builtin_exec && argv[i+1] == NULL) {
Eric Andersen83a2ae22001-05-07 17:59:25 +00001740 debug_printf("magic exec\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001741 setup_redirects(child, NULL);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001742 return EXIT_SUCCESS;
1743 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001744 debug_printf("builtin inline %s\n", argv[0]);
Eric Andersen25f27032001-04-26 23:22:31 +00001745 /* XXX setup_redirects acts on file descriptors, not FILEs.
1746 * This is perfect for work that comes after exec().
1747 * Is it really safe for inline use? Experimentally,
1748 * things seem to work with glibc. */
1749 setup_redirects(child, squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001750 debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv[i+1]);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001751 //sp: if (child->sp) /* btw we can do it unconditionally... */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00001752 argv_expanded = expand_variables_to_list(argv + i);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001753 rcode = x->function(argv_expanded);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001754 free(argv_expanded);
Eric Andersen25f27032001-04-26 23:22:31 +00001755 restore_redirects(squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001756 debug_printf_exec("run_pipe_real return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00001757 return rcode;
1758 }
1759 }
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001760#if ENABLE_FEATURE_SH_STANDALONE
1761 {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001762 const struct bb_applet *a = find_applet_by_name(argv[i]);
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001763 if (a && a->nofork) {
1764 setup_redirects(child, squirrel);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001765 save_nofork_data(&nofork_save);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001766 argv_expanded = argv + i;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001767 //sp: if (child->sp)
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00001768 argv_expanded = expand_variables_to_list(argv + i);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001769 debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]);
1770 rcode = run_nofork_applet_prime(&nofork_save, a, argv_expanded);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001771 free(argv_expanded);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001772 restore_redirects(squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001773 debug_printf_exec("run_pipe_real return %d\n", rcode);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001774 return rcode;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001775 }
1776 }
1777#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001778 }
1779
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001780 /* Going to fork a child per each pipe member */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001781 pi->running_progs = 0;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001782
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001783 /* Disable job control signals for shell (parent) and
1784 * for initial child code after fork */
1785 set_jobctrl_sighandler(SIG_IGN);
1786
Eric Andersen25f27032001-04-26 23:22:31 +00001787 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001788 child = &(pi->progs[i]);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001789 if (child->argv)
1790 debug_printf_exec(": pipe member '%s' '%s'...\n", child->argv[0], child->argv[1]);
1791 else
1792 debug_printf_exec(": pipe member with no argv\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001793
1794 /* pipes are inserted between pairs of commands */
1795 if ((i + 1) < pi->num_progs) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001796 if (pipe(pipefds) < 0)
1797 bb_perror_msg_and_die("pipe");
Eric Andersen25f27032001-04-26 23:22:31 +00001798 nextout = pipefds[1];
1799 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001800 nextout = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00001801 pipefds[0] = -1;
1802 }
1803
1804 /* XXX test for failed fork()? */
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001805#if BB_MMU
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001806 child->pid = fork();
Eric Andersen72f9a422001-10-28 05:12:20 +00001807#else
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001808 child->pid = vfork();
Eric Andersen72f9a422001-10-28 05:12:20 +00001809#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001810 if (!child->pid) { /* child */
1811 /* Every child adds itself to new process group
1812 * with pgid == pid of first child in pipe */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001813#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001814 if (interactive_fd) {
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001815 /* Don't do pgrp restore anymore on fatal signals */
1816 set_fatal_sighandler(SIG_DFL);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001817 if (pi->pgrp < 0) /* true for 1st process only */
1818 pi->pgrp = getpid();
1819 if (setpgid(0, pi->pgrp) == 0 && pi->followup != PIPE_BG) {
1820 /* We do it in *every* child, not just first,
1821 * to avoid races */
1822 tcsetpgrp(interactive_fd, pi->pgrp);
1823 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001824 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001825#endif
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001826 /* in non-interactive case fatal sigs are already SIG_DFL */
Eric Andersen25f27032001-04-26 23:22:31 +00001827 close_all();
Eric Andersen25f27032001-04-26 23:22:31 +00001828 if (nextin != 0) {
1829 dup2(nextin, 0);
1830 close(nextin);
1831 }
1832 if (nextout != 1) {
1833 dup2(nextout, 1);
1834 close(nextout);
1835 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00001836 if (pipefds[0] != -1) {
Eric Andersen25f27032001-04-26 23:22:31 +00001837 close(pipefds[0]); /* opposite end of our output pipe */
1838 }
Eric Andersen25f27032001-04-26 23:22:31 +00001839 /* Like bash, explicit redirects override pipes,
1840 * and the pipe fd is available for dup'ing. */
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001841 setup_redirects(child, NULL);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001842
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001843 /* Restore default handlers just prior to exec */
1844 set_jobctrl_sighandler(SIG_DFL);
1845 set_misc_sighandler(SIG_DFL);
1846 signal(SIGCHLD, SIG_DFL);
Eric Andersen25f27032001-04-26 23:22:31 +00001847 pseudo_exec(child);
1848 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001849
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001850 pi->running_progs++;
1851
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001852#if ENABLE_HUSH_JOB
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00001853 /* Second and next children need to know pid of first one */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001854 if (pi->pgrp < 0)
Eric Andersen0fcd4472001-05-02 20:12:03 +00001855 pi->pgrp = child->pid;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001856#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001857 if (nextin != 0)
1858 close(nextin);
1859 if (nextout != 1)
1860 close(nextout);
1861
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001862 /* If there isn't another process, nextin is garbage
Eric Andersen25f27032001-04-26 23:22:31 +00001863 but it doesn't matter */
1864 nextin = pipefds[0];
1865 }
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001866 debug_printf_exec("run_pipe_real return -1\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001867 return -1;
1868}
1869
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001870#ifndef debug_print_tree
1871static void debug_print_tree(struct pipe *pi, int lvl)
1872{
1873 static const char *PIPE[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001874 [PIPE_SEQ] = "SEQ",
1875 [PIPE_AND] = "AND",
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001876 [PIPE_OR ] = "OR" ,
1877 [PIPE_BG ] = "BG" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001878 };
1879 static const char *RES[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001880 [RES_NONE ] = "NONE" ,
1881 [RES_IF ] = "IF" ,
1882 [RES_THEN ] = "THEN" ,
1883 [RES_ELIF ] = "ELIF" ,
1884 [RES_ELSE ] = "ELSE" ,
1885 [RES_FI ] = "FI" ,
1886 [RES_FOR ] = "FOR" ,
1887 [RES_WHILE] = "WHILE",
1888 [RES_UNTIL] = "UNTIL",
1889 [RES_DO ] = "DO" ,
1890 [RES_DONE ] = "DONE" ,
1891 [RES_XXXX ] = "XXXX" ,
1892 [RES_IN ] = "IN" ,
1893 [RES_SNTX ] = "SNTX" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001894 };
1895
1896 int pin, prn;
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001897 pin = 0;
1898 while (pi) {
1899 fprintf(stderr, "%*spipe %d r_mode=%s followup=%d %s\n", lvl*2, "",
1900 pin, RES[pi->r_mode], pi->followup, PIPE[pi->followup]);
1901 prn = 0;
1902 while (prn < pi->num_progs) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00001903 struct child_prog *child = &pi->progs[prn];
1904 char **argv = child->argv;
1905
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001906 fprintf(stderr, "%*s prog %d", lvl*2, "", prn);
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00001907 if (child->group) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001908 fprintf(stderr, " group %s: (argv=%p)\n",
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00001909 (child->subshell ? "()" : "{}"),
1910 argv);
1911 debug_print_tree(child->group, lvl+1);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001912 prn++;
1913 continue;
1914 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001915 if (argv) while (*argv) {
1916 fprintf(stderr, " '%s'", *argv);
1917 argv++;
1918 }
1919 fprintf(stderr, "\n");
1920 prn++;
1921 }
1922 pi = pi->next;
1923 pin++;
1924 }
1925}
1926#endif
1927
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001928/* NB: called by pseudo_exec, and therefore must not modify any
1929 * global data until exec/_exit (we can be a child after vfork!) */
Eric Andersen25f27032001-04-26 23:22:31 +00001930static int run_list_real(struct pipe *pi)
1931{
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00001932 char *for_varname = NULL;
1933 char **for_lcur = NULL;
1934 char **for_list = NULL;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001935 struct pipe *rpipe;
1936 int flag_rep = 0;
1937 int save_num_progs;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001938 int flag_skip = 1;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00001939 int rcode = 0; /* probably for gcc only */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001940 int flag_restore = 0;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001941 int if_code = 0, next_if_code = 0; /* need double-buffer to handle elif */
1942 reserved_style rmode, skip_more_in_this_rmode = RES_XXXX;
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001943
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001944 debug_printf_exec("run_list_real start lvl %d\n", run_list_level + 1);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001945
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001946 /* check syntax for "for" */
1947 for (rpipe = pi; rpipe; rpipe = rpipe->next) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001948 if ((rpipe->r_mode == RES_IN || rpipe->r_mode == RES_FOR)
1949 && (rpipe->next == NULL)
1950 ) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001951 syntax(); /* unterminated FOR (no IN or no commands after IN) */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001952 debug_printf_exec("run_list_real lvl %d return 1\n", run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001953 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001954 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001955 if ((rpipe->r_mode == RES_IN && rpipe->next->r_mode == RES_IN && rpipe->next->progs[0].argv != NULL)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001956 || (rpipe->r_mode == RES_FOR && rpipe->next->r_mode != RES_IN)
1957 ) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001958 /* TODO: what is tested in the first condition? */
1959 syntax(); /* 2nd: malformed FOR (not followed by IN) */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001960 debug_printf_exec("run_list_real lvl %d return 1\n", run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001961 return 1;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001962 }
1963 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001964
1965#if ENABLE_HUSH_JOB
1966 /* Example of nested list: "while true; do { sleep 1 | exit 2; } done".
1967 * We are saving state before entering outermost list ("while...done")
1968 * so that ctrl-Z will correctly background _entire_ outermost list,
1969 * not just a part of it (like "sleep 1 | exit 2") */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001970 if (++run_list_level == 1 && interactive_fd) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001971 if (sigsetjmp(toplevel_jb, 1)) {
1972 /* ctrl-Z forked and we are parent; or ctrl-C.
1973 * Sighandler has longjmped us here */
1974 signal(SIGINT, SIG_IGN);
1975 signal(SIGTSTP, SIG_IGN);
1976 /* Restore level (we can be coming from deep inside
1977 * nested levels) */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001978 run_list_level = 1;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001979#if ENABLE_FEATURE_SH_STANDALONE
1980 if (nofork_save.saved) { /* if save area is valid */
1981 debug_printf_jobs("exiting nofork early\n");
1982 restore_nofork_data(&nofork_save);
1983 }
1984#endif
1985 if (ctrl_z_flag) {
1986 /* ctrl-Z has forked and stored pid of the child in pi->pid.
1987 * Remember this child as background job */
1988 insert_bg_job(pi);
1989 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001990 /* ctrl-C. We just stop doing whatever we were doing */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001991 putchar('\n');
1992 }
1993 rcode = 0;
1994 goto ret;
1995 }
1996 /* ctrl-Z handler will store pid etc in pi */
1997 toplevel_list = pi;
1998 ctrl_z_flag = 0;
1999#if ENABLE_FEATURE_SH_STANDALONE
2000 nofork_save.saved = 0; /* in case we will run a nofork later */
2001#endif
2002 signal_SA_RESTART(SIGTSTP, handler_ctrl_z);
2003 signal(SIGINT, handler_ctrl_c);
2004 }
2005#endif
2006
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002007 for (; pi; pi = flag_restore ? rpipe : pi->next) {
2008 rmode = pi->r_mode;
2009 if (rmode == RES_WHILE || rmode == RES_UNTIL || rmode == RES_FOR) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002010 flag_restore = 0;
2011 if (!rpipe) {
2012 flag_rep = 0;
2013 rpipe = pi;
2014 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002015 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002016 debug_printf_exec(": rmode=%d if_code=%d next_if_code=%d skip_more=%d\n",
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002017 rmode, if_code, next_if_code, skip_more_in_this_rmode);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002018 if (rmode == skip_more_in_this_rmode && flag_skip) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002019 if (pi->followup == PIPE_SEQ)
2020 flag_skip = 0;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002021 continue;
2022 }
2023 flag_skip = 1;
Eric Andersen4ed5e372001-05-01 01:49:50 +00002024 skip_more_in_this_rmode = RES_XXXX;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002025 if (rmode == RES_THEN || rmode == RES_ELSE)
2026 if_code = next_if_code;
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00002027 if (rmode == RES_THEN && if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002028 continue;
2029 if (rmode == RES_ELSE && !if_code)
2030 continue;
2031 if (rmode == RES_ELIF && !if_code)
2032 break;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002033 if (rmode == RES_FOR && pi->num_progs) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002034 if (!for_lcur) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002035 /* if no variable values after "in" we skip "for" */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002036 if (!pi->next->progs->argv)
2037 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002038 /* create list of variable values */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002039 for_list = expand_variables_to_list(pi->next->progs->argv);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002040 for_lcur = for_list;
2041 for_varname = pi->progs->argv[0];
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002042 pi->progs->argv[0] = NULL;
2043 flag_rep = 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002044 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002045 free(pi->progs->argv[0]);
2046 if (!*for_lcur) {
2047 free(for_list);
2048 for_lcur = NULL;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002049 flag_rep = 0;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002050 pi->progs->argv[0] = for_varname;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002051 pi->progs->glob_result.gl_pathv[0] = pi->progs->argv[0];
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002052 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002053 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002054 /* insert next value from for_lcur */
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002055 /* vda: does it need escaping? */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002056 pi->progs->argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002057 pi->progs->glob_result.gl_pathv[0] = pi->progs->argv[0];
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002058 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002059 if (rmode == RES_IN)
2060 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002061 if (rmode == RES_DO) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002062 if (!flag_rep)
2063 continue;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002064 }
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002065 if (rmode == RES_DONE) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002066 if (flag_rep) {
2067 flag_restore = 1;
2068 } else {
2069 rpipe = NULL;
2070 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002071 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002072 if (pi->num_progs == 0)
2073 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002074 save_num_progs = pi->num_progs; /* save number of programs */
Denis Vlasenkod01ff132007-05-02 21:40:23 +00002075 debug_printf_exec(": run_pipe_real with %d members\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00002076 rcode = run_pipe_real(pi);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002077 if (rcode != -1) {
Eric Andersen25f27032001-04-26 23:22:31 +00002078 /* We only ran a builtin: rcode was set by the return value
2079 * of run_pipe_real(), and we don't need to wait for anything. */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002080 } else if (pi->followup == PIPE_BG) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002081 /* What does bash do with attempts to background builtins? */
2082
2083 /* Even bash 3.2 doesn't do that well with nested bg:
2084 * try "{ { sleep 10; echo DEEP; } & echo HERE; } &".
2085 * I'm considering NOT treating inner bgs as jobs -
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002086 * thus maybe "if (run_list_level == 1 && pi->followup == PIPE_BG)"
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002087 * above? */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002088#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00002089 insert_bg_job(pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002090#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002091 rcode = EXIT_SUCCESS;
2092 } else {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002093#if ENABLE_HUSH_JOB
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002094 /* Paranoia, just "interactive_fd" should be enough */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002095 if (run_list_level == 1 && interactive_fd) {
Denis Vlasenko52881e92007-04-21 13:42:52 +00002096 rcode = checkjobs_and_fg_shell(pi);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002097 } else
2098#endif
2099 {
Eric Andersenc798b072001-06-22 06:23:03 +00002100 rcode = checkjobs(pi);
Eric Andersen25f27032001-04-26 23:22:31 +00002101 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002102 debug_printf_exec(": checkjobs returned %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002103 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002104 debug_printf_exec(": setting last_return_code=%d\n", rcode);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002105 last_return_code = rcode;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002106 pi->num_progs = save_num_progs; /* restore number of programs */
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002107 if (rmode == RES_IF || rmode == RES_ELIF)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002108 next_if_code = rcode; /* can be overwritten a number of times */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002109 if (rmode == RES_WHILE)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002110 flag_rep = !last_return_code;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002111 if (rmode == RES_UNTIL)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002112 flag_rep = last_return_code;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002113 if ((rcode == EXIT_SUCCESS && pi->followup == PIPE_OR)
2114 || (rcode != EXIT_SUCCESS && pi->followup == PIPE_AND)
2115 ) {
2116 skip_more_in_this_rmode = rmode;
2117 }
Eric Andersen028b65b2001-06-28 01:10:11 +00002118 checkjobs(NULL);
Eric Andersen25f27032001-04-26 23:22:31 +00002119 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002120
2121#if ENABLE_HUSH_JOB
2122 if (ctrl_z_flag) {
2123 /* ctrl-Z forked somewhere in the past, we are the child,
2124 * and now we completed running the list. Exit. */
2125 exit(rcode);
2126 }
2127 ret:
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002128 run_list_level--;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002129#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002130 debug_printf_exec("run_list_real lvl %d return %d\n", run_list_level + 1, rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002131 return rcode;
2132}
2133
Eric Andersen25f27032001-04-26 23:22:31 +00002134/* return code is the exit status of the pipe */
Eric Andersenbf7df042001-05-23 22:18:35 +00002135static int free_pipe(struct pipe *pi, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002136{
2137 char **p;
2138 struct child_prog *child;
2139 struct redir_struct *r, *rnext;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002140 int a, i, ret_code = 0;
Eric Andersen52a97ca2001-06-22 06:49:26 +00002141
2142 if (pi->stopped_progs > 0)
2143 return ret_code;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002144 debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid());
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002145 for (i = 0; i < pi->num_progs; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002146 child = &pi->progs[i];
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002147 debug_printf_clean("%s command %d:\n", indenter(indent), i);
Eric Andersen25f27032001-04-26 23:22:31 +00002148 if (child->argv) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002149 for (a = 0, p = child->argv; *p; a++, p++) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002150 debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p);
Eric Andersen25f27032001-04-26 23:22:31 +00002151 }
2152 globfree(&child->glob_result);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002153 child->argv = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002154 } else if (child->group) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002155 debug_printf_clean("%s begin group (subshell:%d)\n", indenter(indent), child->subshell);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002156 ret_code = free_pipe_list(child->group, indent+3);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002157 debug_printf_clean("%s end group\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002158 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002159 debug_printf_clean("%s (nil)\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002160 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002161 for (r = child->redirects; r; r = rnext) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002162 debug_printf_clean("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->type].descrip);
Eric Andersen25f27032001-04-26 23:22:31 +00002163 if (r->dup == -1) {
Eric Andersen817e73c2001-06-06 17:56:09 +00002164 /* guard against the case >$FOO, where foo is unset or blank */
2165 if (r->word.gl_pathv) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002166 debug_printf_clean(" %s\n", *r->word.gl_pathv);
Eric Andersen817e73c2001-06-06 17:56:09 +00002167 globfree(&r->word);
2168 }
Eric Andersen25f27032001-04-26 23:22:31 +00002169 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002170 debug_printf_clean("&%d\n", r->dup);
Eric Andersen25f27032001-04-26 23:22:31 +00002171 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002172 rnext = r->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002173 free(r);
2174 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002175 child->redirects = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002176 }
2177 free(pi->progs); /* children are an array, they get freed all at once */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002178 pi->progs = NULL;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002179#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002180 free(pi->cmdtext);
2181 pi->cmdtext = NULL;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002182#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002183 return ret_code;
2184}
2185
Eric Andersenbf7df042001-05-23 22:18:35 +00002186static int free_pipe_list(struct pipe *head, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002187{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002188 int rcode = 0; /* if list has no members */
Eric Andersen25f27032001-04-26 23:22:31 +00002189 struct pipe *pi, *next;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002190
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002191 for (pi = head; pi; pi = next) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002192 debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->r_mode);
Eric Andersenbf7df042001-05-23 22:18:35 +00002193 rcode = free_pipe(pi, indent);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002194 debug_printf_clean("%s pipe followup code %d\n", indenter(indent), pi->followup);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002195 next = pi->next;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002196 /*pi->next = NULL;*/
Eric Andersen25f27032001-04-26 23:22:31 +00002197 free(pi);
2198 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002199 return rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00002200}
2201
2202/* Select which version we will use */
2203static int run_list(struct pipe *pi)
2204{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002205 int rcode = 0;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002206 debug_printf_exec("run_list entered\n");
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002207 if (fake_mode == 0) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002208 debug_printf_exec(": run_list_real with %d members\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00002209 rcode = run_list_real(pi);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002210 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002211 /* free_pipe_list has the side effect of clearing memory.
Eric Andersen25f27032001-04-26 23:22:31 +00002212 * In the long run that function can be merged with run_list_real,
2213 * but doing that now would hobble the debugging effort. */
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002214 free_pipe_list(pi, 0);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002215 debug_printf_exec("run_list return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002216 return rcode;
2217}
2218
2219/* The API for glob is arguably broken. This routine pushes a non-matching
2220 * string into the output structure, removing non-backslashed backslashes.
2221 * If someone can prove me wrong, by performing this function within the
2222 * original glob(3) api, feel free to rewrite this routine into oblivion.
2223 * Return code (0 vs. GLOB_NOSPACE) matches glob(3).
2224 * XXX broken if the last character is '\\', check that before calling.
2225 */
2226static int globhack(const char *src, int flags, glob_t *pglob)
2227{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002228 int cnt = 0, pathc;
Eric Andersen25f27032001-04-26 23:22:31 +00002229 const char *s;
2230 char *dest;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002231 for (cnt = 1, s = src; s && *s; s++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002232 if (*s == '\\') s++;
2233 cnt++;
2234 }
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002235 dest = xmalloc(cnt);
Eric Andersen25f27032001-04-26 23:22:31 +00002236 if (!(flags & GLOB_APPEND)) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002237 pglob->gl_pathv = NULL;
2238 pglob->gl_pathc = 0;
2239 pglob->gl_offs = 0;
2240 pglob->gl_offs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002241 }
2242 pathc = ++pglob->gl_pathc;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002243 pglob->gl_pathv = xrealloc(pglob->gl_pathv, (pathc+1) * sizeof(*pglob->gl_pathv));
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002244 pglob->gl_pathv[pathc-1] = dest;
2245 pglob->gl_pathv[pathc] = NULL;
2246 for (s = src; s && *s; s++, dest++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002247 if (*s == '\\') s++;
2248 *dest = *s;
2249 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002250 *dest = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00002251 return 0;
2252}
2253
2254/* XXX broken if the last character is '\\', check that before calling */
2255static int glob_needed(const char *s)
2256{
2257 for (; *s; s++) {
2258 if (*s == '\\') s++;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002259 if (strchr("*[?", *s)) return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002260 }
2261 return 0;
2262}
2263
Eric Andersen25f27032001-04-26 23:22:31 +00002264static int xglob(o_string *dest, int flags, glob_t *pglob)
2265{
2266 int gr;
2267
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002268 /* short-circuit for null word */
Eric Andersen25f27032001-04-26 23:22:31 +00002269 /* we can code this better when the debug_printf's are gone */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002270 if (dest->length == 0) {
2271 if (dest->nonnull) {
2272 /* bash man page calls this an "explicit" null */
2273 gr = globhack(dest->data, flags, pglob);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002274 debug_printf("globhack returned %d\n", gr);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002275 } else {
Eric Andersen25f27032001-04-26 23:22:31 +00002276 return 0;
2277 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002278 } else if (glob_needed(dest->data)) {
Eric Andersen25f27032001-04-26 23:22:31 +00002279 gr = glob(dest->data, flags, NULL, pglob);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002280 debug_printf("glob returned %d\n", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002281 if (gr == GLOB_NOMATCH) {
2282 /* quote removal, or more accurately, backslash removal */
2283 gr = globhack(dest->data, flags, pglob);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002284 debug_printf("globhack returned %d\n", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002285 }
2286 } else {
2287 gr = globhack(dest->data, flags, pglob);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002288 debug_printf("globhack returned %d\n", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002289 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002290 if (gr == GLOB_NOSPACE)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002291 bb_error_msg_and_die("out of memory during glob");
Eric Andersen25f27032001-04-26 23:22:31 +00002292 if (gr != 0) { /* GLOB_ABORTED ? */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002293 bb_error_msg("glob(3) error %d", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002294 }
2295 /* globprint(glob_target); */
2296 return gr;
2297}
2298
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002299/* expand_variables_to_list() takes a list of strings, expands
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002300 * all variable references within and returns a pointer to
2301 * a list of expanded strings, possibly with larger number
2302 * of strings. (Think VAR="a b"; echo $VAR).
2303 * This new list is allocated as a single malloc block.
2304 * NULL-terminated list of char* pointers is at the beginning of it,
2305 * followed by strings themself.
2306 * Caller can deallocate entire list by single free(list). */
2307
2308/* Helpers first:
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00002309 * count_XXX estimates size of the block we need. It's okay
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002310 * to over-estimate sizes a bit, if it makes code simpler */
2311static int count_ifs(const char *str)
2312{
2313 int cnt = 0;
2314 debug_printf_expand("count_ifs('%s') ifs='%s'", str, ifs);
2315 while (1) {
2316 str += strcspn(str, ifs);
2317 if (!*str) break;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002318 str++; /* str += strspn(str, ifs); */
2319 cnt++; /* cnt += strspn(str, ifs); - but this code is larger */
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002320 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002321 debug_printf_expand(" return %d\n", cnt);
2322 return cnt;
2323}
2324
2325static void count_var_expansion_space(int *countp, int *lenp, char *arg)
2326{
2327 char first_ch;
2328 int i;
2329 int len = *lenp;
2330 int count = *countp;
2331 const char *val;
2332 char *p;
2333
2334 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL))) {
2335 len += p - arg;
2336 arg = ++p;
2337 p = strchr(p, SPECIAL_VAR_SYMBOL);
2338 first_ch = arg[0];
2339
2340 switch (first_ch & 0x7f) {
2341 /* high bit in 1st_ch indicates that var is double-quoted */
2342 case '$': /* pid */
2343 case '!': /* bg pid */
2344 case '?': /* exitcode */
2345 case '#': /* argc */
2346 len += sizeof(int)*3 + 1; /* enough for int */
2347 break;
2348 case '*':
2349 case '@':
2350 for (i = 1; i < global_argc; i++) {
2351 len += strlen(global_argv[i]) + 1;
2352 count++;
2353 if (!(first_ch & 0x80))
2354 count += count_ifs(global_argv[i]);
2355 }
2356 break;
2357 default:
2358 *p = '\0';
2359 arg[0] = first_ch & 0x7f;
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002360 if (isdigit(arg[0])) {
2361 i = xatoi_u(arg);
2362 val = NULL;
2363 if (i < global_argc)
2364 val = global_argv[i];
2365 } else
2366 val = lookup_param(arg);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002367 arg[0] = first_ch;
2368 *p = SPECIAL_VAR_SYMBOL;
2369
2370 if (val) {
2371 len += strlen(val) + 1;
2372 if (!(first_ch & 0x80))
2373 count += count_ifs(val);
2374 }
2375 }
2376 arg = ++p;
2377 }
2378
2379 len += strlen(arg) + 1;
2380 count++;
2381 *lenp = len;
2382 *countp = count;
2383}
2384
2385/* Store given string, finalizing the word and starting new one whenever
2386 * we encounter ifs char(s). This is used for expanding variable values.
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002387 * End-of-string does NOT finalize word: think about 'echo -$VAR-' */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002388static int expand_on_ifs(char **list, int n, char **posp, const char *str)
2389{
2390 char *pos = *posp;
2391 while (1) {
2392 int word_len = strcspn(str, ifs);
2393 if (word_len) {
2394 memcpy(pos, str, word_len); /* store non-ifs chars */
2395 pos += word_len;
2396 str += word_len;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002397 }
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002398 if (!*str) /* EOL - do not finalize word */
2399 break;
2400 *pos++ = '\0';
2401 if (n) debug_printf_expand("expand_on_ifs finalized list[%d]=%p '%s' "
2402 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2403 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2404 list[n++] = pos;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002405 str += strspn(str, ifs); /* skip ifs chars */
2406 }
2407 *posp = pos;
2408 return n;
2409}
2410
2411/* Expand all variable references in given string, adding words to list[]
2412 * at n, n+1,... positions. Return updated n (so that list[n] is next one
2413 * to be filled). This routine is extremely tricky: has to deal with
2414 * variables/parameters with whitespace, $* and $@, and constructs like
2415 * 'echo -$*-'. If you play here, you must run testsuite afterwards! */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002416/* NB: another bug is that we cannot detect empty strings yet:
2417 * "" or $empty"" expands to zero words, has to expand to empty word */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002418static int expand_vars_to_list(char **list, int n, char **posp, char *arg, char or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002419{
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002420 /* or_mask is either 0 (normal case) or 0x80
2421 * (expansion of right-hand side of assignment == 1-element expand) */
2422
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002423 char first_ch, ored_ch;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002424 int i;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002425 const char *val;
2426 char *p;
2427 char *pos = *posp;
2428
2429 ored_ch = 0;
2430
2431 if (n) debug_printf_expand("expand_vars_to_list finalized list[%d]=%p '%s' "
2432 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2433 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2434 list[n++] = pos;
2435
2436 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL))) {
2437 memcpy(pos, arg, p - arg);
2438 pos += (p - arg);
2439 arg = ++p;
2440 p = strchr(p, SPECIAL_VAR_SYMBOL);
2441
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002442 first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002443 ored_ch |= first_ch;
2444 val = NULL;
2445 switch (first_ch & 0x7f) {
2446 /* Highest bit in first_ch indicates that var is double-quoted */
2447 case '$': /* pid */
2448 /* FIXME: (echo $$) should still print pid of main shell */
2449 val = utoa(getpid());
2450 break;
2451 case '!': /* bg pid */
2452 val = last_bg_pid ? utoa(last_bg_pid) : (char*)"";
2453 break;
2454 case '?': /* exitcode */
2455 val = utoa(last_return_code);
2456 break;
2457 case '#': /* argc */
2458 val = utoa(global_argc ? global_argc-1 : 0);
2459 break;
2460 case '*':
2461 case '@':
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002462 i = 1;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002463 if (!(first_ch & 0x80)) { /* unquoted $* or $@ */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002464 while (i < global_argc) {
2465 n = expand_on_ifs(list, n, &pos, global_argv[i]);
2466 debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, global_argc-1);
2467 if (global_argv[i++][0] && i < global_argc) {
2468 /* this argv[] is not empty and not last:
2469 * put terminating NUL, start new word */
2470 *pos++ = '\0';
2471 if (n) debug_printf_expand("expand_vars_to_list 2 finalized list[%d]=%p '%s' "
2472 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2473 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2474 list[n++] = pos;
2475 }
2476 }
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002477 } else
2478 /* If or_mask is nonzero, we handle assignment 'a=....$@.....'
2479 * and in this case should theat it like '$*' */
2480 if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002481 while (1) {
2482 strcpy(pos, global_argv[i]);
2483 pos += strlen(global_argv[i]);
2484 if (++i >= global_argc)
2485 break;
2486 *pos++ = '\0';
2487 if (n) debug_printf_expand("expand_vars_to_list 3 finalized list[%d]=%p '%s' "
2488 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2489 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2490 list[n++] = pos;
2491 }
2492 } else { /* quoted $*: add as one word */
2493 while (1) {
2494 strcpy(pos, global_argv[i]);
2495 pos += strlen(global_argv[i]);
2496 if (++i >= global_argc)
2497 break;
2498 if (ifs[0])
2499 *pos++ = ifs[0];
2500 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002501 }
2502 break;
2503 default:
2504 *p = '\0';
2505 arg[0] = first_ch & 0x7f;
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002506 if (isdigit(arg[0])) {
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002507 i = xatoi_u(arg);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002508 val = NULL;
2509 if (i < global_argc)
2510 val = global_argv[i];
2511 } else
2512 val = lookup_param(arg);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002513 arg[0] = first_ch;
2514 *p = SPECIAL_VAR_SYMBOL;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002515 if (!(first_ch & 0x80)) { /* unquoted $VAR */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002516 if (val) {
2517 n = expand_on_ifs(list, n, &pos, val);
2518 val = NULL;
2519 }
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002520 } /* else: quoted $VAR, val will be appended at pos */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002521 }
2522 if (val) {
2523 strcpy(pos, val);
2524 pos += strlen(val);
2525 }
2526 arg = ++p;
2527 }
2528 debug_printf_expand("expand_vars_to_list adding tail '%s' at %p\n", arg, pos);
2529 strcpy(pos, arg);
2530 pos += strlen(arg) + 1;
2531 if (pos == list[n-1] + 1) { /* expansion is empty */
2532 if (!(ored_ch & 0x80)) { /* all vars were not quoted... */
2533 debug_printf_expand("expand_vars_to_list list[%d] empty, going back\n", n);
2534 pos--;
2535 n--;
2536 }
2537 }
2538
2539 *posp = pos;
2540 return n;
2541}
2542
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002543static char **expand_variables(char **argv, char or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002544{
2545 int n;
2546 int count = 1;
2547 int len = 0;
2548 char *pos, **v, **list;
2549
2550 v = argv;
2551 if (!*v) debug_printf_expand("count_var_expansion_space: "
2552 "argv[0]=NULL count=%d len=%d alloc_space=%d\n",
2553 count, len, sizeof(char*) * count + len);
2554 while (*v) {
2555 count_var_expansion_space(&count, &len, *v);
2556 debug_printf_expand("count_var_expansion_space: "
2557 "'%s' count=%d len=%d alloc_space=%d\n",
2558 *v, count, len, sizeof(char*) * count + len);
2559 v++;
2560 }
2561 len += sizeof(char*) * count; /* total to alloc */
2562 list = xmalloc(len);
2563 pos = (char*)(list + count);
2564 debug_printf_expand("list=%p, list[0] should be %p\n", list, pos);
2565 n = 0;
2566 v = argv;
2567 while (*v)
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002568 n = expand_vars_to_list(list, n, &pos, *v++, or_mask);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002569
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002570 if (n) debug_printf_expand("finalized list[%d]=%p '%s' "
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002571 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2572 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002573 list[n] = NULL;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002574
2575#ifdef DEBUG_EXPAND
2576 {
2577 int m = 0;
2578 while (m <= n) {
2579 debug_printf_expand("list[%d]=%p '%s'\n", m, list[m], list[m]);
2580 m++;
2581 }
2582 debug_printf_expand("used_space=%d\n", pos - (char*)list);
2583 }
2584#endif
2585 /* To be removed / made conditional later. */
2586 if (pos - (char*)list > len)
2587 bb_error_msg_and_die("BUG in varexp");
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002588 return list;
2589}
2590
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002591static char **expand_variables_to_list(char **argv)
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002592{
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002593 return expand_variables(argv, 0);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002594}
2595
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002596static char *expand_variables_to_string(const char *str)
2597{
2598 char *argv[2], **list;
2599
2600 argv[0] = (char*)str;
2601 argv[1] = NULL;
2602 list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */
2603 /* To be removed / made conditional later. */
2604 if (!list[0] || list[1])
2605 bb_error_msg_and_die("BUG in varexp");
2606 /* actually, just move string 2*sizeof(char*) bytes back */
2607 strcpy((char*)list, list[0]);
2608 return (char*)list;
2609}
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002610
Eric Andersenf72f5622001-05-15 23:21:41 +00002611/* This is used to get/check local shell variables */
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00002612static const char *get_local_var(const char *s)
Eric Andersenf72f5622001-05-15 23:21:41 +00002613{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002614 struct variables *cur;
Eric Andersenf72f5622001-05-15 23:21:41 +00002615
2616 if (!s)
2617 return NULL;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002618 for (cur = top_vars; cur; cur = cur->next) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002619 if (strcmp(cur->name, s) == 0)
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002620 return cur->value;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002621 }
Eric Andersenf72f5622001-05-15 23:21:41 +00002622 return NULL;
2623}
2624
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002625/* This is used to set local shell variables
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002626 flg_export == 0 if only local (not exporting) variable
2627 flg_export == 1 if "new" exporting environ
2628 flg_export > 1 if current startup environ (not call putenv()) */
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002629static int set_local_var(const char *s, int flg_export)
Eric Andersen78a7c992001-05-15 16:30:25 +00002630{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002631 char *name, *value;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002632 int result = 0;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002633 struct variables *cur;
Eric Andersen20a69a72001-05-15 17:24:44 +00002634
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002635 name = xstrdup(s);
Eric Andersen20a69a72001-05-15 17:24:44 +00002636
2637 /* Assume when we enter this function that we are already in
2638 * NAME=VALUE format. So the first order of business is to
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002639 * split 's' on the '=' into 'name' and 'value' */
Eric Andersen20a69a72001-05-15 17:24:44 +00002640 value = strchr(name, '=');
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002641 /*if (value == 0 && ++value == 0) ??? -vda */
2642 if (value == NULL || value[1] == '\0') {
Eric Andersen99785762001-05-22 21:37:48 +00002643 free(name);
2644 return -1;
2645 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002646 *value++ = '\0';
Eric Andersen20a69a72001-05-15 17:24:44 +00002647
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002648 for (cur = top_vars; cur; cur = cur->next) {
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002649 if (strcmp(cur->name, name) == 0) {
2650 if (strcmp(cur->value, value) == 0) {
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00002651 if (flg_export && !cur->flg_export)
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002652 cur->flg_export = flg_export;
2653 else
2654 result++;
2655 } else if (cur->flg_read_only) {
2656 bb_error_msg("%s: readonly variable", name);
Eric Andersen99785762001-05-22 21:37:48 +00002657 result = -1;
2658 } else {
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002659 if (flg_export > 0 || cur->flg_export > 1)
2660 cur->flg_export = 1;
2661 free((char*)cur->value);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002662 cur->value = xstrdup(value);
Eric Andersen20a69a72001-05-15 17:24:44 +00002663 }
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00002664 goto skip;
Eric Andersen20a69a72001-05-15 17:24:44 +00002665 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002666 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002667
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002668 cur = xzalloc(sizeof(*cur));
2669 cur->name = xstrdup(name);
2670 cur->value = xstrdup(value);
2671 /*cur->next = 0;*/
2672 cur->flg_export = flg_export;
2673 /*cur->flg_read_only = 0;*/
2674 {
2675 struct variables *bottom = top_vars;
2676 while (bottom->next)
2677 bottom = bottom->next;
2678 bottom->next = cur;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002679 }
2680 skip:
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002681 if (result == 0 && cur->flg_export == 1) {
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002682 *(value-1) = '=';
2683 result = putenv(name);
2684 } else {
Eric Andersen94ac2442001-05-22 19:05:18 +00002685 free(name);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002686 if (result > 0) /* equivalent to previous set */
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002687 result = 0;
2688 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002689 return result;
2690}
2691
Eric Andersenf72f5622001-05-15 23:21:41 +00002692static void unset_local_var(const char *name)
Eric Andersen20a69a72001-05-15 17:24:44 +00002693{
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002694 struct variables *cur, *next;
Eric Andersen20a69a72001-05-15 17:24:44 +00002695
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002696 if (!name)
2697 return;
2698 for (cur = top_vars; cur; cur = cur->next) {
2699 if (strcmp(cur->name, name) == 0) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002700 if (cur->flg_read_only) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002701 bb_error_msg("%s: readonly variable", name);
Eric Andersen94ac2442001-05-22 19:05:18 +00002702 return;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002703 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002704 if (cur->flg_export)
2705 unsetenv(cur->name);
2706 free((char*)cur->name);
2707 free((char*)cur->value);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002708 next = top_vars;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002709 while (next->next != cur)
2710 next = next->next;
2711 next->next = cur->next;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002712 free(cur);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002713 return;
Eric Andersenf72f5622001-05-15 23:21:41 +00002714 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002715 }
Eric Andersen78a7c992001-05-15 16:30:25 +00002716}
2717
2718static int is_assignment(const char *s)
2719{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002720 if (!s || !isalpha(*s))
2721 return 0;
2722 s++;
2723 while (isalnum(*s) || *s == '_')
2724 s++;
2725 return *s == '=';
Eric Andersen78a7c992001-05-15 16:30:25 +00002726}
2727
Eric Andersen25f27032001-04-26 23:22:31 +00002728/* the src parameter allows us to peek forward to a possible &n syntax
2729 * for file descriptor duplication, e.g., "2>&1".
2730 * Return code is 0 normally, 1 if a syntax error is detected in src.
2731 * Resource errors (in xmalloc) cause the process to exit */
2732static int setup_redirect(struct p_context *ctx, int fd, redir_type style,
2733 struct in_str *input)
2734{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002735 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00002736 struct redir_struct *redir = child->redirects;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002737 struct redir_struct *last_redir = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002738
2739 /* Create a new redir_struct and drop it onto the end of the linked list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002740 while (redir) {
2741 last_redir = redir;
2742 redir = redir->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002743 }
2744 redir = xmalloc(sizeof(struct redir_struct));
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002745 redir->next = NULL;
2746 redir->word.gl_pathv = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002747 if (last_redir) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002748 last_redir->next = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002749 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002750 child->redirects = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002751 }
2752
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002753 redir->type = style;
2754 redir->fd = (fd == -1) ? redir_table[style].default_fd : fd;
Eric Andersen25f27032001-04-26 23:22:31 +00002755
2756 debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
2757
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002758 /* Check for a '2>&1' type redirect */
Eric Andersen25f27032001-04-26 23:22:31 +00002759 redir->dup = redirect_dup_num(input);
2760 if (redir->dup == -2) return 1; /* syntax error */
2761 if (redir->dup != -1) {
2762 /* Erik had a check here that the file descriptor in question
Eric Andersen83a2ae22001-05-07 17:59:25 +00002763 * is legit; I postpone that to "run time"
2764 * A "-" representation of "close me" shows up as a -3 here */
Eric Andersen25f27032001-04-26 23:22:31 +00002765 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
2766 } else {
2767 /* We do _not_ try to open the file that src points to,
2768 * since we need to return and let src be expanded first.
2769 * Set ctx->pending_redirect, so we know what to do at the
2770 * end of the next parsed word.
2771 */
2772 ctx->pending_redirect = redir;
2773 }
2774 return 0;
2775}
2776
Denis Vlasenkoac678ec2007-04-16 22:32:04 +00002777static struct pipe *new_pipe(void)
2778{
Eric Andersen25f27032001-04-26 23:22:31 +00002779 struct pipe *pi;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002780 pi = xzalloc(sizeof(struct pipe));
2781 /*pi->num_progs = 0;*/
2782 /*pi->progs = NULL;*/
2783 /*pi->next = NULL;*/
2784 /*pi->followup = 0; invalid */
2785 if (RES_NONE)
2786 pi->r_mode = RES_NONE;
Eric Andersen25f27032001-04-26 23:22:31 +00002787 return pi;
2788}
2789
2790static void initialize_context(struct p_context *ctx)
2791{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002792 ctx->pipe = NULL;
2793 ctx->pending_redirect = NULL;
2794 ctx->child = NULL;
2795 ctx->list_head = new_pipe();
2796 ctx->pipe = ctx->list_head;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002797 ctx->res_w = RES_NONE;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002798 ctx->stack = NULL;
2799 ctx->old_flag = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002800 done_command(ctx); /* creates the memory for working child */
2801}
2802
2803/* normal return is 0
2804 * if a reserved word is found, and processed, return 1
2805 * should handle if, then, elif, else, fi, for, while, until, do, done.
2806 * case, function, and select are obnoxious, save those for later.
2807 */
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00002808static int reserved_word(o_string *dest, struct p_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00002809{
2810 struct reserved_combo {
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002811 char literal[7];
2812 unsigned char code;
2813 int flag;
Eric Andersen25f27032001-04-26 23:22:31 +00002814 };
2815 /* Mostly a list of accepted follow-up reserved words.
2816 * FLAG_END means we are done with the sequence, and are ready
2817 * to turn the compound list into a command.
2818 * FLAG_START means the word must start a new compound list.
2819 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002820 static const struct reserved_combo reserved_list[] = {
Eric Andersen25f27032001-04-26 23:22:31 +00002821 { "if", RES_IF, FLAG_THEN | FLAG_START },
2822 { "then", RES_THEN, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
2823 { "elif", RES_ELIF, FLAG_THEN },
2824 { "else", RES_ELSE, FLAG_FI },
2825 { "fi", RES_FI, FLAG_END },
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002826 { "for", RES_FOR, FLAG_IN | FLAG_START },
Eric Andersen25f27032001-04-26 23:22:31 +00002827 { "while", RES_WHILE, FLAG_DO | FLAG_START },
2828 { "until", RES_UNTIL, FLAG_DO | FLAG_START },
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002829 { "in", RES_IN, FLAG_DO },
Eric Andersen25f27032001-04-26 23:22:31 +00002830 { "do", RES_DO, FLAG_DONE },
2831 { "done", RES_DONE, FLAG_END }
2832 };
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00002833 enum { NRES = sizeof(reserved_list)/sizeof(reserved_list[0]) };
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002834 const struct reserved_combo *r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002835
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002836 for (r = reserved_list; r < reserved_list + NRES; r++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002837 if (strcmp(dest->data, r->literal) == 0) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002838 debug_printf("found reserved word %s, code %d\n", r->literal, r->code);
Eric Andersen25f27032001-04-26 23:22:31 +00002839 if (r->flag & FLAG_START) {
2840 struct p_context *new = xmalloc(sizeof(struct p_context));
2841 debug_printf("push stack\n");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002842 if (ctx->res_w == RES_IN || ctx->res_w == RES_FOR) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002843 syntax();
2844 free(new);
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002845 ctx->res_w = RES_SNTX;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002846 b_reset(dest);
2847 return 1;
2848 }
Eric Andersen25f27032001-04-26 23:22:31 +00002849 *new = *ctx; /* physical copy */
2850 initialize_context(ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002851 ctx->stack = new;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002852 } else if (ctx->res_w == RES_NONE || !(ctx->old_flag & (1 << r->code))) {
Eric Andersenaf44a0e2001-04-27 07:26:12 +00002853 syntax();
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002854 ctx->res_w = RES_SNTX;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002855 b_reset(dest);
Eric Andersenaf44a0e2001-04-27 07:26:12 +00002856 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002857 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002858 ctx->res_w = r->code;
Eric Andersen25f27032001-04-26 23:22:31 +00002859 ctx->old_flag = r->flag;
2860 if (ctx->old_flag & FLAG_END) {
2861 struct p_context *old;
2862 debug_printf("pop stack\n");
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002863 done_pipe(ctx, PIPE_SEQ);
Eric Andersen25f27032001-04-26 23:22:31 +00002864 old = ctx->stack;
2865 old->child->group = ctx->list_head;
Eric Andersen04407e52001-06-07 16:42:05 +00002866 old->child->subshell = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002867 *ctx = *old; /* physical copy */
2868 free(old);
Eric Andersen25f27032001-04-26 23:22:31 +00002869 }
Denis Vlasenkoff131b92007-04-10 15:42:06 +00002870 b_reset(dest);
Eric Andersen25f27032001-04-26 23:22:31 +00002871 return 1;
2872 }
2873 }
2874 return 0;
2875}
2876
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002877/* Normal return is 0.
Eric Andersen25f27032001-04-26 23:22:31 +00002878 * Syntax or xglob errors return 1. */
2879static int done_word(o_string *dest, struct p_context *ctx)
2880{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002881 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00002882 glob_t *glob_target;
2883 int gr, flags = 0;
2884
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002885 debug_printf_parse("done_word entered: '%s' %p\n", dest->data, child);
Eric Andersen25f27032001-04-26 23:22:31 +00002886 if (dest->length == 0 && !dest->nonnull) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002887 debug_printf_parse("done_word return 0: true null, ignored\n");
Eric Andersen25f27032001-04-26 23:22:31 +00002888 return 0;
2889 }
2890 if (ctx->pending_redirect) {
2891 glob_target = &ctx->pending_redirect->word;
2892 } else {
2893 if (child->group) {
2894 syntax();
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002895 debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n");
2896 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002897 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002898 if (!child->argv && (ctx->parse_type & FLAG_PARSE_SEMICOLON)) {
2899 debug_printf_parse(": checking '%s' for reserved-ness\n", dest->data);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002900 if (reserved_word(dest, ctx)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002901 debug_printf_parse("done_word return %d\n", (ctx->res_w == RES_SNTX));
2902 return (ctx->res_w == RES_SNTX);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002903 }
Eric Andersen25f27032001-04-26 23:22:31 +00002904 }
2905 glob_target = &child->glob_result;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002906 if (child->argv)
2907 flags |= GLOB_APPEND;
Eric Andersen25f27032001-04-26 23:22:31 +00002908 }
2909 gr = xglob(dest, flags, glob_target);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002910 if (gr != 0) {
2911 debug_printf_parse("done_word return 1: xglob returned %d\n", gr);
2912 return 1;
2913 }
Eric Andersen25f27032001-04-26 23:22:31 +00002914
2915 b_reset(dest);
2916 if (ctx->pending_redirect) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002917 ctx->pending_redirect = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002918 if (glob_target->gl_pathc != 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002919 bb_error_msg("ambiguous redirect");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002920 debug_printf_parse("done_word return 1: ambiguous redirect\n");
Eric Andersen25f27032001-04-26 23:22:31 +00002921 return 1;
2922 }
2923 } else {
2924 child->argv = glob_target->gl_pathv;
2925 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002926 if (ctx->res_w == RES_FOR) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002927 done_word(dest, ctx);
2928 done_pipe(ctx, PIPE_SEQ);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002929 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002930 debug_printf_parse("done_word return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00002931 return 0;
2932}
2933
2934/* The only possible error here is out of memory, in which case
2935 * xmalloc exits. */
2936static int done_command(struct p_context *ctx)
2937{
2938 /* The child is really already in the pipe structure, so
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002939 * advance the pipe counter and make a new, null child. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002940 struct pipe *pi = ctx->pipe;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002941 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00002942
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002943 if (child) {
2944 if (child->group == NULL
2945 && child->argv == NULL
2946 && child->redirects == NULL
2947 ) {
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00002948 debug_printf_parse("done_command: skipping null cmd, num_progs=%d\n", pi->num_progs);
2949 return pi->num_progs;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002950 }
Eric Andersen25f27032001-04-26 23:22:31 +00002951 pi->num_progs++;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002952 debug_printf_parse("done_command: ++num_progs=%d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00002953 } else {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002954 debug_printf_parse("done_command: initializing, num_progs=%d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00002955 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002956
2957 /* Only real trickiness here is that the uncommitted
2958 * child structure is not counted in pi->num_progs. */
Eric Andersen25f27032001-04-26 23:22:31 +00002959 pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1));
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002960 child = &pi->progs[pi->num_progs];
Eric Andersen25f27032001-04-26 23:22:31 +00002961
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002962 memset(child, 0, sizeof(*child));
2963 /*child->redirects = NULL;*/
2964 /*child->argv = NULL;*/
2965 /*child->is_stopped = 0;*/
2966 /*child->group = NULL;*/
2967 /*child->glob_result.gl_pathv = NULL;*/
2968 child->family = pi;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002969 //sp: /*child->sp = 0;*/
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002970 child->type = ctx->parse_type;
Eric Andersen25f27032001-04-26 23:22:31 +00002971
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002972 ctx->child = child;
Eric Andersen25f27032001-04-26 23:22:31 +00002973 /* but ctx->pipe and ctx->list_head remain unchanged */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002974
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00002975 return pi->num_progs; /* used only for 0/nonzero check */
Eric Andersen25f27032001-04-26 23:22:31 +00002976}
2977
2978static int done_pipe(struct p_context *ctx, pipe_style type)
2979{
2980 struct pipe *new_p;
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00002981 int not_null;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002982
2983 debug_printf_parse("done_pipe entered, followup %d\n", type);
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00002984 not_null = done_command(ctx); /* implicit closure of previous command */
Eric Andersen25f27032001-04-26 23:22:31 +00002985 ctx->pipe->followup = type;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002986 ctx->pipe->r_mode = ctx->res_w;
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00002987 /* Without this check, even just <enter> on command line generates
2988 * tree of three NOPs (!). Which is harmless but annoying.
2989 * IOW: it is safe to do it unconditionally. */
2990 if (not_null) {
2991 new_p = new_pipe();
2992 ctx->pipe->next = new_p;
2993 ctx->pipe = new_p;
2994 ctx->child = NULL;
2995 done_command(ctx); /* set up new pipe to accept commands */
2996 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002997 debug_printf_parse("done_pipe return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00002998 return 0;
2999}
3000
3001/* peek ahead in the in_str to find out if we have a "&n" construct,
3002 * as in "2>&1", that represents duplicating a file descriptor.
3003 * returns either -2 (syntax error), -1 (no &), or the number found.
3004 */
3005static int redirect_dup_num(struct in_str *input)
3006{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003007 int ch, d = 0, ok = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003008 ch = b_peek(input);
3009 if (ch != '&') return -1;
3010
3011 b_getch(input); /* get the & */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003012 ch = b_peek(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003013 if (ch == '-') {
3014 b_getch(input);
3015 return -3; /* "-" represents "close me" */
3016 }
3017 while (isdigit(ch)) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00003018 d = d*10 + (ch-'0');
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003019 ok = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003020 b_getch(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003021 ch = b_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003022 }
3023 if (ok) return d;
3024
Manuel Novoa III cad53642003-03-19 09:13:01 +00003025 bb_error_msg("ambiguous redirect");
Eric Andersen25f27032001-04-26 23:22:31 +00003026 return -2;
3027}
3028
3029/* If a redirect is immediately preceded by a number, that number is
3030 * supposed to tell which file descriptor to redirect. This routine
3031 * looks for such preceding numbers. In an ideal world this routine
3032 * needs to handle all the following classes of redirects...
3033 * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
3034 * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
3035 * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
3036 * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
3037 * A -1 output from this program means no valid number was found, so the
3038 * caller should use the appropriate default for this redirection.
3039 */
3040static int redirect_opt_num(o_string *o)
3041{
3042 int num;
3043
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003044 if (o->length == 0)
3045 return -1;
3046 for (num = 0; num < o->length; num++) {
3047 if (!isdigit(*(o->data + num))) {
Eric Andersen25f27032001-04-26 23:22:31 +00003048 return -1;
3049 }
3050 }
3051 /* reuse num (and save an int) */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003052 num = atoi(o->data);
Eric Andersen25f27032001-04-26 23:22:31 +00003053 b_reset(o);
3054 return num;
3055}
3056
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00003057static FILE *generate_stream_from_list(struct pipe *head)
Eric Andersen25f27032001-04-26 23:22:31 +00003058{
3059 FILE *pf;
Eric Andersen25f27032001-04-26 23:22:31 +00003060 int pid, channel[2];
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003061 if (pipe(channel) < 0) bb_perror_msg_and_die("pipe");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003062#if BB_MMU
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003063 pid = fork();
Eric Andersen72f9a422001-10-28 05:12:20 +00003064#else
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003065 pid = vfork();
Eric Andersen72f9a422001-10-28 05:12:20 +00003066#endif
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003067 if (pid < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00003068 bb_perror_msg_and_die("fork");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003069 } else if (pid == 0) {
Eric Andersen25f27032001-04-26 23:22:31 +00003070 close(channel[0]);
3071 if (channel[1] != 1) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00003072 dup2(channel[1], 1);
Eric Andersen25f27032001-04-26 23:22:31 +00003073 close(channel[1]);
3074 }
Eric Andersen94ac2442001-05-22 19:05:18 +00003075 _exit(run_list_real(head)); /* leaks memory */
Eric Andersen25f27032001-04-26 23:22:31 +00003076 }
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003077 debug_printf("forked child %d\n", pid);
Eric Andersen25f27032001-04-26 23:22:31 +00003078 close(channel[1]);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003079 pf = fdopen(channel[0], "r");
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003080 debug_printf("pipe on FILE *%p\n", pf);
Eric Andersen25f27032001-04-26 23:22:31 +00003081 return pf;
3082}
3083
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003084/* Return code is exit status of the process that is run. */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003085static int process_command_subs(o_string *dest, struct p_context *ctx,
3086 struct in_str *input, const char *subst_end)
Eric Andersen25f27032001-04-26 23:22:31 +00003087{
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003088 int retcode, ch, eol_cnt;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003089 o_string result = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003090 struct p_context inner;
3091 FILE *p;
3092 struct in_str pipe_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003093
Eric Andersen25f27032001-04-26 23:22:31 +00003094 initialize_context(&inner);
3095
3096 /* recursion to generate command */
3097 retcode = parse_stream(&result, &inner, input, subst_end);
3098 if (retcode != 0) return retcode; /* syntax error or EOF */
3099 done_word(&result, &inner);
3100 done_pipe(&inner, PIPE_SEQ);
3101 b_free(&result);
3102
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003103 p = generate_stream_from_list(inner.list_head);
3104 if (p == NULL) return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003105 mark_open(fileno(p));
3106 setup_file_in_str(&pipe_str, p);
3107
3108 /* now send results of command back into original context */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003109 eol_cnt = 0;
3110 while ((ch = b_getch(&pipe_str)) != EOF) {
3111 if (ch == '\n') {
3112 eol_cnt++;
3113 continue;
3114 }
3115 while (eol_cnt) {
3116 b_addqchr(dest, '\n', dest->quote);
3117 eol_cnt--;
3118 }
3119 b_addqchr(dest, ch, dest->quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003120 }
3121
3122 debug_printf("done reading from pipe, pclose()ing\n");
3123 /* This is the step that wait()s for the child. Should be pretty
3124 * safe, since we just read an EOF from its stdout. We could try
3125 * to better, by using wait(), and keeping track of background jobs
3126 * at the same time. That would be a lot of work, and contrary
3127 * to the KISS philosophy of this program. */
3128 mark_closed(fileno(p));
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003129 retcode = pclose(p);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003130 free_pipe_list(inner.list_head, 0);
3131 debug_printf("pclosed, retcode=%d\n", retcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003132 return retcode;
3133}
3134
3135static int parse_group(o_string *dest, struct p_context *ctx,
3136 struct in_str *input, int ch)
3137{
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003138 int rcode;
3139 const char *endch = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003140 struct p_context sub;
3141 struct child_prog *child = ctx->child;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003142
3143 debug_printf_parse("parse_group entered\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003144 if (child->argv) {
3145 syntax();
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003146 debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n");
3147 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003148 }
3149 initialize_context(&sub);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00003150 switch (ch) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003151 case '(':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003152 endch = ")";
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003153 child->subshell = 1;
3154 break;
3155 case '{':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003156 endch = "}";
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003157 break;
3158 default:
3159 syntax(); /* really logic error */
Eric Andersen25f27032001-04-26 23:22:31 +00003160 }
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003161 rcode = parse_stream(dest, &sub, input, endch);
3162 done_word(dest, &sub); /* finish off the final word in the subcontext */
Eric Andersen25f27032001-04-26 23:22:31 +00003163 done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
3164 child->group = sub.list_head;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003165
3166 debug_printf_parse("parse_group return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003167 return rcode;
3168 /* child remains "open", available for possible redirects */
3169}
3170
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003171/* Basically useful version until someone wants to get fancier,
Eric Andersen25f27032001-04-26 23:22:31 +00003172 * see the bash man page under "Parameter Expansion" */
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00003173static const char *lookup_param(const char *src)
Eric Andersen25f27032001-04-26 23:22:31 +00003174{
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00003175 const char *p = NULL;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003176 if (src) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003177 p = getenv(src);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003178 if (!p)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003179 p = get_local_var(src);
Eric Andersen20a69a72001-05-15 17:24:44 +00003180 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003181 return p;
Eric Andersen25f27032001-04-26 23:22:31 +00003182}
3183
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00003184/* Make new string for parser */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00003185static char* make_string(char **inp)
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00003186{
3187 char *p;
3188 char *str = NULL;
3189 int n;
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003190 int val_len;
3191 int len = 0;
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00003192
3193 for (n = 0; inp[n]; n++) {
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00003194 p = expand_variables_to_string(inp[n]);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003195 val_len = strlen(p);
3196 str = xrealloc(str, len + val_len + 3); /* +3: space, '\n', <nul>*/
3197 str[len++] = ' ';
3198 strcpy(str + len, p);
3199 len += val_len;
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00003200 if (p != inp[n]) free(p);
3201 }
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003202 /* We do not check for case where loop had no iterations at all
3203 * - cannot happen? */
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00003204 str[len] = '\n';
3205 str[len+1] = '\0';
3206 return str;
3207}
3208
Eric Andersen25f27032001-04-26 23:22:31 +00003209/* return code: 0 for OK, 1 for syntax error */
3210static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input)
3211{
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003212 int ch = b_peek(input); /* first character after the $ */
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003213 unsigned char quote_mask = dest->quote ? 0x80 : 0;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003214
3215 debug_printf_parse("handle_dollar entered: ch='%c'\n", ch);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003216 if (isalpha(ch)) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003217 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003218 //sp: ctx->child->sp++;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003219 while (1) {
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003220 debug_printf_parse(": '%c'\n", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003221 b_getch(input);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003222 b_addchr(dest, ch | quote_mask);
3223 quote_mask = 0;
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003224 ch = b_peek(input);
3225 if (!isalnum(ch) && ch != '_')
3226 break;
Eric Andersen25f27032001-04-26 23:22:31 +00003227 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003228 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003229 } else if (isdigit(ch)) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003230 make_one_char_var:
3231 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003232 //sp: ctx->child->sp++;
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003233 debug_printf_parse(": '%c'\n", ch);
3234 b_getch(input);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003235 b_addchr(dest, ch | quote_mask);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003236 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003237 } else switch (ch) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003238 case '$': /* pid */
3239 case '!': /* last bg pid */
3240 case '?': /* last exit code */
3241 case '#': /* number of args */
3242 case '*': /* args */
3243 case '@': /* args */
3244 goto make_one_char_var;
Eric Andersen25f27032001-04-26 23:22:31 +00003245 case '{':
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003246 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003247 //sp: ctx->child->sp++;
Eric Andersen25f27032001-04-26 23:22:31 +00003248 b_getch(input);
3249 /* XXX maybe someone will try to escape the '}' */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003250 while (1) {
3251 ch = b_getch(input);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003252 if (ch == EOF) {
3253 syntax();
3254 debug_printf_parse("handle_dollar return 1: unterminated ${name}\n");
3255 return 1;
3256 }
3257 if (ch == '}')
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003258 break;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003259 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003260 b_addchr(dest, ch | quote_mask);
3261 quote_mask = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003262 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003263 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003264 break;
3265 case '(':
Matt Kraai9f8caf12001-05-02 16:26:12 +00003266 b_getch(input);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003267 process_command_subs(dest, ctx, input, ")");
Eric Andersen25f27032001-04-26 23:22:31 +00003268 break;
Eric Andersen25f27032001-04-26 23:22:31 +00003269 case '-':
3270 case '_':
3271 /* still unhandled, but should be eventually */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003272 bb_error_msg("unhandled syntax: $%c", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003273 return 1;
3274 break;
3275 default:
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003276 b_addqchr(dest, '$', dest->quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003277 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003278 debug_printf_parse("handle_dollar return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003279 return 0;
3280}
3281
Eric Andersen25f27032001-04-26 23:22:31 +00003282/* return code is 0 for normal exit, 1 for syntax error */
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003283static int parse_stream(o_string *dest, struct p_context *ctx,
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003284 struct in_str *input, const char *end_trigger)
Eric Andersen25f27032001-04-26 23:22:31 +00003285{
"Vladimir N. Oleynik"4ccd2b42006-01-31 09:27:48 +00003286 int ch, m;
Eric Andersen25f27032001-04-26 23:22:31 +00003287 int redir_fd;
3288 redir_type redir_style;
3289 int next;
3290
3291 /* Only double-quote state is handled in the state variable dest->quote.
3292 * A single-quote triggers a bypass of the main loop until its mate is
3293 * found. When recursing, quote state is passed in via dest->quote. */
3294
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003295 debug_printf_parse("parse_stream entered, end_trigger='%s'\n", end_trigger);
3296
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003297 while ((ch = b_getch(input)) != EOF) {
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003298 m = charmap[ch];
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003299 next = (ch == '\n') ? '\0' : b_peek(input);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003300 debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n",
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003301 ch, ch, m, dest->quote);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003302 if (m == CHAR_ORDINARY
3303 || (m != CHAR_SPECIAL && dest->quote)
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003304 ) {
Eric Andersen25f27032001-04-26 23:22:31 +00003305 b_addqchr(dest, ch, dest->quote);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003306 continue;
3307 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003308 if (m == CHAR_IFS) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003309 if (done_word(dest, ctx)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003310 debug_printf_parse("parse_stream return 1: done_word!=0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003311 return 1;
Eric Andersenaac75e52001-04-30 18:18:45 +00003312 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003313 /* If we aren't performing a substitution, treat
3314 * a newline as a command separator.
3315 * [why we don't handle it exactly like ';'? --vda] */
3316 if (end_trigger && ch == '\n') {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003317 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003318 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003319 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003320 if ((end_trigger && strchr(end_trigger, ch))
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003321 && !dest->quote && ctx->res_w == RES_NONE
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003322 ) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003323 debug_printf_parse("parse_stream return 0: end_trigger char found\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003324 return 0;
3325 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003326 if (m == CHAR_IFS)
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003327 continue;
3328 switch (ch) {
Eric Andersen25f27032001-04-26 23:22:31 +00003329 case '#':
3330 if (dest->length == 0 && !dest->quote) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003331 while (1) {
3332 ch = b_peek(input);
3333 if (ch == EOF || ch == '\n')
3334 break;
3335 b_getch(input);
3336 }
Eric Andersen25f27032001-04-26 23:22:31 +00003337 } else {
3338 b_addqchr(dest, ch, dest->quote);
3339 }
3340 break;
3341 case '\\':
3342 if (next == EOF) {
3343 syntax();
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003344 debug_printf_parse("parse_stream return 1: \\<eof>\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003345 return 1;
3346 }
3347 b_addqchr(dest, '\\', dest->quote);
3348 b_addqchr(dest, b_getch(input), dest->quote);
3349 break;
3350 case '$':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003351 if (handle_dollar(dest, ctx, input) != 0) {
3352 debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n");
3353 return 1;
3354 }
Eric Andersen25f27032001-04-26 23:22:31 +00003355 break;
3356 case '\'':
3357 dest->nonnull = 1;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003358 while (1) {
3359 ch = b_getch(input);
3360 if (ch == EOF || ch == '\'')
3361 break;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003362 b_addchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003363 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003364 if (ch == EOF) {
Eric Andersen25f27032001-04-26 23:22:31 +00003365 syntax();
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003366 debug_printf_parse("parse_stream return 1: unterminated '\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003367 return 1;
3368 }
3369 break;
3370 case '"':
3371 dest->nonnull = 1;
3372 dest->quote = !dest->quote;
3373 break;
3374 case '`':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003375 process_command_subs(dest, ctx, input, "`");
Eric Andersen25f27032001-04-26 23:22:31 +00003376 break;
3377 case '>':
3378 redir_fd = redirect_opt_num(dest);
3379 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003380 redir_style = REDIRECT_OVERWRITE;
Eric Andersen25f27032001-04-26 23:22:31 +00003381 if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003382 redir_style = REDIRECT_APPEND;
Eric Andersen25f27032001-04-26 23:22:31 +00003383 b_getch(input);
3384 } else if (next == '(') {
3385 syntax(); /* until we support >(list) Process Substitution */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003386 debug_printf_parse("parse_stream return 1: >(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003387 return 1;
3388 }
3389 setup_redirect(ctx, redir_fd, redir_style, input);
3390 break;
3391 case '<':
3392 redir_fd = redirect_opt_num(dest);
3393 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003394 redir_style = REDIRECT_INPUT;
Eric Andersen25f27032001-04-26 23:22:31 +00003395 if (next == '<') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003396 redir_style = REDIRECT_HEREIS;
Eric Andersen25f27032001-04-26 23:22:31 +00003397 b_getch(input);
3398 } else if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003399 redir_style = REDIRECT_IO;
Eric Andersen25f27032001-04-26 23:22:31 +00003400 b_getch(input);
3401 } else if (next == '(') {
3402 syntax(); /* until we support <(list) Process Substitution */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003403 debug_printf_parse("parse_stream return 1: <(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003404 return 1;
3405 }
3406 setup_redirect(ctx, redir_fd, redir_style, input);
3407 break;
3408 case ';':
3409 done_word(dest, ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003410 done_pipe(ctx, PIPE_SEQ);
Eric Andersen25f27032001-04-26 23:22:31 +00003411 break;
3412 case '&':
3413 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003414 if (next == '&') {
Eric Andersen25f27032001-04-26 23:22:31 +00003415 b_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003416 done_pipe(ctx, PIPE_AND);
Eric Andersen25f27032001-04-26 23:22:31 +00003417 } else {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003418 done_pipe(ctx, PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00003419 }
3420 break;
3421 case '|':
3422 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003423 if (next == '|') {
Eric Andersen25f27032001-04-26 23:22:31 +00003424 b_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003425 done_pipe(ctx, PIPE_OR);
Eric Andersen25f27032001-04-26 23:22:31 +00003426 } else {
3427 /* we could pick up a file descriptor choice here
3428 * with redirect_opt_num(), but bash doesn't do it.
3429 * "echo foo 2| cat" yields "foo 2". */
3430 done_command(ctx);
3431 }
3432 break;
3433 case '(':
3434 case '{':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003435 if (parse_group(dest, ctx, input, ch) != 0) {
3436 debug_printf_parse("parse_stream return 1: parse_group returned non-0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003437 return 1;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003438 }
Eric Andersen25f27032001-04-26 23:22:31 +00003439 break;
3440 case ')':
3441 case '}':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003442 syntax(); /* Proper use of this character is caught by end_trigger */
3443 debug_printf_parse("parse_stream return 1: unexpected '}'\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003444 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003445 default:
3446 syntax(); /* this is really an internal logic error */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003447 debug_printf_parse("parse_stream return 1: internal logic error\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003448 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003449 }
3450 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003451 /* Complain if quote? No, maybe we just finished a command substitution
Eric Andersen25f27032001-04-26 23:22:31 +00003452 * that was quoted. Example:
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003453 * $ echo "`cat foo` plus more"
Eric Andersen25f27032001-04-26 23:22:31 +00003454 * and we just got the EOF generated by the subshell that ran "cat foo"
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003455 * The only real complaint is if we got an EOF when end_trigger != NULL,
Eric Andersen25f27032001-04-26 23:22:31 +00003456 * that is, we were really supposed to get end_trigger, and never got
3457 * one before the EOF. Can't use the standard "syntax error" return code,
3458 * so that parse_stream_outer can distinguish the EOF and exit smoothly. */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003459 debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL));
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003460 if (end_trigger)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003461 return -1;
Eric Andersen25f27032001-04-26 23:22:31 +00003462 return 0;
3463}
3464
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003465static void set_in_charmap(const char *set, int code)
Eric Andersen25f27032001-04-26 23:22:31 +00003466{
Denis Vlasenkof5294e12007-04-14 10:09:57 +00003467 while (*set)
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003468 charmap[(unsigned char)*set++] = code;
Eric Andersen25f27032001-04-26 23:22:31 +00003469}
3470
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003471static void update_charmap(void)
Eric Andersen25f27032001-04-26 23:22:31 +00003472{
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003473 /* char *ifs and char charmap[256] are both globals. */
Eric Andersen25f27032001-04-26 23:22:31 +00003474 ifs = getenv("IFS");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003475 if (ifs == NULL)
3476 ifs = " \t\n";
Eric Andersen25f27032001-04-26 23:22:31 +00003477 /* Precompute a list of 'flow through' behavior so it can be treated
3478 * quickly up front. Computation is necessary because of IFS.
3479 * Special case handling of IFS == " \t\n" is not implemented.
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003480 * The charmap[] array only really needs two bits each,
3481 * and on most machines that would be faster (reduced L1 cache use).
Eric Andersen25f27032001-04-26 23:22:31 +00003482 */
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003483 memset(charmap, CHAR_ORDINARY, sizeof(charmap));
3484 set_in_charmap("\\$\"`", CHAR_SPECIAL);
3485 set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED);
3486 set_in_charmap(ifs, CHAR_IFS); /* also flow through if quoted */
Eric Andersen25f27032001-04-26 23:22:31 +00003487}
3488
Eric Andersenaff114c2004-04-14 17:51:38 +00003489/* most recursion does not come through here, the exception is
Eric Andersen25f27032001-04-26 23:22:31 +00003490 * from builtin_source() */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003491static int parse_stream_outer(struct in_str *inp, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003492{
Eric Andersen25f27032001-04-26 23:22:31 +00003493 struct p_context ctx;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003494 o_string temp = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003495 int rcode;
3496 do {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003497 ctx.parse_type = parse_flag;
Eric Andersen25f27032001-04-26 23:22:31 +00003498 initialize_context(&ctx);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003499 update_charmap();
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003500 if (!(parse_flag & FLAG_PARSE_SEMICOLON) || (parse_flag & FLAG_REPARSING))
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003501 set_in_charmap(";$&|", CHAR_ORDINARY);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003502#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003503 inp->promptmode = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003504#endif
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003505 /* We will stop & execute after each ';' or '\n'.
3506 * Example: "sleep 9999; echo TEST" + ctrl-C:
3507 * TEST should be printed */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003508 rcode = parse_stream(&temp, &ctx, inp, ";\n");
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003509 if (rcode != 1 && ctx.old_flag != 0) {
3510 syntax();
3511 }
3512 if (rcode != 1 && ctx.old_flag == 0) {
3513 done_word(&temp, &ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003514 done_pipe(&ctx, PIPE_SEQ);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00003515 debug_print_tree(ctx.list_head, 0);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003516 debug_printf_exec("parse_stream_outer: run_list\n");
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003517 run_list(ctx.list_head);
3518 } else {
3519 if (ctx.old_flag != 0) {
3520 free(ctx.stack);
3521 b_reset(&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003522 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003523 temp.nonnull = 0;
3524 temp.quote = 0;
3525 inp->p = NULL;
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003526 free_pipe_list(ctx.list_head, 0);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003527 }
Eric Andersena813afc2001-05-24 16:19:36 +00003528 b_free(&temp);
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003529 } while (rcode != -1 && !(parse_flag & FLAG_EXIT_FROM_LOOP)); /* loop on syntax errors, return on EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003530 return 0;
3531}
3532
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003533static int parse_string_outer(const char *s, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003534{
3535 struct in_str input;
3536 setup_string_in_str(&input, s);
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003537 return parse_stream_outer(&input, parse_flag);
Eric Andersen25f27032001-04-26 23:22:31 +00003538}
3539
3540static int parse_file_outer(FILE *f)
3541{
3542 int rcode;
3543 struct in_str input;
3544 setup_file_in_str(&input, f);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003545 rcode = parse_stream_outer(&input, FLAG_PARSE_SEMICOLON);
Eric Andersen25f27032001-04-26 23:22:31 +00003546 return rcode;
3547}
3548
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003549#if ENABLE_HUSH_JOB
Eric Andersen6c947d22001-06-25 22:24:38 +00003550/* Make sure we have a controlling tty. If we get started under a job
3551 * aware app (like bash for example), make sure we are now in charge so
3552 * we don't fight over who gets the foreground */
Eric Anderseneaecbf32001-10-31 10:41:31 +00003553static void setup_job_control(void)
Eric Andersen52a97ca2001-06-22 06:49:26 +00003554{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003555 pid_t shell_pgrp;
3556
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003557 saved_task_pgrp = shell_pgrp = getpgrp();
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003558 debug_printf_jobs("saved_task_pgrp=%d\n", saved_task_pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003559 fcntl(interactive_fd, F_SETFD, FD_CLOEXEC);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003560
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003561 /* If we were ran as 'hush &',
3562 * sleep until we are in the foreground. */
3563 while (tcgetpgrp(interactive_fd) != shell_pgrp) {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003564 /* Send TTIN to ourself (should stop us) */
Denis Vlasenkoff131b92007-04-10 15:42:06 +00003565 kill(- shell_pgrp, SIGTTIN);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003566 shell_pgrp = getpgrp();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003567 }
Eric Andersen52a97ca2001-06-22 06:49:26 +00003568
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003569 /* Ignore job-control and misc signals. */
3570 set_jobctrl_sighandler(SIG_IGN);
3571 set_misc_sighandler(SIG_IGN);
3572//huh? signal(SIGCHLD, SIG_IGN);
3573
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003574 /* We _must_ restore tty pgrp on fatal signals */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003575 set_fatal_sighandler(sigexit);
Eric Andersen6c947d22001-06-25 22:24:38 +00003576
3577 /* Put ourselves in our own process group. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003578 setpgrp(); /* is the same as setpgid(our_pid, our_pid); */
Eric Andersen6c947d22001-06-25 22:24:38 +00003579 /* Grab control of the terminal. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003580 tcsetpgrp(interactive_fd, getpid());
Eric Andersen6c947d22001-06-25 22:24:38 +00003581}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003582#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00003583
Denis Vlasenko06af2162007-02-03 17:28:39 +00003584int hush_main(int argc, char **argv);
Matt Kraai2d91deb2001-08-01 17:21:35 +00003585int hush_main(int argc, char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00003586{
3587 int opt;
3588 FILE *input;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003589 char **e;
Eric Andersenbc604a22001-05-16 05:24:03 +00003590
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003591 PTR_TO_GLOBALS = xzalloc(sizeof(G));
3592 top_vars = &shell_ver;
3593 shell_ver = const_shell_ver; /* copying struct here */
3594
Denis Vlasenko38f63192007-01-22 09:03:07 +00003595#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003596 line_input_state = new_line_input_t(FOR_SHELL);
3597#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003598 /* XXX what should these be while sourcing /etc/profile? */
3599 global_argc = argc;
3600 global_argv = argv;
Eric Andersen94ac2442001-05-22 19:05:18 +00003601 /* Initialize some more globals to non-zero values */
3602 set_cwd();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003603#if ENABLE_HUSH_INTERACTIVE
3604#if ENABLE_FEATURE_EDITING
3605 cmdedit_set_initial_prompt();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003606#endif
Eric Andersen94ac2442001-05-22 19:05:18 +00003607 PS2 = "> ";
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003608#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003609
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003610 /* initialize our shell local variables with the values
Eric Andersen94ac2442001-05-22 19:05:18 +00003611 * currently living in the environment */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003612 e = environ;
3613 if (e)
3614 while (*e)
3615 set_local_var(*e++, 2); /* without call putenv() */
Eric Andersen94ac2442001-05-22 19:05:18 +00003616
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003617 last_return_code = EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00003618
3619 if (argv[0] && argv[0][0] == '-') {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003620 debug_printf("sourcing /etc/profile\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003621 input = fopen("/etc/profile", "r");
3622 if (input != NULL) {
Eric Andersena90f20b2001-06-26 23:00:21 +00003623 mark_open(fileno(input));
3624 parse_file_outer(input);
3625 mark_closed(fileno(input));
3626 fclose(input);
3627 }
Eric Andersen25f27032001-04-26 23:22:31 +00003628 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003629 input = stdin;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003630
Eric Andersen25f27032001-04-26 23:22:31 +00003631 while ((opt = getopt(argc, argv, "c:xif")) > 0) {
3632 switch (opt) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003633 case 'c':
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003634 global_argv = argv + optind;
3635 global_argc = argc - optind;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003636 opt = parse_string_outer(optarg, FLAG_PARSE_SEMICOLON);
3637 goto final_return;
3638 case 'i':
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003639 /* Well, we cannot just declare interactiveness,
3640 * we have to have some stuff (ctty, etc) */
3641 /* interactive_fd++; */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003642 break;
3643 case 'f':
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003644 fake_mode = 1;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003645 break;
3646 default:
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003647#ifndef BB_VER
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003648 fprintf(stderr, "Usage: sh [FILE]...\n"
3649 " or: sh -c command [args]...\n\n");
3650 exit(EXIT_FAILURE);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003651#else
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003652 bb_show_usage();
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003653#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003654 }
3655 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003656#if ENABLE_HUSH_JOB
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003657 /* A shell is interactive if the '-i' flag was given, or if all of
Eric Andersen25f27032001-04-26 23:22:31 +00003658 * the following conditions are met:
Denis Vlasenko55b2de72007-04-18 17:21:28 +00003659 * no -c command
Eric Andersen25f27032001-04-26 23:22:31 +00003660 * no arguments remaining or the -s flag given
3661 * standard input is a terminal
3662 * standard output is a terminal
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003663 * Refer to Posix.2, the description of the 'sh' utility. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003664 if (argv[optind] == NULL && input == stdin
3665 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
3666 ) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003667 saved_tty_pgrp = tcgetpgrp(STDIN_FILENO);
3668 debug_printf("saved_tty_pgrp=%d\n", saved_tty_pgrp);
3669 if (saved_tty_pgrp >= 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003670 /* try to dup to high fd#, >= 255 */
3671 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
3672 if (interactive_fd < 0) {
3673 /* try to dup to any fd */
3674 interactive_fd = dup(STDIN_FILENO);
3675 if (interactive_fd < 0)
3676 /* give up */
3677 interactive_fd = 0;
3678 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003679 // TODO: track & disallow any attempts of user
3680 // to (inadvertently) close/redirect it
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003681 }
Eric Andersen25f27032001-04-26 23:22:31 +00003682 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003683 debug_printf("interactive_fd=%d\n", interactive_fd);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003684 if (interactive_fd) {
Eric Andersen25f27032001-04-26 23:22:31 +00003685 /* Looks like they want an interactive shell */
Eric Andersen52a97ca2001-06-22 06:49:26 +00003686 setup_job_control();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003687 /* Make xfuncs do cleanup on exit */
3688 die_sleep = -1; /* flag */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003689// FIXME: should we reset die_sleep = 0 whereever we fork?
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003690 if (setjmp(die_jmp)) {
3691 /* xfunc has failed! die die die */
3692 hush_exit(xfunc_error_retval);
3693 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003694#if !ENABLE_FEATURE_SH_EXTRA_QUIET
Denis Vlasenkoef36ead2007-05-02 15:34:47 +00003695 printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", BB_BANNER);
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003696 printf("Enter 'help' for a list of built-in commands.\n\n");
3697#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00003698 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003699#elif ENABLE_HUSH_INTERACTIVE
3700/* no job control compiled, only prompt/line editing */
3701 if (argv[optind] == NULL && input == stdin
3702 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
3703 ) {
3704 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
3705 if (interactive_fd < 0) {
3706 /* try to dup to any fd */
3707 interactive_fd = dup(STDIN_FILENO);
3708 if (interactive_fd < 0)
3709 /* give up */
3710 interactive_fd = 0;
3711 }
3712 }
3713
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003714#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003715
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003716 if (argv[optind] == NULL) {
3717 opt = parse_file_outer(stdin);
Eric Andersene67c3ce2001-05-02 02:09:36 +00003718 goto final_return;
Eric Andersen25f27032001-04-26 23:22:31 +00003719 }
Eric Andersen25f27032001-04-26 23:22:31 +00003720
3721 debug_printf("\nrunning script '%s'\n", argv[optind]);
Denis Vlasenko4c978632007-02-03 03:31:13 +00003722 global_argv = argv + optind;
3723 global_argc = argc - optind;
Rob Landleyd921b2e2006-08-03 15:41:12 +00003724 input = xfopen(argv[optind], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00003725 opt = parse_file_outer(input);
3726
Denis Vlasenko38f63192007-01-22 09:03:07 +00003727#if ENABLE_FEATURE_CLEAN_UP
Eric Andersenaeb44c42001-05-22 20:29:00 +00003728 fclose(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003729 if (cwd != bb_msg_unknown)
Eric Andersenaeb44c42001-05-22 20:29:00 +00003730 free((char*)cwd);
3731 {
3732 struct variables *cur, *tmp;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003733 for (cur = top_vars; cur; cur = tmp) {
Eric Andersenaeb44c42001-05-22 20:29:00 +00003734 tmp = cur->next;
3735 if (!cur->flg_read_only) {
Denis Vlasenko4c978632007-02-03 03:31:13 +00003736 free((char*)cur->name);
3737 free((char*)cur->value);
Eric Andersenaeb44c42001-05-22 20:29:00 +00003738 free(cur);
3739 }
3740 }
3741 }
Eric Andersen25f27032001-04-26 23:22:31 +00003742#endif
3743
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003744 final_return:
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003745 hush_exit(opt ? opt : last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +00003746}