blob: 9dc85d0bad1debb17b4b9c8a04abea36e4e24c2d [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:
Eric Andersen25f27032001-04-26 23:22:31 +000023 * b_addchr() derived from similar w_addchar function in glibc-2.2
24 * 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
Eric Andersen25f27032001-04-26 23:22:31 +000026 * miscellaneous bugfixes from Matt Kraai
27 *
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 * $_
41 * ! negation operator for pipes
42 * &> and >& redirection of stdout+stderr
43 * Brace Expansion
44 * Tilde Expansion
45 * fancy forms of Parameter Expansion
Eric Andersen78a7c992001-05-15 16:30:25 +000046 * aliases
Eric Andersen25f27032001-04-26 23:22:31 +000047 * Arithmetic Expansion
48 * <(list) and >(list) Process Substitution
Eric Andersen83a2ae22001-05-07 17:59:25 +000049 * reserved words: case, esac, select, function
Eric Andersen25f27032001-04-26 23:22:31 +000050 * Here Documents ( << word )
51 * Functions
52 * Major bugs:
Denis Vlasenkob81b3df2007-04-28 16:48:04 +000053 * job handling woefully incomplete and buggy (improved --vda)
Eric Andersen25f27032001-04-26 23:22:31 +000054 * reserved word execution woefully incomplete and buggy
Eric Andersen25f27032001-04-26 23:22:31 +000055 * to-do:
Eric Andersen83a2ae22001-05-07 17:59:25 +000056 * port selected bugfixes from post-0.49 busybox lash - done?
57 * finish implementing reserved words: for, while, until, do, done
58 * change { and } from special chars to reserved words
59 * builtins: break, continue, eval, return, set, trap, ulimit
60 * test magic exec
Eric Andersen25f27032001-04-26 23:22:31 +000061 * handle children going into background
62 * clean up recognition of null pipes
Eric Andersen25f27032001-04-26 23:22:31 +000063 * check setting of global_argc and global_argv
64 * control-C handling, probably with longjmp
Eric Andersen25f27032001-04-26 23:22:31 +000065 * follow IFS rules more precisely, including update semantics
Eric Andersen25f27032001-04-26 23:22:31 +000066 * figure out what to do with backslash-newline
67 * explain why we use signal instead of sigaction
68 * propagate syntax errors, die on resource errors?
69 * continuation lines, both explicit and implicit - done?
70 * memory leak finding and plugging - done?
71 * more testing, especially quoting rules and redirection
Eric Andersen78a7c992001-05-15 16:30:25 +000072 * document how quoting rules not precisely followed for variable assignments
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +000073 * maybe change charmap[] to use 2-bit entries
Eric Andersen25f27032001-04-26 23:22:31 +000074 * (eventually) remove all the printf's
Eric Andersen25f27032001-04-26 23:22:31 +000075 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000076 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen25f27032001-04-26 23:22:31 +000077 */
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000078
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000079
Eric Andersen25f27032001-04-26 23:22:31 +000080#include <glob.h> /* glob, of course */
Eric Andersen25f27032001-04-26 23:22:31 +000081#include <getopt.h> /* should be pretty obvious */
Eric Andersen25f27032001-04-26 23:22:31 +000082/* #include <dmalloc.h> */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000083
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +000084extern char **environ; /* This is in <unistd.h>, but protected with __USE_GNU */
Denis Vlasenkod01ff132007-05-02 21:40:23 +000085
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000086#include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000087
Denis Vlasenkod01ff132007-05-02 21:40:23 +000088
Denis Vlasenko8412d792007-10-01 09:59:47 +000089#if !BB_MMU
90/* A bit drastic. Can allow some simpler commands
91 * by analysing command in generate_stream_from_list()
92 */
93#undef ENABLE_HUSH_TICK
94#define ENABLE_HUSH_TICK 0
95#endif
96
97
Denis Vlasenkod01ff132007-05-02 21:40:23 +000098/* If you comment out one of these below, it will be #defined later
99 * to perform debug printfs to stderr: */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000100#define debug_printf(...) do {} while (0)
Denis Vlasenko400c5b62007-05-04 13:07:27 +0000101/* Finer-grained debug switches */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000102#define debug_printf_parse(...) do {} while (0)
103#define debug_print_tree(a, b) do {} while (0)
104#define debug_printf_exec(...) do {} while (0)
105#define debug_printf_jobs(...) do {} while (0)
106#define debug_printf_expand(...) do {} while (0)
107#define debug_printf_clean(...) do {} while (0)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000108
109#ifndef debug_printf
110#define debug_printf(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000111#endif
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000112
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000113#ifndef debug_printf_parse
114#define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000115#endif
116
117#ifndef debug_printf_exec
118#define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__)
119#endif
120
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000121#ifndef debug_printf_jobs
122#define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__)
123#define DEBUG_SHELL_JOBS 1
124#endif
125
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000126#ifndef debug_printf_expand
127#define debug_printf_expand(...) fprintf(stderr, __VA_ARGS__)
128#define DEBUG_EXPAND 1
129#endif
130
Denis Vlasenko170435c2007-05-23 13:01:10 +0000131/* Keep unconditionally on for now */
132#define ENABLE_HUSH_DEBUG 1
133
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000134#ifndef debug_printf_clean
135/* broken, of course, but OK for testing */
136static const char *indenter(int i)
137{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000138 static const char blanks[] ALIGN1 =
139 " ";
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000140 return &blanks[sizeof(blanks) - i - 1];
141}
142#define debug_printf_clean(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000143#define DEBUG_CLEAN 1
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000144#endif
145
Eric Andersen25f27032001-04-26 23:22:31 +0000146
Denis Vlasenkof962a032007-11-23 12:50:54 +0000147/*
148 * Leak hunting. Use hush_leaktool.sh for post-processing.
149 */
150#ifdef FOR_HUSH_LEAKTOOL
151void *xxmalloc(int lineno, size_t size)
152{
153 void *ptr = xmalloc((size + 0xff) & ~0xff);
154 fprintf(stderr, "line %d: malloc %p\n", lineno, ptr);
155 return ptr;
156}
157void *xxrealloc(int lineno, void *ptr, size_t size)
158{
159 ptr = xrealloc(ptr, (size + 0xff) & ~0xff);
160 fprintf(stderr, "line %d: realloc %p\n", lineno, ptr);
161 return ptr;
162}
163char *xxstrdup(int lineno, const char *str)
164{
165 char *ptr = xstrdup(str);
166 fprintf(stderr, "line %d: strdup %p\n", lineno, ptr);
167 return ptr;
168}
169void xxfree(void *ptr)
170{
171 fprintf(stderr, "free %p\n", ptr);
172 free(ptr);
173}
174#define xmalloc(s) xxmalloc(__LINE__, s)
175#define xrealloc(p, s) xxrealloc(__LINE__, p, s)
176#define xstrdup(s) xxstrdup(__LINE__, s)
177#define free(p) xxfree(p)
178#endif
179
180
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000181#if !ENABLE_HUSH_INTERACTIVE
182#undef ENABLE_FEATURE_EDITING
183#define ENABLE_FEATURE_EDITING 0
184#undef ENABLE_FEATURE_EDITING_FANCY_PROMPT
185#define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0
186#endif
"Robert P. J. Day"f3501602006-07-01 12:19:39 +0000187
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +0000188#define SPECIAL_VAR_SYMBOL 3
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000189
190#define PARSEFLAG_EXIT_FROM_LOOP 1
191#define PARSEFLAG_SEMICOLON (1 << 1) /* symbol ';' is special for parser */
192#define PARSEFLAG_REPARSING (1 << 2) /* >= 2nd pass */
Eric Andersen25f27032001-04-26 23:22:31 +0000193
194typedef enum {
195 REDIRECT_INPUT = 1,
196 REDIRECT_OVERWRITE = 2,
197 REDIRECT_APPEND = 3,
198 REDIRECT_HEREIS = 4,
199 REDIRECT_IO = 5
200} redir_type;
201
202/* The descrip member of this structure is only used to make debugging
203 * output pretty */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000204static const struct {
205 int mode;
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000206 signed char default_fd;
207 char descrip[3];
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000208} redir_table[] = {
Eric Andersen25f27032001-04-26 23:22:31 +0000209 { 0, 0, "()" },
210 { O_RDONLY, 0, "<" },
211 { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
212 { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" },
213 { O_RDONLY, -1, "<<" },
214 { O_RDWR, 1, "<>" }
215};
216
217typedef enum {
218 PIPE_SEQ = 1,
219 PIPE_AND = 2,
220 PIPE_OR = 3,
221 PIPE_BG = 4,
222} pipe_style;
223
224/* might eventually control execution */
225typedef enum {
226 RES_NONE = 0,
Denis Vlasenko06810332007-05-21 23:30:54 +0000227#if ENABLE_HUSH_IF
Eric Andersen25f27032001-04-26 23:22:31 +0000228 RES_IF = 1,
229 RES_THEN = 2,
230 RES_ELIF = 3,
231 RES_ELSE = 4,
232 RES_FI = 5,
Denis Vlasenko06810332007-05-21 23:30:54 +0000233#endif
234#if ENABLE_HUSH_LOOPS
Eric Andersen25f27032001-04-26 23:22:31 +0000235 RES_FOR = 6,
236 RES_WHILE = 7,
237 RES_UNTIL = 8,
238 RES_DO = 9,
239 RES_DONE = 10,
Denis Vlasenko06810332007-05-21 23:30:54 +0000240 RES_IN = 11,
241#endif
242 RES_XXXX = 12,
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000243 RES_SNTX = 13
Eric Andersen25f27032001-04-26 23:22:31 +0000244} reserved_style;
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000245enum {
246 FLAG_END = (1 << RES_NONE ),
Denis Vlasenko06810332007-05-21 23:30:54 +0000247#if ENABLE_HUSH_IF
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000248 FLAG_IF = (1 << RES_IF ),
249 FLAG_THEN = (1 << RES_THEN ),
250 FLAG_ELIF = (1 << RES_ELIF ),
251 FLAG_ELSE = (1 << RES_ELSE ),
252 FLAG_FI = (1 << RES_FI ),
Denis Vlasenko06810332007-05-21 23:30:54 +0000253#endif
254#if ENABLE_HUSH_LOOPS
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000255 FLAG_FOR = (1 << RES_FOR ),
256 FLAG_WHILE = (1 << RES_WHILE),
257 FLAG_UNTIL = (1 << RES_UNTIL),
258 FLAG_DO = (1 << RES_DO ),
259 FLAG_DONE = (1 << RES_DONE ),
260 FLAG_IN = (1 << RES_IN ),
Denis Vlasenko06810332007-05-21 23:30:54 +0000261#endif
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000262 FLAG_START = (1 << RES_XXXX ),
263};
Eric Andersen25f27032001-04-26 23:22:31 +0000264
265/* This holds pointers to the various results of parsing */
266struct p_context {
267 struct child_prog *child;
268 struct pipe *list_head;
269 struct pipe *pipe;
270 struct redir_struct *pending_redirect;
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000271 smallint res_w;
272 smallint parse_type; /* bitmask of PARSEFLAG_xxx, defines type of parser : ";$" common or special symbol */
273 int old_flag; /* bitmask of FLAG_xxx, for figuring out valid reserved words */
Eric Andersen25f27032001-04-26 23:22:31 +0000274 struct p_context *stack;
275 /* How about quoting status? */
276};
277
278struct redir_struct {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000279 struct redir_struct *next; /* pointer to the next redirect in the list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000280 redir_type type; /* type of redirection */
281 int fd; /* file descriptor being redirected */
282 int dup; /* -1, or file descriptor being duplicated */
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000283 char **glob_word; /* *word.gl_pathv is the filename */
Eric Andersen25f27032001-04-26 23:22:31 +0000284};
285
286struct child_prog {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000287 pid_t pid; /* 0 if exited */
288 char **argv; /* program name and arguments */
289 struct pipe *group; /* if non-NULL, first in group or subshell */
Denis Vlasenko262d7652007-05-20 21:52:49 +0000290 smallint subshell; /* flag, non-zero if group must be forked */
291 smallint is_stopped; /* is the program currently running? */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000292 struct redir_struct *redirects; /* I/O redirections */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000293 struct pipe *family; /* pointer back to the child's parent pipe */
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000294 //sp counting seems to be broken... so commented out, grep for '//sp:'
295 //sp: int sp; /* number of SPECIAL_VAR_SYMBOL */
Denis Vlasenko262d7652007-05-20 21:52:49 +0000296 //seems to be unused, grep for '//pt:'
297 //pt: int parse_type;
Eric Andersen25f27032001-04-26 23:22:31 +0000298};
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000299/* argv vector may contain variable references (^Cvar^C, ^C0^C etc)
300 * and on execution these are substituted with their values.
301 * Substitution can make _several_ words out of one argv[n]!
302 * Example: argv[0]=='.^C*^C.' here: echo .$*.
303 */
Eric Andersen25f27032001-04-26 23:22:31 +0000304
305struct pipe {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000306 struct pipe *next;
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000307 int num_progs; /* total number of programs in job */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000308 int running_progs; /* number of programs running (not exited) */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000309 int stopped_progs; /* number of programs alive, but stopped */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000310#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000311 int jobid; /* job number */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000312 pid_t pgrp; /* process group ID for the job */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000313 char *cmdtext; /* name of job */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000314#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000315 char *cmdbuf; /* buffer various argv's point into */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000316 struct child_prog *progs; /* array of commands in pipe */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000317 int job_context; /* bitmask defining current context */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000318 smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
319 smallint res_word; /* needed for if, for, while, until... */
Eric Andersen25f27032001-04-26 23:22:31 +0000320};
321
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000322/* On program start, environ points to initial environment.
323 * putenv adds new pointers into it, unsetenv removes them.
324 * Neither of these (de)allocates the strings.
325 * setenv allocates new strings in malloc space and does putenv,
326 * and thus setenv is unusable (leaky) for shell's purposes */
327#define setenv(...) setenv_is_leaky_dont_use()
328struct variable {
329 struct variable *next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000330 char *varstr; /* points to "name=" portion */
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000331 int max_len; /* if > 0, name is part of initial env; else name is malloced */
332 smallint flg_export; /* putenv should be done on this var */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000333 smallint flg_read_only;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000334};
335
Eric Andersen25f27032001-04-26 23:22:31 +0000336typedef struct {
337 char *data;
338 int length;
339 int maxlen;
Denis Vlasenkoff097622007-10-01 10:00:45 +0000340 smallint o_quote;
341 smallint nonnull;
Eric Andersen25f27032001-04-26 23:22:31 +0000342} o_string;
343#define NULL_O_STRING {NULL,0,0,0,0}
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000344/* used for initialization: o_string foo = NULL_O_STRING; */
Eric Andersen25f27032001-04-26 23:22:31 +0000345
346/* I can almost use ordinary FILE *. Is open_memstream() universally
347 * available? Where is it documented? */
348struct in_str {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +0000349 const char *p;
350 /* eof_flag=1: last char in ->p is really an EOF */
351 char eof_flag; /* meaningless if ->p == NULL */
352 char peek_buf[2];
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000353#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000354 smallint promptme;
355 smallint promptmode; /* 0: PS1, 1: PS2 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000356#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000357 FILE *file;
358 int (*get) (struct in_str *);
359 int (*peek) (struct in_str *);
360};
361#define b_getch(input) ((input)->get(input))
362#define b_peek(input) ((input)->peek(input))
363
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000364enum {
365 CHAR_ORDINARY = 0,
366 CHAR_ORDINARY_IF_QUOTED = 1, /* example: *, # */
367 CHAR_IFS = 2, /* treated as ordinary if quoted */
368 CHAR_SPECIAL = 3, /* example: $ */
Eric Andersen25f27032001-04-26 23:22:31 +0000369};
370
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000371#define HUSH_VER_STR "0.02"
372
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000373/* "Globals" within this file */
374
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000375/* Sorted roughly by size (smaller offsets == smaller code) */
376struct globals {
377#if ENABLE_HUSH_INTERACTIVE
378 /* 'interactive_fd' is a fd# open to ctty, if we have one
379 * _AND_ if we decided to act interactively */
380 int interactive_fd;
381 const char *PS1;
382 const char *PS2;
383#endif
384#if ENABLE_FEATURE_EDITING
385 line_input_t *line_input_state;
386#endif
387#if ENABLE_HUSH_JOB
388 int run_list_level;
389 pid_t saved_task_pgrp;
390 pid_t saved_tty_pgrp;
391 int last_jobid;
392 struct pipe *job_list;
393 struct pipe *toplevel_list;
394 smallint ctrl_z_flag;
395#endif
396 smallint fake_mode;
397 /* these three support $?, $#, and $1 */
398 char **global_argv;
399 int global_argc;
400 int last_return_code;
401 const char *ifs;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000402 const char *cwd;
403 unsigned last_bg_pid;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +0000404 struct variable *top_var; /* = &shell_ver (set in main()) */
405 struct variable shell_ver;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000406#if ENABLE_FEATURE_SH_STANDALONE
407 struct nofork_save_area nofork_save;
408#endif
409#if ENABLE_HUSH_JOB
410 sigjmp_buf toplevel_jb;
411#endif
412 unsigned char charmap[256];
413 char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2];
414};
415
416#define G (*ptr_to_globals)
417
418#if !ENABLE_HUSH_INTERACTIVE
419enum { interactive_fd = 0 };
420#endif
421#if !ENABLE_HUSH_JOB
422enum { run_list_level = 0 };
423#endif
424
425#if ENABLE_HUSH_INTERACTIVE
426#define interactive_fd (G.interactive_fd )
427#define PS1 (G.PS1 )
428#define PS2 (G.PS2 )
429#endif
430#if ENABLE_FEATURE_EDITING
431#define line_input_state (G.line_input_state)
432#endif
433#if ENABLE_HUSH_JOB
434#define run_list_level (G.run_list_level )
435#define saved_task_pgrp (G.saved_task_pgrp )
436#define saved_tty_pgrp (G.saved_tty_pgrp )
437#define last_jobid (G.last_jobid )
438#define job_list (G.job_list )
439#define toplevel_list (G.toplevel_list )
440#define toplevel_jb (G.toplevel_jb )
441#define ctrl_z_flag (G.ctrl_z_flag )
442#endif /* JOB */
443#define global_argv (G.global_argv )
444#define global_argc (G.global_argc )
445#define last_return_code (G.last_return_code)
446#define ifs (G.ifs )
447#define fake_mode (G.fake_mode )
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000448#define cwd (G.cwd )
449#define last_bg_pid (G.last_bg_pid )
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000450#define top_var (G.top_var )
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000451#define shell_ver (G.shell_ver )
452#if ENABLE_FEATURE_SH_STANDALONE
453#define nofork_save (G.nofork_save )
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000454#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000455#if ENABLE_HUSH_JOB
456#define toplevel_jb (G.toplevel_jb )
457#endif
458#define charmap (G.charmap )
459#define user_input_buf (G.user_input_buf )
460
461
462#define B_CHUNK 100
463#define B_NOSPAC 1
464#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
465
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000466#if 1
467/* Normal */
468static void syntax(const char *msg)
469{
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000470 /* Was using fancy stuff:
471 * (interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...)
472 * but it SEGVs. ?! Oh well... explicit temp ptr works around that */
473 void (*fp)(const char *s, ...);
474
475 fp = (interactive_fd ? bb_error_msg : bb_error_msg_and_die);
476 fp(msg ? "%s: %s" : "syntax error", "syntax error", msg);
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000477}
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000478
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000479#else
480/* Debug */
481static void syntax_lineno(int line)
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000482{
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000483 void (*fp)(const char *s, ...);
484
485 fp = (interactive_fd ? bb_error_msg : bb_error_msg_and_die);
486 fp("syntax error hush.c:%d", line);
Eric Andersen25f27032001-04-26 23:22:31 +0000487}
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000488#define syntax(str) syntax_lineno(__LINE__)
489#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000490
491/* Index of subroutines: */
Eric Andersen25f27032001-04-26 23:22:31 +0000492/* o_string manipulation: */
493static int b_check_space(o_string *o, int len);
494static int b_addchr(o_string *o, int ch);
495static void b_reset(o_string *o);
496static int b_addqchr(o_string *o, int ch, int quote);
Eric Andersen25f27032001-04-26 23:22:31 +0000497/* in_str manipulations: */
498static int static_get(struct in_str *i);
499static int static_peek(struct in_str *i);
500static int file_get(struct in_str *i);
501static int file_peek(struct in_str *i);
502static void setup_file_in_str(struct in_str *i, FILE *f);
503static void setup_string_in_str(struct in_str *i, const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000504/* "run" the final data structures: */
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000505#if !defined(DEBUG_CLEAN)
506#define free_pipe_list(head, indent) free_pipe_list(head)
507#define free_pipe(pi, indent) free_pipe(pi)
508#endif
Eric Andersenbf7df042001-05-23 22:18:35 +0000509static int free_pipe_list(struct pipe *head, int indent);
510static int free_pipe(struct pipe *pi, int indent);
Eric Andersen25f27032001-04-26 23:22:31 +0000511/* really run the final data structures: */
512static int setup_redirects(struct child_prog *prog, int squirrel[]);
Eric Andersen25f27032001-04-26 23:22:31 +0000513static int run_list_real(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000514static void pseudo_exec_argv(char **argv) ATTRIBUTE_NORETURN;
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000515static void pseudo_exec(struct child_prog *child) ATTRIBUTE_NORETURN;
Eric Andersen25f27032001-04-26 23:22:31 +0000516static int run_pipe_real(struct pipe *pi);
517/* extended glob support: */
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000518static char **globhack(const char *src, char **strings);
Eric Andersen25f27032001-04-26 23:22:31 +0000519static int glob_needed(const char *s);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000520static int xglob(o_string *dest, char ***pglob);
Eric Andersen78a7c992001-05-15 16:30:25 +0000521/* variable assignment: */
Eric Andersen78a7c992001-05-15 16:30:25 +0000522static int is_assignment(const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000523/* data structure manipulation: */
524static int setup_redirect(struct p_context *ctx, int fd, redir_type style, struct in_str *input);
525static void initialize_context(struct p_context *ctx);
526static int done_word(o_string *dest, struct p_context *ctx);
527static int done_command(struct p_context *ctx);
528static int done_pipe(struct p_context *ctx, pipe_style type);
529/* primary string parsing: */
530static int redirect_dup_num(struct in_str *input);
531static int redirect_opt_num(o_string *o);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000532#if ENABLE_HUSH_TICK
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000533static int process_command_subs(o_string *dest, struct p_context *ctx, struct in_str *input, const char *subst_end);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000534#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000535static int parse_group(o_string *dest, struct p_context *ctx, struct in_str *input, int ch);
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000536static const char *lookup_param(const char *src);
Eric Andersen25f27032001-04-26 23:22:31 +0000537static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000538static int parse_stream(o_string *dest, struct p_context *ctx, struct in_str *input0, const char *end_trigger);
Eric Andersen25f27032001-04-26 23:22:31 +0000539/* setup: */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000540static int parse_and_run_stream(struct in_str *inp, int parse_flag);
541static int parse_and_run_string(const char *s, int parse_flag);
542static int parse_and_run_file(FILE *f);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000543/* job management: */
Eric Andersenc798b072001-06-22 06:23:03 +0000544static int checkjobs(struct pipe* fg_pipe);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000545#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +0000546static int checkjobs_and_fg_shell(struct pipe* fg_pipe);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000547static void insert_bg_job(struct pipe *pi);
548static void remove_bg_job(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000549static void delete_finished_bg_job(struct pipe *pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000550#else
551int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */
552#endif
Eric Andersenf72f5622001-05-15 23:21:41 +0000553/* local variable support */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000554static char **expand_strvec_to_strvec(char **argv);
555/* used for eval */
556static char *expand_strvec_to_string(char **argv);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +0000557/* used for expansion of right hand of assignments */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000558static char *expand_string_to_string(const char *str);
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000559static struct variable *get_local_var(const char *name);
560static int set_local_var(char *str, int flg_export);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000561static void unset_local_var(const char *name);
Eric Andersen25f27032001-04-26 23:22:31 +0000562
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000563
564static char **add_strings_to_strings(int need_xstrdup, char **strings, char **add)
565{
566 int i;
567 unsigned count1;
568 unsigned count2;
569 char **v;
570
571 v = strings;
572 count1 = 0;
573 if (v) {
574 while (*v) {
575 count1++;
576 v++;
577 }
578 }
579 count2 = 0;
580 v = add;
581 while (*v) {
582 count2++;
583 v++;
584 }
585 v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*));
586 v[count1 + count2] = NULL;
587 i = count2;
588 while (--i >= 0)
589 v[count1 + i] = need_xstrdup ? xstrdup(add[i]) : add[i];
590 return v;
591}
592
593/* 'add' should be a malloced pointer */
594static char **add_string_to_strings(char **strings, char *add)
595{
596 char *v[2];
597
598 v[0] = add;
599 v[1] = NULL;
600
601 return add_strings_to_strings(0, strings, v);
602}
603
604static void free_strings(char **strings)
605{
606 if (strings) {
607 char **v = strings;
608 while (*v)
609 free(*v++);
610 free(strings);
611 }
612}
613
614
Denis Vlasenko83506862007-11-23 13:11:42 +0000615/* Function prototypes for builtins */
616static int builtin_cd(char **argv);
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000617static int builtin_echo(char **argv);
Denis Vlasenko83506862007-11-23 13:11:42 +0000618static int builtin_eval(char **argv);
619static int builtin_exec(char **argv);
620static int builtin_exit(char **argv);
621static int builtin_export(char **argv);
622#if ENABLE_HUSH_JOB
623static int builtin_fg_bg(char **argv);
624static int builtin_jobs(char **argv);
625#endif
626#if ENABLE_HUSH_HELP
627static int builtin_help(char **argv);
628#endif
629static int builtin_pwd(char **argv);
630static int builtin_read(char **argv);
631static int builtin_test(char **argv);
632static int builtin_set(char **argv);
633static int builtin_shift(char **argv);
634static int builtin_source(char **argv);
635static int builtin_umask(char **argv);
636static int builtin_unset(char **argv);
637//static int builtin_not_written(char **argv);
638
Eric Andersen25f27032001-04-26 23:22:31 +0000639/* Table of built-in functions. They can be forked or not, depending on
640 * context: within pipes, they fork. As simple commands, they do not.
641 * When used in non-forking context, they can change global variables
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000642 * in the parent shell process. If forked, of course they cannot.
Eric Andersen25f27032001-04-26 23:22:31 +0000643 * For example, 'unset foo | whatever' will parse and run, but foo will
644 * still be set at the end. */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000645struct built_in_command {
646 const char *cmd; /* name */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000647 int (*function) (char **argv); /* function ptr */
Denis Vlasenko06810332007-05-21 23:30:54 +0000648#if ENABLE_HUSH_HELP
649 const char *descr; /* description */
650#define BLTIN(cmd, func, help) { cmd, func, help }
651#else
652#define BLTIN(cmd, func, help) { cmd, func }
653#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000654};
655
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000656/* For now, echo and test are unconditionally enabled.
657 * Maybe make it configurable? */
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +0000658static const struct built_in_command bltins[] = {
Denis Vlasenko83506862007-11-23 13:11:42 +0000659 BLTIN("[" , builtin_test, "Test condition"),
660 BLTIN("[[" , builtin_test, "Test condition"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000661#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000662 BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000663#endif
Denis Vlasenko06810332007-05-21 23:30:54 +0000664// BLTIN("break" , builtin_not_written, "Exit for, while or until loop"),
665 BLTIN("cd" , builtin_cd, "Change working directory"),
666// BLTIN("continue", builtin_not_written, "Continue for, while or until loop"),
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000667 BLTIN("echo" , builtin_echo, "Write strings to stdout"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000668 BLTIN("eval" , builtin_eval, "Construct and run shell command"),
669 BLTIN("exec" , builtin_exec, "Exec command, replacing this shell with the exec'd process"),
670 BLTIN("exit" , builtin_exit, "Exit from shell"),
671 BLTIN("export", builtin_export, "Set environment variable"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000672#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000673 BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"),
674 BLTIN("jobs" , builtin_jobs, "Lists the active jobs"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000675#endif
Denis Vlasenko06810332007-05-21 23:30:54 +0000676// TODO: remove pwd? we have it as an applet...
677 BLTIN("pwd" , builtin_pwd, "Print current directory"),
678 BLTIN("read" , builtin_read, "Input environment variable"),
679// BLTIN("return", builtin_not_written, "Return from a function"),
680 BLTIN("set" , builtin_set, "Set/unset shell local variables"),
681 BLTIN("shift" , builtin_shift, "Shift positional parameters"),
682// BLTIN("trap" , builtin_not_written, "Trap signals"),
Denis Vlasenko83506862007-11-23 13:11:42 +0000683 BLTIN("test" , builtin_test, "Test condition"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000684// BLTIN("ulimit", builtin_not_written, "Controls resource limits"),
685 BLTIN("umask" , builtin_umask, "Sets file creation mask"),
686 BLTIN("unset" , builtin_unset, "Unset environment variable"),
687 BLTIN("." , builtin_source, "Source-in and run commands in a file"),
688#if ENABLE_HUSH_HELP
689 BLTIN("help" , builtin_help, "List shell built-in commands"),
690#endif
691 BLTIN(NULL, NULL, NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000692};
693
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000694#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000695
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000696/* move to libbb? */
697static void signal_SA_RESTART(int sig, void (*handler)(int))
698{
699 struct sigaction sa;
700 sa.sa_handler = handler;
701 sa.sa_flags = SA_RESTART;
702 sigemptyset(&sa.sa_mask);
703 sigaction(sig, &sa, NULL);
704}
705
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000706/* Signals are grouped, we handle them in batches */
707static void set_fatal_sighandler(void (*handler)(int))
708{
709 signal(SIGILL , handler);
710 signal(SIGTRAP, handler);
711 signal(SIGABRT, handler);
712 signal(SIGFPE , handler);
713 signal(SIGBUS , handler);
714 signal(SIGSEGV, handler);
715 /* bash 3.2 seems to handle these just like 'fatal' ones */
716 signal(SIGHUP , handler);
717 signal(SIGPIPE, handler);
718 signal(SIGALRM, handler);
719}
720static void set_jobctrl_sighandler(void (*handler)(int))
721{
722 signal(SIGTSTP, handler);
723 signal(SIGTTIN, handler);
724 signal(SIGTTOU, handler);
725}
726static void set_misc_sighandler(void (*handler)(int))
727{
728 signal(SIGINT , handler);
729 signal(SIGQUIT, handler);
730 signal(SIGTERM, handler);
731}
732/* SIGCHLD is special and handled separately */
733
734static void set_every_sighandler(void (*handler)(int))
735{
736 set_fatal_sighandler(handler);
737 set_jobctrl_sighandler(handler);
738 set_misc_sighandler(handler);
739 signal(SIGCHLD, handler);
740}
741
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000742static void handler_ctrl_c(int sig)
743{
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000744 debug_printf_jobs("got sig %d\n", sig);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000745// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000746 siglongjmp(toplevel_jb, 1);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000747}
748
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000749static void handler_ctrl_z(int sig)
750{
751 pid_t pid;
752
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000753 debug_printf_jobs("got tty sig %d in pid %d\n", sig, getpid());
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000754 pid = fork();
Denis Vlasenkoc2990322007-05-16 12:57:12 +0000755 if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000756 return;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000757 ctrl_z_flag = 1;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000758 if (!pid) { /* child */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000759 setpgrp();
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000760 debug_printf_jobs("set pgrp for child %d ok\n", getpid());
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000761 set_every_sighandler(SIG_DFL);
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000762 raise(SIGTSTP); /* resend TSTP so that child will be stopped */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000763 debug_printf_jobs("returning in child\n");
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000764 /* return to nofork, it will eventually exit now,
765 * not return back to shell */
766 return;
767 }
768 /* parent */
769 /* finish filling up pipe info */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000770 toplevel_list->pgrp = pid; /* child is in its own pgrp */
771 toplevel_list->progs[0].pid = pid;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000772 /* parent needs to longjmp out of running nofork.
773 * we will "return" exitcode 0, with child put in background */
774// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000775 debug_printf_jobs("siglongjmp in parent\n");
776 siglongjmp(toplevel_jb, 1);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000777}
778
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000779/* Restores tty foreground process group, and exits.
780 * May be called as signal handler for fatal signal
781 * (will faithfully resend signal to itself, producing correct exit state)
782 * or called directly with -EXITCODE.
783 * We also call it if xfunc is exiting. */
784static void sigexit(int sig) ATTRIBUTE_NORETURN;
785static void sigexit(int sig)
786{
787 sigset_t block_all;
788
789 /* Disable all signals: job control, SIGPIPE, etc. */
790 sigfillset(&block_all);
791 sigprocmask(SIG_SETMASK, &block_all, NULL);
792
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000793 if (interactive_fd)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000794 tcsetpgrp(interactive_fd, saved_tty_pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000795
796 /* Not a signal, just exit */
797 if (sig <= 0)
798 _exit(- sig);
799
800 /* Enable only this sig and kill ourself with it */
801 signal(sig, SIG_DFL);
802 sigdelset(&block_all, sig);
803 sigprocmask(SIG_SETMASK, &block_all, NULL);
804 raise(sig);
805 _exit(1); /* Should not reach it */
806}
807
808/* Restores tty foreground process group, and exits. */
809static void hush_exit(int exitcode) ATTRIBUTE_NORETURN;
810static void hush_exit(int exitcode)
811{
812 fflush(NULL); /* flush all streams */
813 sigexit(- (exitcode & 0xff));
814}
815
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000816#else /* !JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000817
818#define set_fatal_sighandler(handler) ((void)0)
819#define set_jobctrl_sighandler(handler) ((void)0)
820#define set_misc_sighandler(handler) ((void)0)
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000821#define hush_exit(e) exit(e)
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000822
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000823#endif /* JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000824
825
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000826static const char *set_cwd(void)
827{
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000828 if (cwd == bb_msg_unknown)
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000829 cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000830 cwd = xrealloc_getcwd_or_warn((char *)cwd);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000831 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000832 cwd = bb_msg_unknown;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000833 return cwd;
834}
835
Denis Vlasenko83506862007-11-23 13:11:42 +0000836
837/* built-in 'test' handler */
838static int builtin_test(char **argv)
839{
840 int argc = 0;
841 while (*argv) {
842 argc++;
843 argv++;
844 }
845 return test_main(argc, argv - argc);
846}
847
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000848/* built-in 'test' handler */
849static int builtin_echo(char **argv)
850{
851 int argc = 0;
852 while (*argv) {
853 argc++;
854 argv++;
855 }
Denis Vlasenkofe5e23b2007-11-24 02:23:51 +0000856 return echo_main(argc, argv - argc);
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000857}
858
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000859/* built-in 'eval' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000860static int builtin_eval(char **argv)
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000861{
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000862 int rcode = EXIT_SUCCESS;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000863
Denis Vlasenko1359da62007-04-21 23:27:30 +0000864 if (argv[1]) {
Denis Vlasenko170435c2007-05-23 13:01:10 +0000865 char *str = expand_strvec_to_string(argv + 1);
866 parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP |
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000867 PARSEFLAG_SEMICOLON);
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000868 free(str);
869 rcode = last_return_code;
870 }
871 return rcode;
872}
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000873
Eric Andersen25f27032001-04-26 23:22:31 +0000874/* built-in 'cd <path>' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000875static int builtin_cd(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000876{
Denis Vlasenko170435c2007-05-23 13:01:10 +0000877 const char *newdir;
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000878 if (argv[1] == NULL) {
879 // bash does nothing (exitcode 0) if HOME is ""; if it's unset,
880 // bash says "bash: cd: HOME not set" and does nothing (exitcode 1)
Denis Vlasenko170435c2007-05-23 13:01:10 +0000881 newdir = getenv("HOME") ? : "/";
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000882 } else
Denis Vlasenko1359da62007-04-21 23:27:30 +0000883 newdir = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000884 if (chdir(newdir)) {
885 printf("cd: %s: %s\n", newdir, strerror(errno));
886 return EXIT_FAILURE;
887 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000888 set_cwd();
Eric Andersen25f27032001-04-26 23:22:31 +0000889 return EXIT_SUCCESS;
890}
891
Eric Andersen25f27032001-04-26 23:22:31 +0000892/* built-in 'exec' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000893static int builtin_exec(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000894{
Denis Vlasenko1359da62007-04-21 23:27:30 +0000895 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000896 return EXIT_SUCCESS; /* Really? */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000897 pseudo_exec_argv(argv + 1);
Eric Andersen25f27032001-04-26 23:22:31 +0000898 /* never returns */
899}
900
901/* built-in 'exit' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000902static int builtin_exit(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000903{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000904// TODO: bash does it ONLY on top-level sh exit (+interacive only?)
905 //puts("exit"); /* bash does it */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000906// TODO: warn if we have background jobs: "There are stopped jobs"
907// On second consecutive 'exit', exit anyway.
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000908
Denis Vlasenko1359da62007-04-21 23:27:30 +0000909 if (argv[1] == NULL)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000910 hush_exit(last_return_code);
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000911 /* mimic bash: exit 123abc == exit 255 + error msg */
912 xfunc_error_retval = 255;
913 /* bash: exit -2 == exit 254, no error msg */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000914 hush_exit(xatoi(argv[1]) & 0xff);
Eric Andersen25f27032001-04-26 23:22:31 +0000915}
916
917/* built-in 'export VAR=value' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000918static int builtin_export(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000919{
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000920 const char *value;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000921 char *name = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000922
Eric Andersenf72f5622001-05-15 23:21:41 +0000923 if (name == NULL) {
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000924 // TODO:
925 // ash emits: export VAR='VAL'
926 // bash: declare -x VAR="VAL"
927 // (both also escape as needed (quotes, $, etc))
928 char **e = environ;
929 if (e)
930 while (*e)
931 puts(*e++);
932 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000933 }
Eric Andersenf72f5622001-05-15 23:21:41 +0000934
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000935 value = strchr(name, '=');
936 if (!value) {
937 /* They are exporting something without a =VALUE */
938 struct variable *var;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000939
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000940 var = get_local_var(name);
941 if (var) {
942 var->flg_export = 1;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000943 putenv(var->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000944 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000945 /* bash does not return an error when trying to export
946 * an undefined variable. Do likewise. */
947 return EXIT_SUCCESS;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000948 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000949
950 set_local_var(xstrdup(name), 1);
951 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000952}
953
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000954#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +0000955/* built-in 'fg' and 'bg' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000956static int builtin_fg_bg(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000957{
Eric Andersen0fcd4472001-05-02 20:12:03 +0000958 int i, jobnum;
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000959 struct pipe *pi;
Eric Andersen25f27032001-04-26 23:22:31 +0000960
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000961 if (!interactive_fd)
Eric Andersenc798b072001-06-22 06:23:03 +0000962 return EXIT_FAILURE;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000963 /* If they gave us no args, assume they want the last backgrounded task */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000964 if (!argv[1]) {
Eric Andersenc798b072001-06-22 06:23:03 +0000965 for (pi = job_list; pi; pi = pi->next) {
966 if (pi->jobid == last_jobid) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000967 goto found;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000968 }
969 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000970 bb_error_msg("%s: no current job", argv[0]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000971 return EXIT_FAILURE;
972 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000973 if (sscanf(argv[1], "%%%d", &jobnum) != 1) {
974 bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000975 return EXIT_FAILURE;
976 }
977 for (pi = job_list; pi; pi = pi->next) {
978 if (pi->jobid == jobnum) {
979 goto found;
Eric Andersen25f27032001-04-26 23:22:31 +0000980 }
981 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000982 bb_error_msg("%s: %d: no such job", argv[0], jobnum);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000983 return EXIT_FAILURE;
984 found:
Denis Vlasenko52881e92007-04-21 13:42:52 +0000985 // TODO: bash prints a string representation
986 // of job being foregrounded (like "sleep 1 | cat")
Denis Vlasenko1359da62007-04-21 23:27:30 +0000987 if (*argv[0] == 'f') {
Eric Andersen028b65b2001-06-28 01:10:11 +0000988 /* Put the job into the foreground. */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000989 tcsetpgrp(interactive_fd, pi->pgrp);
Eric Andersen25f27032001-04-26 23:22:31 +0000990 }
991
992 /* Restart the processes in the job */
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000993 debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_progs, pi->pgrp);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000994 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000995 debug_printf_jobs("reviving pid %d\n", pi->progs[i].pid);
Eric Andersen0fcd4472001-05-02 20:12:03 +0000996 pi->progs[i].is_stopped = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000997 }
998 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +0000999
Denis Vlasenkobb81c582007-01-30 22:32:09 +00001000 i = kill(- pi->pgrp, SIGCONT);
1001 if (i < 0) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +00001002 if (errno == ESRCH) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001003 delete_finished_bg_job(pi);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001004 return EXIT_SUCCESS;
Eric Andersen028b65b2001-06-28 01:10:11 +00001005 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001006 bb_perror_msg("kill (SIGCONT)");
Eric Andersen028b65b2001-06-28 01:10:11 +00001007 }
1008 }
Eric Andersen25f27032001-04-26 23:22:31 +00001009
Denis Vlasenko1359da62007-04-21 23:27:30 +00001010 if (*argv[0] == 'f') {
1011 remove_bg_job(pi);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001012 return checkjobs_and_fg_shell(pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001013 }
Eric Andersen25f27032001-04-26 23:22:31 +00001014 return EXIT_SUCCESS;
1015}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001016#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001017
1018/* built-in 'help' handler */
Denis Vlasenko06810332007-05-21 23:30:54 +00001019#if ENABLE_HUSH_HELP
Denis Vlasenko1359da62007-04-21 23:27:30 +00001020static int builtin_help(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +00001021{
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001022 const struct built_in_command *x;
Eric Andersen25f27032001-04-26 23:22:31 +00001023
1024 printf("\nBuilt-in commands:\n");
1025 printf("-------------------\n");
1026 for (x = bltins; x->cmd; x++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001027 printf("%s\t%s\n", x->cmd, x->descr);
1028 }
1029 printf("\n\n");
1030 return EXIT_SUCCESS;
1031}
Denis Vlasenko06810332007-05-21 23:30:54 +00001032#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001033
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001034#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +00001035/* built-in 'jobs' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001036static int builtin_jobs(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +00001037{
1038 struct pipe *job;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001039 const char *status_string;
Eric Andersen25f27032001-04-26 23:22:31 +00001040
Eric Andersenc798b072001-06-22 06:23:03 +00001041 for (job = job_list; job; job = job->next) {
Eric Andersen25f27032001-04-26 23:22:31 +00001042 if (job->running_progs == job->stopped_progs)
1043 status_string = "Stopped";
1044 else
1045 status_string = "Running";
Eric Andersen52a97ca2001-06-22 06:49:26 +00001046
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001047 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
Eric Andersen25f27032001-04-26 23:22:31 +00001048 }
1049 return EXIT_SUCCESS;
1050}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001051#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001052
Eric Andersen25f27032001-04-26 23:22:31 +00001053/* built-in 'pwd' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001054static int builtin_pwd(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +00001055{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001056 puts(set_cwd());
Eric Andersen25f27032001-04-26 23:22:31 +00001057 return EXIT_SUCCESS;
1058}
1059
1060/* built-in 'read VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001061static int builtin_read(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001062{
Denis Vlasenkod67cef22007-06-13 06:47:47 +00001063 char *string;
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001064 const char *name = argv[1] ? argv[1] : "REPLY";
Eric Andersen25f27032001-04-26 23:22:31 +00001065
Denis Vlasenkod67cef22007-06-13 06:47:47 +00001066 string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name));
1067 return set_local_var(string, 0);
Eric Andersen25f27032001-04-26 23:22:31 +00001068}
1069
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001070/* built-in 'set [VAR=value]' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001071static int builtin_set(char **argv)
Eric Andersenf72f5622001-05-15 23:21:41 +00001072{
Denis Vlasenko1359da62007-04-21 23:27:30 +00001073 char *temp = argv[1];
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001074 struct variable *e;
Eric Andersenf72f5622001-05-15 23:21:41 +00001075
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001076 if (temp == NULL)
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001077 for (e = top_var; e; e = e->next)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00001078 puts(e->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001079 else
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001080 set_local_var(xstrdup(temp), 0);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001081
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001082 return EXIT_SUCCESS;
Eric Andersenf72f5622001-05-15 23:21:41 +00001083}
1084
1085
Eric Andersen25f27032001-04-26 23:22:31 +00001086/* Built-in 'shift' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001087static int builtin_shift(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001088{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001089 int n = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001090 if (argv[1]) {
1091 n = atoi(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001092 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001093 if (n >= 0 && n < global_argc) {
Denis Vlasenko004baba2007-05-20 22:22:18 +00001094 global_argv[n] = global_argv[0];
Eric Andersen25f27032001-04-26 23:22:31 +00001095 global_argc -= n;
1096 global_argv += n;
1097 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00001098 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001099 return EXIT_FAILURE;
Eric Andersen25f27032001-04-26 23:22:31 +00001100}
1101
1102/* Built-in '.' handler (read-in and execute commands from file) */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001103static int builtin_source(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001104{
1105 FILE *input;
1106 int status;
1107
Denis Vlasenko1359da62007-04-21 23:27:30 +00001108 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +00001109 return EXIT_FAILURE;
1110
1111 /* XXX search through $PATH is missing */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001112 input = fopen(argv[1], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00001113 if (!input) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001114 bb_error_msg("cannot open '%s'", argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001115 return EXIT_FAILURE;
1116 }
Denis Vlasenkoa0898172007-10-01 09:59:01 +00001117 close_on_exec_on(fileno(input));
Eric Andersen25f27032001-04-26 23:22:31 +00001118
1119 /* Now run the file */
1120 /* XXX argv and argc are broken; need to save old global_argv
1121 * (pointer only is OK!) on this stack frame,
Denis Vlasenko1359da62007-04-21 23:27:30 +00001122 * set global_argv=argv+1, recurse, and restore. */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001123 status = parse_and_run_file(input);
Eric Andersen25f27032001-04-26 23:22:31 +00001124 fclose(input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001125 return status;
Eric Andersen25f27032001-04-26 23:22:31 +00001126}
1127
Denis Vlasenko1359da62007-04-21 23:27:30 +00001128static int builtin_umask(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001129{
Eric Andersen83a2ae22001-05-07 17:59:25 +00001130 mode_t new_umask;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001131 const char *arg = argv[1];
Eric Andersen83a2ae22001-05-07 17:59:25 +00001132 char *end;
1133 if (arg) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001134 new_umask = strtoul(arg, &end, 8);
1135 if (*end != '\0' || end == arg) {
Eric Andersen83a2ae22001-05-07 17:59:25 +00001136 return EXIT_FAILURE;
1137 }
1138 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001139 new_umask = umask(0);
1140 printf("%.3o\n", (unsigned) new_umask);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001141 }
1142 umask(new_umask);
1143 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00001144}
1145
1146/* built-in 'unset VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001147static int builtin_unset(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001148{
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001149 /* bash always returns true */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001150 unset_local_var(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001151 return EXIT_SUCCESS;
1152}
1153
Denis Vlasenko06810332007-05-21 23:30:54 +00001154//static int builtin_not_written(char **argv)
1155//{
1156// printf("builtin_%s not written\n", argv[0]);
1157// return EXIT_FAILURE;
1158//}
Eric Andersen83a2ae22001-05-07 17:59:25 +00001159
Eric Andersen25f27032001-04-26 23:22:31 +00001160static int b_check_space(o_string *o, int len)
1161{
1162 /* It would be easy to drop a more restrictive policy
1163 * in here, such as setting a maximum string length */
1164 if (o->length + len > o->maxlen) {
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001165 /* assert(data == NULL || o->maxlen != 0); */
Denis Vlasenkoef36ead2007-05-02 15:34:47 +00001166 o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001167 o->data = xrealloc(o->data, 1 + o->maxlen);
Eric Andersen25f27032001-04-26 23:22:31 +00001168 }
1169 return o->data == NULL;
1170}
1171
1172static int b_addchr(o_string *o, int ch)
1173{
Denis Vlasenko831dcc42007-05-16 15:05:36 +00001174 debug_printf("b_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001175 if (b_check_space(o, 1))
1176 return B_NOSPAC;
Eric Andersen25f27032001-04-26 23:22:31 +00001177 o->data[o->length] = ch;
1178 o->length++;
1179 o->data[o->length] = '\0';
1180 return 0;
1181}
1182
1183static void b_reset(o_string *o)
1184{
1185 o->length = 0;
1186 o->nonnull = 0;
Denis Vlasenkod65ea392007-10-01 10:02:25 +00001187 if (o->data)
1188 o->data[0] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00001189}
1190
1191static void b_free(o_string *o)
1192{
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001193 free(o->data);
Denis Vlasenkod65ea392007-10-01 10:02:25 +00001194 memset(o, 0, sizeof(*o));
Eric Andersen25f27032001-04-26 23:22:31 +00001195}
1196
1197/* My analysis of quoting semantics tells me that state information
1198 * is associated with a destination, not a source.
1199 */
1200static int b_addqchr(o_string *o, int ch, int quote)
1201{
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00001202 if (quote && strchr("*?[\\", ch)) {
Eric Andersen25f27032001-04-26 23:22:31 +00001203 int rc;
1204 rc = b_addchr(o, '\\');
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001205 if (rc)
1206 return rc;
Eric Andersen25f27032001-04-26 23:22:31 +00001207 }
1208 return b_addchr(o, ch);
1209}
1210
Eric Andersen25f27032001-04-26 23:22:31 +00001211static int static_get(struct in_str *i)
1212{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001213 int ch = *i->p++;
1214 if (ch == '\0') return EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001215 return ch;
1216}
1217
1218static int static_peek(struct in_str *i)
1219{
1220 return *i->p;
1221}
1222
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001223#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001224#if ENABLE_FEATURE_EDITING
Rob Landley88621d72006-08-29 19:41:06 +00001225static void cmdedit_set_initial_prompt(void)
Eric Andersen25f27032001-04-26 23:22:31 +00001226{
Denis Vlasenko38f63192007-01-22 09:03:07 +00001227#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001228 PS1 = NULL;
1229#else
1230 PS1 = getenv("PS1");
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001231 if (PS1 == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +00001232 PS1 = "\\w \\$ ";
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001233#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001234}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001235#endif /* EDITING */
Eric Andersen25f27032001-04-26 23:22:31 +00001236
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001237static const char* setup_prompt_string(int promptmode)
Eric Andersen25f27032001-04-26 23:22:31 +00001238{
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001239 const char *prompt_str;
1240 debug_printf("setup_prompt_string %d ", promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001241#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001242 /* Set up the prompt */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001243 if (promptmode == 0) { /* PS1 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001244 free((char*)PS1);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001245 PS1 = xasprintf("%s %c ", cwd, (geteuid() != 0) ? '$' : '#');
1246 prompt_str = PS1;
Eric Andersen25f27032001-04-26 23:22:31 +00001247 } else {
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001248 prompt_str = PS2;
Eric Andersen25f27032001-04-26 23:22:31 +00001249 }
1250#else
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001251 prompt_str = (promptmode == 0) ? PS1 : PS2;
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001252#endif
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001253 debug_printf("result '%s'\n", prompt_str);
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001254 return prompt_str;
Eric Andersen25f27032001-04-26 23:22:31 +00001255}
1256
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001257static void get_user_input(struct in_str *i)
Eric Andersen25f27032001-04-26 23:22:31 +00001258{
Denis Vlasenko0937be52007-04-28 16:47:08 +00001259 int r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001260 const char *prompt_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001261
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001262 prompt_str = setup_prompt_string(i->promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001263#if ENABLE_FEATURE_EDITING
Denis Vlasenko170435c2007-05-23 13:01:10 +00001264 /* Enable command line editing only while a command line
1265 * is actually being read; otherwise, we'll end up bequeathing
1266 * atexit() handlers and other unwanted stuff to our
1267 * child processes (rob@sysgo.de) */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001268 r = read_line_input(prompt_str, user_input_buf, BUFSIZ-1, line_input_state);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001269 i->eof_flag = (r < 0);
1270 if (i->eof_flag) { /* EOF/error detected */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001271 user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */
1272 user_input_buf[1] = '\0';
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001273 }
Eric Andersen25f27032001-04-26 23:22:31 +00001274#else
1275 fputs(prompt_str, stdout);
1276 fflush(stdout);
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001277 user_input_buf[0] = r = fgetc(i->file);
1278 /*user_input_buf[1] = '\0'; - already is and never changed */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001279 i->eof_flag = (r == EOF);
Eric Andersen25f27032001-04-26 23:22:31 +00001280#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001281 i->p = user_input_buf;
Eric Andersen25f27032001-04-26 23:22:31 +00001282}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001283#endif /* INTERACTIVE */
Eric Andersen25f27032001-04-26 23:22:31 +00001284
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001285/* This is the magic location that prints prompts
Eric Andersen25f27032001-04-26 23:22:31 +00001286 * and gets data back from the user */
1287static int file_get(struct in_str *i)
1288{
1289 int ch;
1290
Eric Andersen25f27032001-04-26 23:22:31 +00001291 /* If there is data waiting, eat it up */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001292 if (i->p && *i->p) {
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001293#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001294 take_cached:
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001295#endif
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001296 ch = *i->p++;
1297 if (i->eof_flag && !*i->p)
1298 ch = EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001299 } else {
1300 /* need to double check i->file because we might be doing something
1301 * more complicated by now, like sourcing or substituting. */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001302#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001303 if (interactive_fd && i->promptme && i->file == stdin) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001304 do {
1305 get_user_input(i);
1306 } while (!*i->p); /* need non-empty line */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001307 i->promptmode = 1; /* PS2 */
1308 i->promptme = 0;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001309 goto take_cached;
Eric Andersen25f27032001-04-26 23:22:31 +00001310 }
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001311#endif
1312 ch = fgetc(i->file);
Eric Andersen25f27032001-04-26 23:22:31 +00001313 }
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001314 debug_printf("file_get: got a '%c' %d\n", ch, ch);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001315#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001316 if (ch == '\n')
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001317 i->promptme = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001318#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001319 return ch;
1320}
1321
1322/* All the callers guarantee this routine will never be
1323 * used right after a newline, so prompting is not needed.
1324 */
1325static int file_peek(struct in_str *i)
1326{
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001327 int ch;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001328 if (i->p && *i->p) {
1329 if (i->eof_flag && !i->p[1])
1330 return EOF;
1331 return *i->p;
Eric Andersen25f27032001-04-26 23:22:31 +00001332 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001333 ch = fgetc(i->file);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001334 i->eof_flag = (ch == EOF);
1335 i->peek_buf[0] = ch;
1336 i->peek_buf[1] = '\0';
1337 i->p = i->peek_buf;
1338 debug_printf("file_peek: got a '%c' %d\n", *i->p, *i->p);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001339 return ch;
Eric Andersen25f27032001-04-26 23:22:31 +00001340}
1341
1342static void setup_file_in_str(struct in_str *i, FILE *f)
1343{
1344 i->peek = file_peek;
1345 i->get = file_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001346#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001347 i->promptme = 1;
1348 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001349#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001350 i->file = f;
1351 i->p = NULL;
1352}
1353
1354static void setup_string_in_str(struct in_str *i, const char *s)
1355{
1356 i->peek = static_peek;
1357 i->get = static_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001358#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001359 i->promptme = 1;
1360 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001361#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001362 i->p = s;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001363 i->eof_flag = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001364}
1365
Eric Andersen25f27032001-04-26 23:22:31 +00001366/* squirrel != NULL means we squirrel away copies of stdin, stdout,
1367 * and stderr if they are redirected. */
1368static int setup_redirects(struct child_prog *prog, int squirrel[])
1369{
1370 int openfd, mode;
1371 struct redir_struct *redir;
1372
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001373 for (redir = prog->redirects; redir; redir = redir->next) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00001374 if (redir->dup == -1 && redir->glob_word == NULL) {
Eric Andersen817e73c2001-06-06 17:56:09 +00001375 /* something went wrong in the parse. Pretend it didn't happen */
1376 continue;
1377 }
Eric Andersen25f27032001-04-26 23:22:31 +00001378 if (redir->dup == -1) {
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001379 char *p;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001380 mode = redir_table[redir->type].mode;
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001381 p = expand_string_to_string(redir->glob_word[0]);
1382 openfd = open_or_warn(p, mode);
1383 free(p);
Eric Andersen25f27032001-04-26 23:22:31 +00001384 if (openfd < 0) {
1385 /* this could get lost if stderr has been redirected, but
1386 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersen25f27032001-04-26 23:22:31 +00001387 return 1;
1388 }
1389 } else {
1390 openfd = redir->dup;
1391 }
1392
1393 if (openfd != redir->fd) {
1394 if (squirrel && redir->fd < 3) {
1395 squirrel[redir->fd] = dup(redir->fd);
1396 }
Eric Andersen83a2ae22001-05-07 17:59:25 +00001397 if (openfd == -3) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001398 //close(openfd); // close(-3) ??!
Eric Andersen83a2ae22001-05-07 17:59:25 +00001399 } else {
1400 dup2(openfd, redir->fd);
Matt Kraaic616e532001-06-05 16:50:08 +00001401 if (redir->dup == -1)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001402 close(openfd);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001403 }
Eric Andersen25f27032001-04-26 23:22:31 +00001404 }
1405 }
1406 return 0;
1407}
1408
1409static void restore_redirects(int squirrel[])
1410{
1411 int i, fd;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001412 for (i = 0; i < 3; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001413 fd = squirrel[i];
1414 if (fd != -1) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00001415 /* We simply die on error */
1416 xmove_fd(fd, i);
Eric Andersen25f27032001-04-26 23:22:31 +00001417 }
1418 }
1419}
1420
Denis Vlasenko8412d792007-10-01 09:59:47 +00001421/* Called after [v]fork() in run_pipe_real(), or from builtin_exec().
1422 * Never returns.
1423 * XXX no exit() here. If you don't exec, use _exit instead.
Eric Andersen94ac2442001-05-22 19:05:18 +00001424 * The at_exit handlers apparently confuse the calling process,
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001425 * in particular stdin handling. Not sure why? -- because of vfork! (vda) */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001426static void pseudo_exec_argv(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001427{
Eric Andersen78a7c992001-05-15 16:30:25 +00001428 int i, rcode;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001429 char *p;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001430 const struct built_in_command *x;
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001431
Denis Vlasenko1359da62007-04-21 23:27:30 +00001432 for (i = 0; is_assignment(argv[i]); i++) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001433 debug_printf_exec("pid %d environment modification: %s\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001434 getpid(), argv[i]);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001435// FIXME: vfork case??
Denis Vlasenko170435c2007-05-23 13:01:10 +00001436 p = expand_string_to_string(argv[i]);
1437 putenv(p);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001438 }
1439 argv += i;
1440 /* If a variable is assigned in a forest, and nobody listens,
1441 * was it ever really set?
1442 */
1443 if (argv[0] == NULL) {
1444 _exit(EXIT_SUCCESS);
1445 }
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001446
Denis Vlasenko170435c2007-05-23 13:01:10 +00001447 argv = expand_strvec_to_strvec(argv);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001448
Denis Vlasenko1359da62007-04-21 23:27:30 +00001449 /*
1450 * Check if the command matches any of the builtins.
1451 * Depending on context, this might be redundant. But it's
1452 * easier to waste a few CPU cycles than it is to figure out
1453 * if this is one of those cases.
1454 */
1455 for (x = bltins; x->cmd; x++) {
1456 if (strcmp(argv[0], x->cmd) == 0) {
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001457 debug_printf_exec("running builtin '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001458 rcode = x->function(argv);
1459 fflush(stdout);
1460 _exit(rcode);
Eric Andersen94ac2442001-05-22 19:05:18 +00001461 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001462 }
Eric Andersen78a7c992001-05-15 16:30:25 +00001463
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001464 /* Check if the command matches any busybox applets */
Denis Vlasenko80d14be2007-04-10 23:03:30 +00001465#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001466 if (strchr(argv[0], '/') == NULL) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001467 int a = find_applet_by_name(argv[0]);
1468 if (a >= 0) {
1469 if (APPLET_IS_NOEXEC(a)) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001470 debug_printf_exec("running applet '%s'\n", argv[0]);
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001471// is it ok that run_applet_no_and_exit() does exit(), not _exit()?
1472 run_applet_no_and_exit(a, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001473 }
1474 /* re-exec ourselves with the new arguments */
1475 debug_printf_exec("re-execing applet '%s'\n", argv[0]);
Denis Vlasenkobdbbb7e2007-06-08 15:02:55 +00001476 execvp(bb_busybox_exec_path, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001477 /* If they called chroot or otherwise made the binary no longer
1478 * executable, fall through */
1479 }
1480 }
Eric Andersenaac75e52001-04-30 18:18:45 +00001481#endif
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001482
1483 debug_printf_exec("execing '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001484 execvp(argv[0], argv);
1485 bb_perror_msg("cannot exec '%s'", argv[0]);
1486 _exit(1);
1487}
1488
Denis Vlasenko8412d792007-10-01 09:59:47 +00001489/* Called after [v]fork() in run_pipe_real()
1490 */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001491static void pseudo_exec(struct child_prog *child)
1492{
Denis Vlasenkof2fffd02007-05-02 23:39:04 +00001493// FIXME: buggy wrt NOMMU! Must not modify any global data
1494// until it does exec/_exit, but currently it does.
Denis Vlasenko1359da62007-04-21 23:27:30 +00001495 if (child->argv) {
1496 pseudo_exec_argv(child->argv);
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001497 }
1498
1499 if (child->group) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001500#if !BB_MMU
Denis Vlasenko3b492162007-12-24 14:26:57 +00001501 bb_error_msg_and_die("nested lists are not supported on NOMMU");
Denis Vlasenko8412d792007-10-01 09:59:47 +00001502#else
Denis Vlasenko3b492162007-12-24 14:26:57 +00001503 int rcode;
1504
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001505#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001506 debug_printf_exec("pseudo_exec: setting interactive_fd=0\n");
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001507 interactive_fd = 0; /* crucial!!!! */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001508#endif
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001509 debug_printf_exec("pseudo_exec: run_list_real\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001510 rcode = run_list_real(child->group);
Eric Andersenbf7df042001-05-23 22:18:35 +00001511 /* OK to leak memory by not calling free_pipe_list,
Eric Andersen25f27032001-04-26 23:22:31 +00001512 * since this process is about to exit */
Eric Andersen94ac2442001-05-22 19:05:18 +00001513 _exit(rcode);
Denis Vlasenko8412d792007-10-01 09:59:47 +00001514#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001515 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001516
1517 /* Can happen. See what bash does with ">foo" by itself. */
1518 debug_printf("trying to pseudo_exec null command\n");
1519 _exit(EXIT_SUCCESS);
Eric Andersen25f27032001-04-26 23:22:31 +00001520}
1521
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001522#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001523static const char *get_cmdtext(struct pipe *pi)
1524{
1525 char **argv;
1526 char *p;
1527 int len;
1528
1529 /* This is subtle. ->cmdtext is created only on first backgrounding.
1530 * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...)
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001531 * On subsequent bg argv is trashed, but we won't use it */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001532 if (pi->cmdtext)
1533 return pi->cmdtext;
1534 argv = pi->progs[0].argv;
1535 if (!argv || !argv[0])
1536 return (pi->cmdtext = xzalloc(1));
1537
1538 len = 0;
1539 do len += strlen(*argv) + 1; while (*++argv);
1540 pi->cmdtext = p = xmalloc(len);
1541 argv = pi->progs[0].argv;
1542 do {
1543 len = strlen(*argv);
1544 memcpy(p, *argv, len);
1545 p += len;
1546 *p++ = ' ';
1547 } while (*++argv);
1548 p[-1] = '\0';
1549 return pi->cmdtext;
1550}
1551
Eric Andersenbafd94f2001-05-02 16:11:59 +00001552static void insert_bg_job(struct pipe *pi)
1553{
1554 struct pipe *thejob;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001555 int i;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001556
1557 /* Linear search for the ID of the job to use */
1558 pi->jobid = 1;
Eric Andersenc798b072001-06-22 06:23:03 +00001559 for (thejob = job_list; thejob; thejob = thejob->next)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001560 if (thejob->jobid >= pi->jobid)
1561 pi->jobid = thejob->jobid + 1;
1562
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001563 /* Add thejob to the list of running jobs */
Eric Andersenc798b072001-06-22 06:23:03 +00001564 if (!job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001565 thejob = job_list = xmalloc(sizeof(*thejob));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001566 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001567 for (thejob = job_list; thejob->next; thejob = thejob->next)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001568 continue;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001569 thejob->next = xmalloc(sizeof(*thejob));
1570 thejob = thejob->next;
1571 }
1572
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001573 /* Physically copy the struct job */
Eric Andersen0fcd4472001-05-02 20:12:03 +00001574 memcpy(thejob, pi, sizeof(struct pipe));
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001575 thejob->progs = xzalloc(sizeof(pi->progs[0]) * pi->num_progs);
1576 /* We cannot copy entire pi->progs[] vector! Double free()s will happen */
1577 for (i = 0; i < pi->num_progs; i++) {
1578// TODO: do we really need to have so many fields which are just dead weight
1579// at execution stage?
1580 thejob->progs[i].pid = pi->progs[i].pid;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001581 /* all other fields are not used and stay zero */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001582 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001583 thejob->next = NULL;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001584 thejob->cmdtext = xstrdup(get_cmdtext(pi));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001585
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001586 /* We don't wait for background thejobs to return -- append it
Eric Andersenbafd94f2001-05-02 16:11:59 +00001587 to the list of backgrounded thejobs and leave it alone */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001588 printf("[%d] %d %s\n", thejob->jobid, thejob->progs[0].pid, thejob->cmdtext);
Eric Andersen1a6d39b2001-05-08 05:11:54 +00001589 last_bg_pid = thejob->progs[0].pid;
Eric Andersenc798b072001-06-22 06:23:03 +00001590 last_jobid = thejob->jobid;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001591}
1592
Eric Andersenbafd94f2001-05-02 16:11:59 +00001593static void remove_bg_job(struct pipe *pi)
1594{
1595 struct pipe *prev_pipe;
1596
Eric Andersenc798b072001-06-22 06:23:03 +00001597 if (pi == job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001598 job_list = pi->next;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001599 } else {
Eric Andersenc798b072001-06-22 06:23:03 +00001600 prev_pipe = job_list;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001601 while (prev_pipe->next != pi)
1602 prev_pipe = prev_pipe->next;
1603 prev_pipe->next = pi->next;
1604 }
Eric Andersen028b65b2001-06-28 01:10:11 +00001605 if (job_list)
1606 last_jobid = job_list->jobid;
1607 else
1608 last_jobid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001609}
Eric Andersen028b65b2001-06-28 01:10:11 +00001610
Denis Vlasenko1359da62007-04-21 23:27:30 +00001611/* remove a backgrounded job */
1612static void delete_finished_bg_job(struct pipe *pi)
1613{
1614 remove_bg_job(pi);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001615 pi->stopped_progs = 0;
Eric Andersenbf7df042001-05-23 22:18:35 +00001616 free_pipe(pi, 0);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001617 free(pi);
1618}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001619#endif /* JOB */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001620
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001621/* Checks to see if any processes have exited -- if they
Eric Andersenbafd94f2001-05-02 16:11:59 +00001622 have, figure out why and see if a job has completed */
Eric Andersenc798b072001-06-22 06:23:03 +00001623static int checkjobs(struct pipe* fg_pipe)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001624{
Eric Andersenc798b072001-06-22 06:23:03 +00001625 int attributes;
1626 int status;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001627#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00001628 int prognum = 0;
1629 struct pipe *pi;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001630#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001631 pid_t childpid;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001632 int rcode = 0;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001633
Eric Andersenc798b072001-06-22 06:23:03 +00001634 attributes = WUNTRACED;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001635 if (fg_pipe == NULL) {
Eric Andersenc798b072001-06-22 06:23:03 +00001636 attributes |= WNOHANG;
1637 }
1638
Denis Vlasenko1359da62007-04-21 23:27:30 +00001639/* Do we do this right?
1640 * bash-3.00# sleep 20 | false
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001641 * <ctrl-Z pressed>
Denis Vlasenko1359da62007-04-21 23:27:30 +00001642 * [3]+ Stopped sleep 20 | false
1643 * bash-3.00# echo $?
1644 * 1 <========== bg pipe is not fully done, but exitcode is already known!
1645 */
1646
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001647//FIXME: non-interactive bash does not continue even if all processes in fg pipe
1648//are stopped. Testcase: "cat | cat" in a script (not on command line)
1649// + killall -STOP cat
1650
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001651 wait_more:
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001652// TODO: safe_waitpid?
Eric Andersenc798b072001-06-22 06:23:03 +00001653 while ((childpid = waitpid(-1, &status, attributes)) > 0) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001654 const int dead = WIFEXITED(status) || WIFSIGNALED(status);
1655
1656#ifdef DEBUG_SHELL_JOBS
1657 if (WIFSTOPPED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001658 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001659 childpid, WSTOPSIG(status), WEXITSTATUS(status));
1660 if (WIFSIGNALED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001661 debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001662 childpid, WTERMSIG(status), WEXITSTATUS(status));
1663 if (WIFEXITED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001664 debug_printf_jobs("pid %d exited, exitcode %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001665 childpid, WEXITSTATUS(status));
1666#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001667 /* Were we asked to wait for fg pipe? */
Eric Andersenc798b072001-06-22 06:23:03 +00001668 if (fg_pipe) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001669 int i;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001670 for (i = 0; i < fg_pipe->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001671 debug_printf_jobs("check pid %d\n", fg_pipe->progs[i].pid);
Eric Andersenc798b072001-06-22 06:23:03 +00001672 if (fg_pipe->progs[i].pid == childpid) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001673 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001674 if (dead) {
1675 fg_pipe->progs[i].pid = 0;
1676 fg_pipe->running_progs--;
1677 if (i == fg_pipe->num_progs-1)
1678 /* last process gives overall exitstatus */
1679 rcode = WEXITSTATUS(status);
1680 } else {
1681 fg_pipe->progs[i].is_stopped = 1;
1682 fg_pipe->stopped_progs++;
1683 }
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001684 debug_printf_jobs("fg_pipe: running_progs %d stopped_progs %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001685 fg_pipe->running_progs, fg_pipe->stopped_progs);
1686 if (fg_pipe->running_progs - fg_pipe->stopped_progs <= 0) {
1687 /* All processes in fg pipe have exited/stopped */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001688#if ENABLE_HUSH_JOB
Denis Vlasenko1359da62007-04-21 23:27:30 +00001689 if (fg_pipe->running_progs)
1690 insert_bg_job(fg_pipe);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001691#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001692 return rcode;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001693 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001694 /* There are still running processes in the fg pipe */
1695 goto wait_more;
Eric Andersenc798b072001-06-22 06:23:03 +00001696 }
1697 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001698 /* fall through to searching process in bg pipes */
Eric Andersenc798b072001-06-22 06:23:03 +00001699 }
1700
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001701#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001702 /* We asked to wait for bg or orphaned children */
1703 /* No need to remember exitcode in this case */
Eric Andersenc798b072001-06-22 06:23:03 +00001704 for (pi = job_list; pi; pi = pi->next) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001705 prognum = 0;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001706 while (prognum < pi->num_progs) {
1707 if (pi->progs[prognum].pid == childpid)
1708 goto found_pi_and_prognum;
Eric Andersen52a97ca2001-06-22 06:49:26 +00001709 prognum++;
1710 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001711 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001712#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001713
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001714 /* Happens when shell is used as init process (init=/bin/sh) */
1715 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001716 goto wait_more;
Eric Andersenaeb44c42001-05-22 20:29:00 +00001717
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001718#if ENABLE_HUSH_JOB
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001719 found_pi_and_prognum:
Denis Vlasenko1359da62007-04-21 23:27:30 +00001720 if (dead) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001721 /* child exited */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001722 pi->progs[prognum].pid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001723 pi->running_progs--;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001724 if (!pi->running_progs) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001725 printf(JOB_STATUS_FORMAT, pi->jobid,
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001726 "Done", pi->cmdtext);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001727 delete_finished_bg_job(pi);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001728 }
1729 } else {
1730 /* child stopped */
1731 pi->stopped_progs++;
1732 pi->progs[prognum].is_stopped = 1;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001733 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001734#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001735 }
1736
Denis Vlasenko1359da62007-04-21 23:27:30 +00001737 /* wait found no children or failed */
1738
1739 if (childpid && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001740 bb_perror_msg("waitpid");
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001741 return rcode;
Eric Andersenada18ff2001-05-21 16:18:22 +00001742}
1743
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001744#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +00001745static int checkjobs_and_fg_shell(struct pipe* fg_pipe)
1746{
1747 pid_t p;
1748 int rcode = checkjobs(fg_pipe);
1749 /* Job finished, move the shell to the foreground */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001750 p = getpgid(0); /* pgid of our process */
1751 debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001752 if (tcsetpgrp(interactive_fd, p) && errno != ENOTTY)
1753 bb_perror_msg("tcsetpgrp-4a");
1754 return rcode;
1755}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001756#endif
Denis Vlasenko52881e92007-04-21 13:42:52 +00001757
Eric Andersen25f27032001-04-26 23:22:31 +00001758/* run_pipe_real() starts all the jobs, but doesn't wait for anything
Eric Andersenc798b072001-06-22 06:23:03 +00001759 * to finish. See checkjobs().
Eric Andersen25f27032001-04-26 23:22:31 +00001760 *
1761 * return code is normally -1, when the caller has to wait for children
1762 * to finish to determine the exit status of the pipe. If the pipe
1763 * is a simple builtin command, however, the action is done by the
1764 * time run_pipe_real returns, and the exit code is provided as the
1765 * return value.
1766 *
1767 * The input of the pipe is always stdin, the output is always
1768 * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
1769 * because it tries to avoid running the command substitution in
1770 * subshell, when that is in fact necessary. The subshell process
1771 * now has its stdout directed to the input of the appropriate pipe,
1772 * so this routine is noticeably simpler.
Denis Vlasenko170435c2007-05-23 13:01:10 +00001773 *
1774 * Returns -1 only if started some children. IOW: we have to
1775 * mask out retvals of builtins etc with 0xff!
Eric Andersen25f27032001-04-26 23:22:31 +00001776 */
1777static int run_pipe_real(struct pipe *pi)
1778{
1779 int i;
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001780 int nextin;
1781 int pipefds[2]; /* pipefds[0] is for reading */
Rob Landley53702e52006-07-19 21:43:53 +00001782 struct child_prog *child;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001783 const struct built_in_command *x;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001784 char *p;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001785 /* it is not always needed, but we aim to smaller code */
1786 int squirrel[] = { -1, -1, -1 };
1787 int rcode;
Denis Vlasenko52881e92007-04-21 13:42:52 +00001788 const int single_fg = (pi->num_progs == 1 && pi->followup != PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00001789
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001790 debug_printf_exec("run_pipe_real start: single_fg=%d\n", single_fg);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001791
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001792#if ENABLE_HUSH_JOB
Eric Andersenada18ff2001-05-21 16:18:22 +00001793 pi->pgrp = -1;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001794#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001795 pi->running_progs = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001796 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001797
1798 /* Check if this is a simple builtin (not part of a pipe).
1799 * Builtins within pipes have to fork anyway, and are handled in
1800 * pseudo_exec. "echo foo | read bar" doesn't work on bash, either.
1801 */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001802 child = &(pi->progs[0]);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001803 if (single_fg && child->group && child->subshell == 0) {
Eric Andersen04407e52001-06-07 16:42:05 +00001804 debug_printf("non-subshell grouping\n");
1805 setup_redirects(child, squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001806 debug_printf_exec(": run_list_real\n");
Eric Andersen04407e52001-06-07 16:42:05 +00001807 rcode = run_list_real(child->group);
1808 restore_redirects(squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001809 debug_printf_exec("run_pipe_real return %d\n", rcode);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001810 return rcode; // do we need to add '... & 0xff' ?
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001811 }
1812
Denis Vlasenko1359da62007-04-21 23:27:30 +00001813 if (single_fg && child->argv != NULL) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001814 char **argv_expanded;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001815 char **argv = child->argv;
1816
1817 for (i = 0; is_assignment(argv[i]); i++)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001818 continue;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001819 if (i != 0 && argv[i] == NULL) {
Eric Andersen78a7c992001-05-15 16:30:25 +00001820 /* assignments, but no command: set the local environment */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001821 for (i = 0; argv[i] != NULL; i++) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001822 debug_printf("local environment set: %s\n", argv[i]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001823 p = expand_string_to_string(argv[i]);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001824 set_local_var(p, 0);
Eric Andersen78a7c992001-05-15 16:30:25 +00001825 }
1826 return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */
1827 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001828 for (i = 0; is_assignment(argv[i]); i++) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00001829 p = expand_string_to_string(argv[i]);
1830 //sp: child->sp--;
1831 putenv(p);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001832 }
Eric Andersen25f27032001-04-26 23:22:31 +00001833 for (x = bltins; x->cmd; x++) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001834 if (strcmp(argv[i], x->cmd) == 0) {
1835 if (x->function == builtin_exec && argv[i+1] == NULL) {
Eric Andersen83a2ae22001-05-07 17:59:25 +00001836 debug_printf("magic exec\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001837 setup_redirects(child, NULL);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001838 return EXIT_SUCCESS;
1839 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001840 debug_printf("builtin inline %s\n", argv[0]);
Eric Andersen25f27032001-04-26 23:22:31 +00001841 /* XXX setup_redirects acts on file descriptors, not FILEs.
1842 * This is perfect for work that comes after exec().
1843 * Is it really safe for inline use? Experimentally,
1844 * things seem to work with glibc. */
1845 setup_redirects(child, squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001846 debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv[i+1]);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001847 //sp: if (child->sp) /* btw we can do it unconditionally... */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001848 argv_expanded = expand_strvec_to_strvec(argv + i);
1849 rcode = x->function(argv_expanded) & 0xff;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001850 free(argv_expanded);
Eric Andersen25f27032001-04-26 23:22:31 +00001851 restore_redirects(squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001852 debug_printf_exec("run_pipe_real return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00001853 return rcode;
1854 }
1855 }
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001856#if ENABLE_FEATURE_SH_STANDALONE
1857 {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001858 int a = find_applet_by_name(argv[i]);
1859 if (a >= 0 && APPLET_IS_NOFORK(a)) {
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001860 setup_redirects(child, squirrel);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001861 save_nofork_data(&nofork_save);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001862 argv_expanded = argv + i;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001863 //sp: if (child->sp)
Denis Vlasenko170435c2007-05-23 13:01:10 +00001864 argv_expanded = expand_strvec_to_strvec(argv + i);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001865 debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001866 rcode = run_nofork_applet_prime(&nofork_save, a, argv_expanded) & 0xff;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001867 free(argv_expanded);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001868 restore_redirects(squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001869 debug_printf_exec("run_pipe_real return %d\n", rcode);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001870 return rcode;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001871 }
1872 }
1873#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001874 }
1875
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001876 /* Disable job control signals for shell (parent) and
1877 * for initial child code after fork */
1878 set_jobctrl_sighandler(SIG_IGN);
1879
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001880 /* Going to fork a child per each pipe member */
1881 pi->running_progs = 0;
1882 nextin = 0;
1883
Eric Andersen25f27032001-04-26 23:22:31 +00001884 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001885 child = &(pi->progs[i]);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001886 if (child->argv)
1887 debug_printf_exec(": pipe member '%s' '%s'...\n", child->argv[0], child->argv[1]);
1888 else
1889 debug_printf_exec(": pipe member with no argv\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001890
1891 /* pipes are inserted between pairs of commands */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001892 pipefds[0] = 0;
1893 pipefds[1] = 1;
1894 if ((i + 1) < pi->num_progs)
1895 xpipe(pipefds);
Eric Andersen25f27032001-04-26 23:22:31 +00001896
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001897#if BB_MMU
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001898 child->pid = fork();
Eric Andersen72f9a422001-10-28 05:12:20 +00001899#else
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001900 child->pid = vfork();
Eric Andersen72f9a422001-10-28 05:12:20 +00001901#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001902 if (!child->pid) { /* child */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001903#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001904 /* Every child adds itself to new process group
1905 * with pgid == pid of first child in pipe */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001906 if (run_list_level == 1 && interactive_fd) {
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001907 /* Don't do pgrp restore anymore on fatal signals */
1908 set_fatal_sighandler(SIG_DFL);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001909 if (pi->pgrp < 0) /* true for 1st process only */
1910 pi->pgrp = getpid();
1911 if (setpgid(0, pi->pgrp) == 0 && pi->followup != PIPE_BG) {
1912 /* We do it in *every* child, not just first,
1913 * to avoid races */
1914 tcsetpgrp(interactive_fd, pi->pgrp);
1915 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001916 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001917#endif
Denis Vlasenko8412d792007-10-01 09:59:47 +00001918 xmove_fd(nextin, 0);
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001919 xmove_fd(pipefds[1], 1); /* write end */
1920 if (pipefds[0] > 1)
1921 close(pipefds[0]); /* read end */
Eric Andersen25f27032001-04-26 23:22:31 +00001922 /* Like bash, explicit redirects override pipes,
1923 * and the pipe fd is available for dup'ing. */
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001924 setup_redirects(child, NULL);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001925
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001926 /* Restore default handlers just prior to exec */
1927 set_jobctrl_sighandler(SIG_DFL);
1928 set_misc_sighandler(SIG_DFL);
1929 signal(SIGCHLD, SIG_DFL);
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001930 pseudo_exec(child); /* does not return */
Eric Andersen25f27032001-04-26 23:22:31 +00001931 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001932
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001933 if (child->pid < 0) { /* [v]fork failed */
1934 /* Clearly indicate, was it fork or vfork */
1935 bb_perror_msg(BB_MMU ? "cannot fork" : "cannot vfork");
1936 } else {
1937 pi->running_progs++;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001938#if ENABLE_HUSH_JOB
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001939 /* Second and next children need to know pid of first one */
1940 if (pi->pgrp < 0)
1941 pi->pgrp = child->pid;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001942#endif
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001943 }
Eric Andersen25f27032001-04-26 23:22:31 +00001944
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001945 if (i)
1946 close(nextin);
1947 if ((i + 1) < pi->num_progs)
1948 close(pipefds[1]); /* write end */
1949 /* Pass read (output) pipe end to next iteration */
Eric Andersen25f27032001-04-26 23:22:31 +00001950 nextin = pipefds[0];
1951 }
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001952
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001953 debug_printf_exec("run_pipe_real return -1\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001954 return -1;
1955}
1956
Denis Vlasenko4b924f32007-05-30 00:29:55 +00001957#ifndef debug_print_tree
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001958static void debug_print_tree(struct pipe *pi, int lvl)
1959{
1960 static const char *PIPE[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001961 [PIPE_SEQ] = "SEQ",
1962 [PIPE_AND] = "AND",
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001963 [PIPE_OR ] = "OR" ,
1964 [PIPE_BG ] = "BG" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001965 };
1966 static const char *RES[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001967 [RES_NONE ] = "NONE" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00001968#if ENABLE_HUSH_IF
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001969 [RES_IF ] = "IF" ,
1970 [RES_THEN ] = "THEN" ,
1971 [RES_ELIF ] = "ELIF" ,
1972 [RES_ELSE ] = "ELSE" ,
1973 [RES_FI ] = "FI" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00001974#endif
1975#if ENABLE_HUSH_LOOPS
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001976 [RES_FOR ] = "FOR" ,
1977 [RES_WHILE] = "WHILE",
1978 [RES_UNTIL] = "UNTIL",
1979 [RES_DO ] = "DO" ,
1980 [RES_DONE ] = "DONE" ,
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001981 [RES_IN ] = "IN" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00001982#endif
1983 [RES_XXXX ] = "XXXX" ,
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001984 [RES_SNTX ] = "SNTX" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001985 };
1986
1987 int pin, prn;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00001988
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001989 pin = 0;
1990 while (pi) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00001991 fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "",
1992 pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001993 prn = 0;
1994 while (prn < pi->num_progs) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00001995 struct child_prog *child = &pi->progs[prn];
1996 char **argv = child->argv;
1997
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001998 fprintf(stderr, "%*s prog %d", lvl*2, "", prn);
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00001999 if (child->group) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002000 fprintf(stderr, " group %s: (argv=%p)\n",
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002001 (child->subshell ? "()" : "{}"),
2002 argv);
2003 debug_print_tree(child->group, lvl+1);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002004 prn++;
2005 continue;
2006 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002007 if (argv) while (*argv) {
2008 fprintf(stderr, " '%s'", *argv);
2009 argv++;
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002010 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002011 fprintf(stderr, "\n");
2012 prn++;
2013 }
2014 pi = pi->next;
2015 pin++;
2016 }
2017}
2018#endif
2019
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002020/* NB: called by pseudo_exec, and therefore must not modify any
2021 * global data until exec/_exit (we can be a child after vfork!) */
Eric Andersen25f27032001-04-26 23:22:31 +00002022static int run_list_real(struct pipe *pi)
2023{
Denis Vlasenko06810332007-05-21 23:30:54 +00002024 struct pipe *rpipe;
2025#if ENABLE_HUSH_LOOPS
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002026 char *for_varname = NULL;
2027 char **for_lcur = NULL;
2028 char **for_list = NULL;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002029 int flag_rep = 0;
Denis Vlasenko06810332007-05-21 23:30:54 +00002030#endif
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002031 int save_num_progs;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002032 int flag_skip = 1;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002033 int rcode = 0; /* probably for gcc only */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002034 int flag_restore = 0;
Denis Vlasenko06810332007-05-21 23:30:54 +00002035#if ENABLE_HUSH_IF
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002036 int if_code = 0, next_if_code = 0; /* need double-buffer to handle elif */
Denis Vlasenko06810332007-05-21 23:30:54 +00002037#else
2038 enum { if_code = 0, next_if_code = 0 };
2039#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002040 reserved_style rword;
2041 reserved_style skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002042
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002043 debug_printf_exec("run_list_real start lvl %d\n", run_list_level + 1);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002044
Denis Vlasenko06810332007-05-21 23:30:54 +00002045#if ENABLE_HUSH_LOOPS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002046 /* check syntax for "for" */
2047 for (rpipe = pi; rpipe; rpipe = rpipe->next) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002048 if ((rpipe->res_word == RES_IN || rpipe->res_word == RES_FOR)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002049 && (rpipe->next == NULL)
2050 ) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00002051 syntax("malformed for"); /* no IN or no commands after IN */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002052 debug_printf_exec("run_list_real lvl %d return 1\n", run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002053 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002054 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002055 if ((rpipe->res_word == RES_IN && rpipe->next->res_word == RES_IN && rpipe->next->progs[0].argv != NULL)
2056 || (rpipe->res_word == RES_FOR && rpipe->next->res_word != RES_IN)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002057 ) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002058 /* TODO: what is tested in the first condition? */
Denis Vlasenko90e485c2007-05-23 15:22:50 +00002059 syntax("malformed for"); /* 2nd condition: not followed by IN */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002060 debug_printf_exec("run_list_real lvl %d return 1\n", run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002061 return 1;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002062 }
2063 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002064#else
2065 rpipe = NULL;
2066#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002067
2068#if ENABLE_HUSH_JOB
2069 /* Example of nested list: "while true; do { sleep 1 | exit 2; } done".
2070 * We are saving state before entering outermost list ("while...done")
2071 * so that ctrl-Z will correctly background _entire_ outermost list,
2072 * not just a part of it (like "sleep 1 | exit 2") */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002073 if (++run_list_level == 1 && interactive_fd) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002074 if (sigsetjmp(toplevel_jb, 1)) {
2075 /* ctrl-Z forked and we are parent; or ctrl-C.
2076 * Sighandler has longjmped us here */
2077 signal(SIGINT, SIG_IGN);
2078 signal(SIGTSTP, SIG_IGN);
2079 /* Restore level (we can be coming from deep inside
2080 * nested levels) */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002081 run_list_level = 1;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002082#if ENABLE_FEATURE_SH_STANDALONE
2083 if (nofork_save.saved) { /* if save area is valid */
2084 debug_printf_jobs("exiting nofork early\n");
2085 restore_nofork_data(&nofork_save);
2086 }
2087#endif
2088 if (ctrl_z_flag) {
2089 /* ctrl-Z has forked and stored pid of the child in pi->pid.
2090 * Remember this child as background job */
2091 insert_bg_job(pi);
2092 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002093 /* ctrl-C. We just stop doing whatever we were doing */
Denis Vlasenko4daad902007-09-27 10:20:47 +00002094 bb_putchar('\n');
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002095 }
2096 rcode = 0;
2097 goto ret;
2098 }
2099 /* ctrl-Z handler will store pid etc in pi */
2100 toplevel_list = pi;
2101 ctrl_z_flag = 0;
2102#if ENABLE_FEATURE_SH_STANDALONE
2103 nofork_save.saved = 0; /* in case we will run a nofork later */
2104#endif
2105 signal_SA_RESTART(SIGTSTP, handler_ctrl_z);
2106 signal(SIGINT, handler_ctrl_c);
2107 }
2108#endif
2109
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002110 for (; pi; pi = flag_restore ? rpipe : pi->next) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002111 rword = pi->res_word;
Denis Vlasenko06810332007-05-21 23:30:54 +00002112#if ENABLE_HUSH_LOOPS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002113 if (rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002114 flag_restore = 0;
2115 if (!rpipe) {
2116 flag_rep = 0;
2117 rpipe = pi;
2118 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002119 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002120#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002121 debug_printf_exec(": rword=%d if_code=%d next_if_code=%d skip_more=%d\n",
2122 rword, if_code, next_if_code, skip_more_for_this_rword);
2123 if (rword == skip_more_for_this_rword && flag_skip) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002124 if (pi->followup == PIPE_SEQ)
2125 flag_skip = 0;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002126 continue;
2127 }
2128 flag_skip = 1;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002129 skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko06810332007-05-21 23:30:54 +00002130#if ENABLE_HUSH_IF
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002131 if (rword == RES_THEN || rword == RES_ELSE)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002132 if_code = next_if_code;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002133 if (rword == RES_THEN && if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002134 continue;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002135 if (rword == RES_ELSE && !if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002136 continue;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002137 if (rword == RES_ELIF && !if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002138 break;
Denis Vlasenko06810332007-05-21 23:30:54 +00002139#endif
2140#if ENABLE_HUSH_LOOPS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002141 if (rword == RES_FOR && pi->num_progs) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002142 if (!for_lcur) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002143 /* first loop through for */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002144 /* if no variable values after "in" we skip "for" */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002145 if (!pi->next->progs->argv)
2146 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002147 /* create list of variable values */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002148 for_list = expand_strvec_to_strvec(pi->next->progs->argv);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002149 for_lcur = for_list;
2150 for_varname = pi->progs->argv[0];
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002151 pi->progs->argv[0] = NULL;
2152 flag_rep = 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002153 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002154 free(pi->progs->argv[0]);
2155 if (!*for_lcur) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002156 /* for loop is over, clean up */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002157 free(for_list);
2158 for_lcur = NULL;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002159 flag_rep = 0;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002160 pi->progs->argv[0] = for_varname;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002161 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002162 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002163 /* insert next value from for_lcur */
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002164 /* vda: does it need escaping? */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002165 pi->progs->argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002166 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002167 if (rword == RES_IN)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002168 continue;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002169 if (rword == RES_DO) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002170 if (!flag_rep)
2171 continue;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002172 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002173 if (rword == RES_DONE) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002174 if (flag_rep) {
2175 flag_restore = 1;
2176 } else {
2177 rpipe = NULL;
2178 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002179 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002180#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002181 if (pi->num_progs == 0)
2182 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002183 save_num_progs = pi->num_progs; /* save number of programs */
Denis Vlasenkod01ff132007-05-02 21:40:23 +00002184 debug_printf_exec(": run_pipe_real with %d members\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00002185 rcode = run_pipe_real(pi);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002186 if (rcode != -1) {
Eric Andersen25f27032001-04-26 23:22:31 +00002187 /* We only ran a builtin: rcode was set by the return value
2188 * of run_pipe_real(), and we don't need to wait for anything. */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002189 } else if (pi->followup == PIPE_BG) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002190 /* What does bash do with attempts to background builtins? */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002191 /* Even bash 3.2 doesn't do that well with nested bg:
2192 * try "{ { sleep 10; echo DEEP; } & echo HERE; } &".
Denis Vlasenko170435c2007-05-23 13:01:10 +00002193 * I'm NOT treating inner &'s as jobs */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002194#if ENABLE_HUSH_JOB
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002195 if (run_list_level == 1)
Denis Vlasenko170435c2007-05-23 13:01:10 +00002196 insert_bg_job(pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002197#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002198 rcode = EXIT_SUCCESS;
2199 } else {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002200#if ENABLE_HUSH_JOB
Denis Vlasenko170435c2007-05-23 13:01:10 +00002201 /* Paranoia, just "interactive_fd" should be enough? */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002202 if (run_list_level == 1 && interactive_fd) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00002203 /* waits for completion, then fg's main shell */
Denis Vlasenko52881e92007-04-21 13:42:52 +00002204 rcode = checkjobs_and_fg_shell(pi);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002205 } else
2206#endif
2207 {
Denis Vlasenko170435c2007-05-23 13:01:10 +00002208 /* this one just waits for completion */
Eric Andersenc798b072001-06-22 06:23:03 +00002209 rcode = checkjobs(pi);
Eric Andersen25f27032001-04-26 23:22:31 +00002210 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002211 debug_printf_exec(": checkjobs returned %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002212 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002213 debug_printf_exec(": setting last_return_code=%d\n", rcode);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002214 last_return_code = rcode;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002215 pi->num_progs = save_num_progs; /* restore number of programs */
Denis Vlasenko06810332007-05-21 23:30:54 +00002216#if ENABLE_HUSH_IF
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002217 if (rword == RES_IF || rword == RES_ELIF)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002218 next_if_code = rcode; /* can be overwritten a number of times */
Denis Vlasenko06810332007-05-21 23:30:54 +00002219#endif
2220#if ENABLE_HUSH_LOOPS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002221 if (rword == RES_WHILE)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002222 flag_rep = !last_return_code;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002223 if (rword == RES_UNTIL)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002224 flag_rep = last_return_code;
Denis Vlasenko06810332007-05-21 23:30:54 +00002225#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002226 if ((rcode == EXIT_SUCCESS && pi->followup == PIPE_OR)
2227 || (rcode != EXIT_SUCCESS && pi->followup == PIPE_AND)
2228 ) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002229 skip_more_for_this_rword = rword;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002230 }
Eric Andersen028b65b2001-06-28 01:10:11 +00002231 checkjobs(NULL);
Eric Andersen25f27032001-04-26 23:22:31 +00002232 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002233
2234#if ENABLE_HUSH_JOB
2235 if (ctrl_z_flag) {
2236 /* ctrl-Z forked somewhere in the past, we are the child,
2237 * and now we completed running the list. Exit. */
2238 exit(rcode);
2239 }
2240 ret:
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00002241 if (!--run_list_level && interactive_fd) {
2242 signal(SIGTSTP, SIG_IGN);
2243 signal(SIGINT, SIG_IGN);
2244 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002245#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002246 debug_printf_exec("run_list_real lvl %d return %d\n", run_list_level + 1, rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002247 return rcode;
2248}
2249
Eric Andersen25f27032001-04-26 23:22:31 +00002250/* return code is the exit status of the pipe */
Eric Andersenbf7df042001-05-23 22:18:35 +00002251static int free_pipe(struct pipe *pi, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002252{
2253 char **p;
2254 struct child_prog *child;
2255 struct redir_struct *r, *rnext;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002256 int a, i, ret_code = 0;
Eric Andersen52a97ca2001-06-22 06:49:26 +00002257
2258 if (pi->stopped_progs > 0)
2259 return ret_code;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002260 debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid());
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002261 for (i = 0; i < pi->num_progs; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002262 child = &pi->progs[i];
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002263 debug_printf_clean("%s command %d:\n", indenter(indent), i);
Eric Andersen25f27032001-04-26 23:22:31 +00002264 if (child->argv) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002265 for (a = 0, p = child->argv; *p; a++, p++) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002266 debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p);
Eric Andersen25f27032001-04-26 23:22:31 +00002267 }
Denis Vlasenkof962a032007-11-23 12:50:54 +00002268 free_strings(child->argv);
2269 child->argv = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002270 } else if (child->group) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002271 debug_printf_clean("%s begin group (subshell:%d)\n", indenter(indent), child->subshell);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002272 ret_code = free_pipe_list(child->group, indent+3);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002273 debug_printf_clean("%s end group\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002274 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002275 debug_printf_clean("%s (nil)\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002276 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002277 for (r = child->redirects; r; r = rnext) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002278 debug_printf_clean("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->type].descrip);
Eric Andersen25f27032001-04-26 23:22:31 +00002279 if (r->dup == -1) {
Eric Andersen817e73c2001-06-06 17:56:09 +00002280 /* guard against the case >$FOO, where foo is unset or blank */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002281 if (r->glob_word) {
2282 debug_printf_clean(" %s\n", r->glob_word[0]);
2283 free_strings(r->glob_word);
2284 r->glob_word = NULL;
Eric Andersen817e73c2001-06-06 17:56:09 +00002285 }
Eric Andersen25f27032001-04-26 23:22:31 +00002286 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002287 debug_printf_clean("&%d\n", r->dup);
Eric Andersen25f27032001-04-26 23:22:31 +00002288 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002289 rnext = r->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002290 free(r);
2291 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002292 child->redirects = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002293 }
2294 free(pi->progs); /* children are an array, they get freed all at once */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002295 pi->progs = NULL;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002296#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002297 free(pi->cmdtext);
2298 pi->cmdtext = NULL;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002299#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002300 return ret_code;
2301}
2302
Eric Andersenbf7df042001-05-23 22:18:35 +00002303static int free_pipe_list(struct pipe *head, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002304{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002305 int rcode = 0; /* if list has no members */
Eric Andersen25f27032001-04-26 23:22:31 +00002306 struct pipe *pi, *next;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002307
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002308 for (pi = head; pi; pi = next) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002309 debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word);
Eric Andersenbf7df042001-05-23 22:18:35 +00002310 rcode = free_pipe(pi, indent);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002311 debug_printf_clean("%s pipe followup code %d\n", indenter(indent), pi->followup);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002312 next = pi->next;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002313 /*pi->next = NULL;*/
Eric Andersen25f27032001-04-26 23:22:31 +00002314 free(pi);
2315 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002316 return rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00002317}
2318
2319/* Select which version we will use */
2320static int run_list(struct pipe *pi)
2321{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002322 int rcode = 0;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002323 debug_printf_exec("run_list entered\n");
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002324 if (fake_mode == 0) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002325 debug_printf_exec(": run_list_real with %d members\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00002326 rcode = run_list_real(pi);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002327 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002328 /* free_pipe_list has the side effect of clearing memory.
Eric Andersen25f27032001-04-26 23:22:31 +00002329 * In the long run that function can be merged with run_list_real,
2330 * but doing that now would hobble the debugging effort. */
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002331 free_pipe_list(pi, 0);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002332 debug_printf_exec("run_list return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002333 return rcode;
2334}
2335
Denis Vlasenkoff097622007-10-01 10:00:45 +00002336/* Whoever decided to muck with glob internal data is AN IDIOT! */
2337/* uclibc happily changed the way it works (and it has rights to do so!),
2338 all hell broke loose (SEGVs) */
2339
Eric Andersen25f27032001-04-26 23:22:31 +00002340/* The API for glob is arguably broken. This routine pushes a non-matching
2341 * string into the output structure, removing non-backslashed backslashes.
2342 * If someone can prove me wrong, by performing this function within the
2343 * original glob(3) api, feel free to rewrite this routine into oblivion.
Eric Andersen25f27032001-04-26 23:22:31 +00002344 * XXX broken if the last character is '\\', check that before calling.
2345 */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002346static char **globhack(const char *src, char **strings)
Eric Andersen25f27032001-04-26 23:22:31 +00002347{
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002348 int cnt;
Eric Andersen25f27032001-04-26 23:22:31 +00002349 const char *s;
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002350 char *v, *dest;
2351
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002352 for (cnt = 1, s = src; s && *s; s++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002353 if (*s == '\\') s++;
2354 cnt++;
2355 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002356 v = dest = xmalloc(cnt);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002357 for (s = src; s && *s; s++, dest++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002358 if (*s == '\\') s++;
2359 *dest = *s;
2360 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002361 *dest = '\0';
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002362
2363 return add_string_to_strings(strings, v);
Eric Andersen25f27032001-04-26 23:22:31 +00002364}
2365
2366/* XXX broken if the last character is '\\', check that before calling */
2367static int glob_needed(const char *s)
2368{
2369 for (; *s; s++) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002370 if (*s == '\\')
2371 s++;
2372 if (strchr("*[?", *s))
2373 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002374 }
2375 return 0;
2376}
2377
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002378static int xglob(o_string *dest, char ***pglob)
Eric Andersen25f27032001-04-26 23:22:31 +00002379{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002380 /* short-circuit for null word */
Eric Andersen25f27032001-04-26 23:22:31 +00002381 /* we can code this better when the debug_printf's are gone */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002382 if (dest->length == 0) {
2383 if (dest->nonnull) {
2384 /* bash man page calls this an "explicit" null */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002385 *pglob = globhack(dest->data, *pglob);
2386 }
2387 return 0;
2388 }
2389
2390 if (glob_needed(dest->data)) {
2391 glob_t globdata;
2392 int gr;
2393
2394 memset(&globdata, 0, sizeof(globdata));
2395 gr = glob(dest->data, 0, NULL, &globdata);
2396 debug_printf("glob returned %d\n", gr);
2397 if (gr == GLOB_NOSPACE)
2398 bb_error_msg_and_die("out of memory during glob");
2399 if (gr == GLOB_NOMATCH) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002400 debug_printf("globhack returned %d\n", gr);
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002401 /* quote removal, or more accurately, backslash removal */
2402 *pglob = globhack(dest->data, *pglob);
Denis Vlasenkof962a032007-11-23 12:50:54 +00002403 globfree(&globdata);
Eric Andersen25f27032001-04-26 23:22:31 +00002404 return 0;
2405 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002406 if (gr != 0) { /* GLOB_ABORTED ? */
2407 bb_error_msg("glob(3) error %d", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002408 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002409 if (globdata.gl_pathv && globdata.gl_pathv[0])
2410 *pglob = add_strings_to_strings(1, *pglob, globdata.gl_pathv);
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002411 globfree(&globdata);
2412 return gr;
Eric Andersen25f27032001-04-26 23:22:31 +00002413 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002414
2415 *pglob = globhack(dest->data, *pglob);
2416 return 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002417}
2418
Denis Vlasenko170435c2007-05-23 13:01:10 +00002419/* expand_strvec_to_strvec() takes a list of strings, expands
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002420 * all variable references within and returns a pointer to
2421 * a list of expanded strings, possibly with larger number
2422 * of strings. (Think VAR="a b"; echo $VAR).
2423 * This new list is allocated as a single malloc block.
2424 * NULL-terminated list of char* pointers is at the beginning of it,
2425 * followed by strings themself.
2426 * Caller can deallocate entire list by single free(list). */
2427
2428/* Helpers first:
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00002429 * count_XXX estimates size of the block we need. It's okay
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002430 * to over-estimate sizes a bit, if it makes code simpler */
2431static int count_ifs(const char *str)
2432{
2433 int cnt = 0;
2434 debug_printf_expand("count_ifs('%s') ifs='%s'", str, ifs);
2435 while (1) {
2436 str += strcspn(str, ifs);
2437 if (!*str) break;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002438 str++; /* str += strspn(str, ifs); */
2439 cnt++; /* cnt += strspn(str, ifs); - but this code is larger */
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002440 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002441 debug_printf_expand(" return %d\n", cnt);
2442 return cnt;
2443}
2444
2445static void count_var_expansion_space(int *countp, int *lenp, char *arg)
2446{
2447 char first_ch;
2448 int i;
2449 int len = *lenp;
2450 int count = *countp;
2451 const char *val;
2452 char *p;
2453
2454 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL))) {
2455 len += p - arg;
2456 arg = ++p;
2457 p = strchr(p, SPECIAL_VAR_SYMBOL);
2458 first_ch = arg[0];
2459
2460 switch (first_ch & 0x7f) {
2461 /* high bit in 1st_ch indicates that var is double-quoted */
2462 case '$': /* pid */
2463 case '!': /* bg pid */
2464 case '?': /* exitcode */
2465 case '#': /* argc */
2466 len += sizeof(int)*3 + 1; /* enough for int */
2467 break;
2468 case '*':
2469 case '@':
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002470 for (i = 1; global_argv[i]; i++) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002471 len += strlen(global_argv[i]) + 1;
2472 count++;
2473 if (!(first_ch & 0x80))
2474 count += count_ifs(global_argv[i]);
2475 }
2476 break;
2477 default:
2478 *p = '\0';
2479 arg[0] = first_ch & 0x7f;
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002480 if (isdigit(arg[0])) {
2481 i = xatoi_u(arg);
2482 val = NULL;
2483 if (i < global_argc)
2484 val = global_argv[i];
2485 } else
2486 val = lookup_param(arg);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002487 arg[0] = first_ch;
2488 *p = SPECIAL_VAR_SYMBOL;
2489
2490 if (val) {
2491 len += strlen(val) + 1;
2492 if (!(first_ch & 0x80))
2493 count += count_ifs(val);
2494 }
2495 }
2496 arg = ++p;
2497 }
2498
2499 len += strlen(arg) + 1;
2500 count++;
2501 *lenp = len;
2502 *countp = count;
2503}
2504
2505/* Store given string, finalizing the word and starting new one whenever
2506 * we encounter ifs char(s). This is used for expanding variable values.
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002507 * End-of-string does NOT finalize word: think about 'echo -$VAR-' */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002508static int expand_on_ifs(char **list, int n, char **posp, const char *str)
2509{
2510 char *pos = *posp;
2511 while (1) {
2512 int word_len = strcspn(str, ifs);
2513 if (word_len) {
2514 memcpy(pos, str, word_len); /* store non-ifs chars */
2515 pos += word_len;
2516 str += word_len;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002517 }
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002518 if (!*str) /* EOL - do not finalize word */
2519 break;
2520 *pos++ = '\0';
2521 if (n) debug_printf_expand("expand_on_ifs finalized list[%d]=%p '%s' "
2522 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2523 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2524 list[n++] = pos;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002525 str += strspn(str, ifs); /* skip ifs chars */
2526 }
2527 *posp = pos;
2528 return n;
2529}
2530
2531/* Expand all variable references in given string, adding words to list[]
2532 * at n, n+1,... positions. Return updated n (so that list[n] is next one
2533 * to be filled). This routine is extremely tricky: has to deal with
2534 * variables/parameters with whitespace, $* and $@, and constructs like
2535 * 'echo -$*-'. If you play here, you must run testsuite afterwards! */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002536/* NB: another bug is that we cannot detect empty strings yet:
2537 * "" or $empty"" expands to zero words, has to expand to empty word */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002538static int expand_vars_to_list(char **list, int n, char **posp, char *arg, char or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002539{
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002540 /* or_mask is either 0 (normal case) or 0x80
2541 * (expansion of right-hand side of assignment == 1-element expand) */
2542
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002543 char first_ch, ored_ch;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002544 int i;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002545 const char *val;
2546 char *p;
2547 char *pos = *posp;
2548
2549 ored_ch = 0;
2550
2551 if (n) debug_printf_expand("expand_vars_to_list finalized list[%d]=%p '%s' "
2552 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2553 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2554 list[n++] = pos;
2555
2556 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL))) {
2557 memcpy(pos, arg, p - arg);
2558 pos += (p - arg);
2559 arg = ++p;
2560 p = strchr(p, SPECIAL_VAR_SYMBOL);
2561
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002562 first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002563 ored_ch |= first_ch;
2564 val = NULL;
2565 switch (first_ch & 0x7f) {
2566 /* Highest bit in first_ch indicates that var is double-quoted */
2567 case '$': /* pid */
2568 /* FIXME: (echo $$) should still print pid of main shell */
2569 val = utoa(getpid());
2570 break;
2571 case '!': /* bg pid */
2572 val = last_bg_pid ? utoa(last_bg_pid) : (char*)"";
2573 break;
2574 case '?': /* exitcode */
2575 val = utoa(last_return_code);
2576 break;
2577 case '#': /* argc */
2578 val = utoa(global_argc ? global_argc-1 : 0);
2579 break;
2580 case '*':
2581 case '@':
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002582 i = 1;
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002583 if (!global_argv[i])
2584 break;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002585 if (!(first_ch & 0x80)) { /* unquoted $* or $@ */
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002586 while (global_argv[i]) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002587 n = expand_on_ifs(list, n, &pos, global_argv[i]);
2588 debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, global_argc-1);
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002589 if (global_argv[i++][0] && global_argv[i]) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002590 /* this argv[] is not empty and not last:
2591 * put terminating NUL, start new word */
2592 *pos++ = '\0';
2593 if (n) debug_printf_expand("expand_vars_to_list 2 finalized list[%d]=%p '%s' "
2594 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2595 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2596 list[n++] = pos;
2597 }
2598 }
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002599 } else
2600 /* If or_mask is nonzero, we handle assignment 'a=....$@.....'
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00002601 * and in this case should treat it like '$*' - see 'else...' below */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002602 if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002603 while (1) {
2604 strcpy(pos, global_argv[i]);
2605 pos += strlen(global_argv[i]);
2606 if (++i >= global_argc)
2607 break;
2608 *pos++ = '\0';
2609 if (n) debug_printf_expand("expand_vars_to_list 3 finalized list[%d]=%p '%s' "
2610 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2611 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2612 list[n++] = pos;
2613 }
2614 } else { /* quoted $*: add as one word */
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002615 while (1) {
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002616 strcpy(pos, global_argv[i]);
2617 pos += strlen(global_argv[i]);
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00002618 if (!global_argv[++i])
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002619 break;
2620 if (ifs[0])
2621 *pos++ = ifs[0];
2622 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002623 }
2624 break;
2625 default:
2626 *p = '\0';
2627 arg[0] = first_ch & 0x7f;
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002628 if (isdigit(arg[0])) {
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002629 i = xatoi_u(arg);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002630 val = NULL;
2631 if (i < global_argc)
2632 val = global_argv[i];
2633 } else
2634 val = lookup_param(arg);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002635 arg[0] = first_ch;
2636 *p = SPECIAL_VAR_SYMBOL;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002637 if (!(first_ch & 0x80)) { /* unquoted $VAR */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002638 if (val) {
2639 n = expand_on_ifs(list, n, &pos, val);
2640 val = NULL;
2641 }
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002642 } /* else: quoted $VAR, val will be appended at pos */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002643 }
2644 if (val) {
2645 strcpy(pos, val);
2646 pos += strlen(val);
2647 }
2648 arg = ++p;
2649 }
2650 debug_printf_expand("expand_vars_to_list adding tail '%s' at %p\n", arg, pos);
2651 strcpy(pos, arg);
2652 pos += strlen(arg) + 1;
2653 if (pos == list[n-1] + 1) { /* expansion is empty */
2654 if (!(ored_ch & 0x80)) { /* all vars were not quoted... */
2655 debug_printf_expand("expand_vars_to_list list[%d] empty, going back\n", n);
2656 pos--;
2657 n--;
2658 }
2659 }
2660
2661 *posp = pos;
2662 return n;
2663}
2664
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002665static char **expand_variables(char **argv, char or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002666{
2667 int n;
2668 int count = 1;
2669 int len = 0;
2670 char *pos, **v, **list;
2671
2672 v = argv;
2673 if (!*v) debug_printf_expand("count_var_expansion_space: "
2674 "argv[0]=NULL count=%d len=%d alloc_space=%d\n",
2675 count, len, sizeof(char*) * count + len);
2676 while (*v) {
2677 count_var_expansion_space(&count, &len, *v);
2678 debug_printf_expand("count_var_expansion_space: "
2679 "'%s' count=%d len=%d alloc_space=%d\n",
2680 *v, count, len, sizeof(char*) * count + len);
2681 v++;
2682 }
2683 len += sizeof(char*) * count; /* total to alloc */
2684 list = xmalloc(len);
2685 pos = (char*)(list + count);
2686 debug_printf_expand("list=%p, list[0] should be %p\n", list, pos);
2687 n = 0;
2688 v = argv;
2689 while (*v)
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002690 n = expand_vars_to_list(list, n, &pos, *v++, or_mask);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002691
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002692 if (n) debug_printf_expand("finalized list[%d]=%p '%s' "
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002693 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2694 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002695 list[n] = NULL;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002696
2697#ifdef DEBUG_EXPAND
2698 {
2699 int m = 0;
2700 while (m <= n) {
2701 debug_printf_expand("list[%d]=%p '%s'\n", m, list[m], list[m]);
2702 m++;
2703 }
2704 debug_printf_expand("used_space=%d\n", pos - (char*)list);
2705 }
2706#endif
Denis Vlasenko170435c2007-05-23 13:01:10 +00002707 if (ENABLE_HUSH_DEBUG)
2708 if (pos - (char*)list > len)
2709 bb_error_msg_and_die("BUG in varexp");
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002710 return list;
2711}
2712
Denis Vlasenko170435c2007-05-23 13:01:10 +00002713static char **expand_strvec_to_strvec(char **argv)
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002714{
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002715 return expand_variables(argv, 0);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002716}
2717
Denis Vlasenko170435c2007-05-23 13:01:10 +00002718static char *expand_string_to_string(const char *str)
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002719{
2720 char *argv[2], **list;
2721
2722 argv[0] = (char*)str;
2723 argv[1] = NULL;
2724 list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002725 if (ENABLE_HUSH_DEBUG)
2726 if (!list[0] || list[1])
2727 bb_error_msg_and_die("BUG in varexp2");
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002728 /* actually, just move string 2*sizeof(char*) bytes back */
2729 strcpy((char*)list, list[0]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00002730 debug_printf_expand("string_to_string='%s'\n", (char*)list);
2731 return (char*)list;
2732}
2733
2734static char* expand_strvec_to_string(char **argv)
2735{
2736 char **list;
2737
2738 list = expand_variables(argv, 0x80);
2739 /* Convert all NULs to spaces */
2740 if (list[0]) {
2741 int n = 1;
2742 while (list[n]) {
2743 if (ENABLE_HUSH_DEBUG)
2744 if (list[n-1] + strlen(list[n-1]) + 1 != list[n])
2745 bb_error_msg_and_die("BUG in varexp3");
2746 list[n][-1] = ' '; /* TODO: or to ifs[0]? */
2747 n++;
2748 }
2749 }
2750 strcpy((char*)list, list[0]);
2751 debug_printf_expand("strvec_to_string='%s'\n", (char*)list);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002752 return (char*)list;
2753}
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002754
Eric Andersenf72f5622001-05-15 23:21:41 +00002755/* This is used to get/check local shell variables */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002756static struct variable *get_local_var(const char *name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002757{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002758 struct variable *cur;
2759 int len;
Eric Andersenf72f5622001-05-15 23:21:41 +00002760
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002761 if (!name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002762 return NULL;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002763 len = strlen(name);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002764 for (cur = top_var; cur; cur = cur->next) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002765 if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=')
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002766 return cur;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002767 }
Eric Andersenf72f5622001-05-15 23:21:41 +00002768 return NULL;
2769}
2770
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002771/* str holds "NAME=VAL" and is expected to be malloced.
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002772 * We take ownership of it. */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002773static int set_local_var(char *str, int flg_export)
Eric Andersen78a7c992001-05-15 16:30:25 +00002774{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002775 struct variable *cur;
2776 char *value;
2777 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002778
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002779 value = strchr(str, '=');
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002780 if (!value) { /* not expected to ever happen? */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002781 free(str);
Eric Andersen99785762001-05-22 21:37:48 +00002782 return -1;
2783 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002784
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002785 name_len = value - str + 1; /* including '=' */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002786 cur = top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */
2787 while (1) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002788 if (strncmp(cur->varstr, str, name_len) != 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002789 if (!cur->next) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002790 /* Bail out. Note that now cur points
2791 * to last var in linked list */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002792 break;
Eric Andersen20a69a72001-05-15 17:24:44 +00002793 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002794 cur = cur->next;
2795 continue;
Eric Andersen20a69a72001-05-15 17:24:44 +00002796 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002797 /* We found an existing var with this name */
2798 *value = '\0';
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002799 if (cur->flg_read_only) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002800 bb_error_msg("%s: readonly variable", str);
2801 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002802 return -1;
2803 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002804 unsetenv(str); /* just in case */
2805 *value = '=';
2806 if (strcmp(cur->varstr, str) == 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002807 free_and_exp:
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002808 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002809 goto exp;
2810 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002811 if (cur->max_len >= strlen(str)) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002812 /* This one is from startup env, reuse space */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002813 strcpy(cur->varstr, str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002814 goto free_and_exp;
2815 }
2816 /* max_len == 0 signifies "malloced" var, which we can
2817 * (and has to) free */
2818 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002819 free(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002820 cur->max_len = 0;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002821 goto set_str_and_exp;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002822 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002823
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002824 /* Not found - create next variable struct */
2825 cur->next = xzalloc(sizeof(*cur));
2826 cur = cur->next;
2827
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002828 set_str_and_exp:
2829 cur->varstr = str;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002830 exp:
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002831 if (flg_export)
2832 cur->flg_export = 1;
2833 if (cur->flg_export)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002834 return putenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002835 return 0;
Eric Andersen20a69a72001-05-15 17:24:44 +00002836}
2837
Eric Andersenf72f5622001-05-15 23:21:41 +00002838static void unset_local_var(const char *name)
Eric Andersen20a69a72001-05-15 17:24:44 +00002839{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002840 struct variable *cur;
2841 struct variable *prev = prev; /* for gcc */
2842 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002843
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002844 if (!name)
2845 return;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002846 name_len = strlen(name);
2847 cur = top_var;
2848 while (cur) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002849 if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002850 if (cur->flg_read_only) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002851 bb_error_msg("%s: readonly variable", name);
Eric Andersen94ac2442001-05-22 19:05:18 +00002852 return;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002853 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002854 /* prev is ok to use here because 1st variable, HUSH_VERSION,
2855 * is ro, and we cannot reach this code on the 1st pass */
2856 prev->next = cur->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002857 unsetenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002858 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002859 free(cur->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002860 free(cur);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002861 return;
Eric Andersenf72f5622001-05-15 23:21:41 +00002862 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002863 prev = cur;
2864 cur = cur->next;
Eric Andersen20a69a72001-05-15 17:24:44 +00002865 }
Eric Andersen78a7c992001-05-15 16:30:25 +00002866}
2867
2868static int is_assignment(const char *s)
2869{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002870 if (!s || !isalpha(*s))
2871 return 0;
2872 s++;
2873 while (isalnum(*s) || *s == '_')
2874 s++;
2875 return *s == '=';
Eric Andersen78a7c992001-05-15 16:30:25 +00002876}
2877
Eric Andersen25f27032001-04-26 23:22:31 +00002878/* the src parameter allows us to peek forward to a possible &n syntax
2879 * for file descriptor duplication, e.g., "2>&1".
2880 * Return code is 0 normally, 1 if a syntax error is detected in src.
2881 * Resource errors (in xmalloc) cause the process to exit */
2882static int setup_redirect(struct p_context *ctx, int fd, redir_type style,
2883 struct in_str *input)
2884{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002885 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00002886 struct redir_struct *redir = child->redirects;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002887 struct redir_struct *last_redir = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002888
2889 /* Create a new redir_struct and drop it onto the end of the linked list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002890 while (redir) {
2891 last_redir = redir;
2892 redir = redir->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002893 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00002894 redir = xzalloc(sizeof(struct redir_struct));
2895 /* redir->next = NULL; */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002896 /* redir->glob_word = NULL; */
Eric Andersen25f27032001-04-26 23:22:31 +00002897 if (last_redir) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002898 last_redir->next = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002899 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002900 child->redirects = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002901 }
2902
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002903 redir->type = style;
2904 redir->fd = (fd == -1) ? redir_table[style].default_fd : fd;
Eric Andersen25f27032001-04-26 23:22:31 +00002905
2906 debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
2907
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002908 /* Check for a '2>&1' type redirect */
Eric Andersen25f27032001-04-26 23:22:31 +00002909 redir->dup = redirect_dup_num(input);
2910 if (redir->dup == -2) return 1; /* syntax error */
2911 if (redir->dup != -1) {
2912 /* Erik had a check here that the file descriptor in question
Eric Andersen83a2ae22001-05-07 17:59:25 +00002913 * is legit; I postpone that to "run time"
2914 * A "-" representation of "close me" shows up as a -3 here */
Eric Andersen25f27032001-04-26 23:22:31 +00002915 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
2916 } else {
2917 /* We do _not_ try to open the file that src points to,
2918 * since we need to return and let src be expanded first.
2919 * Set ctx->pending_redirect, so we know what to do at the
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002920 * end of the next parsed word. */
Eric Andersen25f27032001-04-26 23:22:31 +00002921 ctx->pending_redirect = redir;
2922 }
2923 return 0;
2924}
2925
Denis Vlasenkoac678ec2007-04-16 22:32:04 +00002926static struct pipe *new_pipe(void)
2927{
Eric Andersen25f27032001-04-26 23:22:31 +00002928 struct pipe *pi;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002929 pi = xzalloc(sizeof(struct pipe));
2930 /*pi->num_progs = 0;*/
2931 /*pi->progs = NULL;*/
2932 /*pi->next = NULL;*/
2933 /*pi->followup = 0; invalid */
2934 if (RES_NONE)
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002935 pi->res_word = RES_NONE;
Eric Andersen25f27032001-04-26 23:22:31 +00002936 return pi;
2937}
2938
2939static void initialize_context(struct p_context *ctx)
2940{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002941 ctx->child = NULL;
Denis Vlasenko1a735862007-05-23 00:32:25 +00002942 ctx->pipe = ctx->list_head = new_pipe();
2943 ctx->pending_redirect = NULL;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002944 ctx->res_w = RES_NONE;
Denis Vlasenko1a735862007-05-23 00:32:25 +00002945 //only ctx->parse_type is not touched... is this intentional?
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002946 ctx->old_flag = 0;
Denis Vlasenko1a735862007-05-23 00:32:25 +00002947 ctx->stack = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002948 done_command(ctx); /* creates the memory for working child */
2949}
2950
2951/* normal return is 0
2952 * if a reserved word is found, and processed, return 1
2953 * should handle if, then, elif, else, fi, for, while, until, do, done.
2954 * case, function, and select are obnoxious, save those for later.
2955 */
Denis Vlasenko06810332007-05-21 23:30:54 +00002956#if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00002957static int reserved_word(o_string *dest, struct p_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00002958{
2959 struct reserved_combo {
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002960 char literal[7];
2961 unsigned char code;
2962 int flag;
Eric Andersen25f27032001-04-26 23:22:31 +00002963 };
2964 /* Mostly a list of accepted follow-up reserved words.
2965 * FLAG_END means we are done with the sequence, and are ready
2966 * to turn the compound list into a command.
2967 * FLAG_START means the word must start a new compound list.
2968 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002969 static const struct reserved_combo reserved_list[] = {
Denis Vlasenko06810332007-05-21 23:30:54 +00002970#if ENABLE_HUSH_IF
Eric Andersen25f27032001-04-26 23:22:31 +00002971 { "if", RES_IF, FLAG_THEN | FLAG_START },
2972 { "then", RES_THEN, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
2973 { "elif", RES_ELIF, FLAG_THEN },
2974 { "else", RES_ELSE, FLAG_FI },
2975 { "fi", RES_FI, FLAG_END },
Denis Vlasenko06810332007-05-21 23:30:54 +00002976#endif
2977#if ENABLE_HUSH_LOOPS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002978 { "for", RES_FOR, FLAG_IN | FLAG_START },
Eric Andersen25f27032001-04-26 23:22:31 +00002979 { "while", RES_WHILE, FLAG_DO | FLAG_START },
2980 { "until", RES_UNTIL, FLAG_DO | FLAG_START },
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002981 { "in", RES_IN, FLAG_DO },
Eric Andersen25f27032001-04-26 23:22:31 +00002982 { "do", RES_DO, FLAG_DONE },
2983 { "done", RES_DONE, FLAG_END }
Denis Vlasenko06810332007-05-21 23:30:54 +00002984#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002985 };
Denis Vlasenko80b8b392007-06-25 10:55:35 +00002986
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002987 const struct reserved_combo *r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002988
Denis Vlasenko80b8b392007-06-25 10:55:35 +00002989 for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) {
Denis Vlasenko1a735862007-05-23 00:32:25 +00002990 if (strcmp(dest->data, r->literal) != 0)
2991 continue;
2992 debug_printf("found reserved word %s, code %d\n", r->literal, r->code);
2993 if (r->flag & FLAG_START) {
2994 struct p_context *new;
2995 debug_printf("push stack\n");
Denis Vlasenko06810332007-05-21 23:30:54 +00002996#if ENABLE_HUSH_LOOPS
Denis Vlasenko1a735862007-05-23 00:32:25 +00002997 if (ctx->res_w == RES_IN || ctx->res_w == RES_FOR) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00002998 syntax("malformed for"); /* example: 'for if' */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002999 ctx->res_w = RES_SNTX;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003000 b_reset(dest);
Eric Andersenaf44a0e2001-04-27 07:26:12 +00003001 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003002 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003003#endif
3004 new = xmalloc(sizeof(*new));
3005 *new = *ctx; /* physical copy */
3006 initialize_context(ctx);
3007 ctx->stack = new;
3008 } else if (ctx->res_w == RES_NONE || !(ctx->old_flag & (1 << r->code))) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003009 syntax(NULL);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003010 ctx->res_w = RES_SNTX;
Denis Vlasenkoff131b92007-04-10 15:42:06 +00003011 b_reset(dest);
Eric Andersen25f27032001-04-26 23:22:31 +00003012 return 1;
3013 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003014 ctx->res_w = r->code;
3015 ctx->old_flag = r->flag;
3016 if (ctx->old_flag & FLAG_END) {
3017 struct p_context *old;
3018 debug_printf("pop stack\n");
3019 done_pipe(ctx, PIPE_SEQ);
3020 old = ctx->stack;
3021 old->child->group = ctx->list_head;
3022 old->child->subshell = 0;
3023 *ctx = *old; /* physical copy */
3024 free(old);
3025 }
3026 b_reset(dest);
3027 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003028 }
3029 return 0;
3030}
Denis Vlasenko06810332007-05-21 23:30:54 +00003031#else
3032#define reserved_word(dest, ctx) ((int)0)
3033#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003034
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003035/* Normal return is 0.
Eric Andersen25f27032001-04-26 23:22:31 +00003036 * Syntax or xglob errors return 1. */
3037static int done_word(o_string *dest, struct p_context *ctx)
3038{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003039 struct child_prog *child = ctx->child;
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003040 char ***glob_target;
3041 int gr;
Eric Andersen25f27032001-04-26 23:22:31 +00003042
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003043 debug_printf_parse("done_word entered: '%s' %p\n", dest->data, child);
Eric Andersen25f27032001-04-26 23:22:31 +00003044 if (dest->length == 0 && !dest->nonnull) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003045 debug_printf_parse("done_word return 0: true null, ignored\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003046 return 0;
3047 }
3048 if (ctx->pending_redirect) {
Denis Vlasenkoff097622007-10-01 10:00:45 +00003049 glob_target = &ctx->pending_redirect->glob_word;
Eric Andersen25f27032001-04-26 23:22:31 +00003050 } else {
3051 if (child->group) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003052 syntax(NULL);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003053 debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n");
3054 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003055 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003056 if (!child->argv && (ctx->parse_type & PARSEFLAG_SEMICOLON)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003057 debug_printf_parse(": checking '%s' for reserved-ness\n", dest->data);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003058 if (reserved_word(dest, ctx)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003059 debug_printf_parse("done_word return %d\n", (ctx->res_w == RES_SNTX));
3060 return (ctx->res_w == RES_SNTX);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003061 }
Eric Andersen25f27032001-04-26 23:22:31 +00003062 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003063 glob_target = &child->argv;
Eric Andersen25f27032001-04-26 23:22:31 +00003064 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003065 gr = xglob(dest, glob_target);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003066 if (gr != 0) {
3067 debug_printf_parse("done_word return 1: xglob returned %d\n", gr);
3068 return 1;
3069 }
Eric Andersen25f27032001-04-26 23:22:31 +00003070
3071 b_reset(dest);
3072 if (ctx->pending_redirect) {
Denis Vlasenkof962a032007-11-23 12:50:54 +00003073 /* NB: don't free_strings(ctx->pending_redirect->glob_word) here */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003074 if (ctx->pending_redirect->glob_word
3075 && ctx->pending_redirect->glob_word[0]
3076 && ctx->pending_redirect->glob_word[1]
3077 ) {
3078 /* more than one word resulted from globbing redir */
3079 ctx->pending_redirect = NULL;
Manuel Novoa III cad53642003-03-19 09:13:01 +00003080 bb_error_msg("ambiguous redirect");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003081 debug_printf_parse("done_word return 1: ambiguous redirect\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003082 return 1;
3083 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003084 ctx->pending_redirect = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003085 }
Denis Vlasenko06810332007-05-21 23:30:54 +00003086#if ENABLE_HUSH_LOOPS
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003087 if (ctx->res_w == RES_FOR) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003088 done_word(dest, ctx);
3089 done_pipe(ctx, PIPE_SEQ);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003090 }
Denis Vlasenko06810332007-05-21 23:30:54 +00003091#endif
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003092 debug_printf_parse("done_word return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003093 return 0;
3094}
3095
3096/* The only possible error here is out of memory, in which case
3097 * xmalloc exits. */
3098static int done_command(struct p_context *ctx)
3099{
3100 /* The child is really already in the pipe structure, so
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003101 * advance the pipe counter and make a new, null child. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003102 struct pipe *pi = ctx->pipe;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003103 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00003104
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003105 if (child) {
3106 if (child->group == NULL
3107 && child->argv == NULL
3108 && child->redirects == NULL
3109 ) {
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003110 debug_printf_parse("done_command: skipping null cmd, num_progs=%d\n", pi->num_progs);
3111 return pi->num_progs;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003112 }
Eric Andersen25f27032001-04-26 23:22:31 +00003113 pi->num_progs++;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003114 debug_printf_parse("done_command: ++num_progs=%d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00003115 } else {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003116 debug_printf_parse("done_command: initializing, num_progs=%d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00003117 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003118
3119 /* Only real trickiness here is that the uncommitted
3120 * child structure is not counted in pi->num_progs. */
Eric Andersen25f27032001-04-26 23:22:31 +00003121 pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1));
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003122 child = &pi->progs[pi->num_progs];
Eric Andersen25f27032001-04-26 23:22:31 +00003123
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003124 memset(child, 0, sizeof(*child));
3125 /*child->redirects = NULL;*/
3126 /*child->argv = NULL;*/
3127 /*child->is_stopped = 0;*/
3128 /*child->group = NULL;*/
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003129 child->family = pi;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003130 //sp: /*child->sp = 0;*/
Denis Vlasenko262d7652007-05-20 21:52:49 +00003131 //pt: child->parse_type = ctx->parse_type;
Eric Andersen25f27032001-04-26 23:22:31 +00003132
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003133 ctx->child = child;
Eric Andersen25f27032001-04-26 23:22:31 +00003134 /* but ctx->pipe and ctx->list_head remain unchanged */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003135
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003136 return pi->num_progs; /* used only for 0/nonzero check */
Eric Andersen25f27032001-04-26 23:22:31 +00003137}
3138
3139static int done_pipe(struct p_context *ctx, pipe_style type)
3140{
3141 struct pipe *new_p;
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003142 int not_null;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003143
3144 debug_printf_parse("done_pipe entered, followup %d\n", type);
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003145 not_null = done_command(ctx); /* implicit closure of previous command */
Eric Andersen25f27032001-04-26 23:22:31 +00003146 ctx->pipe->followup = type;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003147 ctx->pipe->res_word = ctx->res_w;
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003148 /* Without this check, even just <enter> on command line generates
3149 * tree of three NOPs (!). Which is harmless but annoying.
3150 * IOW: it is safe to do it unconditionally. */
3151 if (not_null) {
3152 new_p = new_pipe();
3153 ctx->pipe->next = new_p;
3154 ctx->pipe = new_p;
3155 ctx->child = NULL;
3156 done_command(ctx); /* set up new pipe to accept commands */
3157 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003158 debug_printf_parse("done_pipe return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003159 return 0;
3160}
3161
3162/* peek ahead in the in_str to find out if we have a "&n" construct,
3163 * as in "2>&1", that represents duplicating a file descriptor.
3164 * returns either -2 (syntax error), -1 (no &), or the number found.
3165 */
3166static int redirect_dup_num(struct in_str *input)
3167{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003168 int ch, d = 0, ok = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003169 ch = b_peek(input);
3170 if (ch != '&') return -1;
3171
3172 b_getch(input); /* get the & */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003173 ch = b_peek(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003174 if (ch == '-') {
3175 b_getch(input);
3176 return -3; /* "-" represents "close me" */
3177 }
3178 while (isdigit(ch)) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00003179 d = d*10 + (ch-'0');
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003180 ok = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003181 b_getch(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003182 ch = b_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003183 }
3184 if (ok) return d;
3185
Manuel Novoa III cad53642003-03-19 09:13:01 +00003186 bb_error_msg("ambiguous redirect");
Eric Andersen25f27032001-04-26 23:22:31 +00003187 return -2;
3188}
3189
3190/* If a redirect is immediately preceded by a number, that number is
3191 * supposed to tell which file descriptor to redirect. This routine
3192 * looks for such preceding numbers. In an ideal world this routine
3193 * needs to handle all the following classes of redirects...
3194 * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
3195 * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
3196 * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
3197 * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
3198 * A -1 output from this program means no valid number was found, so the
3199 * caller should use the appropriate default for this redirection.
3200 */
3201static int redirect_opt_num(o_string *o)
3202{
3203 int num;
3204
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003205 if (o->length == 0)
3206 return -1;
3207 for (num = 0; num < o->length; num++) {
3208 if (!isdigit(*(o->data + num))) {
Eric Andersen25f27032001-04-26 23:22:31 +00003209 return -1;
3210 }
3211 }
3212 /* reuse num (and save an int) */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003213 num = atoi(o->data);
Eric Andersen25f27032001-04-26 23:22:31 +00003214 b_reset(o);
3215 return num;
3216}
3217
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003218#if ENABLE_HUSH_TICK
Denis Vlasenko8412d792007-10-01 09:59:47 +00003219/* NB: currently disabled on NOMMU */
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00003220static FILE *generate_stream_from_list(struct pipe *head)
Eric Andersen25f27032001-04-26 23:22:31 +00003221{
3222 FILE *pf;
Eric Andersen25f27032001-04-26 23:22:31 +00003223 int pid, channel[2];
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003224
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +00003225 xpipe(channel);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003226 pid = fork();
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003227 if (pid < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00003228 bb_perror_msg_and_die("fork");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003229 } else if (pid == 0) {
Eric Andersen25f27032001-04-26 23:22:31 +00003230 close(channel[0]);
3231 if (channel[1] != 1) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00003232 dup2(channel[1], 1);
Eric Andersen25f27032001-04-26 23:22:31 +00003233 close(channel[1]);
3234 }
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003235 /* Prevent it from trying to handle ctrl-z etc */
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003236#if ENABLE_HUSH_JOB
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003237 run_list_level = 1;
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003238#endif
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003239 /* Process substitution is not considered to be usual
3240 * 'command execution'.
3241 * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. */
3242 /* Not needed, we are relying on it being disabled
3243 * everywhere outside actual command execution. */
3244 /*set_jobctrl_sighandler(SIG_IGN);*/
3245 set_misc_sighandler(SIG_DFL);
Eric Andersen94ac2442001-05-22 19:05:18 +00003246 _exit(run_list_real(head)); /* leaks memory */
Eric Andersen25f27032001-04-26 23:22:31 +00003247 }
Eric Andersen25f27032001-04-26 23:22:31 +00003248 close(channel[1]);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003249 pf = fdopen(channel[0], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00003250 return pf;
3251}
3252
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003253/* Return code is exit status of the process that is run. */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003254static int process_command_subs(o_string *dest, struct p_context *ctx,
3255 struct in_str *input, const char *subst_end)
Eric Andersen25f27032001-04-26 23:22:31 +00003256{
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003257 int retcode, ch, eol_cnt;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003258 o_string result = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003259 struct p_context inner;
3260 FILE *p;
3261 struct in_str pipe_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003262
Eric Andersen25f27032001-04-26 23:22:31 +00003263 initialize_context(&inner);
3264
3265 /* recursion to generate command */
3266 retcode = parse_stream(&result, &inner, input, subst_end);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003267 if (retcode != 0)
3268 return retcode; /* syntax error or EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003269 done_word(&result, &inner);
3270 done_pipe(&inner, PIPE_SEQ);
3271 b_free(&result);
3272
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003273 p = generate_stream_from_list(inner.list_head);
3274 if (p == NULL) return 1;
Denis Vlasenkoa0898172007-10-01 09:59:01 +00003275 close_on_exec_on(fileno(p));
Eric Andersen25f27032001-04-26 23:22:31 +00003276 setup_file_in_str(&pipe_str, p);
3277
3278 /* now send results of command back into original context */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003279 eol_cnt = 0;
3280 while ((ch = b_getch(&pipe_str)) != EOF) {
3281 if (ch == '\n') {
3282 eol_cnt++;
3283 continue;
3284 }
3285 while (eol_cnt) {
Denis Vlasenkoff097622007-10-01 10:00:45 +00003286 b_addqchr(dest, '\n', dest->o_quote);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003287 eol_cnt--;
3288 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00003289 b_addqchr(dest, ch, dest->o_quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003290 }
3291
3292 debug_printf("done reading from pipe, pclose()ing\n");
3293 /* This is the step that wait()s for the child. Should be pretty
3294 * safe, since we just read an EOF from its stdout. We could try
Denis Vlasenko262d7652007-05-20 21:52:49 +00003295 * to do better, by using wait(), and keeping track of background jobs
Eric Andersen25f27032001-04-26 23:22:31 +00003296 * at the same time. That would be a lot of work, and contrary
3297 * to the KISS philosophy of this program. */
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003298 retcode = fclose(p);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003299 free_pipe_list(inner.list_head, 0);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003300 debug_printf("closed FILE from child, retcode=%d\n", retcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003301 return retcode;
3302}
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003303#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003304
3305static int parse_group(o_string *dest, struct p_context *ctx,
3306 struct in_str *input, int ch)
3307{
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003308 int rcode;
3309 const char *endch = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003310 struct p_context sub;
3311 struct child_prog *child = ctx->child;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003312
3313 debug_printf_parse("parse_group entered\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003314 if (child->argv) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003315 syntax(NULL);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003316 debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n");
3317 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003318 }
3319 initialize_context(&sub);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003320 endch = "}";
3321 if (ch == '(') {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003322 endch = ")";
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003323 child->subshell = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003324 }
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003325 rcode = parse_stream(dest, &sub, input, endch);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00003326//vda: err chk?
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003327 done_word(dest, &sub); /* finish off the final word in the subcontext */
Eric Andersen25f27032001-04-26 23:22:31 +00003328 done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
3329 child->group = sub.list_head;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003330
3331 debug_printf_parse("parse_group return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003332 return rcode;
3333 /* child remains "open", available for possible redirects */
3334}
3335
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003336/* Basically useful version until someone wants to get fancier,
Eric Andersen25f27032001-04-26 23:22:31 +00003337 * see the bash man page under "Parameter Expansion" */
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00003338static const char *lookup_param(const char *src)
Eric Andersen25f27032001-04-26 23:22:31 +00003339{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003340 struct variable *var = get_local_var(src);
3341 if (var)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003342 return strchr(var->varstr, '=') + 1;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003343 return NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003344}
3345
3346/* return code: 0 for OK, 1 for syntax error */
3347static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input)
3348{
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003349 int ch = b_peek(input); /* first character after the $ */
Denis Vlasenkoff097622007-10-01 10:00:45 +00003350 unsigned char quote_mask = dest->o_quote ? 0x80 : 0;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003351
3352 debug_printf_parse("handle_dollar entered: ch='%c'\n", ch);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003353 if (isalpha(ch)) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003354 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003355 //sp: ctx->child->sp++;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003356 while (1) {
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003357 debug_printf_parse(": '%c'\n", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003358 b_getch(input);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003359 b_addchr(dest, ch | quote_mask);
3360 quote_mask = 0;
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003361 ch = b_peek(input);
3362 if (!isalnum(ch) && ch != '_')
3363 break;
Eric Andersen25f27032001-04-26 23:22:31 +00003364 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003365 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003366 } else if (isdigit(ch)) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003367 make_one_char_var:
3368 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003369 //sp: ctx->child->sp++;
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003370 debug_printf_parse(": '%c'\n", ch);
3371 b_getch(input);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003372 b_addchr(dest, ch | quote_mask);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003373 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003374 } else switch (ch) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003375 case '$': /* pid */
3376 case '!': /* last bg pid */
3377 case '?': /* last exit code */
3378 case '#': /* number of args */
3379 case '*': /* args */
3380 case '@': /* args */
3381 goto make_one_char_var;
Eric Andersen25f27032001-04-26 23:22:31 +00003382 case '{':
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003383 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003384 //sp: ctx->child->sp++;
Eric Andersen25f27032001-04-26 23:22:31 +00003385 b_getch(input);
3386 /* XXX maybe someone will try to escape the '}' */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003387 while (1) {
3388 ch = b_getch(input);
Denis Vlasenkob0550012007-05-24 12:18:16 +00003389 if (ch == '}')
3390 break;
3391 if (!isalnum(ch) && ch != '_') {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003392 syntax("unterminated ${name}");
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003393 debug_printf_parse("handle_dollar return 1: unterminated ${name}\n");
3394 return 1;
3395 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003396 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003397 b_addchr(dest, ch | quote_mask);
3398 quote_mask = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003399 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003400 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003401 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003402#if ENABLE_HUSH_TICK
Eric Andersen25f27032001-04-26 23:22:31 +00003403 case '(':
Matt Kraai9f8caf12001-05-02 16:26:12 +00003404 b_getch(input);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003405 process_command_subs(dest, ctx, input, ")");
Eric Andersen25f27032001-04-26 23:22:31 +00003406 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003407#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003408 case '-':
3409 case '_':
3410 /* still unhandled, but should be eventually */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003411 bb_error_msg("unhandled syntax: $%c", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003412 return 1;
3413 break;
3414 default:
Denis Vlasenkoff097622007-10-01 10:00:45 +00003415 b_addqchr(dest, '$', dest->o_quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003416 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003417 debug_printf_parse("handle_dollar return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003418 return 0;
3419}
3420
Eric Andersen25f27032001-04-26 23:22:31 +00003421/* return code is 0 for normal exit, 1 for syntax error */
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003422static int parse_stream(o_string *dest, struct p_context *ctx,
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003423 struct in_str *input, const char *end_trigger)
Eric Andersen25f27032001-04-26 23:22:31 +00003424{
"Vladimir N. Oleynik"4ccd2b42006-01-31 09:27:48 +00003425 int ch, m;
Eric Andersen25f27032001-04-26 23:22:31 +00003426 int redir_fd;
3427 redir_type redir_style;
3428 int next;
3429
Denis Vlasenkoff097622007-10-01 10:00:45 +00003430 /* Only double-quote state is handled in the state variable dest->o_quote.
Eric Andersen25f27032001-04-26 23:22:31 +00003431 * A single-quote triggers a bypass of the main loop until its mate is
Denis Vlasenkoff097622007-10-01 10:00:45 +00003432 * found. When recursing, quote state is passed in via dest->o_quote. */
Eric Andersen25f27032001-04-26 23:22:31 +00003433
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003434 debug_printf_parse("parse_stream entered, end_trigger='%s'\n", end_trigger);
3435
Denis Vlasenko1a735862007-05-23 00:32:25 +00003436 while (1) {
Denis Vlasenko1a735862007-05-23 00:32:25 +00003437 m = CHAR_IFS;
3438 next = '\0';
Denis Vlasenko170435c2007-05-23 13:01:10 +00003439 ch = b_getch(input);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003440 if (ch != EOF) {
3441 m = charmap[ch];
3442 if (ch != '\n')
3443 next = b_peek(input);
3444 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003445 debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n",
Denis Vlasenkoff097622007-10-01 10:00:45 +00003446 ch, ch, m, dest->o_quote);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003447 if (m == CHAR_ORDINARY
Denis Vlasenkoff097622007-10-01 10:00:45 +00003448 || (m != CHAR_SPECIAL && dest->o_quote)
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003449 ) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00003450 if (ch == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003451 syntax("unterminated \"");
Denis Vlasenko170435c2007-05-23 13:01:10 +00003452 debug_printf_parse("parse_stream return 1: unterminated \"\n");
3453 return 1;
3454 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00003455 b_addqchr(dest, ch, dest->o_quote);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003456 continue;
3457 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003458 if (m == CHAR_IFS) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003459 if (done_word(dest, ctx)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003460 debug_printf_parse("parse_stream return 1: done_word!=0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003461 return 1;
Eric Andersenaac75e52001-04-30 18:18:45 +00003462 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003463 if (ch == EOF)
3464 break;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003465 /* If we aren't performing a substitution, treat
3466 * a newline as a command separator.
3467 * [why we don't handle it exactly like ';'? --vda] */
3468 if (end_trigger && ch == '\n') {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003469 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003470 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003471 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003472 if ((end_trigger && strchr(end_trigger, ch))
Denis Vlasenkoff097622007-10-01 10:00:45 +00003473 && !dest->o_quote && ctx->res_w == RES_NONE
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003474 ) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003475 debug_printf_parse("parse_stream return 0: end_trigger char found\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003476 return 0;
3477 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003478 if (m == CHAR_IFS)
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003479 continue;
3480 switch (ch) {
Eric Andersen25f27032001-04-26 23:22:31 +00003481 case '#':
Denis Vlasenkoff097622007-10-01 10:00:45 +00003482 if (dest->length == 0 && !dest->o_quote) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003483 while (1) {
3484 ch = b_peek(input);
3485 if (ch == EOF || ch == '\n')
3486 break;
3487 b_getch(input);
3488 }
Eric Andersen25f27032001-04-26 23:22:31 +00003489 } else {
Denis Vlasenkoff097622007-10-01 10:00:45 +00003490 b_addqchr(dest, ch, dest->o_quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003491 }
3492 break;
3493 case '\\':
3494 if (next == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003495 syntax("\\<eof>");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003496 debug_printf_parse("parse_stream return 1: \\<eof>\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003497 return 1;
3498 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00003499 b_addqchr(dest, '\\', dest->o_quote);
3500 b_addqchr(dest, b_getch(input), dest->o_quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003501 break;
3502 case '$':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003503 if (handle_dollar(dest, ctx, input) != 0) {
3504 debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n");
3505 return 1;
3506 }
Eric Andersen25f27032001-04-26 23:22:31 +00003507 break;
3508 case '\'':
3509 dest->nonnull = 1;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003510 while (1) {
3511 ch = b_getch(input);
3512 if (ch == EOF || ch == '\'')
3513 break;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003514 b_addchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003515 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003516 if (ch == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003517 syntax("unterminated '");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003518 debug_printf_parse("parse_stream return 1: unterminated '\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003519 return 1;
3520 }
3521 break;
3522 case '"':
3523 dest->nonnull = 1;
Denis Vlasenkoff097622007-10-01 10:00:45 +00003524 dest->o_quote ^= 1; /* invert */
Eric Andersen25f27032001-04-26 23:22:31 +00003525 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003526#if ENABLE_HUSH_TICK
Eric Andersen25f27032001-04-26 23:22:31 +00003527 case '`':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003528 process_command_subs(dest, ctx, input, "`");
Eric Andersen25f27032001-04-26 23:22:31 +00003529 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003530#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003531 case '>':
3532 redir_fd = redirect_opt_num(dest);
3533 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003534 redir_style = REDIRECT_OVERWRITE;
Eric Andersen25f27032001-04-26 23:22:31 +00003535 if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003536 redir_style = REDIRECT_APPEND;
Eric Andersen25f27032001-04-26 23:22:31 +00003537 b_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003538 }
3539#if 0
3540 else if (next == '(') {
3541 syntax(">(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003542 debug_printf_parse("parse_stream return 1: >(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003543 return 1;
3544 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003545#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003546 setup_redirect(ctx, redir_fd, redir_style, input);
3547 break;
3548 case '<':
3549 redir_fd = redirect_opt_num(dest);
3550 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003551 redir_style = REDIRECT_INPUT;
Eric Andersen25f27032001-04-26 23:22:31 +00003552 if (next == '<') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003553 redir_style = REDIRECT_HEREIS;
Eric Andersen25f27032001-04-26 23:22:31 +00003554 b_getch(input);
3555 } else if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003556 redir_style = REDIRECT_IO;
Eric Andersen25f27032001-04-26 23:22:31 +00003557 b_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003558 }
3559#if 0
3560 else if (next == '(') {
3561 syntax("<(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003562 debug_printf_parse("parse_stream return 1: <(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003563 return 1;
3564 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003565#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003566 setup_redirect(ctx, redir_fd, redir_style, input);
3567 break;
3568 case ';':
3569 done_word(dest, ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003570 done_pipe(ctx, PIPE_SEQ);
Eric Andersen25f27032001-04-26 23:22:31 +00003571 break;
3572 case '&':
3573 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003574 if (next == '&') {
Eric Andersen25f27032001-04-26 23:22:31 +00003575 b_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003576 done_pipe(ctx, PIPE_AND);
Eric Andersen25f27032001-04-26 23:22:31 +00003577 } else {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003578 done_pipe(ctx, PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00003579 }
3580 break;
3581 case '|':
3582 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003583 if (next == '|') {
Eric Andersen25f27032001-04-26 23:22:31 +00003584 b_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003585 done_pipe(ctx, PIPE_OR);
Eric Andersen25f27032001-04-26 23:22:31 +00003586 } else {
3587 /* we could pick up a file descriptor choice here
3588 * with redirect_opt_num(), but bash doesn't do it.
3589 * "echo foo 2| cat" yields "foo 2". */
3590 done_command(ctx);
3591 }
3592 break;
3593 case '(':
3594 case '{':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003595 if (parse_group(dest, ctx, input, ch) != 0) {
3596 debug_printf_parse("parse_stream return 1: parse_group returned non-0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003597 return 1;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003598 }
Eric Andersen25f27032001-04-26 23:22:31 +00003599 break;
3600 case ')':
3601 case '}':
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003602 syntax("unexpected }"); /* Proper use of this character is caught by end_trigger */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003603 debug_printf_parse("parse_stream return 1: unexpected '}'\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003604 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003605 default:
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003606 if (ENABLE_HUSH_DEBUG)
3607 bb_error_msg_and_die("BUG: unexpected %c\n", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003608 }
3609 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003610 /* Complain if quote? No, maybe we just finished a command substitution
Eric Andersen25f27032001-04-26 23:22:31 +00003611 * that was quoted. Example:
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003612 * $ echo "`cat foo` plus more"
Eric Andersen25f27032001-04-26 23:22:31 +00003613 * and we just got the EOF generated by the subshell that ran "cat foo"
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003614 * The only real complaint is if we got an EOF when end_trigger != NULL,
Eric Andersen25f27032001-04-26 23:22:31 +00003615 * that is, we were really supposed to get end_trigger, and never got
3616 * one before the EOF. Can't use the standard "syntax error" return code,
3617 * so that parse_stream_outer can distinguish the EOF and exit smoothly. */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003618 debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL));
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003619 if (end_trigger)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003620 return -1;
Eric Andersen25f27032001-04-26 23:22:31 +00003621 return 0;
3622}
3623
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003624static void set_in_charmap(const char *set, int code)
Eric Andersen25f27032001-04-26 23:22:31 +00003625{
Denis Vlasenkof5294e12007-04-14 10:09:57 +00003626 while (*set)
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003627 charmap[(unsigned char)*set++] = code;
Eric Andersen25f27032001-04-26 23:22:31 +00003628}
3629
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003630static void update_charmap(void)
Eric Andersen25f27032001-04-26 23:22:31 +00003631{
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003632 /* char *ifs and char charmap[256] are both globals. */
Eric Andersen25f27032001-04-26 23:22:31 +00003633 ifs = getenv("IFS");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003634 if (ifs == NULL)
3635 ifs = " \t\n";
Eric Andersen25f27032001-04-26 23:22:31 +00003636 /* Precompute a list of 'flow through' behavior so it can be treated
3637 * quickly up front. Computation is necessary because of IFS.
3638 * Special case handling of IFS == " \t\n" is not implemented.
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003639 * The charmap[] array only really needs two bits each,
3640 * and on most machines that would be faster (reduced L1 cache use).
Eric Andersen25f27032001-04-26 23:22:31 +00003641 */
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003642 memset(charmap, CHAR_ORDINARY, sizeof(charmap));
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003643#if ENABLE_HUSH_TICK
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003644 set_in_charmap("\\$\"`", CHAR_SPECIAL);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003645#else
3646 set_in_charmap("\\$\"", CHAR_SPECIAL);
3647#endif
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003648 set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003649 set_in_charmap(ifs, CHAR_IFS); /* are ordinary if quoted */
Eric Andersen25f27032001-04-26 23:22:31 +00003650}
3651
Eric Andersenaff114c2004-04-14 17:51:38 +00003652/* most recursion does not come through here, the exception is
Denis Vlasenko170435c2007-05-23 13:01:10 +00003653 * from builtin_source() and builtin_eval() */
3654static int parse_and_run_stream(struct in_str *inp, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003655{
Eric Andersen25f27032001-04-26 23:22:31 +00003656 struct p_context ctx;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003657 o_string temp = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003658 int rcode;
3659 do {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003660 ctx.parse_type = parse_flag;
Eric Andersen25f27032001-04-26 23:22:31 +00003661 initialize_context(&ctx);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003662 update_charmap();
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003663 if (!(parse_flag & PARSEFLAG_SEMICOLON) || (parse_flag & PARSEFLAG_REPARSING))
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003664 set_in_charmap(";$&|", CHAR_ORDINARY);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003665#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00003666 inp->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003667#endif
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003668 /* We will stop & execute after each ';' or '\n'.
3669 * Example: "sleep 9999; echo TEST" + ctrl-C:
3670 * TEST should be printed */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003671 rcode = parse_stream(&temp, &ctx, inp, ";\n");
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003672 if (rcode != 1 && ctx.old_flag != 0) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003673 syntax(NULL);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003674 }
3675 if (rcode != 1 && ctx.old_flag == 0) {
3676 done_word(&temp, &ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003677 done_pipe(&ctx, PIPE_SEQ);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00003678 debug_print_tree(ctx.list_head, 0);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003679 debug_printf_exec("parse_stream_outer: run_list\n");
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003680 run_list(ctx.list_head);
3681 } else {
3682 if (ctx.old_flag != 0) {
3683 free(ctx.stack);
3684 b_reset(&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003685 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003686 temp.nonnull = 0;
Denis Vlasenkoff097622007-10-01 10:00:45 +00003687 temp.o_quote = 0;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003688 inp->p = NULL;
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003689 free_pipe_list(ctx.list_head, 0);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003690 }
Eric Andersena813afc2001-05-24 16:19:36 +00003691 b_free(&temp);
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003692 } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP)); /* loop on syntax errors, return on EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003693 return 0;
3694}
3695
Denis Vlasenko170435c2007-05-23 13:01:10 +00003696static int parse_and_run_string(const char *s, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003697{
3698 struct in_str input;
3699 setup_string_in_str(&input, s);
Denis Vlasenko170435c2007-05-23 13:01:10 +00003700 return parse_and_run_stream(&input, parse_flag);
Eric Andersen25f27032001-04-26 23:22:31 +00003701}
3702
Denis Vlasenko170435c2007-05-23 13:01:10 +00003703static int parse_and_run_file(FILE *f)
Eric Andersen25f27032001-04-26 23:22:31 +00003704{
3705 int rcode;
3706 struct in_str input;
3707 setup_file_in_str(&input, f);
Denis Vlasenko170435c2007-05-23 13:01:10 +00003708 rcode = parse_and_run_stream(&input, PARSEFLAG_SEMICOLON);
Eric Andersen25f27032001-04-26 23:22:31 +00003709 return rcode;
3710}
3711
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003712#if ENABLE_HUSH_JOB
Eric Andersen6c947d22001-06-25 22:24:38 +00003713/* Make sure we have a controlling tty. If we get started under a job
3714 * aware app (like bash for example), make sure we are now in charge so
3715 * we don't fight over who gets the foreground */
Eric Anderseneaecbf32001-10-31 10:41:31 +00003716static void setup_job_control(void)
Eric Andersen52a97ca2001-06-22 06:49:26 +00003717{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003718 pid_t shell_pgrp;
3719
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003720 saved_task_pgrp = shell_pgrp = getpgrp();
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003721 debug_printf_jobs("saved_task_pgrp=%d\n", saved_task_pgrp);
Denis Vlasenko96e1b382007-09-30 23:50:48 +00003722 close_on_exec_on(interactive_fd);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003723
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003724 /* If we were ran as 'hush &',
3725 * sleep until we are in the foreground. */
3726 while (tcgetpgrp(interactive_fd) != shell_pgrp) {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003727 /* Send TTIN to ourself (should stop us) */
Denis Vlasenkoff131b92007-04-10 15:42:06 +00003728 kill(- shell_pgrp, SIGTTIN);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003729 shell_pgrp = getpgrp();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003730 }
Eric Andersen52a97ca2001-06-22 06:49:26 +00003731
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003732 /* Ignore job-control and misc signals. */
3733 set_jobctrl_sighandler(SIG_IGN);
3734 set_misc_sighandler(SIG_IGN);
3735//huh? signal(SIGCHLD, SIG_IGN);
3736
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003737 /* We _must_ restore tty pgrp on fatal signals */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003738 set_fatal_sighandler(sigexit);
Eric Andersen6c947d22001-06-25 22:24:38 +00003739
3740 /* Put ourselves in our own process group. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003741 setpgrp(); /* is the same as setpgid(our_pid, our_pid); */
Eric Andersen6c947d22001-06-25 22:24:38 +00003742 /* Grab control of the terminal. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003743 tcsetpgrp(interactive_fd, getpid());
Eric Andersen6c947d22001-06-25 22:24:38 +00003744}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003745#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00003746
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00003747int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Matt Kraai2d91deb2001-08-01 17:21:35 +00003748int hush_main(int argc, char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00003749{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00003750 static const char version_str[] ALIGN1 = "HUSH_VERSION="HUSH_VER_STR;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003751 static const struct variable const_shell_ver = {
3752 .next = NULL,
3753 .varstr = (char*)version_str,
3754 .max_len = 1, /* 0 can provoke free(name) */
3755 .flg_export = 1,
3756 .flg_read_only = 1,
3757 };
3758
Eric Andersen25f27032001-04-26 23:22:31 +00003759 int opt;
3760 FILE *input;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003761 char **e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003762 struct variable *cur_var;
Eric Andersenbc604a22001-05-16 05:24:03 +00003763
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003764 PTR_TO_GLOBALS = xzalloc(sizeof(G));
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003765
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003766 /* Deal with HUSH_VERSION */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003767 shell_ver = const_shell_ver; /* copying struct here */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003768 top_var = &shell_ver;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003769 unsetenv("HUSH_VERSION"); /* in case it exists in initial env */
3770 /* Initialize our shell local variables with the values
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003771 * currently living in the environment */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003772 cur_var = top_var;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003773 e = environ;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003774 if (e) while (*e) {
3775 char *value = strchr(*e, '=');
3776 if (value) { /* paranoia */
3777 cur_var->next = xzalloc(sizeof(*cur_var));
3778 cur_var = cur_var->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003779 cur_var->varstr = *e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003780 cur_var->max_len = strlen(*e);
3781 cur_var->flg_export = 1;
3782 }
3783 e++;
3784 }
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003785 putenv((char *)version_str); /* reinstate HUSH_VERSION */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003786
Denis Vlasenko38f63192007-01-22 09:03:07 +00003787#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003788 line_input_state = new_line_input_t(FOR_SHELL);
3789#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003790 /* XXX what should these be while sourcing /etc/profile? */
3791 global_argc = argc;
3792 global_argv = argv;
Eric Andersen94ac2442001-05-22 19:05:18 +00003793 /* Initialize some more globals to non-zero values */
3794 set_cwd();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003795#if ENABLE_HUSH_INTERACTIVE
3796#if ENABLE_FEATURE_EDITING
3797 cmdedit_set_initial_prompt();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003798#endif
Eric Andersen94ac2442001-05-22 19:05:18 +00003799 PS2 = "> ";
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003800#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003801
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003802 if (EXIT_SUCCESS) /* otherwise is already done */
3803 last_return_code = EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00003804
3805 if (argv[0] && argv[0][0] == '-') {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003806 debug_printf("sourcing /etc/profile\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003807 input = fopen("/etc/profile", "r");
3808 if (input != NULL) {
Denis Vlasenkoa0898172007-10-01 09:59:01 +00003809 close_on_exec_on(fileno(input));
Denis Vlasenko170435c2007-05-23 13:01:10 +00003810 parse_and_run_file(input);
Eric Andersena90f20b2001-06-26 23:00:21 +00003811 fclose(input);
3812 }
Eric Andersen25f27032001-04-26 23:22:31 +00003813 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003814 input = stdin;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003815
Eric Andersen25f27032001-04-26 23:22:31 +00003816 while ((opt = getopt(argc, argv, "c:xif")) > 0) {
3817 switch (opt) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003818 case 'c':
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003819 global_argv = argv + optind;
3820 global_argc = argc - optind;
Denis Vlasenko170435c2007-05-23 13:01:10 +00003821 opt = parse_and_run_string(optarg, PARSEFLAG_SEMICOLON);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003822 goto final_return;
3823 case 'i':
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003824 /* Well, we cannot just declare interactiveness,
3825 * we have to have some stuff (ctty, etc) */
3826 /* interactive_fd++; */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003827 break;
3828 case 'f':
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003829 fake_mode = 1;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003830 break;
3831 default:
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003832#ifndef BB_VER
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003833 fprintf(stderr, "Usage: sh [FILE]...\n"
3834 " or: sh -c command [args]...\n\n");
3835 exit(EXIT_FAILURE);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003836#else
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003837 bb_show_usage();
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003838#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003839 }
3840 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003841#if ENABLE_HUSH_JOB
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003842 /* A shell is interactive if the '-i' flag was given, or if all of
Eric Andersen25f27032001-04-26 23:22:31 +00003843 * the following conditions are met:
Denis Vlasenko55b2de72007-04-18 17:21:28 +00003844 * no -c command
Eric Andersen25f27032001-04-26 23:22:31 +00003845 * no arguments remaining or the -s flag given
3846 * standard input is a terminal
3847 * standard output is a terminal
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003848 * Refer to Posix.2, the description of the 'sh' utility. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003849 if (argv[optind] == NULL && input == stdin
3850 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
3851 ) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003852 saved_tty_pgrp = tcgetpgrp(STDIN_FILENO);
3853 debug_printf("saved_tty_pgrp=%d\n", saved_tty_pgrp);
3854 if (saved_tty_pgrp >= 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003855 /* try to dup to high fd#, >= 255 */
3856 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
3857 if (interactive_fd < 0) {
3858 /* try to dup to any fd */
3859 interactive_fd = dup(STDIN_FILENO);
3860 if (interactive_fd < 0)
3861 /* give up */
3862 interactive_fd = 0;
3863 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003864 // TODO: track & disallow any attempts of user
3865 // to (inadvertently) close/redirect it
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003866 }
Eric Andersen25f27032001-04-26 23:22:31 +00003867 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003868 debug_printf("interactive_fd=%d\n", interactive_fd);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003869 if (interactive_fd) {
Eric Andersen25f27032001-04-26 23:22:31 +00003870 /* Looks like they want an interactive shell */
Eric Andersen52a97ca2001-06-22 06:49:26 +00003871 setup_job_control();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003872 /* Make xfuncs do cleanup on exit */
3873 die_sleep = -1; /* flag */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003874// FIXME: should we reset die_sleep = 0 whereever we fork?
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003875 if (setjmp(die_jmp)) {
3876 /* xfunc has failed! die die die */
3877 hush_exit(xfunc_error_retval);
3878 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003879#if !ENABLE_FEATURE_SH_EXTRA_QUIET
Denis Vlasenkoca525b42007-06-13 12:27:17 +00003880 printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", bb_banner);
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003881 printf("Enter 'help' for a list of built-in commands.\n\n");
3882#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00003883 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003884#elif ENABLE_HUSH_INTERACTIVE
3885/* no job control compiled, only prompt/line editing */
3886 if (argv[optind] == NULL && input == stdin
3887 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
3888 ) {
3889 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
3890 if (interactive_fd < 0) {
3891 /* try to dup to any fd */
3892 interactive_fd = dup(STDIN_FILENO);
3893 if (interactive_fd < 0)
3894 /* give up */
3895 interactive_fd = 0;
3896 }
3897 }
3898
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003899#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003900
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003901 if (argv[optind] == NULL) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00003902 opt = parse_and_run_file(stdin);
Eric Andersene67c3ce2001-05-02 02:09:36 +00003903 goto final_return;
Eric Andersen25f27032001-04-26 23:22:31 +00003904 }
Eric Andersen25f27032001-04-26 23:22:31 +00003905
3906 debug_printf("\nrunning script '%s'\n", argv[optind]);
Denis Vlasenko4c978632007-02-03 03:31:13 +00003907 global_argv = argv + optind;
3908 global_argc = argc - optind;
Rob Landleyd921b2e2006-08-03 15:41:12 +00003909 input = xfopen(argv[optind], "r");
Denis Vlasenko170435c2007-05-23 13:01:10 +00003910 opt = parse_and_run_file(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003911
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003912 final_return:
3913
Denis Vlasenko38f63192007-01-22 09:03:07 +00003914#if ENABLE_FEATURE_CLEAN_UP
Eric Andersenaeb44c42001-05-22 20:29:00 +00003915 fclose(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003916 if (cwd != bb_msg_unknown)
Eric Andersenaeb44c42001-05-22 20:29:00 +00003917 free((char*)cwd);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003918 cur_var = top_var->next;
3919 while (cur_var) {
3920 struct variable *tmp = cur_var;
3921 if (!cur_var->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003922 free(cur_var->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003923 cur_var = cur_var->next;
3924 free(tmp);
Eric Andersenaeb44c42001-05-22 20:29:00 +00003925 }
Eric Andersen25f27032001-04-26 23:22:31 +00003926#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003927 hush_exit(opt ? opt : last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +00003928}
Denis Vlasenko96702ca2007-11-23 23:28:55 +00003929
3930
3931#if ENABLE_LASH
3932int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
3933int lash_main(int argc, char **argv)
3934{
3935 //bb_error_msg("lash is deprecated, please use hush instead");
3936 return hush_main(argc, argv);
3937}
3938#endif