blob: 2d5697269da70fce4ba3c3b6b79dc4d2fa2e585a [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 Vlasenko1aa7e472007-11-28 06:49:03 +000084#include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000085
Denis Vlasenkod01ff132007-05-02 21:40:23 +000086
Denis Vlasenko05743d72008-02-10 12:10:08 +000087#if !BB_MMU && ENABLE_HUSH_TICK
88//#undef ENABLE_HUSH_TICK
89//#define ENABLE_HUSH_TICK 0
90#warning On NOMMU, hush command substitution is dangerous.
91#warning Dont use it for commands which produce lots of output.
92#warning For more info see shell/hush.c, generate_stream_from_list().
93#endif
94
95#if !BB_MMU && ENABLE_HUSH_JOB
96#undef ENABLE_HUSH_JOB
97#define ENABLE_HUSH_JOB 0
98#endif
99
100#if !ENABLE_HUSH_INTERACTIVE
101#undef ENABLE_FEATURE_EDITING
102#define ENABLE_FEATURE_EDITING 0
103#undef ENABLE_FEATURE_EDITING_FANCY_PROMPT
104#define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0
Denis Vlasenko8412d792007-10-01 09:59:47 +0000105#endif
106
107
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000108/* If you comment out one of these below, it will be #defined later
109 * to perform debug printfs to stderr: */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000110#define debug_printf(...) do {} while (0)
Denis Vlasenko400c5b62007-05-04 13:07:27 +0000111/* Finer-grained debug switches */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000112#define debug_printf_parse(...) do {} while (0)
113#define debug_print_tree(a, b) do {} while (0)
114#define debug_printf_exec(...) do {} while (0)
115#define debug_printf_jobs(...) do {} while (0)
116#define debug_printf_expand(...) do {} while (0)
117#define debug_printf_clean(...) do {} while (0)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000118
119#ifndef debug_printf
120#define debug_printf(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000121#endif
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000122
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000123#ifndef debug_printf_parse
124#define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000125#endif
126
127#ifndef debug_printf_exec
128#define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__)
129#endif
130
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000131#ifndef debug_printf_jobs
132#define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__)
133#define DEBUG_SHELL_JOBS 1
134#endif
135
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000136#ifndef debug_printf_expand
137#define debug_printf_expand(...) fprintf(stderr, __VA_ARGS__)
138#define DEBUG_EXPAND 1
139#endif
140
Denis Vlasenko170435c2007-05-23 13:01:10 +0000141/* Keep unconditionally on for now */
142#define ENABLE_HUSH_DEBUG 1
143
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000144#ifndef debug_printf_clean
145/* broken, of course, but OK for testing */
146static const char *indenter(int i)
147{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000148 static const char blanks[] ALIGN1 =
149 " ";
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +0000150 return &blanks[sizeof(blanks) - i - 1];
151}
152#define debug_printf_clean(...) fprintf(stderr, __VA_ARGS__)
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000153#define DEBUG_CLEAN 1
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000154#endif
155
Eric Andersen25f27032001-04-26 23:22:31 +0000156
Denis Vlasenkof962a032007-11-23 12:50:54 +0000157/*
158 * Leak hunting. Use hush_leaktool.sh for post-processing.
159 */
160#ifdef FOR_HUSH_LEAKTOOL
161void *xxmalloc(int lineno, size_t size)
162{
163 void *ptr = xmalloc((size + 0xff) & ~0xff);
164 fprintf(stderr, "line %d: malloc %p\n", lineno, ptr);
165 return ptr;
166}
167void *xxrealloc(int lineno, void *ptr, size_t size)
168{
169 ptr = xrealloc(ptr, (size + 0xff) & ~0xff);
170 fprintf(stderr, "line %d: realloc %p\n", lineno, ptr);
171 return ptr;
172}
173char *xxstrdup(int lineno, const char *str)
174{
175 char *ptr = xstrdup(str);
176 fprintf(stderr, "line %d: strdup %p\n", lineno, ptr);
177 return ptr;
178}
179void xxfree(void *ptr)
180{
181 fprintf(stderr, "free %p\n", ptr);
182 free(ptr);
183}
184#define xmalloc(s) xxmalloc(__LINE__, s)
185#define xrealloc(p, s) xxrealloc(__LINE__, p, s)
186#define xstrdup(s) xxstrdup(__LINE__, s)
187#define free(p) xxfree(p)
188#endif
189
190
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +0000191#define SPECIAL_VAR_SYMBOL 3
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000192
193#define PARSEFLAG_EXIT_FROM_LOOP 1
194#define PARSEFLAG_SEMICOLON (1 << 1) /* symbol ';' is special for parser */
195#define PARSEFLAG_REPARSING (1 << 2) /* >= 2nd pass */
Eric Andersen25f27032001-04-26 23:22:31 +0000196
197typedef enum {
198 REDIRECT_INPUT = 1,
199 REDIRECT_OVERWRITE = 2,
200 REDIRECT_APPEND = 3,
201 REDIRECT_HEREIS = 4,
202 REDIRECT_IO = 5
203} redir_type;
204
205/* The descrip member of this structure is only used to make debugging
206 * output pretty */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000207static const struct {
208 int mode;
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000209 signed char default_fd;
210 char descrip[3];
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000211} redir_table[] = {
Eric Andersen25f27032001-04-26 23:22:31 +0000212 { 0, 0, "()" },
213 { O_RDONLY, 0, "<" },
214 { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
215 { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" },
216 { O_RDONLY, -1, "<<" },
217 { O_RDWR, 1, "<>" }
218};
219
220typedef enum {
221 PIPE_SEQ = 1,
222 PIPE_AND = 2,
223 PIPE_OR = 3,
224 PIPE_BG = 4,
225} pipe_style;
226
227/* might eventually control execution */
228typedef enum {
229 RES_NONE = 0,
Denis Vlasenko06810332007-05-21 23:30:54 +0000230#if ENABLE_HUSH_IF
Eric Andersen25f27032001-04-26 23:22:31 +0000231 RES_IF = 1,
232 RES_THEN = 2,
233 RES_ELIF = 3,
234 RES_ELSE = 4,
235 RES_FI = 5,
Denis Vlasenko06810332007-05-21 23:30:54 +0000236#endif
237#if ENABLE_HUSH_LOOPS
Eric Andersen25f27032001-04-26 23:22:31 +0000238 RES_FOR = 6,
239 RES_WHILE = 7,
240 RES_UNTIL = 8,
241 RES_DO = 9,
242 RES_DONE = 10,
Denis Vlasenko06810332007-05-21 23:30:54 +0000243 RES_IN = 11,
244#endif
245 RES_XXXX = 12,
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000246 RES_SNTX = 13
Eric Andersen25f27032001-04-26 23:22:31 +0000247} reserved_style;
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000248enum {
249 FLAG_END = (1 << RES_NONE ),
Denis Vlasenko06810332007-05-21 23:30:54 +0000250#if ENABLE_HUSH_IF
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000251 FLAG_IF = (1 << RES_IF ),
252 FLAG_THEN = (1 << RES_THEN ),
253 FLAG_ELIF = (1 << RES_ELIF ),
254 FLAG_ELSE = (1 << RES_ELSE ),
255 FLAG_FI = (1 << RES_FI ),
Denis Vlasenko06810332007-05-21 23:30:54 +0000256#endif
257#if ENABLE_HUSH_LOOPS
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000258 FLAG_FOR = (1 << RES_FOR ),
259 FLAG_WHILE = (1 << RES_WHILE),
260 FLAG_UNTIL = (1 << RES_UNTIL),
261 FLAG_DO = (1 << RES_DO ),
262 FLAG_DONE = (1 << RES_DONE ),
263 FLAG_IN = (1 << RES_IN ),
Denis Vlasenko06810332007-05-21 23:30:54 +0000264#endif
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000265 FLAG_START = (1 << RES_XXXX ),
266};
Eric Andersen25f27032001-04-26 23:22:31 +0000267
268/* This holds pointers to the various results of parsing */
269struct p_context {
270 struct child_prog *child;
271 struct pipe *list_head;
272 struct pipe *pipe;
273 struct redir_struct *pending_redirect;
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000274 smallint res_w;
275 smallint parse_type; /* bitmask of PARSEFLAG_xxx, defines type of parser : ";$" common or special symbol */
276 int old_flag; /* bitmask of FLAG_xxx, for figuring out valid reserved words */
Eric Andersen25f27032001-04-26 23:22:31 +0000277 struct p_context *stack;
278 /* How about quoting status? */
279};
280
281struct redir_struct {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000282 struct redir_struct *next; /* pointer to the next redirect in the list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000283 redir_type type; /* type of redirection */
284 int fd; /* file descriptor being redirected */
285 int dup; /* -1, or file descriptor being duplicated */
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000286 char **glob_word; /* *word.gl_pathv is the filename */
Eric Andersen25f27032001-04-26 23:22:31 +0000287};
288
289struct child_prog {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000290 pid_t pid; /* 0 if exited */
291 char **argv; /* program name and arguments */
292 struct pipe *group; /* if non-NULL, first in group or subshell */
Denis Vlasenko262d7652007-05-20 21:52:49 +0000293 smallint subshell; /* flag, non-zero if group must be forked */
294 smallint is_stopped; /* is the program currently running? */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000295 struct redir_struct *redirects; /* I/O redirections */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000296 struct pipe *family; /* pointer back to the child's parent pipe */
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000297 //sp counting seems to be broken... so commented out, grep for '//sp:'
298 //sp: int sp; /* number of SPECIAL_VAR_SYMBOL */
Denis Vlasenko262d7652007-05-20 21:52:49 +0000299 //seems to be unused, grep for '//pt:'
300 //pt: int parse_type;
Eric Andersen25f27032001-04-26 23:22:31 +0000301};
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +0000302/* argv vector may contain variable references (^Cvar^C, ^C0^C etc)
303 * and on execution these are substituted with their values.
304 * Substitution can make _several_ words out of one argv[n]!
305 * Example: argv[0]=='.^C*^C.' here: echo .$*.
306 */
Eric Andersen25f27032001-04-26 23:22:31 +0000307
308struct pipe {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000309 struct pipe *next;
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000310 int num_progs; /* total number of programs in job */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000311 int running_progs; /* number of programs running (not exited) */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000312 int stopped_progs; /* number of programs alive, but stopped */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000313#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000314 int jobid; /* job number */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000315 pid_t pgrp; /* process group ID for the job */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000316 char *cmdtext; /* name of job */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000317#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000318 char *cmdbuf; /* buffer various argv's point into */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000319 struct child_prog *progs; /* array of commands in pipe */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000320 int job_context; /* bitmask defining current context */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000321 smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
322 smallint res_word; /* needed for if, for, while, until... */
Eric Andersen25f27032001-04-26 23:22:31 +0000323};
324
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000325/* On program start, environ points to initial environment.
326 * putenv adds new pointers into it, unsetenv removes them.
327 * Neither of these (de)allocates the strings.
328 * setenv allocates new strings in malloc space and does putenv,
329 * and thus setenv is unusable (leaky) for shell's purposes */
330#define setenv(...) setenv_is_leaky_dont_use()
331struct variable {
332 struct variable *next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000333 char *varstr; /* points to "name=" portion */
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000334 int max_len; /* if > 0, name is part of initial env; else name is malloced */
335 smallint flg_export; /* putenv should be done on this var */
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000336 smallint flg_read_only;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000337};
338
Eric Andersen25f27032001-04-26 23:22:31 +0000339typedef struct {
340 char *data;
341 int length;
342 int maxlen;
Denis Vlasenkoff097622007-10-01 10:00:45 +0000343 smallint o_quote;
344 smallint nonnull;
Eric Andersen25f27032001-04-26 23:22:31 +0000345} o_string;
346#define NULL_O_STRING {NULL,0,0,0,0}
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000347/* used for initialization: o_string foo = NULL_O_STRING; */
Eric Andersen25f27032001-04-26 23:22:31 +0000348
349/* I can almost use ordinary FILE *. Is open_memstream() universally
350 * available? Where is it documented? */
351struct in_str {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +0000352 const char *p;
353 /* eof_flag=1: last char in ->p is really an EOF */
354 char eof_flag; /* meaningless if ->p == NULL */
355 char peek_buf[2];
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000356#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000357 smallint promptme;
358 smallint promptmode; /* 0: PS1, 1: PS2 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000359#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000360 FILE *file;
361 int (*get) (struct in_str *);
362 int (*peek) (struct in_str *);
363};
364#define b_getch(input) ((input)->get(input))
365#define b_peek(input) ((input)->peek(input))
366
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000367enum {
368 CHAR_ORDINARY = 0,
369 CHAR_ORDINARY_IF_QUOTED = 1, /* example: *, # */
370 CHAR_IFS = 2, /* treated as ordinary if quoted */
371 CHAR_SPECIAL = 3, /* example: $ */
Eric Andersen25f27032001-04-26 23:22:31 +0000372};
373
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000374#define HUSH_VER_STR "0.02"
375
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000376/* "Globals" within this file */
377
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000378/* Sorted roughly by size (smaller offsets == smaller code) */
379struct globals {
380#if ENABLE_HUSH_INTERACTIVE
381 /* 'interactive_fd' is a fd# open to ctty, if we have one
382 * _AND_ if we decided to act interactively */
383 int interactive_fd;
384 const char *PS1;
385 const char *PS2;
386#endif
387#if ENABLE_FEATURE_EDITING
388 line_input_t *line_input_state;
389#endif
390#if ENABLE_HUSH_JOB
391 int run_list_level;
392 pid_t saved_task_pgrp;
393 pid_t saved_tty_pgrp;
394 int last_jobid;
395 struct pipe *job_list;
396 struct pipe *toplevel_list;
397 smallint ctrl_z_flag;
398#endif
399 smallint fake_mode;
400 /* these three support $?, $#, and $1 */
401 char **global_argv;
402 int global_argc;
403 int last_return_code;
404 const char *ifs;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000405 const char *cwd;
406 unsigned last_bg_pid;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +0000407 struct variable *top_var; /* = &shell_ver (set in main()) */
408 struct variable shell_ver;
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000409#if ENABLE_FEATURE_SH_STANDALONE
410 struct nofork_save_area nofork_save;
411#endif
412#if ENABLE_HUSH_JOB
413 sigjmp_buf toplevel_jb;
414#endif
415 unsigned char charmap[256];
416 char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2];
417};
418
419#define G (*ptr_to_globals)
420
421#if !ENABLE_HUSH_INTERACTIVE
422enum { interactive_fd = 0 };
423#endif
424#if !ENABLE_HUSH_JOB
425enum { run_list_level = 0 };
426#endif
427
428#if ENABLE_HUSH_INTERACTIVE
429#define interactive_fd (G.interactive_fd )
430#define PS1 (G.PS1 )
431#define PS2 (G.PS2 )
432#endif
433#if ENABLE_FEATURE_EDITING
434#define line_input_state (G.line_input_state)
435#endif
436#if ENABLE_HUSH_JOB
437#define run_list_level (G.run_list_level )
438#define saved_task_pgrp (G.saved_task_pgrp )
439#define saved_tty_pgrp (G.saved_tty_pgrp )
440#define last_jobid (G.last_jobid )
441#define job_list (G.job_list )
442#define toplevel_list (G.toplevel_list )
443#define toplevel_jb (G.toplevel_jb )
444#define ctrl_z_flag (G.ctrl_z_flag )
445#endif /* JOB */
446#define global_argv (G.global_argv )
447#define global_argc (G.global_argc )
448#define last_return_code (G.last_return_code)
449#define ifs (G.ifs )
450#define fake_mode (G.fake_mode )
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000451#define cwd (G.cwd )
452#define last_bg_pid (G.last_bg_pid )
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000453#define top_var (G.top_var )
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000454#define shell_ver (G.shell_ver )
455#if ENABLE_FEATURE_SH_STANDALONE
456#define nofork_save (G.nofork_save )
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000457#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000458#if ENABLE_HUSH_JOB
459#define toplevel_jb (G.toplevel_jb )
460#endif
461#define charmap (G.charmap )
462#define user_input_buf (G.user_input_buf )
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000463#define INIT_G() do { \
464 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
465} while (0)
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000466
467
468#define B_CHUNK 100
469#define B_NOSPAC 1
470#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
471
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000472#if 1
473/* Normal */
474static void syntax(const char *msg)
475{
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000476 /* Was using fancy stuff:
477 * (interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...)
478 * but it SEGVs. ?! Oh well... explicit temp ptr works around that */
479 void (*fp)(const char *s, ...);
480
481 fp = (interactive_fd ? bb_error_msg : bb_error_msg_and_die);
482 fp(msg ? "%s: %s" : "syntax error", "syntax error", msg);
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000483}
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000484
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000485#else
486/* Debug */
487static void syntax_lineno(int line)
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000488{
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000489 void (*fp)(const char *s, ...);
490
491 fp = (interactive_fd ? bb_error_msg : bb_error_msg_and_die);
492 fp("syntax error hush.c:%d", line);
Eric Andersen25f27032001-04-26 23:22:31 +0000493}
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000494#define syntax(str) syntax_lineno(__LINE__)
495#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000496
497/* Index of subroutines: */
Eric Andersen25f27032001-04-26 23:22:31 +0000498/* o_string manipulation: */
499static int b_check_space(o_string *o, int len);
500static int b_addchr(o_string *o, int ch);
501static void b_reset(o_string *o);
502static int b_addqchr(o_string *o, int ch, int quote);
Eric Andersen25f27032001-04-26 23:22:31 +0000503/* in_str manipulations: */
504static int static_get(struct in_str *i);
505static int static_peek(struct in_str *i);
506static int file_get(struct in_str *i);
507static int file_peek(struct in_str *i);
508static void setup_file_in_str(struct in_str *i, FILE *f);
509static void setup_string_in_str(struct in_str *i, const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000510/* "run" the final data structures: */
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000511#if !defined(DEBUG_CLEAN)
512#define free_pipe_list(head, indent) free_pipe_list(head)
513#define free_pipe(pi, indent) free_pipe(pi)
514#endif
Eric Andersenbf7df042001-05-23 22:18:35 +0000515static int free_pipe_list(struct pipe *head, int indent);
516static int free_pipe(struct pipe *pi, int indent);
Eric Andersen25f27032001-04-26 23:22:31 +0000517/* really run the final data structures: */
518static int setup_redirects(struct child_prog *prog, int squirrel[]);
Denis Vlasenko05743d72008-02-10 12:10:08 +0000519static int run_list(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000520static void pseudo_exec_argv(char **argv) ATTRIBUTE_NORETURN;
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000521static void pseudo_exec(struct child_prog *child) ATTRIBUTE_NORETURN;
Denis Vlasenko05743d72008-02-10 12:10:08 +0000522static int run_pipe(struct pipe *pi);
Eric Andersen25f27032001-04-26 23:22:31 +0000523/* extended glob support: */
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000524static char **globhack(const char *src, char **strings);
Eric Andersen25f27032001-04-26 23:22:31 +0000525static int glob_needed(const char *s);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000526static int xglob(o_string *dest, char ***pglob);
Eric Andersen78a7c992001-05-15 16:30:25 +0000527/* variable assignment: */
Eric Andersen78a7c992001-05-15 16:30:25 +0000528static int is_assignment(const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000529/* data structure manipulation: */
530static int setup_redirect(struct p_context *ctx, int fd, redir_type style, struct in_str *input);
531static void initialize_context(struct p_context *ctx);
532static int done_word(o_string *dest, struct p_context *ctx);
533static int done_command(struct p_context *ctx);
534static int done_pipe(struct p_context *ctx, pipe_style type);
535/* primary string parsing: */
536static int redirect_dup_num(struct in_str *input);
537static int redirect_opt_num(o_string *o);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000538#if ENABLE_HUSH_TICK
Denis Vlasenko68404f12008-03-17 09:00:54 +0000539static int process_command_subs(o_string *dest, /*struct p_context *ctx,*/
540 struct in_str *input, const char *subst_end);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000541#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000542static int parse_group(o_string *dest, struct p_context *ctx, struct in_str *input, int ch);
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000543static const char *lookup_param(const char *src);
Denis Vlasenko68404f12008-03-17 09:00:54 +0000544static int handle_dollar(o_string *dest, /*struct p_context *ctx,*/
545 struct in_str *input);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000546static 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 +0000547/* setup: */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000548static int parse_and_run_stream(struct in_str *inp, int parse_flag);
549static int parse_and_run_string(const char *s, int parse_flag);
550static int parse_and_run_file(FILE *f);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000551/* job management: */
Eric Andersenc798b072001-06-22 06:23:03 +0000552static int checkjobs(struct pipe* fg_pipe);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000553#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +0000554static int checkjobs_and_fg_shell(struct pipe* fg_pipe);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000555static void insert_bg_job(struct pipe *pi);
556static void remove_bg_job(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000557static void delete_finished_bg_job(struct pipe *pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000558#else
559int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */
560#endif
Eric Andersenf72f5622001-05-15 23:21:41 +0000561/* local variable support */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000562static char **expand_strvec_to_strvec(char **argv);
563/* used for eval */
564static char *expand_strvec_to_string(char **argv);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +0000565/* used for expansion of right hand of assignments */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000566static char *expand_string_to_string(const char *str);
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000567static struct variable *get_local_var(const char *name);
568static int set_local_var(char *str, int flg_export);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000569static void unset_local_var(const char *name);
Eric Andersen25f27032001-04-26 23:22:31 +0000570
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000571
572static char **add_strings_to_strings(int need_xstrdup, char **strings, char **add)
573{
574 int i;
575 unsigned count1;
576 unsigned count2;
577 char **v;
578
579 v = strings;
580 count1 = 0;
581 if (v) {
582 while (*v) {
583 count1++;
584 v++;
585 }
586 }
587 count2 = 0;
588 v = add;
589 while (*v) {
590 count2++;
591 v++;
592 }
593 v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*));
594 v[count1 + count2] = NULL;
595 i = count2;
596 while (--i >= 0)
597 v[count1 + i] = need_xstrdup ? xstrdup(add[i]) : add[i];
598 return v;
599}
600
601/* 'add' should be a malloced pointer */
602static char **add_string_to_strings(char **strings, char *add)
603{
604 char *v[2];
605
606 v[0] = add;
607 v[1] = NULL;
608
609 return add_strings_to_strings(0, strings, v);
610}
611
612static void free_strings(char **strings)
613{
614 if (strings) {
615 char **v = strings;
616 while (*v)
617 free(*v++);
618 free(strings);
619 }
620}
621
622
Denis Vlasenko83506862007-11-23 13:11:42 +0000623/* Function prototypes for builtins */
624static int builtin_cd(char **argv);
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000625static int builtin_echo(char **argv);
Denis Vlasenko83506862007-11-23 13:11:42 +0000626static int builtin_eval(char **argv);
627static int builtin_exec(char **argv);
628static int builtin_exit(char **argv);
629static int builtin_export(char **argv);
630#if ENABLE_HUSH_JOB
631static int builtin_fg_bg(char **argv);
632static int builtin_jobs(char **argv);
633#endif
634#if ENABLE_HUSH_HELP
635static int builtin_help(char **argv);
636#endif
637static int builtin_pwd(char **argv);
638static int builtin_read(char **argv);
639static int builtin_test(char **argv);
640static int builtin_set(char **argv);
641static int builtin_shift(char **argv);
642static int builtin_source(char **argv);
643static int builtin_umask(char **argv);
644static int builtin_unset(char **argv);
645//static int builtin_not_written(char **argv);
646
Eric Andersen25f27032001-04-26 23:22:31 +0000647/* Table of built-in functions. They can be forked or not, depending on
648 * context: within pipes, they fork. As simple commands, they do not.
649 * When used in non-forking context, they can change global variables
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000650 * in the parent shell process. If forked, of course they cannot.
Eric Andersen25f27032001-04-26 23:22:31 +0000651 * For example, 'unset foo | whatever' will parse and run, but foo will
652 * still be set at the end. */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000653struct built_in_command {
654 const char *cmd; /* name */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000655 int (*function) (char **argv); /* function ptr */
Denis Vlasenko06810332007-05-21 23:30:54 +0000656#if ENABLE_HUSH_HELP
657 const char *descr; /* description */
658#define BLTIN(cmd, func, help) { cmd, func, help }
659#else
660#define BLTIN(cmd, func, help) { cmd, func }
661#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000662};
663
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000664/* For now, echo and test are unconditionally enabled.
665 * Maybe make it configurable? */
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +0000666static const struct built_in_command bltins[] = {
Denis Vlasenko83506862007-11-23 13:11:42 +0000667 BLTIN("[" , builtin_test, "Test condition"),
668 BLTIN("[[" , builtin_test, "Test condition"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000669#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000670 BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000671#endif
Denis Vlasenko06810332007-05-21 23:30:54 +0000672// BLTIN("break" , builtin_not_written, "Exit for, while or until loop"),
673 BLTIN("cd" , builtin_cd, "Change working directory"),
674// BLTIN("continue", builtin_not_written, "Continue for, while or until loop"),
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000675 BLTIN("echo" , builtin_echo, "Write strings to stdout"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000676 BLTIN("eval" , builtin_eval, "Construct and run shell command"),
677 BLTIN("exec" , builtin_exec, "Exec command, replacing this shell with the exec'd process"),
678 BLTIN("exit" , builtin_exit, "Exit from shell"),
679 BLTIN("export", builtin_export, "Set environment variable"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000680#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000681 BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"),
682 BLTIN("jobs" , builtin_jobs, "Lists the active jobs"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000683#endif
Denis Vlasenko06810332007-05-21 23:30:54 +0000684// TODO: remove pwd? we have it as an applet...
685 BLTIN("pwd" , builtin_pwd, "Print current directory"),
686 BLTIN("read" , builtin_read, "Input environment variable"),
687// BLTIN("return", builtin_not_written, "Return from a function"),
688 BLTIN("set" , builtin_set, "Set/unset shell local variables"),
689 BLTIN("shift" , builtin_shift, "Shift positional parameters"),
690// BLTIN("trap" , builtin_not_written, "Trap signals"),
Denis Vlasenko83506862007-11-23 13:11:42 +0000691 BLTIN("test" , builtin_test, "Test condition"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000692// BLTIN("ulimit", builtin_not_written, "Controls resource limits"),
693 BLTIN("umask" , builtin_umask, "Sets file creation mask"),
694 BLTIN("unset" , builtin_unset, "Unset environment variable"),
695 BLTIN("." , builtin_source, "Source-in and run commands in a file"),
696#if ENABLE_HUSH_HELP
697 BLTIN("help" , builtin_help, "List shell built-in commands"),
698#endif
699 BLTIN(NULL, NULL, NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000700};
701
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000702#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000703
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000704/* Signals are grouped, we handle them in batches */
705static void set_fatal_sighandler(void (*handler)(int))
706{
Denis Vlasenko25591c32008-02-16 22:58:56 +0000707 bb_signals(0
708 + (1 << SIGILL)
709 + (1 << SIGTRAP)
710 + (1 << SIGABRT)
711 + (1 << SIGFPE)
712 + (1 << SIGBUS)
713 + (1 << SIGSEGV)
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000714 /* bash 3.2 seems to handle these just like 'fatal' ones */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000715 + (1 << SIGHUP)
716 + (1 << SIGPIPE)
717 + (1 << SIGALRM)
718 , handler);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000719}
720static void set_jobctrl_sighandler(void (*handler)(int))
721{
Denis Vlasenko25591c32008-02-16 22:58:56 +0000722 bb_signals(0
723 + (1 << SIGTSTP)
724 + (1 << SIGTTIN)
725 + (1 << SIGTTOU)
726 , handler);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000727}
728static void set_misc_sighandler(void (*handler)(int))
729{
Denis Vlasenko25591c32008-02-16 22:58:56 +0000730 bb_signals(0
731 + (1 << SIGINT)
732 + (1 << SIGQUIT)
733 + (1 << SIGTERM)
734 , handler);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000735}
736/* SIGCHLD is special and handled separately */
737
738static void set_every_sighandler(void (*handler)(int))
739{
740 set_fatal_sighandler(handler);
741 set_jobctrl_sighandler(handler);
742 set_misc_sighandler(handler);
743 signal(SIGCHLD, handler);
744}
745
Denis Vlasenko68404f12008-03-17 09:00:54 +0000746static void handler_ctrl_c(int sig ATTRIBUTE_UNUSED)
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000747{
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000748 debug_printf_jobs("got sig %d\n", sig);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000749// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000750 siglongjmp(toplevel_jb, 1);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000751}
752
Denis Vlasenko68404f12008-03-17 09:00:54 +0000753static void handler_ctrl_z(int sig ATTRIBUTE_UNUSED)
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000754{
755 pid_t pid;
756
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000757 debug_printf_jobs("got tty sig %d in pid %d\n", sig, getpid());
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000758 pid = fork();
Denis Vlasenkoc2990322007-05-16 12:57:12 +0000759 if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000760 return;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000761 ctrl_z_flag = 1;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000762 if (!pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +0000763 if (ENABLE_HUSH_JOB)
764 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000765 setpgrp();
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000766 debug_printf_jobs("set pgrp for child %d ok\n", getpid());
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000767 set_every_sighandler(SIG_DFL);
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000768 raise(SIGTSTP); /* resend TSTP so that child will be stopped */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000769 debug_printf_jobs("returning in child\n");
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000770 /* return to nofork, it will eventually exit now,
771 * not return back to shell */
772 return;
773 }
774 /* parent */
775 /* finish filling up pipe info */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000776 toplevel_list->pgrp = pid; /* child is in its own pgrp */
777 toplevel_list->progs[0].pid = pid;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000778 /* parent needs to longjmp out of running nofork.
779 * we will "return" exitcode 0, with child put in background */
780// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000781 debug_printf_jobs("siglongjmp in parent\n");
782 siglongjmp(toplevel_jb, 1);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000783}
784
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000785/* Restores tty foreground process group, and exits.
786 * May be called as signal handler for fatal signal
787 * (will faithfully resend signal to itself, producing correct exit state)
788 * or called directly with -EXITCODE.
789 * We also call it if xfunc is exiting. */
790static void sigexit(int sig) ATTRIBUTE_NORETURN;
791static void sigexit(int sig)
792{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000793 /* Disable all signals: job control, SIGPIPE, etc. */
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000794 sigprocmask_allsigs(SIG_BLOCK);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000795
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000796 if (interactive_fd)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000797 tcsetpgrp(interactive_fd, saved_tty_pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000798
799 /* Not a signal, just exit */
800 if (sig <= 0)
801 _exit(- sig);
802
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000803 kill_myself_with_sig(sig); /* does not return */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000804}
805
806/* Restores tty foreground process group, and exits. */
807static void hush_exit(int exitcode) ATTRIBUTE_NORETURN;
808static void hush_exit(int exitcode)
809{
810 fflush(NULL); /* flush all streams */
811 sigexit(- (exitcode & 0xff));
812}
813
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000814#else /* !JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000815
816#define set_fatal_sighandler(handler) ((void)0)
817#define set_jobctrl_sighandler(handler) ((void)0)
818#define set_misc_sighandler(handler) ((void)0)
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000819#define hush_exit(e) exit(e)
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000820
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000821#endif /* JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000822
823
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000824static const char *set_cwd(void)
825{
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000826 if (cwd == bb_msg_unknown)
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000827 cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000828 cwd = xrealloc_getcwd_or_warn((char *)cwd);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000829 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000830 cwd = bb_msg_unknown;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000831 return cwd;
832}
833
Denis Vlasenko83506862007-11-23 13:11:42 +0000834
835/* built-in 'test' handler */
836static int builtin_test(char **argv)
837{
838 int argc = 0;
839 while (*argv) {
840 argc++;
841 argv++;
842 }
843 return test_main(argc, argv - argc);
844}
845
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000846/* built-in 'test' handler */
847static int builtin_echo(char **argv)
848{
849 int argc = 0;
850 while (*argv) {
851 argc++;
852 argv++;
853 }
Denis Vlasenkofe5e23b2007-11-24 02:23:51 +0000854 return echo_main(argc, argv - argc);
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000855}
856
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000857/* built-in 'eval' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000858static int builtin_eval(char **argv)
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000859{
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000860 int rcode = EXIT_SUCCESS;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000861
Denis Vlasenko1359da62007-04-21 23:27:30 +0000862 if (argv[1]) {
Denis Vlasenko170435c2007-05-23 13:01:10 +0000863 char *str = expand_strvec_to_string(argv + 1);
864 parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP |
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000865 PARSEFLAG_SEMICOLON);
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000866 free(str);
867 rcode = last_return_code;
868 }
869 return rcode;
870}
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000871
Eric Andersen25f27032001-04-26 23:22:31 +0000872/* built-in 'cd <path>' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000873static int builtin_cd(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000874{
Denis Vlasenko170435c2007-05-23 13:01:10 +0000875 const char *newdir;
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000876 if (argv[1] == NULL) {
877 // bash does nothing (exitcode 0) if HOME is ""; if it's unset,
878 // bash says "bash: cd: HOME not set" and does nothing (exitcode 1)
Denis Vlasenko170435c2007-05-23 13:01:10 +0000879 newdir = getenv("HOME") ? : "/";
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000880 } else
Denis Vlasenko1359da62007-04-21 23:27:30 +0000881 newdir = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000882 if (chdir(newdir)) {
883 printf("cd: %s: %s\n", newdir, strerror(errno));
884 return EXIT_FAILURE;
885 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000886 set_cwd();
Eric Andersen25f27032001-04-26 23:22:31 +0000887 return EXIT_SUCCESS;
888}
889
Eric Andersen25f27032001-04-26 23:22:31 +0000890/* built-in 'exec' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000891static int builtin_exec(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000892{
Denis Vlasenko1359da62007-04-21 23:27:30 +0000893 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000894 return EXIT_SUCCESS; /* Really? */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000895 pseudo_exec_argv(argv + 1);
Eric Andersen25f27032001-04-26 23:22:31 +0000896 /* never returns */
897}
898
899/* built-in 'exit' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000900static int builtin_exit(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000901{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000902// TODO: bash does it ONLY on top-level sh exit (+interacive only?)
903 //puts("exit"); /* bash does it */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000904// TODO: warn if we have background jobs: "There are stopped jobs"
905// On second consecutive 'exit', exit anyway.
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000906
Denis Vlasenko1359da62007-04-21 23:27:30 +0000907 if (argv[1] == NULL)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000908 hush_exit(last_return_code);
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000909 /* mimic bash: exit 123abc == exit 255 + error msg */
910 xfunc_error_retval = 255;
911 /* bash: exit -2 == exit 254, no error msg */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000912 hush_exit(xatoi(argv[1]) & 0xff);
Eric Andersen25f27032001-04-26 23:22:31 +0000913}
914
915/* built-in 'export VAR=value' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000916static int builtin_export(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000917{
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000918 const char *value;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000919 char *name = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000920
Eric Andersenf72f5622001-05-15 23:21:41 +0000921 if (name == NULL) {
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000922 // TODO:
923 // ash emits: export VAR='VAL'
924 // bash: declare -x VAR="VAL"
925 // (both also escape as needed (quotes, $, etc))
926 char **e = environ;
927 if (e)
928 while (*e)
929 puts(*e++);
930 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000931 }
Eric Andersenf72f5622001-05-15 23:21:41 +0000932
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000933 value = strchr(name, '=');
934 if (!value) {
935 /* They are exporting something without a =VALUE */
936 struct variable *var;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000937
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000938 var = get_local_var(name);
939 if (var) {
940 var->flg_export = 1;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000941 putenv(var->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000942 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000943 /* bash does not return an error when trying to export
944 * an undefined variable. Do likewise. */
945 return EXIT_SUCCESS;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000946 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000947
948 set_local_var(xstrdup(name), 1);
949 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000950}
951
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000952#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +0000953/* built-in 'fg' and 'bg' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000954static int builtin_fg_bg(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000955{
Eric Andersen0fcd4472001-05-02 20:12:03 +0000956 int i, jobnum;
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000957 struct pipe *pi;
Eric Andersen25f27032001-04-26 23:22:31 +0000958
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000959 if (!interactive_fd)
Eric Andersenc798b072001-06-22 06:23:03 +0000960 return EXIT_FAILURE;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000961 /* If they gave us no args, assume they want the last backgrounded task */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000962 if (!argv[1]) {
Eric Andersenc798b072001-06-22 06:23:03 +0000963 for (pi = job_list; pi; pi = pi->next) {
964 if (pi->jobid == last_jobid) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000965 goto found;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000966 }
967 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000968 bb_error_msg("%s: no current job", argv[0]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000969 return EXIT_FAILURE;
970 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000971 if (sscanf(argv[1], "%%%d", &jobnum) != 1) {
972 bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000973 return EXIT_FAILURE;
974 }
975 for (pi = job_list; pi; pi = pi->next) {
976 if (pi->jobid == jobnum) {
977 goto found;
Eric Andersen25f27032001-04-26 23:22:31 +0000978 }
979 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000980 bb_error_msg("%s: %d: no such job", argv[0], jobnum);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000981 return EXIT_FAILURE;
982 found:
Denis Vlasenko52881e92007-04-21 13:42:52 +0000983 // TODO: bash prints a string representation
984 // of job being foregrounded (like "sleep 1 | cat")
Denis Vlasenko1359da62007-04-21 23:27:30 +0000985 if (*argv[0] == 'f') {
Eric Andersen028b65b2001-06-28 01:10:11 +0000986 /* Put the job into the foreground. */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000987 tcsetpgrp(interactive_fd, pi->pgrp);
Eric Andersen25f27032001-04-26 23:22:31 +0000988 }
989
990 /* Restart the processes in the job */
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000991 debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_progs, pi->pgrp);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000992 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000993 debug_printf_jobs("reviving pid %d\n", pi->progs[i].pid);
Eric Andersen0fcd4472001-05-02 20:12:03 +0000994 pi->progs[i].is_stopped = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000995 }
996 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +0000997
Denis Vlasenkobb81c582007-01-30 22:32:09 +0000998 i = kill(- pi->pgrp, SIGCONT);
999 if (i < 0) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +00001000 if (errno == ESRCH) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001001 delete_finished_bg_job(pi);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001002 return EXIT_SUCCESS;
Eric Andersen028b65b2001-06-28 01:10:11 +00001003 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001004 bb_perror_msg("kill (SIGCONT)");
Eric Andersen028b65b2001-06-28 01:10:11 +00001005 }
1006 }
Eric Andersen25f27032001-04-26 23:22:31 +00001007
Denis Vlasenko1359da62007-04-21 23:27:30 +00001008 if (*argv[0] == 'f') {
1009 remove_bg_job(pi);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001010 return checkjobs_and_fg_shell(pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001011 }
Eric Andersen25f27032001-04-26 23:22:31 +00001012 return EXIT_SUCCESS;
1013}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001014#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001015
1016/* built-in 'help' handler */
Denis Vlasenko06810332007-05-21 23:30:54 +00001017#if ENABLE_HUSH_HELP
Denis Vlasenko1359da62007-04-21 23:27:30 +00001018static int builtin_help(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +00001019{
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001020 const struct built_in_command *x;
Eric Andersen25f27032001-04-26 23:22:31 +00001021
1022 printf("\nBuilt-in commands:\n");
1023 printf("-------------------\n");
1024 for (x = bltins; x->cmd; x++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001025 printf("%s\t%s\n", x->cmd, x->descr);
1026 }
1027 printf("\n\n");
1028 return EXIT_SUCCESS;
1029}
Denis Vlasenko06810332007-05-21 23:30:54 +00001030#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001031
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001032#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +00001033/* built-in 'jobs' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001034static int builtin_jobs(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +00001035{
1036 struct pipe *job;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001037 const char *status_string;
Eric Andersen25f27032001-04-26 23:22:31 +00001038
Eric Andersenc798b072001-06-22 06:23:03 +00001039 for (job = job_list; job; job = job->next) {
Eric Andersen25f27032001-04-26 23:22:31 +00001040 if (job->running_progs == job->stopped_progs)
1041 status_string = "Stopped";
1042 else
1043 status_string = "Running";
Eric Andersen52a97ca2001-06-22 06:49:26 +00001044
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001045 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
Eric Andersen25f27032001-04-26 23:22:31 +00001046 }
1047 return EXIT_SUCCESS;
1048}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001049#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001050
Eric Andersen25f27032001-04-26 23:22:31 +00001051/* built-in 'pwd' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001052static int builtin_pwd(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +00001053{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001054 puts(set_cwd());
Eric Andersen25f27032001-04-26 23:22:31 +00001055 return EXIT_SUCCESS;
1056}
1057
1058/* built-in 'read VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001059static int builtin_read(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001060{
Denis Vlasenkod67cef22007-06-13 06:47:47 +00001061 char *string;
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001062 const char *name = argv[1] ? argv[1] : "REPLY";
Eric Andersen25f27032001-04-26 23:22:31 +00001063
Denis Vlasenkod67cef22007-06-13 06:47:47 +00001064 string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name));
1065 return set_local_var(string, 0);
Eric Andersen25f27032001-04-26 23:22:31 +00001066}
1067
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001068/* built-in 'set [VAR=value]' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001069static int builtin_set(char **argv)
Eric Andersenf72f5622001-05-15 23:21:41 +00001070{
Denis Vlasenko1359da62007-04-21 23:27:30 +00001071 char *temp = argv[1];
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001072 struct variable *e;
Eric Andersenf72f5622001-05-15 23:21:41 +00001073
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001074 if (temp == NULL)
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001075 for (e = top_var; e; e = e->next)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00001076 puts(e->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001077 else
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001078 set_local_var(xstrdup(temp), 0);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001079
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001080 return EXIT_SUCCESS;
Eric Andersenf72f5622001-05-15 23:21:41 +00001081}
1082
1083
Eric Andersen25f27032001-04-26 23:22:31 +00001084/* Built-in 'shift' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001085static int builtin_shift(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001086{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001087 int n = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001088 if (argv[1]) {
1089 n = atoi(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001090 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001091 if (n >= 0 && n < global_argc) {
Denis Vlasenko004baba2007-05-20 22:22:18 +00001092 global_argv[n] = global_argv[0];
Eric Andersen25f27032001-04-26 23:22:31 +00001093 global_argc -= n;
1094 global_argv += n;
1095 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00001096 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001097 return EXIT_FAILURE;
Eric Andersen25f27032001-04-26 23:22:31 +00001098}
1099
1100/* Built-in '.' handler (read-in and execute commands from file) */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001101static int builtin_source(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001102{
1103 FILE *input;
1104 int status;
1105
Denis Vlasenko1359da62007-04-21 23:27:30 +00001106 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +00001107 return EXIT_FAILURE;
1108
1109 /* XXX search through $PATH is missing */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001110 input = fopen(argv[1], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00001111 if (!input) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001112 bb_error_msg("cannot open '%s'", argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001113 return EXIT_FAILURE;
1114 }
Denis Vlasenkoa0898172007-10-01 09:59:01 +00001115 close_on_exec_on(fileno(input));
Eric Andersen25f27032001-04-26 23:22:31 +00001116
1117 /* Now run the file */
1118 /* XXX argv and argc are broken; need to save old global_argv
1119 * (pointer only is OK!) on this stack frame,
Denis Vlasenko1359da62007-04-21 23:27:30 +00001120 * set global_argv=argv+1, recurse, and restore. */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001121 status = parse_and_run_file(input);
Eric Andersen25f27032001-04-26 23:22:31 +00001122 fclose(input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001123 return status;
Eric Andersen25f27032001-04-26 23:22:31 +00001124}
1125
Denis Vlasenko1359da62007-04-21 23:27:30 +00001126static int builtin_umask(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001127{
Eric Andersen83a2ae22001-05-07 17:59:25 +00001128 mode_t new_umask;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001129 const char *arg = argv[1];
Eric Andersen83a2ae22001-05-07 17:59:25 +00001130 char *end;
1131 if (arg) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001132 new_umask = strtoul(arg, &end, 8);
1133 if (*end != '\0' || end == arg) {
Eric Andersen83a2ae22001-05-07 17:59:25 +00001134 return EXIT_FAILURE;
1135 }
1136 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001137 new_umask = umask(0);
1138 printf("%.3o\n", (unsigned) new_umask);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001139 }
1140 umask(new_umask);
1141 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00001142}
1143
1144/* built-in 'unset VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001145static int builtin_unset(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001146{
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001147 /* bash always returns true */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001148 unset_local_var(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001149 return EXIT_SUCCESS;
1150}
1151
Denis Vlasenko06810332007-05-21 23:30:54 +00001152//static int builtin_not_written(char **argv)
1153//{
1154// printf("builtin_%s not written\n", argv[0]);
1155// return EXIT_FAILURE;
1156//}
Eric Andersen83a2ae22001-05-07 17:59:25 +00001157
Eric Andersen25f27032001-04-26 23:22:31 +00001158static int b_check_space(o_string *o, int len)
1159{
1160 /* It would be easy to drop a more restrictive policy
1161 * in here, such as setting a maximum string length */
1162 if (o->length + len > o->maxlen) {
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001163 /* assert(data == NULL || o->maxlen != 0); */
Denis Vlasenkoef36ead2007-05-02 15:34:47 +00001164 o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001165 o->data = xrealloc(o->data, 1 + o->maxlen);
Eric Andersen25f27032001-04-26 23:22:31 +00001166 }
1167 return o->data == NULL;
1168}
1169
1170static int b_addchr(o_string *o, int ch)
1171{
Denis Vlasenko831dcc42007-05-16 15:05:36 +00001172 debug_printf("b_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001173 if (b_check_space(o, 1))
1174 return B_NOSPAC;
Eric Andersen25f27032001-04-26 23:22:31 +00001175 o->data[o->length] = ch;
1176 o->length++;
1177 o->data[o->length] = '\0';
1178 return 0;
1179}
1180
1181static void b_reset(o_string *o)
1182{
1183 o->length = 0;
1184 o->nonnull = 0;
Denis Vlasenkod65ea392007-10-01 10:02:25 +00001185 if (o->data)
1186 o->data[0] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00001187}
1188
1189static void b_free(o_string *o)
1190{
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001191 free(o->data);
Denis Vlasenkod65ea392007-10-01 10:02:25 +00001192 memset(o, 0, sizeof(*o));
Eric Andersen25f27032001-04-26 23:22:31 +00001193}
1194
1195/* My analysis of quoting semantics tells me that state information
1196 * is associated with a destination, not a source.
1197 */
1198static int b_addqchr(o_string *o, int ch, int quote)
1199{
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00001200 if (quote && strchr("*?[\\", ch)) {
Eric Andersen25f27032001-04-26 23:22:31 +00001201 int rc;
1202 rc = b_addchr(o, '\\');
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001203 if (rc)
1204 return rc;
Eric Andersen25f27032001-04-26 23:22:31 +00001205 }
1206 return b_addchr(o, ch);
1207}
1208
Eric Andersen25f27032001-04-26 23:22:31 +00001209static int static_get(struct in_str *i)
1210{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001211 int ch = *i->p++;
1212 if (ch == '\0') return EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001213 return ch;
1214}
1215
1216static int static_peek(struct in_str *i)
1217{
1218 return *i->p;
1219}
1220
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001221#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001222#if ENABLE_FEATURE_EDITING
Rob Landley88621d72006-08-29 19:41:06 +00001223static void cmdedit_set_initial_prompt(void)
Eric Andersen25f27032001-04-26 23:22:31 +00001224{
Denis Vlasenko38f63192007-01-22 09:03:07 +00001225#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001226 PS1 = NULL;
1227#else
1228 PS1 = getenv("PS1");
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001229 if (PS1 == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +00001230 PS1 = "\\w \\$ ";
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001231#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001232}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001233#endif /* EDITING */
Eric Andersen25f27032001-04-26 23:22:31 +00001234
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001235static const char* setup_prompt_string(int promptmode)
Eric Andersen25f27032001-04-26 23:22:31 +00001236{
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001237 const char *prompt_str;
1238 debug_printf("setup_prompt_string %d ", promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001239#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001240 /* Set up the prompt */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001241 if (promptmode == 0) { /* PS1 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001242 free((char*)PS1);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001243 PS1 = xasprintf("%s %c ", cwd, (geteuid() != 0) ? '$' : '#');
1244 prompt_str = PS1;
Eric Andersen25f27032001-04-26 23:22:31 +00001245 } else {
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001246 prompt_str = PS2;
Eric Andersen25f27032001-04-26 23:22:31 +00001247 }
1248#else
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001249 prompt_str = (promptmode == 0) ? PS1 : PS2;
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001250#endif
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001251 debug_printf("result '%s'\n", prompt_str);
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001252 return prompt_str;
Eric Andersen25f27032001-04-26 23:22:31 +00001253}
1254
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001255static void get_user_input(struct in_str *i)
Eric Andersen25f27032001-04-26 23:22:31 +00001256{
Denis Vlasenko0937be52007-04-28 16:47:08 +00001257 int r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001258 const char *prompt_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001259
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001260 prompt_str = setup_prompt_string(i->promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001261#if ENABLE_FEATURE_EDITING
Denis Vlasenko170435c2007-05-23 13:01:10 +00001262 /* Enable command line editing only while a command line
Denis Vlasenkoe376d452008-02-20 22:23:24 +00001263 * is actually being read */
1264 do {
1265 r = read_line_input(prompt_str, user_input_buf, BUFSIZ-1, line_input_state);
1266 } while (r == 0); /* repeat if Ctrl-C */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001267 i->eof_flag = (r < 0);
1268 if (i->eof_flag) { /* EOF/error detected */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001269 user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */
1270 user_input_buf[1] = '\0';
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001271 }
Eric Andersen25f27032001-04-26 23:22:31 +00001272#else
1273 fputs(prompt_str, stdout);
1274 fflush(stdout);
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001275 user_input_buf[0] = r = fgetc(i->file);
1276 /*user_input_buf[1] = '\0'; - already is and never changed */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001277 i->eof_flag = (r == EOF);
Eric Andersen25f27032001-04-26 23:22:31 +00001278#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001279 i->p = user_input_buf;
Eric Andersen25f27032001-04-26 23:22:31 +00001280}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001281#endif /* INTERACTIVE */
Eric Andersen25f27032001-04-26 23:22:31 +00001282
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001283/* This is the magic location that prints prompts
Eric Andersen25f27032001-04-26 23:22:31 +00001284 * and gets data back from the user */
1285static int file_get(struct in_str *i)
1286{
1287 int ch;
1288
Eric Andersen25f27032001-04-26 23:22:31 +00001289 /* If there is data waiting, eat it up */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001290 if (i->p && *i->p) {
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001291#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001292 take_cached:
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001293#endif
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001294 ch = *i->p++;
1295 if (i->eof_flag && !*i->p)
1296 ch = EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001297 } else {
1298 /* need to double check i->file because we might be doing something
1299 * more complicated by now, like sourcing or substituting. */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001300#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001301 if (interactive_fd && i->promptme && i->file == stdin) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001302 do {
1303 get_user_input(i);
1304 } while (!*i->p); /* need non-empty line */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001305 i->promptmode = 1; /* PS2 */
1306 i->promptme = 0;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001307 goto take_cached;
Eric Andersen25f27032001-04-26 23:22:31 +00001308 }
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001309#endif
1310 ch = fgetc(i->file);
Eric Andersen25f27032001-04-26 23:22:31 +00001311 }
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001312 debug_printf("file_get: got a '%c' %d\n", ch, ch);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001313#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001314 if (ch == '\n')
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001315 i->promptme = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001316#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001317 return ch;
1318}
1319
1320/* All the callers guarantee this routine will never be
1321 * used right after a newline, so prompting is not needed.
1322 */
1323static int file_peek(struct in_str *i)
1324{
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001325 int ch;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001326 if (i->p && *i->p) {
1327 if (i->eof_flag && !i->p[1])
1328 return EOF;
1329 return *i->p;
Eric Andersen25f27032001-04-26 23:22:31 +00001330 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001331 ch = fgetc(i->file);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001332 i->eof_flag = (ch == EOF);
1333 i->peek_buf[0] = ch;
1334 i->peek_buf[1] = '\0';
1335 i->p = i->peek_buf;
1336 debug_printf("file_peek: got a '%c' %d\n", *i->p, *i->p);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001337 return ch;
Eric Andersen25f27032001-04-26 23:22:31 +00001338}
1339
1340static void setup_file_in_str(struct in_str *i, FILE *f)
1341{
1342 i->peek = file_peek;
1343 i->get = file_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001344#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001345 i->promptme = 1;
1346 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001347#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001348 i->file = f;
1349 i->p = NULL;
1350}
1351
1352static void setup_string_in_str(struct in_str *i, const char *s)
1353{
1354 i->peek = static_peek;
1355 i->get = static_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001356#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001357 i->promptme = 1;
1358 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001359#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001360 i->p = s;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001361 i->eof_flag = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001362}
1363
Eric Andersen25f27032001-04-26 23:22:31 +00001364/* squirrel != NULL means we squirrel away copies of stdin, stdout,
1365 * and stderr if they are redirected. */
1366static int setup_redirects(struct child_prog *prog, int squirrel[])
1367{
1368 int openfd, mode;
1369 struct redir_struct *redir;
1370
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001371 for (redir = prog->redirects; redir; redir = redir->next) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00001372 if (redir->dup == -1 && redir->glob_word == NULL) {
Eric Andersen817e73c2001-06-06 17:56:09 +00001373 /* something went wrong in the parse. Pretend it didn't happen */
1374 continue;
1375 }
Eric Andersen25f27032001-04-26 23:22:31 +00001376 if (redir->dup == -1) {
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001377 char *p;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001378 mode = redir_table[redir->type].mode;
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001379 p = expand_string_to_string(redir->glob_word[0]);
1380 openfd = open_or_warn(p, mode);
1381 free(p);
Eric Andersen25f27032001-04-26 23:22:31 +00001382 if (openfd < 0) {
1383 /* this could get lost if stderr has been redirected, but
1384 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersen25f27032001-04-26 23:22:31 +00001385 return 1;
1386 }
1387 } else {
1388 openfd = redir->dup;
1389 }
1390
1391 if (openfd != redir->fd) {
1392 if (squirrel && redir->fd < 3) {
1393 squirrel[redir->fd] = dup(redir->fd);
1394 }
Eric Andersen83a2ae22001-05-07 17:59:25 +00001395 if (openfd == -3) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001396 //close(openfd); // close(-3) ??!
Eric Andersen83a2ae22001-05-07 17:59:25 +00001397 } else {
1398 dup2(openfd, redir->fd);
Matt Kraaic616e532001-06-05 16:50:08 +00001399 if (redir->dup == -1)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001400 close(openfd);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001401 }
Eric Andersen25f27032001-04-26 23:22:31 +00001402 }
1403 }
1404 return 0;
1405}
1406
1407static void restore_redirects(int squirrel[])
1408{
1409 int i, fd;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001410 for (i = 0; i < 3; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001411 fd = squirrel[i];
1412 if (fd != -1) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00001413 /* We simply die on error */
1414 xmove_fd(fd, i);
Eric Andersen25f27032001-04-26 23:22:31 +00001415 }
1416 }
1417}
1418
Denis Vlasenko05743d72008-02-10 12:10:08 +00001419/* Called after [v]fork() in run_pipe(), or from builtin_exec().
Denis Vlasenko8412d792007-10-01 09:59:47 +00001420 * Never returns.
1421 * XXX no exit() here. If you don't exec, use _exit instead.
Eric Andersen94ac2442001-05-22 19:05:18 +00001422 * The at_exit handlers apparently confuse the calling process,
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001423 * in particular stdin handling. Not sure why? -- because of vfork! (vda) */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001424static void pseudo_exec_argv(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001425{
Eric Andersen78a7c992001-05-15 16:30:25 +00001426 int i, rcode;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001427 char *p;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001428 const struct built_in_command *x;
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001429
Denis Vlasenko1359da62007-04-21 23:27:30 +00001430 for (i = 0; is_assignment(argv[i]); i++) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001431 debug_printf_exec("pid %d environment modification: %s\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001432 getpid(), argv[i]);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001433// FIXME: vfork case??
Denis Vlasenko170435c2007-05-23 13:01:10 +00001434 p = expand_string_to_string(argv[i]);
1435 putenv(p);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001436 }
1437 argv += i;
1438 /* If a variable is assigned in a forest, and nobody listens,
1439 * was it ever really set?
1440 */
Denis Vlasenko05743d72008-02-10 12:10:08 +00001441 if (!argv[0])
Denis Vlasenko1359da62007-04-21 23:27:30 +00001442 _exit(EXIT_SUCCESS);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001443
Denis Vlasenko170435c2007-05-23 13:01:10 +00001444 argv = expand_strvec_to_strvec(argv);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001445
Denis Vlasenko1359da62007-04-21 23:27:30 +00001446 /*
1447 * Check if the command matches any of the builtins.
1448 * Depending on context, this might be redundant. But it's
1449 * easier to waste a few CPU cycles than it is to figure out
1450 * if this is one of those cases.
1451 */
1452 for (x = bltins; x->cmd; x++) {
1453 if (strcmp(argv[0], x->cmd) == 0) {
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001454 debug_printf_exec("running builtin '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001455 rcode = x->function(argv);
1456 fflush(stdout);
1457 _exit(rcode);
Eric Andersen94ac2442001-05-22 19:05:18 +00001458 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001459 }
Eric Andersen78a7c992001-05-15 16:30:25 +00001460
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001461 /* Check if the command matches any busybox applets */
Denis Vlasenko80d14be2007-04-10 23:03:30 +00001462#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001463 if (strchr(argv[0], '/') == NULL) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001464 int a = find_applet_by_name(argv[0]);
1465 if (a >= 0) {
1466 if (APPLET_IS_NOEXEC(a)) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001467 debug_printf_exec("running applet '%s'\n", argv[0]);
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001468// is it ok that run_applet_no_and_exit() does exit(), not _exit()?
1469 run_applet_no_and_exit(a, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001470 }
1471 /* re-exec ourselves with the new arguments */
1472 debug_printf_exec("re-execing applet '%s'\n", argv[0]);
Denis Vlasenkobdbbb7e2007-06-08 15:02:55 +00001473 execvp(bb_busybox_exec_path, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001474 /* If they called chroot or otherwise made the binary no longer
1475 * executable, fall through */
1476 }
1477 }
Eric Andersenaac75e52001-04-30 18:18:45 +00001478#endif
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001479
1480 debug_printf_exec("execing '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001481 execvp(argv[0], argv);
1482 bb_perror_msg("cannot exec '%s'", argv[0]);
1483 _exit(1);
1484}
1485
Denis Vlasenko05743d72008-02-10 12:10:08 +00001486/* Called after [v]fork() in run_pipe()
Denis Vlasenko8412d792007-10-01 09:59:47 +00001487 */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001488static void pseudo_exec(struct child_prog *child)
1489{
Denis Vlasenkof2fffd02007-05-02 23:39:04 +00001490// FIXME: buggy wrt NOMMU! Must not modify any global data
Denis Vlasenko05743d72008-02-10 12:10:08 +00001491// until it does exec/_exit, but currently it does
1492// (puts malloc'ed stuff into environment)
1493 if (child->argv)
Denis Vlasenko1359da62007-04-21 23:27:30 +00001494 pseudo_exec_argv(child->argv);
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001495
1496 if (child->group) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001497#if !BB_MMU
Denis Vlasenko3b492162007-12-24 14:26:57 +00001498 bb_error_msg_and_die("nested lists are not supported on NOMMU");
Denis Vlasenko8412d792007-10-01 09:59:47 +00001499#else
Denis Vlasenko3b492162007-12-24 14:26:57 +00001500 int rcode;
1501
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001502#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko05743d72008-02-10 12:10:08 +00001503// run_list_level now takes care of it?
1504// debug_printf_exec("pseudo_exec: setting interactive_fd=0\n");
1505// interactive_fd = 0; /* crucial!!!! */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001506#endif
Denis Vlasenko05743d72008-02-10 12:10:08 +00001507 debug_printf_exec("pseudo_exec: run_list\n");
1508 rcode = run_list(child->group);
Eric Andersenbf7df042001-05-23 22:18:35 +00001509 /* OK to leak memory by not calling free_pipe_list,
Eric Andersen25f27032001-04-26 23:22:31 +00001510 * since this process is about to exit */
Eric Andersen94ac2442001-05-22 19:05:18 +00001511 _exit(rcode);
Denis Vlasenko8412d792007-10-01 09:59:47 +00001512#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001513 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001514
1515 /* Can happen. See what bash does with ">foo" by itself. */
1516 debug_printf("trying to pseudo_exec null command\n");
1517 _exit(EXIT_SUCCESS);
Eric Andersen25f27032001-04-26 23:22:31 +00001518}
1519
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001520#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001521static const char *get_cmdtext(struct pipe *pi)
1522{
1523 char **argv;
1524 char *p;
1525 int len;
1526
1527 /* This is subtle. ->cmdtext is created only on first backgrounding.
1528 * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...)
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001529 * On subsequent bg argv is trashed, but we won't use it */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001530 if (pi->cmdtext)
1531 return pi->cmdtext;
1532 argv = pi->progs[0].argv;
1533 if (!argv || !argv[0])
1534 return (pi->cmdtext = xzalloc(1));
1535
1536 len = 0;
1537 do len += strlen(*argv) + 1; while (*++argv);
1538 pi->cmdtext = p = xmalloc(len);
1539 argv = pi->progs[0].argv;
1540 do {
1541 len = strlen(*argv);
1542 memcpy(p, *argv, len);
1543 p += len;
1544 *p++ = ' ';
1545 } while (*++argv);
1546 p[-1] = '\0';
1547 return pi->cmdtext;
1548}
1549
Eric Andersenbafd94f2001-05-02 16:11:59 +00001550static void insert_bg_job(struct pipe *pi)
1551{
1552 struct pipe *thejob;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001553 int i;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001554
1555 /* Linear search for the ID of the job to use */
1556 pi->jobid = 1;
Eric Andersenc798b072001-06-22 06:23:03 +00001557 for (thejob = job_list; thejob; thejob = thejob->next)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001558 if (thejob->jobid >= pi->jobid)
1559 pi->jobid = thejob->jobid + 1;
1560
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001561 /* Add thejob to the list of running jobs */
Eric Andersenc798b072001-06-22 06:23:03 +00001562 if (!job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001563 thejob = job_list = xmalloc(sizeof(*thejob));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001564 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001565 for (thejob = job_list; thejob->next; thejob = thejob->next)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001566 continue;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001567 thejob->next = xmalloc(sizeof(*thejob));
1568 thejob = thejob->next;
1569 }
1570
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001571 /* Physically copy the struct job */
Eric Andersen0fcd4472001-05-02 20:12:03 +00001572 memcpy(thejob, pi, sizeof(struct pipe));
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001573 thejob->progs = xzalloc(sizeof(pi->progs[0]) * pi->num_progs);
1574 /* We cannot copy entire pi->progs[] vector! Double free()s will happen */
1575 for (i = 0; i < pi->num_progs; i++) {
1576// TODO: do we really need to have so many fields which are just dead weight
1577// at execution stage?
1578 thejob->progs[i].pid = pi->progs[i].pid;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001579 /* all other fields are not used and stay zero */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001580 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001581 thejob->next = NULL;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001582 thejob->cmdtext = xstrdup(get_cmdtext(pi));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001583
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001584 /* We don't wait for background thejobs to return -- append it
Eric Andersenbafd94f2001-05-02 16:11:59 +00001585 to the list of backgrounded thejobs and leave it alone */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001586 printf("[%d] %d %s\n", thejob->jobid, thejob->progs[0].pid, thejob->cmdtext);
Eric Andersen1a6d39b2001-05-08 05:11:54 +00001587 last_bg_pid = thejob->progs[0].pid;
Eric Andersenc798b072001-06-22 06:23:03 +00001588 last_jobid = thejob->jobid;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001589}
1590
Eric Andersenbafd94f2001-05-02 16:11:59 +00001591static void remove_bg_job(struct pipe *pi)
1592{
1593 struct pipe *prev_pipe;
1594
Eric Andersenc798b072001-06-22 06:23:03 +00001595 if (pi == job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001596 job_list = pi->next;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001597 } else {
Eric Andersenc798b072001-06-22 06:23:03 +00001598 prev_pipe = job_list;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001599 while (prev_pipe->next != pi)
1600 prev_pipe = prev_pipe->next;
1601 prev_pipe->next = pi->next;
1602 }
Eric Andersen028b65b2001-06-28 01:10:11 +00001603 if (job_list)
1604 last_jobid = job_list->jobid;
1605 else
1606 last_jobid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001607}
Eric Andersen028b65b2001-06-28 01:10:11 +00001608
Denis Vlasenko1359da62007-04-21 23:27:30 +00001609/* remove a backgrounded job */
1610static void delete_finished_bg_job(struct pipe *pi)
1611{
1612 remove_bg_job(pi);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001613 pi->stopped_progs = 0;
Eric Andersenbf7df042001-05-23 22:18:35 +00001614 free_pipe(pi, 0);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001615 free(pi);
1616}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001617#endif /* JOB */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001618
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001619/* Checks to see if any processes have exited -- if they
Eric Andersenbafd94f2001-05-02 16:11:59 +00001620 have, figure out why and see if a job has completed */
Eric Andersenc798b072001-06-22 06:23:03 +00001621static int checkjobs(struct pipe* fg_pipe)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001622{
Eric Andersenc798b072001-06-22 06:23:03 +00001623 int attributes;
1624 int status;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001625#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00001626 int prognum = 0;
1627 struct pipe *pi;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001628#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001629 pid_t childpid;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001630 int rcode = 0;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001631
Eric Andersenc798b072001-06-22 06:23:03 +00001632 attributes = WUNTRACED;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001633 if (fg_pipe == NULL) {
Eric Andersenc798b072001-06-22 06:23:03 +00001634 attributes |= WNOHANG;
1635 }
1636
Denis Vlasenko1359da62007-04-21 23:27:30 +00001637/* Do we do this right?
1638 * bash-3.00# sleep 20 | false
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001639 * <ctrl-Z pressed>
Denis Vlasenko1359da62007-04-21 23:27:30 +00001640 * [3]+ Stopped sleep 20 | false
1641 * bash-3.00# echo $?
1642 * 1 <========== bg pipe is not fully done, but exitcode is already known!
1643 */
1644
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001645//FIXME: non-interactive bash does not continue even if all processes in fg pipe
1646//are stopped. Testcase: "cat | cat" in a script (not on command line)
1647// + killall -STOP cat
1648
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001649 wait_more:
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001650// TODO: safe_waitpid?
Eric Andersenc798b072001-06-22 06:23:03 +00001651 while ((childpid = waitpid(-1, &status, attributes)) > 0) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001652 const int dead = WIFEXITED(status) || WIFSIGNALED(status);
1653
1654#ifdef DEBUG_SHELL_JOBS
1655 if (WIFSTOPPED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001656 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001657 childpid, WSTOPSIG(status), WEXITSTATUS(status));
1658 if (WIFSIGNALED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001659 debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001660 childpid, WTERMSIG(status), WEXITSTATUS(status));
1661 if (WIFEXITED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001662 debug_printf_jobs("pid %d exited, exitcode %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001663 childpid, WEXITSTATUS(status));
1664#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001665 /* Were we asked to wait for fg pipe? */
Eric Andersenc798b072001-06-22 06:23:03 +00001666 if (fg_pipe) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001667 int i;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001668 for (i = 0; i < fg_pipe->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001669 debug_printf_jobs("check pid %d\n", fg_pipe->progs[i].pid);
Eric Andersenc798b072001-06-22 06:23:03 +00001670 if (fg_pipe->progs[i].pid == childpid) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001671 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001672 if (dead) {
1673 fg_pipe->progs[i].pid = 0;
1674 fg_pipe->running_progs--;
Denis Vlasenko05743d72008-02-10 12:10:08 +00001675 if (i == fg_pipe->num_progs - 1)
Denis Vlasenko1359da62007-04-21 23:27:30 +00001676 /* last process gives overall exitstatus */
1677 rcode = WEXITSTATUS(status);
1678 } else {
1679 fg_pipe->progs[i].is_stopped = 1;
1680 fg_pipe->stopped_progs++;
1681 }
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001682 debug_printf_jobs("fg_pipe: running_progs %d stopped_progs %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001683 fg_pipe->running_progs, fg_pipe->stopped_progs);
1684 if (fg_pipe->running_progs - fg_pipe->stopped_progs <= 0) {
1685 /* All processes in fg pipe have exited/stopped */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001686#if ENABLE_HUSH_JOB
Denis Vlasenko1359da62007-04-21 23:27:30 +00001687 if (fg_pipe->running_progs)
1688 insert_bg_job(fg_pipe);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001689#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001690 return rcode;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001691 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001692 /* There are still running processes in the fg pipe */
1693 goto wait_more;
Eric Andersenc798b072001-06-22 06:23:03 +00001694 }
1695 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001696 /* fall through to searching process in bg pipes */
Eric Andersenc798b072001-06-22 06:23:03 +00001697 }
1698
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001699#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001700 /* We asked to wait for bg or orphaned children */
1701 /* No need to remember exitcode in this case */
Eric Andersenc798b072001-06-22 06:23:03 +00001702 for (pi = job_list; pi; pi = pi->next) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001703 prognum = 0;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001704 while (prognum < pi->num_progs) {
1705 if (pi->progs[prognum].pid == childpid)
1706 goto found_pi_and_prognum;
Eric Andersen52a97ca2001-06-22 06:49:26 +00001707 prognum++;
1708 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001709 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001710#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001711
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001712 /* Happens when shell is used as init process (init=/bin/sh) */
1713 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001714 goto wait_more;
Eric Andersenaeb44c42001-05-22 20:29:00 +00001715
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001716#if ENABLE_HUSH_JOB
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001717 found_pi_and_prognum:
Denis Vlasenko1359da62007-04-21 23:27:30 +00001718 if (dead) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001719 /* child exited */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001720 pi->progs[prognum].pid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001721 pi->running_progs--;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001722 if (!pi->running_progs) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001723 printf(JOB_STATUS_FORMAT, pi->jobid,
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001724 "Done", pi->cmdtext);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001725 delete_finished_bg_job(pi);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001726 }
1727 } else {
1728 /* child stopped */
1729 pi->stopped_progs++;
1730 pi->progs[prognum].is_stopped = 1;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001731 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001732#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001733 }
1734
Denis Vlasenko1359da62007-04-21 23:27:30 +00001735 /* wait found no children or failed */
1736
1737 if (childpid && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001738 bb_perror_msg("waitpid");
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001739 return rcode;
Eric Andersenada18ff2001-05-21 16:18:22 +00001740}
1741
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001742#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +00001743static int checkjobs_and_fg_shell(struct pipe* fg_pipe)
1744{
1745 pid_t p;
1746 int rcode = checkjobs(fg_pipe);
1747 /* Job finished, move the shell to the foreground */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001748 p = getpgid(0); /* pgid of our process */
1749 debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001750 if (tcsetpgrp(interactive_fd, p) && errno != ENOTTY)
1751 bb_perror_msg("tcsetpgrp-4a");
1752 return rcode;
1753}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001754#endif
Denis Vlasenko52881e92007-04-21 13:42:52 +00001755
Denis Vlasenko05743d72008-02-10 12:10:08 +00001756/* run_pipe() starts all the jobs, but doesn't wait for anything
Eric Andersenc798b072001-06-22 06:23:03 +00001757 * to finish. See checkjobs().
Eric Andersen25f27032001-04-26 23:22:31 +00001758 *
1759 * return code is normally -1, when the caller has to wait for children
1760 * to finish to determine the exit status of the pipe. If the pipe
1761 * is a simple builtin command, however, the action is done by the
Denis Vlasenko05743d72008-02-10 12:10:08 +00001762 * time run_pipe returns, and the exit code is provided as the
Eric Andersen25f27032001-04-26 23:22:31 +00001763 * return value.
1764 *
1765 * The input of the pipe is always stdin, the output is always
1766 * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
1767 * because it tries to avoid running the command substitution in
1768 * subshell, when that is in fact necessary. The subshell process
1769 * now has its stdout directed to the input of the appropriate pipe,
1770 * so this routine is noticeably simpler.
Denis Vlasenko170435c2007-05-23 13:01:10 +00001771 *
1772 * Returns -1 only if started some children. IOW: we have to
1773 * mask out retvals of builtins etc with 0xff!
Eric Andersen25f27032001-04-26 23:22:31 +00001774 */
Denis Vlasenko05743d72008-02-10 12:10:08 +00001775static int run_pipe(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00001776{
1777 int i;
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001778 int nextin;
1779 int pipefds[2]; /* pipefds[0] is for reading */
Rob Landley53702e52006-07-19 21:43:53 +00001780 struct child_prog *child;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001781 const struct built_in_command *x;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001782 char *p;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001783 /* it is not always needed, but we aim to smaller code */
1784 int squirrel[] = { -1, -1, -1 };
1785 int rcode;
Denis Vlasenko52881e92007-04-21 13:42:52 +00001786 const int single_fg = (pi->num_progs == 1 && pi->followup != PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00001787
Denis Vlasenko05743d72008-02-10 12:10:08 +00001788 debug_printf_exec("run_pipe start: single_fg=%d\n", single_fg);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001789
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001790#if ENABLE_HUSH_JOB
Eric Andersenada18ff2001-05-21 16:18:22 +00001791 pi->pgrp = -1;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001792#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001793 pi->running_progs = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001794 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001795
1796 /* Check if this is a simple builtin (not part of a pipe).
1797 * Builtins within pipes have to fork anyway, and are handled in
1798 * pseudo_exec. "echo foo | read bar" doesn't work on bash, either.
1799 */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001800 child = &(pi->progs[0]);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001801 if (single_fg && child->group && child->subshell == 0) {
Eric Andersen04407e52001-06-07 16:42:05 +00001802 debug_printf("non-subshell grouping\n");
1803 setup_redirects(child, squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001804 debug_printf_exec(": run_list\n");
1805 rcode = run_list(child->group) & 0xff;
Eric Andersen04407e52001-06-07 16:42:05 +00001806 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001807 debug_printf_exec("run_pipe return %d\n", rcode);
1808 return rcode;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001809 }
1810
Denis Vlasenko1359da62007-04-21 23:27:30 +00001811 if (single_fg && child->argv != NULL) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001812 char **argv_expanded;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001813 char **argv = child->argv;
1814
1815 for (i = 0; is_assignment(argv[i]); i++)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001816 continue;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001817 if (i != 0 && argv[i] == NULL) {
Eric Andersen78a7c992001-05-15 16:30:25 +00001818 /* assignments, but no command: set the local environment */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001819 for (i = 0; argv[i] != NULL; i++) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001820 debug_printf("local environment set: %s\n", argv[i]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001821 p = expand_string_to_string(argv[i]);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001822 set_local_var(p, 0);
Eric Andersen78a7c992001-05-15 16:30:25 +00001823 }
1824 return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */
1825 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001826 for (i = 0; is_assignment(argv[i]); i++) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00001827 p = expand_string_to_string(argv[i]);
1828 //sp: child->sp--;
1829 putenv(p);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001830 }
Eric Andersen25f27032001-04-26 23:22:31 +00001831 for (x = bltins; x->cmd; x++) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001832 if (strcmp(argv[i], x->cmd) == 0) {
1833 if (x->function == builtin_exec && argv[i+1] == NULL) {
Eric Andersen83a2ae22001-05-07 17:59:25 +00001834 debug_printf("magic exec\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001835 setup_redirects(child, NULL);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001836 return EXIT_SUCCESS;
1837 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001838 debug_printf("builtin inline %s\n", argv[0]);
Eric Andersen25f27032001-04-26 23:22:31 +00001839 /* XXX setup_redirects acts on file descriptors, not FILEs.
1840 * This is perfect for work that comes after exec().
1841 * Is it really safe for inline use? Experimentally,
1842 * things seem to work with glibc. */
1843 setup_redirects(child, squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001844 debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv[i+1]);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001845 //sp: if (child->sp) /* btw we can do it unconditionally... */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001846 argv_expanded = expand_strvec_to_strvec(argv + i);
1847 rcode = x->function(argv_expanded) & 0xff;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001848 free(argv_expanded);
Eric Andersen25f27032001-04-26 23:22:31 +00001849 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001850 debug_printf_exec("run_pipe return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00001851 return rcode;
1852 }
1853 }
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001854#if ENABLE_FEATURE_SH_STANDALONE
1855 {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001856 int a = find_applet_by_name(argv[i]);
1857 if (a >= 0 && APPLET_IS_NOFORK(a)) {
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001858 setup_redirects(child, squirrel);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001859 save_nofork_data(&nofork_save);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001860 argv_expanded = argv + i;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001861 //sp: if (child->sp)
Denis Vlasenko170435c2007-05-23 13:01:10 +00001862 argv_expanded = expand_strvec_to_strvec(argv + i);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001863 debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001864 rcode = run_nofork_applet_prime(&nofork_save, a, argv_expanded) & 0xff;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001865 free(argv_expanded);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001866 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001867 debug_printf_exec("run_pipe return %d\n", rcode);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001868 return rcode;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001869 }
1870 }
1871#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001872 }
1873
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001874 /* Disable job control signals for shell (parent) and
1875 * for initial child code after fork */
1876 set_jobctrl_sighandler(SIG_IGN);
1877
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001878 /* Going to fork a child per each pipe member */
1879 pi->running_progs = 0;
1880 nextin = 0;
1881
Eric Andersen25f27032001-04-26 23:22:31 +00001882 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001883 child = &(pi->progs[i]);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001884 if (child->argv)
1885 debug_printf_exec(": pipe member '%s' '%s'...\n", child->argv[0], child->argv[1]);
1886 else
1887 debug_printf_exec(": pipe member with no argv\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001888
1889 /* pipes are inserted between pairs of commands */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001890 pipefds[0] = 0;
1891 pipefds[1] = 1;
1892 if ((i + 1) < pi->num_progs)
1893 xpipe(pipefds);
Eric Andersen25f27032001-04-26 23:22:31 +00001894
Denis Vlasenko05743d72008-02-10 12:10:08 +00001895 child->pid = BB_MMU ? fork() : vfork();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001896 if (!child->pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00001897 if (ENABLE_HUSH_JOB)
1898 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001899#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001900 /* Every child adds itself to new process group
Denis Vlasenko05743d72008-02-10 12:10:08 +00001901 * with pgid == pid_of_first_child_in_pipe */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001902 if (run_list_level == 1 && interactive_fd) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00001903 pid_t pgrp;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001904 /* Don't do pgrp restore anymore on fatal signals */
1905 set_fatal_sighandler(SIG_DFL);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001906 pgrp = pi->pgrp;
1907 if (pgrp < 0) /* true for 1st process only */
1908 pgrp = getpid();
1909 if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001910 /* We do it in *every* child, not just first,
1911 * to avoid races */
Denis Vlasenko05743d72008-02-10 12:10:08 +00001912 tcsetpgrp(interactive_fd, pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001913 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001914 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001915#endif
Denis Vlasenko8412d792007-10-01 09:59:47 +00001916 xmove_fd(nextin, 0);
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001917 xmove_fd(pipefds[1], 1); /* write end */
1918 if (pipefds[0] > 1)
1919 close(pipefds[0]); /* read end */
Eric Andersen25f27032001-04-26 23:22:31 +00001920 /* Like bash, explicit redirects override pipes,
1921 * and the pipe fd is available for dup'ing. */
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001922 setup_redirects(child, NULL);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001923
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001924 /* Restore default handlers just prior to exec */
1925 set_jobctrl_sighandler(SIG_DFL);
1926 set_misc_sighandler(SIG_DFL);
1927 signal(SIGCHLD, SIG_DFL);
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001928 pseudo_exec(child); /* does not return */
Eric Andersen25f27032001-04-26 23:22:31 +00001929 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001930
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001931 if (child->pid < 0) { /* [v]fork failed */
1932 /* Clearly indicate, was it fork or vfork */
Denis Vlasenko05743d72008-02-10 12:10:08 +00001933 bb_perror_msg(BB_MMU ? "fork" : "vfork");
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001934 } else {
1935 pi->running_progs++;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001936#if ENABLE_HUSH_JOB
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001937 /* Second and next children need to know pid of first one */
1938 if (pi->pgrp < 0)
1939 pi->pgrp = child->pid;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001940#endif
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001941 }
Eric Andersen25f27032001-04-26 23:22:31 +00001942
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001943 if (i)
1944 close(nextin);
1945 if ((i + 1) < pi->num_progs)
1946 close(pipefds[1]); /* write end */
1947 /* Pass read (output) pipe end to next iteration */
Eric Andersen25f27032001-04-26 23:22:31 +00001948 nextin = pipefds[0];
1949 }
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001950
Denis Vlasenko05743d72008-02-10 12:10:08 +00001951 if (!pi->running_progs) {
1952 debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n");
1953 return 1;
1954 }
1955
1956 debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->running_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00001957 return -1;
1958}
1959
Denis Vlasenko4b924f32007-05-30 00:29:55 +00001960#ifndef debug_print_tree
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001961static void debug_print_tree(struct pipe *pi, int lvl)
1962{
1963 static const char *PIPE[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001964 [PIPE_SEQ] = "SEQ",
1965 [PIPE_AND] = "AND",
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001966 [PIPE_OR ] = "OR" ,
1967 [PIPE_BG ] = "BG" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001968 };
1969 static const char *RES[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001970 [RES_NONE ] = "NONE" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00001971#if ENABLE_HUSH_IF
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001972 [RES_IF ] = "IF" ,
1973 [RES_THEN ] = "THEN" ,
1974 [RES_ELIF ] = "ELIF" ,
1975 [RES_ELSE ] = "ELSE" ,
1976 [RES_FI ] = "FI" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00001977#endif
1978#if ENABLE_HUSH_LOOPS
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001979 [RES_FOR ] = "FOR" ,
1980 [RES_WHILE] = "WHILE",
1981 [RES_UNTIL] = "UNTIL",
1982 [RES_DO ] = "DO" ,
1983 [RES_DONE ] = "DONE" ,
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001984 [RES_IN ] = "IN" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00001985#endif
1986 [RES_XXXX ] = "XXXX" ,
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001987 [RES_SNTX ] = "SNTX" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001988 };
1989
1990 int pin, prn;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00001991
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001992 pin = 0;
1993 while (pi) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00001994 fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "",
1995 pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001996 prn = 0;
1997 while (prn < pi->num_progs) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00001998 struct child_prog *child = &pi->progs[prn];
1999 char **argv = child->argv;
2000
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002001 fprintf(stderr, "%*s prog %d", lvl*2, "", prn);
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002002 if (child->group) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002003 fprintf(stderr, " group %s: (argv=%p)\n",
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002004 (child->subshell ? "()" : "{}"),
2005 argv);
2006 debug_print_tree(child->group, lvl+1);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002007 prn++;
2008 continue;
2009 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002010 if (argv) while (*argv) {
2011 fprintf(stderr, " '%s'", *argv);
2012 argv++;
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002013 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002014 fprintf(stderr, "\n");
2015 prn++;
2016 }
2017 pi = pi->next;
2018 pin++;
2019 }
2020}
2021#endif
2022
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002023/* NB: called by pseudo_exec, and therefore must not modify any
2024 * global data until exec/_exit (we can be a child after vfork!) */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002025static int run_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002026{
Denis Vlasenko06810332007-05-21 23:30:54 +00002027 struct pipe *rpipe;
2028#if ENABLE_HUSH_LOOPS
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002029 char *for_varname = NULL;
2030 char **for_lcur = NULL;
2031 char **for_list = NULL;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002032 int flag_rep = 0;
Denis Vlasenko06810332007-05-21 23:30:54 +00002033#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002034 int flag_skip = 1;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002035 int rcode = 0; /* probably for gcc only */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002036 int flag_restore = 0;
Denis Vlasenko06810332007-05-21 23:30:54 +00002037#if ENABLE_HUSH_IF
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002038 int if_code = 0, next_if_code = 0; /* need double-buffer to handle elif */
Denis Vlasenko06810332007-05-21 23:30:54 +00002039#else
2040 enum { if_code = 0, next_if_code = 0 };
2041#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002042 reserved_style rword;
2043 reserved_style skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002044
Denis Vlasenko05743d72008-02-10 12:10:08 +00002045 debug_printf_exec("run_list start lvl %d\n", run_list_level + 1);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002046
Denis Vlasenko06810332007-05-21 23:30:54 +00002047#if ENABLE_HUSH_LOOPS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002048 /* check syntax for "for" */
2049 for (rpipe = pi; rpipe; rpipe = rpipe->next) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002050 if ((rpipe->res_word == RES_IN || rpipe->res_word == RES_FOR)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002051 && (rpipe->next == NULL)
2052 ) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00002053 syntax("malformed for"); /* no IN or no commands after IN */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002054 debug_printf_exec("run_list lvl %d return 1\n", run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002055 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002056 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002057 if ((rpipe->res_word == RES_IN && rpipe->next->res_word == RES_IN && rpipe->next->progs[0].argv != NULL)
2058 || (rpipe->res_word == RES_FOR && rpipe->next->res_word != RES_IN)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002059 ) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002060 /* TODO: what is tested in the first condition? */
Denis Vlasenko90e485c2007-05-23 15:22:50 +00002061 syntax("malformed for"); /* 2nd condition: not followed by IN */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002062 debug_printf_exec("run_list lvl %d return 1\n", run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002063 return 1;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002064 }
2065 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002066#else
2067 rpipe = NULL;
2068#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002069
2070#if ENABLE_HUSH_JOB
2071 /* Example of nested list: "while true; do { sleep 1 | exit 2; } done".
2072 * We are saving state before entering outermost list ("while...done")
2073 * so that ctrl-Z will correctly background _entire_ outermost list,
2074 * not just a part of it (like "sleep 1 | exit 2") */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002075 if (++run_list_level == 1 && interactive_fd) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002076 if (sigsetjmp(toplevel_jb, 1)) {
2077 /* ctrl-Z forked and we are parent; or ctrl-C.
2078 * Sighandler has longjmped us here */
2079 signal(SIGINT, SIG_IGN);
2080 signal(SIGTSTP, SIG_IGN);
2081 /* Restore level (we can be coming from deep inside
2082 * nested levels) */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002083 run_list_level = 1;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002084#if ENABLE_FEATURE_SH_STANDALONE
2085 if (nofork_save.saved) { /* if save area is valid */
2086 debug_printf_jobs("exiting nofork early\n");
2087 restore_nofork_data(&nofork_save);
2088 }
2089#endif
2090 if (ctrl_z_flag) {
2091 /* ctrl-Z has forked and stored pid of the child in pi->pid.
2092 * Remember this child as background job */
2093 insert_bg_job(pi);
2094 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002095 /* ctrl-C. We just stop doing whatever we were doing */
Denis Vlasenko4daad902007-09-27 10:20:47 +00002096 bb_putchar('\n');
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002097 }
2098 rcode = 0;
2099 goto ret;
2100 }
2101 /* ctrl-Z handler will store pid etc in pi */
2102 toplevel_list = pi;
2103 ctrl_z_flag = 0;
2104#if ENABLE_FEATURE_SH_STANDALONE
2105 nofork_save.saved = 0; /* in case we will run a nofork later */
2106#endif
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +00002107 signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002108 signal(SIGINT, handler_ctrl_c);
2109 }
Denis Vlasenko05743d72008-02-10 12:10:08 +00002110#endif /* JOB */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002111
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002112 for (; pi; pi = flag_restore ? rpipe : pi->next) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00002113//why? int save_num_progs;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002114 rword = pi->res_word;
Denis Vlasenko06810332007-05-21 23:30:54 +00002115#if ENABLE_HUSH_LOOPS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002116 if (rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002117 flag_restore = 0;
2118 if (!rpipe) {
2119 flag_rep = 0;
2120 rpipe = pi;
2121 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002122 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002123#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002124 debug_printf_exec(": rword=%d if_code=%d next_if_code=%d skip_more=%d\n",
2125 rword, if_code, next_if_code, skip_more_for_this_rword);
2126 if (rword == skip_more_for_this_rword && flag_skip) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002127 if (pi->followup == PIPE_SEQ)
2128 flag_skip = 0;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002129 continue;
2130 }
2131 flag_skip = 1;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002132 skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko06810332007-05-21 23:30:54 +00002133#if ENABLE_HUSH_IF
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002134 if (rword == RES_THEN || rword == RES_ELSE)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002135 if_code = next_if_code;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002136 if (rword == RES_THEN && if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002137 continue;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002138 if (rword == RES_ELSE && !if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002139 continue;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002140 if (rword == RES_ELIF && !if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002141 break;
Denis Vlasenko06810332007-05-21 23:30:54 +00002142#endif
2143#if ENABLE_HUSH_LOOPS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002144 if (rword == RES_FOR && pi->num_progs) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002145 if (!for_lcur) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002146 /* first loop through for */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002147 /* if no variable values after "in" we skip "for" */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002148 if (!pi->next->progs->argv)
2149 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002150 /* create list of variable values */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002151 for_list = expand_strvec_to_strvec(pi->next->progs->argv);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002152 for_lcur = for_list;
2153 for_varname = pi->progs->argv[0];
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002154 pi->progs->argv[0] = NULL;
2155 flag_rep = 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002156 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002157 free(pi->progs->argv[0]);
2158 if (!*for_lcur) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002159 /* for loop is over, clean up */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002160 free(for_list);
2161 for_lcur = NULL;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002162 flag_rep = 0;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002163 pi->progs->argv[0] = for_varname;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002164 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002165 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002166 /* insert next value from for_lcur */
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002167 /* vda: does it need escaping? */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002168 pi->progs->argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002169 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002170 if (rword == RES_IN)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002171 continue;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002172 if (rword == RES_DO) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002173 if (!flag_rep)
2174 continue;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002175 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002176 if (rword == RES_DONE) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002177 if (flag_rep) {
2178 flag_restore = 1;
2179 } else {
2180 rpipe = NULL;
2181 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002182 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002183#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002184 if (pi->num_progs == 0)
2185 continue;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002186//why? save_num_progs = pi->num_progs;
2187 debug_printf_exec(": run_pipe with %d members\n", pi->num_progs);
2188 rcode = run_pipe(pi);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002189 if (rcode != -1) {
Eric Andersen25f27032001-04-26 23:22:31 +00002190 /* We only ran a builtin: rcode was set by the return value
Denis Vlasenko05743d72008-02-10 12:10:08 +00002191 * of run_pipe(), and we don't need to wait for anything. */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002192 } else if (pi->followup == PIPE_BG) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002193 /* What does bash do with attempts to background builtins? */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002194 /* Even bash 3.2 doesn't do that well with nested bg:
2195 * try "{ { sleep 10; echo DEEP; } & echo HERE; } &".
Denis Vlasenko170435c2007-05-23 13:01:10 +00002196 * I'm NOT treating inner &'s as jobs */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002197#if ENABLE_HUSH_JOB
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002198 if (run_list_level == 1)
Denis Vlasenko170435c2007-05-23 13:01:10 +00002199 insert_bg_job(pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002200#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002201 rcode = EXIT_SUCCESS;
2202 } else {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002203#if ENABLE_HUSH_JOB
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002204 if (run_list_level == 1 && interactive_fd) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00002205 /* waits for completion, then fg's main shell */
Denis Vlasenko52881e92007-04-21 13:42:52 +00002206 rcode = checkjobs_and_fg_shell(pi);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002207 } else
2208#endif
2209 {
Denis Vlasenko170435c2007-05-23 13:01:10 +00002210 /* this one just waits for completion */
Eric Andersenc798b072001-06-22 06:23:03 +00002211 rcode = checkjobs(pi);
Eric Andersen25f27032001-04-26 23:22:31 +00002212 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002213 debug_printf_exec(": checkjobs returned %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002214 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002215 debug_printf_exec(": setting last_return_code=%d\n", rcode);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002216 last_return_code = rcode;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002217//why? pi->num_progs = save_num_progs;
Denis Vlasenko06810332007-05-21 23:30:54 +00002218#if ENABLE_HUSH_IF
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002219 if (rword == RES_IF || rword == RES_ELIF)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002220 next_if_code = rcode; /* can be overwritten a number of times */
Denis Vlasenko06810332007-05-21 23:30:54 +00002221#endif
2222#if ENABLE_HUSH_LOOPS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002223 if (rword == RES_WHILE)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002224 flag_rep = !last_return_code;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002225 if (rword == RES_UNTIL)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002226 flag_rep = last_return_code;
Denis Vlasenko06810332007-05-21 23:30:54 +00002227#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002228 if ((rcode == EXIT_SUCCESS && pi->followup == PIPE_OR)
2229 || (rcode != EXIT_SUCCESS && pi->followup == PIPE_AND)
2230 ) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002231 skip_more_for_this_rword = rword;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002232 }
Eric Andersen028b65b2001-06-28 01:10:11 +00002233 checkjobs(NULL);
Eric Andersen25f27032001-04-26 23:22:31 +00002234 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002235
2236#if ENABLE_HUSH_JOB
2237 if (ctrl_z_flag) {
2238 /* ctrl-Z forked somewhere in the past, we are the child,
2239 * and now we completed running the list. Exit. */
2240 exit(rcode);
2241 }
2242 ret:
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00002243 if (!--run_list_level && interactive_fd) {
2244 signal(SIGTSTP, SIG_IGN);
2245 signal(SIGINT, SIG_IGN);
2246 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002247#endif
Denis Vlasenko05743d72008-02-10 12:10:08 +00002248 debug_printf_exec("run_list lvl %d return %d\n", run_list_level + 1, rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002249 return rcode;
2250}
2251
Eric Andersen25f27032001-04-26 23:22:31 +00002252/* return code is the exit status of the pipe */
Eric Andersenbf7df042001-05-23 22:18:35 +00002253static int free_pipe(struct pipe *pi, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002254{
2255 char **p;
2256 struct child_prog *child;
2257 struct redir_struct *r, *rnext;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002258 int a, i, ret_code = 0;
Eric Andersen52a97ca2001-06-22 06:49:26 +00002259
2260 if (pi->stopped_progs > 0)
2261 return ret_code;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002262 debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid());
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002263 for (i = 0; i < pi->num_progs; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002264 child = &pi->progs[i];
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002265 debug_printf_clean("%s command %d:\n", indenter(indent), i);
Eric Andersen25f27032001-04-26 23:22:31 +00002266 if (child->argv) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002267 for (a = 0, p = child->argv; *p; a++, p++) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002268 debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p);
Eric Andersen25f27032001-04-26 23:22:31 +00002269 }
Denis Vlasenkof962a032007-11-23 12:50:54 +00002270 free_strings(child->argv);
2271 child->argv = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002272 } else if (child->group) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002273 debug_printf_clean("%s begin group (subshell:%d)\n", indenter(indent), child->subshell);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002274 ret_code = free_pipe_list(child->group, indent+3);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002275 debug_printf_clean("%s end group\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002276 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002277 debug_printf_clean("%s (nil)\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002278 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002279 for (r = child->redirects; r; r = rnext) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002280 debug_printf_clean("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->type].descrip);
Eric Andersen25f27032001-04-26 23:22:31 +00002281 if (r->dup == -1) {
Eric Andersen817e73c2001-06-06 17:56:09 +00002282 /* guard against the case >$FOO, where foo is unset or blank */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002283 if (r->glob_word) {
2284 debug_printf_clean(" %s\n", r->glob_word[0]);
2285 free_strings(r->glob_word);
2286 r->glob_word = NULL;
Eric Andersen817e73c2001-06-06 17:56:09 +00002287 }
Eric Andersen25f27032001-04-26 23:22:31 +00002288 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002289 debug_printf_clean("&%d\n", r->dup);
Eric Andersen25f27032001-04-26 23:22:31 +00002290 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002291 rnext = r->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002292 free(r);
2293 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002294 child->redirects = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002295 }
2296 free(pi->progs); /* children are an array, they get freed all at once */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002297 pi->progs = NULL;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002298#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002299 free(pi->cmdtext);
2300 pi->cmdtext = NULL;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002301#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002302 return ret_code;
2303}
2304
Eric Andersenbf7df042001-05-23 22:18:35 +00002305static int free_pipe_list(struct pipe *head, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002306{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002307 int rcode = 0; /* if list has no members */
Eric Andersen25f27032001-04-26 23:22:31 +00002308 struct pipe *pi, *next;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002309
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002310 for (pi = head; pi; pi = next) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002311 debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word);
Eric Andersenbf7df042001-05-23 22:18:35 +00002312 rcode = free_pipe(pi, indent);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002313 debug_printf_clean("%s pipe followup code %d\n", indenter(indent), pi->followup);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002314 next = pi->next;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002315 /*pi->next = NULL;*/
Eric Andersen25f27032001-04-26 23:22:31 +00002316 free(pi);
2317 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002318 return rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00002319}
2320
2321/* Select which version we will use */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002322static int run_and_free_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002323{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002324 int rcode = 0;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002325 debug_printf_exec("run_and_free_list entered\n");
2326 if (!fake_mode) {
2327 debug_printf_exec(": run_list with %d members\n", pi->num_progs);
2328 rcode = run_list(pi);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002329 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002330 /* free_pipe_list has the side effect of clearing memory.
Denis Vlasenko05743d72008-02-10 12:10:08 +00002331 * In the long run that function can be merged with run_list,
Eric Andersen25f27032001-04-26 23:22:31 +00002332 * but doing that now would hobble the debugging effort. */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002333 free_pipe_list(pi, /* indent: */ 0);
2334 debug_printf_exec("run_nad_free_list return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002335 return rcode;
2336}
2337
Denis Vlasenkoff097622007-10-01 10:00:45 +00002338/* Whoever decided to muck with glob internal data is AN IDIOT! */
2339/* uclibc happily changed the way it works (and it has rights to do so!),
2340 all hell broke loose (SEGVs) */
2341
Eric Andersen25f27032001-04-26 23:22:31 +00002342/* The API for glob is arguably broken. This routine pushes a non-matching
2343 * string into the output structure, removing non-backslashed backslashes.
2344 * If someone can prove me wrong, by performing this function within the
2345 * original glob(3) api, feel free to rewrite this routine into oblivion.
Eric Andersen25f27032001-04-26 23:22:31 +00002346 * XXX broken if the last character is '\\', check that before calling.
2347 */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002348static char **globhack(const char *src, char **strings)
Eric Andersen25f27032001-04-26 23:22:31 +00002349{
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002350 int cnt;
Eric Andersen25f27032001-04-26 23:22:31 +00002351 const char *s;
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002352 char *v, *dest;
2353
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002354 for (cnt = 1, s = src; s && *s; s++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002355 if (*s == '\\') s++;
2356 cnt++;
2357 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002358 v = dest = xmalloc(cnt);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002359 for (s = src; s && *s; s++, dest++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002360 if (*s == '\\') s++;
2361 *dest = *s;
2362 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002363 *dest = '\0';
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002364
2365 return add_string_to_strings(strings, v);
Eric Andersen25f27032001-04-26 23:22:31 +00002366}
2367
2368/* XXX broken if the last character is '\\', check that before calling */
2369static int glob_needed(const char *s)
2370{
2371 for (; *s; s++) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002372 if (*s == '\\')
2373 s++;
2374 if (strchr("*[?", *s))
2375 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002376 }
2377 return 0;
2378}
2379
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002380static int xglob(o_string *dest, char ***pglob)
Eric Andersen25f27032001-04-26 23:22:31 +00002381{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002382 /* short-circuit for null word */
Eric Andersen25f27032001-04-26 23:22:31 +00002383 /* we can code this better when the debug_printf's are gone */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002384 if (dest->length == 0) {
2385 if (dest->nonnull) {
2386 /* bash man page calls this an "explicit" null */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002387 *pglob = globhack(dest->data, *pglob);
2388 }
2389 return 0;
2390 }
2391
2392 if (glob_needed(dest->data)) {
2393 glob_t globdata;
2394 int gr;
2395
2396 memset(&globdata, 0, sizeof(globdata));
2397 gr = glob(dest->data, 0, NULL, &globdata);
2398 debug_printf("glob returned %d\n", gr);
2399 if (gr == GLOB_NOSPACE)
2400 bb_error_msg_and_die("out of memory during glob");
2401 if (gr == GLOB_NOMATCH) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002402 debug_printf("globhack returned %d\n", gr);
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002403 /* quote removal, or more accurately, backslash removal */
2404 *pglob = globhack(dest->data, *pglob);
Denis Vlasenkof962a032007-11-23 12:50:54 +00002405 globfree(&globdata);
Eric Andersen25f27032001-04-26 23:22:31 +00002406 return 0;
2407 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002408 if (gr != 0) { /* GLOB_ABORTED ? */
2409 bb_error_msg("glob(3) error %d", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002410 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002411 if (globdata.gl_pathv && globdata.gl_pathv[0])
2412 *pglob = add_strings_to_strings(1, *pglob, globdata.gl_pathv);
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002413 globfree(&globdata);
2414 return gr;
Eric Andersen25f27032001-04-26 23:22:31 +00002415 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002416
2417 *pglob = globhack(dest->data, *pglob);
2418 return 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002419}
2420
Denis Vlasenko170435c2007-05-23 13:01:10 +00002421/* expand_strvec_to_strvec() takes a list of strings, expands
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002422 * all variable references within and returns a pointer to
2423 * a list of expanded strings, possibly with larger number
2424 * of strings. (Think VAR="a b"; echo $VAR).
2425 * This new list is allocated as a single malloc block.
2426 * NULL-terminated list of char* pointers is at the beginning of it,
2427 * followed by strings themself.
2428 * Caller can deallocate entire list by single free(list). */
2429
2430/* Helpers first:
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00002431 * count_XXX estimates size of the block we need. It's okay
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002432 * to over-estimate sizes a bit, if it makes code simpler */
2433static int count_ifs(const char *str)
2434{
2435 int cnt = 0;
2436 debug_printf_expand("count_ifs('%s') ifs='%s'", str, ifs);
2437 while (1) {
2438 str += strcspn(str, ifs);
2439 if (!*str) break;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002440 str++; /* str += strspn(str, ifs); */
2441 cnt++; /* cnt += strspn(str, ifs); - but this code is larger */
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002442 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002443 debug_printf_expand(" return %d\n", cnt);
2444 return cnt;
2445}
2446
2447static void count_var_expansion_space(int *countp, int *lenp, char *arg)
2448{
2449 char first_ch;
2450 int i;
2451 int len = *lenp;
2452 int count = *countp;
2453 const char *val;
2454 char *p;
2455
2456 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL))) {
2457 len += p - arg;
2458 arg = ++p;
2459 p = strchr(p, SPECIAL_VAR_SYMBOL);
2460 first_ch = arg[0];
2461
2462 switch (first_ch & 0x7f) {
2463 /* high bit in 1st_ch indicates that var is double-quoted */
2464 case '$': /* pid */
2465 case '!': /* bg pid */
2466 case '?': /* exitcode */
2467 case '#': /* argc */
2468 len += sizeof(int)*3 + 1; /* enough for int */
2469 break;
2470 case '*':
2471 case '@':
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002472 for (i = 1; global_argv[i]; i++) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002473 len += strlen(global_argv[i]) + 1;
2474 count++;
2475 if (!(first_ch & 0x80))
2476 count += count_ifs(global_argv[i]);
2477 }
2478 break;
2479 default:
2480 *p = '\0';
2481 arg[0] = first_ch & 0x7f;
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002482 if (isdigit(arg[0])) {
2483 i = xatoi_u(arg);
2484 val = NULL;
2485 if (i < global_argc)
2486 val = global_argv[i];
2487 } else
2488 val = lookup_param(arg);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002489 arg[0] = first_ch;
2490 *p = SPECIAL_VAR_SYMBOL;
2491
2492 if (val) {
2493 len += strlen(val) + 1;
2494 if (!(first_ch & 0x80))
2495 count += count_ifs(val);
2496 }
2497 }
2498 arg = ++p;
2499 }
2500
2501 len += strlen(arg) + 1;
2502 count++;
2503 *lenp = len;
2504 *countp = count;
2505}
2506
2507/* Store given string, finalizing the word and starting new one whenever
2508 * we encounter ifs char(s). This is used for expanding variable values.
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002509 * End-of-string does NOT finalize word: think about 'echo -$VAR-' */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002510static int expand_on_ifs(char **list, int n, char **posp, const char *str)
2511{
2512 char *pos = *posp;
2513 while (1) {
2514 int word_len = strcspn(str, ifs);
2515 if (word_len) {
2516 memcpy(pos, str, word_len); /* store non-ifs chars */
2517 pos += word_len;
2518 str += word_len;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002519 }
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002520 if (!*str) /* EOL - do not finalize word */
2521 break;
2522 *pos++ = '\0';
2523 if (n) debug_printf_expand("expand_on_ifs finalized list[%d]=%p '%s' "
2524 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2525 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2526 list[n++] = pos;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002527 str += strspn(str, ifs); /* skip ifs chars */
2528 }
2529 *posp = pos;
2530 return n;
2531}
2532
2533/* Expand all variable references in given string, adding words to list[]
2534 * at n, n+1,... positions. Return updated n (so that list[n] is next one
2535 * to be filled). This routine is extremely tricky: has to deal with
2536 * variables/parameters with whitespace, $* and $@, and constructs like
2537 * 'echo -$*-'. If you play here, you must run testsuite afterwards! */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002538/* NB: another bug is that we cannot detect empty strings yet:
2539 * "" or $empty"" expands to zero words, has to expand to empty word */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002540static int expand_vars_to_list(char **list, int n, char **posp, char *arg, char or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002541{
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002542 /* or_mask is either 0 (normal case) or 0x80
2543 * (expansion of right-hand side of assignment == 1-element expand) */
2544
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002545 char first_ch, ored_ch;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002546 int i;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002547 const char *val;
2548 char *p;
2549 char *pos = *posp;
2550
2551 ored_ch = 0;
2552
2553 if (n) debug_printf_expand("expand_vars_to_list finalized list[%d]=%p '%s' "
2554 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2555 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2556 list[n++] = pos;
2557
2558 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL))) {
2559 memcpy(pos, arg, p - arg);
2560 pos += (p - arg);
2561 arg = ++p;
2562 p = strchr(p, SPECIAL_VAR_SYMBOL);
2563
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002564 first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002565 ored_ch |= first_ch;
2566 val = NULL;
2567 switch (first_ch & 0x7f) {
2568 /* Highest bit in first_ch indicates that var is double-quoted */
2569 case '$': /* pid */
2570 /* FIXME: (echo $$) should still print pid of main shell */
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00002571 val = utoa(getpid()); /* rootpid? */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002572 break;
2573 case '!': /* bg pid */
2574 val = last_bg_pid ? utoa(last_bg_pid) : (char*)"";
2575 break;
2576 case '?': /* exitcode */
2577 val = utoa(last_return_code);
2578 break;
2579 case '#': /* argc */
2580 val = utoa(global_argc ? global_argc-1 : 0);
2581 break;
2582 case '*':
2583 case '@':
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002584 i = 1;
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002585 if (!global_argv[i])
2586 break;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002587 if (!(first_ch & 0x80)) { /* unquoted $* or $@ */
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002588 while (global_argv[i]) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002589 n = expand_on_ifs(list, n, &pos, global_argv[i]);
2590 debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, global_argc-1);
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002591 if (global_argv[i++][0] && global_argv[i]) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002592 /* this argv[] is not empty and not last:
2593 * put terminating NUL, start new word */
2594 *pos++ = '\0';
2595 if (n) debug_printf_expand("expand_vars_to_list 2 finalized list[%d]=%p '%s' "
2596 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2597 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2598 list[n++] = pos;
2599 }
2600 }
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002601 } else
2602 /* If or_mask is nonzero, we handle assignment 'a=....$@.....'
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00002603 * and in this case should treat it like '$*' - see 'else...' below */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002604 if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002605 while (1) {
2606 strcpy(pos, global_argv[i]);
2607 pos += strlen(global_argv[i]);
2608 if (++i >= global_argc)
2609 break;
2610 *pos++ = '\0';
2611 if (n) debug_printf_expand("expand_vars_to_list 3 finalized list[%d]=%p '%s' "
2612 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2613 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2614 list[n++] = pos;
2615 }
2616 } else { /* quoted $*: add as one word */
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002617 while (1) {
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002618 strcpy(pos, global_argv[i]);
2619 pos += strlen(global_argv[i]);
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00002620 if (!global_argv[++i])
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002621 break;
2622 if (ifs[0])
2623 *pos++ = ifs[0];
2624 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002625 }
2626 break;
2627 default:
2628 *p = '\0';
2629 arg[0] = first_ch & 0x7f;
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002630 if (isdigit(arg[0])) {
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002631 i = xatoi_u(arg);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002632 val = NULL;
2633 if (i < global_argc)
2634 val = global_argv[i];
2635 } else
2636 val = lookup_param(arg);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002637 arg[0] = first_ch;
2638 *p = SPECIAL_VAR_SYMBOL;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002639 if (!(first_ch & 0x80)) { /* unquoted $VAR */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002640 if (val) {
2641 n = expand_on_ifs(list, n, &pos, val);
2642 val = NULL;
2643 }
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002644 } /* else: quoted $VAR, val will be appended at pos */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002645 }
2646 if (val) {
2647 strcpy(pos, val);
2648 pos += strlen(val);
2649 }
2650 arg = ++p;
2651 }
2652 debug_printf_expand("expand_vars_to_list adding tail '%s' at %p\n", arg, pos);
2653 strcpy(pos, arg);
2654 pos += strlen(arg) + 1;
2655 if (pos == list[n-1] + 1) { /* expansion is empty */
2656 if (!(ored_ch & 0x80)) { /* all vars were not quoted... */
2657 debug_printf_expand("expand_vars_to_list list[%d] empty, going back\n", n);
2658 pos--;
2659 n--;
2660 }
2661 }
2662
2663 *posp = pos;
2664 return n;
2665}
2666
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002667static char **expand_variables(char **argv, char or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002668{
2669 int n;
2670 int count = 1;
2671 int len = 0;
2672 char *pos, **v, **list;
2673
2674 v = argv;
2675 if (!*v) debug_printf_expand("count_var_expansion_space: "
2676 "argv[0]=NULL count=%d len=%d alloc_space=%d\n",
2677 count, len, sizeof(char*) * count + len);
2678 while (*v) {
2679 count_var_expansion_space(&count, &len, *v);
2680 debug_printf_expand("count_var_expansion_space: "
2681 "'%s' count=%d len=%d alloc_space=%d\n",
2682 *v, count, len, sizeof(char*) * count + len);
2683 v++;
2684 }
2685 len += sizeof(char*) * count; /* total to alloc */
2686 list = xmalloc(len);
2687 pos = (char*)(list + count);
2688 debug_printf_expand("list=%p, list[0] should be %p\n", list, pos);
2689 n = 0;
2690 v = argv;
2691 while (*v)
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002692 n = expand_vars_to_list(list, n, &pos, *v++, or_mask);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002693
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002694 if (n) debug_printf_expand("finalized list[%d]=%p '%s' "
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002695 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2696 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002697 list[n] = NULL;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002698
2699#ifdef DEBUG_EXPAND
2700 {
2701 int m = 0;
2702 while (m <= n) {
2703 debug_printf_expand("list[%d]=%p '%s'\n", m, list[m], list[m]);
2704 m++;
2705 }
2706 debug_printf_expand("used_space=%d\n", pos - (char*)list);
2707 }
2708#endif
Denis Vlasenko170435c2007-05-23 13:01:10 +00002709 if (ENABLE_HUSH_DEBUG)
2710 if (pos - (char*)list > len)
2711 bb_error_msg_and_die("BUG in varexp");
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002712 return list;
2713}
2714
Denis Vlasenko170435c2007-05-23 13:01:10 +00002715static char **expand_strvec_to_strvec(char **argv)
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002716{
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002717 return expand_variables(argv, 0);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002718}
2719
Denis Vlasenko170435c2007-05-23 13:01:10 +00002720static char *expand_string_to_string(const char *str)
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002721{
2722 char *argv[2], **list;
2723
2724 argv[0] = (char*)str;
2725 argv[1] = NULL;
2726 list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002727 if (ENABLE_HUSH_DEBUG)
2728 if (!list[0] || list[1])
2729 bb_error_msg_and_die("BUG in varexp2");
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002730 /* actually, just move string 2*sizeof(char*) bytes back */
2731 strcpy((char*)list, list[0]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00002732 debug_printf_expand("string_to_string='%s'\n", (char*)list);
2733 return (char*)list;
2734}
2735
2736static char* expand_strvec_to_string(char **argv)
2737{
2738 char **list;
2739
2740 list = expand_variables(argv, 0x80);
2741 /* Convert all NULs to spaces */
2742 if (list[0]) {
2743 int n = 1;
2744 while (list[n]) {
2745 if (ENABLE_HUSH_DEBUG)
2746 if (list[n-1] + strlen(list[n-1]) + 1 != list[n])
2747 bb_error_msg_and_die("BUG in varexp3");
2748 list[n][-1] = ' '; /* TODO: or to ifs[0]? */
2749 n++;
2750 }
2751 }
2752 strcpy((char*)list, list[0]);
2753 debug_printf_expand("strvec_to_string='%s'\n", (char*)list);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002754 return (char*)list;
2755}
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002756
Eric Andersenf72f5622001-05-15 23:21:41 +00002757/* This is used to get/check local shell variables */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002758static struct variable *get_local_var(const char *name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002759{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002760 struct variable *cur;
2761 int len;
Eric Andersenf72f5622001-05-15 23:21:41 +00002762
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002763 if (!name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002764 return NULL;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002765 len = strlen(name);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002766 for (cur = top_var; cur; cur = cur->next) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002767 if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=')
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002768 return cur;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002769 }
Eric Andersenf72f5622001-05-15 23:21:41 +00002770 return NULL;
2771}
2772
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002773/* str holds "NAME=VAL" and is expected to be malloced.
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002774 * We take ownership of it. */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002775static int set_local_var(char *str, int flg_export)
Eric Andersen78a7c992001-05-15 16:30:25 +00002776{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002777 struct variable *cur;
2778 char *value;
2779 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002780
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002781 value = strchr(str, '=');
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002782 if (!value) { /* not expected to ever happen? */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002783 free(str);
Eric Andersen99785762001-05-22 21:37:48 +00002784 return -1;
2785 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002786
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002787 name_len = value - str + 1; /* including '=' */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002788 cur = top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */
2789 while (1) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002790 if (strncmp(cur->varstr, str, name_len) != 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002791 if (!cur->next) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002792 /* Bail out. Note that now cur points
2793 * to last var in linked list */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002794 break;
Eric Andersen20a69a72001-05-15 17:24:44 +00002795 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002796 cur = cur->next;
2797 continue;
Eric Andersen20a69a72001-05-15 17:24:44 +00002798 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002799 /* We found an existing var with this name */
2800 *value = '\0';
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002801 if (cur->flg_read_only) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002802 bb_error_msg("%s: readonly variable", str);
2803 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002804 return -1;
2805 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002806 unsetenv(str); /* just in case */
2807 *value = '=';
2808 if (strcmp(cur->varstr, str) == 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002809 free_and_exp:
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002810 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002811 goto exp;
2812 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002813 if (cur->max_len >= strlen(str)) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002814 /* This one is from startup env, reuse space */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002815 strcpy(cur->varstr, str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002816 goto free_and_exp;
2817 }
2818 /* max_len == 0 signifies "malloced" var, which we can
2819 * (and has to) free */
2820 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002821 free(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002822 cur->max_len = 0;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002823 goto set_str_and_exp;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002824 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002825
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002826 /* Not found - create next variable struct */
2827 cur->next = xzalloc(sizeof(*cur));
2828 cur = cur->next;
2829
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002830 set_str_and_exp:
2831 cur->varstr = str;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002832 exp:
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002833 if (flg_export)
2834 cur->flg_export = 1;
2835 if (cur->flg_export)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002836 return putenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002837 return 0;
Eric Andersen20a69a72001-05-15 17:24:44 +00002838}
2839
Eric Andersenf72f5622001-05-15 23:21:41 +00002840static void unset_local_var(const char *name)
Eric Andersen20a69a72001-05-15 17:24:44 +00002841{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002842 struct variable *cur;
2843 struct variable *prev = prev; /* for gcc */
2844 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002845
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002846 if (!name)
2847 return;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002848 name_len = strlen(name);
2849 cur = top_var;
2850 while (cur) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002851 if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002852 if (cur->flg_read_only) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002853 bb_error_msg("%s: readonly variable", name);
Eric Andersen94ac2442001-05-22 19:05:18 +00002854 return;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002855 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002856 /* prev is ok to use here because 1st variable, HUSH_VERSION,
2857 * is ro, and we cannot reach this code on the 1st pass */
2858 prev->next = cur->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002859 unsetenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002860 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002861 free(cur->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002862 free(cur);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002863 return;
Eric Andersenf72f5622001-05-15 23:21:41 +00002864 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002865 prev = cur;
2866 cur = cur->next;
Eric Andersen20a69a72001-05-15 17:24:44 +00002867 }
Eric Andersen78a7c992001-05-15 16:30:25 +00002868}
2869
2870static int is_assignment(const char *s)
2871{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002872 if (!s || !isalpha(*s))
2873 return 0;
2874 s++;
2875 while (isalnum(*s) || *s == '_')
2876 s++;
2877 return *s == '=';
Eric Andersen78a7c992001-05-15 16:30:25 +00002878}
2879
Eric Andersen25f27032001-04-26 23:22:31 +00002880/* the src parameter allows us to peek forward to a possible &n syntax
2881 * for file descriptor duplication, e.g., "2>&1".
2882 * Return code is 0 normally, 1 if a syntax error is detected in src.
2883 * Resource errors (in xmalloc) cause the process to exit */
2884static int setup_redirect(struct p_context *ctx, int fd, redir_type style,
2885 struct in_str *input)
2886{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002887 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00002888 struct redir_struct *redir = child->redirects;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002889 struct redir_struct *last_redir = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002890
2891 /* Create a new redir_struct and drop it onto the end of the linked list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002892 while (redir) {
2893 last_redir = redir;
2894 redir = redir->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002895 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00002896 redir = xzalloc(sizeof(struct redir_struct));
2897 /* redir->next = NULL; */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002898 /* redir->glob_word = NULL; */
Eric Andersen25f27032001-04-26 23:22:31 +00002899 if (last_redir) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002900 last_redir->next = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002901 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002902 child->redirects = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002903 }
2904
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002905 redir->type = style;
2906 redir->fd = (fd == -1) ? redir_table[style].default_fd : fd;
Eric Andersen25f27032001-04-26 23:22:31 +00002907
2908 debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
2909
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002910 /* Check for a '2>&1' type redirect */
Eric Andersen25f27032001-04-26 23:22:31 +00002911 redir->dup = redirect_dup_num(input);
2912 if (redir->dup == -2) return 1; /* syntax error */
2913 if (redir->dup != -1) {
2914 /* Erik had a check here that the file descriptor in question
Eric Andersen83a2ae22001-05-07 17:59:25 +00002915 * is legit; I postpone that to "run time"
2916 * A "-" representation of "close me" shows up as a -3 here */
Eric Andersen25f27032001-04-26 23:22:31 +00002917 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
2918 } else {
2919 /* We do _not_ try to open the file that src points to,
2920 * since we need to return and let src be expanded first.
2921 * Set ctx->pending_redirect, so we know what to do at the
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002922 * end of the next parsed word. */
Eric Andersen25f27032001-04-26 23:22:31 +00002923 ctx->pending_redirect = redir;
2924 }
2925 return 0;
2926}
2927
Denis Vlasenkoac678ec2007-04-16 22:32:04 +00002928static struct pipe *new_pipe(void)
2929{
Eric Andersen25f27032001-04-26 23:22:31 +00002930 struct pipe *pi;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002931 pi = xzalloc(sizeof(struct pipe));
2932 /*pi->num_progs = 0;*/
2933 /*pi->progs = NULL;*/
2934 /*pi->next = NULL;*/
2935 /*pi->followup = 0; invalid */
2936 if (RES_NONE)
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002937 pi->res_word = RES_NONE;
Eric Andersen25f27032001-04-26 23:22:31 +00002938 return pi;
2939}
2940
2941static void initialize_context(struct p_context *ctx)
2942{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002943 ctx->child = NULL;
Denis Vlasenko1a735862007-05-23 00:32:25 +00002944 ctx->pipe = ctx->list_head = new_pipe();
2945 ctx->pending_redirect = NULL;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002946 ctx->res_w = RES_NONE;
Denis Vlasenko1a735862007-05-23 00:32:25 +00002947 //only ctx->parse_type is not touched... is this intentional?
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002948 ctx->old_flag = 0;
Denis Vlasenko1a735862007-05-23 00:32:25 +00002949 ctx->stack = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002950 done_command(ctx); /* creates the memory for working child */
2951}
2952
2953/* normal return is 0
2954 * if a reserved word is found, and processed, return 1
2955 * should handle if, then, elif, else, fi, for, while, until, do, done.
2956 * case, function, and select are obnoxious, save those for later.
2957 */
Denis Vlasenko06810332007-05-21 23:30:54 +00002958#if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00002959static int reserved_word(o_string *dest, struct p_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00002960{
2961 struct reserved_combo {
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002962 char literal[7];
2963 unsigned char code;
2964 int flag;
Eric Andersen25f27032001-04-26 23:22:31 +00002965 };
2966 /* Mostly a list of accepted follow-up reserved words.
2967 * FLAG_END means we are done with the sequence, and are ready
2968 * to turn the compound list into a command.
2969 * FLAG_START means the word must start a new compound list.
2970 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002971 static const struct reserved_combo reserved_list[] = {
Denis Vlasenko06810332007-05-21 23:30:54 +00002972#if ENABLE_HUSH_IF
Eric Andersen25f27032001-04-26 23:22:31 +00002973 { "if", RES_IF, FLAG_THEN | FLAG_START },
2974 { "then", RES_THEN, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
2975 { "elif", RES_ELIF, FLAG_THEN },
2976 { "else", RES_ELSE, FLAG_FI },
2977 { "fi", RES_FI, FLAG_END },
Denis Vlasenko06810332007-05-21 23:30:54 +00002978#endif
2979#if ENABLE_HUSH_LOOPS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002980 { "for", RES_FOR, FLAG_IN | FLAG_START },
Eric Andersen25f27032001-04-26 23:22:31 +00002981 { "while", RES_WHILE, FLAG_DO | FLAG_START },
2982 { "until", RES_UNTIL, FLAG_DO | FLAG_START },
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002983 { "in", RES_IN, FLAG_DO },
Eric Andersen25f27032001-04-26 23:22:31 +00002984 { "do", RES_DO, FLAG_DONE },
2985 { "done", RES_DONE, FLAG_END }
Denis Vlasenko06810332007-05-21 23:30:54 +00002986#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002987 };
Denis Vlasenko80b8b392007-06-25 10:55:35 +00002988
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002989 const struct reserved_combo *r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002990
Denis Vlasenko80b8b392007-06-25 10:55:35 +00002991 for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) {
Denis Vlasenko1a735862007-05-23 00:32:25 +00002992 if (strcmp(dest->data, r->literal) != 0)
2993 continue;
2994 debug_printf("found reserved word %s, code %d\n", r->literal, r->code);
2995 if (r->flag & FLAG_START) {
2996 struct p_context *new;
2997 debug_printf("push stack\n");
Denis Vlasenko06810332007-05-21 23:30:54 +00002998#if ENABLE_HUSH_LOOPS
Denis Vlasenko1a735862007-05-23 00:32:25 +00002999 if (ctx->res_w == RES_IN || ctx->res_w == RES_FOR) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003000 syntax("malformed for"); /* example: 'for if' */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003001 ctx->res_w = RES_SNTX;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003002 b_reset(dest);
Eric Andersenaf44a0e2001-04-27 07:26:12 +00003003 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003004 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003005#endif
3006 new = xmalloc(sizeof(*new));
3007 *new = *ctx; /* physical copy */
3008 initialize_context(ctx);
3009 ctx->stack = new;
3010 } else if (ctx->res_w == RES_NONE || !(ctx->old_flag & (1 << r->code))) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003011 syntax(NULL);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003012 ctx->res_w = RES_SNTX;
Denis Vlasenkoff131b92007-04-10 15:42:06 +00003013 b_reset(dest);
Eric Andersen25f27032001-04-26 23:22:31 +00003014 return 1;
3015 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003016 ctx->res_w = r->code;
3017 ctx->old_flag = r->flag;
3018 if (ctx->old_flag & FLAG_END) {
3019 struct p_context *old;
3020 debug_printf("pop stack\n");
3021 done_pipe(ctx, PIPE_SEQ);
3022 old = ctx->stack;
3023 old->child->group = ctx->list_head;
3024 old->child->subshell = 0;
3025 *ctx = *old; /* physical copy */
3026 free(old);
3027 }
3028 b_reset(dest);
3029 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003030 }
3031 return 0;
3032}
Denis Vlasenko06810332007-05-21 23:30:54 +00003033#else
3034#define reserved_word(dest, ctx) ((int)0)
3035#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003036
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003037/* Normal return is 0.
Eric Andersen25f27032001-04-26 23:22:31 +00003038 * Syntax or xglob errors return 1. */
3039static int done_word(o_string *dest, struct p_context *ctx)
3040{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003041 struct child_prog *child = ctx->child;
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003042 char ***glob_target;
3043 int gr;
Eric Andersen25f27032001-04-26 23:22:31 +00003044
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003045 debug_printf_parse("done_word entered: '%s' %p\n", dest->data, child);
Eric Andersen25f27032001-04-26 23:22:31 +00003046 if (dest->length == 0 && !dest->nonnull) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003047 debug_printf_parse("done_word return 0: true null, ignored\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003048 return 0;
3049 }
3050 if (ctx->pending_redirect) {
Denis Vlasenkoff097622007-10-01 10:00:45 +00003051 glob_target = &ctx->pending_redirect->glob_word;
Eric Andersen25f27032001-04-26 23:22:31 +00003052 } else {
3053 if (child->group) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003054 syntax(NULL);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003055 debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n");
3056 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003057 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003058 if (!child->argv && (ctx->parse_type & PARSEFLAG_SEMICOLON)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003059 debug_printf_parse(": checking '%s' for reserved-ness\n", dest->data);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003060 if (reserved_word(dest, ctx)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003061 debug_printf_parse("done_word return %d\n", (ctx->res_w == RES_SNTX));
3062 return (ctx->res_w == RES_SNTX);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003063 }
Eric Andersen25f27032001-04-26 23:22:31 +00003064 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003065 glob_target = &child->argv;
Eric Andersen25f27032001-04-26 23:22:31 +00003066 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003067 gr = xglob(dest, glob_target);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003068 if (gr != 0) {
3069 debug_printf_parse("done_word return 1: xglob returned %d\n", gr);
3070 return 1;
3071 }
Eric Andersen25f27032001-04-26 23:22:31 +00003072
3073 b_reset(dest);
3074 if (ctx->pending_redirect) {
Denis Vlasenkof962a032007-11-23 12:50:54 +00003075 /* NB: don't free_strings(ctx->pending_redirect->glob_word) here */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003076 if (ctx->pending_redirect->glob_word
3077 && ctx->pending_redirect->glob_word[0]
3078 && ctx->pending_redirect->glob_word[1]
3079 ) {
3080 /* more than one word resulted from globbing redir */
3081 ctx->pending_redirect = NULL;
Manuel Novoa III cad53642003-03-19 09:13:01 +00003082 bb_error_msg("ambiguous redirect");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003083 debug_printf_parse("done_word return 1: ambiguous redirect\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003084 return 1;
3085 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003086 ctx->pending_redirect = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003087 }
Denis Vlasenko06810332007-05-21 23:30:54 +00003088#if ENABLE_HUSH_LOOPS
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003089 if (ctx->res_w == RES_FOR) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003090 done_word(dest, ctx);
3091 done_pipe(ctx, PIPE_SEQ);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003092 }
Denis Vlasenko06810332007-05-21 23:30:54 +00003093#endif
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003094 debug_printf_parse("done_word return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003095 return 0;
3096}
3097
3098/* The only possible error here is out of memory, in which case
3099 * xmalloc exits. */
3100static int done_command(struct p_context *ctx)
3101{
3102 /* The child is really already in the pipe structure, so
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003103 * advance the pipe counter and make a new, null child. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003104 struct pipe *pi = ctx->pipe;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003105 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00003106
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003107 if (child) {
3108 if (child->group == NULL
3109 && child->argv == NULL
3110 && child->redirects == NULL
3111 ) {
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003112 debug_printf_parse("done_command: skipping null cmd, num_progs=%d\n", pi->num_progs);
3113 return pi->num_progs;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003114 }
Eric Andersen25f27032001-04-26 23:22:31 +00003115 pi->num_progs++;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003116 debug_printf_parse("done_command: ++num_progs=%d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00003117 } else {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003118 debug_printf_parse("done_command: initializing, num_progs=%d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00003119 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003120
3121 /* Only real trickiness here is that the uncommitted
3122 * child structure is not counted in pi->num_progs. */
Eric Andersen25f27032001-04-26 23:22:31 +00003123 pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1));
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003124 child = &pi->progs[pi->num_progs];
Eric Andersen25f27032001-04-26 23:22:31 +00003125
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003126 memset(child, 0, sizeof(*child));
3127 /*child->redirects = NULL;*/
3128 /*child->argv = NULL;*/
3129 /*child->is_stopped = 0;*/
3130 /*child->group = NULL;*/
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003131 child->family = pi;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003132 //sp: /*child->sp = 0;*/
Denis Vlasenko262d7652007-05-20 21:52:49 +00003133 //pt: child->parse_type = ctx->parse_type;
Eric Andersen25f27032001-04-26 23:22:31 +00003134
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003135 ctx->child = child;
Eric Andersen25f27032001-04-26 23:22:31 +00003136 /* but ctx->pipe and ctx->list_head remain unchanged */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003137
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003138 return pi->num_progs; /* used only for 0/nonzero check */
Eric Andersen25f27032001-04-26 23:22:31 +00003139}
3140
3141static int done_pipe(struct p_context *ctx, pipe_style type)
3142{
3143 struct pipe *new_p;
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003144 int not_null;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003145
3146 debug_printf_parse("done_pipe entered, followup %d\n", type);
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003147 not_null = done_command(ctx); /* implicit closure of previous command */
Eric Andersen25f27032001-04-26 23:22:31 +00003148 ctx->pipe->followup = type;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003149 ctx->pipe->res_word = ctx->res_w;
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003150 /* Without this check, even just <enter> on command line generates
3151 * tree of three NOPs (!). Which is harmless but annoying.
3152 * IOW: it is safe to do it unconditionally. */
3153 if (not_null) {
3154 new_p = new_pipe();
3155 ctx->pipe->next = new_p;
3156 ctx->pipe = new_p;
3157 ctx->child = NULL;
3158 done_command(ctx); /* set up new pipe to accept commands */
3159 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003160 debug_printf_parse("done_pipe return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003161 return 0;
3162}
3163
3164/* peek ahead in the in_str to find out if we have a "&n" construct,
3165 * as in "2>&1", that represents duplicating a file descriptor.
3166 * returns either -2 (syntax error), -1 (no &), or the number found.
3167 */
3168static int redirect_dup_num(struct in_str *input)
3169{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003170 int ch, d = 0, ok = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003171 ch = b_peek(input);
3172 if (ch != '&') return -1;
3173
3174 b_getch(input); /* get the & */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003175 ch = b_peek(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003176 if (ch == '-') {
3177 b_getch(input);
3178 return -3; /* "-" represents "close me" */
3179 }
3180 while (isdigit(ch)) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00003181 d = d*10 + (ch-'0');
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003182 ok = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003183 b_getch(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003184 ch = b_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003185 }
3186 if (ok) return d;
3187
Manuel Novoa III cad53642003-03-19 09:13:01 +00003188 bb_error_msg("ambiguous redirect");
Eric Andersen25f27032001-04-26 23:22:31 +00003189 return -2;
3190}
3191
3192/* If a redirect is immediately preceded by a number, that number is
3193 * supposed to tell which file descriptor to redirect. This routine
3194 * looks for such preceding numbers. In an ideal world this routine
3195 * needs to handle all the following classes of redirects...
3196 * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
3197 * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
3198 * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
3199 * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
3200 * A -1 output from this program means no valid number was found, so the
3201 * caller should use the appropriate default for this redirection.
3202 */
3203static int redirect_opt_num(o_string *o)
3204{
3205 int num;
3206
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003207 if (o->length == 0)
3208 return -1;
3209 for (num = 0; num < o->length; num++) {
3210 if (!isdigit(*(o->data + num))) {
Eric Andersen25f27032001-04-26 23:22:31 +00003211 return -1;
3212 }
3213 }
3214 /* reuse num (and save an int) */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003215 num = atoi(o->data);
Eric Andersen25f27032001-04-26 23:22:31 +00003216 b_reset(o);
3217 return num;
3218}
3219
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003220#if ENABLE_HUSH_TICK
Denis Vlasenko8412d792007-10-01 09:59:47 +00003221/* NB: currently disabled on NOMMU */
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00003222static FILE *generate_stream_from_list(struct pipe *head)
Eric Andersen25f27032001-04-26 23:22:31 +00003223{
3224 FILE *pf;
Eric Andersen25f27032001-04-26 23:22:31 +00003225 int pid, channel[2];
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003226
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +00003227 xpipe(channel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003228/* *** NOMMU WARNING *** */
3229/* By using vfork here, we suspend parent till child exits or execs.
3230 * If child will not do it before it fills the pipe, it can block forever
3231 * in write(STDOUT_FILENO), and parent (shell) will be also stuck.
3232 */
3233 pid = BB_MMU ? fork() : vfork();
3234 if (pid < 0)
3235 bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork");
3236 if (pid == 0) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00003237 if (ENABLE_HUSH_JOB)
3238 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenko284d0fa2008-02-16 13:18:17 +00003239 close(channel[0]); /* NB: close _first_, then move fd! */
Denis Vlasenko05743d72008-02-10 12:10:08 +00003240 xmove_fd(channel[1], 1);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003241 /* Prevent it from trying to handle ctrl-z etc */
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003242#if ENABLE_HUSH_JOB
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003243 run_list_level = 1;
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003244#endif
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003245 /* Process substitution is not considered to be usual
3246 * 'command execution'.
3247 * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. */
3248 /* Not needed, we are relying on it being disabled
3249 * everywhere outside actual command execution. */
3250 /*set_jobctrl_sighandler(SIG_IGN);*/
3251 set_misc_sighandler(SIG_DFL);
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003252 /* Freeing 'head' here would break NOMMU. */
3253 _exit(run_list(head));
Eric Andersen25f27032001-04-26 23:22:31 +00003254 }
Eric Andersen25f27032001-04-26 23:22:31 +00003255 close(channel[1]);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003256 pf = fdopen(channel[0], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00003257 return pf;
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003258 /* 'head' is freed by the caller */
Eric Andersen25f27032001-04-26 23:22:31 +00003259}
3260
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003261/* Return code is exit status of the process that is run. */
Denis Vlasenko68404f12008-03-17 09:00:54 +00003262static int process_command_subs(o_string *dest,
3263 /*struct p_context *ctx,*/
3264 struct in_str *input,
3265 const char *subst_end)
Eric Andersen25f27032001-04-26 23:22:31 +00003266{
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003267 int retcode, ch, eol_cnt;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003268 o_string result = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003269 struct p_context inner;
3270 FILE *p;
3271 struct in_str pipe_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003272
Eric Andersen25f27032001-04-26 23:22:31 +00003273 initialize_context(&inner);
3274
3275 /* recursion to generate command */
3276 retcode = parse_stream(&result, &inner, input, subst_end);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003277 if (retcode != 0)
3278 return retcode; /* syntax error or EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003279 done_word(&result, &inner);
3280 done_pipe(&inner, PIPE_SEQ);
3281 b_free(&result);
3282
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003283 p = generate_stream_from_list(inner.list_head);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003284 if (p == NULL)
3285 return 1;
Denis Vlasenkoa0898172007-10-01 09:59:01 +00003286 close_on_exec_on(fileno(p));
Eric Andersen25f27032001-04-26 23:22:31 +00003287 setup_file_in_str(&pipe_str, p);
3288
3289 /* now send results of command back into original context */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003290 eol_cnt = 0;
3291 while ((ch = b_getch(&pipe_str)) != EOF) {
3292 if (ch == '\n') {
3293 eol_cnt++;
3294 continue;
3295 }
3296 while (eol_cnt) {
Denis Vlasenkoff097622007-10-01 10:00:45 +00003297 b_addqchr(dest, '\n', dest->o_quote);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003298 eol_cnt--;
3299 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00003300 b_addqchr(dest, ch, dest->o_quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003301 }
3302
3303 debug_printf("done reading from pipe, pclose()ing\n");
3304 /* This is the step that wait()s for the child. Should be pretty
3305 * safe, since we just read an EOF from its stdout. We could try
Denis Vlasenko262d7652007-05-20 21:52:49 +00003306 * to do better, by using wait(), and keeping track of background jobs
Eric Andersen25f27032001-04-26 23:22:31 +00003307 * at the same time. That would be a lot of work, and contrary
3308 * to the KISS philosophy of this program. */
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003309 retcode = fclose(p);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003310 free_pipe_list(inner.list_head, /* indent: */ 0);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003311 debug_printf("closed FILE from child, retcode=%d\n", retcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003312 return retcode;
3313}
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003314#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003315
3316static int parse_group(o_string *dest, struct p_context *ctx,
3317 struct in_str *input, int ch)
3318{
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003319 int rcode;
3320 const char *endch = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003321 struct p_context sub;
3322 struct child_prog *child = ctx->child;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003323
3324 debug_printf_parse("parse_group entered\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003325 if (child->argv) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003326 syntax(NULL);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003327 debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n");
3328 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003329 }
3330 initialize_context(&sub);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003331 endch = "}";
3332 if (ch == '(') {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003333 endch = ")";
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003334 child->subshell = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003335 }
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003336 rcode = parse_stream(dest, &sub, input, endch);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00003337//vda: err chk?
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003338 done_word(dest, &sub); /* finish off the final word in the subcontext */
Eric Andersen25f27032001-04-26 23:22:31 +00003339 done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
3340 child->group = sub.list_head;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003341
3342 debug_printf_parse("parse_group return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003343 return rcode;
3344 /* child remains "open", available for possible redirects */
3345}
3346
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003347/* Basically useful version until someone wants to get fancier,
Eric Andersen25f27032001-04-26 23:22:31 +00003348 * see the bash man page under "Parameter Expansion" */
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00003349static const char *lookup_param(const char *src)
Eric Andersen25f27032001-04-26 23:22:31 +00003350{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003351 struct variable *var = get_local_var(src);
3352 if (var)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003353 return strchr(var->varstr, '=') + 1;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003354 return NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003355}
3356
3357/* return code: 0 for OK, 1 for syntax error */
Denis Vlasenko68404f12008-03-17 09:00:54 +00003358static int handle_dollar(o_string *dest, /*struct p_context *ctx,*/ struct in_str *input)
Eric Andersen25f27032001-04-26 23:22:31 +00003359{
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003360 int ch = b_peek(input); /* first character after the $ */
Denis Vlasenkoff097622007-10-01 10:00:45 +00003361 unsigned char quote_mask = dest->o_quote ? 0x80 : 0;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003362
3363 debug_printf_parse("handle_dollar entered: ch='%c'\n", ch);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003364 if (isalpha(ch)) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003365 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003366 //sp: ctx->child->sp++;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003367 while (1) {
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003368 debug_printf_parse(": '%c'\n", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003369 b_getch(input);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003370 b_addchr(dest, ch | quote_mask);
3371 quote_mask = 0;
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003372 ch = b_peek(input);
3373 if (!isalnum(ch) && ch != '_')
3374 break;
Eric Andersen25f27032001-04-26 23:22:31 +00003375 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003376 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003377 } else if (isdigit(ch)) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003378 make_one_char_var:
3379 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003380 //sp: ctx->child->sp++;
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003381 debug_printf_parse(": '%c'\n", ch);
3382 b_getch(input);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003383 b_addchr(dest, ch | quote_mask);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003384 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003385 } else switch (ch) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003386 case '$': /* pid */
3387 case '!': /* last bg pid */
3388 case '?': /* last exit code */
3389 case '#': /* number of args */
3390 case '*': /* args */
3391 case '@': /* args */
3392 goto make_one_char_var;
Eric Andersen25f27032001-04-26 23:22:31 +00003393 case '{':
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003394 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003395 //sp: ctx->child->sp++;
Eric Andersen25f27032001-04-26 23:22:31 +00003396 b_getch(input);
3397 /* XXX maybe someone will try to escape the '}' */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003398 while (1) {
3399 ch = b_getch(input);
Denis Vlasenkob0550012007-05-24 12:18:16 +00003400 if (ch == '}')
3401 break;
3402 if (!isalnum(ch) && ch != '_') {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003403 syntax("unterminated ${name}");
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003404 debug_printf_parse("handle_dollar return 1: unterminated ${name}\n");
3405 return 1;
3406 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003407 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003408 b_addchr(dest, ch | quote_mask);
3409 quote_mask = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003410 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003411 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003412 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003413#if ENABLE_HUSH_TICK
Eric Andersen25f27032001-04-26 23:22:31 +00003414 case '(':
Matt Kraai9f8caf12001-05-02 16:26:12 +00003415 b_getch(input);
Denis Vlasenko68404f12008-03-17 09:00:54 +00003416 process_command_subs(dest, /*ctx,*/ input, ")");
Eric Andersen25f27032001-04-26 23:22:31 +00003417 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003418#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003419 case '-':
3420 case '_':
3421 /* still unhandled, but should be eventually */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003422 bb_error_msg("unhandled syntax: $%c", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003423 return 1;
3424 break;
3425 default:
Denis Vlasenkoff097622007-10-01 10:00:45 +00003426 b_addqchr(dest, '$', dest->o_quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003427 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003428 debug_printf_parse("handle_dollar return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003429 return 0;
3430}
3431
Eric Andersen25f27032001-04-26 23:22:31 +00003432/* return code is 0 for normal exit, 1 for syntax error */
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003433static int parse_stream(o_string *dest, struct p_context *ctx,
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003434 struct in_str *input, const char *end_trigger)
Eric Andersen25f27032001-04-26 23:22:31 +00003435{
"Vladimir N. Oleynik"4ccd2b42006-01-31 09:27:48 +00003436 int ch, m;
Eric Andersen25f27032001-04-26 23:22:31 +00003437 int redir_fd;
3438 redir_type redir_style;
3439 int next;
3440
Denis Vlasenkoff097622007-10-01 10:00:45 +00003441 /* Only double-quote state is handled in the state variable dest->o_quote.
Eric Andersen25f27032001-04-26 23:22:31 +00003442 * A single-quote triggers a bypass of the main loop until its mate is
Denis Vlasenkoff097622007-10-01 10:00:45 +00003443 * found. When recursing, quote state is passed in via dest->o_quote. */
Eric Andersen25f27032001-04-26 23:22:31 +00003444
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003445 debug_printf_parse("parse_stream entered, end_trigger='%s'\n", end_trigger);
3446
Denis Vlasenko1a735862007-05-23 00:32:25 +00003447 while (1) {
Denis Vlasenko1a735862007-05-23 00:32:25 +00003448 m = CHAR_IFS;
3449 next = '\0';
Denis Vlasenko170435c2007-05-23 13:01:10 +00003450 ch = b_getch(input);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003451 if (ch != EOF) {
3452 m = charmap[ch];
3453 if (ch != '\n')
3454 next = b_peek(input);
3455 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003456 debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n",
Denis Vlasenkoff097622007-10-01 10:00:45 +00003457 ch, ch, m, dest->o_quote);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003458 if (m == CHAR_ORDINARY
Denis Vlasenkoff097622007-10-01 10:00:45 +00003459 || (m != CHAR_SPECIAL && dest->o_quote)
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003460 ) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00003461 if (ch == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003462 syntax("unterminated \"");
Denis Vlasenko170435c2007-05-23 13:01:10 +00003463 debug_printf_parse("parse_stream return 1: unterminated \"\n");
3464 return 1;
3465 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00003466 b_addqchr(dest, ch, dest->o_quote);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003467 continue;
3468 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003469 if (m == CHAR_IFS) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003470 if (done_word(dest, ctx)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003471 debug_printf_parse("parse_stream return 1: done_word!=0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003472 return 1;
Eric Andersenaac75e52001-04-30 18:18:45 +00003473 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003474 if (ch == EOF)
3475 break;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003476 /* If we aren't performing a substitution, treat
3477 * a newline as a command separator.
3478 * [why we don't handle it exactly like ';'? --vda] */
3479 if (end_trigger && ch == '\n') {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003480 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003481 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003482 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003483 if ((end_trigger && strchr(end_trigger, ch))
Denis Vlasenkoff097622007-10-01 10:00:45 +00003484 && !dest->o_quote && ctx->res_w == RES_NONE
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003485 ) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003486 debug_printf_parse("parse_stream return 0: end_trigger char found\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003487 return 0;
3488 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003489 if (m == CHAR_IFS)
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003490 continue;
3491 switch (ch) {
Eric Andersen25f27032001-04-26 23:22:31 +00003492 case '#':
Denis Vlasenkoff097622007-10-01 10:00:45 +00003493 if (dest->length == 0 && !dest->o_quote) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003494 while (1) {
3495 ch = b_peek(input);
3496 if (ch == EOF || ch == '\n')
3497 break;
3498 b_getch(input);
3499 }
Eric Andersen25f27032001-04-26 23:22:31 +00003500 } else {
Denis Vlasenkoff097622007-10-01 10:00:45 +00003501 b_addqchr(dest, ch, dest->o_quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003502 }
3503 break;
3504 case '\\':
3505 if (next == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003506 syntax("\\<eof>");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003507 debug_printf_parse("parse_stream return 1: \\<eof>\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003508 return 1;
3509 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00003510 b_addqchr(dest, '\\', dest->o_quote);
3511 b_addqchr(dest, b_getch(input), dest->o_quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003512 break;
3513 case '$':
Denis Vlasenko68404f12008-03-17 09:00:54 +00003514 if (handle_dollar(dest, /*ctx,*/ input) != 0) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003515 debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n");
3516 return 1;
3517 }
Eric Andersen25f27032001-04-26 23:22:31 +00003518 break;
3519 case '\'':
3520 dest->nonnull = 1;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003521 while (1) {
3522 ch = b_getch(input);
3523 if (ch == EOF || ch == '\'')
3524 break;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003525 b_addchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003526 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003527 if (ch == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003528 syntax("unterminated '");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003529 debug_printf_parse("parse_stream return 1: unterminated '\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003530 return 1;
3531 }
3532 break;
3533 case '"':
3534 dest->nonnull = 1;
Denis Vlasenkoff097622007-10-01 10:00:45 +00003535 dest->o_quote ^= 1; /* invert */
Eric Andersen25f27032001-04-26 23:22:31 +00003536 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003537#if ENABLE_HUSH_TICK
Eric Andersen25f27032001-04-26 23:22:31 +00003538 case '`':
Denis Vlasenko68404f12008-03-17 09:00:54 +00003539 process_command_subs(dest, /*ctx,*/ input, "`");
Eric Andersen25f27032001-04-26 23:22:31 +00003540 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003541#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003542 case '>':
3543 redir_fd = redirect_opt_num(dest);
3544 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003545 redir_style = REDIRECT_OVERWRITE;
Eric Andersen25f27032001-04-26 23:22:31 +00003546 if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003547 redir_style = REDIRECT_APPEND;
Eric Andersen25f27032001-04-26 23:22:31 +00003548 b_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003549 }
3550#if 0
3551 else if (next == '(') {
3552 syntax(">(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003553 debug_printf_parse("parse_stream return 1: >(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003554 return 1;
3555 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003556#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003557 setup_redirect(ctx, redir_fd, redir_style, input);
3558 break;
3559 case '<':
3560 redir_fd = redirect_opt_num(dest);
3561 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003562 redir_style = REDIRECT_INPUT;
Eric Andersen25f27032001-04-26 23:22:31 +00003563 if (next == '<') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003564 redir_style = REDIRECT_HEREIS;
Eric Andersen25f27032001-04-26 23:22:31 +00003565 b_getch(input);
3566 } else if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003567 redir_style = REDIRECT_IO;
Eric Andersen25f27032001-04-26 23:22:31 +00003568 b_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003569 }
3570#if 0
3571 else if (next == '(') {
3572 syntax("<(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003573 debug_printf_parse("parse_stream return 1: <(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003574 return 1;
3575 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003576#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003577 setup_redirect(ctx, redir_fd, redir_style, input);
3578 break;
3579 case ';':
3580 done_word(dest, ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003581 done_pipe(ctx, PIPE_SEQ);
Eric Andersen25f27032001-04-26 23:22:31 +00003582 break;
3583 case '&':
3584 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003585 if (next == '&') {
Eric Andersen25f27032001-04-26 23:22:31 +00003586 b_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003587 done_pipe(ctx, PIPE_AND);
Eric Andersen25f27032001-04-26 23:22:31 +00003588 } else {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003589 done_pipe(ctx, PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00003590 }
3591 break;
3592 case '|':
3593 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003594 if (next == '|') {
Eric Andersen25f27032001-04-26 23:22:31 +00003595 b_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003596 done_pipe(ctx, PIPE_OR);
Eric Andersen25f27032001-04-26 23:22:31 +00003597 } else {
3598 /* we could pick up a file descriptor choice here
3599 * with redirect_opt_num(), but bash doesn't do it.
3600 * "echo foo 2| cat" yields "foo 2". */
3601 done_command(ctx);
3602 }
3603 break;
3604 case '(':
3605 case '{':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003606 if (parse_group(dest, ctx, input, ch) != 0) {
3607 debug_printf_parse("parse_stream return 1: parse_group returned non-0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003608 return 1;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003609 }
Eric Andersen25f27032001-04-26 23:22:31 +00003610 break;
3611 case ')':
3612 case '}':
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003613 syntax("unexpected }"); /* Proper use of this character is caught by end_trigger */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003614 debug_printf_parse("parse_stream return 1: unexpected '}'\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003615 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003616 default:
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003617 if (ENABLE_HUSH_DEBUG)
3618 bb_error_msg_and_die("BUG: unexpected %c\n", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003619 }
3620 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003621 /* Complain if quote? No, maybe we just finished a command substitution
Eric Andersen25f27032001-04-26 23:22:31 +00003622 * that was quoted. Example:
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003623 * $ echo "`cat foo` plus more"
Eric Andersen25f27032001-04-26 23:22:31 +00003624 * and we just got the EOF generated by the subshell that ran "cat foo"
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003625 * The only real complaint is if we got an EOF when end_trigger != NULL,
Eric Andersen25f27032001-04-26 23:22:31 +00003626 * that is, we were really supposed to get end_trigger, and never got
3627 * one before the EOF. Can't use the standard "syntax error" return code,
3628 * so that parse_stream_outer can distinguish the EOF and exit smoothly. */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003629 debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL));
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003630 if (end_trigger)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003631 return -1;
Eric Andersen25f27032001-04-26 23:22:31 +00003632 return 0;
3633}
3634
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003635static void set_in_charmap(const char *set, int code)
Eric Andersen25f27032001-04-26 23:22:31 +00003636{
Denis Vlasenkof5294e12007-04-14 10:09:57 +00003637 while (*set)
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003638 charmap[(unsigned char)*set++] = code;
Eric Andersen25f27032001-04-26 23:22:31 +00003639}
3640
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003641static void update_charmap(void)
Eric Andersen25f27032001-04-26 23:22:31 +00003642{
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003643 /* char *ifs and char charmap[256] are both globals. */
Eric Andersen25f27032001-04-26 23:22:31 +00003644 ifs = getenv("IFS");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003645 if (ifs == NULL)
3646 ifs = " \t\n";
Eric Andersen25f27032001-04-26 23:22:31 +00003647 /* Precompute a list of 'flow through' behavior so it can be treated
3648 * quickly up front. Computation is necessary because of IFS.
3649 * Special case handling of IFS == " \t\n" is not implemented.
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003650 * The charmap[] array only really needs two bits each,
3651 * and on most machines that would be faster (reduced L1 cache use).
Eric Andersen25f27032001-04-26 23:22:31 +00003652 */
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003653 memset(charmap, CHAR_ORDINARY, sizeof(charmap));
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003654#if ENABLE_HUSH_TICK
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003655 set_in_charmap("\\$\"`", CHAR_SPECIAL);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003656#else
3657 set_in_charmap("\\$\"", CHAR_SPECIAL);
3658#endif
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003659 set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003660 set_in_charmap(ifs, CHAR_IFS); /* are ordinary if quoted */
Eric Andersen25f27032001-04-26 23:22:31 +00003661}
3662
Eric Andersenaff114c2004-04-14 17:51:38 +00003663/* most recursion does not come through here, the exception is
Denis Vlasenko170435c2007-05-23 13:01:10 +00003664 * from builtin_source() and builtin_eval() */
3665static int parse_and_run_stream(struct in_str *inp, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003666{
Eric Andersen25f27032001-04-26 23:22:31 +00003667 struct p_context ctx;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003668 o_string temp = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003669 int rcode;
3670 do {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003671 ctx.parse_type = parse_flag;
Eric Andersen25f27032001-04-26 23:22:31 +00003672 initialize_context(&ctx);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003673 update_charmap();
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003674 if (!(parse_flag & PARSEFLAG_SEMICOLON) || (parse_flag & PARSEFLAG_REPARSING))
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003675 set_in_charmap(";$&|", CHAR_ORDINARY);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003676#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00003677 inp->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003678#endif
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003679 /* We will stop & execute after each ';' or '\n'.
3680 * Example: "sleep 9999; echo TEST" + ctrl-C:
3681 * TEST should be printed */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003682 rcode = parse_stream(&temp, &ctx, inp, ";\n");
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003683 if (rcode != 1 && ctx.old_flag != 0) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003684 syntax(NULL);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003685 }
3686 if (rcode != 1 && ctx.old_flag == 0) {
3687 done_word(&temp, &ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003688 done_pipe(&ctx, PIPE_SEQ);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00003689 debug_print_tree(ctx.list_head, 0);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003690 debug_printf_exec("parse_stream_outer: run_and_free_list\n");
3691 run_and_free_list(ctx.list_head);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003692 } else {
3693 if (ctx.old_flag != 0) {
3694 free(ctx.stack);
3695 b_reset(&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003696 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003697 temp.nonnull = 0;
Denis Vlasenkoff097622007-10-01 10:00:45 +00003698 temp.o_quote = 0;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003699 inp->p = NULL;
Denis Vlasenko05743d72008-02-10 12:10:08 +00003700 free_pipe_list(ctx.list_head, /* indent: */ 0);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003701 }
Eric Andersena813afc2001-05-24 16:19:36 +00003702 b_free(&temp);
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003703 } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP)); /* loop on syntax errors, return on EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003704 return 0;
3705}
3706
Denis Vlasenko170435c2007-05-23 13:01:10 +00003707static int parse_and_run_string(const char *s, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003708{
3709 struct in_str input;
3710 setup_string_in_str(&input, s);
Denis Vlasenko170435c2007-05-23 13:01:10 +00003711 return parse_and_run_stream(&input, parse_flag);
Eric Andersen25f27032001-04-26 23:22:31 +00003712}
3713
Denis Vlasenko170435c2007-05-23 13:01:10 +00003714static int parse_and_run_file(FILE *f)
Eric Andersen25f27032001-04-26 23:22:31 +00003715{
3716 int rcode;
3717 struct in_str input;
3718 setup_file_in_str(&input, f);
Denis Vlasenko170435c2007-05-23 13:01:10 +00003719 rcode = parse_and_run_stream(&input, PARSEFLAG_SEMICOLON);
Eric Andersen25f27032001-04-26 23:22:31 +00003720 return rcode;
3721}
3722
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003723#if ENABLE_HUSH_JOB
Eric Andersen6c947d22001-06-25 22:24:38 +00003724/* Make sure we have a controlling tty. If we get started under a job
3725 * aware app (like bash for example), make sure we are now in charge so
3726 * we don't fight over who gets the foreground */
Eric Anderseneaecbf32001-10-31 10:41:31 +00003727static void setup_job_control(void)
Eric Andersen52a97ca2001-06-22 06:49:26 +00003728{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003729 pid_t shell_pgrp;
3730
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003731 saved_task_pgrp = shell_pgrp = getpgrp();
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003732 debug_printf_jobs("saved_task_pgrp=%d\n", saved_task_pgrp);
Denis Vlasenko96e1b382007-09-30 23:50:48 +00003733 close_on_exec_on(interactive_fd);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003734
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003735 /* If we were ran as 'hush &',
3736 * sleep until we are in the foreground. */
3737 while (tcgetpgrp(interactive_fd) != shell_pgrp) {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003738 /* Send TTIN to ourself (should stop us) */
Denis Vlasenkoff131b92007-04-10 15:42:06 +00003739 kill(- shell_pgrp, SIGTTIN);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003740 shell_pgrp = getpgrp();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003741 }
Eric Andersen52a97ca2001-06-22 06:49:26 +00003742
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003743 /* Ignore job-control and misc signals. */
3744 set_jobctrl_sighandler(SIG_IGN);
3745 set_misc_sighandler(SIG_IGN);
3746//huh? signal(SIGCHLD, SIG_IGN);
3747
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003748 /* We _must_ restore tty pgrp on fatal signals */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003749 set_fatal_sighandler(sigexit);
Eric Andersen6c947d22001-06-25 22:24:38 +00003750
3751 /* Put ourselves in our own process group. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003752 setpgrp(); /* is the same as setpgid(our_pid, our_pid); */
Eric Andersen6c947d22001-06-25 22:24:38 +00003753 /* Grab control of the terminal. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003754 tcsetpgrp(interactive_fd, getpid());
Eric Andersen6c947d22001-06-25 22:24:38 +00003755}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003756#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00003757
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00003758int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Matt Kraai2d91deb2001-08-01 17:21:35 +00003759int hush_main(int argc, char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00003760{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00003761 static const char version_str[] ALIGN1 = "HUSH_VERSION="HUSH_VER_STR;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003762 static const struct variable const_shell_ver = {
3763 .next = NULL,
3764 .varstr = (char*)version_str,
3765 .max_len = 1, /* 0 can provoke free(name) */
3766 .flg_export = 1,
3767 .flg_read_only = 1,
3768 };
3769
Eric Andersen25f27032001-04-26 23:22:31 +00003770 int opt;
3771 FILE *input;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003772 char **e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003773 struct variable *cur_var;
Eric Andersenbc604a22001-05-16 05:24:03 +00003774
Denis Vlasenko574f2f42008-02-27 18:41:59 +00003775 INIT_G();
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003776
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003777 /* Deal with HUSH_VERSION */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003778 shell_ver = const_shell_ver; /* copying struct here */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003779 top_var = &shell_ver;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003780 unsetenv("HUSH_VERSION"); /* in case it exists in initial env */
3781 /* Initialize our shell local variables with the values
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003782 * currently living in the environment */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003783 cur_var = top_var;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003784 e = environ;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003785 if (e) while (*e) {
3786 char *value = strchr(*e, '=');
3787 if (value) { /* paranoia */
3788 cur_var->next = xzalloc(sizeof(*cur_var));
3789 cur_var = cur_var->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003790 cur_var->varstr = *e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003791 cur_var->max_len = strlen(*e);
3792 cur_var->flg_export = 1;
3793 }
3794 e++;
3795 }
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003796 putenv((char *)version_str); /* reinstate HUSH_VERSION */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003797
Denis Vlasenko38f63192007-01-22 09:03:07 +00003798#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003799 line_input_state = new_line_input_t(FOR_SHELL);
3800#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003801 /* XXX what should these be while sourcing /etc/profile? */
3802 global_argc = argc;
3803 global_argv = argv;
Eric Andersen94ac2442001-05-22 19:05:18 +00003804 /* Initialize some more globals to non-zero values */
3805 set_cwd();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003806#if ENABLE_HUSH_INTERACTIVE
3807#if ENABLE_FEATURE_EDITING
3808 cmdedit_set_initial_prompt();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003809#endif
Eric Andersen94ac2442001-05-22 19:05:18 +00003810 PS2 = "> ";
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003811#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003812
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003813 if (EXIT_SUCCESS) /* otherwise is already done */
3814 last_return_code = EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00003815
3816 if (argv[0] && argv[0][0] == '-') {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003817 debug_printf("sourcing /etc/profile\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003818 input = fopen("/etc/profile", "r");
3819 if (input != NULL) {
Denis Vlasenkoa0898172007-10-01 09:59:01 +00003820 close_on_exec_on(fileno(input));
Denis Vlasenko170435c2007-05-23 13:01:10 +00003821 parse_and_run_file(input);
Eric Andersena90f20b2001-06-26 23:00:21 +00003822 fclose(input);
3823 }
Eric Andersen25f27032001-04-26 23:22:31 +00003824 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003825 input = stdin;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003826
Eric Andersen25f27032001-04-26 23:22:31 +00003827 while ((opt = getopt(argc, argv, "c:xif")) > 0) {
3828 switch (opt) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003829 case 'c':
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003830 global_argv = argv + optind;
3831 global_argc = argc - optind;
Denis Vlasenko170435c2007-05-23 13:01:10 +00003832 opt = parse_and_run_string(optarg, PARSEFLAG_SEMICOLON);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003833 goto final_return;
3834 case 'i':
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003835 /* Well, we cannot just declare interactiveness,
3836 * we have to have some stuff (ctty, etc) */
3837 /* interactive_fd++; */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003838 break;
3839 case 'f':
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003840 fake_mode = 1;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003841 break;
3842 default:
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003843#ifndef BB_VER
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003844 fprintf(stderr, "Usage: sh [FILE]...\n"
3845 " or: sh -c command [args]...\n\n");
3846 exit(EXIT_FAILURE);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003847#else
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003848 bb_show_usage();
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003849#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003850 }
3851 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003852#if ENABLE_HUSH_JOB
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003853 /* A shell is interactive if the '-i' flag was given, or if all of
Eric Andersen25f27032001-04-26 23:22:31 +00003854 * the following conditions are met:
Denis Vlasenko55b2de72007-04-18 17:21:28 +00003855 * no -c command
Eric Andersen25f27032001-04-26 23:22:31 +00003856 * no arguments remaining or the -s flag given
3857 * standard input is a terminal
3858 * standard output is a terminal
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003859 * Refer to Posix.2, the description of the 'sh' utility. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003860 if (argv[optind] == NULL && input == stdin
3861 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
3862 ) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003863 saved_tty_pgrp = tcgetpgrp(STDIN_FILENO);
3864 debug_printf("saved_tty_pgrp=%d\n", saved_tty_pgrp);
3865 if (saved_tty_pgrp >= 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003866 /* try to dup to high fd#, >= 255 */
3867 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
3868 if (interactive_fd < 0) {
3869 /* try to dup to any fd */
3870 interactive_fd = dup(STDIN_FILENO);
3871 if (interactive_fd < 0)
3872 /* give up */
3873 interactive_fd = 0;
3874 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003875 // TODO: track & disallow any attempts of user
3876 // to (inadvertently) close/redirect it
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003877 }
Eric Andersen25f27032001-04-26 23:22:31 +00003878 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003879 debug_printf("interactive_fd=%d\n", interactive_fd);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003880 if (interactive_fd) {
Denis Vlasenko08126f62008-02-11 08:39:11 +00003881 fcntl(interactive_fd, F_SETFD, FD_CLOEXEC);
Eric Andersen25f27032001-04-26 23:22:31 +00003882 /* Looks like they want an interactive shell */
Eric Andersen52a97ca2001-06-22 06:49:26 +00003883 setup_job_control();
Denis Vlasenko4ecfcdc2008-02-11 08:32:31 +00003884 /* -1 is special - makes xfuncs longjmp, not exit
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003885 * (we reset die_sleep = 0 whereever we [v]fork) */
3886 die_sleep = -1;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003887 if (setjmp(die_jmp)) {
3888 /* xfunc has failed! die die die */
3889 hush_exit(xfunc_error_retval);
3890 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003891#if !ENABLE_FEATURE_SH_EXTRA_QUIET
Denis Vlasenkoca525b42007-06-13 12:27:17 +00003892 printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", bb_banner);
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003893 printf("Enter 'help' for a list of built-in commands.\n\n");
3894#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00003895 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003896#elif ENABLE_HUSH_INTERACTIVE
3897/* no job control compiled, only prompt/line editing */
3898 if (argv[optind] == NULL && input == stdin
3899 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
3900 ) {
3901 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
3902 if (interactive_fd < 0) {
3903 /* try to dup to any fd */
3904 interactive_fd = dup(STDIN_FILENO);
3905 if (interactive_fd < 0)
3906 /* give up */
3907 interactive_fd = 0;
3908 }
Denis Vlasenko08126f62008-02-11 08:39:11 +00003909 if (interactive_fd)
3910 fcntl(interactive_fd, F_SETFD, FD_CLOEXEC);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003911 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003912#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003913
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003914 if (argv[optind] == NULL) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00003915 opt = parse_and_run_file(stdin);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003916 } else {
3917 debug_printf("\nrunning script '%s'\n", argv[optind]);
3918 global_argv = argv + optind;
3919 global_argc = argc - optind;
3920 input = xfopen(argv[optind], "r");
Denis Vlasenko459a5ad2008-02-11 08:35:03 +00003921 fcntl(fileno(input), F_SETFD, FD_CLOEXEC);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003922 opt = parse_and_run_file(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003923 }
Eric Andersen25f27032001-04-26 23:22:31 +00003924
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003925 final_return:
3926
Denis Vlasenko38f63192007-01-22 09:03:07 +00003927#if ENABLE_FEATURE_CLEAN_UP
Eric Andersenaeb44c42001-05-22 20:29:00 +00003928 fclose(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003929 if (cwd != bb_msg_unknown)
Eric Andersenaeb44c42001-05-22 20:29:00 +00003930 free((char*)cwd);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003931 cur_var = top_var->next;
3932 while (cur_var) {
3933 struct variable *tmp = cur_var;
3934 if (!cur_var->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003935 free(cur_var->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003936 cur_var = cur_var->next;
3937 free(tmp);
Eric Andersenaeb44c42001-05-22 20:29:00 +00003938 }
Eric Andersen25f27032001-04-26 23:22:31 +00003939#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003940 hush_exit(opt ? opt : last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +00003941}
Denis Vlasenko96702ca2007-11-23 23:28:55 +00003942
3943
3944#if ENABLE_LASH
3945int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
3946int lash_main(int argc, char **argv)
3947{
3948 //bb_error_msg("lash is deprecated, please use hush instead");
3949 return hush_main(argc, argv);
3950}
3951#endif