blob: e6189f841efe1fbc26054754f1e976f8b1f95ba8 [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
Denis Vlasenko9af22c72008-10-09 12:54:58 +000048 * reserved words: 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 * to-do:
Eric Andersen83a2ae22001-05-07 17:59:25 +000054 * port selected bugfixes from post-0.49 busybox lash - done?
Eric Andersen83a2ae22001-05-07 17:59:25 +000055 * change { and } from special chars to reserved words
Denis Vlasenkobcb25532008-07-28 23:04:34 +000056 * builtins: return, trap, ulimit
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +000057 * test magic exec with redirection only
Eric Andersen25f27032001-04-26 23:22:31 +000058 * check setting of global_argc and global_argv
Eric Andersen25f27032001-04-26 23:22:31 +000059 * follow IFS rules more precisely, including update semantics
Eric Andersen25f27032001-04-26 23:22:31 +000060 * figure out what to do with backslash-newline
Eric Andersen25f27032001-04-26 23:22:31 +000061 * propagate syntax errors, die on resource errors?
62 * continuation lines, both explicit and implicit - done?
63 * memory leak finding and plugging - done?
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +000064 * maybe change charmap[] to use 2-bit entries
Eric Andersen25f27032001-04-26 23:22:31 +000065 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000066 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen25f27032001-04-26 23:22:31 +000067 */
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000068
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000069#include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */
Denis Vlasenko61befda2008-11-25 01:36:03 +000070//TODO: pull in some .h and find out do we have SINGLE_APPLET_MAIN?
71//#include "applet_tables.h" doesn't work
Denis Vlasenkobe709c22008-07-28 00:01:16 +000072#include <glob.h>
73/* #include <dmalloc.h> */
74#if ENABLE_HUSH_CASE
75#include <fnmatch.h>
76#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +000077
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +000078#define HUSH_VER_STR "0.91"
Denis Vlasenkod01ff132007-05-02 21:40:23 +000079
Denis Vlasenko61befda2008-11-25 01:36:03 +000080#if defined SINGLE_APPLET_MAIN
81/* STANDALONE does not make sense, and won't compile */
82#undef CONFIG_FEATURE_SH_STANDALONE
83#undef ENABLE_FEATURE_SH_STANDALONE
84#undef USE_FEATURE_SH_STANDALONE
85#define SKIP_FEATURE_SH_STANDALONE(...) __VA_ARGS__
86#define ENABLE_FEATURE_SH_STANDALONE 0
87#define USE_FEATURE_SH_STANDALONE(...)
88#define SKIP_FEATURE_SH_STANDALONE(...) __VA_ARGS__
89#endif
90
Denis Vlasenko05743d72008-02-10 12:10:08 +000091#if !BB_MMU && ENABLE_HUSH_TICK
92//#undef ENABLE_HUSH_TICK
93//#define ENABLE_HUSH_TICK 0
94#warning On NOMMU, hush command substitution is dangerous.
95#warning Dont use it for commands which produce lots of output.
96#warning For more info see shell/hush.c, generate_stream_from_list().
97#endif
98
99#if !BB_MMU && ENABLE_HUSH_JOB
100#undef ENABLE_HUSH_JOB
101#define ENABLE_HUSH_JOB 0
102#endif
103
104#if !ENABLE_HUSH_INTERACTIVE
105#undef ENABLE_FEATURE_EDITING
106#define ENABLE_FEATURE_EDITING 0
107#undef ENABLE_FEATURE_EDITING_FANCY_PROMPT
108#define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0
Denis Vlasenko8412d792007-10-01 09:59:47 +0000109#endif
110
111
Denis Vlasenko371de4a2008-10-14 12:43:13 +0000112/* Keep unconditionally on for now */
113#define HUSH_DEBUG 1
114/* In progress... */
115#define ENABLE_HUSH_FUNCTIONS 0
116
117
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000118/* If you comment out one of these below, it will be #defined later
119 * to perform debug printfs to stderr: */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000120#define debug_printf(...) do {} while (0)
Denis Vlasenko400c5b62007-05-04 13:07:27 +0000121/* Finer-grained debug switches */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000122#define debug_printf_parse(...) do {} while (0)
123#define debug_print_tree(a, b) do {} while (0)
124#define debug_printf_exec(...) do {} while (0)
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000125#define debug_printf_env(...) do {} while (0)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000126#define debug_printf_jobs(...) do {} while (0)
127#define debug_printf_expand(...) do {} while (0)
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000128#define debug_printf_glob(...) do {} while (0)
129#define debug_printf_list(...) do {} while (0)
Denis Vlasenko30c9cc52008-06-17 07:24:29 +0000130#define debug_printf_subst(...) do {} while (0)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000131#define debug_printf_clean(...) do {} while (0)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000132
133#ifndef debug_printf
134#define debug_printf(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000135#endif
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000136
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000137#ifndef debug_printf_parse
138#define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000139#endif
140
141#ifndef debug_printf_exec
142#define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__)
143#endif
144
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000145#ifndef debug_printf_env
146#define debug_printf_env(...) fprintf(stderr, __VA_ARGS__)
147#endif
148
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000149#ifndef debug_printf_jobs
150#define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000151#define DEBUG_JOBS 1
152#else
153#define DEBUG_JOBS 0
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000154#endif
155
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000156#ifndef debug_printf_expand
157#define debug_printf_expand(...) fprintf(stderr, __VA_ARGS__)
158#define DEBUG_EXPAND 1
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000159#else
160#define DEBUG_EXPAND 0
161#endif
162
163#ifndef debug_printf_glob
164#define debug_printf_glob(...) fprintf(stderr, __VA_ARGS__)
165#define DEBUG_GLOB 1
166#else
167#define DEBUG_GLOB 0
168#endif
169
170#ifndef debug_printf_list
171#define debug_printf_list(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000172#endif
173
Denis Vlasenko30c9cc52008-06-17 07:24:29 +0000174#ifndef debug_printf_subst
175#define debug_printf_subst(...) fprintf(stderr, __VA_ARGS__)
176#endif
177
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000178#ifndef debug_printf_clean
179/* broken, of course, but OK for testing */
180static const char *indenter(int i)
181{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000182 static const char blanks[] ALIGN1 =
183 " ";
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000184 return &blanks[sizeof(blanks) - i - 1];
185}
186#define debug_printf_clean(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000187#define DEBUG_CLEAN 1
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000188#endif
189
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000190#if DEBUG_EXPAND
191static void debug_print_strings(const char *prefix, char **vv)
192{
193 fprintf(stderr, "%s:\n", prefix);
194 while (*vv)
195 fprintf(stderr, " '%s'\n", *vv++);
196}
197#else
198#define debug_print_strings(prefix, vv) ((void)0)
199#endif
200
Denis Vlasenkof962a032007-11-23 12:50:54 +0000201/*
202 * Leak hunting. Use hush_leaktool.sh for post-processing.
203 */
204#ifdef FOR_HUSH_LEAKTOOL
Denis Vlasenkoccce59d2008-06-16 14:35:57 +0000205/* suppress "warning: no previous prototype..." */
206void *xxmalloc(int lineno, size_t size);
207void *xxrealloc(int lineno, void *ptr, size_t size);
208char *xxstrdup(int lineno, const char *str);
209void xxfree(void *ptr);
Denis Vlasenkof962a032007-11-23 12:50:54 +0000210void *xxmalloc(int lineno, size_t size)
211{
212 void *ptr = xmalloc((size + 0xff) & ~0xff);
213 fprintf(stderr, "line %d: malloc %p\n", lineno, ptr);
214 return ptr;
215}
216void *xxrealloc(int lineno, void *ptr, size_t size)
217{
218 ptr = xrealloc(ptr, (size + 0xff) & ~0xff);
219 fprintf(stderr, "line %d: realloc %p\n", lineno, ptr);
220 return ptr;
221}
222char *xxstrdup(int lineno, const char *str)
223{
224 char *ptr = xstrdup(str);
225 fprintf(stderr, "line %d: strdup %p\n", lineno, ptr);
226 return ptr;
227}
228void xxfree(void *ptr)
229{
230 fprintf(stderr, "free %p\n", ptr);
231 free(ptr);
232}
233#define xmalloc(s) xxmalloc(__LINE__, s)
234#define xrealloc(p, s) xxrealloc(__LINE__, p, s)
235#define xstrdup(s) xxstrdup(__LINE__, s)
236#define free(p) xxfree(p)
237#endif
238
239
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000240/* Do we support ANY keywords? */
Denis Vlasenkod91afa32008-07-29 11:10:01 +0000241#if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000242#define HAS_KEYWORDS 1
243#define IF_HAS_KEYWORDS(...) __VA_ARGS__
244#define IF_HAS_NO_KEYWORDS(...)
245#else
246#define HAS_KEYWORDS 0
247#define IF_HAS_KEYWORDS(...)
248#define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__
249#endif
250
251
Denis Vlasenko5703c222008-06-15 11:49:42 +0000252#define SPECIAL_VAR_SYMBOL 3
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000253#define PARSEFLAG_EXIT_FROM_LOOP 1
Eric Andersen25f27032001-04-26 23:22:31 +0000254
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000255typedef enum redir_type {
Eric Andersen25f27032001-04-26 23:22:31 +0000256 REDIRECT_INPUT = 1,
257 REDIRECT_OVERWRITE = 2,
258 REDIRECT_APPEND = 3,
259 REDIRECT_HEREIS = 4,
260 REDIRECT_IO = 5
261} redir_type;
262
Denis Vlasenko55789c62008-06-18 16:30:42 +0000263/* The descrip member of this structure is only used to make
264 * debugging output pretty */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000265static const struct {
266 int mode;
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000267 signed char default_fd;
268 char descrip[3];
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000269} redir_table[] = {
Eric Andersen25f27032001-04-26 23:22:31 +0000270 { 0, 0, "()" },
271 { O_RDONLY, 0, "<" },
272 { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
273 { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" },
274 { O_RDONLY, -1, "<<" },
275 { O_RDWR, 1, "<>" }
276};
277
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000278typedef enum pipe_style {
Eric Andersen25f27032001-04-26 23:22:31 +0000279 PIPE_SEQ = 1,
280 PIPE_AND = 2,
281 PIPE_OR = 3,
282 PIPE_BG = 4,
283} pipe_style;
284
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000285typedef enum reserved_style {
Eric Andersen25f27032001-04-26 23:22:31 +0000286 RES_NONE = 0,
Denis Vlasenko06810332007-05-21 23:30:54 +0000287#if ENABLE_HUSH_IF
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000288 RES_IF ,
289 RES_THEN ,
290 RES_ELIF ,
291 RES_ELSE ,
292 RES_FI ,
Denis Vlasenko06810332007-05-21 23:30:54 +0000293#endif
294#if ENABLE_HUSH_LOOPS
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000295 RES_FOR ,
296 RES_WHILE ,
297 RES_UNTIL ,
298 RES_DO ,
299 RES_DONE ,
Denis Vlasenkod91afa32008-07-29 11:10:01 +0000300#endif
301#if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000302 RES_IN ,
Denis Vlasenko06810332007-05-21 23:30:54 +0000303#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000304#if ENABLE_HUSH_CASE
305 RES_CASE ,
306 /* two pseudo-keywords support contrived "case" syntax: */
307 RES_MATCH , /* "word)" */
308 RES_CASEI , /* "this command is inside CASE" */
309 RES_ESAC ,
310#endif
311 RES_XXXX ,
312 RES_SNTX
Eric Andersen25f27032001-04-26 23:22:31 +0000313} reserved_style;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000314
Eric Andersen25f27032001-04-26 23:22:31 +0000315struct redir_struct {
Denis Vlasenko55789c62008-06-18 16:30:42 +0000316 struct redir_struct *next;
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000317 char *rd_filename; /* filename */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000318 int fd; /* file descriptor being redirected */
319 int dup; /* -1, or file descriptor being duplicated */
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000320 smallint /*enum redir_type*/ rd_type;
Eric Andersen25f27032001-04-26 23:22:31 +0000321};
322
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000323struct command {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000324 pid_t pid; /* 0 if exited */
Denis Vlasenko2b576b82008-08-04 00:46:07 +0000325 int assignment_cnt; /* how many argv[i] are assignments? */
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000326 smallint is_stopped; /* is the command currently running? */
Denis Vlasenko371de4a2008-10-14 12:43:13 +0000327 smallint grp_type;
Denis Vlasenko2b576b82008-08-04 00:46:07 +0000328 struct pipe *group; /* if non-NULL, this "prog" is {} group,
329 * subshell, or a compound statement */
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000330 char **argv; /* command name and arguments */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000331 struct redir_struct *redirects; /* I/O redirections */
Eric Andersen25f27032001-04-26 23:22:31 +0000332};
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000333/* argv vector may contain variable references (^Cvar^C, ^C0^C etc)
334 * and on execution these are substituted with their values.
335 * Substitution can make _several_ words out of one argv[n]!
336 * Example: argv[0]=='.^C*^C.' here: echo .$*.
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000337 * References of the form ^C`cmd arg^C are `cmd arg` substitutions.
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000338 */
Denis Vlasenko371de4a2008-10-14 12:43:13 +0000339#define GRP_NORMAL 0
340#define GRP_SUBSHELL 1
341#if ENABLE_HUSH_FUNCTIONS
342#define GRP_FUNCTION 2
343#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000344
345struct pipe {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000346 struct pipe *next;
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000347 int num_cmds; /* total number of commands in job */
348 int alive_cmds; /* number of commands running (not exited) */
349 int stopped_cmds; /* number of commands alive, but stopped */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000350#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000351 int jobid; /* job number */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000352 pid_t pgrp; /* process group ID for the job */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000353 char *cmdtext; /* name of job */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000354#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000355 struct command *cmds; /* array of commands in pipe */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000356 smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000357 IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */
358 IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */
Eric Andersen25f27032001-04-26 23:22:31 +0000359};
360
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000361/* This holds pointers to the various results of parsing */
362struct parse_context {
363 struct command *command;
364 struct pipe *list_head;
365 struct pipe *pipe;
366 struct redir_struct *pending_redirect;
367#if HAS_KEYWORDS
368 smallint ctx_res_w;
369 smallint ctx_inverted; /* "! cmd | cmd" */
370#if ENABLE_HUSH_CASE
371 smallint ctx_dsemicolon; /* ";;" seen */
372#endif
373 int old_flag; /* bitmask of FLAG_xxx, for figuring out valid reserved words */
374 struct parse_context *stack;
375#endif
376};
377
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000378/* On program start, environ points to initial environment.
379 * putenv adds new pointers into it, unsetenv removes them.
380 * Neither of these (de)allocates the strings.
381 * setenv allocates new strings in malloc space and does putenv,
382 * and thus setenv is unusable (leaky) for shell's purposes */
383#define setenv(...) setenv_is_leaky_dont_use()
384struct variable {
385 struct variable *next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000386 char *varstr; /* points to "name=" portion */
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000387 int max_len; /* if > 0, name is part of initial env; else name is malloced */
388 smallint flg_export; /* putenv should be done on this var */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000389 smallint flg_read_only;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000390};
391
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000392typedef struct o_string {
Eric Andersen25f27032001-04-26 23:22:31 +0000393 char *data;
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000394 int length; /* position where data is appended */
Eric Andersen25f27032001-04-26 23:22:31 +0000395 int maxlen;
Denis Vlasenko324a3fd2008-06-18 17:49:58 +0000396 /* Misnomer! it's not "quoting", it's "protection against globbing"!
397 * (by prepending \ to *, ?, [ and to \ too) */
Denis Vlasenkoff097622007-10-01 10:00:45 +0000398 smallint o_quote;
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000399 smallint o_glob;
Denis Vlasenkoff097622007-10-01 10:00:45 +0000400 smallint nonnull;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000401 smallint has_empty_slot;
Denis Vlasenko55789c62008-06-18 16:30:42 +0000402 smallint o_assignment; /* 0:maybe, 1:yes, 2:no */
Eric Andersen25f27032001-04-26 23:22:31 +0000403} o_string;
Denis Vlasenko55789c62008-06-18 16:30:42 +0000404enum {
405 MAYBE_ASSIGNMENT = 0,
406 DEFINITELY_ASSIGNMENT = 1,
407 NOT_ASSIGNMENT = 2,
Denis Vlasenko2b576b82008-08-04 00:46:07 +0000408 WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */
Denis Vlasenko55789c62008-06-18 16:30:42 +0000409};
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000410/* Used for initialization: o_string foo = NULL_O_STRING; */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000411#define NULL_O_STRING { NULL }
Eric Andersen25f27032001-04-26 23:22:31 +0000412
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000413/* I can almost use ordinary FILE*. Is open_memstream() universally
Eric Andersen25f27032001-04-26 23:22:31 +0000414 * available? Where is it documented? */
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000415typedef struct in_str {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +0000416 const char *p;
417 /* eof_flag=1: last char in ->p is really an EOF */
418 char eof_flag; /* meaningless if ->p == NULL */
419 char peek_buf[2];
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000420#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000421 smallint promptme;
422 smallint promptmode; /* 0: PS1, 1: PS2 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000423#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000424 FILE *file;
425 int (*get) (struct in_str *);
426 int (*peek) (struct in_str *);
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000427} in_str;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000428#define i_getch(input) ((input)->get(input))
429#define i_peek(input) ((input)->peek(input))
Eric Andersen25f27032001-04-26 23:22:31 +0000430
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000431enum {
432 CHAR_ORDINARY = 0,
433 CHAR_ORDINARY_IF_QUOTED = 1, /* example: *, # */
434 CHAR_IFS = 2, /* treated as ordinary if quoted */
435 CHAR_SPECIAL = 3, /* example: $ */
Eric Andersen25f27032001-04-26 23:22:31 +0000436};
437
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000438enum {
439 BC_BREAK = 1,
440 BC_CONTINUE = 2,
441};
442
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000443
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000444/* "Globals" within this file */
445
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000446/* Sorted roughly by size (smaller offsets == smaller code) */
447struct globals {
448#if ENABLE_HUSH_INTERACTIVE
449 /* 'interactive_fd' is a fd# open to ctty, if we have one
450 * _AND_ if we decided to act interactively */
451 int interactive_fd;
452 const char *PS1;
453 const char *PS2;
454#endif
455#if ENABLE_FEATURE_EDITING
456 line_input_t *line_input_state;
457#endif
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +0000458 pid_t root_pid;
Denis Vlasenko87a86552008-07-29 19:43:10 +0000459 pid_t last_bg_pid;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000460#if ENABLE_HUSH_JOB
461 int run_list_level;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000462 pid_t saved_tty_pgrp;
463 int last_jobid;
464 struct pipe *job_list;
465 struct pipe *toplevel_list;
466 smallint ctrl_z_flag;
467#endif
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000468#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000469 smallint flag_break_continue;
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000470#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000471 smallint fake_mode;
472 /* these three support $?, $#, and $1 */
Denis Vlasenko5e052ca2008-07-28 15:15:09 +0000473 smalluint last_return_code;
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +0000474 smalluint global_args_malloced;
475 int global_argc; /* NB: $# + 1 */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000476 char **global_argv;
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000477#if ENABLE_HUSH_LOOPS
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +0000478 unsigned depth_break_continue;
Denis Vlasenkofcf37c32008-07-29 11:37:15 +0000479 unsigned depth_of_loop;
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000480#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000481 const char *ifs;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000482 const char *cwd;
Denis Vlasenko87a86552008-07-29 19:43:10 +0000483 struct variable *top_var; /* = &G.shell_ver (set in main()) */
Denis Vlasenko0a83fc32007-05-25 11:12:32 +0000484 struct variable shell_ver;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000485#if ENABLE_FEATURE_SH_STANDALONE
486 struct nofork_save_area nofork_save;
487#endif
488#if ENABLE_HUSH_JOB
489 sigjmp_buf toplevel_jb;
490#endif
491 unsigned char charmap[256];
492 char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2];
493};
494
495#define G (*ptr_to_globals)
Denis Vlasenko87a86552008-07-29 19:43:10 +0000496/* Not #defining name to G.name - this quickly gets unwieldy
497 * (too many defines). Also, I actually prefer to see when a variable
498 * is global, thus "G." prefix is a useful hint */
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000499#define INIT_G() do { \
500 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
501} while (0)
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000502
503
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000504#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
505
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000506#if 1
507/* Normal */
508static void syntax(const char *msg)
509{
Denis Vlasenko87a86552008-07-29 19:43:10 +0000510#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000511 /* Was using fancy stuff:
Denis Vlasenko87a86552008-07-29 19:43:10 +0000512 * (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...)
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000513 * but it SEGVs. ?! Oh well... explicit temp ptr works around that */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000514 void FAST_FUNC (*fp)(const char *s, ...);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000515 fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000516 fp(msg ? "%s: %s" : "syntax error", "syntax error", msg);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000517#else
518 bb_error_msg_and_die(msg ? "%s: %s" : "syntax error", "syntax error", msg);
519#endif
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000520}
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000521
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000522#else
523/* Debug */
524static void syntax_lineno(int line)
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000525{
Denis Vlasenko87a86552008-07-29 19:43:10 +0000526#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkoff182a32008-07-05 20:29:59 +0000527 void FAST_FUNC (*fp)(const char *s, ...);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000528 fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000529 fp("syntax error hush.c:%d", line);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000530#else
531 bb_error_msg_and_die("syntax error hush.c:%d", line);
532#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000533}
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000534#define syntax(str) syntax_lineno(__LINE__)
535#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000536
537/* Index of subroutines: */
Eric Andersen25f27032001-04-26 23:22:31 +0000538/* in_str manipulations: */
539static int static_get(struct in_str *i);
540static int static_peek(struct in_str *i);
541static int file_get(struct in_str *i);
542static int file_peek(struct in_str *i);
543static void setup_file_in_str(struct in_str *i, FILE *f);
544static void setup_string_in_str(struct in_str *i, const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000545/* "run" the final data structures: */
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000546#if !defined(DEBUG_CLEAN)
547#define free_pipe_list(head, indent) free_pipe_list(head)
548#define free_pipe(pi, indent) free_pipe(pi)
549#endif
Eric Andersenbf7df042001-05-23 22:18:35 +0000550static int free_pipe_list(struct pipe *head, int indent);
551static int free_pipe(struct pipe *pi, int indent);
Eric Andersen25f27032001-04-26 23:22:31 +0000552/* really run the final data structures: */
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000553typedef struct nommu_save_t {
554 char **new_env;
555 char **old_env;
556 char **argv;
557} nommu_save_t;
Denis Vlasenko76d50412008-06-10 16:19:39 +0000558#if BB_MMU
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000559#define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \
Denis Vlasenko2b576b82008-08-04 00:46:07 +0000560 pseudo_exec_argv(argv, assignment_cnt, argv_expanded)
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000561#define pseudo_exec(nommu_save, command, argv_expanded) \
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000562 pseudo_exec(command, argv_expanded)
Denis Vlasenko76d50412008-06-10 16:19:39 +0000563#endif
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000564static void pseudo_exec_argv(nommu_save_t *nommu_save, char **argv, int assignment_cnt, char **argv_expanded) NORETURN;
565static void pseudo_exec(nommu_save_t *nommu_save, struct command *command, char **argv_expanded) NORETURN;
566static int setup_redirects(struct command *prog, int squirrel[]);
567static int run_list(struct pipe *pi);
Denis Vlasenko05743d72008-02-10 12:10:08 +0000568static int run_pipe(struct pipe *pi);
Eric Andersen25f27032001-04-26 23:22:31 +0000569/* data structure manipulation: */
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000570static int setup_redirect(struct parse_context *ctx, int fd, redir_type style, struct in_str *input);
571static void initialize_context(struct parse_context *ctx);
572static int done_word(o_string *dest, struct parse_context *ctx);
573static int done_command(struct parse_context *ctx);
574static void done_pipe(struct parse_context *ctx, pipe_style type);
Eric Andersen25f27032001-04-26 23:22:31 +0000575/* primary string parsing: */
576static int redirect_dup_num(struct in_str *input);
577static int redirect_opt_num(o_string *o);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000578#if ENABLE_HUSH_TICK
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000579static int process_command_subs(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000580 struct in_str *input, const char *subst_end);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000581#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000582static int parse_group(o_string *dest, struct parse_context *ctx, struct in_str *input, int ch);
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000583static const char *lookup_param(const char *src);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000584static int handle_dollar(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000585 struct in_str *input);
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000586static int parse_stream(o_string *dest, struct parse_context *ctx, struct in_str *input0, const char *end_trigger);
Eric Andersen25f27032001-04-26 23:22:31 +0000587/* setup: */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000588static int parse_and_run_stream(struct in_str *inp, int parse_flag);
589static int parse_and_run_string(const char *s, int parse_flag);
590static int parse_and_run_file(FILE *f);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000591/* job management: */
Eric Andersenc798b072001-06-22 06:23:03 +0000592static int checkjobs(struct pipe* fg_pipe);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000593#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +0000594static int checkjobs_and_fg_shell(struct pipe* fg_pipe);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000595static void insert_bg_job(struct pipe *pi);
596static void remove_bg_job(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000597static void delete_finished_bg_job(struct pipe *pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000598#else
599int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */
600#endif
Eric Andersenf72f5622001-05-15 23:21:41 +0000601/* local variable support */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000602static char **expand_strvec_to_strvec(char **argv);
603/* used for eval */
604static char *expand_strvec_to_string(char **argv);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +0000605/* used for expansion of right hand of assignments */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000606static char *expand_string_to_string(const char *str);
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000607static struct variable *get_local_var(const char *name);
608static int set_local_var(char *str, int flg_export);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000609static void unset_local_var(const char *name);
Eric Andersen25f27032001-04-26 23:22:31 +0000610
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000611
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000612static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="HUSH_VER_STR;
613
614
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000615static int glob_needed(const char *s)
616{
617 while (*s) {
618 if (*s == '\\')
619 s++;
620 if (*s == '*' || *s == '[' || *s == '?')
621 return 1;
622 s++;
623 }
624 return 0;
625}
626
Denis Vlasenko30c9cc52008-06-17 07:24:29 +0000627static int is_assignment(const char *s)
628{
Denis Vlasenkod4981312008-07-31 10:34:48 +0000629 if (!s || !(isalpha(*s) || *s == '_'))
Denis Vlasenko30c9cc52008-06-17 07:24:29 +0000630 return 0;
631 s++;
632 while (isalnum(*s) || *s == '_')
633 s++;
634 return *s == '=';
635}
636
Denis Vlasenko55789c62008-06-18 16:30:42 +0000637/* Replace each \x with x in place, return ptr past NUL. */
638static char *unbackslash(char *src)
639{
640 char *dst = src;
641 while (1) {
642 if (*src == '\\')
643 src++;
644 if ((*dst++ = *src++) == '\0')
645 break;
646 }
647 return dst;
648}
649
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +0000650static char **add_strings_to_strings(char **strings, char **add, int need_to_dup)
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000651{
652 int i;
653 unsigned count1;
654 unsigned count2;
655 char **v;
656
657 v = strings;
658 count1 = 0;
659 if (v) {
660 while (*v) {
661 count1++;
662 v++;
663 }
664 }
665 count2 = 0;
666 v = add;
667 while (*v) {
668 count2++;
669 v++;
670 }
671 v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*));
672 v[count1 + count2] = NULL;
673 i = count2;
674 while (--i >= 0)
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +0000675 v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000676 return v;
677}
678
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000679static char **add_string_to_strings(char **strings, char *add)
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000680{
681 char *v[2];
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000682 v[0] = add;
683 v[1] = NULL;
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +0000684 return add_strings_to_strings(strings, v, /*dup:*/ 0);
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000685}
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000686
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000687static void putenv_all(char **strings)
688{
689 if (!strings)
690 return;
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000691 while (*strings) {
692 debug_printf_env("putenv '%s'\n", *strings);
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000693 putenv(*strings++);
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000694 }
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000695}
696
697static char **putenv_all_and_save_old(char **strings)
698{
699 char **old = NULL;
700 char **s = strings;
701
702 if (!strings)
703 return old;
704 while (*strings) {
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000705 char *v, *eq;
706
707 eq = strchr(*strings, '=');
708 if (eq) {
709 *eq = '\0';
710 v = getenv(*strings);
711 *eq = '=';
712 if (v) {
713 /* v points to VAL in VAR=VAL, go back to VAR */
714 v -= (eq - *strings) + 1;
715 old = add_string_to_strings(old, v);
716 }
717 }
718 strings++;
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000719 }
720 putenv_all(s);
721 return old;
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000722}
723
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000724static void free_strings_and_unsetenv(char **strings, int unset)
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000725{
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000726 char **v;
727
728 if (!strings)
729 return;
730
731 v = strings;
732 while (*v) {
733 if (unset) {
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000734 debug_printf_env("unsetenv '%s'\n", *v);
735 bb_unsetenv(*v);
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000736 }
737 free(*v++);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000738 }
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000739 free(strings);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000740}
741
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000742static void free_strings(char **strings)
Denis Vlasenko76d50412008-06-10 16:19:39 +0000743{
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000744 free_strings_and_unsetenv(strings, 0);
Denis Vlasenko76d50412008-06-10 16:19:39 +0000745}
Denis Vlasenko76d50412008-06-10 16:19:39 +0000746
747
Denis Vlasenko83506862007-11-23 13:11:42 +0000748/* Function prototypes for builtins */
749static int builtin_cd(char **argv);
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000750static int builtin_echo(char **argv);
Denis Vlasenko83506862007-11-23 13:11:42 +0000751static int builtin_eval(char **argv);
752static int builtin_exec(char **argv);
753static int builtin_exit(char **argv);
754static int builtin_export(char **argv);
755#if ENABLE_HUSH_JOB
756static int builtin_fg_bg(char **argv);
757static int builtin_jobs(char **argv);
758#endif
759#if ENABLE_HUSH_HELP
760static int builtin_help(char **argv);
761#endif
762static int builtin_pwd(char **argv);
763static int builtin_read(char **argv);
764static int builtin_test(char **argv);
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000765static int builtin_true(char **argv);
Denis Vlasenko83506862007-11-23 13:11:42 +0000766static int builtin_set(char **argv);
767static int builtin_shift(char **argv);
768static int builtin_source(char **argv);
769static int builtin_umask(char **argv);
770static int builtin_unset(char **argv);
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000771#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000772static int builtin_break(char **argv);
773static int builtin_continue(char **argv);
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000774#endif
Denis Vlasenko83506862007-11-23 13:11:42 +0000775//static int builtin_not_written(char **argv);
776
Eric Andersen25f27032001-04-26 23:22:31 +0000777/* Table of built-in functions. They can be forked or not, depending on
778 * context: within pipes, they fork. As simple commands, they do not.
779 * When used in non-forking context, they can change global variables
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000780 * in the parent shell process. If forked, of course they cannot.
Eric Andersen25f27032001-04-26 23:22:31 +0000781 * For example, 'unset foo | whatever' will parse and run, but foo will
782 * still be set at the end. */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000783struct built_in_command {
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000784 const char *cmd;
785 int (*function)(char **argv);
Denis Vlasenko06810332007-05-21 23:30:54 +0000786#if ENABLE_HUSH_HELP
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000787 const char *descr;
Denis Vlasenko06810332007-05-21 23:30:54 +0000788#define BLTIN(cmd, func, help) { cmd, func, help }
789#else
790#define BLTIN(cmd, func, help) { cmd, func }
791#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000792};
793
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000794/* For now, echo and test are unconditionally enabled.
795 * Maybe make it configurable? */
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +0000796static const struct built_in_command bltins[] = {
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000797 BLTIN("." , builtin_source, "Run commands in a file"),
798 BLTIN(":" , builtin_true, "No-op"),
Denis Vlasenko83506862007-11-23 13:11:42 +0000799 BLTIN("[" , builtin_test, "Test condition"),
800 BLTIN("[[" , builtin_test, "Test condition"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000801#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000802 BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000803#endif
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000804#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000805 BLTIN("break" , builtin_break, "Exit from a loop"),
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000806#endif
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000807 BLTIN("cd" , builtin_cd, "Change directory"),
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000808#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000809 BLTIN("continue", builtin_continue, "Start new loop iteration"),
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000810#endif
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000811 BLTIN("echo" , builtin_echo, "Write to stdout"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000812 BLTIN("eval" , builtin_eval, "Construct and run shell command"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000813 BLTIN("exec" , builtin_exec, "Execute command, don't return to shell"),
814 BLTIN("exit" , builtin_exit, "Exit"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000815 BLTIN("export", builtin_export, "Set environment variable"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000816#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000817 BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000818 BLTIN("jobs" , builtin_jobs, "List active jobs"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000819#endif
Denis Vlasenko06810332007-05-21 23:30:54 +0000820 BLTIN("pwd" , builtin_pwd, "Print current directory"),
821 BLTIN("read" , builtin_read, "Input environment variable"),
822// BLTIN("return", builtin_not_written, "Return from a function"),
823 BLTIN("set" , builtin_set, "Set/unset shell local variables"),
824 BLTIN("shift" , builtin_shift, "Shift positional parameters"),
825// BLTIN("trap" , builtin_not_written, "Trap signals"),
Denis Vlasenko83506862007-11-23 13:11:42 +0000826 BLTIN("test" , builtin_test, "Test condition"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000827// BLTIN("ulimit", builtin_not_written, "Control resource limits"),
828 BLTIN("umask" , builtin_umask, "Set file creation mask"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000829 BLTIN("unset" , builtin_unset, "Unset environment variable"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000830#if ENABLE_HUSH_HELP
831 BLTIN("help" , builtin_help, "List shell built-in commands"),
832#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000833};
834
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000835
Denis Vlasenko4830fc52008-05-25 21:50:55 +0000836/* Signals are grouped, we handle them in batches */
837static void set_misc_sighandler(void (*handler)(int))
838{
839 bb_signals(0
840 + (1 << SIGINT)
841 + (1 << SIGQUIT)
842 + (1 << SIGTERM)
843 , handler);
844}
845
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000846#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000847
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000848static void set_fatal_sighandler(void (*handler)(int))
849{
Denis Vlasenko25591c32008-02-16 22:58:56 +0000850 bb_signals(0
851 + (1 << SIGILL)
852 + (1 << SIGTRAP)
853 + (1 << SIGABRT)
854 + (1 << SIGFPE)
855 + (1 << SIGBUS)
856 + (1 << SIGSEGV)
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000857 /* bash 3.2 seems to handle these just like 'fatal' ones */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000858 + (1 << SIGHUP)
859 + (1 << SIGPIPE)
860 + (1 << SIGALRM)
861 , handler);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000862}
863static void set_jobctrl_sighandler(void (*handler)(int))
864{
Denis Vlasenko25591c32008-02-16 22:58:56 +0000865 bb_signals(0
866 + (1 << SIGTSTP)
867 + (1 << SIGTTIN)
868 + (1 << SIGTTOU)
869 , handler);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000870}
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000871/* SIGCHLD is special and handled separately */
872
873static void set_every_sighandler(void (*handler)(int))
874{
875 set_fatal_sighandler(handler);
876 set_jobctrl_sighandler(handler);
877 set_misc_sighandler(handler);
878 signal(SIGCHLD, handler);
879}
880
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000881static void handler_ctrl_c(int sig UNUSED_PARAM)
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000882{
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000883 debug_printf_jobs("got sig %d\n", sig);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000884// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenko87a86552008-07-29 19:43:10 +0000885 siglongjmp(G.toplevel_jb, 1);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000886}
887
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000888static void handler_ctrl_z(int sig UNUSED_PARAM)
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000889{
890 pid_t pid;
891
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000892 debug_printf_jobs("got tty sig %d in pid %d\n", sig, getpid());
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000893 pid = fork();
Denis Vlasenkoc2990322007-05-16 12:57:12 +0000894 if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000895 return;
Denis Vlasenko87a86552008-07-29 19:43:10 +0000896 G.ctrl_z_flag = 1;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000897 if (!pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +0000898 if (ENABLE_HUSH_JOB)
899 die_sleep = 0; /* let nofork's xfuncs die */
Bernhard Reutner-Fischer864329d2008-09-25 10:55:05 +0000900 bb_setpgrp();
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000901 debug_printf_jobs("set pgrp for child %d ok\n", getpid());
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000902 set_every_sighandler(SIG_DFL);
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000903 raise(SIGTSTP); /* resend TSTP so that child will be stopped */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000904 debug_printf_jobs("returning in child\n");
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000905 /* return to nofork, it will eventually exit now,
906 * not return back to shell */
907 return;
908 }
909 /* parent */
910 /* finish filling up pipe info */
Denis Vlasenko87a86552008-07-29 19:43:10 +0000911 G.toplevel_list->pgrp = pid; /* child is in its own pgrp */
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000912 G.toplevel_list->cmds[0].pid = pid;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000913 /* parent needs to longjmp out of running nofork.
914 * we will "return" exitcode 0, with child put in background */
915// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000916 debug_printf_jobs("siglongjmp in parent\n");
Denis Vlasenko87a86552008-07-29 19:43:10 +0000917 siglongjmp(G.toplevel_jb, 1);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000918}
919
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000920/* Restores tty foreground process group, and exits.
921 * May be called as signal handler for fatal signal
922 * (will faithfully resend signal to itself, producing correct exit state)
923 * or called directly with -EXITCODE.
924 * We also call it if xfunc is exiting. */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000925static void sigexit(int sig) NORETURN;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000926static void sigexit(int sig)
927{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000928 /* Disable all signals: job control, SIGPIPE, etc. */
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000929 sigprocmask_allsigs(SIG_BLOCK);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000930
Denis Vlasenko87a86552008-07-29 19:43:10 +0000931#if ENABLE_HUSH_INTERACTIVE
932 if (G.interactive_fd)
933 tcsetpgrp(G.interactive_fd, G.saved_tty_pgrp);
934#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000935
936 /* Not a signal, just exit */
937 if (sig <= 0)
938 _exit(- sig);
939
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000940 kill_myself_with_sig(sig); /* does not return */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000941}
942
943/* Restores tty foreground process group, and exits. */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000944static void hush_exit(int exitcode) NORETURN;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000945static void hush_exit(int exitcode)
946{
947 fflush(NULL); /* flush all streams */
948 sigexit(- (exitcode & 0xff));
949}
950
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000951#else /* !JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000952
953#define set_fatal_sighandler(handler) ((void)0)
954#define set_jobctrl_sighandler(handler) ((void)0)
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000955#define hush_exit(e) exit(e)
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000956
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000957#endif /* JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000958
959
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000960static const char *set_cwd(void)
961{
Denis Vlasenko87a86552008-07-29 19:43:10 +0000962 if (G.cwd == bb_msg_unknown)
963 G.cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */
964 G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd);
965 if (!G.cwd)
966 G.cwd = bb_msg_unknown;
967 return G.cwd;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000968}
969
Denis Vlasenko83506862007-11-23 13:11:42 +0000970
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000971/*
972 * o_string support
973 */
974#define B_CHUNK (32 * sizeof(char*))
Eric Andersen25f27032001-04-26 23:22:31 +0000975
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000976static void o_reset(o_string *o)
Eric Andersen25f27032001-04-26 23:22:31 +0000977{
978 o->length = 0;
979 o->nonnull = 0;
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000980 if (o->data)
981 o->data[0] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +0000982}
983
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000984static void o_free(o_string *o)
Eric Andersen25f27032001-04-26 23:22:31 +0000985{
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000986 free(o->data);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000987 memset(o, 0, sizeof(*o));
Eric Andersen25f27032001-04-26 23:22:31 +0000988}
989
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000990static void o_grow_by(o_string *o, int len)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000991{
992 if (o->length + len > o->maxlen) {
993 o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK);
994 o->data = xrealloc(o->data, 1 + o->maxlen);
995 }
996}
997
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000998static void o_addchr(o_string *o, int ch)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000999{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001000 debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o);
1001 o_grow_by(o, 1);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001002 o->data[o->length] = ch;
1003 o->length++;
1004 o->data[o->length] = '\0';
1005}
1006
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001007static void o_addstr(o_string *o, const char *str, int len)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001008{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001009 o_grow_by(o, len);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001010 memcpy(&o->data[o->length], str, len);
1011 o->length += len;
1012 o->data[o->length] = '\0';
1013}
1014
Denis Vlasenko55789c62008-06-18 16:30:42 +00001015static void o_addstr_duplicate_backslash(o_string *o, const char *str, int len)
1016{
1017 while (len) {
1018 o_addchr(o, *str);
1019 if (*str++ == '\\'
1020 && (*str != '*' && *str != '?' && *str != '[')
1021 ) {
1022 o_addchr(o, '\\');
1023 }
1024 len--;
1025 }
1026}
1027
Eric Andersen25f27032001-04-26 23:22:31 +00001028/* My analysis of quoting semantics tells me that state information
1029 * is associated with a destination, not a source.
1030 */
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001031static void o_addqchr(o_string *o, int ch)
Eric Andersen25f27032001-04-26 23:22:31 +00001032{
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001033 int sz = 1;
Denis Vlasenko371de4a2008-10-14 12:43:13 +00001034 char *found = strchr("*?[\\", ch);
1035 if (found)
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001036 sz++;
Denis Vlasenko371de4a2008-10-14 12:43:13 +00001037 o_grow_by(o, sz);
1038 if (found) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001039 o->data[o->length] = '\\';
1040 o->length++;
Eric Andersen25f27032001-04-26 23:22:31 +00001041 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001042 o->data[o->length] = ch;
1043 o->length++;
1044 o->data[o->length] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00001045}
1046
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001047static void o_addQchr(o_string *o, int ch)
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001048{
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001049 int sz = 1;
1050 if (o->o_quote && strchr("*?[\\", ch)) {
1051 sz++;
1052 o->data[o->length] = '\\';
1053 o->length++;
1054 }
1055 o_grow_by(o, sz);
1056 o->data[o->length] = ch;
1057 o->length++;
1058 o->data[o->length] = '\0';
1059}
1060
1061static void o_addQstr(o_string *o, const char *str, int len)
1062{
1063 if (!o->o_quote) {
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001064 o_addstr(o, str, len);
1065 return;
1066 }
1067 while (len) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001068 char ch;
1069 int sz;
1070 int ordinary_cnt = strcspn(str, "*?[\\");
1071 if (ordinary_cnt > len) /* paranoia */
1072 ordinary_cnt = len;
1073 o_addstr(o, str, ordinary_cnt);
1074 if (ordinary_cnt == len)
1075 return;
1076 str += ordinary_cnt;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001077 len -= ordinary_cnt + 1; /* we are processing + 1 char below */
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001078
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001079 ch = *str++;
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001080 sz = 1;
1081 if (ch) { /* it is necessarily one of "*?[\\" */
1082 sz++;
1083 o->data[o->length] = '\\';
1084 o->length++;
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001085 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001086 o_grow_by(o, sz);
1087 o->data[o->length] = ch;
1088 o->length++;
1089 o->data[o->length] = '\0';
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001090 }
1091}
1092
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001093/* A special kind of o_string for $VAR and `cmd` expansion.
1094 * It contains char* list[] at the beginning, which is grown in 16 element
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001095 * increments. Actual string data starts at the next multiple of 16 * (char*).
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001096 * list[i] contains an INDEX (int!) into this string data.
1097 * It means that if list[] needs to grow, data needs to be moved higher up
1098 * but list[i]'s need not be modified.
1099 * NB: remembering how many list[i]'s you have there is crucial.
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001100 * o_finalize_list() operation post-processes this structure - calculates
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001101 * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well.
1102 */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001103#if DEBUG_EXPAND || DEBUG_GLOB
1104static void debug_print_list(const char *prefix, o_string *o, int n)
1105{
1106 char **list = (char**)o->data;
1107 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1108 int i = 0;
1109 fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n",
1110 prefix, list, n, string_start, o->length, o->maxlen);
1111 while (i < n) {
1112 fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i],
1113 o->data + (int)list[i] + string_start,
1114 o->data + (int)list[i] + string_start);
1115 i++;
1116 }
1117 if (n) {
1118 const char *p = o->data + (int)list[n - 1] + string_start;
1119 fprintf(stderr, " total_sz:%d\n", (p + strlen(p) + 1) - o->data);
1120 }
1121}
1122#else
1123#define debug_print_list(prefix, o, n) ((void)0)
1124#endif
1125
1126/* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value
1127 * in list[n] so that it points past last stored byte so far.
1128 * It returns n+1. */
1129static int o_save_ptr_helper(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001130{
1131 char **list = (char**)o->data;
Denis Vlasenko895bea22008-06-10 18:06:24 +00001132 int string_start;
1133 int string_len;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001134
1135 if (!o->has_empty_slot) {
Denis Vlasenko895bea22008-06-10 18:06:24 +00001136 string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1137 string_len = o->length - string_start;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001138 if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001139 debug_printf_list("list[%d]=%d string_start=%d (growing)\n", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001140 /* list[n] points to string_start, make space for 16 more pointers */
1141 o->maxlen += 0x10 * sizeof(list[0]);
1142 o->data = xrealloc(o->data, o->maxlen + 1);
Denis Vlasenko7049ff82008-06-25 09:53:17 +00001143 list = (char**)o->data;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001144 memmove(list + n + 0x10, list + n, string_len);
1145 o->length += 0x10 * sizeof(list[0]);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001146 } else
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001147 debug_printf_list("list[%d]=%d string_start=%d\n", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001148 } else {
1149 /* We have empty slot at list[n], reuse without growth */
Denis Vlasenko895bea22008-06-10 18:06:24 +00001150 string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */
1151 string_len = o->length - string_start;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001152 debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001153 o->has_empty_slot = 0;
1154 }
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +00001155 list[n] = (char*)(ptrdiff_t)string_len;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001156 return n + 1;
1157}
1158
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001159/* "What was our last o_save_ptr'ed position (byte offset relative o->data)?" */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001160static int o_get_last_ptr(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001161{
1162 char **list = (char**)o->data;
1163 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1164
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +00001165 return ((int)(ptrdiff_t)list[n-1]) + string_start;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001166}
1167
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001168/* o_glob performs globbing on last list[], saving each result
Denis Vlasenko55789c62008-06-18 16:30:42 +00001169 * as a new list[]. */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001170static int o_glob(o_string *o, int n)
1171{
1172 glob_t globdata;
1173 int gr;
1174 char *pattern;
1175
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001176 debug_printf_glob("start o_glob: n:%d o->data:%p\n", n, o->data);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001177 if (!o->data)
1178 return o_save_ptr_helper(o, n);
1179 pattern = o->data + o_get_last_ptr(o, n);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001180 debug_printf_glob("glob pattern '%s'\n", pattern);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001181 if (!glob_needed(pattern)) {
1182 literal:
1183 o->length = unbackslash(pattern) - o->data;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001184 debug_printf_glob("glob pattern '%s' is literal\n", pattern);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001185 return o_save_ptr_helper(o, n);
1186 }
1187
1188 memset(&globdata, 0, sizeof(globdata));
1189 gr = glob(pattern, 0, NULL, &globdata);
1190 debug_printf_glob("glob('%s'):%d\n", pattern, gr);
1191 if (gr == GLOB_NOSPACE)
1192 bb_error_msg_and_die("out of memory during glob");
1193 if (gr == GLOB_NOMATCH) {
1194 globfree(&globdata);
1195 goto literal;
1196 }
1197 if (gr != 0) { /* GLOB_ABORTED ? */
1198//TODO: testcase for bad glob pattern behavior
1199 bb_error_msg("glob(3) error %d on '%s'", gr, pattern);
1200 }
1201 if (globdata.gl_pathv && globdata.gl_pathv[0]) {
1202 char **argv = globdata.gl_pathv;
1203 o->length = pattern - o->data; /* "forget" pattern */
1204 while (1) {
1205 o_addstr(o, *argv, strlen(*argv) + 1);
1206 n = o_save_ptr_helper(o, n);
1207 argv++;
1208 if (!*argv)
1209 break;
1210 }
1211 }
1212 globfree(&globdata);
1213 if (DEBUG_GLOB)
1214 debug_print_list("o_glob returning", o, n);
1215 return n;
1216}
1217
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001218/* If o->o_glob == 1, glob the string so far remembered.
1219 * Otherwise, just finish current list[] and start new */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001220static int o_save_ptr(o_string *o, int n)
1221{
1222 if (o->o_glob)
1223 return o_glob(o, n); /* o_save_ptr_helper is inside */
1224 return o_save_ptr_helper(o, n);
1225}
1226
1227/* "Please convert list[n] to real char* ptrs, and NULL terminate it." */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001228static char **o_finalize_list(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001229{
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001230 char **list;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001231 int string_start;
1232
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001233 n = o_save_ptr(o, n); /* force growth for list[n] if necessary */
1234 if (DEBUG_EXPAND)
1235 debug_print_list("finalized", o, n);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001236 debug_printf_expand("finalized n:%d\n", n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001237 list = (char**)o->data;
1238 string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1239 list[--n] = NULL;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001240 while (n) {
1241 n--;
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +00001242 list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001243 }
1244 return list;
1245}
1246
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001247
1248/*
1249 * in_str support
1250 */
Eric Andersen25f27032001-04-26 23:22:31 +00001251static int static_get(struct in_str *i)
1252{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001253 int ch = *i->p++;
1254 if (ch == '\0') return EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001255 return ch;
1256}
1257
1258static int static_peek(struct in_str *i)
1259{
1260 return *i->p;
1261}
1262
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001263#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001264
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001265#if ENABLE_FEATURE_EDITING
Rob Landley88621d72006-08-29 19:41:06 +00001266static void cmdedit_set_initial_prompt(void)
Eric Andersen25f27032001-04-26 23:22:31 +00001267{
Denis Vlasenko38f63192007-01-22 09:03:07 +00001268#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko87a86552008-07-29 19:43:10 +00001269 G.PS1 = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00001270#else
Denis Vlasenko87a86552008-07-29 19:43:10 +00001271 G.PS1 = getenv("PS1");
1272 if (G.PS1 == NULL)
1273 G.PS1 = "\\w \\$ ";
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001274#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001275}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001276#endif /* EDITING */
Eric Andersen25f27032001-04-26 23:22:31 +00001277
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001278static const char* setup_prompt_string(int promptmode)
Eric Andersen25f27032001-04-26 23:22:31 +00001279{
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001280 const char *prompt_str;
1281 debug_printf("setup_prompt_string %d ", promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001282#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001283 /* Set up the prompt */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001284 if (promptmode == 0) { /* PS1 */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001285 free((char*)G.PS1);
1286 G.PS1 = xasprintf("%s %c ", G.cwd, (geteuid() != 0) ? '$' : '#');
1287 prompt_str = G.PS1;
Eric Andersen25f27032001-04-26 23:22:31 +00001288 } else {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001289 prompt_str = G.PS2;
Eric Andersen25f27032001-04-26 23:22:31 +00001290 }
1291#else
Denis Vlasenko87a86552008-07-29 19:43:10 +00001292 prompt_str = (promptmode == 0) ? G.PS1 : G.PS2;
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001293#endif
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001294 debug_printf("result '%s'\n", prompt_str);
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001295 return prompt_str;
Eric Andersen25f27032001-04-26 23:22:31 +00001296}
1297
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001298static void get_user_input(struct in_str *i)
Eric Andersen25f27032001-04-26 23:22:31 +00001299{
Denis Vlasenko0937be52007-04-28 16:47:08 +00001300 int r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001301 const char *prompt_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001302
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001303 prompt_str = setup_prompt_string(i->promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001304#if ENABLE_FEATURE_EDITING
Denis Vlasenko170435c2007-05-23 13:01:10 +00001305 /* Enable command line editing only while a command line
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001306 * is actually being read */
1307 do {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001308 r = read_line_input(prompt_str, G.user_input_buf, BUFSIZ-1, G.line_input_state);
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001309 } while (r == 0); /* repeat if Ctrl-C */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001310 i->eof_flag = (r < 0);
1311 if (i->eof_flag) { /* EOF/error detected */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001312 G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */
1313 G.user_input_buf[1] = '\0';
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001314 }
Eric Andersen25f27032001-04-26 23:22:31 +00001315#else
1316 fputs(prompt_str, stdout);
1317 fflush(stdout);
Denis Vlasenko87a86552008-07-29 19:43:10 +00001318 G.user_input_buf[0] = r = fgetc(i->file);
1319 /*G.user_input_buf[1] = '\0'; - already is and never changed */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001320 i->eof_flag = (r == EOF);
Eric Andersen25f27032001-04-26 23:22:31 +00001321#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00001322 i->p = G.user_input_buf;
Eric Andersen25f27032001-04-26 23:22:31 +00001323}
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001324
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001325#endif /* INTERACTIVE */
Eric Andersen25f27032001-04-26 23:22:31 +00001326
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001327/* This is the magic location that prints prompts
Eric Andersen25f27032001-04-26 23:22:31 +00001328 * and gets data back from the user */
1329static int file_get(struct in_str *i)
1330{
1331 int ch;
1332
Eric Andersen25f27032001-04-26 23:22:31 +00001333 /* If there is data waiting, eat it up */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001334 if (i->p && *i->p) {
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001335#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001336 take_cached:
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001337#endif
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001338 ch = *i->p++;
1339 if (i->eof_flag && !*i->p)
1340 ch = EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001341 } else {
1342 /* need to double check i->file because we might be doing something
1343 * more complicated by now, like sourcing or substituting. */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001344#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko87a86552008-07-29 19:43:10 +00001345 if (G.interactive_fd && i->promptme && i->file == stdin) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001346 do {
1347 get_user_input(i);
1348 } while (!*i->p); /* need non-empty line */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001349 i->promptmode = 1; /* PS2 */
1350 i->promptme = 0;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001351 goto take_cached;
Eric Andersen25f27032001-04-26 23:22:31 +00001352 }
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001353#endif
1354 ch = fgetc(i->file);
Eric Andersen25f27032001-04-26 23:22:31 +00001355 }
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001356 debug_printf("file_get: got a '%c' %d\n", ch, ch);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001357#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001358 if (ch == '\n')
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001359 i->promptme = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001360#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001361 return ch;
1362}
1363
1364/* All the callers guarantee this routine will never be
1365 * used right after a newline, so prompting is not needed.
1366 */
1367static int file_peek(struct in_str *i)
1368{
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001369 int ch;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001370 if (i->p && *i->p) {
1371 if (i->eof_flag && !i->p[1])
1372 return EOF;
1373 return *i->p;
Eric Andersen25f27032001-04-26 23:22:31 +00001374 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001375 ch = fgetc(i->file);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001376 i->eof_flag = (ch == EOF);
1377 i->peek_buf[0] = ch;
1378 i->peek_buf[1] = '\0';
1379 i->p = i->peek_buf;
1380 debug_printf("file_peek: got a '%c' %d\n", *i->p, *i->p);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001381 return ch;
Eric Andersen25f27032001-04-26 23:22:31 +00001382}
1383
1384static void setup_file_in_str(struct in_str *i, FILE *f)
1385{
1386 i->peek = file_peek;
1387 i->get = file_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001388#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001389 i->promptme = 1;
1390 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001391#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001392 i->file = f;
1393 i->p = NULL;
1394}
1395
1396static void setup_string_in_str(struct in_str *i, const char *s)
1397{
1398 i->peek = static_peek;
1399 i->get = static_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001400#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001401 i->promptme = 1;
1402 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001403#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001404 i->p = s;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001405 i->eof_flag = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001406}
1407
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001408
Eric Andersen25f27032001-04-26 23:22:31 +00001409/* squirrel != NULL means we squirrel away copies of stdin, stdout,
1410 * and stderr if they are redirected. */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001411static int setup_redirects(struct command *prog, int squirrel[])
Eric Andersen25f27032001-04-26 23:22:31 +00001412{
1413 int openfd, mode;
1414 struct redir_struct *redir;
1415
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001416 for (redir = prog->redirects; redir; redir = redir->next) {
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00001417 if (redir->dup == -1 && redir->rd_filename == NULL) {
Eric Andersen817e73c2001-06-06 17:56:09 +00001418 /* something went wrong in the parse. Pretend it didn't happen */
1419 continue;
1420 }
Eric Andersen25f27032001-04-26 23:22:31 +00001421 if (redir->dup == -1) {
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001422 char *p;
Denis Vlasenko55789c62008-06-18 16:30:42 +00001423 mode = redir_table[redir->rd_type].mode;
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00001424//TODO: check redir for names like '\\'
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00001425 p = expand_string_to_string(redir->rd_filename);
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001426 openfd = open_or_warn(p, mode);
1427 free(p);
Eric Andersen25f27032001-04-26 23:22:31 +00001428 if (openfd < 0) {
1429 /* this could get lost if stderr has been redirected, but
1430 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersen25f27032001-04-26 23:22:31 +00001431 return 1;
1432 }
1433 } else {
1434 openfd = redir->dup;
1435 }
1436
1437 if (openfd != redir->fd) {
1438 if (squirrel && redir->fd < 3) {
1439 squirrel[redir->fd] = dup(redir->fd);
1440 }
Eric Andersen83a2ae22001-05-07 17:59:25 +00001441 if (openfd == -3) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001442 //close(openfd); // close(-3) ??!
Eric Andersen83a2ae22001-05-07 17:59:25 +00001443 } else {
1444 dup2(openfd, redir->fd);
Matt Kraaic616e532001-06-05 16:50:08 +00001445 if (redir->dup == -1)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001446 close(openfd);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001447 }
Eric Andersen25f27032001-04-26 23:22:31 +00001448 }
1449 }
1450 return 0;
1451}
1452
1453static void restore_redirects(int squirrel[])
1454{
1455 int i, fd;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001456 for (i = 0; i < 3; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001457 fd = squirrel[i];
1458 if (fd != -1) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00001459 /* We simply die on error */
1460 xmove_fd(fd, i);
Eric Andersen25f27032001-04-26 23:22:31 +00001461 }
1462 }
1463}
1464
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001465static char **expand_assignments(char **argv, int count)
1466{
1467 int i;
1468 char **p = NULL;
1469 /* Expand assignments into one string each */
1470 for (i = 0; i < count; i++) {
1471 p = add_string_to_strings(p, expand_string_to_string(argv[i]));
1472 }
1473 return p;
1474}
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001475
Denis Vlasenko05743d72008-02-10 12:10:08 +00001476/* Called after [v]fork() in run_pipe(), or from builtin_exec().
Denis Vlasenko8412d792007-10-01 09:59:47 +00001477 * Never returns.
1478 * XXX no exit() here. If you don't exec, use _exit instead.
Eric Andersen94ac2442001-05-22 19:05:18 +00001479 * The at_exit handlers apparently confuse the calling process,
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001480 * in particular stdin handling. Not sure why? -- because of vfork! (vda) */
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001481static void pseudo_exec_argv(nommu_save_t *nommu_save, char **argv, int assignment_cnt, char **argv_expanded)
Eric Andersen25f27032001-04-26 23:22:31 +00001482{
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001483 int rcode;
1484 char **new_env;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001485 const struct built_in_command *x;
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001486
Denis Vlasenko1359da62007-04-21 23:27:30 +00001487 /* If a variable is assigned in a forest, and nobody listens,
1488 * was it ever really set?
1489 */
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001490 if (!argv[assignment_cnt])
Denis Vlasenko1359da62007-04-21 23:27:30 +00001491 _exit(EXIT_SUCCESS);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001492
Denis Vlasenko9504e442008-10-29 01:19:15 +00001493 new_env = expand_assignments(argv, assignment_cnt);
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001494#if BB_MMU
1495 putenv_all(new_env);
1496 free(new_env); /* optional */
1497#else
1498 nommu_save->new_env = new_env;
1499 nommu_save->old_env = putenv_all_and_save_old(new_env);
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001500#endif
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001501 if (argv_expanded) {
1502 argv = argv_expanded;
1503 } else {
1504 argv = expand_strvec_to_strvec(argv);
Denis Vlasenko76d50412008-06-10 16:19:39 +00001505#if !BB_MMU
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001506 nommu_save->argv = argv;
Denis Vlasenko76d50412008-06-10 16:19:39 +00001507#endif
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001508 }
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001509
Denis Vlasenko1359da62007-04-21 23:27:30 +00001510 /*
1511 * Check if the command matches any of the builtins.
1512 * Depending on context, this might be redundant. But it's
1513 * easier to waste a few CPU cycles than it is to figure out
1514 * if this is one of those cases.
1515 */
Denis Vlasenkodd316dd2008-06-14 15:50:55 +00001516 for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001517 if (strcmp(argv[0], x->cmd) == 0) {
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001518 debug_printf_exec("running builtin '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001519 rcode = x->function(argv);
1520 fflush(stdout);
1521 _exit(rcode);
Eric Andersen94ac2442001-05-22 19:05:18 +00001522 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001523 }
Eric Andersen78a7c992001-05-15 16:30:25 +00001524
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001525 /* Check if the command matches any busybox applets */
Denis Vlasenko80d14be2007-04-10 23:03:30 +00001526#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001527 if (strchr(argv[0], '/') == NULL) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001528 int a = find_applet_by_name(argv[0]);
1529 if (a >= 0) {
1530 if (APPLET_IS_NOEXEC(a)) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001531 debug_printf_exec("running applet '%s'\n", argv[0]);
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001532// is it ok that run_applet_no_and_exit() does exit(), not _exit()?
1533 run_applet_no_and_exit(a, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001534 }
1535 /* re-exec ourselves with the new arguments */
1536 debug_printf_exec("re-execing applet '%s'\n", argv[0]);
Denis Vlasenkobdbbb7e2007-06-08 15:02:55 +00001537 execvp(bb_busybox_exec_path, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001538 /* If they called chroot or otherwise made the binary no longer
1539 * executable, fall through */
1540 }
1541 }
Eric Andersenaac75e52001-04-30 18:18:45 +00001542#endif
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001543
1544 debug_printf_exec("execing '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001545 execvp(argv[0], argv);
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +00001546 bb_perror_msg("can't exec '%s'", argv[0]);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001547 _exit(EXIT_FAILURE);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001548}
1549
Denis Vlasenko05743d72008-02-10 12:10:08 +00001550/* Called after [v]fork() in run_pipe()
Denis Vlasenko8412d792007-10-01 09:59:47 +00001551 */
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001552static void pseudo_exec(nommu_save_t *nommu_save, struct command *command, char **argv_expanded)
Denis Vlasenko1359da62007-04-21 23:27:30 +00001553{
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001554 if (command->argv)
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001555 pseudo_exec_argv(nommu_save, command->argv, command->assignment_cnt, argv_expanded);
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001556
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001557 if (command->group) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001558#if !BB_MMU
Denis Vlasenko3b492162007-12-24 14:26:57 +00001559 bb_error_msg_and_die("nested lists are not supported on NOMMU");
Denis Vlasenko8412d792007-10-01 09:59:47 +00001560#else
Denis Vlasenko3b492162007-12-24 14:26:57 +00001561 int rcode;
Denis Vlasenko05743d72008-02-10 12:10:08 +00001562 debug_printf_exec("pseudo_exec: run_list\n");
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001563 rcode = run_list(command->group);
Eric Andersenbf7df042001-05-23 22:18:35 +00001564 /* OK to leak memory by not calling free_pipe_list,
Eric Andersen25f27032001-04-26 23:22:31 +00001565 * since this process is about to exit */
Eric Andersen94ac2442001-05-22 19:05:18 +00001566 _exit(rcode);
Denis Vlasenko8412d792007-10-01 09:59:47 +00001567#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001568 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001569
1570 /* Can happen. See what bash does with ">foo" by itself. */
1571 debug_printf("trying to pseudo_exec null command\n");
1572 _exit(EXIT_SUCCESS);
Eric Andersen25f27032001-04-26 23:22:31 +00001573}
1574
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001575#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001576static const char *get_cmdtext(struct pipe *pi)
1577{
1578 char **argv;
1579 char *p;
1580 int len;
1581
1582 /* This is subtle. ->cmdtext is created only on first backgrounding.
1583 * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...)
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001584 * On subsequent bg argv is trashed, but we won't use it */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001585 if (pi->cmdtext)
1586 return pi->cmdtext;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001587 argv = pi->cmds[0].argv;
Denis Vlasenkoa8442002008-06-14 11:00:17 +00001588 if (!argv || !argv[0]) {
1589 pi->cmdtext = xzalloc(1);
1590 return pi->cmdtext;
1591 }
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001592
1593 len = 0;
1594 do len += strlen(*argv) + 1; while (*++argv);
1595 pi->cmdtext = p = xmalloc(len);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001596 argv = pi->cmds[0].argv;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001597 do {
1598 len = strlen(*argv);
1599 memcpy(p, *argv, len);
1600 p += len;
1601 *p++ = ' ';
1602 } while (*++argv);
1603 p[-1] = '\0';
1604 return pi->cmdtext;
1605}
1606
Eric Andersenbafd94f2001-05-02 16:11:59 +00001607static void insert_bg_job(struct pipe *pi)
1608{
1609 struct pipe *thejob;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001610 int i;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001611
1612 /* Linear search for the ID of the job to use */
1613 pi->jobid = 1;
Denis Vlasenko87a86552008-07-29 19:43:10 +00001614 for (thejob = G.job_list; thejob; thejob = thejob->next)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001615 if (thejob->jobid >= pi->jobid)
1616 pi->jobid = thejob->jobid + 1;
1617
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001618 /* Add thejob to the list of running jobs */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001619 if (!G.job_list) {
1620 thejob = G.job_list = xmalloc(sizeof(*thejob));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001621 } else {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001622 for (thejob = G.job_list; thejob->next; thejob = thejob->next)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001623 continue;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001624 thejob->next = xmalloc(sizeof(*thejob));
1625 thejob = thejob->next;
1626 }
1627
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001628 /* Physically copy the struct job */
Eric Andersen0fcd4472001-05-02 20:12:03 +00001629 memcpy(thejob, pi, sizeof(struct pipe));
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001630 thejob->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds);
1631 /* We cannot copy entire pi->cmds[] vector! Double free()s will happen */
1632 for (i = 0; i < pi->num_cmds; i++) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001633// TODO: do we really need to have so many fields which are just dead weight
1634// at execution stage?
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001635 thejob->cmds[i].pid = pi->cmds[i].pid;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001636 /* all other fields are not used and stay zero */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001637 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001638 thejob->next = NULL;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001639 thejob->cmdtext = xstrdup(get_cmdtext(pi));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001640
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001641 /* We don't wait for background thejobs to return -- append it
Eric Andersenbafd94f2001-05-02 16:11:59 +00001642 to the list of backgrounded thejobs and leave it alone */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001643 printf("[%d] %d %s\n", thejob->jobid, thejob->cmds[0].pid, thejob->cmdtext);
1644 G.last_bg_pid = thejob->cmds[0].pid;
Denis Vlasenko87a86552008-07-29 19:43:10 +00001645 G.last_jobid = thejob->jobid;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001646}
1647
Eric Andersenbafd94f2001-05-02 16:11:59 +00001648static void remove_bg_job(struct pipe *pi)
1649{
1650 struct pipe *prev_pipe;
1651
Denis Vlasenko87a86552008-07-29 19:43:10 +00001652 if (pi == G.job_list) {
1653 G.job_list = pi->next;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001654 } else {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001655 prev_pipe = G.job_list;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001656 while (prev_pipe->next != pi)
1657 prev_pipe = prev_pipe->next;
1658 prev_pipe->next = pi->next;
1659 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00001660 if (G.job_list)
1661 G.last_jobid = G.job_list->jobid;
Eric Andersen028b65b2001-06-28 01:10:11 +00001662 else
Denis Vlasenko87a86552008-07-29 19:43:10 +00001663 G.last_jobid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001664}
Eric Andersen028b65b2001-06-28 01:10:11 +00001665
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001666/* Remove a backgrounded job */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001667static void delete_finished_bg_job(struct pipe *pi)
1668{
1669 remove_bg_job(pi);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001670 pi->stopped_cmds = 0;
Eric Andersenbf7df042001-05-23 22:18:35 +00001671 free_pipe(pi, 0);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001672 free(pi);
1673}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001674#endif /* JOB */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001675
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001676/* Check to see if any processes have exited -- if they
1677 * have, figure out why and see if a job has completed */
Eric Andersenc798b072001-06-22 06:23:03 +00001678static int checkjobs(struct pipe* fg_pipe)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001679{
Eric Andersenc798b072001-06-22 06:23:03 +00001680 int attributes;
1681 int status;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001682#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00001683 struct pipe *pi;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001684#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001685 pid_t childpid;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001686 int rcode = 0;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001687
Eric Andersenc798b072001-06-22 06:23:03 +00001688 attributes = WUNTRACED;
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001689 if (fg_pipe == NULL)
Eric Andersenc798b072001-06-22 06:23:03 +00001690 attributes |= WNOHANG;
Eric Andersenc798b072001-06-22 06:23:03 +00001691
Denis Vlasenko1359da62007-04-21 23:27:30 +00001692/* Do we do this right?
1693 * bash-3.00# sleep 20 | false
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001694 * <ctrl-Z pressed>
Denis Vlasenko1359da62007-04-21 23:27:30 +00001695 * [3]+ Stopped sleep 20 | false
1696 * bash-3.00# echo $?
1697 * 1 <========== bg pipe is not fully done, but exitcode is already known!
1698 */
1699
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001700//FIXME: non-interactive bash does not continue even if all processes in fg pipe
1701//are stopped. Testcase: "cat | cat" in a script (not on command line)
1702// + killall -STOP cat
1703
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001704 wait_more:
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001705// TODO: safe_waitpid?
Eric Andersenc798b072001-06-22 06:23:03 +00001706 while ((childpid = waitpid(-1, &status, attributes)) > 0) {
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001707 int i;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001708 const int dead = WIFEXITED(status) || WIFSIGNALED(status);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001709#if DEBUG_JOBS
Denis Vlasenko1359da62007-04-21 23:27:30 +00001710 if (WIFSTOPPED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001711 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001712 childpid, WSTOPSIG(status), WEXITSTATUS(status));
1713 if (WIFSIGNALED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001714 debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001715 childpid, WTERMSIG(status), WEXITSTATUS(status));
1716 if (WIFEXITED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001717 debug_printf_jobs("pid %d exited, exitcode %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001718 childpid, WEXITSTATUS(status));
1719#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001720 /* Were we asked to wait for fg pipe? */
Eric Andersenc798b072001-06-22 06:23:03 +00001721 if (fg_pipe) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001722 for (i = 0; i < fg_pipe->num_cmds; i++) {
1723 debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid);
1724 if (fg_pipe->cmds[i].pid != childpid)
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001725 continue;
1726 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */
1727 if (dead) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001728 fg_pipe->cmds[i].pid = 0;
1729 fg_pipe->alive_cmds--;
1730 if (i == fg_pipe->num_cmds - 1) {
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001731 /* last process gives overall exitstatus */
1732 rcode = WEXITSTATUS(status);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00001733 IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;)
Denis Vlasenko1359da62007-04-21 23:27:30 +00001734 }
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001735 } else {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001736 fg_pipe->cmds[i].is_stopped = 1;
1737 fg_pipe->stopped_cmds++;
Eric Andersenc798b072001-06-22 06:23:03 +00001738 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001739 debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n",
1740 fg_pipe->alive_cmds, fg_pipe->stopped_cmds);
1741 if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) {
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001742 /* All processes in fg pipe have exited/stopped */
1743#if ENABLE_HUSH_JOB
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001744 if (fg_pipe->alive_cmds)
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001745 insert_bg_job(fg_pipe);
1746#endif
1747 return rcode;
1748 }
1749 /* There are still running processes in the fg pipe */
1750 goto wait_more; /* do waitpid again */
Eric Andersenc798b072001-06-22 06:23:03 +00001751 }
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001752 /* it wasnt fg_pipe, look for process in bg pipes */
Eric Andersenc798b072001-06-22 06:23:03 +00001753 }
1754
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001755#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001756 /* We asked to wait for bg or orphaned children */
1757 /* No need to remember exitcode in this case */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001758 for (pi = G.job_list; pi; pi = pi->next) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001759 for (i = 0; i < pi->num_cmds; i++) {
1760 if (pi->cmds[i].pid == childpid)
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001761 goto found_pi_and_prognum;
Eric Andersen52a97ca2001-06-22 06:49:26 +00001762 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001763 }
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001764 /* Happens when shell is used as init process (init=/bin/sh) */
1765 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001766 continue; /* do waitpid again */
Eric Andersenaeb44c42001-05-22 20:29:00 +00001767
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001768 found_pi_and_prognum:
Denis Vlasenko1359da62007-04-21 23:27:30 +00001769 if (dead) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001770 /* child exited */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001771 pi->cmds[i].pid = 0;
1772 pi->alive_cmds--;
1773 if (!pi->alive_cmds) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001774 printf(JOB_STATUS_FORMAT, pi->jobid,
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001775 "Done", pi->cmdtext);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001776 delete_finished_bg_job(pi);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001777 }
1778 } else {
1779 /* child stopped */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001780 pi->cmds[i].is_stopped = 1;
1781 pi->stopped_cmds++;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001782 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001783#endif
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001784 } /* while (waitpid succeeds)... */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001785
Denis Vlasenko1359da62007-04-21 23:27:30 +00001786 /* wait found no children or failed */
1787
1788 if (childpid && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001789 bb_perror_msg("waitpid");
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001790 return rcode;
Eric Andersenada18ff2001-05-21 16:18:22 +00001791}
1792
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001793#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +00001794static int checkjobs_and_fg_shell(struct pipe* fg_pipe)
1795{
1796 pid_t p;
1797 int rcode = checkjobs(fg_pipe);
1798 /* Job finished, move the shell to the foreground */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001799 p = getpgid(0); /* pgid of our process */
1800 debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p);
Denis Vlasenko87a86552008-07-29 19:43:10 +00001801 tcsetpgrp(G.interactive_fd, p);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001802 return rcode;
1803}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001804#endif
Denis Vlasenko52881e92007-04-21 13:42:52 +00001805
Denis Vlasenko05743d72008-02-10 12:10:08 +00001806/* run_pipe() starts all the jobs, but doesn't wait for anything
Eric Andersenc798b072001-06-22 06:23:03 +00001807 * to finish. See checkjobs().
Eric Andersen25f27032001-04-26 23:22:31 +00001808 *
1809 * return code is normally -1, when the caller has to wait for children
1810 * to finish to determine the exit status of the pipe. If the pipe
1811 * is a simple builtin command, however, the action is done by the
Denis Vlasenko05743d72008-02-10 12:10:08 +00001812 * time run_pipe returns, and the exit code is provided as the
Eric Andersen25f27032001-04-26 23:22:31 +00001813 * return value.
1814 *
1815 * The input of the pipe is always stdin, the output is always
1816 * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
1817 * because it tries to avoid running the command substitution in
1818 * subshell, when that is in fact necessary. The subshell process
1819 * now has its stdout directed to the input of the appropriate pipe,
1820 * so this routine is noticeably simpler.
Denis Vlasenko170435c2007-05-23 13:01:10 +00001821 *
1822 * Returns -1 only if started some children. IOW: we have to
1823 * mask out retvals of builtins etc with 0xff!
Eric Andersen25f27032001-04-26 23:22:31 +00001824 */
Denis Vlasenko05743d72008-02-10 12:10:08 +00001825static int run_pipe(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00001826{
1827 int i;
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001828 int nextin;
1829 int pipefds[2]; /* pipefds[0] is for reading */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001830 struct command *command;
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001831 char **argv_expanded;
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001832 char **argv;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001833 const struct built_in_command *x;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001834 char *p;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001835 /* it is not always needed, but we aim to smaller code */
1836 int squirrel[] = { -1, -1, -1 };
1837 int rcode;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001838 const int single_and_fg = (pi->num_cmds == 1 && pi->followup != PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00001839
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001840 debug_printf_exec("run_pipe start: single_and_fg=%d\n", single_and_fg);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001841
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001842#if ENABLE_HUSH_JOB
Eric Andersenada18ff2001-05-21 16:18:22 +00001843 pi->pgrp = -1;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001844#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001845 pi->alive_cmds = 1;
1846 pi->stopped_cmds = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001847
1848 /* Check if this is a simple builtin (not part of a pipe).
1849 * Builtins within pipes have to fork anyway, and are handled in
1850 * pseudo_exec. "echo foo | read bar" doesn't work on bash, either.
1851 */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001852 command = &(pi->cmds[0]);
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001853
Denis Vlasenko371de4a2008-10-14 12:43:13 +00001854#if ENABLE_HUSH_FUNCTIONS
1855 if (single_and_fg && command->group && command->grp_type == GRP_FUNCTION) {
1856 /* We "execute" function definition */
1857 bb_error_msg("here we ought to remember function definition, and go on");
1858 return EXIT_SUCCESS;
1859 }
1860#endif
1861
1862 if (single_and_fg && command->group && command->grp_type == GRP_NORMAL) {
Eric Andersen04407e52001-06-07 16:42:05 +00001863 debug_printf("non-subshell grouping\n");
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001864 setup_redirects(command, squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001865 debug_printf_exec(": run_list\n");
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001866 rcode = run_list(command->group) & 0xff;
Eric Andersen04407e52001-06-07 16:42:05 +00001867 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001868 debug_printf_exec("run_pipe return %d\n", rcode);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00001869 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
Denis Vlasenko05743d72008-02-10 12:10:08 +00001870 return rcode;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001871 }
1872
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001873 argv = command->argv;
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001874 argv_expanded = NULL;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001875
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001876 if (single_and_fg && argv != NULL) {
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001877 char **new_env = NULL;
1878 char **old_env = NULL;
1879
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001880 i = command->assignment_cnt;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001881 if (i != 0 && argv[i] == NULL) {
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001882 /* assignments, but no command: set local environment */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001883 for (i = 0; argv[i] != NULL; i++) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001884 debug_printf("local environment set: %s\n", argv[i]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001885 p = expand_string_to_string(argv[i]);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001886 set_local_var(p, 0);
Eric Andersen78a7c992001-05-15 16:30:25 +00001887 }
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001888 return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */
Eric Andersen78a7c992001-05-15 16:30:25 +00001889 }
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001890
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001891 /* Expand the rest into (possibly) many strings each */
1892 argv_expanded = expand_strvec_to_strvec(argv + i);
1893
Denis Vlasenkodd316dd2008-06-14 15:50:55 +00001894 for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001895 if (strcmp(argv_expanded[0], x->cmd) != 0)
1896 continue;
1897 if (x->function == builtin_exec && argv_expanded[1] == NULL) {
1898 debug_printf("exec with redirects only\n");
1899 setup_redirects(command, NULL);
1900 rcode = EXIT_SUCCESS;
1901 goto clean_up_and_ret1;
Eric Andersen25f27032001-04-26 23:22:31 +00001902 }
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001903 debug_printf("builtin inline %s\n", argv_expanded[0]);
1904 /* XXX setup_redirects acts on file descriptors, not FILEs.
1905 * This is perfect for work that comes after exec().
1906 * Is it really safe for inline use? Experimentally,
1907 * things seem to work with glibc. */
1908 setup_redirects(command, squirrel);
1909 new_env = expand_assignments(argv, command->assignment_cnt);
1910 old_env = putenv_all_and_save_old(new_env);
1911 debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv_expanded[1]);
1912 rcode = x->function(argv_expanded) & 0xff;
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001913#if ENABLE_FEATURE_SH_STANDALONE
1914 clean_up_and_ret:
1915#endif
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001916 restore_redirects(squirrel);
1917 free_strings_and_unsetenv(new_env, 1);
1918 putenv_all(old_env);
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001919 free(old_env); /* not free_strings()! */
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001920 clean_up_and_ret1:
1921 free(argv_expanded);
1922 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
1923 debug_printf_exec("run_pipe return %d\n", rcode);
1924 return rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00001925 }
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001926#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001927 i = find_applet_by_name(argv_expanded[0]);
1928 if (i >= 0 && APPLET_IS_NOFORK(i)) {
1929 setup_redirects(command, squirrel);
1930 save_nofork_data(&G.nofork_save);
1931 new_env = expand_assignments(argv, command->assignment_cnt);
1932 old_env = putenv_all_and_save_old(new_env);
1933 debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]);
1934 rcode = run_nofork_applet_prime(&G.nofork_save, i, argv_expanded);
1935 goto clean_up_and_ret;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001936 }
1937#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001938 }
1939
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001940 /* NB: argv_expanded may already be created, and that
1941 * might include `cmd` runs! Do not rerun it! We *must*
1942 * use argv_expanded if it's non-NULL */
1943
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001944 /* Disable job control signals for shell (parent) and
1945 * for initial child code after fork */
1946 set_jobctrl_sighandler(SIG_IGN);
1947
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001948 /* Going to fork a child per each pipe member */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001949 pi->alive_cmds = 0;
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001950 nextin = 0;
1951
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001952 for (i = 0; i < pi->num_cmds; i++) {
Denis Vlasenko76d50412008-06-10 16:19:39 +00001953#if !BB_MMU
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001954 volatile nommu_save_t nommu_save;
1955 nommu_save.new_env = NULL;
1956 nommu_save.old_env = NULL;
1957 nommu_save.argv = NULL;
Denis Vlasenko76d50412008-06-10 16:19:39 +00001958#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001959 command = &(pi->cmds[i]);
1960 if (command->argv) {
1961 debug_printf_exec(": pipe member '%s' '%s'...\n", command->argv[0], command->argv[1]);
Denis Vlasenko76d50412008-06-10 16:19:39 +00001962 } else
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001963 debug_printf_exec(": pipe member with no argv\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001964
1965 /* pipes are inserted between pairs of commands */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001966 pipefds[0] = 0;
1967 pipefds[1] = 1;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001968 if ((i + 1) < pi->num_cmds)
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001969 xpipe(pipefds);
Eric Andersen25f27032001-04-26 23:22:31 +00001970
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001971 command->pid = BB_MMU ? fork() : vfork();
1972 if (!command->pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00001973 if (ENABLE_HUSH_JOB)
1974 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001975#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001976 /* Every child adds itself to new process group
Denis Vlasenko05743d72008-02-10 12:10:08 +00001977 * with pgid == pid_of_first_child_in_pipe */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001978 if (G.run_list_level == 1 && G.interactive_fd) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00001979 pid_t pgrp;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001980 /* Don't do pgrp restore anymore on fatal signals */
1981 set_fatal_sighandler(SIG_DFL);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001982 pgrp = pi->pgrp;
1983 if (pgrp < 0) /* true for 1st process only */
1984 pgrp = getpid();
1985 if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001986 /* We do it in *every* child, not just first,
1987 * to avoid races */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001988 tcsetpgrp(G.interactive_fd, pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001989 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001990 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001991#endif
Denis Vlasenko8412d792007-10-01 09:59:47 +00001992 xmove_fd(nextin, 0);
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001993 xmove_fd(pipefds[1], 1); /* write end */
1994 if (pipefds[0] > 1)
1995 close(pipefds[0]); /* read end */
Eric Andersen25f27032001-04-26 23:22:31 +00001996 /* Like bash, explicit redirects override pipes,
1997 * and the pipe fd is available for dup'ing. */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001998 setup_redirects(command, NULL);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001999
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002000 /* Restore default handlers just prior to exec */
2001 set_jobctrl_sighandler(SIG_DFL);
2002 set_misc_sighandler(SIG_DFL);
2003 signal(SIGCHLD, SIG_DFL);
Denis Vlasenkof886fd22008-10-13 12:36:05 +00002004 /* Stores to nommu_save list of env vars putenv'ed
2005 * (NOMMU, on MMU we don't need that) */
2006 /* cast away volatility... */
2007 pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded);
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00002008 /* pseudo_exec() does not return */
Eric Andersen25f27032001-04-26 23:22:31 +00002009 }
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00002010 /* parent */
2011#if !BB_MMU
Denis Vlasenkof886fd22008-10-13 12:36:05 +00002012 /* Clean up after vforked child */
2013 free(nommu_save.argv);
2014 free_strings_and_unsetenv(nommu_save.new_env, 1);
2015 putenv_all(nommu_save.old_env);
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00002016#endif
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00002017 free(argv_expanded);
2018 argv_expanded = NULL;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002019 if (command->pid < 0) { /* [v]fork failed */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002020 /* Clearly indicate, was it fork or vfork */
Denis Vlasenko82604e92008-07-01 15:59:42 +00002021 bb_perror_msg(BB_MMU ? "fork" : "vfork");
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002022 } else {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002023 pi->alive_cmds++;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002024#if ENABLE_HUSH_JOB
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002025 /* Second and next children need to know pid of first one */
2026 if (pi->pgrp < 0)
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002027 pi->pgrp = command->pid;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002028#endif
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002029 }
Eric Andersen25f27032001-04-26 23:22:31 +00002030
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002031 if (i)
2032 close(nextin);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002033 if ((i + 1) < pi->num_cmds)
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002034 close(pipefds[1]); /* write end */
2035 /* Pass read (output) pipe end to next iteration */
Eric Andersen25f27032001-04-26 23:22:31 +00002036 nextin = pipefds[0];
2037 }
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002038
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002039 if (!pi->alive_cmds) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00002040 debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n");
2041 return 1;
2042 }
2043
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002044 debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds);
Eric Andersen25f27032001-04-26 23:22:31 +00002045 return -1;
2046}
2047
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002048#ifndef debug_print_tree
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002049static void debug_print_tree(struct pipe *pi, int lvl)
2050{
Denis Vlasenko371de4a2008-10-14 12:43:13 +00002051 static const char *const PIPE[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002052 [PIPE_SEQ] = "SEQ",
2053 [PIPE_AND] = "AND",
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002054 [PIPE_OR ] = "OR" ,
2055 [PIPE_BG ] = "BG" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002056 };
2057 static const char *RES[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002058 [RES_NONE ] = "NONE" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00002059#if ENABLE_HUSH_IF
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002060 [RES_IF ] = "IF" ,
2061 [RES_THEN ] = "THEN" ,
2062 [RES_ELIF ] = "ELIF" ,
2063 [RES_ELSE ] = "ELSE" ,
2064 [RES_FI ] = "FI" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00002065#endif
2066#if ENABLE_HUSH_LOOPS
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002067 [RES_FOR ] = "FOR" ,
2068 [RES_WHILE] = "WHILE",
2069 [RES_UNTIL] = "UNTIL",
2070 [RES_DO ] = "DO" ,
2071 [RES_DONE ] = "DONE" ,
Denis Vlasenkod91afa32008-07-29 11:10:01 +00002072#endif
2073#if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002074 [RES_IN ] = "IN" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00002075#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002076#if ENABLE_HUSH_CASE
2077 [RES_CASE ] = "CASE" ,
2078 [RES_MATCH] = "MATCH",
2079 [RES_CASEI] = "CASEI",
2080 [RES_ESAC ] = "ESAC" ,
2081#endif
Denis Vlasenko06810332007-05-21 23:30:54 +00002082 [RES_XXXX ] = "XXXX" ,
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002083 [RES_SNTX ] = "SNTX" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002084 };
Denis Vlasenko371de4a2008-10-14 12:43:13 +00002085 static const char *const GRPTYPE[] = {
2086 "()",
2087 "{}",
2088#if ENABLE_HUSH_FUNCTIONS
2089 "func()",
2090#endif
2091 };
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002092
2093 int pin, prn;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002094
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002095 pin = 0;
2096 while (pi) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002097 fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "",
2098 pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002099 prn = 0;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002100 while (prn < pi->num_cmds) {
2101 struct command *command = &pi->cmds[prn];
2102 char **argv = command->argv;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002103
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002104 fprintf(stderr, "%*s prog %d assignment_cnt:%d", lvl*2, "", prn, command->assignment_cnt);
2105 if (command->group) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002106 fprintf(stderr, " group %s: (argv=%p)\n",
Denis Vlasenko371de4a2008-10-14 12:43:13 +00002107 GRPTYPE[command->grp_type],
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002108 argv);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002109 debug_print_tree(command->group, lvl+1);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002110 prn++;
2111 continue;
2112 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002113 if (argv) while (*argv) {
2114 fprintf(stderr, " '%s'", *argv);
2115 argv++;
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002116 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002117 fprintf(stderr, "\n");
2118 prn++;
2119 }
2120 pi = pi->next;
2121 pin++;
2122 }
2123}
2124#endif
2125
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002126/* NB: called by pseudo_exec, and therefore must not modify any
2127 * global data until exec/_exit (we can be a child after vfork!) */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002128static int run_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002129{
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002130#if ENABLE_HUSH_CASE
2131 char *case_word = NULL;
2132#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002133#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002134 struct pipe *loop_top = NULL;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002135 char *for_varname = NULL;
2136 char **for_lcur = NULL;
2137 char **for_list = NULL;
Denis Vlasenko06810332007-05-21 23:30:54 +00002138#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002139 smallint flag_skip = 1;
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002140 smalluint rcode = 0; /* probably just for compiler */
Denis Vlasenkod91afa32008-07-29 11:10:01 +00002141#if ENABLE_HUSH_IF || ENABLE_HUSH_CASE
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002142 smalluint cond_code = 0;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002143#else
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002144 enum { cond_code = 0, };
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002145#endif
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002146 /*enum reserved_style*/ smallint rword = RES_NONE;
2147 /*enum reserved_style*/ smallint skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002148
Denis Vlasenko87a86552008-07-29 19:43:10 +00002149 debug_printf_exec("run_list start lvl %d\n", G.run_list_level + 1);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002150
Denis Vlasenko06810332007-05-21 23:30:54 +00002151#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002152 /* Check syntax for "for" */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002153 for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) {
2154 if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN)
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002155 continue;
2156 /* current word is FOR or IN (BOLD in comments below) */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002157 if (cpipe->next == NULL) {
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002158 syntax("malformed for");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002159 debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002160 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002161 }
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002162 /* "FOR v; do ..." and "for v IN a b; do..." are ok */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002163 if (cpipe->next->res_word == RES_DO)
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002164 continue;
2165 /* next word is not "do". It must be "in" then ("FOR v in ...") */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002166 if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */
2167 || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002168 ) {
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002169 syntax("malformed for");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002170 debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002171 return 1;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002172 }
2173 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002174#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002175
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002176 /* Past this point, all code paths should jump to ret: label
Denis Vlasenko12acec52008-07-28 15:15:59 +00002177 * in order to return, no direct "return" statements please.
2178 * This helps to ensure that no memory is leaked. */
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002179
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002180#if ENABLE_HUSH_JOB
2181 /* Example of nested list: "while true; do { sleep 1 | exit 2; } done".
2182 * We are saving state before entering outermost list ("while...done")
2183 * so that ctrl-Z will correctly background _entire_ outermost list,
2184 * not just a part of it (like "sleep 1 | exit 2") */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002185 if (++G.run_list_level == 1 && G.interactive_fd) {
2186 if (sigsetjmp(G.toplevel_jb, 1)) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002187 /* ctrl-Z forked and we are parent; or ctrl-C.
2188 * Sighandler has longjmped us here */
2189 signal(SIGINT, SIG_IGN);
2190 signal(SIGTSTP, SIG_IGN);
2191 /* Restore level (we can be coming from deep inside
2192 * nested levels) */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002193 G.run_list_level = 1;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002194#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko87a86552008-07-29 19:43:10 +00002195 if (G.nofork_save.saved) { /* if save area is valid */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002196 debug_printf_jobs("exiting nofork early\n");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002197 restore_nofork_data(&G.nofork_save);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002198 }
2199#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00002200 if (G.ctrl_z_flag) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002201 /* ctrl-Z has forked and stored pid of the child in pi->pid.
2202 * Remember this child as background job */
2203 insert_bg_job(pi);
2204 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002205 /* ctrl-C. We just stop doing whatever we were doing */
Denis Vlasenko4daad902007-09-27 10:20:47 +00002206 bb_putchar('\n');
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002207 }
Denis Vlasenkoff29b4f2008-07-29 13:57:59 +00002208 USE_HUSH_LOOPS(loop_top = NULL;)
Denis Vlasenko87a86552008-07-29 19:43:10 +00002209 USE_HUSH_LOOPS(G.depth_of_loop = 0;)
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002210 rcode = 0;
2211 goto ret;
2212 }
2213 /* ctrl-Z handler will store pid etc in pi */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002214 G.toplevel_list = pi;
2215 G.ctrl_z_flag = 0;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002216#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko87a86552008-07-29 19:43:10 +00002217 G.nofork_save.saved = 0; /* in case we will run a nofork later */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002218#endif
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +00002219 signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002220 signal(SIGINT, handler_ctrl_c);
2221 }
Denis Vlasenko05743d72008-02-10 12:10:08 +00002222#endif /* JOB */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002223
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002224 /* Go through list of pipes, (maybe) executing them. */
2225 for (; pi; pi = USE_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) {
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002226 IF_HAS_KEYWORDS(rword = pi->res_word;)
2227 IF_HAS_NO_KEYWORDS(rword = RES_NONE;)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002228 debug_printf_exec(": rword=%d cond_code=%d skip_more=%d\n",
2229 rword, cond_code, skip_more_for_this_rword);
Denis Vlasenko06810332007-05-21 23:30:54 +00002230#if ENABLE_HUSH_LOOPS
Denis Vlasenko4554b722008-07-29 13:36:09 +00002231 if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR)
Denis Vlasenko87a86552008-07-29 19:43:10 +00002232 && loop_top == NULL /* avoid bumping G.depth_of_loop twice */
Denis Vlasenko4554b722008-07-29 13:36:09 +00002233 ) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002234 /* start of a loop: remember where loop starts */
2235 loop_top = pi;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002236 G.depth_of_loop++;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002237 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002238#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002239 if (rword == skip_more_for_this_rword && flag_skip) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002240 if (pi->followup == PIPE_SEQ)
2241 flag_skip = 0;
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002242 /* it is "<false> && CMD" or "<true> || CMD"
2243 * and we should not execute CMD */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002244 continue;
2245 }
2246 flag_skip = 1;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002247 skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko06810332007-05-21 23:30:54 +00002248#if ENABLE_HUSH_IF
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002249 if (cond_code) {
2250 if (rword == RES_THEN) {
2251 /* "if <false> THEN cmd": skip cmd */
2252 continue;
2253 }
2254 } else {
2255 if (rword == RES_ELSE || rword == RES_ELIF) {
2256 /* "if <true> then ... ELSE/ELIF cmd":
2257 * skip cmd and all following ones */
2258 break;
2259 }
2260 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002261#endif
2262#if ENABLE_HUSH_LOOPS
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002263 if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002264 if (!for_lcur) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002265 /* first loop through for */
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002266
2267 static const char encoded_dollar_at[] ALIGN1 = {
2268 SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0'
2269 }; /* encoded representation of "$@" */
2270 static const char *const encoded_dollar_at_argv[] = {
2271 encoded_dollar_at, NULL
2272 }; /* argv list with one element: "$@" */
2273 char **vals;
2274
2275 vals = (char**)encoded_dollar_at_argv;
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002276 if (pi->next->res_word == RES_IN) {
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002277 /* if no variable values after "in" we skip "for" */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002278 if (!pi->next->cmds[0].argv)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002279 break;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002280 vals = pi->next->cmds[0].argv;
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002281 } /* else: "for var; do..." -> assume "$@" list */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002282 /* create list of variable values */
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002283 debug_print_strings("for_list made from", vals);
2284 for_list = expand_strvec_to_strvec(vals);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002285 for_lcur = for_list;
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002286 debug_print_strings("for_list", for_list);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002287 for_varname = pi->cmds[0].argv[0];
2288 pi->cmds[0].argv[0] = NULL;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002289 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002290 free(pi->cmds[0].argv[0]);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002291 if (!*for_lcur) {
Denis Vlasenko12acec52008-07-28 15:15:59 +00002292 /* "for" loop is over, clean up */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002293 free(for_list);
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002294 for_list = NULL;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002295 for_lcur = NULL;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002296 pi->cmds[0].argv[0] = for_varname;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002297 break;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002298 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002299 /* insert next value from for_lcur */
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002300//TODO: does it need escaping?
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002301 pi->cmds[0].argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++);
2302 pi->cmds[0].assignment_cnt = 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002303 }
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002304 if (rword == RES_IN) /* "for v IN list;..." - "in" has no cmds anyway */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002305 continue;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002306 if (rword == RES_DONE) {
2307 continue; /* "done" has no cmds too */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002308 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002309#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002310#if ENABLE_HUSH_CASE
2311 if (rword == RES_CASE) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002312 case_word = expand_strvec_to_string(pi->cmds->argv);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002313 continue;
2314 }
2315 if (rword == RES_MATCH) {
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00002316 char **argv;
2317
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002318 if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */
2319 break;
2320 /* all prev words didn't match, does this one match? */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002321 argv = pi->cmds->argv;
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00002322 while (*argv) {
2323 char *pattern = expand_string_to_string(*argv);
2324 /* TODO: which FNM_xxx flags to use? */
2325 cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0);
2326 free(pattern);
2327 if (cond_code == 0) { /* match! we will execute this branch */
2328 free(case_word); /* make future "word)" stop */
2329 case_word = NULL;
2330 break;
2331 }
2332 argv++;
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002333 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002334 continue;
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002335 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002336 if (rword == RES_CASEI) { /* inside of a case branch */
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002337 if (cond_code != 0)
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002338 continue; /* not matched yet, skip this pipe */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002339 }
2340#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002341 if (pi->num_cmds == 0)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002342 continue;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002343
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002344 /* After analyzing all keywords and conditions, we decided
2345 * to execute this pipe. NB: has to do checkjobs(NULL)
2346 * after run_pipe() to collect any background children,
2347 * even if list execution is to be stopped. */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002348 debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds);
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002349 {
2350 int r;
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002351#if ENABLE_HUSH_LOOPS
Denis Vlasenko87a86552008-07-29 19:43:10 +00002352 G.flag_break_continue = 0;
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002353#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002354 rcode = r = run_pipe(pi); /* NB: rcode is a smallint */
2355 if (r != -1) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002356 /* we only ran a builtin: rcode is already known
2357 * and we don't need to wait for anything. */
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002358#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002359 /* was it "break" or "continue"? */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002360 if (G.flag_break_continue) {
2361 smallint fbc = G.flag_break_continue;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002362 /* we might fall into outer *loop*,
2363 * don't want to break it too */
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002364 if (loop_top) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002365 G.depth_break_continue--;
2366 if (G.depth_break_continue == 0)
2367 G.flag_break_continue = 0;
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002368 /* else: e.g. "continue 2" should *break* once, *then* continue */
2369 } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002370 if (G.depth_break_continue != 0 || fbc == BC_BREAK)
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002371 goto check_jobs_and_break;
2372 /* "continue": simulate end of loop */
2373 rword = RES_DONE;
2374 continue;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002375 }
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002376#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002377 } else if (pi->followup == PIPE_BG) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002378 /* what does bash do with attempts to background builtins? */
2379 /* even bash 3.2 doesn't do that well with nested bg:
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002380 * try "{ { sleep 10; echo DEEP; } & echo HERE; } &".
2381 * I'm NOT treating inner &'s as jobs */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002382#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00002383 if (G.run_list_level == 1)
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002384 insert_bg_job(pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002385#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002386 rcode = 0; /* EXIT_SUCCESS */
2387 } else {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002388#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00002389 if (G.run_list_level == 1 && G.interactive_fd) {
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002390 /* waits for completion, then fg's main shell */
2391 rcode = checkjobs_and_fg_shell(pi);
2392 debug_printf_exec(": checkjobs_and_fg_shell returned %d\n", rcode);
2393 } else
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002394#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002395 { /* this one just waits for completion */
2396 rcode = checkjobs(pi);
2397 debug_printf_exec(": checkjobs returned %d\n", rcode);
2398 }
Eric Andersen25f27032001-04-26 23:22:31 +00002399 }
2400 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002401 debug_printf_exec(": setting last_return_code=%d\n", rcode);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002402 G.last_return_code = rcode;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002403
2404 /* Analyze how result affects subsequent commands */
Denis Vlasenko06810332007-05-21 23:30:54 +00002405#if ENABLE_HUSH_IF
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002406 if (rword == RES_IF || rword == RES_ELIF)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002407 cond_code = rcode;
Denis Vlasenko06810332007-05-21 23:30:54 +00002408#endif
2409#if ENABLE_HUSH_LOOPS
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002410 if (rword == RES_WHILE) {
Denis Vlasenko918a34b2008-07-28 23:17:31 +00002411 if (rcode) {
2412 rcode = 0; /* "while false; do...done" - exitcode 0 */
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002413 goto check_jobs_and_break;
Denis Vlasenko918a34b2008-07-28 23:17:31 +00002414 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002415 }
2416 if (rword == RES_UNTIL) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002417 if (!rcode) {
2418 check_jobs_and_break:
2419 checkjobs(NULL);
2420 break;
2421 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002422 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002423#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002424 if ((rcode == 0 && pi->followup == PIPE_OR)
2425 || (rcode != 0 && pi->followup == PIPE_AND)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002426 ) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002427 skip_more_for_this_rword = rword;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002428 }
Eric Andersen028b65b2001-06-28 01:10:11 +00002429 checkjobs(NULL);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002430 } /* for (pi) */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002431
2432#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00002433 if (G.ctrl_z_flag) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002434 /* ctrl-Z forked somewhere in the past, we are the child,
2435 * and now we completed running the list. Exit. */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002436//TODO: _exit?
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002437 exit(rcode);
2438 }
2439 ret:
Denis Vlasenko87a86552008-07-29 19:43:10 +00002440 if (!--G.run_list_level && G.interactive_fd) {
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00002441 signal(SIGTSTP, SIG_IGN);
2442 signal(SIGINT, SIG_IGN);
2443 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002444#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00002445 debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode);
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002446#if ENABLE_HUSH_LOOPS
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002447 if (loop_top)
Denis Vlasenko87a86552008-07-29 19:43:10 +00002448 G.depth_of_loop--;
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002449 free(for_list);
2450#endif
2451#if ENABLE_HUSH_CASE
2452 free(case_word);
2453#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002454 return rcode;
2455}
2456
Eric Andersen25f27032001-04-26 23:22:31 +00002457/* return code is the exit status of the pipe */
Eric Andersenbf7df042001-05-23 22:18:35 +00002458static int free_pipe(struct pipe *pi, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002459{
2460 char **p;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002461 struct command *command;
Eric Andersen25f27032001-04-26 23:22:31 +00002462 struct redir_struct *r, *rnext;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002463 int a, i, ret_code = 0;
Eric Andersen52a97ca2001-06-22 06:49:26 +00002464
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002465 if (pi->stopped_cmds > 0)
Eric Andersen52a97ca2001-06-22 06:49:26 +00002466 return ret_code;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002467 debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid());
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002468 for (i = 0; i < pi->num_cmds; i++) {
2469 command = &pi->cmds[i];
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002470 debug_printf_clean("%s command %d:\n", indenter(indent), i);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002471 if (command->argv) {
2472 for (a = 0, p = command->argv; *p; a++, p++) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002473 debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p);
Eric Andersen25f27032001-04-26 23:22:31 +00002474 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002475 free_strings(command->argv);
2476 command->argv = NULL;
2477 } else if (command->group) {
Denis Vlasenko371de4a2008-10-14 12:43:13 +00002478 debug_printf_clean("%s begin group (grp_type:%d)\n", indenter(indent), command->grp_type);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002479 ret_code = free_pipe_list(command->group, indent+3);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002480 debug_printf_clean("%s end group\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002481 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002482 debug_printf_clean("%s (nil)\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002483 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002484 for (r = command->redirects; r; r = rnext) {
Denis Vlasenko211b59b2008-06-23 16:28:53 +00002485 debug_printf_clean("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->rd_type].descrip);
Eric Andersen25f27032001-04-26 23:22:31 +00002486 if (r->dup == -1) {
Eric Andersen817e73c2001-06-06 17:56:09 +00002487 /* guard against the case >$FOO, where foo is unset or blank */
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00002488 if (r->rd_filename) {
2489 debug_printf_clean(" %s\n", r->rd_filename);
2490 free(r->rd_filename);
2491 r->rd_filename = NULL;
Eric Andersen817e73c2001-06-06 17:56:09 +00002492 }
Eric Andersen25f27032001-04-26 23:22:31 +00002493 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002494 debug_printf_clean("&%d\n", r->dup);
Eric Andersen25f27032001-04-26 23:22:31 +00002495 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002496 rnext = r->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002497 free(r);
2498 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002499 command->redirects = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002500 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002501 free(pi->cmds); /* children are an array, they get freed all at once */
2502 pi->cmds = NULL;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002503#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002504 free(pi->cmdtext);
2505 pi->cmdtext = NULL;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002506#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002507 return ret_code;
2508}
2509
Eric Andersenbf7df042001-05-23 22:18:35 +00002510static int free_pipe_list(struct pipe *head, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002511{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002512 int rcode = 0; /* if list has no members */
Eric Andersen25f27032001-04-26 23:22:31 +00002513 struct pipe *pi, *next;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002514
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002515 for (pi = head; pi; pi = next) {
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002516#if HAS_KEYWORDS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002517 debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002518#endif
Eric Andersenbf7df042001-05-23 22:18:35 +00002519 rcode = free_pipe(pi, indent);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002520 debug_printf_clean("%s pipe followup code %d\n", indenter(indent), pi->followup);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002521 next = pi->next;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002522 /*pi->next = NULL;*/
Eric Andersen25f27032001-04-26 23:22:31 +00002523 free(pi);
2524 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002525 return rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00002526}
2527
2528/* Select which version we will use */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002529static int run_and_free_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002530{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002531 int rcode = 0;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002532 debug_printf_exec("run_and_free_list entered\n");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002533 if (!G.fake_mode) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002534 debug_printf_exec(": run_list with %d members\n", pi->num_cmds);
Denis Vlasenko05743d72008-02-10 12:10:08 +00002535 rcode = run_list(pi);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002536 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002537 /* free_pipe_list has the side effect of clearing memory.
Denis Vlasenko05743d72008-02-10 12:10:08 +00002538 * In the long run that function can be merged with run_list,
Eric Andersen25f27032001-04-26 23:22:31 +00002539 * but doing that now would hobble the debugging effort. */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002540 free_pipe_list(pi, /* indent: */ 0);
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002541 debug_printf_exec("run_and_free_list return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002542 return rcode;
2543}
2544
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002545
Denis Vlasenko170435c2007-05-23 13:01:10 +00002546/* expand_strvec_to_strvec() takes a list of strings, expands
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002547 * all variable references within and returns a pointer to
2548 * a list of expanded strings, possibly with larger number
2549 * of strings. (Think VAR="a b"; echo $VAR).
2550 * This new list is allocated as a single malloc block.
2551 * NULL-terminated list of char* pointers is at the beginning of it,
2552 * followed by strings themself.
2553 * Caller can deallocate entire list by single free(list). */
2554
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002555/* Store given string, finalizing the word and starting new one whenever
Denis Vlasenko87a86552008-07-29 19:43:10 +00002556 * we encounter IFS char(s). This is used for expanding variable values.
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002557 * End-of-string does NOT finalize word: think about 'echo -$VAR-' */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002558static int expand_on_ifs(o_string *output, int n, const char *str)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002559{
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002560 while (1) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002561 int word_len = strcspn(str, G.ifs);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002562 if (word_len) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00002563 if (output->o_quote || !output->o_glob)
2564 o_addQstr(output, str, word_len);
2565 else /* protect backslashes against globbing up :) */
2566 o_addstr_duplicate_backslash(output, str, word_len);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002567 str += word_len;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002568 }
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002569 if (!*str) /* EOL - do not finalize word */
2570 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002571 o_addchr(output, '\0');
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002572 debug_print_list("expand_on_ifs", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002573 n = o_save_ptr(output, n);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002574 str += strspn(str, G.ifs); /* skip ifs chars */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002575 }
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002576 debug_print_list("expand_on_ifs[1]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002577 return n;
2578}
2579
2580/* Expand all variable references in given string, adding words to list[]
2581 * at n, n+1,... positions. Return updated n (so that list[n] is next one
2582 * to be filled). This routine is extremely tricky: has to deal with
2583 * variables/parameters with whitespace, $* and $@, and constructs like
2584 * 'echo -$*-'. If you play here, you must run testsuite afterwards! */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002585static int expand_vars_to_list(o_string *output, int n, char *arg, char or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002586{
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002587 /* or_mask is either 0 (normal case) or 0x80
Denis Vlasenko55789c62008-06-18 16:30:42 +00002588 * (expansion of right-hand side of assignment == 1-element expand.
2589 * It will also do no globbing, and thus we must not backslash-quote!) */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002590
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002591 char first_ch, ored_ch;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002592 int i;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002593 const char *val;
2594 char *p;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002595
2596 ored_ch = 0;
2597
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002598 debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002599 debug_print_list("expand_vars_to_list", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002600 n = o_save_ptr(output, n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002601 debug_print_list("expand_vars_to_list[0]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002602
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002603 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) {
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002604#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002605 o_string subst_result = NULL_O_STRING;
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002606#endif
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002607 o_addstr(output, arg, p - arg);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002608 debug_print_list("expand_vars_to_list[1]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002609 arg = ++p;
2610 p = strchr(p, SPECIAL_VAR_SYMBOL);
2611
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002612 first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00002613 /* "$@" is special. Even if quoted, it can still
2614 * expand to nothing (not even an empty string) */
2615 if ((first_ch & 0x7f) != '@')
2616 ored_ch |= first_ch;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002617 val = NULL;
2618 switch (first_ch & 0x7f) {
2619 /* Highest bit in first_ch indicates that var is double-quoted */
2620 case '$': /* pid */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002621 val = utoa(G.root_pid);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002622 break;
2623 case '!': /* bg pid */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002624 val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)"";
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002625 break;
2626 case '?': /* exitcode */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002627 val = utoa(G.last_return_code);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002628 break;
2629 case '#': /* argc */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002630 val = utoa(G.global_argc ? G.global_argc-1 : 0);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002631 break;
2632 case '*':
2633 case '@':
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002634 i = 1;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002635 if (!G.global_argv[i])
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002636 break;
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00002637 ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002638 if (!(first_ch & 0x80)) { /* unquoted $* or $@ */
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002639 smallint sv = output->o_quote;
2640 /* unquoted var's contents should be globbed, so don't quote */
2641 output->o_quote = 0;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002642 while (G.global_argv[i]) {
2643 n = expand_on_ifs(output, n, G.global_argv[i]);
2644 debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1);
2645 if (G.global_argv[i++][0] && G.global_argv[i]) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002646 /* this argv[] is not empty and not last:
2647 * put terminating NUL, start new word */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002648 o_addchr(output, '\0');
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002649 debug_print_list("expand_vars_to_list[2]", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002650 n = o_save_ptr(output, n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002651 debug_print_list("expand_vars_to_list[3]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002652 }
2653 }
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002654 output->o_quote = sv;
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002655 } else
2656 /* If or_mask is nonzero, we handle assignment 'a=....$@.....'
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00002657 * and in this case should treat it like '$*' - see 'else...' below */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002658 if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002659 while (1) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002660 o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i]));
2661 if (++i >= G.global_argc)
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002662 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002663 o_addchr(output, '\0');
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002664 debug_print_list("expand_vars_to_list[4]", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002665 n = o_save_ptr(output, n);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002666 }
2667 } else { /* quoted $*: add as one word */
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002668 while (1) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002669 o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i]));
2670 if (!G.global_argv[++i])
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002671 break;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002672 if (G.ifs[0])
2673 o_addchr(output, G.ifs[0]);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002674 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002675 }
2676 break;
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00002677 case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */
2678 /* "Empty variable", used to make "" etc to not disappear */
2679 arg++;
2680 ored_ch = 0x80;
2681 break;
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002682#if ENABLE_HUSH_TICK
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00002683 case '`': { /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002684 struct in_str input;
2685 *p = '\0';
2686 arg++;
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002687//TODO: can we just stuff it into "output" directly?
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002688 debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002689 setup_string_in_str(&input, arg);
2690 process_command_subs(&subst_result, &input, NULL);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002691 debug_printf_subst("SUBST RES '%s'\n", subst_result.data);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002692 val = subst_result.data;
2693 goto store_val;
2694 }
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002695#endif
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00002696 default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002697 *p = '\0';
2698 arg[0] = first_ch & 0x7f;
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002699 if (isdigit(arg[0])) {
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002700 i = xatoi_u(arg);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002701 if (i < G.global_argc)
2702 val = G.global_argv[i];
Denis Vlasenko55789c62008-06-18 16:30:42 +00002703 /* else val remains NULL: $N with too big N */
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002704 } else
2705 val = lookup_param(arg);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002706 arg[0] = first_ch;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002707#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002708 store_val:
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002709#endif
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002710 *p = SPECIAL_VAR_SYMBOL;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002711 if (!(first_ch & 0x80)) { /* unquoted $VAR */
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002712 debug_printf_expand("unquoted '%s', output->o_quote:%d\n", val, output->o_quote);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002713 if (val) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00002714 /* unquoted var's contents should be globbed, so don't quote */
2715 smallint sv = output->o_quote;
2716 output->o_quote = 0;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002717 n = expand_on_ifs(output, n, val);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002718 val = NULL;
Denis Vlasenko55789c62008-06-18 16:30:42 +00002719 output->o_quote = sv;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002720 }
Denis Vlasenko55789c62008-06-18 16:30:42 +00002721 } else { /* quoted $VAR, val will be appended below */
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002722 debug_printf_expand("quoted '%s', output->o_quote:%d\n", val, output->o_quote);
Denis Vlasenko55789c62008-06-18 16:30:42 +00002723 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002724 }
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002725 if (val) {
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002726 o_addQstr(output, val, strlen(val));
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002727 }
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002728
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002729#if ENABLE_HUSH_TICK
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002730 o_free(&subst_result);
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002731#endif
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002732 arg = ++p;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002733 } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */
2734
Denis Vlasenko2e76c3f2008-06-10 18:27:50 +00002735 if (arg[0]) {
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002736 debug_print_list("expand_vars_to_list[a]", output, n);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002737 /* this part is literal, and it was already pre-quoted
2738 * if needed (much earlier), do not use o_addQstr here! */
2739 o_addstr(output, arg, strlen(arg) + 1);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002740 debug_print_list("expand_vars_to_list[b]", output, n);
Denis Vlasenko2e76c3f2008-06-10 18:27:50 +00002741 } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */
2742 && !(ored_ch & 0x80) /* and all vars were not quoted. */
2743 ) {
2744 n--;
2745 /* allow to reuse list[n] later without re-growth */
2746 output->has_empty_slot = 1;
2747 } else {
2748 o_addchr(output, '\0');
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002749 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002750 return n;
2751}
2752
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002753static char **expand_variables(char **argv, int or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002754{
2755 int n;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002756 char **list;
2757 char **v;
2758 o_string output = NULL_O_STRING;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002759
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002760 if (or_mask & 0x100) {
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002761 output.o_quote = 1; /* protect against globbing for "$var" */
2762 /* (unquoted $var will temporarily switch it off) */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002763 output.o_glob = 1;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002764 }
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002765
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002766 n = 0;
2767 v = argv;
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002768 while (*v) {
2769 n = expand_vars_to_list(&output, n, *v, (char)or_mask);
2770 v++;
2771 }
2772 debug_print_list("expand_variables", &output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002773
Denis Vlasenko82dfec32008-06-16 12:47:11 +00002774 /* output.data (malloced in one block) gets returned in "list" */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002775 list = o_finalize_list(&output, n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002776 debug_print_strings("expand_variables[1]", list);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002777 return list;
2778}
2779
Denis Vlasenko170435c2007-05-23 13:01:10 +00002780static char **expand_strvec_to_strvec(char **argv)
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002781{
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002782 return expand_variables(argv, 0x100);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002783}
2784
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002785/* Used for expansion of right hand of assignments */
Denis Vlasenko82dfec32008-06-16 12:47:11 +00002786/* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs
2787 * "v=/bin/c*" */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002788static char *expand_string_to_string(const char *str)
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002789{
2790 char *argv[2], **list;
2791
2792 argv[0] = (char*)str;
2793 argv[1] = NULL;
2794 list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002795 if (HUSH_DEBUG)
Denis Vlasenko170435c2007-05-23 13:01:10 +00002796 if (!list[0] || list[1])
2797 bb_error_msg_and_die("BUG in varexp2");
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002798 /* actually, just move string 2*sizeof(char*) bytes back */
2799 strcpy((char*)list, list[0]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00002800 debug_printf_expand("string_to_string='%s'\n", (char*)list);
2801 return (char*)list;
2802}
2803
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002804/* Used for "eval" builtin */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002805static char* expand_strvec_to_string(char **argv)
2806{
2807 char **list;
2808
2809 list = expand_variables(argv, 0x80);
2810 /* Convert all NULs to spaces */
2811 if (list[0]) {
2812 int n = 1;
2813 while (list[n]) {
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002814 if (HUSH_DEBUG)
Denis Vlasenko170435c2007-05-23 13:01:10 +00002815 if (list[n-1] + strlen(list[n-1]) + 1 != list[n])
2816 bb_error_msg_and_die("BUG in varexp3");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002817 list[n][-1] = ' '; /* TODO: or to G.ifs[0]? */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002818 n++;
2819 }
2820 }
2821 strcpy((char*)list, list[0]);
2822 debug_printf_expand("strvec_to_string='%s'\n", (char*)list);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002823 return (char*)list;
2824}
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002825
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002826
2827/* Used to get/check local shell variables */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002828static struct variable *get_local_var(const char *name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002829{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002830 struct variable *cur;
2831 int len;
Eric Andersenf72f5622001-05-15 23:21:41 +00002832
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002833 if (!name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002834 return NULL;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002835 len = strlen(name);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002836 for (cur = G.top_var; cur; cur = cur->next) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002837 if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=')
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002838 return cur;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002839 }
Eric Andersenf72f5622001-05-15 23:21:41 +00002840 return NULL;
2841}
2842
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002843/* str holds "NAME=VAL" and is expected to be malloced.
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002844 * We take ownership of it. */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002845static int set_local_var(char *str, int flg_export)
Eric Andersen78a7c992001-05-15 16:30:25 +00002846{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002847 struct variable *cur;
2848 char *value;
2849 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002850
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002851 value = strchr(str, '=');
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002852 if (!value) { /* not expected to ever happen? */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002853 free(str);
Eric Andersen99785762001-05-22 21:37:48 +00002854 return -1;
2855 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002856
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002857 name_len = value - str + 1; /* including '=' */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002858 cur = G.top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002859 while (1) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002860 if (strncmp(cur->varstr, str, name_len) != 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002861 if (!cur->next) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002862 /* Bail out. Note that now cur points
2863 * to last var in linked list */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002864 break;
Eric Andersen20a69a72001-05-15 17:24:44 +00002865 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002866 cur = cur->next;
2867 continue;
Eric Andersen20a69a72001-05-15 17:24:44 +00002868 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002869 /* We found an existing var with this name */
2870 *value = '\0';
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002871 if (cur->flg_read_only) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002872 bb_error_msg("%s: readonly variable", str);
2873 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002874 return -1;
2875 }
Denis Vlasenkof886fd22008-10-13 12:36:05 +00002876 debug_printf_env("%s: unsetenv '%s'\n", __func__, str);
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002877 unsetenv(str); /* just in case */
2878 *value = '=';
2879 if (strcmp(cur->varstr, str) == 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002880 free_and_exp:
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002881 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002882 goto exp;
2883 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002884 if (cur->max_len >= strlen(str)) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002885 /* This one is from startup env, reuse space */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002886 strcpy(cur->varstr, str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002887 goto free_and_exp;
2888 }
2889 /* max_len == 0 signifies "malloced" var, which we can
2890 * (and has to) free */
2891 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002892 free(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002893 cur->max_len = 0;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002894 goto set_str_and_exp;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002895 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002896
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002897 /* Not found - create next variable struct */
2898 cur->next = xzalloc(sizeof(*cur));
2899 cur = cur->next;
2900
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002901 set_str_and_exp:
2902 cur->varstr = str;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002903 exp:
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002904 if (flg_export)
2905 cur->flg_export = 1;
Denis Vlasenkof886fd22008-10-13 12:36:05 +00002906 if (cur->flg_export) {
2907 debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr);
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002908 return putenv(cur->varstr);
Denis Vlasenkof886fd22008-10-13 12:36:05 +00002909 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002910 return 0;
Eric Andersen20a69a72001-05-15 17:24:44 +00002911}
2912
Eric Andersenf72f5622001-05-15 23:21:41 +00002913static void unset_local_var(const char *name)
Eric Andersen20a69a72001-05-15 17:24:44 +00002914{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002915 struct variable *cur;
2916 struct variable *prev = prev; /* for gcc */
2917 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002918
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002919 if (!name)
2920 return;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002921 name_len = strlen(name);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002922 cur = G.top_var;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002923 while (cur) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002924 if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002925 if (cur->flg_read_only) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002926 bb_error_msg("%s: readonly variable", name);
Eric Andersen94ac2442001-05-22 19:05:18 +00002927 return;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002928 }
Denis Vlasenkof886fd22008-10-13 12:36:05 +00002929 /* prev is ok to use here because 1st variable, HUSH_VERSION,
2930 * is ro, and we cannot reach this code on the 1st pass */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002931 prev->next = cur->next;
Denis Vlasenkof886fd22008-10-13 12:36:05 +00002932 debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +00002933 bb_unsetenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002934 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002935 free(cur->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002936 free(cur);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002937 return;
Eric Andersenf72f5622001-05-15 23:21:41 +00002938 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002939 prev = cur;
2940 cur = cur->next;
Eric Andersen20a69a72001-05-15 17:24:44 +00002941 }
Eric Andersen78a7c992001-05-15 16:30:25 +00002942}
2943
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002944/* The src parameter allows us to peek forward to a possible &n syntax
Eric Andersen25f27032001-04-26 23:22:31 +00002945 * for file descriptor duplication, e.g., "2>&1".
2946 * Return code is 0 normally, 1 if a syntax error is detected in src.
2947 * Resource errors (in xmalloc) cause the process to exit */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002948static int setup_redirect(struct parse_context *ctx, int fd, redir_type style,
Eric Andersen25f27032001-04-26 23:22:31 +00002949 struct in_str *input)
2950{
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002951 struct command *command = ctx->command;
2952 struct redir_struct *redir = command->redirects;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002953 struct redir_struct *last_redir = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002954
2955 /* Create a new redir_struct and drop it onto the end of the linked list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002956 while (redir) {
2957 last_redir = redir;
2958 redir = redir->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002959 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00002960 redir = xzalloc(sizeof(struct redir_struct));
2961 /* redir->next = NULL; */
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00002962 /* redir->rd_filename = NULL; */
Eric Andersen25f27032001-04-26 23:22:31 +00002963 if (last_redir) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002964 last_redir->next = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002965 } else {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002966 command->redirects = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002967 }
2968
Denis Vlasenko55789c62008-06-18 16:30:42 +00002969 redir->rd_type = style;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002970 redir->fd = (fd == -1) ? redir_table[style].default_fd : fd;
Eric Andersen25f27032001-04-26 23:22:31 +00002971
2972 debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
2973
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002974 /* Check for a '2>&1' type redirect */
Eric Andersen25f27032001-04-26 23:22:31 +00002975 redir->dup = redirect_dup_num(input);
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002976 if (redir->dup == -2)
2977 return 1; /* syntax error */
Eric Andersen25f27032001-04-26 23:22:31 +00002978 if (redir->dup != -1) {
2979 /* Erik had a check here that the file descriptor in question
Eric Andersen83a2ae22001-05-07 17:59:25 +00002980 * is legit; I postpone that to "run time"
2981 * A "-" representation of "close me" shows up as a -3 here */
Eric Andersen25f27032001-04-26 23:22:31 +00002982 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
2983 } else {
2984 /* We do _not_ try to open the file that src points to,
2985 * since we need to return and let src be expanded first.
2986 * Set ctx->pending_redirect, so we know what to do at the
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002987 * end of the next parsed word. */
Eric Andersen25f27032001-04-26 23:22:31 +00002988 ctx->pending_redirect = redir;
2989 }
2990 return 0;
2991}
2992
Denis Vlasenkoac678ec2007-04-16 22:32:04 +00002993static struct pipe *new_pipe(void)
2994{
Eric Andersen25f27032001-04-26 23:22:31 +00002995 struct pipe *pi;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002996 pi = xzalloc(sizeof(struct pipe));
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002997 /*pi->followup = 0; - deliberately invalid value */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002998 /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */
Eric Andersen25f27032001-04-26 23:22:31 +00002999 return pi;
3000}
3001
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003002static void initialize_context(struct parse_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00003003{
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003004 memset(ctx, 0, sizeof(*ctx));
Denis Vlasenko1a735862007-05-23 00:32:25 +00003005 ctx->pipe = ctx->list_head = new_pipe();
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003006 /* Create the memory for command, roughly:
3007 * ctx->pipe->cmds = new struct command;
3008 * ctx->command = &ctx->pipe->cmds[0];
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003009 */
3010 done_command(ctx);
Eric Andersen25f27032001-04-26 23:22:31 +00003011}
3012
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003013/* If a reserved word is found and processed, parse context is modified
3014 * and 1 is returned.
3015 * Handles if, then, elif, else, fi, for, while, until, do, done.
Eric Andersen25f27032001-04-26 23:22:31 +00003016 * case, function, and select are obnoxious, save those for later.
3017 */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003018#if HAS_KEYWORDS
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003019struct reserved_combo {
3020 char literal[6];
3021 unsigned char res;
3022 unsigned char assignment_flag;
3023 int flag;
3024};
3025enum {
3026 FLAG_END = (1 << RES_NONE ),
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003027#if ENABLE_HUSH_IF
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003028 FLAG_IF = (1 << RES_IF ),
3029 FLAG_THEN = (1 << RES_THEN ),
3030 FLAG_ELIF = (1 << RES_ELIF ),
3031 FLAG_ELSE = (1 << RES_ELSE ),
3032 FLAG_FI = (1 << RES_FI ),
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003033#endif
3034#if ENABLE_HUSH_LOOPS
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003035 FLAG_FOR = (1 << RES_FOR ),
3036 FLAG_WHILE = (1 << RES_WHILE),
3037 FLAG_UNTIL = (1 << RES_UNTIL),
3038 FLAG_DO = (1 << RES_DO ),
3039 FLAG_DONE = (1 << RES_DONE ),
3040 FLAG_IN = (1 << RES_IN ),
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003041#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003042#if ENABLE_HUSH_CASE
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003043 FLAG_MATCH = (1 << RES_MATCH),
3044 FLAG_ESAC = (1 << RES_ESAC ),
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003045#endif
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003046 FLAG_START = (1 << RES_XXXX ),
3047};
3048
3049static const struct reserved_combo* match_reserved_word(o_string *word)
3050{
Eric Andersen25f27032001-04-26 23:22:31 +00003051 /* Mostly a list of accepted follow-up reserved words.
3052 * FLAG_END means we are done with the sequence, and are ready
3053 * to turn the compound list into a command.
3054 * FLAG_START means the word must start a new compound list.
3055 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003056 static const struct reserved_combo reserved_list[] = {
Denis Vlasenko06810332007-05-21 23:30:54 +00003057#if ENABLE_HUSH_IF
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003058 { "!", RES_NONE, NOT_ASSIGNMENT , 0 },
3059 { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START },
3060 { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
3061 { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN },
3062 { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI },
3063 { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END },
Denis Vlasenko06810332007-05-21 23:30:54 +00003064#endif
3065#if ENABLE_HUSH_LOOPS
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003066 { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START },
3067 { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START },
3068 { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START },
3069 { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO },
3070 { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE },
3071 { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END },
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003072#endif
3073#if ENABLE_HUSH_CASE
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003074 { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START },
3075 { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END },
Denis Vlasenko06810332007-05-21 23:30:54 +00003076#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003077 };
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003078 const struct reserved_combo *r;
3079
3080 for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) {
3081 if (strcmp(word->data, r->literal) == 0)
3082 return r;
3083 }
3084 return NULL;
3085}
3086static int reserved_word(o_string *word, struct parse_context *ctx)
3087{
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003088#if ENABLE_HUSH_CASE
3089 static const struct reserved_combo reserved_match = {
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003090 "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003091 };
3092#endif
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003093 const struct reserved_combo *r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003094
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003095 r = match_reserved_word(word);
3096 if (!r)
3097 return 0;
3098
3099 debug_printf("found reserved word %s, res %d\n", r->literal, r->res);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003100#if ENABLE_HUSH_CASE
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003101 if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE)
3102 /* "case word IN ..." - IN part starts first match part */
3103 r = &reserved_match;
3104 else
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003105#endif
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003106 if (r->flag == 0) { /* '!' */
3107 if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003108 syntax(NULL);
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003109 IF_HAS_KEYWORDS(ctx->ctx_res_w = RES_SNTX;)
Eric Andersen25f27032001-04-26 23:22:31 +00003110 }
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003111 ctx->ctx_inverted = 1;
Denis Vlasenko1a735862007-05-23 00:32:25 +00003112 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003113 }
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003114 if (r->flag & FLAG_START) {
3115 struct parse_context *new;
3116 debug_printf("push stack\n");
3117 new = xmalloc(sizeof(*new));
3118 *new = *ctx; /* physical copy */
3119 initialize_context(ctx);
3120 ctx->stack = new;
3121 } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) {
3122 syntax(NULL);
3123 ctx->ctx_res_w = RES_SNTX;
3124 return 1;
3125 }
3126 ctx->ctx_res_w = r->res;
3127 ctx->old_flag = r->flag;
3128 if (ctx->old_flag & FLAG_END) {
3129 struct parse_context *old;
3130 debug_printf("pop stack\n");
3131 done_pipe(ctx, PIPE_SEQ);
3132 old = ctx->stack;
3133 old->command->group = ctx->list_head;
Denis Vlasenko371de4a2008-10-14 12:43:13 +00003134 old->command->grp_type = GRP_NORMAL;
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003135 *ctx = *old; /* physical copy */
3136 free(old);
3137 }
3138 word->o_assignment = r->assignment_flag;
3139 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003140}
Denis Vlasenko06810332007-05-21 23:30:54 +00003141#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003142
Denis Vlasenkoddc8ae32008-10-14 12:50:34 +00003143//TODO: many, many callers don't check error from done_word()
3144
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003145/* Word is complete, look at it and update parsing context.
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003146 * Normal return is 0. Syntax errors return 1. */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003147static int done_word(o_string *word, struct parse_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00003148{
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003149 struct command *command = ctx->command;
Eric Andersen25f27032001-04-26 23:22:31 +00003150
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003151 debug_printf_parse("done_word entered: '%s' %p\n", word->data, command);
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003152 if (word->length == 0 && word->nonnull == 0) {
3153 debug_printf_parse("done_word return 0: true null, ignored\n");
3154 return 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003155 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003156 /* If this word wasn't an assignment, next ones definitely
3157 * can't be assignments. Even if they look like ones. */
3158 if (word->o_assignment != DEFINITELY_ASSIGNMENT
3159 && word->o_assignment != WORD_IS_KEYWORD
3160 ) {
3161 word->o_assignment = NOT_ASSIGNMENT;
3162 } else {
3163 if (word->o_assignment == DEFINITELY_ASSIGNMENT)
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003164 command->assignment_cnt++;
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003165 word->o_assignment = MAYBE_ASSIGNMENT;
3166 }
3167
Eric Andersen25f27032001-04-26 23:22:31 +00003168 if (ctx->pending_redirect) {
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003169 /* We do not glob in e.g. >*.tmp case. bash seems to glob here
3170 * only if run as "bash", not "sh" */
3171 ctx->pending_redirect->rd_filename = xstrdup(word->data);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003172 word->o_assignment = NOT_ASSIGNMENT;
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003173 debug_printf("word stored in rd_filename: '%s'\n", word->data);
Eric Andersen25f27032001-04-26 23:22:31 +00003174 } else {
Denis Vlasenko395ae452008-07-14 06:29:38 +00003175 /* "{ echo foo; } echo bar" - bad */
3176 /* NB: bash allows e.g. "if true; then { echo foo; } fi". TODO? */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003177 if (command->group) {
Denis Vlasenko395ae452008-07-14 06:29:38 +00003178 syntax(NULL);
3179 debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n");
3180 return 1;
3181 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003182#if HAS_KEYWORDS
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003183#if ENABLE_HUSH_CASE
Denis Vlasenko757361f2008-07-14 08:26:47 +00003184 if (ctx->ctx_dsemicolon
3185 && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */
3186 ) {
Denis Vlasenko395ae452008-07-14 06:29:38 +00003187 /* already done when ctx_dsemicolon was set to 1: */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003188 /* ctx->ctx_res_w = RES_MATCH; */
3189 ctx->ctx_dsemicolon = 0;
3190 } else
3191#endif
3192
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003193 if (!command->argv /* if it's the first word... */
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003194#if ENABLE_HUSH_LOOPS
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003195 && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */
3196 && ctx->ctx_res_w != RES_IN
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003197#endif
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003198 ) {
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003199 debug_printf_parse(": checking '%s' for reserved-ness\n", word->data);
3200 if (reserved_word(word, ctx)) {
3201 o_reset(word);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003202 debug_printf_parse("done_word return %d\n", (ctx->ctx_res_w == RES_SNTX));
3203 return (ctx->ctx_res_w == RES_SNTX);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003204 }
Eric Andersen25f27032001-04-26 23:22:31 +00003205 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003206#endif
Denis Vlasenkoff182a32008-07-05 20:29:59 +00003207 if (word->nonnull /* word had "xx" or 'xx' at least as part of it. */
Denis Vlasenko55789c62008-06-18 16:30:42 +00003208 /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */
3209 && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL)
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003210 /* (otherwise it's known to be not empty and is already safe) */
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003211 ) {
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003212 /* exclude "$@" - it can expand to no word despite "" */
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00003213 char *p = word->data;
3214 while (p[0] == SPECIAL_VAR_SYMBOL
3215 && (p[1] & 0x7f) == '@'
3216 && p[2] == SPECIAL_VAR_SYMBOL
3217 ) {
3218 p += 3;
3219 }
3220 if (p == word->data || p[0] != '\0') {
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003221 /* saw no "$@", or not only "$@" but some
3222 * real text is there too */
3223 /* insert "empty variable" reference, this makes
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00003224 * e.g. "", $empty"" etc to not disappear */
3225 o_addchr(word, SPECIAL_VAR_SYMBOL);
3226 o_addchr(word, SPECIAL_VAR_SYMBOL);
3227 }
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00003228 }
Denis Vlasenko22d10a02008-10-13 08:53:43 +00003229 command->argv = add_string_to_strings(command->argv, xstrdup(word->data));
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003230 debug_print_strings("word appended to argv", command->argv);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003231 }
Eric Andersen25f27032001-04-26 23:22:31 +00003232
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003233 o_reset(word);
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003234 ctx->pending_redirect = NULL;
3235
Denis Vlasenko06810332007-05-21 23:30:54 +00003236#if ENABLE_HUSH_LOOPS
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003237 /* Force FOR to have just one word (variable name) */
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003238 /* NB: basically, this makes hush see "for v in ..." syntax as if
3239 * as it is "for v; in ...". FOR and IN become two pipe structs
3240 * in parse tree. */
3241 if (ctx->ctx_res_w == RES_FOR) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003242//TODO: check that command->argv[0] is a valid variable name!
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003243 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003244 }
Denis Vlasenko06810332007-05-21 23:30:54 +00003245#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003246#if ENABLE_HUSH_CASE
3247 /* Force CASE to have just one word */
3248 if (ctx->ctx_res_w == RES_CASE) {
3249 done_pipe(ctx, PIPE_SEQ);
3250 }
3251#endif
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003252 debug_printf_parse("done_word return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003253 return 0;
3254}
3255
Denis Vlasenko6eaf8de2008-06-17 12:09:21 +00003256/* Command (member of a pipe) is complete. The only possible error here
3257 * is out of memory, in which case xmalloc exits. */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003258static int done_command(struct parse_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00003259{
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003260 /* The command is really already in the pipe structure, so
3261 * advance the pipe counter and make a new, null command. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003262 struct pipe *pi = ctx->pipe;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003263 struct command *command = ctx->command;
Eric Andersen25f27032001-04-26 23:22:31 +00003264
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003265 if (command) {
3266 if (command->group == NULL
3267 && command->argv == NULL
3268 && command->redirects == NULL
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003269 ) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003270 debug_printf_parse("done_command: skipping null cmd, num_cmds=%d\n", pi->num_cmds);
3271 return pi->num_cmds;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003272 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003273 pi->num_cmds++;
3274 debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds);
Eric Andersen25f27032001-04-26 23:22:31 +00003275 } else {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003276 debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds);
Eric Andersen25f27032001-04-26 23:22:31 +00003277 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003278
3279 /* Only real trickiness here is that the uncommitted
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003280 * command structure is not counted in pi->num_cmds. */
3281 pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1));
3282 command = &pi->cmds[pi->num_cmds];
3283 memset(command, 0, sizeof(*command));
Eric Andersen25f27032001-04-26 23:22:31 +00003284
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003285 ctx->command = command;
Eric Andersen25f27032001-04-26 23:22:31 +00003286 /* but ctx->pipe and ctx->list_head remain unchanged */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003287
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003288 return pi->num_cmds; /* used only for 0/nonzero check */
Eric Andersen25f27032001-04-26 23:22:31 +00003289}
3290
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003291static void done_pipe(struct parse_context *ctx, pipe_style type)
Eric Andersen25f27032001-04-26 23:22:31 +00003292{
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003293 int not_null;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003294
3295 debug_printf_parse("done_pipe entered, followup %d\n", type);
Denis Vlasenko395ae452008-07-14 06:29:38 +00003296 /* Close previous command */
3297 not_null = done_command(ctx);
Eric Andersen25f27032001-04-26 23:22:31 +00003298 ctx->pipe->followup = type;
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003299 IF_HAS_KEYWORDS(ctx->pipe->pi_inverted = ctx->ctx_inverted;)
3300 IF_HAS_KEYWORDS(ctx->ctx_inverted = 0;)
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003301 IF_HAS_KEYWORDS(ctx->pipe->res_word = ctx->ctx_res_w;)
Denis Vlasenko395ae452008-07-14 06:29:38 +00003302
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003303 /* Without this check, even just <enter> on command line generates
3304 * tree of three NOPs (!). Which is harmless but annoying.
Denis Vlasenko6eaf8de2008-06-17 12:09:21 +00003305 * IOW: it is safe to do it unconditionally.
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003306 * RES_NONE case is for "for a in; do ..." (empty IN set)
3307 * to work, possibly other cases too. */
3308 if (not_null IF_HAS_KEYWORDS(|| ctx->ctx_res_w != RES_NONE)) {
Denis Vlasenko395ae452008-07-14 06:29:38 +00003309 struct pipe *new_p;
3310 debug_printf_parse("done_pipe: adding new pipe: "
Denis Vlasenko757361f2008-07-14 08:26:47 +00003311 "not_null:%d ctx->ctx_res_w:%d\n",
Denis Vlasenko395ae452008-07-14 06:29:38 +00003312 not_null, ctx->ctx_res_w);
3313 new_p = new_pipe();
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003314 ctx->pipe->next = new_p;
3315 ctx->pipe = new_p;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003316 ctx->command = NULL; /* needed! */
Denis Vlasenko395ae452008-07-14 06:29:38 +00003317 /* RES_THEN, RES_DO etc are "sticky" -
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003318 * they remain set for commands inside if/while.
3319 * This is used to control execution.
3320 * RES_FOR and RES_IN are NOT sticky (needed to support
3321 * cases where variable or value happens to match a keyword):
3322 */
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003323#if ENABLE_HUSH_LOOPS
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003324 if (ctx->ctx_res_w == RES_FOR
3325 || ctx->ctx_res_w == RES_IN)
3326 ctx->ctx_res_w = RES_NONE;
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003327#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003328#if ENABLE_HUSH_CASE
3329 if (ctx->ctx_res_w == RES_MATCH)
3330 ctx->ctx_res_w = RES_CASEI;
3331#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003332 /* Create the memory for command, roughly:
3333 * ctx->pipe->cmds = new struct command;
3334 * ctx->command = &ctx->pipe->cmds[0];
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003335 */
3336 done_command(ctx);
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003337 }
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003338 debug_printf_parse("done_pipe return\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003339}
3340
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003341/* Peek ahead in the in_str to find out if we have a "&n" construct,
Eric Andersen25f27032001-04-26 23:22:31 +00003342 * as in "2>&1", that represents duplicating a file descriptor.
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003343 * Return either -2 (syntax error), -1 (no &), or the number found.
Eric Andersen25f27032001-04-26 23:22:31 +00003344 */
3345static int redirect_dup_num(struct in_str *input)
3346{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003347 int ch, d = 0, ok = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003348 ch = i_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003349 if (ch != '&') return -1;
3350
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003351 i_getch(input); /* get the & */
3352 ch = i_peek(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003353 if (ch == '-') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003354 i_getch(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003355 return -3; /* "-" represents "close me" */
3356 }
3357 while (isdigit(ch)) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00003358 d = d*10 + (ch-'0');
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003359 ok = 1;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003360 i_getch(input);
3361 ch = i_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003362 }
3363 if (ok) return d;
3364
Manuel Novoa III cad53642003-03-19 09:13:01 +00003365 bb_error_msg("ambiguous redirect");
Eric Andersen25f27032001-04-26 23:22:31 +00003366 return -2;
3367}
3368
3369/* If a redirect is immediately preceded by a number, that number is
3370 * supposed to tell which file descriptor to redirect. This routine
3371 * looks for such preceding numbers. In an ideal world this routine
3372 * needs to handle all the following classes of redirects...
3373 * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
3374 * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
3375 * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
3376 * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
3377 * A -1 output from this program means no valid number was found, so the
3378 * caller should use the appropriate default for this redirection.
3379 */
3380static int redirect_opt_num(o_string *o)
3381{
3382 int num;
3383
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003384 if (o->length == 0)
3385 return -1;
3386 for (num = 0; num < o->length; num++) {
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00003387 if (!isdigit(o->data[num])) {
Eric Andersen25f27032001-04-26 23:22:31 +00003388 return -1;
3389 }
3390 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003391 num = atoi(o->data);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003392 o_reset(o);
Eric Andersen25f27032001-04-26 23:22:31 +00003393 return num;
3394}
3395
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003396#if ENABLE_HUSH_TICK
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00003397static FILE *generate_stream_from_list(struct pipe *head)
Eric Andersen25f27032001-04-26 23:22:31 +00003398{
3399 FILE *pf;
Eric Andersen25f27032001-04-26 23:22:31 +00003400 int pid, channel[2];
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003401
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +00003402 xpipe(channel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003403/* *** NOMMU WARNING *** */
3404/* By using vfork here, we suspend parent till child exits or execs.
3405 * If child will not do it before it fills the pipe, it can block forever
3406 * in write(STDOUT_FILENO), and parent (shell) will be also stuck.
Denis Vlasenko3fe4f982008-06-09 16:02:39 +00003407 * Try this script:
3408 * yes "0123456789012345678901234567890" | dd bs=32 count=64k >TESTFILE
3409 * huge=`cat TESTFILE` # will block here forever
3410 * echo OK
Denis Vlasenko05743d72008-02-10 12:10:08 +00003411 */
Denis Vlasenko82604e92008-07-01 15:59:42 +00003412 pid = BB_MMU ? fork() : vfork();
3413 if (pid < 0)
3414 bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork");
Denis Vlasenko05743d72008-02-10 12:10:08 +00003415 if (pid == 0) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00003416 if (ENABLE_HUSH_JOB)
3417 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenko284d0fa2008-02-16 13:18:17 +00003418 close(channel[0]); /* NB: close _first_, then move fd! */
Denis Vlasenko05743d72008-02-10 12:10:08 +00003419 xmove_fd(channel[1], 1);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003420 /* Prevent it from trying to handle ctrl-z etc */
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003421#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00003422 G.run_list_level = 1;
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003423#endif
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003424 /* Process substitution is not considered to be usual
3425 * 'command execution'.
3426 * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. */
3427 /* Not needed, we are relying on it being disabled
3428 * everywhere outside actual command execution. */
3429 /*set_jobctrl_sighandler(SIG_IGN);*/
3430 set_misc_sighandler(SIG_DFL);
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003431 /* Freeing 'head' here would break NOMMU. */
3432 _exit(run_list(head));
Eric Andersen25f27032001-04-26 23:22:31 +00003433 }
Eric Andersen25f27032001-04-26 23:22:31 +00003434 close(channel[1]);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003435 pf = fdopen(channel[0], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00003436 return pf;
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003437 /* 'head' is freed by the caller */
Eric Andersen25f27032001-04-26 23:22:31 +00003438}
3439
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003440/* Return code is exit status of the process that is run. */
Denis Vlasenko68404f12008-03-17 09:00:54 +00003441static int process_command_subs(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +00003442 struct in_str *input,
3443 const char *subst_end)
Eric Andersen25f27032001-04-26 23:22:31 +00003444{
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003445 int retcode, ch, eol_cnt;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003446 o_string result = NULL_O_STRING;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003447 struct parse_context inner;
Eric Andersen25f27032001-04-26 23:22:31 +00003448 FILE *p;
3449 struct in_str pipe_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003450
Eric Andersen25f27032001-04-26 23:22:31 +00003451 initialize_context(&inner);
3452
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003453 /* Recursion to generate command */
Eric Andersen25f27032001-04-26 23:22:31 +00003454 retcode = parse_stream(&result, &inner, input, subst_end);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003455 if (retcode != 0)
3456 return retcode; /* syntax error or EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003457 done_word(&result, &inner);
3458 done_pipe(&inner, PIPE_SEQ);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003459 o_free(&result);
Eric Andersen25f27032001-04-26 23:22:31 +00003460
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003461 p = generate_stream_from_list(inner.list_head);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003462 if (p == NULL)
3463 return 1;
Denis Vlasenkoa0898172007-10-01 09:59:01 +00003464 close_on_exec_on(fileno(p));
Eric Andersen25f27032001-04-26 23:22:31 +00003465 setup_file_in_str(&pipe_str, p);
3466
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003467 /* Now send results of command back into original context */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003468 eol_cnt = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003469 while ((ch = i_getch(&pipe_str)) != EOF) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003470 if (ch == '\n') {
3471 eol_cnt++;
3472 continue;
3473 }
3474 while (eol_cnt) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00003475 o_addchr(dest, '\n');
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003476 eol_cnt--;
3477 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003478 o_addQchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003479 }
3480
3481 debug_printf("done reading from pipe, pclose()ing\n");
3482 /* This is the step that wait()s for the child. Should be pretty
3483 * safe, since we just read an EOF from its stdout. We could try
Denis Vlasenko262d7652007-05-20 21:52:49 +00003484 * to do better, by using wait(), and keeping track of background jobs
Eric Andersen25f27032001-04-26 23:22:31 +00003485 * at the same time. That would be a lot of work, and contrary
3486 * to the KISS philosophy of this program. */
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003487 retcode = fclose(p);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003488 free_pipe_list(inner.list_head, /* indent: */ 0);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003489 debug_printf("closed FILE from child, retcode=%d\n", retcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003490 return retcode;
3491}
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003492#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003493
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003494static int parse_group(o_string *dest, struct parse_context *ctx,
Eric Andersen25f27032001-04-26 23:22:31 +00003495 struct in_str *input, int ch)
3496{
Denis Vlasenko371de4a2008-10-14 12:43:13 +00003497 /* dest contains characters seen prior to ( or {.
3498 * Typically it's empty, but for functions defs,
3499 * it contains function name (without '()'). */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003500 int rcode;
3501 const char *endch = NULL;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003502 struct parse_context sub;
3503 struct command *command = ctx->command;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003504
3505 debug_printf_parse("parse_group entered\n");
Denis Vlasenko371de4a2008-10-14 12:43:13 +00003506#if ENABLE_HUSH_FUNCTIONS
3507 if (ch == 'F') { /* function definition? */
3508 bb_error_msg("aha '%s' is a function, parsing it...", dest->data);
3509 //command->fname = dest->data;
3510 command->grp_type = GRP_FUNCTION;
3511//TODO: review every o_reset() location... do they handle all o_string fields correctly?
3512 memset(dest, 0, sizeof(*dest));
3513 }
3514#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003515 if (command->argv /* word [word](... */
3516 || dest->length /* word(... */
3517 || dest->nonnull /* ""(... */
3518 ) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003519 syntax(NULL);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003520 debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n");
3521 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003522 }
3523 initialize_context(&sub);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003524 endch = "}";
3525 if (ch == '(') {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003526 endch = ")";
Denis Vlasenko371de4a2008-10-14 12:43:13 +00003527 command->grp_type = GRP_SUBSHELL;
Eric Andersen25f27032001-04-26 23:22:31 +00003528 }
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003529 rcode = parse_stream(dest, &sub, input, endch);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003530 if (rcode == 0) {
3531 done_word(dest, &sub); /* finish off the final word in the subcontext */
3532 done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003533 command->group = sub.list_head;
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003534 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003535 debug_printf_parse("parse_group return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003536 return rcode;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003537 /* command remains "open", available for possible redirects */
Eric Andersen25f27032001-04-26 23:22:31 +00003538}
3539
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003540/* Basically useful version until someone wants to get fancier,
Eric Andersen25f27032001-04-26 23:22:31 +00003541 * see the bash man page under "Parameter Expansion" */
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00003542static const char *lookup_param(const char *src)
Eric Andersen25f27032001-04-26 23:22:31 +00003543{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003544 struct variable *var = get_local_var(src);
3545 if (var)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003546 return strchr(var->varstr, '=') + 1;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003547 return NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003548}
3549
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003550#if ENABLE_HUSH_TICK
3551/* Subroutines for copying $(...) and `...` things */
3552static void add_till_backquote(o_string *dest, struct in_str *input);
3553/* '...' */
3554static void add_till_single_quote(o_string *dest, struct in_str *input)
3555{
3556 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003557 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003558 if (ch == EOF)
3559 break;
3560 if (ch == '\'')
3561 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003562 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003563 }
3564}
3565/* "...\"...`..`...." - do we need to handle "...$(..)..." too? */
3566static void add_till_double_quote(o_string *dest, struct in_str *input)
3567{
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 == '"')
3571 break;
3572 if (ch == '\\') { /* \x. Copy both chars. */
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003573 o_addchr(dest, ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003574 ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003575 }
3576 if (ch == EOF)
3577 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003578 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003579 if (ch == '`') {
3580 add_till_backquote(dest, input);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003581 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003582 continue;
3583 }
Denis Vlasenko5703c222008-06-15 11:49:42 +00003584 //if (ch == '$') ...
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003585 }
3586}
3587/* Process `cmd` - copy contents until "`" is seen. Complicated by
3588 * \` quoting.
3589 * "Within the backquoted style of command substitution, backslash
3590 * shall retain its literal meaning, except when followed by: '$', '`', or '\'.
3591 * The search for the matching backquote shall be satisfied by the first
3592 * backquote found without a preceding backslash; during this search,
3593 * if a non-escaped backquote is encountered within a shell comment,
3594 * a here-document, an embedded command substitution of the $(command)
3595 * form, or a quoted string, undefined results occur. A single-quoted
3596 * or double-quoted string that begins, but does not end, within the
3597 * "`...`" sequence produces undefined results."
3598 * Example Output
3599 * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST
3600 */
3601static void add_till_backquote(o_string *dest, struct in_str *input)
3602{
3603 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003604 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003605 if (ch == '`')
3606 break;
3607 if (ch == '\\') { /* \x. Copy both chars unless it is \` */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003608 int ch2 = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003609 if (ch2 != '`' && ch2 != '$' && ch2 != '\\')
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003610 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003611 ch = ch2;
3612 }
3613 if (ch == EOF)
3614 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003615 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003616 }
3617}
3618/* Process $(cmd) - copy contents until ")" is seen. Complicated by
3619 * quoting and nested ()s.
3620 * "With the $(command) style of command substitution, all characters
3621 * following the open parenthesis to the matching closing parenthesis
3622 * constitute the command. Any valid shell script can be used for command,
3623 * except a script consisting solely of redirections which produces
3624 * unspecified results."
3625 * Example Output
3626 * echo $(echo '(TEST)' BEST) (TEST) BEST
3627 * echo $(echo 'TEST)' BEST) TEST) BEST
3628 * echo $(echo \(\(TEST\) BEST) ((TEST) BEST
3629 */
3630static void add_till_closing_curly_brace(o_string *dest, struct in_str *input)
3631{
3632 int count = 0;
3633 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003634 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003635 if (ch == EOF)
3636 break;
3637 if (ch == '(')
3638 count++;
3639 if (ch == ')')
3640 if (--count < 0)
3641 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003642 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003643 if (ch == '\'') {
3644 add_till_single_quote(dest, input);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003645 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003646 continue;
3647 }
3648 if (ch == '"') {
3649 add_till_double_quote(dest, input);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003650 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003651 continue;
3652 }
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003653 if (ch == '\\') { /* \x. Copy verbatim. Important for \(, \) */
3654 ch = i_getch(input);
3655 if (ch == EOF)
3656 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003657 o_addchr(dest, ch);
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003658 continue;
3659 }
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003660 }
3661}
3662#endif /* ENABLE_HUSH_TICK */
3663
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003664/* Return code: 0 for OK, 1 for syntax error */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003665static int handle_dollar(o_string *dest, struct in_str *input)
Eric Andersen25f27032001-04-26 23:22:31 +00003666{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003667 int ch = i_peek(input); /* first character after the $ */
Denis Vlasenkoff097622007-10-01 10:00:45 +00003668 unsigned char quote_mask = dest->o_quote ? 0x80 : 0;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003669
3670 debug_printf_parse("handle_dollar entered: ch='%c'\n", ch);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003671 if (isalpha(ch)) {
Denis Vlasenkod4981312008-07-31 10:34:48 +00003672 i_getch(input);
3673 make_var:
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003674 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003675 while (1) {
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003676 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003677 o_addchr(dest, ch | quote_mask);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003678 quote_mask = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003679 ch = i_peek(input);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003680 if (!isalnum(ch) && ch != '_')
3681 break;
Denis Vlasenkod4981312008-07-31 10:34:48 +00003682 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003683 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003684 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003685 } else if (isdigit(ch)) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003686 make_one_char_var:
Denis Vlasenkod4981312008-07-31 10:34:48 +00003687 i_getch(input);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003688 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003689 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003690 o_addchr(dest, ch | quote_mask);
3691 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003692 } else switch (ch) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003693 case '$': /* pid */
3694 case '!': /* last bg pid */
3695 case '?': /* last exit code */
3696 case '#': /* number of args */
3697 case '*': /* args */
3698 case '@': /* args */
3699 goto make_one_char_var;
Eric Andersen25f27032001-04-26 23:22:31 +00003700 case '{':
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003701 o_addchr(dest, SPECIAL_VAR_SYMBOL);
3702 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003703 /* XXX maybe someone will try to escape the '}' */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003704 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003705 ch = i_getch(input);
Denis Vlasenkob0550012007-05-24 12:18:16 +00003706 if (ch == '}')
3707 break;
3708 if (!isalnum(ch) && ch != '_') {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003709 syntax("unterminated ${name}");
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003710 debug_printf_parse("handle_dollar return 1: unterminated ${name}\n");
3711 return 1;
3712 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003713 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003714 o_addchr(dest, ch | quote_mask);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003715 quote_mask = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003716 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003717 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003718 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003719#if ENABLE_HUSH_TICK
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003720 case '(': {
3721 //int pos = dest->length;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003722 i_getch(input);
3723 o_addchr(dest, SPECIAL_VAR_SYMBOL);
3724 o_addchr(dest, quote_mask | '`');
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003725 add_till_closing_curly_brace(dest, input);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00003726 //debug_printf_subst("SUBST RES2 '%s'\n", dest->data + pos);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003727 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003728 break;
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003729 }
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003730#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003731 case '_':
Denis Vlasenkod4981312008-07-31 10:34:48 +00003732 i_getch(input);
3733 ch = i_peek(input);
3734 if (isalnum(ch)) { /* it's $_name or $_123 */
3735 ch = '_';
3736 goto make_var;
3737 }
3738 /* else: it's $_ */
3739 case '-':
Eric Andersen25f27032001-04-26 23:22:31 +00003740 /* still unhandled, but should be eventually */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003741 bb_error_msg("unhandled syntax: $%c", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003742 return 1;
3743 break;
3744 default:
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003745 o_addQchr(dest, '$');
Eric Andersen25f27032001-04-26 23:22:31 +00003746 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003747 debug_printf_parse("handle_dollar return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003748 return 0;
3749}
3750
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003751/* Scan input, call done_word() whenever full IFS delimited word was seen.
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003752 * Call done_pipe if '\n' was seen (and end_trigger != NULL).
3753 * Return code is 0 if end_trigger char is met,
3754 * -1 on EOF (but if end_trigger == NULL then return 0),
Denis Vlasenko55789c62008-06-18 16:30:42 +00003755 * 1 for syntax error */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003756static int parse_stream(o_string *dest, struct parse_context *ctx,
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003757 struct in_str *input, const char *end_trigger)
Eric Andersen25f27032001-04-26 23:22:31 +00003758{
"Vladimir N. Oleynik"4ccd2b42006-01-31 09:27:48 +00003759 int ch, m;
Eric Andersen25f27032001-04-26 23:22:31 +00003760 int redir_fd;
3761 redir_type redir_style;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003762 int shadow_quote = dest->o_quote;
Eric Andersen25f27032001-04-26 23:22:31 +00003763 int next;
3764
Denis Vlasenkoff097622007-10-01 10:00:45 +00003765 /* Only double-quote state is handled in the state variable dest->o_quote.
Eric Andersen25f27032001-04-26 23:22:31 +00003766 * A single-quote triggers a bypass of the main loop until its mate is
Denis Vlasenkoff097622007-10-01 10:00:45 +00003767 * found. When recursing, quote state is passed in via dest->o_quote. */
Eric Andersen25f27032001-04-26 23:22:31 +00003768
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003769 debug_printf_parse("parse_stream entered, end_trigger='%s' dest->o_assignment:%d\n", end_trigger, dest->o_assignment);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003770
Denis Vlasenko1a735862007-05-23 00:32:25 +00003771 while (1) {
Denis Vlasenko1a735862007-05-23 00:32:25 +00003772 m = CHAR_IFS;
3773 next = '\0';
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003774 ch = i_getch(input);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003775 if (ch != EOF) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00003776 m = G.charmap[ch];
Denis Vlasenko55789c62008-06-18 16:30:42 +00003777 if (ch != '\n') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003778 next = i_peek(input);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003779 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003780 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003781 debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n",
Denis Vlasenkoff097622007-10-01 10:00:45 +00003782 ch, ch, m, dest->o_quote);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003783 if (m == CHAR_ORDINARY
Denis Vlasenko55789c62008-06-18 16:30:42 +00003784 || (m != CHAR_SPECIAL && shadow_quote)
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003785 ) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00003786 if (ch == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003787 syntax("unterminated \"");
Denis Vlasenko170435c2007-05-23 13:01:10 +00003788 debug_printf_parse("parse_stream return 1: unterminated \"\n");
3789 return 1;
3790 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003791 o_addQchr(dest, ch);
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003792 if ((dest->o_assignment == MAYBE_ASSIGNMENT
3793 || dest->o_assignment == WORD_IS_KEYWORD)
Denis Vlasenko55789c62008-06-18 16:30:42 +00003794 && ch == '='
3795 && is_assignment(dest->data)
3796 ) {
3797 dest->o_assignment = DEFINITELY_ASSIGNMENT;
3798 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003799 continue;
3800 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003801 if (m == CHAR_IFS) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003802 if (done_word(dest, ctx)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003803 debug_printf_parse("parse_stream return 1: done_word!=0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003804 return 1;
Eric Andersenaac75e52001-04-30 18:18:45 +00003805 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003806 if (ch == EOF)
3807 break;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003808 /* If we aren't performing a substitution, treat
3809 * a newline as a command separator.
3810 * [why we don't handle it exactly like ';'? --vda] */
3811 if (end_trigger && ch == '\n') {
Denis Vlasenkof1736072008-07-31 10:09:26 +00003812#if ENABLE_HUSH_CASE
3813 /* "case ... in <newline> word) ..." -
3814 * newlines are ignored (but ';' wouldn't be) */
3815 if (dest->length == 0 // && argv[0] == NULL
3816 && ctx->ctx_res_w == RES_MATCH
3817 ) {
3818 continue;
3819 }
3820#endif
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003821 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003822 dest->o_assignment = MAYBE_ASSIGNMENT;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003823 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003824 }
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003825 if (end_trigger) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00003826 if (!shadow_quote && strchr(end_trigger, ch)) {
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003827 /* Special case: (...word) makes last word terminate,
3828 * as if ';' is seen */
3829 if (ch == ')') {
3830 done_word(dest, ctx);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003831//err chk?
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003832 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003833 dest->o_assignment = MAYBE_ASSIGNMENT;
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003834 }
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003835 if (!HAS_KEYWORDS
3836 IF_HAS_KEYWORDS(|| (ctx->ctx_res_w == RES_NONE && ctx->old_flag == 0))
3837 ) {
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003838 debug_printf_parse("parse_stream return 0: end_trigger char found\n");
3839 return 0;
3840 }
3841 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003842 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003843 if (m == CHAR_IFS)
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003844 continue;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003845
3846 if (dest->o_assignment == MAYBE_ASSIGNMENT) {
3847 /* ch is a special char and thus this word
3848 * cannot be an assignment: */
3849 dest->o_assignment = NOT_ASSIGNMENT;
3850 }
3851
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003852 switch (ch) {
Eric Andersen25f27032001-04-26 23:22:31 +00003853 case '#':
Denis Vlasenko55789c62008-06-18 16:30:42 +00003854 if (dest->length == 0 && !shadow_quote) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003855 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003856 ch = i_peek(input);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003857 if (ch == EOF || ch == '\n')
3858 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003859 i_getch(input);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003860 }
Eric Andersen25f27032001-04-26 23:22:31 +00003861 } else {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003862 o_addQchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003863 }
3864 break;
3865 case '\\':
3866 if (next == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003867 syntax("\\<eof>");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003868 debug_printf_parse("parse_stream return 1: \\<eof>\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003869 return 1;
3870 }
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003871 /* bash:
3872 * "The backslash retains its special meaning [in "..."]
3873 * only when followed by one of the following characters:
3874 * $, `, ", \, or <newline>. A double quote may be quoted
3875 * within double quotes by preceding it with a backslash.
3876 * If enabled, history expansion will be performed unless
3877 * an ! appearing in double quotes is escaped using
3878 * a backslash. The backslash preceding the ! is not removed."
3879 */
Denis Vlasenko55789c62008-06-18 16:30:42 +00003880 if (shadow_quote) { //NOT SURE dest->o_quote) {
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003881 if (strchr("$`\"\\", next) != NULL) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003882 o_addqchr(dest, i_getch(input));
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003883 } else {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003884 o_addqchr(dest, '\\');
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003885 }
3886 } else {
3887 o_addchr(dest, '\\');
3888 o_addchr(dest, i_getch(input));
3889 }
Eric Andersen25f27032001-04-26 23:22:31 +00003890 break;
3891 case '$':
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003892 if (handle_dollar(dest, input) != 0) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003893 debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n");
3894 return 1;
3895 }
Eric Andersen25f27032001-04-26 23:22:31 +00003896 break;
3897 case '\'':
3898 dest->nonnull = 1;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003899 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003900 ch = i_getch(input);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003901 if (ch == EOF) {
3902 syntax("unterminated '");
3903 debug_printf_parse("parse_stream return 1: unterminated '\n");
3904 return 1;
3905 }
3906 if (ch == '\'')
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003907 break;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003908 if (dest->o_assignment == NOT_ASSIGNMENT)
3909 o_addqchr(dest, ch);
3910 else
3911 o_addchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003912 }
Eric Andersen25f27032001-04-26 23:22:31 +00003913 break;
3914 case '"':
3915 dest->nonnull = 1;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003916 shadow_quote ^= 1; /* invert */
3917 if (dest->o_assignment == NOT_ASSIGNMENT)
3918 dest->o_quote ^= 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003919 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003920#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003921 case '`': {
3922 //int pos = dest->length;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003923 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003924 o_addchr(dest, shadow_quote /*or dest->o_quote??*/ ? 0x80 | '`' : '`');
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003925 add_till_backquote(dest, input);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003926 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00003927 //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos);
Eric Andersen25f27032001-04-26 23:22:31 +00003928 break;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003929 }
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003930#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003931 case '>':
3932 redir_fd = redirect_opt_num(dest);
3933 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003934 redir_style = REDIRECT_OVERWRITE;
Eric Andersen25f27032001-04-26 23:22:31 +00003935 if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003936 redir_style = REDIRECT_APPEND;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003937 i_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003938 }
3939#if 0
3940 else if (next == '(') {
3941 syntax(">(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003942 debug_printf_parse("parse_stream return 1: >(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003943 return 1;
3944 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003945#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003946 setup_redirect(ctx, redir_fd, redir_style, input);
3947 break;
3948 case '<':
3949 redir_fd = redirect_opt_num(dest);
3950 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003951 redir_style = REDIRECT_INPUT;
Eric Andersen25f27032001-04-26 23:22:31 +00003952 if (next == '<') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003953 redir_style = REDIRECT_HEREIS;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003954 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003955 } else if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003956 redir_style = REDIRECT_IO;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003957 i_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003958 }
3959#if 0
3960 else if (next == '(') {
3961 syntax("<(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003962 debug_printf_parse("parse_stream return 1: <(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003963 return 1;
3964 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003965#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003966 setup_redirect(ctx, redir_fd, redir_style, input);
3967 break;
3968 case ';':
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003969#if ENABLE_HUSH_CASE
3970 case_semi:
3971#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003972 done_word(dest, ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003973 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003974#if ENABLE_HUSH_CASE
3975 /* Eat multiple semicolons, detect
3976 * whether it means something special */
3977 while (1) {
3978 ch = i_peek(input);
3979 if (ch != ';')
3980 break;
3981 i_getch(input);
3982 if (ctx->ctx_res_w == RES_CASEI) {
3983 ctx->ctx_dsemicolon = 1;
3984 ctx->ctx_res_w = RES_MATCH;
3985 break;
3986 }
3987 }
3988#endif
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003989 new_cmd:
3990 /* We just finished a cmd. New one may start
3991 * with an assignment */
3992 dest->o_assignment = MAYBE_ASSIGNMENT;
Eric Andersen25f27032001-04-26 23:22:31 +00003993 break;
3994 case '&':
3995 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003996 if (next == '&') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003997 i_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003998 done_pipe(ctx, PIPE_AND);
Eric Andersen25f27032001-04-26 23:22:31 +00003999 } else {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004000 done_pipe(ctx, PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00004001 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00004002 goto new_cmd;
Eric Andersen25f27032001-04-26 23:22:31 +00004003 case '|':
4004 done_word(dest, ctx);
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00004005#if ENABLE_HUSH_CASE
4006 if (ctx->ctx_res_w == RES_MATCH)
Denis Vlasenkof1736072008-07-31 10:09:26 +00004007 break; /* we are in case's "word | word)" */
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00004008#endif
Denis Vlasenko2b576b82008-08-04 00:46:07 +00004009 if (next == '|') { /* || */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00004010 i_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004011 done_pipe(ctx, PIPE_OR);
Eric Andersen25f27032001-04-26 23:22:31 +00004012 } else {
4013 /* we could pick up a file descriptor choice here
4014 * with redirect_opt_num(), but bash doesn't do it.
4015 * "echo foo 2| cat" yields "foo 2". */
4016 done_command(ctx);
4017 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00004018 goto new_cmd;
Eric Andersen25f27032001-04-26 23:22:31 +00004019 case '(':
Denis Vlasenko17f02e72008-07-14 04:32:29 +00004020#if ENABLE_HUSH_CASE
Denis Vlasenkof1736072008-07-31 10:09:26 +00004021 /* "case... in [(]word)..." - skip '(' */
Denis Vlasenko22d10a02008-10-13 08:53:43 +00004022 if (ctx->ctx_res_w == RES_MATCH
Denis Vlasenko9af22c72008-10-09 12:54:58 +00004023 && ctx->command->argv == NULL /* not (word|(... */
Denis Vlasenko22d10a02008-10-13 08:53:43 +00004024 && dest->length == 0 /* not word(... */
4025 && dest->nonnull == 0 /* not ""(... */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00004026 ) {
4027 continue;
4028 }
4029#endif
Denis Vlasenko371de4a2008-10-14 12:43:13 +00004030#if ENABLE_HUSH_FUNCTIONS
Denis Vlasenkoc3735272008-10-09 12:58:26 +00004031 if (dest->length != 0 /* not just () but word() */
4032 && dest->nonnull == 0 /* not a"b"c() */
4033 && ctx->command->argv == NULL /* it's the first word */
Denis Vlasenko371de4a2008-10-14 12:43:13 +00004034//TODO: "func ( ) {...}" - note spaces - is valid format too in bash
Denis Vlasenkoc3735272008-10-09 12:58:26 +00004035 && i_peek(input) == ')'
Denis Vlasenko22d10a02008-10-13 08:53:43 +00004036 && !match_reserved_word(dest)
Denis Vlasenkoc3735272008-10-09 12:58:26 +00004037 ) {
4038 bb_error_msg("seems like a function definition");
Denis Vlasenko22d10a02008-10-13 08:53:43 +00004039 i_getch(input);
4040 do {
Denis Vlasenko371de4a2008-10-14 12:43:13 +00004041//TODO: do it properly.
Denis Vlasenko22d10a02008-10-13 08:53:43 +00004042 ch = i_getch(input);
4043 } while (ch == ' ' || ch == '\n');
4044 if (ch != '{') {
4045 syntax("was expecting {");
4046 debug_printf_parse("parse_stream return 1\n");
4047 return 1;
4048 }
Denis Vlasenko371de4a2008-10-14 12:43:13 +00004049 ch = 'F'; /* magic value */
Denis Vlasenkoc3735272008-10-09 12:58:26 +00004050 }
4051#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004052 case '{':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00004053 if (parse_group(dest, ctx, input, ch) != 0) {
4054 debug_printf_parse("parse_stream return 1: parse_group returned non-0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004055 return 1;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00004056 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00004057 goto new_cmd;
Eric Andersen25f27032001-04-26 23:22:31 +00004058 case ')':
Denis Vlasenko17f02e72008-07-14 04:32:29 +00004059#if ENABLE_HUSH_CASE
4060 if (ctx->ctx_res_w == RES_MATCH)
4061 goto case_semi;
4062#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004063 case '}':
Denis Vlasenkoc3735272008-10-09 12:58:26 +00004064 /* proper use of this character is caught by end_trigger:
4065 * if we see {, we call parse_group(..., end_trigger='}')
4066 * and it will match } earlier (not here). */
Denis Vlasenkoa8442002008-06-14 11:00:17 +00004067 syntax("unexpected } or )");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00004068 debug_printf_parse("parse_stream return 1: unexpected '}'\n");
Eric Andersen25f27032001-04-26 23:22:31 +00004069 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00004070 default:
Denis Vlasenko5ec61322008-06-24 00:50:07 +00004071 if (HUSH_DEBUG)
Denis Vlasenko90e485c2007-05-23 15:22:50 +00004072 bb_error_msg_and_die("BUG: unexpected %c\n", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00004073 }
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004074 } /* while (1) */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00004075 debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL));
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00004076 if (end_trigger)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00004077 return -1;
Eric Andersen25f27032001-04-26 23:22:31 +00004078 return 0;
4079}
4080
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004081static void set_in_charmap(const char *set, int code)
Eric Andersen25f27032001-04-26 23:22:31 +00004082{
Denis Vlasenkof5294e12007-04-14 10:09:57 +00004083 while (*set)
Denis Vlasenko87a86552008-07-29 19:43:10 +00004084 G.charmap[(unsigned char)*set++] = code;
Eric Andersen25f27032001-04-26 23:22:31 +00004085}
4086
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004087static void update_charmap(void)
Eric Andersen25f27032001-04-26 23:22:31 +00004088{
Denis Vlasenko87a86552008-07-29 19:43:10 +00004089 G.ifs = getenv("IFS");
4090 if (G.ifs == NULL)
4091 G.ifs = " \t\n";
Eric Andersen25f27032001-04-26 23:22:31 +00004092 /* Precompute a list of 'flow through' behavior so it can be treated
4093 * quickly up front. Computation is necessary because of IFS.
4094 * Special case handling of IFS == " \t\n" is not implemented.
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004095 * The charmap[] array only really needs two bits each,
4096 * and on most machines that would be faster (reduced L1 cache use).
Eric Andersen25f27032001-04-26 23:22:31 +00004097 */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004098 memset(G.charmap, CHAR_ORDINARY, sizeof(G.charmap));
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00004099#if ENABLE_HUSH_TICK
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004100 set_in_charmap("\\$\"`", CHAR_SPECIAL);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00004101#else
4102 set_in_charmap("\\$\"", CHAR_SPECIAL);
4103#endif
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004104 set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004105 set_in_charmap(G.ifs, CHAR_IFS); /* are ordinary if quoted */
Eric Andersen25f27032001-04-26 23:22:31 +00004106}
4107
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004108/* Most recursion does not come through here, the exception is
Denis Vlasenko170435c2007-05-23 13:01:10 +00004109 * from builtin_source() and builtin_eval() */
4110static int parse_and_run_stream(struct in_str *inp, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00004111{
Denis Vlasenko9af22c72008-10-09 12:54:58 +00004112 struct parse_context ctx;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004113 o_string temp = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00004114 int rcode;
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004115
Eric Andersen25f27032001-04-26 23:22:31 +00004116 do {
4117 initialize_context(&ctx);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004118 update_charmap();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004119#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00004120 inp->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004121#endif
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00004122 /* We will stop & execute after each ';' or '\n'.
4123 * Example: "sleep 9999; echo TEST" + ctrl-C:
4124 * TEST should be printed */
Denis Vlasenko2b576b82008-08-04 00:46:07 +00004125 temp.o_assignment = MAYBE_ASSIGNMENT;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00004126 rcode = parse_stream(&temp, &ctx, inp, ";\n");
Denis Vlasenko5ec61322008-06-24 00:50:07 +00004127#if HAS_KEYWORDS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004128 if (rcode != 1 && ctx.old_flag != 0) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00004129 syntax(NULL);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004130 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00004131#endif
4132 if (rcode != 1 IF_HAS_KEYWORDS(&& ctx.old_flag == 0)) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004133 done_word(&temp, &ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004134 done_pipe(&ctx, PIPE_SEQ);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00004135 debug_print_tree(ctx.list_head, 0);
Denis Vlasenko05743d72008-02-10 12:10:08 +00004136 debug_printf_exec("parse_stream_outer: run_and_free_list\n");
4137 run_and_free_list(ctx.list_head);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004138 } else {
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004139 /* We arrive here also if rcode == 1 (error in parse_stream) */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00004140#if HAS_KEYWORDS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004141 if (ctx.old_flag != 0) {
4142 free(ctx.stack);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00004143 o_reset(&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004144 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00004145#endif
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00004146 /*temp.nonnull = 0; - o_free does it below */
4147 /*temp.o_quote = 0; - o_free does it below */
Denis Vlasenko05743d72008-02-10 12:10:08 +00004148 free_pipe_list(ctx.list_head, /* indent: */ 0);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004149 /* Discard all unprocessed line input, force prompt on */
4150 inp->p = NULL;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004151#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004152 inp->promptme = 1;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004153#endif
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004154 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00004155 o_free(&temp);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004156 /* loop on syntax errors, return on EOF: */
4157 } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP));
Eric Andersen25f27032001-04-26 23:22:31 +00004158 return 0;
4159}
4160
Denis Vlasenko170435c2007-05-23 13:01:10 +00004161static int parse_and_run_string(const char *s, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00004162{
4163 struct in_str input;
4164 setup_string_in_str(&input, s);
Denis Vlasenko170435c2007-05-23 13:01:10 +00004165 return parse_and_run_stream(&input, parse_flag);
Eric Andersen25f27032001-04-26 23:22:31 +00004166}
4167
Denis Vlasenko170435c2007-05-23 13:01:10 +00004168static int parse_and_run_file(FILE *f)
Eric Andersen25f27032001-04-26 23:22:31 +00004169{
4170 int rcode;
4171 struct in_str input;
4172 setup_file_in_str(&input, f);
Denis Vlasenko5703c222008-06-15 11:49:42 +00004173 rcode = parse_and_run_stream(&input, 0 /* parse_flag */);
Eric Andersen25f27032001-04-26 23:22:31 +00004174 return rcode;
4175}
4176
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004177#if ENABLE_HUSH_JOB
Eric Andersen6c947d22001-06-25 22:24:38 +00004178/* Make sure we have a controlling tty. If we get started under a job
4179 * aware app (like bash for example), make sure we are now in charge so
4180 * we don't fight over who gets the foreground */
Eric Anderseneaecbf32001-10-31 10:41:31 +00004181static void setup_job_control(void)
Eric Andersen52a97ca2001-06-22 06:49:26 +00004182{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004183 pid_t shell_pgrp;
4184
Denis Vlasenko87a86552008-07-29 19:43:10 +00004185 shell_pgrp = getpgrp();
Denis Vlasenko87a86552008-07-29 19:43:10 +00004186 close_on_exec_on(G.interactive_fd);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004187
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00004188 /* If we were ran as 'hush &',
4189 * sleep until we are in the foreground. */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004190 while (tcgetpgrp(G.interactive_fd) != shell_pgrp) {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004191 /* Send TTIN to ourself (should stop us) */
Denis Vlasenkoff131b92007-04-10 15:42:06 +00004192 kill(- shell_pgrp, SIGTTIN);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00004193 shell_pgrp = getpgrp();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004194 }
Eric Andersen52a97ca2001-06-22 06:49:26 +00004195
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004196 /* Ignore job-control and misc signals. */
4197 set_jobctrl_sighandler(SIG_IGN);
4198 set_misc_sighandler(SIG_IGN);
4199//huh? signal(SIGCHLD, SIG_IGN);
4200
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00004201 /* We _must_ restore tty pgrp on fatal signals */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004202 set_fatal_sighandler(sigexit);
Eric Andersen6c947d22001-06-25 22:24:38 +00004203
4204 /* Put ourselves in our own process group. */
Bernhard Reutner-Fischer864329d2008-09-25 10:55:05 +00004205 bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */
Eric Andersen6c947d22001-06-25 22:24:38 +00004206 /* Grab control of the terminal. */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004207 tcsetpgrp(G.interactive_fd, getpid());
Eric Andersen6c947d22001-06-25 22:24:38 +00004208}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004209#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00004210
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004211
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00004212int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Matt Kraai2d91deb2001-08-01 17:21:35 +00004213int hush_main(int argc, char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00004214{
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004215 static const struct variable const_shell_ver = {
4216 .next = NULL,
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00004217 .varstr = (char*)hush_version_str,
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004218 .max_len = 1, /* 0 can provoke free(name) */
4219 .flg_export = 1,
4220 .flg_read_only = 1,
4221 };
4222
Eric Andersen25f27032001-04-26 23:22:31 +00004223 int opt;
4224 FILE *input;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004225 char **e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004226 struct variable *cur_var;
Eric Andersenbc604a22001-05-16 05:24:03 +00004227
Denis Vlasenko574f2f42008-02-27 18:41:59 +00004228 INIT_G();
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004229
Denis Vlasenko87a86552008-07-29 19:43:10 +00004230 G.root_pid = getpid();
Denis Vlasenko16c2fea2008-06-17 12:28:44 +00004231
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004232 /* Deal with HUSH_VERSION */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004233 G.shell_ver = const_shell_ver; /* copying struct here */
4234 G.top_var = &G.shell_ver;
Denis Vlasenkof886fd22008-10-13 12:36:05 +00004235 debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION");
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004236 unsetenv("HUSH_VERSION"); /* in case it exists in initial env */
4237 /* Initialize our shell local variables with the values
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004238 * currently living in the environment */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004239 cur_var = G.top_var;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004240 e = environ;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004241 if (e) while (*e) {
4242 char *value = strchr(*e, '=');
4243 if (value) { /* paranoia */
4244 cur_var->next = xzalloc(sizeof(*cur_var));
4245 cur_var = cur_var->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00004246 cur_var->varstr = *e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004247 cur_var->max_len = strlen(*e);
4248 cur_var->flg_export = 1;
4249 }
4250 e++;
4251 }
Denis Vlasenkof886fd22008-10-13 12:36:05 +00004252 debug_printf_env("putenv '%s'\n", hush_version_str);
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00004253 putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00004254
Denis Vlasenko38f63192007-01-22 09:03:07 +00004255#if ENABLE_FEATURE_EDITING
Denis Vlasenko87a86552008-07-29 19:43:10 +00004256 G.line_input_state = new_line_input_t(FOR_SHELL);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00004257#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004258 /* XXX what should these be while sourcing /etc/profile? */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004259 G.global_argc = argc;
4260 G.global_argv = argv;
Eric Andersen94ac2442001-05-22 19:05:18 +00004261 /* Initialize some more globals to non-zero values */
4262 set_cwd();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004263#if ENABLE_HUSH_INTERACTIVE
4264#if ENABLE_FEATURE_EDITING
4265 cmdedit_set_initial_prompt();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004266#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00004267 G.PS2 = "> ";
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004268#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00004269
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004270 if (EXIT_SUCCESS) /* otherwise is already done */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004271 G.last_return_code = EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00004272
4273 if (argv[0] && argv[0][0] == '-') {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00004274 debug_printf("sourcing /etc/profile\n");
Denis Vlasenko5415c852008-07-21 23:05:26 +00004275 input = fopen_for_read("/etc/profile");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00004276 if (input != NULL) {
Denis Vlasenkoa0898172007-10-01 09:59:01 +00004277 close_on_exec_on(fileno(input));
Denis Vlasenko170435c2007-05-23 13:01:10 +00004278 parse_and_run_file(input);
Eric Andersena90f20b2001-06-26 23:00:21 +00004279 fclose(input);
4280 }
Eric Andersen25f27032001-04-26 23:22:31 +00004281 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004282 input = stdin;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004283
Eric Andersen25f27032001-04-26 23:22:31 +00004284 while ((opt = getopt(argc, argv, "c:xif")) > 0) {
4285 switch (opt) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004286 case 'c':
Denis Vlasenko87a86552008-07-29 19:43:10 +00004287 G.global_argv = argv + optind;
4288 G.global_argc = argc - optind;
Denis Vlasenko5703c222008-06-15 11:49:42 +00004289 opt = parse_and_run_string(optarg, 0 /* parse_flag */);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004290 goto final_return;
4291 case 'i':
Denis Vlasenkoc666f712007-05-16 22:18:54 +00004292 /* Well, we cannot just declare interactiveness,
4293 * we have to have some stuff (ctty, etc) */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004294 /* G.interactive_fd++; */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004295 break;
4296 case 'f':
Denis Vlasenko87a86552008-07-29 19:43:10 +00004297 G.fake_mode = 1;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004298 break;
4299 default:
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004300#ifndef BB_VER
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004301 fprintf(stderr, "Usage: sh [FILE]...\n"
4302 " or: sh -c command [args]...\n\n");
4303 exit(EXIT_FAILURE);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004304#else
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004305 bb_show_usage();
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004306#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004307 }
4308 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004309#if ENABLE_HUSH_JOB
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004310 /* A shell is interactive if the '-i' flag was given, or if all of
Eric Andersen25f27032001-04-26 23:22:31 +00004311 * the following conditions are met:
Denis Vlasenko55b2de72007-04-18 17:21:28 +00004312 * no -c command
Eric Andersen25f27032001-04-26 23:22:31 +00004313 * no arguments remaining or the -s flag given
4314 * standard input is a terminal
4315 * standard output is a terminal
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004316 * Refer to Posix.2, the description of the 'sh' utility. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004317 if (argv[optind] == NULL && input == stdin
4318 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
4319 ) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004320 G.saved_tty_pgrp = tcgetpgrp(STDIN_FILENO);
4321 debug_printf("saved_tty_pgrp=%d\n", G.saved_tty_pgrp);
4322 if (G.saved_tty_pgrp >= 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004323 /* try to dup to high fd#, >= 255 */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004324 G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
4325 if (G.interactive_fd < 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004326 /* try to dup to any fd */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004327 G.interactive_fd = dup(STDIN_FILENO);
4328 if (G.interactive_fd < 0)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004329 /* give up */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004330 G.interactive_fd = 0;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004331 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004332 // TODO: track & disallow any attempts of user
4333 // to (inadvertently) close/redirect it
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004334 }
Eric Andersen25f27032001-04-26 23:22:31 +00004335 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004336 debug_printf("G.interactive_fd=%d\n", G.interactive_fd);
4337 if (G.interactive_fd) {
4338 fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC);
Eric Andersen25f27032001-04-26 23:22:31 +00004339 /* Looks like they want an interactive shell */
Eric Andersen52a97ca2001-06-22 06:49:26 +00004340 setup_job_control();
Denis Vlasenko4ecfcdc2008-02-11 08:32:31 +00004341 /* -1 is special - makes xfuncs longjmp, not exit
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00004342 * (we reset die_sleep = 0 whereever we [v]fork) */
4343 die_sleep = -1;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004344 if (setjmp(die_jmp)) {
4345 /* xfunc has failed! die die die */
4346 hush_exit(xfunc_error_retval);
4347 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004348#if !ENABLE_FEATURE_SH_EXTRA_QUIET
Denis Vlasenkoca525b42007-06-13 12:27:17 +00004349 printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", bb_banner);
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004350 printf("Enter 'help' for a list of built-in commands.\n\n");
4351#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00004352 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004353#elif ENABLE_HUSH_INTERACTIVE
4354/* no job control compiled, only prompt/line editing */
4355 if (argv[optind] == NULL && input == stdin
4356 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
4357 ) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004358 G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
4359 if (G.interactive_fd < 0) {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004360 /* try to dup to any fd */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004361 G.interactive_fd = dup(STDIN_FILENO);
4362 if (G.interactive_fd < 0)
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004363 /* give up */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004364 G.interactive_fd = 0;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004365 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004366 if (G.interactive_fd) {
4367 fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC);
Denis Vlasenko4830fc52008-05-25 21:50:55 +00004368 set_misc_sighandler(SIG_IGN);
4369 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004370 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004371#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004372
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004373 if (argv[optind] == NULL) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00004374 opt = parse_and_run_file(stdin);
Denis Vlasenko05743d72008-02-10 12:10:08 +00004375 } else {
4376 debug_printf("\nrunning script '%s'\n", argv[optind]);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004377 G.global_argv = argv + optind;
4378 G.global_argc = argc - optind;
Denis Vlasenko5415c852008-07-21 23:05:26 +00004379 input = xfopen_for_read(argv[optind]);
Denis Vlasenko459a5ad2008-02-11 08:35:03 +00004380 fcntl(fileno(input), F_SETFD, FD_CLOEXEC);
Denis Vlasenko05743d72008-02-10 12:10:08 +00004381 opt = parse_and_run_file(input);
Eric Andersen25f27032001-04-26 23:22:31 +00004382 }
Eric Andersen25f27032001-04-26 23:22:31 +00004383
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004384 final_return:
4385
Denis Vlasenko38f63192007-01-22 09:03:07 +00004386#if ENABLE_FEATURE_CLEAN_UP
Eric Andersenaeb44c42001-05-22 20:29:00 +00004387 fclose(input);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004388 if (G.cwd != bb_msg_unknown)
4389 free((char*)G.cwd);
4390 cur_var = G.top_var->next;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004391 while (cur_var) {
4392 struct variable *tmp = cur_var;
4393 if (!cur_var->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00004394 free(cur_var->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004395 cur_var = cur_var->next;
4396 free(tmp);
Eric Andersenaeb44c42001-05-22 20:29:00 +00004397 }
Eric Andersen25f27032001-04-26 23:22:31 +00004398#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00004399 hush_exit(opt ? opt : G.last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +00004400}
Denis Vlasenko96702ca2007-11-23 23:28:55 +00004401
4402
4403#if ENABLE_LASH
4404int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
4405int lash_main(int argc, char **argv)
4406{
4407 //bb_error_msg("lash is deprecated, please use hush instead");
4408 return hush_main(argc, argv);
4409}
4410#endif
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004411
4412
4413/*
4414 * Built-ins
4415 */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004416static int builtin_true(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004417{
4418 return 0;
4419}
4420
4421static int builtin_test(char **argv)
4422{
4423 int argc = 0;
4424 while (*argv) {
4425 argc++;
4426 argv++;
4427 }
4428 return test_main(argc, argv - argc);
4429}
4430
4431static int builtin_echo(char **argv)
4432{
4433 int argc = 0;
4434 while (*argv) {
4435 argc++;
4436 argv++;
4437 }
4438 return echo_main(argc, argv - argc);
4439}
4440
4441static int builtin_eval(char **argv)
4442{
4443 int rcode = EXIT_SUCCESS;
4444
4445 if (argv[1]) {
4446 char *str = expand_strvec_to_string(argv + 1);
4447 parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP);
4448 free(str);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004449 rcode = G.last_return_code;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004450 }
4451 return rcode;
4452}
4453
4454static int builtin_cd(char **argv)
4455{
4456 const char *newdir;
4457 if (argv[1] == NULL) {
4458 // bash does nothing (exitcode 0) if HOME is ""; if it's unset,
4459 // bash says "bash: cd: HOME not set" and does nothing (exitcode 1)
4460 newdir = getenv("HOME") ? : "/";
4461 } else
4462 newdir = argv[1];
4463 if (chdir(newdir)) {
4464 printf("cd: %s: %s\n", newdir, strerror(errno));
4465 return EXIT_FAILURE;
4466 }
4467 set_cwd();
4468 return EXIT_SUCCESS;
4469}
4470
4471static int builtin_exec(char **argv)
4472{
4473 if (argv[1] == NULL)
4474 return EXIT_SUCCESS; /* bash does this */
4475 {
4476#if !BB_MMU
Denis Vlasenkof886fd22008-10-13 12:36:05 +00004477 nommu_save_t dummy;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004478#endif
4479// FIXME: if exec fails, bash does NOT exit! We do...
Denis Vlasenkof886fd22008-10-13 12:36:05 +00004480 pseudo_exec_argv(&dummy, argv + 1, 0, NULL);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004481 /* never returns */
4482 }
4483}
4484
4485static int builtin_exit(char **argv)
4486{
4487// TODO: bash does it ONLY on top-level sh exit (+interacive only?)
4488 //puts("exit"); /* bash does it */
4489// TODO: warn if we have background jobs: "There are stopped jobs"
4490// On second consecutive 'exit', exit anyway.
4491 if (argv[1] == NULL)
Denis Vlasenko87a86552008-07-29 19:43:10 +00004492 hush_exit(G.last_return_code);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004493 /* mimic bash: exit 123abc == exit 255 + error msg */
4494 xfunc_error_retval = 255;
4495 /* bash: exit -2 == exit 254, no error msg */
4496 hush_exit(xatoi(argv[1]) & 0xff);
4497}
4498
4499static int builtin_export(char **argv)
4500{
4501 const char *value;
4502 char *name = argv[1];
4503
4504 if (name == NULL) {
4505 // TODO:
4506 // ash emits: export VAR='VAL'
4507 // bash: declare -x VAR="VAL"
4508 // (both also escape as needed (quotes, $, etc))
4509 char **e = environ;
4510 if (e)
4511 while (*e)
4512 puts(*e++);
4513 return EXIT_SUCCESS;
4514 }
4515
4516 value = strchr(name, '=');
4517 if (!value) {
4518 /* They are exporting something without a =VALUE */
4519 struct variable *var;
4520
4521 var = get_local_var(name);
4522 if (var) {
4523 var->flg_export = 1;
Denis Vlasenkof886fd22008-10-13 12:36:05 +00004524 debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004525 putenv(var->varstr);
4526 }
4527 /* bash does not return an error when trying to export
4528 * an undefined variable. Do likewise. */
4529 return EXIT_SUCCESS;
4530 }
4531
4532 set_local_var(xstrdup(name), 1);
4533 return EXIT_SUCCESS;
4534}
4535
4536#if ENABLE_HUSH_JOB
4537/* built-in 'fg' and 'bg' handler */
4538static int builtin_fg_bg(char **argv)
4539{
4540 int i, jobnum;
4541 struct pipe *pi;
4542
Denis Vlasenko87a86552008-07-29 19:43:10 +00004543 if (!G.interactive_fd)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004544 return EXIT_FAILURE;
4545 /* If they gave us no args, assume they want the last backgrounded task */
4546 if (!argv[1]) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004547 for (pi = G.job_list; pi; pi = pi->next) {
4548 if (pi->jobid == G.last_jobid) {
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004549 goto found;
4550 }
4551 }
4552 bb_error_msg("%s: no current job", argv[0]);
4553 return EXIT_FAILURE;
4554 }
4555 if (sscanf(argv[1], "%%%d", &jobnum) != 1) {
4556 bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]);
4557 return EXIT_FAILURE;
4558 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004559 for (pi = G.job_list; pi; pi = pi->next) {
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004560 if (pi->jobid == jobnum) {
4561 goto found;
4562 }
4563 }
4564 bb_error_msg("%s: %d: no such job", argv[0], jobnum);
4565 return EXIT_FAILURE;
4566 found:
4567 // TODO: bash prints a string representation
4568 // of job being foregrounded (like "sleep 1 | cat")
4569 if (*argv[0] == 'f') {
4570 /* Put the job into the foreground. */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004571 tcsetpgrp(G.interactive_fd, pi->pgrp);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004572 }
4573
4574 /* Restart the processes in the job */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00004575 debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp);
4576 for (i = 0; i < pi->num_cmds; i++) {
4577 debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid);
4578 pi->cmds[i].is_stopped = 0;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004579 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00004580 pi->stopped_cmds = 0;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004581
4582 i = kill(- pi->pgrp, SIGCONT);
4583 if (i < 0) {
4584 if (errno == ESRCH) {
4585 delete_finished_bg_job(pi);
4586 return EXIT_SUCCESS;
4587 } else {
4588 bb_perror_msg("kill (SIGCONT)");
4589 }
4590 }
4591
4592 if (*argv[0] == 'f') {
4593 remove_bg_job(pi);
4594 return checkjobs_and_fg_shell(pi);
4595 }
4596 return EXIT_SUCCESS;
4597}
4598#endif
4599
4600#if ENABLE_HUSH_HELP
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004601static int builtin_help(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004602{
4603 const struct built_in_command *x;
4604
4605 printf("\nBuilt-in commands:\n");
4606 printf("-------------------\n");
4607 for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
4608 printf("%s\t%s\n", x->cmd, x->descr);
4609 }
4610 printf("\n\n");
4611 return EXIT_SUCCESS;
4612}
4613#endif
4614
4615#if ENABLE_HUSH_JOB
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004616static int builtin_jobs(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004617{
4618 struct pipe *job;
4619 const char *status_string;
4620
Denis Vlasenko87a86552008-07-29 19:43:10 +00004621 for (job = G.job_list; job; job = job->next) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00004622 if (job->alive_cmds == job->stopped_cmds)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004623 status_string = "Stopped";
4624 else
4625 status_string = "Running";
4626
4627 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
4628 }
4629 return EXIT_SUCCESS;
4630}
4631#endif
4632
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004633static int builtin_pwd(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004634{
4635 puts(set_cwd());
4636 return EXIT_SUCCESS;
4637}
4638
4639static int builtin_read(char **argv)
4640{
4641 char *string;
4642 const char *name = argv[1] ? argv[1] : "REPLY";
4643
4644 string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL);
4645 return set_local_var(string, 0);
4646}
4647
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +00004648/* built-in 'set' handler
4649 * SUSv3 says:
4650 * set [-abCefmnuvx] [-h] [-o option] [argument...]
4651 * set [+abCefmnuvx] [+h] [+o option] [argument...]
4652 * set -- [argument...]
4653 * set -o
4654 * set +o
4655 * Implementations shall support the options in both their hyphen and
4656 * plus-sign forms. These options can also be specified as options to sh.
4657 * Examples:
4658 * Write out all variables and their values: set
4659 * Set $1, $2, and $3 and set "$#" to 3: set c a b
4660 * Turn on the -x and -v options: set -xv
4661 * Unset all positional parameters: set --
4662 * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x"
4663 * Set the positional parameters to the expansion of x, even if x expands
4664 * with a leading '-' or '+': set -- $x
4665 *
4666 * So far, we only support "set -- [argument...]" by ignoring all options
4667 * (also, "-o option" will be mishandled by taking "option" as parameter #1).
4668 */
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004669static int builtin_set(char **argv)
4670{
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004671 struct variable *e;
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +00004672 char **pp;
4673 char *arg = *++argv;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004674
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +00004675 if (arg == NULL) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004676 for (e = G.top_var; e; e = e->next)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004677 puts(e->varstr);
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +00004678 } else {
4679 /* NB: G.global_argv[0] ($0) is never freed/changed */
4680
4681 if (G.global_args_malloced) {
4682 pp = G.global_argv;
4683 while (*++pp)
4684 free(*pp);
4685 G.global_argv[1] = NULL;
4686 } else {
4687 G.global_args_malloced = 1;
4688 pp = xzalloc(sizeof(pp[0]) * 2);
4689 pp[0] = G.global_argv[0]; /* retain $0 */
4690 G.global_argv = pp;
4691 }
4692 do {
4693 if (arg[0] == '+')
4694 continue;
4695 if (arg[0] != '-')
4696 break;
4697 if (arg[1] == '-' && arg[2] == '\0') {
4698 argv++;
4699 break;
4700 }
4701 } while ((arg = *++argv) != NULL);
4702 /* Now argv[0] is 1st argument */
4703
4704 /* This realloc's G.global_argv */
4705 G.global_argv = pp = add_strings_to_strings(G.global_argv, argv, /*dup:*/ 1);
4706 G.global_argc = 1;
4707 while (*++pp)
4708 G.global_argc++;
4709 }
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004710
4711 return EXIT_SUCCESS;
4712}
4713
4714static int builtin_shift(char **argv)
4715{
4716 int n = 1;
4717 if (argv[1]) {
4718 n = atoi(argv[1]);
4719 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004720 if (n >= 0 && n < G.global_argc) {
4721 G.global_argv[n] = G.global_argv[0];
4722 G.global_argc -= n;
4723 G.global_argv += n;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004724 return EXIT_SUCCESS;
4725 }
4726 return EXIT_FAILURE;
4727}
4728
4729static int builtin_source(char **argv)
4730{
4731 FILE *input;
4732 int status;
4733
4734 if (argv[1] == NULL)
4735 return EXIT_FAILURE;
4736
4737 /* XXX search through $PATH is missing */
Denis Vlasenko5415c852008-07-21 23:05:26 +00004738 input = fopen_for_read(argv[1]);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004739 if (!input) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +00004740 bb_error_msg("can't open '%s'", argv[1]);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004741 return EXIT_FAILURE;
4742 }
4743 close_on_exec_on(fileno(input));
4744
4745 /* Now run the file */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004746 /* XXX argv and argc are broken; need to save old G.global_argv
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004747 * (pointer only is OK!) on this stack frame,
Denis Vlasenko87a86552008-07-29 19:43:10 +00004748 * set G.global_argv=argv+1, recurse, and restore. */
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004749 status = parse_and_run_file(input);
4750 fclose(input);
4751 return status;
4752}
4753
4754static int builtin_umask(char **argv)
4755{
4756 mode_t new_umask;
4757 const char *arg = argv[1];
4758 char *end;
4759 if (arg) {
4760 new_umask = strtoul(arg, &end, 8);
4761 if (*end != '\0' || end == arg) {
4762 return EXIT_FAILURE;
4763 }
4764 } else {
4765 new_umask = umask(0);
4766 printf("%.3o\n", (unsigned) new_umask);
4767 }
4768 umask(new_umask);
4769 return EXIT_SUCCESS;
4770}
4771
4772static int builtin_unset(char **argv)
4773{
4774 /* bash always returns true */
4775 unset_local_var(argv[1]);
4776 return EXIT_SUCCESS;
4777}
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004778
Denis Vlasenkodadfb492008-07-29 10:16:05 +00004779#if ENABLE_HUSH_LOOPS
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004780static int builtin_break(char **argv)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004781{
Denis Vlasenko87a86552008-07-29 19:43:10 +00004782 if (G.depth_of_loop == 0) {
Denis Vlasenko4f504a92008-07-29 19:48:30 +00004783 bb_error_msg("%s: only meaningful in a loop", argv[0]);
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00004784 return EXIT_SUCCESS; /* bash compat */
4785 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004786 G.flag_break_continue++; /* BC_BREAK = 1 */
4787 G.depth_break_continue = 1;
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004788 if (argv[1]) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004789 G.depth_break_continue = bb_strtou(argv[1], NULL, 10);
4790 if (errno || !G.depth_break_continue || argv[2]) {
Denis Vlasenko4f504a92008-07-29 19:48:30 +00004791 bb_error_msg("%s: bad arguments", argv[0]);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004792 G.flag_break_continue = BC_BREAK;
4793 G.depth_break_continue = UINT_MAX;
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004794 }
4795 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004796 if (G.depth_of_loop < G.depth_break_continue)
4797 G.depth_break_continue = G.depth_of_loop;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004798 return EXIT_SUCCESS;
4799}
4800
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004801static int builtin_continue(char **argv)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004802{
Denis Vlasenko4f504a92008-07-29 19:48:30 +00004803 G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */
4804 return builtin_break(argv);
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004805}
Denis Vlasenkodadfb492008-07-29 10:16:05 +00004806#endif