blob: 735526a1d42b210e2a128dee8cec3143b25fd356 [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:
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +000023 * o_addchr() derived from similar w_addchar function in glibc-2.2
Eric Andersen25f27032001-04-26 23:22:31 +000024 * 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?)
Eric Andersen25f27032001-04-26 23:22:31 +000040 * $_
41 * ! negation operator for pipes
42 * &> and >& redirection of stdout+stderr
43 * Brace Expansion
44 * Tilde Expansion
45 * fancy forms of Parameter Expansion
Eric Andersen78a7c992001-05-15 16:30:25 +000046 * aliases
Eric Andersen25f27032001-04-26 23:22:31 +000047 * Arithmetic Expansion
48 * <(list) and >(list) Process Substitution
Eric Andersen83a2ae22001-05-07 17:59:25 +000049 * reserved words: case, esac, select, function
Eric Andersen25f27032001-04-26 23:22:31 +000050 * Here Documents ( << word )
51 * Functions
52 * Major bugs:
Denis Vlasenkob81b3df2007-04-28 16:48:04 +000053 * job handling woefully incomplete and buggy (improved --vda)
Eric Andersen25f27032001-04-26 23:22:31 +000054 * reserved word execution woefully incomplete and buggy
Eric Andersen25f27032001-04-26 23:22:31 +000055 * to-do:
Eric Andersen83a2ae22001-05-07 17:59:25 +000056 * port selected bugfixes from post-0.49 busybox lash - done?
57 * finish implementing reserved words: for, while, until, do, done
58 * change { and } from special chars to reserved words
59 * builtins: break, continue, eval, return, set, trap, ulimit
60 * test magic exec
Eric Andersen25f27032001-04-26 23:22:31 +000061 * handle children going into background
62 * clean up recognition of null pipes
Eric Andersen25f27032001-04-26 23:22:31 +000063 * check setting of global_argc and global_argv
64 * control-C handling, probably with longjmp
Eric Andersen25f27032001-04-26 23:22:31 +000065 * follow IFS rules more precisely, including update semantics
Eric Andersen25f27032001-04-26 23:22:31 +000066 * figure out what to do with backslash-newline
67 * explain why we use signal instead of sigaction
68 * propagate syntax errors, die on resource errors?
69 * continuation lines, both explicit and implicit - done?
70 * memory leak finding and plugging - done?
71 * more testing, especially quoting rules and redirection
Eric Andersen78a7c992001-05-15 16:30:25 +000072 * document how quoting rules not precisely followed for variable assignments
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +000073 * maybe change charmap[] to use 2-bit entries
Eric Andersen25f27032001-04-26 23:22:31 +000074 * (eventually) remove all the printf's
Eric Andersen25f27032001-04-26 23:22:31 +000075 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000076 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen25f27032001-04-26 23:22:31 +000077 */
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000078
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000079
Eric Andersen25f27032001-04-26 23:22:31 +000080#include <glob.h> /* glob, of course */
Eric Andersen25f27032001-04-26 23:22:31 +000081/* #include <dmalloc.h> */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000082
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000083#include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000084
Denis Vlasenkod01ff132007-05-02 21:40:23 +000085
Denis Vlasenko05743d72008-02-10 12:10:08 +000086#if !BB_MMU && ENABLE_HUSH_TICK
87//#undef ENABLE_HUSH_TICK
88//#define ENABLE_HUSH_TICK 0
89#warning On NOMMU, hush command substitution is dangerous.
90#warning Dont use it for commands which produce lots of output.
91#warning For more info see shell/hush.c, generate_stream_from_list().
92#endif
93
94#if !BB_MMU && ENABLE_HUSH_JOB
95#undef ENABLE_HUSH_JOB
96#define ENABLE_HUSH_JOB 0
97#endif
98
99#if !ENABLE_HUSH_INTERACTIVE
100#undef ENABLE_FEATURE_EDITING
101#define ENABLE_FEATURE_EDITING 0
102#undef ENABLE_FEATURE_EDITING_FANCY_PROMPT
103#define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0
Denis Vlasenko8412d792007-10-01 09:59:47 +0000104#endif
105
106
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000107/* If you comment out one of these below, it will be #defined later
108 * to perform debug printfs to stderr: */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000109#define debug_printf(...) do {} while (0)
Denis Vlasenko400c5b62007-05-04 13:07:27 +0000110/* Finer-grained debug switches */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000111#define debug_printf_parse(...) do {} while (0)
112#define debug_print_tree(a, b) do {} while (0)
113#define debug_printf_exec(...) do {} while (0)
114#define debug_printf_jobs(...) do {} while (0)
115#define debug_printf_expand(...) do {} while (0)
116#define debug_printf_clean(...) do {} while (0)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000117
118#ifndef debug_printf
119#define debug_printf(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000120#endif
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000121
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000122#ifndef debug_printf_parse
123#define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000124#endif
125
126#ifndef debug_printf_exec
127#define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__)
128#endif
129
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000130#ifndef debug_printf_jobs
131#define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__)
132#define DEBUG_SHELL_JOBS 1
133#endif
134
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000135#ifndef debug_printf_expand
136#define debug_printf_expand(...) fprintf(stderr, __VA_ARGS__)
137#define DEBUG_EXPAND 1
138#endif
139
Denis Vlasenko170435c2007-05-23 13:01:10 +0000140/* Keep unconditionally on for now */
141#define ENABLE_HUSH_DEBUG 1
142
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000143#ifndef debug_printf_clean
144/* broken, of course, but OK for testing */
145static const char *indenter(int i)
146{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000147 static const char blanks[] ALIGN1 =
148 " ";
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000149 return &blanks[sizeof(blanks) - i - 1];
150}
151#define debug_printf_clean(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000152#define DEBUG_CLEAN 1
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000153#endif
154
Eric Andersen25f27032001-04-26 23:22:31 +0000155
Denis Vlasenkof962a032007-11-23 12:50:54 +0000156/*
157 * Leak hunting. Use hush_leaktool.sh for post-processing.
158 */
159#ifdef FOR_HUSH_LEAKTOOL
160void *xxmalloc(int lineno, size_t size)
161{
162 void *ptr = xmalloc((size + 0xff) & ~0xff);
163 fprintf(stderr, "line %d: malloc %p\n", lineno, ptr);
164 return ptr;
165}
166void *xxrealloc(int lineno, void *ptr, size_t size)
167{
168 ptr = xrealloc(ptr, (size + 0xff) & ~0xff);
169 fprintf(stderr, "line %d: realloc %p\n", lineno, ptr);
170 return ptr;
171}
172char *xxstrdup(int lineno, const char *str)
173{
174 char *ptr = xstrdup(str);
175 fprintf(stderr, "line %d: strdup %p\n", lineno, ptr);
176 return ptr;
177}
178void xxfree(void *ptr)
179{
180 fprintf(stderr, "free %p\n", ptr);
181 free(ptr);
182}
183#define xmalloc(s) xxmalloc(__LINE__, s)
184#define xrealloc(p, s) xxrealloc(__LINE__, p, s)
185#define xstrdup(s) xxstrdup(__LINE__, s)
186#define free(p) xxfree(p)
187#endif
188
189
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +0000190#define SPECIAL_VAR_SYMBOL 3
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000191
192#define PARSEFLAG_EXIT_FROM_LOOP 1
193#define PARSEFLAG_SEMICOLON (1 << 1) /* symbol ';' is special for parser */
194#define PARSEFLAG_REPARSING (1 << 2) /* >= 2nd pass */
Eric Andersen25f27032001-04-26 23:22:31 +0000195
196typedef enum {
197 REDIRECT_INPUT = 1,
198 REDIRECT_OVERWRITE = 2,
199 REDIRECT_APPEND = 3,
200 REDIRECT_HEREIS = 4,
201 REDIRECT_IO = 5
202} redir_type;
203
204/* The descrip member of this structure is only used to make debugging
205 * output pretty */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000206static const struct {
207 int mode;
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000208 signed char default_fd;
209 char descrip[3];
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000210} redir_table[] = {
Eric Andersen25f27032001-04-26 23:22:31 +0000211 { 0, 0, "()" },
212 { O_RDONLY, 0, "<" },
213 { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
214 { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" },
215 { O_RDONLY, -1, "<<" },
216 { O_RDWR, 1, "<>" }
217};
218
219typedef enum {
220 PIPE_SEQ = 1,
221 PIPE_AND = 2,
222 PIPE_OR = 3,
223 PIPE_BG = 4,
224} pipe_style;
225
226/* might eventually control execution */
227typedef enum {
228 RES_NONE = 0,
Denis Vlasenko06810332007-05-21 23:30:54 +0000229#if ENABLE_HUSH_IF
Eric Andersen25f27032001-04-26 23:22:31 +0000230 RES_IF = 1,
231 RES_THEN = 2,
232 RES_ELIF = 3,
233 RES_ELSE = 4,
234 RES_FI = 5,
Denis Vlasenko06810332007-05-21 23:30:54 +0000235#endif
236#if ENABLE_HUSH_LOOPS
Eric Andersen25f27032001-04-26 23:22:31 +0000237 RES_FOR = 6,
238 RES_WHILE = 7,
239 RES_UNTIL = 8,
240 RES_DO = 9,
241 RES_DONE = 10,
Denis Vlasenko06810332007-05-21 23:30:54 +0000242 RES_IN = 11,
243#endif
244 RES_XXXX = 12,
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000245 RES_SNTX = 13
Eric Andersen25f27032001-04-26 23:22:31 +0000246} reserved_style;
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000247enum {
248 FLAG_END = (1 << RES_NONE ),
Denis Vlasenko06810332007-05-21 23:30:54 +0000249#if ENABLE_HUSH_IF
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000250 FLAG_IF = (1 << RES_IF ),
251 FLAG_THEN = (1 << RES_THEN ),
252 FLAG_ELIF = (1 << RES_ELIF ),
253 FLAG_ELSE = (1 << RES_ELSE ),
254 FLAG_FI = (1 << RES_FI ),
Denis Vlasenko06810332007-05-21 23:30:54 +0000255#endif
256#if ENABLE_HUSH_LOOPS
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000257 FLAG_FOR = (1 << RES_FOR ),
258 FLAG_WHILE = (1 << RES_WHILE),
259 FLAG_UNTIL = (1 << RES_UNTIL),
260 FLAG_DO = (1 << RES_DO ),
261 FLAG_DONE = (1 << RES_DONE ),
262 FLAG_IN = (1 << RES_IN ),
Denis Vlasenko06810332007-05-21 23:30:54 +0000263#endif
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000264 FLAG_START = (1 << RES_XXXX ),
265};
Eric Andersen25f27032001-04-26 23:22:31 +0000266
267/* This holds pointers to the various results of parsing */
268struct p_context {
269 struct child_prog *child;
270 struct pipe *list_head;
271 struct pipe *pipe;
272 struct redir_struct *pending_redirect;
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000273 smallint res_w;
274 smallint parse_type; /* bitmask of PARSEFLAG_xxx, defines type of parser : ";$" common or special symbol */
275 int old_flag; /* bitmask of FLAG_xxx, for figuring out valid reserved words */
Eric Andersen25f27032001-04-26 23:22:31 +0000276 struct p_context *stack;
277 /* How about quoting status? */
278};
279
280struct redir_struct {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000281 struct redir_struct *next; /* pointer to the next redirect in the list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000282 redir_type type; /* type of redirection */
283 int fd; /* file descriptor being redirected */
284 int dup; /* -1, or file descriptor being duplicated */
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000285 char **glob_word; /* *word.gl_pathv is the filename */
Eric Andersen25f27032001-04-26 23:22:31 +0000286};
287
288struct child_prog {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000289 pid_t pid; /* 0 if exited */
290 char **argv; /* program name and arguments */
291 struct pipe *group; /* if non-NULL, first in group or subshell */
Denis Vlasenko262d7652007-05-20 21:52:49 +0000292 smallint subshell; /* flag, non-zero if group must be forked */
293 smallint is_stopped; /* is the program currently running? */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000294 struct redir_struct *redirects; /* I/O redirections */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000295 struct pipe *family; /* pointer back to the child's parent pipe */
Eric Andersen25f27032001-04-26 23:22:31 +0000296};
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000297/* argv vector may contain variable references (^Cvar^C, ^C0^C etc)
298 * and on execution these are substituted with their values.
299 * Substitution can make _several_ words out of one argv[n]!
300 * Example: argv[0]=='.^C*^C.' here: echo .$*.
301 */
Eric Andersen25f27032001-04-26 23:22:31 +0000302
303struct pipe {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000304 struct pipe *next;
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000305 int num_progs; /* total number of programs in job */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000306 int running_progs; /* number of programs running (not exited) */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000307 int stopped_progs; /* number of programs alive, but stopped */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000308#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000309 int jobid; /* job number */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000310 pid_t pgrp; /* process group ID for the job */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000311 char *cmdtext; /* name of job */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000312#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000313 char *cmdbuf; /* buffer various argv's point into */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000314 struct child_prog *progs; /* array of commands in pipe */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000315 int job_context; /* bitmask defining current context */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000316 smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
317 smallint res_word; /* needed for if, for, while, until... */
Eric Andersen25f27032001-04-26 23:22:31 +0000318};
319
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000320/* On program start, environ points to initial environment.
321 * putenv adds new pointers into it, unsetenv removes them.
322 * Neither of these (de)allocates the strings.
323 * setenv allocates new strings in malloc space and does putenv,
324 * and thus setenv is unusable (leaky) for shell's purposes */
325#define setenv(...) setenv_is_leaky_dont_use()
326struct variable {
327 struct variable *next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000328 char *varstr; /* points to "name=" portion */
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000329 int max_len; /* if > 0, name is part of initial env; else name is malloced */
330 smallint flg_export; /* putenv should be done on this var */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000331 smallint flg_read_only;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000332};
333
Eric Andersen25f27032001-04-26 23:22:31 +0000334typedef struct {
335 char *data;
336 int length;
337 int maxlen;
Denis Vlasenkoff097622007-10-01 10:00:45 +0000338 smallint o_quote;
339 smallint nonnull;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000340 smallint has_empty_slot;
Eric Andersen25f27032001-04-26 23:22:31 +0000341} o_string;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000342#define NULL_O_STRING { NULL }
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000343/* used for initialization: o_string foo = NULL_O_STRING; */
Eric Andersen25f27032001-04-26 23:22:31 +0000344
345/* I can almost use ordinary FILE *. Is open_memstream() universally
346 * available? Where is it documented? */
347struct in_str {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +0000348 const char *p;
349 /* eof_flag=1: last char in ->p is really an EOF */
350 char eof_flag; /* meaningless if ->p == NULL */
351 char peek_buf[2];
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000352#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000353 smallint promptme;
354 smallint promptmode; /* 0: PS1, 1: PS2 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000355#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000356 FILE *file;
357 int (*get) (struct in_str *);
358 int (*peek) (struct in_str *);
359};
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000360#define i_getch(input) ((input)->get(input))
361#define i_peek(input) ((input)->peek(input))
Eric Andersen25f27032001-04-26 23:22:31 +0000362
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000363enum {
364 CHAR_ORDINARY = 0,
365 CHAR_ORDINARY_IF_QUOTED = 1, /* example: *, # */
366 CHAR_IFS = 2, /* treated as ordinary if quoted */
367 CHAR_SPECIAL = 3, /* example: $ */
Eric Andersen25f27032001-04-26 23:22:31 +0000368};
369
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000370#define HUSH_VER_STR "0.02"
371
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000372/* "Globals" within this file */
373
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000374/* Sorted roughly by size (smaller offsets == smaller code) */
375struct globals {
376#if ENABLE_HUSH_INTERACTIVE
377 /* 'interactive_fd' is a fd# open to ctty, if we have one
378 * _AND_ if we decided to act interactively */
379 int interactive_fd;
380 const char *PS1;
381 const char *PS2;
382#endif
383#if ENABLE_FEATURE_EDITING
384 line_input_t *line_input_state;
385#endif
386#if ENABLE_HUSH_JOB
387 int run_list_level;
388 pid_t saved_task_pgrp;
389 pid_t saved_tty_pgrp;
390 int last_jobid;
391 struct pipe *job_list;
392 struct pipe *toplevel_list;
393 smallint ctrl_z_flag;
394#endif
395 smallint fake_mode;
396 /* these three support $?, $#, and $1 */
397 char **global_argv;
398 int global_argc;
399 int last_return_code;
400 const char *ifs;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000401 const char *cwd;
402 unsigned last_bg_pid;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +0000403 struct variable *top_var; /* = &shell_ver (set in main()) */
404 struct variable shell_ver;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000405#if ENABLE_FEATURE_SH_STANDALONE
406 struct nofork_save_area nofork_save;
407#endif
408#if ENABLE_HUSH_JOB
409 sigjmp_buf toplevel_jb;
410#endif
411 unsigned char charmap[256];
412 char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2];
413};
414
415#define G (*ptr_to_globals)
416
417#if !ENABLE_HUSH_INTERACTIVE
418enum { interactive_fd = 0 };
419#endif
420#if !ENABLE_HUSH_JOB
421enum { run_list_level = 0 };
422#endif
423
424#if ENABLE_HUSH_INTERACTIVE
425#define interactive_fd (G.interactive_fd )
426#define PS1 (G.PS1 )
427#define PS2 (G.PS2 )
428#endif
429#if ENABLE_FEATURE_EDITING
430#define line_input_state (G.line_input_state)
431#endif
432#if ENABLE_HUSH_JOB
433#define run_list_level (G.run_list_level )
434#define saved_task_pgrp (G.saved_task_pgrp )
435#define saved_tty_pgrp (G.saved_tty_pgrp )
436#define last_jobid (G.last_jobid )
437#define job_list (G.job_list )
438#define toplevel_list (G.toplevel_list )
439#define toplevel_jb (G.toplevel_jb )
440#define ctrl_z_flag (G.ctrl_z_flag )
441#endif /* JOB */
442#define global_argv (G.global_argv )
443#define global_argc (G.global_argc )
444#define last_return_code (G.last_return_code)
445#define ifs (G.ifs )
446#define fake_mode (G.fake_mode )
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000447#define cwd (G.cwd )
448#define last_bg_pid (G.last_bg_pid )
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000449#define top_var (G.top_var )
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000450#define shell_ver (G.shell_ver )
451#if ENABLE_FEATURE_SH_STANDALONE
452#define nofork_save (G.nofork_save )
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000453#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000454#if ENABLE_HUSH_JOB
455#define toplevel_jb (G.toplevel_jb )
456#endif
457#define charmap (G.charmap )
458#define user_input_buf (G.user_input_buf )
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000459#define INIT_G() do { \
460 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
461} while (0)
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000462
463
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000464#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
465
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000466#if 1
467/* Normal */
468static void syntax(const char *msg)
469{
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000470 /* Was using fancy stuff:
471 * (interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...)
472 * but it SEGVs. ?! Oh well... explicit temp ptr works around that */
473 void (*fp)(const char *s, ...);
474
475 fp = (interactive_fd ? bb_error_msg : bb_error_msg_and_die);
476 fp(msg ? "%s: %s" : "syntax error", "syntax error", msg);
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000477}
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000478
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000479#else
480/* Debug */
481static void syntax_lineno(int line)
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000482{
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000483 void (*fp)(const char *s, ...);
484
485 fp = (interactive_fd ? bb_error_msg : bb_error_msg_and_die);
486 fp("syntax error hush.c:%d", line);
Eric Andersen25f27032001-04-26 23:22:31 +0000487}
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000488#define syntax(str) syntax_lineno(__LINE__)
489#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000490
491/* Index of subroutines: */
Eric Andersen25f27032001-04-26 23:22:31 +0000492/* in_str manipulations: */
493static int static_get(struct in_str *i);
494static int static_peek(struct in_str *i);
495static int file_get(struct in_str *i);
496static int file_peek(struct in_str *i);
497static void setup_file_in_str(struct in_str *i, FILE *f);
498static void setup_string_in_str(struct in_str *i, const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000499/* "run" the final data structures: */
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000500#if !defined(DEBUG_CLEAN)
501#define free_pipe_list(head, indent) free_pipe_list(head)
502#define free_pipe(pi, indent) free_pipe(pi)
503#endif
Eric Andersenbf7df042001-05-23 22:18:35 +0000504static int free_pipe_list(struct pipe *head, int indent);
505static int free_pipe(struct pipe *pi, int indent);
Eric Andersen25f27032001-04-26 23:22:31 +0000506/* really run the final data structures: */
507static int setup_redirects(struct child_prog *prog, int squirrel[]);
Denis Vlasenko05743d72008-02-10 12:10:08 +0000508static int run_list(struct pipe *pi);
Denis Vlasenko76d50412008-06-10 16:19:39 +0000509#if BB_MMU
510#define pseudo_exec_argv(ptrs2free, argv) pseudo_exec_argv(argv)
511#define pseudo_exec(ptrs2free, child) pseudo_exec(child)
512#endif
513static void pseudo_exec_argv(char **ptrs2free, char **argv) ATTRIBUTE_NORETURN;
514static void pseudo_exec(char **ptrs2free, struct child_prog *child) ATTRIBUTE_NORETURN;
Denis Vlasenko05743d72008-02-10 12:10:08 +0000515static int run_pipe(struct pipe *pi);
Eric Andersen25f27032001-04-26 23:22:31 +0000516/* extended glob support: */
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000517static char **globhack(const char *src, char **strings);
Eric Andersen25f27032001-04-26 23:22:31 +0000518static int glob_needed(const char *s);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000519static int xglob(o_string *dest, char ***pglob);
Eric Andersen78a7c992001-05-15 16:30:25 +0000520/* variable assignment: */
Eric Andersen78a7c992001-05-15 16:30:25 +0000521static int is_assignment(const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000522/* data structure manipulation: */
523static int setup_redirect(struct p_context *ctx, int fd, redir_type style, struct in_str *input);
524static void initialize_context(struct p_context *ctx);
525static int done_word(o_string *dest, struct p_context *ctx);
526static int done_command(struct p_context *ctx);
527static int done_pipe(struct p_context *ctx, pipe_style type);
528/* primary string parsing: */
529static int redirect_dup_num(struct in_str *input);
530static int redirect_opt_num(o_string *o);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000531#if ENABLE_HUSH_TICK
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000532static int process_command_subs(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000533 struct in_str *input, const char *subst_end);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000534#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000535static int parse_group(o_string *dest, struct p_context *ctx, struct in_str *input, int ch);
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000536static const char *lookup_param(const char *src);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000537static int handle_dollar(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000538 struct in_str *input);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000539static 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 +0000540/* setup: */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000541static int parse_and_run_stream(struct in_str *inp, int parse_flag);
542static int parse_and_run_string(const char *s, int parse_flag);
543static int parse_and_run_file(FILE *f);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000544/* job management: */
Eric Andersenc798b072001-06-22 06:23:03 +0000545static int checkjobs(struct pipe* fg_pipe);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000546#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +0000547static int checkjobs_and_fg_shell(struct pipe* fg_pipe);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000548static void insert_bg_job(struct pipe *pi);
549static void remove_bg_job(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000550static void delete_finished_bg_job(struct pipe *pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000551#else
552int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */
553#endif
Eric Andersenf72f5622001-05-15 23:21:41 +0000554/* local variable support */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000555static char **expand_strvec_to_strvec(char **argv);
556/* used for eval */
557static char *expand_strvec_to_string(char **argv);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +0000558/* used for expansion of right hand of assignments */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000559static char *expand_string_to_string(const char *str);
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000560static struct variable *get_local_var(const char *name);
561static int set_local_var(char *str, int flg_export);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000562static void unset_local_var(const char *name);
Eric Andersen25f27032001-04-26 23:22:31 +0000563
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000564
565static char **add_strings_to_strings(int need_xstrdup, char **strings, char **add)
566{
567 int i;
568 unsigned count1;
569 unsigned count2;
570 char **v;
571
572 v = strings;
573 count1 = 0;
574 if (v) {
575 while (*v) {
576 count1++;
577 v++;
578 }
579 }
580 count2 = 0;
581 v = add;
582 while (*v) {
583 count2++;
584 v++;
585 }
586 v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*));
587 v[count1 + count2] = NULL;
588 i = count2;
589 while (--i >= 0)
590 v[count1 + i] = need_xstrdup ? xstrdup(add[i]) : add[i];
591 return v;
592}
593
594/* 'add' should be a malloced pointer */
595static char **add_string_to_strings(char **strings, char *add)
596{
597 char *v[2];
598
599 v[0] = add;
600 v[1] = NULL;
601
602 return add_strings_to_strings(0, strings, v);
603}
604
605static void free_strings(char **strings)
606{
607 if (strings) {
608 char **v = strings;
609 while (*v)
610 free(*v++);
611 free(strings);
612 }
613}
614
615
Denis Vlasenko76d50412008-06-10 16:19:39 +0000616#if !BB_MMU
617#define EXTRA_PTRS 5 /* 1 for NULL, 1 for args, 3 for paranoid reasons */
618static char **alloc_ptrs(char **argv)
619{
620 char **v = argv;
621 while (*v)
622 v++;
623 return xzalloc((v - argv + EXTRA_PTRS) * sizeof(v[0]));
624}
625#endif
626
627
Denis Vlasenko83506862007-11-23 13:11:42 +0000628/* Function prototypes for builtins */
629static int builtin_cd(char **argv);
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000630static int builtin_echo(char **argv);
Denis Vlasenko83506862007-11-23 13:11:42 +0000631static int builtin_eval(char **argv);
632static int builtin_exec(char **argv);
633static int builtin_exit(char **argv);
634static int builtin_export(char **argv);
635#if ENABLE_HUSH_JOB
636static int builtin_fg_bg(char **argv);
637static int builtin_jobs(char **argv);
638#endif
639#if ENABLE_HUSH_HELP
640static int builtin_help(char **argv);
641#endif
642static int builtin_pwd(char **argv);
643static int builtin_read(char **argv);
644static int builtin_test(char **argv);
645static int builtin_set(char **argv);
646static int builtin_shift(char **argv);
647static int builtin_source(char **argv);
648static int builtin_umask(char **argv);
649static int builtin_unset(char **argv);
650//static int builtin_not_written(char **argv);
651
Eric Andersen25f27032001-04-26 23:22:31 +0000652/* Table of built-in functions. They can be forked or not, depending on
653 * context: within pipes, they fork. As simple commands, they do not.
654 * When used in non-forking context, they can change global variables
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000655 * in the parent shell process. If forked, of course they cannot.
Eric Andersen25f27032001-04-26 23:22:31 +0000656 * For example, 'unset foo | whatever' will parse and run, but foo will
657 * still be set at the end. */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000658struct built_in_command {
659 const char *cmd; /* name */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000660 int (*function) (char **argv); /* function ptr */
Denis Vlasenko06810332007-05-21 23:30:54 +0000661#if ENABLE_HUSH_HELP
662 const char *descr; /* description */
663#define BLTIN(cmd, func, help) { cmd, func, help }
664#else
665#define BLTIN(cmd, func, help) { cmd, func }
666#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000667};
668
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000669/* For now, echo and test are unconditionally enabled.
670 * Maybe make it configurable? */
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +0000671static const struct built_in_command bltins[] = {
Denis Vlasenko83506862007-11-23 13:11:42 +0000672 BLTIN("[" , builtin_test, "Test condition"),
673 BLTIN("[[" , builtin_test, "Test condition"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000674#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000675 BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000676#endif
Denis Vlasenko06810332007-05-21 23:30:54 +0000677// BLTIN("break" , builtin_not_written, "Exit for, while or until loop"),
678 BLTIN("cd" , builtin_cd, "Change working directory"),
679// BLTIN("continue", builtin_not_written, "Continue for, while or until loop"),
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000680 BLTIN("echo" , builtin_echo, "Write strings to stdout"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000681 BLTIN("eval" , builtin_eval, "Construct and run shell command"),
682 BLTIN("exec" , builtin_exec, "Exec command, replacing this shell with the exec'd process"),
683 BLTIN("exit" , builtin_exit, "Exit from shell"),
684 BLTIN("export", builtin_export, "Set environment variable"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000685#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000686 BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"),
687 BLTIN("jobs" , builtin_jobs, "Lists the active jobs"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000688#endif
Denis Vlasenko06810332007-05-21 23:30:54 +0000689// TODO: remove pwd? we have it as an applet...
690 BLTIN("pwd" , builtin_pwd, "Print current directory"),
691 BLTIN("read" , builtin_read, "Input environment variable"),
692// BLTIN("return", builtin_not_written, "Return from a function"),
693 BLTIN("set" , builtin_set, "Set/unset shell local variables"),
694 BLTIN("shift" , builtin_shift, "Shift positional parameters"),
695// BLTIN("trap" , builtin_not_written, "Trap signals"),
Denis Vlasenko83506862007-11-23 13:11:42 +0000696 BLTIN("test" , builtin_test, "Test condition"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000697// BLTIN("ulimit", builtin_not_written, "Controls resource limits"),
698 BLTIN("umask" , builtin_umask, "Sets file creation mask"),
699 BLTIN("unset" , builtin_unset, "Unset environment variable"),
700 BLTIN("." , builtin_source, "Source-in and run commands in a file"),
701#if ENABLE_HUSH_HELP
702 BLTIN("help" , builtin_help, "List shell built-in commands"),
703#endif
704 BLTIN(NULL, NULL, NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000705};
706
Denis Vlasenko4830fc52008-05-25 21:50:55 +0000707/* Signals are grouped, we handle them in batches */
708static void set_misc_sighandler(void (*handler)(int))
709{
710 bb_signals(0
711 + (1 << SIGINT)
712 + (1 << SIGQUIT)
713 + (1 << SIGTERM)
714 , handler);
715}
716
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000717#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000718
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000719static void set_fatal_sighandler(void (*handler)(int))
720{
Denis Vlasenko25591c32008-02-16 22:58:56 +0000721 bb_signals(0
722 + (1 << SIGILL)
723 + (1 << SIGTRAP)
724 + (1 << SIGABRT)
725 + (1 << SIGFPE)
726 + (1 << SIGBUS)
727 + (1 << SIGSEGV)
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000728 /* bash 3.2 seems to handle these just like 'fatal' ones */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000729 + (1 << SIGHUP)
730 + (1 << SIGPIPE)
731 + (1 << SIGALRM)
732 , handler);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000733}
734static void set_jobctrl_sighandler(void (*handler)(int))
735{
Denis Vlasenko25591c32008-02-16 22:58:56 +0000736 bb_signals(0
737 + (1 << SIGTSTP)
738 + (1 << SIGTTIN)
739 + (1 << SIGTTOU)
740 , handler);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000741}
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000742/* SIGCHLD is special and handled separately */
743
744static void set_every_sighandler(void (*handler)(int))
745{
746 set_fatal_sighandler(handler);
747 set_jobctrl_sighandler(handler);
748 set_misc_sighandler(handler);
749 signal(SIGCHLD, handler);
750}
751
Denis Vlasenko68404f12008-03-17 09:00:54 +0000752static void handler_ctrl_c(int sig ATTRIBUTE_UNUSED)
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000753{
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000754 debug_printf_jobs("got sig %d\n", sig);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000755// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000756 siglongjmp(toplevel_jb, 1);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000757}
758
Denis Vlasenko68404f12008-03-17 09:00:54 +0000759static void handler_ctrl_z(int sig ATTRIBUTE_UNUSED)
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000760{
761 pid_t pid;
762
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000763 debug_printf_jobs("got tty sig %d in pid %d\n", sig, getpid());
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000764 pid = fork();
Denis Vlasenkoc2990322007-05-16 12:57:12 +0000765 if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000766 return;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000767 ctrl_z_flag = 1;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000768 if (!pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +0000769 if (ENABLE_HUSH_JOB)
770 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000771 setpgrp();
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000772 debug_printf_jobs("set pgrp for child %d ok\n", getpid());
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000773 set_every_sighandler(SIG_DFL);
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000774 raise(SIGTSTP); /* resend TSTP so that child will be stopped */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000775 debug_printf_jobs("returning in child\n");
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000776 /* return to nofork, it will eventually exit now,
777 * not return back to shell */
778 return;
779 }
780 /* parent */
781 /* finish filling up pipe info */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000782 toplevel_list->pgrp = pid; /* child is in its own pgrp */
783 toplevel_list->progs[0].pid = pid;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000784 /* parent needs to longjmp out of running nofork.
785 * we will "return" exitcode 0, with child put in background */
786// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000787 debug_printf_jobs("siglongjmp in parent\n");
788 siglongjmp(toplevel_jb, 1);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000789}
790
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000791/* Restores tty foreground process group, and exits.
792 * May be called as signal handler for fatal signal
793 * (will faithfully resend signal to itself, producing correct exit state)
794 * or called directly with -EXITCODE.
795 * We also call it if xfunc is exiting. */
796static void sigexit(int sig) ATTRIBUTE_NORETURN;
797static void sigexit(int sig)
798{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000799 /* Disable all signals: job control, SIGPIPE, etc. */
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000800 sigprocmask_allsigs(SIG_BLOCK);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000801
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000802 if (interactive_fd)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000803 tcsetpgrp(interactive_fd, saved_tty_pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000804
805 /* Not a signal, just exit */
806 if (sig <= 0)
807 _exit(- sig);
808
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000809 kill_myself_with_sig(sig); /* does not return */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000810}
811
812/* Restores tty foreground process group, and exits. */
813static void hush_exit(int exitcode) ATTRIBUTE_NORETURN;
814static void hush_exit(int exitcode)
815{
816 fflush(NULL); /* flush all streams */
817 sigexit(- (exitcode & 0xff));
818}
819
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000820#else /* !JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000821
822#define set_fatal_sighandler(handler) ((void)0)
823#define set_jobctrl_sighandler(handler) ((void)0)
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000824#define hush_exit(e) exit(e)
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000825
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000826#endif /* JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000827
828
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000829static const char *set_cwd(void)
830{
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000831 if (cwd == bb_msg_unknown)
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000832 cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000833 cwd = xrealloc_getcwd_or_warn((char *)cwd);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000834 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000835 cwd = bb_msg_unknown;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000836 return cwd;
837}
838
Denis Vlasenko83506862007-11-23 13:11:42 +0000839
840/* built-in 'test' handler */
841static int builtin_test(char **argv)
842{
843 int argc = 0;
844 while (*argv) {
845 argc++;
846 argv++;
847 }
848 return test_main(argc, argv - argc);
849}
850
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000851/* built-in 'test' handler */
852static int builtin_echo(char **argv)
853{
854 int argc = 0;
855 while (*argv) {
856 argc++;
857 argv++;
858 }
Denis Vlasenkofe5e23b2007-11-24 02:23:51 +0000859 return echo_main(argc, argv - argc);
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000860}
861
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000862/* built-in 'eval' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000863static int builtin_eval(char **argv)
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000864{
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000865 int rcode = EXIT_SUCCESS;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000866
Denis Vlasenko1359da62007-04-21 23:27:30 +0000867 if (argv[1]) {
Denis Vlasenko170435c2007-05-23 13:01:10 +0000868 char *str = expand_strvec_to_string(argv + 1);
869 parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP |
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000870 PARSEFLAG_SEMICOLON);
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000871 free(str);
872 rcode = last_return_code;
873 }
874 return rcode;
875}
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000876
Eric Andersen25f27032001-04-26 23:22:31 +0000877/* built-in 'cd <path>' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000878static int builtin_cd(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000879{
Denis Vlasenko170435c2007-05-23 13:01:10 +0000880 const char *newdir;
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000881 if (argv[1] == NULL) {
882 // bash does nothing (exitcode 0) if HOME is ""; if it's unset,
883 // bash says "bash: cd: HOME not set" and does nothing (exitcode 1)
Denis Vlasenko170435c2007-05-23 13:01:10 +0000884 newdir = getenv("HOME") ? : "/";
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000885 } else
Denis Vlasenko1359da62007-04-21 23:27:30 +0000886 newdir = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000887 if (chdir(newdir)) {
888 printf("cd: %s: %s\n", newdir, strerror(errno));
889 return EXIT_FAILURE;
890 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000891 set_cwd();
Eric Andersen25f27032001-04-26 23:22:31 +0000892 return EXIT_SUCCESS;
893}
894
Eric Andersen25f27032001-04-26 23:22:31 +0000895/* built-in 'exec' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000896static int builtin_exec(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000897{
Denis Vlasenko1359da62007-04-21 23:27:30 +0000898 if (argv[1] == NULL)
Denis Vlasenkof90ab182008-03-20 21:19:35 +0000899 return EXIT_SUCCESS; /* bash does this */
Denis Vlasenko76d50412008-06-10 16:19:39 +0000900 {
901#if !BB_MMU
902 char **ptrs2free = alloc_ptrs(argv);
903#endif
Denis Vlasenkof90ab182008-03-20 21:19:35 +0000904// FIXME: if exec fails, bash does NOT exit! We do...
Denis Vlasenko76d50412008-06-10 16:19:39 +0000905 pseudo_exec_argv(ptrs2free, argv + 1);
906 /* never returns */
907 }
Eric Andersen25f27032001-04-26 23:22:31 +0000908}
909
910/* built-in 'exit' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000911static int builtin_exit(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000912{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000913// TODO: bash does it ONLY on top-level sh exit (+interacive only?)
914 //puts("exit"); /* bash does it */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000915// TODO: warn if we have background jobs: "There are stopped jobs"
916// On second consecutive 'exit', exit anyway.
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000917
Denis Vlasenko1359da62007-04-21 23:27:30 +0000918 if (argv[1] == NULL)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000919 hush_exit(last_return_code);
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000920 /* mimic bash: exit 123abc == exit 255 + error msg */
921 xfunc_error_retval = 255;
922 /* bash: exit -2 == exit 254, no error msg */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000923 hush_exit(xatoi(argv[1]) & 0xff);
Eric Andersen25f27032001-04-26 23:22:31 +0000924}
925
926/* built-in 'export VAR=value' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000927static int builtin_export(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000928{
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000929 const char *value;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000930 char *name = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000931
Eric Andersenf72f5622001-05-15 23:21:41 +0000932 if (name == NULL) {
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000933 // TODO:
934 // ash emits: export VAR='VAL'
935 // bash: declare -x VAR="VAL"
936 // (both also escape as needed (quotes, $, etc))
937 char **e = environ;
938 if (e)
939 while (*e)
940 puts(*e++);
941 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000942 }
Eric Andersenf72f5622001-05-15 23:21:41 +0000943
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000944 value = strchr(name, '=');
945 if (!value) {
946 /* They are exporting something without a =VALUE */
947 struct variable *var;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000948
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000949 var = get_local_var(name);
950 if (var) {
951 var->flg_export = 1;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000952 putenv(var->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000953 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000954 /* bash does not return an error when trying to export
955 * an undefined variable. Do likewise. */
956 return EXIT_SUCCESS;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000957 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000958
959 set_local_var(xstrdup(name), 1);
960 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000961}
962
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000963#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +0000964/* built-in 'fg' and 'bg' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000965static int builtin_fg_bg(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000966{
Eric Andersen0fcd4472001-05-02 20:12:03 +0000967 int i, jobnum;
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000968 struct pipe *pi;
Eric Andersen25f27032001-04-26 23:22:31 +0000969
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000970 if (!interactive_fd)
Eric Andersenc798b072001-06-22 06:23:03 +0000971 return EXIT_FAILURE;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000972 /* If they gave us no args, assume they want the last backgrounded task */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000973 if (!argv[1]) {
Eric Andersenc798b072001-06-22 06:23:03 +0000974 for (pi = job_list; pi; pi = pi->next) {
975 if (pi->jobid == last_jobid) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000976 goto found;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000977 }
978 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000979 bb_error_msg("%s: no current job", argv[0]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000980 return EXIT_FAILURE;
981 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000982 if (sscanf(argv[1], "%%%d", &jobnum) != 1) {
983 bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000984 return EXIT_FAILURE;
985 }
986 for (pi = job_list; pi; pi = pi->next) {
987 if (pi->jobid == jobnum) {
988 goto found;
Eric Andersen25f27032001-04-26 23:22:31 +0000989 }
990 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000991 bb_error_msg("%s: %d: no such job", argv[0], jobnum);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000992 return EXIT_FAILURE;
993 found:
Denis Vlasenko52881e92007-04-21 13:42:52 +0000994 // TODO: bash prints a string representation
995 // of job being foregrounded (like "sleep 1 | cat")
Denis Vlasenko1359da62007-04-21 23:27:30 +0000996 if (*argv[0] == 'f') {
Eric Andersen028b65b2001-06-28 01:10:11 +0000997 /* Put the job into the foreground. */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000998 tcsetpgrp(interactive_fd, pi->pgrp);
Eric Andersen25f27032001-04-26 23:22:31 +0000999 }
1000
1001 /* Restart the processes in the job */
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001002 debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_progs, pi->pgrp);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001003 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001004 debug_printf_jobs("reviving pid %d\n", pi->progs[i].pid);
Eric Andersen0fcd4472001-05-02 20:12:03 +00001005 pi->progs[i].is_stopped = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001006 }
1007 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001008
Denis Vlasenkobb81c582007-01-30 22:32:09 +00001009 i = kill(- pi->pgrp, SIGCONT);
1010 if (i < 0) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +00001011 if (errno == ESRCH) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001012 delete_finished_bg_job(pi);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001013 return EXIT_SUCCESS;
Eric Andersen028b65b2001-06-28 01:10:11 +00001014 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001015 bb_perror_msg("kill (SIGCONT)");
Eric Andersen028b65b2001-06-28 01:10:11 +00001016 }
1017 }
Eric Andersen25f27032001-04-26 23:22:31 +00001018
Denis Vlasenko1359da62007-04-21 23:27:30 +00001019 if (*argv[0] == 'f') {
1020 remove_bg_job(pi);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001021 return checkjobs_and_fg_shell(pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001022 }
Eric Andersen25f27032001-04-26 23:22:31 +00001023 return EXIT_SUCCESS;
1024}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001025#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001026
1027/* built-in 'help' handler */
Denis Vlasenko06810332007-05-21 23:30:54 +00001028#if ENABLE_HUSH_HELP
Denis Vlasenko1359da62007-04-21 23:27:30 +00001029static int builtin_help(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +00001030{
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001031 const struct built_in_command *x;
Eric Andersen25f27032001-04-26 23:22:31 +00001032
1033 printf("\nBuilt-in commands:\n");
1034 printf("-------------------\n");
1035 for (x = bltins; x->cmd; x++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001036 printf("%s\t%s\n", x->cmd, x->descr);
1037 }
1038 printf("\n\n");
1039 return EXIT_SUCCESS;
1040}
Denis Vlasenko06810332007-05-21 23:30:54 +00001041#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001042
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001043#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +00001044/* built-in 'jobs' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001045static int builtin_jobs(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +00001046{
1047 struct pipe *job;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001048 const char *status_string;
Eric Andersen25f27032001-04-26 23:22:31 +00001049
Eric Andersenc798b072001-06-22 06:23:03 +00001050 for (job = job_list; job; job = job->next) {
Eric Andersen25f27032001-04-26 23:22:31 +00001051 if (job->running_progs == job->stopped_progs)
1052 status_string = "Stopped";
1053 else
1054 status_string = "Running";
Eric Andersen52a97ca2001-06-22 06:49:26 +00001055
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001056 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
Eric Andersen25f27032001-04-26 23:22:31 +00001057 }
1058 return EXIT_SUCCESS;
1059}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001060#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001061
Eric Andersen25f27032001-04-26 23:22:31 +00001062/* built-in 'pwd' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001063static int builtin_pwd(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +00001064{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001065 puts(set_cwd());
Eric Andersen25f27032001-04-26 23:22:31 +00001066 return EXIT_SUCCESS;
1067}
1068
1069/* built-in 'read VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001070static int builtin_read(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001071{
Denis Vlasenkod67cef22007-06-13 06:47:47 +00001072 char *string;
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001073 const char *name = argv[1] ? argv[1] : "REPLY";
Eric Andersen25f27032001-04-26 23:22:31 +00001074
Denis Vlasenko0b6c6a92008-03-24 00:04:42 +00001075 string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL);
Denis Vlasenkod67cef22007-06-13 06:47:47 +00001076 return set_local_var(string, 0);
Eric Andersen25f27032001-04-26 23:22:31 +00001077}
1078
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001079/* built-in 'set [VAR=value]' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001080static int builtin_set(char **argv)
Eric Andersenf72f5622001-05-15 23:21:41 +00001081{
Denis Vlasenko1359da62007-04-21 23:27:30 +00001082 char *temp = argv[1];
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001083 struct variable *e;
Eric Andersenf72f5622001-05-15 23:21:41 +00001084
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001085 if (temp == NULL)
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001086 for (e = top_var; e; e = e->next)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00001087 puts(e->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001088 else
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001089 set_local_var(xstrdup(temp), 0);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001090
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001091 return EXIT_SUCCESS;
Eric Andersenf72f5622001-05-15 23:21:41 +00001092}
1093
1094
Eric Andersen25f27032001-04-26 23:22:31 +00001095/* Built-in 'shift' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001096static int builtin_shift(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001097{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001098 int n = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001099 if (argv[1]) {
1100 n = atoi(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001101 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001102 if (n >= 0 && n < global_argc) {
Denis Vlasenko004baba2007-05-20 22:22:18 +00001103 global_argv[n] = global_argv[0];
Eric Andersen25f27032001-04-26 23:22:31 +00001104 global_argc -= n;
1105 global_argv += n;
1106 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00001107 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001108 return EXIT_FAILURE;
Eric Andersen25f27032001-04-26 23:22:31 +00001109}
1110
1111/* Built-in '.' handler (read-in and execute commands from file) */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001112static int builtin_source(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001113{
1114 FILE *input;
1115 int status;
1116
Denis Vlasenko1359da62007-04-21 23:27:30 +00001117 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +00001118 return EXIT_FAILURE;
1119
1120 /* XXX search through $PATH is missing */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001121 input = fopen(argv[1], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00001122 if (!input) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001123 bb_error_msg("cannot open '%s'", argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001124 return EXIT_FAILURE;
1125 }
Denis Vlasenkoa0898172007-10-01 09:59:01 +00001126 close_on_exec_on(fileno(input));
Eric Andersen25f27032001-04-26 23:22:31 +00001127
1128 /* Now run the file */
1129 /* XXX argv and argc are broken; need to save old global_argv
1130 * (pointer only is OK!) on this stack frame,
Denis Vlasenko1359da62007-04-21 23:27:30 +00001131 * set global_argv=argv+1, recurse, and restore. */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001132 status = parse_and_run_file(input);
Eric Andersen25f27032001-04-26 23:22:31 +00001133 fclose(input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001134 return status;
Eric Andersen25f27032001-04-26 23:22:31 +00001135}
1136
Denis Vlasenko1359da62007-04-21 23:27:30 +00001137static int builtin_umask(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001138{
Eric Andersen83a2ae22001-05-07 17:59:25 +00001139 mode_t new_umask;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001140 const char *arg = argv[1];
Eric Andersen83a2ae22001-05-07 17:59:25 +00001141 char *end;
1142 if (arg) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001143 new_umask = strtoul(arg, &end, 8);
1144 if (*end != '\0' || end == arg) {
Eric Andersen83a2ae22001-05-07 17:59:25 +00001145 return EXIT_FAILURE;
1146 }
1147 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001148 new_umask = umask(0);
1149 printf("%.3o\n", (unsigned) new_umask);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001150 }
1151 umask(new_umask);
1152 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00001153}
1154
1155/* built-in 'unset VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001156static int builtin_unset(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001157{
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001158 /* bash always returns true */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001159 unset_local_var(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001160 return EXIT_SUCCESS;
1161}
1162
Denis Vlasenko06810332007-05-21 23:30:54 +00001163//static int builtin_not_written(char **argv)
1164//{
1165// printf("builtin_%s not written\n", argv[0]);
1166// return EXIT_FAILURE;
1167//}
Eric Andersen83a2ae22001-05-07 17:59:25 +00001168
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001169/*
1170 * o_string support
1171 */
1172#define B_CHUNK (32 * sizeof(char*))
Eric Andersen25f27032001-04-26 23:22:31 +00001173
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001174static void o_reset(o_string *o)
Eric Andersen25f27032001-04-26 23:22:31 +00001175{
1176 o->length = 0;
1177 o->nonnull = 0;
Denis Vlasenkod65ea392007-10-01 10:02:25 +00001178 if (o->data)
1179 o->data[0] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00001180}
1181
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001182static void o_free(o_string *o)
Eric Andersen25f27032001-04-26 23:22:31 +00001183{
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001184 free(o->data);
Denis Vlasenkod65ea392007-10-01 10:02:25 +00001185 memset(o, 0, sizeof(*o));
Eric Andersen25f27032001-04-26 23:22:31 +00001186}
1187
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001188static void o_grow_by(o_string *o, int len)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001189{
1190 if (o->length + len > o->maxlen) {
1191 o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK);
1192 o->data = xrealloc(o->data, 1 + o->maxlen);
1193 }
1194}
1195
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001196static void o_addchr(o_string *o, int ch)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001197{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001198 debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o);
1199 o_grow_by(o, 1);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001200 o->data[o->length] = ch;
1201 o->length++;
1202 o->data[o->length] = '\0';
1203}
1204
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001205static void o_addstr(o_string *o, const char *str, int len)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001206{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001207 o_grow_by(o, len);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001208 memcpy(&o->data[o->length], str, len);
1209 o->length += len;
1210 o->data[o->length] = '\0';
1211}
1212
Eric Andersen25f27032001-04-26 23:22:31 +00001213/* My analysis of quoting semantics tells me that state information
1214 * is associated with a destination, not a source.
1215 */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001216static void o_addqchr(o_string *o, int ch, int quote)
Eric Andersen25f27032001-04-26 23:22:31 +00001217{
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00001218 if (quote && strchr("*?[\\", ch)) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001219 o_addchr(o, '\\');
Eric Andersen25f27032001-04-26 23:22:31 +00001220 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001221 o_addchr(o, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00001222}
1223
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001224static void o_addqstr(o_string *o, const char *str, int len, int quote)
1225{
1226 char ch;
1227 if (!quote || str[strcspn(str, "*?[\\")] == '\0') {
1228 o_addstr(o, str, len);
1229 return;
1230 }
1231 while (len) {
1232 ch = *str++;
1233 if (ch && strchr("*?[\\", ch)) {
1234 o_addchr(o, '\\');
1235 }
1236 o_addchr(o, ch);
1237 len--;
1238 }
1239}
1240
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001241/* A special kind of o_string for $VAR and `cmd` expansion.
1242 * It contains char* list[] at the beginning, which is grown in 16 element
1243 * increments. Actual string data starts at the next multiple of 16.
1244 * list[i] contains an INDEX (int!) into this string data.
1245 * It means that if list[] needs to grow, data needs to be moved higher up
1246 * but list[i]'s need not be modified.
1247 * NB: remembering how many list[i]'s you have there is crucial.
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001248 * o_finalize_list() operation post-processes this structure - calculates
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001249 * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well.
1250 */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001251static int o_save_ptr(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001252{
1253 char **list = (char**)o->data;
Denis Vlasenko895bea22008-06-10 18:06:24 +00001254 int string_start;
1255 int string_len;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001256
1257 if (!o->has_empty_slot) {
Denis Vlasenko895bea22008-06-10 18:06:24 +00001258 string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1259 string_len = o->length - string_start;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001260 if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */
Denis Vlasenko895bea22008-06-10 18:06:24 +00001261 //bb_error_msg("list[%d]=%d string_start=%d (growing)", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001262 /* list[n] points to string_start, make space for 16 more pointers */
1263 o->maxlen += 0x10 * sizeof(list[0]);
1264 o->data = xrealloc(o->data, o->maxlen + 1);
1265 list = (char**)o->data;
1266 memmove(list + n + 0x10, list + n, string_len);
1267 o->length += 0x10 * sizeof(list[0]);
1268 }
Denis Vlasenko895bea22008-06-10 18:06:24 +00001269 //else bb_error_msg("list[%d]=%d string_start=%d", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001270 } else {
1271 /* We have empty slot at list[n], reuse without growth */
Denis Vlasenko895bea22008-06-10 18:06:24 +00001272 string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */
1273 string_len = o->length - string_start;
1274 //bb_error_msg("list[%d]=%d string_start=%d (empty slot)", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001275 o->has_empty_slot = 0;
1276 }
1277 list[n] = (char*)string_len;
1278 return n + 1;
1279}
1280
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001281static int o_get_last_ptr(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001282{
1283 char **list = (char**)o->data;
1284 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1285
1286 return ((int)list[n-1]) + string_start;
1287}
1288
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001289static char **o_finalize_list(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001290{
1291 char **list = (char**)o->data;
1292 int string_start;
1293
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001294 o_save_ptr(o, n); /* force growth for list[n] if necessary */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001295 string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]);
1296 list[n] = NULL;
1297 while (n) {
1298 n--;
1299 list[n] = o->data + (int)list[n] + string_start;
1300 }
1301 return list;
1302}
1303
1304#ifdef DEBUG_EXPAND
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001305static void o_debug_list(const char *prefix, o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001306{
1307 char **list = (char**)o->data;
1308 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1309 int i = 0;
1310 fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n",
1311 prefix, list, n, string_start, o->length, o->maxlen);
1312 while (i < n) {
1313 fprintf(stderr, " list[%d]=%d '%s'\n", i, (int)list[i],
1314 o->data + (int)list[i] + string_start);
1315 i++;
1316 }
1317 if (n) {
1318 const char *p = o->data + (int)list[n] + string_start;
1319 fprintf(stderr, " total_sz:%d\n", (p + strlen(p) + 1) - o->data);
1320 }
1321}
1322#else
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001323#define o_debug_list(prefix, o, n) ((void)0)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001324#endif
1325
1326
1327/*
1328 * in_str support
1329 */
Eric Andersen25f27032001-04-26 23:22:31 +00001330static int static_get(struct in_str *i)
1331{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001332 int ch = *i->p++;
1333 if (ch == '\0') return EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001334 return ch;
1335}
1336
1337static int static_peek(struct in_str *i)
1338{
1339 return *i->p;
1340}
1341
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001342#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001343#if ENABLE_FEATURE_EDITING
Rob Landley88621d72006-08-29 19:41:06 +00001344static void cmdedit_set_initial_prompt(void)
Eric Andersen25f27032001-04-26 23:22:31 +00001345{
Denis Vlasenko38f63192007-01-22 09:03:07 +00001346#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001347 PS1 = NULL;
1348#else
1349 PS1 = getenv("PS1");
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001350 if (PS1 == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +00001351 PS1 = "\\w \\$ ";
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001352#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001353}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001354#endif /* EDITING */
Eric Andersen25f27032001-04-26 23:22:31 +00001355
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001356static const char* setup_prompt_string(int promptmode)
Eric Andersen25f27032001-04-26 23:22:31 +00001357{
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001358 const char *prompt_str;
1359 debug_printf("setup_prompt_string %d ", promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001360#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001361 /* Set up the prompt */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001362 if (promptmode == 0) { /* PS1 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001363 free((char*)PS1);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001364 PS1 = xasprintf("%s %c ", cwd, (geteuid() != 0) ? '$' : '#');
1365 prompt_str = PS1;
Eric Andersen25f27032001-04-26 23:22:31 +00001366 } else {
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001367 prompt_str = PS2;
Eric Andersen25f27032001-04-26 23:22:31 +00001368 }
1369#else
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001370 prompt_str = (promptmode == 0) ? PS1 : PS2;
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001371#endif
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001372 debug_printf("result '%s'\n", prompt_str);
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001373 return prompt_str;
Eric Andersen25f27032001-04-26 23:22:31 +00001374}
1375
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001376static void get_user_input(struct in_str *i)
Eric Andersen25f27032001-04-26 23:22:31 +00001377{
Denis Vlasenko0937be52007-04-28 16:47:08 +00001378 int r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001379 const char *prompt_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001380
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001381 prompt_str = setup_prompt_string(i->promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001382#if ENABLE_FEATURE_EDITING
Denis Vlasenko170435c2007-05-23 13:01:10 +00001383 /* Enable command line editing only while a command line
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001384 * is actually being read */
1385 do {
1386 r = read_line_input(prompt_str, user_input_buf, BUFSIZ-1, line_input_state);
1387 } while (r == 0); /* repeat if Ctrl-C */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001388 i->eof_flag = (r < 0);
1389 if (i->eof_flag) { /* EOF/error detected */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001390 user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */
1391 user_input_buf[1] = '\0';
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001392 }
Eric Andersen25f27032001-04-26 23:22:31 +00001393#else
1394 fputs(prompt_str, stdout);
1395 fflush(stdout);
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001396 user_input_buf[0] = r = fgetc(i->file);
1397 /*user_input_buf[1] = '\0'; - already is and never changed */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001398 i->eof_flag = (r == EOF);
Eric Andersen25f27032001-04-26 23:22:31 +00001399#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001400 i->p = user_input_buf;
Eric Andersen25f27032001-04-26 23:22:31 +00001401}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001402#endif /* INTERACTIVE */
Eric Andersen25f27032001-04-26 23:22:31 +00001403
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001404/* This is the magic location that prints prompts
Eric Andersen25f27032001-04-26 23:22:31 +00001405 * and gets data back from the user */
1406static int file_get(struct in_str *i)
1407{
1408 int ch;
1409
Eric Andersen25f27032001-04-26 23:22:31 +00001410 /* If there is data waiting, eat it up */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001411 if (i->p && *i->p) {
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001412#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001413 take_cached:
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001414#endif
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001415 ch = *i->p++;
1416 if (i->eof_flag && !*i->p)
1417 ch = EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001418 } else {
1419 /* need to double check i->file because we might be doing something
1420 * more complicated by now, like sourcing or substituting. */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001421#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001422 if (interactive_fd && i->promptme && i->file == stdin) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001423 do {
1424 get_user_input(i);
1425 } while (!*i->p); /* need non-empty line */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001426 i->promptmode = 1; /* PS2 */
1427 i->promptme = 0;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001428 goto take_cached;
Eric Andersen25f27032001-04-26 23:22:31 +00001429 }
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001430#endif
1431 ch = fgetc(i->file);
Eric Andersen25f27032001-04-26 23:22:31 +00001432 }
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001433 debug_printf("file_get: got a '%c' %d\n", ch, ch);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001434#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001435 if (ch == '\n')
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001436 i->promptme = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001437#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001438 return ch;
1439}
1440
1441/* All the callers guarantee this routine will never be
1442 * used right after a newline, so prompting is not needed.
1443 */
1444static int file_peek(struct in_str *i)
1445{
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001446 int ch;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001447 if (i->p && *i->p) {
1448 if (i->eof_flag && !i->p[1])
1449 return EOF;
1450 return *i->p;
Eric Andersen25f27032001-04-26 23:22:31 +00001451 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001452 ch = fgetc(i->file);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001453 i->eof_flag = (ch == EOF);
1454 i->peek_buf[0] = ch;
1455 i->peek_buf[1] = '\0';
1456 i->p = i->peek_buf;
1457 debug_printf("file_peek: got a '%c' %d\n", *i->p, *i->p);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001458 return ch;
Eric Andersen25f27032001-04-26 23:22:31 +00001459}
1460
1461static void setup_file_in_str(struct in_str *i, FILE *f)
1462{
1463 i->peek = file_peek;
1464 i->get = file_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001465#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001466 i->promptme = 1;
1467 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001468#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001469 i->file = f;
1470 i->p = NULL;
1471}
1472
1473static void setup_string_in_str(struct in_str *i, const char *s)
1474{
1475 i->peek = static_peek;
1476 i->get = static_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001477#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001478 i->promptme = 1;
1479 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001480#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001481 i->p = s;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001482 i->eof_flag = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001483}
1484
Eric Andersen25f27032001-04-26 23:22:31 +00001485/* squirrel != NULL means we squirrel away copies of stdin, stdout,
1486 * and stderr if they are redirected. */
1487static int setup_redirects(struct child_prog *prog, int squirrel[])
1488{
1489 int openfd, mode;
1490 struct redir_struct *redir;
1491
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001492 for (redir = prog->redirects; redir; redir = redir->next) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00001493 if (redir->dup == -1 && redir->glob_word == NULL) {
Eric Andersen817e73c2001-06-06 17:56:09 +00001494 /* something went wrong in the parse. Pretend it didn't happen */
1495 continue;
1496 }
Eric Andersen25f27032001-04-26 23:22:31 +00001497 if (redir->dup == -1) {
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001498 char *p;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001499 mode = redir_table[redir->type].mode;
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001500 p = expand_string_to_string(redir->glob_word[0]);
1501 openfd = open_or_warn(p, mode);
1502 free(p);
Eric Andersen25f27032001-04-26 23:22:31 +00001503 if (openfd < 0) {
1504 /* this could get lost if stderr has been redirected, but
1505 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersen25f27032001-04-26 23:22:31 +00001506 return 1;
1507 }
1508 } else {
1509 openfd = redir->dup;
1510 }
1511
1512 if (openfd != redir->fd) {
1513 if (squirrel && redir->fd < 3) {
1514 squirrel[redir->fd] = dup(redir->fd);
1515 }
Eric Andersen83a2ae22001-05-07 17:59:25 +00001516 if (openfd == -3) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001517 //close(openfd); // close(-3) ??!
Eric Andersen83a2ae22001-05-07 17:59:25 +00001518 } else {
1519 dup2(openfd, redir->fd);
Matt Kraaic616e532001-06-05 16:50:08 +00001520 if (redir->dup == -1)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001521 close(openfd);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001522 }
Eric Andersen25f27032001-04-26 23:22:31 +00001523 }
1524 }
1525 return 0;
1526}
1527
1528static void restore_redirects(int squirrel[])
1529{
1530 int i, fd;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001531 for (i = 0; i < 3; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001532 fd = squirrel[i];
1533 if (fd != -1) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00001534 /* We simply die on error */
1535 xmove_fd(fd, i);
Eric Andersen25f27032001-04-26 23:22:31 +00001536 }
1537 }
1538}
1539
Denis Vlasenko05743d72008-02-10 12:10:08 +00001540/* Called after [v]fork() in run_pipe(), or from builtin_exec().
Denis Vlasenko8412d792007-10-01 09:59:47 +00001541 * Never returns.
1542 * XXX no exit() here. If you don't exec, use _exit instead.
Eric Andersen94ac2442001-05-22 19:05:18 +00001543 * The at_exit handlers apparently confuse the calling process,
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001544 * in particular stdin handling. Not sure why? -- because of vfork! (vda) */
Denis Vlasenko76d50412008-06-10 16:19:39 +00001545static void pseudo_exec_argv(char **ptrs2free, char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001546{
Eric Andersen78a7c992001-05-15 16:30:25 +00001547 int i, rcode;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001548 char *p;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001549 const struct built_in_command *x;
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001550
Denis Vlasenko1359da62007-04-21 23:27:30 +00001551 for (i = 0; is_assignment(argv[i]); i++) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001552 debug_printf_exec("pid %d environment modification: %s\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001553 getpid(), argv[i]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001554 p = expand_string_to_string(argv[i]);
Denis Vlasenko76d50412008-06-10 16:19:39 +00001555#if !BB_MMU
1556 *ptrs2free++ = p;
1557#endif
Denis Vlasenko170435c2007-05-23 13:01:10 +00001558 putenv(p);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001559 }
1560 argv += i;
1561 /* If a variable is assigned in a forest, and nobody listens,
1562 * was it ever really set?
1563 */
Denis Vlasenko05743d72008-02-10 12:10:08 +00001564 if (!argv[0])
Denis Vlasenko1359da62007-04-21 23:27:30 +00001565 _exit(EXIT_SUCCESS);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001566
Denis Vlasenko170435c2007-05-23 13:01:10 +00001567 argv = expand_strvec_to_strvec(argv);
Denis Vlasenko76d50412008-06-10 16:19:39 +00001568#if !BB_MMU
1569 *ptrs2free++ = (char*) argv;
1570#endif
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001571
Denis Vlasenko1359da62007-04-21 23:27:30 +00001572 /*
1573 * Check if the command matches any of the builtins.
1574 * Depending on context, this might be redundant. But it's
1575 * easier to waste a few CPU cycles than it is to figure out
1576 * if this is one of those cases.
1577 */
1578 for (x = bltins; x->cmd; x++) {
1579 if (strcmp(argv[0], x->cmd) == 0) {
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001580 debug_printf_exec("running builtin '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001581 rcode = x->function(argv);
1582 fflush(stdout);
1583 _exit(rcode);
Eric Andersen94ac2442001-05-22 19:05:18 +00001584 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001585 }
Eric Andersen78a7c992001-05-15 16:30:25 +00001586
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001587 /* Check if the command matches any busybox applets */
Denis Vlasenko80d14be2007-04-10 23:03:30 +00001588#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001589 if (strchr(argv[0], '/') == NULL) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001590 int a = find_applet_by_name(argv[0]);
1591 if (a >= 0) {
1592 if (APPLET_IS_NOEXEC(a)) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001593 debug_printf_exec("running applet '%s'\n", argv[0]);
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001594// is it ok that run_applet_no_and_exit() does exit(), not _exit()?
1595 run_applet_no_and_exit(a, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001596 }
1597 /* re-exec ourselves with the new arguments */
1598 debug_printf_exec("re-execing applet '%s'\n", argv[0]);
Denis Vlasenkobdbbb7e2007-06-08 15:02:55 +00001599 execvp(bb_busybox_exec_path, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001600 /* If they called chroot or otherwise made the binary no longer
1601 * executable, fall through */
1602 }
1603 }
Eric Andersenaac75e52001-04-30 18:18:45 +00001604#endif
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001605
1606 debug_printf_exec("execing '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001607 execvp(argv[0], argv);
1608 bb_perror_msg("cannot exec '%s'", argv[0]);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001609 _exit(EXIT_FAILURE);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001610}
1611
Denis Vlasenko05743d72008-02-10 12:10:08 +00001612/* Called after [v]fork() in run_pipe()
Denis Vlasenko8412d792007-10-01 09:59:47 +00001613 */
Denis Vlasenko76d50412008-06-10 16:19:39 +00001614static void pseudo_exec(char **ptrs2free, struct child_prog *child)
Denis Vlasenko1359da62007-04-21 23:27:30 +00001615{
Denis Vlasenkof2fffd02007-05-02 23:39:04 +00001616// FIXME: buggy wrt NOMMU! Must not modify any global data
Denis Vlasenko05743d72008-02-10 12:10:08 +00001617// until it does exec/_exit, but currently it does
1618// (puts malloc'ed stuff into environment)
1619 if (child->argv)
Denis Vlasenko76d50412008-06-10 16:19:39 +00001620 pseudo_exec_argv(ptrs2free, child->argv);
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001621
1622 if (child->group) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001623#if !BB_MMU
Denis Vlasenko3b492162007-12-24 14:26:57 +00001624 bb_error_msg_and_die("nested lists are not supported on NOMMU");
Denis Vlasenko8412d792007-10-01 09:59:47 +00001625#else
Denis Vlasenko3b492162007-12-24 14:26:57 +00001626 int rcode;
1627
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001628#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko05743d72008-02-10 12:10:08 +00001629// run_list_level now takes care of it?
1630// debug_printf_exec("pseudo_exec: setting interactive_fd=0\n");
1631// interactive_fd = 0; /* crucial!!!! */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001632#endif
Denis Vlasenko05743d72008-02-10 12:10:08 +00001633 debug_printf_exec("pseudo_exec: run_list\n");
1634 rcode = run_list(child->group);
Eric Andersenbf7df042001-05-23 22:18:35 +00001635 /* OK to leak memory by not calling free_pipe_list,
Eric Andersen25f27032001-04-26 23:22:31 +00001636 * since this process is about to exit */
Eric Andersen94ac2442001-05-22 19:05:18 +00001637 _exit(rcode);
Denis Vlasenko8412d792007-10-01 09:59:47 +00001638#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001639 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001640
1641 /* Can happen. See what bash does with ">foo" by itself. */
1642 debug_printf("trying to pseudo_exec null command\n");
1643 _exit(EXIT_SUCCESS);
Eric Andersen25f27032001-04-26 23:22:31 +00001644}
1645
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001646#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001647static const char *get_cmdtext(struct pipe *pi)
1648{
1649 char **argv;
1650 char *p;
1651 int len;
1652
1653 /* This is subtle. ->cmdtext is created only on first backgrounding.
1654 * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...)
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001655 * On subsequent bg argv is trashed, but we won't use it */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001656 if (pi->cmdtext)
1657 return pi->cmdtext;
1658 argv = pi->progs[0].argv;
1659 if (!argv || !argv[0])
1660 return (pi->cmdtext = xzalloc(1));
1661
1662 len = 0;
1663 do len += strlen(*argv) + 1; while (*++argv);
1664 pi->cmdtext = p = xmalloc(len);
1665 argv = pi->progs[0].argv;
1666 do {
1667 len = strlen(*argv);
1668 memcpy(p, *argv, len);
1669 p += len;
1670 *p++ = ' ';
1671 } while (*++argv);
1672 p[-1] = '\0';
1673 return pi->cmdtext;
1674}
1675
Eric Andersenbafd94f2001-05-02 16:11:59 +00001676static void insert_bg_job(struct pipe *pi)
1677{
1678 struct pipe *thejob;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001679 int i;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001680
1681 /* Linear search for the ID of the job to use */
1682 pi->jobid = 1;
Eric Andersenc798b072001-06-22 06:23:03 +00001683 for (thejob = job_list; thejob; thejob = thejob->next)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001684 if (thejob->jobid >= pi->jobid)
1685 pi->jobid = thejob->jobid + 1;
1686
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001687 /* Add thejob to the list of running jobs */
Eric Andersenc798b072001-06-22 06:23:03 +00001688 if (!job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001689 thejob = job_list = xmalloc(sizeof(*thejob));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001690 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001691 for (thejob = job_list; thejob->next; thejob = thejob->next)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001692 continue;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001693 thejob->next = xmalloc(sizeof(*thejob));
1694 thejob = thejob->next;
1695 }
1696
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001697 /* Physically copy the struct job */
Eric Andersen0fcd4472001-05-02 20:12:03 +00001698 memcpy(thejob, pi, sizeof(struct pipe));
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001699 thejob->progs = xzalloc(sizeof(pi->progs[0]) * pi->num_progs);
1700 /* We cannot copy entire pi->progs[] vector! Double free()s will happen */
1701 for (i = 0; i < pi->num_progs; i++) {
1702// TODO: do we really need to have so many fields which are just dead weight
1703// at execution stage?
1704 thejob->progs[i].pid = pi->progs[i].pid;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001705 /* all other fields are not used and stay zero */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001706 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001707 thejob->next = NULL;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001708 thejob->cmdtext = xstrdup(get_cmdtext(pi));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001709
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001710 /* We don't wait for background thejobs to return -- append it
Eric Andersenbafd94f2001-05-02 16:11:59 +00001711 to the list of backgrounded thejobs and leave it alone */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001712 printf("[%d] %d %s\n", thejob->jobid, thejob->progs[0].pid, thejob->cmdtext);
Eric Andersen1a6d39b2001-05-08 05:11:54 +00001713 last_bg_pid = thejob->progs[0].pid;
Eric Andersenc798b072001-06-22 06:23:03 +00001714 last_jobid = thejob->jobid;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001715}
1716
Eric Andersenbafd94f2001-05-02 16:11:59 +00001717static void remove_bg_job(struct pipe *pi)
1718{
1719 struct pipe *prev_pipe;
1720
Eric Andersenc798b072001-06-22 06:23:03 +00001721 if (pi == job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001722 job_list = pi->next;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001723 } else {
Eric Andersenc798b072001-06-22 06:23:03 +00001724 prev_pipe = job_list;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001725 while (prev_pipe->next != pi)
1726 prev_pipe = prev_pipe->next;
1727 prev_pipe->next = pi->next;
1728 }
Eric Andersen028b65b2001-06-28 01:10:11 +00001729 if (job_list)
1730 last_jobid = job_list->jobid;
1731 else
1732 last_jobid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001733}
Eric Andersen028b65b2001-06-28 01:10:11 +00001734
Denis Vlasenko1359da62007-04-21 23:27:30 +00001735/* remove a backgrounded job */
1736static void delete_finished_bg_job(struct pipe *pi)
1737{
1738 remove_bg_job(pi);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001739 pi->stopped_progs = 0;
Eric Andersenbf7df042001-05-23 22:18:35 +00001740 free_pipe(pi, 0);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001741 free(pi);
1742}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001743#endif /* JOB */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001744
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001745/* Checks to see if any processes have exited -- if they
Eric Andersenbafd94f2001-05-02 16:11:59 +00001746 have, figure out why and see if a job has completed */
Eric Andersenc798b072001-06-22 06:23:03 +00001747static int checkjobs(struct pipe* fg_pipe)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001748{
Eric Andersenc798b072001-06-22 06:23:03 +00001749 int attributes;
1750 int status;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001751#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00001752 int prognum = 0;
1753 struct pipe *pi;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001754#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001755 pid_t childpid;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001756 int rcode = 0;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001757
Eric Andersenc798b072001-06-22 06:23:03 +00001758 attributes = WUNTRACED;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001759 if (fg_pipe == NULL) {
Eric Andersenc798b072001-06-22 06:23:03 +00001760 attributes |= WNOHANG;
1761 }
1762
Denis Vlasenko1359da62007-04-21 23:27:30 +00001763/* Do we do this right?
1764 * bash-3.00# sleep 20 | false
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001765 * <ctrl-Z pressed>
Denis Vlasenko1359da62007-04-21 23:27:30 +00001766 * [3]+ Stopped sleep 20 | false
1767 * bash-3.00# echo $?
1768 * 1 <========== bg pipe is not fully done, but exitcode is already known!
1769 */
1770
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001771//FIXME: non-interactive bash does not continue even if all processes in fg pipe
1772//are stopped. Testcase: "cat | cat" in a script (not on command line)
1773// + killall -STOP cat
1774
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001775 wait_more:
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001776// TODO: safe_waitpid?
Eric Andersenc798b072001-06-22 06:23:03 +00001777 while ((childpid = waitpid(-1, &status, attributes)) > 0) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001778 const int dead = WIFEXITED(status) || WIFSIGNALED(status);
1779
1780#ifdef DEBUG_SHELL_JOBS
1781 if (WIFSTOPPED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001782 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001783 childpid, WSTOPSIG(status), WEXITSTATUS(status));
1784 if (WIFSIGNALED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001785 debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001786 childpid, WTERMSIG(status), WEXITSTATUS(status));
1787 if (WIFEXITED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001788 debug_printf_jobs("pid %d exited, exitcode %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001789 childpid, WEXITSTATUS(status));
1790#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001791 /* Were we asked to wait for fg pipe? */
Eric Andersenc798b072001-06-22 06:23:03 +00001792 if (fg_pipe) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001793 int i;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001794 for (i = 0; i < fg_pipe->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001795 debug_printf_jobs("check pid %d\n", fg_pipe->progs[i].pid);
Eric Andersenc798b072001-06-22 06:23:03 +00001796 if (fg_pipe->progs[i].pid == childpid) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001797 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001798 if (dead) {
1799 fg_pipe->progs[i].pid = 0;
1800 fg_pipe->running_progs--;
Denis Vlasenko05743d72008-02-10 12:10:08 +00001801 if (i == fg_pipe->num_progs - 1)
Denis Vlasenko1359da62007-04-21 23:27:30 +00001802 /* last process gives overall exitstatus */
1803 rcode = WEXITSTATUS(status);
1804 } else {
1805 fg_pipe->progs[i].is_stopped = 1;
1806 fg_pipe->stopped_progs++;
1807 }
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001808 debug_printf_jobs("fg_pipe: running_progs %d stopped_progs %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001809 fg_pipe->running_progs, fg_pipe->stopped_progs);
1810 if (fg_pipe->running_progs - fg_pipe->stopped_progs <= 0) {
1811 /* All processes in fg pipe have exited/stopped */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001812#if ENABLE_HUSH_JOB
Denis Vlasenko1359da62007-04-21 23:27:30 +00001813 if (fg_pipe->running_progs)
1814 insert_bg_job(fg_pipe);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001815#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001816 return rcode;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001817 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001818 /* There are still running processes in the fg pipe */
1819 goto wait_more;
Eric Andersenc798b072001-06-22 06:23:03 +00001820 }
1821 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001822 /* fall through to searching process in bg pipes */
Eric Andersenc798b072001-06-22 06:23:03 +00001823 }
1824
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001825#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001826 /* We asked to wait for bg or orphaned children */
1827 /* No need to remember exitcode in this case */
Eric Andersenc798b072001-06-22 06:23:03 +00001828 for (pi = job_list; pi; pi = pi->next) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001829 prognum = 0;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001830 while (prognum < pi->num_progs) {
1831 if (pi->progs[prognum].pid == childpid)
1832 goto found_pi_and_prognum;
Eric Andersen52a97ca2001-06-22 06:49:26 +00001833 prognum++;
1834 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001835 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001836#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001837
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001838 /* Happens when shell is used as init process (init=/bin/sh) */
1839 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001840 goto wait_more;
Eric Andersenaeb44c42001-05-22 20:29:00 +00001841
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001842#if ENABLE_HUSH_JOB
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001843 found_pi_and_prognum:
Denis Vlasenko1359da62007-04-21 23:27:30 +00001844 if (dead) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001845 /* child exited */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001846 pi->progs[prognum].pid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001847 pi->running_progs--;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001848 if (!pi->running_progs) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001849 printf(JOB_STATUS_FORMAT, pi->jobid,
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001850 "Done", pi->cmdtext);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001851 delete_finished_bg_job(pi);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001852 }
1853 } else {
1854 /* child stopped */
1855 pi->stopped_progs++;
1856 pi->progs[prognum].is_stopped = 1;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001857 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001858#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001859 }
1860
Denis Vlasenko1359da62007-04-21 23:27:30 +00001861 /* wait found no children or failed */
1862
1863 if (childpid && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001864 bb_perror_msg("waitpid");
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001865 return rcode;
Eric Andersenada18ff2001-05-21 16:18:22 +00001866}
1867
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001868#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +00001869static int checkjobs_and_fg_shell(struct pipe* fg_pipe)
1870{
1871 pid_t p;
1872 int rcode = checkjobs(fg_pipe);
1873 /* Job finished, move the shell to the foreground */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001874 p = getpgid(0); /* pgid of our process */
1875 debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001876 if (tcsetpgrp(interactive_fd, p) && errno != ENOTTY)
1877 bb_perror_msg("tcsetpgrp-4a");
1878 return rcode;
1879}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001880#endif
Denis Vlasenko52881e92007-04-21 13:42:52 +00001881
Denis Vlasenko05743d72008-02-10 12:10:08 +00001882/* run_pipe() starts all the jobs, but doesn't wait for anything
Eric Andersenc798b072001-06-22 06:23:03 +00001883 * to finish. See checkjobs().
Eric Andersen25f27032001-04-26 23:22:31 +00001884 *
1885 * return code is normally -1, when the caller has to wait for children
1886 * to finish to determine the exit status of the pipe. If the pipe
1887 * is a simple builtin command, however, the action is done by the
Denis Vlasenko05743d72008-02-10 12:10:08 +00001888 * time run_pipe returns, and the exit code is provided as the
Eric Andersen25f27032001-04-26 23:22:31 +00001889 * return value.
1890 *
1891 * The input of the pipe is always stdin, the output is always
1892 * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
1893 * because it tries to avoid running the command substitution in
1894 * subshell, when that is in fact necessary. The subshell process
1895 * now has its stdout directed to the input of the appropriate pipe,
1896 * so this routine is noticeably simpler.
Denis Vlasenko170435c2007-05-23 13:01:10 +00001897 *
1898 * Returns -1 only if started some children. IOW: we have to
1899 * mask out retvals of builtins etc with 0xff!
Eric Andersen25f27032001-04-26 23:22:31 +00001900 */
Denis Vlasenko05743d72008-02-10 12:10:08 +00001901static int run_pipe(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00001902{
1903 int i;
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001904 int nextin;
1905 int pipefds[2]; /* pipefds[0] is for reading */
Rob Landley53702e52006-07-19 21:43:53 +00001906 struct child_prog *child;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001907 const struct built_in_command *x;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001908 char *p;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001909 /* it is not always needed, but we aim to smaller code */
1910 int squirrel[] = { -1, -1, -1 };
1911 int rcode;
Denis Vlasenko52881e92007-04-21 13:42:52 +00001912 const int single_fg = (pi->num_progs == 1 && pi->followup != PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00001913
Denis Vlasenko05743d72008-02-10 12:10:08 +00001914 debug_printf_exec("run_pipe start: single_fg=%d\n", single_fg);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001915
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001916#if ENABLE_HUSH_JOB
Eric Andersenada18ff2001-05-21 16:18:22 +00001917 pi->pgrp = -1;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001918#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001919 pi->running_progs = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001920 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001921
1922 /* Check if this is a simple builtin (not part of a pipe).
1923 * Builtins within pipes have to fork anyway, and are handled in
1924 * pseudo_exec. "echo foo | read bar" doesn't work on bash, either.
1925 */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001926 child = &(pi->progs[0]);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001927 if (single_fg && child->group && child->subshell == 0) {
Eric Andersen04407e52001-06-07 16:42:05 +00001928 debug_printf("non-subshell grouping\n");
1929 setup_redirects(child, squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001930 debug_printf_exec(": run_list\n");
1931 rcode = run_list(child->group) & 0xff;
Eric Andersen04407e52001-06-07 16:42:05 +00001932 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001933 debug_printf_exec("run_pipe return %d\n", rcode);
1934 return rcode;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001935 }
1936
Denis Vlasenko1359da62007-04-21 23:27:30 +00001937 if (single_fg && child->argv != NULL) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001938 char **argv_expanded;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001939 char **argv = child->argv;
1940
1941 for (i = 0; is_assignment(argv[i]); i++)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001942 continue;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001943 if (i != 0 && argv[i] == NULL) {
Eric Andersen78a7c992001-05-15 16:30:25 +00001944 /* assignments, but no command: set the local environment */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001945 for (i = 0; argv[i] != NULL; i++) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001946 debug_printf("local environment set: %s\n", argv[i]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001947 p = expand_string_to_string(argv[i]);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001948 set_local_var(p, 0);
Eric Andersen78a7c992001-05-15 16:30:25 +00001949 }
1950 return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */
1951 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001952 for (i = 0; is_assignment(argv[i]); i++) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00001953 p = expand_string_to_string(argv[i]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001954 putenv(p);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001955 }
Eric Andersen25f27032001-04-26 23:22:31 +00001956 for (x = bltins; x->cmd; x++) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001957 if (strcmp(argv[i], x->cmd) == 0) {
1958 if (x->function == builtin_exec && argv[i+1] == NULL) {
Eric Andersen83a2ae22001-05-07 17:59:25 +00001959 debug_printf("magic exec\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001960 setup_redirects(child, NULL);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001961 return EXIT_SUCCESS;
1962 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001963 debug_printf("builtin inline %s\n", argv[0]);
Eric Andersen25f27032001-04-26 23:22:31 +00001964 /* XXX setup_redirects acts on file descriptors, not FILEs.
1965 * This is perfect for work that comes after exec().
1966 * Is it really safe for inline use? Experimentally,
1967 * things seem to work with glibc. */
1968 setup_redirects(child, squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001969 debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv[i+1]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001970 argv_expanded = expand_strvec_to_strvec(argv + i);
1971 rcode = x->function(argv_expanded) & 0xff;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001972 free(argv_expanded);
Eric Andersen25f27032001-04-26 23:22:31 +00001973 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001974 debug_printf_exec("run_pipe return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00001975 return rcode;
1976 }
1977 }
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001978#if ENABLE_FEATURE_SH_STANDALONE
1979 {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001980 int a = find_applet_by_name(argv[i]);
1981 if (a >= 0 && APPLET_IS_NOFORK(a)) {
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001982 setup_redirects(child, squirrel);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001983 save_nofork_data(&nofork_save);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001984 argv_expanded = argv + i;
Denis Vlasenko170435c2007-05-23 13:01:10 +00001985 argv_expanded = expand_strvec_to_strvec(argv + i);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001986 debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]);
Denis Vlasenko7465dbc2008-04-13 02:25:53 +00001987 rcode = run_nofork_applet_prime(&nofork_save, a, argv_expanded);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001988 free(argv_expanded);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001989 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001990 debug_printf_exec("run_pipe return %d\n", rcode);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001991 return rcode;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001992 }
1993 }
1994#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001995 }
1996
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001997 /* Disable job control signals for shell (parent) and
1998 * for initial child code after fork */
1999 set_jobctrl_sighandler(SIG_IGN);
2000
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002001 /* Going to fork a child per each pipe member */
2002 pi->running_progs = 0;
2003 nextin = 0;
2004
Eric Andersen25f27032001-04-26 23:22:31 +00002005 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenko76d50412008-06-10 16:19:39 +00002006#if !BB_MMU
2007 char **ptrs2free = NULL;
2008#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002009 child = &(pi->progs[i]);
Denis Vlasenko76d50412008-06-10 16:19:39 +00002010 if (child->argv) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002011 debug_printf_exec(": pipe member '%s' '%s'...\n", child->argv[0], child->argv[1]);
Denis Vlasenko76d50412008-06-10 16:19:39 +00002012#if !BB_MMU
2013 ptrs2free = alloc_ptrs(child->argv);
2014#endif
2015 } else
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002016 debug_printf_exec(": pipe member with no argv\n");
Eric Andersen25f27032001-04-26 23:22:31 +00002017
2018 /* pipes are inserted between pairs of commands */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002019 pipefds[0] = 0;
2020 pipefds[1] = 1;
2021 if ((i + 1) < pi->num_progs)
2022 xpipe(pipefds);
Eric Andersen25f27032001-04-26 23:22:31 +00002023
Denis Vlasenko05743d72008-02-10 12:10:08 +00002024 child->pid = BB_MMU ? fork() : vfork();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002025 if (!child->pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00002026 if (ENABLE_HUSH_JOB)
2027 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002028#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002029 /* Every child adds itself to new process group
Denis Vlasenko05743d72008-02-10 12:10:08 +00002030 * with pgid == pid_of_first_child_in_pipe */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002031 if (run_list_level == 1 && interactive_fd) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00002032 pid_t pgrp;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002033 /* Don't do pgrp restore anymore on fatal signals */
2034 set_fatal_sighandler(SIG_DFL);
Denis Vlasenko05743d72008-02-10 12:10:08 +00002035 pgrp = pi->pgrp;
2036 if (pgrp < 0) /* true for 1st process only */
2037 pgrp = getpid();
2038 if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002039 /* We do it in *every* child, not just first,
2040 * to avoid races */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002041 tcsetpgrp(interactive_fd, pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002042 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002043 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002044#endif
Denis Vlasenko8412d792007-10-01 09:59:47 +00002045 xmove_fd(nextin, 0);
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002046 xmove_fd(pipefds[1], 1); /* write end */
2047 if (pipefds[0] > 1)
2048 close(pipefds[0]); /* read end */
Eric Andersen25f27032001-04-26 23:22:31 +00002049 /* Like bash, explicit redirects override pipes,
2050 * and the pipe fd is available for dup'ing. */
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002051 setup_redirects(child, NULL);
Eric Andersen52a97ca2001-06-22 06:49:26 +00002052
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002053 /* Restore default handlers just prior to exec */
2054 set_jobctrl_sighandler(SIG_DFL);
2055 set_misc_sighandler(SIG_DFL);
2056 signal(SIGCHLD, SIG_DFL);
Denis Vlasenko76d50412008-06-10 16:19:39 +00002057 pseudo_exec(ptrs2free, child); /* does not return */
Eric Andersen25f27032001-04-26 23:22:31 +00002058 }
Denis Vlasenko76d50412008-06-10 16:19:39 +00002059#if !BB_MMU
2060 free_strings(ptrs2free);
2061#endif
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002062 if (child->pid < 0) { /* [v]fork failed */
2063 /* Clearly indicate, was it fork or vfork */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002064 bb_perror_msg(BB_MMU ? "fork" : "vfork");
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002065 } else {
2066 pi->running_progs++;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002067#if ENABLE_HUSH_JOB
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002068 /* Second and next children need to know pid of first one */
2069 if (pi->pgrp < 0)
2070 pi->pgrp = child->pid;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002071#endif
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002072 }
Eric Andersen25f27032001-04-26 23:22:31 +00002073
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002074 if (i)
2075 close(nextin);
2076 if ((i + 1) < pi->num_progs)
2077 close(pipefds[1]); /* write end */
2078 /* Pass read (output) pipe end to next iteration */
Eric Andersen25f27032001-04-26 23:22:31 +00002079 nextin = pipefds[0];
2080 }
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002081
Denis Vlasenko05743d72008-02-10 12:10:08 +00002082 if (!pi->running_progs) {
2083 debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n");
2084 return 1;
2085 }
2086
2087 debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->running_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00002088 return -1;
2089}
2090
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002091#ifndef debug_print_tree
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002092static void debug_print_tree(struct pipe *pi, int lvl)
2093{
2094 static const char *PIPE[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002095 [PIPE_SEQ] = "SEQ",
2096 [PIPE_AND] = "AND",
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002097 [PIPE_OR ] = "OR" ,
2098 [PIPE_BG ] = "BG" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002099 };
2100 static const char *RES[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002101 [RES_NONE ] = "NONE" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00002102#if ENABLE_HUSH_IF
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002103 [RES_IF ] = "IF" ,
2104 [RES_THEN ] = "THEN" ,
2105 [RES_ELIF ] = "ELIF" ,
2106 [RES_ELSE ] = "ELSE" ,
2107 [RES_FI ] = "FI" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00002108#endif
2109#if ENABLE_HUSH_LOOPS
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002110 [RES_FOR ] = "FOR" ,
2111 [RES_WHILE] = "WHILE",
2112 [RES_UNTIL] = "UNTIL",
2113 [RES_DO ] = "DO" ,
2114 [RES_DONE ] = "DONE" ,
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002115 [RES_IN ] = "IN" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00002116#endif
2117 [RES_XXXX ] = "XXXX" ,
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002118 [RES_SNTX ] = "SNTX" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002119 };
2120
2121 int pin, prn;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002122
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002123 pin = 0;
2124 while (pi) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002125 fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "",
2126 pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002127 prn = 0;
2128 while (prn < pi->num_progs) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002129 struct child_prog *child = &pi->progs[prn];
2130 char **argv = child->argv;
2131
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002132 fprintf(stderr, "%*s prog %d", lvl*2, "", prn);
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002133 if (child->group) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002134 fprintf(stderr, " group %s: (argv=%p)\n",
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002135 (child->subshell ? "()" : "{}"),
2136 argv);
2137 debug_print_tree(child->group, lvl+1);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002138 prn++;
2139 continue;
2140 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002141 if (argv) while (*argv) {
2142 fprintf(stderr, " '%s'", *argv);
2143 argv++;
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002144 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002145 fprintf(stderr, "\n");
2146 prn++;
2147 }
2148 pi = pi->next;
2149 pin++;
2150 }
2151}
2152#endif
2153
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002154/* NB: called by pseudo_exec, and therefore must not modify any
2155 * global data until exec/_exit (we can be a child after vfork!) */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002156static int run_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002157{
Denis Vlasenko06810332007-05-21 23:30:54 +00002158 struct pipe *rpipe;
2159#if ENABLE_HUSH_LOOPS
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002160 char *for_varname = NULL;
2161 char **for_lcur = NULL;
2162 char **for_list = NULL;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002163 int flag_rep = 0;
Denis Vlasenko06810332007-05-21 23:30:54 +00002164#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002165 int flag_skip = 1;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002166 int rcode = 0; /* probably for gcc only */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002167 int flag_restore = 0;
Denis Vlasenko06810332007-05-21 23:30:54 +00002168#if ENABLE_HUSH_IF
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002169 int if_code = 0, next_if_code = 0; /* need double-buffer to handle elif */
Denis Vlasenko06810332007-05-21 23:30:54 +00002170#else
2171 enum { if_code = 0, next_if_code = 0 };
2172#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002173 reserved_style rword;
2174 reserved_style skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002175
Denis Vlasenko05743d72008-02-10 12:10:08 +00002176 debug_printf_exec("run_list start lvl %d\n", run_list_level + 1);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002177
Denis Vlasenko06810332007-05-21 23:30:54 +00002178#if ENABLE_HUSH_LOOPS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002179 /* check syntax for "for" */
2180 for (rpipe = pi; rpipe; rpipe = rpipe->next) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002181 if ((rpipe->res_word == RES_IN || rpipe->res_word == RES_FOR)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002182 && (rpipe->next == NULL)
2183 ) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00002184 syntax("malformed for"); /* no IN or no commands after IN */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002185 debug_printf_exec("run_list lvl %d return 1\n", run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002186 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002187 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002188 if ((rpipe->res_word == RES_IN && rpipe->next->res_word == RES_IN && rpipe->next->progs[0].argv != NULL)
2189 || (rpipe->res_word == RES_FOR && rpipe->next->res_word != RES_IN)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002190 ) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002191 /* TODO: what is tested in the first condition? */
Denis Vlasenko90e485c2007-05-23 15:22:50 +00002192 syntax("malformed for"); /* 2nd condition: not followed by IN */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002193 debug_printf_exec("run_list lvl %d return 1\n", run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002194 return 1;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002195 }
2196 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002197#else
2198 rpipe = NULL;
2199#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002200
2201#if ENABLE_HUSH_JOB
2202 /* Example of nested list: "while true; do { sleep 1 | exit 2; } done".
2203 * We are saving state before entering outermost list ("while...done")
2204 * so that ctrl-Z will correctly background _entire_ outermost list,
2205 * not just a part of it (like "sleep 1 | exit 2") */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002206 if (++run_list_level == 1 && interactive_fd) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002207 if (sigsetjmp(toplevel_jb, 1)) {
2208 /* ctrl-Z forked and we are parent; or ctrl-C.
2209 * Sighandler has longjmped us here */
2210 signal(SIGINT, SIG_IGN);
2211 signal(SIGTSTP, SIG_IGN);
2212 /* Restore level (we can be coming from deep inside
2213 * nested levels) */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002214 run_list_level = 1;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002215#if ENABLE_FEATURE_SH_STANDALONE
2216 if (nofork_save.saved) { /* if save area is valid */
2217 debug_printf_jobs("exiting nofork early\n");
2218 restore_nofork_data(&nofork_save);
2219 }
2220#endif
2221 if (ctrl_z_flag) {
2222 /* ctrl-Z has forked and stored pid of the child in pi->pid.
2223 * Remember this child as background job */
2224 insert_bg_job(pi);
2225 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002226 /* ctrl-C. We just stop doing whatever we were doing */
Denis Vlasenko4daad902007-09-27 10:20:47 +00002227 bb_putchar('\n');
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002228 }
2229 rcode = 0;
2230 goto ret;
2231 }
2232 /* ctrl-Z handler will store pid etc in pi */
2233 toplevel_list = pi;
2234 ctrl_z_flag = 0;
2235#if ENABLE_FEATURE_SH_STANDALONE
2236 nofork_save.saved = 0; /* in case we will run a nofork later */
2237#endif
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +00002238 signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002239 signal(SIGINT, handler_ctrl_c);
2240 }
Denis Vlasenko05743d72008-02-10 12:10:08 +00002241#endif /* JOB */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002242
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002243 for (; pi; pi = flag_restore ? rpipe : pi->next) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00002244//why? int save_num_progs;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002245 rword = pi->res_word;
Denis Vlasenko06810332007-05-21 23:30:54 +00002246#if ENABLE_HUSH_LOOPS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002247 if (rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002248 flag_restore = 0;
2249 if (!rpipe) {
2250 flag_rep = 0;
2251 rpipe = pi;
2252 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002253 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002254#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002255 debug_printf_exec(": rword=%d if_code=%d next_if_code=%d skip_more=%d\n",
2256 rword, if_code, next_if_code, skip_more_for_this_rword);
2257 if (rword == skip_more_for_this_rword && flag_skip) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002258 if (pi->followup == PIPE_SEQ)
2259 flag_skip = 0;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002260 continue;
2261 }
2262 flag_skip = 1;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002263 skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko06810332007-05-21 23:30:54 +00002264#if ENABLE_HUSH_IF
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002265 if (rword == RES_THEN || rword == RES_ELSE)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002266 if_code = next_if_code;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002267 if (rword == RES_THEN && if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002268 continue;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002269 if (rword == RES_ELSE && !if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002270 continue;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002271 if (rword == RES_ELIF && !if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002272 break;
Denis Vlasenko06810332007-05-21 23:30:54 +00002273#endif
2274#if ENABLE_HUSH_LOOPS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002275 if (rword == RES_FOR && pi->num_progs) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002276 if (!for_lcur) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002277 /* first loop through for */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002278 /* if no variable values after "in" we skip "for" */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002279 if (!pi->next->progs->argv)
2280 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002281 /* create list of variable values */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002282 for_list = expand_strvec_to_strvec(pi->next->progs->argv);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002283 for_lcur = for_list;
2284 for_varname = pi->progs->argv[0];
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002285 pi->progs->argv[0] = NULL;
2286 flag_rep = 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002287 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002288 free(pi->progs->argv[0]);
2289 if (!*for_lcur) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002290 /* for loop is over, clean up */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002291 free(for_list);
2292 for_lcur = NULL;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002293 flag_rep = 0;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002294 pi->progs->argv[0] = for_varname;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002295 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002296 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002297 /* insert next value from for_lcur */
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002298 /* vda: does it need escaping? */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002299 pi->progs->argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002300 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002301 if (rword == RES_IN)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002302 continue;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002303 if (rword == RES_DO) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002304 if (!flag_rep)
2305 continue;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002306 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002307 if (rword == RES_DONE) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002308 if (flag_rep) {
2309 flag_restore = 1;
2310 } else {
2311 rpipe = NULL;
2312 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002313 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002314#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002315 if (pi->num_progs == 0)
2316 continue;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002317//why? save_num_progs = pi->num_progs;
2318 debug_printf_exec(": run_pipe with %d members\n", pi->num_progs);
2319 rcode = run_pipe(pi);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002320 if (rcode != -1) {
Eric Andersen25f27032001-04-26 23:22:31 +00002321 /* We only ran a builtin: rcode was set by the return value
Denis Vlasenko05743d72008-02-10 12:10:08 +00002322 * of run_pipe(), and we don't need to wait for anything. */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002323 } else if (pi->followup == PIPE_BG) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002324 /* What does bash do with attempts to background builtins? */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002325 /* Even bash 3.2 doesn't do that well with nested bg:
2326 * try "{ { sleep 10; echo DEEP; } & echo HERE; } &".
Denis Vlasenko170435c2007-05-23 13:01:10 +00002327 * I'm NOT treating inner &'s as jobs */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002328#if ENABLE_HUSH_JOB
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002329 if (run_list_level == 1)
Denis Vlasenko170435c2007-05-23 13:01:10 +00002330 insert_bg_job(pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002331#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002332 rcode = EXIT_SUCCESS;
2333 } else {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002334#if ENABLE_HUSH_JOB
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002335 if (run_list_level == 1 && interactive_fd) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00002336 /* waits for completion, then fg's main shell */
Denis Vlasenko52881e92007-04-21 13:42:52 +00002337 rcode = checkjobs_and_fg_shell(pi);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002338 } else
2339#endif
2340 {
Denis Vlasenko170435c2007-05-23 13:01:10 +00002341 /* this one just waits for completion */
Eric Andersenc798b072001-06-22 06:23:03 +00002342 rcode = checkjobs(pi);
Eric Andersen25f27032001-04-26 23:22:31 +00002343 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002344 debug_printf_exec(": checkjobs returned %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002345 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002346 debug_printf_exec(": setting last_return_code=%d\n", rcode);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002347 last_return_code = rcode;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002348//why? pi->num_progs = save_num_progs;
Denis Vlasenko06810332007-05-21 23:30:54 +00002349#if ENABLE_HUSH_IF
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002350 if (rword == RES_IF || rword == RES_ELIF)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002351 next_if_code = rcode; /* can be overwritten a number of times */
Denis Vlasenko06810332007-05-21 23:30:54 +00002352#endif
2353#if ENABLE_HUSH_LOOPS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002354 if (rword == RES_WHILE)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002355 flag_rep = !last_return_code;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002356 if (rword == RES_UNTIL)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002357 flag_rep = last_return_code;
Denis Vlasenko06810332007-05-21 23:30:54 +00002358#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002359 if ((rcode == EXIT_SUCCESS && pi->followup == PIPE_OR)
2360 || (rcode != EXIT_SUCCESS && pi->followup == PIPE_AND)
2361 ) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002362 skip_more_for_this_rword = rword;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002363 }
Eric Andersen028b65b2001-06-28 01:10:11 +00002364 checkjobs(NULL);
Eric Andersen25f27032001-04-26 23:22:31 +00002365 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002366
2367#if ENABLE_HUSH_JOB
2368 if (ctrl_z_flag) {
2369 /* ctrl-Z forked somewhere in the past, we are the child,
2370 * and now we completed running the list. Exit. */
2371 exit(rcode);
2372 }
2373 ret:
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00002374 if (!--run_list_level && interactive_fd) {
2375 signal(SIGTSTP, SIG_IGN);
2376 signal(SIGINT, SIG_IGN);
2377 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002378#endif
Denis Vlasenko05743d72008-02-10 12:10:08 +00002379 debug_printf_exec("run_list lvl %d return %d\n", run_list_level + 1, rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002380 return rcode;
2381}
2382
Eric Andersen25f27032001-04-26 23:22:31 +00002383/* return code is the exit status of the pipe */
Eric Andersenbf7df042001-05-23 22:18:35 +00002384static int free_pipe(struct pipe *pi, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002385{
2386 char **p;
2387 struct child_prog *child;
2388 struct redir_struct *r, *rnext;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002389 int a, i, ret_code = 0;
Eric Andersen52a97ca2001-06-22 06:49:26 +00002390
2391 if (pi->stopped_progs > 0)
2392 return ret_code;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002393 debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid());
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002394 for (i = 0; i < pi->num_progs; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002395 child = &pi->progs[i];
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002396 debug_printf_clean("%s command %d:\n", indenter(indent), i);
Eric Andersen25f27032001-04-26 23:22:31 +00002397 if (child->argv) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002398 for (a = 0, p = child->argv; *p; a++, p++) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002399 debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p);
Eric Andersen25f27032001-04-26 23:22:31 +00002400 }
Denis Vlasenkof962a032007-11-23 12:50:54 +00002401 free_strings(child->argv);
2402 child->argv = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002403 } else if (child->group) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002404 debug_printf_clean("%s begin group (subshell:%d)\n", indenter(indent), child->subshell);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002405 ret_code = free_pipe_list(child->group, indent+3);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002406 debug_printf_clean("%s end group\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002407 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002408 debug_printf_clean("%s (nil)\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002409 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002410 for (r = child->redirects; r; r = rnext) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002411 debug_printf_clean("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->type].descrip);
Eric Andersen25f27032001-04-26 23:22:31 +00002412 if (r->dup == -1) {
Eric Andersen817e73c2001-06-06 17:56:09 +00002413 /* guard against the case >$FOO, where foo is unset or blank */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002414 if (r->glob_word) {
2415 debug_printf_clean(" %s\n", r->glob_word[0]);
2416 free_strings(r->glob_word);
2417 r->glob_word = NULL;
Eric Andersen817e73c2001-06-06 17:56:09 +00002418 }
Eric Andersen25f27032001-04-26 23:22:31 +00002419 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002420 debug_printf_clean("&%d\n", r->dup);
Eric Andersen25f27032001-04-26 23:22:31 +00002421 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002422 rnext = r->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002423 free(r);
2424 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002425 child->redirects = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002426 }
2427 free(pi->progs); /* children are an array, they get freed all at once */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002428 pi->progs = NULL;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002429#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002430 free(pi->cmdtext);
2431 pi->cmdtext = NULL;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002432#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002433 return ret_code;
2434}
2435
Eric Andersenbf7df042001-05-23 22:18:35 +00002436static int free_pipe_list(struct pipe *head, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002437{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002438 int rcode = 0; /* if list has no members */
Eric Andersen25f27032001-04-26 23:22:31 +00002439 struct pipe *pi, *next;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002440
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002441 for (pi = head; pi; pi = next) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002442 debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word);
Eric Andersenbf7df042001-05-23 22:18:35 +00002443 rcode = free_pipe(pi, indent);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002444 debug_printf_clean("%s pipe followup code %d\n", indenter(indent), pi->followup);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002445 next = pi->next;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002446 /*pi->next = NULL;*/
Eric Andersen25f27032001-04-26 23:22:31 +00002447 free(pi);
2448 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002449 return rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00002450}
2451
2452/* Select which version we will use */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002453static int run_and_free_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002454{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002455 int rcode = 0;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002456 debug_printf_exec("run_and_free_list entered\n");
2457 if (!fake_mode) {
2458 debug_printf_exec(": run_list with %d members\n", pi->num_progs);
2459 rcode = run_list(pi);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002460 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002461 /* free_pipe_list has the side effect of clearing memory.
Denis Vlasenko05743d72008-02-10 12:10:08 +00002462 * In the long run that function can be merged with run_list,
Eric Andersen25f27032001-04-26 23:22:31 +00002463 * but doing that now would hobble the debugging effort. */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002464 free_pipe_list(pi, /* indent: */ 0);
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002465 debug_printf_exec("run_and_free_list return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002466 return rcode;
2467}
2468
Denis Vlasenkoff097622007-10-01 10:00:45 +00002469/* Whoever decided to muck with glob internal data is AN IDIOT! */
2470/* uclibc happily changed the way it works (and it has rights to do so!),
2471 all hell broke loose (SEGVs) */
2472
Eric Andersen25f27032001-04-26 23:22:31 +00002473/* The API for glob is arguably broken. This routine pushes a non-matching
2474 * string into the output structure, removing non-backslashed backslashes.
2475 * If someone can prove me wrong, by performing this function within the
2476 * original glob(3) api, feel free to rewrite this routine into oblivion.
Eric Andersen25f27032001-04-26 23:22:31 +00002477 * XXX broken if the last character is '\\', check that before calling.
2478 */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002479static char **globhack(const char *src, char **strings)
Eric Andersen25f27032001-04-26 23:22:31 +00002480{
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002481 int cnt;
Eric Andersen25f27032001-04-26 23:22:31 +00002482 const char *s;
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002483 char *v, *dest;
2484
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002485 for (cnt = 1, s = src; s && *s; s++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002486 if (*s == '\\') s++;
2487 cnt++;
2488 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002489 v = dest = xmalloc(cnt);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002490 for (s = src; s && *s; s++, dest++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002491 if (*s == '\\') s++;
2492 *dest = *s;
2493 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002494 *dest = '\0';
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002495
2496 return add_string_to_strings(strings, v);
Eric Andersen25f27032001-04-26 23:22:31 +00002497}
2498
2499/* XXX broken if the last character is '\\', check that before calling */
2500static int glob_needed(const char *s)
2501{
2502 for (; *s; s++) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002503 if (*s == '\\')
2504 s++;
2505 if (strchr("*[?", *s))
2506 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002507 }
2508 return 0;
2509}
2510
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002511static int xglob(o_string *dest, char ***pglob)
Eric Andersen25f27032001-04-26 23:22:31 +00002512{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002513 /* short-circuit for null word */
Eric Andersen25f27032001-04-26 23:22:31 +00002514 /* we can code this better when the debug_printf's are gone */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002515 if (dest->length == 0) {
2516 if (dest->nonnull) {
2517 /* bash man page calls this an "explicit" null */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002518 *pglob = globhack(dest->data, *pglob);
2519 }
2520 return 0;
2521 }
2522
2523 if (glob_needed(dest->data)) {
2524 glob_t globdata;
2525 int gr;
2526
2527 memset(&globdata, 0, sizeof(globdata));
2528 gr = glob(dest->data, 0, NULL, &globdata);
2529 debug_printf("glob returned %d\n", gr);
2530 if (gr == GLOB_NOSPACE)
2531 bb_error_msg_and_die("out of memory during glob");
2532 if (gr == GLOB_NOMATCH) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002533 debug_printf("globhack returned %d\n", gr);
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002534 /* quote removal, or more accurately, backslash removal */
2535 *pglob = globhack(dest->data, *pglob);
Denis Vlasenkof962a032007-11-23 12:50:54 +00002536 globfree(&globdata);
Eric Andersen25f27032001-04-26 23:22:31 +00002537 return 0;
2538 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002539 if (gr != 0) { /* GLOB_ABORTED ? */
2540 bb_error_msg("glob(3) error %d", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002541 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002542 if (globdata.gl_pathv && globdata.gl_pathv[0])
2543 *pglob = add_strings_to_strings(1, *pglob, globdata.gl_pathv);
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002544 globfree(&globdata);
2545 return gr;
Eric Andersen25f27032001-04-26 23:22:31 +00002546 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002547
2548 *pglob = globhack(dest->data, *pglob);
2549 return 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002550}
2551
Denis Vlasenko170435c2007-05-23 13:01:10 +00002552/* expand_strvec_to_strvec() takes a list of strings, expands
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002553 * all variable references within and returns a pointer to
2554 * a list of expanded strings, possibly with larger number
2555 * of strings. (Think VAR="a b"; echo $VAR).
2556 * This new list is allocated as a single malloc block.
2557 * NULL-terminated list of char* pointers is at the beginning of it,
2558 * followed by strings themself.
2559 * Caller can deallocate entire list by single free(list). */
2560
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002561/* Store given string, finalizing the word and starting new one whenever
2562 * we encounter ifs char(s). This is used for expanding variable values.
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002563 * End-of-string does NOT finalize word: think about 'echo -$VAR-' */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002564static int expand_on_ifs(o_string *output, int n, const char *str)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002565{
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002566 while (1) {
2567 int word_len = strcspn(str, ifs);
2568 if (word_len) {
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002569 o_addqstr(output, str, word_len, output->o_quote);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002570 str += word_len;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002571 }
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002572 if (!*str) /* EOL - do not finalize word */
2573 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002574 o_addchr(output, '\0');
2575 o_debug_list("expand_on_ifs", output, n);
2576 n = o_save_ptr(output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002577 str += strspn(str, ifs); /* skip ifs chars */
2578 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002579 o_debug_list("expand_on_ifs[1]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002580 return n;
2581}
2582
2583/* Expand all variable references in given string, adding words to list[]
2584 * at n, n+1,... positions. Return updated n (so that list[n] is next one
2585 * to be filled). This routine is extremely tricky: has to deal with
2586 * variables/parameters with whitespace, $* and $@, and constructs like
2587 * 'echo -$*-'. If you play here, you must run testsuite afterwards! */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002588/* NB: another bug is that we cannot detect empty strings yet:
2589 * "" or $empty"" expands to zero words, has to expand to empty word */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002590static int expand_vars_to_list(o_string *output, int n, char *arg, char or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002591{
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002592 /* or_mask is either 0 (normal case) or 0x80
2593 * (expansion of right-hand side of assignment == 1-element expand) */
2594
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002595 char first_ch, ored_ch;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002596 int i;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002597 const char *val;
2598 char *p;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002599
2600 ored_ch = 0;
2601
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002602 debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002603 o_debug_list("expand_vars_to_list", output, n);
2604 n = o_save_ptr(output, n);
2605 o_debug_list("expand_vars_to_list[0]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002606
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002607 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) {
2608 o_string subst_result = NULL_O_STRING;
2609
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002610 o_addqstr(output, arg, p - arg, output->o_quote);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002611 o_debug_list("expand_vars_to_list[1]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002612 arg = ++p;
2613 p = strchr(p, SPECIAL_VAR_SYMBOL);
2614
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002615 first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002616 ored_ch |= first_ch;
2617 val = NULL;
2618 switch (first_ch & 0x7f) {
2619 /* Highest bit in first_ch indicates that var is double-quoted */
2620 case '$': /* pid */
2621 /* FIXME: (echo $$) should still print pid of main shell */
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00002622 val = utoa(getpid()); /* rootpid? */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002623 break;
2624 case '!': /* bg pid */
2625 val = last_bg_pid ? utoa(last_bg_pid) : (char*)"";
2626 break;
2627 case '?': /* exitcode */
2628 val = utoa(last_return_code);
2629 break;
2630 case '#': /* argc */
2631 val = utoa(global_argc ? global_argc-1 : 0);
2632 break;
2633 case '*':
2634 case '@':
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002635 i = 1;
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002636 if (!global_argv[i])
2637 break;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002638 if (!(first_ch & 0x80)) { /* unquoted $* or $@ */
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002639 while (global_argv[i]) {
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002640 n = expand_on_ifs(output, n, global_argv[i]);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002641 debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, global_argc-1);
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002642 if (global_argv[i++][0] && global_argv[i]) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002643 /* this argv[] is not empty and not last:
2644 * put terminating NUL, start new word */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002645 o_addchr(output, '\0');
2646 o_debug_list("expand_vars_to_list[2]", output, n);
2647 n = o_save_ptr(output, n);
2648 o_debug_list("expand_vars_to_list[3]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002649 }
2650 }
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002651 } else
2652 /* If or_mask is nonzero, we handle assignment 'a=....$@.....'
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00002653 * and in this case should treat it like '$*' - see 'else...' below */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002654 if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002655 while (1) {
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002656 o_addqstr(output, global_argv[i], strlen(global_argv[i]), output->o_quote);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002657 if (++i >= global_argc)
2658 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002659 o_addchr(output, '\0');
2660 o_debug_list("expand_vars_to_list[4]", output, n);
2661 n = o_save_ptr(output, n);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002662 }
2663 } else { /* quoted $*: add as one word */
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002664 while (1) {
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002665 o_addqstr(output, global_argv[i], strlen(global_argv[i]), output->o_quote);
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00002666 if (!global_argv[++i])
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002667 break;
2668 if (ifs[0])
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002669 o_addchr(output, ifs[0]);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002670 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002671 }
2672 break;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002673 case '`': {
2674 struct in_str input;
2675 *p = '\0';
2676 arg++;
2677 //bb_error_msg("SUBST '%s' first_ch %x", arg, first_ch);
2678 setup_string_in_str(&input, arg);
2679 process_command_subs(&subst_result, &input, NULL);
2680 //bb_error_msg("RES '%s'", subst_result.data);
2681 val = subst_result.data;
2682 goto store_val;
2683 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002684 default:
2685 *p = '\0';
2686 arg[0] = first_ch & 0x7f;
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002687 if (isdigit(arg[0])) {
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002688 i = xatoi_u(arg);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002689 val = NULL;
2690 if (i < global_argc)
2691 val = global_argv[i];
2692 } else
2693 val = lookup_param(arg);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002694 arg[0] = first_ch;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002695 store_val:
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002696 *p = SPECIAL_VAR_SYMBOL;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002697 if (!(first_ch & 0x80)) { /* unquoted $VAR */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002698 if (val) {
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002699 n = expand_on_ifs(output, n, val);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002700 val = NULL;
2701 }
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002702 } /* else: quoted $VAR, val will be appended below */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002703 }
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002704 if (val) {
2705 o_addqstr(output, val, strlen(val), output->o_quote);
2706 }
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002707
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002708 o_free(&subst_result);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002709 arg = ++p;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002710 } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */
2711
Denis Vlasenko2e76c3f2008-06-10 18:27:50 +00002712 if (arg[0]) {
2713 o_debug_list("expand_vars_to_list[a]", output, n);
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002714 o_addqstr(output, arg, strlen(arg) + 1, output->o_quote);
Denis Vlasenko2e76c3f2008-06-10 18:27:50 +00002715 o_debug_list("expand_vars_to_list[b]", output, n);
2716 } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */
2717 && !(ored_ch & 0x80) /* and all vars were not quoted. */
2718 ) {
2719 n--;
2720 /* allow to reuse list[n] later without re-growth */
2721 output->has_empty_slot = 1;
2722 } else {
2723 o_addchr(output, '\0');
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002724 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002725 return n;
2726}
2727
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002728static char **expand_variables(char **argv, char or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002729{
2730 int n;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002731 char **list;
2732 char **v;
2733 o_string output = NULL_O_STRING;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002734
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002735 n = 0;
2736 v = argv;
2737 while (*v)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002738 n = expand_vars_to_list(&output, n, *v++, or_mask);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002739 o_debug_list("expand_variables", &output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002740
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002741 /* output.data (malloced) gets returned in "list" */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002742 list = o_finalize_list(&output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002743
2744#ifdef DEBUG_EXPAND
2745 {
2746 int m = 0;
2747 while (m <= n) {
2748 debug_printf_expand("list[%d]=%p '%s'\n", m, list[m], list[m]);
2749 m++;
2750 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002751 }
2752#endif
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002753 return list;
2754}
2755
Denis Vlasenko170435c2007-05-23 13:01:10 +00002756static char **expand_strvec_to_strvec(char **argv)
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002757{
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002758 return expand_variables(argv, 0);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002759}
2760
Denis Vlasenko170435c2007-05-23 13:01:10 +00002761static char *expand_string_to_string(const char *str)
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002762{
2763 char *argv[2], **list;
2764
2765 argv[0] = (char*)str;
2766 argv[1] = NULL;
2767 list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002768 if (ENABLE_HUSH_DEBUG)
2769 if (!list[0] || list[1])
2770 bb_error_msg_and_die("BUG in varexp2");
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002771 /* actually, just move string 2*sizeof(char*) bytes back */
2772 strcpy((char*)list, list[0]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00002773 debug_printf_expand("string_to_string='%s'\n", (char*)list);
2774 return (char*)list;
2775}
2776
2777static char* expand_strvec_to_string(char **argv)
2778{
2779 char **list;
2780
2781 list = expand_variables(argv, 0x80);
2782 /* Convert all NULs to spaces */
2783 if (list[0]) {
2784 int n = 1;
2785 while (list[n]) {
2786 if (ENABLE_HUSH_DEBUG)
2787 if (list[n-1] + strlen(list[n-1]) + 1 != list[n])
2788 bb_error_msg_and_die("BUG in varexp3");
2789 list[n][-1] = ' '; /* TODO: or to ifs[0]? */
2790 n++;
2791 }
2792 }
2793 strcpy((char*)list, list[0]);
2794 debug_printf_expand("strvec_to_string='%s'\n", (char*)list);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002795 return (char*)list;
2796}
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002797
Eric Andersenf72f5622001-05-15 23:21:41 +00002798/* This is used to get/check local shell variables */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002799static struct variable *get_local_var(const char *name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002800{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002801 struct variable *cur;
2802 int len;
Eric Andersenf72f5622001-05-15 23:21:41 +00002803
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002804 if (!name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002805 return NULL;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002806 len = strlen(name);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002807 for (cur = top_var; cur; cur = cur->next) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002808 if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=')
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002809 return cur;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002810 }
Eric Andersenf72f5622001-05-15 23:21:41 +00002811 return NULL;
2812}
2813
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002814/* str holds "NAME=VAL" and is expected to be malloced.
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002815 * We take ownership of it. */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002816static int set_local_var(char *str, int flg_export)
Eric Andersen78a7c992001-05-15 16:30:25 +00002817{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002818 struct variable *cur;
2819 char *value;
2820 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002821
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002822 value = strchr(str, '=');
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002823 if (!value) { /* not expected to ever happen? */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002824 free(str);
Eric Andersen99785762001-05-22 21:37:48 +00002825 return -1;
2826 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002827
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002828 name_len = value - str + 1; /* including '=' */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002829 cur = top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */
2830 while (1) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002831 if (strncmp(cur->varstr, str, name_len) != 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002832 if (!cur->next) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002833 /* Bail out. Note that now cur points
2834 * to last var in linked list */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002835 break;
Eric Andersen20a69a72001-05-15 17:24:44 +00002836 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002837 cur = cur->next;
2838 continue;
Eric Andersen20a69a72001-05-15 17:24:44 +00002839 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002840 /* We found an existing var with this name */
2841 *value = '\0';
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002842 if (cur->flg_read_only) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002843 bb_error_msg("%s: readonly variable", str);
2844 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002845 return -1;
2846 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002847 unsetenv(str); /* just in case */
2848 *value = '=';
2849 if (strcmp(cur->varstr, str) == 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002850 free_and_exp:
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002851 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002852 goto exp;
2853 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002854 if (cur->max_len >= strlen(str)) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002855 /* This one is from startup env, reuse space */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002856 strcpy(cur->varstr, str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002857 goto free_and_exp;
2858 }
2859 /* max_len == 0 signifies "malloced" var, which we can
2860 * (and has to) free */
2861 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002862 free(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002863 cur->max_len = 0;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002864 goto set_str_and_exp;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002865 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002866
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002867 /* Not found - create next variable struct */
2868 cur->next = xzalloc(sizeof(*cur));
2869 cur = cur->next;
2870
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002871 set_str_and_exp:
2872 cur->varstr = str;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002873 exp:
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002874 if (flg_export)
2875 cur->flg_export = 1;
2876 if (cur->flg_export)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002877 return putenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002878 return 0;
Eric Andersen20a69a72001-05-15 17:24:44 +00002879}
2880
Eric Andersenf72f5622001-05-15 23:21:41 +00002881static void unset_local_var(const char *name)
Eric Andersen20a69a72001-05-15 17:24:44 +00002882{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002883 struct variable *cur;
2884 struct variable *prev = prev; /* for gcc */
2885 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002886
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002887 if (!name)
2888 return;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002889 name_len = strlen(name);
2890 cur = top_var;
2891 while (cur) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002892 if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002893 if (cur->flg_read_only) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002894 bb_error_msg("%s: readonly variable", name);
Eric Andersen94ac2442001-05-22 19:05:18 +00002895 return;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002896 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002897 /* prev is ok to use here because 1st variable, HUSH_VERSION,
2898 * is ro, and we cannot reach this code on the 1st pass */
2899 prev->next = cur->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002900 unsetenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002901 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002902 free(cur->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002903 free(cur);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002904 return;
Eric Andersenf72f5622001-05-15 23:21:41 +00002905 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002906 prev = cur;
2907 cur = cur->next;
Eric Andersen20a69a72001-05-15 17:24:44 +00002908 }
Eric Andersen78a7c992001-05-15 16:30:25 +00002909}
2910
2911static int is_assignment(const char *s)
2912{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002913 if (!s || !isalpha(*s))
2914 return 0;
2915 s++;
2916 while (isalnum(*s) || *s == '_')
2917 s++;
2918 return *s == '=';
Eric Andersen78a7c992001-05-15 16:30:25 +00002919}
2920
Eric Andersen25f27032001-04-26 23:22:31 +00002921/* the src parameter allows us to peek forward to a possible &n syntax
2922 * for file descriptor duplication, e.g., "2>&1".
2923 * Return code is 0 normally, 1 if a syntax error is detected in src.
2924 * Resource errors (in xmalloc) cause the process to exit */
2925static int setup_redirect(struct p_context *ctx, int fd, redir_type style,
2926 struct in_str *input)
2927{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002928 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00002929 struct redir_struct *redir = child->redirects;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002930 struct redir_struct *last_redir = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002931
2932 /* Create a new redir_struct and drop it onto the end of the linked list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002933 while (redir) {
2934 last_redir = redir;
2935 redir = redir->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002936 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00002937 redir = xzalloc(sizeof(struct redir_struct));
2938 /* redir->next = NULL; */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002939 /* redir->glob_word = NULL; */
Eric Andersen25f27032001-04-26 23:22:31 +00002940 if (last_redir) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002941 last_redir->next = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002942 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002943 child->redirects = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002944 }
2945
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002946 redir->type = style;
2947 redir->fd = (fd == -1) ? redir_table[style].default_fd : fd;
Eric Andersen25f27032001-04-26 23:22:31 +00002948
2949 debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
2950
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002951 /* Check for a '2>&1' type redirect */
Eric Andersen25f27032001-04-26 23:22:31 +00002952 redir->dup = redirect_dup_num(input);
2953 if (redir->dup == -2) return 1; /* syntax error */
2954 if (redir->dup != -1) {
2955 /* Erik had a check here that the file descriptor in question
Eric Andersen83a2ae22001-05-07 17:59:25 +00002956 * is legit; I postpone that to "run time"
2957 * A "-" representation of "close me" shows up as a -3 here */
Eric Andersen25f27032001-04-26 23:22:31 +00002958 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
2959 } else {
2960 /* We do _not_ try to open the file that src points to,
2961 * since we need to return and let src be expanded first.
2962 * Set ctx->pending_redirect, so we know what to do at the
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002963 * end of the next parsed word. */
Eric Andersen25f27032001-04-26 23:22:31 +00002964 ctx->pending_redirect = redir;
2965 }
2966 return 0;
2967}
2968
Denis Vlasenkoac678ec2007-04-16 22:32:04 +00002969static struct pipe *new_pipe(void)
2970{
Eric Andersen25f27032001-04-26 23:22:31 +00002971 struct pipe *pi;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002972 pi = xzalloc(sizeof(struct pipe));
2973 /*pi->num_progs = 0;*/
2974 /*pi->progs = NULL;*/
2975 /*pi->next = NULL;*/
2976 /*pi->followup = 0; invalid */
2977 if (RES_NONE)
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002978 pi->res_word = RES_NONE;
Eric Andersen25f27032001-04-26 23:22:31 +00002979 return pi;
2980}
2981
2982static void initialize_context(struct p_context *ctx)
2983{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002984 ctx->child = NULL;
Denis Vlasenko1a735862007-05-23 00:32:25 +00002985 ctx->pipe = ctx->list_head = new_pipe();
2986 ctx->pending_redirect = NULL;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002987 ctx->res_w = RES_NONE;
Denis Vlasenko1a735862007-05-23 00:32:25 +00002988 //only ctx->parse_type is not touched... is this intentional?
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002989 ctx->old_flag = 0;
Denis Vlasenko1a735862007-05-23 00:32:25 +00002990 ctx->stack = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002991 done_command(ctx); /* creates the memory for working child */
2992}
2993
2994/* normal return is 0
2995 * if a reserved word is found, and processed, return 1
2996 * should handle if, then, elif, else, fi, for, while, until, do, done.
2997 * case, function, and select are obnoxious, save those for later.
2998 */
Denis Vlasenko06810332007-05-21 23:30:54 +00002999#if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00003000static int reserved_word(o_string *dest, struct p_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00003001{
3002 struct reserved_combo {
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003003 char literal[7];
3004 unsigned char code;
3005 int flag;
Eric Andersen25f27032001-04-26 23:22:31 +00003006 };
3007 /* Mostly a list of accepted follow-up reserved words.
3008 * FLAG_END means we are done with the sequence, and are ready
3009 * to turn the compound list into a command.
3010 * FLAG_START means the word must start a new compound list.
3011 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003012 static const struct reserved_combo reserved_list[] = {
Denis Vlasenko06810332007-05-21 23:30:54 +00003013#if ENABLE_HUSH_IF
Eric Andersen25f27032001-04-26 23:22:31 +00003014 { "if", RES_IF, FLAG_THEN | FLAG_START },
3015 { "then", RES_THEN, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
3016 { "elif", RES_ELIF, FLAG_THEN },
3017 { "else", RES_ELSE, FLAG_FI },
3018 { "fi", RES_FI, FLAG_END },
Denis Vlasenko06810332007-05-21 23:30:54 +00003019#endif
3020#if ENABLE_HUSH_LOOPS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003021 { "for", RES_FOR, FLAG_IN | FLAG_START },
Eric Andersen25f27032001-04-26 23:22:31 +00003022 { "while", RES_WHILE, FLAG_DO | FLAG_START },
3023 { "until", RES_UNTIL, FLAG_DO | FLAG_START },
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003024 { "in", RES_IN, FLAG_DO },
Eric Andersen25f27032001-04-26 23:22:31 +00003025 { "do", RES_DO, FLAG_DONE },
3026 { "done", RES_DONE, FLAG_END }
Denis Vlasenko06810332007-05-21 23:30:54 +00003027#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003028 };
Denis Vlasenko80b8b392007-06-25 10:55:35 +00003029
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003030 const struct reserved_combo *r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003031
Denis Vlasenko80b8b392007-06-25 10:55:35 +00003032 for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) {
Denis Vlasenko1a735862007-05-23 00:32:25 +00003033 if (strcmp(dest->data, r->literal) != 0)
3034 continue;
3035 debug_printf("found reserved word %s, code %d\n", r->literal, r->code);
3036 if (r->flag & FLAG_START) {
3037 struct p_context *new;
3038 debug_printf("push stack\n");
Denis Vlasenko06810332007-05-21 23:30:54 +00003039#if ENABLE_HUSH_LOOPS
Denis Vlasenko1a735862007-05-23 00:32:25 +00003040 if (ctx->res_w == RES_IN || ctx->res_w == RES_FOR) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003041 syntax("malformed for"); /* example: 'for if' */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003042 ctx->res_w = RES_SNTX;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003043 o_reset(dest);
Eric Andersenaf44a0e2001-04-27 07:26:12 +00003044 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003045 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003046#endif
3047 new = xmalloc(sizeof(*new));
3048 *new = *ctx; /* physical copy */
3049 initialize_context(ctx);
3050 ctx->stack = new;
3051 } else if (ctx->res_w == RES_NONE || !(ctx->old_flag & (1 << r->code))) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003052 syntax(NULL);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003053 ctx->res_w = RES_SNTX;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003054 o_reset(dest);
Eric Andersen25f27032001-04-26 23:22:31 +00003055 return 1;
3056 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003057 ctx->res_w = r->code;
3058 ctx->old_flag = r->flag;
3059 if (ctx->old_flag & FLAG_END) {
3060 struct p_context *old;
3061 debug_printf("pop stack\n");
3062 done_pipe(ctx, PIPE_SEQ);
3063 old = ctx->stack;
3064 old->child->group = ctx->list_head;
3065 old->child->subshell = 0;
3066 *ctx = *old; /* physical copy */
3067 free(old);
3068 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003069 o_reset(dest);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003070 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003071 }
3072 return 0;
3073}
Denis Vlasenko06810332007-05-21 23:30:54 +00003074#else
3075#define reserved_word(dest, ctx) ((int)0)
3076#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003077
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003078/* Normal return is 0.
Eric Andersen25f27032001-04-26 23:22:31 +00003079 * Syntax or xglob errors return 1. */
3080static int done_word(o_string *dest, struct p_context *ctx)
3081{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003082 struct child_prog *child = ctx->child;
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003083 char ***glob_target;
3084 int gr;
Eric Andersen25f27032001-04-26 23:22:31 +00003085
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003086 debug_printf_parse("done_word entered: '%s' %p\n", dest->data, child);
Eric Andersen25f27032001-04-26 23:22:31 +00003087 if (dest->length == 0 && !dest->nonnull) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003088 debug_printf_parse("done_word return 0: true null, ignored\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003089 return 0;
3090 }
3091 if (ctx->pending_redirect) {
Denis Vlasenkoff097622007-10-01 10:00:45 +00003092 glob_target = &ctx->pending_redirect->glob_word;
Eric Andersen25f27032001-04-26 23:22:31 +00003093 } else {
3094 if (child->group) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003095 syntax(NULL);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003096 debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n");
3097 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003098 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003099 if (!child->argv && (ctx->parse_type & PARSEFLAG_SEMICOLON)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003100 debug_printf_parse(": checking '%s' for reserved-ness\n", dest->data);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003101 if (reserved_word(dest, ctx)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003102 debug_printf_parse("done_word return %d\n", (ctx->res_w == RES_SNTX));
3103 return (ctx->res_w == RES_SNTX);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003104 }
Eric Andersen25f27032001-04-26 23:22:31 +00003105 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003106 glob_target = &child->argv;
Eric Andersen25f27032001-04-26 23:22:31 +00003107 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003108 gr = xglob(dest, glob_target);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003109 if (gr != 0) {
3110 debug_printf_parse("done_word return 1: xglob returned %d\n", gr);
3111 return 1;
3112 }
Eric Andersen25f27032001-04-26 23:22:31 +00003113
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003114 o_reset(dest);
Eric Andersen25f27032001-04-26 23:22:31 +00003115 if (ctx->pending_redirect) {
Denis Vlasenkof962a032007-11-23 12:50:54 +00003116 /* NB: don't free_strings(ctx->pending_redirect->glob_word) here */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003117 if (ctx->pending_redirect->glob_word
3118 && ctx->pending_redirect->glob_word[0]
3119 && ctx->pending_redirect->glob_word[1]
3120 ) {
3121 /* more than one word resulted from globbing redir */
3122 ctx->pending_redirect = NULL;
Manuel Novoa III cad53642003-03-19 09:13:01 +00003123 bb_error_msg("ambiguous redirect");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003124 debug_printf_parse("done_word return 1: ambiguous redirect\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003125 return 1;
3126 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003127 ctx->pending_redirect = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003128 }
Denis Vlasenko06810332007-05-21 23:30:54 +00003129#if ENABLE_HUSH_LOOPS
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003130 if (ctx->res_w == RES_FOR) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003131 done_word(dest, ctx);
3132 done_pipe(ctx, PIPE_SEQ);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003133 }
Denis Vlasenko06810332007-05-21 23:30:54 +00003134#endif
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003135 debug_printf_parse("done_word return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003136 return 0;
3137}
3138
3139/* The only possible error here is out of memory, in which case
3140 * xmalloc exits. */
3141static int done_command(struct p_context *ctx)
3142{
3143 /* The child is really already in the pipe structure, so
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003144 * advance the pipe counter and make a new, null child. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003145 struct pipe *pi = ctx->pipe;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003146 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00003147
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003148 if (child) {
3149 if (child->group == NULL
3150 && child->argv == NULL
3151 && child->redirects == NULL
3152 ) {
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003153 debug_printf_parse("done_command: skipping null cmd, num_progs=%d\n", pi->num_progs);
3154 return pi->num_progs;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003155 }
Eric Andersen25f27032001-04-26 23:22:31 +00003156 pi->num_progs++;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003157 debug_printf_parse("done_command: ++num_progs=%d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00003158 } else {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003159 debug_printf_parse("done_command: initializing, num_progs=%d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00003160 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003161
3162 /* Only real trickiness here is that the uncommitted
3163 * child structure is not counted in pi->num_progs. */
Eric Andersen25f27032001-04-26 23:22:31 +00003164 pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1));
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003165 child = &pi->progs[pi->num_progs];
Eric Andersen25f27032001-04-26 23:22:31 +00003166
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003167 memset(child, 0, sizeof(*child));
3168 /*child->redirects = NULL;*/
3169 /*child->argv = NULL;*/
3170 /*child->is_stopped = 0;*/
3171 /*child->group = NULL;*/
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003172 child->family = pi;
Eric Andersen25f27032001-04-26 23:22:31 +00003173
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003174 ctx->child = child;
Eric Andersen25f27032001-04-26 23:22:31 +00003175 /* but ctx->pipe and ctx->list_head remain unchanged */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003176
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003177 return pi->num_progs; /* used only for 0/nonzero check */
Eric Andersen25f27032001-04-26 23:22:31 +00003178}
3179
3180static int done_pipe(struct p_context *ctx, pipe_style type)
3181{
3182 struct pipe *new_p;
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003183 int not_null;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003184
3185 debug_printf_parse("done_pipe entered, followup %d\n", type);
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003186 not_null = done_command(ctx); /* implicit closure of previous command */
Eric Andersen25f27032001-04-26 23:22:31 +00003187 ctx->pipe->followup = type;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003188 ctx->pipe->res_word = ctx->res_w;
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003189 /* Without this check, even just <enter> on command line generates
3190 * tree of three NOPs (!). Which is harmless but annoying.
3191 * IOW: it is safe to do it unconditionally. */
3192 if (not_null) {
3193 new_p = new_pipe();
3194 ctx->pipe->next = new_p;
3195 ctx->pipe = new_p;
3196 ctx->child = NULL;
3197 done_command(ctx); /* set up new pipe to accept commands */
3198 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003199 debug_printf_parse("done_pipe return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003200 return 0;
3201}
3202
3203/* peek ahead in the in_str to find out if we have a "&n" construct,
3204 * as in "2>&1", that represents duplicating a file descriptor.
3205 * returns either -2 (syntax error), -1 (no &), or the number found.
3206 */
3207static int redirect_dup_num(struct in_str *input)
3208{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003209 int ch, d = 0, ok = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003210 ch = i_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003211 if (ch != '&') return -1;
3212
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003213 i_getch(input); /* get the & */
3214 ch = i_peek(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003215 if (ch == '-') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003216 i_getch(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003217 return -3; /* "-" represents "close me" */
3218 }
3219 while (isdigit(ch)) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00003220 d = d*10 + (ch-'0');
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003221 ok = 1;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003222 i_getch(input);
3223 ch = i_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003224 }
3225 if (ok) return d;
3226
Manuel Novoa III cad53642003-03-19 09:13:01 +00003227 bb_error_msg("ambiguous redirect");
Eric Andersen25f27032001-04-26 23:22:31 +00003228 return -2;
3229}
3230
3231/* If a redirect is immediately preceded by a number, that number is
3232 * supposed to tell which file descriptor to redirect. This routine
3233 * looks for such preceding numbers. In an ideal world this routine
3234 * needs to handle all the following classes of redirects...
3235 * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
3236 * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
3237 * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
3238 * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
3239 * A -1 output from this program means no valid number was found, so the
3240 * caller should use the appropriate default for this redirection.
3241 */
3242static int redirect_opt_num(o_string *o)
3243{
3244 int num;
3245
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003246 if (o->length == 0)
3247 return -1;
3248 for (num = 0; num < o->length; num++) {
3249 if (!isdigit(*(o->data + num))) {
Eric Andersen25f27032001-04-26 23:22:31 +00003250 return -1;
3251 }
3252 }
3253 /* reuse num (and save an int) */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003254 num = atoi(o->data);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003255 o_reset(o);
Eric Andersen25f27032001-04-26 23:22:31 +00003256 return num;
3257}
3258
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003259#if ENABLE_HUSH_TICK
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00003260static FILE *generate_stream_from_list(struct pipe *head)
Eric Andersen25f27032001-04-26 23:22:31 +00003261{
3262 FILE *pf;
Eric Andersen25f27032001-04-26 23:22:31 +00003263 int pid, channel[2];
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003264
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +00003265 xpipe(channel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003266/* *** NOMMU WARNING *** */
3267/* By using vfork here, we suspend parent till child exits or execs.
3268 * If child will not do it before it fills the pipe, it can block forever
3269 * in write(STDOUT_FILENO), and parent (shell) will be also stuck.
Denis Vlasenko3fe4f982008-06-09 16:02:39 +00003270 * Try this script:
3271 * yes "0123456789012345678901234567890" | dd bs=32 count=64k >TESTFILE
3272 * huge=`cat TESTFILE` # will block here forever
3273 * echo OK
Denis Vlasenko05743d72008-02-10 12:10:08 +00003274 */
3275 pid = BB_MMU ? fork() : vfork();
3276 if (pid < 0)
3277 bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork");
3278 if (pid == 0) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00003279 if (ENABLE_HUSH_JOB)
3280 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenko284d0fa2008-02-16 13:18:17 +00003281 close(channel[0]); /* NB: close _first_, then move fd! */
Denis Vlasenko05743d72008-02-10 12:10:08 +00003282 xmove_fd(channel[1], 1);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003283 /* Prevent it from trying to handle ctrl-z etc */
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003284#if ENABLE_HUSH_JOB
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003285 run_list_level = 1;
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003286#endif
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003287 /* Process substitution is not considered to be usual
3288 * 'command execution'.
3289 * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. */
3290 /* Not needed, we are relying on it being disabled
3291 * everywhere outside actual command execution. */
3292 /*set_jobctrl_sighandler(SIG_IGN);*/
3293 set_misc_sighandler(SIG_DFL);
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003294 /* Freeing 'head' here would break NOMMU. */
3295 _exit(run_list(head));
Eric Andersen25f27032001-04-26 23:22:31 +00003296 }
Eric Andersen25f27032001-04-26 23:22:31 +00003297 close(channel[1]);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003298 pf = fdopen(channel[0], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00003299 return pf;
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003300 /* 'head' is freed by the caller */
Eric Andersen25f27032001-04-26 23:22:31 +00003301}
3302
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003303/* Return code is exit status of the process that is run. */
Denis Vlasenko68404f12008-03-17 09:00:54 +00003304static int process_command_subs(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +00003305 struct in_str *input,
3306 const char *subst_end)
Eric Andersen25f27032001-04-26 23:22:31 +00003307{
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003308 int retcode, ch, eol_cnt;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003309 o_string result = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003310 struct p_context inner;
3311 FILE *p;
3312 struct in_str pipe_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003313
Eric Andersen25f27032001-04-26 23:22:31 +00003314 initialize_context(&inner);
3315
3316 /* recursion to generate command */
3317 retcode = parse_stream(&result, &inner, input, subst_end);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003318 if (retcode != 0)
3319 return retcode; /* syntax error or EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003320 done_word(&result, &inner);
3321 done_pipe(&inner, PIPE_SEQ);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003322 o_free(&result);
Eric Andersen25f27032001-04-26 23:22:31 +00003323
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003324 p = generate_stream_from_list(inner.list_head);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003325 if (p == NULL)
3326 return 1;
Denis Vlasenkoa0898172007-10-01 09:59:01 +00003327 close_on_exec_on(fileno(p));
Eric Andersen25f27032001-04-26 23:22:31 +00003328 setup_file_in_str(&pipe_str, p);
3329
3330 /* now send results of command back into original context */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003331 eol_cnt = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003332 while ((ch = i_getch(&pipe_str)) != EOF) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003333 if (ch == '\n') {
3334 eol_cnt++;
3335 continue;
3336 }
3337 while (eol_cnt) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003338 o_addqchr(dest, '\n', dest->o_quote);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003339 eol_cnt--;
3340 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003341 o_addqchr(dest, ch, dest->o_quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003342 }
3343
3344 debug_printf("done reading from pipe, pclose()ing\n");
3345 /* This is the step that wait()s for the child. Should be pretty
3346 * safe, since we just read an EOF from its stdout. We could try
Denis Vlasenko262d7652007-05-20 21:52:49 +00003347 * to do better, by using wait(), and keeping track of background jobs
Eric Andersen25f27032001-04-26 23:22:31 +00003348 * at the same time. That would be a lot of work, and contrary
3349 * to the KISS philosophy of this program. */
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003350 retcode = fclose(p);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003351 free_pipe_list(inner.list_head, /* indent: */ 0);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003352 debug_printf("closed FILE from child, retcode=%d\n", retcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003353 return retcode;
3354}
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003355#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003356
3357static int parse_group(o_string *dest, struct p_context *ctx,
3358 struct in_str *input, int ch)
3359{
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003360 int rcode;
3361 const char *endch = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003362 struct p_context sub;
3363 struct child_prog *child = ctx->child;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003364
3365 debug_printf_parse("parse_group entered\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003366 if (child->argv) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003367 syntax(NULL);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003368 debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n");
3369 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003370 }
3371 initialize_context(&sub);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003372 endch = "}";
3373 if (ch == '(') {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003374 endch = ")";
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003375 child->subshell = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003376 }
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003377 rcode = parse_stream(dest, &sub, input, endch);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00003378//vda: err chk?
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003379 done_word(dest, &sub); /* finish off the final word in the subcontext */
Eric Andersen25f27032001-04-26 23:22:31 +00003380 done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
3381 child->group = sub.list_head;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003382
3383 debug_printf_parse("parse_group return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003384 return rcode;
3385 /* child remains "open", available for possible redirects */
3386}
3387
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003388/* Basically useful version until someone wants to get fancier,
Eric Andersen25f27032001-04-26 23:22:31 +00003389 * see the bash man page under "Parameter Expansion" */
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00003390static const char *lookup_param(const char *src)
Eric Andersen25f27032001-04-26 23:22:31 +00003391{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003392 struct variable *var = get_local_var(src);
3393 if (var)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003394 return strchr(var->varstr, '=') + 1;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003395 return NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003396}
3397
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003398#if ENABLE_HUSH_TICK
3399/* Subroutines for copying $(...) and `...` things */
3400static void add_till_backquote(o_string *dest, struct in_str *input);
3401/* '...' */
3402static void add_till_single_quote(o_string *dest, struct in_str *input)
3403{
3404 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003405 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003406 if (ch == EOF)
3407 break;
3408 if (ch == '\'')
3409 break;
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003410 o_addqchr(dest, ch, 1);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003411 }
3412}
3413/* "...\"...`..`...." - do we need to handle "...$(..)..." too? */
3414static void add_till_double_quote(o_string *dest, struct in_str *input)
3415{
3416 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003417 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003418 if (ch == '"')
3419 break;
3420 if (ch == '\\') { /* \x. Copy both chars. */
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003421 o_addqchr(dest, ch, 1);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003422 ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003423 }
3424 if (ch == EOF)
3425 break;
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003426 o_addqchr(dest, ch, 1);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003427 if (ch == '`') {
3428 add_till_backquote(dest, input);
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003429 o_addqchr(dest, ch, 1);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003430 continue;
3431 }
3432// if (ch == '$') ...
3433 }
3434}
3435/* Process `cmd` - copy contents until "`" is seen. Complicated by
3436 * \` quoting.
3437 * "Within the backquoted style of command substitution, backslash
3438 * shall retain its literal meaning, except when followed by: '$', '`', or '\'.
3439 * The search for the matching backquote shall be satisfied by the first
3440 * backquote found without a preceding backslash; during this search,
3441 * if a non-escaped backquote is encountered within a shell comment,
3442 * a here-document, an embedded command substitution of the $(command)
3443 * form, or a quoted string, undefined results occur. A single-quoted
3444 * or double-quoted string that begins, but does not end, within the
3445 * "`...`" sequence produces undefined results."
3446 * Example Output
3447 * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST
3448 */
3449static void add_till_backquote(o_string *dest, struct in_str *input)
3450{
3451 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003452 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003453 if (ch == '`')
3454 break;
3455 if (ch == '\\') { /* \x. Copy both chars unless it is \` */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003456 int ch2 = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003457 if (ch2 != '`' && ch2 != '$' && ch2 != '\\')
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003458 o_addqchr(dest, ch, 1);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003459 ch = ch2;
3460 }
3461 if (ch == EOF)
3462 break;
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003463 o_addqchr(dest, ch, 1);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003464 }
3465}
3466/* Process $(cmd) - copy contents until ")" is seen. Complicated by
3467 * quoting and nested ()s.
3468 * "With the $(command) style of command substitution, all characters
3469 * following the open parenthesis to the matching closing parenthesis
3470 * constitute the command. Any valid shell script can be used for command,
3471 * except a script consisting solely of redirections which produces
3472 * unspecified results."
3473 * Example Output
3474 * echo $(echo '(TEST)' BEST) (TEST) BEST
3475 * echo $(echo 'TEST)' BEST) TEST) BEST
3476 * echo $(echo \(\(TEST\) BEST) ((TEST) BEST
3477 */
3478static void add_till_closing_curly_brace(o_string *dest, struct in_str *input)
3479{
3480 int count = 0;
3481 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003482 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003483 if (ch == EOF)
3484 break;
3485 if (ch == '(')
3486 count++;
3487 if (ch == ')')
3488 if (--count < 0)
3489 break;
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003490 o_addqchr(dest, ch, 1);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003491 if (ch == '\'') {
3492 add_till_single_quote(dest, input);
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003493 o_addqchr(dest, ch, 1);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003494 continue;
3495 }
3496 if (ch == '"') {
3497 add_till_double_quote(dest, input);
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003498 o_addqchr(dest, ch, 1);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003499 continue;
3500 }
3501 }
3502}
3503#endif /* ENABLE_HUSH_TICK */
3504
Eric Andersen25f27032001-04-26 23:22:31 +00003505/* return code: 0 for OK, 1 for syntax error */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003506static int handle_dollar(o_string *dest, struct in_str *input)
Eric Andersen25f27032001-04-26 23:22:31 +00003507{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003508 int ch = i_peek(input); /* first character after the $ */
Denis Vlasenkoff097622007-10-01 10:00:45 +00003509 unsigned char quote_mask = dest->o_quote ? 0x80 : 0;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003510
3511 debug_printf_parse("handle_dollar entered: ch='%c'\n", ch);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003512 if (isalpha(ch)) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003513 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003514 while (1) {
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003515 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003516 i_getch(input);
3517 o_addchr(dest, ch | quote_mask);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003518 quote_mask = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003519 ch = i_peek(input);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003520 if (!isalnum(ch) && ch != '_')
3521 break;
Eric Andersen25f27032001-04-26 23:22:31 +00003522 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003523 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003524 } else if (isdigit(ch)) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003525 make_one_char_var:
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003526 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003527 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003528 i_getch(input);
3529 o_addchr(dest, ch | quote_mask);
3530 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003531 } else switch (ch) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003532 case '$': /* pid */
3533 case '!': /* last bg pid */
3534 case '?': /* last exit code */
3535 case '#': /* number of args */
3536 case '*': /* args */
3537 case '@': /* args */
3538 goto make_one_char_var;
Eric Andersen25f27032001-04-26 23:22:31 +00003539 case '{':
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003540 o_addchr(dest, SPECIAL_VAR_SYMBOL);
3541 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003542 /* XXX maybe someone will try to escape the '}' */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003543 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003544 ch = i_getch(input);
Denis Vlasenkob0550012007-05-24 12:18:16 +00003545 if (ch == '}')
3546 break;
3547 if (!isalnum(ch) && ch != '_') {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003548 syntax("unterminated ${name}");
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003549 debug_printf_parse("handle_dollar return 1: unterminated ${name}\n");
3550 return 1;
3551 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003552 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003553 o_addchr(dest, ch | quote_mask);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003554 quote_mask = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003555 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003556 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003557 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003558#if ENABLE_HUSH_TICK
Eric Andersen25f27032001-04-26 23:22:31 +00003559 case '(':
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003560 i_getch(input);
3561 o_addchr(dest, SPECIAL_VAR_SYMBOL);
3562 o_addchr(dest, quote_mask | '`');
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003563 add_till_closing_curly_brace(dest, input);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003564 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003565 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003566#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003567 case '-':
3568 case '_':
3569 /* still unhandled, but should be eventually */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003570 bb_error_msg("unhandled syntax: $%c", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003571 return 1;
3572 break;
3573 default:
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003574 o_addqchr(dest, '$', dest->o_quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003575 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003576 debug_printf_parse("handle_dollar return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003577 return 0;
3578}
3579
Eric Andersen25f27032001-04-26 23:22:31 +00003580/* return code is 0 for normal exit, 1 for syntax error */
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003581static int parse_stream(o_string *dest, struct p_context *ctx,
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003582 struct in_str *input, const char *end_trigger)
Eric Andersen25f27032001-04-26 23:22:31 +00003583{
"Vladimir N. Oleynik"4ccd2b42006-01-31 09:27:48 +00003584 int ch, m;
Eric Andersen25f27032001-04-26 23:22:31 +00003585 int redir_fd;
3586 redir_type redir_style;
3587 int next;
3588
Denis Vlasenkoff097622007-10-01 10:00:45 +00003589 /* Only double-quote state is handled in the state variable dest->o_quote.
Eric Andersen25f27032001-04-26 23:22:31 +00003590 * A single-quote triggers a bypass of the main loop until its mate is
Denis Vlasenkoff097622007-10-01 10:00:45 +00003591 * found. When recursing, quote state is passed in via dest->o_quote. */
Eric Andersen25f27032001-04-26 23:22:31 +00003592
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003593 debug_printf_parse("parse_stream entered, end_trigger='%s'\n", end_trigger);
3594
Denis Vlasenko1a735862007-05-23 00:32:25 +00003595 while (1) {
Denis Vlasenko1a735862007-05-23 00:32:25 +00003596 m = CHAR_IFS;
3597 next = '\0';
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003598 ch = i_getch(input);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003599 if (ch != EOF) {
3600 m = charmap[ch];
3601 if (ch != '\n')
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003602 next = i_peek(input);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003603 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003604 debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n",
Denis Vlasenkoff097622007-10-01 10:00:45 +00003605 ch, ch, m, dest->o_quote);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003606 if (m == CHAR_ORDINARY
Denis Vlasenkoff097622007-10-01 10:00:45 +00003607 || (m != CHAR_SPECIAL && dest->o_quote)
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003608 ) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00003609 if (ch == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003610 syntax("unterminated \"");
Denis Vlasenko170435c2007-05-23 13:01:10 +00003611 debug_printf_parse("parse_stream return 1: unterminated \"\n");
3612 return 1;
3613 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003614 o_addqchr(dest, ch, dest->o_quote);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003615 continue;
3616 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003617 if (m == CHAR_IFS) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003618 if (done_word(dest, ctx)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003619 debug_printf_parse("parse_stream return 1: done_word!=0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003620 return 1;
Eric Andersenaac75e52001-04-30 18:18:45 +00003621 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003622 if (ch == EOF)
3623 break;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003624 /* If we aren't performing a substitution, treat
3625 * a newline as a command separator.
3626 * [why we don't handle it exactly like ';'? --vda] */
3627 if (end_trigger && ch == '\n') {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003628 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003629 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003630 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003631 if ((end_trigger && strchr(end_trigger, ch))
Denis Vlasenkoff097622007-10-01 10:00:45 +00003632 && !dest->o_quote && ctx->res_w == RES_NONE
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003633 ) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003634 debug_printf_parse("parse_stream return 0: end_trigger char found\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003635 return 0;
3636 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003637 if (m == CHAR_IFS)
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003638 continue;
3639 switch (ch) {
Eric Andersen25f27032001-04-26 23:22:31 +00003640 case '#':
Denis Vlasenkoff097622007-10-01 10:00:45 +00003641 if (dest->length == 0 && !dest->o_quote) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003642 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003643 ch = i_peek(input);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003644 if (ch == EOF || ch == '\n')
3645 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003646 i_getch(input);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003647 }
Eric Andersen25f27032001-04-26 23:22:31 +00003648 } else {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003649 o_addqchr(dest, ch, dest->o_quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003650 }
3651 break;
3652 case '\\':
3653 if (next == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003654 syntax("\\<eof>");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003655 debug_printf_parse("parse_stream return 1: \\<eof>\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003656 return 1;
3657 }
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003658 /* bash:
3659 * "The backslash retains its special meaning [in "..."]
3660 * only when followed by one of the following characters:
3661 * $, `, ", \, or <newline>. A double quote may be quoted
3662 * within double quotes by preceding it with a backslash.
3663 * If enabled, history expansion will be performed unless
3664 * an ! appearing in double quotes is escaped using
3665 * a backslash. The backslash preceding the ! is not removed."
3666 */
3667 if (dest->o_quote) {
3668 if (strchr("$`\"\\", next) != NULL) {
3669 o_addqchr(dest, i_getch(input), 1);
3670 } else {
3671 o_addqchr(dest, '\\', 1);
3672 }
3673 } else {
3674 o_addchr(dest, '\\');
3675 o_addchr(dest, i_getch(input));
3676 }
Eric Andersen25f27032001-04-26 23:22:31 +00003677 break;
3678 case '$':
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003679 if (handle_dollar(dest, input) != 0) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003680 debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n");
3681 return 1;
3682 }
Eric Andersen25f27032001-04-26 23:22:31 +00003683 break;
3684 case '\'':
3685 dest->nonnull = 1;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003686 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003687 ch = i_getch(input);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003688 if (ch == EOF || ch == '\'')
3689 break;
Denis Vlasenko43360e52008-06-10 20:13:40 +00003690 o_addqchr(dest, ch, 1);
Eric Andersen25f27032001-04-26 23:22:31 +00003691 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003692 if (ch == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003693 syntax("unterminated '");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003694 debug_printf_parse("parse_stream return 1: unterminated '\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003695 return 1;
3696 }
3697 break;
3698 case '"':
3699 dest->nonnull = 1;
Denis Vlasenkoff097622007-10-01 10:00:45 +00003700 dest->o_quote ^= 1; /* invert */
Eric Andersen25f27032001-04-26 23:22:31 +00003701 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003702#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003703 case '`': {
3704 //int pos = dest->length;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003705 o_addchr(dest, SPECIAL_VAR_SYMBOL);
3706 o_addchr(dest, dest->o_quote ? 0x80 | '`' : '`');
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003707 add_till_backquote(dest, input);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003708 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003709 //bb_error_msg("RES '%s'", dest->data + pos);
Eric Andersen25f27032001-04-26 23:22:31 +00003710 break;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003711 }
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003712#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003713 case '>':
3714 redir_fd = redirect_opt_num(dest);
3715 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003716 redir_style = REDIRECT_OVERWRITE;
Eric Andersen25f27032001-04-26 23:22:31 +00003717 if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003718 redir_style = REDIRECT_APPEND;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003719 i_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003720 }
3721#if 0
3722 else if (next == '(') {
3723 syntax(">(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003724 debug_printf_parse("parse_stream return 1: >(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003725 return 1;
3726 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003727#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003728 setup_redirect(ctx, redir_fd, redir_style, input);
3729 break;
3730 case '<':
3731 redir_fd = redirect_opt_num(dest);
3732 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003733 redir_style = REDIRECT_INPUT;
Eric Andersen25f27032001-04-26 23:22:31 +00003734 if (next == '<') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003735 redir_style = REDIRECT_HEREIS;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003736 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003737 } else if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003738 redir_style = REDIRECT_IO;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003739 i_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003740 }
3741#if 0
3742 else if (next == '(') {
3743 syntax("<(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003744 debug_printf_parse("parse_stream return 1: <(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003745 return 1;
3746 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003747#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003748 setup_redirect(ctx, redir_fd, redir_style, input);
3749 break;
3750 case ';':
3751 done_word(dest, ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003752 done_pipe(ctx, PIPE_SEQ);
Eric Andersen25f27032001-04-26 23:22:31 +00003753 break;
3754 case '&':
3755 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003756 if (next == '&') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003757 i_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003758 done_pipe(ctx, PIPE_AND);
Eric Andersen25f27032001-04-26 23:22:31 +00003759 } else {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003760 done_pipe(ctx, PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00003761 }
3762 break;
3763 case '|':
3764 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003765 if (next == '|') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003766 i_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003767 done_pipe(ctx, PIPE_OR);
Eric Andersen25f27032001-04-26 23:22:31 +00003768 } else {
3769 /* we could pick up a file descriptor choice here
3770 * with redirect_opt_num(), but bash doesn't do it.
3771 * "echo foo 2| cat" yields "foo 2". */
3772 done_command(ctx);
3773 }
3774 break;
3775 case '(':
3776 case '{':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003777 if (parse_group(dest, ctx, input, ch) != 0) {
3778 debug_printf_parse("parse_stream return 1: parse_group returned non-0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003779 return 1;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003780 }
Eric Andersen25f27032001-04-26 23:22:31 +00003781 break;
3782 case ')':
3783 case '}':
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003784 syntax("unexpected }"); /* Proper use of this character is caught by end_trigger */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003785 debug_printf_parse("parse_stream return 1: unexpected '}'\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003786 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003787 default:
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003788 if (ENABLE_HUSH_DEBUG)
3789 bb_error_msg_and_die("BUG: unexpected %c\n", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003790 }
3791 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003792 /* Complain if quote? No, maybe we just finished a command substitution
Eric Andersen25f27032001-04-26 23:22:31 +00003793 * that was quoted. Example:
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003794 * $ echo "`cat foo` plus more"
Eric Andersen25f27032001-04-26 23:22:31 +00003795 * and we just got the EOF generated by the subshell that ran "cat foo"
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003796 * The only real complaint is if we got an EOF when end_trigger != NULL,
Eric Andersen25f27032001-04-26 23:22:31 +00003797 * that is, we were really supposed to get end_trigger, and never got
3798 * one before the EOF. Can't use the standard "syntax error" return code,
3799 * so that parse_stream_outer can distinguish the EOF and exit smoothly. */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003800 debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL));
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003801 if (end_trigger)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003802 return -1;
Eric Andersen25f27032001-04-26 23:22:31 +00003803 return 0;
3804}
3805
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003806static void set_in_charmap(const char *set, int code)
Eric Andersen25f27032001-04-26 23:22:31 +00003807{
Denis Vlasenkof5294e12007-04-14 10:09:57 +00003808 while (*set)
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003809 charmap[(unsigned char)*set++] = code;
Eric Andersen25f27032001-04-26 23:22:31 +00003810}
3811
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003812static void update_charmap(void)
Eric Andersen25f27032001-04-26 23:22:31 +00003813{
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003814 /* char *ifs and char charmap[256] are both globals. */
Eric Andersen25f27032001-04-26 23:22:31 +00003815 ifs = getenv("IFS");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003816 if (ifs == NULL)
3817 ifs = " \t\n";
Eric Andersen25f27032001-04-26 23:22:31 +00003818 /* Precompute a list of 'flow through' behavior so it can be treated
3819 * quickly up front. Computation is necessary because of IFS.
3820 * Special case handling of IFS == " \t\n" is not implemented.
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003821 * The charmap[] array only really needs two bits each,
3822 * and on most machines that would be faster (reduced L1 cache use).
Eric Andersen25f27032001-04-26 23:22:31 +00003823 */
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003824 memset(charmap, CHAR_ORDINARY, sizeof(charmap));
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003825#if ENABLE_HUSH_TICK
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003826 set_in_charmap("\\$\"`", CHAR_SPECIAL);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003827#else
3828 set_in_charmap("\\$\"", CHAR_SPECIAL);
3829#endif
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003830 set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003831 set_in_charmap(ifs, CHAR_IFS); /* are ordinary if quoted */
Eric Andersen25f27032001-04-26 23:22:31 +00003832}
3833
Eric Andersenaff114c2004-04-14 17:51:38 +00003834/* most recursion does not come through here, the exception is
Denis Vlasenko170435c2007-05-23 13:01:10 +00003835 * from builtin_source() and builtin_eval() */
3836static int parse_and_run_stream(struct in_str *inp, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003837{
Eric Andersen25f27032001-04-26 23:22:31 +00003838 struct p_context ctx;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003839 o_string temp = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003840 int rcode;
3841 do {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003842 ctx.parse_type = parse_flag;
Eric Andersen25f27032001-04-26 23:22:31 +00003843 initialize_context(&ctx);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003844 update_charmap();
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003845 if (!(parse_flag & PARSEFLAG_SEMICOLON) || (parse_flag & PARSEFLAG_REPARSING))
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003846 set_in_charmap(";$&|", CHAR_ORDINARY);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003847#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00003848 inp->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003849#endif
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003850 /* We will stop & execute after each ';' or '\n'.
3851 * Example: "sleep 9999; echo TEST" + ctrl-C:
3852 * TEST should be printed */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003853 rcode = parse_stream(&temp, &ctx, inp, ";\n");
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003854 if (rcode != 1 && ctx.old_flag != 0) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003855 syntax(NULL);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003856 }
3857 if (rcode != 1 && ctx.old_flag == 0) {
3858 done_word(&temp, &ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003859 done_pipe(&ctx, PIPE_SEQ);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00003860 debug_print_tree(ctx.list_head, 0);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003861 debug_printf_exec("parse_stream_outer: run_and_free_list\n");
3862 run_and_free_list(ctx.list_head);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003863 } else {
3864 if (ctx.old_flag != 0) {
3865 free(ctx.stack);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003866 o_reset(&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003867 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003868 temp.nonnull = 0;
Denis Vlasenkoff097622007-10-01 10:00:45 +00003869 temp.o_quote = 0;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003870 inp->p = NULL;
Denis Vlasenko05743d72008-02-10 12:10:08 +00003871 free_pipe_list(ctx.list_head, /* indent: */ 0);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003872 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003873 o_free(&temp);
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003874 } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP)); /* loop on syntax errors, return on EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003875 return 0;
3876}
3877
Denis Vlasenko170435c2007-05-23 13:01:10 +00003878static int parse_and_run_string(const char *s, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003879{
3880 struct in_str input;
3881 setup_string_in_str(&input, s);
Denis Vlasenko170435c2007-05-23 13:01:10 +00003882 return parse_and_run_stream(&input, parse_flag);
Eric Andersen25f27032001-04-26 23:22:31 +00003883}
3884
Denis Vlasenko170435c2007-05-23 13:01:10 +00003885static int parse_and_run_file(FILE *f)
Eric Andersen25f27032001-04-26 23:22:31 +00003886{
3887 int rcode;
3888 struct in_str input;
3889 setup_file_in_str(&input, f);
Denis Vlasenko170435c2007-05-23 13:01:10 +00003890 rcode = parse_and_run_stream(&input, PARSEFLAG_SEMICOLON);
Eric Andersen25f27032001-04-26 23:22:31 +00003891 return rcode;
3892}
3893
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003894#if ENABLE_HUSH_JOB
Eric Andersen6c947d22001-06-25 22:24:38 +00003895/* Make sure we have a controlling tty. If we get started under a job
3896 * aware app (like bash for example), make sure we are now in charge so
3897 * we don't fight over who gets the foreground */
Eric Anderseneaecbf32001-10-31 10:41:31 +00003898static void setup_job_control(void)
Eric Andersen52a97ca2001-06-22 06:49:26 +00003899{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003900 pid_t shell_pgrp;
3901
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003902 saved_task_pgrp = shell_pgrp = getpgrp();
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003903 debug_printf_jobs("saved_task_pgrp=%d\n", saved_task_pgrp);
Denis Vlasenko96e1b382007-09-30 23:50:48 +00003904 close_on_exec_on(interactive_fd);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003905
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003906 /* If we were ran as 'hush &',
3907 * sleep until we are in the foreground. */
3908 while (tcgetpgrp(interactive_fd) != shell_pgrp) {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003909 /* Send TTIN to ourself (should stop us) */
Denis Vlasenkoff131b92007-04-10 15:42:06 +00003910 kill(- shell_pgrp, SIGTTIN);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003911 shell_pgrp = getpgrp();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003912 }
Eric Andersen52a97ca2001-06-22 06:49:26 +00003913
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003914 /* Ignore job-control and misc signals. */
3915 set_jobctrl_sighandler(SIG_IGN);
3916 set_misc_sighandler(SIG_IGN);
3917//huh? signal(SIGCHLD, SIG_IGN);
3918
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003919 /* We _must_ restore tty pgrp on fatal signals */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003920 set_fatal_sighandler(sigexit);
Eric Andersen6c947d22001-06-25 22:24:38 +00003921
3922 /* Put ourselves in our own process group. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003923 setpgrp(); /* is the same as setpgid(our_pid, our_pid); */
Eric Andersen6c947d22001-06-25 22:24:38 +00003924 /* Grab control of the terminal. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003925 tcsetpgrp(interactive_fd, getpid());
Eric Andersen6c947d22001-06-25 22:24:38 +00003926}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003927#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00003928
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00003929int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Matt Kraai2d91deb2001-08-01 17:21:35 +00003930int hush_main(int argc, char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00003931{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00003932 static const char version_str[] ALIGN1 = "HUSH_VERSION="HUSH_VER_STR;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003933 static const struct variable const_shell_ver = {
3934 .next = NULL,
3935 .varstr = (char*)version_str,
3936 .max_len = 1, /* 0 can provoke free(name) */
3937 .flg_export = 1,
3938 .flg_read_only = 1,
3939 };
3940
Eric Andersen25f27032001-04-26 23:22:31 +00003941 int opt;
3942 FILE *input;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003943 char **e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003944 struct variable *cur_var;
Eric Andersenbc604a22001-05-16 05:24:03 +00003945
Denis Vlasenko574f2f42008-02-27 18:41:59 +00003946 INIT_G();
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003947
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003948 /* Deal with HUSH_VERSION */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003949 shell_ver = const_shell_ver; /* copying struct here */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003950 top_var = &shell_ver;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003951 unsetenv("HUSH_VERSION"); /* in case it exists in initial env */
3952 /* Initialize our shell local variables with the values
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003953 * currently living in the environment */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003954 cur_var = top_var;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003955 e = environ;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003956 if (e) while (*e) {
3957 char *value = strchr(*e, '=');
3958 if (value) { /* paranoia */
3959 cur_var->next = xzalloc(sizeof(*cur_var));
3960 cur_var = cur_var->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003961 cur_var->varstr = *e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003962 cur_var->max_len = strlen(*e);
3963 cur_var->flg_export = 1;
3964 }
3965 e++;
3966 }
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003967 putenv((char *)version_str); /* reinstate HUSH_VERSION */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003968
Denis Vlasenko38f63192007-01-22 09:03:07 +00003969#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003970 line_input_state = new_line_input_t(FOR_SHELL);
3971#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003972 /* XXX what should these be while sourcing /etc/profile? */
3973 global_argc = argc;
3974 global_argv = argv;
Eric Andersen94ac2442001-05-22 19:05:18 +00003975 /* Initialize some more globals to non-zero values */
3976 set_cwd();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003977#if ENABLE_HUSH_INTERACTIVE
3978#if ENABLE_FEATURE_EDITING
3979 cmdedit_set_initial_prompt();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003980#endif
Eric Andersen94ac2442001-05-22 19:05:18 +00003981 PS2 = "> ";
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003982#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003983
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003984 if (EXIT_SUCCESS) /* otherwise is already done */
3985 last_return_code = EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00003986
3987 if (argv[0] && argv[0][0] == '-') {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003988 debug_printf("sourcing /etc/profile\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003989 input = fopen("/etc/profile", "r");
3990 if (input != NULL) {
Denis Vlasenkoa0898172007-10-01 09:59:01 +00003991 close_on_exec_on(fileno(input));
Denis Vlasenko170435c2007-05-23 13:01:10 +00003992 parse_and_run_file(input);
Eric Andersena90f20b2001-06-26 23:00:21 +00003993 fclose(input);
3994 }
Eric Andersen25f27032001-04-26 23:22:31 +00003995 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003996 input = stdin;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003997
Eric Andersen25f27032001-04-26 23:22:31 +00003998 while ((opt = getopt(argc, argv, "c:xif")) > 0) {
3999 switch (opt) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004000 case 'c':
Denis Vlasenko5f786c22007-04-20 08:35:45 +00004001 global_argv = argv + optind;
4002 global_argc = argc - optind;
Denis Vlasenko170435c2007-05-23 13:01:10 +00004003 opt = parse_and_run_string(optarg, PARSEFLAG_SEMICOLON);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004004 goto final_return;
4005 case 'i':
Denis Vlasenkoc666f712007-05-16 22:18:54 +00004006 /* Well, we cannot just declare interactiveness,
4007 * we have to have some stuff (ctty, etc) */
4008 /* interactive_fd++; */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004009 break;
4010 case 'f':
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00004011 fake_mode = 1;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004012 break;
4013 default:
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004014#ifndef BB_VER
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004015 fprintf(stderr, "Usage: sh [FILE]...\n"
4016 " or: sh -c command [args]...\n\n");
4017 exit(EXIT_FAILURE);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004018#else
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004019 bb_show_usage();
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004020#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004021 }
4022 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004023#if ENABLE_HUSH_JOB
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004024 /* A shell is interactive if the '-i' flag was given, or if all of
Eric Andersen25f27032001-04-26 23:22:31 +00004025 * the following conditions are met:
Denis Vlasenko55b2de72007-04-18 17:21:28 +00004026 * no -c command
Eric Andersen25f27032001-04-26 23:22:31 +00004027 * no arguments remaining or the -s flag given
4028 * standard input is a terminal
4029 * standard output is a terminal
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004030 * Refer to Posix.2, the description of the 'sh' utility. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004031 if (argv[optind] == NULL && input == stdin
4032 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
4033 ) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004034 saved_tty_pgrp = tcgetpgrp(STDIN_FILENO);
4035 debug_printf("saved_tty_pgrp=%d\n", saved_tty_pgrp);
4036 if (saved_tty_pgrp >= 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004037 /* try to dup to high fd#, >= 255 */
4038 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
4039 if (interactive_fd < 0) {
4040 /* try to dup to any fd */
4041 interactive_fd = dup(STDIN_FILENO);
4042 if (interactive_fd < 0)
4043 /* give up */
4044 interactive_fd = 0;
4045 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004046 // TODO: track & disallow any attempts of user
4047 // to (inadvertently) close/redirect it
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004048 }
Eric Andersen25f27032001-04-26 23:22:31 +00004049 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00004050 debug_printf("interactive_fd=%d\n", interactive_fd);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004051 if (interactive_fd) {
Denis Vlasenko08126f62008-02-11 08:39:11 +00004052 fcntl(interactive_fd, F_SETFD, FD_CLOEXEC);
Eric Andersen25f27032001-04-26 23:22:31 +00004053 /* Looks like they want an interactive shell */
Eric Andersen52a97ca2001-06-22 06:49:26 +00004054 setup_job_control();
Denis Vlasenko4ecfcdc2008-02-11 08:32:31 +00004055 /* -1 is special - makes xfuncs longjmp, not exit
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00004056 * (we reset die_sleep = 0 whereever we [v]fork) */
4057 die_sleep = -1;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004058 if (setjmp(die_jmp)) {
4059 /* xfunc has failed! die die die */
4060 hush_exit(xfunc_error_retval);
4061 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004062#if !ENABLE_FEATURE_SH_EXTRA_QUIET
Denis Vlasenkoca525b42007-06-13 12:27:17 +00004063 printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", bb_banner);
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004064 printf("Enter 'help' for a list of built-in commands.\n\n");
4065#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00004066 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004067#elif ENABLE_HUSH_INTERACTIVE
4068/* no job control compiled, only prompt/line editing */
4069 if (argv[optind] == NULL && input == stdin
4070 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
4071 ) {
4072 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
4073 if (interactive_fd < 0) {
4074 /* try to dup to any fd */
4075 interactive_fd = dup(STDIN_FILENO);
4076 if (interactive_fd < 0)
4077 /* give up */
4078 interactive_fd = 0;
4079 }
Denis Vlasenko4830fc52008-05-25 21:50:55 +00004080 if (interactive_fd) {
Denis Vlasenko08126f62008-02-11 08:39:11 +00004081 fcntl(interactive_fd, F_SETFD, FD_CLOEXEC);
Denis Vlasenko4830fc52008-05-25 21:50:55 +00004082 set_misc_sighandler(SIG_IGN);
4083 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004084 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004085#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004086
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004087 if (argv[optind] == NULL) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00004088 opt = parse_and_run_file(stdin);
Denis Vlasenko05743d72008-02-10 12:10:08 +00004089 } else {
4090 debug_printf("\nrunning script '%s'\n", argv[optind]);
4091 global_argv = argv + optind;
4092 global_argc = argc - optind;
4093 input = xfopen(argv[optind], "r");
Denis Vlasenko459a5ad2008-02-11 08:35:03 +00004094 fcntl(fileno(input), F_SETFD, FD_CLOEXEC);
Denis Vlasenko05743d72008-02-10 12:10:08 +00004095 opt = parse_and_run_file(input);
Eric Andersen25f27032001-04-26 23:22:31 +00004096 }
Eric Andersen25f27032001-04-26 23:22:31 +00004097
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004098 final_return:
4099
Denis Vlasenko38f63192007-01-22 09:03:07 +00004100#if ENABLE_FEATURE_CLEAN_UP
Eric Andersenaeb44c42001-05-22 20:29:00 +00004101 fclose(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004102 if (cwd != bb_msg_unknown)
Eric Andersenaeb44c42001-05-22 20:29:00 +00004103 free((char*)cwd);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004104 cur_var = top_var->next;
4105 while (cur_var) {
4106 struct variable *tmp = cur_var;
4107 if (!cur_var->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00004108 free(cur_var->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004109 cur_var = cur_var->next;
4110 free(tmp);
Eric Andersenaeb44c42001-05-22 20:29:00 +00004111 }
Eric Andersen25f27032001-04-26 23:22:31 +00004112#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004113 hush_exit(opt ? opt : last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +00004114}
Denis Vlasenko96702ca2007-11-23 23:28:55 +00004115
4116
4117#if ENABLE_LASH
4118int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
4119int lash_main(int argc, char **argv)
4120{
4121 //bb_error_msg("lash is deprecated, please use hush instead");
4122 return hush_main(argc, argv);
4123}
4124#endif