blob: 0b92c29d4f7f726cbdd775ea7c17796758cf483c [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 Vlasenkoa8442002008-06-14 11:00:17 +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
Denis Vlasenkoa8442002008-06-14 11:00:17 +000026 * miscellaneous bugfixes from Matt Kraai.
Eric Andersen25f27032001-04-26 23:22:31 +000027 *
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 * $_
Eric Andersen25f27032001-04-26 23:22:31 +000041 * &> and >& redirection of stdout+stderr
42 * Brace Expansion
43 * Tilde Expansion
44 * fancy forms of Parameter Expansion
Eric Andersen78a7c992001-05-15 16:30:25 +000045 * aliases
Eric Andersen25f27032001-04-26 23:22:31 +000046 * Arithmetic Expansion
47 * <(list) and >(list) Process Substitution
Eric Andersen83a2ae22001-05-07 17:59:25 +000048 * reserved words: case, esac, select, function
Eric Andersen25f27032001-04-26 23:22:31 +000049 * Here Documents ( << word )
50 * Functions
51 * Major bugs:
Denis Vlasenkob81b3df2007-04-28 16:48:04 +000052 * job handling woefully incomplete and buggy (improved --vda)
Eric Andersen25f27032001-04-26 23:22:31 +000053 * reserved word execution woefully incomplete and buggy
Eric Andersen25f27032001-04-26 23:22:31 +000054 * to-do:
Eric Andersen83a2ae22001-05-07 17:59:25 +000055 * port selected bugfixes from post-0.49 busybox lash - done?
Eric Andersen83a2ae22001-05-07 17:59:25 +000056 * change { and } from special chars to reserved words
57 * builtins: break, continue, eval, return, set, trap, ulimit
58 * test magic exec
Eric Andersen25f27032001-04-26 23:22:31 +000059 * check setting of global_argc and global_argv
Eric Andersen25f27032001-04-26 23:22:31 +000060 * follow IFS rules more precisely, including update semantics
Eric Andersen25f27032001-04-26 23:22:31 +000061 * figure out what to do with backslash-newline
Eric Andersen25f27032001-04-26 23:22:31 +000062 * propagate syntax errors, die on resource errors?
63 * continuation lines, both explicit and implicit - done?
64 * memory leak finding and plugging - done?
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +000065 * maybe change charmap[] to use 2-bit entries
Eric Andersen25f27032001-04-26 23:22:31 +000066 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000067 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen25f27032001-04-26 23:22:31 +000068 */
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000069
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000070
Eric Andersen25f27032001-04-26 23:22:31 +000071#include <glob.h> /* glob, of course */
Eric Andersen25f27032001-04-26 23:22:31 +000072/* #include <dmalloc.h> */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000073
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000074#include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000075
Denis Vlasenkod01ff132007-05-02 21:40:23 +000076
Denis Vlasenko05743d72008-02-10 12:10:08 +000077#if !BB_MMU && ENABLE_HUSH_TICK
78//#undef ENABLE_HUSH_TICK
79//#define ENABLE_HUSH_TICK 0
80#warning On NOMMU, hush command substitution is dangerous.
81#warning Dont use it for commands which produce lots of output.
82#warning For more info see shell/hush.c, generate_stream_from_list().
83#endif
84
85#if !BB_MMU && ENABLE_HUSH_JOB
86#undef ENABLE_HUSH_JOB
87#define ENABLE_HUSH_JOB 0
88#endif
89
90#if !ENABLE_HUSH_INTERACTIVE
91#undef ENABLE_FEATURE_EDITING
92#define ENABLE_FEATURE_EDITING 0
93#undef ENABLE_FEATURE_EDITING_FANCY_PROMPT
94#define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0
Denis Vlasenko8412d792007-10-01 09:59:47 +000095#endif
96
97
Denis Vlasenkod01ff132007-05-02 21:40:23 +000098/* If you comment out one of these below, it will be #defined later
99 * to perform debug printfs to stderr: */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000100#define debug_printf(...) do {} while (0)
Denis Vlasenko400c5b62007-05-04 13:07:27 +0000101/* Finer-grained debug switches */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000102#define debug_printf_parse(...) do {} while (0)
103#define debug_print_tree(a, b) do {} while (0)
104#define debug_printf_exec(...) do {} while (0)
105#define debug_printf_jobs(...) do {} while (0)
106#define debug_printf_expand(...) do {} while (0)
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000107#define debug_printf_glob(...) do {} while (0)
108#define debug_printf_list(...) do {} while (0)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000109#define debug_printf_clean(...) do {} while (0)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000110
111#ifndef debug_printf
112#define debug_printf(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000113#endif
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000114
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000115#ifndef debug_printf_parse
116#define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000117#endif
118
119#ifndef debug_printf_exec
120#define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__)
121#endif
122
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000123#ifndef debug_printf_jobs
124#define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000125#define DEBUG_JOBS 1
126#else
127#define DEBUG_JOBS 0
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000128#endif
129
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000130#ifndef debug_printf_expand
131#define debug_printf_expand(...) fprintf(stderr, __VA_ARGS__)
132#define DEBUG_EXPAND 1
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000133#else
134#define DEBUG_EXPAND 0
135#endif
136
137#ifndef debug_printf_glob
138#define debug_printf_glob(...) fprintf(stderr, __VA_ARGS__)
139#define DEBUG_GLOB 1
140#else
141#define DEBUG_GLOB 0
142#endif
143
144#ifndef debug_printf_list
145#define debug_printf_list(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000146#endif
147
Denis Vlasenko170435c2007-05-23 13:01:10 +0000148/* Keep unconditionally on for now */
149#define ENABLE_HUSH_DEBUG 1
150
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000151#ifndef debug_printf_clean
152/* broken, of course, but OK for testing */
153static const char *indenter(int i)
154{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000155 static const char blanks[] ALIGN1 =
156 " ";
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000157 return &blanks[sizeof(blanks) - i - 1];
158}
159#define debug_printf_clean(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000160#define DEBUG_CLEAN 1
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000161#endif
162
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000163#if DEBUG_EXPAND
164static void debug_print_strings(const char *prefix, char **vv)
165{
166 fprintf(stderr, "%s:\n", prefix);
167 while (*vv)
168 fprintf(stderr, " '%s'\n", *vv++);
169}
170#else
171#define debug_print_strings(prefix, vv) ((void)0)
172#endif
173
Eric Andersen25f27032001-04-26 23:22:31 +0000174
Denis Vlasenkof962a032007-11-23 12:50:54 +0000175/*
176 * Leak hunting. Use hush_leaktool.sh for post-processing.
177 */
178#ifdef FOR_HUSH_LEAKTOOL
Denis Vlasenkoccce59d2008-06-16 14:35:57 +0000179/* suppress "warning: no previous prototype..." */
180void *xxmalloc(int lineno, size_t size);
181void *xxrealloc(int lineno, void *ptr, size_t size);
182char *xxstrdup(int lineno, const char *str);
183void xxfree(void *ptr);
Denis Vlasenkof962a032007-11-23 12:50:54 +0000184void *xxmalloc(int lineno, size_t size)
185{
186 void *ptr = xmalloc((size + 0xff) & ~0xff);
187 fprintf(stderr, "line %d: malloc %p\n", lineno, ptr);
188 return ptr;
189}
190void *xxrealloc(int lineno, void *ptr, size_t size)
191{
192 ptr = xrealloc(ptr, (size + 0xff) & ~0xff);
193 fprintf(stderr, "line %d: realloc %p\n", lineno, ptr);
194 return ptr;
195}
196char *xxstrdup(int lineno, const char *str)
197{
198 char *ptr = xstrdup(str);
199 fprintf(stderr, "line %d: strdup %p\n", lineno, ptr);
200 return ptr;
201}
202void xxfree(void *ptr)
203{
204 fprintf(stderr, "free %p\n", ptr);
205 free(ptr);
206}
207#define xmalloc(s) xxmalloc(__LINE__, s)
208#define xrealloc(p, s) xxrealloc(__LINE__, p, s)
209#define xstrdup(s) xxstrdup(__LINE__, s)
210#define free(p) xxfree(p)
211#endif
212
213
Denis Vlasenko5703c222008-06-15 11:49:42 +0000214#define SPECIAL_VAR_SYMBOL 3
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000215#define PARSEFLAG_EXIT_FROM_LOOP 1
Eric Andersen25f27032001-04-26 23:22:31 +0000216
217typedef enum {
218 REDIRECT_INPUT = 1,
219 REDIRECT_OVERWRITE = 2,
220 REDIRECT_APPEND = 3,
221 REDIRECT_HEREIS = 4,
222 REDIRECT_IO = 5
223} redir_type;
224
225/* The descrip member of this structure is only used to make debugging
226 * output pretty */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000227static const struct {
228 int mode;
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000229 signed char default_fd;
230 char descrip[3];
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000231} redir_table[] = {
Eric Andersen25f27032001-04-26 23:22:31 +0000232 { 0, 0, "()" },
233 { O_RDONLY, 0, "<" },
234 { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
235 { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" },
236 { O_RDONLY, -1, "<<" },
237 { O_RDWR, 1, "<>" }
238};
239
240typedef enum {
241 PIPE_SEQ = 1,
242 PIPE_AND = 2,
243 PIPE_OR = 3,
244 PIPE_BG = 4,
245} pipe_style;
246
247/* might eventually control execution */
248typedef enum {
249 RES_NONE = 0,
Denis Vlasenko06810332007-05-21 23:30:54 +0000250#if ENABLE_HUSH_IF
Eric Andersen25f27032001-04-26 23:22:31 +0000251 RES_IF = 1,
252 RES_THEN = 2,
253 RES_ELIF = 3,
254 RES_ELSE = 4,
255 RES_FI = 5,
Denis Vlasenko06810332007-05-21 23:30:54 +0000256#endif
257#if ENABLE_HUSH_LOOPS
Eric Andersen25f27032001-04-26 23:22:31 +0000258 RES_FOR = 6,
259 RES_WHILE = 7,
260 RES_UNTIL = 8,
261 RES_DO = 9,
262 RES_DONE = 10,
Denis Vlasenko06810332007-05-21 23:30:54 +0000263 RES_IN = 11,
264#endif
265 RES_XXXX = 12,
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000266 RES_SNTX = 13
Eric Andersen25f27032001-04-26 23:22:31 +0000267} reserved_style;
Eric Andersen25f27032001-04-26 23:22:31 +0000268/* This holds pointers to the various results of parsing */
269struct p_context {
270 struct child_prog *child;
271 struct pipe *list_head;
272 struct pipe *pipe;
273 struct redir_struct *pending_redirect;
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000274 smallint res_w;
Denis Vlasenkoa8442002008-06-14 11:00:17 +0000275 smallint ctx_inverted; /* "! cmd | cmd" */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000276 int old_flag; /* bitmask of FLAG_xxx, for figuring out valid reserved words */
Eric Andersen25f27032001-04-26 23:22:31 +0000277 struct p_context *stack;
Eric Andersen25f27032001-04-26 23:22:31 +0000278};
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 Vlasenkoa8442002008-06-14 11:00:17 +0000316 smallint pi_inverted; /* "! cmd | cmd" */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000317 smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
318 smallint res_word; /* needed for if, for, while, until... */
Eric Andersen25f27032001-04-26 23:22:31 +0000319};
320
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000321/* On program start, environ points to initial environment.
322 * putenv adds new pointers into it, unsetenv removes them.
323 * Neither of these (de)allocates the strings.
324 * setenv allocates new strings in malloc space and does putenv,
325 * and thus setenv is unusable (leaky) for shell's purposes */
326#define setenv(...) setenv_is_leaky_dont_use()
327struct variable {
328 struct variable *next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000329 char *varstr; /* points to "name=" portion */
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000330 int max_len; /* if > 0, name is part of initial env; else name is malloced */
331 smallint flg_export; /* putenv should be done on this var */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000332 smallint flg_read_only;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000333};
334
Eric Andersen25f27032001-04-26 23:22:31 +0000335typedef struct {
336 char *data;
337 int length;
338 int maxlen;
Denis Vlasenkoff097622007-10-01 10:00:45 +0000339 smallint o_quote;
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000340 smallint o_glob;
Denis Vlasenkoff097622007-10-01 10:00:45 +0000341 smallint nonnull;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000342 smallint has_empty_slot;
Eric Andersen25f27032001-04-26 23:22:31 +0000343} o_string;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000344#define NULL_O_STRING { NULL }
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000345/* used for initialization: o_string foo = NULL_O_STRING; */
Eric Andersen25f27032001-04-26 23:22:31 +0000346
347/* I can almost use ordinary FILE *. Is open_memstream() universally
348 * available? Where is it documented? */
349struct in_str {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +0000350 const char *p;
351 /* eof_flag=1: last char in ->p is really an EOF */
352 char eof_flag; /* meaningless if ->p == NULL */
353 char peek_buf[2];
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000354#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000355 smallint promptme;
356 smallint promptmode; /* 0: PS1, 1: PS2 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000357#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000358 FILE *file;
359 int (*get) (struct in_str *);
360 int (*peek) (struct in_str *);
361};
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000362#define i_getch(input) ((input)->get(input))
363#define i_peek(input) ((input)->peek(input))
Eric Andersen25f27032001-04-26 23:22:31 +0000364
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000365enum {
366 CHAR_ORDINARY = 0,
367 CHAR_ORDINARY_IF_QUOTED = 1, /* example: *, # */
368 CHAR_IFS = 2, /* treated as ordinary if quoted */
369 CHAR_SPECIAL = 3, /* example: $ */
Eric Andersen25f27032001-04-26 23:22:31 +0000370};
371
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000372#define HUSH_VER_STR "0.02"
373
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000374/* "Globals" within this file */
375
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000376/* Sorted roughly by size (smaller offsets == smaller code) */
377struct globals {
378#if ENABLE_HUSH_INTERACTIVE
379 /* 'interactive_fd' is a fd# open to ctty, if we have one
380 * _AND_ if we decided to act interactively */
381 int interactive_fd;
382 const char *PS1;
383 const char *PS2;
384#endif
385#if ENABLE_FEATURE_EDITING
386 line_input_t *line_input_state;
387#endif
388#if ENABLE_HUSH_JOB
389 int run_list_level;
390 pid_t saved_task_pgrp;
391 pid_t saved_tty_pgrp;
392 int last_jobid;
393 struct pipe *job_list;
394 struct pipe *toplevel_list;
395 smallint ctrl_z_flag;
396#endif
397 smallint fake_mode;
398 /* these three support $?, $#, and $1 */
399 char **global_argv;
400 int global_argc;
401 int last_return_code;
402 const char *ifs;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000403 const char *cwd;
404 unsigned last_bg_pid;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +0000405 struct variable *top_var; /* = &shell_ver (set in main()) */
406 struct variable shell_ver;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000407#if ENABLE_FEATURE_SH_STANDALONE
408 struct nofork_save_area nofork_save;
409#endif
410#if ENABLE_HUSH_JOB
411 sigjmp_buf toplevel_jb;
412#endif
413 unsigned char charmap[256];
414 char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2];
415};
416
417#define G (*ptr_to_globals)
418
419#if !ENABLE_HUSH_INTERACTIVE
420enum { interactive_fd = 0 };
421#endif
422#if !ENABLE_HUSH_JOB
423enum { run_list_level = 0 };
424#endif
425
426#if ENABLE_HUSH_INTERACTIVE
427#define interactive_fd (G.interactive_fd )
428#define PS1 (G.PS1 )
429#define PS2 (G.PS2 )
430#endif
431#if ENABLE_FEATURE_EDITING
432#define line_input_state (G.line_input_state)
433#endif
434#if ENABLE_HUSH_JOB
435#define run_list_level (G.run_list_level )
436#define saved_task_pgrp (G.saved_task_pgrp )
437#define saved_tty_pgrp (G.saved_tty_pgrp )
438#define last_jobid (G.last_jobid )
439#define job_list (G.job_list )
440#define toplevel_list (G.toplevel_list )
441#define toplevel_jb (G.toplevel_jb )
442#define ctrl_z_flag (G.ctrl_z_flag )
443#endif /* JOB */
444#define global_argv (G.global_argv )
445#define global_argc (G.global_argc )
446#define last_return_code (G.last_return_code)
447#define ifs (G.ifs )
448#define fake_mode (G.fake_mode )
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000449#define cwd (G.cwd )
450#define last_bg_pid (G.last_bg_pid )
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000451#define top_var (G.top_var )
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000452#define shell_ver (G.shell_ver )
453#if ENABLE_FEATURE_SH_STANDALONE
454#define nofork_save (G.nofork_save )
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000455#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000456#if ENABLE_HUSH_JOB
457#define toplevel_jb (G.toplevel_jb )
458#endif
459#define charmap (G.charmap )
460#define user_input_buf (G.user_input_buf )
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000461#define INIT_G() do { \
462 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
463} while (0)
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000464
465
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000466#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
467
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000468#if 1
469/* Normal */
470static void syntax(const char *msg)
471{
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000472 /* Was using fancy stuff:
473 * (interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...)
474 * but it SEGVs. ?! Oh well... explicit temp ptr works around that */
475 void (*fp)(const char *s, ...);
476
477 fp = (interactive_fd ? bb_error_msg : bb_error_msg_and_die);
478 fp(msg ? "%s: %s" : "syntax error", "syntax error", msg);
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000479}
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000480
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000481#else
482/* Debug */
483static void syntax_lineno(int line)
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000484{
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000485 void (*fp)(const char *s, ...);
486
487 fp = (interactive_fd ? bb_error_msg : bb_error_msg_and_die);
488 fp("syntax error hush.c:%d", line);
Eric Andersen25f27032001-04-26 23:22:31 +0000489}
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000490#define syntax(str) syntax_lineno(__LINE__)
491#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000492
493/* Index of subroutines: */
Eric Andersen25f27032001-04-26 23:22:31 +0000494/* in_str manipulations: */
495static int static_get(struct in_str *i);
496static int static_peek(struct in_str *i);
497static int file_get(struct in_str *i);
498static int file_peek(struct in_str *i);
499static void setup_file_in_str(struct in_str *i, FILE *f);
500static void setup_string_in_str(struct in_str *i, const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000501/* "run" the final data structures: */
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000502#if !defined(DEBUG_CLEAN)
503#define free_pipe_list(head, indent) free_pipe_list(head)
504#define free_pipe(pi, indent) free_pipe(pi)
505#endif
Eric Andersenbf7df042001-05-23 22:18:35 +0000506static int free_pipe_list(struct pipe *head, int indent);
507static int free_pipe(struct pipe *pi, int indent);
Eric Andersen25f27032001-04-26 23:22:31 +0000508/* really run the final data structures: */
509static int setup_redirects(struct child_prog *prog, int squirrel[]);
Denis Vlasenko05743d72008-02-10 12:10:08 +0000510static int run_list(struct pipe *pi);
Denis Vlasenko76d50412008-06-10 16:19:39 +0000511#if BB_MMU
512#define pseudo_exec_argv(ptrs2free, argv) pseudo_exec_argv(argv)
513#define pseudo_exec(ptrs2free, child) pseudo_exec(child)
514#endif
515static void pseudo_exec_argv(char **ptrs2free, char **argv) ATTRIBUTE_NORETURN;
516static void pseudo_exec(char **ptrs2free, struct child_prog *child) ATTRIBUTE_NORETURN;
Denis Vlasenko05743d72008-02-10 12:10:08 +0000517static int run_pipe(struct pipe *pi);
Eric Andersen78a7c992001-05-15 16:30:25 +0000518/* variable assignment: */
Eric Andersen78a7c992001-05-15 16:30:25 +0000519static int is_assignment(const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000520/* data structure manipulation: */
521static int setup_redirect(struct p_context *ctx, int fd, redir_type style, struct in_str *input);
522static void initialize_context(struct p_context *ctx);
523static int done_word(o_string *dest, struct p_context *ctx);
524static int done_command(struct p_context *ctx);
Denis Vlasenkoa8442002008-06-14 11:00:17 +0000525static void done_pipe(struct p_context *ctx, pipe_style type);
Eric Andersen25f27032001-04-26 23:22:31 +0000526/* primary string parsing: */
527static int redirect_dup_num(struct in_str *input);
528static int redirect_opt_num(o_string *o);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000529#if ENABLE_HUSH_TICK
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000530static int process_command_subs(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000531 struct in_str *input, const char *subst_end);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000532#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000533static int parse_group(o_string *dest, struct p_context *ctx, struct in_str *input, int ch);
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000534static const char *lookup_param(const char *src);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000535static int handle_dollar(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000536 struct in_str *input);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000537static 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 +0000538/* setup: */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000539static int parse_and_run_stream(struct in_str *inp, int parse_flag);
540static int parse_and_run_string(const char *s, int parse_flag);
541static int parse_and_run_file(FILE *f);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000542/* job management: */
Eric Andersenc798b072001-06-22 06:23:03 +0000543static int checkjobs(struct pipe* fg_pipe);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000544#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +0000545static int checkjobs_and_fg_shell(struct pipe* fg_pipe);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000546static void insert_bg_job(struct pipe *pi);
547static void remove_bg_job(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000548static void delete_finished_bg_job(struct pipe *pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000549#else
550int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */
551#endif
Eric Andersenf72f5622001-05-15 23:21:41 +0000552/* local variable support */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000553static char **expand_strvec_to_strvec(char **argv);
554/* used for eval */
555static char *expand_strvec_to_string(char **argv);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +0000556/* used for expansion of right hand of assignments */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000557static char *expand_string_to_string(const char *str);
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000558static struct variable *get_local_var(const char *name);
559static int set_local_var(char *str, int flg_export);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000560static void unset_local_var(const char *name);
Eric Andersen25f27032001-04-26 23:22:31 +0000561
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000562
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000563static int glob_needed(const char *s)
564{
565 while (*s) {
566 if (*s == '\\')
567 s++;
568 if (*s == '*' || *s == '[' || *s == '?')
569 return 1;
570 s++;
571 }
572 return 0;
573}
574
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000575static char **add_strings_to_strings(int need_xstrdup, char **strings, char **add)
576{
577 int i;
578 unsigned count1;
579 unsigned count2;
580 char **v;
581
582 v = strings;
583 count1 = 0;
584 if (v) {
585 while (*v) {
586 count1++;
587 v++;
588 }
589 }
590 count2 = 0;
591 v = add;
592 while (*v) {
593 count2++;
594 v++;
595 }
596 v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*));
597 v[count1 + count2] = NULL;
598 i = count2;
599 while (--i >= 0)
600 v[count1 + i] = need_xstrdup ? xstrdup(add[i]) : add[i];
601 return v;
602}
603
604/* 'add' should be a malloced pointer */
605static char **add_string_to_strings(char **strings, char *add)
606{
607 char *v[2];
608
609 v[0] = add;
610 v[1] = NULL;
611
612 return add_strings_to_strings(0, strings, v);
613}
614
615static void free_strings(char **strings)
616{
617 if (strings) {
618 char **v = strings;
619 while (*v)
620 free(*v++);
621 free(strings);
622 }
623}
624
Denis Vlasenko76d50412008-06-10 16:19:39 +0000625#if !BB_MMU
626#define EXTRA_PTRS 5 /* 1 for NULL, 1 for args, 3 for paranoid reasons */
627static char **alloc_ptrs(char **argv)
628{
629 char **v = argv;
630 while (*v)
631 v++;
632 return xzalloc((v - argv + EXTRA_PTRS) * sizeof(v[0]));
633}
634#endif
635
636
Denis Vlasenko83506862007-11-23 13:11:42 +0000637/* Function prototypes for builtins */
638static int builtin_cd(char **argv);
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000639static int builtin_echo(char **argv);
Denis Vlasenko83506862007-11-23 13:11:42 +0000640static int builtin_eval(char **argv);
641static int builtin_exec(char **argv);
642static int builtin_exit(char **argv);
643static int builtin_export(char **argv);
644#if ENABLE_HUSH_JOB
645static int builtin_fg_bg(char **argv);
646static int builtin_jobs(char **argv);
647#endif
648#if ENABLE_HUSH_HELP
649static int builtin_help(char **argv);
650#endif
651static int builtin_pwd(char **argv);
652static int builtin_read(char **argv);
653static int builtin_test(char **argv);
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000654static int builtin_true(char **argv);
Denis Vlasenko83506862007-11-23 13:11:42 +0000655static int builtin_set(char **argv);
656static int builtin_shift(char **argv);
657static int builtin_source(char **argv);
658static int builtin_umask(char **argv);
659static int builtin_unset(char **argv);
660//static int builtin_not_written(char **argv);
661
Eric Andersen25f27032001-04-26 23:22:31 +0000662/* Table of built-in functions. They can be forked or not, depending on
663 * context: within pipes, they fork. As simple commands, they do not.
664 * When used in non-forking context, they can change global variables
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000665 * in the parent shell process. If forked, of course they cannot.
Eric Andersen25f27032001-04-26 23:22:31 +0000666 * For example, 'unset foo | whatever' will parse and run, but foo will
667 * still be set at the end. */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000668struct built_in_command {
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000669 const char *cmd;
670 int (*function)(char **argv);
Denis Vlasenko06810332007-05-21 23:30:54 +0000671#if ENABLE_HUSH_HELP
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000672 const char *descr;
Denis Vlasenko06810332007-05-21 23:30:54 +0000673#define BLTIN(cmd, func, help) { cmd, func, help }
674#else
675#define BLTIN(cmd, func, help) { cmd, func }
676#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000677};
678
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000679/* For now, echo and test are unconditionally enabled.
680 * Maybe make it configurable? */
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +0000681static const struct built_in_command bltins[] = {
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000682 BLTIN("." , builtin_source, "Run commands in a file"),
683 BLTIN(":" , builtin_true, "No-op"),
Denis Vlasenko83506862007-11-23 13:11:42 +0000684 BLTIN("[" , builtin_test, "Test condition"),
685 BLTIN("[[" , builtin_test, "Test condition"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000686#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000687 BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000688#endif
Denis Vlasenko06810332007-05-21 23:30:54 +0000689// BLTIN("break" , builtin_not_written, "Exit for, while or until loop"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000690 BLTIN("cd" , builtin_cd, "Change directory"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000691// BLTIN("continue", builtin_not_written, "Continue for, while or until loop"),
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000692 BLTIN("echo" , builtin_echo, "Write strings to stdout"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000693 BLTIN("eval" , builtin_eval, "Construct and run shell command"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000694 BLTIN("exec" , builtin_exec, "Execute command, don't return to shell"),
695 BLTIN("exit" , builtin_exit, "Exit"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000696 BLTIN("export", builtin_export, "Set environment variable"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000697#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000698 BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000699 BLTIN("jobs" , builtin_jobs, "List active jobs"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000700#endif
Denis Vlasenko06810332007-05-21 23:30:54 +0000701 BLTIN("pwd" , builtin_pwd, "Print current directory"),
702 BLTIN("read" , builtin_read, "Input environment variable"),
703// BLTIN("return", builtin_not_written, "Return from a function"),
704 BLTIN("set" , builtin_set, "Set/unset shell local variables"),
705 BLTIN("shift" , builtin_shift, "Shift positional parameters"),
706// BLTIN("trap" , builtin_not_written, "Trap signals"),
Denis Vlasenko83506862007-11-23 13:11:42 +0000707 BLTIN("test" , builtin_test, "Test condition"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000708// BLTIN("ulimit", builtin_not_written, "Control resource limits"),
709 BLTIN("umask" , builtin_umask, "Set file creation mask"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000710 BLTIN("unset" , builtin_unset, "Unset environment variable"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000711#if ENABLE_HUSH_HELP
712 BLTIN("help" , builtin_help, "List shell built-in commands"),
713#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000714};
715
Denis Vlasenko4830fc52008-05-25 21:50:55 +0000716/* Signals are grouped, we handle them in batches */
717static void set_misc_sighandler(void (*handler)(int))
718{
719 bb_signals(0
720 + (1 << SIGINT)
721 + (1 << SIGQUIT)
722 + (1 << SIGTERM)
723 , handler);
724}
725
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000726#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000727
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000728static void set_fatal_sighandler(void (*handler)(int))
729{
Denis Vlasenko25591c32008-02-16 22:58:56 +0000730 bb_signals(0
731 + (1 << SIGILL)
732 + (1 << SIGTRAP)
733 + (1 << SIGABRT)
734 + (1 << SIGFPE)
735 + (1 << SIGBUS)
736 + (1 << SIGSEGV)
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000737 /* bash 3.2 seems to handle these just like 'fatal' ones */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000738 + (1 << SIGHUP)
739 + (1 << SIGPIPE)
740 + (1 << SIGALRM)
741 , handler);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000742}
743static void set_jobctrl_sighandler(void (*handler)(int))
744{
Denis Vlasenko25591c32008-02-16 22:58:56 +0000745 bb_signals(0
746 + (1 << SIGTSTP)
747 + (1 << SIGTTIN)
748 + (1 << SIGTTOU)
749 , handler);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000750}
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000751/* SIGCHLD is special and handled separately */
752
753static void set_every_sighandler(void (*handler)(int))
754{
755 set_fatal_sighandler(handler);
756 set_jobctrl_sighandler(handler);
757 set_misc_sighandler(handler);
758 signal(SIGCHLD, handler);
759}
760
Denis Vlasenko68404f12008-03-17 09:00:54 +0000761static void handler_ctrl_c(int sig ATTRIBUTE_UNUSED)
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000762{
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000763 debug_printf_jobs("got sig %d\n", sig);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000764// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000765 siglongjmp(toplevel_jb, 1);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000766}
767
Denis Vlasenko68404f12008-03-17 09:00:54 +0000768static void handler_ctrl_z(int sig ATTRIBUTE_UNUSED)
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000769{
770 pid_t pid;
771
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000772 debug_printf_jobs("got tty sig %d in pid %d\n", sig, getpid());
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000773 pid = fork();
Denis Vlasenkoc2990322007-05-16 12:57:12 +0000774 if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000775 return;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000776 ctrl_z_flag = 1;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000777 if (!pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +0000778 if (ENABLE_HUSH_JOB)
779 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000780 setpgrp();
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000781 debug_printf_jobs("set pgrp for child %d ok\n", getpid());
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000782 set_every_sighandler(SIG_DFL);
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000783 raise(SIGTSTP); /* resend TSTP so that child will be stopped */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000784 debug_printf_jobs("returning in child\n");
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000785 /* return to nofork, it will eventually exit now,
786 * not return back to shell */
787 return;
788 }
789 /* parent */
790 /* finish filling up pipe info */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000791 toplevel_list->pgrp = pid; /* child is in its own pgrp */
792 toplevel_list->progs[0].pid = pid;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000793 /* parent needs to longjmp out of running nofork.
794 * we will "return" exitcode 0, with child put in background */
795// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000796 debug_printf_jobs("siglongjmp in parent\n");
797 siglongjmp(toplevel_jb, 1);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000798}
799
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000800/* Restores tty foreground process group, and exits.
801 * May be called as signal handler for fatal signal
802 * (will faithfully resend signal to itself, producing correct exit state)
803 * or called directly with -EXITCODE.
804 * We also call it if xfunc is exiting. */
805static void sigexit(int sig) ATTRIBUTE_NORETURN;
806static void sigexit(int sig)
807{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000808 /* Disable all signals: job control, SIGPIPE, etc. */
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000809 sigprocmask_allsigs(SIG_BLOCK);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000810
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000811 if (interactive_fd)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000812 tcsetpgrp(interactive_fd, saved_tty_pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000813
814 /* Not a signal, just exit */
815 if (sig <= 0)
816 _exit(- sig);
817
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000818 kill_myself_with_sig(sig); /* does not return */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000819}
820
821/* Restores tty foreground process group, and exits. */
822static void hush_exit(int exitcode) ATTRIBUTE_NORETURN;
823static void hush_exit(int exitcode)
824{
825 fflush(NULL); /* flush all streams */
826 sigexit(- (exitcode & 0xff));
827}
828
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000829#else /* !JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000830
831#define set_fatal_sighandler(handler) ((void)0)
832#define set_jobctrl_sighandler(handler) ((void)0)
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000833#define hush_exit(e) exit(e)
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000834
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000835#endif /* JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000836
837
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000838static const char *set_cwd(void)
839{
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000840 if (cwd == bb_msg_unknown)
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000841 cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000842 cwd = xrealloc_getcwd_or_warn((char *)cwd);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000843 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000844 cwd = bb_msg_unknown;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000845 return cwd;
846}
847
Denis Vlasenko83506862007-11-23 13:11:42 +0000848
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000849/* built-in 'true' handler */
850static int builtin_true(char **argv ATTRIBUTE_UNUSED)
851{
852 return 0;
853}
854
Denis Vlasenko83506862007-11-23 13:11:42 +0000855/* built-in 'test' handler */
856static int builtin_test(char **argv)
857{
858 int argc = 0;
859 while (*argv) {
860 argc++;
861 argv++;
862 }
863 return test_main(argc, argv - argc);
864}
865
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000866/* built-in 'test' handler */
867static int builtin_echo(char **argv)
868{
869 int argc = 0;
870 while (*argv) {
871 argc++;
872 argv++;
873 }
Denis Vlasenkofe5e23b2007-11-24 02:23:51 +0000874 return echo_main(argc, argv - argc);
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000875}
876
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000877/* built-in 'eval' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000878static int builtin_eval(char **argv)
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000879{
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000880 int rcode = EXIT_SUCCESS;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000881
Denis Vlasenko1359da62007-04-21 23:27:30 +0000882 if (argv[1]) {
Denis Vlasenko170435c2007-05-23 13:01:10 +0000883 char *str = expand_strvec_to_string(argv + 1);
Denis Vlasenko5703c222008-06-15 11:49:42 +0000884 parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP);
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000885 free(str);
886 rcode = last_return_code;
887 }
888 return rcode;
889}
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000890
Eric Andersen25f27032001-04-26 23:22:31 +0000891/* built-in 'cd <path>' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000892static int builtin_cd(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000893{
Denis Vlasenko170435c2007-05-23 13:01:10 +0000894 const char *newdir;
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000895 if (argv[1] == NULL) {
896 // bash does nothing (exitcode 0) if HOME is ""; if it's unset,
897 // bash says "bash: cd: HOME not set" and does nothing (exitcode 1)
Denis Vlasenko170435c2007-05-23 13:01:10 +0000898 newdir = getenv("HOME") ? : "/";
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000899 } else
Denis Vlasenko1359da62007-04-21 23:27:30 +0000900 newdir = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000901 if (chdir(newdir)) {
902 printf("cd: %s: %s\n", newdir, strerror(errno));
903 return EXIT_FAILURE;
904 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000905 set_cwd();
Eric Andersen25f27032001-04-26 23:22:31 +0000906 return EXIT_SUCCESS;
907}
908
Eric Andersen25f27032001-04-26 23:22:31 +0000909/* built-in 'exec' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000910static int builtin_exec(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000911{
Denis Vlasenko1359da62007-04-21 23:27:30 +0000912 if (argv[1] == NULL)
Denis Vlasenkof90ab182008-03-20 21:19:35 +0000913 return EXIT_SUCCESS; /* bash does this */
Denis Vlasenko76d50412008-06-10 16:19:39 +0000914 {
915#if !BB_MMU
916 char **ptrs2free = alloc_ptrs(argv);
917#endif
Denis Vlasenkof90ab182008-03-20 21:19:35 +0000918// FIXME: if exec fails, bash does NOT exit! We do...
Denis Vlasenko76d50412008-06-10 16:19:39 +0000919 pseudo_exec_argv(ptrs2free, argv + 1);
920 /* never returns */
921 }
Eric Andersen25f27032001-04-26 23:22:31 +0000922}
923
924/* built-in 'exit' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000925static int builtin_exit(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000926{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000927// TODO: bash does it ONLY on top-level sh exit (+interacive only?)
928 //puts("exit"); /* bash does it */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000929// TODO: warn if we have background jobs: "There are stopped jobs"
930// On second consecutive 'exit', exit anyway.
Denis Vlasenko1359da62007-04-21 23:27:30 +0000931 if (argv[1] == NULL)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000932 hush_exit(last_return_code);
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000933 /* mimic bash: exit 123abc == exit 255 + error msg */
934 xfunc_error_retval = 255;
935 /* bash: exit -2 == exit 254, no error msg */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000936 hush_exit(xatoi(argv[1]) & 0xff);
Eric Andersen25f27032001-04-26 23:22:31 +0000937}
938
939/* built-in 'export VAR=value' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000940static int builtin_export(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000941{
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000942 const char *value;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000943 char *name = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000944
Eric Andersenf72f5622001-05-15 23:21:41 +0000945 if (name == NULL) {
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000946 // TODO:
947 // ash emits: export VAR='VAL'
948 // bash: declare -x VAR="VAL"
949 // (both also escape as needed (quotes, $, etc))
950 char **e = environ;
951 if (e)
952 while (*e)
953 puts(*e++);
954 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000955 }
Eric Andersenf72f5622001-05-15 23:21:41 +0000956
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000957 value = strchr(name, '=');
958 if (!value) {
959 /* They are exporting something without a =VALUE */
960 struct variable *var;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000961
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000962 var = get_local_var(name);
963 if (var) {
964 var->flg_export = 1;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000965 putenv(var->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000966 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000967 /* bash does not return an error when trying to export
968 * an undefined variable. Do likewise. */
969 return EXIT_SUCCESS;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000970 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000971
972 set_local_var(xstrdup(name), 1);
973 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000974}
975
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000976#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +0000977/* built-in 'fg' and 'bg' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000978static int builtin_fg_bg(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000979{
Eric Andersen0fcd4472001-05-02 20:12:03 +0000980 int i, jobnum;
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000981 struct pipe *pi;
Eric Andersen25f27032001-04-26 23:22:31 +0000982
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000983 if (!interactive_fd)
Eric Andersenc798b072001-06-22 06:23:03 +0000984 return EXIT_FAILURE;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000985 /* If they gave us no args, assume they want the last backgrounded task */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000986 if (!argv[1]) {
Eric Andersenc798b072001-06-22 06:23:03 +0000987 for (pi = job_list; pi; pi = pi->next) {
988 if (pi->jobid == last_jobid) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000989 goto found;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000990 }
991 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000992 bb_error_msg("%s: no current job", argv[0]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000993 return EXIT_FAILURE;
994 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000995 if (sscanf(argv[1], "%%%d", &jobnum) != 1) {
996 bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000997 return EXIT_FAILURE;
998 }
999 for (pi = job_list; pi; pi = pi->next) {
1000 if (pi->jobid == jobnum) {
1001 goto found;
Eric Andersen25f27032001-04-26 23:22:31 +00001002 }
1003 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001004 bb_error_msg("%s: %d: no such job", argv[0], jobnum);
Denis Vlasenko8a28e622007-04-14 11:16:29 +00001005 return EXIT_FAILURE;
1006 found:
Denis Vlasenko52881e92007-04-21 13:42:52 +00001007 // TODO: bash prints a string representation
1008 // of job being foregrounded (like "sleep 1 | cat")
Denis Vlasenko1359da62007-04-21 23:27:30 +00001009 if (*argv[0] == 'f') {
Eric Andersen028b65b2001-06-28 01:10:11 +00001010 /* Put the job into the foreground. */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001011 tcsetpgrp(interactive_fd, pi->pgrp);
Eric Andersen25f27032001-04-26 23:22:31 +00001012 }
1013
1014 /* Restart the processes in the job */
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001015 debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_progs, pi->pgrp);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001016 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001017 debug_printf_jobs("reviving pid %d\n", pi->progs[i].pid);
Eric Andersen0fcd4472001-05-02 20:12:03 +00001018 pi->progs[i].is_stopped = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001019 }
1020 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001021
Denis Vlasenkobb81c582007-01-30 22:32:09 +00001022 i = kill(- pi->pgrp, SIGCONT);
1023 if (i < 0) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +00001024 if (errno == ESRCH) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001025 delete_finished_bg_job(pi);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001026 return EXIT_SUCCESS;
Eric Andersen028b65b2001-06-28 01:10:11 +00001027 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001028 bb_perror_msg("kill (SIGCONT)");
Eric Andersen028b65b2001-06-28 01:10:11 +00001029 }
1030 }
Eric Andersen25f27032001-04-26 23:22:31 +00001031
Denis Vlasenko1359da62007-04-21 23:27:30 +00001032 if (*argv[0] == 'f') {
1033 remove_bg_job(pi);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001034 return checkjobs_and_fg_shell(pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001035 }
Eric Andersen25f27032001-04-26 23:22:31 +00001036 return EXIT_SUCCESS;
1037}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001038#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001039
1040/* built-in 'help' handler */
Denis Vlasenko06810332007-05-21 23:30:54 +00001041#if ENABLE_HUSH_HELP
Denis Vlasenko1359da62007-04-21 23:27:30 +00001042static int builtin_help(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +00001043{
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001044 const struct built_in_command *x;
Eric Andersen25f27032001-04-26 23:22:31 +00001045
1046 printf("\nBuilt-in commands:\n");
1047 printf("-------------------\n");
Denis Vlasenkodd316dd2008-06-14 15:50:55 +00001048 for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001049 printf("%s\t%s\n", x->cmd, x->descr);
1050 }
1051 printf("\n\n");
1052 return EXIT_SUCCESS;
1053}
Denis Vlasenko06810332007-05-21 23:30:54 +00001054#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001055
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001056#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +00001057/* built-in 'jobs' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001058static int builtin_jobs(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +00001059{
1060 struct pipe *job;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001061 const char *status_string;
Eric Andersen25f27032001-04-26 23:22:31 +00001062
Eric Andersenc798b072001-06-22 06:23:03 +00001063 for (job = job_list; job; job = job->next) {
Eric Andersen25f27032001-04-26 23:22:31 +00001064 if (job->running_progs == job->stopped_progs)
1065 status_string = "Stopped";
1066 else
1067 status_string = "Running";
Eric Andersen52a97ca2001-06-22 06:49:26 +00001068
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001069 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
Eric Andersen25f27032001-04-26 23:22:31 +00001070 }
1071 return EXIT_SUCCESS;
1072}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001073#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001074
Eric Andersen25f27032001-04-26 23:22:31 +00001075/* built-in 'pwd' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001076static int builtin_pwd(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +00001077{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001078 puts(set_cwd());
Eric Andersen25f27032001-04-26 23:22:31 +00001079 return EXIT_SUCCESS;
1080}
1081
1082/* built-in 'read VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001083static int builtin_read(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001084{
Denis Vlasenkod67cef22007-06-13 06:47:47 +00001085 char *string;
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001086 const char *name = argv[1] ? argv[1] : "REPLY";
Eric Andersen25f27032001-04-26 23:22:31 +00001087
Denis Vlasenko0b6c6a92008-03-24 00:04:42 +00001088 string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL);
Denis Vlasenkod67cef22007-06-13 06:47:47 +00001089 return set_local_var(string, 0);
Eric Andersen25f27032001-04-26 23:22:31 +00001090}
1091
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001092/* built-in 'set [VAR=value]' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001093static int builtin_set(char **argv)
Eric Andersenf72f5622001-05-15 23:21:41 +00001094{
Denis Vlasenko1359da62007-04-21 23:27:30 +00001095 char *temp = argv[1];
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001096 struct variable *e;
Eric Andersenf72f5622001-05-15 23:21:41 +00001097
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001098 if (temp == NULL)
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001099 for (e = top_var; e; e = e->next)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00001100 puts(e->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001101 else
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001102 set_local_var(xstrdup(temp), 0);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001103
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001104 return EXIT_SUCCESS;
Eric Andersenf72f5622001-05-15 23:21:41 +00001105}
1106
1107
Eric Andersen25f27032001-04-26 23:22:31 +00001108/* Built-in 'shift' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001109static int builtin_shift(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001110{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001111 int n = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001112 if (argv[1]) {
1113 n = atoi(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001114 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001115 if (n >= 0 && n < global_argc) {
Denis Vlasenko004baba2007-05-20 22:22:18 +00001116 global_argv[n] = global_argv[0];
Eric Andersen25f27032001-04-26 23:22:31 +00001117 global_argc -= n;
1118 global_argv += n;
1119 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00001120 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001121 return EXIT_FAILURE;
Eric Andersen25f27032001-04-26 23:22:31 +00001122}
1123
1124/* Built-in '.' handler (read-in and execute commands from file) */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001125static int builtin_source(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001126{
1127 FILE *input;
1128 int status;
1129
Denis Vlasenko1359da62007-04-21 23:27:30 +00001130 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +00001131 return EXIT_FAILURE;
1132
1133 /* XXX search through $PATH is missing */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001134 input = fopen(argv[1], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00001135 if (!input) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001136 bb_error_msg("cannot open '%s'", argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001137 return EXIT_FAILURE;
1138 }
Denis Vlasenkoa0898172007-10-01 09:59:01 +00001139 close_on_exec_on(fileno(input));
Eric Andersen25f27032001-04-26 23:22:31 +00001140
1141 /* Now run the file */
1142 /* XXX argv and argc are broken; need to save old global_argv
1143 * (pointer only is OK!) on this stack frame,
Denis Vlasenko1359da62007-04-21 23:27:30 +00001144 * set global_argv=argv+1, recurse, and restore. */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001145 status = parse_and_run_file(input);
Eric Andersen25f27032001-04-26 23:22:31 +00001146 fclose(input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001147 return status;
Eric Andersen25f27032001-04-26 23:22:31 +00001148}
1149
Denis Vlasenko1359da62007-04-21 23:27:30 +00001150static int builtin_umask(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001151{
Eric Andersen83a2ae22001-05-07 17:59:25 +00001152 mode_t new_umask;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001153 const char *arg = argv[1];
Eric Andersen83a2ae22001-05-07 17:59:25 +00001154 char *end;
1155 if (arg) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001156 new_umask = strtoul(arg, &end, 8);
1157 if (*end != '\0' || end == arg) {
Eric Andersen83a2ae22001-05-07 17:59:25 +00001158 return EXIT_FAILURE;
1159 }
1160 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001161 new_umask = umask(0);
1162 printf("%.3o\n", (unsigned) new_umask);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001163 }
1164 umask(new_umask);
1165 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00001166}
1167
1168/* built-in 'unset VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001169static int builtin_unset(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001170{
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001171 /* bash always returns true */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001172 unset_local_var(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001173 return EXIT_SUCCESS;
1174}
1175
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001176/*
1177 * o_string support
1178 */
1179#define B_CHUNK (32 * sizeof(char*))
Eric Andersen25f27032001-04-26 23:22:31 +00001180
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001181static void o_reset(o_string *o)
Eric Andersen25f27032001-04-26 23:22:31 +00001182{
1183 o->length = 0;
1184 o->nonnull = 0;
Denis Vlasenkod65ea392007-10-01 10:02:25 +00001185 if (o->data)
1186 o->data[0] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00001187}
1188
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001189static void o_free(o_string *o)
Eric Andersen25f27032001-04-26 23:22:31 +00001190{
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001191 free(o->data);
Denis Vlasenkod65ea392007-10-01 10:02:25 +00001192 memset(o, 0, sizeof(*o));
Eric Andersen25f27032001-04-26 23:22:31 +00001193}
1194
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001195static void o_grow_by(o_string *o, int len)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001196{
1197 if (o->length + len > o->maxlen) {
1198 o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK);
1199 o->data = xrealloc(o->data, 1 + o->maxlen);
1200 }
1201}
1202
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001203static void o_addchr(o_string *o, int ch)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001204{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001205 debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o);
1206 o_grow_by(o, 1);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001207 o->data[o->length] = ch;
1208 o->length++;
1209 o->data[o->length] = '\0';
1210}
1211
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001212static void o_addstr(o_string *o, const char *str, int len)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001213{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001214 o_grow_by(o, len);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001215 memcpy(&o->data[o->length], str, len);
1216 o->length += len;
1217 o->data[o->length] = '\0';
1218}
1219
Eric Andersen25f27032001-04-26 23:22:31 +00001220/* My analysis of quoting semantics tells me that state information
1221 * is associated with a destination, not a source.
1222 */
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001223static void o_addqchr(o_string *o, int ch)
Eric Andersen25f27032001-04-26 23:22:31 +00001224{
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001225 int sz = 1;
1226 if (strchr("*?[\\", ch)) {
1227 sz++;
1228 o->data[o->length] = '\\';
1229 o->length++;
Eric Andersen25f27032001-04-26 23:22:31 +00001230 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001231 o_grow_by(o, sz);
1232 o->data[o->length] = ch;
1233 o->length++;
1234 o->data[o->length] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00001235}
1236
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001237static void o_addQchr(o_string *o, int ch)
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001238{
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001239 int sz = 1;
1240 if (o->o_quote && strchr("*?[\\", ch)) {
1241 sz++;
1242 o->data[o->length] = '\\';
1243 o->length++;
1244 }
1245 o_grow_by(o, sz);
1246 o->data[o->length] = ch;
1247 o->length++;
1248 o->data[o->length] = '\0';
1249}
1250
1251static void o_addQstr(o_string *o, const char *str, int len)
1252{
1253 if (!o->o_quote) {
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001254 o_addstr(o, str, len);
1255 return;
1256 }
1257 while (len) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001258 char ch;
1259 int sz;
1260 int ordinary_cnt = strcspn(str, "*?[\\");
1261 if (ordinary_cnt > len) /* paranoia */
1262 ordinary_cnt = len;
1263 o_addstr(o, str, ordinary_cnt);
1264 if (ordinary_cnt == len)
1265 return;
1266 str += ordinary_cnt;
1267 len -= ordinary_cnt - 1; /* we are processing + 1 char below */
1268
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001269 ch = *str++;
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001270 sz = 1;
1271 if (ch) { /* it is necessarily one of "*?[\\" */
1272 sz++;
1273 o->data[o->length] = '\\';
1274 o->length++;
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001275 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001276 o_grow_by(o, sz);
1277 o->data[o->length] = ch;
1278 o->length++;
1279 o->data[o->length] = '\0';
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001280 }
1281}
1282
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001283/* A special kind of o_string for $VAR and `cmd` expansion.
1284 * It contains char* list[] at the beginning, which is grown in 16 element
1285 * increments. Actual string data starts at the next multiple of 16.
1286 * list[i] contains an INDEX (int!) into this string data.
1287 * It means that if list[] needs to grow, data needs to be moved higher up
1288 * but list[i]'s need not be modified.
1289 * NB: remembering how many list[i]'s you have there is crucial.
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001290 * o_finalize_list() operation post-processes this structure - calculates
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001291 * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well.
1292 */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001293#if DEBUG_EXPAND || DEBUG_GLOB
1294static void debug_print_list(const char *prefix, o_string *o, int n)
1295{
1296 char **list = (char**)o->data;
1297 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1298 int i = 0;
1299 fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n",
1300 prefix, list, n, string_start, o->length, o->maxlen);
1301 while (i < n) {
1302 fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i],
1303 o->data + (int)list[i] + string_start,
1304 o->data + (int)list[i] + string_start);
1305 i++;
1306 }
1307 if (n) {
1308 const char *p = o->data + (int)list[n - 1] + string_start;
1309 fprintf(stderr, " total_sz:%d\n", (p + strlen(p) + 1) - o->data);
1310 }
1311}
1312#else
1313#define debug_print_list(prefix, o, n) ((void)0)
1314#endif
1315
1316/* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value
1317 * in list[n] so that it points past last stored byte so far.
1318 * It returns n+1. */
1319static int o_save_ptr_helper(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001320{
1321 char **list = (char**)o->data;
Denis Vlasenko895bea22008-06-10 18:06:24 +00001322 int string_start;
1323 int string_len;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001324
1325 if (!o->has_empty_slot) {
Denis Vlasenko895bea22008-06-10 18:06:24 +00001326 string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1327 string_len = o->length - string_start;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001328 if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001329 debug_printf_list("list[%d]=%d string_start=%d (growing)", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001330 /* list[n] points to string_start, make space for 16 more pointers */
1331 o->maxlen += 0x10 * sizeof(list[0]);
1332 o->data = xrealloc(o->data, o->maxlen + 1);
1333 list = (char**)o->data;
1334 memmove(list + n + 0x10, list + n, string_len);
1335 o->length += 0x10 * sizeof(list[0]);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001336 } else
1337 debug_printf_list("list[%d]=%d string_start=%d", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001338 } else {
1339 /* We have empty slot at list[n], reuse without growth */
Denis Vlasenko895bea22008-06-10 18:06:24 +00001340 string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */
1341 string_len = o->length - string_start;
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001342 debug_printf_list("list[%d]=%d string_start=%d (empty slot)", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001343 o->has_empty_slot = 0;
1344 }
1345 list[n] = (char*)string_len;
1346 return n + 1;
1347}
1348
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001349/* "What was our last o_save_ptr'ed position (byte offset relative o->data)?" */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001350static int o_get_last_ptr(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001351{
1352 char **list = (char**)o->data;
1353 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1354
1355 return ((int)list[n-1]) + string_start;
1356}
1357
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001358/* Convert every \x to x in-place, return ptr past NUL. */
1359static char *unbackslash(char *src)
1360{
1361 char *dst = src;
1362 while (1) {
1363 if (*src == '\\')
1364 src++;
1365 if ((*dst++ = *src++) == '\0')
1366 break;
1367 }
1368 return dst;
1369}
1370
1371static int o_glob(o_string *o, int n)
1372{
1373 glob_t globdata;
1374 int gr;
1375 char *pattern;
1376
1377 debug_printf_glob("start o_glob: n:%d o->data:%p", n, o->data);
1378 if (!o->data)
1379 return o_save_ptr_helper(o, n);
1380 pattern = o->data + o_get_last_ptr(o, n);
1381 debug_printf_glob("glob pattern '%s'", pattern);
1382 if (!glob_needed(pattern)) {
1383 literal:
1384 o->length = unbackslash(pattern) - o->data;
1385 debug_printf_glob("glob pattern '%s' is literal", pattern);
1386 return o_save_ptr_helper(o, n);
1387 }
1388
1389 memset(&globdata, 0, sizeof(globdata));
1390 gr = glob(pattern, 0, NULL, &globdata);
1391 debug_printf_glob("glob('%s'):%d\n", pattern, gr);
1392 if (gr == GLOB_NOSPACE)
1393 bb_error_msg_and_die("out of memory during glob");
1394 if (gr == GLOB_NOMATCH) {
1395 globfree(&globdata);
1396 goto literal;
1397 }
1398 if (gr != 0) { /* GLOB_ABORTED ? */
1399//TODO: testcase for bad glob pattern behavior
1400 bb_error_msg("glob(3) error %d on '%s'", gr, pattern);
1401 }
1402 if (globdata.gl_pathv && globdata.gl_pathv[0]) {
1403 char **argv = globdata.gl_pathv;
1404 o->length = pattern - o->data; /* "forget" pattern */
1405 while (1) {
1406 o_addstr(o, *argv, strlen(*argv) + 1);
1407 n = o_save_ptr_helper(o, n);
1408 argv++;
1409 if (!*argv)
1410 break;
1411 }
1412 }
1413 globfree(&globdata);
1414 if (DEBUG_GLOB)
1415 debug_print_list("o_glob returning", o, n);
1416 return n;
1417}
1418
1419/* o_save_ptr_helper + but glob the string so far remembered
1420 * if o->o_glob == 1 */
1421static int o_save_ptr(o_string *o, int n)
1422{
1423 if (o->o_glob)
1424 return o_glob(o, n); /* o_save_ptr_helper is inside */
1425 return o_save_ptr_helper(o, n);
1426}
1427
1428/* "Please convert list[n] to real char* ptrs, and NULL terminate it." */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001429static char **o_finalize_list(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001430{
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001431 char **list;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001432 int string_start;
1433
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001434 n = o_save_ptr(o, n); /* force growth for list[n] if necessary */
1435 if (DEBUG_EXPAND)
1436 debug_print_list("finalized", o, n);
1437 debug_printf_expand("finalized n:%d", n);
1438 list = (char**)o->data;
1439 string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1440 list[--n] = NULL;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001441 while (n) {
1442 n--;
1443 list[n] = o->data + (int)list[n] + string_start;
1444 }
1445 return list;
1446}
1447
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001448
1449/*
1450 * in_str support
1451 */
Eric Andersen25f27032001-04-26 23:22:31 +00001452static int static_get(struct in_str *i)
1453{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001454 int ch = *i->p++;
1455 if (ch == '\0') return EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001456 return ch;
1457}
1458
1459static int static_peek(struct in_str *i)
1460{
1461 return *i->p;
1462}
1463
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001464#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001465#if ENABLE_FEATURE_EDITING
Rob Landley88621d72006-08-29 19:41:06 +00001466static void cmdedit_set_initial_prompt(void)
Eric Andersen25f27032001-04-26 23:22:31 +00001467{
Denis Vlasenko38f63192007-01-22 09:03:07 +00001468#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001469 PS1 = NULL;
1470#else
1471 PS1 = getenv("PS1");
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001472 if (PS1 == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +00001473 PS1 = "\\w \\$ ";
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001474#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001475}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001476#endif /* EDITING */
Eric Andersen25f27032001-04-26 23:22:31 +00001477
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001478static const char* setup_prompt_string(int promptmode)
Eric Andersen25f27032001-04-26 23:22:31 +00001479{
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001480 const char *prompt_str;
1481 debug_printf("setup_prompt_string %d ", promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001482#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001483 /* Set up the prompt */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001484 if (promptmode == 0) { /* PS1 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001485 free((char*)PS1);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001486 PS1 = xasprintf("%s %c ", cwd, (geteuid() != 0) ? '$' : '#');
1487 prompt_str = PS1;
Eric Andersen25f27032001-04-26 23:22:31 +00001488 } else {
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001489 prompt_str = PS2;
Eric Andersen25f27032001-04-26 23:22:31 +00001490 }
1491#else
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001492 prompt_str = (promptmode == 0) ? PS1 : PS2;
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001493#endif
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001494 debug_printf("result '%s'\n", prompt_str);
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001495 return prompt_str;
Eric Andersen25f27032001-04-26 23:22:31 +00001496}
1497
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001498static void get_user_input(struct in_str *i)
Eric Andersen25f27032001-04-26 23:22:31 +00001499{
Denis Vlasenko0937be52007-04-28 16:47:08 +00001500 int r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001501 const char *prompt_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001502
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001503 prompt_str = setup_prompt_string(i->promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001504#if ENABLE_FEATURE_EDITING
Denis Vlasenko170435c2007-05-23 13:01:10 +00001505 /* Enable command line editing only while a command line
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001506 * is actually being read */
1507 do {
1508 r = read_line_input(prompt_str, user_input_buf, BUFSIZ-1, line_input_state);
1509 } while (r == 0); /* repeat if Ctrl-C */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001510 i->eof_flag = (r < 0);
1511 if (i->eof_flag) { /* EOF/error detected */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001512 user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */
1513 user_input_buf[1] = '\0';
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001514 }
Eric Andersen25f27032001-04-26 23:22:31 +00001515#else
1516 fputs(prompt_str, stdout);
1517 fflush(stdout);
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001518 user_input_buf[0] = r = fgetc(i->file);
1519 /*user_input_buf[1] = '\0'; - already is and never changed */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001520 i->eof_flag = (r == EOF);
Eric Andersen25f27032001-04-26 23:22:31 +00001521#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001522 i->p = user_input_buf;
Eric Andersen25f27032001-04-26 23:22:31 +00001523}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001524#endif /* INTERACTIVE */
Eric Andersen25f27032001-04-26 23:22:31 +00001525
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001526/* This is the magic location that prints prompts
Eric Andersen25f27032001-04-26 23:22:31 +00001527 * and gets data back from the user */
1528static int file_get(struct in_str *i)
1529{
1530 int ch;
1531
Eric Andersen25f27032001-04-26 23:22:31 +00001532 /* If there is data waiting, eat it up */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001533 if (i->p && *i->p) {
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001534#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001535 take_cached:
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001536#endif
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001537 ch = *i->p++;
1538 if (i->eof_flag && !*i->p)
1539 ch = EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001540 } else {
1541 /* need to double check i->file because we might be doing something
1542 * more complicated by now, like sourcing or substituting. */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001543#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001544 if (interactive_fd && i->promptme && i->file == stdin) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001545 do {
1546 get_user_input(i);
1547 } while (!*i->p); /* need non-empty line */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001548 i->promptmode = 1; /* PS2 */
1549 i->promptme = 0;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001550 goto take_cached;
Eric Andersen25f27032001-04-26 23:22:31 +00001551 }
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001552#endif
1553 ch = fgetc(i->file);
Eric Andersen25f27032001-04-26 23:22:31 +00001554 }
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001555 debug_printf("file_get: got a '%c' %d\n", ch, ch);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001556#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001557 if (ch == '\n')
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001558 i->promptme = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001559#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001560 return ch;
1561}
1562
1563/* All the callers guarantee this routine will never be
1564 * used right after a newline, so prompting is not needed.
1565 */
1566static int file_peek(struct in_str *i)
1567{
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001568 int ch;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001569 if (i->p && *i->p) {
1570 if (i->eof_flag && !i->p[1])
1571 return EOF;
1572 return *i->p;
Eric Andersen25f27032001-04-26 23:22:31 +00001573 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001574 ch = fgetc(i->file);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001575 i->eof_flag = (ch == EOF);
1576 i->peek_buf[0] = ch;
1577 i->peek_buf[1] = '\0';
1578 i->p = i->peek_buf;
1579 debug_printf("file_peek: got a '%c' %d\n", *i->p, *i->p);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001580 return ch;
Eric Andersen25f27032001-04-26 23:22:31 +00001581}
1582
1583static void setup_file_in_str(struct in_str *i, FILE *f)
1584{
1585 i->peek = file_peek;
1586 i->get = file_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001587#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001588 i->promptme = 1;
1589 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001590#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001591 i->file = f;
1592 i->p = NULL;
1593}
1594
1595static void setup_string_in_str(struct in_str *i, const char *s)
1596{
1597 i->peek = static_peek;
1598 i->get = static_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001599#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001600 i->promptme = 1;
1601 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001602#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001603 i->p = s;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001604 i->eof_flag = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001605}
1606
Eric Andersen25f27032001-04-26 23:22:31 +00001607/* squirrel != NULL means we squirrel away copies of stdin, stdout,
1608 * and stderr if they are redirected. */
1609static int setup_redirects(struct child_prog *prog, int squirrel[])
1610{
1611 int openfd, mode;
1612 struct redir_struct *redir;
1613
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001614 for (redir = prog->redirects; redir; redir = redir->next) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00001615 if (redir->dup == -1 && redir->glob_word == NULL) {
Eric Andersen817e73c2001-06-06 17:56:09 +00001616 /* something went wrong in the parse. Pretend it didn't happen */
1617 continue;
1618 }
Eric Andersen25f27032001-04-26 23:22:31 +00001619 if (redir->dup == -1) {
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001620 char *p;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001621 mode = redir_table[redir->type].mode;
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001622 p = expand_string_to_string(redir->glob_word[0]);
1623 openfd = open_or_warn(p, mode);
1624 free(p);
Eric Andersen25f27032001-04-26 23:22:31 +00001625 if (openfd < 0) {
1626 /* this could get lost if stderr has been redirected, but
1627 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersen25f27032001-04-26 23:22:31 +00001628 return 1;
1629 }
1630 } else {
1631 openfd = redir->dup;
1632 }
1633
1634 if (openfd != redir->fd) {
1635 if (squirrel && redir->fd < 3) {
1636 squirrel[redir->fd] = dup(redir->fd);
1637 }
Eric Andersen83a2ae22001-05-07 17:59:25 +00001638 if (openfd == -3) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001639 //close(openfd); // close(-3) ??!
Eric Andersen83a2ae22001-05-07 17:59:25 +00001640 } else {
1641 dup2(openfd, redir->fd);
Matt Kraaic616e532001-06-05 16:50:08 +00001642 if (redir->dup == -1)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001643 close(openfd);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001644 }
Eric Andersen25f27032001-04-26 23:22:31 +00001645 }
1646 }
1647 return 0;
1648}
1649
1650static void restore_redirects(int squirrel[])
1651{
1652 int i, fd;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001653 for (i = 0; i < 3; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001654 fd = squirrel[i];
1655 if (fd != -1) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00001656 /* We simply die on error */
1657 xmove_fd(fd, i);
Eric Andersen25f27032001-04-26 23:22:31 +00001658 }
1659 }
1660}
1661
Denis Vlasenko05743d72008-02-10 12:10:08 +00001662/* Called after [v]fork() in run_pipe(), or from builtin_exec().
Denis Vlasenko8412d792007-10-01 09:59:47 +00001663 * Never returns.
1664 * XXX no exit() here. If you don't exec, use _exit instead.
Eric Andersen94ac2442001-05-22 19:05:18 +00001665 * The at_exit handlers apparently confuse the calling process,
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001666 * in particular stdin handling. Not sure why? -- because of vfork! (vda) */
Denis Vlasenko76d50412008-06-10 16:19:39 +00001667static void pseudo_exec_argv(char **ptrs2free, char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001668{
Eric Andersen78a7c992001-05-15 16:30:25 +00001669 int i, rcode;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001670 char *p;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001671 const struct built_in_command *x;
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001672
Denis Vlasenko1359da62007-04-21 23:27:30 +00001673 for (i = 0; is_assignment(argv[i]); i++) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001674 debug_printf_exec("pid %d environment modification: %s\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001675 getpid(), argv[i]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001676 p = expand_string_to_string(argv[i]);
Denis Vlasenko76d50412008-06-10 16:19:39 +00001677#if !BB_MMU
1678 *ptrs2free++ = p;
1679#endif
Denis Vlasenko170435c2007-05-23 13:01:10 +00001680 putenv(p);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001681 }
1682 argv += i;
1683 /* If a variable is assigned in a forest, and nobody listens,
1684 * was it ever really set?
1685 */
Denis Vlasenko05743d72008-02-10 12:10:08 +00001686 if (!argv[0])
Denis Vlasenko1359da62007-04-21 23:27:30 +00001687 _exit(EXIT_SUCCESS);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001688
Denis Vlasenko170435c2007-05-23 13:01:10 +00001689 argv = expand_strvec_to_strvec(argv);
Denis Vlasenko76d50412008-06-10 16:19:39 +00001690#if !BB_MMU
1691 *ptrs2free++ = (char*) argv;
1692#endif
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001693
Denis Vlasenko1359da62007-04-21 23:27:30 +00001694 /*
1695 * Check if the command matches any of the builtins.
1696 * Depending on context, this might be redundant. But it's
1697 * easier to waste a few CPU cycles than it is to figure out
1698 * if this is one of those cases.
1699 */
Denis Vlasenkodd316dd2008-06-14 15:50:55 +00001700 for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001701 if (strcmp(argv[0], x->cmd) == 0) {
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001702 debug_printf_exec("running builtin '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001703 rcode = x->function(argv);
1704 fflush(stdout);
1705 _exit(rcode);
Eric Andersen94ac2442001-05-22 19:05:18 +00001706 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001707 }
Eric Andersen78a7c992001-05-15 16:30:25 +00001708
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001709 /* Check if the command matches any busybox applets */
Denis Vlasenko80d14be2007-04-10 23:03:30 +00001710#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001711 if (strchr(argv[0], '/') == NULL) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001712 int a = find_applet_by_name(argv[0]);
1713 if (a >= 0) {
1714 if (APPLET_IS_NOEXEC(a)) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001715 debug_printf_exec("running applet '%s'\n", argv[0]);
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001716// is it ok that run_applet_no_and_exit() does exit(), not _exit()?
1717 run_applet_no_and_exit(a, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001718 }
1719 /* re-exec ourselves with the new arguments */
1720 debug_printf_exec("re-execing applet '%s'\n", argv[0]);
Denis Vlasenkobdbbb7e2007-06-08 15:02:55 +00001721 execvp(bb_busybox_exec_path, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001722 /* If they called chroot or otherwise made the binary no longer
1723 * executable, fall through */
1724 }
1725 }
Eric Andersenaac75e52001-04-30 18:18:45 +00001726#endif
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001727
1728 debug_printf_exec("execing '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001729 execvp(argv[0], argv);
1730 bb_perror_msg("cannot exec '%s'", argv[0]);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001731 _exit(EXIT_FAILURE);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001732}
1733
Denis Vlasenko05743d72008-02-10 12:10:08 +00001734/* Called after [v]fork() in run_pipe()
Denis Vlasenko8412d792007-10-01 09:59:47 +00001735 */
Denis Vlasenko76d50412008-06-10 16:19:39 +00001736static void pseudo_exec(char **ptrs2free, struct child_prog *child)
Denis Vlasenko1359da62007-04-21 23:27:30 +00001737{
Denis Vlasenko05743d72008-02-10 12:10:08 +00001738 if (child->argv)
Denis Vlasenko76d50412008-06-10 16:19:39 +00001739 pseudo_exec_argv(ptrs2free, child->argv);
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001740
1741 if (child->group) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001742#if !BB_MMU
Denis Vlasenko3b492162007-12-24 14:26:57 +00001743 bb_error_msg_and_die("nested lists are not supported on NOMMU");
Denis Vlasenko8412d792007-10-01 09:59:47 +00001744#else
Denis Vlasenko3b492162007-12-24 14:26:57 +00001745 int rcode;
Denis Vlasenko05743d72008-02-10 12:10:08 +00001746 debug_printf_exec("pseudo_exec: run_list\n");
1747 rcode = run_list(child->group);
Eric Andersenbf7df042001-05-23 22:18:35 +00001748 /* OK to leak memory by not calling free_pipe_list,
Eric Andersen25f27032001-04-26 23:22:31 +00001749 * since this process is about to exit */
Eric Andersen94ac2442001-05-22 19:05:18 +00001750 _exit(rcode);
Denis Vlasenko8412d792007-10-01 09:59:47 +00001751#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001752 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001753
1754 /* Can happen. See what bash does with ">foo" by itself. */
1755 debug_printf("trying to pseudo_exec null command\n");
1756 _exit(EXIT_SUCCESS);
Eric Andersen25f27032001-04-26 23:22:31 +00001757}
1758
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001759#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001760static const char *get_cmdtext(struct pipe *pi)
1761{
1762 char **argv;
1763 char *p;
1764 int len;
1765
1766 /* This is subtle. ->cmdtext is created only on first backgrounding.
1767 * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...)
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001768 * On subsequent bg argv is trashed, but we won't use it */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001769 if (pi->cmdtext)
1770 return pi->cmdtext;
1771 argv = pi->progs[0].argv;
Denis Vlasenkoa8442002008-06-14 11:00:17 +00001772 if (!argv || !argv[0]) {
1773 pi->cmdtext = xzalloc(1);
1774 return pi->cmdtext;
1775 }
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001776
1777 len = 0;
1778 do len += strlen(*argv) + 1; while (*++argv);
1779 pi->cmdtext = p = xmalloc(len);
1780 argv = pi->progs[0].argv;
1781 do {
1782 len = strlen(*argv);
1783 memcpy(p, *argv, len);
1784 p += len;
1785 *p++ = ' ';
1786 } while (*++argv);
1787 p[-1] = '\0';
1788 return pi->cmdtext;
1789}
1790
Eric Andersenbafd94f2001-05-02 16:11:59 +00001791static void insert_bg_job(struct pipe *pi)
1792{
1793 struct pipe *thejob;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001794 int i;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001795
1796 /* Linear search for the ID of the job to use */
1797 pi->jobid = 1;
Eric Andersenc798b072001-06-22 06:23:03 +00001798 for (thejob = job_list; thejob; thejob = thejob->next)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001799 if (thejob->jobid >= pi->jobid)
1800 pi->jobid = thejob->jobid + 1;
1801
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001802 /* Add thejob to the list of running jobs */
Eric Andersenc798b072001-06-22 06:23:03 +00001803 if (!job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001804 thejob = job_list = xmalloc(sizeof(*thejob));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001805 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001806 for (thejob = job_list; thejob->next; thejob = thejob->next)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001807 continue;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001808 thejob->next = xmalloc(sizeof(*thejob));
1809 thejob = thejob->next;
1810 }
1811
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001812 /* Physically copy the struct job */
Eric Andersen0fcd4472001-05-02 20:12:03 +00001813 memcpy(thejob, pi, sizeof(struct pipe));
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001814 thejob->progs = xzalloc(sizeof(pi->progs[0]) * pi->num_progs);
1815 /* We cannot copy entire pi->progs[] vector! Double free()s will happen */
1816 for (i = 0; i < pi->num_progs; i++) {
1817// TODO: do we really need to have so many fields which are just dead weight
1818// at execution stage?
1819 thejob->progs[i].pid = pi->progs[i].pid;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001820 /* all other fields are not used and stay zero */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001821 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001822 thejob->next = NULL;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001823 thejob->cmdtext = xstrdup(get_cmdtext(pi));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001824
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001825 /* We don't wait for background thejobs to return -- append it
Eric Andersenbafd94f2001-05-02 16:11:59 +00001826 to the list of backgrounded thejobs and leave it alone */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001827 printf("[%d] %d %s\n", thejob->jobid, thejob->progs[0].pid, thejob->cmdtext);
Eric Andersen1a6d39b2001-05-08 05:11:54 +00001828 last_bg_pid = thejob->progs[0].pid;
Eric Andersenc798b072001-06-22 06:23:03 +00001829 last_jobid = thejob->jobid;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001830}
1831
Eric Andersenbafd94f2001-05-02 16:11:59 +00001832static void remove_bg_job(struct pipe *pi)
1833{
1834 struct pipe *prev_pipe;
1835
Eric Andersenc798b072001-06-22 06:23:03 +00001836 if (pi == job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001837 job_list = pi->next;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001838 } else {
Eric Andersenc798b072001-06-22 06:23:03 +00001839 prev_pipe = job_list;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001840 while (prev_pipe->next != pi)
1841 prev_pipe = prev_pipe->next;
1842 prev_pipe->next = pi->next;
1843 }
Eric Andersen028b65b2001-06-28 01:10:11 +00001844 if (job_list)
1845 last_jobid = job_list->jobid;
1846 else
1847 last_jobid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001848}
Eric Andersen028b65b2001-06-28 01:10:11 +00001849
Denis Vlasenko1359da62007-04-21 23:27:30 +00001850/* remove a backgrounded job */
1851static void delete_finished_bg_job(struct pipe *pi)
1852{
1853 remove_bg_job(pi);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001854 pi->stopped_progs = 0;
Eric Andersenbf7df042001-05-23 22:18:35 +00001855 free_pipe(pi, 0);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001856 free(pi);
1857}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001858#endif /* JOB */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001859
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001860/* Checks to see if any processes have exited -- if they
Eric Andersenbafd94f2001-05-02 16:11:59 +00001861 have, figure out why and see if a job has completed */
Eric Andersenc798b072001-06-22 06:23:03 +00001862static int checkjobs(struct pipe* fg_pipe)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001863{
Eric Andersenc798b072001-06-22 06:23:03 +00001864 int attributes;
1865 int status;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001866#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00001867 int prognum = 0;
1868 struct pipe *pi;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001869#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001870 pid_t childpid;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001871 int rcode = 0;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001872
Eric Andersenc798b072001-06-22 06:23:03 +00001873 attributes = WUNTRACED;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001874 if (fg_pipe == NULL) {
Eric Andersenc798b072001-06-22 06:23:03 +00001875 attributes |= WNOHANG;
1876 }
1877
Denis Vlasenko1359da62007-04-21 23:27:30 +00001878/* Do we do this right?
1879 * bash-3.00# sleep 20 | false
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001880 * <ctrl-Z pressed>
Denis Vlasenko1359da62007-04-21 23:27:30 +00001881 * [3]+ Stopped sleep 20 | false
1882 * bash-3.00# echo $?
1883 * 1 <========== bg pipe is not fully done, but exitcode is already known!
1884 */
1885
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001886//FIXME: non-interactive bash does not continue even if all processes in fg pipe
1887//are stopped. Testcase: "cat | cat" in a script (not on command line)
1888// + killall -STOP cat
1889
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001890 wait_more:
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001891// TODO: safe_waitpid?
Eric Andersenc798b072001-06-22 06:23:03 +00001892 while ((childpid = waitpid(-1, &status, attributes)) > 0) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001893 const int dead = WIFEXITED(status) || WIFSIGNALED(status);
1894
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001895#if DEBUG_JOBS
Denis Vlasenko1359da62007-04-21 23:27:30 +00001896 if (WIFSTOPPED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001897 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001898 childpid, WSTOPSIG(status), WEXITSTATUS(status));
1899 if (WIFSIGNALED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001900 debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001901 childpid, WTERMSIG(status), WEXITSTATUS(status));
1902 if (WIFEXITED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001903 debug_printf_jobs("pid %d exited, exitcode %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001904 childpid, WEXITSTATUS(status));
1905#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001906 /* Were we asked to wait for fg pipe? */
Eric Andersenc798b072001-06-22 06:23:03 +00001907 if (fg_pipe) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001908 int i;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001909 for (i = 0; i < fg_pipe->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001910 debug_printf_jobs("check pid %d\n", fg_pipe->progs[i].pid);
Eric Andersenc798b072001-06-22 06:23:03 +00001911 if (fg_pipe->progs[i].pid == childpid) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001912 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001913 if (dead) {
1914 fg_pipe->progs[i].pid = 0;
1915 fg_pipe->running_progs--;
Denis Vlasenkoa8442002008-06-14 11:00:17 +00001916 if (i == fg_pipe->num_progs - 1) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001917 /* last process gives overall exitstatus */
1918 rcode = WEXITSTATUS(status);
Denis Vlasenkoa8442002008-06-14 11:00:17 +00001919 if (fg_pipe->pi_inverted)
1920 rcode = !rcode;
1921 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001922 } else {
1923 fg_pipe->progs[i].is_stopped = 1;
1924 fg_pipe->stopped_progs++;
1925 }
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001926 debug_printf_jobs("fg_pipe: running_progs %d stopped_progs %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001927 fg_pipe->running_progs, fg_pipe->stopped_progs);
1928 if (fg_pipe->running_progs - fg_pipe->stopped_progs <= 0) {
1929 /* All processes in fg pipe have exited/stopped */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001930#if ENABLE_HUSH_JOB
Denis Vlasenko1359da62007-04-21 23:27:30 +00001931 if (fg_pipe->running_progs)
1932 insert_bg_job(fg_pipe);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001933#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001934 return rcode;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001935 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001936 /* There are still running processes in the fg pipe */
1937 goto wait_more;
Eric Andersenc798b072001-06-22 06:23:03 +00001938 }
1939 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001940 /* fall through to searching process in bg pipes */
Eric Andersenc798b072001-06-22 06:23:03 +00001941 }
1942
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001943#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001944 /* We asked to wait for bg or orphaned children */
1945 /* No need to remember exitcode in this case */
Eric Andersenc798b072001-06-22 06:23:03 +00001946 for (pi = job_list; pi; pi = pi->next) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001947 prognum = 0;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001948 while (prognum < pi->num_progs) {
1949 if (pi->progs[prognum].pid == childpid)
1950 goto found_pi_and_prognum;
Eric Andersen52a97ca2001-06-22 06:49:26 +00001951 prognum++;
1952 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001953 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001954#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001955
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001956 /* Happens when shell is used as init process (init=/bin/sh) */
1957 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001958 goto wait_more;
Eric Andersenaeb44c42001-05-22 20:29:00 +00001959
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001960#if ENABLE_HUSH_JOB
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001961 found_pi_and_prognum:
Denis Vlasenko1359da62007-04-21 23:27:30 +00001962 if (dead) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001963 /* child exited */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001964 pi->progs[prognum].pid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001965 pi->running_progs--;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001966 if (!pi->running_progs) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001967 printf(JOB_STATUS_FORMAT, pi->jobid,
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001968 "Done", pi->cmdtext);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001969 delete_finished_bg_job(pi);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001970 }
1971 } else {
1972 /* child stopped */
1973 pi->stopped_progs++;
1974 pi->progs[prognum].is_stopped = 1;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001975 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001976#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001977 }
1978
Denis Vlasenko1359da62007-04-21 23:27:30 +00001979 /* wait found no children or failed */
1980
1981 if (childpid && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001982 bb_perror_msg("waitpid");
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001983 return rcode;
Eric Andersenada18ff2001-05-21 16:18:22 +00001984}
1985
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001986#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +00001987static int checkjobs_and_fg_shell(struct pipe* fg_pipe)
1988{
1989 pid_t p;
1990 int rcode = checkjobs(fg_pipe);
1991 /* Job finished, move the shell to the foreground */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001992 p = getpgid(0); /* pgid of our process */
1993 debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001994 if (tcsetpgrp(interactive_fd, p) && errno != ENOTTY)
1995 bb_perror_msg("tcsetpgrp-4a");
1996 return rcode;
1997}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001998#endif
Denis Vlasenko52881e92007-04-21 13:42:52 +00001999
Denis Vlasenko05743d72008-02-10 12:10:08 +00002000/* run_pipe() starts all the jobs, but doesn't wait for anything
Eric Andersenc798b072001-06-22 06:23:03 +00002001 * to finish. See checkjobs().
Eric Andersen25f27032001-04-26 23:22:31 +00002002 *
2003 * return code is normally -1, when the caller has to wait for children
2004 * to finish to determine the exit status of the pipe. If the pipe
2005 * is a simple builtin command, however, the action is done by the
Denis Vlasenko05743d72008-02-10 12:10:08 +00002006 * time run_pipe returns, and the exit code is provided as the
Eric Andersen25f27032001-04-26 23:22:31 +00002007 * return value.
2008 *
2009 * The input of the pipe is always stdin, the output is always
2010 * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
2011 * because it tries to avoid running the command substitution in
2012 * subshell, when that is in fact necessary. The subshell process
2013 * now has its stdout directed to the input of the appropriate pipe,
2014 * so this routine is noticeably simpler.
Denis Vlasenko170435c2007-05-23 13:01:10 +00002015 *
2016 * Returns -1 only if started some children. IOW: we have to
2017 * mask out retvals of builtins etc with 0xff!
Eric Andersen25f27032001-04-26 23:22:31 +00002018 */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002019static int run_pipe(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002020{
2021 int i;
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002022 int nextin;
2023 int pipefds[2]; /* pipefds[0] is for reading */
Rob Landley53702e52006-07-19 21:43:53 +00002024 struct child_prog *child;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00002025 const struct built_in_command *x;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002026 char *p;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00002027 /* it is not always needed, but we aim to smaller code */
2028 int squirrel[] = { -1, -1, -1 };
2029 int rcode;
Denis Vlasenko52881e92007-04-21 13:42:52 +00002030 const int single_fg = (pi->num_progs == 1 && pi->followup != PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00002031
Denis Vlasenko05743d72008-02-10 12:10:08 +00002032 debug_printf_exec("run_pipe start: single_fg=%d\n", single_fg);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002033
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002034#if ENABLE_HUSH_JOB
Eric Andersenada18ff2001-05-21 16:18:22 +00002035 pi->pgrp = -1;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002036#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002037 pi->running_progs = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +00002038 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002039
2040 /* Check if this is a simple builtin (not part of a pipe).
2041 * Builtins within pipes have to fork anyway, and are handled in
2042 * pseudo_exec. "echo foo | read bar" doesn't work on bash, either.
2043 */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002044 child = &(pi->progs[0]);
Denis Vlasenko52881e92007-04-21 13:42:52 +00002045 if (single_fg && child->group && child->subshell == 0) {
Eric Andersen04407e52001-06-07 16:42:05 +00002046 debug_printf("non-subshell grouping\n");
2047 setup_redirects(child, squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00002048 debug_printf_exec(": run_list\n");
2049 rcode = run_list(child->group) & 0xff;
Eric Andersen04407e52001-06-07 16:42:05 +00002050 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00002051 debug_printf_exec("run_pipe return %d\n", rcode);
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002052 if (pi->pi_inverted)
2053 rcode = !rcode;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002054 return rcode;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002055 }
2056
Denis Vlasenko1359da62007-04-21 23:27:30 +00002057 if (single_fg && child->argv != NULL) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002058 char **argv_expanded;
Denis Vlasenko1359da62007-04-21 23:27:30 +00002059 char **argv = child->argv;
2060
2061 for (i = 0; is_assignment(argv[i]); i++)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002062 continue;
Denis Vlasenko1359da62007-04-21 23:27:30 +00002063 if (i != 0 && argv[i] == NULL) {
Eric Andersen78a7c992001-05-15 16:30:25 +00002064 /* assignments, but no command: set the local environment */
Denis Vlasenko1359da62007-04-21 23:27:30 +00002065 for (i = 0; argv[i] != NULL; i++) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002066 debug_printf("local environment set: %s\n", argv[i]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00002067 p = expand_string_to_string(argv[i]);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002068 set_local_var(p, 0);
Eric Andersen78a7c992001-05-15 16:30:25 +00002069 }
2070 return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */
2071 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00002072 for (i = 0; is_assignment(argv[i]); i++) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00002073 p = expand_string_to_string(argv[i]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00002074 putenv(p);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002075 }
Denis Vlasenkodd316dd2008-06-14 15:50:55 +00002076 for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00002077 if (strcmp(argv[i], x->cmd) == 0) {
2078 if (x->function == builtin_exec && argv[i+1] == NULL) {
Eric Andersen83a2ae22001-05-07 17:59:25 +00002079 debug_printf("magic exec\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002080 setup_redirects(child, NULL);
Eric Andersen83a2ae22001-05-07 17:59:25 +00002081 return EXIT_SUCCESS;
2082 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00002083 debug_printf("builtin inline %s\n", argv[0]);
Eric Andersen25f27032001-04-26 23:22:31 +00002084 /* XXX setup_redirects acts on file descriptors, not FILEs.
2085 * This is perfect for work that comes after exec().
2086 * Is it really safe for inline use? Experimentally,
2087 * things seem to work with glibc. */
2088 setup_redirects(child, squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00002089 debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv[i+1]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00002090 argv_expanded = expand_strvec_to_strvec(argv + i);
2091 rcode = x->function(argv_expanded) & 0xff;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002092 free(argv_expanded);
Eric Andersen25f27032001-04-26 23:22:31 +00002093 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00002094 debug_printf_exec("run_pipe return %d\n", rcode);
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002095 if (pi->pi_inverted)
2096 rcode = !rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00002097 return rcode;
2098 }
2099 }
Denis Vlasenkof5294e12007-04-14 10:09:57 +00002100#if ENABLE_FEATURE_SH_STANDALONE
2101 {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00002102 int a = find_applet_by_name(argv[i]);
2103 if (a >= 0 && APPLET_IS_NOFORK(a)) {
Denis Vlasenkof5294e12007-04-14 10:09:57 +00002104 setup_redirects(child, squirrel);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002105 save_nofork_data(&nofork_save);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002106 argv_expanded = argv + i;
Denis Vlasenko170435c2007-05-23 13:01:10 +00002107 argv_expanded = expand_strvec_to_strvec(argv + i);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002108 debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]);
Denis Vlasenko7465dbc2008-04-13 02:25:53 +00002109 rcode = run_nofork_applet_prime(&nofork_save, a, argv_expanded);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002110 free(argv_expanded);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002111 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00002112 debug_printf_exec("run_pipe return %d\n", rcode);
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002113 if (pi->pi_inverted)
2114 rcode = !rcode;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002115 return rcode;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00002116 }
2117 }
2118#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002119 }
2120
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002121 /* Disable job control signals for shell (parent) and
2122 * for initial child code after fork */
2123 set_jobctrl_sighandler(SIG_IGN);
2124
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002125 /* Going to fork a child per each pipe member */
2126 pi->running_progs = 0;
2127 nextin = 0;
2128
Eric Andersen25f27032001-04-26 23:22:31 +00002129 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenko76d50412008-06-10 16:19:39 +00002130#if !BB_MMU
2131 char **ptrs2free = NULL;
2132#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002133 child = &(pi->progs[i]);
Denis Vlasenko76d50412008-06-10 16:19:39 +00002134 if (child->argv) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002135 debug_printf_exec(": pipe member '%s' '%s'...\n", child->argv[0], child->argv[1]);
Denis Vlasenko76d50412008-06-10 16:19:39 +00002136#if !BB_MMU
2137 ptrs2free = alloc_ptrs(child->argv);
2138#endif
2139 } else
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002140 debug_printf_exec(": pipe member with no argv\n");
Eric Andersen25f27032001-04-26 23:22:31 +00002141
2142 /* pipes are inserted between pairs of commands */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002143 pipefds[0] = 0;
2144 pipefds[1] = 1;
2145 if ((i + 1) < pi->num_progs)
2146 xpipe(pipefds);
Eric Andersen25f27032001-04-26 23:22:31 +00002147
Denis Vlasenko05743d72008-02-10 12:10:08 +00002148 child->pid = BB_MMU ? fork() : vfork();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002149 if (!child->pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00002150 if (ENABLE_HUSH_JOB)
2151 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002152#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002153 /* Every child adds itself to new process group
Denis Vlasenko05743d72008-02-10 12:10:08 +00002154 * with pgid == pid_of_first_child_in_pipe */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002155 if (run_list_level == 1 && interactive_fd) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00002156 pid_t pgrp;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002157 /* Don't do pgrp restore anymore on fatal signals */
2158 set_fatal_sighandler(SIG_DFL);
Denis Vlasenko05743d72008-02-10 12:10:08 +00002159 pgrp = pi->pgrp;
2160 if (pgrp < 0) /* true for 1st process only */
2161 pgrp = getpid();
2162 if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002163 /* We do it in *every* child, not just first,
2164 * to avoid races */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002165 tcsetpgrp(interactive_fd, pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002166 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002167 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002168#endif
Denis Vlasenko8412d792007-10-01 09:59:47 +00002169 xmove_fd(nextin, 0);
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002170 xmove_fd(pipefds[1], 1); /* write end */
2171 if (pipefds[0] > 1)
2172 close(pipefds[0]); /* read end */
Eric Andersen25f27032001-04-26 23:22:31 +00002173 /* Like bash, explicit redirects override pipes,
2174 * and the pipe fd is available for dup'ing. */
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002175 setup_redirects(child, NULL);
Eric Andersen52a97ca2001-06-22 06:49:26 +00002176
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002177 /* Restore default handlers just prior to exec */
2178 set_jobctrl_sighandler(SIG_DFL);
2179 set_misc_sighandler(SIG_DFL);
2180 signal(SIGCHLD, SIG_DFL);
Denis Vlasenko76d50412008-06-10 16:19:39 +00002181 pseudo_exec(ptrs2free, child); /* does not return */
Eric Andersen25f27032001-04-26 23:22:31 +00002182 }
Denis Vlasenko76d50412008-06-10 16:19:39 +00002183#if !BB_MMU
2184 free_strings(ptrs2free);
2185#endif
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002186 if (child->pid < 0) { /* [v]fork failed */
2187 /* Clearly indicate, was it fork or vfork */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002188 bb_perror_msg(BB_MMU ? "fork" : "vfork");
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002189 } else {
2190 pi->running_progs++;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002191#if ENABLE_HUSH_JOB
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002192 /* Second and next children need to know pid of first one */
2193 if (pi->pgrp < 0)
2194 pi->pgrp = child->pid;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002195#endif
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002196 }
Eric Andersen25f27032001-04-26 23:22:31 +00002197
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002198 if (i)
2199 close(nextin);
2200 if ((i + 1) < pi->num_progs)
2201 close(pipefds[1]); /* write end */
2202 /* Pass read (output) pipe end to next iteration */
Eric Andersen25f27032001-04-26 23:22:31 +00002203 nextin = pipefds[0];
2204 }
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002205
Denis Vlasenko05743d72008-02-10 12:10:08 +00002206 if (!pi->running_progs) {
2207 debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n");
2208 return 1;
2209 }
2210
2211 debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->running_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00002212 return -1;
2213}
2214
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002215#ifndef debug_print_tree
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002216static void debug_print_tree(struct pipe *pi, int lvl)
2217{
2218 static const char *PIPE[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002219 [PIPE_SEQ] = "SEQ",
2220 [PIPE_AND] = "AND",
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002221 [PIPE_OR ] = "OR" ,
2222 [PIPE_BG ] = "BG" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002223 };
2224 static const char *RES[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002225 [RES_NONE ] = "NONE" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00002226#if ENABLE_HUSH_IF
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002227 [RES_IF ] = "IF" ,
2228 [RES_THEN ] = "THEN" ,
2229 [RES_ELIF ] = "ELIF" ,
2230 [RES_ELSE ] = "ELSE" ,
2231 [RES_FI ] = "FI" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00002232#endif
2233#if ENABLE_HUSH_LOOPS
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002234 [RES_FOR ] = "FOR" ,
2235 [RES_WHILE] = "WHILE",
2236 [RES_UNTIL] = "UNTIL",
2237 [RES_DO ] = "DO" ,
2238 [RES_DONE ] = "DONE" ,
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002239 [RES_IN ] = "IN" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00002240#endif
2241 [RES_XXXX ] = "XXXX" ,
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002242 [RES_SNTX ] = "SNTX" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002243 };
2244
2245 int pin, prn;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002246
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002247 pin = 0;
2248 while (pi) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002249 fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "",
2250 pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002251 prn = 0;
2252 while (prn < pi->num_progs) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002253 struct child_prog *child = &pi->progs[prn];
2254 char **argv = child->argv;
2255
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002256 fprintf(stderr, "%*s prog %d", lvl*2, "", prn);
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002257 if (child->group) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002258 fprintf(stderr, " group %s: (argv=%p)\n",
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002259 (child->subshell ? "()" : "{}"),
2260 argv);
2261 debug_print_tree(child->group, lvl+1);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002262 prn++;
2263 continue;
2264 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002265 if (argv) while (*argv) {
2266 fprintf(stderr, " '%s'", *argv);
2267 argv++;
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002268 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002269 fprintf(stderr, "\n");
2270 prn++;
2271 }
2272 pi = pi->next;
2273 pin++;
2274 }
2275}
2276#endif
2277
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002278/* NB: called by pseudo_exec, and therefore must not modify any
2279 * global data until exec/_exit (we can be a child after vfork!) */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002280static int run_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002281{
Denis Vlasenko06810332007-05-21 23:30:54 +00002282 struct pipe *rpipe;
2283#if ENABLE_HUSH_LOOPS
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002284 char *for_varname = NULL;
2285 char **for_lcur = NULL;
2286 char **for_list = NULL;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002287 int flag_rep = 0;
Denis Vlasenko06810332007-05-21 23:30:54 +00002288#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002289 int flag_skip = 1;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002290 int rcode = 0; /* probably for gcc only */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002291 int flag_restore = 0;
Denis Vlasenko06810332007-05-21 23:30:54 +00002292#if ENABLE_HUSH_IF
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002293 int if_code = 0, next_if_code = 0; /* need double-buffer to handle elif */
Denis Vlasenko06810332007-05-21 23:30:54 +00002294#else
2295 enum { if_code = 0, next_if_code = 0 };
2296#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002297 reserved_style rword;
2298 reserved_style skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002299
Denis Vlasenko05743d72008-02-10 12:10:08 +00002300 debug_printf_exec("run_list start lvl %d\n", run_list_level + 1);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002301
Denis Vlasenko06810332007-05-21 23:30:54 +00002302#if ENABLE_HUSH_LOOPS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002303 /* check syntax for "for" */
2304 for (rpipe = pi; rpipe; rpipe = rpipe->next) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002305 if ((rpipe->res_word == RES_IN || rpipe->res_word == RES_FOR)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002306 && (rpipe->next == NULL)
2307 ) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00002308 syntax("malformed for"); /* no IN or no commands after IN */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002309 debug_printf_exec("run_list lvl %d return 1\n", run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002310 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002311 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002312 if ((rpipe->res_word == RES_IN && rpipe->next->res_word == RES_IN && rpipe->next->progs[0].argv != NULL)
2313 || (rpipe->res_word == RES_FOR && rpipe->next->res_word != RES_IN)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002314 ) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002315 /* TODO: what is tested in the first condition? */
Denis Vlasenko90e485c2007-05-23 15:22:50 +00002316 syntax("malformed for"); /* 2nd condition: not followed by IN */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002317 debug_printf_exec("run_list lvl %d return 1\n", run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002318 return 1;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002319 }
2320 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002321#else
2322 rpipe = NULL;
2323#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002324
2325#if ENABLE_HUSH_JOB
2326 /* Example of nested list: "while true; do { sleep 1 | exit 2; } done".
2327 * We are saving state before entering outermost list ("while...done")
2328 * so that ctrl-Z will correctly background _entire_ outermost list,
2329 * not just a part of it (like "sleep 1 | exit 2") */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002330 if (++run_list_level == 1 && interactive_fd) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002331 if (sigsetjmp(toplevel_jb, 1)) {
2332 /* ctrl-Z forked and we are parent; or ctrl-C.
2333 * Sighandler has longjmped us here */
2334 signal(SIGINT, SIG_IGN);
2335 signal(SIGTSTP, SIG_IGN);
2336 /* Restore level (we can be coming from deep inside
2337 * nested levels) */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002338 run_list_level = 1;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002339#if ENABLE_FEATURE_SH_STANDALONE
2340 if (nofork_save.saved) { /* if save area is valid */
2341 debug_printf_jobs("exiting nofork early\n");
2342 restore_nofork_data(&nofork_save);
2343 }
2344#endif
2345 if (ctrl_z_flag) {
2346 /* ctrl-Z has forked and stored pid of the child in pi->pid.
2347 * Remember this child as background job */
2348 insert_bg_job(pi);
2349 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002350 /* ctrl-C. We just stop doing whatever we were doing */
Denis Vlasenko4daad902007-09-27 10:20:47 +00002351 bb_putchar('\n');
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002352 }
2353 rcode = 0;
2354 goto ret;
2355 }
2356 /* ctrl-Z handler will store pid etc in pi */
2357 toplevel_list = pi;
2358 ctrl_z_flag = 0;
2359#if ENABLE_FEATURE_SH_STANDALONE
2360 nofork_save.saved = 0; /* in case we will run a nofork later */
2361#endif
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +00002362 signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002363 signal(SIGINT, handler_ctrl_c);
2364 }
Denis Vlasenko05743d72008-02-10 12:10:08 +00002365#endif /* JOB */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002366
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002367 for (; pi; pi = flag_restore ? rpipe : pi->next) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002368 rword = pi->res_word;
Denis Vlasenko06810332007-05-21 23:30:54 +00002369#if ENABLE_HUSH_LOOPS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002370 if (rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002371 flag_restore = 0;
2372 if (!rpipe) {
2373 flag_rep = 0;
2374 rpipe = pi;
2375 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002376 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002377#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002378 debug_printf_exec(": rword=%d if_code=%d next_if_code=%d skip_more=%d\n",
2379 rword, if_code, next_if_code, skip_more_for_this_rword);
2380 if (rword == skip_more_for_this_rword && flag_skip) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002381 if (pi->followup == PIPE_SEQ)
2382 flag_skip = 0;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002383 continue;
2384 }
2385 flag_skip = 1;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002386 skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko06810332007-05-21 23:30:54 +00002387#if ENABLE_HUSH_IF
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002388 if (rword == RES_THEN || rword == RES_ELSE)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002389 if_code = next_if_code;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002390 if (rword == RES_THEN && if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002391 continue;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002392 if (rword == RES_ELSE && !if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002393 continue;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002394 if (rword == RES_ELIF && !if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002395 break;
Denis Vlasenko06810332007-05-21 23:30:54 +00002396#endif
2397#if ENABLE_HUSH_LOOPS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002398 if (rword == RES_FOR && pi->num_progs) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002399 if (!for_lcur) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002400 /* first loop through for */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002401 /* if no variable values after "in" we skip "for" */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002402 if (!pi->next->progs->argv)
2403 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002404 /* create list of variable values */
Denis Vlasenko82dfec32008-06-16 12:47:11 +00002405 debug_print_strings("for_list made from", pi->next->progs->argv);
Denis Vlasenko170435c2007-05-23 13:01:10 +00002406 for_list = expand_strvec_to_strvec(pi->next->progs->argv);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00002407 debug_print_strings("for_list", for_list);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002408 for_lcur = for_list;
2409 for_varname = pi->progs->argv[0];
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002410 pi->progs->argv[0] = NULL;
2411 flag_rep = 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002412 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002413 free(pi->progs->argv[0]);
2414 if (!*for_lcur) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002415 /* for loop is over, clean up */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002416 free(for_list);
2417 for_lcur = NULL;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002418 flag_rep = 0;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002419 pi->progs->argv[0] = for_varname;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002420 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002421 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002422 /* insert next value from for_lcur */
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002423 /* vda: does it need escaping? */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002424 pi->progs->argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002425 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002426 if (rword == RES_IN)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002427 continue;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002428 if (rword == RES_DO) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002429 if (!flag_rep)
2430 continue;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002431 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002432 if (rword == RES_DONE) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002433 if (flag_rep) {
2434 flag_restore = 1;
2435 } else {
2436 rpipe = NULL;
2437 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002438 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002439#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002440 if (pi->num_progs == 0)
2441 continue;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002442 debug_printf_exec(": run_pipe with %d members\n", pi->num_progs);
2443 rcode = run_pipe(pi);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002444 if (rcode != -1) {
Eric Andersen25f27032001-04-26 23:22:31 +00002445 /* We only ran a builtin: rcode was set by the return value
Denis Vlasenko05743d72008-02-10 12:10:08 +00002446 * of run_pipe(), and we don't need to wait for anything. */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002447 } else if (pi->followup == PIPE_BG) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002448 /* What does bash do with attempts to background builtins? */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002449 /* Even bash 3.2 doesn't do that well with nested bg:
2450 * try "{ { sleep 10; echo DEEP; } & echo HERE; } &".
Denis Vlasenko170435c2007-05-23 13:01:10 +00002451 * I'm NOT treating inner &'s as jobs */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002452#if ENABLE_HUSH_JOB
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002453 if (run_list_level == 1)
Denis Vlasenko170435c2007-05-23 13:01:10 +00002454 insert_bg_job(pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002455#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002456 rcode = EXIT_SUCCESS;
2457 } else {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002458#if ENABLE_HUSH_JOB
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002459 if (run_list_level == 1 && interactive_fd) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00002460 /* waits for completion, then fg's main shell */
Denis Vlasenko52881e92007-04-21 13:42:52 +00002461 rcode = checkjobs_and_fg_shell(pi);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002462 } else
2463#endif
2464 {
Denis Vlasenko170435c2007-05-23 13:01:10 +00002465 /* this one just waits for completion */
Eric Andersenc798b072001-06-22 06:23:03 +00002466 rcode = checkjobs(pi);
Eric Andersen25f27032001-04-26 23:22:31 +00002467 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002468 debug_printf_exec(": checkjobs returned %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002469 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002470 debug_printf_exec(": setting last_return_code=%d\n", rcode);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002471 last_return_code = rcode;
Denis Vlasenko06810332007-05-21 23:30:54 +00002472#if ENABLE_HUSH_IF
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002473 if (rword == RES_IF || rword == RES_ELIF)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002474 next_if_code = rcode; /* can be overwritten a number of times */
Denis Vlasenko06810332007-05-21 23:30:54 +00002475#endif
2476#if ENABLE_HUSH_LOOPS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002477 if (rword == RES_WHILE)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002478 flag_rep = !last_return_code;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002479 if (rword == RES_UNTIL)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002480 flag_rep = last_return_code;
Denis Vlasenko06810332007-05-21 23:30:54 +00002481#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002482 if ((rcode == EXIT_SUCCESS && pi->followup == PIPE_OR)
2483 || (rcode != EXIT_SUCCESS && pi->followup == PIPE_AND)
2484 ) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002485 skip_more_for_this_rword = rword;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002486 }
Eric Andersen028b65b2001-06-28 01:10:11 +00002487 checkjobs(NULL);
Eric Andersen25f27032001-04-26 23:22:31 +00002488 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002489
2490#if ENABLE_HUSH_JOB
2491 if (ctrl_z_flag) {
2492 /* ctrl-Z forked somewhere in the past, we are the child,
2493 * and now we completed running the list. Exit. */
2494 exit(rcode);
2495 }
2496 ret:
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00002497 if (!--run_list_level && interactive_fd) {
2498 signal(SIGTSTP, SIG_IGN);
2499 signal(SIGINT, SIG_IGN);
2500 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002501#endif
Denis Vlasenko05743d72008-02-10 12:10:08 +00002502 debug_printf_exec("run_list lvl %d return %d\n", run_list_level + 1, rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002503 return rcode;
2504}
2505
Eric Andersen25f27032001-04-26 23:22:31 +00002506/* return code is the exit status of the pipe */
Eric Andersenbf7df042001-05-23 22:18:35 +00002507static int free_pipe(struct pipe *pi, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002508{
2509 char **p;
2510 struct child_prog *child;
2511 struct redir_struct *r, *rnext;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002512 int a, i, ret_code = 0;
Eric Andersen52a97ca2001-06-22 06:49:26 +00002513
2514 if (pi->stopped_progs > 0)
2515 return ret_code;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002516 debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid());
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002517 for (i = 0; i < pi->num_progs; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002518 child = &pi->progs[i];
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002519 debug_printf_clean("%s command %d:\n", indenter(indent), i);
Eric Andersen25f27032001-04-26 23:22:31 +00002520 if (child->argv) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002521 for (a = 0, p = child->argv; *p; a++, p++) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002522 debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p);
Eric Andersen25f27032001-04-26 23:22:31 +00002523 }
Denis Vlasenkof962a032007-11-23 12:50:54 +00002524 free_strings(child->argv);
2525 child->argv = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002526 } else if (child->group) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002527 debug_printf_clean("%s begin group (subshell:%d)\n", indenter(indent), child->subshell);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002528 ret_code = free_pipe_list(child->group, indent+3);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002529 debug_printf_clean("%s end group\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002530 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002531 debug_printf_clean("%s (nil)\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002532 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002533 for (r = child->redirects; r; r = rnext) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002534 debug_printf_clean("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->type].descrip);
Eric Andersen25f27032001-04-26 23:22:31 +00002535 if (r->dup == -1) {
Eric Andersen817e73c2001-06-06 17:56:09 +00002536 /* guard against the case >$FOO, where foo is unset or blank */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002537 if (r->glob_word) {
2538 debug_printf_clean(" %s\n", r->glob_word[0]);
2539 free_strings(r->glob_word);
2540 r->glob_word = NULL;
Eric Andersen817e73c2001-06-06 17:56:09 +00002541 }
Eric Andersen25f27032001-04-26 23:22:31 +00002542 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002543 debug_printf_clean("&%d\n", r->dup);
Eric Andersen25f27032001-04-26 23:22:31 +00002544 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002545 rnext = r->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002546 free(r);
2547 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002548 child->redirects = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002549 }
2550 free(pi->progs); /* children are an array, they get freed all at once */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002551 pi->progs = NULL;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002552#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002553 free(pi->cmdtext);
2554 pi->cmdtext = NULL;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002555#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002556 return ret_code;
2557}
2558
Eric Andersenbf7df042001-05-23 22:18:35 +00002559static int free_pipe_list(struct pipe *head, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002560{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002561 int rcode = 0; /* if list has no members */
Eric Andersen25f27032001-04-26 23:22:31 +00002562 struct pipe *pi, *next;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002563
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002564 for (pi = head; pi; pi = next) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002565 debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word);
Eric Andersenbf7df042001-05-23 22:18:35 +00002566 rcode = free_pipe(pi, indent);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002567 debug_printf_clean("%s pipe followup code %d\n", indenter(indent), pi->followup);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002568 next = pi->next;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002569 /*pi->next = NULL;*/
Eric Andersen25f27032001-04-26 23:22:31 +00002570 free(pi);
2571 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002572 return rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00002573}
2574
2575/* Select which version we will use */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002576static int run_and_free_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002577{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002578 int rcode = 0;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002579 debug_printf_exec("run_and_free_list entered\n");
2580 if (!fake_mode) {
2581 debug_printf_exec(": run_list with %d members\n", pi->num_progs);
2582 rcode = run_list(pi);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002583 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002584 /* free_pipe_list has the side effect of clearing memory.
Denis Vlasenko05743d72008-02-10 12:10:08 +00002585 * In the long run that function can be merged with run_list,
Eric Andersen25f27032001-04-26 23:22:31 +00002586 * but doing that now would hobble the debugging effort. */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002587 free_pipe_list(pi, /* indent: */ 0);
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002588 debug_printf_exec("run_and_free_list return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002589 return rcode;
2590}
2591
Denis Vlasenko170435c2007-05-23 13:01:10 +00002592/* expand_strvec_to_strvec() takes a list of strings, expands
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002593 * all variable references within and returns a pointer to
2594 * a list of expanded strings, possibly with larger number
2595 * of strings. (Think VAR="a b"; echo $VAR).
2596 * This new list is allocated as a single malloc block.
2597 * NULL-terminated list of char* pointers is at the beginning of it,
2598 * followed by strings themself.
2599 * Caller can deallocate entire list by single free(list). */
2600
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002601/* Store given string, finalizing the word and starting new one whenever
2602 * we encounter ifs char(s). This is used for expanding variable values.
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002603 * End-of-string does NOT finalize word: think about 'echo -$VAR-' */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002604static int expand_on_ifs(o_string *output, int n, const char *str)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002605{
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002606 while (1) {
2607 int word_len = strcspn(str, ifs);
2608 if (word_len) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00002609 o_addQstr(output, str, word_len);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002610 str += word_len;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002611 }
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002612 if (!*str) /* EOL - do not finalize word */
2613 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002614 o_addchr(output, '\0');
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002615 debug_print_list("expand_on_ifs", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002616 n = o_save_ptr(output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002617 str += strspn(str, ifs); /* skip ifs chars */
2618 }
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002619 debug_print_list("expand_on_ifs[1]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002620 return n;
2621}
2622
2623/* Expand all variable references in given string, adding words to list[]
2624 * at n, n+1,... positions. Return updated n (so that list[n] is next one
2625 * to be filled). This routine is extremely tricky: has to deal with
2626 * variables/parameters with whitespace, $* and $@, and constructs like
2627 * 'echo -$*-'. If you play here, you must run testsuite afterwards! */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002628/* NB: another bug is that we cannot detect empty strings yet:
2629 * "" or $empty"" expands to zero words, has to expand to empty word */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002630static int expand_vars_to_list(o_string *output, int n, char *arg, char or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002631{
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002632 /* or_mask is either 0 (normal case) or 0x80
2633 * (expansion of right-hand side of assignment == 1-element expand) */
2634
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002635 char first_ch, ored_ch;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002636 int i;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002637 const char *val;
2638 char *p;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002639
2640 ored_ch = 0;
2641
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002642 debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002643 debug_print_list("expand_vars_to_list", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002644 n = o_save_ptr(output, n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002645 debug_print_list("expand_vars_to_list[0]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002646
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002647 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) {
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002648#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002649 o_string subst_result = NULL_O_STRING;
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002650#endif
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002651
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00002652 o_addQstr(output, arg, p - arg);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002653 debug_print_list("expand_vars_to_list[1]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002654 arg = ++p;
2655 p = strchr(p, SPECIAL_VAR_SYMBOL);
2656
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002657 first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002658 ored_ch |= first_ch;
2659 val = NULL;
2660 switch (first_ch & 0x7f) {
2661 /* Highest bit in first_ch indicates that var is double-quoted */
2662 case '$': /* pid */
2663 /* FIXME: (echo $$) should still print pid of main shell */
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00002664 val = utoa(getpid()); /* rootpid? */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002665 break;
2666 case '!': /* bg pid */
2667 val = last_bg_pid ? utoa(last_bg_pid) : (char*)"";
2668 break;
2669 case '?': /* exitcode */
2670 val = utoa(last_return_code);
2671 break;
2672 case '#': /* argc */
2673 val = utoa(global_argc ? global_argc-1 : 0);
2674 break;
2675 case '*':
2676 case '@':
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002677 i = 1;
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002678 if (!global_argv[i])
2679 break;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002680 if (!(first_ch & 0x80)) { /* unquoted $* or $@ */
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002681 while (global_argv[i]) {
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002682 n = expand_on_ifs(output, n, global_argv[i]);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002683 debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, global_argc-1);
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002684 if (global_argv[i++][0] && global_argv[i]) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002685 /* this argv[] is not empty and not last:
2686 * put terminating NUL, start new word */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002687 o_addchr(output, '\0');
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002688 debug_print_list("expand_vars_to_list[2]", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002689 n = o_save_ptr(output, n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002690 debug_print_list("expand_vars_to_list[3]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002691 }
2692 }
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002693 } else
2694 /* If or_mask is nonzero, we handle assignment 'a=....$@.....'
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00002695 * and in this case should treat it like '$*' - see 'else...' below */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002696 if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002697 while (1) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00002698 o_addQstr(output, global_argv[i], strlen(global_argv[i]));
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002699 if (++i >= global_argc)
2700 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002701 o_addchr(output, '\0');
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002702 debug_print_list("expand_vars_to_list[4]", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002703 n = o_save_ptr(output, n);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002704 }
2705 } else { /* quoted $*: add as one word */
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002706 while (1) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00002707 o_addQstr(output, global_argv[i], strlen(global_argv[i]));
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00002708 if (!global_argv[++i])
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002709 break;
2710 if (ifs[0])
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002711 o_addchr(output, ifs[0]);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002712 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002713 }
2714 break;
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002715#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002716 case '`': {
2717 struct in_str input;
2718 *p = '\0';
2719 arg++;
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002720//TODO: can we just stuff it into "output" directly?
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002721 //bb_error_msg("SUBST '%s' first_ch %x", arg, first_ch);
2722 setup_string_in_str(&input, arg);
2723 process_command_subs(&subst_result, &input, NULL);
2724 //bb_error_msg("RES '%s'", subst_result.data);
2725 val = subst_result.data;
2726 goto store_val;
2727 }
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002728#endif
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002729 default:
2730 *p = '\0';
2731 arg[0] = first_ch & 0x7f;
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002732 if (isdigit(arg[0])) {
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002733 i = xatoi_u(arg);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002734 val = NULL;
2735 if (i < global_argc)
2736 val = global_argv[i];
2737 } else
2738 val = lookup_param(arg);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002739 arg[0] = first_ch;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002740 store_val:
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002741 *p = SPECIAL_VAR_SYMBOL;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002742 if (!(first_ch & 0x80)) { /* unquoted $VAR */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002743 if (val) {
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002744 n = expand_on_ifs(output, n, val);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002745 val = NULL;
2746 }
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002747 } /* else: quoted $VAR, val will be appended below */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002748 }
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002749 if (val) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00002750 o_addQstr(output, val, strlen(val));
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002751 }
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002752
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002753#if ENABLE_HUSH_TICK
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002754 o_free(&subst_result);
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002755#endif
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002756 arg = ++p;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002757 } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */
2758
Denis Vlasenko2e76c3f2008-06-10 18:27:50 +00002759 if (arg[0]) {
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002760 debug_print_list("expand_vars_to_list[a]", output, n);
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00002761 o_addQstr(output, arg, strlen(arg) + 1);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002762 debug_print_list("expand_vars_to_list[b]", output, n);
Denis Vlasenko2e76c3f2008-06-10 18:27:50 +00002763 } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */
2764 && !(ored_ch & 0x80) /* and all vars were not quoted. */
2765 ) {
2766 n--;
2767 /* allow to reuse list[n] later without re-growth */
2768 output->has_empty_slot = 1;
2769 } else {
2770 o_addchr(output, '\0');
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002771 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002772 return n;
2773}
2774
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002775static char **expand_variables(char **argv, int or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002776{
2777 int n;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002778 char **list;
2779 char **v;
2780 o_string output = NULL_O_STRING;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002781
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002782 if (or_mask & 0x100)
2783 output.o_glob = 1;
2784
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002785 n = 0;
2786 v = argv;
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002787 while (*v) {
2788 n = expand_vars_to_list(&output, n, *v, (char)or_mask);
2789 v++;
2790 }
2791 debug_print_list("expand_variables", &output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002792
Denis Vlasenko82dfec32008-06-16 12:47:11 +00002793 /* output.data (malloced in one block) gets returned in "list" */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002794 list = o_finalize_list(&output, n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002795 debug_print_strings("expand_variables[1]", list);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002796 return list;
2797}
2798
Denis Vlasenko170435c2007-05-23 13:01:10 +00002799static char **expand_strvec_to_strvec(char **argv)
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002800{
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002801 return expand_variables(argv, 0x100);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002802}
2803
Denis Vlasenko82dfec32008-06-16 12:47:11 +00002804/* used for expansion of right hand of assignments */
2805/* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs
2806 * "v=/bin/c*" */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002807static char *expand_string_to_string(const char *str)
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002808{
2809 char *argv[2], **list;
2810
2811 argv[0] = (char*)str;
2812 argv[1] = NULL;
2813 list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002814 if (ENABLE_HUSH_DEBUG)
2815 if (!list[0] || list[1])
2816 bb_error_msg_and_die("BUG in varexp2");
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002817 /* actually, just move string 2*sizeof(char*) bytes back */
2818 strcpy((char*)list, list[0]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00002819 debug_printf_expand("string_to_string='%s'\n", (char*)list);
2820 return (char*)list;
2821}
2822
Denis Vlasenko82dfec32008-06-16 12:47:11 +00002823/* used for eval */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002824static char* expand_strvec_to_string(char **argv)
2825{
2826 char **list;
2827
2828 list = expand_variables(argv, 0x80);
2829 /* Convert all NULs to spaces */
2830 if (list[0]) {
2831 int n = 1;
2832 while (list[n]) {
2833 if (ENABLE_HUSH_DEBUG)
2834 if (list[n-1] + strlen(list[n-1]) + 1 != list[n])
2835 bb_error_msg_and_die("BUG in varexp3");
2836 list[n][-1] = ' '; /* TODO: or to ifs[0]? */
2837 n++;
2838 }
2839 }
2840 strcpy((char*)list, list[0]);
2841 debug_printf_expand("strvec_to_string='%s'\n", (char*)list);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002842 return (char*)list;
2843}
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002844
Eric Andersenf72f5622001-05-15 23:21:41 +00002845/* This is used to get/check local shell variables */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002846static struct variable *get_local_var(const char *name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002847{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002848 struct variable *cur;
2849 int len;
Eric Andersenf72f5622001-05-15 23:21:41 +00002850
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002851 if (!name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002852 return NULL;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002853 len = strlen(name);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002854 for (cur = top_var; cur; cur = cur->next) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002855 if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=')
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002856 return cur;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002857 }
Eric Andersenf72f5622001-05-15 23:21:41 +00002858 return NULL;
2859}
2860
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002861/* str holds "NAME=VAL" and is expected to be malloced.
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002862 * We take ownership of it. */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002863static int set_local_var(char *str, int flg_export)
Eric Andersen78a7c992001-05-15 16:30:25 +00002864{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002865 struct variable *cur;
2866 char *value;
2867 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002868
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002869 value = strchr(str, '=');
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002870 if (!value) { /* not expected to ever happen? */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002871 free(str);
Eric Andersen99785762001-05-22 21:37:48 +00002872 return -1;
2873 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002874
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002875 name_len = value - str + 1; /* including '=' */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002876 cur = top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */
2877 while (1) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002878 if (strncmp(cur->varstr, str, name_len) != 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002879 if (!cur->next) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002880 /* Bail out. Note that now cur points
2881 * to last var in linked list */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002882 break;
Eric Andersen20a69a72001-05-15 17:24:44 +00002883 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002884 cur = cur->next;
2885 continue;
Eric Andersen20a69a72001-05-15 17:24:44 +00002886 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002887 /* We found an existing var with this name */
2888 *value = '\0';
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002889 if (cur->flg_read_only) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002890 bb_error_msg("%s: readonly variable", str);
2891 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002892 return -1;
2893 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002894 unsetenv(str); /* just in case */
2895 *value = '=';
2896 if (strcmp(cur->varstr, str) == 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002897 free_and_exp:
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002898 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002899 goto exp;
2900 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002901 if (cur->max_len >= strlen(str)) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002902 /* This one is from startup env, reuse space */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002903 strcpy(cur->varstr, str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002904 goto free_and_exp;
2905 }
2906 /* max_len == 0 signifies "malloced" var, which we can
2907 * (and has to) free */
2908 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002909 free(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002910 cur->max_len = 0;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002911 goto set_str_and_exp;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002912 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002913
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002914 /* Not found - create next variable struct */
2915 cur->next = xzalloc(sizeof(*cur));
2916 cur = cur->next;
2917
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002918 set_str_and_exp:
2919 cur->varstr = str;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002920 exp:
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002921 if (flg_export)
2922 cur->flg_export = 1;
2923 if (cur->flg_export)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002924 return putenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002925 return 0;
Eric Andersen20a69a72001-05-15 17:24:44 +00002926}
2927
Eric Andersenf72f5622001-05-15 23:21:41 +00002928static void unset_local_var(const char *name)
Eric Andersen20a69a72001-05-15 17:24:44 +00002929{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002930 struct variable *cur;
2931 struct variable *prev = prev; /* for gcc */
2932 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002933
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002934 if (!name)
2935 return;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002936 name_len = strlen(name);
2937 cur = top_var;
2938 while (cur) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002939 if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002940 if (cur->flg_read_only) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002941 bb_error_msg("%s: readonly variable", name);
Eric Andersen94ac2442001-05-22 19:05:18 +00002942 return;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002943 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002944 /* prev is ok to use here because 1st variable, HUSH_VERSION,
2945 * is ro, and we cannot reach this code on the 1st pass */
2946 prev->next = cur->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002947 unsetenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002948 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002949 free(cur->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002950 free(cur);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002951 return;
Eric Andersenf72f5622001-05-15 23:21:41 +00002952 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002953 prev = cur;
2954 cur = cur->next;
Eric Andersen20a69a72001-05-15 17:24:44 +00002955 }
Eric Andersen78a7c992001-05-15 16:30:25 +00002956}
2957
2958static int is_assignment(const char *s)
2959{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002960 if (!s || !isalpha(*s))
2961 return 0;
2962 s++;
2963 while (isalnum(*s) || *s == '_')
2964 s++;
2965 return *s == '=';
Eric Andersen78a7c992001-05-15 16:30:25 +00002966}
2967
Eric Andersen25f27032001-04-26 23:22:31 +00002968/* the src parameter allows us to peek forward to a possible &n syntax
2969 * for file descriptor duplication, e.g., "2>&1".
2970 * Return code is 0 normally, 1 if a syntax error is detected in src.
2971 * Resource errors (in xmalloc) cause the process to exit */
2972static int setup_redirect(struct p_context *ctx, int fd, redir_type style,
2973 struct in_str *input)
2974{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002975 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00002976 struct redir_struct *redir = child->redirects;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002977 struct redir_struct *last_redir = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002978
2979 /* Create a new redir_struct and drop it onto the end of the linked list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002980 while (redir) {
2981 last_redir = redir;
2982 redir = redir->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002983 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00002984 redir = xzalloc(sizeof(struct redir_struct));
2985 /* redir->next = NULL; */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002986 /* redir->glob_word = NULL; */
Eric Andersen25f27032001-04-26 23:22:31 +00002987 if (last_redir) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002988 last_redir->next = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002989 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002990 child->redirects = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002991 }
2992
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002993 redir->type = style;
2994 redir->fd = (fd == -1) ? redir_table[style].default_fd : fd;
Eric Andersen25f27032001-04-26 23:22:31 +00002995
2996 debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
2997
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002998 /* Check for a '2>&1' type redirect */
Eric Andersen25f27032001-04-26 23:22:31 +00002999 redir->dup = redirect_dup_num(input);
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003000 if (redir->dup == -2)
3001 return 1; /* syntax error */
Eric Andersen25f27032001-04-26 23:22:31 +00003002 if (redir->dup != -1) {
3003 /* Erik had a check here that the file descriptor in question
Eric Andersen83a2ae22001-05-07 17:59:25 +00003004 * is legit; I postpone that to "run time"
3005 * A "-" representation of "close me" shows up as a -3 here */
Eric Andersen25f27032001-04-26 23:22:31 +00003006 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
3007 } else {
3008 /* We do _not_ try to open the file that src points to,
3009 * since we need to return and let src be expanded first.
3010 * Set ctx->pending_redirect, so we know what to do at the
Denis Vlasenko201c72a2007-05-25 10:00:36 +00003011 * end of the next parsed word. */
Eric Andersen25f27032001-04-26 23:22:31 +00003012 ctx->pending_redirect = redir;
3013 }
3014 return 0;
3015}
3016
Denis Vlasenkoac678ec2007-04-16 22:32:04 +00003017static struct pipe *new_pipe(void)
3018{
Eric Andersen25f27032001-04-26 23:22:31 +00003019 struct pipe *pi;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003020 pi = xzalloc(sizeof(struct pipe));
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003021 /*pi->followup = 0; - deliberately invalid value */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003022 if (RES_NONE)
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003023 pi->res_word = RES_NONE;
Eric Andersen25f27032001-04-26 23:22:31 +00003024 return pi;
3025}
3026
3027static void initialize_context(struct p_context *ctx)
3028{
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003029 memset(ctx, 0, sizeof(*ctx));
Denis Vlasenko1a735862007-05-23 00:32:25 +00003030 ctx->pipe = ctx->list_head = new_pipe();
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003031 /* Create the memory for child, roughly:
3032 * ctx->pipe->progs = new struct child_prog;
3033 * ctx->pipe->progs[0].family = ctx->pipe;
3034 * ctx->child = &ctx->pipe->progs[0];
3035 */
3036 done_command(ctx);
Eric Andersen25f27032001-04-26 23:22:31 +00003037}
3038
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003039/* If a reserved word is found and processed, parse context is modified
3040 * and 1 is returned.
3041 * Handles if, then, elif, else, fi, for, while, until, do, done.
Eric Andersen25f27032001-04-26 23:22:31 +00003042 * case, function, and select are obnoxious, save those for later.
3043 */
Denis Vlasenko06810332007-05-21 23:30:54 +00003044#if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003045static int reserved_word(const o_string *word, struct p_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00003046{
3047 struct reserved_combo {
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003048 char literal[7];
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003049 unsigned char res;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003050 int flag;
Eric Andersen25f27032001-04-26 23:22:31 +00003051 };
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003052 enum {
3053 FLAG_END = (1 << RES_NONE ),
3054#if ENABLE_HUSH_IF
3055 FLAG_IF = (1 << RES_IF ),
3056 FLAG_THEN = (1 << RES_THEN ),
3057 FLAG_ELIF = (1 << RES_ELIF ),
3058 FLAG_ELSE = (1 << RES_ELSE ),
3059 FLAG_FI = (1 << RES_FI ),
3060#endif
3061#if ENABLE_HUSH_LOOPS
3062 FLAG_FOR = (1 << RES_FOR ),
3063 FLAG_WHILE = (1 << RES_WHILE),
3064 FLAG_UNTIL = (1 << RES_UNTIL),
3065 FLAG_DO = (1 << RES_DO ),
3066 FLAG_DONE = (1 << RES_DONE ),
3067 FLAG_IN = (1 << RES_IN ),
3068#endif
3069 FLAG_START = (1 << RES_XXXX ),
3070 };
Eric Andersen25f27032001-04-26 23:22:31 +00003071 /* Mostly a list of accepted follow-up reserved words.
3072 * FLAG_END means we are done with the sequence, and are ready
3073 * to turn the compound list into a command.
3074 * FLAG_START means the word must start a new compound list.
3075 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003076 static const struct reserved_combo reserved_list[] = {
Denis Vlasenko06810332007-05-21 23:30:54 +00003077#if ENABLE_HUSH_IF
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003078 { "!", RES_NONE, 0 },
Eric Andersen25f27032001-04-26 23:22:31 +00003079 { "if", RES_IF, FLAG_THEN | FLAG_START },
3080 { "then", RES_THEN, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
3081 { "elif", RES_ELIF, FLAG_THEN },
3082 { "else", RES_ELSE, FLAG_FI },
3083 { "fi", RES_FI, FLAG_END },
Denis Vlasenko06810332007-05-21 23:30:54 +00003084#endif
3085#if ENABLE_HUSH_LOOPS
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003086 { "for", RES_FOR, FLAG_IN | FLAG_START },
3087 { "while", RES_WHILE, FLAG_DO | FLAG_START },
3088 { "until", RES_UNTIL, FLAG_DO | FLAG_START },
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003089 { "in", RES_IN, FLAG_DO },
Eric Andersen25f27032001-04-26 23:22:31 +00003090 { "do", RES_DO, FLAG_DONE },
3091 { "done", RES_DONE, FLAG_END }
Denis Vlasenko06810332007-05-21 23:30:54 +00003092#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003093 };
Denis Vlasenko80b8b392007-06-25 10:55:35 +00003094
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003095 const struct reserved_combo *r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003096
Denis Vlasenko80b8b392007-06-25 10:55:35 +00003097 for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) {
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003098 if (strcmp(word->data, r->literal) != 0)
Denis Vlasenko1a735862007-05-23 00:32:25 +00003099 continue;
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003100 debug_printf("found reserved word %s, res %d\n", r->literal, r->res);
3101 if (r->flag == 0) { /* '!' */
3102 if (ctx->res_w == RES_IN) {
3103 /* 'for a in ! a b c; ...' - ! isn't a keyword here */
3104 break;
3105 }
3106 if (ctx->res_w == RES_FOR /* example: 'for ! a' */
3107 || ctx->ctx_inverted /* bash doesn't accept '! ! true' */
3108 ) {
3109 syntax(NULL);
3110 ctx->res_w = RES_SNTX;
3111 }
3112 ctx->ctx_inverted = 1;
3113 return 1;
3114 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003115 if (r->flag & FLAG_START) {
3116 struct p_context *new;
3117 debug_printf("push stack\n");
Denis Vlasenko06810332007-05-21 23:30:54 +00003118#if ENABLE_HUSH_LOOPS
Denis Vlasenko1a735862007-05-23 00:32:25 +00003119 if (ctx->res_w == RES_IN || ctx->res_w == RES_FOR) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003120 syntax("malformed for"); /* example: 'for if' */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003121 ctx->res_w = RES_SNTX;
Eric Andersenaf44a0e2001-04-27 07:26:12 +00003122 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003123 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003124#endif
3125 new = xmalloc(sizeof(*new));
3126 *new = *ctx; /* physical copy */
3127 initialize_context(ctx);
3128 ctx->stack = new;
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003129 } else if (ctx->res_w == RES_NONE || !(ctx->old_flag & (1 << r->res))) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003130 syntax(NULL);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003131 ctx->res_w = RES_SNTX;
Eric Andersen25f27032001-04-26 23:22:31 +00003132 return 1;
3133 }
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003134 ctx->res_w = r->res;
Denis Vlasenko1a735862007-05-23 00:32:25 +00003135 ctx->old_flag = r->flag;
3136 if (ctx->old_flag & FLAG_END) {
3137 struct p_context *old;
3138 debug_printf("pop stack\n");
3139 done_pipe(ctx, PIPE_SEQ);
3140 old = ctx->stack;
3141 old->child->group = ctx->list_head;
3142 old->child->subshell = 0;
3143 *ctx = *old; /* physical copy */
3144 free(old);
3145 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003146 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003147 }
3148 return 0;
3149}
Denis Vlasenko06810332007-05-21 23:30:54 +00003150#else
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003151#define reserved_word(word, ctx) ((int)0)
Denis Vlasenko06810332007-05-21 23:30:54 +00003152#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003153
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003154/* Word is complete, look at it and update parsing context.
3155 * Normal return is 0.
Eric Andersen25f27032001-04-26 23:22:31 +00003156 * Syntax or xglob errors return 1. */
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003157static int done_word(o_string *word, struct p_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00003158{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003159 struct child_prog *child = ctx->child;
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003160 char ***glob_target;
Eric Andersen25f27032001-04-26 23:22:31 +00003161
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003162 debug_printf_parse("done_word entered: '%s' %p\n", word->data, child);
3163 if (word->length == 0 && !word->nonnull) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003164 debug_printf_parse("done_word return 0: true null, ignored\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003165 return 0;
3166 }
3167 if (ctx->pending_redirect) {
Denis Vlasenkoff097622007-10-01 10:00:45 +00003168 glob_target = &ctx->pending_redirect->glob_word;
Eric Andersen25f27032001-04-26 23:22:31 +00003169 } else {
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003170 if (child->group) { /* TODO: example how to trigger? */
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003171 syntax(NULL);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003172 debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n");
3173 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003174 }
Denis Vlasenko5703c222008-06-15 11:49:42 +00003175 if (!child->argv) {
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003176 debug_printf_parse(": checking '%s' for reserved-ness\n", word->data);
3177 if (reserved_word(word, ctx)) {
3178 o_reset(word);
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003179 debug_printf_parse("done_word return %d\n", (ctx->res_w == RES_SNTX));
3180 return (ctx->res_w == RES_SNTX);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003181 }
Eric Andersen25f27032001-04-26 23:22:31 +00003182 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003183 glob_target = &child->argv;
Eric Andersen25f27032001-04-26 23:22:31 +00003184 }
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003185
3186 if (word->length || word->nonnull) {
3187 *glob_target = add_string_to_strings(*glob_target, xstrdup(word->data));
3188 debug_print_strings("glob_target appended", *glob_target);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003189 }
Eric Andersen25f27032001-04-26 23:22:31 +00003190
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003191 o_reset(word);
Eric Andersen25f27032001-04-26 23:22:31 +00003192 if (ctx->pending_redirect) {
Denis Vlasenkof962a032007-11-23 12:50:54 +00003193 /* NB: don't free_strings(ctx->pending_redirect->glob_word) here */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003194 if (ctx->pending_redirect->glob_word
3195 && ctx->pending_redirect->glob_word[0]
3196 && ctx->pending_redirect->glob_word[1]
3197 ) {
3198 /* more than one word resulted from globbing redir */
3199 ctx->pending_redirect = NULL;
Manuel Novoa III cad53642003-03-19 09:13:01 +00003200 bb_error_msg("ambiguous redirect");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003201 debug_printf_parse("done_word return 1: ambiguous redirect\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003202 return 1;
3203 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003204 ctx->pending_redirect = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003205 }
Denis Vlasenko06810332007-05-21 23:30:54 +00003206#if ENABLE_HUSH_LOOPS
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003207 if (ctx->res_w == RES_FOR) { /* comment? */
3208//TESTING
3209//looks like (word->length == 0 && !word->nonnull) is true here, always
3210//(due to o_reset). done_word would return at once. Why then?
3211// done_word(word, ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003212 done_pipe(ctx, PIPE_SEQ);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003213 }
Denis Vlasenko06810332007-05-21 23:30:54 +00003214#endif
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003215 debug_printf_parse("done_word return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003216 return 0;
3217}
3218
3219/* The only possible error here is out of memory, in which case
3220 * xmalloc exits. */
3221static int done_command(struct p_context *ctx)
3222{
3223 /* The child is really already in the pipe structure, so
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003224 * advance the pipe counter and make a new, null child. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003225 struct pipe *pi = ctx->pipe;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003226 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00003227
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003228 if (child) {
3229 if (child->group == NULL
3230 && child->argv == NULL
3231 && child->redirects == NULL
3232 ) {
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003233 debug_printf_parse("done_command: skipping null cmd, num_progs=%d\n", pi->num_progs);
3234 return pi->num_progs;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003235 }
Eric Andersen25f27032001-04-26 23:22:31 +00003236 pi->num_progs++;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003237 debug_printf_parse("done_command: ++num_progs=%d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00003238 } else {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003239 debug_printf_parse("done_command: initializing, num_progs=%d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00003240 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003241
3242 /* Only real trickiness here is that the uncommitted
3243 * child structure is not counted in pi->num_progs. */
Eric Andersen25f27032001-04-26 23:22:31 +00003244 pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1));
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003245 child = &pi->progs[pi->num_progs];
Eric Andersen25f27032001-04-26 23:22:31 +00003246
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003247 memset(child, 0, sizeof(*child));
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003248 child->family = pi;
Eric Andersen25f27032001-04-26 23:22:31 +00003249
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003250 ctx->child = child;
Eric Andersen25f27032001-04-26 23:22:31 +00003251 /* but ctx->pipe and ctx->list_head remain unchanged */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003252
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003253 return pi->num_progs; /* used only for 0/nonzero check */
Eric Andersen25f27032001-04-26 23:22:31 +00003254}
3255
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003256static void done_pipe(struct p_context *ctx, pipe_style type)
Eric Andersen25f27032001-04-26 23:22:31 +00003257{
3258 struct pipe *new_p;
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003259 int not_null;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003260
3261 debug_printf_parse("done_pipe entered, followup %d\n", type);
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003262 not_null = done_command(ctx); /* implicit closure of previous command */
Eric Andersen25f27032001-04-26 23:22:31 +00003263 ctx->pipe->followup = type;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003264 ctx->pipe->res_word = ctx->res_w;
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003265 ctx->pipe->pi_inverted = ctx->ctx_inverted;
3266 ctx->ctx_inverted = 0;
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003267 /* Without this check, even just <enter> on command line generates
3268 * tree of three NOPs (!). Which is harmless but annoying.
3269 * IOW: it is safe to do it unconditionally. */
3270 if (not_null) {
3271 new_p = new_pipe();
3272 ctx->pipe->next = new_p;
3273 ctx->pipe = new_p;
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003274 ctx->child = NULL; /* needed! */
3275 /* Create the memory for child, roughly:
3276 * ctx->pipe->progs = new struct child_prog;
3277 * ctx->pipe->progs[0].family = ctx->pipe;
3278 * ctx->child = &ctx->pipe->progs[0];
3279 */
3280 done_command(ctx);
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003281 }
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003282 debug_printf_parse("done_pipe return\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003283}
3284
3285/* peek ahead in the in_str to find out if we have a "&n" construct,
3286 * as in "2>&1", that represents duplicating a file descriptor.
3287 * returns either -2 (syntax error), -1 (no &), or the number found.
3288 */
3289static int redirect_dup_num(struct in_str *input)
3290{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003291 int ch, d = 0, ok = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003292 ch = i_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003293 if (ch != '&') return -1;
3294
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003295 i_getch(input); /* get the & */
3296 ch = i_peek(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003297 if (ch == '-') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003298 i_getch(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003299 return -3; /* "-" represents "close me" */
3300 }
3301 while (isdigit(ch)) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00003302 d = d*10 + (ch-'0');
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003303 ok = 1;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003304 i_getch(input);
3305 ch = i_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003306 }
3307 if (ok) return d;
3308
Manuel Novoa III cad53642003-03-19 09:13:01 +00003309 bb_error_msg("ambiguous redirect");
Eric Andersen25f27032001-04-26 23:22:31 +00003310 return -2;
3311}
3312
3313/* If a redirect is immediately preceded by a number, that number is
3314 * supposed to tell which file descriptor to redirect. This routine
3315 * looks for such preceding numbers. In an ideal world this routine
3316 * needs to handle all the following classes of redirects...
3317 * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
3318 * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
3319 * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
3320 * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
3321 * A -1 output from this program means no valid number was found, so the
3322 * caller should use the appropriate default for this redirection.
3323 */
3324static int redirect_opt_num(o_string *o)
3325{
3326 int num;
3327
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003328 if (o->length == 0)
3329 return -1;
3330 for (num = 0; num < o->length; num++) {
3331 if (!isdigit(*(o->data + num))) {
Eric Andersen25f27032001-04-26 23:22:31 +00003332 return -1;
3333 }
3334 }
3335 /* reuse num (and save an int) */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003336 num = atoi(o->data);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003337 o_reset(o);
Eric Andersen25f27032001-04-26 23:22:31 +00003338 return num;
3339}
3340
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003341#if ENABLE_HUSH_TICK
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00003342static FILE *generate_stream_from_list(struct pipe *head)
Eric Andersen25f27032001-04-26 23:22:31 +00003343{
3344 FILE *pf;
Eric Andersen25f27032001-04-26 23:22:31 +00003345 int pid, channel[2];
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003346
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +00003347 xpipe(channel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003348/* *** NOMMU WARNING *** */
3349/* By using vfork here, we suspend parent till child exits or execs.
3350 * If child will not do it before it fills the pipe, it can block forever
3351 * in write(STDOUT_FILENO), and parent (shell) will be also stuck.
Denis Vlasenko3fe4f982008-06-09 16:02:39 +00003352 * Try this script:
3353 * yes "0123456789012345678901234567890" | dd bs=32 count=64k >TESTFILE
3354 * huge=`cat TESTFILE` # will block here forever
3355 * echo OK
Denis Vlasenko05743d72008-02-10 12:10:08 +00003356 */
3357 pid = BB_MMU ? fork() : vfork();
3358 if (pid < 0)
3359 bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork");
3360 if (pid == 0) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00003361 if (ENABLE_HUSH_JOB)
3362 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenko284d0fa2008-02-16 13:18:17 +00003363 close(channel[0]); /* NB: close _first_, then move fd! */
Denis Vlasenko05743d72008-02-10 12:10:08 +00003364 xmove_fd(channel[1], 1);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003365 /* Prevent it from trying to handle ctrl-z etc */
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003366#if ENABLE_HUSH_JOB
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003367 run_list_level = 1;
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003368#endif
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003369 /* Process substitution is not considered to be usual
3370 * 'command execution'.
3371 * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. */
3372 /* Not needed, we are relying on it being disabled
3373 * everywhere outside actual command execution. */
3374 /*set_jobctrl_sighandler(SIG_IGN);*/
3375 set_misc_sighandler(SIG_DFL);
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003376 /* Freeing 'head' here would break NOMMU. */
3377 _exit(run_list(head));
Eric Andersen25f27032001-04-26 23:22:31 +00003378 }
Eric Andersen25f27032001-04-26 23:22:31 +00003379 close(channel[1]);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003380 pf = fdopen(channel[0], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00003381 return pf;
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003382 /* 'head' is freed by the caller */
Eric Andersen25f27032001-04-26 23:22:31 +00003383}
3384
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003385/* Return code is exit status of the process that is run. */
Denis Vlasenko68404f12008-03-17 09:00:54 +00003386static int process_command_subs(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +00003387 struct in_str *input,
3388 const char *subst_end)
Eric Andersen25f27032001-04-26 23:22:31 +00003389{
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003390 int retcode, ch, eol_cnt;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003391 o_string result = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003392 struct p_context inner;
3393 FILE *p;
3394 struct in_str pipe_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003395
Eric Andersen25f27032001-04-26 23:22:31 +00003396 initialize_context(&inner);
3397
3398 /* recursion to generate command */
3399 retcode = parse_stream(&result, &inner, input, subst_end);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003400 if (retcode != 0)
3401 return retcode; /* syntax error or EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003402 done_word(&result, &inner);
3403 done_pipe(&inner, PIPE_SEQ);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003404 o_free(&result);
Eric Andersen25f27032001-04-26 23:22:31 +00003405
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003406 p = generate_stream_from_list(inner.list_head);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003407 if (p == NULL)
3408 return 1;
Denis Vlasenkoa0898172007-10-01 09:59:01 +00003409 close_on_exec_on(fileno(p));
Eric Andersen25f27032001-04-26 23:22:31 +00003410 setup_file_in_str(&pipe_str, p);
3411
3412 /* now send results of command back into original context */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003413 eol_cnt = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003414 while ((ch = i_getch(&pipe_str)) != EOF) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003415 if (ch == '\n') {
3416 eol_cnt++;
3417 continue;
3418 }
3419 while (eol_cnt) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003420 o_addQchr(dest, '\n');
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003421 eol_cnt--;
3422 }
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003423 /* Even unquoted `echo '\'` results in two backslashes
3424 * (which are converted into one by globbing later) */
3425 if (!dest->o_quote && ch == '\\') {
3426 o_addchr(dest, ch);
3427 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003428 o_addQchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003429 }
3430
3431 debug_printf("done reading from pipe, pclose()ing\n");
3432 /* This is the step that wait()s for the child. Should be pretty
3433 * safe, since we just read an EOF from its stdout. We could try
Denis Vlasenko262d7652007-05-20 21:52:49 +00003434 * to do better, by using wait(), and keeping track of background jobs
Eric Andersen25f27032001-04-26 23:22:31 +00003435 * at the same time. That would be a lot of work, and contrary
3436 * to the KISS philosophy of this program. */
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003437 retcode = fclose(p);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003438 free_pipe_list(inner.list_head, /* indent: */ 0);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003439 debug_printf("closed FILE from child, retcode=%d\n", retcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003440 return retcode;
3441}
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003442#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003443
3444static int parse_group(o_string *dest, struct p_context *ctx,
3445 struct in_str *input, int ch)
3446{
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003447 int rcode;
3448 const char *endch = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003449 struct p_context sub;
3450 struct child_prog *child = ctx->child;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003451
3452 debug_printf_parse("parse_group entered\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003453 if (child->argv) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003454 syntax(NULL);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003455 debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n");
3456 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003457 }
3458 initialize_context(&sub);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003459 endch = "}";
3460 if (ch == '(') {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003461 endch = ")";
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003462 child->subshell = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003463 }
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003464 rcode = parse_stream(dest, &sub, input, endch);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003465 if (rcode == 0) {
3466 done_word(dest, &sub); /* finish off the final word in the subcontext */
3467 done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
3468 child->group = sub.list_head;
3469 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003470 debug_printf_parse("parse_group return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003471 return rcode;
3472 /* child remains "open", available for possible redirects */
3473}
3474
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003475/* Basically useful version until someone wants to get fancier,
Eric Andersen25f27032001-04-26 23:22:31 +00003476 * see the bash man page under "Parameter Expansion" */
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00003477static const char *lookup_param(const char *src)
Eric Andersen25f27032001-04-26 23:22:31 +00003478{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003479 struct variable *var = get_local_var(src);
3480 if (var)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003481 return strchr(var->varstr, '=') + 1;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003482 return NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003483}
3484
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003485#if ENABLE_HUSH_TICK
3486/* Subroutines for copying $(...) and `...` things */
3487static void add_till_backquote(o_string *dest, struct in_str *input);
3488/* '...' */
3489static void add_till_single_quote(o_string *dest, struct in_str *input)
3490{
3491 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003492 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003493 if (ch == EOF)
3494 break;
3495 if (ch == '\'')
3496 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003497 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003498 }
3499}
3500/* "...\"...`..`...." - do we need to handle "...$(..)..." too? */
3501static void add_till_double_quote(o_string *dest, struct in_str *input)
3502{
3503 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003504 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003505 if (ch == '"')
3506 break;
3507 if (ch == '\\') { /* \x. Copy both chars. */
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003508 o_addchr(dest, ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003509 ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003510 }
3511 if (ch == EOF)
3512 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003513 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003514 if (ch == '`') {
3515 add_till_backquote(dest, input);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003516 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003517 continue;
3518 }
Denis Vlasenko5703c222008-06-15 11:49:42 +00003519 //if (ch == '$') ...
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003520 }
3521}
3522/* Process `cmd` - copy contents until "`" is seen. Complicated by
3523 * \` quoting.
3524 * "Within the backquoted style of command substitution, backslash
3525 * shall retain its literal meaning, except when followed by: '$', '`', or '\'.
3526 * The search for the matching backquote shall be satisfied by the first
3527 * backquote found without a preceding backslash; during this search,
3528 * if a non-escaped backquote is encountered within a shell comment,
3529 * a here-document, an embedded command substitution of the $(command)
3530 * form, or a quoted string, undefined results occur. A single-quoted
3531 * or double-quoted string that begins, but does not end, within the
3532 * "`...`" sequence produces undefined results."
3533 * Example Output
3534 * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST
3535 */
3536static void add_till_backquote(o_string *dest, struct in_str *input)
3537{
3538 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003539 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003540 if (ch == '`')
3541 break;
3542 if (ch == '\\') { /* \x. Copy both chars unless it is \` */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003543 int ch2 = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003544 if (ch2 != '`' && ch2 != '$' && ch2 != '\\')
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003545 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003546 ch = ch2;
3547 }
3548 if (ch == EOF)
3549 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003550 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003551 }
3552}
3553/* Process $(cmd) - copy contents until ")" is seen. Complicated by
3554 * quoting and nested ()s.
3555 * "With the $(command) style of command substitution, all characters
3556 * following the open parenthesis to the matching closing parenthesis
3557 * constitute the command. Any valid shell script can be used for command,
3558 * except a script consisting solely of redirections which produces
3559 * unspecified results."
3560 * Example Output
3561 * echo $(echo '(TEST)' BEST) (TEST) BEST
3562 * echo $(echo 'TEST)' BEST) TEST) BEST
3563 * echo $(echo \(\(TEST\) BEST) ((TEST) BEST
3564 */
3565static void add_till_closing_curly_brace(o_string *dest, struct in_str *input)
3566{
3567 int count = 0;
3568 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003569 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003570 if (ch == EOF)
3571 break;
3572 if (ch == '(')
3573 count++;
3574 if (ch == ')')
3575 if (--count < 0)
3576 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003577 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003578 if (ch == '\'') {
3579 add_till_single_quote(dest, input);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003580 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003581 continue;
3582 }
3583 if (ch == '"') {
3584 add_till_double_quote(dest, input);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003585 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003586 continue;
3587 }
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003588 if (ch == '\\') { /* \x. Copy verbatim. Important for \(, \) */
3589 ch = i_getch(input);
3590 if (ch == EOF)
3591 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003592 o_addchr(dest, ch);
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003593 continue;
3594 }
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003595 }
3596}
3597#endif /* ENABLE_HUSH_TICK */
3598
Eric Andersen25f27032001-04-26 23:22:31 +00003599/* return code: 0 for OK, 1 for syntax error */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003600static int handle_dollar(o_string *dest, struct in_str *input)
Eric Andersen25f27032001-04-26 23:22:31 +00003601{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003602 int ch = i_peek(input); /* first character after the $ */
Denis Vlasenkoff097622007-10-01 10:00:45 +00003603 unsigned char quote_mask = dest->o_quote ? 0x80 : 0;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003604
3605 debug_printf_parse("handle_dollar entered: ch='%c'\n", ch);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003606 if (isalpha(ch)) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003607 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003608 while (1) {
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003609 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003610 i_getch(input);
3611 o_addchr(dest, ch | quote_mask);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003612 quote_mask = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003613 ch = i_peek(input);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003614 if (!isalnum(ch) && ch != '_')
3615 break;
Eric Andersen25f27032001-04-26 23:22:31 +00003616 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003617 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003618 } else if (isdigit(ch)) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003619 make_one_char_var:
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003620 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003621 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003622 i_getch(input);
3623 o_addchr(dest, ch | quote_mask);
3624 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003625 } else switch (ch) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003626 case '$': /* pid */
3627 case '!': /* last bg pid */
3628 case '?': /* last exit code */
3629 case '#': /* number of args */
3630 case '*': /* args */
3631 case '@': /* args */
3632 goto make_one_char_var;
Eric Andersen25f27032001-04-26 23:22:31 +00003633 case '{':
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003634 o_addchr(dest, SPECIAL_VAR_SYMBOL);
3635 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003636 /* XXX maybe someone will try to escape the '}' */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003637 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003638 ch = i_getch(input);
Denis Vlasenkob0550012007-05-24 12:18:16 +00003639 if (ch == '}')
3640 break;
3641 if (!isalnum(ch) && ch != '_') {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003642 syntax("unterminated ${name}");
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003643 debug_printf_parse("handle_dollar return 1: unterminated ${name}\n");
3644 return 1;
3645 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003646 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003647 o_addchr(dest, ch | quote_mask);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003648 quote_mask = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003649 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003650 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003651 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003652#if ENABLE_HUSH_TICK
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003653 case '(': {
3654 //int pos = dest->length;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003655 i_getch(input);
3656 o_addchr(dest, SPECIAL_VAR_SYMBOL);
3657 o_addchr(dest, quote_mask | '`');
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003658 add_till_closing_curly_brace(dest, input);
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003659 //bb_error_msg("RES '%s'", dest->data + pos);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003660 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003661 break;
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003662 }
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003663#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003664 case '-':
3665 case '_':
3666 /* still unhandled, but should be eventually */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003667 bb_error_msg("unhandled syntax: $%c", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003668 return 1;
3669 break;
3670 default:
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003671 o_addQchr(dest, '$');
Eric Andersen25f27032001-04-26 23:22:31 +00003672 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003673 debug_printf_parse("handle_dollar return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003674 return 0;
3675}
3676
Eric Andersen25f27032001-04-26 23:22:31 +00003677/* return code is 0 for normal exit, 1 for syntax error */
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003678static int parse_stream(o_string *dest, struct p_context *ctx,
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003679 struct in_str *input, const char *end_trigger)
Eric Andersen25f27032001-04-26 23:22:31 +00003680{
"Vladimir N. Oleynik"4ccd2b42006-01-31 09:27:48 +00003681 int ch, m;
Eric Andersen25f27032001-04-26 23:22:31 +00003682 int redir_fd;
3683 redir_type redir_style;
3684 int next;
3685
Denis Vlasenkoff097622007-10-01 10:00:45 +00003686 /* Only double-quote state is handled in the state variable dest->o_quote.
Eric Andersen25f27032001-04-26 23:22:31 +00003687 * A single-quote triggers a bypass of the main loop until its mate is
Denis Vlasenkoff097622007-10-01 10:00:45 +00003688 * found. When recursing, quote state is passed in via dest->o_quote. */
Eric Andersen25f27032001-04-26 23:22:31 +00003689
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003690 debug_printf_parse("parse_stream entered, end_trigger='%s'\n", end_trigger);
3691
Denis Vlasenko1a735862007-05-23 00:32:25 +00003692 while (1) {
Denis Vlasenko1a735862007-05-23 00:32:25 +00003693 m = CHAR_IFS;
3694 next = '\0';
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003695 ch = i_getch(input);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003696 if (ch != EOF) {
3697 m = charmap[ch];
3698 if (ch != '\n')
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003699 next = i_peek(input);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003700 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003701 debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n",
Denis Vlasenkoff097622007-10-01 10:00:45 +00003702 ch, ch, m, dest->o_quote);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003703 if (m == CHAR_ORDINARY
Denis Vlasenkoff097622007-10-01 10:00:45 +00003704 || (m != CHAR_SPECIAL && dest->o_quote)
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003705 ) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00003706 if (ch == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003707 syntax("unterminated \"");
Denis Vlasenko170435c2007-05-23 13:01:10 +00003708 debug_printf_parse("parse_stream return 1: unterminated \"\n");
3709 return 1;
3710 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003711 o_addQchr(dest, ch);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003712 continue;
3713 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003714 if (m == CHAR_IFS) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003715 if (done_word(dest, ctx)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003716 debug_printf_parse("parse_stream return 1: done_word!=0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003717 return 1;
Eric Andersenaac75e52001-04-30 18:18:45 +00003718 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003719 if (ch == EOF)
3720 break;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003721 /* If we aren't performing a substitution, treat
3722 * a newline as a command separator.
3723 * [why we don't handle it exactly like ';'? --vda] */
3724 if (end_trigger && ch == '\n') {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003725 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003726 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003727 }
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003728 if (end_trigger) {
3729 if (!dest->o_quote && strchr(end_trigger, ch)) {
3730 /* Special case: (...word) makes last word terminate,
3731 * as if ';' is seen */
3732 if (ch == ')') {
3733 done_word(dest, ctx);
3734 done_pipe(ctx, PIPE_SEQ);
3735 }
3736 if (ctx->res_w == RES_NONE) {
3737 debug_printf_parse("parse_stream return 0: end_trigger char found\n");
3738 return 0;
3739 }
3740 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003741 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003742 if (m == CHAR_IFS)
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003743 continue;
3744 switch (ch) {
Eric Andersen25f27032001-04-26 23:22:31 +00003745 case '#':
Denis Vlasenkoff097622007-10-01 10:00:45 +00003746 if (dest->length == 0 && !dest->o_quote) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003747 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003748 ch = i_peek(input);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003749 if (ch == EOF || ch == '\n')
3750 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003751 i_getch(input);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003752 }
Eric Andersen25f27032001-04-26 23:22:31 +00003753 } else {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003754 o_addQchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003755 }
3756 break;
3757 case '\\':
3758 if (next == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003759 syntax("\\<eof>");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003760 debug_printf_parse("parse_stream return 1: \\<eof>\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003761 return 1;
3762 }
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003763 /* bash:
3764 * "The backslash retains its special meaning [in "..."]
3765 * only when followed by one of the following characters:
3766 * $, `, ", \, or <newline>. A double quote may be quoted
3767 * within double quotes by preceding it with a backslash.
3768 * If enabled, history expansion will be performed unless
3769 * an ! appearing in double quotes is escaped using
3770 * a backslash. The backslash preceding the ! is not removed."
3771 */
3772 if (dest->o_quote) {
3773 if (strchr("$`\"\\", next) != NULL) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003774 o_addqchr(dest, i_getch(input));
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003775 } else {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003776 o_addqchr(dest, '\\');
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003777 }
3778 } else {
3779 o_addchr(dest, '\\');
3780 o_addchr(dest, i_getch(input));
3781 }
Eric Andersen25f27032001-04-26 23:22:31 +00003782 break;
3783 case '$':
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003784 if (handle_dollar(dest, input) != 0) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003785 debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n");
3786 return 1;
3787 }
Eric Andersen25f27032001-04-26 23:22:31 +00003788 break;
3789 case '\'':
3790 dest->nonnull = 1;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003791 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003792 ch = i_getch(input);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003793 if (ch == EOF) {
3794 syntax("unterminated '");
3795 debug_printf_parse("parse_stream return 1: unterminated '\n");
3796 return 1;
3797 }
3798 if (ch == '\'')
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003799 break;
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003800 o_addqchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003801 }
Eric Andersen25f27032001-04-26 23:22:31 +00003802 break;
3803 case '"':
3804 dest->nonnull = 1;
Denis Vlasenkoff097622007-10-01 10:00:45 +00003805 dest->o_quote ^= 1; /* invert */
Eric Andersen25f27032001-04-26 23:22:31 +00003806 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003807#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003808 case '`': {
3809 //int pos = dest->length;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003810 o_addchr(dest, SPECIAL_VAR_SYMBOL);
3811 o_addchr(dest, dest->o_quote ? 0x80 | '`' : '`');
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003812 add_till_backquote(dest, input);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003813 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003814 //bb_error_msg("RES '%s'", dest->data + pos);
Eric Andersen25f27032001-04-26 23:22:31 +00003815 break;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003816 }
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003817#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003818 case '>':
3819 redir_fd = redirect_opt_num(dest);
3820 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003821 redir_style = REDIRECT_OVERWRITE;
Eric Andersen25f27032001-04-26 23:22:31 +00003822 if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003823 redir_style = REDIRECT_APPEND;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003824 i_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003825 }
3826#if 0
3827 else if (next == '(') {
3828 syntax(">(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003829 debug_printf_parse("parse_stream return 1: >(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003830 return 1;
3831 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003832#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003833 setup_redirect(ctx, redir_fd, redir_style, input);
3834 break;
3835 case '<':
3836 redir_fd = redirect_opt_num(dest);
3837 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003838 redir_style = REDIRECT_INPUT;
Eric Andersen25f27032001-04-26 23:22:31 +00003839 if (next == '<') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003840 redir_style = REDIRECT_HEREIS;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003841 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003842 } else if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003843 redir_style = REDIRECT_IO;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003844 i_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003845 }
3846#if 0
3847 else if (next == '(') {
3848 syntax("<(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003849 debug_printf_parse("parse_stream return 1: <(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003850 return 1;
3851 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003852#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003853 setup_redirect(ctx, redir_fd, redir_style, input);
3854 break;
3855 case ';':
3856 done_word(dest, ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003857 done_pipe(ctx, PIPE_SEQ);
Eric Andersen25f27032001-04-26 23:22:31 +00003858 break;
3859 case '&':
3860 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003861 if (next == '&') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003862 i_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003863 done_pipe(ctx, PIPE_AND);
Eric Andersen25f27032001-04-26 23:22:31 +00003864 } else {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003865 done_pipe(ctx, PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00003866 }
3867 break;
3868 case '|':
3869 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003870 if (next == '|') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003871 i_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003872 done_pipe(ctx, PIPE_OR);
Eric Andersen25f27032001-04-26 23:22:31 +00003873 } else {
3874 /* we could pick up a file descriptor choice here
3875 * with redirect_opt_num(), but bash doesn't do it.
3876 * "echo foo 2| cat" yields "foo 2". */
3877 done_command(ctx);
3878 }
3879 break;
3880 case '(':
3881 case '{':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003882 if (parse_group(dest, ctx, input, ch) != 0) {
3883 debug_printf_parse("parse_stream return 1: parse_group returned non-0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003884 return 1;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003885 }
Eric Andersen25f27032001-04-26 23:22:31 +00003886 break;
3887 case ')':
3888 case '}':
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003889 /* proper use of this character is caught by end_trigger */
3890 syntax("unexpected } or )");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003891 debug_printf_parse("parse_stream return 1: unexpected '}'\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003892 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003893 default:
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003894 if (ENABLE_HUSH_DEBUG)
3895 bb_error_msg_and_die("BUG: unexpected %c\n", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003896 }
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003897 } /* while (1) */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003898 /* Complain if quote? No, maybe we just finished a command substitution
Eric Andersen25f27032001-04-26 23:22:31 +00003899 * that was quoted. Example:
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003900 * $ echo "`cat foo` plus more"
Eric Andersen25f27032001-04-26 23:22:31 +00003901 * and we just got the EOF generated by the subshell that ran "cat foo"
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003902 * The only real complaint is if we got an EOF when end_trigger != NULL,
Eric Andersen25f27032001-04-26 23:22:31 +00003903 * that is, we were really supposed to get end_trigger, and never got
3904 * one before the EOF. Can't use the standard "syntax error" return code,
3905 * so that parse_stream_outer can distinguish the EOF and exit smoothly. */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003906 debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL));
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003907 if (end_trigger)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003908 return -1;
Eric Andersen25f27032001-04-26 23:22:31 +00003909 return 0;
3910}
3911
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003912static void set_in_charmap(const char *set, int code)
Eric Andersen25f27032001-04-26 23:22:31 +00003913{
Denis Vlasenkof5294e12007-04-14 10:09:57 +00003914 while (*set)
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003915 charmap[(unsigned char)*set++] = code;
Eric Andersen25f27032001-04-26 23:22:31 +00003916}
3917
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003918static void update_charmap(void)
Eric Andersen25f27032001-04-26 23:22:31 +00003919{
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003920 /* char *ifs and char charmap[256] are both globals. */
Eric Andersen25f27032001-04-26 23:22:31 +00003921 ifs = getenv("IFS");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003922 if (ifs == NULL)
3923 ifs = " \t\n";
Eric Andersen25f27032001-04-26 23:22:31 +00003924 /* Precompute a list of 'flow through' behavior so it can be treated
3925 * quickly up front. Computation is necessary because of IFS.
3926 * Special case handling of IFS == " \t\n" is not implemented.
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003927 * The charmap[] array only really needs two bits each,
3928 * and on most machines that would be faster (reduced L1 cache use).
Eric Andersen25f27032001-04-26 23:22:31 +00003929 */
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003930 memset(charmap, CHAR_ORDINARY, sizeof(charmap));
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003931#if ENABLE_HUSH_TICK
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003932 set_in_charmap("\\$\"`", CHAR_SPECIAL);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003933#else
3934 set_in_charmap("\\$\"", CHAR_SPECIAL);
3935#endif
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003936 set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003937 set_in_charmap(ifs, CHAR_IFS); /* are ordinary if quoted */
Eric Andersen25f27032001-04-26 23:22:31 +00003938}
3939
Eric Andersenaff114c2004-04-14 17:51:38 +00003940/* most recursion does not come through here, the exception is
Denis Vlasenko170435c2007-05-23 13:01:10 +00003941 * from builtin_source() and builtin_eval() */
3942static int parse_and_run_stream(struct in_str *inp, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003943{
Eric Andersen25f27032001-04-26 23:22:31 +00003944 struct p_context ctx;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003945 o_string temp = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003946 int rcode;
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003947
Eric Andersen25f27032001-04-26 23:22:31 +00003948 do {
3949 initialize_context(&ctx);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003950 update_charmap();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003951#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00003952 inp->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003953#endif
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003954 /* We will stop & execute after each ';' or '\n'.
3955 * Example: "sleep 9999; echo TEST" + ctrl-C:
3956 * TEST should be printed */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003957 rcode = parse_stream(&temp, &ctx, inp, ";\n");
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003958 if (rcode != 1 && ctx.old_flag != 0) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003959 syntax(NULL);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003960 }
3961 if (rcode != 1 && ctx.old_flag == 0) {
3962 done_word(&temp, &ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003963 done_pipe(&ctx, PIPE_SEQ);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00003964 debug_print_tree(ctx.list_head, 0);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003965 debug_printf_exec("parse_stream_outer: run_and_free_list\n");
3966 run_and_free_list(ctx.list_head);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003967 } else {
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003968 /* We arrive here also if rcode == 1 (error in parse_stream) */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003969 if (ctx.old_flag != 0) {
3970 free(ctx.stack);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003971 o_reset(&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003972 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003973 temp.nonnull = 0;
Denis Vlasenkoff097622007-10-01 10:00:45 +00003974 temp.o_quote = 0;
Denis Vlasenko05743d72008-02-10 12:10:08 +00003975 free_pipe_list(ctx.list_head, /* indent: */ 0);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003976 /* Discard all unprocessed line input, force prompt on */
3977 inp->p = NULL;
3978 inp->promptme = 1;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003979 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003980 o_free(&temp);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003981 /* loop on syntax errors, return on EOF: */
3982 } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP));
Eric Andersen25f27032001-04-26 23:22:31 +00003983 return 0;
3984}
3985
Denis Vlasenko170435c2007-05-23 13:01:10 +00003986static int parse_and_run_string(const char *s, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003987{
3988 struct in_str input;
3989 setup_string_in_str(&input, s);
Denis Vlasenko170435c2007-05-23 13:01:10 +00003990 return parse_and_run_stream(&input, parse_flag);
Eric Andersen25f27032001-04-26 23:22:31 +00003991}
3992
Denis Vlasenko170435c2007-05-23 13:01:10 +00003993static int parse_and_run_file(FILE *f)
Eric Andersen25f27032001-04-26 23:22:31 +00003994{
3995 int rcode;
3996 struct in_str input;
3997 setup_file_in_str(&input, f);
Denis Vlasenko5703c222008-06-15 11:49:42 +00003998 rcode = parse_and_run_stream(&input, 0 /* parse_flag */);
Eric Andersen25f27032001-04-26 23:22:31 +00003999 return rcode;
4000}
4001
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004002#if ENABLE_HUSH_JOB
Eric Andersen6c947d22001-06-25 22:24:38 +00004003/* Make sure we have a controlling tty. If we get started under a job
4004 * aware app (like bash for example), make sure we are now in charge so
4005 * we don't fight over who gets the foreground */
Eric Anderseneaecbf32001-10-31 10:41:31 +00004006static void setup_job_control(void)
Eric Andersen52a97ca2001-06-22 06:49:26 +00004007{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004008 pid_t shell_pgrp;
4009
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00004010 saved_task_pgrp = shell_pgrp = getpgrp();
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00004011 debug_printf_jobs("saved_task_pgrp=%d\n", saved_task_pgrp);
Denis Vlasenko96e1b382007-09-30 23:50:48 +00004012 close_on_exec_on(interactive_fd);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004013
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00004014 /* If we were ran as 'hush &',
4015 * sleep until we are in the foreground. */
4016 while (tcgetpgrp(interactive_fd) != shell_pgrp) {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004017 /* Send TTIN to ourself (should stop us) */
Denis Vlasenkoff131b92007-04-10 15:42:06 +00004018 kill(- shell_pgrp, SIGTTIN);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00004019 shell_pgrp = getpgrp();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004020 }
Eric Andersen52a97ca2001-06-22 06:49:26 +00004021
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004022 /* Ignore job-control and misc signals. */
4023 set_jobctrl_sighandler(SIG_IGN);
4024 set_misc_sighandler(SIG_IGN);
4025//huh? signal(SIGCHLD, SIG_IGN);
4026
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00004027 /* We _must_ restore tty pgrp on fatal signals */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004028 set_fatal_sighandler(sigexit);
Eric Andersen6c947d22001-06-25 22:24:38 +00004029
4030 /* Put ourselves in our own process group. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00004031 setpgrp(); /* is the same as setpgid(our_pid, our_pid); */
Eric Andersen6c947d22001-06-25 22:24:38 +00004032 /* Grab control of the terminal. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00004033 tcsetpgrp(interactive_fd, getpid());
Eric Andersen6c947d22001-06-25 22:24:38 +00004034}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004035#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00004036
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00004037int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Matt Kraai2d91deb2001-08-01 17:21:35 +00004038int hush_main(int argc, char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00004039{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00004040 static const char version_str[] ALIGN1 = "HUSH_VERSION="HUSH_VER_STR;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004041 static const struct variable const_shell_ver = {
4042 .next = NULL,
4043 .varstr = (char*)version_str,
4044 .max_len = 1, /* 0 can provoke free(name) */
4045 .flg_export = 1,
4046 .flg_read_only = 1,
4047 };
4048
Eric Andersen25f27032001-04-26 23:22:31 +00004049 int opt;
4050 FILE *input;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004051 char **e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004052 struct variable *cur_var;
Eric Andersenbc604a22001-05-16 05:24:03 +00004053
Denis Vlasenko574f2f42008-02-27 18:41:59 +00004054 INIT_G();
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004055
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004056 /* Deal with HUSH_VERSION */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00004057 shell_ver = const_shell_ver; /* copying struct here */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004058 top_var = &shell_ver;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004059 unsetenv("HUSH_VERSION"); /* in case it exists in initial env */
4060 /* Initialize our shell local variables with the values
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004061 * currently living in the environment */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004062 cur_var = top_var;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004063 e = environ;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004064 if (e) while (*e) {
4065 char *value = strchr(*e, '=');
4066 if (value) { /* paranoia */
4067 cur_var->next = xzalloc(sizeof(*cur_var));
4068 cur_var = cur_var->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00004069 cur_var->varstr = *e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004070 cur_var->max_len = strlen(*e);
4071 cur_var->flg_export = 1;
4072 }
4073 e++;
4074 }
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004075 putenv((char *)version_str); /* reinstate HUSH_VERSION */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00004076
Denis Vlasenko38f63192007-01-22 09:03:07 +00004077#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00004078 line_input_state = new_line_input_t(FOR_SHELL);
4079#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004080 /* XXX what should these be while sourcing /etc/profile? */
4081 global_argc = argc;
4082 global_argv = argv;
Eric Andersen94ac2442001-05-22 19:05:18 +00004083 /* Initialize some more globals to non-zero values */
4084 set_cwd();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004085#if ENABLE_HUSH_INTERACTIVE
4086#if ENABLE_FEATURE_EDITING
4087 cmdedit_set_initial_prompt();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004088#endif
Eric Andersen94ac2442001-05-22 19:05:18 +00004089 PS2 = "> ";
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004090#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00004091
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004092 if (EXIT_SUCCESS) /* otherwise is already done */
4093 last_return_code = EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00004094
4095 if (argv[0] && argv[0][0] == '-') {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00004096 debug_printf("sourcing /etc/profile\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00004097 input = fopen("/etc/profile", "r");
4098 if (input != NULL) {
Denis Vlasenkoa0898172007-10-01 09:59:01 +00004099 close_on_exec_on(fileno(input));
Denis Vlasenko170435c2007-05-23 13:01:10 +00004100 parse_and_run_file(input);
Eric Andersena90f20b2001-06-26 23:00:21 +00004101 fclose(input);
4102 }
Eric Andersen25f27032001-04-26 23:22:31 +00004103 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004104 input = stdin;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004105
Eric Andersen25f27032001-04-26 23:22:31 +00004106 while ((opt = getopt(argc, argv, "c:xif")) > 0) {
4107 switch (opt) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004108 case 'c':
Denis Vlasenko5f786c22007-04-20 08:35:45 +00004109 global_argv = argv + optind;
4110 global_argc = argc - optind;
Denis Vlasenko5703c222008-06-15 11:49:42 +00004111 opt = parse_and_run_string(optarg, 0 /* parse_flag */);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004112 goto final_return;
4113 case 'i':
Denis Vlasenkoc666f712007-05-16 22:18:54 +00004114 /* Well, we cannot just declare interactiveness,
4115 * we have to have some stuff (ctty, etc) */
4116 /* interactive_fd++; */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004117 break;
4118 case 'f':
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00004119 fake_mode = 1;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004120 break;
4121 default:
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004122#ifndef BB_VER
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004123 fprintf(stderr, "Usage: sh [FILE]...\n"
4124 " or: sh -c command [args]...\n\n");
4125 exit(EXIT_FAILURE);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004126#else
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004127 bb_show_usage();
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004128#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004129 }
4130 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004131#if ENABLE_HUSH_JOB
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004132 /* A shell is interactive if the '-i' flag was given, or if all of
Eric Andersen25f27032001-04-26 23:22:31 +00004133 * the following conditions are met:
Denis Vlasenko55b2de72007-04-18 17:21:28 +00004134 * no -c command
Eric Andersen25f27032001-04-26 23:22:31 +00004135 * no arguments remaining or the -s flag given
4136 * standard input is a terminal
4137 * standard output is a terminal
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004138 * Refer to Posix.2, the description of the 'sh' utility. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004139 if (argv[optind] == NULL && input == stdin
4140 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
4141 ) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004142 saved_tty_pgrp = tcgetpgrp(STDIN_FILENO);
4143 debug_printf("saved_tty_pgrp=%d\n", saved_tty_pgrp);
4144 if (saved_tty_pgrp >= 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004145 /* try to dup to high fd#, >= 255 */
4146 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
4147 if (interactive_fd < 0) {
4148 /* try to dup to any fd */
4149 interactive_fd = dup(STDIN_FILENO);
4150 if (interactive_fd < 0)
4151 /* give up */
4152 interactive_fd = 0;
4153 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004154 // TODO: track & disallow any attempts of user
4155 // to (inadvertently) close/redirect it
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004156 }
Eric Andersen25f27032001-04-26 23:22:31 +00004157 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00004158 debug_printf("interactive_fd=%d\n", interactive_fd);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004159 if (interactive_fd) {
Denis Vlasenko08126f62008-02-11 08:39:11 +00004160 fcntl(interactive_fd, F_SETFD, FD_CLOEXEC);
Eric Andersen25f27032001-04-26 23:22:31 +00004161 /* Looks like they want an interactive shell */
Eric Andersen52a97ca2001-06-22 06:49:26 +00004162 setup_job_control();
Denis Vlasenko4ecfcdc2008-02-11 08:32:31 +00004163 /* -1 is special - makes xfuncs longjmp, not exit
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00004164 * (we reset die_sleep = 0 whereever we [v]fork) */
4165 die_sleep = -1;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004166 if (setjmp(die_jmp)) {
4167 /* xfunc has failed! die die die */
4168 hush_exit(xfunc_error_retval);
4169 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004170#if !ENABLE_FEATURE_SH_EXTRA_QUIET
Denis Vlasenkoca525b42007-06-13 12:27:17 +00004171 printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", bb_banner);
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004172 printf("Enter 'help' for a list of built-in commands.\n\n");
4173#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00004174 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004175#elif ENABLE_HUSH_INTERACTIVE
4176/* no job control compiled, only prompt/line editing */
4177 if (argv[optind] == NULL && input == stdin
4178 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
4179 ) {
4180 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
4181 if (interactive_fd < 0) {
4182 /* try to dup to any fd */
4183 interactive_fd = dup(STDIN_FILENO);
4184 if (interactive_fd < 0)
4185 /* give up */
4186 interactive_fd = 0;
4187 }
Denis Vlasenko4830fc52008-05-25 21:50:55 +00004188 if (interactive_fd) {
Denis Vlasenko08126f62008-02-11 08:39:11 +00004189 fcntl(interactive_fd, F_SETFD, FD_CLOEXEC);
Denis Vlasenko4830fc52008-05-25 21:50:55 +00004190 set_misc_sighandler(SIG_IGN);
4191 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004192 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004193#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004194
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004195 if (argv[optind] == NULL) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00004196 opt = parse_and_run_file(stdin);
Denis Vlasenko05743d72008-02-10 12:10:08 +00004197 } else {
4198 debug_printf("\nrunning script '%s'\n", argv[optind]);
4199 global_argv = argv + optind;
4200 global_argc = argc - optind;
4201 input = xfopen(argv[optind], "r");
Denis Vlasenko459a5ad2008-02-11 08:35:03 +00004202 fcntl(fileno(input), F_SETFD, FD_CLOEXEC);
Denis Vlasenko05743d72008-02-10 12:10:08 +00004203 opt = parse_and_run_file(input);
Eric Andersen25f27032001-04-26 23:22:31 +00004204 }
Eric Andersen25f27032001-04-26 23:22:31 +00004205
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004206 final_return:
4207
Denis Vlasenko38f63192007-01-22 09:03:07 +00004208#if ENABLE_FEATURE_CLEAN_UP
Eric Andersenaeb44c42001-05-22 20:29:00 +00004209 fclose(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004210 if (cwd != bb_msg_unknown)
Eric Andersenaeb44c42001-05-22 20:29:00 +00004211 free((char*)cwd);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004212 cur_var = top_var->next;
4213 while (cur_var) {
4214 struct variable *tmp = cur_var;
4215 if (!cur_var->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00004216 free(cur_var->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004217 cur_var = cur_var->next;
4218 free(tmp);
Eric Andersenaeb44c42001-05-22 20:29:00 +00004219 }
Eric Andersen25f27032001-04-26 23:22:31 +00004220#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004221 hush_exit(opt ? opt : last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +00004222}
Denis Vlasenko96702ca2007-11-23 23:28:55 +00004223
4224
4225#if ENABLE_LASH
4226int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
4227int lash_main(int argc, char **argv)
4228{
4229 //bb_error_msg("lash is deprecated, please use hush instead");
4230 return hush_main(argc, argv);
4231}
4232#endif