blob: 78622d10642726210284d3d9e4b9d187b7d125f6 [file] [log] [blame]
Eric Andersen25f27032001-04-26 23:22:31 +00001/* vi: set sw=4 ts=4: */
2/*
3 * sh.c -- a prototype Bourne shell grammar parser
4 * Intended to follow the original Thompson and Ritchie
5 * "small and simple is beautiful" philosophy, which
6 * incidentally is a good match to today's BusyBox.
7 *
8 * Copyright (C) 2000,2001 Larry Doolittle <larry@doolittle.boa.org>
9 *
10 * Credits:
11 * The parser routines proper are all original material, first
Eric Andersencb81e642003-07-14 21:21:08 +000012 * written Dec 2000 and Jan 2001 by Larry Doolittle. The
13 * execution engine, the builtins, and much of the underlying
14 * support has been adapted from busybox-0.49pre's lash, which is
Eric Andersenc7bda1c2004-03-15 08:29:22 +000015 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersencb81e642003-07-14 21:21:08 +000016 * written by Erik Andersen <andersen@codepoet.org>. That, in turn,
17 * is based in part on ladsh.c, by Michael K. Johnson and Erik W.
18 * Troan, which they placed in the public domain. I don't know
19 * how much of the Johnson/Troan code has survived the repeated
20 * rewrites.
21 *
Eric Andersen25f27032001-04-26 23:22:31 +000022 * Other credits:
Denis Vlasenkoa8442002008-06-14 11:00:17 +000023 * o_addchr() derived from similar w_addchar function in glibc-2.2.
Eric Andersen25f27032001-04-26 23:22:31 +000024 * setup_redirect(), redirect_opt_num(), and big chunks of main()
Denis Vlasenko55b2de72007-04-18 17:21:28 +000025 * and many builtins derived from contributions by Erik Andersen
Denis Vlasenkoa8442002008-06-14 11:00:17 +000026 * miscellaneous bugfixes from Matt Kraai.
Eric Andersen25f27032001-04-26 23:22:31 +000027 *
28 * There are two big (and related) architecture differences between
29 * this parser and the lash parser. One is that this version is
30 * actually designed from the ground up to understand nearly all
31 * of the Bourne grammar. The second, consequential change is that
32 * the parser and input reader have been turned inside out. Now,
33 * the parser is in control, and asks for input as needed. The old
34 * way had the input reader in control, and it asked for parsing to
35 * take place as needed. The new way makes it much easier to properly
36 * handle the recursion implicit in the various substitutions, especially
37 * across continuation lines.
38 *
39 * Bash grammar not implemented: (how many of these were in original sh?)
Eric Andersen25f27032001-04-26 23:22:31 +000040 * $_
Eric Andersen25f27032001-04-26 23:22:31 +000041 * &> and >& redirection of stdout+stderr
42 * Brace Expansion
43 * Tilde Expansion
44 * fancy forms of Parameter Expansion
Eric Andersen78a7c992001-05-15 16:30:25 +000045 * aliases
Eric Andersen25f27032001-04-26 23:22:31 +000046 * Arithmetic Expansion
47 * <(list) and >(list) Process Substitution
Eric Andersen83a2ae22001-05-07 17:59:25 +000048 * reserved words: case, esac, select, function
Eric Andersen25f27032001-04-26 23:22:31 +000049 * Here Documents ( << word )
50 * Functions
51 * Major bugs:
Denis Vlasenkob81b3df2007-04-28 16:48:04 +000052 * job handling woefully incomplete and buggy (improved --vda)
Eric Andersen25f27032001-04-26 23:22:31 +000053 * reserved word execution woefully incomplete and buggy
Eric Andersen25f27032001-04-26 23:22:31 +000054 * to-do:
Eric Andersen83a2ae22001-05-07 17:59:25 +000055 * port selected bugfixes from post-0.49 busybox lash - done?
Eric Andersen83a2ae22001-05-07 17:59:25 +000056 * change { and } from special chars to reserved words
Denis Vlasenkobcb25532008-07-28 23:04:34 +000057 * builtins: return, trap, ulimit
Eric Andersen83a2ae22001-05-07 17:59:25 +000058 * test magic exec
Eric Andersen25f27032001-04-26 23:22:31 +000059 * check setting of global_argc and global_argv
Eric Andersen25f27032001-04-26 23:22:31 +000060 * follow IFS rules more precisely, including update semantics
Eric Andersen25f27032001-04-26 23:22:31 +000061 * figure out what to do with backslash-newline
Eric Andersen25f27032001-04-26 23:22:31 +000062 * propagate syntax errors, die on resource errors?
63 * continuation lines, both explicit and implicit - done?
64 * memory leak finding and plugging - done?
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +000065 * maybe change charmap[] to use 2-bit entries
Eric Andersen25f27032001-04-26 23:22:31 +000066 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000067 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen25f27032001-04-26 23:22:31 +000068 */
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000069
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000070#include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */
Denis Vlasenkobe709c22008-07-28 00:01:16 +000071#include <glob.h>
72/* #include <dmalloc.h> */
73#if ENABLE_HUSH_CASE
74#include <fnmatch.h>
75#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +000076
Denis Vlasenkobcb25532008-07-28 23:04:34 +000077#define HUSH_VER_STR "0.9"
Denis Vlasenkod01ff132007-05-02 21:40:23 +000078
Denis Vlasenko05743d72008-02-10 12:10:08 +000079#if !BB_MMU && ENABLE_HUSH_TICK
80//#undef ENABLE_HUSH_TICK
81//#define ENABLE_HUSH_TICK 0
82#warning On NOMMU, hush command substitution is dangerous.
83#warning Dont use it for commands which produce lots of output.
84#warning For more info see shell/hush.c, generate_stream_from_list().
85#endif
86
87#if !BB_MMU && ENABLE_HUSH_JOB
88#undef ENABLE_HUSH_JOB
89#define ENABLE_HUSH_JOB 0
90#endif
91
92#if !ENABLE_HUSH_INTERACTIVE
93#undef ENABLE_FEATURE_EDITING
94#define ENABLE_FEATURE_EDITING 0
95#undef ENABLE_FEATURE_EDITING_FANCY_PROMPT
96#define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0
Denis Vlasenko8412d792007-10-01 09:59:47 +000097#endif
98
99
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000100/* If you comment out one of these below, it will be #defined later
101 * to perform debug printfs to stderr: */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000102#define debug_printf(...) do {} while (0)
Denis Vlasenko400c5b62007-05-04 13:07:27 +0000103/* Finer-grained debug switches */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000104#define debug_printf_parse(...) do {} while (0)
105#define debug_print_tree(a, b) do {} while (0)
106#define debug_printf_exec(...) do {} while (0)
107#define debug_printf_jobs(...) do {} while (0)
108#define debug_printf_expand(...) do {} while (0)
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000109#define debug_printf_glob(...) do {} while (0)
110#define debug_printf_list(...) do {} while (0)
Denis Vlasenko30c9cc52008-06-17 07:24:29 +0000111#define debug_printf_subst(...) do {} while (0)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000112#define debug_printf_clean(...) do {} while (0)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000113
114#ifndef debug_printf
115#define debug_printf(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000116#endif
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000117
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000118#ifndef debug_printf_parse
119#define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000120#endif
121
122#ifndef debug_printf_exec
123#define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__)
124#endif
125
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000126#ifndef debug_printf_jobs
127#define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000128#define DEBUG_JOBS 1
129#else
130#define DEBUG_JOBS 0
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000131#endif
132
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000133#ifndef debug_printf_expand
134#define debug_printf_expand(...) fprintf(stderr, __VA_ARGS__)
135#define DEBUG_EXPAND 1
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000136#else
137#define DEBUG_EXPAND 0
138#endif
139
140#ifndef debug_printf_glob
141#define debug_printf_glob(...) fprintf(stderr, __VA_ARGS__)
142#define DEBUG_GLOB 1
143#else
144#define DEBUG_GLOB 0
145#endif
146
147#ifndef debug_printf_list
148#define debug_printf_list(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000149#endif
150
Denis Vlasenko30c9cc52008-06-17 07:24:29 +0000151#ifndef debug_printf_subst
152#define debug_printf_subst(...) fprintf(stderr, __VA_ARGS__)
153#endif
154
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000155#ifndef debug_printf_clean
156/* broken, of course, but OK for testing */
157static const char *indenter(int i)
158{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000159 static const char blanks[] ALIGN1 =
160 " ";
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000161 return &blanks[sizeof(blanks) - i - 1];
162}
163#define debug_printf_clean(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000164#define DEBUG_CLEAN 1
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000165#endif
166
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000167#if DEBUG_EXPAND
168static void debug_print_strings(const char *prefix, char **vv)
169{
170 fprintf(stderr, "%s:\n", prefix);
171 while (*vv)
172 fprintf(stderr, " '%s'\n", *vv++);
173}
174#else
175#define debug_print_strings(prefix, vv) ((void)0)
176#endif
177
Denis Vlasenkof962a032007-11-23 12:50:54 +0000178/*
179 * Leak hunting. Use hush_leaktool.sh for post-processing.
180 */
181#ifdef FOR_HUSH_LEAKTOOL
Denis Vlasenkoccce59d2008-06-16 14:35:57 +0000182/* suppress "warning: no previous prototype..." */
183void *xxmalloc(int lineno, size_t size);
184void *xxrealloc(int lineno, void *ptr, size_t size);
185char *xxstrdup(int lineno, const char *str);
186void xxfree(void *ptr);
Denis Vlasenkof962a032007-11-23 12:50:54 +0000187void *xxmalloc(int lineno, size_t size)
188{
189 void *ptr = xmalloc((size + 0xff) & ~0xff);
190 fprintf(stderr, "line %d: malloc %p\n", lineno, ptr);
191 return ptr;
192}
193void *xxrealloc(int lineno, void *ptr, size_t size)
194{
195 ptr = xrealloc(ptr, (size + 0xff) & ~0xff);
196 fprintf(stderr, "line %d: realloc %p\n", lineno, ptr);
197 return ptr;
198}
199char *xxstrdup(int lineno, const char *str)
200{
201 char *ptr = xstrdup(str);
202 fprintf(stderr, "line %d: strdup %p\n", lineno, ptr);
203 return ptr;
204}
205void xxfree(void *ptr)
206{
207 fprintf(stderr, "free %p\n", ptr);
208 free(ptr);
209}
210#define xmalloc(s) xxmalloc(__LINE__, s)
211#define xrealloc(p, s) xxrealloc(__LINE__, p, s)
212#define xstrdup(s) xxstrdup(__LINE__, s)
213#define free(p) xxfree(p)
214#endif
215
216
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000217/* Keep unconditionally on for now */
218#define HUSH_DEBUG 1
219/* Do we support ANY keywords? */
Denis Vlasenkod91afa32008-07-29 11:10:01 +0000220#if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000221#define HAS_KEYWORDS 1
222#define IF_HAS_KEYWORDS(...) __VA_ARGS__
223#define IF_HAS_NO_KEYWORDS(...)
224#else
225#define HAS_KEYWORDS 0
226#define IF_HAS_KEYWORDS(...)
227#define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__
228#endif
229
230
Denis Vlasenko5703c222008-06-15 11:49:42 +0000231#define SPECIAL_VAR_SYMBOL 3
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000232#define PARSEFLAG_EXIT_FROM_LOOP 1
Eric Andersen25f27032001-04-26 23:22:31 +0000233
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000234typedef enum redir_type {
Eric Andersen25f27032001-04-26 23:22:31 +0000235 REDIRECT_INPUT = 1,
236 REDIRECT_OVERWRITE = 2,
237 REDIRECT_APPEND = 3,
238 REDIRECT_HEREIS = 4,
239 REDIRECT_IO = 5
240} redir_type;
241
Denis Vlasenko55789c62008-06-18 16:30:42 +0000242/* The descrip member of this structure is only used to make
243 * debugging output pretty */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000244static const struct {
245 int mode;
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000246 signed char default_fd;
247 char descrip[3];
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000248} redir_table[] = {
Eric Andersen25f27032001-04-26 23:22:31 +0000249 { 0, 0, "()" },
250 { O_RDONLY, 0, "<" },
251 { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
252 { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" },
253 { O_RDONLY, -1, "<<" },
254 { O_RDWR, 1, "<>" }
255};
256
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000257typedef enum pipe_style {
Eric Andersen25f27032001-04-26 23:22:31 +0000258 PIPE_SEQ = 1,
259 PIPE_AND = 2,
260 PIPE_OR = 3,
261 PIPE_BG = 4,
262} pipe_style;
263
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000264typedef enum reserved_style {
Eric Andersen25f27032001-04-26 23:22:31 +0000265 RES_NONE = 0,
Denis Vlasenko06810332007-05-21 23:30:54 +0000266#if ENABLE_HUSH_IF
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000267 RES_IF ,
268 RES_THEN ,
269 RES_ELIF ,
270 RES_ELSE ,
271 RES_FI ,
Denis Vlasenko06810332007-05-21 23:30:54 +0000272#endif
273#if ENABLE_HUSH_LOOPS
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000274 RES_FOR ,
275 RES_WHILE ,
276 RES_UNTIL ,
277 RES_DO ,
278 RES_DONE ,
Denis Vlasenkod91afa32008-07-29 11:10:01 +0000279#endif
280#if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000281 RES_IN ,
Denis Vlasenko06810332007-05-21 23:30:54 +0000282#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000283#if ENABLE_HUSH_CASE
284 RES_CASE ,
285 /* two pseudo-keywords support contrived "case" syntax: */
286 RES_MATCH , /* "word)" */
287 RES_CASEI , /* "this command is inside CASE" */
288 RES_ESAC ,
289#endif
290 RES_XXXX ,
291 RES_SNTX
Eric Andersen25f27032001-04-26 23:22:31 +0000292} reserved_style;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000293
Eric Andersen25f27032001-04-26 23:22:31 +0000294/* This holds pointers to the various results of parsing */
295struct p_context {
296 struct child_prog *child;
297 struct pipe *list_head;
298 struct pipe *pipe;
299 struct redir_struct *pending_redirect;
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000300#if HAS_KEYWORDS
301 smallint ctx_res_w;
302 smallint ctx_inverted; /* "! cmd | cmd" */
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000303#if ENABLE_HUSH_CASE
304 smallint ctx_dsemicolon; /* ";;" seen */
305#endif
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000306 int old_flag; /* bitmask of FLAG_xxx, for figuring out valid reserved words */
Eric Andersen25f27032001-04-26 23:22:31 +0000307 struct p_context *stack;
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000308#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000309};
310
311struct redir_struct {
Denis Vlasenko55789c62008-06-18 16:30:42 +0000312 struct redir_struct *next;
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000313 char *rd_filename; /* filename */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000314 int fd; /* file descriptor being redirected */
315 int dup; /* -1, or file descriptor being duplicated */
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000316 smallint /*enum redir_type*/ rd_type;
Eric Andersen25f27032001-04-26 23:22:31 +0000317};
318
319struct child_prog {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000320 pid_t pid; /* 0 if exited */
Denis Vlasenko2b576b82008-08-04 00:46:07 +0000321 int assignment_cnt; /* how many argv[i] are assignments? */
Denis Vlasenko55789c62008-06-18 16:30:42 +0000322 smallint is_stopped; /* is the program currently running? */
323 smallint subshell; /* flag, non-zero if group must be forked */
Denis Vlasenko2b576b82008-08-04 00:46:07 +0000324 struct pipe *group; /* if non-NULL, this "prog" is {} group,
325 * subshell, or a compound statement */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000326 char **argv; /* program name and arguments */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000327 struct redir_struct *redirects; /* I/O redirections */
Eric Andersen25f27032001-04-26 23:22:31 +0000328};
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000329/* argv vector may contain variable references (^Cvar^C, ^C0^C etc)
330 * and on execution these are substituted with their values.
331 * Substitution can make _several_ words out of one argv[n]!
332 * Example: argv[0]=='.^C*^C.' here: echo .$*.
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000333 * References of the form ^C`cmd arg^C are `cmd arg` substitutions.
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000334 */
Eric Andersen25f27032001-04-26 23:22:31 +0000335
336struct pipe {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000337 struct pipe *next;
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000338 int num_progs; /* total number of programs in job */
Denis Vlasenko003f9fb2008-06-24 00:47:58 +0000339 int alive_progs; /* number of programs running (not exited) */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000340 int stopped_progs; /* number of programs alive, but stopped */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000341#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000342 int jobid; /* job number */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000343 pid_t pgrp; /* process group ID for the job */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000344 char *cmdtext; /* name of job */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000345#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000346 struct child_prog *progs; /* array of commands in pipe */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000347 smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000348 IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */
349 IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */
Eric Andersen25f27032001-04-26 23:22:31 +0000350};
351
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000352/* On program start, environ points to initial environment.
353 * putenv adds new pointers into it, unsetenv removes them.
354 * Neither of these (de)allocates the strings.
355 * setenv allocates new strings in malloc space and does putenv,
356 * and thus setenv is unusable (leaky) for shell's purposes */
357#define setenv(...) setenv_is_leaky_dont_use()
358struct variable {
359 struct variable *next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000360 char *varstr; /* points to "name=" portion */
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000361 int max_len; /* if > 0, name is part of initial env; else name is malloced */
362 smallint flg_export; /* putenv should be done on this var */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000363 smallint flg_read_only;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000364};
365
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000366typedef struct o_string {
Eric Andersen25f27032001-04-26 23:22:31 +0000367 char *data;
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000368 int length; /* position where data is appended */
Eric Andersen25f27032001-04-26 23:22:31 +0000369 int maxlen;
Denis Vlasenko324a3fd2008-06-18 17:49:58 +0000370 /* Misnomer! it's not "quoting", it's "protection against globbing"!
371 * (by prepending \ to *, ?, [ and to \ too) */
Denis Vlasenkoff097622007-10-01 10:00:45 +0000372 smallint o_quote;
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000373 smallint o_glob;
Denis Vlasenkoff097622007-10-01 10:00:45 +0000374 smallint nonnull;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000375 smallint has_empty_slot;
Denis Vlasenko55789c62008-06-18 16:30:42 +0000376 smallint o_assignment; /* 0:maybe, 1:yes, 2:no */
Eric Andersen25f27032001-04-26 23:22:31 +0000377} o_string;
Denis Vlasenko55789c62008-06-18 16:30:42 +0000378enum {
379 MAYBE_ASSIGNMENT = 0,
380 DEFINITELY_ASSIGNMENT = 1,
381 NOT_ASSIGNMENT = 2,
Denis Vlasenko2b576b82008-08-04 00:46:07 +0000382 WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */
Denis Vlasenko55789c62008-06-18 16:30:42 +0000383};
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000384/* Used for initialization: o_string foo = NULL_O_STRING; */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000385#define NULL_O_STRING { NULL }
Eric Andersen25f27032001-04-26 23:22:31 +0000386
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000387/* I can almost use ordinary FILE*. Is open_memstream() universally
Eric Andersen25f27032001-04-26 23:22:31 +0000388 * available? Where is it documented? */
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000389typedef struct in_str {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +0000390 const char *p;
391 /* eof_flag=1: last char in ->p is really an EOF */
392 char eof_flag; /* meaningless if ->p == NULL */
393 char peek_buf[2];
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000394#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000395 smallint promptme;
396 smallint promptmode; /* 0: PS1, 1: PS2 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000397#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000398 FILE *file;
399 int (*get) (struct in_str *);
400 int (*peek) (struct in_str *);
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000401} in_str;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000402#define i_getch(input) ((input)->get(input))
403#define i_peek(input) ((input)->peek(input))
Eric Andersen25f27032001-04-26 23:22:31 +0000404
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000405enum {
406 CHAR_ORDINARY = 0,
407 CHAR_ORDINARY_IF_QUOTED = 1, /* example: *, # */
408 CHAR_IFS = 2, /* treated as ordinary if quoted */
409 CHAR_SPECIAL = 3, /* example: $ */
Eric Andersen25f27032001-04-26 23:22:31 +0000410};
411
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000412enum {
413 BC_BREAK = 1,
414 BC_CONTINUE = 2,
415};
416
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000417
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000418/* "Globals" within this file */
419
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000420/* Sorted roughly by size (smaller offsets == smaller code) */
421struct globals {
422#if ENABLE_HUSH_INTERACTIVE
423 /* 'interactive_fd' is a fd# open to ctty, if we have one
424 * _AND_ if we decided to act interactively */
425 int interactive_fd;
426 const char *PS1;
427 const char *PS2;
428#endif
429#if ENABLE_FEATURE_EDITING
430 line_input_t *line_input_state;
431#endif
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +0000432 pid_t root_pid;
Denis Vlasenko87a86552008-07-29 19:43:10 +0000433 pid_t last_bg_pid;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000434#if ENABLE_HUSH_JOB
435 int run_list_level;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000436 pid_t saved_tty_pgrp;
437 int last_jobid;
438 struct pipe *job_list;
439 struct pipe *toplevel_list;
440 smallint ctrl_z_flag;
441#endif
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000442#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000443 smallint flag_break_continue;
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000444#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000445 smallint fake_mode;
446 /* these three support $?, $#, and $1 */
Denis Vlasenko5e052ca2008-07-28 15:15:09 +0000447 smalluint last_return_code;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000448 char **global_argv;
449 int global_argc;
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000450#if ENABLE_HUSH_LOOPS
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +0000451 unsigned depth_break_continue;
Denis Vlasenkofcf37c32008-07-29 11:37:15 +0000452 unsigned depth_of_loop;
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000453#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000454 const char *ifs;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000455 const char *cwd;
Denis Vlasenko87a86552008-07-29 19:43:10 +0000456 struct variable *top_var; /* = &G.shell_ver (set in main()) */
Denis Vlasenko0a83fc32007-05-25 11:12:32 +0000457 struct variable shell_ver;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000458#if ENABLE_FEATURE_SH_STANDALONE
459 struct nofork_save_area nofork_save;
460#endif
461#if ENABLE_HUSH_JOB
462 sigjmp_buf toplevel_jb;
463#endif
464 unsigned char charmap[256];
465 char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2];
466};
467
468#define G (*ptr_to_globals)
Denis Vlasenko87a86552008-07-29 19:43:10 +0000469/* Not #defining name to G.name - this quickly gets unwieldy
470 * (too many defines). Also, I actually prefer to see when a variable
471 * is global, thus "G." prefix is a useful hint */
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000472#define INIT_G() do { \
473 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
474} while (0)
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000475
476
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000477#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
478
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000479#if 1
480/* Normal */
481static void syntax(const char *msg)
482{
Denis Vlasenko87a86552008-07-29 19:43:10 +0000483#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000484 /* Was using fancy stuff:
Denis Vlasenko87a86552008-07-29 19:43:10 +0000485 * (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...)
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000486 * but it SEGVs. ?! Oh well... explicit temp ptr works around that */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000487 void FAST_FUNC (*fp)(const char *s, ...);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000488 fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000489 fp(msg ? "%s: %s" : "syntax error", "syntax error", msg);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000490#else
491 bb_error_msg_and_die(msg ? "%s: %s" : "syntax error", "syntax error", msg);
492#endif
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000493}
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000494
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000495#else
496/* Debug */
497static void syntax_lineno(int line)
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000498{
Denis Vlasenko87a86552008-07-29 19:43:10 +0000499#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkoff182a32008-07-05 20:29:59 +0000500 void FAST_FUNC (*fp)(const char *s, ...);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000501 fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000502 fp("syntax error hush.c:%d", line);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000503#else
504 bb_error_msg_and_die("syntax error hush.c:%d", line);
505#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000506}
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000507#define syntax(str) syntax_lineno(__LINE__)
508#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000509
510/* Index of subroutines: */
Eric Andersen25f27032001-04-26 23:22:31 +0000511/* in_str manipulations: */
512static int static_get(struct in_str *i);
513static int static_peek(struct in_str *i);
514static int file_get(struct in_str *i);
515static int file_peek(struct in_str *i);
516static void setup_file_in_str(struct in_str *i, FILE *f);
517static void setup_string_in_str(struct in_str *i, const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000518/* "run" the final data structures: */
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000519#if !defined(DEBUG_CLEAN)
520#define free_pipe_list(head, indent) free_pipe_list(head)
521#define free_pipe(pi, indent) free_pipe(pi)
522#endif
Eric Andersenbf7df042001-05-23 22:18:35 +0000523static int free_pipe_list(struct pipe *head, int indent);
524static int free_pipe(struct pipe *pi, int indent);
Eric Andersen25f27032001-04-26 23:22:31 +0000525/* really run the final data structures: */
526static int setup_redirects(struct child_prog *prog, int squirrel[]);
Denis Vlasenko05743d72008-02-10 12:10:08 +0000527static int run_list(struct pipe *pi);
Denis Vlasenko76d50412008-06-10 16:19:39 +0000528#if BB_MMU
Denis Vlasenko2b576b82008-08-04 00:46:07 +0000529#define pseudo_exec_argv(ptrs2free, argv, assignment_cnt, argv_expanded) \
530 pseudo_exec_argv(argv, assignment_cnt, argv_expanded)
531#define pseudo_exec(ptrs2free, child, argv_expanded) \
532 pseudo_exec(child, argv_expanded)
Denis Vlasenko76d50412008-06-10 16:19:39 +0000533#endif
Denis Vlasenko2b576b82008-08-04 00:46:07 +0000534static void pseudo_exec_argv(char **ptrs2free, char **argv, int assignment_cnt, char **argv_expanded) NORETURN;
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +0000535static void pseudo_exec(char **ptrs2free, struct child_prog *child, char **argv_expanded) NORETURN;
Denis Vlasenko05743d72008-02-10 12:10:08 +0000536static int run_pipe(struct pipe *pi);
Eric Andersen25f27032001-04-26 23:22:31 +0000537/* data structure manipulation: */
538static int setup_redirect(struct p_context *ctx, int fd, redir_type style, struct in_str *input);
539static void initialize_context(struct p_context *ctx);
540static int done_word(o_string *dest, struct p_context *ctx);
541static int done_command(struct p_context *ctx);
Denis Vlasenkoa8442002008-06-14 11:00:17 +0000542static void done_pipe(struct p_context *ctx, pipe_style type);
Eric Andersen25f27032001-04-26 23:22:31 +0000543/* primary string parsing: */
544static int redirect_dup_num(struct in_str *input);
545static int redirect_opt_num(o_string *o);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000546#if ENABLE_HUSH_TICK
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000547static int process_command_subs(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000548 struct in_str *input, const char *subst_end);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000549#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000550static int parse_group(o_string *dest, struct p_context *ctx, struct in_str *input, int ch);
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000551static const char *lookup_param(const char *src);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000552static int handle_dollar(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000553 struct in_str *input);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000554static int parse_stream(o_string *dest, struct p_context *ctx, struct in_str *input0, const char *end_trigger);
Eric Andersen25f27032001-04-26 23:22:31 +0000555/* setup: */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000556static int parse_and_run_stream(struct in_str *inp, int parse_flag);
557static int parse_and_run_string(const char *s, int parse_flag);
558static int parse_and_run_file(FILE *f);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000559/* job management: */
Eric Andersenc798b072001-06-22 06:23:03 +0000560static int checkjobs(struct pipe* fg_pipe);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000561#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +0000562static int checkjobs_and_fg_shell(struct pipe* fg_pipe);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000563static void insert_bg_job(struct pipe *pi);
564static void remove_bg_job(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000565static void delete_finished_bg_job(struct pipe *pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000566#else
567int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */
568#endif
Eric Andersenf72f5622001-05-15 23:21:41 +0000569/* local variable support */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000570static char **expand_strvec_to_strvec(char **argv);
571/* used for eval */
572static char *expand_strvec_to_string(char **argv);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +0000573/* used for expansion of right hand of assignments */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000574static char *expand_string_to_string(const char *str);
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000575static struct variable *get_local_var(const char *name);
576static int set_local_var(char *str, int flg_export);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000577static void unset_local_var(const char *name);
Eric Andersen25f27032001-04-26 23:22:31 +0000578
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000579
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000580static int glob_needed(const char *s)
581{
582 while (*s) {
583 if (*s == '\\')
584 s++;
585 if (*s == '*' || *s == '[' || *s == '?')
586 return 1;
587 s++;
588 }
589 return 0;
590}
591
Denis Vlasenko30c9cc52008-06-17 07:24:29 +0000592static int is_assignment(const char *s)
593{
Denis Vlasenkod4981312008-07-31 10:34:48 +0000594 if (!s || !(isalpha(*s) || *s == '_'))
Denis Vlasenko30c9cc52008-06-17 07:24:29 +0000595 return 0;
596 s++;
597 while (isalnum(*s) || *s == '_')
598 s++;
599 return *s == '=';
600}
601
Denis Vlasenko55789c62008-06-18 16:30:42 +0000602/* Replace each \x with x in place, return ptr past NUL. */
603static char *unbackslash(char *src)
604{
605 char *dst = src;
606 while (1) {
607 if (*src == '\\')
608 src++;
609 if ((*dst++ = *src++) == '\0')
610 break;
611 }
612 return dst;
613}
614
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000615static char **add_malloced_strings_to_strings(char **strings, char **add)
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000616{
617 int i;
618 unsigned count1;
619 unsigned count2;
620 char **v;
621
622 v = strings;
623 count1 = 0;
624 if (v) {
625 while (*v) {
626 count1++;
627 v++;
628 }
629 }
630 count2 = 0;
631 v = add;
632 while (*v) {
633 count2++;
634 v++;
635 }
636 v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*));
637 v[count1 + count2] = NULL;
638 i = count2;
639 while (--i >= 0)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000640 v[count1 + i] = add[i];
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000641 return v;
642}
643
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000644static char **add_malloced_string_to_strings(char **strings, char *add)
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000645{
646 char *v[2];
647
648 v[0] = add;
649 v[1] = NULL;
650
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000651 return add_malloced_strings_to_strings(strings, v);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000652}
653
654static void free_strings(char **strings)
655{
656 if (strings) {
657 char **v = strings;
658 while (*v)
659 free(*v++);
660 free(strings);
661 }
662}
663
Denis Vlasenko76d50412008-06-10 16:19:39 +0000664#if !BB_MMU
665#define EXTRA_PTRS 5 /* 1 for NULL, 1 for args, 3 for paranoid reasons */
666static char **alloc_ptrs(char **argv)
667{
668 char **v = argv;
669 while (*v)
670 v++;
671 return xzalloc((v - argv + EXTRA_PTRS) * sizeof(v[0]));
672}
673#endif
674
675
Denis Vlasenko83506862007-11-23 13:11:42 +0000676/* Function prototypes for builtins */
677static int builtin_cd(char **argv);
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000678static int builtin_echo(char **argv);
Denis Vlasenko83506862007-11-23 13:11:42 +0000679static int builtin_eval(char **argv);
680static int builtin_exec(char **argv);
681static int builtin_exit(char **argv);
682static int builtin_export(char **argv);
683#if ENABLE_HUSH_JOB
684static int builtin_fg_bg(char **argv);
685static int builtin_jobs(char **argv);
686#endif
687#if ENABLE_HUSH_HELP
688static int builtin_help(char **argv);
689#endif
690static int builtin_pwd(char **argv);
691static int builtin_read(char **argv);
692static int builtin_test(char **argv);
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000693static int builtin_true(char **argv);
Denis Vlasenko83506862007-11-23 13:11:42 +0000694static int builtin_set(char **argv);
695static int builtin_shift(char **argv);
696static int builtin_source(char **argv);
697static int builtin_umask(char **argv);
698static int builtin_unset(char **argv);
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000699#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000700static int builtin_break(char **argv);
701static int builtin_continue(char **argv);
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000702#endif
Denis Vlasenko83506862007-11-23 13:11:42 +0000703//static int builtin_not_written(char **argv);
704
Eric Andersen25f27032001-04-26 23:22:31 +0000705/* Table of built-in functions. They can be forked or not, depending on
706 * context: within pipes, they fork. As simple commands, they do not.
707 * When used in non-forking context, they can change global variables
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000708 * in the parent shell process. If forked, of course they cannot.
Eric Andersen25f27032001-04-26 23:22:31 +0000709 * For example, 'unset foo | whatever' will parse and run, but foo will
710 * still be set at the end. */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000711struct built_in_command {
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000712 const char *cmd;
713 int (*function)(char **argv);
Denis Vlasenko06810332007-05-21 23:30:54 +0000714#if ENABLE_HUSH_HELP
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000715 const char *descr;
Denis Vlasenko06810332007-05-21 23:30:54 +0000716#define BLTIN(cmd, func, help) { cmd, func, help }
717#else
718#define BLTIN(cmd, func, help) { cmd, func }
719#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000720};
721
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000722/* For now, echo and test are unconditionally enabled.
723 * Maybe make it configurable? */
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +0000724static const struct built_in_command bltins[] = {
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000725 BLTIN("." , builtin_source, "Run commands in a file"),
726 BLTIN(":" , builtin_true, "No-op"),
Denis Vlasenko83506862007-11-23 13:11:42 +0000727 BLTIN("[" , builtin_test, "Test condition"),
728 BLTIN("[[" , builtin_test, "Test condition"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000729#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000730 BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000731#endif
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000732#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000733 BLTIN("break" , builtin_break, "Exit from a loop"),
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000734#endif
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000735 BLTIN("cd" , builtin_cd, "Change directory"),
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000736#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000737 BLTIN("continue", builtin_continue, "Start new loop iteration"),
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000738#endif
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000739 BLTIN("echo" , builtin_echo, "Write to stdout"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000740 BLTIN("eval" , builtin_eval, "Construct and run shell command"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000741 BLTIN("exec" , builtin_exec, "Execute command, don't return to shell"),
742 BLTIN("exit" , builtin_exit, "Exit"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000743 BLTIN("export", builtin_export, "Set environment variable"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000744#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000745 BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000746 BLTIN("jobs" , builtin_jobs, "List active jobs"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000747#endif
Denis Vlasenko06810332007-05-21 23:30:54 +0000748 BLTIN("pwd" , builtin_pwd, "Print current directory"),
749 BLTIN("read" , builtin_read, "Input environment variable"),
750// BLTIN("return", builtin_not_written, "Return from a function"),
751 BLTIN("set" , builtin_set, "Set/unset shell local variables"),
752 BLTIN("shift" , builtin_shift, "Shift positional parameters"),
753// BLTIN("trap" , builtin_not_written, "Trap signals"),
Denis Vlasenko83506862007-11-23 13:11:42 +0000754 BLTIN("test" , builtin_test, "Test condition"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000755// BLTIN("ulimit", builtin_not_written, "Control resource limits"),
756 BLTIN("umask" , builtin_umask, "Set file creation mask"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000757 BLTIN("unset" , builtin_unset, "Unset environment variable"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000758#if ENABLE_HUSH_HELP
759 BLTIN("help" , builtin_help, "List shell built-in commands"),
760#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000761};
762
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000763
Denis Vlasenko4830fc52008-05-25 21:50:55 +0000764/* Signals are grouped, we handle them in batches */
765static void set_misc_sighandler(void (*handler)(int))
766{
767 bb_signals(0
768 + (1 << SIGINT)
769 + (1 << SIGQUIT)
770 + (1 << SIGTERM)
771 , handler);
772}
773
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000774#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000775
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000776static void set_fatal_sighandler(void (*handler)(int))
777{
Denis Vlasenko25591c32008-02-16 22:58:56 +0000778 bb_signals(0
779 + (1 << SIGILL)
780 + (1 << SIGTRAP)
781 + (1 << SIGABRT)
782 + (1 << SIGFPE)
783 + (1 << SIGBUS)
784 + (1 << SIGSEGV)
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000785 /* bash 3.2 seems to handle these just like 'fatal' ones */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000786 + (1 << SIGHUP)
787 + (1 << SIGPIPE)
788 + (1 << SIGALRM)
789 , handler);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000790}
791static void set_jobctrl_sighandler(void (*handler)(int))
792{
Denis Vlasenko25591c32008-02-16 22:58:56 +0000793 bb_signals(0
794 + (1 << SIGTSTP)
795 + (1 << SIGTTIN)
796 + (1 << SIGTTOU)
797 , handler);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000798}
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000799/* SIGCHLD is special and handled separately */
800
801static void set_every_sighandler(void (*handler)(int))
802{
803 set_fatal_sighandler(handler);
804 set_jobctrl_sighandler(handler);
805 set_misc_sighandler(handler);
806 signal(SIGCHLD, handler);
807}
808
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000809static void handler_ctrl_c(int sig UNUSED_PARAM)
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000810{
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000811 debug_printf_jobs("got sig %d\n", sig);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000812// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenko87a86552008-07-29 19:43:10 +0000813 siglongjmp(G.toplevel_jb, 1);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000814}
815
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000816static void handler_ctrl_z(int sig UNUSED_PARAM)
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000817{
818 pid_t pid;
819
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000820 debug_printf_jobs("got tty sig %d in pid %d\n", sig, getpid());
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000821 pid = fork();
Denis Vlasenkoc2990322007-05-16 12:57:12 +0000822 if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000823 return;
Denis Vlasenko87a86552008-07-29 19:43:10 +0000824 G.ctrl_z_flag = 1;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000825 if (!pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +0000826 if (ENABLE_HUSH_JOB)
827 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000828 setpgrp();
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000829 debug_printf_jobs("set pgrp for child %d ok\n", getpid());
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000830 set_every_sighandler(SIG_DFL);
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000831 raise(SIGTSTP); /* resend TSTP so that child will be stopped */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000832 debug_printf_jobs("returning in child\n");
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000833 /* return to nofork, it will eventually exit now,
834 * not return back to shell */
835 return;
836 }
837 /* parent */
838 /* finish filling up pipe info */
Denis Vlasenko87a86552008-07-29 19:43:10 +0000839 G.toplevel_list->pgrp = pid; /* child is in its own pgrp */
840 G.toplevel_list->progs[0].pid = pid;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000841 /* parent needs to longjmp out of running nofork.
842 * we will "return" exitcode 0, with child put in background */
843// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000844 debug_printf_jobs("siglongjmp in parent\n");
Denis Vlasenko87a86552008-07-29 19:43:10 +0000845 siglongjmp(G.toplevel_jb, 1);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000846}
847
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000848/* Restores tty foreground process group, and exits.
849 * May be called as signal handler for fatal signal
850 * (will faithfully resend signal to itself, producing correct exit state)
851 * or called directly with -EXITCODE.
852 * We also call it if xfunc is exiting. */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000853static void sigexit(int sig) NORETURN;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000854static void sigexit(int sig)
855{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000856 /* Disable all signals: job control, SIGPIPE, etc. */
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000857 sigprocmask_allsigs(SIG_BLOCK);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000858
Denis Vlasenko87a86552008-07-29 19:43:10 +0000859#if ENABLE_HUSH_INTERACTIVE
860 if (G.interactive_fd)
861 tcsetpgrp(G.interactive_fd, G.saved_tty_pgrp);
862#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000863
864 /* Not a signal, just exit */
865 if (sig <= 0)
866 _exit(- sig);
867
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000868 kill_myself_with_sig(sig); /* does not return */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000869}
870
871/* Restores tty foreground process group, and exits. */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000872static void hush_exit(int exitcode) NORETURN;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000873static void hush_exit(int exitcode)
874{
875 fflush(NULL); /* flush all streams */
876 sigexit(- (exitcode & 0xff));
877}
878
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000879#else /* !JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000880
881#define set_fatal_sighandler(handler) ((void)0)
882#define set_jobctrl_sighandler(handler) ((void)0)
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000883#define hush_exit(e) exit(e)
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000884
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000885#endif /* JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000886
887
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000888static const char *set_cwd(void)
889{
Denis Vlasenko87a86552008-07-29 19:43:10 +0000890 if (G.cwd == bb_msg_unknown)
891 G.cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */
892 G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd);
893 if (!G.cwd)
894 G.cwd = bb_msg_unknown;
895 return G.cwd;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000896}
897
Denis Vlasenko83506862007-11-23 13:11:42 +0000898
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000899/*
900 * o_string support
901 */
902#define B_CHUNK (32 * sizeof(char*))
Eric Andersen25f27032001-04-26 23:22:31 +0000903
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000904static void o_reset(o_string *o)
Eric Andersen25f27032001-04-26 23:22:31 +0000905{
906 o->length = 0;
907 o->nonnull = 0;
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000908 if (o->data)
909 o->data[0] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +0000910}
911
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000912static void o_free(o_string *o)
Eric Andersen25f27032001-04-26 23:22:31 +0000913{
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000914 free(o->data);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000915 memset(o, 0, sizeof(*o));
Eric Andersen25f27032001-04-26 23:22:31 +0000916}
917
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000918static void o_grow_by(o_string *o, int len)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000919{
920 if (o->length + len > o->maxlen) {
921 o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK);
922 o->data = xrealloc(o->data, 1 + o->maxlen);
923 }
924}
925
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000926static void o_addchr(o_string *o, int ch)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000927{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000928 debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o);
929 o_grow_by(o, 1);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000930 o->data[o->length] = ch;
931 o->length++;
932 o->data[o->length] = '\0';
933}
934
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000935static void o_addstr(o_string *o, const char *str, int len)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000936{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000937 o_grow_by(o, len);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000938 memcpy(&o->data[o->length], str, len);
939 o->length += len;
940 o->data[o->length] = '\0';
941}
942
Denis Vlasenko55789c62008-06-18 16:30:42 +0000943static void o_addstr_duplicate_backslash(o_string *o, const char *str, int len)
944{
945 while (len) {
946 o_addchr(o, *str);
947 if (*str++ == '\\'
948 && (*str != '*' && *str != '?' && *str != '[')
949 ) {
950 o_addchr(o, '\\');
951 }
952 len--;
953 }
954}
955
Eric Andersen25f27032001-04-26 23:22:31 +0000956/* My analysis of quoting semantics tells me that state information
957 * is associated with a destination, not a source.
958 */
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +0000959static void o_addqchr(o_string *o, int ch)
Eric Andersen25f27032001-04-26 23:22:31 +0000960{
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +0000961 int sz = 1;
962 if (strchr("*?[\\", ch)) {
963 sz++;
964 o->data[o->length] = '\\';
965 o->length++;
Eric Andersen25f27032001-04-26 23:22:31 +0000966 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +0000967 o_grow_by(o, sz);
968 o->data[o->length] = ch;
969 o->length++;
970 o->data[o->length] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +0000971}
972
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +0000973static void o_addQchr(o_string *o, int ch)
Denis Vlasenko87f40ba2008-06-10 22:39:37 +0000974{
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +0000975 int sz = 1;
976 if (o->o_quote && strchr("*?[\\", ch)) {
977 sz++;
978 o->data[o->length] = '\\';
979 o->length++;
980 }
981 o_grow_by(o, sz);
982 o->data[o->length] = ch;
983 o->length++;
984 o->data[o->length] = '\0';
985}
986
987static void o_addQstr(o_string *o, const char *str, int len)
988{
989 if (!o->o_quote) {
Denis Vlasenko87f40ba2008-06-10 22:39:37 +0000990 o_addstr(o, str, len);
991 return;
992 }
993 while (len) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +0000994 char ch;
995 int sz;
996 int ordinary_cnt = strcspn(str, "*?[\\");
997 if (ordinary_cnt > len) /* paranoia */
998 ordinary_cnt = len;
999 o_addstr(o, str, ordinary_cnt);
1000 if (ordinary_cnt == len)
1001 return;
1002 str += ordinary_cnt;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001003 len -= ordinary_cnt + 1; /* we are processing + 1 char below */
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001004
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001005 ch = *str++;
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001006 sz = 1;
1007 if (ch) { /* it is necessarily one of "*?[\\" */
1008 sz++;
1009 o->data[o->length] = '\\';
1010 o->length++;
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001011 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001012 o_grow_by(o, sz);
1013 o->data[o->length] = ch;
1014 o->length++;
1015 o->data[o->length] = '\0';
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001016 }
1017}
1018
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001019/* A special kind of o_string for $VAR and `cmd` expansion.
1020 * It contains char* list[] at the beginning, which is grown in 16 element
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001021 * increments. Actual string data starts at the next multiple of 16 * (char*).
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001022 * list[i] contains an INDEX (int!) into this string data.
1023 * It means that if list[] needs to grow, data needs to be moved higher up
1024 * but list[i]'s need not be modified.
1025 * NB: remembering how many list[i]'s you have there is crucial.
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001026 * o_finalize_list() operation post-processes this structure - calculates
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001027 * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well.
1028 */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001029#if DEBUG_EXPAND || DEBUG_GLOB
1030static void debug_print_list(const char *prefix, o_string *o, int n)
1031{
1032 char **list = (char**)o->data;
1033 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1034 int i = 0;
1035 fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n",
1036 prefix, list, n, string_start, o->length, o->maxlen);
1037 while (i < n) {
1038 fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i],
1039 o->data + (int)list[i] + string_start,
1040 o->data + (int)list[i] + string_start);
1041 i++;
1042 }
1043 if (n) {
1044 const char *p = o->data + (int)list[n - 1] + string_start;
1045 fprintf(stderr, " total_sz:%d\n", (p + strlen(p) + 1) - o->data);
1046 }
1047}
1048#else
1049#define debug_print_list(prefix, o, n) ((void)0)
1050#endif
1051
1052/* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value
1053 * in list[n] so that it points past last stored byte so far.
1054 * It returns n+1. */
1055static int o_save_ptr_helper(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001056{
1057 char **list = (char**)o->data;
Denis Vlasenko895bea22008-06-10 18:06:24 +00001058 int string_start;
1059 int string_len;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001060
1061 if (!o->has_empty_slot) {
Denis Vlasenko895bea22008-06-10 18:06:24 +00001062 string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1063 string_len = o->length - string_start;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001064 if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001065 debug_printf_list("list[%d]=%d string_start=%d (growing)\n", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001066 /* list[n] points to string_start, make space for 16 more pointers */
1067 o->maxlen += 0x10 * sizeof(list[0]);
1068 o->data = xrealloc(o->data, o->maxlen + 1);
Denis Vlasenko7049ff82008-06-25 09:53:17 +00001069 list = (char**)o->data;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001070 memmove(list + n + 0x10, list + n, string_len);
1071 o->length += 0x10 * sizeof(list[0]);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001072 } else
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001073 debug_printf_list("list[%d]=%d string_start=%d\n", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001074 } else {
1075 /* We have empty slot at list[n], reuse without growth */
Denis Vlasenko895bea22008-06-10 18:06:24 +00001076 string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */
1077 string_len = o->length - string_start;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001078 debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001079 o->has_empty_slot = 0;
1080 }
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +00001081 list[n] = (char*)(ptrdiff_t)string_len;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001082 return n + 1;
1083}
1084
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001085/* "What was our last o_save_ptr'ed position (byte offset relative o->data)?" */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001086static int o_get_last_ptr(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001087{
1088 char **list = (char**)o->data;
1089 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1090
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +00001091 return ((int)(ptrdiff_t)list[n-1]) + string_start;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001092}
1093
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001094/* o_glob performs globbing on last list[], saving each result
Denis Vlasenko55789c62008-06-18 16:30:42 +00001095 * as a new list[]. */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001096static int o_glob(o_string *o, int n)
1097{
1098 glob_t globdata;
1099 int gr;
1100 char *pattern;
1101
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001102 debug_printf_glob("start o_glob: n:%d o->data:%p\n", n, o->data);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001103 if (!o->data)
1104 return o_save_ptr_helper(o, n);
1105 pattern = o->data + o_get_last_ptr(o, n);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001106 debug_printf_glob("glob pattern '%s'\n", pattern);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001107 if (!glob_needed(pattern)) {
1108 literal:
1109 o->length = unbackslash(pattern) - o->data;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001110 debug_printf_glob("glob pattern '%s' is literal\n", pattern);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001111 return o_save_ptr_helper(o, n);
1112 }
1113
1114 memset(&globdata, 0, sizeof(globdata));
1115 gr = glob(pattern, 0, NULL, &globdata);
1116 debug_printf_glob("glob('%s'):%d\n", pattern, gr);
1117 if (gr == GLOB_NOSPACE)
1118 bb_error_msg_and_die("out of memory during glob");
1119 if (gr == GLOB_NOMATCH) {
1120 globfree(&globdata);
1121 goto literal;
1122 }
1123 if (gr != 0) { /* GLOB_ABORTED ? */
1124//TODO: testcase for bad glob pattern behavior
1125 bb_error_msg("glob(3) error %d on '%s'", gr, pattern);
1126 }
1127 if (globdata.gl_pathv && globdata.gl_pathv[0]) {
1128 char **argv = globdata.gl_pathv;
1129 o->length = pattern - o->data; /* "forget" pattern */
1130 while (1) {
1131 o_addstr(o, *argv, strlen(*argv) + 1);
1132 n = o_save_ptr_helper(o, n);
1133 argv++;
1134 if (!*argv)
1135 break;
1136 }
1137 }
1138 globfree(&globdata);
1139 if (DEBUG_GLOB)
1140 debug_print_list("o_glob returning", o, n);
1141 return n;
1142}
1143
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001144/* If o->o_glob == 1, glob the string so far remembered.
1145 * Otherwise, just finish current list[] and start new */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001146static int o_save_ptr(o_string *o, int n)
1147{
1148 if (o->o_glob)
1149 return o_glob(o, n); /* o_save_ptr_helper is inside */
1150 return o_save_ptr_helper(o, n);
1151}
1152
1153/* "Please convert list[n] to real char* ptrs, and NULL terminate it." */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001154static char **o_finalize_list(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001155{
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001156 char **list;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001157 int string_start;
1158
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001159 n = o_save_ptr(o, n); /* force growth for list[n] if necessary */
1160 if (DEBUG_EXPAND)
1161 debug_print_list("finalized", o, n);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001162 debug_printf_expand("finalized n:%d\n", n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001163 list = (char**)o->data;
1164 string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1165 list[--n] = NULL;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001166 while (n) {
1167 n--;
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +00001168 list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001169 }
1170 return list;
1171}
1172
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001173
1174/*
1175 * in_str support
1176 */
Eric Andersen25f27032001-04-26 23:22:31 +00001177static int static_get(struct in_str *i)
1178{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001179 int ch = *i->p++;
1180 if (ch == '\0') return EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001181 return ch;
1182}
1183
1184static int static_peek(struct in_str *i)
1185{
1186 return *i->p;
1187}
1188
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001189#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001190
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001191#if ENABLE_FEATURE_EDITING
Rob Landley88621d72006-08-29 19:41:06 +00001192static void cmdedit_set_initial_prompt(void)
Eric Andersen25f27032001-04-26 23:22:31 +00001193{
Denis Vlasenko38f63192007-01-22 09:03:07 +00001194#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko87a86552008-07-29 19:43:10 +00001195 G.PS1 = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00001196#else
Denis Vlasenko87a86552008-07-29 19:43:10 +00001197 G.PS1 = getenv("PS1");
1198 if (G.PS1 == NULL)
1199 G.PS1 = "\\w \\$ ";
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001200#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001201}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001202#endif /* EDITING */
Eric Andersen25f27032001-04-26 23:22:31 +00001203
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001204static const char* setup_prompt_string(int promptmode)
Eric Andersen25f27032001-04-26 23:22:31 +00001205{
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001206 const char *prompt_str;
1207 debug_printf("setup_prompt_string %d ", promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001208#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001209 /* Set up the prompt */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001210 if (promptmode == 0) { /* PS1 */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001211 free((char*)G.PS1);
1212 G.PS1 = xasprintf("%s %c ", G.cwd, (geteuid() != 0) ? '$' : '#');
1213 prompt_str = G.PS1;
Eric Andersen25f27032001-04-26 23:22:31 +00001214 } else {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001215 prompt_str = G.PS2;
Eric Andersen25f27032001-04-26 23:22:31 +00001216 }
1217#else
Denis Vlasenko87a86552008-07-29 19:43:10 +00001218 prompt_str = (promptmode == 0) ? G.PS1 : G.PS2;
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001219#endif
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001220 debug_printf("result '%s'\n", prompt_str);
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001221 return prompt_str;
Eric Andersen25f27032001-04-26 23:22:31 +00001222}
1223
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001224static void get_user_input(struct in_str *i)
Eric Andersen25f27032001-04-26 23:22:31 +00001225{
Denis Vlasenko0937be52007-04-28 16:47:08 +00001226 int r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001227 const char *prompt_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001228
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001229 prompt_str = setup_prompt_string(i->promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001230#if ENABLE_FEATURE_EDITING
Denis Vlasenko170435c2007-05-23 13:01:10 +00001231 /* Enable command line editing only while a command line
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001232 * is actually being read */
1233 do {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001234 r = read_line_input(prompt_str, G.user_input_buf, BUFSIZ-1, G.line_input_state);
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001235 } while (r == 0); /* repeat if Ctrl-C */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001236 i->eof_flag = (r < 0);
1237 if (i->eof_flag) { /* EOF/error detected */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001238 G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */
1239 G.user_input_buf[1] = '\0';
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001240 }
Eric Andersen25f27032001-04-26 23:22:31 +00001241#else
1242 fputs(prompt_str, stdout);
1243 fflush(stdout);
Denis Vlasenko87a86552008-07-29 19:43:10 +00001244 G.user_input_buf[0] = r = fgetc(i->file);
1245 /*G.user_input_buf[1] = '\0'; - already is and never changed */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001246 i->eof_flag = (r == EOF);
Eric Andersen25f27032001-04-26 23:22:31 +00001247#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00001248 i->p = G.user_input_buf;
Eric Andersen25f27032001-04-26 23:22:31 +00001249}
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001250
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001251#endif /* INTERACTIVE */
Eric Andersen25f27032001-04-26 23:22:31 +00001252
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001253/* This is the magic location that prints prompts
Eric Andersen25f27032001-04-26 23:22:31 +00001254 * and gets data back from the user */
1255static int file_get(struct in_str *i)
1256{
1257 int ch;
1258
Eric Andersen25f27032001-04-26 23:22:31 +00001259 /* If there is data waiting, eat it up */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001260 if (i->p && *i->p) {
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001261#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001262 take_cached:
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001263#endif
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001264 ch = *i->p++;
1265 if (i->eof_flag && !*i->p)
1266 ch = EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001267 } else {
1268 /* need to double check i->file because we might be doing something
1269 * more complicated by now, like sourcing or substituting. */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001270#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko87a86552008-07-29 19:43:10 +00001271 if (G.interactive_fd && i->promptme && i->file == stdin) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001272 do {
1273 get_user_input(i);
1274 } while (!*i->p); /* need non-empty line */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001275 i->promptmode = 1; /* PS2 */
1276 i->promptme = 0;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001277 goto take_cached;
Eric Andersen25f27032001-04-26 23:22:31 +00001278 }
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001279#endif
1280 ch = fgetc(i->file);
Eric Andersen25f27032001-04-26 23:22:31 +00001281 }
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001282 debug_printf("file_get: got a '%c' %d\n", ch, ch);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001283#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001284 if (ch == '\n')
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001285 i->promptme = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001286#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001287 return ch;
1288}
1289
1290/* All the callers guarantee this routine will never be
1291 * used right after a newline, so prompting is not needed.
1292 */
1293static int file_peek(struct in_str *i)
1294{
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001295 int ch;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001296 if (i->p && *i->p) {
1297 if (i->eof_flag && !i->p[1])
1298 return EOF;
1299 return *i->p;
Eric Andersen25f27032001-04-26 23:22:31 +00001300 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001301 ch = fgetc(i->file);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001302 i->eof_flag = (ch == EOF);
1303 i->peek_buf[0] = ch;
1304 i->peek_buf[1] = '\0';
1305 i->p = i->peek_buf;
1306 debug_printf("file_peek: got a '%c' %d\n", *i->p, *i->p);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001307 return ch;
Eric Andersen25f27032001-04-26 23:22:31 +00001308}
1309
1310static void setup_file_in_str(struct in_str *i, FILE *f)
1311{
1312 i->peek = file_peek;
1313 i->get = file_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001314#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001315 i->promptme = 1;
1316 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001317#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001318 i->file = f;
1319 i->p = NULL;
1320}
1321
1322static void setup_string_in_str(struct in_str *i, const char *s)
1323{
1324 i->peek = static_peek;
1325 i->get = static_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001326#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001327 i->promptme = 1;
1328 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001329#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001330 i->p = s;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001331 i->eof_flag = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001332}
1333
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001334
Eric Andersen25f27032001-04-26 23:22:31 +00001335/* squirrel != NULL means we squirrel away copies of stdin, stdout,
1336 * and stderr if they are redirected. */
1337static int setup_redirects(struct child_prog *prog, int squirrel[])
1338{
1339 int openfd, mode;
1340 struct redir_struct *redir;
1341
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001342 for (redir = prog->redirects; redir; redir = redir->next) {
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00001343 if (redir->dup == -1 && redir->rd_filename == NULL) {
Eric Andersen817e73c2001-06-06 17:56:09 +00001344 /* something went wrong in the parse. Pretend it didn't happen */
1345 continue;
1346 }
Eric Andersen25f27032001-04-26 23:22:31 +00001347 if (redir->dup == -1) {
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001348 char *p;
Denis Vlasenko55789c62008-06-18 16:30:42 +00001349 mode = redir_table[redir->rd_type].mode;
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00001350//TODO: check redir for names like '\\'
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00001351 p = expand_string_to_string(redir->rd_filename);
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001352 openfd = open_or_warn(p, mode);
1353 free(p);
Eric Andersen25f27032001-04-26 23:22:31 +00001354 if (openfd < 0) {
1355 /* this could get lost if stderr has been redirected, but
1356 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersen25f27032001-04-26 23:22:31 +00001357 return 1;
1358 }
1359 } else {
1360 openfd = redir->dup;
1361 }
1362
1363 if (openfd != redir->fd) {
1364 if (squirrel && redir->fd < 3) {
1365 squirrel[redir->fd] = dup(redir->fd);
1366 }
Eric Andersen83a2ae22001-05-07 17:59:25 +00001367 if (openfd == -3) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001368 //close(openfd); // close(-3) ??!
Eric Andersen83a2ae22001-05-07 17:59:25 +00001369 } else {
1370 dup2(openfd, redir->fd);
Matt Kraaic616e532001-06-05 16:50:08 +00001371 if (redir->dup == -1)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001372 close(openfd);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001373 }
Eric Andersen25f27032001-04-26 23:22:31 +00001374 }
1375 }
1376 return 0;
1377}
1378
1379static void restore_redirects(int squirrel[])
1380{
1381 int i, fd;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001382 for (i = 0; i < 3; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001383 fd = squirrel[i];
1384 if (fd != -1) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00001385 /* We simply die on error */
1386 xmove_fd(fd, i);
Eric Andersen25f27032001-04-26 23:22:31 +00001387 }
1388 }
1389}
1390
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001391
Denis Vlasenko05743d72008-02-10 12:10:08 +00001392/* Called after [v]fork() in run_pipe(), or from builtin_exec().
Denis Vlasenko8412d792007-10-01 09:59:47 +00001393 * Never returns.
1394 * XXX no exit() here. If you don't exec, use _exit instead.
Eric Andersen94ac2442001-05-22 19:05:18 +00001395 * The at_exit handlers apparently confuse the calling process,
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001396 * in particular stdin handling. Not sure why? -- because of vfork! (vda) */
Denis Vlasenko2b576b82008-08-04 00:46:07 +00001397static void pseudo_exec_argv(char **ptrs2free, char **argv, int assignment_cnt, char **argv_expanded)
Eric Andersen25f27032001-04-26 23:22:31 +00001398{
Eric Andersen78a7c992001-05-15 16:30:25 +00001399 int i, rcode;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001400 char *p;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001401 const struct built_in_command *x;
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001402
Denis Vlasenko2b576b82008-08-04 00:46:07 +00001403 for (i = 0; i < assignment_cnt; i++) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001404 debug_printf_exec("pid %d environment modification: %s\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001405 getpid(), argv[i]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001406 p = expand_string_to_string(argv[i]);
Denis Vlasenko76d50412008-06-10 16:19:39 +00001407#if !BB_MMU
1408 *ptrs2free++ = p;
1409#endif
Denis Vlasenko170435c2007-05-23 13:01:10 +00001410 putenv(p);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001411 }
1412 argv += i;
1413 /* If a variable is assigned in a forest, and nobody listens,
1414 * was it ever really set?
1415 */
Denis Vlasenko05743d72008-02-10 12:10:08 +00001416 if (!argv[0])
Denis Vlasenko1359da62007-04-21 23:27:30 +00001417 _exit(EXIT_SUCCESS);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001418
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001419 if (argv_expanded) {
1420 argv = argv_expanded;
1421 } else {
1422 argv = expand_strvec_to_strvec(argv);
Denis Vlasenko76d50412008-06-10 16:19:39 +00001423#if !BB_MMU
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001424 *ptrs2free++ = (char*) argv;
Denis Vlasenko76d50412008-06-10 16:19:39 +00001425#endif
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001426 }
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001427
Denis Vlasenko1359da62007-04-21 23:27:30 +00001428 /*
1429 * Check if the command matches any of the builtins.
1430 * Depending on context, this might be redundant. But it's
1431 * easier to waste a few CPU cycles than it is to figure out
1432 * if this is one of those cases.
1433 */
Denis Vlasenkodd316dd2008-06-14 15:50:55 +00001434 for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001435 if (strcmp(argv[0], x->cmd) == 0) {
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001436 debug_printf_exec("running builtin '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001437 rcode = x->function(argv);
1438 fflush(stdout);
1439 _exit(rcode);
Eric Andersen94ac2442001-05-22 19:05:18 +00001440 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001441 }
Eric Andersen78a7c992001-05-15 16:30:25 +00001442
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001443 /* Check if the command matches any busybox applets */
Denis Vlasenko80d14be2007-04-10 23:03:30 +00001444#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001445 if (strchr(argv[0], '/') == NULL) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001446 int a = find_applet_by_name(argv[0]);
1447 if (a >= 0) {
1448 if (APPLET_IS_NOEXEC(a)) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001449 debug_printf_exec("running applet '%s'\n", argv[0]);
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001450// is it ok that run_applet_no_and_exit() does exit(), not _exit()?
1451 run_applet_no_and_exit(a, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001452 }
1453 /* re-exec ourselves with the new arguments */
1454 debug_printf_exec("re-execing applet '%s'\n", argv[0]);
Denis Vlasenkobdbbb7e2007-06-08 15:02:55 +00001455 execvp(bb_busybox_exec_path, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001456 /* If they called chroot or otherwise made the binary no longer
1457 * executable, fall through */
1458 }
1459 }
Eric Andersenaac75e52001-04-30 18:18:45 +00001460#endif
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001461
1462 debug_printf_exec("execing '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001463 execvp(argv[0], argv);
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +00001464 bb_perror_msg("can't exec '%s'", argv[0]);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001465 _exit(EXIT_FAILURE);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001466}
1467
Denis Vlasenko05743d72008-02-10 12:10:08 +00001468/* Called after [v]fork() in run_pipe()
Denis Vlasenko8412d792007-10-01 09:59:47 +00001469 */
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001470static void pseudo_exec(char **ptrs2free, struct child_prog *child, char **argv_expanded)
Denis Vlasenko1359da62007-04-21 23:27:30 +00001471{
Denis Vlasenko05743d72008-02-10 12:10:08 +00001472 if (child->argv)
Denis Vlasenko2b576b82008-08-04 00:46:07 +00001473 pseudo_exec_argv(ptrs2free, child->argv, child->assignment_cnt, argv_expanded);
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001474
1475 if (child->group) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001476#if !BB_MMU
Denis Vlasenko3b492162007-12-24 14:26:57 +00001477 bb_error_msg_and_die("nested lists are not supported on NOMMU");
Denis Vlasenko8412d792007-10-01 09:59:47 +00001478#else
Denis Vlasenko3b492162007-12-24 14:26:57 +00001479 int rcode;
Denis Vlasenko05743d72008-02-10 12:10:08 +00001480 debug_printf_exec("pseudo_exec: run_list\n");
1481 rcode = run_list(child->group);
Eric Andersenbf7df042001-05-23 22:18:35 +00001482 /* OK to leak memory by not calling free_pipe_list,
Eric Andersen25f27032001-04-26 23:22:31 +00001483 * since this process is about to exit */
Eric Andersen94ac2442001-05-22 19:05:18 +00001484 _exit(rcode);
Denis Vlasenko8412d792007-10-01 09:59:47 +00001485#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001486 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001487
1488 /* Can happen. See what bash does with ">foo" by itself. */
1489 debug_printf("trying to pseudo_exec null command\n");
1490 _exit(EXIT_SUCCESS);
Eric Andersen25f27032001-04-26 23:22:31 +00001491}
1492
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001493#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001494static const char *get_cmdtext(struct pipe *pi)
1495{
1496 char **argv;
1497 char *p;
1498 int len;
1499
1500 /* This is subtle. ->cmdtext is created only on first backgrounding.
1501 * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...)
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001502 * On subsequent bg argv is trashed, but we won't use it */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001503 if (pi->cmdtext)
1504 return pi->cmdtext;
1505 argv = pi->progs[0].argv;
Denis Vlasenkoa8442002008-06-14 11:00:17 +00001506 if (!argv || !argv[0]) {
1507 pi->cmdtext = xzalloc(1);
1508 return pi->cmdtext;
1509 }
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001510
1511 len = 0;
1512 do len += strlen(*argv) + 1; while (*++argv);
1513 pi->cmdtext = p = xmalloc(len);
1514 argv = pi->progs[0].argv;
1515 do {
1516 len = strlen(*argv);
1517 memcpy(p, *argv, len);
1518 p += len;
1519 *p++ = ' ';
1520 } while (*++argv);
1521 p[-1] = '\0';
1522 return pi->cmdtext;
1523}
1524
Eric Andersenbafd94f2001-05-02 16:11:59 +00001525static void insert_bg_job(struct pipe *pi)
1526{
1527 struct pipe *thejob;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001528 int i;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001529
1530 /* Linear search for the ID of the job to use */
1531 pi->jobid = 1;
Denis Vlasenko87a86552008-07-29 19:43:10 +00001532 for (thejob = G.job_list; thejob; thejob = thejob->next)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001533 if (thejob->jobid >= pi->jobid)
1534 pi->jobid = thejob->jobid + 1;
1535
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001536 /* Add thejob to the list of running jobs */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001537 if (!G.job_list) {
1538 thejob = G.job_list = xmalloc(sizeof(*thejob));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001539 } else {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001540 for (thejob = G.job_list; thejob->next; thejob = thejob->next)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001541 continue;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001542 thejob->next = xmalloc(sizeof(*thejob));
1543 thejob = thejob->next;
1544 }
1545
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001546 /* Physically copy the struct job */
Eric Andersen0fcd4472001-05-02 20:12:03 +00001547 memcpy(thejob, pi, sizeof(struct pipe));
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001548 thejob->progs = xzalloc(sizeof(pi->progs[0]) * pi->num_progs);
1549 /* We cannot copy entire pi->progs[] vector! Double free()s will happen */
1550 for (i = 0; i < pi->num_progs; i++) {
1551// TODO: do we really need to have so many fields which are just dead weight
1552// at execution stage?
1553 thejob->progs[i].pid = pi->progs[i].pid;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001554 /* all other fields are not used and stay zero */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001555 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001556 thejob->next = NULL;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001557 thejob->cmdtext = xstrdup(get_cmdtext(pi));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001558
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001559 /* We don't wait for background thejobs to return -- append it
Eric Andersenbafd94f2001-05-02 16:11:59 +00001560 to the list of backgrounded thejobs and leave it alone */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001561 printf("[%d] %d %s\n", thejob->jobid, thejob->progs[0].pid, thejob->cmdtext);
Denis Vlasenko87a86552008-07-29 19:43:10 +00001562 G.last_bg_pid = thejob->progs[0].pid;
1563 G.last_jobid = thejob->jobid;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001564}
1565
Eric Andersenbafd94f2001-05-02 16:11:59 +00001566static void remove_bg_job(struct pipe *pi)
1567{
1568 struct pipe *prev_pipe;
1569
Denis Vlasenko87a86552008-07-29 19:43:10 +00001570 if (pi == G.job_list) {
1571 G.job_list = pi->next;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001572 } else {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001573 prev_pipe = G.job_list;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001574 while (prev_pipe->next != pi)
1575 prev_pipe = prev_pipe->next;
1576 prev_pipe->next = pi->next;
1577 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00001578 if (G.job_list)
1579 G.last_jobid = G.job_list->jobid;
Eric Andersen028b65b2001-06-28 01:10:11 +00001580 else
Denis Vlasenko87a86552008-07-29 19:43:10 +00001581 G.last_jobid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001582}
Eric Andersen028b65b2001-06-28 01:10:11 +00001583
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001584/* Remove a backgrounded job */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001585static void delete_finished_bg_job(struct pipe *pi)
1586{
1587 remove_bg_job(pi);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001588 pi->stopped_progs = 0;
Eric Andersenbf7df042001-05-23 22:18:35 +00001589 free_pipe(pi, 0);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001590 free(pi);
1591}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001592#endif /* JOB */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001593
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001594/* Check to see if any processes have exited -- if they
1595 * have, figure out why and see if a job has completed */
Eric Andersenc798b072001-06-22 06:23:03 +00001596static int checkjobs(struct pipe* fg_pipe)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001597{
Eric Andersenc798b072001-06-22 06:23:03 +00001598 int attributes;
1599 int status;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001600#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00001601 struct pipe *pi;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001602#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001603 pid_t childpid;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001604 int rcode = 0;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001605
Eric Andersenc798b072001-06-22 06:23:03 +00001606 attributes = WUNTRACED;
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001607 if (fg_pipe == NULL)
Eric Andersenc798b072001-06-22 06:23:03 +00001608 attributes |= WNOHANG;
Eric Andersenc798b072001-06-22 06:23:03 +00001609
Denis Vlasenko1359da62007-04-21 23:27:30 +00001610/* Do we do this right?
1611 * bash-3.00# sleep 20 | false
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001612 * <ctrl-Z pressed>
Denis Vlasenko1359da62007-04-21 23:27:30 +00001613 * [3]+ Stopped sleep 20 | false
1614 * bash-3.00# echo $?
1615 * 1 <========== bg pipe is not fully done, but exitcode is already known!
1616 */
1617
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001618//FIXME: non-interactive bash does not continue even if all processes in fg pipe
1619//are stopped. Testcase: "cat | cat" in a script (not on command line)
1620// + killall -STOP cat
1621
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001622 wait_more:
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001623// TODO: safe_waitpid?
Eric Andersenc798b072001-06-22 06:23:03 +00001624 while ((childpid = waitpid(-1, &status, attributes)) > 0) {
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001625 int i;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001626 const int dead = WIFEXITED(status) || WIFSIGNALED(status);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001627#if DEBUG_JOBS
Denis Vlasenko1359da62007-04-21 23:27:30 +00001628 if (WIFSTOPPED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001629 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001630 childpid, WSTOPSIG(status), WEXITSTATUS(status));
1631 if (WIFSIGNALED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001632 debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001633 childpid, WTERMSIG(status), WEXITSTATUS(status));
1634 if (WIFEXITED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001635 debug_printf_jobs("pid %d exited, exitcode %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001636 childpid, WEXITSTATUS(status));
1637#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001638 /* Were we asked to wait for fg pipe? */
Eric Andersenc798b072001-06-22 06:23:03 +00001639 if (fg_pipe) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001640 for (i = 0; i < fg_pipe->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001641 debug_printf_jobs("check pid %d\n", fg_pipe->progs[i].pid);
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001642 if (fg_pipe->progs[i].pid != childpid)
1643 continue;
1644 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */
1645 if (dead) {
1646 fg_pipe->progs[i].pid = 0;
1647 fg_pipe->alive_progs--;
1648 if (i == fg_pipe->num_progs - 1) {
1649 /* last process gives overall exitstatus */
1650 rcode = WEXITSTATUS(status);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00001651 IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;)
Denis Vlasenko1359da62007-04-21 23:27:30 +00001652 }
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001653 } else {
1654 fg_pipe->progs[i].is_stopped = 1;
1655 fg_pipe->stopped_progs++;
Eric Andersenc798b072001-06-22 06:23:03 +00001656 }
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001657 debug_printf_jobs("fg_pipe: alive_progs %d stopped_progs %d\n",
1658 fg_pipe->alive_progs, fg_pipe->stopped_progs);
1659 if (fg_pipe->alive_progs - fg_pipe->stopped_progs <= 0) {
1660 /* All processes in fg pipe have exited/stopped */
1661#if ENABLE_HUSH_JOB
1662 if (fg_pipe->alive_progs)
1663 insert_bg_job(fg_pipe);
1664#endif
1665 return rcode;
1666 }
1667 /* There are still running processes in the fg pipe */
1668 goto wait_more; /* do waitpid again */
Eric Andersenc798b072001-06-22 06:23:03 +00001669 }
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001670 /* it wasnt fg_pipe, look for process in bg pipes */
Eric Andersenc798b072001-06-22 06:23:03 +00001671 }
1672
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001673#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001674 /* We asked to wait for bg or orphaned children */
1675 /* No need to remember exitcode in this case */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001676 for (pi = G.job_list; pi; pi = pi->next) {
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001677 for (i = 0; i < pi->num_progs; i++) {
1678 if (pi->progs[i].pid == childpid)
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001679 goto found_pi_and_prognum;
Eric Andersen52a97ca2001-06-22 06:49:26 +00001680 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001681 }
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001682 /* Happens when shell is used as init process (init=/bin/sh) */
1683 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001684 continue; /* do waitpid again */
Eric Andersenaeb44c42001-05-22 20:29:00 +00001685
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001686 found_pi_and_prognum:
Denis Vlasenko1359da62007-04-21 23:27:30 +00001687 if (dead) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001688 /* child exited */
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001689 pi->progs[i].pid = 0;
1690 pi->alive_progs--;
1691 if (!pi->alive_progs) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001692 printf(JOB_STATUS_FORMAT, pi->jobid,
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001693 "Done", pi->cmdtext);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001694 delete_finished_bg_job(pi);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001695 }
1696 } else {
1697 /* child stopped */
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001698 pi->progs[i].is_stopped = 1;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001699 pi->stopped_progs++;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001700 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001701#endif
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001702 } /* while (waitpid succeeds)... */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001703
Denis Vlasenko1359da62007-04-21 23:27:30 +00001704 /* wait found no children or failed */
1705
1706 if (childpid && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001707 bb_perror_msg("waitpid");
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001708 return rcode;
Eric Andersenada18ff2001-05-21 16:18:22 +00001709}
1710
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001711#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +00001712static int checkjobs_and_fg_shell(struct pipe* fg_pipe)
1713{
1714 pid_t p;
1715 int rcode = checkjobs(fg_pipe);
1716 /* Job finished, move the shell to the foreground */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001717 p = getpgid(0); /* pgid of our process */
1718 debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p);
Denis Vlasenko87a86552008-07-29 19:43:10 +00001719 tcsetpgrp(G.interactive_fd, p);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001720 return rcode;
1721}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001722#endif
Denis Vlasenko52881e92007-04-21 13:42:52 +00001723
Denis Vlasenko05743d72008-02-10 12:10:08 +00001724/* run_pipe() starts all the jobs, but doesn't wait for anything
Eric Andersenc798b072001-06-22 06:23:03 +00001725 * to finish. See checkjobs().
Eric Andersen25f27032001-04-26 23:22:31 +00001726 *
1727 * return code is normally -1, when the caller has to wait for children
1728 * to finish to determine the exit status of the pipe. If the pipe
1729 * is a simple builtin command, however, the action is done by the
Denis Vlasenko05743d72008-02-10 12:10:08 +00001730 * time run_pipe returns, and the exit code is provided as the
Eric Andersen25f27032001-04-26 23:22:31 +00001731 * return value.
1732 *
1733 * The input of the pipe is always stdin, the output is always
1734 * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
1735 * because it tries to avoid running the command substitution in
1736 * subshell, when that is in fact necessary. The subshell process
1737 * now has its stdout directed to the input of the appropriate pipe,
1738 * so this routine is noticeably simpler.
Denis Vlasenko170435c2007-05-23 13:01:10 +00001739 *
1740 * Returns -1 only if started some children. IOW: we have to
1741 * mask out retvals of builtins etc with 0xff!
Eric Andersen25f27032001-04-26 23:22:31 +00001742 */
Denis Vlasenko05743d72008-02-10 12:10:08 +00001743static int run_pipe(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00001744{
1745 int i;
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001746 int nextin;
1747 int pipefds[2]; /* pipefds[0] is for reading */
Rob Landley53702e52006-07-19 21:43:53 +00001748 struct child_prog *child;
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001749 char **argv_expanded = NULL;
1750 char **argv;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001751 const struct built_in_command *x;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001752 char *p;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001753 /* it is not always needed, but we aim to smaller code */
1754 int squirrel[] = { -1, -1, -1 };
1755 int rcode;
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001756 const int single_and_fg = (pi->num_progs == 1 && pi->followup != PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00001757
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001758 debug_printf_exec("run_pipe start: single_and_fg=%d\n", single_and_fg);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001759
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001760#if ENABLE_HUSH_JOB
Eric Andersenada18ff2001-05-21 16:18:22 +00001761 pi->pgrp = -1;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001762#endif
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001763 pi->alive_progs = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001764 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001765
1766 /* Check if this is a simple builtin (not part of a pipe).
1767 * Builtins within pipes have to fork anyway, and are handled in
1768 * pseudo_exec. "echo foo | read bar" doesn't work on bash, either.
1769 */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001770 child = &(pi->progs[0]);
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001771 if (single_and_fg && child->group && child->subshell == 0) {
Eric Andersen04407e52001-06-07 16:42:05 +00001772 debug_printf("non-subshell grouping\n");
1773 setup_redirects(child, squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001774 debug_printf_exec(": run_list\n");
1775 rcode = run_list(child->group) & 0xff;
Eric Andersen04407e52001-06-07 16:42:05 +00001776 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001777 debug_printf_exec("run_pipe return %d\n", rcode);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00001778 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
Denis Vlasenko05743d72008-02-10 12:10:08 +00001779 return rcode;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001780 }
1781
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001782 argv = child->argv;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001783
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001784 if (single_and_fg && argv != NULL) {
Denis Vlasenko2b576b82008-08-04 00:46:07 +00001785 i = child->assignment_cnt;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001786 if (i != 0 && argv[i] == NULL) {
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001787 /* assignments, but no command: set local environment */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001788 for (i = 0; argv[i] != NULL; i++) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001789 debug_printf("local environment set: %s\n", argv[i]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001790 p = expand_string_to_string(argv[i]);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001791 set_local_var(p, 0);
Eric Andersen78a7c992001-05-15 16:30:25 +00001792 }
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001793 return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */
Eric Andersen78a7c992001-05-15 16:30:25 +00001794 }
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001795
1796 /* Expand assignments into one string each */
Denis Vlasenko2b576b82008-08-04 00:46:07 +00001797 for (i = 0; i < child->assignment_cnt; i++) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00001798 p = expand_string_to_string(argv[i]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001799 putenv(p);
Denis Vlasenko55789c62008-06-18 16:30:42 +00001800//FIXME: do we leak p?!
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001801 }
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001802
1803 /* Expand the rest into (possibly) many strings each */
1804 argv_expanded = expand_strvec_to_strvec(argv + i);
1805
Denis Vlasenkodd316dd2008-06-14 15:50:55 +00001806 for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001807 if (strcmp(argv_expanded[0], x->cmd) == 0) {
1808 if (x->function == builtin_exec && argv_expanded[1] == NULL) {
Eric Andersen83a2ae22001-05-07 17:59:25 +00001809 debug_printf("magic exec\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001810 setup_redirects(child, NULL);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001811 return EXIT_SUCCESS;
1812 }
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001813 debug_printf("builtin inline %s\n", argv_expanded[0]);
Eric Andersen25f27032001-04-26 23:22:31 +00001814 /* XXX setup_redirects acts on file descriptors, not FILEs.
1815 * This is perfect for work that comes after exec().
1816 * Is it really safe for inline use? Experimentally,
1817 * things seem to work with glibc. */
1818 setup_redirects(child, squirrel);
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001819 debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv_expanded[1]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001820 rcode = x->function(argv_expanded) & 0xff;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001821 free(argv_expanded);
Eric Andersen25f27032001-04-26 23:22:31 +00001822 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001823 debug_printf_exec("run_pipe return %d\n", rcode);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00001824 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
Eric Andersen25f27032001-04-26 23:22:31 +00001825 return rcode;
1826 }
1827 }
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001828#if ENABLE_FEATURE_SH_STANDALONE
1829 {
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001830 int a = find_applet_by_name(argv_expanded[0]);
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001831 if (a >= 0 && APPLET_IS_NOFORK(a)) {
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001832 setup_redirects(child, squirrel);
Denis Vlasenko87a86552008-07-29 19:43:10 +00001833 save_nofork_data(&G.nofork_save);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001834 debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]);
Denis Vlasenko87a86552008-07-29 19:43:10 +00001835 rcode = run_nofork_applet_prime(&G.nofork_save, a, argv_expanded);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001836 free(argv_expanded);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001837 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001838 debug_printf_exec("run_pipe return %d\n", rcode);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00001839 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001840 return rcode;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001841 }
1842 }
1843#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001844 }
1845
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001846 /* NB: argv_expanded may already be created, and that
1847 * might include `cmd` runs! Do not rerun it! We *must*
1848 * use argv_expanded if it's non-NULL */
1849
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001850 /* Disable job control signals for shell (parent) and
1851 * for initial child code after fork */
1852 set_jobctrl_sighandler(SIG_IGN);
1853
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001854 /* Going to fork a child per each pipe member */
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001855 pi->alive_progs = 0;
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001856 nextin = 0;
1857
Eric Andersen25f27032001-04-26 23:22:31 +00001858 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenko76d50412008-06-10 16:19:39 +00001859#if !BB_MMU
1860 char **ptrs2free = NULL;
1861#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001862 child = &(pi->progs[i]);
Denis Vlasenko76d50412008-06-10 16:19:39 +00001863 if (child->argv) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001864 debug_printf_exec(": pipe member '%s' '%s'...\n", child->argv[0], child->argv[1]);
Denis Vlasenko76d50412008-06-10 16:19:39 +00001865#if !BB_MMU
1866 ptrs2free = alloc_ptrs(child->argv);
1867#endif
1868 } else
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001869 debug_printf_exec(": pipe member with no argv\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001870
1871 /* pipes are inserted between pairs of commands */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001872 pipefds[0] = 0;
1873 pipefds[1] = 1;
1874 if ((i + 1) < pi->num_progs)
1875 xpipe(pipefds);
Eric Andersen25f27032001-04-26 23:22:31 +00001876
Denis Vlasenko05743d72008-02-10 12:10:08 +00001877 child->pid = BB_MMU ? fork() : vfork();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001878 if (!child->pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00001879 if (ENABLE_HUSH_JOB)
1880 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001881#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001882 /* Every child adds itself to new process group
Denis Vlasenko05743d72008-02-10 12:10:08 +00001883 * with pgid == pid_of_first_child_in_pipe */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001884 if (G.run_list_level == 1 && G.interactive_fd) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00001885 pid_t pgrp;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001886 /* Don't do pgrp restore anymore on fatal signals */
1887 set_fatal_sighandler(SIG_DFL);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001888 pgrp = pi->pgrp;
1889 if (pgrp < 0) /* true for 1st process only */
1890 pgrp = getpid();
1891 if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001892 /* We do it in *every* child, not just first,
1893 * to avoid races */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001894 tcsetpgrp(G.interactive_fd, pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001895 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001896 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001897#endif
Denis Vlasenko8412d792007-10-01 09:59:47 +00001898 xmove_fd(nextin, 0);
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001899 xmove_fd(pipefds[1], 1); /* write end */
1900 if (pipefds[0] > 1)
1901 close(pipefds[0]); /* read end */
Eric Andersen25f27032001-04-26 23:22:31 +00001902 /* Like bash, explicit redirects override pipes,
1903 * and the pipe fd is available for dup'ing. */
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001904 setup_redirects(child, NULL);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001905
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001906 /* Restore default handlers just prior to exec */
1907 set_jobctrl_sighandler(SIG_DFL);
1908 set_misc_sighandler(SIG_DFL);
1909 signal(SIGCHLD, SIG_DFL);
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001910 pseudo_exec(ptrs2free, child, argv_expanded); /* does not return */
Eric Andersen25f27032001-04-26 23:22:31 +00001911 }
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001912 free(argv_expanded);
1913 argv_expanded = NULL;
Denis Vlasenko76d50412008-06-10 16:19:39 +00001914#if !BB_MMU
1915 free_strings(ptrs2free);
1916#endif
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001917 if (child->pid < 0) { /* [v]fork failed */
1918 /* Clearly indicate, was it fork or vfork */
Denis Vlasenko82604e92008-07-01 15:59:42 +00001919 bb_perror_msg(BB_MMU ? "fork" : "vfork");
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001920 } else {
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001921 pi->alive_progs++;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001922#if ENABLE_HUSH_JOB
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001923 /* Second and next children need to know pid of first one */
1924 if (pi->pgrp < 0)
1925 pi->pgrp = child->pid;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001926#endif
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001927 }
Eric Andersen25f27032001-04-26 23:22:31 +00001928
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001929 if (i)
1930 close(nextin);
1931 if ((i + 1) < pi->num_progs)
1932 close(pipefds[1]); /* write end */
1933 /* Pass read (output) pipe end to next iteration */
Eric Andersen25f27032001-04-26 23:22:31 +00001934 nextin = pipefds[0];
1935 }
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001936
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001937 if (!pi->alive_progs) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00001938 debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n");
1939 return 1;
1940 }
1941
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001942 debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00001943 return -1;
1944}
1945
Denis Vlasenko4b924f32007-05-30 00:29:55 +00001946#ifndef debug_print_tree
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001947static void debug_print_tree(struct pipe *pi, int lvl)
1948{
1949 static const char *PIPE[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001950 [PIPE_SEQ] = "SEQ",
1951 [PIPE_AND] = "AND",
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001952 [PIPE_OR ] = "OR" ,
1953 [PIPE_BG ] = "BG" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001954 };
1955 static const char *RES[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001956 [RES_NONE ] = "NONE" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00001957#if ENABLE_HUSH_IF
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001958 [RES_IF ] = "IF" ,
1959 [RES_THEN ] = "THEN" ,
1960 [RES_ELIF ] = "ELIF" ,
1961 [RES_ELSE ] = "ELSE" ,
1962 [RES_FI ] = "FI" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00001963#endif
1964#if ENABLE_HUSH_LOOPS
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001965 [RES_FOR ] = "FOR" ,
1966 [RES_WHILE] = "WHILE",
1967 [RES_UNTIL] = "UNTIL",
1968 [RES_DO ] = "DO" ,
1969 [RES_DONE ] = "DONE" ,
Denis Vlasenkod91afa32008-07-29 11:10:01 +00001970#endif
1971#if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001972 [RES_IN ] = "IN" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00001973#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00001974#if ENABLE_HUSH_CASE
1975 [RES_CASE ] = "CASE" ,
1976 [RES_MATCH] = "MATCH",
1977 [RES_CASEI] = "CASEI",
1978 [RES_ESAC ] = "ESAC" ,
1979#endif
Denis Vlasenko06810332007-05-21 23:30:54 +00001980 [RES_XXXX ] = "XXXX" ,
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001981 [RES_SNTX ] = "SNTX" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001982 };
1983
1984 int pin, prn;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00001985
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001986 pin = 0;
1987 while (pi) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00001988 fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "",
1989 pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001990 prn = 0;
1991 while (prn < pi->num_progs) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00001992 struct child_prog *child = &pi->progs[prn];
1993 char **argv = child->argv;
1994
Denis Vlasenko2b576b82008-08-04 00:46:07 +00001995 fprintf(stderr, "%*s prog %d assignment_cnt:%d", lvl*2, "", prn, child->assignment_cnt);
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00001996 if (child->group) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001997 fprintf(stderr, " group %s: (argv=%p)\n",
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00001998 (child->subshell ? "()" : "{}"),
1999 argv);
2000 debug_print_tree(child->group, lvl+1);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002001 prn++;
2002 continue;
2003 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002004 if (argv) while (*argv) {
2005 fprintf(stderr, " '%s'", *argv);
2006 argv++;
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002007 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002008 fprintf(stderr, "\n");
2009 prn++;
2010 }
2011 pi = pi->next;
2012 pin++;
2013 }
2014}
2015#endif
2016
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002017/* NB: called by pseudo_exec, and therefore must not modify any
2018 * global data until exec/_exit (we can be a child after vfork!) */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002019static int run_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002020{
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002021#if ENABLE_HUSH_CASE
2022 char *case_word = NULL;
2023#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002024#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002025 struct pipe *loop_top = NULL;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002026 char *for_varname = NULL;
2027 char **for_lcur = NULL;
2028 char **for_list = NULL;
Denis Vlasenko06810332007-05-21 23:30:54 +00002029#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002030 smallint flag_skip = 1;
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002031 smalluint rcode = 0; /* probably just for compiler */
Denis Vlasenkod91afa32008-07-29 11:10:01 +00002032#if ENABLE_HUSH_IF || ENABLE_HUSH_CASE
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002033 smalluint cond_code = 0;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002034#else
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002035 enum { cond_code = 0, };
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002036#endif
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002037 /*enum reserved_style*/ smallint rword = RES_NONE;
2038 /*enum reserved_style*/ smallint skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002039
Denis Vlasenko87a86552008-07-29 19:43:10 +00002040 debug_printf_exec("run_list start lvl %d\n", G.run_list_level + 1);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002041
Denis Vlasenko06810332007-05-21 23:30:54 +00002042#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002043 /* Check syntax for "for" */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002044 for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) {
2045 if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN)
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002046 continue;
2047 /* current word is FOR or IN (BOLD in comments below) */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002048 if (cpipe->next == NULL) {
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002049 syntax("malformed for");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002050 debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002051 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002052 }
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002053 /* "FOR v; do ..." and "for v IN a b; do..." are ok */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002054 if (cpipe->next->res_word == RES_DO)
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002055 continue;
2056 /* next word is not "do". It must be "in" then ("FOR v in ...") */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002057 if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */
2058 || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002059 ) {
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002060 syntax("malformed for");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002061 debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002062 return 1;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002063 }
2064 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002065#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002066
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002067 /* Past this point, all code paths should jump to ret: label
Denis Vlasenko12acec52008-07-28 15:15:59 +00002068 * in order to return, no direct "return" statements please.
2069 * This helps to ensure that no memory is leaked. */
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002070
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002071#if ENABLE_HUSH_JOB
2072 /* Example of nested list: "while true; do { sleep 1 | exit 2; } done".
2073 * We are saving state before entering outermost list ("while...done")
2074 * so that ctrl-Z will correctly background _entire_ outermost list,
2075 * not just a part of it (like "sleep 1 | exit 2") */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002076 if (++G.run_list_level == 1 && G.interactive_fd) {
2077 if (sigsetjmp(G.toplevel_jb, 1)) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002078 /* ctrl-Z forked and we are parent; or ctrl-C.
2079 * Sighandler has longjmped us here */
2080 signal(SIGINT, SIG_IGN);
2081 signal(SIGTSTP, SIG_IGN);
2082 /* Restore level (we can be coming from deep inside
2083 * nested levels) */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002084 G.run_list_level = 1;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002085#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko87a86552008-07-29 19:43:10 +00002086 if (G.nofork_save.saved) { /* if save area is valid */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002087 debug_printf_jobs("exiting nofork early\n");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002088 restore_nofork_data(&G.nofork_save);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002089 }
2090#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00002091 if (G.ctrl_z_flag) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002092 /* ctrl-Z has forked and stored pid of the child in pi->pid.
2093 * Remember this child as background job */
2094 insert_bg_job(pi);
2095 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002096 /* ctrl-C. We just stop doing whatever we were doing */
Denis Vlasenko4daad902007-09-27 10:20:47 +00002097 bb_putchar('\n');
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002098 }
Denis Vlasenkoff29b4f2008-07-29 13:57:59 +00002099 USE_HUSH_LOOPS(loop_top = NULL;)
Denis Vlasenko87a86552008-07-29 19:43:10 +00002100 USE_HUSH_LOOPS(G.depth_of_loop = 0;)
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002101 rcode = 0;
2102 goto ret;
2103 }
2104 /* ctrl-Z handler will store pid etc in pi */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002105 G.toplevel_list = pi;
2106 G.ctrl_z_flag = 0;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002107#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko87a86552008-07-29 19:43:10 +00002108 G.nofork_save.saved = 0; /* in case we will run a nofork later */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002109#endif
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +00002110 signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002111 signal(SIGINT, handler_ctrl_c);
2112 }
Denis Vlasenko05743d72008-02-10 12:10:08 +00002113#endif /* JOB */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002114
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002115 /* Go through list of pipes, (maybe) executing them. */
2116 for (; pi; pi = USE_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) {
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002117 IF_HAS_KEYWORDS(rword = pi->res_word;)
2118 IF_HAS_NO_KEYWORDS(rword = RES_NONE;)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002119 debug_printf_exec(": rword=%d cond_code=%d skip_more=%d\n",
2120 rword, cond_code, skip_more_for_this_rword);
Denis Vlasenko06810332007-05-21 23:30:54 +00002121#if ENABLE_HUSH_LOOPS
Denis Vlasenko4554b722008-07-29 13:36:09 +00002122 if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR)
Denis Vlasenko87a86552008-07-29 19:43:10 +00002123 && loop_top == NULL /* avoid bumping G.depth_of_loop twice */
Denis Vlasenko4554b722008-07-29 13:36:09 +00002124 ) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002125 /* start of a loop: remember where loop starts */
2126 loop_top = pi;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002127 G.depth_of_loop++;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002128 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002129#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002130 if (rword == skip_more_for_this_rword && flag_skip) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002131 if (pi->followup == PIPE_SEQ)
2132 flag_skip = 0;
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002133 /* it is "<false> && CMD" or "<true> || CMD"
2134 * and we should not execute CMD */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002135 continue;
2136 }
2137 flag_skip = 1;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002138 skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko06810332007-05-21 23:30:54 +00002139#if ENABLE_HUSH_IF
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002140 if (cond_code) {
2141 if (rword == RES_THEN) {
2142 /* "if <false> THEN cmd": skip cmd */
2143 continue;
2144 }
2145 } else {
2146 if (rword == RES_ELSE || rword == RES_ELIF) {
2147 /* "if <true> then ... ELSE/ELIF cmd":
2148 * skip cmd and all following ones */
2149 break;
2150 }
2151 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002152#endif
2153#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002154 if (rword == RES_FOR) { /* && pi->num_progs - always == 1 */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002155 if (!for_lcur) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002156 /* first loop through for */
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002157
2158 static const char encoded_dollar_at[] ALIGN1 = {
2159 SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0'
2160 }; /* encoded representation of "$@" */
2161 static const char *const encoded_dollar_at_argv[] = {
2162 encoded_dollar_at, NULL
2163 }; /* argv list with one element: "$@" */
2164 char **vals;
2165
2166 vals = (char**)encoded_dollar_at_argv;
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002167 if (pi->next->res_word == RES_IN) {
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002168 /* if no variable values after "in" we skip "for" */
Denis Vlasenko2b576b82008-08-04 00:46:07 +00002169 if (!pi->next->progs[0].argv)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002170 break;
Denis Vlasenko2b576b82008-08-04 00:46:07 +00002171 vals = pi->next->progs[0].argv;
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002172 } /* else: "for var; do..." -> assume "$@" list */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002173 /* create list of variable values */
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002174 debug_print_strings("for_list made from", vals);
2175 for_list = expand_strvec_to_strvec(vals);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002176 for_lcur = for_list;
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002177 debug_print_strings("for_list", for_list);
Denis Vlasenko2b576b82008-08-04 00:46:07 +00002178 for_varname = pi->progs[0].argv[0];
2179 pi->progs[0].argv[0] = NULL;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002180 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00002181 free(pi->progs[0].argv[0]);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002182 if (!*for_lcur) {
Denis Vlasenko12acec52008-07-28 15:15:59 +00002183 /* "for" loop is over, clean up */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002184 free(for_list);
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002185 for_list = NULL;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002186 for_lcur = NULL;
Denis Vlasenko2b576b82008-08-04 00:46:07 +00002187 pi->progs[0].argv[0] = for_varname;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002188 break;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002189 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002190 /* insert next value from for_lcur */
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002191//TODO: does it need escaping?
Denis Vlasenko2b576b82008-08-04 00:46:07 +00002192 pi->progs[0].argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++);
2193 pi->progs[0].assignment_cnt = 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002194 }
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002195 if (rword == RES_IN) /* "for v IN list;..." - "in" has no cmds anyway */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002196 continue;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002197 if (rword == RES_DONE) {
2198 continue; /* "done" has no cmds too */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002199 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002200#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002201#if ENABLE_HUSH_CASE
2202 if (rword == RES_CASE) {
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002203 case_word = expand_strvec_to_string(pi->progs->argv);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002204 continue;
2205 }
2206 if (rword == RES_MATCH) {
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00002207 char **argv;
2208
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002209 if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */
2210 break;
2211 /* all prev words didn't match, does this one match? */
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00002212 argv = pi->progs->argv;
2213 while (*argv) {
2214 char *pattern = expand_string_to_string(*argv);
2215 /* TODO: which FNM_xxx flags to use? */
2216 cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0);
2217 free(pattern);
2218 if (cond_code == 0) { /* match! we will execute this branch */
2219 free(case_word); /* make future "word)" stop */
2220 case_word = NULL;
2221 break;
2222 }
2223 argv++;
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002224 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002225 continue;
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002226 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002227 if (rword == RES_CASEI) { /* inside of a case branch */
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002228 if (cond_code != 0)
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002229 continue; /* not matched yet, skip this pipe */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002230 }
2231#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002232 if (pi->num_progs == 0)
2233 continue;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002234
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002235 /* After analyzing all keywords and conditions, we decided
2236 * to execute this pipe. NB: has to do checkjobs(NULL)
2237 * after run_pipe() to collect any background children,
2238 * even if list execution is to be stopped. */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002239 debug_printf_exec(": run_pipe with %d members\n", pi->num_progs);
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002240 {
2241 int r;
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002242#if ENABLE_HUSH_LOOPS
Denis Vlasenko87a86552008-07-29 19:43:10 +00002243 G.flag_break_continue = 0;
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002244#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002245 rcode = r = run_pipe(pi); /* NB: rcode is a smallint */
2246 if (r != -1) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002247 /* we only ran a builtin: rcode is already known
2248 * and we don't need to wait for anything. */
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002249#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002250 /* was it "break" or "continue"? */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002251 if (G.flag_break_continue) {
2252 smallint fbc = G.flag_break_continue;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002253 /* we might fall into outer *loop*,
2254 * don't want to break it too */
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002255 if (loop_top) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002256 G.depth_break_continue--;
2257 if (G.depth_break_continue == 0)
2258 G.flag_break_continue = 0;
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002259 /* else: e.g. "continue 2" should *break* once, *then* continue */
2260 } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002261 if (G.depth_break_continue != 0 || fbc == BC_BREAK)
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002262 goto check_jobs_and_break;
2263 /* "continue": simulate end of loop */
2264 rword = RES_DONE;
2265 continue;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002266 }
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002267#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002268 } else if (pi->followup == PIPE_BG) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002269 /* what does bash do with attempts to background builtins? */
2270 /* even bash 3.2 doesn't do that well with nested bg:
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002271 * try "{ { sleep 10; echo DEEP; } & echo HERE; } &".
2272 * I'm NOT treating inner &'s as jobs */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002273#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00002274 if (G.run_list_level == 1)
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002275 insert_bg_job(pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002276#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002277 rcode = 0; /* EXIT_SUCCESS */
2278 } else {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002279#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00002280 if (G.run_list_level == 1 && G.interactive_fd) {
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002281 /* waits for completion, then fg's main shell */
2282 rcode = checkjobs_and_fg_shell(pi);
2283 debug_printf_exec(": checkjobs_and_fg_shell returned %d\n", rcode);
2284 } else
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002285#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002286 { /* this one just waits for completion */
2287 rcode = checkjobs(pi);
2288 debug_printf_exec(": checkjobs returned %d\n", rcode);
2289 }
Eric Andersen25f27032001-04-26 23:22:31 +00002290 }
2291 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002292 debug_printf_exec(": setting last_return_code=%d\n", rcode);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002293 G.last_return_code = rcode;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002294
2295 /* Analyze how result affects subsequent commands */
Denis Vlasenko06810332007-05-21 23:30:54 +00002296#if ENABLE_HUSH_IF
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002297 if (rword == RES_IF || rword == RES_ELIF)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002298 cond_code = rcode;
Denis Vlasenko06810332007-05-21 23:30:54 +00002299#endif
2300#if ENABLE_HUSH_LOOPS
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002301 if (rword == RES_WHILE) {
Denis Vlasenko918a34b2008-07-28 23:17:31 +00002302 if (rcode) {
2303 rcode = 0; /* "while false; do...done" - exitcode 0 */
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002304 goto check_jobs_and_break;
Denis Vlasenko918a34b2008-07-28 23:17:31 +00002305 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002306 }
2307 if (rword == RES_UNTIL) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002308 if (!rcode) {
2309 check_jobs_and_break:
2310 checkjobs(NULL);
2311 break;
2312 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002313 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002314#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002315 if ((rcode == 0 && pi->followup == PIPE_OR)
2316 || (rcode != 0 && pi->followup == PIPE_AND)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002317 ) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002318 skip_more_for_this_rword = rword;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002319 }
Eric Andersen028b65b2001-06-28 01:10:11 +00002320 checkjobs(NULL);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002321 } /* for (pi) */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002322
2323#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00002324 if (G.ctrl_z_flag) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002325 /* ctrl-Z forked somewhere in the past, we are the child,
2326 * and now we completed running the list. Exit. */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002327//TODO: _exit?
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002328 exit(rcode);
2329 }
2330 ret:
Denis Vlasenko87a86552008-07-29 19:43:10 +00002331 if (!--G.run_list_level && G.interactive_fd) {
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00002332 signal(SIGTSTP, SIG_IGN);
2333 signal(SIGINT, SIG_IGN);
2334 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002335#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00002336 debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode);
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002337#if ENABLE_HUSH_LOOPS
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002338 if (loop_top)
Denis Vlasenko87a86552008-07-29 19:43:10 +00002339 G.depth_of_loop--;
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002340 free(for_list);
2341#endif
2342#if ENABLE_HUSH_CASE
2343 free(case_word);
2344#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002345 return rcode;
2346}
2347
Eric Andersen25f27032001-04-26 23:22:31 +00002348/* return code is the exit status of the pipe */
Eric Andersenbf7df042001-05-23 22:18:35 +00002349static int free_pipe(struct pipe *pi, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002350{
2351 char **p;
2352 struct child_prog *child;
2353 struct redir_struct *r, *rnext;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002354 int a, i, ret_code = 0;
Eric Andersen52a97ca2001-06-22 06:49:26 +00002355
2356 if (pi->stopped_progs > 0)
2357 return ret_code;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002358 debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid());
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002359 for (i = 0; i < pi->num_progs; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002360 child = &pi->progs[i];
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002361 debug_printf_clean("%s command %d:\n", indenter(indent), i);
Eric Andersen25f27032001-04-26 23:22:31 +00002362 if (child->argv) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002363 for (a = 0, p = child->argv; *p; a++, p++) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002364 debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p);
Eric Andersen25f27032001-04-26 23:22:31 +00002365 }
Denis Vlasenkof962a032007-11-23 12:50:54 +00002366 free_strings(child->argv);
2367 child->argv = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002368 } else if (child->group) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002369 debug_printf_clean("%s begin group (subshell:%d)\n", indenter(indent), child->subshell);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002370 ret_code = free_pipe_list(child->group, indent+3);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002371 debug_printf_clean("%s end group\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002372 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002373 debug_printf_clean("%s (nil)\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002374 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002375 for (r = child->redirects; r; r = rnext) {
Denis Vlasenko211b59b2008-06-23 16:28:53 +00002376 debug_printf_clean("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->rd_type].descrip);
Eric Andersen25f27032001-04-26 23:22:31 +00002377 if (r->dup == -1) {
Eric Andersen817e73c2001-06-06 17:56:09 +00002378 /* guard against the case >$FOO, where foo is unset or blank */
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00002379 if (r->rd_filename) {
2380 debug_printf_clean(" %s\n", r->rd_filename);
2381 free(r->rd_filename);
2382 r->rd_filename = NULL;
Eric Andersen817e73c2001-06-06 17:56:09 +00002383 }
Eric Andersen25f27032001-04-26 23:22:31 +00002384 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002385 debug_printf_clean("&%d\n", r->dup);
Eric Andersen25f27032001-04-26 23:22:31 +00002386 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002387 rnext = r->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002388 free(r);
2389 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002390 child->redirects = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002391 }
2392 free(pi->progs); /* children are an array, they get freed all at once */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002393 pi->progs = NULL;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002394#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002395 free(pi->cmdtext);
2396 pi->cmdtext = NULL;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002397#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002398 return ret_code;
2399}
2400
Eric Andersenbf7df042001-05-23 22:18:35 +00002401static int free_pipe_list(struct pipe *head, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002402{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002403 int rcode = 0; /* if list has no members */
Eric Andersen25f27032001-04-26 23:22:31 +00002404 struct pipe *pi, *next;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002405
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002406 for (pi = head; pi; pi = next) {
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002407#if HAS_KEYWORDS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002408 debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002409#endif
Eric Andersenbf7df042001-05-23 22:18:35 +00002410 rcode = free_pipe(pi, indent);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002411 debug_printf_clean("%s pipe followup code %d\n", indenter(indent), pi->followup);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002412 next = pi->next;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002413 /*pi->next = NULL;*/
Eric Andersen25f27032001-04-26 23:22:31 +00002414 free(pi);
2415 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002416 return rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00002417}
2418
2419/* Select which version we will use */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002420static int run_and_free_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002421{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002422 int rcode = 0;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002423 debug_printf_exec("run_and_free_list entered\n");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002424 if (!G.fake_mode) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00002425 debug_printf_exec(": run_list with %d members\n", pi->num_progs);
2426 rcode = run_list(pi);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002427 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002428 /* free_pipe_list has the side effect of clearing memory.
Denis Vlasenko05743d72008-02-10 12:10:08 +00002429 * In the long run that function can be merged with run_list,
Eric Andersen25f27032001-04-26 23:22:31 +00002430 * but doing that now would hobble the debugging effort. */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002431 free_pipe_list(pi, /* indent: */ 0);
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002432 debug_printf_exec("run_and_free_list return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002433 return rcode;
2434}
2435
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002436
Denis Vlasenko170435c2007-05-23 13:01:10 +00002437/* expand_strvec_to_strvec() takes a list of strings, expands
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002438 * all variable references within and returns a pointer to
2439 * a list of expanded strings, possibly with larger number
2440 * of strings. (Think VAR="a b"; echo $VAR).
2441 * This new list is allocated as a single malloc block.
2442 * NULL-terminated list of char* pointers is at the beginning of it,
2443 * followed by strings themself.
2444 * Caller can deallocate entire list by single free(list). */
2445
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002446/* Store given string, finalizing the word and starting new one whenever
Denis Vlasenko87a86552008-07-29 19:43:10 +00002447 * we encounter IFS char(s). This is used for expanding variable values.
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002448 * End-of-string does NOT finalize word: think about 'echo -$VAR-' */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002449static int expand_on_ifs(o_string *output, int n, const char *str)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002450{
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002451 while (1) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002452 int word_len = strcspn(str, G.ifs);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002453 if (word_len) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00002454 if (output->o_quote || !output->o_glob)
2455 o_addQstr(output, str, word_len);
2456 else /* protect backslashes against globbing up :) */
2457 o_addstr_duplicate_backslash(output, str, word_len);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002458 str += word_len;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002459 }
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002460 if (!*str) /* EOL - do not finalize word */
2461 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002462 o_addchr(output, '\0');
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002463 debug_print_list("expand_on_ifs", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002464 n = o_save_ptr(output, n);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002465 str += strspn(str, G.ifs); /* skip ifs chars */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002466 }
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002467 debug_print_list("expand_on_ifs[1]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002468 return n;
2469}
2470
2471/* Expand all variable references in given string, adding words to list[]
2472 * at n, n+1,... positions. Return updated n (so that list[n] is next one
2473 * to be filled). This routine is extremely tricky: has to deal with
2474 * variables/parameters with whitespace, $* and $@, and constructs like
2475 * 'echo -$*-'. If you play here, you must run testsuite afterwards! */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002476static int expand_vars_to_list(o_string *output, int n, char *arg, char or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002477{
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002478 /* or_mask is either 0 (normal case) or 0x80
Denis Vlasenko55789c62008-06-18 16:30:42 +00002479 * (expansion of right-hand side of assignment == 1-element expand.
2480 * It will also do no globbing, and thus we must not backslash-quote!) */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002481
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002482 char first_ch, ored_ch;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002483 int i;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002484 const char *val;
2485 char *p;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002486
2487 ored_ch = 0;
2488
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002489 debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002490 debug_print_list("expand_vars_to_list", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002491 n = o_save_ptr(output, n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002492 debug_print_list("expand_vars_to_list[0]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002493
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002494 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) {
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002495#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002496 o_string subst_result = NULL_O_STRING;
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002497#endif
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002498
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002499 o_addstr(output, arg, p - arg);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002500 debug_print_list("expand_vars_to_list[1]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002501 arg = ++p;
2502 p = strchr(p, SPECIAL_VAR_SYMBOL);
2503
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002504 first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00002505 /* "$@" is special. Even if quoted, it can still
2506 * expand to nothing (not even an empty string) */
2507 if ((first_ch & 0x7f) != '@')
2508 ored_ch |= first_ch;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002509 val = NULL;
2510 switch (first_ch & 0x7f) {
2511 /* Highest bit in first_ch indicates that var is double-quoted */
2512 case '$': /* pid */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002513 val = utoa(G.root_pid);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002514 break;
2515 case '!': /* bg pid */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002516 val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)"";
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002517 break;
2518 case '?': /* exitcode */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002519 val = utoa(G.last_return_code);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002520 break;
2521 case '#': /* argc */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002522 val = utoa(G.global_argc ? G.global_argc-1 : 0);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002523 break;
2524 case '*':
2525 case '@':
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002526 i = 1;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002527 if (!G.global_argv[i])
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002528 break;
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00002529 ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002530 if (!(first_ch & 0x80)) { /* unquoted $* or $@ */
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002531 smallint sv = output->o_quote;
2532 /* unquoted var's contents should be globbed, so don't quote */
2533 output->o_quote = 0;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002534 while (G.global_argv[i]) {
2535 n = expand_on_ifs(output, n, G.global_argv[i]);
2536 debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1);
2537 if (G.global_argv[i++][0] && G.global_argv[i]) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002538 /* this argv[] is not empty and not last:
2539 * put terminating NUL, start new word */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002540 o_addchr(output, '\0');
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002541 debug_print_list("expand_vars_to_list[2]", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002542 n = o_save_ptr(output, n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002543 debug_print_list("expand_vars_to_list[3]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002544 }
2545 }
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002546 output->o_quote = sv;
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002547 } else
2548 /* If or_mask is nonzero, we handle assignment 'a=....$@.....'
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00002549 * and in this case should treat it like '$*' - see 'else...' below */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002550 if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002551 while (1) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002552 o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i]));
2553 if (++i >= G.global_argc)
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002554 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002555 o_addchr(output, '\0');
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002556 debug_print_list("expand_vars_to_list[4]", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002557 n = o_save_ptr(output, n);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002558 }
2559 } else { /* quoted $*: add as one word */
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002560 while (1) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002561 o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i]));
2562 if (!G.global_argv[++i])
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002563 break;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002564 if (G.ifs[0])
2565 o_addchr(output, G.ifs[0]);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002566 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002567 }
2568 break;
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00002569 case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */
2570 /* "Empty variable", used to make "" etc to not disappear */
2571 arg++;
2572 ored_ch = 0x80;
2573 break;
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002574#if ENABLE_HUSH_TICK
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00002575 case '`': { /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002576 struct in_str input;
2577 *p = '\0';
2578 arg++;
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002579//TODO: can we just stuff it into "output" directly?
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002580 debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002581 setup_string_in_str(&input, arg);
2582 process_command_subs(&subst_result, &input, NULL);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002583 debug_printf_subst("SUBST RES '%s'\n", subst_result.data);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002584 val = subst_result.data;
2585 goto store_val;
2586 }
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002587#endif
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00002588 default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002589 *p = '\0';
2590 arg[0] = first_ch & 0x7f;
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002591 if (isdigit(arg[0])) {
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002592 i = xatoi_u(arg);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002593 if (i < G.global_argc)
2594 val = G.global_argv[i];
Denis Vlasenko55789c62008-06-18 16:30:42 +00002595 /* else val remains NULL: $N with too big N */
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002596 } else
2597 val = lookup_param(arg);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002598 arg[0] = first_ch;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002599#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002600 store_val:
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002601#endif
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002602 *p = SPECIAL_VAR_SYMBOL;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002603 if (!(first_ch & 0x80)) { /* unquoted $VAR */
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002604 debug_printf_expand("unquoted '%s', output->o_quote:%d\n", val, output->o_quote);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002605 if (val) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00002606 /* unquoted var's contents should be globbed, so don't quote */
2607 smallint sv = output->o_quote;
2608 output->o_quote = 0;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002609 n = expand_on_ifs(output, n, val);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002610 val = NULL;
Denis Vlasenko55789c62008-06-18 16:30:42 +00002611 output->o_quote = sv;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002612 }
Denis Vlasenko55789c62008-06-18 16:30:42 +00002613 } else { /* quoted $VAR, val will be appended below */
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002614 debug_printf_expand("quoted '%s', output->o_quote:%d\n", val, output->o_quote);
Denis Vlasenko55789c62008-06-18 16:30:42 +00002615 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002616 }
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002617 if (val) {
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002618 o_addQstr(output, val, strlen(val));
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002619 }
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002620
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002621#if ENABLE_HUSH_TICK
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002622 o_free(&subst_result);
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002623#endif
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002624 arg = ++p;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002625 } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */
2626
Denis Vlasenko2e76c3f2008-06-10 18:27:50 +00002627 if (arg[0]) {
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002628 debug_print_list("expand_vars_to_list[a]", output, n);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002629 /* this part is literal, and it was already pre-quoted
2630 * if needed (much earlier), do not use o_addQstr here! */
2631 o_addstr(output, arg, strlen(arg) + 1);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002632 debug_print_list("expand_vars_to_list[b]", output, n);
Denis Vlasenko2e76c3f2008-06-10 18:27:50 +00002633 } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */
2634 && !(ored_ch & 0x80) /* and all vars were not quoted. */
2635 ) {
2636 n--;
2637 /* allow to reuse list[n] later without re-growth */
2638 output->has_empty_slot = 1;
2639 } else {
2640 o_addchr(output, '\0');
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002641 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002642 return n;
2643}
2644
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002645static char **expand_variables(char **argv, int or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002646{
2647 int n;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002648 char **list;
2649 char **v;
2650 o_string output = NULL_O_STRING;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002651
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002652 if (or_mask & 0x100) {
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002653 output.o_quote = 1; /* protect against globbing for "$var" */
2654 /* (unquoted $var will temporarily switch it off) */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002655 output.o_glob = 1;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002656 }
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002657
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002658 n = 0;
2659 v = argv;
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002660 while (*v) {
2661 n = expand_vars_to_list(&output, n, *v, (char)or_mask);
2662 v++;
2663 }
2664 debug_print_list("expand_variables", &output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002665
Denis Vlasenko82dfec32008-06-16 12:47:11 +00002666 /* output.data (malloced in one block) gets returned in "list" */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002667 list = o_finalize_list(&output, n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002668 debug_print_strings("expand_variables[1]", list);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002669 return list;
2670}
2671
Denis Vlasenko170435c2007-05-23 13:01:10 +00002672static char **expand_strvec_to_strvec(char **argv)
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002673{
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002674 return expand_variables(argv, 0x100);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002675}
2676
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002677/* Used for expansion of right hand of assignments */
Denis Vlasenko82dfec32008-06-16 12:47:11 +00002678/* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs
2679 * "v=/bin/c*" */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002680static char *expand_string_to_string(const char *str)
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002681{
2682 char *argv[2], **list;
2683
2684 argv[0] = (char*)str;
2685 argv[1] = NULL;
2686 list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002687 if (HUSH_DEBUG)
Denis Vlasenko170435c2007-05-23 13:01:10 +00002688 if (!list[0] || list[1])
2689 bb_error_msg_and_die("BUG in varexp2");
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002690 /* actually, just move string 2*sizeof(char*) bytes back */
2691 strcpy((char*)list, list[0]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00002692 debug_printf_expand("string_to_string='%s'\n", (char*)list);
2693 return (char*)list;
2694}
2695
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002696/* Used for "eval" builtin */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002697static char* expand_strvec_to_string(char **argv)
2698{
2699 char **list;
2700
2701 list = expand_variables(argv, 0x80);
2702 /* Convert all NULs to spaces */
2703 if (list[0]) {
2704 int n = 1;
2705 while (list[n]) {
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002706 if (HUSH_DEBUG)
Denis Vlasenko170435c2007-05-23 13:01:10 +00002707 if (list[n-1] + strlen(list[n-1]) + 1 != list[n])
2708 bb_error_msg_and_die("BUG in varexp3");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002709 list[n][-1] = ' '; /* TODO: or to G.ifs[0]? */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002710 n++;
2711 }
2712 }
2713 strcpy((char*)list, list[0]);
2714 debug_printf_expand("strvec_to_string='%s'\n", (char*)list);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002715 return (char*)list;
2716}
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002717
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002718
2719/* Used to get/check local shell variables */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002720static struct variable *get_local_var(const char *name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002721{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002722 struct variable *cur;
2723 int len;
Eric Andersenf72f5622001-05-15 23:21:41 +00002724
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002725 if (!name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002726 return NULL;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002727 len = strlen(name);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002728 for (cur = G.top_var; cur; cur = cur->next) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002729 if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=')
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002730 return cur;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002731 }
Eric Andersenf72f5622001-05-15 23:21:41 +00002732 return NULL;
2733}
2734
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002735/* str holds "NAME=VAL" and is expected to be malloced.
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002736 * We take ownership of it. */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002737static int set_local_var(char *str, int flg_export)
Eric Andersen78a7c992001-05-15 16:30:25 +00002738{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002739 struct variable *cur;
2740 char *value;
2741 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002742
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002743 value = strchr(str, '=');
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002744 if (!value) { /* not expected to ever happen? */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002745 free(str);
Eric Andersen99785762001-05-22 21:37:48 +00002746 return -1;
2747 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002748
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002749 name_len = value - str + 1; /* including '=' */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002750 cur = G.top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002751 while (1) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002752 if (strncmp(cur->varstr, str, name_len) != 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002753 if (!cur->next) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002754 /* Bail out. Note that now cur points
2755 * to last var in linked list */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002756 break;
Eric Andersen20a69a72001-05-15 17:24:44 +00002757 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002758 cur = cur->next;
2759 continue;
Eric Andersen20a69a72001-05-15 17:24:44 +00002760 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002761 /* We found an existing var with this name */
2762 *value = '\0';
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002763 if (cur->flg_read_only) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002764 bb_error_msg("%s: readonly variable", str);
2765 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002766 return -1;
2767 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002768 unsetenv(str); /* just in case */
2769 *value = '=';
2770 if (strcmp(cur->varstr, str) == 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002771 free_and_exp:
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002772 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002773 goto exp;
2774 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002775 if (cur->max_len >= strlen(str)) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002776 /* This one is from startup env, reuse space */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002777 strcpy(cur->varstr, str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002778 goto free_and_exp;
2779 }
2780 /* max_len == 0 signifies "malloced" var, which we can
2781 * (and has to) free */
2782 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002783 free(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002784 cur->max_len = 0;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002785 goto set_str_and_exp;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002786 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002787
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002788 /* Not found - create next variable struct */
2789 cur->next = xzalloc(sizeof(*cur));
2790 cur = cur->next;
2791
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002792 set_str_and_exp:
2793 cur->varstr = str;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002794 exp:
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002795 if (flg_export)
2796 cur->flg_export = 1;
2797 if (cur->flg_export)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002798 return putenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002799 return 0;
Eric Andersen20a69a72001-05-15 17:24:44 +00002800}
2801
Eric Andersenf72f5622001-05-15 23:21:41 +00002802static void unset_local_var(const char *name)
Eric Andersen20a69a72001-05-15 17:24:44 +00002803{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002804 struct variable *cur;
2805 struct variable *prev = prev; /* for gcc */
2806 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002807
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002808 if (!name)
2809 return;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002810 name_len = strlen(name);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002811 cur = G.top_var;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002812 while (cur) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002813 if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002814 if (cur->flg_read_only) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002815 bb_error_msg("%s: readonly variable", name);
Eric Andersen94ac2442001-05-22 19:05:18 +00002816 return;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002817 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002818 /* prev is ok to use here because 1st variable, HUSH_VERSION,
2819 * is ro, and we cannot reach this code on the 1st pass */
2820 prev->next = cur->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002821 unsetenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002822 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002823 free(cur->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002824 free(cur);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002825 return;
Eric Andersenf72f5622001-05-15 23:21:41 +00002826 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002827 prev = cur;
2828 cur = cur->next;
Eric Andersen20a69a72001-05-15 17:24:44 +00002829 }
Eric Andersen78a7c992001-05-15 16:30:25 +00002830}
2831
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002832/* The src parameter allows us to peek forward to a possible &n syntax
Eric Andersen25f27032001-04-26 23:22:31 +00002833 * for file descriptor duplication, e.g., "2>&1".
2834 * Return code is 0 normally, 1 if a syntax error is detected in src.
2835 * Resource errors (in xmalloc) cause the process to exit */
2836static int setup_redirect(struct p_context *ctx, int fd, redir_type style,
2837 struct in_str *input)
2838{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002839 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00002840 struct redir_struct *redir = child->redirects;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002841 struct redir_struct *last_redir = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002842
2843 /* Create a new redir_struct and drop it onto the end of the linked list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002844 while (redir) {
2845 last_redir = redir;
2846 redir = redir->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002847 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00002848 redir = xzalloc(sizeof(struct redir_struct));
2849 /* redir->next = NULL; */
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00002850 /* redir->rd_filename = NULL; */
Eric Andersen25f27032001-04-26 23:22:31 +00002851 if (last_redir) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002852 last_redir->next = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002853 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002854 child->redirects = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002855 }
2856
Denis Vlasenko55789c62008-06-18 16:30:42 +00002857 redir->rd_type = style;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002858 redir->fd = (fd == -1) ? redir_table[style].default_fd : fd;
Eric Andersen25f27032001-04-26 23:22:31 +00002859
2860 debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
2861
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002862 /* Check for a '2>&1' type redirect */
Eric Andersen25f27032001-04-26 23:22:31 +00002863 redir->dup = redirect_dup_num(input);
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002864 if (redir->dup == -2)
2865 return 1; /* syntax error */
Eric Andersen25f27032001-04-26 23:22:31 +00002866 if (redir->dup != -1) {
2867 /* Erik had a check here that the file descriptor in question
Eric Andersen83a2ae22001-05-07 17:59:25 +00002868 * is legit; I postpone that to "run time"
2869 * A "-" representation of "close me" shows up as a -3 here */
Eric Andersen25f27032001-04-26 23:22:31 +00002870 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
2871 } else {
2872 /* We do _not_ try to open the file that src points to,
2873 * since we need to return and let src be expanded first.
2874 * Set ctx->pending_redirect, so we know what to do at the
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002875 * end of the next parsed word. */
Eric Andersen25f27032001-04-26 23:22:31 +00002876 ctx->pending_redirect = redir;
2877 }
2878 return 0;
2879}
2880
Denis Vlasenkoac678ec2007-04-16 22:32:04 +00002881static struct pipe *new_pipe(void)
2882{
Eric Andersen25f27032001-04-26 23:22:31 +00002883 struct pipe *pi;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002884 pi = xzalloc(sizeof(struct pipe));
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002885 /*pi->followup = 0; - deliberately invalid value */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002886 /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */
Eric Andersen25f27032001-04-26 23:22:31 +00002887 return pi;
2888}
2889
2890static void initialize_context(struct p_context *ctx)
2891{
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002892 memset(ctx, 0, sizeof(*ctx));
Denis Vlasenko1a735862007-05-23 00:32:25 +00002893 ctx->pipe = ctx->list_head = new_pipe();
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002894 /* Create the memory for child, roughly:
2895 * ctx->pipe->progs = new struct child_prog;
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002896 * ctx->child = &ctx->pipe->progs[0];
2897 */
2898 done_command(ctx);
Eric Andersen25f27032001-04-26 23:22:31 +00002899}
2900
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002901/* If a reserved word is found and processed, parse context is modified
2902 * and 1 is returned.
2903 * Handles if, then, elif, else, fi, for, while, until, do, done.
Eric Andersen25f27032001-04-26 23:22:31 +00002904 * case, function, and select are obnoxious, save those for later.
2905 */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002906#if HAS_KEYWORDS
Denis Vlasenko2b576b82008-08-04 00:46:07 +00002907static int reserved_word(o_string *word, struct p_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00002908{
2909 struct reserved_combo {
Denis Vlasenko2b576b82008-08-04 00:46:07 +00002910 char literal[6];
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002911 unsigned char res;
Denis Vlasenko2b576b82008-08-04 00:46:07 +00002912 unsigned char assignment_flag;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002913 int flag;
Eric Andersen25f27032001-04-26 23:22:31 +00002914 };
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002915 enum {
2916 FLAG_END = (1 << RES_NONE ),
2917#if ENABLE_HUSH_IF
2918 FLAG_IF = (1 << RES_IF ),
2919 FLAG_THEN = (1 << RES_THEN ),
2920 FLAG_ELIF = (1 << RES_ELIF ),
2921 FLAG_ELSE = (1 << RES_ELSE ),
2922 FLAG_FI = (1 << RES_FI ),
2923#endif
2924#if ENABLE_HUSH_LOOPS
2925 FLAG_FOR = (1 << RES_FOR ),
2926 FLAG_WHILE = (1 << RES_WHILE),
2927 FLAG_UNTIL = (1 << RES_UNTIL),
2928 FLAG_DO = (1 << RES_DO ),
2929 FLAG_DONE = (1 << RES_DONE ),
2930 FLAG_IN = (1 << RES_IN ),
2931#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002932#if ENABLE_HUSH_CASE
2933 FLAG_MATCH = (1 << RES_MATCH),
2934 FLAG_ESAC = (1 << RES_ESAC ),
2935#endif
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002936 FLAG_START = (1 << RES_XXXX ),
2937 };
Eric Andersen25f27032001-04-26 23:22:31 +00002938 /* Mostly a list of accepted follow-up reserved words.
2939 * FLAG_END means we are done with the sequence, and are ready
2940 * to turn the compound list into a command.
2941 * FLAG_START means the word must start a new compound list.
2942 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002943 static const struct reserved_combo reserved_list[] = {
Denis Vlasenko06810332007-05-21 23:30:54 +00002944#if ENABLE_HUSH_IF
Denis Vlasenko2b576b82008-08-04 00:46:07 +00002945 { "!", RES_NONE, NOT_ASSIGNMENT , 0 },
2946 { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START },
2947 { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
2948 { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN },
2949 { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI },
2950 { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END },
Denis Vlasenko06810332007-05-21 23:30:54 +00002951#endif
2952#if ENABLE_HUSH_LOOPS
Denis Vlasenko2b576b82008-08-04 00:46:07 +00002953 { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START },
2954 { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START },
2955 { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START },
2956 { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO },
2957 { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE },
2958 { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END },
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002959#endif
2960#if ENABLE_HUSH_CASE
Denis Vlasenko2b576b82008-08-04 00:46:07 +00002961 { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START },
2962 { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END },
Denis Vlasenko06810332007-05-21 23:30:54 +00002963#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002964 };
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002965#if ENABLE_HUSH_CASE
2966 static const struct reserved_combo reserved_match = {
Denis Vlasenko2b576b82008-08-04 00:46:07 +00002967 "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002968 };
2969#endif
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002970 const struct reserved_combo *r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002971
Denis Vlasenko80b8b392007-06-25 10:55:35 +00002972 for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) {
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002973 if (strcmp(word->data, r->literal) != 0)
Denis Vlasenko1a735862007-05-23 00:32:25 +00002974 continue;
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002975 debug_printf("found reserved word %s, res %d\n", r->literal, r->res);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002976#if ENABLE_HUSH_CASE
2977 if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE)
2978 /* "case word IN ..." - IN part starts first match part */
2979 r = &reserved_match;
2980 else
2981#endif
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002982 if (r->flag == 0) { /* '!' */
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00002983 if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002984 syntax(NULL);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002985 IF_HAS_KEYWORDS(ctx->ctx_res_w = RES_SNTX;)
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002986 }
2987 ctx->ctx_inverted = 1;
2988 return 1;
2989 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00002990 if (r->flag & FLAG_START) {
2991 struct p_context *new;
2992 debug_printf("push stack\n");
Denis Vlasenko1a735862007-05-23 00:32:25 +00002993 new = xmalloc(sizeof(*new));
2994 *new = *ctx; /* physical copy */
2995 initialize_context(ctx);
2996 ctx->stack = new;
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00002997 } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00002998 syntax(NULL);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002999 ctx->ctx_res_w = RES_SNTX;
Eric Andersen25f27032001-04-26 23:22:31 +00003000 return 1;
3001 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003002 ctx->ctx_res_w = r->res;
Denis Vlasenko1a735862007-05-23 00:32:25 +00003003 ctx->old_flag = r->flag;
3004 if (ctx->old_flag & FLAG_END) {
3005 struct p_context *old;
3006 debug_printf("pop stack\n");
3007 done_pipe(ctx, PIPE_SEQ);
3008 old = ctx->stack;
3009 old->child->group = ctx->list_head;
3010 old->child->subshell = 0;
3011 *ctx = *old; /* physical copy */
3012 free(old);
3013 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003014 word->o_assignment = r->assignment_flag;
Denis Vlasenko1a735862007-05-23 00:32:25 +00003015 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003016 }
3017 return 0;
3018}
Denis Vlasenko06810332007-05-21 23:30:54 +00003019#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003020
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003021/* Word is complete, look at it and update parsing context.
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003022 * Normal return is 0. Syntax errors return 1. */
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003023static int done_word(o_string *word, struct p_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00003024{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003025 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00003026
Denis Vlasenko395ae452008-07-14 06:29:38 +00003027 debug_printf_parse("done_word entered: '%s' %p\n", word->data, child);
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003028 if (word->length == 0 && word->nonnull == 0) {
3029 debug_printf_parse("done_word return 0: true null, ignored\n");
3030 return 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003031 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003032 /* If this word wasn't an assignment, next ones definitely
3033 * can't be assignments. Even if they look like ones. */
3034 if (word->o_assignment != DEFINITELY_ASSIGNMENT
3035 && word->o_assignment != WORD_IS_KEYWORD
3036 ) {
3037 word->o_assignment = NOT_ASSIGNMENT;
3038 } else {
3039 if (word->o_assignment == DEFINITELY_ASSIGNMENT)
3040 child->assignment_cnt++;
3041 word->o_assignment = MAYBE_ASSIGNMENT;
3042 }
3043
Eric Andersen25f27032001-04-26 23:22:31 +00003044 if (ctx->pending_redirect) {
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003045 /* We do not glob in e.g. >*.tmp case. bash seems to glob here
3046 * only if run as "bash", not "sh" */
3047 ctx->pending_redirect->rd_filename = xstrdup(word->data);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003048 word->o_assignment = NOT_ASSIGNMENT;
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003049 debug_printf("word stored in rd_filename: '%s'\n", word->data);
Eric Andersen25f27032001-04-26 23:22:31 +00003050 } else {
Denis Vlasenko395ae452008-07-14 06:29:38 +00003051 /* "{ echo foo; } echo bar" - bad */
3052 /* NB: bash allows e.g. "if true; then { echo foo; } fi". TODO? */
3053 if (child->group) {
3054 syntax(NULL);
3055 debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n");
3056 return 1;
3057 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003058#if HAS_KEYWORDS
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003059#if ENABLE_HUSH_CASE
Denis Vlasenko757361f2008-07-14 08:26:47 +00003060 if (ctx->ctx_dsemicolon
3061 && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */
3062 ) {
Denis Vlasenko395ae452008-07-14 06:29:38 +00003063 /* already done when ctx_dsemicolon was set to 1: */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003064 /* ctx->ctx_res_w = RES_MATCH; */
3065 ctx->ctx_dsemicolon = 0;
3066 } else
3067#endif
3068
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003069 if (!child->argv /* if it's the first word... */
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003070#if ENABLE_HUSH_LOOPS
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003071 && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */
3072 && ctx->ctx_res_w != RES_IN
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003073#endif
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003074 ) {
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003075 debug_printf_parse(": checking '%s' for reserved-ness\n", word->data);
3076 if (reserved_word(word, ctx)) {
3077 o_reset(word);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003078 debug_printf_parse("done_word return %d\n", (ctx->ctx_res_w == RES_SNTX));
3079 return (ctx->ctx_res_w == RES_SNTX);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003080 }
Eric Andersen25f27032001-04-26 23:22:31 +00003081 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003082#endif
Denis Vlasenkoff182a32008-07-05 20:29:59 +00003083 if (word->nonnull /* word had "xx" or 'xx' at least as part of it. */
Denis Vlasenko55789c62008-06-18 16:30:42 +00003084 /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */
3085 && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL)
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003086 /* (otherwise it's known to be not empty and is already safe) */
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003087 ) {
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003088 /* exclude "$@" - it can expand to no word despite "" */
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00003089 char *p = word->data;
3090 while (p[0] == SPECIAL_VAR_SYMBOL
3091 && (p[1] & 0x7f) == '@'
3092 && p[2] == SPECIAL_VAR_SYMBOL
3093 ) {
3094 p += 3;
3095 }
3096 if (p == word->data || p[0] != '\0') {
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003097 /* saw no "$@", or not only "$@" but some
3098 * real text is there too */
3099 /* insert "empty variable" reference, this makes
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00003100 * e.g. "", $empty"" etc to not disappear */
3101 o_addchr(word, SPECIAL_VAR_SYMBOL);
3102 o_addchr(word, SPECIAL_VAR_SYMBOL);
3103 }
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00003104 }
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003105 child->argv = add_malloced_string_to_strings(child->argv, xstrdup(word->data));
3106 debug_print_strings("word appended to argv", child->argv);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003107 }
Eric Andersen25f27032001-04-26 23:22:31 +00003108
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003109 o_reset(word);
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003110 ctx->pending_redirect = NULL;
3111
Denis Vlasenko06810332007-05-21 23:30:54 +00003112#if ENABLE_HUSH_LOOPS
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003113 /* Force FOR to have just one word (variable name) */
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003114 /* NB: basically, this makes hush see "for v in ..." syntax as if
3115 * as it is "for v; in ...". FOR and IN become two pipe structs
3116 * in parse tree. */
3117 if (ctx->ctx_res_w == RES_FOR) {
3118//TODO: check that child->argv[0] is a valid variable name!
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003119 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003120 }
Denis Vlasenko06810332007-05-21 23:30:54 +00003121#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003122#if ENABLE_HUSH_CASE
3123 /* Force CASE to have just one word */
3124 if (ctx->ctx_res_w == RES_CASE) {
3125 done_pipe(ctx, PIPE_SEQ);
3126 }
3127#endif
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003128 debug_printf_parse("done_word return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003129 return 0;
3130}
3131
Denis Vlasenko6eaf8de2008-06-17 12:09:21 +00003132/* Command (member of a pipe) is complete. The only possible error here
3133 * is out of memory, in which case xmalloc exits. */
Eric Andersen25f27032001-04-26 23:22:31 +00003134static int done_command(struct p_context *ctx)
3135{
3136 /* The child is really already in the pipe structure, so
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003137 * advance the pipe counter and make a new, null child. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003138 struct pipe *pi = ctx->pipe;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003139 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00003140
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003141 if (child) {
3142 if (child->group == NULL
3143 && child->argv == NULL
3144 && child->redirects == NULL
3145 ) {
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003146 debug_printf_parse("done_command: skipping null cmd, num_progs=%d\n", pi->num_progs);
3147 return pi->num_progs;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003148 }
Eric Andersen25f27032001-04-26 23:22:31 +00003149 pi->num_progs++;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003150 debug_printf_parse("done_command: ++num_progs=%d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00003151 } else {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003152 debug_printf_parse("done_command: initializing, num_progs=%d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00003153 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003154
3155 /* Only real trickiness here is that the uncommitted
3156 * child structure is not counted in pi->num_progs. */
Eric Andersen25f27032001-04-26 23:22:31 +00003157 pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1));
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003158 child = &pi->progs[pi->num_progs];
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003159 memset(child, 0, sizeof(*child));
Eric Andersen25f27032001-04-26 23:22:31 +00003160
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003161 ctx->child = child;
Eric Andersen25f27032001-04-26 23:22:31 +00003162 /* but ctx->pipe and ctx->list_head remain unchanged */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003163
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003164 return pi->num_progs; /* used only for 0/nonzero check */
Eric Andersen25f27032001-04-26 23:22:31 +00003165}
3166
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003167static void done_pipe(struct p_context *ctx, pipe_style type)
Eric Andersen25f27032001-04-26 23:22:31 +00003168{
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003169 int not_null;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003170
3171 debug_printf_parse("done_pipe entered, followup %d\n", type);
Denis Vlasenko395ae452008-07-14 06:29:38 +00003172 /* Close previous command */
3173 not_null = done_command(ctx);
Eric Andersen25f27032001-04-26 23:22:31 +00003174 ctx->pipe->followup = type;
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003175 IF_HAS_KEYWORDS(ctx->pipe->pi_inverted = ctx->ctx_inverted;)
3176 IF_HAS_KEYWORDS(ctx->ctx_inverted = 0;)
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003177 IF_HAS_KEYWORDS(ctx->pipe->res_word = ctx->ctx_res_w;)
Denis Vlasenko395ae452008-07-14 06:29:38 +00003178
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003179 /* Without this check, even just <enter> on command line generates
3180 * tree of three NOPs (!). Which is harmless but annoying.
Denis Vlasenko6eaf8de2008-06-17 12:09:21 +00003181 * IOW: it is safe to do it unconditionally.
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003182 * RES_NONE case is for "for a in; do ..." (empty IN set)
3183 * to work, possibly other cases too. */
3184 if (not_null IF_HAS_KEYWORDS(|| ctx->ctx_res_w != RES_NONE)) {
Denis Vlasenko395ae452008-07-14 06:29:38 +00003185 struct pipe *new_p;
3186 debug_printf_parse("done_pipe: adding new pipe: "
Denis Vlasenko757361f2008-07-14 08:26:47 +00003187 "not_null:%d ctx->ctx_res_w:%d\n",
Denis Vlasenko395ae452008-07-14 06:29:38 +00003188 not_null, ctx->ctx_res_w);
3189 new_p = new_pipe();
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003190 ctx->pipe->next = new_p;
3191 ctx->pipe = new_p;
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003192 ctx->child = NULL; /* needed! */
Denis Vlasenko395ae452008-07-14 06:29:38 +00003193 /* RES_THEN, RES_DO etc are "sticky" -
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003194 * they remain set for commands inside if/while.
3195 * This is used to control execution.
3196 * RES_FOR and RES_IN are NOT sticky (needed to support
3197 * cases where variable or value happens to match a keyword):
3198 */
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003199#if ENABLE_HUSH_LOOPS
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003200 if (ctx->ctx_res_w == RES_FOR
3201 || ctx->ctx_res_w == RES_IN)
3202 ctx->ctx_res_w = RES_NONE;
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003203#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003204#if ENABLE_HUSH_CASE
3205 if (ctx->ctx_res_w == RES_MATCH)
3206 ctx->ctx_res_w = RES_CASEI;
3207#endif
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003208 /* Create the memory for child, roughly:
3209 * ctx->pipe->progs = new struct child_prog;
Denis Vlasenko7049ff82008-06-25 09:53:17 +00003210 * ctx->child = &ctx->pipe->progs[0];
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003211 */
3212 done_command(ctx);
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003213 }
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003214 debug_printf_parse("done_pipe return\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003215}
3216
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003217/* Peek ahead in the in_str to find out if we have a "&n" construct,
Eric Andersen25f27032001-04-26 23:22:31 +00003218 * as in "2>&1", that represents duplicating a file descriptor.
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003219 * Return either -2 (syntax error), -1 (no &), or the number found.
Eric Andersen25f27032001-04-26 23:22:31 +00003220 */
3221static int redirect_dup_num(struct in_str *input)
3222{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003223 int ch, d = 0, ok = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003224 ch = i_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003225 if (ch != '&') return -1;
3226
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003227 i_getch(input); /* get the & */
3228 ch = i_peek(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003229 if (ch == '-') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003230 i_getch(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003231 return -3; /* "-" represents "close me" */
3232 }
3233 while (isdigit(ch)) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00003234 d = d*10 + (ch-'0');
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003235 ok = 1;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003236 i_getch(input);
3237 ch = i_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003238 }
3239 if (ok) return d;
3240
Manuel Novoa III cad53642003-03-19 09:13:01 +00003241 bb_error_msg("ambiguous redirect");
Eric Andersen25f27032001-04-26 23:22:31 +00003242 return -2;
3243}
3244
3245/* If a redirect is immediately preceded by a number, that number is
3246 * supposed to tell which file descriptor to redirect. This routine
3247 * looks for such preceding numbers. In an ideal world this routine
3248 * needs to handle all the following classes of redirects...
3249 * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
3250 * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
3251 * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
3252 * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
3253 * A -1 output from this program means no valid number was found, so the
3254 * caller should use the appropriate default for this redirection.
3255 */
3256static int redirect_opt_num(o_string *o)
3257{
3258 int num;
3259
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003260 if (o->length == 0)
3261 return -1;
3262 for (num = 0; num < o->length; num++) {
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00003263 if (!isdigit(o->data[num])) {
Eric Andersen25f27032001-04-26 23:22:31 +00003264 return -1;
3265 }
3266 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003267 num = atoi(o->data);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003268 o_reset(o);
Eric Andersen25f27032001-04-26 23:22:31 +00003269 return num;
3270}
3271
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003272#if ENABLE_HUSH_TICK
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00003273static FILE *generate_stream_from_list(struct pipe *head)
Eric Andersen25f27032001-04-26 23:22:31 +00003274{
3275 FILE *pf;
Eric Andersen25f27032001-04-26 23:22:31 +00003276 int pid, channel[2];
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003277
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +00003278 xpipe(channel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003279/* *** NOMMU WARNING *** */
3280/* By using vfork here, we suspend parent till child exits or execs.
3281 * If child will not do it before it fills the pipe, it can block forever
3282 * in write(STDOUT_FILENO), and parent (shell) will be also stuck.
Denis Vlasenko3fe4f982008-06-09 16:02:39 +00003283 * Try this script:
3284 * yes "0123456789012345678901234567890" | dd bs=32 count=64k >TESTFILE
3285 * huge=`cat TESTFILE` # will block here forever
3286 * echo OK
Denis Vlasenko05743d72008-02-10 12:10:08 +00003287 */
Denis Vlasenko82604e92008-07-01 15:59:42 +00003288 pid = BB_MMU ? fork() : vfork();
3289 if (pid < 0)
3290 bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork");
Denis Vlasenko05743d72008-02-10 12:10:08 +00003291 if (pid == 0) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00003292 if (ENABLE_HUSH_JOB)
3293 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenko284d0fa2008-02-16 13:18:17 +00003294 close(channel[0]); /* NB: close _first_, then move fd! */
Denis Vlasenko05743d72008-02-10 12:10:08 +00003295 xmove_fd(channel[1], 1);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003296 /* Prevent it from trying to handle ctrl-z etc */
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003297#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00003298 G.run_list_level = 1;
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003299#endif
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003300 /* Process substitution is not considered to be usual
3301 * 'command execution'.
3302 * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. */
3303 /* Not needed, we are relying on it being disabled
3304 * everywhere outside actual command execution. */
3305 /*set_jobctrl_sighandler(SIG_IGN);*/
3306 set_misc_sighandler(SIG_DFL);
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003307 /* Freeing 'head' here would break NOMMU. */
3308 _exit(run_list(head));
Eric Andersen25f27032001-04-26 23:22:31 +00003309 }
Eric Andersen25f27032001-04-26 23:22:31 +00003310 close(channel[1]);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003311 pf = fdopen(channel[0], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00003312 return pf;
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003313 /* 'head' is freed by the caller */
Eric Andersen25f27032001-04-26 23:22:31 +00003314}
3315
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003316/* Return code is exit status of the process that is run. */
Denis Vlasenko68404f12008-03-17 09:00:54 +00003317static int process_command_subs(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +00003318 struct in_str *input,
3319 const char *subst_end)
Eric Andersen25f27032001-04-26 23:22:31 +00003320{
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003321 int retcode, ch, eol_cnt;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003322 o_string result = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003323 struct p_context inner;
3324 FILE *p;
3325 struct in_str pipe_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003326
Eric Andersen25f27032001-04-26 23:22:31 +00003327 initialize_context(&inner);
3328
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003329 /* Recursion to generate command */
Eric Andersen25f27032001-04-26 23:22:31 +00003330 retcode = parse_stream(&result, &inner, input, subst_end);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003331 if (retcode != 0)
3332 return retcode; /* syntax error or EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003333 done_word(&result, &inner);
3334 done_pipe(&inner, PIPE_SEQ);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003335 o_free(&result);
Eric Andersen25f27032001-04-26 23:22:31 +00003336
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003337 p = generate_stream_from_list(inner.list_head);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003338 if (p == NULL)
3339 return 1;
Denis Vlasenkoa0898172007-10-01 09:59:01 +00003340 close_on_exec_on(fileno(p));
Eric Andersen25f27032001-04-26 23:22:31 +00003341 setup_file_in_str(&pipe_str, p);
3342
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003343 /* Now send results of command back into original context */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003344 eol_cnt = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003345 while ((ch = i_getch(&pipe_str)) != EOF) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003346 if (ch == '\n') {
3347 eol_cnt++;
3348 continue;
3349 }
3350 while (eol_cnt) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00003351 o_addchr(dest, '\n');
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003352 eol_cnt--;
3353 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003354 o_addQchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003355 }
3356
3357 debug_printf("done reading from pipe, pclose()ing\n");
3358 /* This is the step that wait()s for the child. Should be pretty
3359 * safe, since we just read an EOF from its stdout. We could try
Denis Vlasenko262d7652007-05-20 21:52:49 +00003360 * to do better, by using wait(), and keeping track of background jobs
Eric Andersen25f27032001-04-26 23:22:31 +00003361 * at the same time. That would be a lot of work, and contrary
3362 * to the KISS philosophy of this program. */
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003363 retcode = fclose(p);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003364 free_pipe_list(inner.list_head, /* indent: */ 0);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003365 debug_printf("closed FILE from child, retcode=%d\n", retcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003366 return retcode;
3367}
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003368#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003369
3370static int parse_group(o_string *dest, struct p_context *ctx,
3371 struct in_str *input, int ch)
3372{
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003373 /* NB: parse_group may create and use its own o_string,
3374 * without any code changes. It just so happens that code is smaller
3375 * if we (ab)use caller's one. */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003376 int rcode;
3377 const char *endch = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003378 struct p_context sub;
3379 struct child_prog *child = ctx->child;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003380
3381 debug_printf_parse("parse_group entered\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003382 if (child->argv) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003383 syntax(NULL);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003384 debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n");
3385 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003386 }
3387 initialize_context(&sub);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003388 endch = "}";
3389 if (ch == '(') {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003390 endch = ")";
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003391 child->subshell = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003392 }
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003393 rcode = parse_stream(dest, &sub, input, endch);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003394 if (rcode == 0) {
3395 done_word(dest, &sub); /* finish off the final word in the subcontext */
3396 done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
3397 child->group = sub.list_head;
3398 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003399 debug_printf_parse("parse_group return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003400 return rcode;
3401 /* child remains "open", available for possible redirects */
3402}
3403
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003404/* Basically useful version until someone wants to get fancier,
Eric Andersen25f27032001-04-26 23:22:31 +00003405 * see the bash man page under "Parameter Expansion" */
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00003406static const char *lookup_param(const char *src)
Eric Andersen25f27032001-04-26 23:22:31 +00003407{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003408 struct variable *var = get_local_var(src);
3409 if (var)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003410 return strchr(var->varstr, '=') + 1;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003411 return NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003412}
3413
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003414#if ENABLE_HUSH_TICK
3415/* Subroutines for copying $(...) and `...` things */
3416static void add_till_backquote(o_string *dest, struct in_str *input);
3417/* '...' */
3418static void add_till_single_quote(o_string *dest, struct in_str *input)
3419{
3420 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003421 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003422 if (ch == EOF)
3423 break;
3424 if (ch == '\'')
3425 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003426 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003427 }
3428}
3429/* "...\"...`..`...." - do we need to handle "...$(..)..." too? */
3430static void add_till_double_quote(o_string *dest, struct in_str *input)
3431{
3432 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003433 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003434 if (ch == '"')
3435 break;
3436 if (ch == '\\') { /* \x. Copy both chars. */
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003437 o_addchr(dest, ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003438 ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003439 }
3440 if (ch == EOF)
3441 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003442 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003443 if (ch == '`') {
3444 add_till_backquote(dest, input);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003445 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003446 continue;
3447 }
Denis Vlasenko5703c222008-06-15 11:49:42 +00003448 //if (ch == '$') ...
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003449 }
3450}
3451/* Process `cmd` - copy contents until "`" is seen. Complicated by
3452 * \` quoting.
3453 * "Within the backquoted style of command substitution, backslash
3454 * shall retain its literal meaning, except when followed by: '$', '`', or '\'.
3455 * The search for the matching backquote shall be satisfied by the first
3456 * backquote found without a preceding backslash; during this search,
3457 * if a non-escaped backquote is encountered within a shell comment,
3458 * a here-document, an embedded command substitution of the $(command)
3459 * form, or a quoted string, undefined results occur. A single-quoted
3460 * or double-quoted string that begins, but does not end, within the
3461 * "`...`" sequence produces undefined results."
3462 * Example Output
3463 * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST
3464 */
3465static void add_till_backquote(o_string *dest, struct in_str *input)
3466{
3467 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003468 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003469 if (ch == '`')
3470 break;
3471 if (ch == '\\') { /* \x. Copy both chars unless it is \` */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003472 int ch2 = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003473 if (ch2 != '`' && ch2 != '$' && ch2 != '\\')
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003474 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003475 ch = ch2;
3476 }
3477 if (ch == EOF)
3478 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003479 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003480 }
3481}
3482/* Process $(cmd) - copy contents until ")" is seen. Complicated by
3483 * quoting and nested ()s.
3484 * "With the $(command) style of command substitution, all characters
3485 * following the open parenthesis to the matching closing parenthesis
3486 * constitute the command. Any valid shell script can be used for command,
3487 * except a script consisting solely of redirections which produces
3488 * unspecified results."
3489 * Example Output
3490 * echo $(echo '(TEST)' BEST) (TEST) BEST
3491 * echo $(echo 'TEST)' BEST) TEST) BEST
3492 * echo $(echo \(\(TEST\) BEST) ((TEST) BEST
3493 */
3494static void add_till_closing_curly_brace(o_string *dest, struct in_str *input)
3495{
3496 int count = 0;
3497 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003498 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003499 if (ch == EOF)
3500 break;
3501 if (ch == '(')
3502 count++;
3503 if (ch == ')')
3504 if (--count < 0)
3505 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003506 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003507 if (ch == '\'') {
3508 add_till_single_quote(dest, input);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003509 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003510 continue;
3511 }
3512 if (ch == '"') {
3513 add_till_double_quote(dest, input);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003514 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003515 continue;
3516 }
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003517 if (ch == '\\') { /* \x. Copy verbatim. Important for \(, \) */
3518 ch = i_getch(input);
3519 if (ch == EOF)
3520 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003521 o_addchr(dest, ch);
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003522 continue;
3523 }
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003524 }
3525}
3526#endif /* ENABLE_HUSH_TICK */
3527
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003528/* Return code: 0 for OK, 1 for syntax error */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003529static int handle_dollar(o_string *dest, struct in_str *input)
Eric Andersen25f27032001-04-26 23:22:31 +00003530{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003531 int ch = i_peek(input); /* first character after the $ */
Denis Vlasenkoff097622007-10-01 10:00:45 +00003532 unsigned char quote_mask = dest->o_quote ? 0x80 : 0;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003533
3534 debug_printf_parse("handle_dollar entered: ch='%c'\n", ch);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003535 if (isalpha(ch)) {
Denis Vlasenkod4981312008-07-31 10:34:48 +00003536 i_getch(input);
3537 make_var:
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003538 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003539 while (1) {
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003540 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003541 o_addchr(dest, ch | quote_mask);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003542 quote_mask = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003543 ch = i_peek(input);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003544 if (!isalnum(ch) && ch != '_')
3545 break;
Denis Vlasenkod4981312008-07-31 10:34:48 +00003546 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003547 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003548 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003549 } else if (isdigit(ch)) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003550 make_one_char_var:
Denis Vlasenkod4981312008-07-31 10:34:48 +00003551 i_getch(input);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003552 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003553 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003554 o_addchr(dest, ch | quote_mask);
3555 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003556 } else switch (ch) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003557 case '$': /* pid */
3558 case '!': /* last bg pid */
3559 case '?': /* last exit code */
3560 case '#': /* number of args */
3561 case '*': /* args */
3562 case '@': /* args */
3563 goto make_one_char_var;
Eric Andersen25f27032001-04-26 23:22:31 +00003564 case '{':
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003565 o_addchr(dest, SPECIAL_VAR_SYMBOL);
3566 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003567 /* XXX maybe someone will try to escape the '}' */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003568 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003569 ch = i_getch(input);
Denis Vlasenkob0550012007-05-24 12:18:16 +00003570 if (ch == '}')
3571 break;
3572 if (!isalnum(ch) && ch != '_') {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003573 syntax("unterminated ${name}");
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003574 debug_printf_parse("handle_dollar return 1: unterminated ${name}\n");
3575 return 1;
3576 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003577 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003578 o_addchr(dest, ch | quote_mask);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003579 quote_mask = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003580 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003581 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003582 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003583#if ENABLE_HUSH_TICK
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003584 case '(': {
3585 //int pos = dest->length;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003586 i_getch(input);
3587 o_addchr(dest, SPECIAL_VAR_SYMBOL);
3588 o_addchr(dest, quote_mask | '`');
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003589 add_till_closing_curly_brace(dest, input);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00003590 //debug_printf_subst("SUBST RES2 '%s'\n", dest->data + pos);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003591 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003592 break;
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003593 }
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003594#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003595 case '_':
Denis Vlasenkod4981312008-07-31 10:34:48 +00003596 i_getch(input);
3597 ch = i_peek(input);
3598 if (isalnum(ch)) { /* it's $_name or $_123 */
3599 ch = '_';
3600 goto make_var;
3601 }
3602 /* else: it's $_ */
3603 case '-':
Eric Andersen25f27032001-04-26 23:22:31 +00003604 /* still unhandled, but should be eventually */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003605 bb_error_msg("unhandled syntax: $%c", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003606 return 1;
3607 break;
3608 default:
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003609 o_addQchr(dest, '$');
Eric Andersen25f27032001-04-26 23:22:31 +00003610 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003611 debug_printf_parse("handle_dollar return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003612 return 0;
3613}
3614
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003615/* Scan input, call done_word() whenever full IFS delimited word was seen.
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003616 * Call done_pipe if '\n' was seen (and end_trigger != NULL).
3617 * Return code is 0 if end_trigger char is met,
3618 * -1 on EOF (but if end_trigger == NULL then return 0),
Denis Vlasenko55789c62008-06-18 16:30:42 +00003619 * 1 for syntax error */
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003620static int parse_stream(o_string *dest, struct p_context *ctx,
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003621 struct in_str *input, const char *end_trigger)
Eric Andersen25f27032001-04-26 23:22:31 +00003622{
"Vladimir N. Oleynik"4ccd2b42006-01-31 09:27:48 +00003623 int ch, m;
Eric Andersen25f27032001-04-26 23:22:31 +00003624 int redir_fd;
3625 redir_type redir_style;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003626 int shadow_quote = dest->o_quote;
Eric Andersen25f27032001-04-26 23:22:31 +00003627 int next;
3628
Denis Vlasenkoff097622007-10-01 10:00:45 +00003629 /* Only double-quote state is handled in the state variable dest->o_quote.
Eric Andersen25f27032001-04-26 23:22:31 +00003630 * A single-quote triggers a bypass of the main loop until its mate is
Denis Vlasenkoff097622007-10-01 10:00:45 +00003631 * found. When recursing, quote state is passed in via dest->o_quote. */
Eric Andersen25f27032001-04-26 23:22:31 +00003632
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003633 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 +00003634
Denis Vlasenko1a735862007-05-23 00:32:25 +00003635 while (1) {
Denis Vlasenko1a735862007-05-23 00:32:25 +00003636 m = CHAR_IFS;
3637 next = '\0';
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003638 ch = i_getch(input);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003639 if (ch != EOF) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00003640 m = G.charmap[ch];
Denis Vlasenko55789c62008-06-18 16:30:42 +00003641 if (ch != '\n') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003642 next = i_peek(input);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003643 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003644 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003645 debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n",
Denis Vlasenkoff097622007-10-01 10:00:45 +00003646 ch, ch, m, dest->o_quote);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003647 if (m == CHAR_ORDINARY
Denis Vlasenko55789c62008-06-18 16:30:42 +00003648 || (m != CHAR_SPECIAL && shadow_quote)
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003649 ) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00003650 if (ch == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003651 syntax("unterminated \"");
Denis Vlasenko170435c2007-05-23 13:01:10 +00003652 debug_printf_parse("parse_stream return 1: unterminated \"\n");
3653 return 1;
3654 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003655 o_addQchr(dest, ch);
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003656 if ((dest->o_assignment == MAYBE_ASSIGNMENT
3657 || dest->o_assignment == WORD_IS_KEYWORD)
Denis Vlasenko55789c62008-06-18 16:30:42 +00003658 && ch == '='
3659 && is_assignment(dest->data)
3660 ) {
3661 dest->o_assignment = DEFINITELY_ASSIGNMENT;
3662 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003663 continue;
3664 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003665 if (m == CHAR_IFS) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003666 if (done_word(dest, ctx)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003667 debug_printf_parse("parse_stream return 1: done_word!=0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003668 return 1;
Eric Andersenaac75e52001-04-30 18:18:45 +00003669 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003670 if (ch == EOF)
3671 break;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003672 /* If we aren't performing a substitution, treat
3673 * a newline as a command separator.
3674 * [why we don't handle it exactly like ';'? --vda] */
3675 if (end_trigger && ch == '\n') {
Denis Vlasenkof1736072008-07-31 10:09:26 +00003676#if ENABLE_HUSH_CASE
3677 /* "case ... in <newline> word) ..." -
3678 * newlines are ignored (but ';' wouldn't be) */
3679 if (dest->length == 0 // && argv[0] == NULL
3680 && ctx->ctx_res_w == RES_MATCH
3681 ) {
3682 continue;
3683 }
3684#endif
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003685 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003686 dest->o_assignment = MAYBE_ASSIGNMENT;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003687 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003688 }
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003689 if (end_trigger) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00003690 if (!shadow_quote && strchr(end_trigger, ch)) {
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003691 /* Special case: (...word) makes last word terminate,
3692 * as if ';' is seen */
3693 if (ch == ')') {
3694 done_word(dest, ctx);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003695//err chk?
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003696 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003697 dest->o_assignment = MAYBE_ASSIGNMENT;
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003698 }
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003699 if (!HAS_KEYWORDS
3700 IF_HAS_KEYWORDS(|| (ctx->ctx_res_w == RES_NONE && ctx->old_flag == 0))
3701 ) {
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003702 debug_printf_parse("parse_stream return 0: end_trigger char found\n");
3703 return 0;
3704 }
3705 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003706 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003707 if (m == CHAR_IFS)
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003708 continue;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003709
3710 if (dest->o_assignment == MAYBE_ASSIGNMENT) {
3711 /* ch is a special char and thus this word
3712 * cannot be an assignment: */
3713 dest->o_assignment = NOT_ASSIGNMENT;
3714 }
3715
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003716 switch (ch) {
Eric Andersen25f27032001-04-26 23:22:31 +00003717 case '#':
Denis Vlasenko55789c62008-06-18 16:30:42 +00003718 if (dest->length == 0 && !shadow_quote) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003719 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003720 ch = i_peek(input);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003721 if (ch == EOF || ch == '\n')
3722 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003723 i_getch(input);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003724 }
Eric Andersen25f27032001-04-26 23:22:31 +00003725 } else {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003726 o_addQchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003727 }
3728 break;
3729 case '\\':
3730 if (next == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003731 syntax("\\<eof>");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003732 debug_printf_parse("parse_stream return 1: \\<eof>\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003733 return 1;
3734 }
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003735 /* bash:
3736 * "The backslash retains its special meaning [in "..."]
3737 * only when followed by one of the following characters:
3738 * $, `, ", \, or <newline>. A double quote may be quoted
3739 * within double quotes by preceding it with a backslash.
3740 * If enabled, history expansion will be performed unless
3741 * an ! appearing in double quotes is escaped using
3742 * a backslash. The backslash preceding the ! is not removed."
3743 */
Denis Vlasenko55789c62008-06-18 16:30:42 +00003744 if (shadow_quote) { //NOT SURE dest->o_quote) {
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003745 if (strchr("$`\"\\", next) != NULL) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003746 o_addqchr(dest, i_getch(input));
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003747 } else {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003748 o_addqchr(dest, '\\');
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003749 }
3750 } else {
3751 o_addchr(dest, '\\');
3752 o_addchr(dest, i_getch(input));
3753 }
Eric Andersen25f27032001-04-26 23:22:31 +00003754 break;
3755 case '$':
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003756 if (handle_dollar(dest, input) != 0) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003757 debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n");
3758 return 1;
3759 }
Eric Andersen25f27032001-04-26 23:22:31 +00003760 break;
3761 case '\'':
3762 dest->nonnull = 1;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003763 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003764 ch = i_getch(input);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003765 if (ch == EOF) {
3766 syntax("unterminated '");
3767 debug_printf_parse("parse_stream return 1: unterminated '\n");
3768 return 1;
3769 }
3770 if (ch == '\'')
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003771 break;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003772 if (dest->o_assignment == NOT_ASSIGNMENT)
3773 o_addqchr(dest, ch);
3774 else
3775 o_addchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003776 }
Eric Andersen25f27032001-04-26 23:22:31 +00003777 break;
3778 case '"':
3779 dest->nonnull = 1;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003780 shadow_quote ^= 1; /* invert */
3781 if (dest->o_assignment == NOT_ASSIGNMENT)
3782 dest->o_quote ^= 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003783 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003784#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003785 case '`': {
3786 //int pos = dest->length;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003787 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003788 o_addchr(dest, shadow_quote /*or dest->o_quote??*/ ? 0x80 | '`' : '`');
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003789 add_till_backquote(dest, input);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003790 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00003791 //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos);
Eric Andersen25f27032001-04-26 23:22:31 +00003792 break;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003793 }
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003794#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003795 case '>':
3796 redir_fd = redirect_opt_num(dest);
3797 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003798 redir_style = REDIRECT_OVERWRITE;
Eric Andersen25f27032001-04-26 23:22:31 +00003799 if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003800 redir_style = REDIRECT_APPEND;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003801 i_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003802 }
3803#if 0
3804 else if (next == '(') {
3805 syntax(">(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003806 debug_printf_parse("parse_stream return 1: >(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003807 return 1;
3808 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003809#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003810 setup_redirect(ctx, redir_fd, redir_style, input);
3811 break;
3812 case '<':
3813 redir_fd = redirect_opt_num(dest);
3814 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003815 redir_style = REDIRECT_INPUT;
Eric Andersen25f27032001-04-26 23:22:31 +00003816 if (next == '<') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003817 redir_style = REDIRECT_HEREIS;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003818 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003819 } else if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003820 redir_style = REDIRECT_IO;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003821 i_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003822 }
3823#if 0
3824 else if (next == '(') {
3825 syntax("<(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003826 debug_printf_parse("parse_stream return 1: <(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003827 return 1;
3828 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003829#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003830 setup_redirect(ctx, redir_fd, redir_style, input);
3831 break;
3832 case ';':
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003833#if ENABLE_HUSH_CASE
3834 case_semi:
3835#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003836 done_word(dest, ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003837 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003838#if ENABLE_HUSH_CASE
3839 /* Eat multiple semicolons, detect
3840 * whether it means something special */
3841 while (1) {
3842 ch = i_peek(input);
3843 if (ch != ';')
3844 break;
3845 i_getch(input);
3846 if (ctx->ctx_res_w == RES_CASEI) {
3847 ctx->ctx_dsemicolon = 1;
3848 ctx->ctx_res_w = RES_MATCH;
3849 break;
3850 }
3851 }
3852#endif
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003853 new_cmd:
3854 /* We just finished a cmd. New one may start
3855 * with an assignment */
3856 dest->o_assignment = MAYBE_ASSIGNMENT;
Eric Andersen25f27032001-04-26 23:22:31 +00003857 break;
3858 case '&':
3859 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003860 if (next == '&') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003861 i_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003862 done_pipe(ctx, PIPE_AND);
Eric Andersen25f27032001-04-26 23:22:31 +00003863 } else {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003864 done_pipe(ctx, PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00003865 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003866 goto new_cmd;
Eric Andersen25f27032001-04-26 23:22:31 +00003867 case '|':
3868 done_word(dest, ctx);
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00003869#if ENABLE_HUSH_CASE
3870 if (ctx->ctx_res_w == RES_MATCH)
Denis Vlasenkof1736072008-07-31 10:09:26 +00003871 break; /* we are in case's "word | word)" */
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00003872#endif
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003873 if (next == '|') { /* || */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003874 i_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003875 done_pipe(ctx, PIPE_OR);
Eric Andersen25f27032001-04-26 23:22:31 +00003876 } else {
3877 /* we could pick up a file descriptor choice here
3878 * with redirect_opt_num(), but bash doesn't do it.
3879 * "echo foo 2| cat" yields "foo 2". */
3880 done_command(ctx);
3881 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003882 goto new_cmd;
Eric Andersen25f27032001-04-26 23:22:31 +00003883 case '(':
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003884#if ENABLE_HUSH_CASE
Denis Vlasenkof1736072008-07-31 10:09:26 +00003885 /* "case... in [(]word)..." - skip '(' */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003886 if (dest->length == 0 // && argv[0] == NULL
3887 && ctx->ctx_res_w == RES_MATCH
3888 ) {
3889 continue;
3890 }
3891#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003892 case '{':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003893 if (parse_group(dest, ctx, input, ch) != 0) {
3894 debug_printf_parse("parse_stream return 1: parse_group returned non-0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003895 return 1;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003896 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003897 goto new_cmd;
Eric Andersen25f27032001-04-26 23:22:31 +00003898 case ')':
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003899#if ENABLE_HUSH_CASE
3900 if (ctx->ctx_res_w == RES_MATCH)
3901 goto case_semi;
3902#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003903 case '}':
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003904 /* proper use of this character is caught by end_trigger */
3905 syntax("unexpected } or )");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003906 debug_printf_parse("parse_stream return 1: unexpected '}'\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003907 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003908 default:
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003909 if (HUSH_DEBUG)
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003910 bb_error_msg_and_die("BUG: unexpected %c\n", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003911 }
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003912 } /* while (1) */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003913 debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL));
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003914 if (end_trigger)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003915 return -1;
Eric Andersen25f27032001-04-26 23:22:31 +00003916 return 0;
3917}
3918
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003919static void set_in_charmap(const char *set, int code)
Eric Andersen25f27032001-04-26 23:22:31 +00003920{
Denis Vlasenkof5294e12007-04-14 10:09:57 +00003921 while (*set)
Denis Vlasenko87a86552008-07-29 19:43:10 +00003922 G.charmap[(unsigned char)*set++] = code;
Eric Andersen25f27032001-04-26 23:22:31 +00003923}
3924
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003925static void update_charmap(void)
Eric Andersen25f27032001-04-26 23:22:31 +00003926{
Denis Vlasenko87a86552008-07-29 19:43:10 +00003927 G.ifs = getenv("IFS");
3928 if (G.ifs == NULL)
3929 G.ifs = " \t\n";
Eric Andersen25f27032001-04-26 23:22:31 +00003930 /* Precompute a list of 'flow through' behavior so it can be treated
3931 * quickly up front. Computation is necessary because of IFS.
3932 * Special case handling of IFS == " \t\n" is not implemented.
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003933 * The charmap[] array only really needs two bits each,
3934 * and on most machines that would be faster (reduced L1 cache use).
Eric Andersen25f27032001-04-26 23:22:31 +00003935 */
Denis Vlasenko87a86552008-07-29 19:43:10 +00003936 memset(G.charmap, CHAR_ORDINARY, sizeof(G.charmap));
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003937#if ENABLE_HUSH_TICK
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003938 set_in_charmap("\\$\"`", CHAR_SPECIAL);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003939#else
3940 set_in_charmap("\\$\"", CHAR_SPECIAL);
3941#endif
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003942 set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED);
Denis Vlasenko87a86552008-07-29 19:43:10 +00003943 set_in_charmap(G.ifs, CHAR_IFS); /* are ordinary if quoted */
Eric Andersen25f27032001-04-26 23:22:31 +00003944}
3945
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003946/* Most recursion does not come through here, the exception is
Denis Vlasenko170435c2007-05-23 13:01:10 +00003947 * from builtin_source() and builtin_eval() */
3948static int parse_and_run_stream(struct in_str *inp, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003949{
Eric Andersen25f27032001-04-26 23:22:31 +00003950 struct p_context ctx;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003951 o_string temp = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003952 int rcode;
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003953
Eric Andersen25f27032001-04-26 23:22:31 +00003954 do {
3955 initialize_context(&ctx);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003956 update_charmap();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003957#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00003958 inp->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003959#endif
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003960 /* We will stop & execute after each ';' or '\n'.
3961 * Example: "sleep 9999; echo TEST" + ctrl-C:
3962 * TEST should be printed */
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003963 temp.o_assignment = MAYBE_ASSIGNMENT;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003964 rcode = parse_stream(&temp, &ctx, inp, ";\n");
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003965#if HAS_KEYWORDS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003966 if (rcode != 1 && ctx.old_flag != 0) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003967 syntax(NULL);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003968 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003969#endif
3970 if (rcode != 1 IF_HAS_KEYWORDS(&& ctx.old_flag == 0)) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003971 done_word(&temp, &ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003972 done_pipe(&ctx, PIPE_SEQ);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00003973 debug_print_tree(ctx.list_head, 0);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003974 debug_printf_exec("parse_stream_outer: run_and_free_list\n");
3975 run_and_free_list(ctx.list_head);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003976 } else {
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003977 /* We arrive here also if rcode == 1 (error in parse_stream) */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003978#if HAS_KEYWORDS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003979 if (ctx.old_flag != 0) {
3980 free(ctx.stack);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003981 o_reset(&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003982 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003983#endif
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00003984 /*temp.nonnull = 0; - o_free does it below */
3985 /*temp.o_quote = 0; - o_free does it below */
Denis Vlasenko05743d72008-02-10 12:10:08 +00003986 free_pipe_list(ctx.list_head, /* indent: */ 0);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003987 /* Discard all unprocessed line input, force prompt on */
3988 inp->p = NULL;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003989#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003990 inp->promptme = 1;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003991#endif
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003992 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003993 o_free(&temp);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003994 /* loop on syntax errors, return on EOF: */
3995 } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP));
Eric Andersen25f27032001-04-26 23:22:31 +00003996 return 0;
3997}
3998
Denis Vlasenko170435c2007-05-23 13:01:10 +00003999static int parse_and_run_string(const char *s, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00004000{
4001 struct in_str input;
4002 setup_string_in_str(&input, s);
Denis Vlasenko170435c2007-05-23 13:01:10 +00004003 return parse_and_run_stream(&input, parse_flag);
Eric Andersen25f27032001-04-26 23:22:31 +00004004}
4005
Denis Vlasenko170435c2007-05-23 13:01:10 +00004006static int parse_and_run_file(FILE *f)
Eric Andersen25f27032001-04-26 23:22:31 +00004007{
4008 int rcode;
4009 struct in_str input;
4010 setup_file_in_str(&input, f);
Denis Vlasenko5703c222008-06-15 11:49:42 +00004011 rcode = parse_and_run_stream(&input, 0 /* parse_flag */);
Eric Andersen25f27032001-04-26 23:22:31 +00004012 return rcode;
4013}
4014
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004015#if ENABLE_HUSH_JOB
Eric Andersen6c947d22001-06-25 22:24:38 +00004016/* Make sure we have a controlling tty. If we get started under a job
4017 * aware app (like bash for example), make sure we are now in charge so
4018 * we don't fight over who gets the foreground */
Eric Anderseneaecbf32001-10-31 10:41:31 +00004019static void setup_job_control(void)
Eric Andersen52a97ca2001-06-22 06:49:26 +00004020{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004021 pid_t shell_pgrp;
4022
Denis Vlasenko87a86552008-07-29 19:43:10 +00004023 shell_pgrp = getpgrp();
Denis Vlasenko87a86552008-07-29 19:43:10 +00004024 close_on_exec_on(G.interactive_fd);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004025
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00004026 /* If we were ran as 'hush &',
4027 * sleep until we are in the foreground. */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004028 while (tcgetpgrp(G.interactive_fd) != shell_pgrp) {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004029 /* Send TTIN to ourself (should stop us) */
Denis Vlasenkoff131b92007-04-10 15:42:06 +00004030 kill(- shell_pgrp, SIGTTIN);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00004031 shell_pgrp = getpgrp();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004032 }
Eric Andersen52a97ca2001-06-22 06:49:26 +00004033
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004034 /* Ignore job-control and misc signals. */
4035 set_jobctrl_sighandler(SIG_IGN);
4036 set_misc_sighandler(SIG_IGN);
4037//huh? signal(SIGCHLD, SIG_IGN);
4038
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00004039 /* We _must_ restore tty pgrp on fatal signals */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004040 set_fatal_sighandler(sigexit);
Eric Andersen6c947d22001-06-25 22:24:38 +00004041
4042 /* Put ourselves in our own process group. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00004043 setpgrp(); /* is the same as setpgid(our_pid, our_pid); */
Eric Andersen6c947d22001-06-25 22:24:38 +00004044 /* Grab control of the terminal. */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004045 tcsetpgrp(G.interactive_fd, getpid());
Eric Andersen6c947d22001-06-25 22:24:38 +00004046}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004047#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00004048
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004049
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00004050int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Matt Kraai2d91deb2001-08-01 17:21:35 +00004051int hush_main(int argc, char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00004052{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00004053 static const char version_str[] ALIGN1 = "HUSH_VERSION="HUSH_VER_STR;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004054 static const struct variable const_shell_ver = {
4055 .next = NULL,
4056 .varstr = (char*)version_str,
4057 .max_len = 1, /* 0 can provoke free(name) */
4058 .flg_export = 1,
4059 .flg_read_only = 1,
4060 };
4061
Eric Andersen25f27032001-04-26 23:22:31 +00004062 int opt;
4063 FILE *input;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004064 char **e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004065 struct variable *cur_var;
Eric Andersenbc604a22001-05-16 05:24:03 +00004066
Denis Vlasenko574f2f42008-02-27 18:41:59 +00004067 INIT_G();
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004068
Denis Vlasenko87a86552008-07-29 19:43:10 +00004069 G.root_pid = getpid();
Denis Vlasenko16c2fea2008-06-17 12:28:44 +00004070
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004071 /* Deal with HUSH_VERSION */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004072 G.shell_ver = const_shell_ver; /* copying struct here */
4073 G.top_var = &G.shell_ver;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004074 unsetenv("HUSH_VERSION"); /* in case it exists in initial env */
4075 /* Initialize our shell local variables with the values
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004076 * currently living in the environment */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004077 cur_var = G.top_var;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004078 e = environ;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004079 if (e) while (*e) {
4080 char *value = strchr(*e, '=');
4081 if (value) { /* paranoia */
4082 cur_var->next = xzalloc(sizeof(*cur_var));
4083 cur_var = cur_var->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00004084 cur_var->varstr = *e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004085 cur_var->max_len = strlen(*e);
4086 cur_var->flg_export = 1;
4087 }
4088 e++;
4089 }
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004090 putenv((char *)version_str); /* reinstate HUSH_VERSION */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00004091
Denis Vlasenko38f63192007-01-22 09:03:07 +00004092#if ENABLE_FEATURE_EDITING
Denis Vlasenko87a86552008-07-29 19:43:10 +00004093 G.line_input_state = new_line_input_t(FOR_SHELL);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00004094#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004095 /* XXX what should these be while sourcing /etc/profile? */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004096 G.global_argc = argc;
4097 G.global_argv = argv;
Eric Andersen94ac2442001-05-22 19:05:18 +00004098 /* Initialize some more globals to non-zero values */
4099 set_cwd();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004100#if ENABLE_HUSH_INTERACTIVE
4101#if ENABLE_FEATURE_EDITING
4102 cmdedit_set_initial_prompt();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004103#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00004104 G.PS2 = "> ";
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004105#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00004106
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004107 if (EXIT_SUCCESS) /* otherwise is already done */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004108 G.last_return_code = EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00004109
4110 if (argv[0] && argv[0][0] == '-') {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00004111 debug_printf("sourcing /etc/profile\n");
Denis Vlasenko5415c852008-07-21 23:05:26 +00004112 input = fopen_for_read("/etc/profile");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00004113 if (input != NULL) {
Denis Vlasenkoa0898172007-10-01 09:59:01 +00004114 close_on_exec_on(fileno(input));
Denis Vlasenko170435c2007-05-23 13:01:10 +00004115 parse_and_run_file(input);
Eric Andersena90f20b2001-06-26 23:00:21 +00004116 fclose(input);
4117 }
Eric Andersen25f27032001-04-26 23:22:31 +00004118 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004119 input = stdin;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004120
Eric Andersen25f27032001-04-26 23:22:31 +00004121 while ((opt = getopt(argc, argv, "c:xif")) > 0) {
4122 switch (opt) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004123 case 'c':
Denis Vlasenko87a86552008-07-29 19:43:10 +00004124 G.global_argv = argv + optind;
4125 G.global_argc = argc - optind;
Denis Vlasenko5703c222008-06-15 11:49:42 +00004126 opt = parse_and_run_string(optarg, 0 /* parse_flag */);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004127 goto final_return;
4128 case 'i':
Denis Vlasenkoc666f712007-05-16 22:18:54 +00004129 /* Well, we cannot just declare interactiveness,
4130 * we have to have some stuff (ctty, etc) */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004131 /* G.interactive_fd++; */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004132 break;
4133 case 'f':
Denis Vlasenko87a86552008-07-29 19:43:10 +00004134 G.fake_mode = 1;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004135 break;
4136 default:
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004137#ifndef BB_VER
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004138 fprintf(stderr, "Usage: sh [FILE]...\n"
4139 " or: sh -c command [args]...\n\n");
4140 exit(EXIT_FAILURE);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004141#else
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004142 bb_show_usage();
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004143#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004144 }
4145 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004146#if ENABLE_HUSH_JOB
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004147 /* A shell is interactive if the '-i' flag was given, or if all of
Eric Andersen25f27032001-04-26 23:22:31 +00004148 * the following conditions are met:
Denis Vlasenko55b2de72007-04-18 17:21:28 +00004149 * no -c command
Eric Andersen25f27032001-04-26 23:22:31 +00004150 * no arguments remaining or the -s flag given
4151 * standard input is a terminal
4152 * standard output is a terminal
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004153 * Refer to Posix.2, the description of the 'sh' utility. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004154 if (argv[optind] == NULL && input == stdin
4155 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
4156 ) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004157 G.saved_tty_pgrp = tcgetpgrp(STDIN_FILENO);
4158 debug_printf("saved_tty_pgrp=%d\n", G.saved_tty_pgrp);
4159 if (G.saved_tty_pgrp >= 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004160 /* try to dup to high fd#, >= 255 */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004161 G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
4162 if (G.interactive_fd < 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004163 /* try to dup to any fd */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004164 G.interactive_fd = dup(STDIN_FILENO);
4165 if (G.interactive_fd < 0)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004166 /* give up */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004167 G.interactive_fd = 0;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004168 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004169 // TODO: track & disallow any attempts of user
4170 // to (inadvertently) close/redirect it
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004171 }
Eric Andersen25f27032001-04-26 23:22:31 +00004172 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004173 debug_printf("G.interactive_fd=%d\n", G.interactive_fd);
4174 if (G.interactive_fd) {
4175 fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC);
Eric Andersen25f27032001-04-26 23:22:31 +00004176 /* Looks like they want an interactive shell */
Eric Andersen52a97ca2001-06-22 06:49:26 +00004177 setup_job_control();
Denis Vlasenko4ecfcdc2008-02-11 08:32:31 +00004178 /* -1 is special - makes xfuncs longjmp, not exit
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00004179 * (we reset die_sleep = 0 whereever we [v]fork) */
4180 die_sleep = -1;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004181 if (setjmp(die_jmp)) {
4182 /* xfunc has failed! die die die */
4183 hush_exit(xfunc_error_retval);
4184 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004185#if !ENABLE_FEATURE_SH_EXTRA_QUIET
Denis Vlasenkoca525b42007-06-13 12:27:17 +00004186 printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", bb_banner);
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004187 printf("Enter 'help' for a list of built-in commands.\n\n");
4188#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00004189 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004190#elif ENABLE_HUSH_INTERACTIVE
4191/* no job control compiled, only prompt/line editing */
4192 if (argv[optind] == NULL && input == stdin
4193 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
4194 ) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004195 G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
4196 if (G.interactive_fd < 0) {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004197 /* try to dup to any fd */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004198 G.interactive_fd = dup(STDIN_FILENO);
4199 if (G.interactive_fd < 0)
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004200 /* give up */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004201 G.interactive_fd = 0;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004202 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004203 if (G.interactive_fd) {
4204 fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC);
Denis Vlasenko4830fc52008-05-25 21:50:55 +00004205 set_misc_sighandler(SIG_IGN);
4206 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004207 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004208#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004209
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004210 if (argv[optind] == NULL) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00004211 opt = parse_and_run_file(stdin);
Denis Vlasenko05743d72008-02-10 12:10:08 +00004212 } else {
4213 debug_printf("\nrunning script '%s'\n", argv[optind]);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004214 G.global_argv = argv + optind;
4215 G.global_argc = argc - optind;
Denis Vlasenko5415c852008-07-21 23:05:26 +00004216 input = xfopen_for_read(argv[optind]);
Denis Vlasenko459a5ad2008-02-11 08:35:03 +00004217 fcntl(fileno(input), F_SETFD, FD_CLOEXEC);
Denis Vlasenko05743d72008-02-10 12:10:08 +00004218 opt = parse_and_run_file(input);
Eric Andersen25f27032001-04-26 23:22:31 +00004219 }
Eric Andersen25f27032001-04-26 23:22:31 +00004220
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004221 final_return:
4222
Denis Vlasenko38f63192007-01-22 09:03:07 +00004223#if ENABLE_FEATURE_CLEAN_UP
Eric Andersenaeb44c42001-05-22 20:29:00 +00004224 fclose(input);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004225 if (G.cwd != bb_msg_unknown)
4226 free((char*)G.cwd);
4227 cur_var = G.top_var->next;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004228 while (cur_var) {
4229 struct variable *tmp = cur_var;
4230 if (!cur_var->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00004231 free(cur_var->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004232 cur_var = cur_var->next;
4233 free(tmp);
Eric Andersenaeb44c42001-05-22 20:29:00 +00004234 }
Eric Andersen25f27032001-04-26 23:22:31 +00004235#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00004236 hush_exit(opt ? opt : G.last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +00004237}
Denis Vlasenko96702ca2007-11-23 23:28:55 +00004238
4239
4240#if ENABLE_LASH
4241int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
4242int lash_main(int argc, char **argv)
4243{
4244 //bb_error_msg("lash is deprecated, please use hush instead");
4245 return hush_main(argc, argv);
4246}
4247#endif
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004248
4249
4250/*
4251 * Built-ins
4252 */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004253static int builtin_true(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004254{
4255 return 0;
4256}
4257
4258static int builtin_test(char **argv)
4259{
4260 int argc = 0;
4261 while (*argv) {
4262 argc++;
4263 argv++;
4264 }
4265 return test_main(argc, argv - argc);
4266}
4267
4268static int builtin_echo(char **argv)
4269{
4270 int argc = 0;
4271 while (*argv) {
4272 argc++;
4273 argv++;
4274 }
4275 return echo_main(argc, argv - argc);
4276}
4277
4278static int builtin_eval(char **argv)
4279{
4280 int rcode = EXIT_SUCCESS;
4281
4282 if (argv[1]) {
4283 char *str = expand_strvec_to_string(argv + 1);
4284 parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP);
4285 free(str);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004286 rcode = G.last_return_code;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004287 }
4288 return rcode;
4289}
4290
4291static int builtin_cd(char **argv)
4292{
4293 const char *newdir;
4294 if (argv[1] == NULL) {
4295 // bash does nothing (exitcode 0) if HOME is ""; if it's unset,
4296 // bash says "bash: cd: HOME not set" and does nothing (exitcode 1)
4297 newdir = getenv("HOME") ? : "/";
4298 } else
4299 newdir = argv[1];
4300 if (chdir(newdir)) {
4301 printf("cd: %s: %s\n", newdir, strerror(errno));
4302 return EXIT_FAILURE;
4303 }
4304 set_cwd();
4305 return EXIT_SUCCESS;
4306}
4307
4308static int builtin_exec(char **argv)
4309{
4310 if (argv[1] == NULL)
4311 return EXIT_SUCCESS; /* bash does this */
4312 {
4313#if !BB_MMU
4314 char **ptrs2free = alloc_ptrs(argv);
4315#endif
4316// FIXME: if exec fails, bash does NOT exit! We do...
Denis Vlasenko2b576b82008-08-04 00:46:07 +00004317 pseudo_exec_argv(ptrs2free, argv + 1, 0, NULL);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004318 /* never returns */
4319 }
4320}
4321
4322static int builtin_exit(char **argv)
4323{
4324// TODO: bash does it ONLY on top-level sh exit (+interacive only?)
4325 //puts("exit"); /* bash does it */
4326// TODO: warn if we have background jobs: "There are stopped jobs"
4327// On second consecutive 'exit', exit anyway.
4328 if (argv[1] == NULL)
Denis Vlasenko87a86552008-07-29 19:43:10 +00004329 hush_exit(G.last_return_code);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004330 /* mimic bash: exit 123abc == exit 255 + error msg */
4331 xfunc_error_retval = 255;
4332 /* bash: exit -2 == exit 254, no error msg */
4333 hush_exit(xatoi(argv[1]) & 0xff);
4334}
4335
4336static int builtin_export(char **argv)
4337{
4338 const char *value;
4339 char *name = argv[1];
4340
4341 if (name == NULL) {
4342 // TODO:
4343 // ash emits: export VAR='VAL'
4344 // bash: declare -x VAR="VAL"
4345 // (both also escape as needed (quotes, $, etc))
4346 char **e = environ;
4347 if (e)
4348 while (*e)
4349 puts(*e++);
4350 return EXIT_SUCCESS;
4351 }
4352
4353 value = strchr(name, '=');
4354 if (!value) {
4355 /* They are exporting something without a =VALUE */
4356 struct variable *var;
4357
4358 var = get_local_var(name);
4359 if (var) {
4360 var->flg_export = 1;
4361 putenv(var->varstr);
4362 }
4363 /* bash does not return an error when trying to export
4364 * an undefined variable. Do likewise. */
4365 return EXIT_SUCCESS;
4366 }
4367
4368 set_local_var(xstrdup(name), 1);
4369 return EXIT_SUCCESS;
4370}
4371
4372#if ENABLE_HUSH_JOB
4373/* built-in 'fg' and 'bg' handler */
4374static int builtin_fg_bg(char **argv)
4375{
4376 int i, jobnum;
4377 struct pipe *pi;
4378
Denis Vlasenko87a86552008-07-29 19:43:10 +00004379 if (!G.interactive_fd)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004380 return EXIT_FAILURE;
4381 /* If they gave us no args, assume they want the last backgrounded task */
4382 if (!argv[1]) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004383 for (pi = G.job_list; pi; pi = pi->next) {
4384 if (pi->jobid == G.last_jobid) {
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004385 goto found;
4386 }
4387 }
4388 bb_error_msg("%s: no current job", argv[0]);
4389 return EXIT_FAILURE;
4390 }
4391 if (sscanf(argv[1], "%%%d", &jobnum) != 1) {
4392 bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]);
4393 return EXIT_FAILURE;
4394 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004395 for (pi = G.job_list; pi; pi = pi->next) {
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004396 if (pi->jobid == jobnum) {
4397 goto found;
4398 }
4399 }
4400 bb_error_msg("%s: %d: no such job", argv[0], jobnum);
4401 return EXIT_FAILURE;
4402 found:
4403 // TODO: bash prints a string representation
4404 // of job being foregrounded (like "sleep 1 | cat")
4405 if (*argv[0] == 'f') {
4406 /* Put the job into the foreground. */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004407 tcsetpgrp(G.interactive_fd, pi->pgrp);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004408 }
4409
4410 /* Restart the processes in the job */
4411 debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_progs, pi->pgrp);
4412 for (i = 0; i < pi->num_progs; i++) {
4413 debug_printf_jobs("reviving pid %d\n", pi->progs[i].pid);
4414 pi->progs[i].is_stopped = 0;
4415 }
4416 pi->stopped_progs = 0;
4417
4418 i = kill(- pi->pgrp, SIGCONT);
4419 if (i < 0) {
4420 if (errno == ESRCH) {
4421 delete_finished_bg_job(pi);
4422 return EXIT_SUCCESS;
4423 } else {
4424 bb_perror_msg("kill (SIGCONT)");
4425 }
4426 }
4427
4428 if (*argv[0] == 'f') {
4429 remove_bg_job(pi);
4430 return checkjobs_and_fg_shell(pi);
4431 }
4432 return EXIT_SUCCESS;
4433}
4434#endif
4435
4436#if ENABLE_HUSH_HELP
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004437static int builtin_help(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004438{
4439 const struct built_in_command *x;
4440
4441 printf("\nBuilt-in commands:\n");
4442 printf("-------------------\n");
4443 for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
4444 printf("%s\t%s\n", x->cmd, x->descr);
4445 }
4446 printf("\n\n");
4447 return EXIT_SUCCESS;
4448}
4449#endif
4450
4451#if ENABLE_HUSH_JOB
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004452static int builtin_jobs(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004453{
4454 struct pipe *job;
4455 const char *status_string;
4456
Denis Vlasenko87a86552008-07-29 19:43:10 +00004457 for (job = G.job_list; job; job = job->next) {
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00004458 if (job->alive_progs == job->stopped_progs)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004459 status_string = "Stopped";
4460 else
4461 status_string = "Running";
4462
4463 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
4464 }
4465 return EXIT_SUCCESS;
4466}
4467#endif
4468
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004469static int builtin_pwd(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004470{
4471 puts(set_cwd());
4472 return EXIT_SUCCESS;
4473}
4474
4475static int builtin_read(char **argv)
4476{
4477 char *string;
4478 const char *name = argv[1] ? argv[1] : "REPLY";
4479
4480 string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL);
4481 return set_local_var(string, 0);
4482}
4483
4484/* built-in 'set [VAR=value]' handler */
4485static int builtin_set(char **argv)
4486{
4487 char *temp = argv[1];
4488 struct variable *e;
4489
4490 if (temp == NULL)
Denis Vlasenko87a86552008-07-29 19:43:10 +00004491 for (e = G.top_var; e; e = e->next)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004492 puts(e->varstr);
4493 else
4494 set_local_var(xstrdup(temp), 0);
4495
4496 return EXIT_SUCCESS;
4497}
4498
4499static int builtin_shift(char **argv)
4500{
4501 int n = 1;
4502 if (argv[1]) {
4503 n = atoi(argv[1]);
4504 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004505 if (n >= 0 && n < G.global_argc) {
4506 G.global_argv[n] = G.global_argv[0];
4507 G.global_argc -= n;
4508 G.global_argv += n;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004509 return EXIT_SUCCESS;
4510 }
4511 return EXIT_FAILURE;
4512}
4513
4514static int builtin_source(char **argv)
4515{
4516 FILE *input;
4517 int status;
4518
4519 if (argv[1] == NULL)
4520 return EXIT_FAILURE;
4521
4522 /* XXX search through $PATH is missing */
Denis Vlasenko5415c852008-07-21 23:05:26 +00004523 input = fopen_for_read(argv[1]);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004524 if (!input) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +00004525 bb_error_msg("can't open '%s'", argv[1]);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004526 return EXIT_FAILURE;
4527 }
4528 close_on_exec_on(fileno(input));
4529
4530 /* Now run the file */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004531 /* XXX argv and argc are broken; need to save old G.global_argv
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004532 * (pointer only is OK!) on this stack frame,
Denis Vlasenko87a86552008-07-29 19:43:10 +00004533 * set G.global_argv=argv+1, recurse, and restore. */
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004534 status = parse_and_run_file(input);
4535 fclose(input);
4536 return status;
4537}
4538
4539static int builtin_umask(char **argv)
4540{
4541 mode_t new_umask;
4542 const char *arg = argv[1];
4543 char *end;
4544 if (arg) {
4545 new_umask = strtoul(arg, &end, 8);
4546 if (*end != '\0' || end == arg) {
4547 return EXIT_FAILURE;
4548 }
4549 } else {
4550 new_umask = umask(0);
4551 printf("%.3o\n", (unsigned) new_umask);
4552 }
4553 umask(new_umask);
4554 return EXIT_SUCCESS;
4555}
4556
4557static int builtin_unset(char **argv)
4558{
4559 /* bash always returns true */
4560 unset_local_var(argv[1]);
4561 return EXIT_SUCCESS;
4562}
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004563
Denis Vlasenkodadfb492008-07-29 10:16:05 +00004564#if ENABLE_HUSH_LOOPS
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004565static int builtin_break(char **argv)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004566{
Denis Vlasenko87a86552008-07-29 19:43:10 +00004567 if (G.depth_of_loop == 0) {
Denis Vlasenko4f504a92008-07-29 19:48:30 +00004568 bb_error_msg("%s: only meaningful in a loop", argv[0]);
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00004569 return EXIT_SUCCESS; /* bash compat */
4570 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004571 G.flag_break_continue++; /* BC_BREAK = 1 */
4572 G.depth_break_continue = 1;
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004573 if (argv[1]) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004574 G.depth_break_continue = bb_strtou(argv[1], NULL, 10);
4575 if (errno || !G.depth_break_continue || argv[2]) {
Denis Vlasenko4f504a92008-07-29 19:48:30 +00004576 bb_error_msg("%s: bad arguments", argv[0]);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004577 G.flag_break_continue = BC_BREAK;
4578 G.depth_break_continue = UINT_MAX;
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004579 }
4580 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004581 if (G.depth_of_loop < G.depth_break_continue)
4582 G.depth_break_continue = G.depth_of_loop;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004583 return EXIT_SUCCESS;
4584}
4585
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004586static int builtin_continue(char **argv)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004587{
Denis Vlasenko4f504a92008-07-29 19:48:30 +00004588 G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */
4589 return builtin_break(argv);
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004590}
Denis Vlasenkodadfb492008-07-29 10:16:05 +00004591#endif