blob: b623504047f9e1ca555081a897748db72263ed66 [file] [log] [blame]
Eric Andersen25f27032001-04-26 23:22:31 +00001/* vi: set sw=4 ts=4: */
2/*
3 * sh.c -- a prototype Bourne shell grammar parser
4 * Intended to follow the original Thompson and Ritchie
5 * "small and simple is beautiful" philosophy, which
6 * incidentally is a good match to today's BusyBox.
7 *
8 * Copyright (C) 2000,2001 Larry Doolittle <larry@doolittle.boa.org>
9 *
10 * Credits:
11 * The parser routines proper are all original material, first
Eric Andersencb81e642003-07-14 21:21:08 +000012 * written Dec 2000 and Jan 2001 by Larry Doolittle. The
13 * execution engine, the builtins, and much of the underlying
14 * support has been adapted from busybox-0.49pre's lash, which is
Eric Andersenc7bda1c2004-03-15 08:29:22 +000015 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersencb81e642003-07-14 21:21:08 +000016 * written by Erik Andersen <andersen@codepoet.org>. That, in turn,
17 * is based in part on ladsh.c, by Michael K. Johnson and Erik W.
18 * Troan, which they placed in the public domain. I don't know
19 * how much of the Johnson/Troan code has survived the repeated
20 * rewrites.
21 *
Eric Andersen25f27032001-04-26 23:22:31 +000022 * Other credits:
Denis Vlasenkoa8442002008-06-14 11:00:17 +000023 * o_addchr() derived from similar w_addchar function in glibc-2.2.
Eric Andersen25f27032001-04-26 23:22:31 +000024 * setup_redirect(), redirect_opt_num(), and big chunks of main()
Denis Vlasenko55b2de72007-04-18 17:21:28 +000025 * and many builtins derived from contributions by Erik Andersen
Denis Vlasenkoa8442002008-06-14 11:00:17 +000026 * miscellaneous bugfixes from Matt Kraai.
Eric Andersen25f27032001-04-26 23:22:31 +000027 *
28 * There are two big (and related) architecture differences between
29 * this parser and the lash parser. One is that this version is
30 * actually designed from the ground up to understand nearly all
31 * of the Bourne grammar. The second, consequential change is that
32 * the parser and input reader have been turned inside out. Now,
33 * the parser is in control, and asks for input as needed. The old
34 * way had the input reader in control, and it asked for parsing to
35 * take place as needed. The new way makes it much easier to properly
36 * handle the recursion implicit in the various substitutions, especially
37 * across continuation lines.
38 *
39 * Bash grammar not implemented: (how many of these were in original sh?)
Eric Andersen25f27032001-04-26 23:22:31 +000040 * $_
Eric Andersen25f27032001-04-26 23:22:31 +000041 * &> and >& redirection of stdout+stderr
42 * Brace Expansion
43 * Tilde Expansion
44 * fancy forms of Parameter Expansion
Eric Andersen78a7c992001-05-15 16:30:25 +000045 * aliases
Eric Andersen25f27032001-04-26 23:22:31 +000046 * Arithmetic Expansion
47 * <(list) and >(list) Process Substitution
Denis Vlasenko9af22c72008-10-09 12:54:58 +000048 * reserved words: select, function
Eric Andersen25f27032001-04-26 23:22:31 +000049 * Here Documents ( << word )
50 * Functions
51 * Major bugs:
Denis Vlasenkob81b3df2007-04-28 16:48:04 +000052 * job handling woefully incomplete and buggy (improved --vda)
Eric Andersen25f27032001-04-26 23:22:31 +000053 * to-do:
Eric Andersen83a2ae22001-05-07 17:59:25 +000054 * port selected bugfixes from post-0.49 busybox lash - done?
Eric Andersen83a2ae22001-05-07 17:59:25 +000055 * change { and } from special chars to reserved words
Denis Vlasenkobcb25532008-07-28 23:04:34 +000056 * builtins: return, trap, ulimit
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +000057 * test magic exec with redirection only
Eric Andersen25f27032001-04-26 23:22:31 +000058 * check setting of global_argc and global_argv
Eric Andersen25f27032001-04-26 23:22:31 +000059 * follow IFS rules more precisely, including update semantics
Eric Andersen25f27032001-04-26 23:22:31 +000060 * figure out what to do with backslash-newline
Eric Andersen25f27032001-04-26 23:22:31 +000061 * propagate syntax errors, die on resource errors?
62 * continuation lines, both explicit and implicit - done?
63 * memory leak finding and plugging - done?
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +000064 * maybe change charmap[] to use 2-bit entries
Eric Andersen25f27032001-04-26 23:22:31 +000065 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000066 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen25f27032001-04-26 23:22:31 +000067 */
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000068
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000069#include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */
Denis Vlasenkobe709c22008-07-28 00:01:16 +000070#include <glob.h>
71/* #include <dmalloc.h> */
72#if ENABLE_HUSH_CASE
73#include <fnmatch.h>
74#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +000075
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +000076#define HUSH_VER_STR "0.91"
Denis Vlasenkod01ff132007-05-02 21:40:23 +000077
Denis Vlasenko05743d72008-02-10 12:10:08 +000078#if !BB_MMU && ENABLE_HUSH_TICK
79//#undef ENABLE_HUSH_TICK
80//#define ENABLE_HUSH_TICK 0
81#warning On NOMMU, hush command substitution is dangerous.
82#warning Dont use it for commands which produce lots of output.
83#warning For more info see shell/hush.c, generate_stream_from_list().
84#endif
85
86#if !BB_MMU && ENABLE_HUSH_JOB
87#undef ENABLE_HUSH_JOB
88#define ENABLE_HUSH_JOB 0
89#endif
90
91#if !ENABLE_HUSH_INTERACTIVE
92#undef ENABLE_FEATURE_EDITING
93#define ENABLE_FEATURE_EDITING 0
94#undef ENABLE_FEATURE_EDITING_FANCY_PROMPT
95#define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0
Denis Vlasenko8412d792007-10-01 09:59:47 +000096#endif
97
98
Denis Vlasenkod01ff132007-05-02 21:40:23 +000099/* If you comment out one of these below, it will be #defined later
100 * to perform debug printfs to stderr: */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000101#define debug_printf(...) do {} while (0)
Denis Vlasenko400c5b62007-05-04 13:07:27 +0000102/* Finer-grained debug switches */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000103#define debug_printf_parse(...) do {} while (0)
104#define debug_print_tree(a, b) do {} while (0)
105#define debug_printf_exec(...) do {} while (0)
106#define debug_printf_jobs(...) do {} while (0)
107#define debug_printf_expand(...) do {} while (0)
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000108#define debug_printf_glob(...) do {} while (0)
109#define debug_printf_list(...) do {} while (0)
Denis Vlasenko30c9cc52008-06-17 07:24:29 +0000110#define debug_printf_subst(...) do {} while (0)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000111#define debug_printf_clean(...) do {} while (0)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000112
113#ifndef debug_printf
114#define debug_printf(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000115#endif
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000116
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000117#ifndef debug_printf_parse
118#define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000119#endif
120
121#ifndef debug_printf_exec
122#define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__)
123#endif
124
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000125#ifndef debug_printf_jobs
126#define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000127#define DEBUG_JOBS 1
128#else
129#define DEBUG_JOBS 0
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000130#endif
131
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000132#ifndef debug_printf_expand
133#define debug_printf_expand(...) fprintf(stderr, __VA_ARGS__)
134#define DEBUG_EXPAND 1
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000135#else
136#define DEBUG_EXPAND 0
137#endif
138
139#ifndef debug_printf_glob
140#define debug_printf_glob(...) fprintf(stderr, __VA_ARGS__)
141#define DEBUG_GLOB 1
142#else
143#define DEBUG_GLOB 0
144#endif
145
146#ifndef debug_printf_list
147#define debug_printf_list(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000148#endif
149
Denis Vlasenko30c9cc52008-06-17 07:24:29 +0000150#ifndef debug_printf_subst
151#define debug_printf_subst(...) fprintf(stderr, __VA_ARGS__)
152#endif
153
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000154#ifndef debug_printf_clean
155/* broken, of course, but OK for testing */
156static const char *indenter(int i)
157{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000158 static const char blanks[] ALIGN1 =
159 " ";
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000160 return &blanks[sizeof(blanks) - i - 1];
161}
162#define debug_printf_clean(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000163#define DEBUG_CLEAN 1
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000164#endif
165
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000166#if DEBUG_EXPAND
167static void debug_print_strings(const char *prefix, char **vv)
168{
169 fprintf(stderr, "%s:\n", prefix);
170 while (*vv)
171 fprintf(stderr, " '%s'\n", *vv++);
172}
173#else
174#define debug_print_strings(prefix, vv) ((void)0)
175#endif
176
Denis Vlasenkof962a032007-11-23 12:50:54 +0000177/*
178 * Leak hunting. Use hush_leaktool.sh for post-processing.
179 */
180#ifdef FOR_HUSH_LEAKTOOL
Denis Vlasenkoccce59d2008-06-16 14:35:57 +0000181/* suppress "warning: no previous prototype..." */
182void *xxmalloc(int lineno, size_t size);
183void *xxrealloc(int lineno, void *ptr, size_t size);
184char *xxstrdup(int lineno, const char *str);
185void xxfree(void *ptr);
Denis Vlasenkof962a032007-11-23 12:50:54 +0000186void *xxmalloc(int lineno, size_t size)
187{
188 void *ptr = xmalloc((size + 0xff) & ~0xff);
189 fprintf(stderr, "line %d: malloc %p\n", lineno, ptr);
190 return ptr;
191}
192void *xxrealloc(int lineno, void *ptr, size_t size)
193{
194 ptr = xrealloc(ptr, (size + 0xff) & ~0xff);
195 fprintf(stderr, "line %d: realloc %p\n", lineno, ptr);
196 return ptr;
197}
198char *xxstrdup(int lineno, const char *str)
199{
200 char *ptr = xstrdup(str);
201 fprintf(stderr, "line %d: strdup %p\n", lineno, ptr);
202 return ptr;
203}
204void xxfree(void *ptr)
205{
206 fprintf(stderr, "free %p\n", ptr);
207 free(ptr);
208}
209#define xmalloc(s) xxmalloc(__LINE__, s)
210#define xrealloc(p, s) xxrealloc(__LINE__, p, s)
211#define xstrdup(s) xxstrdup(__LINE__, s)
212#define free(p) xxfree(p)
213#endif
214
215
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000216/* Keep unconditionally on for now */
217#define HUSH_DEBUG 1
218/* Do we support ANY keywords? */
Denis Vlasenkod91afa32008-07-29 11:10:01 +0000219#if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000220#define HAS_KEYWORDS 1
221#define IF_HAS_KEYWORDS(...) __VA_ARGS__
222#define IF_HAS_NO_KEYWORDS(...)
223#else
224#define HAS_KEYWORDS 0
225#define IF_HAS_KEYWORDS(...)
226#define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__
227#endif
228
229
Denis Vlasenko5703c222008-06-15 11:49:42 +0000230#define SPECIAL_VAR_SYMBOL 3
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000231#define PARSEFLAG_EXIT_FROM_LOOP 1
Eric Andersen25f27032001-04-26 23:22:31 +0000232
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000233typedef enum redir_type {
Eric Andersen25f27032001-04-26 23:22:31 +0000234 REDIRECT_INPUT = 1,
235 REDIRECT_OVERWRITE = 2,
236 REDIRECT_APPEND = 3,
237 REDIRECT_HEREIS = 4,
238 REDIRECT_IO = 5
239} redir_type;
240
Denis Vlasenko55789c62008-06-18 16:30:42 +0000241/* The descrip member of this structure is only used to make
242 * debugging output pretty */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000243static const struct {
244 int mode;
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000245 signed char default_fd;
246 char descrip[3];
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000247} redir_table[] = {
Eric Andersen25f27032001-04-26 23:22:31 +0000248 { 0, 0, "()" },
249 { O_RDONLY, 0, "<" },
250 { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
251 { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" },
252 { O_RDONLY, -1, "<<" },
253 { O_RDWR, 1, "<>" }
254};
255
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000256typedef enum pipe_style {
Eric Andersen25f27032001-04-26 23:22:31 +0000257 PIPE_SEQ = 1,
258 PIPE_AND = 2,
259 PIPE_OR = 3,
260 PIPE_BG = 4,
261} pipe_style;
262
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000263typedef enum reserved_style {
Eric Andersen25f27032001-04-26 23:22:31 +0000264 RES_NONE = 0,
Denis Vlasenko06810332007-05-21 23:30:54 +0000265#if ENABLE_HUSH_IF
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000266 RES_IF ,
267 RES_THEN ,
268 RES_ELIF ,
269 RES_ELSE ,
270 RES_FI ,
Denis Vlasenko06810332007-05-21 23:30:54 +0000271#endif
272#if ENABLE_HUSH_LOOPS
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000273 RES_FOR ,
274 RES_WHILE ,
275 RES_UNTIL ,
276 RES_DO ,
277 RES_DONE ,
Denis Vlasenkod91afa32008-07-29 11:10:01 +0000278#endif
279#if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000280 RES_IN ,
Denis Vlasenko06810332007-05-21 23:30:54 +0000281#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000282#if ENABLE_HUSH_CASE
283 RES_CASE ,
284 /* two pseudo-keywords support contrived "case" syntax: */
285 RES_MATCH , /* "word)" */
286 RES_CASEI , /* "this command is inside CASE" */
287 RES_ESAC ,
288#endif
289 RES_XXXX ,
290 RES_SNTX
Eric Andersen25f27032001-04-26 23:22:31 +0000291} reserved_style;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000292
Eric Andersen25f27032001-04-26 23:22:31 +0000293struct redir_struct {
Denis Vlasenko55789c62008-06-18 16:30:42 +0000294 struct redir_struct *next;
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000295 char *rd_filename; /* filename */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000296 int fd; /* file descriptor being redirected */
297 int dup; /* -1, or file descriptor being duplicated */
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000298 smallint /*enum redir_type*/ rd_type;
Eric Andersen25f27032001-04-26 23:22:31 +0000299};
300
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000301struct command {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000302 pid_t pid; /* 0 if exited */
Denis Vlasenko2b576b82008-08-04 00:46:07 +0000303 int assignment_cnt; /* how many argv[i] are assignments? */
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000304 smallint is_stopped; /* is the command currently running? */
Denis Vlasenko55789c62008-06-18 16:30:42 +0000305 smallint subshell; /* flag, non-zero if group must be forked */
Denis Vlasenko2b576b82008-08-04 00:46:07 +0000306 struct pipe *group; /* if non-NULL, this "prog" is {} group,
307 * subshell, or a compound statement */
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000308 char **argv; /* command name and arguments */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000309 struct redir_struct *redirects; /* I/O redirections */
Eric Andersen25f27032001-04-26 23:22:31 +0000310};
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000311/* argv vector may contain variable references (^Cvar^C, ^C0^C etc)
312 * and on execution these are substituted with their values.
313 * Substitution can make _several_ words out of one argv[n]!
314 * Example: argv[0]=='.^C*^C.' here: echo .$*.
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000315 * References of the form ^C`cmd arg^C are `cmd arg` substitutions.
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000316 */
Eric Andersen25f27032001-04-26 23:22:31 +0000317
318struct pipe {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000319 struct pipe *next;
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000320 int num_cmds; /* total number of commands in job */
321 int alive_cmds; /* number of commands running (not exited) */
322 int stopped_cmds; /* number of commands alive, but stopped */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000323#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000324 int jobid; /* job number */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000325 pid_t pgrp; /* process group ID for the job */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000326 char *cmdtext; /* name of job */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000327#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000328 struct command *cmds; /* array of commands in pipe */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000329 smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
Denis Vlasenko5ec61322008-06-24 00:50:07 +0000330 IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */
331 IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */
Eric Andersen25f27032001-04-26 23:22:31 +0000332};
333
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000334/* This holds pointers to the various results of parsing */
335struct parse_context {
336 struct command *command;
337 struct pipe *list_head;
338 struct pipe *pipe;
339 struct redir_struct *pending_redirect;
340#if HAS_KEYWORDS
341 smallint ctx_res_w;
342 smallint ctx_inverted; /* "! cmd | cmd" */
343#if ENABLE_HUSH_CASE
344 smallint ctx_dsemicolon; /* ";;" seen */
345#endif
346 int old_flag; /* bitmask of FLAG_xxx, for figuring out valid reserved words */
347 struct parse_context *stack;
348#endif
349};
350
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000351/* On program start, environ points to initial environment.
352 * putenv adds new pointers into it, unsetenv removes them.
353 * Neither of these (de)allocates the strings.
354 * setenv allocates new strings in malloc space and does putenv,
355 * and thus setenv is unusable (leaky) for shell's purposes */
356#define setenv(...) setenv_is_leaky_dont_use()
357struct variable {
358 struct variable *next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000359 char *varstr; /* points to "name=" portion */
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000360 int max_len; /* if > 0, name is part of initial env; else name is malloced */
361 smallint flg_export; /* putenv should be done on this var */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000362 smallint flg_read_only;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000363};
364
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000365typedef struct o_string {
Eric Andersen25f27032001-04-26 23:22:31 +0000366 char *data;
Denis Vlasenko17f02e72008-07-14 04:32:29 +0000367 int length; /* position where data is appended */
Eric Andersen25f27032001-04-26 23:22:31 +0000368 int maxlen;
Denis Vlasenko324a3fd2008-06-18 17:49:58 +0000369 /* Misnomer! it's not "quoting", it's "protection against globbing"!
370 * (by prepending \ to *, ?, [ and to \ too) */
Denis Vlasenkoff097622007-10-01 10:00:45 +0000371 smallint o_quote;
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000372 smallint o_glob;
Denis Vlasenkoff097622007-10-01 10:00:45 +0000373 smallint nonnull;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000374 smallint has_empty_slot;
Denis Vlasenko55789c62008-06-18 16:30:42 +0000375 smallint o_assignment; /* 0:maybe, 1:yes, 2:no */
Eric Andersen25f27032001-04-26 23:22:31 +0000376} o_string;
Denis Vlasenko55789c62008-06-18 16:30:42 +0000377enum {
378 MAYBE_ASSIGNMENT = 0,
379 DEFINITELY_ASSIGNMENT = 1,
380 NOT_ASSIGNMENT = 2,
Denis Vlasenko2b576b82008-08-04 00:46:07 +0000381 WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */
Denis Vlasenko55789c62008-06-18 16:30:42 +0000382};
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000383/* Used for initialization: o_string foo = NULL_O_STRING; */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000384#define NULL_O_STRING { NULL }
Eric Andersen25f27032001-04-26 23:22:31 +0000385
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000386/* I can almost use ordinary FILE*. Is open_memstream() universally
Eric Andersen25f27032001-04-26 23:22:31 +0000387 * available? Where is it documented? */
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000388typedef struct in_str {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +0000389 const char *p;
390 /* eof_flag=1: last char in ->p is really an EOF */
391 char eof_flag; /* meaningless if ->p == NULL */
392 char peek_buf[2];
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000393#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000394 smallint promptme;
395 smallint promptmode; /* 0: PS1, 1: PS2 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000396#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000397 FILE *file;
398 int (*get) (struct in_str *);
399 int (*peek) (struct in_str *);
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000400} in_str;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000401#define i_getch(input) ((input)->get(input))
402#define i_peek(input) ((input)->peek(input))
Eric Andersen25f27032001-04-26 23:22:31 +0000403
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000404enum {
405 CHAR_ORDINARY = 0,
406 CHAR_ORDINARY_IF_QUOTED = 1, /* example: *, # */
407 CHAR_IFS = 2, /* treated as ordinary if quoted */
408 CHAR_SPECIAL = 3, /* example: $ */
Eric Andersen25f27032001-04-26 23:22:31 +0000409};
410
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000411enum {
412 BC_BREAK = 1,
413 BC_CONTINUE = 2,
414};
415
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000416
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000417/* "Globals" within this file */
418
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000419/* Sorted roughly by size (smaller offsets == smaller code) */
420struct globals {
421#if ENABLE_HUSH_INTERACTIVE
422 /* 'interactive_fd' is a fd# open to ctty, if we have one
423 * _AND_ if we decided to act interactively */
424 int interactive_fd;
425 const char *PS1;
426 const char *PS2;
427#endif
428#if ENABLE_FEATURE_EDITING
429 line_input_t *line_input_state;
430#endif
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +0000431 pid_t root_pid;
Denis Vlasenko87a86552008-07-29 19:43:10 +0000432 pid_t last_bg_pid;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000433#if ENABLE_HUSH_JOB
434 int run_list_level;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000435 pid_t saved_tty_pgrp;
436 int last_jobid;
437 struct pipe *job_list;
438 struct pipe *toplevel_list;
439 smallint ctrl_z_flag;
440#endif
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000441#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000442 smallint flag_break_continue;
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000443#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000444 smallint fake_mode;
445 /* these three support $?, $#, and $1 */
Denis Vlasenko5e052ca2008-07-28 15:15:09 +0000446 smalluint last_return_code;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000447 char **global_argv;
448 int global_argc;
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000449#if ENABLE_HUSH_LOOPS
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +0000450 unsigned depth_break_continue;
Denis Vlasenkofcf37c32008-07-29 11:37:15 +0000451 unsigned depth_of_loop;
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000452#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000453 const char *ifs;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000454 const char *cwd;
Denis Vlasenko87a86552008-07-29 19:43:10 +0000455 struct variable *top_var; /* = &G.shell_ver (set in main()) */
Denis Vlasenko0a83fc32007-05-25 11:12:32 +0000456 struct variable shell_ver;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000457#if ENABLE_FEATURE_SH_STANDALONE
458 struct nofork_save_area nofork_save;
459#endif
460#if ENABLE_HUSH_JOB
461 sigjmp_buf toplevel_jb;
462#endif
463 unsigned char charmap[256];
464 char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2];
465};
466
467#define G (*ptr_to_globals)
Denis Vlasenko87a86552008-07-29 19:43:10 +0000468/* Not #defining name to G.name - this quickly gets unwieldy
469 * (too many defines). Also, I actually prefer to see when a variable
470 * is global, thus "G." prefix is a useful hint */
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000471#define INIT_G() do { \
472 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
473} while (0)
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000474
475
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000476#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
477
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000478#if 1
479/* Normal */
480static void syntax(const char *msg)
481{
Denis Vlasenko87a86552008-07-29 19:43:10 +0000482#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000483 /* Was using fancy stuff:
Denis Vlasenko87a86552008-07-29 19:43:10 +0000484 * (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...)
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000485 * but it SEGVs. ?! Oh well... explicit temp ptr works around that */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000486 void FAST_FUNC (*fp)(const char *s, ...);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000487 fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000488 fp(msg ? "%s: %s" : "syntax error", "syntax error", msg);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000489#else
490 bb_error_msg_and_die(msg ? "%s: %s" : "syntax error", "syntax error", msg);
491#endif
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000492}
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000493
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000494#else
495/* Debug */
496static void syntax_lineno(int line)
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000497{
Denis Vlasenko87a86552008-07-29 19:43:10 +0000498#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkoff182a32008-07-05 20:29:59 +0000499 void FAST_FUNC (*fp)(const char *s, ...);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000500 fp = (G.interactive_fd ? bb_error_msg : bb_error_msg_and_die);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000501 fp("syntax error hush.c:%d", line);
Denis Vlasenko87a86552008-07-29 19:43:10 +0000502#else
503 bb_error_msg_and_die("syntax error hush.c:%d", line);
504#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000505}
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000506#define syntax(str) syntax_lineno(__LINE__)
507#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000508
509/* Index of subroutines: */
Eric Andersen25f27032001-04-26 23:22:31 +0000510/* in_str manipulations: */
511static int static_get(struct in_str *i);
512static int static_peek(struct in_str *i);
513static int file_get(struct in_str *i);
514static int file_peek(struct in_str *i);
515static void setup_file_in_str(struct in_str *i, FILE *f);
516static void setup_string_in_str(struct in_str *i, const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000517/* "run" the final data structures: */
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000518#if !defined(DEBUG_CLEAN)
519#define free_pipe_list(head, indent) free_pipe_list(head)
520#define free_pipe(pi, indent) free_pipe(pi)
521#endif
Eric Andersenbf7df042001-05-23 22:18:35 +0000522static int free_pipe_list(struct pipe *head, int indent);
523static int free_pipe(struct pipe *pi, int indent);
Eric Andersen25f27032001-04-26 23:22:31 +0000524/* really run the final data structures: */
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000525static int setup_redirects(struct command *prog, int squirrel[]);
Denis Vlasenko05743d72008-02-10 12:10:08 +0000526static int run_list(struct pipe *pi);
Denis Vlasenko76d50412008-06-10 16:19:39 +0000527#if BB_MMU
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000528#define pseudo_exec_argv(ptr_ptrs2free, argv, assignment_cnt, argv_expanded) \
Denis Vlasenko2b576b82008-08-04 00:46:07 +0000529 pseudo_exec_argv(argv, assignment_cnt, argv_expanded)
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000530#define pseudo_exec(ptr_ptrs2free, command, argv_expanded) \
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000531 pseudo_exec(command, argv_expanded)
Denis Vlasenko76d50412008-06-10 16:19:39 +0000532#endif
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000533static void pseudo_exec_argv(char ***ptr_ptrs2free, char **argv, int assignment_cnt, char **argv_expanded) NORETURN;
534static void pseudo_exec(char ***ptr_ptrs2free, struct command *command, char **argv_expanded) NORETURN;
Denis Vlasenko05743d72008-02-10 12:10:08 +0000535static int run_pipe(struct pipe *pi);
Eric Andersen25f27032001-04-26 23:22:31 +0000536/* data structure manipulation: */
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000537static int setup_redirect(struct parse_context *ctx, int fd, redir_type style, struct in_str *input);
538static void initialize_context(struct parse_context *ctx);
539static int done_word(o_string *dest, struct parse_context *ctx);
540static int done_command(struct parse_context *ctx);
541static void done_pipe(struct parse_context *ctx, pipe_style type);
Eric Andersen25f27032001-04-26 23:22:31 +0000542/* primary string parsing: */
543static int redirect_dup_num(struct in_str *input);
544static int redirect_opt_num(o_string *o);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000545#if ENABLE_HUSH_TICK
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000546static int process_command_subs(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000547 struct in_str *input, const char *subst_end);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000548#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000549static int parse_group(o_string *dest, struct parse_context *ctx, struct in_str *input, int ch);
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000550static const char *lookup_param(const char *src);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000551static int handle_dollar(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000552 struct in_str *input);
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000553static int parse_stream(o_string *dest, struct parse_context *ctx, struct in_str *input0, const char *end_trigger);
Eric Andersen25f27032001-04-26 23:22:31 +0000554/* setup: */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000555static int parse_and_run_stream(struct in_str *inp, int parse_flag);
556static int parse_and_run_string(const char *s, int parse_flag);
557static int parse_and_run_file(FILE *f);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000558/* job management: */
Eric Andersenc798b072001-06-22 06:23:03 +0000559static int checkjobs(struct pipe* fg_pipe);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000560#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +0000561static int checkjobs_and_fg_shell(struct pipe* fg_pipe);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000562static void insert_bg_job(struct pipe *pi);
563static void remove_bg_job(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000564static void delete_finished_bg_job(struct pipe *pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000565#else
566int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */
567#endif
Eric Andersenf72f5622001-05-15 23:21:41 +0000568/* local variable support */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000569static char **expand_strvec_to_strvec(char **argv);
570/* used for eval */
571static char *expand_strvec_to_string(char **argv);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +0000572/* used for expansion of right hand of assignments */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000573static char *expand_string_to_string(const char *str);
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000574static struct variable *get_local_var(const char *name);
575static int set_local_var(char *str, int flg_export);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000576static void unset_local_var(const char *name);
Eric Andersen25f27032001-04-26 23:22:31 +0000577
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000578
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000579static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="HUSH_VER_STR;
580
581
Denis Vlasenkob61e13d2008-06-17 05:11:43 +0000582static int glob_needed(const char *s)
583{
584 while (*s) {
585 if (*s == '\\')
586 s++;
587 if (*s == '*' || *s == '[' || *s == '?')
588 return 1;
589 s++;
590 }
591 return 0;
592}
593
Denis Vlasenko30c9cc52008-06-17 07:24:29 +0000594static int is_assignment(const char *s)
595{
Denis Vlasenkod4981312008-07-31 10:34:48 +0000596 if (!s || !(isalpha(*s) || *s == '_'))
Denis Vlasenko30c9cc52008-06-17 07:24:29 +0000597 return 0;
598 s++;
599 while (isalnum(*s) || *s == '_')
600 s++;
601 return *s == '=';
602}
603
Denis Vlasenko55789c62008-06-18 16:30:42 +0000604/* Replace each \x with x in place, return ptr past NUL. */
605static char *unbackslash(char *src)
606{
607 char *dst = src;
608 while (1) {
609 if (*src == '\\')
610 src++;
611 if ((*dst++ = *src++) == '\0')
612 break;
613 }
614 return dst;
615}
616
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000617static char **add_strings_to_strings(char **strings, char **add)
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000618{
619 int i;
620 unsigned count1;
621 unsigned count2;
622 char **v;
623
624 v = strings;
625 count1 = 0;
626 if (v) {
627 while (*v) {
628 count1++;
629 v++;
630 }
631 }
632 count2 = 0;
633 v = add;
634 while (*v) {
635 count2++;
636 v++;
637 }
638 v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*));
639 v[count1 + count2] = NULL;
640 i = count2;
641 while (--i >= 0)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000642 v[count1 + i] = add[i];
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000643 return v;
644}
645
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000646static char **add_string_to_strings(char **strings, char *add)
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000647{
648 char *v[2];
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000649 v[0] = add;
650 v[1] = NULL;
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000651 return add_strings_to_strings(strings, v);
652}
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000653
Denis Vlasenko22d10a02008-10-13 08:53:43 +0000654static void putenv_all(char **strings)
655{
656 if (!strings)
657 return;
658 while (*strings)
659 putenv(*strings++);
660}
661
662static char **putenv_all_and_save_old(char **strings)
663{
664 char **old = NULL;
665 char **s = strings;
666
667 if (!strings)
668 return old;
669 while (*strings) {
670 char *v = getenv(*strings++);
671 if (!v)
672 continue;
673 old = add_string_to_strings(old, v);
674 }
675 putenv_all(s);
676 return old;
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000677}
678
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000679static void free_strings_and_unsetenv(char **strings, int unset)
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000680{
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000681 char **v;
682
683 if (!strings)
684 return;
685
686 v = strings;
687 while (*v) {
688 if (unset) {
689 char *copy;
690#if !BB_MMU
691 /* If this is a magic guard pointer, do not free it,
692 * and stop unsetting */
693 if (*v == hush_version_str) {
694 unset = 0;
695 v++;
696 continue;
697 }
698#endif
699 /* *strchrnul(*v, '=') = '\0'; -- BAD
700 * In case *v was putenv'ed, we can't
701 * unsetenv(*v) after taking out '=':
702 * it won't work, env is modified by taking out!
703 * horror :( */
704 copy = xstrndup(*v, strchrnul(*v, '=') - *v);
705 unsetenv(copy);
706 free(copy);
707 }
708 free(*v++);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000709 }
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000710 free(strings);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000711}
712
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000713static void free_strings(char **strings)
Denis Vlasenko76d50412008-06-10 16:19:39 +0000714{
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +0000715 free_strings_and_unsetenv(strings, 0);
Denis Vlasenko76d50412008-06-10 16:19:39 +0000716}
Denis Vlasenko76d50412008-06-10 16:19:39 +0000717
718
Denis Vlasenko83506862007-11-23 13:11:42 +0000719/* Function prototypes for builtins */
720static int builtin_cd(char **argv);
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000721static int builtin_echo(char **argv);
Denis Vlasenko83506862007-11-23 13:11:42 +0000722static int builtin_eval(char **argv);
723static int builtin_exec(char **argv);
724static int builtin_exit(char **argv);
725static int builtin_export(char **argv);
726#if ENABLE_HUSH_JOB
727static int builtin_fg_bg(char **argv);
728static int builtin_jobs(char **argv);
729#endif
730#if ENABLE_HUSH_HELP
731static int builtin_help(char **argv);
732#endif
733static int builtin_pwd(char **argv);
734static int builtin_read(char **argv);
735static int builtin_test(char **argv);
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000736static int builtin_true(char **argv);
Denis Vlasenko83506862007-11-23 13:11:42 +0000737static int builtin_set(char **argv);
738static int builtin_shift(char **argv);
739static int builtin_source(char **argv);
740static int builtin_umask(char **argv);
741static int builtin_unset(char **argv);
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000742#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000743static int builtin_break(char **argv);
744static int builtin_continue(char **argv);
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000745#endif
Denis Vlasenko83506862007-11-23 13:11:42 +0000746//static int builtin_not_written(char **argv);
747
Eric Andersen25f27032001-04-26 23:22:31 +0000748/* Table of built-in functions. They can be forked or not, depending on
749 * context: within pipes, they fork. As simple commands, they do not.
750 * When used in non-forking context, they can change global variables
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000751 * in the parent shell process. If forked, of course they cannot.
Eric Andersen25f27032001-04-26 23:22:31 +0000752 * For example, 'unset foo | whatever' will parse and run, but foo will
753 * still be set at the end. */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000754struct built_in_command {
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000755 const char *cmd;
756 int (*function)(char **argv);
Denis Vlasenko06810332007-05-21 23:30:54 +0000757#if ENABLE_HUSH_HELP
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000758 const char *descr;
Denis Vlasenko06810332007-05-21 23:30:54 +0000759#define BLTIN(cmd, func, help) { cmd, func, help }
760#else
761#define BLTIN(cmd, func, help) { cmd, func }
762#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000763};
764
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000765/* For now, echo and test are unconditionally enabled.
766 * Maybe make it configurable? */
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +0000767static const struct built_in_command bltins[] = {
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000768 BLTIN("." , builtin_source, "Run commands in a file"),
769 BLTIN(":" , builtin_true, "No-op"),
Denis Vlasenko83506862007-11-23 13:11:42 +0000770 BLTIN("[" , builtin_test, "Test condition"),
771 BLTIN("[[" , builtin_test, "Test condition"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000772#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000773 BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000774#endif
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000775#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000776 BLTIN("break" , builtin_break, "Exit from a loop"),
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000777#endif
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000778 BLTIN("cd" , builtin_cd, "Change directory"),
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000779#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000780 BLTIN("continue", builtin_continue, "Start new loop iteration"),
Denis Vlasenkodadfb492008-07-29 10:16:05 +0000781#endif
Denis Vlasenkobcb25532008-07-28 23:04:34 +0000782 BLTIN("echo" , builtin_echo, "Write to stdout"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000783 BLTIN("eval" , builtin_eval, "Construct and run shell command"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000784 BLTIN("exec" , builtin_exec, "Execute command, don't return to shell"),
785 BLTIN("exit" , builtin_exit, "Exit"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000786 BLTIN("export", builtin_export, "Set environment variable"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000787#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000788 BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000789 BLTIN("jobs" , builtin_jobs, "List active jobs"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000790#endif
Denis Vlasenko06810332007-05-21 23:30:54 +0000791 BLTIN("pwd" , builtin_pwd, "Print current directory"),
792 BLTIN("read" , builtin_read, "Input environment variable"),
793// BLTIN("return", builtin_not_written, "Return from a function"),
794 BLTIN("set" , builtin_set, "Set/unset shell local variables"),
795 BLTIN("shift" , builtin_shift, "Shift positional parameters"),
796// BLTIN("trap" , builtin_not_written, "Trap signals"),
Denis Vlasenko83506862007-11-23 13:11:42 +0000797 BLTIN("test" , builtin_test, "Test condition"),
Denis Vlasenkodd316dd2008-06-14 15:50:55 +0000798// BLTIN("ulimit", builtin_not_written, "Control resource limits"),
799 BLTIN("umask" , builtin_umask, "Set file creation mask"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000800 BLTIN("unset" , builtin_unset, "Unset environment variable"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000801#if ENABLE_HUSH_HELP
802 BLTIN("help" , builtin_help, "List shell built-in commands"),
803#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000804};
805
Denis Vlasenkoc7985b72008-06-17 05:43:38 +0000806
Denis Vlasenko4830fc52008-05-25 21:50:55 +0000807/* Signals are grouped, we handle them in batches */
808static void set_misc_sighandler(void (*handler)(int))
809{
810 bb_signals(0
811 + (1 << SIGINT)
812 + (1 << SIGQUIT)
813 + (1 << SIGTERM)
814 , handler);
815}
816
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000817#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000818
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000819static void set_fatal_sighandler(void (*handler)(int))
820{
Denis Vlasenko25591c32008-02-16 22:58:56 +0000821 bb_signals(0
822 + (1 << SIGILL)
823 + (1 << SIGTRAP)
824 + (1 << SIGABRT)
825 + (1 << SIGFPE)
826 + (1 << SIGBUS)
827 + (1 << SIGSEGV)
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000828 /* bash 3.2 seems to handle these just like 'fatal' ones */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000829 + (1 << SIGHUP)
830 + (1 << SIGPIPE)
831 + (1 << SIGALRM)
832 , handler);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000833}
834static void set_jobctrl_sighandler(void (*handler)(int))
835{
Denis Vlasenko25591c32008-02-16 22:58:56 +0000836 bb_signals(0
837 + (1 << SIGTSTP)
838 + (1 << SIGTTIN)
839 + (1 << SIGTTOU)
840 , handler);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000841}
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000842/* SIGCHLD is special and handled separately */
843
844static void set_every_sighandler(void (*handler)(int))
845{
846 set_fatal_sighandler(handler);
847 set_jobctrl_sighandler(handler);
848 set_misc_sighandler(handler);
849 signal(SIGCHLD, handler);
850}
851
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000852static void handler_ctrl_c(int sig UNUSED_PARAM)
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000853{
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000854 debug_printf_jobs("got sig %d\n", sig);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000855// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenko87a86552008-07-29 19:43:10 +0000856 siglongjmp(G.toplevel_jb, 1);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000857}
858
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000859static void handler_ctrl_z(int sig UNUSED_PARAM)
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000860{
861 pid_t pid;
862
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000863 debug_printf_jobs("got tty sig %d in pid %d\n", sig, getpid());
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000864 pid = fork();
Denis Vlasenkoc2990322007-05-16 12:57:12 +0000865 if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000866 return;
Denis Vlasenko87a86552008-07-29 19:43:10 +0000867 G.ctrl_z_flag = 1;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000868 if (!pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +0000869 if (ENABLE_HUSH_JOB)
870 die_sleep = 0; /* let nofork's xfuncs die */
Bernhard Reutner-Fischer864329d2008-09-25 10:55:05 +0000871 bb_setpgrp();
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000872 debug_printf_jobs("set pgrp for child %d ok\n", getpid());
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000873 set_every_sighandler(SIG_DFL);
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000874 raise(SIGTSTP); /* resend TSTP so that child will be stopped */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000875 debug_printf_jobs("returning in child\n");
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000876 /* return to nofork, it will eventually exit now,
877 * not return back to shell */
878 return;
879 }
880 /* parent */
881 /* finish filling up pipe info */
Denis Vlasenko87a86552008-07-29 19:43:10 +0000882 G.toplevel_list->pgrp = pid; /* child is in its own pgrp */
Denis Vlasenko9af22c72008-10-09 12:54:58 +0000883 G.toplevel_list->cmds[0].pid = pid;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000884 /* parent needs to longjmp out of running nofork.
885 * we will "return" exitcode 0, with child put in background */
886// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000887 debug_printf_jobs("siglongjmp in parent\n");
Denis Vlasenko87a86552008-07-29 19:43:10 +0000888 siglongjmp(G.toplevel_jb, 1);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000889}
890
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000891/* Restores tty foreground process group, and exits.
892 * May be called as signal handler for fatal signal
893 * (will faithfully resend signal to itself, producing correct exit state)
894 * or called directly with -EXITCODE.
895 * We also call it if xfunc is exiting. */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000896static void sigexit(int sig) NORETURN;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000897static void sigexit(int sig)
898{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000899 /* Disable all signals: job control, SIGPIPE, etc. */
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000900 sigprocmask_allsigs(SIG_BLOCK);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000901
Denis Vlasenko87a86552008-07-29 19:43:10 +0000902#if ENABLE_HUSH_INTERACTIVE
903 if (G.interactive_fd)
904 tcsetpgrp(G.interactive_fd, G.saved_tty_pgrp);
905#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000906
907 /* Not a signal, just exit */
908 if (sig <= 0)
909 _exit(- sig);
910
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000911 kill_myself_with_sig(sig); /* does not return */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000912}
913
914/* Restores tty foreground process group, and exits. */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000915static void hush_exit(int exitcode) NORETURN;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000916static void hush_exit(int exitcode)
917{
918 fflush(NULL); /* flush all streams */
919 sigexit(- (exitcode & 0xff));
920}
921
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000922#else /* !JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000923
924#define set_fatal_sighandler(handler) ((void)0)
925#define set_jobctrl_sighandler(handler) ((void)0)
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000926#define hush_exit(e) exit(e)
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000927
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000928#endif /* JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000929
930
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000931static const char *set_cwd(void)
932{
Denis Vlasenko87a86552008-07-29 19:43:10 +0000933 if (G.cwd == bb_msg_unknown)
934 G.cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */
935 G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd);
936 if (!G.cwd)
937 G.cwd = bb_msg_unknown;
938 return G.cwd;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000939}
940
Denis Vlasenko83506862007-11-23 13:11:42 +0000941
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000942/*
943 * o_string support
944 */
945#define B_CHUNK (32 * sizeof(char*))
Eric Andersen25f27032001-04-26 23:22:31 +0000946
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000947static void o_reset(o_string *o)
Eric Andersen25f27032001-04-26 23:22:31 +0000948{
949 o->length = 0;
950 o->nonnull = 0;
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000951 if (o->data)
952 o->data[0] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +0000953}
954
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000955static void o_free(o_string *o)
Eric Andersen25f27032001-04-26 23:22:31 +0000956{
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000957 free(o->data);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000958 memset(o, 0, sizeof(*o));
Eric Andersen25f27032001-04-26 23:22:31 +0000959}
960
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000961static void o_grow_by(o_string *o, int len)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000962{
963 if (o->length + len > o->maxlen) {
964 o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK);
965 o->data = xrealloc(o->data, 1 + o->maxlen);
966 }
967}
968
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000969static void o_addchr(o_string *o, int ch)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000970{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000971 debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o);
972 o_grow_by(o, 1);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000973 o->data[o->length] = ch;
974 o->length++;
975 o->data[o->length] = '\0';
976}
977
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000978static void o_addstr(o_string *o, const char *str, int len)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000979{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +0000980 o_grow_by(o, len);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +0000981 memcpy(&o->data[o->length], str, len);
982 o->length += len;
983 o->data[o->length] = '\0';
984}
985
Denis Vlasenko55789c62008-06-18 16:30:42 +0000986static void o_addstr_duplicate_backslash(o_string *o, const char *str, int len)
987{
988 while (len) {
989 o_addchr(o, *str);
990 if (*str++ == '\\'
991 && (*str != '*' && *str != '?' && *str != '[')
992 ) {
993 o_addchr(o, '\\');
994 }
995 len--;
996 }
997}
998
Eric Andersen25f27032001-04-26 23:22:31 +0000999/* My analysis of quoting semantics tells me that state information
1000 * is associated with a destination, not a source.
1001 */
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001002static void o_addqchr(o_string *o, int ch)
Eric Andersen25f27032001-04-26 23:22:31 +00001003{
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001004 int sz = 1;
1005 if (strchr("*?[\\", ch)) {
1006 sz++;
1007 o->data[o->length] = '\\';
1008 o->length++;
Eric Andersen25f27032001-04-26 23:22:31 +00001009 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001010 o_grow_by(o, sz);
1011 o->data[o->length] = ch;
1012 o->length++;
1013 o->data[o->length] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00001014}
1015
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001016static void o_addQchr(o_string *o, int ch)
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001017{
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001018 int sz = 1;
1019 if (o->o_quote && strchr("*?[\\", ch)) {
1020 sz++;
1021 o->data[o->length] = '\\';
1022 o->length++;
1023 }
1024 o_grow_by(o, sz);
1025 o->data[o->length] = ch;
1026 o->length++;
1027 o->data[o->length] = '\0';
1028}
1029
1030static void o_addQstr(o_string *o, const char *str, int len)
1031{
1032 if (!o->o_quote) {
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001033 o_addstr(o, str, len);
1034 return;
1035 }
1036 while (len) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001037 char ch;
1038 int sz;
1039 int ordinary_cnt = strcspn(str, "*?[\\");
1040 if (ordinary_cnt > len) /* paranoia */
1041 ordinary_cnt = len;
1042 o_addstr(o, str, ordinary_cnt);
1043 if (ordinary_cnt == len)
1044 return;
1045 str += ordinary_cnt;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001046 len -= ordinary_cnt + 1; /* we are processing + 1 char below */
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001047
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001048 ch = *str++;
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001049 sz = 1;
1050 if (ch) { /* it is necessarily one of "*?[\\" */
1051 sz++;
1052 o->data[o->length] = '\\';
1053 o->length++;
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001054 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00001055 o_grow_by(o, sz);
1056 o->data[o->length] = ch;
1057 o->length++;
1058 o->data[o->length] = '\0';
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00001059 }
1060}
1061
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001062/* A special kind of o_string for $VAR and `cmd` expansion.
1063 * It contains char* list[] at the beginning, which is grown in 16 element
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001064 * increments. Actual string data starts at the next multiple of 16 * (char*).
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001065 * list[i] contains an INDEX (int!) into this string data.
1066 * It means that if list[] needs to grow, data needs to be moved higher up
1067 * but list[i]'s need not be modified.
1068 * NB: remembering how many list[i]'s you have there is crucial.
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001069 * o_finalize_list() operation post-processes this structure - calculates
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001070 * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well.
1071 */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001072#if DEBUG_EXPAND || DEBUG_GLOB
1073static void debug_print_list(const char *prefix, o_string *o, int n)
1074{
1075 char **list = (char**)o->data;
1076 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1077 int i = 0;
1078 fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n",
1079 prefix, list, n, string_start, o->length, o->maxlen);
1080 while (i < n) {
1081 fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i],
1082 o->data + (int)list[i] + string_start,
1083 o->data + (int)list[i] + string_start);
1084 i++;
1085 }
1086 if (n) {
1087 const char *p = o->data + (int)list[n - 1] + string_start;
1088 fprintf(stderr, " total_sz:%d\n", (p + strlen(p) + 1) - o->data);
1089 }
1090}
1091#else
1092#define debug_print_list(prefix, o, n) ((void)0)
1093#endif
1094
1095/* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value
1096 * in list[n] so that it points past last stored byte so far.
1097 * It returns n+1. */
1098static int o_save_ptr_helper(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001099{
1100 char **list = (char**)o->data;
Denis Vlasenko895bea22008-06-10 18:06:24 +00001101 int string_start;
1102 int string_len;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001103
1104 if (!o->has_empty_slot) {
Denis Vlasenko895bea22008-06-10 18:06:24 +00001105 string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1106 string_len = o->length - string_start;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001107 if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001108 debug_printf_list("list[%d]=%d string_start=%d (growing)\n", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001109 /* list[n] points to string_start, make space for 16 more pointers */
1110 o->maxlen += 0x10 * sizeof(list[0]);
1111 o->data = xrealloc(o->data, o->maxlen + 1);
Denis Vlasenko7049ff82008-06-25 09:53:17 +00001112 list = (char**)o->data;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001113 memmove(list + n + 0x10, list + n, string_len);
1114 o->length += 0x10 * sizeof(list[0]);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001115 } else
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001116 debug_printf_list("list[%d]=%d string_start=%d\n", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001117 } else {
1118 /* We have empty slot at list[n], reuse without growth */
Denis Vlasenko895bea22008-06-10 18:06:24 +00001119 string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */
1120 string_len = o->length - string_start;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001121 debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", n, string_len, string_start);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001122 o->has_empty_slot = 0;
1123 }
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +00001124 list[n] = (char*)(ptrdiff_t)string_len;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001125 return n + 1;
1126}
1127
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001128/* "What was our last o_save_ptr'ed position (byte offset relative o->data)?" */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001129static int o_get_last_ptr(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001130{
1131 char **list = (char**)o->data;
1132 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1133
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +00001134 return ((int)(ptrdiff_t)list[n-1]) + string_start;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001135}
1136
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001137/* o_glob performs globbing on last list[], saving each result
Denis Vlasenko55789c62008-06-18 16:30:42 +00001138 * as a new list[]. */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001139static int o_glob(o_string *o, int n)
1140{
1141 glob_t globdata;
1142 int gr;
1143 char *pattern;
1144
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001145 debug_printf_glob("start o_glob: n:%d o->data:%p\n", n, o->data);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001146 if (!o->data)
1147 return o_save_ptr_helper(o, n);
1148 pattern = o->data + o_get_last_ptr(o, n);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001149 debug_printf_glob("glob pattern '%s'\n", pattern);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001150 if (!glob_needed(pattern)) {
1151 literal:
1152 o->length = unbackslash(pattern) - o->data;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001153 debug_printf_glob("glob pattern '%s' is literal\n", pattern);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001154 return o_save_ptr_helper(o, n);
1155 }
1156
1157 memset(&globdata, 0, sizeof(globdata));
1158 gr = glob(pattern, 0, NULL, &globdata);
1159 debug_printf_glob("glob('%s'):%d\n", pattern, gr);
1160 if (gr == GLOB_NOSPACE)
1161 bb_error_msg_and_die("out of memory during glob");
1162 if (gr == GLOB_NOMATCH) {
1163 globfree(&globdata);
1164 goto literal;
1165 }
1166 if (gr != 0) { /* GLOB_ABORTED ? */
1167//TODO: testcase for bad glob pattern behavior
1168 bb_error_msg("glob(3) error %d on '%s'", gr, pattern);
1169 }
1170 if (globdata.gl_pathv && globdata.gl_pathv[0]) {
1171 char **argv = globdata.gl_pathv;
1172 o->length = pattern - o->data; /* "forget" pattern */
1173 while (1) {
1174 o_addstr(o, *argv, strlen(*argv) + 1);
1175 n = o_save_ptr_helper(o, n);
1176 argv++;
1177 if (!*argv)
1178 break;
1179 }
1180 }
1181 globfree(&globdata);
1182 if (DEBUG_GLOB)
1183 debug_print_list("o_glob returning", o, n);
1184 return n;
1185}
1186
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001187/* If o->o_glob == 1, glob the string so far remembered.
1188 * Otherwise, just finish current list[] and start new */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001189static int o_save_ptr(o_string *o, int n)
1190{
1191 if (o->o_glob)
1192 return o_glob(o, n); /* o_save_ptr_helper is inside */
1193 return o_save_ptr_helper(o, n);
1194}
1195
1196/* "Please convert list[n] to real char* ptrs, and NULL terminate it." */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00001197static char **o_finalize_list(o_string *o, int n)
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001198{
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001199 char **list;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001200 int string_start;
1201
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001202 n = o_save_ptr(o, n); /* force growth for list[n] if necessary */
1203 if (DEBUG_EXPAND)
1204 debug_print_list("finalized", o, n);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00001205 debug_printf_expand("finalized n:%d\n", n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001206 list = (char**)o->data;
1207 string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
1208 list[--n] = NULL;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001209 while (n) {
1210 n--;
Denis Vlasenkocc3f20b2008-06-23 22:31:52 +00001211 list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001212 }
1213 return list;
1214}
1215
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00001216
1217/*
1218 * in_str support
1219 */
Eric Andersen25f27032001-04-26 23:22:31 +00001220static int static_get(struct in_str *i)
1221{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001222 int ch = *i->p++;
1223 if (ch == '\0') return EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001224 return ch;
1225}
1226
1227static int static_peek(struct in_str *i)
1228{
1229 return *i->p;
1230}
1231
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001232#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001233
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001234#if ENABLE_FEATURE_EDITING
Rob Landley88621d72006-08-29 19:41:06 +00001235static void cmdedit_set_initial_prompt(void)
Eric Andersen25f27032001-04-26 23:22:31 +00001236{
Denis Vlasenko38f63192007-01-22 09:03:07 +00001237#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Denis Vlasenko87a86552008-07-29 19:43:10 +00001238 G.PS1 = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00001239#else
Denis Vlasenko87a86552008-07-29 19:43:10 +00001240 G.PS1 = getenv("PS1");
1241 if (G.PS1 == NULL)
1242 G.PS1 = "\\w \\$ ";
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001243#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001244}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001245#endif /* EDITING */
Eric Andersen25f27032001-04-26 23:22:31 +00001246
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001247static const char* setup_prompt_string(int promptmode)
Eric Andersen25f27032001-04-26 23:22:31 +00001248{
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001249 const char *prompt_str;
1250 debug_printf("setup_prompt_string %d ", promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001251#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001252 /* Set up the prompt */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001253 if (promptmode == 0) { /* PS1 */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001254 free((char*)G.PS1);
1255 G.PS1 = xasprintf("%s %c ", G.cwd, (geteuid() != 0) ? '$' : '#');
1256 prompt_str = G.PS1;
Eric Andersen25f27032001-04-26 23:22:31 +00001257 } else {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001258 prompt_str = G.PS2;
Eric Andersen25f27032001-04-26 23:22:31 +00001259 }
1260#else
Denis Vlasenko87a86552008-07-29 19:43:10 +00001261 prompt_str = (promptmode == 0) ? G.PS1 : G.PS2;
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001262#endif
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001263 debug_printf("result '%s'\n", prompt_str);
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001264 return prompt_str;
Eric Andersen25f27032001-04-26 23:22:31 +00001265}
1266
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001267static void get_user_input(struct in_str *i)
Eric Andersen25f27032001-04-26 23:22:31 +00001268{
Denis Vlasenko0937be52007-04-28 16:47:08 +00001269 int r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001270 const char *prompt_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001271
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001272 prompt_str = setup_prompt_string(i->promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001273#if ENABLE_FEATURE_EDITING
Denis Vlasenko170435c2007-05-23 13:01:10 +00001274 /* Enable command line editing only while a command line
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001275 * is actually being read */
1276 do {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001277 r = read_line_input(prompt_str, G.user_input_buf, BUFSIZ-1, G.line_input_state);
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001278 } while (r == 0); /* repeat if Ctrl-C */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001279 i->eof_flag = (r < 0);
1280 if (i->eof_flag) { /* EOF/error detected */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001281 G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */
1282 G.user_input_buf[1] = '\0';
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001283 }
Eric Andersen25f27032001-04-26 23:22:31 +00001284#else
1285 fputs(prompt_str, stdout);
1286 fflush(stdout);
Denis Vlasenko87a86552008-07-29 19:43:10 +00001287 G.user_input_buf[0] = r = fgetc(i->file);
1288 /*G.user_input_buf[1] = '\0'; - already is and never changed */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001289 i->eof_flag = (r == EOF);
Eric Andersen25f27032001-04-26 23:22:31 +00001290#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00001291 i->p = G.user_input_buf;
Eric Andersen25f27032001-04-26 23:22:31 +00001292}
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001293
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001294#endif /* INTERACTIVE */
Eric Andersen25f27032001-04-26 23:22:31 +00001295
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001296/* This is the magic location that prints prompts
Eric Andersen25f27032001-04-26 23:22:31 +00001297 * and gets data back from the user */
1298static int file_get(struct in_str *i)
1299{
1300 int ch;
1301
Eric Andersen25f27032001-04-26 23:22:31 +00001302 /* If there is data waiting, eat it up */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001303 if (i->p && *i->p) {
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001304#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001305 take_cached:
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001306#endif
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001307 ch = *i->p++;
1308 if (i->eof_flag && !*i->p)
1309 ch = EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001310 } else {
1311 /* need to double check i->file because we might be doing something
1312 * more complicated by now, like sourcing or substituting. */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001313#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko87a86552008-07-29 19:43:10 +00001314 if (G.interactive_fd && i->promptme && i->file == stdin) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001315 do {
1316 get_user_input(i);
1317 } while (!*i->p); /* need non-empty line */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001318 i->promptmode = 1; /* PS2 */
1319 i->promptme = 0;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001320 goto take_cached;
Eric Andersen25f27032001-04-26 23:22:31 +00001321 }
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001322#endif
1323 ch = fgetc(i->file);
Eric Andersen25f27032001-04-26 23:22:31 +00001324 }
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001325 debug_printf("file_get: got a '%c' %d\n", ch, ch);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001326#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001327 if (ch == '\n')
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001328 i->promptme = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001329#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001330 return ch;
1331}
1332
1333/* All the callers guarantee this routine will never be
1334 * used right after a newline, so prompting is not needed.
1335 */
1336static int file_peek(struct in_str *i)
1337{
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001338 int ch;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001339 if (i->p && *i->p) {
1340 if (i->eof_flag && !i->p[1])
1341 return EOF;
1342 return *i->p;
Eric Andersen25f27032001-04-26 23:22:31 +00001343 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001344 ch = fgetc(i->file);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001345 i->eof_flag = (ch == EOF);
1346 i->peek_buf[0] = ch;
1347 i->peek_buf[1] = '\0';
1348 i->p = i->peek_buf;
1349 debug_printf("file_peek: got a '%c' %d\n", *i->p, *i->p);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001350 return ch;
Eric Andersen25f27032001-04-26 23:22:31 +00001351}
1352
1353static void setup_file_in_str(struct in_str *i, FILE *f)
1354{
1355 i->peek = file_peek;
1356 i->get = file_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001357#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001358 i->promptme = 1;
1359 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001360#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001361 i->file = f;
1362 i->p = NULL;
1363}
1364
1365static void setup_string_in_str(struct in_str *i, const char *s)
1366{
1367 i->peek = static_peek;
1368 i->get = static_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001369#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001370 i->promptme = 1;
1371 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001372#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001373 i->p = s;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001374 i->eof_flag = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001375}
1376
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001377
Eric Andersen25f27032001-04-26 23:22:31 +00001378/* squirrel != NULL means we squirrel away copies of stdin, stdout,
1379 * and stderr if they are redirected. */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001380static int setup_redirects(struct command *prog, int squirrel[])
Eric Andersen25f27032001-04-26 23:22:31 +00001381{
1382 int openfd, mode;
1383 struct redir_struct *redir;
1384
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001385 for (redir = prog->redirects; redir; redir = redir->next) {
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00001386 if (redir->dup == -1 && redir->rd_filename == NULL) {
Eric Andersen817e73c2001-06-06 17:56:09 +00001387 /* something went wrong in the parse. Pretend it didn't happen */
1388 continue;
1389 }
Eric Andersen25f27032001-04-26 23:22:31 +00001390 if (redir->dup == -1) {
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001391 char *p;
Denis Vlasenko55789c62008-06-18 16:30:42 +00001392 mode = redir_table[redir->rd_type].mode;
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00001393//TODO: check redir for names like '\\'
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00001394 p = expand_string_to_string(redir->rd_filename);
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001395 openfd = open_or_warn(p, mode);
1396 free(p);
Eric Andersen25f27032001-04-26 23:22:31 +00001397 if (openfd < 0) {
1398 /* this could get lost if stderr has been redirected, but
1399 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersen25f27032001-04-26 23:22:31 +00001400 return 1;
1401 }
1402 } else {
1403 openfd = redir->dup;
1404 }
1405
1406 if (openfd != redir->fd) {
1407 if (squirrel && redir->fd < 3) {
1408 squirrel[redir->fd] = dup(redir->fd);
1409 }
Eric Andersen83a2ae22001-05-07 17:59:25 +00001410 if (openfd == -3) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001411 //close(openfd); // close(-3) ??!
Eric Andersen83a2ae22001-05-07 17:59:25 +00001412 } else {
1413 dup2(openfd, redir->fd);
Matt Kraaic616e532001-06-05 16:50:08 +00001414 if (redir->dup == -1)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001415 close(openfd);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001416 }
Eric Andersen25f27032001-04-26 23:22:31 +00001417 }
1418 }
1419 return 0;
1420}
1421
1422static void restore_redirects(int squirrel[])
1423{
1424 int i, fd;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001425 for (i = 0; i < 3; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001426 fd = squirrel[i];
1427 if (fd != -1) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00001428 /* We simply die on error */
1429 xmove_fd(fd, i);
Eric Andersen25f27032001-04-26 23:22:31 +00001430 }
1431 }
1432}
1433
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00001434
Denis Vlasenko05743d72008-02-10 12:10:08 +00001435/* Called after [v]fork() in run_pipe(), or from builtin_exec().
Denis Vlasenko8412d792007-10-01 09:59:47 +00001436 * Never returns.
1437 * XXX no exit() here. If you don't exec, use _exit instead.
Eric Andersen94ac2442001-05-22 19:05:18 +00001438 * The at_exit handlers apparently confuse the calling process,
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001439 * in particular stdin handling. Not sure why? -- because of vfork! (vda) */
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001440static void pseudo_exec_argv(char ***ptr_ptrs2free, char **argv, int assignment_cnt, char **argv_expanded)
Eric Andersen25f27032001-04-26 23:22:31 +00001441{
Eric Andersen78a7c992001-05-15 16:30:25 +00001442 int i, rcode;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001443 char *p;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001444 const struct built_in_command *x;
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001445
Denis Vlasenko1359da62007-04-21 23:27:30 +00001446 /* If a variable is assigned in a forest, and nobody listens,
1447 * was it ever really set?
1448 */
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001449 if (!argv[assignment_cnt])
Denis Vlasenko1359da62007-04-21 23:27:30 +00001450 _exit(EXIT_SUCCESS);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001451
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001452 for (i = 0; i < assignment_cnt; i++) {
1453 debug_printf_exec("pid %d environment modification: %s\n",
1454 getpid(), *argv);
1455 p = expand_string_to_string(*argv);
1456 putenv(p);
1457#if !BB_MMU
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001458 *ptr_ptrs2free = add_string_to_strings(*ptr_ptrs2free, p);
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001459#endif
1460 argv++;
1461 }
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001462 if (argv_expanded) {
1463 argv = argv_expanded;
1464 } else {
1465 argv = expand_strvec_to_strvec(argv);
Denis Vlasenko76d50412008-06-10 16:19:39 +00001466#if !BB_MMU
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001467 /* Inserting magic guard pointer to not unsetenv junk later */
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001468 *ptr_ptrs2free = add_string_to_strings(*ptr_ptrs2free, (char*)hush_version_str);
1469 *ptr_ptrs2free = add_string_to_strings(*ptr_ptrs2free, (char*)argv);
Denis Vlasenko76d50412008-06-10 16:19:39 +00001470#endif
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001471 }
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001472
Denis Vlasenko1359da62007-04-21 23:27:30 +00001473 /*
1474 * Check if the command matches any of the builtins.
1475 * Depending on context, this might be redundant. But it's
1476 * easier to waste a few CPU cycles than it is to figure out
1477 * if this is one of those cases.
1478 */
Denis Vlasenkodd316dd2008-06-14 15:50:55 +00001479 for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001480 if (strcmp(argv[0], x->cmd) == 0) {
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001481 debug_printf_exec("running builtin '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001482 rcode = x->function(argv);
1483 fflush(stdout);
1484 _exit(rcode);
Eric Andersen94ac2442001-05-22 19:05:18 +00001485 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001486 }
Eric Andersen78a7c992001-05-15 16:30:25 +00001487
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001488 /* Check if the command matches any busybox applets */
Denis Vlasenko80d14be2007-04-10 23:03:30 +00001489#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001490 if (strchr(argv[0], '/') == NULL) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001491 int a = find_applet_by_name(argv[0]);
1492 if (a >= 0) {
1493 if (APPLET_IS_NOEXEC(a)) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001494 debug_printf_exec("running applet '%s'\n", argv[0]);
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001495// is it ok that run_applet_no_and_exit() does exit(), not _exit()?
1496 run_applet_no_and_exit(a, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001497 }
1498 /* re-exec ourselves with the new arguments */
1499 debug_printf_exec("re-execing applet '%s'\n", argv[0]);
Denis Vlasenkobdbbb7e2007-06-08 15:02:55 +00001500 execvp(bb_busybox_exec_path, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001501 /* If they called chroot or otherwise made the binary no longer
1502 * executable, fall through */
1503 }
1504 }
Eric Andersenaac75e52001-04-30 18:18:45 +00001505#endif
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001506
1507 debug_printf_exec("execing '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001508 execvp(argv[0], argv);
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +00001509 bb_perror_msg("can't exec '%s'", argv[0]);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +00001510 _exit(EXIT_FAILURE);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001511}
1512
Denis Vlasenko05743d72008-02-10 12:10:08 +00001513/* Called after [v]fork() in run_pipe()
Denis Vlasenko8412d792007-10-01 09:59:47 +00001514 */
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001515static void pseudo_exec(char ***ptr_ptrs2free, struct command *command, char **argv_expanded)
Denis Vlasenko1359da62007-04-21 23:27:30 +00001516{
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001517 if (command->argv)
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001518 pseudo_exec_argv(ptr_ptrs2free, command->argv, command->assignment_cnt, argv_expanded);
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001519
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001520 if (command->group) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001521#if !BB_MMU
Denis Vlasenko3b492162007-12-24 14:26:57 +00001522 bb_error_msg_and_die("nested lists are not supported on NOMMU");
Denis Vlasenko8412d792007-10-01 09:59:47 +00001523#else
Denis Vlasenko3b492162007-12-24 14:26:57 +00001524 int rcode;
Denis Vlasenko05743d72008-02-10 12:10:08 +00001525 debug_printf_exec("pseudo_exec: run_list\n");
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001526 rcode = run_list(command->group);
Eric Andersenbf7df042001-05-23 22:18:35 +00001527 /* OK to leak memory by not calling free_pipe_list,
Eric Andersen25f27032001-04-26 23:22:31 +00001528 * since this process is about to exit */
Eric Andersen94ac2442001-05-22 19:05:18 +00001529 _exit(rcode);
Denis Vlasenko8412d792007-10-01 09:59:47 +00001530#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001531 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001532
1533 /* Can happen. See what bash does with ">foo" by itself. */
1534 debug_printf("trying to pseudo_exec null command\n");
1535 _exit(EXIT_SUCCESS);
Eric Andersen25f27032001-04-26 23:22:31 +00001536}
1537
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001538#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001539static const char *get_cmdtext(struct pipe *pi)
1540{
1541 char **argv;
1542 char *p;
1543 int len;
1544
1545 /* This is subtle. ->cmdtext is created only on first backgrounding.
1546 * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...)
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001547 * On subsequent bg argv is trashed, but we won't use it */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001548 if (pi->cmdtext)
1549 return pi->cmdtext;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001550 argv = pi->cmds[0].argv;
Denis Vlasenkoa8442002008-06-14 11:00:17 +00001551 if (!argv || !argv[0]) {
1552 pi->cmdtext = xzalloc(1);
1553 return pi->cmdtext;
1554 }
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001555
1556 len = 0;
1557 do len += strlen(*argv) + 1; while (*++argv);
1558 pi->cmdtext = p = xmalloc(len);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001559 argv = pi->cmds[0].argv;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001560 do {
1561 len = strlen(*argv);
1562 memcpy(p, *argv, len);
1563 p += len;
1564 *p++ = ' ';
1565 } while (*++argv);
1566 p[-1] = '\0';
1567 return pi->cmdtext;
1568}
1569
Eric Andersenbafd94f2001-05-02 16:11:59 +00001570static void insert_bg_job(struct pipe *pi)
1571{
1572 struct pipe *thejob;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001573 int i;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001574
1575 /* Linear search for the ID of the job to use */
1576 pi->jobid = 1;
Denis Vlasenko87a86552008-07-29 19:43:10 +00001577 for (thejob = G.job_list; thejob; thejob = thejob->next)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001578 if (thejob->jobid >= pi->jobid)
1579 pi->jobid = thejob->jobid + 1;
1580
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001581 /* Add thejob to the list of running jobs */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001582 if (!G.job_list) {
1583 thejob = G.job_list = xmalloc(sizeof(*thejob));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001584 } else {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001585 for (thejob = G.job_list; thejob->next; thejob = thejob->next)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001586 continue;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001587 thejob->next = xmalloc(sizeof(*thejob));
1588 thejob = thejob->next;
1589 }
1590
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001591 /* Physically copy the struct job */
Eric Andersen0fcd4472001-05-02 20:12:03 +00001592 memcpy(thejob, pi, sizeof(struct pipe));
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001593 thejob->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds);
1594 /* We cannot copy entire pi->cmds[] vector! Double free()s will happen */
1595 for (i = 0; i < pi->num_cmds; i++) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001596// TODO: do we really need to have so many fields which are just dead weight
1597// at execution stage?
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001598 thejob->cmds[i].pid = pi->cmds[i].pid;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001599 /* all other fields are not used and stay zero */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001600 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001601 thejob->next = NULL;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001602 thejob->cmdtext = xstrdup(get_cmdtext(pi));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001603
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001604 /* We don't wait for background thejobs to return -- append it
Eric Andersenbafd94f2001-05-02 16:11:59 +00001605 to the list of backgrounded thejobs and leave it alone */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001606 printf("[%d] %d %s\n", thejob->jobid, thejob->cmds[0].pid, thejob->cmdtext);
1607 G.last_bg_pid = thejob->cmds[0].pid;
Denis Vlasenko87a86552008-07-29 19:43:10 +00001608 G.last_jobid = thejob->jobid;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001609}
1610
Eric Andersenbafd94f2001-05-02 16:11:59 +00001611static void remove_bg_job(struct pipe *pi)
1612{
1613 struct pipe *prev_pipe;
1614
Denis Vlasenko87a86552008-07-29 19:43:10 +00001615 if (pi == G.job_list) {
1616 G.job_list = pi->next;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001617 } else {
Denis Vlasenko87a86552008-07-29 19:43:10 +00001618 prev_pipe = G.job_list;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001619 while (prev_pipe->next != pi)
1620 prev_pipe = prev_pipe->next;
1621 prev_pipe->next = pi->next;
1622 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00001623 if (G.job_list)
1624 G.last_jobid = G.job_list->jobid;
Eric Andersen028b65b2001-06-28 01:10:11 +00001625 else
Denis Vlasenko87a86552008-07-29 19:43:10 +00001626 G.last_jobid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001627}
Eric Andersen028b65b2001-06-28 01:10:11 +00001628
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001629/* Remove a backgrounded job */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001630static void delete_finished_bg_job(struct pipe *pi)
1631{
1632 remove_bg_job(pi);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001633 pi->stopped_cmds = 0;
Eric Andersenbf7df042001-05-23 22:18:35 +00001634 free_pipe(pi, 0);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001635 free(pi);
1636}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001637#endif /* JOB */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001638
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001639/* Check to see if any processes have exited -- if they
1640 * have, figure out why and see if a job has completed */
Eric Andersenc798b072001-06-22 06:23:03 +00001641static int checkjobs(struct pipe* fg_pipe)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001642{
Eric Andersenc798b072001-06-22 06:23:03 +00001643 int attributes;
1644 int status;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001645#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00001646 struct pipe *pi;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001647#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001648 pid_t childpid;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001649 int rcode = 0;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001650
Eric Andersenc798b072001-06-22 06:23:03 +00001651 attributes = WUNTRACED;
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001652 if (fg_pipe == NULL)
Eric Andersenc798b072001-06-22 06:23:03 +00001653 attributes |= WNOHANG;
Eric Andersenc798b072001-06-22 06:23:03 +00001654
Denis Vlasenko1359da62007-04-21 23:27:30 +00001655/* Do we do this right?
1656 * bash-3.00# sleep 20 | false
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001657 * <ctrl-Z pressed>
Denis Vlasenko1359da62007-04-21 23:27:30 +00001658 * [3]+ Stopped sleep 20 | false
1659 * bash-3.00# echo $?
1660 * 1 <========== bg pipe is not fully done, but exitcode is already known!
1661 */
1662
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001663//FIXME: non-interactive bash does not continue even if all processes in fg pipe
1664//are stopped. Testcase: "cat | cat" in a script (not on command line)
1665// + killall -STOP cat
1666
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001667 wait_more:
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001668// TODO: safe_waitpid?
Eric Andersenc798b072001-06-22 06:23:03 +00001669 while ((childpid = waitpid(-1, &status, attributes)) > 0) {
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001670 int i;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001671 const int dead = WIFEXITED(status) || WIFSIGNALED(status);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00001672#if DEBUG_JOBS
Denis Vlasenko1359da62007-04-21 23:27:30 +00001673 if (WIFSTOPPED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001674 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001675 childpid, WSTOPSIG(status), WEXITSTATUS(status));
1676 if (WIFSIGNALED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001677 debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001678 childpid, WTERMSIG(status), WEXITSTATUS(status));
1679 if (WIFEXITED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001680 debug_printf_jobs("pid %d exited, exitcode %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001681 childpid, WEXITSTATUS(status));
1682#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001683 /* Were we asked to wait for fg pipe? */
Eric Andersenc798b072001-06-22 06:23:03 +00001684 if (fg_pipe) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001685 for (i = 0; i < fg_pipe->num_cmds; i++) {
1686 debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid);
1687 if (fg_pipe->cmds[i].pid != childpid)
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001688 continue;
1689 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */
1690 if (dead) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001691 fg_pipe->cmds[i].pid = 0;
1692 fg_pipe->alive_cmds--;
1693 if (i == fg_pipe->num_cmds - 1) {
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001694 /* last process gives overall exitstatus */
1695 rcode = WEXITSTATUS(status);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00001696 IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;)
Denis Vlasenko1359da62007-04-21 23:27:30 +00001697 }
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001698 } else {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001699 fg_pipe->cmds[i].is_stopped = 1;
1700 fg_pipe->stopped_cmds++;
Eric Andersenc798b072001-06-22 06:23:03 +00001701 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001702 debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n",
1703 fg_pipe->alive_cmds, fg_pipe->stopped_cmds);
1704 if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) {
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001705 /* All processes in fg pipe have exited/stopped */
1706#if ENABLE_HUSH_JOB
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001707 if (fg_pipe->alive_cmds)
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001708 insert_bg_job(fg_pipe);
1709#endif
1710 return rcode;
1711 }
1712 /* There are still running processes in the fg pipe */
1713 goto wait_more; /* do waitpid again */
Eric Andersenc798b072001-06-22 06:23:03 +00001714 }
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001715 /* it wasnt fg_pipe, look for process in bg pipes */
Eric Andersenc798b072001-06-22 06:23:03 +00001716 }
1717
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001718#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001719 /* We asked to wait for bg or orphaned children */
1720 /* No need to remember exitcode in this case */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001721 for (pi = G.job_list; pi; pi = pi->next) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001722 for (i = 0; i < pi->num_cmds; i++) {
1723 if (pi->cmds[i].pid == childpid)
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001724 goto found_pi_and_prognum;
Eric Andersen52a97ca2001-06-22 06:49:26 +00001725 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001726 }
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001727 /* Happens when shell is used as init process (init=/bin/sh) */
1728 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001729 continue; /* do waitpid again */
Eric Andersenaeb44c42001-05-22 20:29:00 +00001730
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001731 found_pi_and_prognum:
Denis Vlasenko1359da62007-04-21 23:27:30 +00001732 if (dead) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001733 /* child exited */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001734 pi->cmds[i].pid = 0;
1735 pi->alive_cmds--;
1736 if (!pi->alive_cmds) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001737 printf(JOB_STATUS_FORMAT, pi->jobid,
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001738 "Done", pi->cmdtext);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001739 delete_finished_bg_job(pi);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001740 }
1741 } else {
1742 /* child stopped */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001743 pi->cmds[i].is_stopped = 1;
1744 pi->stopped_cmds++;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001745 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001746#endif
Denis Vlasenko003f9fb2008-06-24 00:47:58 +00001747 } /* while (waitpid succeeds)... */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001748
Denis Vlasenko1359da62007-04-21 23:27:30 +00001749 /* wait found no children or failed */
1750
1751 if (childpid && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001752 bb_perror_msg("waitpid");
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001753 return rcode;
Eric Andersenada18ff2001-05-21 16:18:22 +00001754}
1755
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001756#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +00001757static int checkjobs_and_fg_shell(struct pipe* fg_pipe)
1758{
1759 pid_t p;
1760 int rcode = checkjobs(fg_pipe);
1761 /* Job finished, move the shell to the foreground */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001762 p = getpgid(0); /* pgid of our process */
1763 debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p);
Denis Vlasenko87a86552008-07-29 19:43:10 +00001764 tcsetpgrp(G.interactive_fd, p);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001765 return rcode;
1766}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001767#endif
Denis Vlasenko52881e92007-04-21 13:42:52 +00001768
Denis Vlasenko05743d72008-02-10 12:10:08 +00001769/* run_pipe() starts all the jobs, but doesn't wait for anything
Eric Andersenc798b072001-06-22 06:23:03 +00001770 * to finish. See checkjobs().
Eric Andersen25f27032001-04-26 23:22:31 +00001771 *
1772 * return code is normally -1, when the caller has to wait for children
1773 * to finish to determine the exit status of the pipe. If the pipe
1774 * is a simple builtin command, however, the action is done by the
Denis Vlasenko05743d72008-02-10 12:10:08 +00001775 * time run_pipe returns, and the exit code is provided as the
Eric Andersen25f27032001-04-26 23:22:31 +00001776 * return value.
1777 *
1778 * The input of the pipe is always stdin, the output is always
1779 * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
1780 * because it tries to avoid running the command substitution in
1781 * subshell, when that is in fact necessary. The subshell process
1782 * now has its stdout directed to the input of the appropriate pipe,
1783 * so this routine is noticeably simpler.
Denis Vlasenko170435c2007-05-23 13:01:10 +00001784 *
1785 * Returns -1 only if started some children. IOW: we have to
1786 * mask out retvals of builtins etc with 0xff!
Eric Andersen25f27032001-04-26 23:22:31 +00001787 */
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001788/* A little helper first */
1789static char **expand_assignments(char **argv, int count)
1790{
1791 int i;
1792 char **p = NULL;
1793 /* Expand assignments into one string each */
1794 for (i = 0; i < count; i++) {
1795 p = add_string_to_strings(p, expand_string_to_string(argv[i]));
1796 }
1797 return p;
1798}
Denis Vlasenko05743d72008-02-10 12:10:08 +00001799static int run_pipe(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00001800{
1801 int i;
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001802 int nextin;
1803 int pipefds[2]; /* pipefds[0] is for reading */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001804 struct command *command;
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001805 char **argv_expanded;
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001806 char **argv;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001807 const struct built_in_command *x;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001808 char *p;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001809 /* it is not always needed, but we aim to smaller code */
1810 int squirrel[] = { -1, -1, -1 };
1811 int rcode;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001812 const int single_and_fg = (pi->num_cmds == 1 && pi->followup != PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00001813
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001814 debug_printf_exec("run_pipe start: single_and_fg=%d\n", single_and_fg);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001815
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001816#if ENABLE_HUSH_JOB
Eric Andersenada18ff2001-05-21 16:18:22 +00001817 pi->pgrp = -1;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001818#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001819 pi->alive_cmds = 1;
1820 pi->stopped_cmds = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001821
1822 /* Check if this is a simple builtin (not part of a pipe).
1823 * Builtins within pipes have to fork anyway, and are handled in
1824 * pseudo_exec. "echo foo | read bar" doesn't work on bash, either.
1825 */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001826 command = &(pi->cmds[0]);
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001827
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001828 if (single_and_fg && command->group && command->subshell == 0) {
Eric Andersen04407e52001-06-07 16:42:05 +00001829 debug_printf("non-subshell grouping\n");
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001830 setup_redirects(command, squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001831 debug_printf_exec(": run_list\n");
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001832 rcode = run_list(command->group) & 0xff;
Eric Andersen04407e52001-06-07 16:42:05 +00001833 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001834 debug_printf_exec("run_pipe return %d\n", rcode);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00001835 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
Denis Vlasenko05743d72008-02-10 12:10:08 +00001836 return rcode;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001837 }
1838
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001839 argv = command->argv;
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001840 argv_expanded = NULL;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001841
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001842 if (single_and_fg && argv != NULL) {
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001843 char **new_env = NULL;
1844 char **old_env = NULL;
1845
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001846 i = command->assignment_cnt;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001847 if (i != 0 && argv[i] == NULL) {
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001848 /* assignments, but no command: set local environment */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001849 for (i = 0; argv[i] != NULL; i++) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001850 debug_printf("local environment set: %s\n", argv[i]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001851 p = expand_string_to_string(argv[i]);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001852 set_local_var(p, 0);
Eric Andersen78a7c992001-05-15 16:30:25 +00001853 }
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001854 return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */
Eric Andersen78a7c992001-05-15 16:30:25 +00001855 }
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001856
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001857 /* Expand the rest into (possibly) many strings each */
1858 argv_expanded = expand_strvec_to_strvec(argv + i);
1859
Denis Vlasenkodd316dd2008-06-14 15:50:55 +00001860 for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001861 if (strcmp(argv_expanded[0], x->cmd) != 0)
1862 continue;
1863 if (x->function == builtin_exec && argv_expanded[1] == NULL) {
1864 debug_printf("exec with redirects only\n");
1865 setup_redirects(command, NULL);
1866 rcode = EXIT_SUCCESS;
1867 goto clean_up_and_ret1;
Eric Andersen25f27032001-04-26 23:22:31 +00001868 }
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001869 debug_printf("builtin inline %s\n", argv_expanded[0]);
1870 /* XXX setup_redirects acts on file descriptors, not FILEs.
1871 * This is perfect for work that comes after exec().
1872 * Is it really safe for inline use? Experimentally,
1873 * things seem to work with glibc. */
1874 setup_redirects(command, squirrel);
1875 new_env = expand_assignments(argv, command->assignment_cnt);
1876 old_env = putenv_all_and_save_old(new_env);
1877 debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv_expanded[1]);
1878 rcode = x->function(argv_expanded) & 0xff;
1879 USE_FEATURE_SH_STANDALONE(clean_up_and_ret:)
1880 restore_redirects(squirrel);
1881 free_strings_and_unsetenv(new_env, 1);
1882 putenv_all(old_env);
1883 free_strings(old_env);
1884 clean_up_and_ret1:
1885 free(argv_expanded);
1886 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
1887 debug_printf_exec("run_pipe return %d\n", rcode);
1888 return rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00001889 }
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001890#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001891 i = find_applet_by_name(argv_expanded[0]);
1892 if (i >= 0 && APPLET_IS_NOFORK(i)) {
1893 setup_redirects(command, squirrel);
1894 save_nofork_data(&G.nofork_save);
1895 new_env = expand_assignments(argv, command->assignment_cnt);
1896 old_env = putenv_all_and_save_old(new_env);
1897 debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]);
1898 rcode = run_nofork_applet_prime(&G.nofork_save, i, argv_expanded);
1899 goto clean_up_and_ret;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001900 }
1901#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001902 }
1903
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001904 /* NB: argv_expanded may already be created, and that
1905 * might include `cmd` runs! Do not rerun it! We *must*
1906 * use argv_expanded if it's non-NULL */
1907
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001908 /* Disable job control signals for shell (parent) and
1909 * for initial child code after fork */
1910 set_jobctrl_sighandler(SIG_IGN);
1911
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001912 /* Going to fork a child per each pipe member */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001913 pi->alive_cmds = 0;
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001914 nextin = 0;
1915
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001916 for (i = 0; i < pi->num_cmds; i++) {
Denis Vlasenko76d50412008-06-10 16:19:39 +00001917#if !BB_MMU
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001918 /* Avoid confusion WHAT is volatile. Pointer is volatile,
1919 * not the stuff it points to. */
1920 typedef char **ppchar_t;
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001921 volatile ppchar_t shared_across_vfork = NULL;
Denis Vlasenko76d50412008-06-10 16:19:39 +00001922#endif
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001923
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001924 command = &(pi->cmds[i]);
1925 if (command->argv) {
1926 debug_printf_exec(": pipe member '%s' '%s'...\n", command->argv[0], command->argv[1]);
Denis Vlasenko76d50412008-06-10 16:19:39 +00001927 } else
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001928 debug_printf_exec(": pipe member with no argv\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001929
1930 /* pipes are inserted between pairs of commands */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001931 pipefds[0] = 0;
1932 pipefds[1] = 1;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001933 if ((i + 1) < pi->num_cmds)
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001934 xpipe(pipefds);
Eric Andersen25f27032001-04-26 23:22:31 +00001935
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001936 command->pid = BB_MMU ? fork() : vfork();
1937 if (!command->pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00001938 if (ENABLE_HUSH_JOB)
1939 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001940#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001941 /* Every child adds itself to new process group
Denis Vlasenko05743d72008-02-10 12:10:08 +00001942 * with pgid == pid_of_first_child_in_pipe */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001943 if (G.run_list_level == 1 && G.interactive_fd) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00001944 pid_t pgrp;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001945 /* Don't do pgrp restore anymore on fatal signals */
1946 set_fatal_sighandler(SIG_DFL);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001947 pgrp = pi->pgrp;
1948 if (pgrp < 0) /* true for 1st process only */
1949 pgrp = getpid();
1950 if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001951 /* We do it in *every* child, not just first,
1952 * to avoid races */
Denis Vlasenko87a86552008-07-29 19:43:10 +00001953 tcsetpgrp(G.interactive_fd, pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001954 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001955 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001956#endif
Denis Vlasenko8412d792007-10-01 09:59:47 +00001957 xmove_fd(nextin, 0);
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001958 xmove_fd(pipefds[1], 1); /* write end */
1959 if (pipefds[0] > 1)
1960 close(pipefds[0]); /* read end */
Eric Andersen25f27032001-04-26 23:22:31 +00001961 /* Like bash, explicit redirects override pipes,
1962 * and the pipe fd is available for dup'ing. */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001963 setup_redirects(command, NULL);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001964
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001965 /* Restore default handlers just prior to exec */
1966 set_jobctrl_sighandler(SIG_DFL);
1967 set_misc_sighandler(SIG_DFL);
1968 signal(SIGCHLD, SIG_DFL);
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001969/* comment how it sets env????
1970for single_and_fg, it's already set yes? */
1971 pseudo_exec((char ***) &shared_across_vfork, command, argv_expanded);
1972 /* pseudo_exec() does not return */
Eric Andersen25f27032001-04-26 23:22:31 +00001973 }
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001974 /* parent */
1975#if !BB_MMU
Denis Vlasenko22d10a02008-10-13 08:53:43 +00001976//BUG: does not restore OLD env var contents
1977 free_strings_and_unsetenv((char **)shared_across_vfork, 1);
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00001978#endif
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00001979 free(argv_expanded);
1980 argv_expanded = NULL;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001981 if (command->pid < 0) { /* [v]fork failed */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001982 /* Clearly indicate, was it fork or vfork */
Denis Vlasenko82604e92008-07-01 15:59:42 +00001983 bb_perror_msg(BB_MMU ? "fork" : "vfork");
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001984 } else {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001985 pi->alive_cmds++;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001986#if ENABLE_HUSH_JOB
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001987 /* Second and next children need to know pid of first one */
1988 if (pi->pgrp < 0)
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001989 pi->pgrp = command->pid;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001990#endif
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001991 }
Eric Andersen25f27032001-04-26 23:22:31 +00001992
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001993 if (i)
1994 close(nextin);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00001995 if ((i + 1) < pi->num_cmds)
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001996 close(pipefds[1]); /* write end */
1997 /* Pass read (output) pipe end to next iteration */
Eric Andersen25f27032001-04-26 23:22:31 +00001998 nextin = pipefds[0];
1999 }
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00002000
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002001 if (!pi->alive_cmds) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00002002 debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n");
2003 return 1;
2004 }
2005
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002006 debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds);
Eric Andersen25f27032001-04-26 23:22:31 +00002007 return -1;
2008}
2009
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002010#ifndef debug_print_tree
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002011static void debug_print_tree(struct pipe *pi, int lvl)
2012{
2013 static const char *PIPE[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002014 [PIPE_SEQ] = "SEQ",
2015 [PIPE_AND] = "AND",
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002016 [PIPE_OR ] = "OR" ,
2017 [PIPE_BG ] = "BG" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002018 };
2019 static const char *RES[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002020 [RES_NONE ] = "NONE" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00002021#if ENABLE_HUSH_IF
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002022 [RES_IF ] = "IF" ,
2023 [RES_THEN ] = "THEN" ,
2024 [RES_ELIF ] = "ELIF" ,
2025 [RES_ELSE ] = "ELSE" ,
2026 [RES_FI ] = "FI" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00002027#endif
2028#if ENABLE_HUSH_LOOPS
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002029 [RES_FOR ] = "FOR" ,
2030 [RES_WHILE] = "WHILE",
2031 [RES_UNTIL] = "UNTIL",
2032 [RES_DO ] = "DO" ,
2033 [RES_DONE ] = "DONE" ,
Denis Vlasenkod91afa32008-07-29 11:10:01 +00002034#endif
2035#if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002036 [RES_IN ] = "IN" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00002037#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002038#if ENABLE_HUSH_CASE
2039 [RES_CASE ] = "CASE" ,
2040 [RES_MATCH] = "MATCH",
2041 [RES_CASEI] = "CASEI",
2042 [RES_ESAC ] = "ESAC" ,
2043#endif
Denis Vlasenko06810332007-05-21 23:30:54 +00002044 [RES_XXXX ] = "XXXX" ,
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002045 [RES_SNTX ] = "SNTX" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002046 };
2047
2048 int pin, prn;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002049
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002050 pin = 0;
2051 while (pi) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002052 fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "",
2053 pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002054 prn = 0;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002055 while (prn < pi->num_cmds) {
2056 struct command *command = &pi->cmds[prn];
2057 char **argv = command->argv;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002058
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002059 fprintf(stderr, "%*s prog %d assignment_cnt:%d", lvl*2, "", prn, command->assignment_cnt);
2060 if (command->group) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002061 fprintf(stderr, " group %s: (argv=%p)\n",
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002062 (command->subshell ? "()" : "{}"),
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002063 argv);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002064 debug_print_tree(command->group, lvl+1);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002065 prn++;
2066 continue;
2067 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002068 if (argv) while (*argv) {
2069 fprintf(stderr, " '%s'", *argv);
2070 argv++;
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002071 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002072 fprintf(stderr, "\n");
2073 prn++;
2074 }
2075 pi = pi->next;
2076 pin++;
2077 }
2078}
2079#endif
2080
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002081/* NB: called by pseudo_exec, and therefore must not modify any
2082 * global data until exec/_exit (we can be a child after vfork!) */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002083static int run_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002084{
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002085#if ENABLE_HUSH_CASE
2086 char *case_word = NULL;
2087#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002088#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002089 struct pipe *loop_top = NULL;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002090 char *for_varname = NULL;
2091 char **for_lcur = NULL;
2092 char **for_list = NULL;
Denis Vlasenko06810332007-05-21 23:30:54 +00002093#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002094 smallint flag_skip = 1;
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002095 smalluint rcode = 0; /* probably just for compiler */
Denis Vlasenkod91afa32008-07-29 11:10:01 +00002096#if ENABLE_HUSH_IF || ENABLE_HUSH_CASE
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002097 smalluint cond_code = 0;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002098#else
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002099 enum { cond_code = 0, };
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002100#endif
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002101 /*enum reserved_style*/ smallint rword = RES_NONE;
2102 /*enum reserved_style*/ smallint skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002103
Denis Vlasenko87a86552008-07-29 19:43:10 +00002104 debug_printf_exec("run_list start lvl %d\n", G.run_list_level + 1);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002105
Denis Vlasenko06810332007-05-21 23:30:54 +00002106#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002107 /* Check syntax for "for" */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002108 for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) {
2109 if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN)
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002110 continue;
2111 /* current word is FOR or IN (BOLD in comments below) */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002112 if (cpipe->next == NULL) {
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002113 syntax("malformed for");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002114 debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002115 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002116 }
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002117 /* "FOR v; do ..." and "for v IN a b; do..." are ok */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002118 if (cpipe->next->res_word == RES_DO)
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002119 continue;
2120 /* next word is not "do". It must be "in" then ("FOR v in ...") */
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002121 if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */
2122 || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002123 ) {
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002124 syntax("malformed for");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002125 debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002126 return 1;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002127 }
2128 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002129#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002130
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002131 /* Past this point, all code paths should jump to ret: label
Denis Vlasenko12acec52008-07-28 15:15:59 +00002132 * in order to return, no direct "return" statements please.
2133 * This helps to ensure that no memory is leaked. */
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002134
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002135#if ENABLE_HUSH_JOB
2136 /* Example of nested list: "while true; do { sleep 1 | exit 2; } done".
2137 * We are saving state before entering outermost list ("while...done")
2138 * so that ctrl-Z will correctly background _entire_ outermost list,
2139 * not just a part of it (like "sleep 1 | exit 2") */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002140 if (++G.run_list_level == 1 && G.interactive_fd) {
2141 if (sigsetjmp(G.toplevel_jb, 1)) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002142 /* ctrl-Z forked and we are parent; or ctrl-C.
2143 * Sighandler has longjmped us here */
2144 signal(SIGINT, SIG_IGN);
2145 signal(SIGTSTP, SIG_IGN);
2146 /* Restore level (we can be coming from deep inside
2147 * nested levels) */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002148 G.run_list_level = 1;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002149#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko87a86552008-07-29 19:43:10 +00002150 if (G.nofork_save.saved) { /* if save area is valid */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002151 debug_printf_jobs("exiting nofork early\n");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002152 restore_nofork_data(&G.nofork_save);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002153 }
2154#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00002155 if (G.ctrl_z_flag) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002156 /* ctrl-Z has forked and stored pid of the child in pi->pid.
2157 * Remember this child as background job */
2158 insert_bg_job(pi);
2159 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002160 /* ctrl-C. We just stop doing whatever we were doing */
Denis Vlasenko4daad902007-09-27 10:20:47 +00002161 bb_putchar('\n');
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002162 }
Denis Vlasenkoff29b4f2008-07-29 13:57:59 +00002163 USE_HUSH_LOOPS(loop_top = NULL;)
Denis Vlasenko87a86552008-07-29 19:43:10 +00002164 USE_HUSH_LOOPS(G.depth_of_loop = 0;)
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002165 rcode = 0;
2166 goto ret;
2167 }
2168 /* ctrl-Z handler will store pid etc in pi */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002169 G.toplevel_list = pi;
2170 G.ctrl_z_flag = 0;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002171#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko87a86552008-07-29 19:43:10 +00002172 G.nofork_save.saved = 0; /* in case we will run a nofork later */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002173#endif
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +00002174 signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002175 signal(SIGINT, handler_ctrl_c);
2176 }
Denis Vlasenko05743d72008-02-10 12:10:08 +00002177#endif /* JOB */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002178
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002179 /* Go through list of pipes, (maybe) executing them. */
2180 for (; pi; pi = USE_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) {
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002181 IF_HAS_KEYWORDS(rword = pi->res_word;)
2182 IF_HAS_NO_KEYWORDS(rword = RES_NONE;)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002183 debug_printf_exec(": rword=%d cond_code=%d skip_more=%d\n",
2184 rword, cond_code, skip_more_for_this_rword);
Denis Vlasenko06810332007-05-21 23:30:54 +00002185#if ENABLE_HUSH_LOOPS
Denis Vlasenko4554b722008-07-29 13:36:09 +00002186 if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR)
Denis Vlasenko87a86552008-07-29 19:43:10 +00002187 && loop_top == NULL /* avoid bumping G.depth_of_loop twice */
Denis Vlasenko4554b722008-07-29 13:36:09 +00002188 ) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002189 /* start of a loop: remember where loop starts */
2190 loop_top = pi;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002191 G.depth_of_loop++;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002192 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002193#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002194 if (rword == skip_more_for_this_rword && flag_skip) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002195 if (pi->followup == PIPE_SEQ)
2196 flag_skip = 0;
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002197 /* it is "<false> && CMD" or "<true> || CMD"
2198 * and we should not execute CMD */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002199 continue;
2200 }
2201 flag_skip = 1;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002202 skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko06810332007-05-21 23:30:54 +00002203#if ENABLE_HUSH_IF
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002204 if (cond_code) {
2205 if (rword == RES_THEN) {
2206 /* "if <false> THEN cmd": skip cmd */
2207 continue;
2208 }
2209 } else {
2210 if (rword == RES_ELSE || rword == RES_ELIF) {
2211 /* "if <true> then ... ELSE/ELIF cmd":
2212 * skip cmd and all following ones */
2213 break;
2214 }
2215 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002216#endif
2217#if ENABLE_HUSH_LOOPS
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002218 if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002219 if (!for_lcur) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002220 /* first loop through for */
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002221
2222 static const char encoded_dollar_at[] ALIGN1 = {
2223 SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0'
2224 }; /* encoded representation of "$@" */
2225 static const char *const encoded_dollar_at_argv[] = {
2226 encoded_dollar_at, NULL
2227 }; /* argv list with one element: "$@" */
2228 char **vals;
2229
2230 vals = (char**)encoded_dollar_at_argv;
Denis Vlasenkocf22c892008-07-28 15:17:44 +00002231 if (pi->next->res_word == RES_IN) {
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002232 /* if no variable values after "in" we skip "for" */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002233 if (!pi->next->cmds[0].argv)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002234 break;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002235 vals = pi->next->cmds[0].argv;
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002236 } /* else: "for var; do..." -> assume "$@" list */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002237 /* create list of variable values */
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002238 debug_print_strings("for_list made from", vals);
2239 for_list = expand_strvec_to_strvec(vals);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002240 for_lcur = for_list;
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002241 debug_print_strings("for_list", for_list);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002242 for_varname = pi->cmds[0].argv[0];
2243 pi->cmds[0].argv[0] = NULL;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002244 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002245 free(pi->cmds[0].argv[0]);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002246 if (!*for_lcur) {
Denis Vlasenko12acec52008-07-28 15:15:59 +00002247 /* "for" loop is over, clean up */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002248 free(for_list);
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002249 for_list = NULL;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002250 for_lcur = NULL;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002251 pi->cmds[0].argv[0] = for_varname;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002252 break;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002253 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002254 /* insert next value from for_lcur */
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002255//TODO: does it need escaping?
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002256 pi->cmds[0].argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++);
2257 pi->cmds[0].assignment_cnt = 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002258 }
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002259 if (rword == RES_IN) /* "for v IN list;..." - "in" has no cmds anyway */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002260 continue;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002261 if (rword == RES_DONE) {
2262 continue; /* "done" has no cmds too */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002263 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002264#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002265#if ENABLE_HUSH_CASE
2266 if (rword == RES_CASE) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002267 case_word = expand_strvec_to_string(pi->cmds->argv);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002268 continue;
2269 }
2270 if (rword == RES_MATCH) {
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00002271 char **argv;
2272
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002273 if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */
2274 break;
2275 /* all prev words didn't match, does this one match? */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002276 argv = pi->cmds->argv;
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00002277 while (*argv) {
2278 char *pattern = expand_string_to_string(*argv);
2279 /* TODO: which FNM_xxx flags to use? */
2280 cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0);
2281 free(pattern);
2282 if (cond_code == 0) { /* match! we will execute this branch */
2283 free(case_word); /* make future "word)" stop */
2284 case_word = NULL;
2285 break;
2286 }
2287 argv++;
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002288 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002289 continue;
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002290 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002291 if (rword == RES_CASEI) { /* inside of a case branch */
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002292 if (cond_code != 0)
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002293 continue; /* not matched yet, skip this pipe */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002294 }
2295#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002296 if (pi->num_cmds == 0)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002297 continue;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002298
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002299 /* After analyzing all keywords and conditions, we decided
2300 * to execute this pipe. NB: has to do checkjobs(NULL)
2301 * after run_pipe() to collect any background children,
2302 * even if list execution is to be stopped. */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002303 debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds);
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002304 {
2305 int r;
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002306#if ENABLE_HUSH_LOOPS
Denis Vlasenko87a86552008-07-29 19:43:10 +00002307 G.flag_break_continue = 0;
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002308#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002309 rcode = r = run_pipe(pi); /* NB: rcode is a smallint */
2310 if (r != -1) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002311 /* we only ran a builtin: rcode is already known
2312 * and we don't need to wait for anything. */
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002313#if ENABLE_HUSH_LOOPS
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002314 /* was it "break" or "continue"? */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002315 if (G.flag_break_continue) {
2316 smallint fbc = G.flag_break_continue;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002317 /* we might fall into outer *loop*,
2318 * don't want to break it too */
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002319 if (loop_top) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002320 G.depth_break_continue--;
2321 if (G.depth_break_continue == 0)
2322 G.flag_break_continue = 0;
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002323 /* else: e.g. "continue 2" should *break* once, *then* continue */
2324 } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002325 if (G.depth_break_continue != 0 || fbc == BC_BREAK)
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002326 goto check_jobs_and_break;
2327 /* "continue": simulate end of loop */
2328 rword = RES_DONE;
2329 continue;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002330 }
Denis Vlasenkodadfb492008-07-29 10:16:05 +00002331#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002332 } else if (pi->followup == PIPE_BG) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002333 /* what does bash do with attempts to background builtins? */
2334 /* even bash 3.2 doesn't do that well with nested bg:
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002335 * try "{ { sleep 10; echo DEEP; } & echo HERE; } &".
2336 * I'm NOT treating inner &'s as jobs */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002337#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00002338 if (G.run_list_level == 1)
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002339 insert_bg_job(pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002340#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002341 rcode = 0; /* EXIT_SUCCESS */
2342 } else {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002343#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00002344 if (G.run_list_level == 1 && G.interactive_fd) {
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002345 /* waits for completion, then fg's main shell */
2346 rcode = checkjobs_and_fg_shell(pi);
2347 debug_printf_exec(": checkjobs_and_fg_shell returned %d\n", rcode);
2348 } else
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002349#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002350 { /* this one just waits for completion */
2351 rcode = checkjobs(pi);
2352 debug_printf_exec(": checkjobs returned %d\n", rcode);
2353 }
Eric Andersen25f27032001-04-26 23:22:31 +00002354 }
2355 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002356 debug_printf_exec(": setting last_return_code=%d\n", rcode);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002357 G.last_return_code = rcode;
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002358
2359 /* Analyze how result affects subsequent commands */
Denis Vlasenko06810332007-05-21 23:30:54 +00002360#if ENABLE_HUSH_IF
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002361 if (rword == RES_IF || rword == RES_ELIF)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002362 cond_code = rcode;
Denis Vlasenko06810332007-05-21 23:30:54 +00002363#endif
2364#if ENABLE_HUSH_LOOPS
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002365 if (rword == RES_WHILE) {
Denis Vlasenko918a34b2008-07-28 23:17:31 +00002366 if (rcode) {
2367 rcode = 0; /* "while false; do...done" - exitcode 0 */
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002368 goto check_jobs_and_break;
Denis Vlasenko918a34b2008-07-28 23:17:31 +00002369 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002370 }
2371 if (rword == RES_UNTIL) {
Denis Vlasenkobcb25532008-07-28 23:04:34 +00002372 if (!rcode) {
2373 check_jobs_and_break:
2374 checkjobs(NULL);
2375 break;
2376 }
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002377 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002378#endif
Denis Vlasenko5e052ca2008-07-28 15:15:09 +00002379 if ((rcode == 0 && pi->followup == PIPE_OR)
2380 || (rcode != 0 && pi->followup == PIPE_AND)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002381 ) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002382 skip_more_for_this_rword = rword;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002383 }
Eric Andersen028b65b2001-06-28 01:10:11 +00002384 checkjobs(NULL);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002385 } /* for (pi) */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002386
2387#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00002388 if (G.ctrl_z_flag) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002389 /* ctrl-Z forked somewhere in the past, we are the child,
2390 * and now we completed running the list. Exit. */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002391//TODO: _exit?
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002392 exit(rcode);
2393 }
2394 ret:
Denis Vlasenko87a86552008-07-29 19:43:10 +00002395 if (!--G.run_list_level && G.interactive_fd) {
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00002396 signal(SIGTSTP, SIG_IGN);
2397 signal(SIGINT, SIG_IGN);
2398 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002399#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00002400 debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode);
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002401#if ENABLE_HUSH_LOOPS
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00002402 if (loop_top)
Denis Vlasenko87a86552008-07-29 19:43:10 +00002403 G.depth_of_loop--;
Denis Vlasenkobe709c22008-07-28 00:01:16 +00002404 free(for_list);
2405#endif
2406#if ENABLE_HUSH_CASE
2407 free(case_word);
2408#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002409 return rcode;
2410}
2411
Eric Andersen25f27032001-04-26 23:22:31 +00002412/* return code is the exit status of the pipe */
Eric Andersenbf7df042001-05-23 22:18:35 +00002413static int free_pipe(struct pipe *pi, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002414{
2415 char **p;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002416 struct command *command;
Eric Andersen25f27032001-04-26 23:22:31 +00002417 struct redir_struct *r, *rnext;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002418 int a, i, ret_code = 0;
Eric Andersen52a97ca2001-06-22 06:49:26 +00002419
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002420 if (pi->stopped_cmds > 0)
Eric Andersen52a97ca2001-06-22 06:49:26 +00002421 return ret_code;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002422 debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid());
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002423 for (i = 0; i < pi->num_cmds; i++) {
2424 command = &pi->cmds[i];
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002425 debug_printf_clean("%s command %d:\n", indenter(indent), i);
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002426 if (command->argv) {
2427 for (a = 0, p = command->argv; *p; a++, p++) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002428 debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p);
Eric Andersen25f27032001-04-26 23:22:31 +00002429 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002430 free_strings(command->argv);
2431 command->argv = NULL;
2432 } else if (command->group) {
2433 debug_printf_clean("%s begin group (subshell:%d)\n", indenter(indent), command->subshell);
2434 ret_code = free_pipe_list(command->group, indent+3);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002435 debug_printf_clean("%s end group\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002436 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002437 debug_printf_clean("%s (nil)\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002438 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002439 for (r = command->redirects; r; r = rnext) {
Denis Vlasenko211b59b2008-06-23 16:28:53 +00002440 debug_printf_clean("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->rd_type].descrip);
Eric Andersen25f27032001-04-26 23:22:31 +00002441 if (r->dup == -1) {
Eric Andersen817e73c2001-06-06 17:56:09 +00002442 /* guard against the case >$FOO, where foo is unset or blank */
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00002443 if (r->rd_filename) {
2444 debug_printf_clean(" %s\n", r->rd_filename);
2445 free(r->rd_filename);
2446 r->rd_filename = NULL;
Eric Andersen817e73c2001-06-06 17:56:09 +00002447 }
Eric Andersen25f27032001-04-26 23:22:31 +00002448 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002449 debug_printf_clean("&%d\n", r->dup);
Eric Andersen25f27032001-04-26 23:22:31 +00002450 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002451 rnext = r->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002452 free(r);
2453 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002454 command->redirects = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002455 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002456 free(pi->cmds); /* children are an array, they get freed all at once */
2457 pi->cmds = NULL;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002458#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002459 free(pi->cmdtext);
2460 pi->cmdtext = NULL;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002461#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002462 return ret_code;
2463}
2464
Eric Andersenbf7df042001-05-23 22:18:35 +00002465static int free_pipe_list(struct pipe *head, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002466{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002467 int rcode = 0; /* if list has no members */
Eric Andersen25f27032001-04-26 23:22:31 +00002468 struct pipe *pi, *next;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002469
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002470 for (pi = head; pi; pi = next) {
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002471#if HAS_KEYWORDS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002472 debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002473#endif
Eric Andersenbf7df042001-05-23 22:18:35 +00002474 rcode = free_pipe(pi, indent);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002475 debug_printf_clean("%s pipe followup code %d\n", indenter(indent), pi->followup);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002476 next = pi->next;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002477 /*pi->next = NULL;*/
Eric Andersen25f27032001-04-26 23:22:31 +00002478 free(pi);
2479 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002480 return rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00002481}
2482
2483/* Select which version we will use */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002484static int run_and_free_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002485{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002486 int rcode = 0;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002487 debug_printf_exec("run_and_free_list entered\n");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002488 if (!G.fake_mode) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002489 debug_printf_exec(": run_list with %d members\n", pi->num_cmds);
Denis Vlasenko05743d72008-02-10 12:10:08 +00002490 rcode = run_list(pi);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002491 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002492 /* free_pipe_list has the side effect of clearing memory.
Denis Vlasenko05743d72008-02-10 12:10:08 +00002493 * In the long run that function can be merged with run_list,
Eric Andersen25f27032001-04-26 23:22:31 +00002494 * but doing that now would hobble the debugging effort. */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002495 free_pipe_list(pi, /* indent: */ 0);
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002496 debug_printf_exec("run_and_free_list return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002497 return rcode;
2498}
2499
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002500
Denis Vlasenko170435c2007-05-23 13:01:10 +00002501/* expand_strvec_to_strvec() takes a list of strings, expands
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002502 * all variable references within and returns a pointer to
2503 * a list of expanded strings, possibly with larger number
2504 * of strings. (Think VAR="a b"; echo $VAR).
2505 * This new list is allocated as a single malloc block.
2506 * NULL-terminated list of char* pointers is at the beginning of it,
2507 * followed by strings themself.
2508 * Caller can deallocate entire list by single free(list). */
2509
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002510/* Store given string, finalizing the word and starting new one whenever
Denis Vlasenko87a86552008-07-29 19:43:10 +00002511 * we encounter IFS char(s). This is used for expanding variable values.
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002512 * End-of-string does NOT finalize word: think about 'echo -$VAR-' */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002513static int expand_on_ifs(o_string *output, int n, const char *str)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002514{
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002515 while (1) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002516 int word_len = strcspn(str, G.ifs);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002517 if (word_len) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00002518 if (output->o_quote || !output->o_glob)
2519 o_addQstr(output, str, word_len);
2520 else /* protect backslashes against globbing up :) */
2521 o_addstr_duplicate_backslash(output, str, word_len);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002522 str += word_len;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002523 }
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002524 if (!*str) /* EOL - do not finalize word */
2525 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002526 o_addchr(output, '\0');
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002527 debug_print_list("expand_on_ifs", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002528 n = o_save_ptr(output, n);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002529 str += strspn(str, G.ifs); /* skip ifs chars */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002530 }
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002531 debug_print_list("expand_on_ifs[1]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002532 return n;
2533}
2534
2535/* Expand all variable references in given string, adding words to list[]
2536 * at n, n+1,... positions. Return updated n (so that list[n] is next one
2537 * to be filled). This routine is extremely tricky: has to deal with
2538 * variables/parameters with whitespace, $* and $@, and constructs like
2539 * 'echo -$*-'. If you play here, you must run testsuite afterwards! */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002540static int expand_vars_to_list(o_string *output, int n, char *arg, char or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002541{
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002542 /* or_mask is either 0 (normal case) or 0x80
Denis Vlasenko55789c62008-06-18 16:30:42 +00002543 * (expansion of right-hand side of assignment == 1-element expand.
2544 * It will also do no globbing, and thus we must not backslash-quote!) */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002545
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002546 char first_ch, ored_ch;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002547 int i;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002548 const char *val;
2549 char *p;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002550
2551 ored_ch = 0;
2552
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002553 debug_printf_expand("expand_vars_to_list: arg '%s'\n", arg);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002554 debug_print_list("expand_vars_to_list", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002555 n = o_save_ptr(output, n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002556 debug_print_list("expand_vars_to_list[0]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002557
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002558 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) {
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002559#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002560 o_string subst_result = NULL_O_STRING;
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002561#endif
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002562
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002563 o_addstr(output, arg, p - arg);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002564 debug_print_list("expand_vars_to_list[1]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002565 arg = ++p;
2566 p = strchr(p, SPECIAL_VAR_SYMBOL);
2567
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002568 first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00002569 /* "$@" is special. Even if quoted, it can still
2570 * expand to nothing (not even an empty string) */
2571 if ((first_ch & 0x7f) != '@')
2572 ored_ch |= first_ch;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002573 val = NULL;
2574 switch (first_ch & 0x7f) {
2575 /* Highest bit in first_ch indicates that var is double-quoted */
2576 case '$': /* pid */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002577 val = utoa(G.root_pid);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002578 break;
2579 case '!': /* bg pid */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002580 val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)"";
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002581 break;
2582 case '?': /* exitcode */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002583 val = utoa(G.last_return_code);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002584 break;
2585 case '#': /* argc */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002586 val = utoa(G.global_argc ? G.global_argc-1 : 0);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002587 break;
2588 case '*':
2589 case '@':
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002590 i = 1;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002591 if (!G.global_argv[i])
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002592 break;
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00002593 ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002594 if (!(first_ch & 0x80)) { /* unquoted $* or $@ */
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002595 smallint sv = output->o_quote;
2596 /* unquoted var's contents should be globbed, so don't quote */
2597 output->o_quote = 0;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002598 while (G.global_argv[i]) {
2599 n = expand_on_ifs(output, n, G.global_argv[i]);
2600 debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1);
2601 if (G.global_argv[i++][0] && G.global_argv[i]) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002602 /* this argv[] is not empty and not last:
2603 * put terminating NUL, start new word */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002604 o_addchr(output, '\0');
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002605 debug_print_list("expand_vars_to_list[2]", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002606 n = o_save_ptr(output, n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002607 debug_print_list("expand_vars_to_list[3]", output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002608 }
2609 }
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002610 output->o_quote = sv;
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002611 } else
2612 /* If or_mask is nonzero, we handle assignment 'a=....$@.....'
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00002613 * and in this case should treat it like '$*' - see 'else...' below */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002614 if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002615 while (1) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002616 o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i]));
2617 if (++i >= G.global_argc)
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002618 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002619 o_addchr(output, '\0');
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002620 debug_print_list("expand_vars_to_list[4]", output, n);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002621 n = o_save_ptr(output, n);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002622 }
2623 } else { /* quoted $*: add as one word */
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002624 while (1) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00002625 o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i]));
2626 if (!G.global_argv[++i])
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002627 break;
Denis Vlasenko87a86552008-07-29 19:43:10 +00002628 if (G.ifs[0])
2629 o_addchr(output, G.ifs[0]);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002630 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002631 }
2632 break;
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00002633 case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */
2634 /* "Empty variable", used to make "" etc to not disappear */
2635 arg++;
2636 ored_ch = 0x80;
2637 break;
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002638#if ENABLE_HUSH_TICK
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00002639 case '`': { /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002640 struct in_str input;
2641 *p = '\0';
2642 arg++;
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002643//TODO: can we just stuff it into "output" directly?
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002644 debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002645 setup_string_in_str(&input, arg);
2646 process_command_subs(&subst_result, &input, NULL);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002647 debug_printf_subst("SUBST RES '%s'\n", subst_result.data);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002648 val = subst_result.data;
2649 goto store_val;
2650 }
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002651#endif
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00002652 default: /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002653 *p = '\0';
2654 arg[0] = first_ch & 0x7f;
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002655 if (isdigit(arg[0])) {
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002656 i = xatoi_u(arg);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002657 if (i < G.global_argc)
2658 val = G.global_argv[i];
Denis Vlasenko55789c62008-06-18 16:30:42 +00002659 /* else val remains NULL: $N with too big N */
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002660 } else
2661 val = lookup_param(arg);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002662 arg[0] = first_ch;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002663#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002664 store_val:
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002665#endif
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002666 *p = SPECIAL_VAR_SYMBOL;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002667 if (!(first_ch & 0x80)) { /* unquoted $VAR */
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002668 debug_printf_expand("unquoted '%s', output->o_quote:%d\n", val, output->o_quote);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002669 if (val) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00002670 /* unquoted var's contents should be globbed, so don't quote */
2671 smallint sv = output->o_quote;
2672 output->o_quote = 0;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002673 n = expand_on_ifs(output, n, val);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002674 val = NULL;
Denis Vlasenko55789c62008-06-18 16:30:42 +00002675 output->o_quote = sv;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002676 }
Denis Vlasenko55789c62008-06-18 16:30:42 +00002677 } else { /* quoted $VAR, val will be appended below */
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002678 debug_printf_expand("quoted '%s', output->o_quote:%d\n", val, output->o_quote);
Denis Vlasenko55789c62008-06-18 16:30:42 +00002679 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002680 }
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002681 if (val) {
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002682 o_addQstr(output, val, strlen(val));
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00002683 }
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002684
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002685#if ENABLE_HUSH_TICK
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002686 o_free(&subst_result);
Denis Vlasenkoccce59d2008-06-16 14:35:57 +00002687#endif
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002688 arg = ++p;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002689 } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */
2690
Denis Vlasenko2e76c3f2008-06-10 18:27:50 +00002691 if (arg[0]) {
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002692 debug_print_list("expand_vars_to_list[a]", output, n);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002693 /* this part is literal, and it was already pre-quoted
2694 * if needed (much earlier), do not use o_addQstr here! */
2695 o_addstr(output, arg, strlen(arg) + 1);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002696 debug_print_list("expand_vars_to_list[b]", output, n);
Denis Vlasenko2e76c3f2008-06-10 18:27:50 +00002697 } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */
2698 && !(ored_ch & 0x80) /* and all vars were not quoted. */
2699 ) {
2700 n--;
2701 /* allow to reuse list[n] later without re-growth */
2702 output->has_empty_slot = 1;
2703 } else {
2704 o_addchr(output, '\0');
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002705 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002706 return n;
2707}
2708
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002709static char **expand_variables(char **argv, int or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002710{
2711 int n;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00002712 char **list;
2713 char **v;
2714 o_string output = NULL_O_STRING;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002715
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002716 if (or_mask & 0x100) {
Denis Vlasenko324a3fd2008-06-18 17:49:58 +00002717 output.o_quote = 1; /* protect against globbing for "$var" */
2718 /* (unquoted $var will temporarily switch it off) */
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002719 output.o_glob = 1;
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00002720 }
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002721
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002722 n = 0;
2723 v = argv;
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002724 while (*v) {
2725 n = expand_vars_to_list(&output, n, *v, (char)or_mask);
2726 v++;
2727 }
2728 debug_print_list("expand_variables", &output, n);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002729
Denis Vlasenko82dfec32008-06-16 12:47:11 +00002730 /* output.data (malloced in one block) gets returned in "list" */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00002731 list = o_finalize_list(&output, n);
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002732 debug_print_strings("expand_variables[1]", list);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002733 return list;
2734}
2735
Denis Vlasenko170435c2007-05-23 13:01:10 +00002736static char **expand_strvec_to_strvec(char **argv)
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002737{
Denis Vlasenkob61e13d2008-06-17 05:11:43 +00002738 return expand_variables(argv, 0x100);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002739}
2740
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002741/* Used for expansion of right hand of assignments */
Denis Vlasenko82dfec32008-06-16 12:47:11 +00002742/* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs
2743 * "v=/bin/c*" */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002744static char *expand_string_to_string(const char *str)
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002745{
2746 char *argv[2], **list;
2747
2748 argv[0] = (char*)str;
2749 argv[1] = NULL;
2750 list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002751 if (HUSH_DEBUG)
Denis Vlasenko170435c2007-05-23 13:01:10 +00002752 if (!list[0] || list[1])
2753 bb_error_msg_and_die("BUG in varexp2");
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002754 /* actually, just move string 2*sizeof(char*) bytes back */
2755 strcpy((char*)list, list[0]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00002756 debug_printf_expand("string_to_string='%s'\n", (char*)list);
2757 return (char*)list;
2758}
2759
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002760/* Used for "eval" builtin */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002761static char* expand_strvec_to_string(char **argv)
2762{
2763 char **list;
2764
2765 list = expand_variables(argv, 0x80);
2766 /* Convert all NULs to spaces */
2767 if (list[0]) {
2768 int n = 1;
2769 while (list[n]) {
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002770 if (HUSH_DEBUG)
Denis Vlasenko170435c2007-05-23 13:01:10 +00002771 if (list[n-1] + strlen(list[n-1]) + 1 != list[n])
2772 bb_error_msg_and_die("BUG in varexp3");
Denis Vlasenko87a86552008-07-29 19:43:10 +00002773 list[n][-1] = ' '; /* TODO: or to G.ifs[0]? */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002774 n++;
2775 }
2776 }
2777 strcpy((char*)list, list[0]);
2778 debug_printf_expand("strvec_to_string='%s'\n", (char*)list);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002779 return (char*)list;
2780}
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002781
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00002782
2783/* Used to get/check local shell variables */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002784static struct variable *get_local_var(const char *name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002785{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002786 struct variable *cur;
2787 int len;
Eric Andersenf72f5622001-05-15 23:21:41 +00002788
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002789 if (!name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002790 return NULL;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002791 len = strlen(name);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002792 for (cur = G.top_var; cur; cur = cur->next) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002793 if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=')
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002794 return cur;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002795 }
Eric Andersenf72f5622001-05-15 23:21:41 +00002796 return NULL;
2797}
2798
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002799/* str holds "NAME=VAL" and is expected to be malloced.
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002800 * We take ownership of it. */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002801static int set_local_var(char *str, int flg_export)
Eric Andersen78a7c992001-05-15 16:30:25 +00002802{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002803 struct variable *cur;
2804 char *value;
2805 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002806
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002807 value = strchr(str, '=');
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002808 if (!value) { /* not expected to ever happen? */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002809 free(str);
Eric Andersen99785762001-05-22 21:37:48 +00002810 return -1;
2811 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002812
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002813 name_len = value - str + 1; /* including '=' */
Denis Vlasenko87a86552008-07-29 19:43:10 +00002814 cur = G.top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002815 while (1) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002816 if (strncmp(cur->varstr, str, name_len) != 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002817 if (!cur->next) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002818 /* Bail out. Note that now cur points
2819 * to last var in linked list */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002820 break;
Eric Andersen20a69a72001-05-15 17:24:44 +00002821 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002822 cur = cur->next;
2823 continue;
Eric Andersen20a69a72001-05-15 17:24:44 +00002824 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002825 /* We found an existing var with this name */
2826 *value = '\0';
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002827 if (cur->flg_read_only) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002828 bb_error_msg("%s: readonly variable", str);
2829 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002830 return -1;
2831 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002832 unsetenv(str); /* just in case */
2833 *value = '=';
2834 if (strcmp(cur->varstr, str) == 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002835 free_and_exp:
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002836 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002837 goto exp;
2838 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002839 if (cur->max_len >= strlen(str)) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002840 /* This one is from startup env, reuse space */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002841 strcpy(cur->varstr, str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002842 goto free_and_exp;
2843 }
2844 /* max_len == 0 signifies "malloced" var, which we can
2845 * (and has to) free */
2846 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002847 free(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002848 cur->max_len = 0;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002849 goto set_str_and_exp;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002850 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002851
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002852 /* Not found - create next variable struct */
2853 cur->next = xzalloc(sizeof(*cur));
2854 cur = cur->next;
2855
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002856 set_str_and_exp:
2857 cur->varstr = str;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002858 exp:
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002859 if (flg_export)
2860 cur->flg_export = 1;
2861 if (cur->flg_export)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002862 return putenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002863 return 0;
Eric Andersen20a69a72001-05-15 17:24:44 +00002864}
2865
Eric Andersenf72f5622001-05-15 23:21:41 +00002866static void unset_local_var(const char *name)
Eric Andersen20a69a72001-05-15 17:24:44 +00002867{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002868 struct variable *cur;
2869 struct variable *prev = prev; /* for gcc */
2870 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002871
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002872 if (!name)
2873 return;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002874 name_len = strlen(name);
Denis Vlasenko87a86552008-07-29 19:43:10 +00002875 cur = G.top_var;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002876 while (cur) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002877 if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002878 if (cur->flg_read_only) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002879 bb_error_msg("%s: readonly variable", name);
Eric Andersen94ac2442001-05-22 19:05:18 +00002880 return;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002881 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002882 /* prev is ok to use here because 1st variable, HUSH_VERSION,
2883 * is ro, and we cannot reach this code on the 1st pass */
2884 prev->next = cur->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002885 unsetenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002886 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002887 free(cur->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002888 free(cur);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002889 return;
Eric Andersenf72f5622001-05-15 23:21:41 +00002890 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002891 prev = cur;
2892 cur = cur->next;
Eric Andersen20a69a72001-05-15 17:24:44 +00002893 }
Eric Andersen78a7c992001-05-15 16:30:25 +00002894}
2895
Denis Vlasenkoff182a32008-07-05 20:29:59 +00002896/* The src parameter allows us to peek forward to a possible &n syntax
Eric Andersen25f27032001-04-26 23:22:31 +00002897 * for file descriptor duplication, e.g., "2>&1".
2898 * Return code is 0 normally, 1 if a syntax error is detected in src.
2899 * Resource errors (in xmalloc) cause the process to exit */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002900static int setup_redirect(struct parse_context *ctx, int fd, redir_type style,
Eric Andersen25f27032001-04-26 23:22:31 +00002901 struct in_str *input)
2902{
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002903 struct command *command = ctx->command;
2904 struct redir_struct *redir = command->redirects;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002905 struct redir_struct *last_redir = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002906
2907 /* Create a new redir_struct and drop it onto the end of the linked list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002908 while (redir) {
2909 last_redir = redir;
2910 redir = redir->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002911 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00002912 redir = xzalloc(sizeof(struct redir_struct));
2913 /* redir->next = NULL; */
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00002914 /* redir->rd_filename = NULL; */
Eric Andersen25f27032001-04-26 23:22:31 +00002915 if (last_redir) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002916 last_redir->next = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002917 } else {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002918 command->redirects = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002919 }
2920
Denis Vlasenko55789c62008-06-18 16:30:42 +00002921 redir->rd_type = style;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002922 redir->fd = (fd == -1) ? redir_table[style].default_fd : fd;
Eric Andersen25f27032001-04-26 23:22:31 +00002923
2924 debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
2925
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002926 /* Check for a '2>&1' type redirect */
Eric Andersen25f27032001-04-26 23:22:31 +00002927 redir->dup = redirect_dup_num(input);
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002928 if (redir->dup == -2)
2929 return 1; /* syntax error */
Eric Andersen25f27032001-04-26 23:22:31 +00002930 if (redir->dup != -1) {
2931 /* Erik had a check here that the file descriptor in question
Eric Andersen83a2ae22001-05-07 17:59:25 +00002932 * is legit; I postpone that to "run time"
2933 * A "-" representation of "close me" shows up as a -3 here */
Eric Andersen25f27032001-04-26 23:22:31 +00002934 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
2935 } else {
2936 /* We do _not_ try to open the file that src points to,
2937 * since we need to return and let src be expanded first.
2938 * Set ctx->pending_redirect, so we know what to do at the
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002939 * end of the next parsed word. */
Eric Andersen25f27032001-04-26 23:22:31 +00002940 ctx->pending_redirect = redir;
2941 }
2942 return 0;
2943}
2944
Denis Vlasenkoac678ec2007-04-16 22:32:04 +00002945static struct pipe *new_pipe(void)
2946{
Eric Andersen25f27032001-04-26 23:22:31 +00002947 struct pipe *pi;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002948 pi = xzalloc(sizeof(struct pipe));
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002949 /*pi->followup = 0; - deliberately invalid value */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002950 /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */
Eric Andersen25f27032001-04-26 23:22:31 +00002951 return pi;
2952}
2953
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002954static void initialize_context(struct parse_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00002955{
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002956 memset(ctx, 0, sizeof(*ctx));
Denis Vlasenko1a735862007-05-23 00:32:25 +00002957 ctx->pipe = ctx->list_head = new_pipe();
Denis Vlasenko9af22c72008-10-09 12:54:58 +00002958 /* Create the memory for command, roughly:
2959 * ctx->pipe->cmds = new struct command;
2960 * ctx->command = &ctx->pipe->cmds[0];
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002961 */
2962 done_command(ctx);
Eric Andersen25f27032001-04-26 23:22:31 +00002963}
2964
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002965/* If a reserved word is found and processed, parse context is modified
2966 * and 1 is returned.
2967 * Handles if, then, elif, else, fi, for, while, until, do, done.
Eric Andersen25f27032001-04-26 23:22:31 +00002968 * case, function, and select are obnoxious, save those for later.
2969 */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00002970#if HAS_KEYWORDS
Denis Vlasenkoc3735272008-10-09 12:58:26 +00002971struct reserved_combo {
2972 char literal[6];
2973 unsigned char res;
2974 unsigned char assignment_flag;
2975 int flag;
2976};
2977enum {
2978 FLAG_END = (1 << RES_NONE ),
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002979#if ENABLE_HUSH_IF
Denis Vlasenkoc3735272008-10-09 12:58:26 +00002980 FLAG_IF = (1 << RES_IF ),
2981 FLAG_THEN = (1 << RES_THEN ),
2982 FLAG_ELIF = (1 << RES_ELIF ),
2983 FLAG_ELSE = (1 << RES_ELSE ),
2984 FLAG_FI = (1 << RES_FI ),
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002985#endif
2986#if ENABLE_HUSH_LOOPS
Denis Vlasenkoc3735272008-10-09 12:58:26 +00002987 FLAG_FOR = (1 << RES_FOR ),
2988 FLAG_WHILE = (1 << RES_WHILE),
2989 FLAG_UNTIL = (1 << RES_UNTIL),
2990 FLAG_DO = (1 << RES_DO ),
2991 FLAG_DONE = (1 << RES_DONE ),
2992 FLAG_IN = (1 << RES_IN ),
Denis Vlasenkoa8442002008-06-14 11:00:17 +00002993#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002994#if ENABLE_HUSH_CASE
Denis Vlasenkoc3735272008-10-09 12:58:26 +00002995 FLAG_MATCH = (1 << RES_MATCH),
2996 FLAG_ESAC = (1 << RES_ESAC ),
Denis Vlasenko17f02e72008-07-14 04:32:29 +00002997#endif
Denis Vlasenkoc3735272008-10-09 12:58:26 +00002998 FLAG_START = (1 << RES_XXXX ),
2999};
3000
3001static const struct reserved_combo* match_reserved_word(o_string *word)
3002{
Eric Andersen25f27032001-04-26 23:22:31 +00003003 /* Mostly a list of accepted follow-up reserved words.
3004 * FLAG_END means we are done with the sequence, and are ready
3005 * to turn the compound list into a command.
3006 * FLAG_START means the word must start a new compound list.
3007 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003008 static const struct reserved_combo reserved_list[] = {
Denis Vlasenko06810332007-05-21 23:30:54 +00003009#if ENABLE_HUSH_IF
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003010 { "!", RES_NONE, NOT_ASSIGNMENT , 0 },
3011 { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START },
3012 { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
3013 { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN },
3014 { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI },
3015 { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END },
Denis Vlasenko06810332007-05-21 23:30:54 +00003016#endif
3017#if ENABLE_HUSH_LOOPS
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003018 { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START },
3019 { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START },
3020 { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START },
3021 { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO },
3022 { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE },
3023 { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END },
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003024#endif
3025#if ENABLE_HUSH_CASE
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003026 { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START },
3027 { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END },
Denis Vlasenko06810332007-05-21 23:30:54 +00003028#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003029 };
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003030 const struct reserved_combo *r;
3031
3032 for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) {
3033 if (strcmp(word->data, r->literal) == 0)
3034 return r;
3035 }
3036 return NULL;
3037}
3038static int reserved_word(o_string *word, struct parse_context *ctx)
3039{
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003040#if ENABLE_HUSH_CASE
3041 static const struct reserved_combo reserved_match = {
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003042 "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003043 };
3044#endif
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003045 const struct reserved_combo *r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003046
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003047 r = match_reserved_word(word);
3048 if (!r)
3049 return 0;
3050
3051 debug_printf("found reserved word %s, res %d\n", r->literal, r->res);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003052#if ENABLE_HUSH_CASE
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003053 if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE)
3054 /* "case word IN ..." - IN part starts first match part */
3055 r = &reserved_match;
3056 else
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003057#endif
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003058 if (r->flag == 0) { /* '!' */
3059 if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003060 syntax(NULL);
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003061 IF_HAS_KEYWORDS(ctx->ctx_res_w = RES_SNTX;)
Eric Andersen25f27032001-04-26 23:22:31 +00003062 }
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003063 ctx->ctx_inverted = 1;
Denis Vlasenko1a735862007-05-23 00:32:25 +00003064 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003065 }
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003066 if (r->flag & FLAG_START) {
3067 struct parse_context *new;
3068 debug_printf("push stack\n");
3069 new = xmalloc(sizeof(*new));
3070 *new = *ctx; /* physical copy */
3071 initialize_context(ctx);
3072 ctx->stack = new;
3073 } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) {
3074 syntax(NULL);
3075 ctx->ctx_res_w = RES_SNTX;
3076 return 1;
3077 }
3078 ctx->ctx_res_w = r->res;
3079 ctx->old_flag = r->flag;
3080 if (ctx->old_flag & FLAG_END) {
3081 struct parse_context *old;
3082 debug_printf("pop stack\n");
3083 done_pipe(ctx, PIPE_SEQ);
3084 old = ctx->stack;
3085 old->command->group = ctx->list_head;
3086 old->command->subshell = 0;
3087 *ctx = *old; /* physical copy */
3088 free(old);
3089 }
3090 word->o_assignment = r->assignment_flag;
3091 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003092}
Denis Vlasenko06810332007-05-21 23:30:54 +00003093#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003094
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003095/* Word is complete, look at it and update parsing context.
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003096 * Normal return is 0. Syntax errors return 1. */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003097static int done_word(o_string *word, struct parse_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00003098{
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003099 struct command *command = ctx->command;
Eric Andersen25f27032001-04-26 23:22:31 +00003100
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003101 debug_printf_parse("done_word entered: '%s' %p\n", word->data, command);
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003102 if (word->length == 0 && word->nonnull == 0) {
3103 debug_printf_parse("done_word return 0: true null, ignored\n");
3104 return 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003105 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003106 /* If this word wasn't an assignment, next ones definitely
3107 * can't be assignments. Even if they look like ones. */
3108 if (word->o_assignment != DEFINITELY_ASSIGNMENT
3109 && word->o_assignment != WORD_IS_KEYWORD
3110 ) {
3111 word->o_assignment = NOT_ASSIGNMENT;
3112 } else {
3113 if (word->o_assignment == DEFINITELY_ASSIGNMENT)
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003114 command->assignment_cnt++;
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003115 word->o_assignment = MAYBE_ASSIGNMENT;
3116 }
3117
Eric Andersen25f27032001-04-26 23:22:31 +00003118 if (ctx->pending_redirect) {
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003119 /* We do not glob in e.g. >*.tmp case. bash seems to glob here
3120 * only if run as "bash", not "sh" */
3121 ctx->pending_redirect->rd_filename = xstrdup(word->data);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003122 word->o_assignment = NOT_ASSIGNMENT;
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003123 debug_printf("word stored in rd_filename: '%s'\n", word->data);
Eric Andersen25f27032001-04-26 23:22:31 +00003124 } else {
Denis Vlasenko395ae452008-07-14 06:29:38 +00003125 /* "{ echo foo; } echo bar" - bad */
3126 /* NB: bash allows e.g. "if true; then { echo foo; } fi". TODO? */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003127 if (command->group) {
Denis Vlasenko395ae452008-07-14 06:29:38 +00003128 syntax(NULL);
3129 debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n");
3130 return 1;
3131 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003132#if HAS_KEYWORDS
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003133#if ENABLE_HUSH_CASE
Denis Vlasenko757361f2008-07-14 08:26:47 +00003134 if (ctx->ctx_dsemicolon
3135 && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */
3136 ) {
Denis Vlasenko395ae452008-07-14 06:29:38 +00003137 /* already done when ctx_dsemicolon was set to 1: */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003138 /* ctx->ctx_res_w = RES_MATCH; */
3139 ctx->ctx_dsemicolon = 0;
3140 } else
3141#endif
3142
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003143 if (!command->argv /* if it's the first word... */
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003144#if ENABLE_HUSH_LOOPS
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003145 && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */
3146 && ctx->ctx_res_w != RES_IN
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003147#endif
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003148 ) {
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003149 debug_printf_parse(": checking '%s' for reserved-ness\n", word->data);
3150 if (reserved_word(word, ctx)) {
3151 o_reset(word);
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003152 debug_printf_parse("done_word return %d\n", (ctx->ctx_res_w == RES_SNTX));
3153 return (ctx->ctx_res_w == RES_SNTX);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003154 }
Eric Andersen25f27032001-04-26 23:22:31 +00003155 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003156#endif
Denis Vlasenkoff182a32008-07-05 20:29:59 +00003157 if (word->nonnull /* word had "xx" or 'xx' at least as part of it. */
Denis Vlasenko55789c62008-06-18 16:30:42 +00003158 /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */
3159 && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL)
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003160 /* (otherwise it's known to be not empty and is already safe) */
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003161 ) {
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003162 /* exclude "$@" - it can expand to no word despite "" */
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00003163 char *p = word->data;
3164 while (p[0] == SPECIAL_VAR_SYMBOL
3165 && (p[1] & 0x7f) == '@'
3166 && p[2] == SPECIAL_VAR_SYMBOL
3167 ) {
3168 p += 3;
3169 }
3170 if (p == word->data || p[0] != '\0') {
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003171 /* saw no "$@", or not only "$@" but some
3172 * real text is there too */
3173 /* insert "empty variable" reference, this makes
Denis Vlasenkoafdcd122008-07-05 17:40:04 +00003174 * e.g. "", $empty"" etc to not disappear */
3175 o_addchr(word, SPECIAL_VAR_SYMBOL);
3176 o_addchr(word, SPECIAL_VAR_SYMBOL);
3177 }
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00003178 }
Denis Vlasenko22d10a02008-10-13 08:53:43 +00003179 command->argv = add_string_to_strings(command->argv, xstrdup(word->data));
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003180 debug_print_strings("word appended to argv", command->argv);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003181 }
Eric Andersen25f27032001-04-26 23:22:31 +00003182
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003183 o_reset(word);
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003184 ctx->pending_redirect = NULL;
3185
Denis Vlasenko06810332007-05-21 23:30:54 +00003186#if ENABLE_HUSH_LOOPS
Denis Vlasenkoab876cd2008-06-18 16:29:32 +00003187 /* Force FOR to have just one word (variable name) */
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003188 /* NB: basically, this makes hush see "for v in ..." syntax as if
3189 * as it is "for v; in ...". FOR and IN become two pipe structs
3190 * in parse tree. */
3191 if (ctx->ctx_res_w == RES_FOR) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003192//TODO: check that command->argv[0] is a valid variable name!
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003193 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003194 }
Denis Vlasenko06810332007-05-21 23:30:54 +00003195#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003196#if ENABLE_HUSH_CASE
3197 /* Force CASE to have just one word */
3198 if (ctx->ctx_res_w == RES_CASE) {
3199 done_pipe(ctx, PIPE_SEQ);
3200 }
3201#endif
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003202 debug_printf_parse("done_word return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003203 return 0;
3204}
3205
Denis Vlasenko6eaf8de2008-06-17 12:09:21 +00003206/* Command (member of a pipe) is complete. The only possible error here
3207 * is out of memory, in which case xmalloc exits. */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003208static int done_command(struct parse_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00003209{
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003210 /* The command is really already in the pipe structure, so
3211 * advance the pipe counter and make a new, null command. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003212 struct pipe *pi = ctx->pipe;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003213 struct command *command = ctx->command;
Eric Andersen25f27032001-04-26 23:22:31 +00003214
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003215 if (command) {
3216 if (command->group == NULL
3217 && command->argv == NULL
3218 && command->redirects == NULL
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003219 ) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003220 debug_printf_parse("done_command: skipping null cmd, num_cmds=%d\n", pi->num_cmds);
3221 return pi->num_cmds;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003222 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003223 pi->num_cmds++;
3224 debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds);
Eric Andersen25f27032001-04-26 23:22:31 +00003225 } else {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003226 debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds);
Eric Andersen25f27032001-04-26 23:22:31 +00003227 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003228
3229 /* Only real trickiness here is that the uncommitted
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003230 * command structure is not counted in pi->num_cmds. */
3231 pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1));
3232 command = &pi->cmds[pi->num_cmds];
3233 memset(command, 0, sizeof(*command));
Eric Andersen25f27032001-04-26 23:22:31 +00003234
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003235 ctx->command = command;
Eric Andersen25f27032001-04-26 23:22:31 +00003236 /* but ctx->pipe and ctx->list_head remain unchanged */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003237
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003238 return pi->num_cmds; /* used only for 0/nonzero check */
Eric Andersen25f27032001-04-26 23:22:31 +00003239}
3240
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003241static void done_pipe(struct parse_context *ctx, pipe_style type)
Eric Andersen25f27032001-04-26 23:22:31 +00003242{
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003243 int not_null;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003244
3245 debug_printf_parse("done_pipe entered, followup %d\n", type);
Denis Vlasenko395ae452008-07-14 06:29:38 +00003246 /* Close previous command */
3247 not_null = done_command(ctx);
Eric Andersen25f27032001-04-26 23:22:31 +00003248 ctx->pipe->followup = type;
Denis Vlasenko5ec61322008-06-24 00:50:07 +00003249 IF_HAS_KEYWORDS(ctx->pipe->pi_inverted = ctx->ctx_inverted;)
3250 IF_HAS_KEYWORDS(ctx->ctx_inverted = 0;)
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003251 IF_HAS_KEYWORDS(ctx->pipe->res_word = ctx->ctx_res_w;)
Denis Vlasenko395ae452008-07-14 06:29:38 +00003252
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003253 /* Without this check, even just <enter> on command line generates
3254 * tree of three NOPs (!). Which is harmless but annoying.
Denis Vlasenko6eaf8de2008-06-17 12:09:21 +00003255 * IOW: it is safe to do it unconditionally.
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003256 * RES_NONE case is for "for a in; do ..." (empty IN set)
3257 * to work, possibly other cases too. */
3258 if (not_null IF_HAS_KEYWORDS(|| ctx->ctx_res_w != RES_NONE)) {
Denis Vlasenko395ae452008-07-14 06:29:38 +00003259 struct pipe *new_p;
3260 debug_printf_parse("done_pipe: adding new pipe: "
Denis Vlasenko757361f2008-07-14 08:26:47 +00003261 "not_null:%d ctx->ctx_res_w:%d\n",
Denis Vlasenko395ae452008-07-14 06:29:38 +00003262 not_null, ctx->ctx_res_w);
3263 new_p = new_pipe();
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003264 ctx->pipe->next = new_p;
3265 ctx->pipe = new_p;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003266 ctx->command = NULL; /* needed! */
Denis Vlasenko395ae452008-07-14 06:29:38 +00003267 /* RES_THEN, RES_DO etc are "sticky" -
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003268 * they remain set for commands inside if/while.
3269 * This is used to control execution.
3270 * RES_FOR and RES_IN are NOT sticky (needed to support
3271 * cases where variable or value happens to match a keyword):
3272 */
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003273#if ENABLE_HUSH_LOOPS
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003274 if (ctx->ctx_res_w == RES_FOR
3275 || ctx->ctx_res_w == RES_IN)
3276 ctx->ctx_res_w = RES_NONE;
Denis Vlasenko6bdff082008-07-09 20:14:53 +00003277#endif
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003278#if ENABLE_HUSH_CASE
3279 if (ctx->ctx_res_w == RES_MATCH)
3280 ctx->ctx_res_w = RES_CASEI;
3281#endif
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003282 /* Create the memory for command, roughly:
3283 * ctx->pipe->cmds = new struct command;
3284 * ctx->command = &ctx->pipe->cmds[0];
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003285 */
3286 done_command(ctx);
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003287 }
Denis Vlasenkoa8442002008-06-14 11:00:17 +00003288 debug_printf_parse("done_pipe return\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003289}
3290
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003291/* Peek ahead in the in_str to find out if we have a "&n" construct,
Eric Andersen25f27032001-04-26 23:22:31 +00003292 * as in "2>&1", that represents duplicating a file descriptor.
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003293 * Return either -2 (syntax error), -1 (no &), or the number found.
Eric Andersen25f27032001-04-26 23:22:31 +00003294 */
3295static int redirect_dup_num(struct in_str *input)
3296{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003297 int ch, d = 0, ok = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003298 ch = i_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003299 if (ch != '&') return -1;
3300
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003301 i_getch(input); /* get the & */
3302 ch = i_peek(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003303 if (ch == '-') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003304 i_getch(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003305 return -3; /* "-" represents "close me" */
3306 }
3307 while (isdigit(ch)) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00003308 d = d*10 + (ch-'0');
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003309 ok = 1;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003310 i_getch(input);
3311 ch = i_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003312 }
3313 if (ok) return d;
3314
Manuel Novoa III cad53642003-03-19 09:13:01 +00003315 bb_error_msg("ambiguous redirect");
Eric Andersen25f27032001-04-26 23:22:31 +00003316 return -2;
3317}
3318
3319/* If a redirect is immediately preceded by a number, that number is
3320 * supposed to tell which file descriptor to redirect. This routine
3321 * looks for such preceding numbers. In an ideal world this routine
3322 * needs to handle all the following classes of redirects...
3323 * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
3324 * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
3325 * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
3326 * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
3327 * A -1 output from this program means no valid number was found, so the
3328 * caller should use the appropriate default for this redirection.
3329 */
3330static int redirect_opt_num(o_string *o)
3331{
3332 int num;
3333
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003334 if (o->length == 0)
3335 return -1;
3336 for (num = 0; num < o->length; num++) {
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00003337 if (!isdigit(o->data[num])) {
Eric Andersen25f27032001-04-26 23:22:31 +00003338 return -1;
3339 }
3340 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003341 num = atoi(o->data);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003342 o_reset(o);
Eric Andersen25f27032001-04-26 23:22:31 +00003343 return num;
3344}
3345
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003346#if ENABLE_HUSH_TICK
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00003347static FILE *generate_stream_from_list(struct pipe *head)
Eric Andersen25f27032001-04-26 23:22:31 +00003348{
3349 FILE *pf;
Eric Andersen25f27032001-04-26 23:22:31 +00003350 int pid, channel[2];
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003351
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +00003352 xpipe(channel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003353/* *** NOMMU WARNING *** */
3354/* By using vfork here, we suspend parent till child exits or execs.
3355 * If child will not do it before it fills the pipe, it can block forever
3356 * in write(STDOUT_FILENO), and parent (shell) will be also stuck.
Denis Vlasenko3fe4f982008-06-09 16:02:39 +00003357 * Try this script:
3358 * yes "0123456789012345678901234567890" | dd bs=32 count=64k >TESTFILE
3359 * huge=`cat TESTFILE` # will block here forever
3360 * echo OK
Denis Vlasenko05743d72008-02-10 12:10:08 +00003361 */
Denis Vlasenko82604e92008-07-01 15:59:42 +00003362 pid = BB_MMU ? fork() : vfork();
3363 if (pid < 0)
3364 bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork");
Denis Vlasenko05743d72008-02-10 12:10:08 +00003365 if (pid == 0) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00003366 if (ENABLE_HUSH_JOB)
3367 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenko284d0fa2008-02-16 13:18:17 +00003368 close(channel[0]); /* NB: close _first_, then move fd! */
Denis Vlasenko05743d72008-02-10 12:10:08 +00003369 xmove_fd(channel[1], 1);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003370 /* Prevent it from trying to handle ctrl-z etc */
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003371#if ENABLE_HUSH_JOB
Denis Vlasenko87a86552008-07-29 19:43:10 +00003372 G.run_list_level = 1;
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003373#endif
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003374 /* Process substitution is not considered to be usual
3375 * 'command execution'.
3376 * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. */
3377 /* Not needed, we are relying on it being disabled
3378 * everywhere outside actual command execution. */
3379 /*set_jobctrl_sighandler(SIG_IGN);*/
3380 set_misc_sighandler(SIG_DFL);
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003381 /* Freeing 'head' here would break NOMMU. */
3382 _exit(run_list(head));
Eric Andersen25f27032001-04-26 23:22:31 +00003383 }
Eric Andersen25f27032001-04-26 23:22:31 +00003384 close(channel[1]);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003385 pf = fdopen(channel[0], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00003386 return pf;
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003387 /* 'head' is freed by the caller */
Eric Andersen25f27032001-04-26 23:22:31 +00003388}
3389
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003390/* Return code is exit status of the process that is run. */
Denis Vlasenko68404f12008-03-17 09:00:54 +00003391static int process_command_subs(o_string *dest,
Denis Vlasenko68404f12008-03-17 09:00:54 +00003392 struct in_str *input,
3393 const char *subst_end)
Eric Andersen25f27032001-04-26 23:22:31 +00003394{
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003395 int retcode, ch, eol_cnt;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003396 o_string result = NULL_O_STRING;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003397 struct parse_context inner;
Eric Andersen25f27032001-04-26 23:22:31 +00003398 FILE *p;
3399 struct in_str pipe_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003400
Eric Andersen25f27032001-04-26 23:22:31 +00003401 initialize_context(&inner);
3402
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003403 /* Recursion to generate command */
Eric Andersen25f27032001-04-26 23:22:31 +00003404 retcode = parse_stream(&result, &inner, input, subst_end);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003405 if (retcode != 0)
3406 return retcode; /* syntax error or EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003407 done_word(&result, &inner);
3408 done_pipe(&inner, PIPE_SEQ);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003409 o_free(&result);
Eric Andersen25f27032001-04-26 23:22:31 +00003410
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003411 p = generate_stream_from_list(inner.list_head);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003412 if (p == NULL)
3413 return 1;
Denis Vlasenkoa0898172007-10-01 09:59:01 +00003414 close_on_exec_on(fileno(p));
Eric Andersen25f27032001-04-26 23:22:31 +00003415 setup_file_in_str(&pipe_str, p);
3416
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003417 /* Now send results of command back into original context */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003418 eol_cnt = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003419 while ((ch = i_getch(&pipe_str)) != EOF) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003420 if (ch == '\n') {
3421 eol_cnt++;
3422 continue;
3423 }
3424 while (eol_cnt) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00003425 o_addchr(dest, '\n');
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003426 eol_cnt--;
3427 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003428 o_addQchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003429 }
3430
3431 debug_printf("done reading from pipe, pclose()ing\n");
3432 /* This is the step that wait()s for the child. Should be pretty
3433 * safe, since we just read an EOF from its stdout. We could try
Denis Vlasenko262d7652007-05-20 21:52:49 +00003434 * to do better, by using wait(), and keeping track of background jobs
Eric Andersen25f27032001-04-26 23:22:31 +00003435 * at the same time. That would be a lot of work, and contrary
3436 * to the KISS philosophy of this program. */
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003437 retcode = fclose(p);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003438 free_pipe_list(inner.list_head, /* indent: */ 0);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003439 debug_printf("closed FILE from child, retcode=%d\n", retcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003440 return retcode;
3441}
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003442#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003443
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003444static int parse_group(o_string *dest, struct parse_context *ctx,
Eric Andersen25f27032001-04-26 23:22:31 +00003445 struct in_str *input, int ch)
3446{
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003447 /* NB: parse_group may create and use its own o_string,
3448 * without any code changes. It just so happens that code is smaller
3449 * if we (ab)use caller's one. */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003450 int rcode;
3451 const char *endch = NULL;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003452 struct parse_context sub;
3453 struct command *command = ctx->command;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003454
3455 debug_printf_parse("parse_group entered\n");
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003456 if (command->argv /* word [word](... */
3457 || dest->length /* word(... */
3458 || dest->nonnull /* ""(... */
3459 ) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003460 syntax(NULL);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003461 debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n");
3462 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003463 }
3464 initialize_context(&sub);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003465 endch = "}";
3466 if (ch == '(') {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003467 endch = ")";
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003468 command->subshell = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003469 }
Denis Vlasenko22d10a02008-10-13 08:53:43 +00003470#if 0 /* TODO function support */
3471 if (ch == 'F') { /* function definition */
3472 command->subshell = 2;
3473 }
3474#endif
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003475 rcode = parse_stream(dest, &sub, input, endch);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003476 if (rcode == 0) {
3477 done_word(dest, &sub); /* finish off the final word in the subcontext */
3478 done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003479 command->group = sub.list_head;
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003480 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003481 debug_printf_parse("parse_group return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003482 return rcode;
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003483 /* command remains "open", available for possible redirects */
Eric Andersen25f27032001-04-26 23:22:31 +00003484}
3485
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003486/* Basically useful version until someone wants to get fancier,
Eric Andersen25f27032001-04-26 23:22:31 +00003487 * see the bash man page under "Parameter Expansion" */
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00003488static const char *lookup_param(const char *src)
Eric Andersen25f27032001-04-26 23:22:31 +00003489{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003490 struct variable *var = get_local_var(src);
3491 if (var)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003492 return strchr(var->varstr, '=') + 1;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003493 return NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003494}
3495
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003496#if ENABLE_HUSH_TICK
3497/* Subroutines for copying $(...) and `...` things */
3498static void add_till_backquote(o_string *dest, struct in_str *input);
3499/* '...' */
3500static void add_till_single_quote(o_string *dest, struct in_str *input)
3501{
3502 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003503 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003504 if (ch == EOF)
3505 break;
3506 if (ch == '\'')
3507 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003508 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003509 }
3510}
3511/* "...\"...`..`...." - do we need to handle "...$(..)..." too? */
3512static void add_till_double_quote(o_string *dest, struct in_str *input)
3513{
3514 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003515 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003516 if (ch == '"')
3517 break;
3518 if (ch == '\\') { /* \x. Copy both chars. */
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003519 o_addchr(dest, ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003520 ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003521 }
3522 if (ch == EOF)
3523 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003524 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003525 if (ch == '`') {
3526 add_till_backquote(dest, input);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003527 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003528 continue;
3529 }
Denis Vlasenko5703c222008-06-15 11:49:42 +00003530 //if (ch == '$') ...
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003531 }
3532}
3533/* Process `cmd` - copy contents until "`" is seen. Complicated by
3534 * \` quoting.
3535 * "Within the backquoted style of command substitution, backslash
3536 * shall retain its literal meaning, except when followed by: '$', '`', or '\'.
3537 * The search for the matching backquote shall be satisfied by the first
3538 * backquote found without a preceding backslash; during this search,
3539 * if a non-escaped backquote is encountered within a shell comment,
3540 * a here-document, an embedded command substitution of the $(command)
3541 * form, or a quoted string, undefined results occur. A single-quoted
3542 * or double-quoted string that begins, but does not end, within the
3543 * "`...`" sequence produces undefined results."
3544 * Example Output
3545 * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST
3546 */
3547static void add_till_backquote(o_string *dest, struct in_str *input)
3548{
3549 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003550 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003551 if (ch == '`')
3552 break;
3553 if (ch == '\\') { /* \x. Copy both chars unless it is \` */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003554 int ch2 = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003555 if (ch2 != '`' && ch2 != '$' && ch2 != '\\')
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003556 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003557 ch = ch2;
3558 }
3559 if (ch == EOF)
3560 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003561 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003562 }
3563}
3564/* Process $(cmd) - copy contents until ")" is seen. Complicated by
3565 * quoting and nested ()s.
3566 * "With the $(command) style of command substitution, all characters
3567 * following the open parenthesis to the matching closing parenthesis
3568 * constitute the command. Any valid shell script can be used for command,
3569 * except a script consisting solely of redirections which produces
3570 * unspecified results."
3571 * Example Output
3572 * echo $(echo '(TEST)' BEST) (TEST) BEST
3573 * echo $(echo 'TEST)' BEST) TEST) BEST
3574 * echo $(echo \(\(TEST\) BEST) ((TEST) BEST
3575 */
3576static void add_till_closing_curly_brace(o_string *dest, struct in_str *input)
3577{
3578 int count = 0;
3579 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003580 int ch = i_getch(input);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003581 if (ch == EOF)
3582 break;
3583 if (ch == '(')
3584 count++;
3585 if (ch == ')')
3586 if (--count < 0)
3587 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003588 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003589 if (ch == '\'') {
3590 add_till_single_quote(dest, input);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003591 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003592 continue;
3593 }
3594 if (ch == '"') {
3595 add_till_double_quote(dest, input);
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003596 o_addchr(dest, ch);
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003597 continue;
3598 }
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003599 if (ch == '\\') { /* \x. Copy verbatim. Important for \(, \) */
3600 ch = i_getch(input);
3601 if (ch == EOF)
3602 break;
Denis Vlasenko82dfec32008-06-16 12:47:11 +00003603 o_addchr(dest, ch);
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003604 continue;
3605 }
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003606 }
3607}
3608#endif /* ENABLE_HUSH_TICK */
3609
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00003610/* Return code: 0 for OK, 1 for syntax error */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003611static int handle_dollar(o_string *dest, struct in_str *input)
Eric Andersen25f27032001-04-26 23:22:31 +00003612{
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003613 int ch = i_peek(input); /* first character after the $ */
Denis Vlasenkoff097622007-10-01 10:00:45 +00003614 unsigned char quote_mask = dest->o_quote ? 0x80 : 0;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003615
3616 debug_printf_parse("handle_dollar entered: ch='%c'\n", ch);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003617 if (isalpha(ch)) {
Denis Vlasenkod4981312008-07-31 10:34:48 +00003618 i_getch(input);
3619 make_var:
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003620 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003621 while (1) {
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003622 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003623 o_addchr(dest, ch | quote_mask);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003624 quote_mask = 0;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003625 ch = i_peek(input);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003626 if (!isalnum(ch) && ch != '_')
3627 break;
Denis Vlasenkod4981312008-07-31 10:34:48 +00003628 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003629 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003630 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003631 } else if (isdigit(ch)) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003632 make_one_char_var:
Denis Vlasenkod4981312008-07-31 10:34:48 +00003633 i_getch(input);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003634 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003635 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003636 o_addchr(dest, ch | quote_mask);
3637 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003638 } else switch (ch) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003639 case '$': /* pid */
3640 case '!': /* last bg pid */
3641 case '?': /* last exit code */
3642 case '#': /* number of args */
3643 case '*': /* args */
3644 case '@': /* args */
3645 goto make_one_char_var;
Eric Andersen25f27032001-04-26 23:22:31 +00003646 case '{':
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003647 o_addchr(dest, SPECIAL_VAR_SYMBOL);
3648 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003649 /* XXX maybe someone will try to escape the '}' */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003650 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003651 ch = i_getch(input);
Denis Vlasenkob0550012007-05-24 12:18:16 +00003652 if (ch == '}')
3653 break;
3654 if (!isalnum(ch) && ch != '_') {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003655 syntax("unterminated ${name}");
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003656 debug_printf_parse("handle_dollar return 1: unterminated ${name}\n");
3657 return 1;
3658 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003659 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003660 o_addchr(dest, ch | quote_mask);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003661 quote_mask = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003662 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003663 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003664 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003665#if ENABLE_HUSH_TICK
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003666 case '(': {
3667 //int pos = dest->length;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003668 i_getch(input);
3669 o_addchr(dest, SPECIAL_VAR_SYMBOL);
3670 o_addchr(dest, quote_mask | '`');
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003671 add_till_closing_curly_brace(dest, input);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00003672 //debug_printf_subst("SUBST RES2 '%s'\n", dest->data + pos);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003673 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003674 break;
Denis Vlasenko76db5ad2008-06-12 12:58:20 +00003675 }
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003676#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003677 case '_':
Denis Vlasenkod4981312008-07-31 10:34:48 +00003678 i_getch(input);
3679 ch = i_peek(input);
3680 if (isalnum(ch)) { /* it's $_name or $_123 */
3681 ch = '_';
3682 goto make_var;
3683 }
3684 /* else: it's $_ */
3685 case '-':
Eric Andersen25f27032001-04-26 23:22:31 +00003686 /* still unhandled, but should be eventually */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003687 bb_error_msg("unhandled syntax: $%c", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003688 return 1;
3689 break;
3690 default:
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003691 o_addQchr(dest, '$');
Eric Andersen25f27032001-04-26 23:22:31 +00003692 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003693 debug_printf_parse("handle_dollar return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003694 return 0;
3695}
3696
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003697/* Scan input, call done_word() whenever full IFS delimited word was seen.
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003698 * Call done_pipe if '\n' was seen (and end_trigger != NULL).
3699 * Return code is 0 if end_trigger char is met,
3700 * -1 on EOF (but if end_trigger == NULL then return 0),
Denis Vlasenko55789c62008-06-18 16:30:42 +00003701 * 1 for syntax error */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003702static int parse_stream(o_string *dest, struct parse_context *ctx,
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003703 struct in_str *input, const char *end_trigger)
Eric Andersen25f27032001-04-26 23:22:31 +00003704{
"Vladimir N. Oleynik"4ccd2b42006-01-31 09:27:48 +00003705 int ch, m;
Eric Andersen25f27032001-04-26 23:22:31 +00003706 int redir_fd;
3707 redir_type redir_style;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003708 int shadow_quote = dest->o_quote;
Eric Andersen25f27032001-04-26 23:22:31 +00003709 int next;
3710
Denis Vlasenkoff097622007-10-01 10:00:45 +00003711 /* Only double-quote state is handled in the state variable dest->o_quote.
Eric Andersen25f27032001-04-26 23:22:31 +00003712 * A single-quote triggers a bypass of the main loop until its mate is
Denis Vlasenkoff097622007-10-01 10:00:45 +00003713 * found. When recursing, quote state is passed in via dest->o_quote. */
Eric Andersen25f27032001-04-26 23:22:31 +00003714
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003715 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 +00003716
Denis Vlasenko1a735862007-05-23 00:32:25 +00003717 while (1) {
Denis Vlasenko1a735862007-05-23 00:32:25 +00003718 m = CHAR_IFS;
3719 next = '\0';
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003720 ch = i_getch(input);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003721 if (ch != EOF) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00003722 m = G.charmap[ch];
Denis Vlasenko55789c62008-06-18 16:30:42 +00003723 if (ch != '\n') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003724 next = i_peek(input);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003725 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003726 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003727 debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n",
Denis Vlasenkoff097622007-10-01 10:00:45 +00003728 ch, ch, m, dest->o_quote);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003729 if (m == CHAR_ORDINARY
Denis Vlasenko55789c62008-06-18 16:30:42 +00003730 || (m != CHAR_SPECIAL && shadow_quote)
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003731 ) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00003732 if (ch == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003733 syntax("unterminated \"");
Denis Vlasenko170435c2007-05-23 13:01:10 +00003734 debug_printf_parse("parse_stream return 1: unterminated \"\n");
3735 return 1;
3736 }
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003737 o_addQchr(dest, ch);
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003738 if ((dest->o_assignment == MAYBE_ASSIGNMENT
3739 || dest->o_assignment == WORD_IS_KEYWORD)
Denis Vlasenko55789c62008-06-18 16:30:42 +00003740 && ch == '='
3741 && is_assignment(dest->data)
3742 ) {
3743 dest->o_assignment = DEFINITELY_ASSIGNMENT;
3744 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003745 continue;
3746 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003747 if (m == CHAR_IFS) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003748 if (done_word(dest, ctx)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003749 debug_printf_parse("parse_stream return 1: done_word!=0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003750 return 1;
Eric Andersenaac75e52001-04-30 18:18:45 +00003751 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003752 if (ch == EOF)
3753 break;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003754 /* If we aren't performing a substitution, treat
3755 * a newline as a command separator.
3756 * [why we don't handle it exactly like ';'? --vda] */
3757 if (end_trigger && ch == '\n') {
Denis Vlasenkof1736072008-07-31 10:09:26 +00003758#if ENABLE_HUSH_CASE
3759 /* "case ... in <newline> word) ..." -
3760 * newlines are ignored (but ';' wouldn't be) */
3761 if (dest->length == 0 // && argv[0] == NULL
3762 && ctx->ctx_res_w == RES_MATCH
3763 ) {
3764 continue;
3765 }
3766#endif
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003767 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003768 dest->o_assignment = MAYBE_ASSIGNMENT;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003769 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003770 }
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003771 if (end_trigger) {
Denis Vlasenko55789c62008-06-18 16:30:42 +00003772 if (!shadow_quote && strchr(end_trigger, ch)) {
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003773 /* Special case: (...word) makes last word terminate,
3774 * as if ';' is seen */
3775 if (ch == ')') {
3776 done_word(dest, ctx);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003777//err chk?
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003778 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003779 dest->o_assignment = MAYBE_ASSIGNMENT;
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003780 }
Denis Vlasenko733e3fb2008-07-06 10:01:13 +00003781 if (!HAS_KEYWORDS
3782 IF_HAS_KEYWORDS(|| (ctx->ctx_res_w == RES_NONE && ctx->old_flag == 0))
3783 ) {
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003784 debug_printf_parse("parse_stream return 0: end_trigger char found\n");
3785 return 0;
3786 }
3787 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003788 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003789 if (m == CHAR_IFS)
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003790 continue;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003791
3792 if (dest->o_assignment == MAYBE_ASSIGNMENT) {
3793 /* ch is a special char and thus this word
3794 * cannot be an assignment: */
3795 dest->o_assignment = NOT_ASSIGNMENT;
3796 }
3797
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003798 switch (ch) {
Eric Andersen25f27032001-04-26 23:22:31 +00003799 case '#':
Denis Vlasenko55789c62008-06-18 16:30:42 +00003800 if (dest->length == 0 && !shadow_quote) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003801 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003802 ch = i_peek(input);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003803 if (ch == EOF || ch == '\n')
3804 break;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003805 i_getch(input);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003806 }
Eric Andersen25f27032001-04-26 23:22:31 +00003807 } else {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003808 o_addQchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003809 }
3810 break;
3811 case '\\':
3812 if (next == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003813 syntax("\\<eof>");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003814 debug_printf_parse("parse_stream return 1: \\<eof>\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003815 return 1;
3816 }
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003817 /* bash:
3818 * "The backslash retains its special meaning [in "..."]
3819 * only when followed by one of the following characters:
3820 * $, `, ", \, or <newline>. A double quote may be quoted
3821 * within double quotes by preceding it with a backslash.
3822 * If enabled, history expansion will be performed unless
3823 * an ! appearing in double quotes is escaped using
3824 * a backslash. The backslash preceding the ! is not removed."
3825 */
Denis Vlasenko55789c62008-06-18 16:30:42 +00003826 if (shadow_quote) { //NOT SURE dest->o_quote) {
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003827 if (strchr("$`\"\\", next) != NULL) {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003828 o_addqchr(dest, i_getch(input));
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003829 } else {
Denis Vlasenko7e3d33b2008-06-12 13:31:04 +00003830 o_addqchr(dest, '\\');
Denis Vlasenko87f40ba2008-06-10 22:39:37 +00003831 }
3832 } else {
3833 o_addchr(dest, '\\');
3834 o_addchr(dest, i_getch(input));
3835 }
Eric Andersen25f27032001-04-26 23:22:31 +00003836 break;
3837 case '$':
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003838 if (handle_dollar(dest, input) != 0) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003839 debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n");
3840 return 1;
3841 }
Eric Andersen25f27032001-04-26 23:22:31 +00003842 break;
3843 case '\'':
3844 dest->nonnull = 1;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003845 while (1) {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003846 ch = i_getch(input);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00003847 if (ch == EOF) {
3848 syntax("unterminated '");
3849 debug_printf_parse("parse_stream return 1: unterminated '\n");
3850 return 1;
3851 }
3852 if (ch == '\'')
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003853 break;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003854 if (dest->o_assignment == NOT_ASSIGNMENT)
3855 o_addqchr(dest, ch);
3856 else
3857 o_addchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003858 }
Eric Andersen25f27032001-04-26 23:22:31 +00003859 break;
3860 case '"':
3861 dest->nonnull = 1;
Denis Vlasenko55789c62008-06-18 16:30:42 +00003862 shadow_quote ^= 1; /* invert */
3863 if (dest->o_assignment == NOT_ASSIGNMENT)
3864 dest->o_quote ^= 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003865 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003866#if ENABLE_HUSH_TICK
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003867 case '`': {
3868 //int pos = dest->length;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003869 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko55789c62008-06-18 16:30:42 +00003870 o_addchr(dest, shadow_quote /*or dest->o_quote??*/ ? 0x80 | '`' : '`');
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003871 add_till_backquote(dest, input);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003872 o_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenko30c9cc52008-06-17 07:24:29 +00003873 //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos);
Eric Andersen25f27032001-04-26 23:22:31 +00003874 break;
Denis Vlasenko7b4f3f12008-06-10 18:04:32 +00003875 }
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003876#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003877 case '>':
3878 redir_fd = redirect_opt_num(dest);
3879 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003880 redir_style = REDIRECT_OVERWRITE;
Eric Andersen25f27032001-04-26 23:22:31 +00003881 if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003882 redir_style = REDIRECT_APPEND;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003883 i_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003884 }
3885#if 0
3886 else if (next == '(') {
3887 syntax(">(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003888 debug_printf_parse("parse_stream return 1: >(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003889 return 1;
3890 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003891#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003892 setup_redirect(ctx, redir_fd, redir_style, input);
3893 break;
3894 case '<':
3895 redir_fd = redirect_opt_num(dest);
3896 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003897 redir_style = REDIRECT_INPUT;
Eric Andersen25f27032001-04-26 23:22:31 +00003898 if (next == '<') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003899 redir_style = REDIRECT_HEREIS;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003900 i_getch(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003901 } else if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003902 redir_style = REDIRECT_IO;
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003903 i_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003904 }
3905#if 0
3906 else if (next == '(') {
3907 syntax("<(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003908 debug_printf_parse("parse_stream return 1: <(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003909 return 1;
3910 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003911#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003912 setup_redirect(ctx, redir_fd, redir_style, input);
3913 break;
3914 case ';':
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003915#if ENABLE_HUSH_CASE
3916 case_semi:
3917#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003918 done_word(dest, ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003919 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003920#if ENABLE_HUSH_CASE
3921 /* Eat multiple semicolons, detect
3922 * whether it means something special */
3923 while (1) {
3924 ch = i_peek(input);
3925 if (ch != ';')
3926 break;
3927 i_getch(input);
3928 if (ctx->ctx_res_w == RES_CASEI) {
3929 ctx->ctx_dsemicolon = 1;
3930 ctx->ctx_res_w = RES_MATCH;
3931 break;
3932 }
3933 }
3934#endif
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003935 new_cmd:
3936 /* We just finished a cmd. New one may start
3937 * with an assignment */
3938 dest->o_assignment = MAYBE_ASSIGNMENT;
Eric Andersen25f27032001-04-26 23:22:31 +00003939 break;
3940 case '&':
3941 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003942 if (next == '&') {
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003943 i_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003944 done_pipe(ctx, PIPE_AND);
Eric Andersen25f27032001-04-26 23:22:31 +00003945 } else {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003946 done_pipe(ctx, PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00003947 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003948 goto new_cmd;
Eric Andersen25f27032001-04-26 23:22:31 +00003949 case '|':
3950 done_word(dest, ctx);
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00003951#if ENABLE_HUSH_CASE
3952 if (ctx->ctx_res_w == RES_MATCH)
Denis Vlasenkof1736072008-07-31 10:09:26 +00003953 break; /* we are in case's "word | word)" */
Denis Vlasenkofbeeb322008-07-31 00:17:01 +00003954#endif
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003955 if (next == '|') { /* || */
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00003956 i_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003957 done_pipe(ctx, PIPE_OR);
Eric Andersen25f27032001-04-26 23:22:31 +00003958 } else {
3959 /* we could pick up a file descriptor choice here
3960 * with redirect_opt_num(), but bash doesn't do it.
3961 * "echo foo 2| cat" yields "foo 2". */
3962 done_command(ctx);
3963 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00003964 goto new_cmd;
Eric Andersen25f27032001-04-26 23:22:31 +00003965 case '(':
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003966#if ENABLE_HUSH_CASE
Denis Vlasenkof1736072008-07-31 10:09:26 +00003967 /* "case... in [(]word)..." - skip '(' */
Denis Vlasenko22d10a02008-10-13 08:53:43 +00003968 if (ctx->ctx_res_w == RES_MATCH
Denis Vlasenko9af22c72008-10-09 12:54:58 +00003969 && ctx->command->argv == NULL /* not (word|(... */
Denis Vlasenko22d10a02008-10-13 08:53:43 +00003970 && dest->length == 0 /* not word(... */
3971 && dest->nonnull == 0 /* not ""(... */
Denis Vlasenko17f02e72008-07-14 04:32:29 +00003972 ) {
3973 continue;
3974 }
3975#endif
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003976#if 0 /* TODO: implement functions */
3977 if (dest->length != 0 /* not just () but word() */
3978 && dest->nonnull == 0 /* not a"b"c() */
3979 && ctx->command->argv == NULL /* it's the first word */
3980 && i_peek(input) == ')'
Denis Vlasenko22d10a02008-10-13 08:53:43 +00003981 && !match_reserved_word(dest)
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003982 ) {
3983 bb_error_msg("seems like a function definition");
Denis Vlasenko22d10a02008-10-13 08:53:43 +00003984 i_getch(input);
3985 do {
3986 ch = i_getch(input);
3987 } while (ch == ' ' || ch == '\n');
3988 if (ch != '{') {
3989 syntax("was expecting {");
3990 debug_printf_parse("parse_stream return 1\n");
3991 return 1;
3992 }
Denis Vlasenkoc3735272008-10-09 12:58:26 +00003993 }
3994#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003995 case '{':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003996 if (parse_group(dest, ctx, input, ch) != 0) {
3997 debug_printf_parse("parse_stream return 1: parse_group returned non-0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003998 return 1;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003999 }
Denis Vlasenko2b576b82008-08-04 00:46:07 +00004000 goto new_cmd;
Eric Andersen25f27032001-04-26 23:22:31 +00004001 case ')':
Denis Vlasenko17f02e72008-07-14 04:32:29 +00004002#if ENABLE_HUSH_CASE
4003 if (ctx->ctx_res_w == RES_MATCH)
4004 goto case_semi;
4005#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004006 case '}':
Denis Vlasenkoc3735272008-10-09 12:58:26 +00004007 /* proper use of this character is caught by end_trigger:
4008 * if we see {, we call parse_group(..., end_trigger='}')
4009 * and it will match } earlier (not here). */
Denis Vlasenkoa8442002008-06-14 11:00:17 +00004010 syntax("unexpected } or )");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00004011 debug_printf_parse("parse_stream return 1: unexpected '}'\n");
Eric Andersen25f27032001-04-26 23:22:31 +00004012 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00004013 default:
Denis Vlasenko5ec61322008-06-24 00:50:07 +00004014 if (HUSH_DEBUG)
Denis Vlasenko90e485c2007-05-23 15:22:50 +00004015 bb_error_msg_and_die("BUG: unexpected %c\n", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00004016 }
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004017 } /* while (1) */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00004018 debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL));
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00004019 if (end_trigger)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00004020 return -1;
Eric Andersen25f27032001-04-26 23:22:31 +00004021 return 0;
4022}
4023
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004024static void set_in_charmap(const char *set, int code)
Eric Andersen25f27032001-04-26 23:22:31 +00004025{
Denis Vlasenkof5294e12007-04-14 10:09:57 +00004026 while (*set)
Denis Vlasenko87a86552008-07-29 19:43:10 +00004027 G.charmap[(unsigned char)*set++] = code;
Eric Andersen25f27032001-04-26 23:22:31 +00004028}
4029
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004030static void update_charmap(void)
Eric Andersen25f27032001-04-26 23:22:31 +00004031{
Denis Vlasenko87a86552008-07-29 19:43:10 +00004032 G.ifs = getenv("IFS");
4033 if (G.ifs == NULL)
4034 G.ifs = " \t\n";
Eric Andersen25f27032001-04-26 23:22:31 +00004035 /* Precompute a list of 'flow through' behavior so it can be treated
4036 * quickly up front. Computation is necessary because of IFS.
4037 * Special case handling of IFS == " \t\n" is not implemented.
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004038 * The charmap[] array only really needs two bits each,
4039 * and on most machines that would be faster (reduced L1 cache use).
Eric Andersen25f27032001-04-26 23:22:31 +00004040 */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004041 memset(G.charmap, CHAR_ORDINARY, sizeof(G.charmap));
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00004042#if ENABLE_HUSH_TICK
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004043 set_in_charmap("\\$\"`", CHAR_SPECIAL);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00004044#else
4045 set_in_charmap("\\$\"", CHAR_SPECIAL);
4046#endif
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004047 set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004048 set_in_charmap(G.ifs, CHAR_IFS); /* are ordinary if quoted */
Eric Andersen25f27032001-04-26 23:22:31 +00004049}
4050
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004051/* Most recursion does not come through here, the exception is
Denis Vlasenko170435c2007-05-23 13:01:10 +00004052 * from builtin_source() and builtin_eval() */
4053static int parse_and_run_stream(struct in_str *inp, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00004054{
Denis Vlasenko9af22c72008-10-09 12:54:58 +00004055 struct parse_context ctx;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004056 o_string temp = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00004057 int rcode;
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004058
Eric Andersen25f27032001-04-26 23:22:31 +00004059 do {
4060 initialize_context(&ctx);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00004061 update_charmap();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004062#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00004063 inp->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004064#endif
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00004065 /* We will stop & execute after each ';' or '\n'.
4066 * Example: "sleep 9999; echo TEST" + ctrl-C:
4067 * TEST should be printed */
Denis Vlasenko2b576b82008-08-04 00:46:07 +00004068 temp.o_assignment = MAYBE_ASSIGNMENT;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00004069 rcode = parse_stream(&temp, &ctx, inp, ";\n");
Denis Vlasenko5ec61322008-06-24 00:50:07 +00004070#if HAS_KEYWORDS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004071 if (rcode != 1 && ctx.old_flag != 0) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00004072 syntax(NULL);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004073 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00004074#endif
4075 if (rcode != 1 IF_HAS_KEYWORDS(&& ctx.old_flag == 0)) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004076 done_word(&temp, &ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004077 done_pipe(&ctx, PIPE_SEQ);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00004078 debug_print_tree(ctx.list_head, 0);
Denis Vlasenko05743d72008-02-10 12:10:08 +00004079 debug_printf_exec("parse_stream_outer: run_and_free_list\n");
4080 run_and_free_list(ctx.list_head);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004081 } else {
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004082 /* We arrive here also if rcode == 1 (error in parse_stream) */
Denis Vlasenko5ec61322008-06-24 00:50:07 +00004083#if HAS_KEYWORDS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004084 if (ctx.old_flag != 0) {
4085 free(ctx.stack);
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00004086 o_reset(&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004087 }
Denis Vlasenko5ec61322008-06-24 00:50:07 +00004088#endif
Denis Vlasenkoc1c63b62008-06-18 09:20:35 +00004089 /*temp.nonnull = 0; - o_free does it below */
4090 /*temp.o_quote = 0; - o_free does it below */
Denis Vlasenko05743d72008-02-10 12:10:08 +00004091 free_pipe_list(ctx.list_head, /* indent: */ 0);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004092 /* Discard all unprocessed line input, force prompt on */
4093 inp->p = NULL;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004094#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004095 inp->promptme = 1;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004096#endif
Eric Andersen4c9b68f2002-04-13 12:33:41 +00004097 }
Denis Vlasenko46ccdcb2008-06-10 18:05:12 +00004098 o_free(&temp);
Denis Vlasenkof8d01d32008-06-14 17:13:20 +00004099 /* loop on syntax errors, return on EOF: */
4100 } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP));
Eric Andersen25f27032001-04-26 23:22:31 +00004101 return 0;
4102}
4103
Denis Vlasenko170435c2007-05-23 13:01:10 +00004104static int parse_and_run_string(const char *s, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00004105{
4106 struct in_str input;
4107 setup_string_in_str(&input, s);
Denis Vlasenko170435c2007-05-23 13:01:10 +00004108 return parse_and_run_stream(&input, parse_flag);
Eric Andersen25f27032001-04-26 23:22:31 +00004109}
4110
Denis Vlasenko170435c2007-05-23 13:01:10 +00004111static int parse_and_run_file(FILE *f)
Eric Andersen25f27032001-04-26 23:22:31 +00004112{
4113 int rcode;
4114 struct in_str input;
4115 setup_file_in_str(&input, f);
Denis Vlasenko5703c222008-06-15 11:49:42 +00004116 rcode = parse_and_run_stream(&input, 0 /* parse_flag */);
Eric Andersen25f27032001-04-26 23:22:31 +00004117 return rcode;
4118}
4119
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004120#if ENABLE_HUSH_JOB
Eric Andersen6c947d22001-06-25 22:24:38 +00004121/* Make sure we have a controlling tty. If we get started under a job
4122 * aware app (like bash for example), make sure we are now in charge so
4123 * we don't fight over who gets the foreground */
Eric Anderseneaecbf32001-10-31 10:41:31 +00004124static void setup_job_control(void)
Eric Andersen52a97ca2001-06-22 06:49:26 +00004125{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004126 pid_t shell_pgrp;
4127
Denis Vlasenko87a86552008-07-29 19:43:10 +00004128 shell_pgrp = getpgrp();
Denis Vlasenko87a86552008-07-29 19:43:10 +00004129 close_on_exec_on(G.interactive_fd);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004130
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00004131 /* If we were ran as 'hush &',
4132 * sleep until we are in the foreground. */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004133 while (tcgetpgrp(G.interactive_fd) != shell_pgrp) {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004134 /* Send TTIN to ourself (should stop us) */
Denis Vlasenkoff131b92007-04-10 15:42:06 +00004135 kill(- shell_pgrp, SIGTTIN);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00004136 shell_pgrp = getpgrp();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004137 }
Eric Andersen52a97ca2001-06-22 06:49:26 +00004138
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004139 /* Ignore job-control and misc signals. */
4140 set_jobctrl_sighandler(SIG_IGN);
4141 set_misc_sighandler(SIG_IGN);
4142//huh? signal(SIGCHLD, SIG_IGN);
4143
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00004144 /* We _must_ restore tty pgrp on fatal signals */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004145 set_fatal_sighandler(sigexit);
Eric Andersen6c947d22001-06-25 22:24:38 +00004146
4147 /* Put ourselves in our own process group. */
Bernhard Reutner-Fischer864329d2008-09-25 10:55:05 +00004148 bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */
Eric Andersen6c947d22001-06-25 22:24:38 +00004149 /* Grab control of the terminal. */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004150 tcsetpgrp(G.interactive_fd, getpid());
Eric Andersen6c947d22001-06-25 22:24:38 +00004151}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004152#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00004153
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004154
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00004155int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Matt Kraai2d91deb2001-08-01 17:21:35 +00004156int hush_main(int argc, char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00004157{
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004158 static const struct variable const_shell_ver = {
4159 .next = NULL,
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00004160 .varstr = (char*)hush_version_str,
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004161 .max_len = 1, /* 0 can provoke free(name) */
4162 .flg_export = 1,
4163 .flg_read_only = 1,
4164 };
4165
Eric Andersen25f27032001-04-26 23:22:31 +00004166 int opt;
4167 FILE *input;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004168 char **e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004169 struct variable *cur_var;
Eric Andersenbc604a22001-05-16 05:24:03 +00004170
Denis Vlasenko574f2f42008-02-27 18:41:59 +00004171 INIT_G();
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004172
Denis Vlasenko87a86552008-07-29 19:43:10 +00004173 G.root_pid = getpid();
Denis Vlasenko16c2fea2008-06-17 12:28:44 +00004174
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004175 /* Deal with HUSH_VERSION */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004176 G.shell_ver = const_shell_ver; /* copying struct here */
4177 G.top_var = &G.shell_ver;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004178 unsetenv("HUSH_VERSION"); /* in case it exists in initial env */
4179 /* Initialize our shell local variables with the values
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004180 * currently living in the environment */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004181 cur_var = G.top_var;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00004182 e = environ;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004183 if (e) while (*e) {
4184 char *value = strchr(*e, '=');
4185 if (value) { /* paranoia */
4186 cur_var->next = xzalloc(sizeof(*cur_var));
4187 cur_var = cur_var->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00004188 cur_var->varstr = *e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004189 cur_var->max_len = strlen(*e);
4190 cur_var->flg_export = 1;
4191 }
4192 e++;
4193 }
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00004194 putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00004195
Denis Vlasenko38f63192007-01-22 09:03:07 +00004196#if ENABLE_FEATURE_EDITING
Denis Vlasenko87a86552008-07-29 19:43:10 +00004197 G.line_input_state = new_line_input_t(FOR_SHELL);
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00004198#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004199 /* XXX what should these be while sourcing /etc/profile? */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004200 G.global_argc = argc;
4201 G.global_argv = argv;
Eric Andersen94ac2442001-05-22 19:05:18 +00004202 /* Initialize some more globals to non-zero values */
4203 set_cwd();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004204#if ENABLE_HUSH_INTERACTIVE
4205#if ENABLE_FEATURE_EDITING
4206 cmdedit_set_initial_prompt();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004207#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00004208 G.PS2 = "> ";
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004209#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00004210
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004211 if (EXIT_SUCCESS) /* otherwise is already done */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004212 G.last_return_code = EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00004213
4214 if (argv[0] && argv[0][0] == '-') {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00004215 debug_printf("sourcing /etc/profile\n");
Denis Vlasenko5415c852008-07-21 23:05:26 +00004216 input = fopen_for_read("/etc/profile");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00004217 if (input != NULL) {
Denis Vlasenkoa0898172007-10-01 09:59:01 +00004218 close_on_exec_on(fileno(input));
Denis Vlasenko170435c2007-05-23 13:01:10 +00004219 parse_and_run_file(input);
Eric Andersena90f20b2001-06-26 23:00:21 +00004220 fclose(input);
4221 }
Eric Andersen25f27032001-04-26 23:22:31 +00004222 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004223 input = stdin;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004224
Eric Andersen25f27032001-04-26 23:22:31 +00004225 while ((opt = getopt(argc, argv, "c:xif")) > 0) {
4226 switch (opt) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004227 case 'c':
Denis Vlasenko87a86552008-07-29 19:43:10 +00004228 G.global_argv = argv + optind;
4229 G.global_argc = argc - optind;
Denis Vlasenko5703c222008-06-15 11:49:42 +00004230 opt = parse_and_run_string(optarg, 0 /* parse_flag */);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004231 goto final_return;
4232 case 'i':
Denis Vlasenkoc666f712007-05-16 22:18:54 +00004233 /* Well, we cannot just declare interactiveness,
4234 * we have to have some stuff (ctty, etc) */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004235 /* G.interactive_fd++; */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004236 break;
4237 case 'f':
Denis Vlasenko87a86552008-07-29 19:43:10 +00004238 G.fake_mode = 1;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004239 break;
4240 default:
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004241#ifndef BB_VER
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004242 fprintf(stderr, "Usage: sh [FILE]...\n"
4243 " or: sh -c command [args]...\n\n");
4244 exit(EXIT_FAILURE);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004245#else
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004246 bb_show_usage();
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00004247#endif
Eric Andersen25f27032001-04-26 23:22:31 +00004248 }
4249 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004250#if ENABLE_HUSH_JOB
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004251 /* A shell is interactive if the '-i' flag was given, or if all of
Eric Andersen25f27032001-04-26 23:22:31 +00004252 * the following conditions are met:
Denis Vlasenko55b2de72007-04-18 17:21:28 +00004253 * no -c command
Eric Andersen25f27032001-04-26 23:22:31 +00004254 * no arguments remaining or the -s flag given
4255 * standard input is a terminal
4256 * standard output is a terminal
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00004257 * Refer to Posix.2, the description of the 'sh' utility. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004258 if (argv[optind] == NULL && input == stdin
4259 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
4260 ) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004261 G.saved_tty_pgrp = tcgetpgrp(STDIN_FILENO);
4262 debug_printf("saved_tty_pgrp=%d\n", G.saved_tty_pgrp);
4263 if (G.saved_tty_pgrp >= 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004264 /* try to dup to high fd#, >= 255 */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004265 G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
4266 if (G.interactive_fd < 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004267 /* try to dup to any fd */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004268 G.interactive_fd = dup(STDIN_FILENO);
4269 if (G.interactive_fd < 0)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004270 /* give up */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004271 G.interactive_fd = 0;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004272 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004273 // TODO: track & disallow any attempts of user
4274 // to (inadvertently) close/redirect it
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004275 }
Eric Andersen25f27032001-04-26 23:22:31 +00004276 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004277 debug_printf("G.interactive_fd=%d\n", G.interactive_fd);
4278 if (G.interactive_fd) {
4279 fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC);
Eric Andersen25f27032001-04-26 23:22:31 +00004280 /* Looks like they want an interactive shell */
Eric Andersen52a97ca2001-06-22 06:49:26 +00004281 setup_job_control();
Denis Vlasenko4ecfcdc2008-02-11 08:32:31 +00004282 /* -1 is special - makes xfuncs longjmp, not exit
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00004283 * (we reset die_sleep = 0 whereever we [v]fork) */
4284 die_sleep = -1;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00004285 if (setjmp(die_jmp)) {
4286 /* xfunc has failed! die die die */
4287 hush_exit(xfunc_error_retval);
4288 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004289#if !ENABLE_FEATURE_SH_EXTRA_QUIET
Denis Vlasenkoca525b42007-06-13 12:27:17 +00004290 printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", bb_banner);
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00004291 printf("Enter 'help' for a list of built-in commands.\n\n");
4292#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00004293 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004294#elif ENABLE_HUSH_INTERACTIVE
4295/* no job control compiled, only prompt/line editing */
4296 if (argv[optind] == NULL && input == stdin
4297 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
4298 ) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004299 G.interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
4300 if (G.interactive_fd < 0) {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004301 /* try to dup to any fd */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004302 G.interactive_fd = dup(STDIN_FILENO);
4303 if (G.interactive_fd < 0)
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004304 /* give up */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004305 G.interactive_fd = 0;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004306 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004307 if (G.interactive_fd) {
4308 fcntl(G.interactive_fd, F_SETFD, FD_CLOEXEC);
Denis Vlasenko4830fc52008-05-25 21:50:55 +00004309 set_misc_sighandler(SIG_IGN);
4310 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00004311 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00004312#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004313
Denis Vlasenkobb81c582007-01-30 22:32:09 +00004314 if (argv[optind] == NULL) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00004315 opt = parse_and_run_file(stdin);
Denis Vlasenko05743d72008-02-10 12:10:08 +00004316 } else {
4317 debug_printf("\nrunning script '%s'\n", argv[optind]);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004318 G.global_argv = argv + optind;
4319 G.global_argc = argc - optind;
Denis Vlasenko5415c852008-07-21 23:05:26 +00004320 input = xfopen_for_read(argv[optind]);
Denis Vlasenko459a5ad2008-02-11 08:35:03 +00004321 fcntl(fileno(input), F_SETFD, FD_CLOEXEC);
Denis Vlasenko05743d72008-02-10 12:10:08 +00004322 opt = parse_and_run_file(input);
Eric Andersen25f27032001-04-26 23:22:31 +00004323 }
Eric Andersen25f27032001-04-26 23:22:31 +00004324
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004325 final_return:
4326
Denis Vlasenko38f63192007-01-22 09:03:07 +00004327#if ENABLE_FEATURE_CLEAN_UP
Eric Andersenaeb44c42001-05-22 20:29:00 +00004328 fclose(input);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004329 if (G.cwd != bb_msg_unknown)
4330 free((char*)G.cwd);
4331 cur_var = G.top_var->next;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004332 while (cur_var) {
4333 struct variable *tmp = cur_var;
4334 if (!cur_var->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00004335 free(cur_var->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00004336 cur_var = cur_var->next;
4337 free(tmp);
Eric Andersenaeb44c42001-05-22 20:29:00 +00004338 }
Eric Andersen25f27032001-04-26 23:22:31 +00004339#endif
Denis Vlasenko87a86552008-07-29 19:43:10 +00004340 hush_exit(opt ? opt : G.last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +00004341}
Denis Vlasenko96702ca2007-11-23 23:28:55 +00004342
4343
4344#if ENABLE_LASH
4345int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
4346int lash_main(int argc, char **argv)
4347{
4348 //bb_error_msg("lash is deprecated, please use hush instead");
4349 return hush_main(argc, argv);
4350}
4351#endif
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004352
4353
4354/*
4355 * Built-ins
4356 */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004357static int builtin_true(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004358{
4359 return 0;
4360}
4361
4362static int builtin_test(char **argv)
4363{
4364 int argc = 0;
4365 while (*argv) {
4366 argc++;
4367 argv++;
4368 }
4369 return test_main(argc, argv - argc);
4370}
4371
4372static int builtin_echo(char **argv)
4373{
4374 int argc = 0;
4375 while (*argv) {
4376 argc++;
4377 argv++;
4378 }
4379 return echo_main(argc, argv - argc);
4380}
4381
4382static int builtin_eval(char **argv)
4383{
4384 int rcode = EXIT_SUCCESS;
4385
4386 if (argv[1]) {
4387 char *str = expand_strvec_to_string(argv + 1);
4388 parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP);
4389 free(str);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004390 rcode = G.last_return_code;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004391 }
4392 return rcode;
4393}
4394
4395static int builtin_cd(char **argv)
4396{
4397 const char *newdir;
4398 if (argv[1] == NULL) {
4399 // bash does nothing (exitcode 0) if HOME is ""; if it's unset,
4400 // bash says "bash: cd: HOME not set" and does nothing (exitcode 1)
4401 newdir = getenv("HOME") ? : "/";
4402 } else
4403 newdir = argv[1];
4404 if (chdir(newdir)) {
4405 printf("cd: %s: %s\n", newdir, strerror(errno));
4406 return EXIT_FAILURE;
4407 }
4408 set_cwd();
4409 return EXIT_SUCCESS;
4410}
4411
4412static int builtin_exec(char **argv)
4413{
4414 if (argv[1] == NULL)
4415 return EXIT_SUCCESS; /* bash does this */
4416 {
4417#if !BB_MMU
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00004418 char **ptrs2free = NULL;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004419#endif
4420// FIXME: if exec fails, bash does NOT exit! We do...
Denis Vlasenkoafd7a8d2008-10-09 16:29:44 +00004421 pseudo_exec_argv(&ptrs2free, argv + 1, 0, NULL);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004422 /* never returns */
4423 }
4424}
4425
4426static int builtin_exit(char **argv)
4427{
4428// TODO: bash does it ONLY on top-level sh exit (+interacive only?)
4429 //puts("exit"); /* bash does it */
4430// TODO: warn if we have background jobs: "There are stopped jobs"
4431// On second consecutive 'exit', exit anyway.
4432 if (argv[1] == NULL)
Denis Vlasenko87a86552008-07-29 19:43:10 +00004433 hush_exit(G.last_return_code);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004434 /* mimic bash: exit 123abc == exit 255 + error msg */
4435 xfunc_error_retval = 255;
4436 /* bash: exit -2 == exit 254, no error msg */
4437 hush_exit(xatoi(argv[1]) & 0xff);
4438}
4439
4440static int builtin_export(char **argv)
4441{
4442 const char *value;
4443 char *name = argv[1];
4444
4445 if (name == NULL) {
4446 // TODO:
4447 // ash emits: export VAR='VAL'
4448 // bash: declare -x VAR="VAL"
4449 // (both also escape as needed (quotes, $, etc))
4450 char **e = environ;
4451 if (e)
4452 while (*e)
4453 puts(*e++);
4454 return EXIT_SUCCESS;
4455 }
4456
4457 value = strchr(name, '=');
4458 if (!value) {
4459 /* They are exporting something without a =VALUE */
4460 struct variable *var;
4461
4462 var = get_local_var(name);
4463 if (var) {
4464 var->flg_export = 1;
4465 putenv(var->varstr);
4466 }
4467 /* bash does not return an error when trying to export
4468 * an undefined variable. Do likewise. */
4469 return EXIT_SUCCESS;
4470 }
4471
4472 set_local_var(xstrdup(name), 1);
4473 return EXIT_SUCCESS;
4474}
4475
4476#if ENABLE_HUSH_JOB
4477/* built-in 'fg' and 'bg' handler */
4478static int builtin_fg_bg(char **argv)
4479{
4480 int i, jobnum;
4481 struct pipe *pi;
4482
Denis Vlasenko87a86552008-07-29 19:43:10 +00004483 if (!G.interactive_fd)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004484 return EXIT_FAILURE;
4485 /* If they gave us no args, assume they want the last backgrounded task */
4486 if (!argv[1]) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004487 for (pi = G.job_list; pi; pi = pi->next) {
4488 if (pi->jobid == G.last_jobid) {
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004489 goto found;
4490 }
4491 }
4492 bb_error_msg("%s: no current job", argv[0]);
4493 return EXIT_FAILURE;
4494 }
4495 if (sscanf(argv[1], "%%%d", &jobnum) != 1) {
4496 bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]);
4497 return EXIT_FAILURE;
4498 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004499 for (pi = G.job_list; pi; pi = pi->next) {
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004500 if (pi->jobid == jobnum) {
4501 goto found;
4502 }
4503 }
4504 bb_error_msg("%s: %d: no such job", argv[0], jobnum);
4505 return EXIT_FAILURE;
4506 found:
4507 // TODO: bash prints a string representation
4508 // of job being foregrounded (like "sleep 1 | cat")
4509 if (*argv[0] == 'f') {
4510 /* Put the job into the foreground. */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004511 tcsetpgrp(G.interactive_fd, pi->pgrp);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004512 }
4513
4514 /* Restart the processes in the job */
Denis Vlasenko9af22c72008-10-09 12:54:58 +00004515 debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp);
4516 for (i = 0; i < pi->num_cmds; i++) {
4517 debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid);
4518 pi->cmds[i].is_stopped = 0;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004519 }
Denis Vlasenko9af22c72008-10-09 12:54:58 +00004520 pi->stopped_cmds = 0;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004521
4522 i = kill(- pi->pgrp, SIGCONT);
4523 if (i < 0) {
4524 if (errno == ESRCH) {
4525 delete_finished_bg_job(pi);
4526 return EXIT_SUCCESS;
4527 } else {
4528 bb_perror_msg("kill (SIGCONT)");
4529 }
4530 }
4531
4532 if (*argv[0] == 'f') {
4533 remove_bg_job(pi);
4534 return checkjobs_and_fg_shell(pi);
4535 }
4536 return EXIT_SUCCESS;
4537}
4538#endif
4539
4540#if ENABLE_HUSH_HELP
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004541static int builtin_help(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004542{
4543 const struct built_in_command *x;
4544
4545 printf("\nBuilt-in commands:\n");
4546 printf("-------------------\n");
4547 for (x = bltins; x != &bltins[ARRAY_SIZE(bltins)]; x++) {
4548 printf("%s\t%s\n", x->cmd, x->descr);
4549 }
4550 printf("\n\n");
4551 return EXIT_SUCCESS;
4552}
4553#endif
4554
4555#if ENABLE_HUSH_JOB
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004556static int builtin_jobs(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004557{
4558 struct pipe *job;
4559 const char *status_string;
4560
Denis Vlasenko87a86552008-07-29 19:43:10 +00004561 for (job = G.job_list; job; job = job->next) {
Denis Vlasenko9af22c72008-10-09 12:54:58 +00004562 if (job->alive_cmds == job->stopped_cmds)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004563 status_string = "Stopped";
4564 else
4565 status_string = "Running";
4566
4567 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
4568 }
4569 return EXIT_SUCCESS;
4570}
4571#endif
4572
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00004573static int builtin_pwd(char **argv UNUSED_PARAM)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004574{
4575 puts(set_cwd());
4576 return EXIT_SUCCESS;
4577}
4578
4579static int builtin_read(char **argv)
4580{
4581 char *string;
4582 const char *name = argv[1] ? argv[1] : "REPLY";
4583
4584 string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name), NULL);
4585 return set_local_var(string, 0);
4586}
4587
4588/* built-in 'set [VAR=value]' handler */
4589static int builtin_set(char **argv)
4590{
4591 char *temp = argv[1];
4592 struct variable *e;
4593
4594 if (temp == NULL)
Denis Vlasenko87a86552008-07-29 19:43:10 +00004595 for (e = G.top_var; e; e = e->next)
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004596 puts(e->varstr);
4597 else
4598 set_local_var(xstrdup(temp), 0);
4599
4600 return EXIT_SUCCESS;
4601}
4602
4603static int builtin_shift(char **argv)
4604{
4605 int n = 1;
4606 if (argv[1]) {
4607 n = atoi(argv[1]);
4608 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004609 if (n >= 0 && n < G.global_argc) {
4610 G.global_argv[n] = G.global_argv[0];
4611 G.global_argc -= n;
4612 G.global_argv += n;
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004613 return EXIT_SUCCESS;
4614 }
4615 return EXIT_FAILURE;
4616}
4617
4618static int builtin_source(char **argv)
4619{
4620 FILE *input;
4621 int status;
4622
4623 if (argv[1] == NULL)
4624 return EXIT_FAILURE;
4625
4626 /* XXX search through $PATH is missing */
Denis Vlasenko5415c852008-07-21 23:05:26 +00004627 input = fopen_for_read(argv[1]);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004628 if (!input) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +00004629 bb_error_msg("can't open '%s'", argv[1]);
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004630 return EXIT_FAILURE;
4631 }
4632 close_on_exec_on(fileno(input));
4633
4634 /* Now run the file */
Denis Vlasenko87a86552008-07-29 19:43:10 +00004635 /* XXX argv and argc are broken; need to save old G.global_argv
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004636 * (pointer only is OK!) on this stack frame,
Denis Vlasenko87a86552008-07-29 19:43:10 +00004637 * set G.global_argv=argv+1, recurse, and restore. */
Denis Vlasenkoc7985b72008-06-17 05:43:38 +00004638 status = parse_and_run_file(input);
4639 fclose(input);
4640 return status;
4641}
4642
4643static int builtin_umask(char **argv)
4644{
4645 mode_t new_umask;
4646 const char *arg = argv[1];
4647 char *end;
4648 if (arg) {
4649 new_umask = strtoul(arg, &end, 8);
4650 if (*end != '\0' || end == arg) {
4651 return EXIT_FAILURE;
4652 }
4653 } else {
4654 new_umask = umask(0);
4655 printf("%.3o\n", (unsigned) new_umask);
4656 }
4657 umask(new_umask);
4658 return EXIT_SUCCESS;
4659}
4660
4661static int builtin_unset(char **argv)
4662{
4663 /* bash always returns true */
4664 unset_local_var(argv[1]);
4665 return EXIT_SUCCESS;
4666}
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004667
Denis Vlasenkodadfb492008-07-29 10:16:05 +00004668#if ENABLE_HUSH_LOOPS
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004669static int builtin_break(char **argv)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004670{
Denis Vlasenko87a86552008-07-29 19:43:10 +00004671 if (G.depth_of_loop == 0) {
Denis Vlasenko4f504a92008-07-29 19:48:30 +00004672 bb_error_msg("%s: only meaningful in a loop", argv[0]);
Denis Vlasenkofcf37c32008-07-29 11:37:15 +00004673 return EXIT_SUCCESS; /* bash compat */
4674 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004675 G.flag_break_continue++; /* BC_BREAK = 1 */
4676 G.depth_break_continue = 1;
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004677 if (argv[1]) {
Denis Vlasenko87a86552008-07-29 19:43:10 +00004678 G.depth_break_continue = bb_strtou(argv[1], NULL, 10);
4679 if (errno || !G.depth_break_continue || argv[2]) {
Denis Vlasenko4f504a92008-07-29 19:48:30 +00004680 bb_error_msg("%s: bad arguments", argv[0]);
Denis Vlasenko87a86552008-07-29 19:43:10 +00004681 G.flag_break_continue = BC_BREAK;
4682 G.depth_break_continue = UINT_MAX;
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004683 }
4684 }
Denis Vlasenko87a86552008-07-29 19:43:10 +00004685 if (G.depth_of_loop < G.depth_break_continue)
4686 G.depth_break_continue = G.depth_of_loop;
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004687 return EXIT_SUCCESS;
4688}
4689
Denis Vlasenko6a2d40f2008-07-28 23:07:06 +00004690static int builtin_continue(char **argv)
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004691{
Denis Vlasenko4f504a92008-07-29 19:48:30 +00004692 G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */
4693 return builtin_break(argv);
Denis Vlasenkobcb25532008-07-28 23:04:34 +00004694}
Denis Vlasenkodadfb492008-07-29 10:16:05 +00004695#endif