blob: 9c0cd7c8edb7e0c44e42f5a5a2ecdb4bfb2c127d [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 Vlasenkoe1300f62009-03-22 11:41:18 +0000474 /* is global_argv and global_argv[1..n] malloced? (note: not [0]) */
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +0000475 smalluint global_args_malloced;
Denis Vlasenkoe1300f62009-03-22 11:41:18 +0000476 /* how many non-NULL argv's we have. NB: $# + 1 */
477 int global_argc;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000478 char **global_argv;
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000479#if ENABLE_HUSH_LOOPS
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +0000480 unsigned depth_break_continue;
Denis Vlasenkofcf37c32008-07-29 11:37:15 +0000481 unsigned depth_of_loop;
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000482#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000483 const char *ifs;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000484 const char *cwd;
Denis Vlasenko87a86552008-07-29 19:43:10 +0000485 struct variable *top_var; /* = &G.shell_ver (set in main()) */
Denis Vlasenko0a83fc32007-05-25 11:12:32 +0000486 struct variable shell_ver;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000487#if ENABLE_FEATURE_SH_STANDALONE
488 struct nofork_save_area nofork_save;
489#endif
490#if ENABLE_HUSH_JOB
491 sigjmp_buf toplevel_jb;
492#endif
493 unsigned char charmap[256];
494 char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2];
495};
496
497#define G (*ptr_to_globals)
Denis Vlasenko87a86552008-07-29 19:43:10 +0000498/* Not #defining name to G.name - this quickly gets unwieldy
499 * (too many defines). Also, I actually prefer to see when a variable
500 * is global, thus "G." prefix is a useful hint */
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000501#define INIT_G() do { \
502 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
503} while (0)
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000504
505
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000506#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
507
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000508#if 1
509/* Normal */
510static void syntax(const char *msg)
511{
Denis Vlasenko87a86552008-07-29 19:43:10 +0000512#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000513 /* Was using fancy stuff:
Denis Vlasenko87a86552008-07-29 19:43:10 +0000514 * (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...)
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000515 * but it SEGVs. ?! Oh well... explicit temp ptr works around that */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000516 void FAST_FUNC (*fp)(const char *s, ...);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000517 fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000518 fp(msg ? "%s: %s" : "syntax error", "syntax error", msg);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000519#else
520 bb_error_msg_and_die(msg ? "%s: %s" : "syntax error", "syntax error", msg);
521#endif
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000522}
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000523
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000524#else
525/* Debug */
526static void syntax_lineno(int line)
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000527{
Denis Vlasenko87a86552008-07-29 19:43:10 +0000528#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkoff182a32008-07-05 20:29:59 +0000529 void FAST_FUNC (*fp)(const char *s, ...);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000530 fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000531 fp("syntax error hush.c:%d", line);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000532#else
533 bb_error_msg_and_die("syntax error hush.c:%d", line);
534#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000535}
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000536#define syntax(str) syntax_lineno(__LINE__)
537#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000538
539/* Index of subroutines: */
Eric Andersen25f27032001-04-26 23:22:31 +0000540/* in_str manipulations: */
541static int static_get(struct in_str *i);
542static int static_peek(struct in_str *i);
543static int file_get(struct in_str *i);
544static int file_peek(struct in_str *i);
545static void setup_file_in_str(struct in_str *i, FILE *f);
546static void setup_string_in_str(struct in_str *i, const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000547/* "run" the final data structures: */
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000548#if !defined(DEBUG_CLEAN)
549#define free_pipe_list(head, indent) free_pipe_list(head)
550#define free_pipe(pi, indent) free_pipe(pi)
551#endif
Eric Andersenbf7df042001-05-23 22:18:35 +0000552static int free_pipe_list(struct pipe *head, int indent);
553static int free_pipe(struct pipe *pi, int indent);
Eric Andersen25f27032001-04-26 23:22:31 +0000554/* really run the final data structures: */
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000555typedef struct nommu_save_t {
556 char **new_env;
557 char **old_env;
558 char **argv;
559} nommu_save_t;
Denis Vlasenko76d50412008-06-10 16:19:39 +0000560#if BB_MMU
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000561#define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \
Denis Vlasenko2b576b82008-08-04 00:46:07 +0000562 pseudo_exec_argv(argv, assignment_cnt, argv_expanded)
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000563#define pseudo_exec(nommu_save, command, argv_expanded) \
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000564 pseudo_exec(command, argv_expanded)
Denis Vlasenko76d50412008-06-10 16:19:39 +0000565#endif
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000566static void pseudo_exec_argv(nommu_save_t *nommu_save, char **argv, int assignment_cnt, char **argv_expanded) NORETURN;
567static void pseudo_exec(nommu_save_t *nommu_save, struct command *command, char **argv_expanded) NORETURN;
568static int setup_redirects(struct command *prog, int squirrel[]);
569static int run_list(struct pipe *pi);
Denis Vlasenko05743d72008-02-10 12:10:08 +0000570static int run_pipe(struct pipe *pi);
Eric Andersen25f27032001-04-26 23:22:31 +0000571/* data structure manipulation: */
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000572static int setup_redirect(struct parse_context *ctx, int fd, redir_type style, struct in_str *input);
573static void initialize_context(struct parse_context *ctx);
574static int done_word(o_string *dest, struct parse_context *ctx);
575static int done_command(struct parse_context *ctx);
576static void done_pipe(struct parse_context *ctx, pipe_style type);
Eric Andersen25f27032001-04-26 23:22:31 +0000577/* primary string parsing: */
578static int redirect_dup_num(struct in_str *input);
579static int redirect_opt_num(o_string *o);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000580#if ENABLE_HUSH_TICK
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000581static int process_command_subs(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000582 struct in_str *input, const char *subst_end);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000583#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000584static int parse_group(o_string *dest, struct parse_context *ctx, struct in_str *input, int ch);
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000585static const char *lookup_param(const char *src);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000586static int handle_dollar(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000587 struct in_str *input);
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000588static 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 +0000589/* setup: */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000590static int parse_and_run_stream(struct in_str *inp, int parse_flag);
591static int parse_and_run_string(const char *s, int parse_flag);
592static int parse_and_run_file(FILE *f);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000593/* job management: */
Eric Andersenc798b072001-06-22 06:23:03 +0000594static int checkjobs(struct pipe* fg_pipe);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000595#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +0000596static int checkjobs_and_fg_shell(struct pipe* fg_pipe);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000597static void insert_bg_job(struct pipe *pi);
598static void remove_bg_job(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000599static void delete_finished_bg_job(struct pipe *pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000600#else
601int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */
602#endif
Eric Andersenf72f5622001-05-15 23:21:41 +0000603/* local variable support */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000604static char **expand_strvec_to_strvec(char **argv);
605/* used for eval */
606static char *expand_strvec_to_string(char **argv);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +0000607/* used for expansion of right hand of assignments */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000608static char *expand_string_to_string(const char *str);
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000609static struct variable *get_local_var(const char *name);
610static int set_local_var(char *str, int flg_export);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000611static void unset_local_var(const char *name);
Eric Andersen25f27032001-04-26 23:22:31 +0000612
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000613
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000614static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="HUSH_VER_STR;
615
616
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000617static int glob_needed(const char *s)
618{
619 while (*s) {
620 if (*s == '\\')
621 s++;
622 if (*s == '*' || *s == '[' || *s == '?')
623 return 1;
624 s++;
625 }
626 return 0;
627}
628
Denis Vlasenko30c9cc52008-06-17 07:24:29 +0000629static int is_assignment(const char *s)
630{
Denis Vlasenkod4981312008-07-31 10:34:48 +0000631 if (!s || !(isalpha(*s) || *s == '_'))
Denis Vlasenko30c9cc52008-06-17 07:24:29 +0000632 return 0;
633 s++;
634 while (isalnum(*s) || *s == '_')
635 s++;
636 return *s == '=';
637}
638
Denis Vlasenko55789c62008-06-18 16:30:42 +0000639/* Replace each \x with x in place, return ptr past NUL. */
640static char *unbackslash(char *src)
641{
642 char *dst = src;
643 while (1) {
644 if (*src == '\\')
645 src++;
646 if ((*dst++ = *src++) == '\0')
647 break;
648 }
649 return dst;
650}
651
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +0000652static char **add_strings_to_strings(char **strings, char **add, int need_to_dup)
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000653{
654 int i;
655 unsigned count1;
656 unsigned count2;
657 char **v;
658
659 v = strings;
660 count1 = 0;
661 if (v) {
662 while (*v) {
663 count1++;
664 v++;
665 }
666 }
667 count2 = 0;
668 v = add;
669 while (*v) {
670 count2++;
671 v++;
672 }
673 v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*));
674 v[count1 + count2] = NULL;
675 i = count2;
676 while (--i >= 0)
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +0000677 v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000678 return v;
679}
680
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000681static char **add_string_to_strings(char **strings, char *add)
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000682{
683 char *v[2];
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000684 v[0] = add;
685 v[1] = NULL;
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +0000686 return add_strings_to_strings(strings, v, /*dup:*/ 0);
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000687}
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000688
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000689static void putenv_all(char **strings)
690{
691 if (!strings)
692 return;
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000693 while (*strings) {
694 debug_printf_env("putenv '%s'\n", *strings);
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000695 putenv(*strings++);
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000696 }
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000697}
698
699static char **putenv_all_and_save_old(char **strings)
700{
701 char **old = NULL;
702 char **s = strings;
703
704 if (!strings)
705 return old;
706 while (*strings) {
Denis Vlasenkof886fd22008-10-13 12:36:05 +0000707 char *v, *eq;
708
709 eq = strchr(*strings, '=');
710 if (eq) {
711 *eq = '\0';
712 v = getenv(*strings);
713 *eq = '=';
714 if (v) {
715 /* v points to VAL in VAR=VAL, go back to VAR */
716 v -= (eq - *strings) + 1;
717 old = add_string_to_strings(old, v);
718 }
719 }
720 strings++;
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000721 }
722 putenv_all(s);
723 return old;
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000724}
725
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000726static void free_strings_and_unsetenv(char **strings, int unset)
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000727{
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000728 char **v;
729
730 if (!strings)
731 return;
732
733 v = strings;
734 while (*v) {
735 if (unset) {
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000736 debug_printf_env("unsetenv '%s'\n", *v);
737 bb_unsetenv(*v);
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000738 }
739 free(*v++);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000740 }
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000741 free(strings);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000742}
743
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000744static void free_strings(char **strings)
Denis Vlasenko76d50412008-06-10 16:19:39 +0000745{
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000746 free_strings_and_unsetenv(strings, 0);
Denis Vlasenko76d50412008-06-10 16:19:39 +0000747}
Denis Vlasenko76d50412008-06-10 16:19:39 +0000748
749
Denis Vlasenko83506862007-11-23 13:11:42 +0000750/* Function prototypes for builtins */
751static int builtin_cd(char **argv);
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000752static int builtin_echo(char **argv);
Denis Vlasenko83506862007-11-23 13:11:42 +0000753static int builtin_eval(char **argv);
754static int builtin_exec(char **argv);
755static int builtin_exit(char **argv);
756static int builtin_export(char **argv);
757#if ENABLE_HUSH_JOB
758static int builtin_fg_bg(char **argv);
759static int builtin_jobs(char **argv);
760#endif
761#if ENABLE_HUSH_HELP
762static int builtin_help(char **argv);
763#endif
764static int builtin_pwd(char **argv);
765static int builtin_read(char **argv);
766static int builtin_test(char **argv);
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000767static int builtin_true(char **argv);
Denis Vlasenko83506862007-11-23 13:11:42 +0000768static int builtin_set(char **argv);
769static int builtin_shift(char **argv);
770static int builtin_source(char **argv);
771static int builtin_umask(char **argv);
772static int builtin_unset(char **argv);
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000773#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000774static int builtin_break(char **argv);
775static int builtin_continue(char **argv);
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000776#endif
Denis Vlasenko83506862007-11-23 13:11:42 +0000777//static int builtin_not_written(char **argv);
778
Eric Andersen25f27032001-04-26 23:22:31 +0000779/* Table of built-in functions. They can be forked or not, depending on
780 * context: within pipes, they fork. As simple commands, they do not.
781 * When used in non-forking context, they can change global variables
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000782 * in the parent shell process. If forked, of course they cannot.
Eric Andersen25f27032001-04-26 23:22:31 +0000783 * For example, 'unset foo | whatever' will parse and run, but foo will
784 * still be set at the end. */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000785struct built_in_command {
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000786 const char *cmd;
787 int (*function)(char **argv);
Denis Vlasenko06810332007-05-21 23:30:54 +0000788#if ENABLE_HUSH_HELP
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000789 const char *descr;
Denis Vlasenko06810332007-05-21 23:30:54 +0000790#define BLTIN(cmd, func, help) { cmd, func, help }
791#else
792#define BLTIN(cmd, func, help) { cmd, func }
793#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000794};
795
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000796/* For now, echo and test are unconditionally enabled.
797 * Maybe make it configurable? */
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +0000798static const struct built_in_command bltins[] = {
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000799 BLTIN("." , builtin_source, "Run commands in a file"),
800 BLTIN(":" , builtin_true, "No-op"),
Denis Vlasenko83506862007-11-23 13:11:42 +0000801 BLTIN("[" , builtin_test, "Test condition"),
802 BLTIN("[[" , builtin_test, "Test condition"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000803#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000804 BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000805#endif
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000806#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000807 BLTIN("break" , builtin_break, "Exit from a loop"),
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000808#endif
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000809 BLTIN("cd" , builtin_cd, "Change directory"),
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000810#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000811 BLTIN("continue", builtin_continue, "Start new loop iteration"),
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000812#endif
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000813 BLTIN("echo" , builtin_echo, "Write to stdout"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000814 BLTIN("eval" , builtin_eval, "Construct and run shell command"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000815 BLTIN("exec" , builtin_exec, "Execute command, don't return to shell"),
816 BLTIN("exit" , builtin_exit, "Exit"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000817 BLTIN("export", builtin_export, "Set environment variable"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000818#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000819 BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000820 BLTIN("jobs" , builtin_jobs, "List active jobs"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000821#endif
Denis Vlasenko06810332007-05-21 23:30:54 +0000822 BLTIN("pwd" , builtin_pwd, "Print current directory"),
823 BLTIN("read" , builtin_read, "Input environment variable"),
824// BLTIN("return", builtin_not_written, "Return from a function"),
825 BLTIN("set" , builtin_set, "Set/unset shell local variables"),
826 BLTIN("shift" , builtin_shift, "Shift positional parameters"),
827// BLTIN("trap" , builtin_not_written, "Trap signals"),
Denis Vlasenko83506862007-11-23 13:11:42 +0000828 BLTIN("test" , builtin_test, "Test condition"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000829// BLTIN("ulimit", builtin_not_written, "Control resource limits"),
830 BLTIN("umask" , builtin_umask, "Set file creation mask"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000831 BLTIN("unset" , builtin_unset, "Unset environment variable"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000832#if ENABLE_HUSH_HELP
833 BLTIN("help" , builtin_help, "List shell built-in commands"),
834#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000835};
836
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000837
Denis Vlasenko4830fc52008-05-25 21:50:55 +0000838/* Signals are grouped, we handle them in batches */
839static void set_misc_sighandler(void (*handler)(int))
840{
841 bb_signals(0
842 + (1 << SIGINT)
843 + (1 << SIGQUIT)
844 + (1 << SIGTERM)
845 , handler);
846}
847
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000848#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000849
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000850static void set_fatal_sighandler(void (*handler)(int))
851{
Denis Vlasenko25591c32008-02-16 22:58:56 +0000852 bb_signals(0
853 + (1 << SIGILL)
854 + (1 << SIGTRAP)
855 + (1 << SIGABRT)
856 + (1 << SIGFPE)
857 + (1 << SIGBUS)
858 + (1 << SIGSEGV)
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000859 /* bash 3.2 seems to handle these just like 'fatal' ones */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000860 + (1 << SIGHUP)
861 + (1 << SIGPIPE)
862 + (1 << SIGALRM)
863 , handler);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000864}
865static void set_jobctrl_sighandler(void (*handler)(int))
866{
Denis Vlasenko25591c32008-02-16 22:58:56 +0000867 bb_signals(0
868 + (1 << SIGTSTP)
869 + (1 << SIGTTIN)
870 + (1 << SIGTTOU)
871 , handler);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000872}
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000873/* SIGCHLD is special and handled separately */
874
875static void set_every_sighandler(void (*handler)(int))
876{
877 set_fatal_sighandler(handler);
878 set_jobctrl_sighandler(handler);
879 set_misc_sighandler(handler);
880 signal(SIGCHLD, handler);
881}
882
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000883static void handler_ctrl_c(int sig UNUSED_PARAM)
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000884{
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000885 debug_printf_jobs("got sig %d\n", sig);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000886// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenko87a86552008-07-29 19:43:10 +0000887 siglongjmp(G.toplevel_jb, 1);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000888}
889
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000890static void handler_ctrl_z(int sig UNUSED_PARAM)
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000891{
892 pid_t pid;
893
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000894 debug_printf_jobs("got tty sig %d in pid %d\n", sig, getpid());
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000895 pid = fork();
Denis Vlasenkoc2990322007-05-16 12:57:12 +0000896 if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000897 return;
Denis Vlasenko87a86552008-07-29 19:43:10 +0000898 G.ctrl_z_flag = 1;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000899 if (!pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +0000900 if (ENABLE_HUSH_JOB)
901 die_sleep = 0; /* let nofork's xfuncs die */
Bernhard Reutner-Fischer864329d2008-09-25 10:55:05 +0000902 bb_setpgrp();
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000903 debug_printf_jobs("set pgrp for child %d ok\n", getpid());
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000904 set_every_sighandler(SIG_DFL);
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000905 raise(SIGTSTP); /* resend TSTP so that child will be stopped */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000906 debug_printf_jobs("returning in child\n");
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000907 /* return to nofork, it will eventually exit now,
908 * not return back to shell */
909 return;
910 }
911 /* parent */
912 /* finish filling up pipe info */
Denis Vlasenko87a86552008-07-29 19:43:10 +0000913 G.toplevel_list->pgrp = pid; /* child is in its own pgrp */
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000914 G.toplevel_list->cmds[0].pid = pid;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000915 /* parent needs to longjmp out of running nofork.
916 * we will "return" exitcode 0, with child put in background */
917// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000918 debug_printf_jobs("siglongjmp in parent\n");
Denis Vlasenko87a86552008-07-29 19:43:10 +0000919 siglongjmp(G.toplevel_jb, 1);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000920}
921
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000922/* Restores tty foreground process group, and exits.
923 * May be called as signal handler for fatal signal
924 * (will faithfully resend signal to itself, producing correct exit state)
925 * or called directly with -EXITCODE.
926 * We also call it if xfunc is exiting. */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000927static void sigexit(int sig) NORETURN;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000928static void sigexit(int sig)
929{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000930 /* Disable all signals: job control, SIGPIPE, etc. */
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000931 sigprocmask_allsigs(SIG_BLOCK);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000932
Denis Vlasenko87a86552008-07-29 19:43:10 +0000933#if ENABLE_HUSH_INTERACTIVE
934 if (G.interactive_fd)
935 tcsetpgrp(G.interactive_fd, G.saved_tty_pgrp);
936#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000937
938 /* Not a signal, just exit */
939 if (sig <= 0)
940 _exit(- sig);
941
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000942 kill_myself_with_sig(sig); /* does not return */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000943}
944
945/* Restores tty foreground process group, and exits. */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000946static void hush_exit(int exitcode) NORETURN;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000947static void hush_exit(int exitcode)
948{
949 fflush(NULL); /* flush all streams */
950 sigexit(- (exitcode & 0xff));
951}
952
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000953#else /* !JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000954
955#define set_fatal_sighandler(handler) ((void)0)
956#define set_jobctrl_sighandler(handler) ((void)0)
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000957#define hush_exit(e) exit(e)
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000958
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000959#endif /* JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000960
961
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000962static const char *set_cwd(void)
963{
Denis Vlasenko87a86552008-07-29 19:43:10 +0000964 if (G.cwd == bb_msg_unknown)
965 G.cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */
966 G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd);
967 if (!G.cwd)
968 G.cwd = bb_msg_unknown;
969 return G.cwd;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000970}
971
Denis Vlasenko83506862007-11-23 13:11:42 +0000972
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000973/*
974 * o_string support
975 */
976#define B_CHUNK (32 * sizeof(char*))
Eric Andersen25f27032001-04-26 23:22:31 +0000977
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000978static void o_reset(o_string *o)
Eric Andersen25f27032001-04-26 23:22:31 +0000979{
980 o->length = 0;
981 o->nonnull = 0;
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000982 if (o->data)
983 o->data[0] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +0000984}
985
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000986static void o_free(o_string *o)
Eric Andersen25f27032001-04-26 23:22:31 +0000987{
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000988 free(o->data);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000989 memset(o, 0, sizeof(*o));
Eric Andersen25f27032001-04-26 23:22:31 +0000990}
991
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000992static void o_grow_by(o_string *o, int len)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000993{
994 if (o->length + len > o->maxlen) {
995 o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK);
996 o->data = xrealloc(o->data, 1 + o->maxlen);
997 }
998}
999
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001000static void o_addchr(o_string *o, int ch)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001001{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001002 debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o);
1003 o_grow_by(o, 1);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001004 o->data[o->length] = ch;
1005 o->length++;
1006 o->data[o->length] = '\0';
1007}
1008
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001009static void o_addstr(o_string *o, const char *str, int len)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001010{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001011 o_grow_by(o, len);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001012 memcpy(&o->data[o->length], str, len);
1013 o->length += len;
1014 o->data[o->length] = '\0';
1015}
1016
Denis Vlasenko55789c62008-06-18 16:30:42 +00001017static void o_addstr_duplicate_backslash(o_string *o, const char *str, int len)
1018{
1019 while (len) {
1020 o_addchr(o, *str);
1021 if (*str++ == '\\'
1022 && (*str != '*' && *str != '?' && *str != '[')
1023 ) {
1024 o_addchr(o, '\\');
1025 }
1026 len--;
1027 }
1028}
1029
Eric Andersen25f27032001-04-26 23:22:31 +00001030/* My analysis of quoting semantics tells me that state information
1031 * is associated with a destination, not a source.
1032 */
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001033static void o_addqchr(o_string *o, int ch)
Eric Andersen25f27032001-04-26 23:22:31 +00001034{
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001035 int sz = 1;
Denis Vlasenko371de4a2008-10-14 12:43:13 +00001036 char *found = strchr("*?[\\", ch);
1037 if (found)
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001038 sz++;
Denis Vlasenko371de4a2008-10-14 12:43:13 +00001039 o_grow_by(o, sz);
1040 if (found) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001041 o->data[o->length] = '\\';
1042 o->length++;
Eric Andersen25f27032001-04-26 23:22:31 +00001043 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001044 o->data[o->length] = ch;
1045 o->length++;
1046 o->data[o->length] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00001047}
1048
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001049static void o_addQchr(o_string *o, int ch)
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001050{
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001051 int sz = 1;
1052 if (o->o_quote && strchr("*?[\\", ch)) {
1053 sz++;
1054 o->data[o->length] = '\\';
1055 o->length++;
1056 }
1057 o_grow_by(o, sz);
1058 o->data[o->length] = ch;
1059 o->length++;
1060 o->data[o->length] = '\0';
1061}
1062
1063static void o_addQstr(o_string *o, const char *str, int len)
1064{
1065 if (!o->o_quote) {
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001066 o_addstr(o, str, len);
1067 return;
1068 }
1069 while (len) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001070 char ch;
1071 int sz;
1072 int ordinary_cnt = strcspn(str, "*?[\\");
1073 if (ordinary_cnt > len) /* paranoia */
1074 ordinary_cnt = len;
1075 o_addstr(o, str, ordinary_cnt);
1076 if (ordinary_cnt == len)
1077 return;
1078 str += ordinary_cnt;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001079 len -= ordinary_cnt + 1; /* we are processing + 1 char below */
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001080
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001081 ch = *str++;
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001082 sz = 1;
1083 if (ch) { /* it is necessarily one of "*?[\\" */
1084 sz++;
1085 o->data[o->length] = '\\';
1086 o->length++;
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001087 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001088 o_grow_by(o, sz);
1089 o->data[o->length] = ch;
1090 o->length++;
1091 o->data[o->length] = '\0';
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001092 }
1093}
1094
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001095/* A special kind of o_string for $VAR and `cmd` expansion.
1096 * It contains char* list[] at the beginning, which is grown in 16 element
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001097 * increments. Actual string data starts at the next multiple of 16 * (char*).
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001098 * list[i] contains an INDEX (int!) into this string data.
1099 * It means that if list[] needs to grow, data needs to be moved higher up
1100 * but list[i]'s need not be modified.
1101 * NB: remembering how many list[i]'s you have there is crucial.
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001102 * o_finalize_list() operation post-processes this structure - calculates
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001103 * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well.
1104 */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001105#if DEBUG_EXPAND || DEBUG_GLOB
1106static void debug_print_list(const char *prefix, o_string *o, int n)
1107{
1108 char **list = (char**)o->data;
1109 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1110 int i = 0;
1111 fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n",
1112 prefix, list, n, string_start, o->length, o->maxlen);
1113 while (i < n) {
1114 fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i],
1115 o->data + (int)list[i] + string_start,
1116 o->data + (int)list[i] + string_start);
1117 i++;
1118 }
1119 if (n) {
1120 const char *p = o->data + (int)list[n - 1] + string_start;
1121 fprintf(stderr, " total_sz:%d\n", (p + strlen(p) + 1) - o->data);
1122 }
1123}
1124#else
1125#define debug_print_list(prefix, o, n) ((void)0)
1126#endif
1127
1128/* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value
1129 * in list[n] so that it points past last stored byte so far.
1130 * It returns n+1. */
1131static int o_save_ptr_helper(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001132{
1133 char **list = (char**)o->data;
Denis Vlasenko895bea22008-06-10 18:06:24 +00001134 int string_start;
1135 int string_len;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001136
1137 if (!o->has_empty_slot) {
Denis Vlasenko895bea22008-06-10 18:06:24 +00001138 string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1139 string_len = o->length - string_start;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001140 if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001141 debug_printf_list("list[%d]=%d string_start=%d (growing)\n", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001142 /* list[n] points to string_start, make space for 16 more pointers */
1143 o->maxlen += 0x10 * sizeof(list[0]);
1144 o->data = xrealloc(o->data, o->maxlen + 1);
Denis Vlasenko7049ff82008-06-25 09:53:17 +00001145 list = (char**)o->data;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001146 memmove(list + n + 0x10, list + n, string_len);
1147 o->length += 0x10 * sizeof(list[0]);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001148 } else
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001149 debug_printf_list("list[%d]=%d string_start=%d\n", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001150 } else {
1151 /* We have empty slot at list[n], reuse without growth */
Denis Vlasenko895bea22008-06-10 18:06:24 +00001152 string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */
1153 string_len = o->length - string_start;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001154 debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001155 o->has_empty_slot = 0;
1156 }
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +00001157 list[n] = (char*)(ptrdiff_t)string_len;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001158 return n + 1;
1159}
1160
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001161/* "What was our last o_save_ptr'ed position (byte offset relative o->data)?" */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001162static int o_get_last_ptr(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001163{
1164 char **list = (char**)o->data;
1165 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1166
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +00001167 return ((int)(ptrdiff_t)list[n-1]) + string_start;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001168}
1169
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001170/* o_glob performs globbing on last list[], saving each result
Denis Vlasenko55789c62008-06-18 16:30:42 +00001171 * as a new list[]. */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001172static int o_glob(o_string *o, int n)
1173{
1174 glob_t globdata;
1175 int gr;
1176 char *pattern;
1177
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001178 debug_printf_glob("start o_glob: n:%d o->data:%p\n", n, o->data);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001179 if (!o->data)
1180 return o_save_ptr_helper(o, n);
1181 pattern = o->data + o_get_last_ptr(o, n);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001182 debug_printf_glob("glob pattern '%s'\n", pattern);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001183 if (!glob_needed(pattern)) {
1184 literal:
1185 o->length = unbackslash(pattern) - o->data;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001186 debug_printf_glob("glob pattern '%s' is literal\n", pattern);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001187 return o_save_ptr_helper(o, n);
1188 }
1189
1190 memset(&globdata, 0, sizeof(globdata));
1191 gr = glob(pattern, 0, NULL, &globdata);
1192 debug_printf_glob("glob('%s'):%d\n", pattern, gr);
1193 if (gr == GLOB_NOSPACE)
1194 bb_error_msg_and_die("out of memory during glob");
1195 if (gr == GLOB_NOMATCH) {
1196 globfree(&globdata);
1197 goto literal;
1198 }
1199 if (gr != 0) { /* GLOB_ABORTED ? */
1200//TODO: testcase for bad glob pattern behavior
1201 bb_error_msg("glob(3) error %d on '%s'", gr, pattern);
1202 }
1203 if (globdata.gl_pathv && globdata.gl_pathv[0]) {
1204 char **argv = globdata.gl_pathv;
1205 o->length = pattern - o->data; /* "forget" pattern */
1206 while (1) {
1207 o_addstr(o, *argv, strlen(*argv) + 1);
1208 n = o_save_ptr_helper(o, n);
1209 argv++;
1210 if (!*argv)
1211 break;
1212 }
1213 }
1214 globfree(&globdata);
1215 if (DEBUG_GLOB)
1216 debug_print_list("o_glob returning", o, n);
1217 return n;
1218}
1219
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001220/* If o->o_glob == 1, glob the string so far remembered.
1221 * Otherwise, just finish current list[] and start new */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001222static int o_save_ptr(o_string *o, int n)
1223{
Denis Vlasenkoa8b6dff2009-03-20 12:05:14 +00001224 if (o->o_glob) { /* if globbing is requested */
1225 /* If o->has_empty_slot, list[n] was already globbed
1226 * (if it was requested back then when it was filled)
1227 * so don't do that again! */
1228 if (!o->has_empty_slot)
1229 return o_glob(o, n); /* o_save_ptr_helper is inside */
1230 }
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001231 return o_save_ptr_helper(o, n);
1232}
1233
1234/* "Please convert list[n] to real char* ptrs, and NULL terminate it." */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001235static char **o_finalize_list(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001236{
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001237 char **list;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001238 int string_start;
1239
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001240 n = o_save_ptr(o, n); /* force growth for list[n] if necessary */
1241 if (DEBUG_EXPAND)
1242 debug_print_list("finalized", o, n);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001243 debug_printf_expand("finalized n:%d\n", n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001244 list = (char**)o->data;
1245 string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1246 list[--n] = NULL;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001247 while (n) {
1248 n--;
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +00001249 list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001250 }
1251 return list;
1252}
1253
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001254
1255/*
1256 * in_str support
1257 */
Eric Andersen25f27032001-04-26 23:22:31 +00001258static int static_get(struct in_str *i)
1259{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001260 int ch = *i->p++;
1261 if (ch == '\0') return EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001262 return ch;
1263}
1264
1265static int static_peek(struct in_str *i)
1266{
1267 return *i->p;
1268}
1269
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001270#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001271
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001272#if ENABLE_FEATURE_EDITING
Rob Landley88621d72006-08-29 19:41:06 +00001273static void cmdedit_set_initial_prompt(void)
Eric Andersen25f27032001-04-26 23:22:31 +00001274{
Denis Vlasenko38f63192007-01-22 09:03:07 +00001275#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko87a86552008-07-29 19:43:10 +00001276 G.PS1 = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00001277#else
Denis Vlasenko87a86552008-07-29 19:43:10 +00001278 G.PS1 = getenv("PS1");
1279 if (G.PS1 == NULL)
1280 G.PS1 = "\\w \\$ ";
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001281#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001282}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001283#endif /* EDITING */
Eric Andersen25f27032001-04-26 23:22:31 +00001284
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001285static const char* setup_prompt_string(int promptmode)
Eric Andersen25f27032001-04-26 23:22:31 +00001286{
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001287 const char *prompt_str;
1288 debug_printf("setup_prompt_string %d ", promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001289#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001290 /* Set up the prompt */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001291 if (promptmode == 0) { /* PS1 */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001292 free((char*)G.PS1);
1293 G.PS1 = xasprintf("%s %c ", G.cwd, (geteuid() != 0) ? '$' : '#');
1294 prompt_str = G.PS1;
Eric Andersen25f27032001-04-26 23:22:31 +00001295 } else {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001296 prompt_str = G.PS2;
Eric Andersen25f27032001-04-26 23:22:31 +00001297 }
1298#else
Denis Vlasenko87a86552008-07-29 19:43:10 +00001299 prompt_str = (promptmode == 0) ? G.PS1 : G.PS2;
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001300#endif
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001301 debug_printf("result '%s'\n", prompt_str);
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001302 return prompt_str;
Eric Andersen25f27032001-04-26 23:22:31 +00001303}
1304
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001305static void get_user_input(struct in_str *i)
Eric Andersen25f27032001-04-26 23:22:31 +00001306{
Denis Vlasenko0937be52007-04-28 16:47:08 +00001307 int r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001308 const char *prompt_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001309
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001310 prompt_str = setup_prompt_string(i->promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001311#if ENABLE_FEATURE_EDITING
Denis Vlasenko170435c2007-05-23 13:01:10 +00001312 /* Enable command line editing only while a command line
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001313 * is actually being read */
1314 do {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001315 r = read_line_input(prompt_str, G.user_input_buf, BUFSIZ-1, G.line_input_state);
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001316 } while (r == 0); /* repeat if Ctrl-C */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001317 i->eof_flag = (r < 0);
1318 if (i->eof_flag) { /* EOF/error detected */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001319 G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */
1320 G.user_input_buf[1] = '\0';
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001321 }
Eric Andersen25f27032001-04-26 23:22:31 +00001322#else
1323 fputs(prompt_str, stdout);
1324 fflush(stdout);
Denis Vlasenko87a86552008-07-29 19:43:10 +00001325 G.user_input_buf[0] = r = fgetc(i->file);
1326 /*G.user_input_buf[1] = '\0'; - already is and never changed */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001327 i->eof_flag = (r == EOF);
Eric Andersen25f27032001-04-26 23:22:31 +00001328#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00001329 i->p = G.user_input_buf;
Eric Andersen25f27032001-04-26 23:22:31 +00001330}
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001331
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001332#endif /* INTERACTIVE */
Eric Andersen25f27032001-04-26 23:22:31 +00001333
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001334/* This is the magic location that prints prompts
Eric Andersen25f27032001-04-26 23:22:31 +00001335 * and gets data back from the user */
1336static int file_get(struct in_str *i)
1337{
1338 int ch;
1339
Eric Andersen25f27032001-04-26 23:22:31 +00001340 /* If there is data waiting, eat it up */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001341 if (i->p && *i->p) {
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001342#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001343 take_cached:
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001344#endif
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001345 ch = *i->p++;
1346 if (i->eof_flag && !*i->p)
1347 ch = EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001348 } else {
1349 /* need to double check i->file because we might be doing something
1350 * more complicated by now, like sourcing or substituting. */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001351#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko87a86552008-07-29 19:43:10 +00001352 if (G.interactive_fd && i->promptme && i->file == stdin) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001353 do {
1354 get_user_input(i);
1355 } while (!*i->p); /* need non-empty line */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001356 i->promptmode = 1; /* PS2 */
1357 i->promptme = 0;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001358 goto take_cached;
Eric Andersen25f27032001-04-26 23:22:31 +00001359 }
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001360#endif
1361 ch = fgetc(i->file);
Eric Andersen25f27032001-04-26 23:22:31 +00001362 }
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001363 debug_printf("file_get: got a '%c' %d\n", ch, ch);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001364#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001365 if (ch == '\n')
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001366 i->promptme = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001367#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001368 return ch;
1369}
1370
1371/* All the callers guarantee this routine will never be
1372 * used right after a newline, so prompting is not needed.
1373 */
1374static int file_peek(struct in_str *i)
1375{
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001376 int ch;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001377 if (i->p && *i->p) {
1378 if (i->eof_flag && !i->p[1])
1379 return EOF;
1380 return *i->p;
Eric Andersen25f27032001-04-26 23:22:31 +00001381 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001382 ch = fgetc(i->file);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001383 i->eof_flag = (ch == EOF);
1384 i->peek_buf[0] = ch;
1385 i->peek_buf[1] = '\0';
1386 i->p = i->peek_buf;
1387 debug_printf("file_peek: got a '%c' %d\n", *i->p, *i->p);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001388 return ch;
Eric Andersen25f27032001-04-26 23:22:31 +00001389}
1390
1391static void setup_file_in_str(struct in_str *i, FILE *f)
1392{
1393 i->peek = file_peek;
1394 i->get = file_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001395#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001396 i->promptme = 1;
1397 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001398#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001399 i->file = f;
1400 i->p = NULL;
1401}
1402
1403static void setup_string_in_str(struct in_str *i, const char *s)
1404{
1405 i->peek = static_peek;
1406 i->get = static_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001407#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001408 i->promptme = 1;
1409 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001410#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001411 i->p = s;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001412 i->eof_flag = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001413}
1414
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001415
Eric Andersen25f27032001-04-26 23:22:31 +00001416/* squirrel != NULL means we squirrel away copies of stdin, stdout,
1417 * and stderr if they are redirected. */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001418static int setup_redirects(struct command *prog, int squirrel[])
Eric Andersen25f27032001-04-26 23:22:31 +00001419{
1420 int openfd, mode;
1421 struct redir_struct *redir;
1422
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001423 for (redir = prog->redirects; redir; redir = redir->next) {
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00001424 if (redir->dup == -1 && redir->rd_filename == NULL) {
Eric Andersen817e73c2001-06-06 17:56:09 +00001425 /* something went wrong in the parse. Pretend it didn't happen */
1426 continue;
1427 }
Eric Andersen25f27032001-04-26 23:22:31 +00001428 if (redir->dup == -1) {
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001429 char *p;
Denis Vlasenko55789c62008-06-18 16:30:42 +00001430 mode = redir_table[redir->rd_type].mode;
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00001431//TODO: check redir for names like '\\'
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00001432 p = expand_string_to_string(redir->rd_filename);
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001433 openfd = open_or_warn(p, mode);
1434 free(p);
Eric Andersen25f27032001-04-26 23:22:31 +00001435 if (openfd < 0) {
1436 /* this could get lost if stderr has been redirected, but
1437 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersen25f27032001-04-26 23:22:31 +00001438 return 1;
1439 }
1440 } else {
1441 openfd = redir->dup;
1442 }
1443
1444 if (openfd != redir->fd) {
1445 if (squirrel && redir->fd < 3) {
1446 squirrel[redir->fd] = dup(redir->fd);
1447 }
Eric Andersen83a2ae22001-05-07 17:59:25 +00001448 if (openfd == -3) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001449 //close(openfd); // close(-3) ??!
Eric Andersen83a2ae22001-05-07 17:59:25 +00001450 } else {
1451 dup2(openfd, redir->fd);
Matt Kraaic616e532001-06-05 16:50:08 +00001452 if (redir->dup == -1)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001453 close(openfd);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001454 }
Eric Andersen25f27032001-04-26 23:22:31 +00001455 }
1456 }
1457 return 0;
1458}
1459
1460static void restore_redirects(int squirrel[])
1461{
1462 int i, fd;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001463 for (i = 0; i < 3; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001464 fd = squirrel[i];
1465 if (fd != -1) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00001466 /* We simply die on error */
1467 xmove_fd(fd, i);
Eric Andersen25f27032001-04-26 23:22:31 +00001468 }
1469 }
1470}
1471
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001472static char **expand_assignments(char **argv, int count)
1473{
1474 int i;
1475 char **p = NULL;
1476 /* Expand assignments into one string each */
1477 for (i = 0; i < count; i++) {
1478 p = add_string_to_strings(p, expand_string_to_string(argv[i]));
1479 }
1480 return p;
1481}
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001482
Denis Vlasenko05743d72008-02-10 12:10:08 +00001483/* Called after [v]fork() in run_pipe(), or from builtin_exec().
Denis Vlasenko8412d792007-10-01 09:59:47 +00001484 * Never returns.
1485 * XXX no exit() here. If you don't exec, use _exit instead.
Eric Andersen94ac2442001-05-22 19:05:18 +00001486 * The at_exit handlers apparently confuse the calling process,
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001487 * in particular stdin handling. Not sure why? -- because of vfork! (vda) */
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001488static void pseudo_exec_argv(nommu_save_t *nommu_save, char **argv, int assignment_cnt, char **argv_expanded)
Eric Andersen25f27032001-04-26 23:22:31 +00001489{
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001490 int rcode;
1491 char **new_env;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001492 const struct built_in_command *x;
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001493
Denis Vlasenko1359da62007-04-21 23:27:30 +00001494 /* If a variable is assigned in a forest, and nobody listens,
1495 * was it ever really set?
1496 */
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001497 if (!argv[assignment_cnt])
Denis Vlasenko1359da62007-04-21 23:27:30 +00001498 _exit(EXIT_SUCCESS);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001499
Denis Vlasenko9504e442008-10-29 01:19:15 +00001500 new_env = expand_assignments(argv, assignment_cnt);
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001501#if BB_MMU
1502 putenv_all(new_env);
1503 free(new_env); /* optional */
1504#else
1505 nommu_save->new_env = new_env;
1506 nommu_save->old_env = putenv_all_and_save_old(new_env);
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001507#endif
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001508 if (argv_expanded) {
1509 argv = argv_expanded;
1510 } else {
1511 argv = expand_strvec_to_strvec(argv);
Denis Vlasenko76d50412008-06-10 16:19:39 +00001512#if !BB_MMU
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001513 nommu_save->argv = argv;
Denis Vlasenko76d50412008-06-10 16:19:39 +00001514#endif
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001515 }
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001516
Denis Vlasenko1359da62007-04-21 23:27:30 +00001517 /*
1518 * Check if the command matches any of the builtins.
1519 * Depending on context, this might be redundant. But it's
1520 * easier to waste a few CPU cycles than it is to figure out
1521 * if this is one of those cases.
1522 */
Denis Vlasenkodd316dd2008-06-14 15:50:55 +00001523 for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001524 if (strcmp(argv[0], x->cmd) == 0) {
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001525 debug_printf_exec("running builtin '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001526 rcode = x->function(argv);
1527 fflush(stdout);
1528 _exit(rcode);
Eric Andersen94ac2442001-05-22 19:05:18 +00001529 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001530 }
Eric Andersen78a7c992001-05-15 16:30:25 +00001531
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001532 /* Check if the command matches any busybox applets */
Denis Vlasenko80d14be2007-04-10 23:03:30 +00001533#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001534 if (strchr(argv[0], '/') == NULL) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001535 int a = find_applet_by_name(argv[0]);
1536 if (a >= 0) {
1537 if (APPLET_IS_NOEXEC(a)) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001538 debug_printf_exec("running applet '%s'\n", argv[0]);
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001539// is it ok that run_applet_no_and_exit() does exit(), not _exit()?
1540 run_applet_no_and_exit(a, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001541 }
1542 /* re-exec ourselves with the new arguments */
1543 debug_printf_exec("re-execing applet '%s'\n", argv[0]);
Denis Vlasenkobdbbb7e2007-06-08 15:02:55 +00001544 execvp(bb_busybox_exec_path, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001545 /* If they called chroot or otherwise made the binary no longer
1546 * executable, fall through */
1547 }
1548 }
Eric Andersenaac75e52001-04-30 18:18:45 +00001549#endif
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001550
1551 debug_printf_exec("execing '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001552 execvp(argv[0], argv);
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +00001553 bb_perror_msg("can't exec '%s'", argv[0]);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001554 _exit(EXIT_FAILURE);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001555}
1556
Denis Vlasenko05743d72008-02-10 12:10:08 +00001557/* Called after [v]fork() in run_pipe()
Denis Vlasenko8412d792007-10-01 09:59:47 +00001558 */
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001559static void pseudo_exec(nommu_save_t *nommu_save, struct command *command, char **argv_expanded)
Denis Vlasenko1359da62007-04-21 23:27:30 +00001560{
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001561 if (command->argv)
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001562 pseudo_exec_argv(nommu_save, command->argv, command->assignment_cnt, argv_expanded);
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001563
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001564 if (command->group) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001565#if !BB_MMU
Denis Vlasenko3b492162007-12-24 14:26:57 +00001566 bb_error_msg_and_die("nested lists are not supported on NOMMU");
Denis Vlasenko8412d792007-10-01 09:59:47 +00001567#else
Denis Vlasenko3b492162007-12-24 14:26:57 +00001568 int rcode;
Denis Vlasenko05743d72008-02-10 12:10:08 +00001569 debug_printf_exec("pseudo_exec: run_list\n");
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001570 rcode = run_list(command->group);
Eric Andersenbf7df042001-05-23 22:18:35 +00001571 /* OK to leak memory by not calling free_pipe_list,
Eric Andersen25f27032001-04-26 23:22:31 +00001572 * since this process is about to exit */
Eric Andersen94ac2442001-05-22 19:05:18 +00001573 _exit(rcode);
Denis Vlasenko8412d792007-10-01 09:59:47 +00001574#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001575 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001576
1577 /* Can happen. See what bash does with ">foo" by itself. */
1578 debug_printf("trying to pseudo_exec null command\n");
1579 _exit(EXIT_SUCCESS);
Eric Andersen25f27032001-04-26 23:22:31 +00001580}
1581
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001582#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001583static const char *get_cmdtext(struct pipe *pi)
1584{
1585 char **argv;
1586 char *p;
1587 int len;
1588
1589 /* This is subtle. ->cmdtext is created only on first backgrounding.
1590 * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...)
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001591 * On subsequent bg argv is trashed, but we won't use it */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001592 if (pi->cmdtext)
1593 return pi->cmdtext;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001594 argv = pi->cmds[0].argv;
Denis Vlasenkoa8442002008-06-14 11:00:17 +00001595 if (!argv || !argv[0]) {
1596 pi->cmdtext = xzalloc(1);
1597 return pi->cmdtext;
1598 }
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001599
1600 len = 0;
1601 do len += strlen(*argv) + 1; while (*++argv);
1602 pi->cmdtext = p = xmalloc(len);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001603 argv = pi->cmds[0].argv;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001604 do {
1605 len = strlen(*argv);
1606 memcpy(p, *argv, len);
1607 p += len;
1608 *p++ = ' ';
1609 } while (*++argv);
1610 p[-1] = '\0';
1611 return pi->cmdtext;
1612}
1613
Eric Andersenbafd94f2001-05-02 16:11:59 +00001614static void insert_bg_job(struct pipe *pi)
1615{
1616 struct pipe *thejob;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001617 int i;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001618
1619 /* Linear search for the ID of the job to use */
1620 pi->jobid = 1;
Denis Vlasenko87a86552008-07-29 19:43:10 +00001621 for (thejob = G.job_list; thejob; thejob = thejob->next)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001622 if (thejob->jobid >= pi->jobid)
1623 pi->jobid = thejob->jobid + 1;
1624
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001625 /* Add thejob to the list of running jobs */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001626 if (!G.job_list) {
1627 thejob = G.job_list = xmalloc(sizeof(*thejob));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001628 } else {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001629 for (thejob = G.job_list; thejob->next; thejob = thejob->next)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001630 continue;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001631 thejob->next = xmalloc(sizeof(*thejob));
1632 thejob = thejob->next;
1633 }
1634
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001635 /* Physically copy the struct job */
Eric Andersen0fcd4472001-05-02 20:12:03 +00001636 memcpy(thejob, pi, sizeof(struct pipe));
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001637 thejob->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds);
1638 /* We cannot copy entire pi->cmds[] vector! Double free()s will happen */
1639 for (i = 0; i < pi->num_cmds; i++) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001640// TODO: do we really need to have so many fields which are just dead weight
1641// at execution stage?
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001642 thejob->cmds[i].pid = pi->cmds[i].pid;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001643 /* all other fields are not used and stay zero */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001644 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001645 thejob->next = NULL;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001646 thejob->cmdtext = xstrdup(get_cmdtext(pi));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001647
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001648 /* We don't wait for background thejobs to return -- append it
Eric Andersenbafd94f2001-05-02 16:11:59 +00001649 to the list of backgrounded thejobs and leave it alone */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001650 printf("[%d] %d %s\n", thejob->jobid, thejob->cmds[0].pid, thejob->cmdtext);
1651 G.last_bg_pid = thejob->cmds[0].pid;
Denis Vlasenko87a86552008-07-29 19:43:10 +00001652 G.last_jobid = thejob->jobid;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001653}
1654
Eric Andersenbafd94f2001-05-02 16:11:59 +00001655static void remove_bg_job(struct pipe *pi)
1656{
1657 struct pipe *prev_pipe;
1658
Denis Vlasenko87a86552008-07-29 19:43:10 +00001659 if (pi == G.job_list) {
1660 G.job_list = pi->next;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001661 } else {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001662 prev_pipe = G.job_list;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001663 while (prev_pipe->next != pi)
1664 prev_pipe = prev_pipe->next;
1665 prev_pipe->next = pi->next;
1666 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00001667 if (G.job_list)
1668 G.last_jobid = G.job_list->jobid;
Eric Andersen028b65b2001-06-28 01:10:11 +00001669 else
Denis Vlasenko87a86552008-07-29 19:43:10 +00001670 G.last_jobid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001671}
Eric Andersen028b65b2001-06-28 01:10:11 +00001672
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001673/* Remove a backgrounded job */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001674static void delete_finished_bg_job(struct pipe *pi)
1675{
1676 remove_bg_job(pi);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001677 pi->stopped_cmds = 0;
Eric Andersenbf7df042001-05-23 22:18:35 +00001678 free_pipe(pi, 0);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001679 free(pi);
1680}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001681#endif /* JOB */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001682
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001683/* Check to see if any processes have exited -- if they
1684 * have, figure out why and see if a job has completed */
Eric Andersenc798b072001-06-22 06:23:03 +00001685static int checkjobs(struct pipe* fg_pipe)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001686{
Eric Andersenc798b072001-06-22 06:23:03 +00001687 int attributes;
1688 int status;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001689#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00001690 struct pipe *pi;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001691#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001692 pid_t childpid;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001693 int rcode = 0;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001694
Eric Andersenc798b072001-06-22 06:23:03 +00001695 attributes = WUNTRACED;
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001696 if (fg_pipe == NULL)
Eric Andersenc798b072001-06-22 06:23:03 +00001697 attributes |= WNOHANG;
Eric Andersenc798b072001-06-22 06:23:03 +00001698
Denis Vlasenko1359da62007-04-21 23:27:30 +00001699/* Do we do this right?
1700 * bash-3.00# sleep 20 | false
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001701 * <ctrl-Z pressed>
Denis Vlasenko1359da62007-04-21 23:27:30 +00001702 * [3]+ Stopped sleep 20 | false
1703 * bash-3.00# echo $?
1704 * 1 <========== bg pipe is not fully done, but exitcode is already known!
1705 */
1706
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001707//FIXME: non-interactive bash does not continue even if all processes in fg pipe
1708//are stopped. Testcase: "cat | cat" in a script (not on command line)
1709// + killall -STOP cat
1710
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001711 wait_more:
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001712// TODO: safe_waitpid?
Eric Andersenc798b072001-06-22 06:23:03 +00001713 while ((childpid = waitpid(-1, &status, attributes)) > 0) {
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001714 int i;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001715 const int dead = WIFEXITED(status) || WIFSIGNALED(status);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001716#if DEBUG_JOBS
Denis Vlasenko1359da62007-04-21 23:27:30 +00001717 if (WIFSTOPPED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001718 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001719 childpid, WSTOPSIG(status), WEXITSTATUS(status));
1720 if (WIFSIGNALED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001721 debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001722 childpid, WTERMSIG(status), WEXITSTATUS(status));
1723 if (WIFEXITED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001724 debug_printf_jobs("pid %d exited, exitcode %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001725 childpid, WEXITSTATUS(status));
1726#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001727 /* Were we asked to wait for fg pipe? */
Eric Andersenc798b072001-06-22 06:23:03 +00001728 if (fg_pipe) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001729 for (i = 0; i < fg_pipe->num_cmds; i++) {
1730 debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid);
1731 if (fg_pipe->cmds[i].pid != childpid)
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001732 continue;
1733 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */
1734 if (dead) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001735 fg_pipe->cmds[i].pid = 0;
1736 fg_pipe->alive_cmds--;
1737 if (i == fg_pipe->num_cmds - 1) {
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001738 /* last process gives overall exitstatus */
1739 rcode = WEXITSTATUS(status);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00001740 IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;)
Denis Vlasenko1359da62007-04-21 23:27:30 +00001741 }
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001742 } else {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001743 fg_pipe->cmds[i].is_stopped = 1;
1744 fg_pipe->stopped_cmds++;
Eric Andersenc798b072001-06-22 06:23:03 +00001745 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001746 debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n",
1747 fg_pipe->alive_cmds, fg_pipe->stopped_cmds);
1748 if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) {
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001749 /* All processes in fg pipe have exited/stopped */
1750#if ENABLE_HUSH_JOB
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001751 if (fg_pipe->alive_cmds)
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001752 insert_bg_job(fg_pipe);
1753#endif
1754 return rcode;
1755 }
1756 /* There are still running processes in the fg pipe */
1757 goto wait_more; /* do waitpid again */
Eric Andersenc798b072001-06-22 06:23:03 +00001758 }
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001759 /* it wasnt fg_pipe, look for process in bg pipes */
Eric Andersenc798b072001-06-22 06:23:03 +00001760 }
1761
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001762#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001763 /* We asked to wait for bg or orphaned children */
1764 /* No need to remember exitcode in this case */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001765 for (pi = G.job_list; pi; pi = pi->next) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001766 for (i = 0; i < pi->num_cmds; i++) {
1767 if (pi->cmds[i].pid == childpid)
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001768 goto found_pi_and_prognum;
Eric Andersen52a97ca2001-06-22 06:49:26 +00001769 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001770 }
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001771 /* Happens when shell is used as init process (init=/bin/sh) */
1772 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001773 continue; /* do waitpid again */
Eric Andersenaeb44c42001-05-22 20:29:00 +00001774
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001775 found_pi_and_prognum:
Denis Vlasenko1359da62007-04-21 23:27:30 +00001776 if (dead) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001777 /* child exited */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001778 pi->cmds[i].pid = 0;
1779 pi->alive_cmds--;
1780 if (!pi->alive_cmds) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001781 printf(JOB_STATUS_FORMAT, pi->jobid,
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001782 "Done", pi->cmdtext);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001783 delete_finished_bg_job(pi);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001784 }
1785 } else {
1786 /* child stopped */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001787 pi->cmds[i].is_stopped = 1;
1788 pi->stopped_cmds++;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001789 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001790#endif
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001791 } /* while (waitpid succeeds)... */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001792
Denis Vlasenko1359da62007-04-21 23:27:30 +00001793 /* wait found no children or failed */
1794
1795 if (childpid && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001796 bb_perror_msg("waitpid");
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001797 return rcode;
Eric Andersenada18ff2001-05-21 16:18:22 +00001798}
1799
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001800#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +00001801static int checkjobs_and_fg_shell(struct pipe* fg_pipe)
1802{
1803 pid_t p;
1804 int rcode = checkjobs(fg_pipe);
1805 /* Job finished, move the shell to the foreground */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001806 p = getpgid(0); /* pgid of our process */
1807 debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p);
Denis Vlasenko87a86552008-07-29 19:43:10 +00001808 tcsetpgrp(G.interactive_fd, p);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001809 return rcode;
1810}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001811#endif
Denis Vlasenko52881e92007-04-21 13:42:52 +00001812
Denis Vlasenko05743d72008-02-10 12:10:08 +00001813/* run_pipe() starts all the jobs, but doesn't wait for anything
Eric Andersenc798b072001-06-22 06:23:03 +00001814 * to finish. See checkjobs().
Eric Andersen25f27032001-04-26 23:22:31 +00001815 *
1816 * return code is normally -1, when the caller has to wait for children
1817 * to finish to determine the exit status of the pipe. If the pipe
1818 * is a simple builtin command, however, the action is done by the
Denis Vlasenko05743d72008-02-10 12:10:08 +00001819 * time run_pipe returns, and the exit code is provided as the
Eric Andersen25f27032001-04-26 23:22:31 +00001820 * return value.
1821 *
1822 * The input of the pipe is always stdin, the output is always
1823 * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
1824 * because it tries to avoid running the command substitution in
1825 * subshell, when that is in fact necessary. The subshell process
1826 * now has its stdout directed to the input of the appropriate pipe,
1827 * so this routine is noticeably simpler.
Denis Vlasenko170435c2007-05-23 13:01:10 +00001828 *
1829 * Returns -1 only if started some children. IOW: we have to
1830 * mask out retvals of builtins etc with 0xff!
Eric Andersen25f27032001-04-26 23:22:31 +00001831 */
Denis Vlasenko05743d72008-02-10 12:10:08 +00001832static int run_pipe(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00001833{
1834 int i;
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001835 int nextin;
1836 int pipefds[2]; /* pipefds[0] is for reading */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001837 struct command *command;
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001838 char **argv_expanded;
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001839 char **argv;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001840 const struct built_in_command *x;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001841 char *p;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001842 /* it is not always needed, but we aim to smaller code */
1843 int squirrel[] = { -1, -1, -1 };
1844 int rcode;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001845 const int single_and_fg = (pi->num_cmds == 1 && pi->followup != PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00001846
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001847 debug_printf_exec("run_pipe start: single_and_fg=%d\n", single_and_fg);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001848
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001849#if ENABLE_HUSH_JOB
Eric Andersenada18ff2001-05-21 16:18:22 +00001850 pi->pgrp = -1;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001851#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001852 pi->alive_cmds = 1;
1853 pi->stopped_cmds = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001854
1855 /* Check if this is a simple builtin (not part of a pipe).
1856 * Builtins within pipes have to fork anyway, and are handled in
1857 * pseudo_exec. "echo foo | read bar" doesn't work on bash, either.
1858 */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001859 command = &(pi->cmds[0]);
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001860
Denis Vlasenko371de4a2008-10-14 12:43:13 +00001861#if ENABLE_HUSH_FUNCTIONS
1862 if (single_and_fg && command->group && command->grp_type == GRP_FUNCTION) {
1863 /* We "execute" function definition */
1864 bb_error_msg("here we ought to remember function definition, and go on");
1865 return EXIT_SUCCESS;
1866 }
1867#endif
1868
1869 if (single_and_fg && command->group && command->grp_type == GRP_NORMAL) {
Eric Andersen04407e52001-06-07 16:42:05 +00001870 debug_printf("non-subshell grouping\n");
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001871 setup_redirects(command, squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001872 debug_printf_exec(": run_list\n");
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001873 rcode = run_list(command->group) & 0xff;
Eric Andersen04407e52001-06-07 16:42:05 +00001874 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001875 debug_printf_exec("run_pipe return %d\n", rcode);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00001876 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
Denis Vlasenko05743d72008-02-10 12:10:08 +00001877 return rcode;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001878 }
1879
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001880 argv = command->argv;
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001881 argv_expanded = NULL;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001882
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001883 if (single_and_fg && argv != NULL) {
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001884 char **new_env = NULL;
1885 char **old_env = NULL;
1886
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001887 i = command->assignment_cnt;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001888 if (i != 0 && argv[i] == NULL) {
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001889 /* assignments, but no command: set local environment */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001890 for (i = 0; argv[i] != NULL; i++) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001891 debug_printf("local environment set: %s\n", argv[i]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001892 p = expand_string_to_string(argv[i]);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001893 set_local_var(p, 0);
Eric Andersen78a7c992001-05-15 16:30:25 +00001894 }
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001895 return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */
Eric Andersen78a7c992001-05-15 16:30:25 +00001896 }
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001897
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001898 /* Expand the rest into (possibly) many strings each */
1899 argv_expanded = expand_strvec_to_strvec(argv + i);
1900
Denis Vlasenkodd316dd2008-06-14 15:50:55 +00001901 for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001902 if (strcmp(argv_expanded[0], x->cmd) != 0)
1903 continue;
1904 if (x->function == builtin_exec && argv_expanded[1] == NULL) {
1905 debug_printf("exec with redirects only\n");
1906 setup_redirects(command, NULL);
1907 rcode = EXIT_SUCCESS;
1908 goto clean_up_and_ret1;
Eric Andersen25f27032001-04-26 23:22:31 +00001909 }
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001910 debug_printf("builtin inline %s\n", argv_expanded[0]);
1911 /* XXX setup_redirects acts on file descriptors, not FILEs.
1912 * This is perfect for work that comes after exec().
1913 * Is it really safe for inline use? Experimentally,
1914 * things seem to work with glibc. */
1915 setup_redirects(command, squirrel);
1916 new_env = expand_assignments(argv, command->assignment_cnt);
1917 old_env = putenv_all_and_save_old(new_env);
1918 debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv_expanded[1]);
1919 rcode = x->function(argv_expanded) & 0xff;
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001920#if ENABLE_FEATURE_SH_STANDALONE
1921 clean_up_and_ret:
1922#endif
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001923 restore_redirects(squirrel);
1924 free_strings_and_unsetenv(new_env, 1);
1925 putenv_all(old_env);
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001926 free(old_env); /* not free_strings()! */
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001927 clean_up_and_ret1:
1928 free(argv_expanded);
1929 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
1930 debug_printf_exec("run_pipe return %d\n", rcode);
1931 return rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00001932 }
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001933#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001934 i = find_applet_by_name(argv_expanded[0]);
1935 if (i >= 0 && APPLET_IS_NOFORK(i)) {
1936 setup_redirects(command, squirrel);
1937 save_nofork_data(&G.nofork_save);
1938 new_env = expand_assignments(argv, command->assignment_cnt);
1939 old_env = putenv_all_and_save_old(new_env);
1940 debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]);
1941 rcode = run_nofork_applet_prime(&G.nofork_save, i, argv_expanded);
1942 goto clean_up_and_ret;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001943 }
1944#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001945 }
1946
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001947 /* NB: argv_expanded may already be created, and that
1948 * might include `cmd` runs! Do not rerun it! We *must*
1949 * use argv_expanded if it's non-NULL */
1950
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001951 /* Disable job control signals for shell (parent) and
1952 * for initial child code after fork */
1953 set_jobctrl_sighandler(SIG_IGN);
1954
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001955 /* Going to fork a child per each pipe member */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001956 pi->alive_cmds = 0;
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001957 nextin = 0;
1958
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001959 for (i = 0; i < pi->num_cmds; i++) {
Denis Vlasenko76d50412008-06-10 16:19:39 +00001960#if !BB_MMU
Denis Vlasenkof886fd22008-10-13 12:36:05 +00001961 volatile nommu_save_t nommu_save;
1962 nommu_save.new_env = NULL;
1963 nommu_save.old_env = NULL;
1964 nommu_save.argv = NULL;
Denis Vlasenko76d50412008-06-10 16:19:39 +00001965#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001966 command = &(pi->cmds[i]);
1967 if (command->argv) {
1968 debug_printf_exec(": pipe member '%s' '%s'...\n", command->argv[0], command->argv[1]);
Denis Vlasenko76d50412008-06-10 16:19:39 +00001969 } else
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001970 debug_printf_exec(": pipe member with no argv\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001971
1972 /* pipes are inserted between pairs of commands */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001973 pipefds[0] = 0;
1974 pipefds[1] = 1;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001975 if ((i + 1) < pi->num_cmds)
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001976 xpipe(pipefds);
Eric Andersen25f27032001-04-26 23:22:31 +00001977
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001978 command->pid = BB_MMU ? fork() : vfork();
1979 if (!command->pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00001980 if (ENABLE_HUSH_JOB)
1981 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001982#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001983 /* Every child adds itself to new process group
Denis Vlasenko05743d72008-02-10 12:10:08 +00001984 * with pgid == pid_of_first_child_in_pipe */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001985 if (G.run_list_level == 1 && G.interactive_fd) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00001986 pid_t pgrp;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001987 /* Don't do pgrp restore anymore on fatal signals */
1988 set_fatal_sighandler(SIG_DFL);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001989 pgrp = pi->pgrp;
1990 if (pgrp < 0) /* true for 1st process only */
1991 pgrp = getpid();
1992 if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001993 /* We do it in *every* child, not just first,
1994 * to avoid races */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001995 tcsetpgrp(G.interactive_fd, pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001996 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001997 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001998#endif
Denis Vlasenko8412d792007-10-01 09:59:47 +00001999 xmove_fd(nextin, 0);
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002000 xmove_fd(pipefds[1], 1); /* write end */
2001 if (pipefds[0] > 1)
2002 close(pipefds[0]); /* read end */
Eric Andersen25f27032001-04-26 23:22:31 +00002003 /* Like bash, explicit redirects override pipes,
2004 * and the pipe fd is available for dup'ing. */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002005 setup_redirects(command, NULL);
Eric Andersen52a97ca2001-06-22 06:49:26 +00002006
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002007 /* Restore default handlers just prior to exec */
2008 set_jobctrl_sighandler(SIG_DFL);
2009 set_misc_sighandler(SIG_DFL);
2010 signal(SIGCHLD, SIG_DFL);
Denis Vlasenkof886fd22008-10-13 12:36:05 +00002011 /* Stores to nommu_save list of env vars putenv'ed
2012 * (NOMMU, on MMU we don't need that) */
2013 /* cast away volatility... */
2014 pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded);
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00002015 /* pseudo_exec() does not return */
Eric Andersen25f27032001-04-26 23:22:31 +00002016 }
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00002017 /* parent */
2018#if !BB_MMU
Denis Vlasenkof886fd22008-10-13 12:36:05 +00002019 /* Clean up after vforked child */
2020 free(nommu_save.argv);
2021 free_strings_and_unsetenv(nommu_save.new_env, 1);
2022 putenv_all(nommu_save.old_env);
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00002023#endif
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00002024 free(argv_expanded);
2025 argv_expanded = NULL;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002026 if (command->pid < 0) { /* [v]fork failed */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002027 /* Clearly indicate, was it fork or vfork */
Denis Vlasenko82604e92008-07-01 15:59:42 +00002028 bb_perror_msg(BB_MMU ? "fork" : "vfork");
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002029 } else {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002030 pi->alive_cmds++;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002031#if ENABLE_HUSH_JOB
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002032 /* Second and next children need to know pid of first one */
2033 if (pi->pgrp < 0)
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002034 pi->pgrp = command->pid;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002035#endif
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002036 }
Eric Andersen25f27032001-04-26 23:22:31 +00002037
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002038 if (i)
2039 close(nextin);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002040 if ((i + 1) < pi->num_cmds)
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002041 close(pipefds[1]); /* write end */
2042 /* Pass read (output) pipe end to next iteration */
Eric Andersen25f27032001-04-26 23:22:31 +00002043 nextin = pipefds[0];
2044 }
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002045
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002046 if (!pi->alive_cmds) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00002047 debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n");
2048 return 1;
2049 }
2050
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002051 debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds);
Eric Andersen25f27032001-04-26 23:22:31 +00002052 return -1;
2053}
2054
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002055#ifndef debug_print_tree
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002056static void debug_print_tree(struct pipe *pi, int lvl)
2057{
Denis Vlasenko371de4a2008-10-14 12:43:13 +00002058 static const char *const PIPE[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002059 [PIPE_SEQ] = "SEQ",
2060 [PIPE_AND] = "AND",
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002061 [PIPE_OR ] = "OR" ,
2062 [PIPE_BG ] = "BG" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002063 };
2064 static const char *RES[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002065 [RES_NONE ] = "NONE" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00002066#if ENABLE_HUSH_IF
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002067 [RES_IF ] = "IF" ,
2068 [RES_THEN ] = "THEN" ,
2069 [RES_ELIF ] = "ELIF" ,
2070 [RES_ELSE ] = "ELSE" ,
2071 [RES_FI ] = "FI" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00002072#endif
2073#if ENABLE_HUSH_LOOPS
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002074 [RES_FOR ] = "FOR" ,
2075 [RES_WHILE] = "WHILE",
2076 [RES_UNTIL] = "UNTIL",
2077 [RES_DO ] = "DO" ,
2078 [RES_DONE ] = "DONE" ,
Denis Vlasenkod91afa32008-07-29 11:10:01 +00002079#endif
2080#if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002081 [RES_IN ] = "IN" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00002082#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002083#if ENABLE_HUSH_CASE
2084 [RES_CASE ] = "CASE" ,
2085 [RES_MATCH] = "MATCH",
2086 [RES_CASEI] = "CASEI",
2087 [RES_ESAC ] = "ESAC" ,
2088#endif
Denis Vlasenko06810332007-05-21 23:30:54 +00002089 [RES_XXXX ] = "XXXX" ,
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002090 [RES_SNTX ] = "SNTX" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002091 };
Denis Vlasenko371de4a2008-10-14 12:43:13 +00002092 static const char *const GRPTYPE[] = {
2093 "()",
2094 "{}",
2095#if ENABLE_HUSH_FUNCTIONS
2096 "func()",
2097#endif
2098 };
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002099
2100 int pin, prn;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002101
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002102 pin = 0;
2103 while (pi) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002104 fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "",
2105 pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002106 prn = 0;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002107 while (prn < pi->num_cmds) {
2108 struct command *command = &pi->cmds[prn];
2109 char **argv = command->argv;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002110
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002111 fprintf(stderr, "%*s prog %d assignment_cnt:%d", lvl*2, "", prn, command->assignment_cnt);
2112 if (command->group) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002113 fprintf(stderr, " group %s: (argv=%p)\n",
Denis Vlasenko371de4a2008-10-14 12:43:13 +00002114 GRPTYPE[command->grp_type],
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002115 argv);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002116 debug_print_tree(command->group, lvl+1);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002117 prn++;
2118 continue;
2119 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002120 if (argv) while (*argv) {
2121 fprintf(stderr, " '%s'", *argv);
2122 argv++;
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002123 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002124 fprintf(stderr, "\n");
2125 prn++;
2126 }
2127 pi = pi->next;
2128 pin++;
2129 }
2130}
2131#endif
2132
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002133/* NB: called by pseudo_exec, and therefore must not modify any
2134 * global data until exec/_exit (we can be a child after vfork!) */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002135static int run_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002136{
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002137#if ENABLE_HUSH_CASE
2138 char *case_word = NULL;
2139#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002140#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002141 struct pipe *loop_top = NULL;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002142 char *for_varname = NULL;
2143 char **for_lcur = NULL;
2144 char **for_list = NULL;
Denis Vlasenko06810332007-05-21 23:30:54 +00002145#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002146 smallint flag_skip = 1;
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002147 smalluint rcode = 0; /* probably just for compiler */
Denis Vlasenkod91afa32008-07-29 11:10:01 +00002148#if ENABLE_HUSH_IF || ENABLE_HUSH_CASE
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002149 smalluint cond_code = 0;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002150#else
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002151 enum { cond_code = 0, };
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002152#endif
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002153 /*enum reserved_style*/ smallint rword = RES_NONE;
2154 /*enum reserved_style*/ smallint skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002155
Denis Vlasenko87a86552008-07-29 19:43:10 +00002156 debug_printf_exec("run_list start lvl %d\n", G.run_list_level + 1);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002157
Denis Vlasenko06810332007-05-21 23:30:54 +00002158#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002159 /* Check syntax for "for" */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002160 for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) {
2161 if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN)
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002162 continue;
2163 /* current word is FOR or IN (BOLD in comments below) */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002164 if (cpipe->next == NULL) {
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002165 syntax("malformed for");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002166 debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002167 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002168 }
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002169 /* "FOR v; do ..." and "for v IN a b; do..." are ok */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002170 if (cpipe->next->res_word == RES_DO)
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002171 continue;
2172 /* next word is not "do". It must be "in" then ("FOR v in ...") */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002173 if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */
2174 || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002175 ) {
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002176 syntax("malformed for");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002177 debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002178 return 1;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002179 }
2180 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002181#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002182
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002183 /* Past this point, all code paths should jump to ret: label
Denis Vlasenko12acec52008-07-28 15:15:59 +00002184 * in order to return, no direct "return" statements please.
2185 * This helps to ensure that no memory is leaked. */
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002186
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002187#if ENABLE_HUSH_JOB
2188 /* Example of nested list: "while true; do { sleep 1 | exit 2; } done".
2189 * We are saving state before entering outermost list ("while...done")
2190 * so that ctrl-Z will correctly background _entire_ outermost list,
2191 * not just a part of it (like "sleep 1 | exit 2") */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002192 if (++G.run_list_level == 1 && G.interactive_fd) {
2193 if (sigsetjmp(G.toplevel_jb, 1)) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002194 /* ctrl-Z forked and we are parent; or ctrl-C.
2195 * Sighandler has longjmped us here */
2196 signal(SIGINT, SIG_IGN);
2197 signal(SIGTSTP, SIG_IGN);
2198 /* Restore level (we can be coming from deep inside
2199 * nested levels) */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002200 G.run_list_level = 1;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002201#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko87a86552008-07-29 19:43:10 +00002202 if (G.nofork_save.saved) { /* if save area is valid */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002203 debug_printf_jobs("exiting nofork early\n");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002204 restore_nofork_data(&G.nofork_save);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002205 }
2206#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00002207 if (G.ctrl_z_flag) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002208 /* ctrl-Z has forked and stored pid of the child in pi->pid.
2209 * Remember this child as background job */
2210 insert_bg_job(pi);
2211 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002212 /* ctrl-C. We just stop doing whatever we were doing */
Denis Vlasenko4daad902007-09-27 10:20:47 +00002213 bb_putchar('\n');
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002214 }
Denis Vlasenkoff29b4f2008-07-29 13:57:59 +00002215 USE_HUSH_LOOPS(loop_top = NULL;)
Denis Vlasenko87a86552008-07-29 19:43:10 +00002216 USE_HUSH_LOOPS(G.depth_of_loop = 0;)
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002217 rcode = 0;
2218 goto ret;
2219 }
2220 /* ctrl-Z handler will store pid etc in pi */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002221 G.toplevel_list = pi;
2222 G.ctrl_z_flag = 0;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002223#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko87a86552008-07-29 19:43:10 +00002224 G.nofork_save.saved = 0; /* in case we will run a nofork later */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002225#endif
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +00002226 signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002227 signal(SIGINT, handler_ctrl_c);
2228 }
Denis Vlasenko05743d72008-02-10 12:10:08 +00002229#endif /* JOB */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002230
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002231 /* Go through list of pipes, (maybe) executing them. */
2232 for (; pi; pi = USE_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) {
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002233 IF_HAS_KEYWORDS(rword = pi->res_word;)
2234 IF_HAS_NO_KEYWORDS(rword = RES_NONE;)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002235 debug_printf_exec(": rword=%d cond_code=%d skip_more=%d\n",
2236 rword, cond_code, skip_more_for_this_rword);
Denis Vlasenko06810332007-05-21 23:30:54 +00002237#if ENABLE_HUSH_LOOPS
Denis Vlasenko4554b722008-07-29 13:36:09 +00002238 if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR)
Denis Vlasenko87a86552008-07-29 19:43:10 +00002239 && loop_top == NULL /* avoid bumping G.depth_of_loop twice */
Denis Vlasenko4554b722008-07-29 13:36:09 +00002240 ) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002241 /* start of a loop: remember where loop starts */
2242 loop_top = pi;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002243 G.depth_of_loop++;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002244 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002245#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002246 if (rword == skip_more_for_this_rword && flag_skip) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002247 if (pi->followup == PIPE_SEQ)
2248 flag_skip = 0;
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002249 /* it is "<false> && CMD" or "<true> || CMD"
2250 * and we should not execute CMD */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002251 continue;
2252 }
2253 flag_skip = 1;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002254 skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko06810332007-05-21 23:30:54 +00002255#if ENABLE_HUSH_IF
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002256 if (cond_code) {
2257 if (rword == RES_THEN) {
2258 /* "if <false> THEN cmd": skip cmd */
2259 continue;
2260 }
2261 } else {
2262 if (rword == RES_ELSE || rword == RES_ELIF) {
2263 /* "if <true> then ... ELSE/ELIF cmd":
2264 * skip cmd and all following ones */
2265 break;
2266 }
2267 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002268#endif
2269#if ENABLE_HUSH_LOOPS
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002270 if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002271 if (!for_lcur) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002272 /* first loop through for */
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002273
2274 static const char encoded_dollar_at[] ALIGN1 = {
2275 SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0'
2276 }; /* encoded representation of "$@" */
2277 static const char *const encoded_dollar_at_argv[] = {
2278 encoded_dollar_at, NULL
2279 }; /* argv list with one element: "$@" */
2280 char **vals;
2281
2282 vals = (char**)encoded_dollar_at_argv;
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002283 if (pi->next->res_word == RES_IN) {
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002284 /* if no variable values after "in" we skip "for" */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002285 if (!pi->next->cmds[0].argv)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002286 break;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002287 vals = pi->next->cmds[0].argv;
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002288 } /* else: "for var; do..." -> assume "$@" list */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002289 /* create list of variable values */
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002290 debug_print_strings("for_list made from", vals);
2291 for_list = expand_strvec_to_strvec(vals);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002292 for_lcur = for_list;
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002293 debug_print_strings("for_list", for_list);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002294 for_varname = pi->cmds[0].argv[0];
2295 pi->cmds[0].argv[0] = NULL;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002296 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002297 free(pi->cmds[0].argv[0]);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002298 if (!*for_lcur) {
Denis Vlasenko12acec52008-07-28 15:15:59 +00002299 /* "for" loop is over, clean up */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002300 free(for_list);
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002301 for_list = NULL;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002302 for_lcur = NULL;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002303 pi->cmds[0].argv[0] = for_varname;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002304 break;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002305 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002306 /* insert next value from for_lcur */
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002307//TODO: does it need escaping?
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002308 pi->cmds[0].argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++);
2309 pi->cmds[0].assignment_cnt = 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002310 }
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002311 if (rword == RES_IN) /* "for v IN list;..." - "in" has no cmds anyway */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002312 continue;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002313 if (rword == RES_DONE) {
2314 continue; /* "done" has no cmds too */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002315 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002316#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002317#if ENABLE_HUSH_CASE
2318 if (rword == RES_CASE) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002319 case_word = expand_strvec_to_string(pi->cmds->argv);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002320 continue;
2321 }
2322 if (rword == RES_MATCH) {
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00002323 char **argv;
2324
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002325 if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */
2326 break;
2327 /* all prev words didn't match, does this one match? */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002328 argv = pi->cmds->argv;
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00002329 while (*argv) {
2330 char *pattern = expand_string_to_string(*argv);
2331 /* TODO: which FNM_xxx flags to use? */
2332 cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0);
2333 free(pattern);
2334 if (cond_code == 0) { /* match! we will execute this branch */
2335 free(case_word); /* make future "word)" stop */
2336 case_word = NULL;
2337 break;
2338 }
2339 argv++;
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002340 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002341 continue;
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002342 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002343 if (rword == RES_CASEI) { /* inside of a case branch */
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002344 if (cond_code != 0)
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002345 continue; /* not matched yet, skip this pipe */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002346 }
2347#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002348 if (pi->num_cmds == 0)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002349 continue;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002350
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002351 /* After analyzing all keywords and conditions, we decided
2352 * to execute this pipe. NB: has to do checkjobs(NULL)
2353 * after run_pipe() to collect any background children,
2354 * even if list execution is to be stopped. */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002355 debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds);
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002356 {
2357 int r;
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002358#if ENABLE_HUSH_LOOPS
Denis Vlasenko87a86552008-07-29 19:43:10 +00002359 G.flag_break_continue = 0;
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002360#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002361 rcode = r = run_pipe(pi); /* NB: rcode is a smallint */
2362 if (r != -1) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002363 /* we only ran a builtin: rcode is already known
2364 * and we don't need to wait for anything. */
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002365#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002366 /* was it "break" or "continue"? */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002367 if (G.flag_break_continue) {
2368 smallint fbc = G.flag_break_continue;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002369 /* we might fall into outer *loop*,
2370 * don't want to break it too */
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002371 if (loop_top) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002372 G.depth_break_continue--;
2373 if (G.depth_break_continue == 0)
2374 G.flag_break_continue = 0;
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002375 /* else: e.g. "continue 2" should *break* once, *then* continue */
2376 } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002377 if (G.depth_break_continue != 0 || fbc == BC_BREAK)
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002378 goto check_jobs_and_break;
2379 /* "continue": simulate end of loop */
2380 rword = RES_DONE;
2381 continue;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002382 }
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002383#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002384 } else if (pi->followup == PIPE_BG) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002385 /* what does bash do with attempts to background builtins? */
2386 /* even bash 3.2 doesn't do that well with nested bg:
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002387 * try "{ { sleep 10; echo DEEP; } & echo HERE; } &".
2388 * I'm NOT treating inner &'s as jobs */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002389#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00002390 if (G.run_list_level == 1)
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002391 insert_bg_job(pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002392#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002393 rcode = 0; /* EXIT_SUCCESS */
2394 } else {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002395#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00002396 if (G.run_list_level == 1 && G.interactive_fd) {
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002397 /* waits for completion, then fg's main shell */
2398 rcode = checkjobs_and_fg_shell(pi);
2399 debug_printf_exec(": checkjobs_and_fg_shell returned %d\n", rcode);
2400 } else
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002401#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002402 { /* this one just waits for completion */
2403 rcode = checkjobs(pi);
2404 debug_printf_exec(": checkjobs returned %d\n", rcode);
2405 }
Eric Andersen25f27032001-04-26 23:22:31 +00002406 }
2407 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002408 debug_printf_exec(": setting last_return_code=%d\n", rcode);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002409 G.last_return_code = rcode;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002410
2411 /* Analyze how result affects subsequent commands */
Denis Vlasenko06810332007-05-21 23:30:54 +00002412#if ENABLE_HUSH_IF
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002413 if (rword == RES_IF || rword == RES_ELIF)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002414 cond_code = rcode;
Denis Vlasenko06810332007-05-21 23:30:54 +00002415#endif
2416#if ENABLE_HUSH_LOOPS
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002417 if (rword == RES_WHILE) {
Denis Vlasenko918a34b2008-07-28 23:17:31 +00002418 if (rcode) {
2419 rcode = 0; /* "while false; do...done" - exitcode 0 */
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002420 goto check_jobs_and_break;
Denis Vlasenko918a34b2008-07-28 23:17:31 +00002421 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002422 }
2423 if (rword == RES_UNTIL) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002424 if (!rcode) {
2425 check_jobs_and_break:
2426 checkjobs(NULL);
2427 break;
2428 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002429 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002430#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002431 if ((rcode == 0 && pi->followup == PIPE_OR)
2432 || (rcode != 0 && pi->followup == PIPE_AND)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002433 ) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002434 skip_more_for_this_rword = rword;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002435 }
Eric Andersen028b65b2001-06-28 01:10:11 +00002436 checkjobs(NULL);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002437 } /* for (pi) */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002438
2439#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00002440 if (G.ctrl_z_flag) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002441 /* ctrl-Z forked somewhere in the past, we are the child,
2442 * and now we completed running the list. Exit. */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002443//TODO: _exit?
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002444 exit(rcode);
2445 }
2446 ret:
Denis Vlasenko87a86552008-07-29 19:43:10 +00002447 if (!--G.run_list_level && G.interactive_fd) {
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00002448 signal(SIGTSTP, SIG_IGN);
2449 signal(SIGINT, SIG_IGN);
2450 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002451#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00002452 debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode);
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002453#if ENABLE_HUSH_LOOPS
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002454 if (loop_top)
Denis Vlasenko87a86552008-07-29 19:43:10 +00002455 G.depth_of_loop--;
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002456 free(for_list);
2457#endif
2458#if ENABLE_HUSH_CASE
2459 free(case_word);
2460#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002461 return rcode;
2462}
2463
Eric Andersen25f27032001-04-26 23:22:31 +00002464/* return code is the exit status of the pipe */
Eric Andersenbf7df042001-05-23 22:18:35 +00002465static int free_pipe(struct pipe *pi, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002466{
2467 char **p;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002468 struct command *command;
Eric Andersen25f27032001-04-26 23:22:31 +00002469 struct redir_struct *r, *rnext;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002470 int a, i, ret_code = 0;
Eric Andersen52a97ca2001-06-22 06:49:26 +00002471
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002472 if (pi->stopped_cmds > 0)
Eric Andersen52a97ca2001-06-22 06:49:26 +00002473 return ret_code;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002474 debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid());
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002475 for (i = 0; i < pi->num_cmds; i++) {
2476 command = &pi->cmds[i];
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002477 debug_printf_clean("%s command %d:\n", indenter(indent), i);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002478 if (command->argv) {
2479 for (a = 0, p = command->argv; *p; a++, p++) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002480 debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p);
Eric Andersen25f27032001-04-26 23:22:31 +00002481 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002482 free_strings(command->argv);
2483 command->argv = NULL;
2484 } else if (command->group) {
Denis Vlasenko371de4a2008-10-14 12:43:13 +00002485 debug_printf_clean("%s begin group (grp_type:%d)\n", indenter(indent), command->grp_type);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002486 ret_code = free_pipe_list(command->group, indent+3);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002487 debug_printf_clean("%s end group\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002488 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002489 debug_printf_clean("%s (nil)\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002490 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002491 for (r = command->redirects; r; r = rnext) {
Denis Vlasenko211b59b2008-06-23 16:28:53 +00002492 debug_printf_clean("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->rd_type].descrip);
Eric Andersen25f27032001-04-26 23:22:31 +00002493 if (r->dup == -1) {
Eric Andersen817e73c2001-06-06 17:56:09 +00002494 /* guard against the case >$FOO, where foo is unset or blank */
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00002495 if (r->rd_filename) {
2496 debug_printf_clean(" %s\n", r->rd_filename);
2497 free(r->rd_filename);
2498 r->rd_filename = NULL;
Eric Andersen817e73c2001-06-06 17:56:09 +00002499 }
Eric Andersen25f27032001-04-26 23:22:31 +00002500 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002501 debug_printf_clean("&%d\n", r->dup);
Eric Andersen25f27032001-04-26 23:22:31 +00002502 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002503 rnext = r->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002504 free(r);
2505 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002506 command->redirects = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002507 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002508 free(pi->cmds); /* children are an array, they get freed all at once */
2509 pi->cmds = NULL;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002510#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002511 free(pi->cmdtext);
2512 pi->cmdtext = NULL;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002513#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002514 return ret_code;
2515}
2516
Eric Andersenbf7df042001-05-23 22:18:35 +00002517static int free_pipe_list(struct pipe *head, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002518{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002519 int rcode = 0; /* if list has no members */
Eric Andersen25f27032001-04-26 23:22:31 +00002520 struct pipe *pi, *next;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002521
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002522 for (pi = head; pi; pi = next) {
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002523#if HAS_KEYWORDS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002524 debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002525#endif
Eric Andersenbf7df042001-05-23 22:18:35 +00002526 rcode = free_pipe(pi, indent);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002527 debug_printf_clean("%s pipe followup code %d\n", indenter(indent), pi->followup);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002528 next = pi->next;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002529 /*pi->next = NULL;*/
Eric Andersen25f27032001-04-26 23:22:31 +00002530 free(pi);
2531 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002532 return rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00002533}
2534
2535/* Select which version we will use */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002536static int run_and_free_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002537{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002538 int rcode = 0;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002539 debug_printf_exec("run_and_free_list entered\n");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002540 if (!G.fake_mode) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002541 debug_printf_exec(": run_list with %d members\n", pi->num_cmds);
Denis Vlasenko05743d72008-02-10 12:10:08 +00002542 rcode = run_list(pi);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002543 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002544 /* free_pipe_list has the side effect of clearing memory.
Denis Vlasenko05743d72008-02-10 12:10:08 +00002545 * In the long run that function can be merged with run_list,
Eric Andersen25f27032001-04-26 23:22:31 +00002546 * but doing that now would hobble the debugging effort. */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002547 free_pipe_list(pi, /* indent: */ 0);
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002548 debug_printf_exec("run_and_free_list return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002549 return rcode;
2550}
2551
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002552
Denis Vlasenko170435c2007-05-23 13:01:10 +00002553/* expand_strvec_to_strvec() takes a list of strings, expands
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002554 * all variable references within and returns a pointer to
2555 * a list of expanded strings, possibly with larger number
2556 * of strings. (Think VAR="a b"; echo $VAR).
2557 * This new list is allocated as a single malloc block.
2558 * NULL-terminated list of char* pointers is at the beginning of it,
2559 * followed by strings themself.
2560 * Caller can deallocate entire list by single free(list). */
2561
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002562/* Store given string, finalizing the word and starting new one whenever
Denis Vlasenko87a86552008-07-29 19:43:10 +00002563 * we encounter IFS char(s). This is used for expanding variable values.
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002564 * End-of-string does NOT finalize word: think about 'echo -$VAR-' */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002565static int expand_on_ifs(o_string *output, int n, const char *str)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002566{
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002567 while (1) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002568 int word_len = strcspn(str, G.ifs);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002569 if (word_len) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00002570 if (output->o_quote || !output->o_glob)
2571 o_addQstr(output, str, word_len);
2572 else /* protect backslashes against globbing up :) */
2573 o_addstr_duplicate_backslash(output, str, word_len);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002574 str += word_len;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002575 }
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002576 if (!*str) /* EOL - do not finalize word */
2577 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002578 o_addchr(output, '\0');
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002579 debug_print_list("expand_on_ifs", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002580 n = o_save_ptr(output, n);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002581 str += strspn(str, G.ifs); /* skip ifs chars */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002582 }
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002583 debug_print_list("expand_on_ifs[1]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002584 return n;
2585}
2586
2587/* Expand all variable references in given string, adding words to list[]
2588 * at n, n+1,... positions. Return updated n (so that list[n] is next one
2589 * to be filled). This routine is extremely tricky: has to deal with
2590 * variables/parameters with whitespace, $* and $@, and constructs like
2591 * 'echo -$*-'. If you play here, you must run testsuite afterwards! */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002592static int expand_vars_to_list(o_string *output, int n, char *arg, char or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002593{
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002594 /* or_mask is either 0 (normal case) or 0x80
Denis Vlasenko55789c62008-06-18 16:30:42 +00002595 * (expansion of right-hand side of assignment == 1-element expand.
2596 * It will also do no globbing, and thus we must not backslash-quote!) */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002597
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002598 char first_ch, ored_ch;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002599 int i;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002600 const char *val;
2601 char *p;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002602
2603 ored_ch = 0;
2604
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002605 debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002606 debug_print_list("expand_vars_to_list", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002607 n = o_save_ptr(output, n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002608 debug_print_list("expand_vars_to_list[0]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002609
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002610 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) {
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002611#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002612 o_string subst_result = NULL_O_STRING;
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002613#endif
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002614 o_addstr(output, arg, p - arg);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002615 debug_print_list("expand_vars_to_list[1]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002616 arg = ++p;
2617 p = strchr(p, SPECIAL_VAR_SYMBOL);
2618
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002619 first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00002620 /* "$@" is special. Even if quoted, it can still
2621 * expand to nothing (not even an empty string) */
2622 if ((first_ch & 0x7f) != '@')
2623 ored_ch |= first_ch;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002624 val = NULL;
2625 switch (first_ch & 0x7f) {
2626 /* Highest bit in first_ch indicates that var is double-quoted */
2627 case '$': /* pid */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002628 val = utoa(G.root_pid);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002629 break;
2630 case '!': /* bg pid */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002631 val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)"";
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002632 break;
2633 case '?': /* exitcode */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002634 val = utoa(G.last_return_code);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002635 break;
2636 case '#': /* argc */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002637 val = utoa(G.global_argc ? G.global_argc-1 : 0);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002638 break;
2639 case '*':
2640 case '@':
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002641 i = 1;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002642 if (!G.global_argv[i])
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002643 break;
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00002644 ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002645 if (!(first_ch & 0x80)) { /* unquoted $* or $@ */
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002646 smallint sv = output->o_quote;
2647 /* unquoted var's contents should be globbed, so don't quote */
2648 output->o_quote = 0;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002649 while (G.global_argv[i]) {
2650 n = expand_on_ifs(output, n, G.global_argv[i]);
2651 debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1);
2652 if (G.global_argv[i++][0] && G.global_argv[i]) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002653 /* this argv[] is not empty and not last:
2654 * put terminating NUL, start new word */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002655 o_addchr(output, '\0');
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002656 debug_print_list("expand_vars_to_list[2]", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002657 n = o_save_ptr(output, n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002658 debug_print_list("expand_vars_to_list[3]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002659 }
2660 }
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002661 output->o_quote = sv;
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002662 } else
2663 /* If or_mask is nonzero, we handle assignment 'a=....$@.....'
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00002664 * and in this case should treat it like '$*' - see 'else...' below */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002665 if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002666 while (1) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002667 o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i]));
2668 if (++i >= G.global_argc)
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002669 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002670 o_addchr(output, '\0');
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002671 debug_print_list("expand_vars_to_list[4]", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002672 n = o_save_ptr(output, n);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002673 }
2674 } else { /* quoted $*: add as one word */
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002675 while (1) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002676 o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i]));
2677 if (!G.global_argv[++i])
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002678 break;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002679 if (G.ifs[0])
2680 o_addchr(output, G.ifs[0]);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002681 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002682 }
2683 break;
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00002684 case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */
2685 /* "Empty variable", used to make "" etc to not disappear */
2686 arg++;
2687 ored_ch = 0x80;
2688 break;
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002689#if ENABLE_HUSH_TICK
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00002690 case '`': { /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002691 struct in_str input;
2692 *p = '\0';
2693 arg++;
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002694//TODO: can we just stuff it into "output" directly?
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002695 debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002696 setup_string_in_str(&input, arg);
2697 process_command_subs(&subst_result, &input, NULL);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002698 debug_printf_subst("SUBST RES '%s'\n", subst_result.data);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002699 val = subst_result.data;
2700 goto store_val;
2701 }
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002702#endif
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00002703 default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002704 *p = '\0';
2705 arg[0] = first_ch & 0x7f;
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002706 if (isdigit(arg[0])) {
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002707 i = xatoi_u(arg);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002708 if (i < G.global_argc)
2709 val = G.global_argv[i];
Denis Vlasenko55789c62008-06-18 16:30:42 +00002710 /* else val remains NULL: $N with too big N */
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002711 } else
2712 val = lookup_param(arg);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002713 arg[0] = first_ch;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002714#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002715 store_val:
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002716#endif
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002717 *p = SPECIAL_VAR_SYMBOL;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002718 if (!(first_ch & 0x80)) { /* unquoted $VAR */
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002719 debug_printf_expand("unquoted '%s', output->o_quote:%d\n", val, output->o_quote);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002720 if (val) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00002721 /* unquoted var's contents should be globbed, so don't quote */
2722 smallint sv = output->o_quote;
2723 output->o_quote = 0;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002724 n = expand_on_ifs(output, n, val);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002725 val = NULL;
Denis Vlasenko55789c62008-06-18 16:30:42 +00002726 output->o_quote = sv;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002727 }
Denis Vlasenko55789c62008-06-18 16:30:42 +00002728 } else { /* quoted $VAR, val will be appended below */
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002729 debug_printf_expand("quoted '%s', output->o_quote:%d\n", val, output->o_quote);
Denis Vlasenko55789c62008-06-18 16:30:42 +00002730 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002731 }
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002732 if (val) {
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002733 o_addQstr(output, val, strlen(val));
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002734 }
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002735
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002736#if ENABLE_HUSH_TICK
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002737 o_free(&subst_result);
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002738#endif
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002739 arg = ++p;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002740 } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */
2741
Denis Vlasenko2e76c3f2008-06-10 18:27:50 +00002742 if (arg[0]) {
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002743 debug_print_list("expand_vars_to_list[a]", output, n);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002744 /* this part is literal, and it was already pre-quoted
2745 * if needed (much earlier), do not use o_addQstr here! */
2746 o_addstr(output, arg, strlen(arg) + 1);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002747 debug_print_list("expand_vars_to_list[b]", output, n);
Denis Vlasenko2e76c3f2008-06-10 18:27:50 +00002748 } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */
2749 && !(ored_ch & 0x80) /* and all vars were not quoted. */
2750 ) {
2751 n--;
2752 /* allow to reuse list[n] later without re-growth */
2753 output->has_empty_slot = 1;
2754 } else {
2755 o_addchr(output, '\0');
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002756 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002757 return n;
2758}
2759
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002760static char **expand_variables(char **argv, int or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002761{
2762 int n;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002763 char **list;
2764 char **v;
2765 o_string output = NULL_O_STRING;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002766
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002767 if (or_mask & 0x100) {
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002768 output.o_quote = 1; /* protect against globbing for "$var" */
2769 /* (unquoted $var will temporarily switch it off) */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002770 output.o_glob = 1;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002771 }
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002772
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002773 n = 0;
2774 v = argv;
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002775 while (*v) {
2776 n = expand_vars_to_list(&output, n, *v, (char)or_mask);
2777 v++;
2778 }
2779 debug_print_list("expand_variables", &output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002780
Denis Vlasenko82dfec32008-06-16 12:47:11 +00002781 /* output.data (malloced in one block) gets returned in "list" */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002782 list = o_finalize_list(&output, n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002783 debug_print_strings("expand_variables[1]", list);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002784 return list;
2785}
2786
Denis Vlasenko170435c2007-05-23 13:01:10 +00002787static char **expand_strvec_to_strvec(char **argv)
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002788{
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002789 return expand_variables(argv, 0x100);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002790}
2791
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002792/* Used for expansion of right hand of assignments */
Denis Vlasenko82dfec32008-06-16 12:47:11 +00002793/* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs
2794 * "v=/bin/c*" */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002795static char *expand_string_to_string(const char *str)
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002796{
2797 char *argv[2], **list;
2798
2799 argv[0] = (char*)str;
2800 argv[1] = NULL;
2801 list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002802 if (HUSH_DEBUG)
Denis Vlasenko170435c2007-05-23 13:01:10 +00002803 if (!list[0] || list[1])
2804 bb_error_msg_and_die("BUG in varexp2");
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002805 /* actually, just move string 2*sizeof(char*) bytes back */
Denis Vlasenko786ce172009-03-21 21:51:11 +00002806 overlapping_strcpy((char*)list, list[0]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00002807 debug_printf_expand("string_to_string='%s'\n", (char*)list);
2808 return (char*)list;
2809}
2810
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002811/* Used for "eval" builtin */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002812static char* expand_strvec_to_string(char **argv)
2813{
2814 char **list;
2815
2816 list = expand_variables(argv, 0x80);
2817 /* Convert all NULs to spaces */
2818 if (list[0]) {
2819 int n = 1;
2820 while (list[n]) {
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002821 if (HUSH_DEBUG)
Denis Vlasenko170435c2007-05-23 13:01:10 +00002822 if (list[n-1] + strlen(list[n-1]) + 1 != list[n])
2823 bb_error_msg_and_die("BUG in varexp3");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002824 list[n][-1] = ' '; /* TODO: or to G.ifs[0]? */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002825 n++;
2826 }
2827 }
Denis Vlasenko786ce172009-03-21 21:51:11 +00002828 overlapping_strcpy((char*)list, list[0]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00002829 debug_printf_expand("strvec_to_string='%s'\n", (char*)list);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002830 return (char*)list;
2831}
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002832
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002833
2834/* Used to get/check local shell variables */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002835static struct variable *get_local_var(const char *name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002836{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002837 struct variable *cur;
2838 int len;
Eric Andersenf72f5622001-05-15 23:21:41 +00002839
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002840 if (!name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002841 return NULL;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002842 len = strlen(name);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002843 for (cur = G.top_var; cur; cur = cur->next) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002844 if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=')
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002845 return cur;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002846 }
Eric Andersenf72f5622001-05-15 23:21:41 +00002847 return NULL;
2848}
2849
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002850/* str holds "NAME=VAL" and is expected to be malloced.
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002851 * We take ownership of it. */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002852static int set_local_var(char *str, int flg_export)
Eric Andersen78a7c992001-05-15 16:30:25 +00002853{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002854 struct variable *cur;
2855 char *value;
2856 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002857
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002858 value = strchr(str, '=');
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002859 if (!value) { /* not expected to ever happen? */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002860 free(str);
Eric Andersen99785762001-05-22 21:37:48 +00002861 return -1;
2862 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002863
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002864 name_len = value - str + 1; /* including '=' */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002865 cur = G.top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002866 while (1) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002867 if (strncmp(cur->varstr, str, name_len) != 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002868 if (!cur->next) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002869 /* Bail out. Note that now cur points
2870 * to last var in linked list */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002871 break;
Eric Andersen20a69a72001-05-15 17:24:44 +00002872 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002873 cur = cur->next;
2874 continue;
Eric Andersen20a69a72001-05-15 17:24:44 +00002875 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002876 /* We found an existing var with this name */
2877 *value = '\0';
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002878 if (cur->flg_read_only) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002879 bb_error_msg("%s: readonly variable", str);
2880 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002881 return -1;
2882 }
Denis Vlasenkof886fd22008-10-13 12:36:05 +00002883 debug_printf_env("%s: unsetenv '%s'\n", __func__, str);
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002884 unsetenv(str); /* just in case */
2885 *value = '=';
2886 if (strcmp(cur->varstr, str) == 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002887 free_and_exp:
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002888 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002889 goto exp;
2890 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002891 if (cur->max_len >= strlen(str)) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002892 /* This one is from startup env, reuse space */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002893 strcpy(cur->varstr, str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002894 goto free_and_exp;
2895 }
2896 /* max_len == 0 signifies "malloced" var, which we can
2897 * (and has to) free */
2898 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002899 free(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002900 cur->max_len = 0;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002901 goto set_str_and_exp;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002902 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002903
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002904 /* Not found - create next variable struct */
2905 cur->next = xzalloc(sizeof(*cur));
2906 cur = cur->next;
2907
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002908 set_str_and_exp:
2909 cur->varstr = str;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002910 exp:
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002911 if (flg_export)
2912 cur->flg_export = 1;
Denis Vlasenkof886fd22008-10-13 12:36:05 +00002913 if (cur->flg_export) {
2914 debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr);
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002915 return putenv(cur->varstr);
Denis Vlasenkof886fd22008-10-13 12:36:05 +00002916 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002917 return 0;
Eric Andersen20a69a72001-05-15 17:24:44 +00002918}
2919
Eric Andersenf72f5622001-05-15 23:21:41 +00002920static void unset_local_var(const char *name)
Eric Andersen20a69a72001-05-15 17:24:44 +00002921{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002922 struct variable *cur;
2923 struct variable *prev = prev; /* for gcc */
2924 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002925
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002926 if (!name)
2927 return;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002928 name_len = strlen(name);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002929 cur = G.top_var;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002930 while (cur) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002931 if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002932 if (cur->flg_read_only) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002933 bb_error_msg("%s: readonly variable", name);
Eric Andersen94ac2442001-05-22 19:05:18 +00002934 return;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002935 }
Denis Vlasenkof886fd22008-10-13 12:36:05 +00002936 /* prev is ok to use here because 1st variable, HUSH_VERSION,
2937 * is ro, and we cannot reach this code on the 1st pass */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002938 prev->next = cur->next;
Denis Vlasenkof886fd22008-10-13 12:36:05 +00002939 debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +00002940 bb_unsetenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002941 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002942 free(cur->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002943 free(cur);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002944 return;
Eric Andersenf72f5622001-05-15 23:21:41 +00002945 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002946 prev = cur;
2947 cur = cur->next;
Eric Andersen20a69a72001-05-15 17:24:44 +00002948 }
Eric Andersen78a7c992001-05-15 16:30:25 +00002949}
2950
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002951/* The src parameter allows us to peek forward to a possible &n syntax
Eric Andersen25f27032001-04-26 23:22:31 +00002952 * for file descriptor duplication, e.g., "2>&1".
2953 * Return code is 0 normally, 1 if a syntax error is detected in src.
2954 * Resource errors (in xmalloc) cause the process to exit */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002955static int setup_redirect(struct parse_context *ctx, int fd, redir_type style,
Eric Andersen25f27032001-04-26 23:22:31 +00002956 struct in_str *input)
2957{
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002958 struct command *command = ctx->command;
2959 struct redir_struct *redir = command->redirects;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002960 struct redir_struct *last_redir = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002961
2962 /* Create a new redir_struct and drop it onto the end of the linked list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002963 while (redir) {
2964 last_redir = redir;
2965 redir = redir->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002966 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00002967 redir = xzalloc(sizeof(struct redir_struct));
2968 /* redir->next = NULL; */
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00002969 /* redir->rd_filename = NULL; */
Eric Andersen25f27032001-04-26 23:22:31 +00002970 if (last_redir) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002971 last_redir->next = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002972 } else {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002973 command->redirects = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002974 }
2975
Denis Vlasenko55789c62008-06-18 16:30:42 +00002976 redir->rd_type = style;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002977 redir->fd = (fd == -1) ? redir_table[style].default_fd : fd;
Eric Andersen25f27032001-04-26 23:22:31 +00002978
2979 debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
2980
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002981 /* Check for a '2>&1' type redirect */
Eric Andersen25f27032001-04-26 23:22:31 +00002982 redir->dup = redirect_dup_num(input);
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002983 if (redir->dup == -2)
2984 return 1; /* syntax error */
Eric Andersen25f27032001-04-26 23:22:31 +00002985 if (redir->dup != -1) {
2986 /* Erik had a check here that the file descriptor in question
Eric Andersen83a2ae22001-05-07 17:59:25 +00002987 * is legit; I postpone that to "run time"
2988 * A "-" representation of "close me" shows up as a -3 here */
Eric Andersen25f27032001-04-26 23:22:31 +00002989 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
2990 } else {
2991 /* We do _not_ try to open the file that src points to,
2992 * since we need to return and let src be expanded first.
2993 * Set ctx->pending_redirect, so we know what to do at the
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002994 * end of the next parsed word. */
Eric Andersen25f27032001-04-26 23:22:31 +00002995 ctx->pending_redirect = redir;
2996 }
2997 return 0;
2998}
2999
Denis Vlasenkoac678ec2007-04-16 22:32:04 +00003000static struct pipe *new_pipe(void)
3001{
Eric Andersen25f27032001-04-26 23:22:31 +00003002 struct pipe *pi;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003003 pi = xzalloc(sizeof(struct pipe));
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003004 /*pi->followup = 0; - deliberately invalid value */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003005 /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */
Eric Andersen25f27032001-04-26 23:22:31 +00003006 return pi;
3007}
3008
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003009static void initialize_context(struct parse_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00003010{
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003011 memset(ctx, 0, sizeof(*ctx));
Denis Vlasenko1a735862007-05-23 00:32:25 +00003012 ctx->pipe = ctx->list_head = new_pipe();
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003013 /* Create the memory for command, roughly:
3014 * ctx->pipe->cmds = new struct command;
3015 * ctx->command = &ctx->pipe->cmds[0];
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003016 */
3017 done_command(ctx);
Eric Andersen25f27032001-04-26 23:22:31 +00003018}
3019
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003020/* If a reserved word is found and processed, parse context is modified
3021 * and 1 is returned.
3022 * Handles if, then, elif, else, fi, for, while, until, do, done.
Eric Andersen25f27032001-04-26 23:22:31 +00003023 * case, function, and select are obnoxious, save those for later.
3024 */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003025#if HAS_KEYWORDS
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003026struct reserved_combo {
3027 char literal[6];
3028 unsigned char res;
3029 unsigned char assignment_flag;
3030 int flag;
3031};
3032enum {
3033 FLAG_END = (1 << RES_NONE ),
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003034#if ENABLE_HUSH_IF
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003035 FLAG_IF = (1 << RES_IF ),
3036 FLAG_THEN = (1 << RES_THEN ),
3037 FLAG_ELIF = (1 << RES_ELIF ),
3038 FLAG_ELSE = (1 << RES_ELSE ),
3039 FLAG_FI = (1 << RES_FI ),
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003040#endif
3041#if ENABLE_HUSH_LOOPS
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003042 FLAG_FOR = (1 << RES_FOR ),
3043 FLAG_WHILE = (1 << RES_WHILE),
3044 FLAG_UNTIL = (1 << RES_UNTIL),
3045 FLAG_DO = (1 << RES_DO ),
3046 FLAG_DONE = (1 << RES_DONE ),
3047 FLAG_IN = (1 << RES_IN ),
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003048#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003049#if ENABLE_HUSH_CASE
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003050 FLAG_MATCH = (1 << RES_MATCH),
3051 FLAG_ESAC = (1 << RES_ESAC ),
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003052#endif
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003053 FLAG_START = (1 << RES_XXXX ),
3054};
3055
3056static const struct reserved_combo* match_reserved_word(o_string *word)
3057{
Eric Andersen25f27032001-04-26 23:22:31 +00003058 /* Mostly a list of accepted follow-up reserved words.
3059 * FLAG_END means we are done with the sequence, and are ready
3060 * to turn the compound list into a command.
3061 * FLAG_START means the word must start a new compound list.
3062 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003063 static const struct reserved_combo reserved_list[] = {
Denis Vlasenko06810332007-05-21 23:30:54 +00003064#if ENABLE_HUSH_IF
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003065 { "!", RES_NONE, NOT_ASSIGNMENT , 0 },
3066 { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START },
3067 { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
3068 { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN },
3069 { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI },
3070 { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END },
Denis Vlasenko06810332007-05-21 23:30:54 +00003071#endif
3072#if ENABLE_HUSH_LOOPS
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003073 { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START },
3074 { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START },
3075 { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START },
3076 { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO },
3077 { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE },
3078 { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END },
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003079#endif
3080#if ENABLE_HUSH_CASE
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003081 { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START },
3082 { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END },
Denis Vlasenko06810332007-05-21 23:30:54 +00003083#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003084 };
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003085 const struct reserved_combo *r;
3086
3087 for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) {
3088 if (strcmp(word->data, r->literal) == 0)
3089 return r;
3090 }
3091 return NULL;
3092}
3093static int reserved_word(o_string *word, struct parse_context *ctx)
3094{
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003095#if ENABLE_HUSH_CASE
3096 static const struct reserved_combo reserved_match = {
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003097 "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003098 };
3099#endif
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003100 const struct reserved_combo *r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003101
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003102 r = match_reserved_word(word);
3103 if (!r)
3104 return 0;
3105
3106 debug_printf("found reserved word %s, res %d\n", r->literal, r->res);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003107#if ENABLE_HUSH_CASE
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003108 if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE)
3109 /* "case word IN ..." - IN part starts first match part */
3110 r = &reserved_match;
3111 else
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003112#endif
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003113 if (r->flag == 0) { /* '!' */
3114 if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003115 syntax(NULL);
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003116 IF_HAS_KEYWORDS(ctx->ctx_res_w = RES_SNTX;)
Eric Andersen25f27032001-04-26 23:22:31 +00003117 }
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003118 ctx->ctx_inverted = 1;
Denis Vlasenko1a735862007-05-23 00:32:25 +00003119 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003120 }
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003121 if (r->flag & FLAG_START) {
3122 struct parse_context *new;
3123 debug_printf("push stack\n");
3124 new = xmalloc(sizeof(*new));
3125 *new = *ctx; /* physical copy */
3126 initialize_context(ctx);
3127 ctx->stack = new;
3128 } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) {
3129 syntax(NULL);
3130 ctx->ctx_res_w = RES_SNTX;
3131 return 1;
3132 }
3133 ctx->ctx_res_w = r->res;
3134 ctx->old_flag = r->flag;
3135 if (ctx->old_flag & FLAG_END) {
3136 struct parse_context *old;
3137 debug_printf("pop stack\n");
3138 done_pipe(ctx, PIPE_SEQ);
3139 old = ctx->stack;
3140 old->command->group = ctx->list_head;
Denis Vlasenko371de4a2008-10-14 12:43:13 +00003141 old->command->grp_type = GRP_NORMAL;
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003142 *ctx = *old; /* physical copy */
3143 free(old);
3144 }
3145 word->o_assignment = r->assignment_flag;
3146 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003147}
Denis Vlasenko06810332007-05-21 23:30:54 +00003148#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003149
Denis Vlasenkoddc8ae32008-10-14 12:50:34 +00003150//TODO: many, many callers don't check error from done_word()
3151
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003152/* Word is complete, look at it and update parsing context.
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003153 * Normal return is 0. Syntax errors return 1. */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003154static int done_word(o_string *word, struct parse_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00003155{
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003156 struct command *command = ctx->command;
Eric Andersen25f27032001-04-26 23:22:31 +00003157
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003158 debug_printf_parse("done_word entered: '%s' %p\n", word->data, command);
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003159 if (word->length == 0 && word->nonnull == 0) {
3160 debug_printf_parse("done_word return 0: true null, ignored\n");
3161 return 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003162 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003163 /* If this word wasn't an assignment, next ones definitely
3164 * can't be assignments. Even if they look like ones. */
3165 if (word->o_assignment != DEFINITELY_ASSIGNMENT
3166 && word->o_assignment != WORD_IS_KEYWORD
3167 ) {
3168 word->o_assignment = NOT_ASSIGNMENT;
3169 } else {
3170 if (word->o_assignment == DEFINITELY_ASSIGNMENT)
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003171 command->assignment_cnt++;
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003172 word->o_assignment = MAYBE_ASSIGNMENT;
3173 }
3174
Eric Andersen25f27032001-04-26 23:22:31 +00003175 if (ctx->pending_redirect) {
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003176 /* We do not glob in e.g. >*.tmp case. bash seems to glob here
3177 * only if run as "bash", not "sh" */
3178 ctx->pending_redirect->rd_filename = xstrdup(word->data);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003179 word->o_assignment = NOT_ASSIGNMENT;
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003180 debug_printf("word stored in rd_filename: '%s'\n", word->data);
Eric Andersen25f27032001-04-26 23:22:31 +00003181 } else {
Denis Vlasenko395ae452008-07-14 06:29:38 +00003182 /* "{ echo foo; } echo bar" - bad */
3183 /* NB: bash allows e.g. "if true; then { echo foo; } fi". TODO? */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003184 if (command->group) {
Denis Vlasenko395ae452008-07-14 06:29:38 +00003185 syntax(NULL);
3186 debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n");
3187 return 1;
3188 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003189#if HAS_KEYWORDS
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003190#if ENABLE_HUSH_CASE
Denis Vlasenko757361f2008-07-14 08:26:47 +00003191 if (ctx->ctx_dsemicolon
3192 && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */
3193 ) {
Denis Vlasenko395ae452008-07-14 06:29:38 +00003194 /* already done when ctx_dsemicolon was set to 1: */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003195 /* ctx->ctx_res_w = RES_MATCH; */
3196 ctx->ctx_dsemicolon = 0;
3197 } else
3198#endif
3199
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003200 if (!command->argv /* if it's the first word... */
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003201#if ENABLE_HUSH_LOOPS
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003202 && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */
3203 && ctx->ctx_res_w != RES_IN
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003204#endif
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003205 ) {
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003206 debug_printf_parse(": checking '%s' for reserved-ness\n", word->data);
3207 if (reserved_word(word, ctx)) {
3208 o_reset(word);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003209 debug_printf_parse("done_word return %d\n", (ctx->ctx_res_w == RES_SNTX));
3210 return (ctx->ctx_res_w == RES_SNTX);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003211 }
Eric Andersen25f27032001-04-26 23:22:31 +00003212 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003213#endif
Denis Vlasenkoff182a32008-07-05 20:29:59 +00003214 if (word->nonnull /* word had "xx" or 'xx' at least as part of it. */
Denis Vlasenko55789c62008-06-18 16:30:42 +00003215 /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */
3216 && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL)
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003217 /* (otherwise it's known to be not empty and is already safe) */
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003218 ) {
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003219 /* exclude "$@" - it can expand to no word despite "" */
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00003220 char *p = word->data;
3221 while (p[0] == SPECIAL_VAR_SYMBOL
3222 && (p[1] & 0x7f) == '@'
3223 && p[2] == SPECIAL_VAR_SYMBOL
3224 ) {
3225 p += 3;
3226 }
3227 if (p == word->data || p[0] != '\0') {
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003228 /* saw no "$@", or not only "$@" but some
3229 * real text is there too */
3230 /* insert "empty variable" reference, this makes
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00003231 * e.g. "", $empty"" etc to not disappear */
3232 o_addchr(word, SPECIAL_VAR_SYMBOL);
3233 o_addchr(word, SPECIAL_VAR_SYMBOL);
3234 }
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00003235 }
Denis Vlasenko22d10a02008-10-13 08:53:43 +00003236 command->argv = add_string_to_strings(command->argv, xstrdup(word->data));
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003237 debug_print_strings("word appended to argv", command->argv);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003238 }
Eric Andersen25f27032001-04-26 23:22:31 +00003239
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003240 o_reset(word);
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003241 ctx->pending_redirect = NULL;
3242
Denis Vlasenko06810332007-05-21 23:30:54 +00003243#if ENABLE_HUSH_LOOPS
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003244 /* Force FOR to have just one word (variable name) */
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003245 /* NB: basically, this makes hush see "for v in ..." syntax as if
3246 * as it is "for v; in ...". FOR and IN become two pipe structs
3247 * in parse tree. */
3248 if (ctx->ctx_res_w == RES_FOR) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003249//TODO: check that command->argv[0] is a valid variable name!
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003250 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003251 }
Denis Vlasenko06810332007-05-21 23:30:54 +00003252#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003253#if ENABLE_HUSH_CASE
3254 /* Force CASE to have just one word */
3255 if (ctx->ctx_res_w == RES_CASE) {
3256 done_pipe(ctx, PIPE_SEQ);
3257 }
3258#endif
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003259 debug_printf_parse("done_word return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003260 return 0;
3261}
3262
Denis Vlasenko6eaf8de2008-06-17 12:09:21 +00003263/* Command (member of a pipe) is complete. The only possible error here
3264 * is out of memory, in which case xmalloc exits. */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003265static int done_command(struct parse_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00003266{
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003267 /* The command is really already in the pipe structure, so
3268 * advance the pipe counter and make a new, null command. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003269 struct pipe *pi = ctx->pipe;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003270 struct command *command = ctx->command;
Eric Andersen25f27032001-04-26 23:22:31 +00003271
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003272 if (command) {
3273 if (command->group == NULL
3274 && command->argv == NULL
3275 && command->redirects == NULL
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003276 ) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003277 debug_printf_parse("done_command: skipping null cmd, num_cmds=%d\n", pi->num_cmds);
3278 return pi->num_cmds;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003279 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003280 pi->num_cmds++;
3281 debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds);
Eric Andersen25f27032001-04-26 23:22:31 +00003282 } else {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003283 debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds);
Eric Andersen25f27032001-04-26 23:22:31 +00003284 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003285
3286 /* Only real trickiness here is that the uncommitted
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003287 * command structure is not counted in pi->num_cmds. */
3288 pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1));
3289 command = &pi->cmds[pi->num_cmds];
3290 memset(command, 0, sizeof(*command));
Eric Andersen25f27032001-04-26 23:22:31 +00003291
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003292 ctx->command = command;
Eric Andersen25f27032001-04-26 23:22:31 +00003293 /* but ctx->pipe and ctx->list_head remain unchanged */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003294
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003295 return pi->num_cmds; /* used only for 0/nonzero check */
Eric Andersen25f27032001-04-26 23:22:31 +00003296}
3297
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003298static void done_pipe(struct parse_context *ctx, pipe_style type)
Eric Andersen25f27032001-04-26 23:22:31 +00003299{
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003300 int not_null;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003301
3302 debug_printf_parse("done_pipe entered, followup %d\n", type);
Denis Vlasenko395ae452008-07-14 06:29:38 +00003303 /* Close previous command */
3304 not_null = done_command(ctx);
Eric Andersen25f27032001-04-26 23:22:31 +00003305 ctx->pipe->followup = type;
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003306 IF_HAS_KEYWORDS(ctx->pipe->pi_inverted = ctx->ctx_inverted;)
3307 IF_HAS_KEYWORDS(ctx->ctx_inverted = 0;)
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003308 IF_HAS_KEYWORDS(ctx->pipe->res_word = ctx->ctx_res_w;)
Denis Vlasenko395ae452008-07-14 06:29:38 +00003309
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003310 /* Without this check, even just <enter> on command line generates
3311 * tree of three NOPs (!). Which is harmless but annoying.
Denis Vlasenko6eaf8de2008-06-17 12:09:21 +00003312 * IOW: it is safe to do it unconditionally.
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003313 * RES_NONE case is for "for a in; do ..." (empty IN set)
3314 * to work, possibly other cases too. */
3315 if (not_null IF_HAS_KEYWORDS(|| ctx->ctx_res_w != RES_NONE)) {
Denis Vlasenko395ae452008-07-14 06:29:38 +00003316 struct pipe *new_p;
3317 debug_printf_parse("done_pipe: adding new pipe: "
Denis Vlasenko757361f2008-07-14 08:26:47 +00003318 "not_null:%d ctx->ctx_res_w:%d\n",
Denis Vlasenko395ae452008-07-14 06:29:38 +00003319 not_null, ctx->ctx_res_w);
3320 new_p = new_pipe();
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003321 ctx->pipe->next = new_p;
3322 ctx->pipe = new_p;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003323 ctx->command = NULL; /* needed! */
Denis Vlasenko395ae452008-07-14 06:29:38 +00003324 /* RES_THEN, RES_DO etc are "sticky" -
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003325 * they remain set for commands inside if/while.
3326 * This is used to control execution.
3327 * RES_FOR and RES_IN are NOT sticky (needed to support
3328 * cases where variable or value happens to match a keyword):
3329 */
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003330#if ENABLE_HUSH_LOOPS
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003331 if (ctx->ctx_res_w == RES_FOR
3332 || ctx->ctx_res_w == RES_IN)
3333 ctx->ctx_res_w = RES_NONE;
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003334#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003335#if ENABLE_HUSH_CASE
3336 if (ctx->ctx_res_w == RES_MATCH)
3337 ctx->ctx_res_w = RES_CASEI;
3338#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003339 /* Create the memory for command, roughly:
3340 * ctx->pipe->cmds = new struct command;
3341 * ctx->command = &ctx->pipe->cmds[0];
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003342 */
3343 done_command(ctx);
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003344 }
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003345 debug_printf_parse("done_pipe return\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003346}
3347
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003348/* Peek ahead in the in_str to find out if we have a "&n" construct,
Eric Andersen25f27032001-04-26 23:22:31 +00003349 * as in "2>&1", that represents duplicating a file descriptor.
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003350 * Return either -2 (syntax error), -1 (no &), or the number found.
Eric Andersen25f27032001-04-26 23:22:31 +00003351 */
3352static int redirect_dup_num(struct in_str *input)
3353{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003354 int ch, d = 0, ok = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003355 ch = i_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003356 if (ch != '&') return -1;
3357
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003358 i_getch(input); /* get the & */
3359 ch = i_peek(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003360 if (ch == '-') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003361 i_getch(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003362 return -3; /* "-" represents "close me" */
3363 }
3364 while (isdigit(ch)) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00003365 d = d*10 + (ch-'0');
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003366 ok = 1;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003367 i_getch(input);
3368 ch = i_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003369 }
3370 if (ok) return d;
3371
Manuel Novoa III cad53642003-03-19 09:13:01 +00003372 bb_error_msg("ambiguous redirect");
Eric Andersen25f27032001-04-26 23:22:31 +00003373 return -2;
3374}
3375
3376/* If a redirect is immediately preceded by a number, that number is
3377 * supposed to tell which file descriptor to redirect. This routine
3378 * looks for such preceding numbers. In an ideal world this routine
3379 * needs to handle all the following classes of redirects...
3380 * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
3381 * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
3382 * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
3383 * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
3384 * A -1 output from this program means no valid number was found, so the
3385 * caller should use the appropriate default for this redirection.
3386 */
3387static int redirect_opt_num(o_string *o)
3388{
3389 int num;
3390
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003391 if (o->length == 0)
3392 return -1;
3393 for (num = 0; num < o->length; num++) {
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00003394 if (!isdigit(o->data[num])) {
Eric Andersen25f27032001-04-26 23:22:31 +00003395 return -1;
3396 }
3397 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003398 num = atoi(o->data);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003399 o_reset(o);
Eric Andersen25f27032001-04-26 23:22:31 +00003400 return num;
3401}
3402
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003403#if ENABLE_HUSH_TICK
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00003404static FILE *generate_stream_from_list(struct pipe *head)
Eric Andersen25f27032001-04-26 23:22:31 +00003405{
3406 FILE *pf;
Eric Andersen25f27032001-04-26 23:22:31 +00003407 int pid, channel[2];
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003408
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +00003409 xpipe(channel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003410/* *** NOMMU WARNING *** */
3411/* By using vfork here, we suspend parent till child exits or execs.
3412 * If child will not do it before it fills the pipe, it can block forever
3413 * in write(STDOUT_FILENO), and parent (shell) will be also stuck.
Denis Vlasenko3fe4f982008-06-09 16:02:39 +00003414 * Try this script:
3415 * yes "0123456789012345678901234567890" | dd bs=32 count=64k >TESTFILE
3416 * huge=`cat TESTFILE` # will block here forever
3417 * echo OK
Denis Vlasenko05743d72008-02-10 12:10:08 +00003418 */
Denis Vlasenko82604e92008-07-01 15:59:42 +00003419 pid = BB_MMU ? fork() : vfork();
3420 if (pid < 0)
3421 bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork");
Denis Vlasenko05743d72008-02-10 12:10:08 +00003422 if (pid == 0) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00003423 if (ENABLE_HUSH_JOB)
3424 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenko284d0fa2008-02-16 13:18:17 +00003425 close(channel[0]); /* NB: close _first_, then move fd! */
Denis Vlasenko05743d72008-02-10 12:10:08 +00003426 xmove_fd(channel[1], 1);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003427 /* Prevent it from trying to handle ctrl-z etc */
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003428#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00003429 G.run_list_level = 1;
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003430#endif
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003431 /* Process substitution is not considered to be usual
3432 * 'command execution'.
3433 * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. */
3434 /* Not needed, we are relying on it being disabled
3435 * everywhere outside actual command execution. */
3436 /*set_jobctrl_sighandler(SIG_IGN);*/
3437 set_misc_sighandler(SIG_DFL);
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003438 /* Freeing 'head' here would break NOMMU. */
3439 _exit(run_list(head));
Eric Andersen25f27032001-04-26 23:22:31 +00003440 }
Eric Andersen25f27032001-04-26 23:22:31 +00003441 close(channel[1]);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003442 pf = fdopen(channel[0], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00003443 return pf;
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003444 /* 'head' is freed by the caller */
Eric Andersen25f27032001-04-26 23:22:31 +00003445}
3446
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003447/* Return code is exit status of the process that is run. */
Denis Vlasenko68404f12008-03-17 09:00:54 +00003448static int process_command_subs(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +00003449 struct in_str *input,
3450 const char *subst_end)
Eric Andersen25f27032001-04-26 23:22:31 +00003451{
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003452 int retcode, ch, eol_cnt;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003453 o_string result = NULL_O_STRING;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003454 struct parse_context inner;
Eric Andersen25f27032001-04-26 23:22:31 +00003455 FILE *p;
3456 struct in_str pipe_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003457
Eric Andersen25f27032001-04-26 23:22:31 +00003458 initialize_context(&inner);
3459
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003460 /* Recursion to generate command */
Eric Andersen25f27032001-04-26 23:22:31 +00003461 retcode = parse_stream(&result, &inner, input, subst_end);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003462 if (retcode != 0)
3463 return retcode; /* syntax error or EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003464 done_word(&result, &inner);
3465 done_pipe(&inner, PIPE_SEQ);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003466 o_free(&result);
Eric Andersen25f27032001-04-26 23:22:31 +00003467
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003468 p = generate_stream_from_list(inner.list_head);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003469 if (p == NULL)
3470 return 1;
Denis Vlasenkoa0898172007-10-01 09:59:01 +00003471 close_on_exec_on(fileno(p));
Eric Andersen25f27032001-04-26 23:22:31 +00003472 setup_file_in_str(&pipe_str, p);
3473
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003474 /* Now send results of command back into original context */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003475 eol_cnt = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003476 while ((ch = i_getch(&pipe_str)) != EOF) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003477 if (ch == '\n') {
3478 eol_cnt++;
3479 continue;
3480 }
3481 while (eol_cnt) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00003482 o_addchr(dest, '\n');
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003483 eol_cnt--;
3484 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003485 o_addQchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003486 }
3487
3488 debug_printf("done reading from pipe, pclose()ing\n");
3489 /* This is the step that wait()s for the child. Should be pretty
3490 * safe, since we just read an EOF from its stdout. We could try
Denis Vlasenko262d7652007-05-20 21:52:49 +00003491 * to do better, by using wait(), and keeping track of background jobs
Eric Andersen25f27032001-04-26 23:22:31 +00003492 * at the same time. That would be a lot of work, and contrary
3493 * to the KISS philosophy of this program. */
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003494 retcode = fclose(p);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003495 free_pipe_list(inner.list_head, /* indent: */ 0);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003496 debug_printf("closed FILE from child, retcode=%d\n", retcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003497 return retcode;
3498}
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003499#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003500
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003501static int parse_group(o_string *dest, struct parse_context *ctx,
Eric Andersen25f27032001-04-26 23:22:31 +00003502 struct in_str *input, int ch)
3503{
Denis Vlasenko371de4a2008-10-14 12:43:13 +00003504 /* dest contains characters seen prior to ( or {.
3505 * Typically it's empty, but for functions defs,
3506 * it contains function name (without '()'). */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003507 int rcode;
3508 const char *endch = NULL;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003509 struct parse_context sub;
3510 struct command *command = ctx->command;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003511
3512 debug_printf_parse("parse_group entered\n");
Denis Vlasenko371de4a2008-10-14 12:43:13 +00003513#if ENABLE_HUSH_FUNCTIONS
3514 if (ch == 'F') { /* function definition? */
3515 bb_error_msg("aha '%s' is a function, parsing it...", dest->data);
3516 //command->fname = dest->data;
3517 command->grp_type = GRP_FUNCTION;
3518//TODO: review every o_reset() location... do they handle all o_string fields correctly?
3519 memset(dest, 0, sizeof(*dest));
3520 }
3521#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003522 if (command->argv /* word [word](... */
3523 || dest->length /* word(... */
3524 || dest->nonnull /* ""(... */
3525 ) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003526 syntax(NULL);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003527 debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n");
3528 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003529 }
3530 initialize_context(&sub);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003531 endch = "}";
3532 if (ch == '(') {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003533 endch = ")";
Denis Vlasenko371de4a2008-10-14 12:43:13 +00003534 command->grp_type = GRP_SUBSHELL;
Eric Andersen25f27032001-04-26 23:22:31 +00003535 }
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003536 rcode = parse_stream(dest, &sub, input, endch);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003537 if (rcode == 0) {
3538 done_word(dest, &sub); /* finish off the final word in the subcontext */
3539 done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003540 command->group = sub.list_head;
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003541 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003542 debug_printf_parse("parse_group return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003543 return rcode;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003544 /* command remains "open", available for possible redirects */
Eric Andersen25f27032001-04-26 23:22:31 +00003545}
3546
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003547/* Basically useful version until someone wants to get fancier,
Eric Andersen25f27032001-04-26 23:22:31 +00003548 * see the bash man page under "Parameter Expansion" */
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00003549static const char *lookup_param(const char *src)
Eric Andersen25f27032001-04-26 23:22:31 +00003550{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003551 struct variable *var = get_local_var(src);
3552 if (var)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003553 return strchr(var->varstr, '=') + 1;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003554 return NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003555}
3556
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003557#if ENABLE_HUSH_TICK
3558/* Subroutines for copying $(...) and `...` things */
3559static void add_till_backquote(o_string *dest, struct in_str *input);
3560/* '...' */
3561static void add_till_single_quote(o_string *dest, struct in_str *input)
3562{
3563 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003564 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003565 if (ch == EOF)
3566 break;
3567 if (ch == '\'')
3568 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003569 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003570 }
3571}
3572/* "...\"...`..`...." - do we need to handle "...$(..)..." too? */
3573static void add_till_double_quote(o_string *dest, struct in_str *input)
3574{
3575 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003576 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003577 if (ch == '"')
3578 break;
3579 if (ch == '\\') { /* \x. Copy both chars. */
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003580 o_addchr(dest, ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003581 ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003582 }
3583 if (ch == EOF)
3584 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003585 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003586 if (ch == '`') {
3587 add_till_backquote(dest, input);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003588 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003589 continue;
3590 }
Denis Vlasenko5703c222008-06-15 11:49:42 +00003591 //if (ch == '$') ...
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003592 }
3593}
3594/* Process `cmd` - copy contents until "`" is seen. Complicated by
3595 * \` quoting.
3596 * "Within the backquoted style of command substitution, backslash
3597 * shall retain its literal meaning, except when followed by: '$', '`', or '\'.
3598 * The search for the matching backquote shall be satisfied by the first
3599 * backquote found without a preceding backslash; during this search,
3600 * if a non-escaped backquote is encountered within a shell comment,
3601 * a here-document, an embedded command substitution of the $(command)
3602 * form, or a quoted string, undefined results occur. A single-quoted
3603 * or double-quoted string that begins, but does not end, within the
3604 * "`...`" sequence produces undefined results."
3605 * Example Output
3606 * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST
3607 */
3608static void add_till_backquote(o_string *dest, struct in_str *input)
3609{
3610 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003611 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003612 if (ch == '`')
3613 break;
3614 if (ch == '\\') { /* \x. Copy both chars unless it is \` */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003615 int ch2 = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003616 if (ch2 != '`' && ch2 != '$' && ch2 != '\\')
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003617 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003618 ch = ch2;
3619 }
3620 if (ch == EOF)
3621 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003622 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003623 }
3624}
3625/* Process $(cmd) - copy contents until ")" is seen. Complicated by
3626 * quoting and nested ()s.
3627 * "With the $(command) style of command substitution, all characters
3628 * following the open parenthesis to the matching closing parenthesis
3629 * constitute the command. Any valid shell script can be used for command,
3630 * except a script consisting solely of redirections which produces
3631 * unspecified results."
3632 * Example Output
3633 * echo $(echo '(TEST)' BEST) (TEST) BEST
3634 * echo $(echo 'TEST)' BEST) TEST) BEST
3635 * echo $(echo \(\(TEST\) BEST) ((TEST) BEST
3636 */
3637static void add_till_closing_curly_brace(o_string *dest, struct in_str *input)
3638{
3639 int count = 0;
3640 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003641 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003642 if (ch == EOF)
3643 break;
3644 if (ch == '(')
3645 count++;
3646 if (ch == ')')
3647 if (--count < 0)
3648 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003649 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003650 if (ch == '\'') {
3651 add_till_single_quote(dest, input);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003652 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003653 continue;
3654 }
3655 if (ch == '"') {
3656 add_till_double_quote(dest, input);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003657 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003658 continue;
3659 }
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003660 if (ch == '\\') { /* \x. Copy verbatim. Important for \(, \) */
3661 ch = i_getch(input);
3662 if (ch == EOF)
3663 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003664 o_addchr(dest, ch);
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003665 continue;
3666 }
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003667 }
3668}
3669#endif /* ENABLE_HUSH_TICK */
3670
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003671/* Return code: 0 for OK, 1 for syntax error */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003672static int handle_dollar(o_string *dest, struct in_str *input)
Eric Andersen25f27032001-04-26 23:22:31 +00003673{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003674 int ch = i_peek(input); /* first character after the $ */
Denis Vlasenkoff097622007-10-01 10:00:45 +00003675 unsigned char quote_mask = dest->o_quote ? 0x80 : 0;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003676
3677 debug_printf_parse("handle_dollar entered: ch='%c'\n", ch);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003678 if (isalpha(ch)) {
Denis Vlasenkod4981312008-07-31 10:34:48 +00003679 i_getch(input);
3680 make_var:
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003681 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003682 while (1) {
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003683 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003684 o_addchr(dest, ch | quote_mask);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003685 quote_mask = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003686 ch = i_peek(input);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003687 if (!isalnum(ch) && ch != '_')
3688 break;
Denis Vlasenkod4981312008-07-31 10:34:48 +00003689 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003690 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003691 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003692 } else if (isdigit(ch)) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003693 make_one_char_var:
Denis Vlasenkod4981312008-07-31 10:34:48 +00003694 i_getch(input);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003695 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003696 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003697 o_addchr(dest, ch | quote_mask);
3698 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003699 } else switch (ch) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003700 case '$': /* pid */
3701 case '!': /* last bg pid */
3702 case '?': /* last exit code */
3703 case '#': /* number of args */
3704 case '*': /* args */
3705 case '@': /* args */
3706 goto make_one_char_var;
Eric Andersen25f27032001-04-26 23:22:31 +00003707 case '{':
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003708 o_addchr(dest, SPECIAL_VAR_SYMBOL);
3709 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003710 /* XXX maybe someone will try to escape the '}' */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003711 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003712 ch = i_getch(input);
Denis Vlasenkob0550012007-05-24 12:18:16 +00003713 if (ch == '}')
3714 break;
3715 if (!isalnum(ch) && ch != '_') {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003716 syntax("unterminated ${name}");
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003717 debug_printf_parse("handle_dollar return 1: unterminated ${name}\n");
3718 return 1;
3719 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003720 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003721 o_addchr(dest, ch | quote_mask);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003722 quote_mask = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003723 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003724 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003725 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003726#if ENABLE_HUSH_TICK
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003727 case '(': {
3728 //int pos = dest->length;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003729 i_getch(input);
3730 o_addchr(dest, SPECIAL_VAR_SYMBOL);
3731 o_addchr(dest, quote_mask | '`');
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003732 add_till_closing_curly_brace(dest, input);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00003733 //debug_printf_subst("SUBST RES2 '%s'\n", dest->data + pos);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003734 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003735 break;
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003736 }
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003737#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003738 case '_':
Denis Vlasenkod4981312008-07-31 10:34:48 +00003739 i_getch(input);
3740 ch = i_peek(input);
3741 if (isalnum(ch)) { /* it's $_name or $_123 */
3742 ch = '_';
3743 goto make_var;
3744 }
3745 /* else: it's $_ */
3746 case '-':
Eric Andersen25f27032001-04-26 23:22:31 +00003747 /* still unhandled, but should be eventually */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003748 bb_error_msg("unhandled syntax: $%c", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003749 return 1;
3750 break;
3751 default:
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003752 o_addQchr(dest, '$');
Eric Andersen25f27032001-04-26 23:22:31 +00003753 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003754 debug_printf_parse("handle_dollar return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003755 return 0;
3756}
3757
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003758/* Scan input, call done_word() whenever full IFS delimited word was seen.
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003759 * Call done_pipe if '\n' was seen (and end_trigger != NULL).
3760 * Return code is 0 if end_trigger char is met,
3761 * -1 on EOF (but if end_trigger == NULL then return 0),
Denis Vlasenko55789c62008-06-18 16:30:42 +00003762 * 1 for syntax error */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003763static int parse_stream(o_string *dest, struct parse_context *ctx,
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003764 struct in_str *input, const char *end_trigger)
Eric Andersen25f27032001-04-26 23:22:31 +00003765{
"Vladimir N. Oleynik"4ccd2b42006-01-31 09:27:48 +00003766 int ch, m;
Eric Andersen25f27032001-04-26 23:22:31 +00003767 int redir_fd;
3768 redir_type redir_style;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003769 int shadow_quote = dest->o_quote;
Eric Andersen25f27032001-04-26 23:22:31 +00003770 int next;
3771
Denis Vlasenkoff097622007-10-01 10:00:45 +00003772 /* Only double-quote state is handled in the state variable dest->o_quote.
Eric Andersen25f27032001-04-26 23:22:31 +00003773 * A single-quote triggers a bypass of the main loop until its mate is
Denis Vlasenkoff097622007-10-01 10:00:45 +00003774 * found. When recursing, quote state is passed in via dest->o_quote. */
Eric Andersen25f27032001-04-26 23:22:31 +00003775
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003776 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 +00003777
Denis Vlasenko1a735862007-05-23 00:32:25 +00003778 while (1) {
Denis Vlasenko1a735862007-05-23 00:32:25 +00003779 m = CHAR_IFS;
3780 next = '\0';
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003781 ch = i_getch(input);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003782 if (ch != EOF) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00003783 m = G.charmap[ch];
Denis Vlasenko55789c62008-06-18 16:30:42 +00003784 if (ch != '\n') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003785 next = i_peek(input);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003786 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003787 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003788 debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n",
Denis Vlasenkoff097622007-10-01 10:00:45 +00003789 ch, ch, m, dest->o_quote);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003790 if (m == CHAR_ORDINARY
Denis Vlasenko55789c62008-06-18 16:30:42 +00003791 || (m != CHAR_SPECIAL && shadow_quote)
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003792 ) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00003793 if (ch == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003794 syntax("unterminated \"");
Denis Vlasenko170435c2007-05-23 13:01:10 +00003795 debug_printf_parse("parse_stream return 1: unterminated \"\n");
3796 return 1;
3797 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003798 o_addQchr(dest, ch);
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003799 if ((dest->o_assignment == MAYBE_ASSIGNMENT
3800 || dest->o_assignment == WORD_IS_KEYWORD)
Denis Vlasenko55789c62008-06-18 16:30:42 +00003801 && ch == '='
3802 && is_assignment(dest->data)
3803 ) {
3804 dest->o_assignment = DEFINITELY_ASSIGNMENT;
3805 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003806 continue;
3807 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003808 if (m == CHAR_IFS) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003809 if (done_word(dest, ctx)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003810 debug_printf_parse("parse_stream return 1: done_word!=0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003811 return 1;
Eric Andersenaac75e52001-04-30 18:18:45 +00003812 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003813 if (ch == EOF)
3814 break;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003815 /* If we aren't performing a substitution, treat
3816 * a newline as a command separator.
3817 * [why we don't handle it exactly like ';'? --vda] */
3818 if (end_trigger && ch == '\n') {
Denis Vlasenkof1736072008-07-31 10:09:26 +00003819#if ENABLE_HUSH_CASE
3820 /* "case ... in <newline> word) ..." -
3821 * newlines are ignored (but ';' wouldn't be) */
3822 if (dest->length == 0 // && argv[0] == NULL
3823 && ctx->ctx_res_w == RES_MATCH
3824 ) {
3825 continue;
3826 }
3827#endif
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003828 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003829 dest->o_assignment = MAYBE_ASSIGNMENT;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003830 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003831 }
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003832 if (end_trigger) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00003833 if (!shadow_quote && strchr(end_trigger, ch)) {
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003834 /* Special case: (...word) makes last word terminate,
3835 * as if ';' is seen */
3836 if (ch == ')') {
3837 done_word(dest, ctx);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003838//err chk?
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003839 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003840 dest->o_assignment = MAYBE_ASSIGNMENT;
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003841 }
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003842 if (!HAS_KEYWORDS
3843 IF_HAS_KEYWORDS(|| (ctx->ctx_res_w == RES_NONE && ctx->old_flag == 0))
3844 ) {
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003845 debug_printf_parse("parse_stream return 0: end_trigger char found\n");
3846 return 0;
3847 }
3848 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003849 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003850 if (m == CHAR_IFS)
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003851 continue;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003852
3853 if (dest->o_assignment == MAYBE_ASSIGNMENT) {
3854 /* ch is a special char and thus this word
3855 * cannot be an assignment: */
3856 dest->o_assignment = NOT_ASSIGNMENT;
3857 }
3858
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003859 switch (ch) {
Eric Andersen25f27032001-04-26 23:22:31 +00003860 case '#':
Denis Vlasenko55789c62008-06-18 16:30:42 +00003861 if (dest->length == 0 && !shadow_quote) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003862 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003863 ch = i_peek(input);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003864 if (ch == EOF || ch == '\n')
3865 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003866 i_getch(input);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003867 }
Eric Andersen25f27032001-04-26 23:22:31 +00003868 } else {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003869 o_addQchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003870 }
3871 break;
3872 case '\\':
3873 if (next == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003874 syntax("\\<eof>");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003875 debug_printf_parse("parse_stream return 1: \\<eof>\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003876 return 1;
3877 }
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003878 /* bash:
3879 * "The backslash retains its special meaning [in "..."]
3880 * only when followed by one of the following characters:
3881 * $, `, ", \, or <newline>. A double quote may be quoted
3882 * within double quotes by preceding it with a backslash.
3883 * If enabled, history expansion will be performed unless
3884 * an ! appearing in double quotes is escaped using
3885 * a backslash. The backslash preceding the ! is not removed."
3886 */
Denis Vlasenko55789c62008-06-18 16:30:42 +00003887 if (shadow_quote) { //NOT SURE dest->o_quote) {
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003888 if (strchr("$`\"\\", next) != NULL) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003889 o_addqchr(dest, i_getch(input));
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003890 } else {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003891 o_addqchr(dest, '\\');
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003892 }
3893 } else {
3894 o_addchr(dest, '\\');
3895 o_addchr(dest, i_getch(input));
3896 }
Eric Andersen25f27032001-04-26 23:22:31 +00003897 break;
3898 case '$':
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003899 if (handle_dollar(dest, input) != 0) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003900 debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n");
3901 return 1;
3902 }
Eric Andersen25f27032001-04-26 23:22:31 +00003903 break;
3904 case '\'':
3905 dest->nonnull = 1;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003906 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003907 ch = i_getch(input);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003908 if (ch == EOF) {
3909 syntax("unterminated '");
3910 debug_printf_parse("parse_stream return 1: unterminated '\n");
3911 return 1;
3912 }
3913 if (ch == '\'')
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003914 break;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003915 if (dest->o_assignment == NOT_ASSIGNMENT)
3916 o_addqchr(dest, ch);
3917 else
3918 o_addchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003919 }
Eric Andersen25f27032001-04-26 23:22:31 +00003920 break;
3921 case '"':
3922 dest->nonnull = 1;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003923 shadow_quote ^= 1; /* invert */
3924 if (dest->o_assignment == NOT_ASSIGNMENT)
3925 dest->o_quote ^= 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003926 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003927#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003928 case '`': {
3929 //int pos = dest->length;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003930 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003931 o_addchr(dest, shadow_quote /*or dest->o_quote??*/ ? 0x80 | '`' : '`');
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003932 add_till_backquote(dest, input);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003933 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00003934 //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos);
Eric Andersen25f27032001-04-26 23:22:31 +00003935 break;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003936 }
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003937#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003938 case '>':
3939 redir_fd = redirect_opt_num(dest);
3940 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003941 redir_style = REDIRECT_OVERWRITE;
Eric Andersen25f27032001-04-26 23:22:31 +00003942 if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003943 redir_style = REDIRECT_APPEND;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003944 i_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003945 }
3946#if 0
3947 else if (next == '(') {
3948 syntax(">(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003949 debug_printf_parse("parse_stream return 1: >(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003950 return 1;
3951 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003952#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003953 setup_redirect(ctx, redir_fd, redir_style, input);
3954 break;
3955 case '<':
3956 redir_fd = redirect_opt_num(dest);
3957 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003958 redir_style = REDIRECT_INPUT;
Eric Andersen25f27032001-04-26 23:22:31 +00003959 if (next == '<') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003960 redir_style = REDIRECT_HEREIS;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003961 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003962 } else if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003963 redir_style = REDIRECT_IO;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003964 i_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003965 }
3966#if 0
3967 else if (next == '(') {
3968 syntax("<(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003969 debug_printf_parse("parse_stream return 1: <(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003970 return 1;
3971 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003972#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003973 setup_redirect(ctx, redir_fd, redir_style, input);
3974 break;
3975 case ';':
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003976#if ENABLE_HUSH_CASE
3977 case_semi:
3978#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003979 done_word(dest, ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003980 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003981#if ENABLE_HUSH_CASE
3982 /* Eat multiple semicolons, detect
3983 * whether it means something special */
3984 while (1) {
3985 ch = i_peek(input);
3986 if (ch != ';')
3987 break;
3988 i_getch(input);
3989 if (ctx->ctx_res_w == RES_CASEI) {
3990 ctx->ctx_dsemicolon = 1;
3991 ctx->ctx_res_w = RES_MATCH;
3992 break;
3993 }
3994 }
3995#endif
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003996 new_cmd:
3997 /* We just finished a cmd. New one may start
3998 * with an assignment */
3999 dest->o_assignment = MAYBE_ASSIGNMENT;
Eric Andersen25f27032001-04-26 23:22:31 +00004000 break;
4001 case '&':
4002 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004003 if (next == '&') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00004004 i_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004005 done_pipe(ctx, PIPE_AND);
Eric Andersen25f27032001-04-26 23:22:31 +00004006 } else {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004007 done_pipe(ctx, PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00004008 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00004009 goto new_cmd;
Eric Andersen25f27032001-04-26 23:22:31 +00004010 case '|':
4011 done_word(dest, ctx);
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00004012#if ENABLE_HUSH_CASE
4013 if (ctx->ctx_res_w == RES_MATCH)
Denis Vlasenkof1736072008-07-31 10:09:26 +00004014 break; /* we are in case's "word | word)" */
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00004015#endif
Denis Vlasenko2b576b82008-08-04 00:46:07 +00004016 if (next == '|') { /* || */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00004017 i_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004018 done_pipe(ctx, PIPE_OR);
Eric Andersen25f27032001-04-26 23:22:31 +00004019 } else {
4020 /* we could pick up a file descriptor choice here
4021 * with redirect_opt_num(), but bash doesn't do it.
4022 * "echo foo 2| cat" yields "foo 2". */
4023 done_command(ctx);
4024 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00004025 goto new_cmd;
Eric Andersen25f27032001-04-26 23:22:31 +00004026 case '(':
Denis Vlasenko17f02e72008-07-14 04:32:29 +00004027#if ENABLE_HUSH_CASE
Denis Vlasenkof1736072008-07-31 10:09:26 +00004028 /* "case... in [(]word)..." - skip '(' */
Denis Vlasenko22d10a02008-10-13 08:53:43 +00004029 if (ctx->ctx_res_w == RES_MATCH
Denis Vlasenko9af22c72008-10-09 12:54:58 +00004030 && ctx->command->argv == NULL /* not (word|(... */
Denis Vlasenko22d10a02008-10-13 08:53:43 +00004031 && dest->length == 0 /* not word(... */
4032 && dest->nonnull == 0 /* not ""(... */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00004033 ) {
4034 continue;
4035 }
4036#endif
Denis Vlasenko371de4a2008-10-14 12:43:13 +00004037#if ENABLE_HUSH_FUNCTIONS
Denis Vlasenkoc3735272008-10-09 12:58:26 +00004038 if (dest->length != 0 /* not just () but word() */
4039 && dest->nonnull == 0 /* not a"b"c() */
4040 && ctx->command->argv == NULL /* it's the first word */
Denis Vlasenko371de4a2008-10-14 12:43:13 +00004041//TODO: "func ( ) {...}" - note spaces - is valid format too in bash
Denis Vlasenkoc3735272008-10-09 12:58:26 +00004042 && i_peek(input) == ')'
Denis Vlasenko22d10a02008-10-13 08:53:43 +00004043 && !match_reserved_word(dest)
Denis Vlasenkoc3735272008-10-09 12:58:26 +00004044 ) {
4045 bb_error_msg("seems like a function definition");
Denis Vlasenko22d10a02008-10-13 08:53:43 +00004046 i_getch(input);
4047 do {
Denis Vlasenko371de4a2008-10-14 12:43:13 +00004048//TODO: do it properly.
Denis Vlasenko22d10a02008-10-13 08:53:43 +00004049 ch = i_getch(input);
4050 } while (ch == ' ' || ch == '\n');
4051 if (ch != '{') {
4052 syntax("was expecting {");
4053 debug_printf_parse("parse_stream return 1\n");
4054 return 1;
4055 }
Denis Vlasenko371de4a2008-10-14 12:43:13 +00004056 ch = 'F'; /* magic value */
Denis Vlasenkoc3735272008-10-09 12:58:26 +00004057 }
4058#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004059 case '{':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00004060 if (parse_group(dest, ctx, input, ch) != 0) {
4061 debug_printf_parse("parse_stream return 1: parse_group returned non-0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004062 return 1;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00004063 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00004064 goto new_cmd;
Eric Andersen25f27032001-04-26 23:22:31 +00004065 case ')':
Denis Vlasenko17f02e72008-07-14 04:32:29 +00004066#if ENABLE_HUSH_CASE
4067 if (ctx->ctx_res_w == RES_MATCH)
4068 goto case_semi;
4069#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004070 case '}':
Denis Vlasenkoc3735272008-10-09 12:58:26 +00004071 /* proper use of this character is caught by end_trigger:
4072 * if we see {, we call parse_group(..., end_trigger='}')
4073 * and it will match } earlier (not here). */
Denis Vlasenkoa8442002008-06-14 11:00:17 +00004074 syntax("unexpected } or )");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00004075 debug_printf_parse("parse_stream return 1: unexpected '}'\n");
Eric Andersen25f27032001-04-26 23:22:31 +00004076 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00004077 default:
Denis Vlasenko5ec61322008-06-24 00:50:07 +00004078 if (HUSH_DEBUG)
Denis Vlasenko90e485c2007-05-23 15:22:50 +00004079 bb_error_msg_and_die("BUG: unexpected %c\n", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00004080 }
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004081 } /* while (1) */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00004082 debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL));
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00004083 if (end_trigger)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00004084 return -1;
Eric Andersen25f27032001-04-26 23:22:31 +00004085 return 0;
4086}
4087
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004088static void set_in_charmap(const char *set, int code)
Eric Andersen25f27032001-04-26 23:22:31 +00004089{
Denis Vlasenkof5294e12007-04-14 10:09:57 +00004090 while (*set)
Denis Vlasenko87a86552008-07-29 19:43:10 +00004091 G.charmap[(unsigned char)*set++] = code;
Eric Andersen25f27032001-04-26 23:22:31 +00004092}
4093
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004094static void update_charmap(void)
Eric Andersen25f27032001-04-26 23:22:31 +00004095{
Denis Vlasenko87a86552008-07-29 19:43:10 +00004096 G.ifs = getenv("IFS");
4097 if (G.ifs == NULL)
4098 G.ifs = " \t\n";
Eric Andersen25f27032001-04-26 23:22:31 +00004099 /* Precompute a list of 'flow through' behavior so it can be treated
4100 * quickly up front. Computation is necessary because of IFS.
4101 * Special case handling of IFS == " \t\n" is not implemented.
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004102 * The charmap[] array only really needs two bits each,
4103 * and on most machines that would be faster (reduced L1 cache use).
Eric Andersen25f27032001-04-26 23:22:31 +00004104 */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004105 memset(G.charmap, CHAR_ORDINARY, sizeof(G.charmap));
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00004106#if ENABLE_HUSH_TICK
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004107 set_in_charmap("\\$\"`", CHAR_SPECIAL);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00004108#else
4109 set_in_charmap("\\$\"", CHAR_SPECIAL);
4110#endif
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004111 set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004112 set_in_charmap(G.ifs, CHAR_IFS); /* are ordinary if quoted */
Eric Andersen25f27032001-04-26 23:22:31 +00004113}
4114
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004115/* Most recursion does not come through here, the exception is
Denis Vlasenko170435c2007-05-23 13:01:10 +00004116 * from builtin_source() and builtin_eval() */
4117static int parse_and_run_stream(struct in_str *inp, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00004118{
Denis Vlasenko9af22c72008-10-09 12:54:58 +00004119 struct parse_context ctx;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004120 o_string temp = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00004121 int rcode;
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004122
Eric Andersen25f27032001-04-26 23:22:31 +00004123 do {
4124 initialize_context(&ctx);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004125 update_charmap();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004126#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00004127 inp->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004128#endif
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00004129 /* We will stop & execute after each ';' or '\n'.
4130 * Example: "sleep 9999; echo TEST" + ctrl-C:
4131 * TEST should be printed */
Denis Vlasenko2b576b82008-08-04 00:46:07 +00004132 temp.o_assignment = MAYBE_ASSIGNMENT;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00004133 rcode = parse_stream(&temp, &ctx, inp, ";\n");
Denis Vlasenko5ec61322008-06-24 00:50:07 +00004134#if HAS_KEYWORDS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004135 if (rcode != 1 && ctx.old_flag != 0) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00004136 syntax(NULL);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004137 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00004138#endif
4139 if (rcode != 1 IF_HAS_KEYWORDS(&& ctx.old_flag == 0)) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004140 done_word(&temp, &ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004141 done_pipe(&ctx, PIPE_SEQ);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00004142 debug_print_tree(ctx.list_head, 0);
Denis Vlasenko05743d72008-02-10 12:10:08 +00004143 debug_printf_exec("parse_stream_outer: run_and_free_list\n");
4144 run_and_free_list(ctx.list_head);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004145 } else {
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004146 /* We arrive here also if rcode == 1 (error in parse_stream) */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00004147#if HAS_KEYWORDS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004148 if (ctx.old_flag != 0) {
4149 free(ctx.stack);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00004150 o_reset(&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004151 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00004152#endif
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00004153 /*temp.nonnull = 0; - o_free does it below */
4154 /*temp.o_quote = 0; - o_free does it below */
Denis Vlasenko05743d72008-02-10 12:10:08 +00004155 free_pipe_list(ctx.list_head, /* indent: */ 0);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004156 /* Discard all unprocessed line input, force prompt on */
4157 inp->p = NULL;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004158#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004159 inp->promptme = 1;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004160#endif
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004161 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00004162 o_free(&temp);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004163 /* loop on syntax errors, return on EOF: */
4164 } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP));
Eric Andersen25f27032001-04-26 23:22:31 +00004165 return 0;
4166}
4167
Denis Vlasenko170435c2007-05-23 13:01:10 +00004168static int parse_and_run_string(const char *s, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00004169{
4170 struct in_str input;
4171 setup_string_in_str(&input, s);
Denis Vlasenko170435c2007-05-23 13:01:10 +00004172 return parse_and_run_stream(&input, parse_flag);
Eric Andersen25f27032001-04-26 23:22:31 +00004173}
4174
Denis Vlasenko170435c2007-05-23 13:01:10 +00004175static int parse_and_run_file(FILE *f)
Eric Andersen25f27032001-04-26 23:22:31 +00004176{
4177 int rcode;
4178 struct in_str input;
4179 setup_file_in_str(&input, f);
Denis Vlasenko5703c222008-06-15 11:49:42 +00004180 rcode = parse_and_run_stream(&input, 0 /* parse_flag */);
Eric Andersen25f27032001-04-26 23:22:31 +00004181 return rcode;
4182}
4183
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004184#if ENABLE_HUSH_JOB
Eric Andersen6c947d22001-06-25 22:24:38 +00004185/* Make sure we have a controlling tty. If we get started under a job
4186 * aware app (like bash for example), make sure we are now in charge so
4187 * we don't fight over who gets the foreground */
Eric Anderseneaecbf32001-10-31 10:41:31 +00004188static void setup_job_control(void)
Eric Andersen52a97ca2001-06-22 06:49:26 +00004189{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004190 pid_t shell_pgrp;
4191
Denis Vlasenko87a86552008-07-29 19:43:10 +00004192 shell_pgrp = getpgrp();
Denis Vlasenko87a86552008-07-29 19:43:10 +00004193 close_on_exec_on(G.interactive_fd);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004194
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00004195 /* If we were ran as 'hush &',
4196 * sleep until we are in the foreground. */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004197 while (tcgetpgrp(G.interactive_fd) != shell_pgrp) {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004198 /* Send TTIN to ourself (should stop us) */
Denis Vlasenkoff131b92007-04-10 15:42:06 +00004199 kill(- shell_pgrp, SIGTTIN);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00004200 shell_pgrp = getpgrp();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004201 }
Eric Andersen52a97ca2001-06-22 06:49:26 +00004202
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004203 /* Ignore job-control and misc signals. */
4204 set_jobctrl_sighandler(SIG_IGN);
4205 set_misc_sighandler(SIG_IGN);
4206//huh? signal(SIGCHLD, SIG_IGN);
4207
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00004208 /* We _must_ restore tty pgrp on fatal signals */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004209 set_fatal_sighandler(sigexit);
Eric Andersen6c947d22001-06-25 22:24:38 +00004210
4211 /* Put ourselves in our own process group. */
Bernhard Reutner-Fischer864329d2008-09-25 10:55:05 +00004212 bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */
Eric Andersen6c947d22001-06-25 22:24:38 +00004213 /* Grab control of the terminal. */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004214 tcsetpgrp(G.interactive_fd, getpid());
Eric Andersen6c947d22001-06-25 22:24:38 +00004215}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004216#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00004217
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004218
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00004219int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Matt Kraai2d91deb2001-08-01 17:21:35 +00004220int hush_main(int argc, char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00004221{
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004222 static const struct variable const_shell_ver = {
4223 .next = NULL,
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00004224 .varstr = (char*)hush_version_str,
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004225 .max_len = 1, /* 0 can provoke free(name) */
4226 .flg_export = 1,
4227 .flg_read_only = 1,
4228 };
4229
Eric Andersen25f27032001-04-26 23:22:31 +00004230 int opt;
4231 FILE *input;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004232 char **e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004233 struct variable *cur_var;
Eric Andersenbc604a22001-05-16 05:24:03 +00004234
Denis Vlasenko574f2f42008-02-27 18:41:59 +00004235 INIT_G();
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004236
Denis Vlasenko87a86552008-07-29 19:43:10 +00004237 G.root_pid = getpid();
Denis Vlasenko16c2fea2008-06-17 12:28:44 +00004238
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004239 /* Deal with HUSH_VERSION */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004240 G.shell_ver = const_shell_ver; /* copying struct here */
4241 G.top_var = &G.shell_ver;
Denis Vlasenkof886fd22008-10-13 12:36:05 +00004242 debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION");
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004243 unsetenv("HUSH_VERSION"); /* in case it exists in initial env */
4244 /* Initialize our shell local variables with the values
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004245 * currently living in the environment */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004246 cur_var = G.top_var;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004247 e = environ;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004248 if (e) while (*e) {
4249 char *value = strchr(*e, '=');
4250 if (value) { /* paranoia */
4251 cur_var->next = xzalloc(sizeof(*cur_var));
4252 cur_var = cur_var->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00004253 cur_var->varstr = *e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004254 cur_var->max_len = strlen(*e);
4255 cur_var->flg_export = 1;
4256 }
4257 e++;
4258 }
Denis Vlasenkof886fd22008-10-13 12:36:05 +00004259 debug_printf_env("putenv '%s'\n", hush_version_str);
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00004260 putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00004261
Denis Vlasenko38f63192007-01-22 09:03:07 +00004262#if ENABLE_FEATURE_EDITING
Denis Vlasenko87a86552008-07-29 19:43:10 +00004263 G.line_input_state = new_line_input_t(FOR_SHELL);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00004264#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004265 /* XXX what should these be while sourcing /etc/profile? */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004266 G.global_argc = argc;
4267 G.global_argv = argv;
Eric Andersen94ac2442001-05-22 19:05:18 +00004268 /* Initialize some more globals to non-zero values */
4269 set_cwd();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004270#if ENABLE_HUSH_INTERACTIVE
4271#if ENABLE_FEATURE_EDITING
4272 cmdedit_set_initial_prompt();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004273#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00004274 G.PS2 = "> ";
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004275#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00004276
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004277 if (EXIT_SUCCESS) /* otherwise is already done */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004278 G.last_return_code = EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00004279
4280 if (argv[0] && argv[0][0] == '-') {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00004281 debug_printf("sourcing /etc/profile\n");
Denis Vlasenko5415c852008-07-21 23:05:26 +00004282 input = fopen_for_read("/etc/profile");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00004283 if (input != NULL) {
Denis Vlasenkoa0898172007-10-01 09:59:01 +00004284 close_on_exec_on(fileno(input));
Denis Vlasenko170435c2007-05-23 13:01:10 +00004285 parse_and_run_file(input);
Eric Andersena90f20b2001-06-26 23:00:21 +00004286 fclose(input);
4287 }
Eric Andersen25f27032001-04-26 23:22:31 +00004288 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004289 input = stdin;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004290
Eric Andersen25f27032001-04-26 23:22:31 +00004291 while ((opt = getopt(argc, argv, "c:xif")) > 0) {
4292 switch (opt) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004293 case 'c':
Denis Vlasenko87a86552008-07-29 19:43:10 +00004294 G.global_argv = argv + optind;
Denis Vlasenkoa8b6dff2009-03-20 12:05:14 +00004295 if (!argv[optind]) {
4296 /* -c 'script' (no params): prevent empty $0 */
4297 *--G.global_argv = argv[0];
4298 optind--;
4299 } /* else -c 'script' PAR0 PAR1: $0 is PAR0 */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004300 G.global_argc = argc - optind;
Denis Vlasenko5703c222008-06-15 11:49:42 +00004301 opt = parse_and_run_string(optarg, 0 /* parse_flag */);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004302 goto final_return;
4303 case 'i':
Denis Vlasenkoc666f712007-05-16 22:18:54 +00004304 /* Well, we cannot just declare interactiveness,
4305 * we have to have some stuff (ctty, etc) */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004306 /* G.interactive_fd++; */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004307 break;
4308 case 'f':
Denis Vlasenko87a86552008-07-29 19:43:10 +00004309 G.fake_mode = 1;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004310 break;
4311 default:
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004312#ifndef BB_VER
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004313 fprintf(stderr, "Usage: sh [FILE]...\n"
4314 " or: sh -c command [args]...\n\n");
4315 exit(EXIT_FAILURE);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004316#else
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004317 bb_show_usage();
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004318#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004319 }
4320 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004321#if ENABLE_HUSH_JOB
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004322 /* A shell is interactive if the '-i' flag was given, or if all of
Eric Andersen25f27032001-04-26 23:22:31 +00004323 * the following conditions are met:
Denis Vlasenko55b2de72007-04-18 17:21:28 +00004324 * no -c command
Eric Andersen25f27032001-04-26 23:22:31 +00004325 * no arguments remaining or the -s flag given
4326 * standard input is a terminal
4327 * standard output is a terminal
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004328 * Refer to Posix.2, the description of the 'sh' utility. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004329 if (argv[optind] == NULL && input == stdin
4330 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
4331 ) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004332 G.saved_tty_pgrp = tcgetpgrp(STDIN_FILENO);
4333 debug_printf("saved_tty_pgrp=%d\n", G.saved_tty_pgrp);
4334 if (G.saved_tty_pgrp >= 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004335 /* try to dup to high fd#, >= 255 */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004336 G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
4337 if (G.interactive_fd < 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004338 /* try to dup to any fd */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004339 G.interactive_fd = dup(STDIN_FILENO);
4340 if (G.interactive_fd < 0)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004341 /* give up */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004342 G.interactive_fd = 0;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004343 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004344 // TODO: track & disallow any attempts of user
4345 // to (inadvertently) close/redirect it
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004346 }
Eric Andersen25f27032001-04-26 23:22:31 +00004347 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004348 debug_printf("G.interactive_fd=%d\n", G.interactive_fd);
4349 if (G.interactive_fd) {
4350 fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC);
Eric Andersen25f27032001-04-26 23:22:31 +00004351 /* Looks like they want an interactive shell */
Eric Andersen52a97ca2001-06-22 06:49:26 +00004352 setup_job_control();
Denis Vlasenko4ecfcdc2008-02-11 08:32:31 +00004353 /* -1 is special - makes xfuncs longjmp, not exit
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00004354 * (we reset die_sleep = 0 whereever we [v]fork) */
4355 die_sleep = -1;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004356 if (setjmp(die_jmp)) {
4357 /* xfunc has failed! die die die */
4358 hush_exit(xfunc_error_retval);
4359 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004360#if !ENABLE_FEATURE_SH_EXTRA_QUIET
Denis Vlasenkoca525b42007-06-13 12:27:17 +00004361 printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", bb_banner);
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004362 printf("Enter 'help' for a list of built-in commands.\n\n");
4363#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00004364 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004365#elif ENABLE_HUSH_INTERACTIVE
4366/* no job control compiled, only prompt/line editing */
4367 if (argv[optind] == NULL && input == stdin
4368 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
4369 ) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004370 G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
4371 if (G.interactive_fd < 0) {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004372 /* try to dup to any fd */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004373 G.interactive_fd = dup(STDIN_FILENO);
4374 if (G.interactive_fd < 0)
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004375 /* give up */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004376 G.interactive_fd = 0;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004377 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004378 if (G.interactive_fd) {
4379 fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC);
Denis Vlasenko4830fc52008-05-25 21:50:55 +00004380 set_misc_sighandler(SIG_IGN);
4381 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004382 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004383#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004384
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004385 if (argv[optind] == NULL) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00004386 opt = parse_and_run_file(stdin);
Denis Vlasenko05743d72008-02-10 12:10:08 +00004387 } else {
4388 debug_printf("\nrunning script '%s'\n", argv[optind]);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004389 G.global_argv = argv + optind;
4390 G.global_argc = argc - optind;
Denis Vlasenko5415c852008-07-21 23:05:26 +00004391 input = xfopen_for_read(argv[optind]);
Denis Vlasenko459a5ad2008-02-11 08:35:03 +00004392 fcntl(fileno(input), F_SETFD, FD_CLOEXEC);
Denis Vlasenko05743d72008-02-10 12:10:08 +00004393 opt = parse_and_run_file(input);
Eric Andersen25f27032001-04-26 23:22:31 +00004394 }
Eric Andersen25f27032001-04-26 23:22:31 +00004395
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004396 final_return:
4397
Denis Vlasenko38f63192007-01-22 09:03:07 +00004398#if ENABLE_FEATURE_CLEAN_UP
Eric Andersenaeb44c42001-05-22 20:29:00 +00004399 fclose(input);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004400 if (G.cwd != bb_msg_unknown)
4401 free((char*)G.cwd);
4402 cur_var = G.top_var->next;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004403 while (cur_var) {
4404 struct variable *tmp = cur_var;
4405 if (!cur_var->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00004406 free(cur_var->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004407 cur_var = cur_var->next;
4408 free(tmp);
Eric Andersenaeb44c42001-05-22 20:29:00 +00004409 }
Eric Andersen25f27032001-04-26 23:22:31 +00004410#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00004411 hush_exit(opt ? opt : G.last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +00004412}
Denis Vlasenko96702ca2007-11-23 23:28:55 +00004413
4414
4415#if ENABLE_LASH
4416int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
4417int lash_main(int argc, char **argv)
4418{
4419 //bb_error_msg("lash is deprecated, please use hush instead");
4420 return hush_main(argc, argv);
4421}
4422#endif
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004423
4424
4425/*
4426 * Built-ins
4427 */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004428static int builtin_true(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004429{
4430 return 0;
4431}
4432
4433static int builtin_test(char **argv)
4434{
4435 int argc = 0;
4436 while (*argv) {
4437 argc++;
4438 argv++;
4439 }
4440 return test_main(argc, argv - argc);
4441}
4442
4443static int builtin_echo(char **argv)
4444{
4445 int argc = 0;
4446 while (*argv) {
4447 argc++;
4448 argv++;
4449 }
4450 return echo_main(argc, argv - argc);
4451}
4452
4453static int builtin_eval(char **argv)
4454{
4455 int rcode = EXIT_SUCCESS;
4456
4457 if (argv[1]) {
4458 char *str = expand_strvec_to_string(argv + 1);
4459 parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP);
4460 free(str);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004461 rcode = G.last_return_code;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004462 }
4463 return rcode;
4464}
4465
4466static int builtin_cd(char **argv)
4467{
4468 const char *newdir;
4469 if (argv[1] == NULL) {
4470 // bash does nothing (exitcode 0) if HOME is ""; if it's unset,
4471 // bash says "bash: cd: HOME not set" and does nothing (exitcode 1)
4472 newdir = getenv("HOME") ? : "/";
4473 } else
4474 newdir = argv[1];
4475 if (chdir(newdir)) {
4476 printf("cd: %s: %s\n", newdir, strerror(errno));
4477 return EXIT_FAILURE;
4478 }
4479 set_cwd();
4480 return EXIT_SUCCESS;
4481}
4482
4483static int builtin_exec(char **argv)
4484{
4485 if (argv[1] == NULL)
4486 return EXIT_SUCCESS; /* bash does this */
4487 {
4488#if !BB_MMU
Denis Vlasenkof886fd22008-10-13 12:36:05 +00004489 nommu_save_t dummy;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004490#endif
4491// FIXME: if exec fails, bash does NOT exit! We do...
Denis Vlasenkof886fd22008-10-13 12:36:05 +00004492 pseudo_exec_argv(&dummy, argv + 1, 0, NULL);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004493 /* never returns */
4494 }
4495}
4496
4497static int builtin_exit(char **argv)
4498{
4499// TODO: bash does it ONLY on top-level sh exit (+interacive only?)
4500 //puts("exit"); /* bash does it */
4501// TODO: warn if we have background jobs: "There are stopped jobs"
4502// On second consecutive 'exit', exit anyway.
4503 if (argv[1] == NULL)
Denis Vlasenko87a86552008-07-29 19:43:10 +00004504 hush_exit(G.last_return_code);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004505 /* mimic bash: exit 123abc == exit 255 + error msg */
4506 xfunc_error_retval = 255;
4507 /* bash: exit -2 == exit 254, no error msg */
4508 hush_exit(xatoi(argv[1]) & 0xff);
4509}
4510
4511static int builtin_export(char **argv)
4512{
4513 const char *value;
4514 char *name = argv[1];
4515
4516 if (name == NULL) {
4517 // TODO:
4518 // ash emits: export VAR='VAL'
4519 // bash: declare -x VAR="VAL"
4520 // (both also escape as needed (quotes, $, etc))
4521 char **e = environ;
4522 if (e)
4523 while (*e)
4524 puts(*e++);
4525 return EXIT_SUCCESS;
4526 }
4527
4528 value = strchr(name, '=');
4529 if (!value) {
4530 /* They are exporting something without a =VALUE */
4531 struct variable *var;
4532
4533 var = get_local_var(name);
4534 if (var) {
4535 var->flg_export = 1;
Denis Vlasenkof886fd22008-10-13 12:36:05 +00004536 debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004537 putenv(var->varstr);
4538 }
4539 /* bash does not return an error when trying to export
4540 * an undefined variable. Do likewise. */
4541 return EXIT_SUCCESS;
4542 }
4543
4544 set_local_var(xstrdup(name), 1);
4545 return EXIT_SUCCESS;
4546}
4547
4548#if ENABLE_HUSH_JOB
4549/* built-in 'fg' and 'bg' handler */
4550static int builtin_fg_bg(char **argv)
4551{
4552 int i, jobnum;
4553 struct pipe *pi;
4554
Denis Vlasenko87a86552008-07-29 19:43:10 +00004555 if (!G.interactive_fd)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004556 return EXIT_FAILURE;
4557 /* If they gave us no args, assume they want the last backgrounded task */
4558 if (!argv[1]) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004559 for (pi = G.job_list; pi; pi = pi->next) {
4560 if (pi->jobid == G.last_jobid) {
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004561 goto found;
4562 }
4563 }
4564 bb_error_msg("%s: no current job", argv[0]);
4565 return EXIT_FAILURE;
4566 }
4567 if (sscanf(argv[1], "%%%d", &jobnum) != 1) {
4568 bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]);
4569 return EXIT_FAILURE;
4570 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004571 for (pi = G.job_list; pi; pi = pi->next) {
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004572 if (pi->jobid == jobnum) {
4573 goto found;
4574 }
4575 }
4576 bb_error_msg("%s: %d: no such job", argv[0], jobnum);
4577 return EXIT_FAILURE;
4578 found:
4579 // TODO: bash prints a string representation
4580 // of job being foregrounded (like "sleep 1 | cat")
4581 if (*argv[0] == 'f') {
4582 /* Put the job into the foreground. */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004583 tcsetpgrp(G.interactive_fd, pi->pgrp);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004584 }
4585
4586 /* Restart the processes in the job */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00004587 debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp);
4588 for (i = 0; i < pi->num_cmds; i++) {
4589 debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid);
4590 pi->cmds[i].is_stopped = 0;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004591 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00004592 pi->stopped_cmds = 0;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004593
4594 i = kill(- pi->pgrp, SIGCONT);
4595 if (i < 0) {
4596 if (errno == ESRCH) {
4597 delete_finished_bg_job(pi);
4598 return EXIT_SUCCESS;
4599 } else {
4600 bb_perror_msg("kill (SIGCONT)");
4601 }
4602 }
4603
4604 if (*argv[0] == 'f') {
4605 remove_bg_job(pi);
4606 return checkjobs_and_fg_shell(pi);
4607 }
4608 return EXIT_SUCCESS;
4609}
4610#endif
4611
4612#if ENABLE_HUSH_HELP
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004613static int builtin_help(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004614{
4615 const struct built_in_command *x;
4616
4617 printf("\nBuilt-in commands:\n");
4618 printf("-------------------\n");
4619 for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
4620 printf("%s\t%s\n", x->cmd, x->descr);
4621 }
4622 printf("\n\n");
4623 return EXIT_SUCCESS;
4624}
4625#endif
4626
4627#if ENABLE_HUSH_JOB
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004628static int builtin_jobs(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004629{
4630 struct pipe *job;
4631 const char *status_string;
4632
Denis Vlasenko87a86552008-07-29 19:43:10 +00004633 for (job = G.job_list; job; job = job->next) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00004634 if (job->alive_cmds == job->stopped_cmds)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004635 status_string = "Stopped";
4636 else
4637 status_string = "Running";
4638
4639 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
4640 }
4641 return EXIT_SUCCESS;
4642}
4643#endif
4644
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004645static int builtin_pwd(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004646{
4647 puts(set_cwd());
4648 return EXIT_SUCCESS;
4649}
4650
4651static int builtin_read(char **argv)
4652{
4653 char *string;
4654 const char *name = argv[1] ? argv[1] : "REPLY";
4655
4656 string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL);
4657 return set_local_var(string, 0);
4658}
4659
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +00004660/* built-in 'set' handler
4661 * SUSv3 says:
4662 * set [-abCefmnuvx] [-h] [-o option] [argument...]
4663 * set [+abCefmnuvx] [+h] [+o option] [argument...]
4664 * set -- [argument...]
4665 * set -o
4666 * set +o
4667 * Implementations shall support the options in both their hyphen and
4668 * plus-sign forms. These options can also be specified as options to sh.
4669 * Examples:
4670 * Write out all variables and their values: set
4671 * Set $1, $2, and $3 and set "$#" to 3: set c a b
4672 * Turn on the -x and -v options: set -xv
4673 * Unset all positional parameters: set --
4674 * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x"
4675 * Set the positional parameters to the expansion of x, even if x expands
4676 * with a leading '-' or '+': set -- $x
4677 *
4678 * So far, we only support "set -- [argument...]" by ignoring all options
4679 * (also, "-o option" will be mishandled by taking "option" as parameter #1).
4680 */
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004681static int builtin_set(char **argv)
4682{
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004683 struct variable *e;
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +00004684 char **pp;
4685 char *arg = *++argv;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004686
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +00004687 if (arg == NULL) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004688 for (e = G.top_var; e; e = e->next)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004689 puts(e->varstr);
Denis Vlasenko11fb7cf2009-03-20 10:13:08 +00004690 } else {
4691 /* NB: G.global_argv[0] ($0) is never freed/changed */
4692
4693 if (G.global_args_malloced) {
4694 pp = G.global_argv;
4695 while (*++pp)
4696 free(*pp);
4697 G.global_argv[1] = NULL;
4698 } else {
4699 G.global_args_malloced = 1;
4700 pp = xzalloc(sizeof(pp[0]) * 2);
4701 pp[0] = G.global_argv[0]; /* retain $0 */
4702 G.global_argv = pp;
4703 }
4704 do {
4705 if (arg[0] == '+')
4706 continue;
4707 if (arg[0] != '-')
4708 break;
4709 if (arg[1] == '-' && arg[2] == '\0') {
4710 argv++;
4711 break;
4712 }
4713 } while ((arg = *++argv) != NULL);
4714 /* Now argv[0] is 1st argument */
4715
4716 /* This realloc's G.global_argv */
4717 G.global_argv = pp = add_strings_to_strings(G.global_argv, argv, /*dup:*/ 1);
4718 G.global_argc = 1;
4719 while (*++pp)
4720 G.global_argc++;
4721 }
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004722
4723 return EXIT_SUCCESS;
4724}
4725
4726static int builtin_shift(char **argv)
4727{
4728 int n = 1;
4729 if (argv[1]) {
4730 n = atoi(argv[1]);
4731 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004732 if (n >= 0 && n < G.global_argc) {
Denis Vlasenkoe1300f62009-03-22 11:41:18 +00004733 if (G.global_args_malloced) {
4734 int m = 1;
4735 while (m <= n)
4736 free(G.global_argv[m++]);
4737 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004738 G.global_argc -= n;
Denis Vlasenkoe1300f62009-03-22 11:41:18 +00004739 memmove(&G.global_argv[1], &G.global_argv[n+1],
4740 G.global_argc * sizeof(G.global_argv[0]));
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004741 return EXIT_SUCCESS;
4742 }
4743 return EXIT_FAILURE;
4744}
4745
4746static int builtin_source(char **argv)
4747{
4748 FILE *input;
4749 int status;
4750
4751 if (argv[1] == NULL)
4752 return EXIT_FAILURE;
4753
4754 /* XXX search through $PATH is missing */
Denis Vlasenko5415c852008-07-21 23:05:26 +00004755 input = fopen_for_read(argv[1]);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004756 if (!input) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +00004757 bb_error_msg("can't open '%s'", argv[1]);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004758 return EXIT_FAILURE;
4759 }
4760 close_on_exec_on(fileno(input));
4761
4762 /* Now run the file */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004763 /* XXX argv and argc are broken; need to save old G.global_argv
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004764 * (pointer only is OK!) on this stack frame,
Denis Vlasenko87a86552008-07-29 19:43:10 +00004765 * set G.global_argv=argv+1, recurse, and restore. */
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004766 status = parse_and_run_file(input);
4767 fclose(input);
4768 return status;
4769}
4770
4771static int builtin_umask(char **argv)
4772{
4773 mode_t new_umask;
4774 const char *arg = argv[1];
4775 char *end;
4776 if (arg) {
4777 new_umask = strtoul(arg, &end, 8);
4778 if (*end != '\0' || end == arg) {
4779 return EXIT_FAILURE;
4780 }
4781 } else {
4782 new_umask = umask(0);
4783 printf("%.3o\n", (unsigned) new_umask);
4784 }
4785 umask(new_umask);
4786 return EXIT_SUCCESS;
4787}
4788
4789static int builtin_unset(char **argv)
4790{
4791 /* bash always returns true */
4792 unset_local_var(argv[1]);
4793 return EXIT_SUCCESS;
4794}
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004795
Denis Vlasenkodadfb492008-07-29 10:16:05 +00004796#if ENABLE_HUSH_LOOPS
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004797static int builtin_break(char **argv)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004798{
Denis Vlasenko87a86552008-07-29 19:43:10 +00004799 if (G.depth_of_loop == 0) {
Denis Vlasenko4f504a92008-07-29 19:48:30 +00004800 bb_error_msg("%s: only meaningful in a loop", argv[0]);
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00004801 return EXIT_SUCCESS; /* bash compat */
4802 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004803 G.flag_break_continue++; /* BC_BREAK = 1 */
4804 G.depth_break_continue = 1;
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004805 if (argv[1]) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004806 G.depth_break_continue = bb_strtou(argv[1], NULL, 10);
4807 if (errno || !G.depth_break_continue || argv[2]) {
Denis Vlasenko4f504a92008-07-29 19:48:30 +00004808 bb_error_msg("%s: bad arguments", argv[0]);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004809 G.flag_break_continue = BC_BREAK;
4810 G.depth_break_continue = UINT_MAX;
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004811 }
4812 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004813 if (G.depth_of_loop < G.depth_break_continue)
4814 G.depth_break_continue = G.depth_of_loop;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004815 return EXIT_SUCCESS;
4816}
4817
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004818static int builtin_continue(char **argv)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004819{
Denis Vlasenko4f504a92008-07-29 19:48:30 +00004820 G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */
4821 return builtin_break(argv);
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004822}
Denis Vlasenkodadfb492008-07-29 10:16:05 +00004823#endif