blob: 3f46103911081ca9d8b0c199993a6539aaef33aa [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 )
463
464
465#define B_CHUNK 100
466#define B_NOSPAC 1
467#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
468
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000469#if 1
470/* Normal */
471static void syntax(const char *msg)
472{
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000473 /* Was using fancy stuff:
474 * (interactive_fd ? bb_error_msg : bb_error_msg_and_die)(...params...)
475 * but it SEGVs. ?! Oh well... explicit temp ptr works around that */
476 void (*fp)(const char *s, ...);
477
478 fp = (interactive_fd ? bb_error_msg : bb_error_msg_and_die);
479 fp(msg ? "%s: %s" : "syntax error", "syntax error", msg);
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000480}
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000481
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000482#else
483/* Debug */
484static void syntax_lineno(int line)
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000485{
Denis Vlasenko5a1437d2007-05-24 13:22:47 +0000486 void (*fp)(const char *s, ...);
487
488 fp = (interactive_fd ? bb_error_msg : bb_error_msg_and_die);
489 fp("syntax error hush.c:%d", line);
Eric Andersen25f27032001-04-26 23:22:31 +0000490}
Denis Vlasenko90e485c2007-05-23 15:22:50 +0000491#define syntax(str) syntax_lineno(__LINE__)
492#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000493
494/* Index of subroutines: */
Eric Andersen25f27032001-04-26 23:22:31 +0000495/* o_string manipulation: */
496static int b_check_space(o_string *o, int len);
497static int b_addchr(o_string *o, int ch);
498static void b_reset(o_string *o);
499static int b_addqchr(o_string *o, int ch, int quote);
Eric Andersen25f27032001-04-26 23:22:31 +0000500/* in_str manipulations: */
501static int static_get(struct in_str *i);
502static int static_peek(struct in_str *i);
503static int file_get(struct in_str *i);
504static int file_peek(struct in_str *i);
505static void setup_file_in_str(struct in_str *i, FILE *f);
506static void setup_string_in_str(struct in_str *i, const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000507/* "run" the final data structures: */
Denis Vlasenkoc666f712007-05-16 22:18:54 +0000508#if !defined(DEBUG_CLEAN)
509#define free_pipe_list(head, indent) free_pipe_list(head)
510#define free_pipe(pi, indent) free_pipe(pi)
511#endif
Eric Andersenbf7df042001-05-23 22:18:35 +0000512static int free_pipe_list(struct pipe *head, int indent);
513static int free_pipe(struct pipe *pi, int indent);
Eric Andersen25f27032001-04-26 23:22:31 +0000514/* really run the final data structures: */
515static int setup_redirects(struct child_prog *prog, int squirrel[]);
Denis Vlasenko05743d72008-02-10 12:10:08 +0000516static int run_list(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000517static void pseudo_exec_argv(char **argv) ATTRIBUTE_NORETURN;
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000518static void pseudo_exec(struct child_prog *child) ATTRIBUTE_NORETURN;
Denis Vlasenko05743d72008-02-10 12:10:08 +0000519static int run_pipe(struct pipe *pi);
Eric Andersen25f27032001-04-26 23:22:31 +0000520/* extended glob support: */
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000521static char **globhack(const char *src, char **strings);
Eric Andersen25f27032001-04-26 23:22:31 +0000522static int glob_needed(const char *s);
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000523static int xglob(o_string *dest, char ***pglob);
Eric Andersen78a7c992001-05-15 16:30:25 +0000524/* variable assignment: */
Eric Andersen78a7c992001-05-15 16:30:25 +0000525static int is_assignment(const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000526/* data structure manipulation: */
527static int setup_redirect(struct p_context *ctx, int fd, redir_type style, struct in_str *input);
528static void initialize_context(struct p_context *ctx);
529static int done_word(o_string *dest, struct p_context *ctx);
530static int done_command(struct p_context *ctx);
531static int done_pipe(struct p_context *ctx, pipe_style type);
532/* primary string parsing: */
533static int redirect_dup_num(struct in_str *input);
534static int redirect_opt_num(o_string *o);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000535#if ENABLE_HUSH_TICK
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000536static int process_command_subs(o_string *dest, struct p_context *ctx, struct in_str *input, const char *subst_end);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +0000537#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000538static int parse_group(o_string *dest, struct p_context *ctx, struct in_str *input, int ch);
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000539static const char *lookup_param(const char *src);
Eric Andersen25f27032001-04-26 23:22:31 +0000540static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000541static 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 +0000542/* setup: */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000543static int parse_and_run_stream(struct in_str *inp, int parse_flag);
544static int parse_and_run_string(const char *s, int parse_flag);
545static int parse_and_run_file(FILE *f);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000546/* job management: */
Eric Andersenc798b072001-06-22 06:23:03 +0000547static int checkjobs(struct pipe* fg_pipe);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000548#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +0000549static int checkjobs_and_fg_shell(struct pipe* fg_pipe);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000550static void insert_bg_job(struct pipe *pi);
551static void remove_bg_job(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000552static void delete_finished_bg_job(struct pipe *pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000553#else
554int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */
555#endif
Eric Andersenf72f5622001-05-15 23:21:41 +0000556/* local variable support */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000557static char **expand_strvec_to_strvec(char **argv);
558/* used for eval */
559static char *expand_strvec_to_string(char **argv);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +0000560/* used for expansion of right hand of assignments */
Denis Vlasenko170435c2007-05-23 13:01:10 +0000561static char *expand_string_to_string(const char *str);
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000562static struct variable *get_local_var(const char *name);
563static int set_local_var(char *str, int flg_export);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000564static void unset_local_var(const char *name);
Eric Andersen25f27032001-04-26 23:22:31 +0000565
Denis Vlasenkod65ea392007-10-01 10:02:25 +0000566
567static char **add_strings_to_strings(int need_xstrdup, char **strings, char **add)
568{
569 int i;
570 unsigned count1;
571 unsigned count2;
572 char **v;
573
574 v = strings;
575 count1 = 0;
576 if (v) {
577 while (*v) {
578 count1++;
579 v++;
580 }
581 }
582 count2 = 0;
583 v = add;
584 while (*v) {
585 count2++;
586 v++;
587 }
588 v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*));
589 v[count1 + count2] = NULL;
590 i = count2;
591 while (--i >= 0)
592 v[count1 + i] = need_xstrdup ? xstrdup(add[i]) : add[i];
593 return v;
594}
595
596/* 'add' should be a malloced pointer */
597static char **add_string_to_strings(char **strings, char *add)
598{
599 char *v[2];
600
601 v[0] = add;
602 v[1] = NULL;
603
604 return add_strings_to_strings(0, strings, v);
605}
606
607static void free_strings(char **strings)
608{
609 if (strings) {
610 char **v = strings;
611 while (*v)
612 free(*v++);
613 free(strings);
614 }
615}
616
617
Denis Vlasenko83506862007-11-23 13:11:42 +0000618/* Function prototypes for builtins */
619static int builtin_cd(char **argv);
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000620static int builtin_echo(char **argv);
Denis Vlasenko83506862007-11-23 13:11:42 +0000621static int builtin_eval(char **argv);
622static int builtin_exec(char **argv);
623static int builtin_exit(char **argv);
624static int builtin_export(char **argv);
625#if ENABLE_HUSH_JOB
626static int builtin_fg_bg(char **argv);
627static int builtin_jobs(char **argv);
628#endif
629#if ENABLE_HUSH_HELP
630static int builtin_help(char **argv);
631#endif
632static int builtin_pwd(char **argv);
633static int builtin_read(char **argv);
634static int builtin_test(char **argv);
635static int builtin_set(char **argv);
636static int builtin_shift(char **argv);
637static int builtin_source(char **argv);
638static int builtin_umask(char **argv);
639static int builtin_unset(char **argv);
640//static int builtin_not_written(char **argv);
641
Eric Andersen25f27032001-04-26 23:22:31 +0000642/* Table of built-in functions. They can be forked or not, depending on
643 * context: within pipes, they fork. As simple commands, they do not.
644 * When used in non-forking context, they can change global variables
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000645 * in the parent shell process. If forked, of course they cannot.
Eric Andersen25f27032001-04-26 23:22:31 +0000646 * For example, 'unset foo | whatever' will parse and run, but foo will
647 * still be set at the end. */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000648struct built_in_command {
649 const char *cmd; /* name */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000650 int (*function) (char **argv); /* function ptr */
Denis Vlasenko06810332007-05-21 23:30:54 +0000651#if ENABLE_HUSH_HELP
652 const char *descr; /* description */
653#define BLTIN(cmd, func, help) { cmd, func, help }
654#else
655#define BLTIN(cmd, func, help) { cmd, func }
656#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +0000657};
658
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000659/* For now, echo and test are unconditionally enabled.
660 * Maybe make it configurable? */
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +0000661static const struct built_in_command bltins[] = {
Denis Vlasenko83506862007-11-23 13:11:42 +0000662 BLTIN("[" , builtin_test, "Test condition"),
663 BLTIN("[[" , builtin_test, "Test condition"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000664#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000665 BLTIN("bg" , builtin_fg_bg, "Resume a job in the background"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000666#endif
Denis Vlasenko06810332007-05-21 23:30:54 +0000667// BLTIN("break" , builtin_not_written, "Exit for, while or until loop"),
668 BLTIN("cd" , builtin_cd, "Change working directory"),
669// BLTIN("continue", builtin_not_written, "Continue for, while or until loop"),
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000670 BLTIN("echo" , builtin_echo, "Write strings to stdout"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000671 BLTIN("eval" , builtin_eval, "Construct and run shell command"),
672 BLTIN("exec" , builtin_exec, "Exec command, replacing this shell with the exec'd process"),
673 BLTIN("exit" , builtin_exit, "Exit from shell"),
674 BLTIN("export", builtin_export, "Set environment variable"),
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000675#if ENABLE_HUSH_JOB
Denis Vlasenko06810332007-05-21 23:30:54 +0000676 BLTIN("fg" , builtin_fg_bg, "Bring job into the foreground"),
677 BLTIN("jobs" , builtin_jobs, "Lists the active jobs"),
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000678#endif
Denis Vlasenko06810332007-05-21 23:30:54 +0000679// TODO: remove pwd? we have it as an applet...
680 BLTIN("pwd" , builtin_pwd, "Print current directory"),
681 BLTIN("read" , builtin_read, "Input environment variable"),
682// BLTIN("return", builtin_not_written, "Return from a function"),
683 BLTIN("set" , builtin_set, "Set/unset shell local variables"),
684 BLTIN("shift" , builtin_shift, "Shift positional parameters"),
685// BLTIN("trap" , builtin_not_written, "Trap signals"),
Denis Vlasenko83506862007-11-23 13:11:42 +0000686 BLTIN("test" , builtin_test, "Test condition"),
Denis Vlasenko06810332007-05-21 23:30:54 +0000687// BLTIN("ulimit", builtin_not_written, "Controls resource limits"),
688 BLTIN("umask" , builtin_umask, "Sets file creation mask"),
689 BLTIN("unset" , builtin_unset, "Unset environment variable"),
690 BLTIN("." , builtin_source, "Source-in and run commands in a file"),
691#if ENABLE_HUSH_HELP
692 BLTIN("help" , builtin_help, "List shell built-in commands"),
693#endif
694 BLTIN(NULL, NULL, NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000695};
696
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000697#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000698
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000699/* move to libbb? */
700static void signal_SA_RESTART(int sig, void (*handler)(int))
701{
702 struct sigaction sa;
703 sa.sa_handler = handler;
704 sa.sa_flags = SA_RESTART;
705 sigemptyset(&sa.sa_mask);
706 sigaction(sig, &sa, NULL);
707}
708
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000709/* Signals are grouped, we handle them in batches */
710static void set_fatal_sighandler(void (*handler)(int))
711{
712 signal(SIGILL , handler);
713 signal(SIGTRAP, handler);
714 signal(SIGABRT, handler);
715 signal(SIGFPE , handler);
716 signal(SIGBUS , handler);
717 signal(SIGSEGV, handler);
718 /* bash 3.2 seems to handle these just like 'fatal' ones */
719 signal(SIGHUP , handler);
720 signal(SIGPIPE, handler);
721 signal(SIGALRM, handler);
722}
723static void set_jobctrl_sighandler(void (*handler)(int))
724{
725 signal(SIGTSTP, handler);
726 signal(SIGTTIN, handler);
727 signal(SIGTTOU, handler);
728}
729static void set_misc_sighandler(void (*handler)(int))
730{
731 signal(SIGINT , handler);
732 signal(SIGQUIT, handler);
733 signal(SIGTERM, handler);
734}
735/* SIGCHLD is special and handled separately */
736
737static void set_every_sighandler(void (*handler)(int))
738{
739 set_fatal_sighandler(handler);
740 set_jobctrl_sighandler(handler);
741 set_misc_sighandler(handler);
742 signal(SIGCHLD, handler);
743}
744
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000745static void handler_ctrl_c(int sig)
746{
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000747 debug_printf_jobs("got sig %d\n", sig);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000748// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000749 siglongjmp(toplevel_jb, 1);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000750}
751
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000752static void handler_ctrl_z(int sig)
753{
754 pid_t pid;
755
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000756 debug_printf_jobs("got tty sig %d in pid %d\n", sig, getpid());
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000757 pid = fork();
Denis Vlasenkoc2990322007-05-16 12:57:12 +0000758 if (pid < 0) /* can't fork. Pretend there was no ctrl-Z */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000759 return;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000760 ctrl_z_flag = 1;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000761 if (!pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +0000762 if (ENABLE_HUSH_JOB)
763 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000764 setpgrp();
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000765 debug_printf_jobs("set pgrp for child %d ok\n", getpid());
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000766 set_every_sighandler(SIG_DFL);
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000767 raise(SIGTSTP); /* resend TSTP so that child will be stopped */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000768 debug_printf_jobs("returning in child\n");
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000769 /* return to nofork, it will eventually exit now,
770 * not return back to shell */
771 return;
772 }
773 /* parent */
774 /* finish filling up pipe info */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000775 toplevel_list->pgrp = pid; /* child is in its own pgrp */
776 toplevel_list->progs[0].pid = pid;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000777 /* parent needs to longjmp out of running nofork.
778 * we will "return" exitcode 0, with child put in background */
779// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000780 debug_printf_jobs("siglongjmp in parent\n");
781 siglongjmp(toplevel_jb, 1);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000782}
783
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000784/* Restores tty foreground process group, and exits.
785 * May be called as signal handler for fatal signal
786 * (will faithfully resend signal to itself, producing correct exit state)
787 * or called directly with -EXITCODE.
788 * We also call it if xfunc is exiting. */
789static void sigexit(int sig) ATTRIBUTE_NORETURN;
790static void sigexit(int sig)
791{
792 sigset_t block_all;
793
794 /* Disable all signals: job control, SIGPIPE, etc. */
795 sigfillset(&block_all);
796 sigprocmask(SIG_SETMASK, &block_all, NULL);
797
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000798 if (interactive_fd)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000799 tcsetpgrp(interactive_fd, saved_tty_pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000800
801 /* Not a signal, just exit */
802 if (sig <= 0)
803 _exit(- sig);
804
805 /* Enable only this sig and kill ourself with it */
806 signal(sig, SIG_DFL);
807 sigdelset(&block_all, sig);
808 sigprocmask(SIG_SETMASK, &block_all, NULL);
809 raise(sig);
810 _exit(1); /* Should not reach it */
811}
812
813/* Restores tty foreground process group, and exits. */
814static void hush_exit(int exitcode) ATTRIBUTE_NORETURN;
815static void hush_exit(int exitcode)
816{
817 fflush(NULL); /* flush all streams */
818 sigexit(- (exitcode & 0xff));
819}
820
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000821#else /* !JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000822
823#define set_fatal_sighandler(handler) ((void)0)
824#define set_jobctrl_sighandler(handler) ((void)0)
825#define set_misc_sighandler(handler) ((void)0)
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000826#define hush_exit(e) exit(e)
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000827
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000828#endif /* JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000829
830
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000831static const char *set_cwd(void)
832{
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000833 if (cwd == bb_msg_unknown)
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000834 cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000835 cwd = xrealloc_getcwd_or_warn((char *)cwd);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000836 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000837 cwd = bb_msg_unknown;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000838 return cwd;
839}
840
Denis Vlasenko83506862007-11-23 13:11:42 +0000841
842/* built-in 'test' handler */
843static int builtin_test(char **argv)
844{
845 int argc = 0;
846 while (*argv) {
847 argc++;
848 argv++;
849 }
850 return test_main(argc, argv - argc);
851}
852
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000853/* built-in 'test' handler */
854static int builtin_echo(char **argv)
855{
856 int argc = 0;
857 while (*argv) {
858 argc++;
859 argv++;
860 }
Denis Vlasenkofe5e23b2007-11-24 02:23:51 +0000861 return echo_main(argc, argv - argc);
Denis Vlasenko5bc593c2007-11-23 21:20:21 +0000862}
863
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000864/* built-in 'eval' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000865static int builtin_eval(char **argv)
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000866{
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000867 int rcode = EXIT_SUCCESS;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000868
Denis Vlasenko1359da62007-04-21 23:27:30 +0000869 if (argv[1]) {
Denis Vlasenko170435c2007-05-23 13:01:10 +0000870 char *str = expand_strvec_to_string(argv + 1);
871 parse_and_run_string(str, PARSEFLAG_EXIT_FROM_LOOP |
Denis Vlasenko219e88d2007-05-21 10:18:23 +0000872 PARSEFLAG_SEMICOLON);
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000873 free(str);
874 rcode = last_return_code;
875 }
876 return rcode;
877}
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000878
Eric Andersen25f27032001-04-26 23:22:31 +0000879/* built-in 'cd <path>' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000880static int builtin_cd(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000881{
Denis Vlasenko170435c2007-05-23 13:01:10 +0000882 const char *newdir;
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000883 if (argv[1] == NULL) {
884 // bash does nothing (exitcode 0) if HOME is ""; if it's unset,
885 // bash says "bash: cd: HOME not set" and does nothing (exitcode 1)
Denis Vlasenko170435c2007-05-23 13:01:10 +0000886 newdir = getenv("HOME") ? : "/";
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000887 } else
Denis Vlasenko1359da62007-04-21 23:27:30 +0000888 newdir = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000889 if (chdir(newdir)) {
890 printf("cd: %s: %s\n", newdir, strerror(errno));
891 return EXIT_FAILURE;
892 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000893 set_cwd();
Eric Andersen25f27032001-04-26 23:22:31 +0000894 return EXIT_SUCCESS;
895}
896
Eric Andersen25f27032001-04-26 23:22:31 +0000897/* built-in 'exec' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000898static int builtin_exec(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000899{
Denis Vlasenko1359da62007-04-21 23:27:30 +0000900 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000901 return EXIT_SUCCESS; /* Really? */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000902 pseudo_exec_argv(argv + 1);
Eric Andersen25f27032001-04-26 23:22:31 +0000903 /* never returns */
904}
905
906/* built-in 'exit' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000907static int builtin_exit(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000908{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000909// TODO: bash does it ONLY on top-level sh exit (+interacive only?)
910 //puts("exit"); /* bash does it */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000911// TODO: warn if we have background jobs: "There are stopped jobs"
912// On second consecutive 'exit', exit anyway.
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000913
Denis Vlasenko1359da62007-04-21 23:27:30 +0000914 if (argv[1] == NULL)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000915 hush_exit(last_return_code);
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000916 /* mimic bash: exit 123abc == exit 255 + error msg */
917 xfunc_error_retval = 255;
918 /* bash: exit -2 == exit 254, no error msg */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000919 hush_exit(xatoi(argv[1]) & 0xff);
Eric Andersen25f27032001-04-26 23:22:31 +0000920}
921
922/* built-in 'export VAR=value' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000923static int builtin_export(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000924{
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000925 const char *value;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000926 char *name = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000927
Eric Andersenf72f5622001-05-15 23:21:41 +0000928 if (name == NULL) {
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000929 // TODO:
930 // ash emits: export VAR='VAL'
931 // bash: declare -x VAR="VAL"
932 // (both also escape as needed (quotes, $, etc))
933 char **e = environ;
934 if (e)
935 while (*e)
936 puts(*e++);
937 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000938 }
Eric Andersenf72f5622001-05-15 23:21:41 +0000939
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000940 value = strchr(name, '=');
941 if (!value) {
942 /* They are exporting something without a =VALUE */
943 struct variable *var;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000944
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000945 var = get_local_var(name);
946 if (var) {
947 var->flg_export = 1;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +0000948 putenv(var->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000949 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000950 /* bash does not return an error when trying to export
951 * an undefined variable. Do likewise. */
952 return EXIT_SUCCESS;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000953 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +0000954
955 set_local_var(xstrdup(name), 1);
956 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000957}
958
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000959#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +0000960/* built-in 'fg' and 'bg' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000961static int builtin_fg_bg(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000962{
Eric Andersen0fcd4472001-05-02 20:12:03 +0000963 int i, jobnum;
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000964 struct pipe *pi;
Eric Andersen25f27032001-04-26 23:22:31 +0000965
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000966 if (!interactive_fd)
Eric Andersenc798b072001-06-22 06:23:03 +0000967 return EXIT_FAILURE;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000968 /* If they gave us no args, assume they want the last backgrounded task */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000969 if (!argv[1]) {
Eric Andersenc798b072001-06-22 06:23:03 +0000970 for (pi = job_list; pi; pi = pi->next) {
971 if (pi->jobid == last_jobid) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000972 goto found;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000973 }
974 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000975 bb_error_msg("%s: no current job", argv[0]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000976 return EXIT_FAILURE;
977 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000978 if (sscanf(argv[1], "%%%d", &jobnum) != 1) {
979 bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000980 return EXIT_FAILURE;
981 }
982 for (pi = job_list; pi; pi = pi->next) {
983 if (pi->jobid == jobnum) {
984 goto found;
Eric Andersen25f27032001-04-26 23:22:31 +0000985 }
986 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000987 bb_error_msg("%s: %d: no such job", argv[0], jobnum);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000988 return EXIT_FAILURE;
989 found:
Denis Vlasenko52881e92007-04-21 13:42:52 +0000990 // TODO: bash prints a string representation
991 // of job being foregrounded (like "sleep 1 | cat")
Denis Vlasenko1359da62007-04-21 23:27:30 +0000992 if (*argv[0] == 'f') {
Eric Andersen028b65b2001-06-28 01:10:11 +0000993 /* Put the job into the foreground. */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000994 tcsetpgrp(interactive_fd, pi->pgrp);
Eric Andersen25f27032001-04-26 23:22:31 +0000995 }
996
997 /* Restart the processes in the job */
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000998 debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_progs, pi->pgrp);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000999 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001000 debug_printf_jobs("reviving pid %d\n", pi->progs[i].pid);
Eric Andersen0fcd4472001-05-02 20:12:03 +00001001 pi->progs[i].is_stopped = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001002 }
1003 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001004
Denis Vlasenkobb81c582007-01-30 22:32:09 +00001005 i = kill(- pi->pgrp, SIGCONT);
1006 if (i < 0) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +00001007 if (errno == ESRCH) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001008 delete_finished_bg_job(pi);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001009 return EXIT_SUCCESS;
Eric Andersen028b65b2001-06-28 01:10:11 +00001010 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001011 bb_perror_msg("kill (SIGCONT)");
Eric Andersen028b65b2001-06-28 01:10:11 +00001012 }
1013 }
Eric Andersen25f27032001-04-26 23:22:31 +00001014
Denis Vlasenko1359da62007-04-21 23:27:30 +00001015 if (*argv[0] == 'f') {
1016 remove_bg_job(pi);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001017 return checkjobs_and_fg_shell(pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001018 }
Eric Andersen25f27032001-04-26 23:22:31 +00001019 return EXIT_SUCCESS;
1020}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001021#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001022
1023/* built-in 'help' handler */
Denis Vlasenko06810332007-05-21 23:30:54 +00001024#if ENABLE_HUSH_HELP
Denis Vlasenko1359da62007-04-21 23:27:30 +00001025static int builtin_help(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +00001026{
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001027 const struct built_in_command *x;
Eric Andersen25f27032001-04-26 23:22:31 +00001028
1029 printf("\nBuilt-in commands:\n");
1030 printf("-------------------\n");
1031 for (x = bltins; x->cmd; x++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001032 printf("%s\t%s\n", x->cmd, x->descr);
1033 }
1034 printf("\n\n");
1035 return EXIT_SUCCESS;
1036}
Denis Vlasenko06810332007-05-21 23:30:54 +00001037#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001038
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001039#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +00001040/* built-in 'jobs' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001041static int builtin_jobs(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +00001042{
1043 struct pipe *job;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001044 const char *status_string;
Eric Andersen25f27032001-04-26 23:22:31 +00001045
Eric Andersenc798b072001-06-22 06:23:03 +00001046 for (job = job_list; job; job = job->next) {
Eric Andersen25f27032001-04-26 23:22:31 +00001047 if (job->running_progs == job->stopped_progs)
1048 status_string = "Stopped";
1049 else
1050 status_string = "Running";
Eric Andersen52a97ca2001-06-22 06:49:26 +00001051
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001052 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
Eric Andersen25f27032001-04-26 23:22:31 +00001053 }
1054 return EXIT_SUCCESS;
1055}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001056#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001057
Eric Andersen25f27032001-04-26 23:22:31 +00001058/* built-in 'pwd' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001059static int builtin_pwd(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +00001060{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001061 puts(set_cwd());
Eric Andersen25f27032001-04-26 23:22:31 +00001062 return EXIT_SUCCESS;
1063}
1064
1065/* built-in 'read VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001066static int builtin_read(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001067{
Denis Vlasenkod67cef22007-06-13 06:47:47 +00001068 char *string;
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001069 const char *name = argv[1] ? argv[1] : "REPLY";
Eric Andersen25f27032001-04-26 23:22:31 +00001070
Denis Vlasenkod67cef22007-06-13 06:47:47 +00001071 string = xmalloc_reads(STDIN_FILENO, xasprintf("%s=", name));
1072 return set_local_var(string, 0);
Eric Andersen25f27032001-04-26 23:22:31 +00001073}
1074
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001075/* built-in 'set [VAR=value]' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001076static int builtin_set(char **argv)
Eric Andersenf72f5622001-05-15 23:21:41 +00001077{
Denis Vlasenko1359da62007-04-21 23:27:30 +00001078 char *temp = argv[1];
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001079 struct variable *e;
Eric Andersenf72f5622001-05-15 23:21:41 +00001080
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001081 if (temp == NULL)
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001082 for (e = top_var; e; e = e->next)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00001083 puts(e->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001084 else
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001085 set_local_var(xstrdup(temp), 0);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00001086
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001087 return EXIT_SUCCESS;
Eric Andersenf72f5622001-05-15 23:21:41 +00001088}
1089
1090
Eric Andersen25f27032001-04-26 23:22:31 +00001091/* Built-in 'shift' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001092static int builtin_shift(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001093{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001094 int n = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001095 if (argv[1]) {
1096 n = atoi(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001097 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001098 if (n >= 0 && n < global_argc) {
Denis Vlasenko004baba2007-05-20 22:22:18 +00001099 global_argv[n] = global_argv[0];
Eric Andersen25f27032001-04-26 23:22:31 +00001100 global_argc -= n;
1101 global_argv += n;
1102 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00001103 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001104 return EXIT_FAILURE;
Eric Andersen25f27032001-04-26 23:22:31 +00001105}
1106
1107/* Built-in '.' handler (read-in and execute commands from file) */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001108static int builtin_source(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001109{
1110 FILE *input;
1111 int status;
1112
Denis Vlasenko1359da62007-04-21 23:27:30 +00001113 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +00001114 return EXIT_FAILURE;
1115
1116 /* XXX search through $PATH is missing */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001117 input = fopen(argv[1], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00001118 if (!input) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001119 bb_error_msg("cannot open '%s'", argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001120 return EXIT_FAILURE;
1121 }
Denis Vlasenkoa0898172007-10-01 09:59:01 +00001122 close_on_exec_on(fileno(input));
Eric Andersen25f27032001-04-26 23:22:31 +00001123
1124 /* Now run the file */
1125 /* XXX argv and argc are broken; need to save old global_argv
1126 * (pointer only is OK!) on this stack frame,
Denis Vlasenko1359da62007-04-21 23:27:30 +00001127 * set global_argv=argv+1, recurse, and restore. */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001128 status = parse_and_run_file(input);
Eric Andersen25f27032001-04-26 23:22:31 +00001129 fclose(input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001130 return status;
Eric Andersen25f27032001-04-26 23:22:31 +00001131}
1132
Denis Vlasenko1359da62007-04-21 23:27:30 +00001133static int builtin_umask(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001134{
Eric Andersen83a2ae22001-05-07 17:59:25 +00001135 mode_t new_umask;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001136 const char *arg = argv[1];
Eric Andersen83a2ae22001-05-07 17:59:25 +00001137 char *end;
1138 if (arg) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001139 new_umask = strtoul(arg, &end, 8);
1140 if (*end != '\0' || end == arg) {
Eric Andersen83a2ae22001-05-07 17:59:25 +00001141 return EXIT_FAILURE;
1142 }
1143 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001144 new_umask = umask(0);
1145 printf("%.3o\n", (unsigned) new_umask);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001146 }
1147 umask(new_umask);
1148 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00001149}
1150
1151/* built-in 'unset VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001152static int builtin_unset(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001153{
Denis Vlasenko3e7b0e62007-05-16 12:52:15 +00001154 /* bash always returns true */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001155 unset_local_var(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001156 return EXIT_SUCCESS;
1157}
1158
Denis Vlasenko06810332007-05-21 23:30:54 +00001159//static int builtin_not_written(char **argv)
1160//{
1161// printf("builtin_%s not written\n", argv[0]);
1162// return EXIT_FAILURE;
1163//}
Eric Andersen83a2ae22001-05-07 17:59:25 +00001164
Eric Andersen25f27032001-04-26 23:22:31 +00001165static int b_check_space(o_string *o, int len)
1166{
1167 /* It would be easy to drop a more restrictive policy
1168 * in here, such as setting a maximum string length */
1169 if (o->length + len > o->maxlen) {
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001170 /* assert(data == NULL || o->maxlen != 0); */
Denis Vlasenkoef36ead2007-05-02 15:34:47 +00001171 o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001172 o->data = xrealloc(o->data, 1 + o->maxlen);
Eric Andersen25f27032001-04-26 23:22:31 +00001173 }
1174 return o->data == NULL;
1175}
1176
1177static int b_addchr(o_string *o, int ch)
1178{
Denis Vlasenko831dcc42007-05-16 15:05:36 +00001179 debug_printf("b_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001180 if (b_check_space(o, 1))
1181 return B_NOSPAC;
Eric Andersen25f27032001-04-26 23:22:31 +00001182 o->data[o->length] = ch;
1183 o->length++;
1184 o->data[o->length] = '\0';
1185 return 0;
1186}
1187
1188static void b_reset(o_string *o)
1189{
1190 o->length = 0;
1191 o->nonnull = 0;
Denis Vlasenkod65ea392007-10-01 10:02:25 +00001192 if (o->data)
1193 o->data[0] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00001194}
1195
1196static void b_free(o_string *o)
1197{
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001198 free(o->data);
Denis Vlasenkod65ea392007-10-01 10:02:25 +00001199 memset(o, 0, sizeof(*o));
Eric Andersen25f27032001-04-26 23:22:31 +00001200}
1201
1202/* My analysis of quoting semantics tells me that state information
1203 * is associated with a destination, not a source.
1204 */
1205static int b_addqchr(o_string *o, int ch, int quote)
1206{
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00001207 if (quote && strchr("*?[\\", ch)) {
Eric Andersen25f27032001-04-26 23:22:31 +00001208 int rc;
1209 rc = b_addchr(o, '\\');
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001210 if (rc)
1211 return rc;
Eric Andersen25f27032001-04-26 23:22:31 +00001212 }
1213 return b_addchr(o, ch);
1214}
1215
Eric Andersen25f27032001-04-26 23:22:31 +00001216static int static_get(struct in_str *i)
1217{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001218 int ch = *i->p++;
1219 if (ch == '\0') return EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001220 return ch;
1221}
1222
1223static int static_peek(struct in_str *i)
1224{
1225 return *i->p;
1226}
1227
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001228#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001229#if ENABLE_FEATURE_EDITING
Rob Landley88621d72006-08-29 19:41:06 +00001230static void cmdedit_set_initial_prompt(void)
Eric Andersen25f27032001-04-26 23:22:31 +00001231{
Denis Vlasenko38f63192007-01-22 09:03:07 +00001232#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001233 PS1 = NULL;
1234#else
1235 PS1 = getenv("PS1");
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001236 if (PS1 == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +00001237 PS1 = "\\w \\$ ";
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001238#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001239}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001240#endif /* EDITING */
Eric Andersen25f27032001-04-26 23:22:31 +00001241
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001242static const char* setup_prompt_string(int promptmode)
Eric Andersen25f27032001-04-26 23:22:31 +00001243{
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001244 const char *prompt_str;
1245 debug_printf("setup_prompt_string %d ", promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001246#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001247 /* Set up the prompt */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001248 if (promptmode == 0) { /* PS1 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001249 free((char*)PS1);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001250 PS1 = xasprintf("%s %c ", cwd, (geteuid() != 0) ? '$' : '#');
1251 prompt_str = PS1;
Eric Andersen25f27032001-04-26 23:22:31 +00001252 } else {
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001253 prompt_str = PS2;
Eric Andersen25f27032001-04-26 23:22:31 +00001254 }
1255#else
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001256 prompt_str = (promptmode == 0) ? PS1 : PS2;
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001257#endif
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001258 debug_printf("result '%s'\n", prompt_str);
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001259 return prompt_str;
Eric Andersen25f27032001-04-26 23:22:31 +00001260}
1261
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001262static void get_user_input(struct in_str *i)
Eric Andersen25f27032001-04-26 23:22:31 +00001263{
Denis Vlasenko0937be52007-04-28 16:47:08 +00001264 int r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001265 const char *prompt_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001266
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001267 prompt_str = setup_prompt_string(i->promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001268#if ENABLE_FEATURE_EDITING
Denis Vlasenko170435c2007-05-23 13:01:10 +00001269 /* Enable command line editing only while a command line
1270 * is actually being read; otherwise, we'll end up bequeathing
1271 * atexit() handlers and other unwanted stuff to our
1272 * child processes (rob@sysgo.de) */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001273 r = read_line_input(prompt_str, user_input_buf, BUFSIZ-1, line_input_state);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001274 i->eof_flag = (r < 0);
1275 if (i->eof_flag) { /* EOF/error detected */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001276 user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */
1277 user_input_buf[1] = '\0';
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001278 }
Eric Andersen25f27032001-04-26 23:22:31 +00001279#else
1280 fputs(prompt_str, stdout);
1281 fflush(stdout);
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001282 user_input_buf[0] = r = fgetc(i->file);
1283 /*user_input_buf[1] = '\0'; - already is and never changed */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001284 i->eof_flag = (r == EOF);
Eric Andersen25f27032001-04-26 23:22:31 +00001285#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00001286 i->p = user_input_buf;
Eric Andersen25f27032001-04-26 23:22:31 +00001287}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001288#endif /* INTERACTIVE */
Eric Andersen25f27032001-04-26 23:22:31 +00001289
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001290/* This is the magic location that prints prompts
Eric Andersen25f27032001-04-26 23:22:31 +00001291 * and gets data back from the user */
1292static int file_get(struct in_str *i)
1293{
1294 int ch;
1295
Eric Andersen25f27032001-04-26 23:22:31 +00001296 /* If there is data waiting, eat it up */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001297 if (i->p && *i->p) {
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001298#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001299 take_cached:
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001300#endif
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001301 ch = *i->p++;
1302 if (i->eof_flag && !*i->p)
1303 ch = EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001304 } else {
1305 /* need to double check i->file because we might be doing something
1306 * more complicated by now, like sourcing or substituting. */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001307#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001308 if (interactive_fd && i->promptme && i->file == stdin) {
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001309 do {
1310 get_user_input(i);
1311 } while (!*i->p); /* need non-empty line */
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001312 i->promptmode = 1; /* PS2 */
1313 i->promptme = 0;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001314 goto take_cached;
Eric Andersen25f27032001-04-26 23:22:31 +00001315 }
Denis Vlasenko96f67dc2007-05-17 13:02:41 +00001316#endif
1317 ch = fgetc(i->file);
Eric Andersen25f27032001-04-26 23:22:31 +00001318 }
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001319 debug_printf("file_get: got a '%c' %d\n", ch, ch);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001320#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001321 if (ch == '\n')
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001322 i->promptme = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001323#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001324 return ch;
1325}
1326
1327/* All the callers guarantee this routine will never be
1328 * used right after a newline, so prompting is not needed.
1329 */
1330static int file_peek(struct in_str *i)
1331{
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001332 int ch;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001333 if (i->p && *i->p) {
1334 if (i->eof_flag && !i->p[1])
1335 return EOF;
1336 return *i->p;
Eric Andersen25f27032001-04-26 23:22:31 +00001337 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001338 ch = fgetc(i->file);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001339 i->eof_flag = (ch == EOF);
1340 i->peek_buf[0] = ch;
1341 i->peek_buf[1] = '\0';
1342 i->p = i->peek_buf;
1343 debug_printf("file_peek: got a '%c' %d\n", *i->p, *i->p);
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00001344 return ch;
Eric Andersen25f27032001-04-26 23:22:31 +00001345}
1346
1347static void setup_file_in_str(struct in_str *i, FILE *f)
1348{
1349 i->peek = file_peek;
1350 i->get = file_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001351#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001352 i->promptme = 1;
1353 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001354#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001355 i->file = f;
1356 i->p = NULL;
1357}
1358
1359static void setup_string_in_str(struct in_str *i, const char *s)
1360{
1361 i->peek = static_peek;
1362 i->get = static_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001363#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00001364 i->promptme = 1;
1365 i->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001366#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001367 i->p = s;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00001368 i->eof_flag = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001369}
1370
Eric Andersen25f27032001-04-26 23:22:31 +00001371/* squirrel != NULL means we squirrel away copies of stdin, stdout,
1372 * and stderr if they are redirected. */
1373static int setup_redirects(struct child_prog *prog, int squirrel[])
1374{
1375 int openfd, mode;
1376 struct redir_struct *redir;
1377
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001378 for (redir = prog->redirects; redir; redir = redir->next) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00001379 if (redir->dup == -1 && redir->glob_word == NULL) {
Eric Andersen817e73c2001-06-06 17:56:09 +00001380 /* something went wrong in the parse. Pretend it didn't happen */
1381 continue;
1382 }
Eric Andersen25f27032001-04-26 23:22:31 +00001383 if (redir->dup == -1) {
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001384 char *p;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001385 mode = redir_table[redir->type].mode;
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00001386 p = expand_string_to_string(redir->glob_word[0]);
1387 openfd = open_or_warn(p, mode);
1388 free(p);
Eric Andersen25f27032001-04-26 23:22:31 +00001389 if (openfd < 0) {
1390 /* this could get lost if stderr has been redirected, but
1391 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersen25f27032001-04-26 23:22:31 +00001392 return 1;
1393 }
1394 } else {
1395 openfd = redir->dup;
1396 }
1397
1398 if (openfd != redir->fd) {
1399 if (squirrel && redir->fd < 3) {
1400 squirrel[redir->fd] = dup(redir->fd);
1401 }
Eric Andersen83a2ae22001-05-07 17:59:25 +00001402 if (openfd == -3) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001403 //close(openfd); // close(-3) ??!
Eric Andersen83a2ae22001-05-07 17:59:25 +00001404 } else {
1405 dup2(openfd, redir->fd);
Matt Kraaic616e532001-06-05 16:50:08 +00001406 if (redir->dup == -1)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001407 close(openfd);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001408 }
Eric Andersen25f27032001-04-26 23:22:31 +00001409 }
1410 }
1411 return 0;
1412}
1413
1414static void restore_redirects(int squirrel[])
1415{
1416 int i, fd;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001417 for (i = 0; i < 3; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001418 fd = squirrel[i];
1419 if (fd != -1) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00001420 /* We simply die on error */
1421 xmove_fd(fd, i);
Eric Andersen25f27032001-04-26 23:22:31 +00001422 }
1423 }
1424}
1425
Denis Vlasenko05743d72008-02-10 12:10:08 +00001426/* Called after [v]fork() in run_pipe(), or from builtin_exec().
Denis Vlasenko8412d792007-10-01 09:59:47 +00001427 * Never returns.
1428 * XXX no exit() here. If you don't exec, use _exit instead.
Eric Andersen94ac2442001-05-22 19:05:18 +00001429 * The at_exit handlers apparently confuse the calling process,
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001430 * in particular stdin handling. Not sure why? -- because of vfork! (vda) */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001431static void pseudo_exec_argv(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001432{
Eric Andersen78a7c992001-05-15 16:30:25 +00001433 int i, rcode;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001434 char *p;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001435 const struct built_in_command *x;
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001436
Denis Vlasenko1359da62007-04-21 23:27:30 +00001437 for (i = 0; is_assignment(argv[i]); i++) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001438 debug_printf_exec("pid %d environment modification: %s\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001439 getpid(), argv[i]);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001440// FIXME: vfork case??
Denis Vlasenko170435c2007-05-23 13:01:10 +00001441 p = expand_string_to_string(argv[i]);
1442 putenv(p);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001443 }
1444 argv += i;
1445 /* If a variable is assigned in a forest, and nobody listens,
1446 * was it ever really set?
1447 */
Denis Vlasenko05743d72008-02-10 12:10:08 +00001448 if (!argv[0])
Denis Vlasenko1359da62007-04-21 23:27:30 +00001449 _exit(EXIT_SUCCESS);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001450
Denis Vlasenko170435c2007-05-23 13:01:10 +00001451 argv = expand_strvec_to_strvec(argv);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001452
Denis Vlasenko1359da62007-04-21 23:27:30 +00001453 /*
1454 * Check if the command matches any of the builtins.
1455 * Depending on context, this might be redundant. But it's
1456 * easier to waste a few CPU cycles than it is to figure out
1457 * if this is one of those cases.
1458 */
1459 for (x = bltins; x->cmd; x++) {
1460 if (strcmp(argv[0], x->cmd) == 0) {
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001461 debug_printf_exec("running builtin '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001462 rcode = x->function(argv);
1463 fflush(stdout);
1464 _exit(rcode);
Eric Andersen94ac2442001-05-22 19:05:18 +00001465 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001466 }
Eric Andersen78a7c992001-05-15 16:30:25 +00001467
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001468 /* Check if the command matches any busybox applets */
Denis Vlasenko80d14be2007-04-10 23:03:30 +00001469#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001470 if (strchr(argv[0], '/') == NULL) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001471 int a = find_applet_by_name(argv[0]);
1472 if (a >= 0) {
1473 if (APPLET_IS_NOEXEC(a)) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001474 debug_printf_exec("running applet '%s'\n", argv[0]);
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001475// is it ok that run_applet_no_and_exit() does exit(), not _exit()?
1476 run_applet_no_and_exit(a, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001477 }
1478 /* re-exec ourselves with the new arguments */
1479 debug_printf_exec("re-execing applet '%s'\n", argv[0]);
Denis Vlasenkobdbbb7e2007-06-08 15:02:55 +00001480 execvp(bb_busybox_exec_path, argv);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001481 /* If they called chroot or otherwise made the binary no longer
1482 * executable, fall through */
1483 }
1484 }
Eric Andersenaac75e52001-04-30 18:18:45 +00001485#endif
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001486
1487 debug_printf_exec("execing '%s'\n", argv[0]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001488 execvp(argv[0], argv);
1489 bb_perror_msg("cannot exec '%s'", argv[0]);
1490 _exit(1);
1491}
1492
Denis Vlasenko05743d72008-02-10 12:10:08 +00001493/* Called after [v]fork() in run_pipe()
Denis Vlasenko8412d792007-10-01 09:59:47 +00001494 */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001495static void pseudo_exec(struct child_prog *child)
1496{
Denis Vlasenkof2fffd02007-05-02 23:39:04 +00001497// FIXME: buggy wrt NOMMU! Must not modify any global data
Denis Vlasenko05743d72008-02-10 12:10:08 +00001498// until it does exec/_exit, but currently it does
1499// (puts malloc'ed stuff into environment)
1500 if (child->argv)
Denis Vlasenko1359da62007-04-21 23:27:30 +00001501 pseudo_exec_argv(child->argv);
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001502
1503 if (child->group) {
Denis Vlasenko8412d792007-10-01 09:59:47 +00001504#if !BB_MMU
Denis Vlasenko3b492162007-12-24 14:26:57 +00001505 bb_error_msg_and_die("nested lists are not supported on NOMMU");
Denis Vlasenko8412d792007-10-01 09:59:47 +00001506#else
Denis Vlasenko3b492162007-12-24 14:26:57 +00001507 int rcode;
1508
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001509#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko05743d72008-02-10 12:10:08 +00001510// run_list_level now takes care of it?
1511// debug_printf_exec("pseudo_exec: setting interactive_fd=0\n");
1512// interactive_fd = 0; /* crucial!!!! */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001513#endif
Denis Vlasenko05743d72008-02-10 12:10:08 +00001514 debug_printf_exec("pseudo_exec: run_list\n");
1515 rcode = run_list(child->group);
Eric Andersenbf7df042001-05-23 22:18:35 +00001516 /* OK to leak memory by not calling free_pipe_list,
Eric Andersen25f27032001-04-26 23:22:31 +00001517 * since this process is about to exit */
Eric Andersen94ac2442001-05-22 19:05:18 +00001518 _exit(rcode);
Denis Vlasenko8412d792007-10-01 09:59:47 +00001519#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001520 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001521
1522 /* Can happen. See what bash does with ">foo" by itself. */
1523 debug_printf("trying to pseudo_exec null command\n");
1524 _exit(EXIT_SUCCESS);
Eric Andersen25f27032001-04-26 23:22:31 +00001525}
1526
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001527#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001528static const char *get_cmdtext(struct pipe *pi)
1529{
1530 char **argv;
1531 char *p;
1532 int len;
1533
1534 /* This is subtle. ->cmdtext is created only on first backgrounding.
1535 * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...)
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001536 * On subsequent bg argv is trashed, but we won't use it */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001537 if (pi->cmdtext)
1538 return pi->cmdtext;
1539 argv = pi->progs[0].argv;
1540 if (!argv || !argv[0])
1541 return (pi->cmdtext = xzalloc(1));
1542
1543 len = 0;
1544 do len += strlen(*argv) + 1; while (*++argv);
1545 pi->cmdtext = p = xmalloc(len);
1546 argv = pi->progs[0].argv;
1547 do {
1548 len = strlen(*argv);
1549 memcpy(p, *argv, len);
1550 p += len;
1551 *p++ = ' ';
1552 } while (*++argv);
1553 p[-1] = '\0';
1554 return pi->cmdtext;
1555}
1556
Eric Andersenbafd94f2001-05-02 16:11:59 +00001557static void insert_bg_job(struct pipe *pi)
1558{
1559 struct pipe *thejob;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001560 int i;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001561
1562 /* Linear search for the ID of the job to use */
1563 pi->jobid = 1;
Eric Andersenc798b072001-06-22 06:23:03 +00001564 for (thejob = job_list; thejob; thejob = thejob->next)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001565 if (thejob->jobid >= pi->jobid)
1566 pi->jobid = thejob->jobid + 1;
1567
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001568 /* Add thejob to the list of running jobs */
Eric Andersenc798b072001-06-22 06:23:03 +00001569 if (!job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001570 thejob = job_list = xmalloc(sizeof(*thejob));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001571 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001572 for (thejob = job_list; thejob->next; thejob = thejob->next)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001573 continue;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001574 thejob->next = xmalloc(sizeof(*thejob));
1575 thejob = thejob->next;
1576 }
1577
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001578 /* Physically copy the struct job */
Eric Andersen0fcd4472001-05-02 20:12:03 +00001579 memcpy(thejob, pi, sizeof(struct pipe));
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001580 thejob->progs = xzalloc(sizeof(pi->progs[0]) * pi->num_progs);
1581 /* We cannot copy entire pi->progs[] vector! Double free()s will happen */
1582 for (i = 0; i < pi->num_progs; i++) {
1583// TODO: do we really need to have so many fields which are just dead weight
1584// at execution stage?
1585 thejob->progs[i].pid = pi->progs[i].pid;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001586 /* all other fields are not used and stay zero */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001587 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001588 thejob->next = NULL;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001589 thejob->cmdtext = xstrdup(get_cmdtext(pi));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001590
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001591 /* We don't wait for background thejobs to return -- append it
Eric Andersenbafd94f2001-05-02 16:11:59 +00001592 to the list of backgrounded thejobs and leave it alone */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001593 printf("[%d] %d %s\n", thejob->jobid, thejob->progs[0].pid, thejob->cmdtext);
Eric Andersen1a6d39b2001-05-08 05:11:54 +00001594 last_bg_pid = thejob->progs[0].pid;
Eric Andersenc798b072001-06-22 06:23:03 +00001595 last_jobid = thejob->jobid;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001596}
1597
Eric Andersenbafd94f2001-05-02 16:11:59 +00001598static void remove_bg_job(struct pipe *pi)
1599{
1600 struct pipe *prev_pipe;
1601
Eric Andersenc798b072001-06-22 06:23:03 +00001602 if (pi == job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001603 job_list = pi->next;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001604 } else {
Eric Andersenc798b072001-06-22 06:23:03 +00001605 prev_pipe = job_list;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001606 while (prev_pipe->next != pi)
1607 prev_pipe = prev_pipe->next;
1608 prev_pipe->next = pi->next;
1609 }
Eric Andersen028b65b2001-06-28 01:10:11 +00001610 if (job_list)
1611 last_jobid = job_list->jobid;
1612 else
1613 last_jobid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001614}
Eric Andersen028b65b2001-06-28 01:10:11 +00001615
Denis Vlasenko1359da62007-04-21 23:27:30 +00001616/* remove a backgrounded job */
1617static void delete_finished_bg_job(struct pipe *pi)
1618{
1619 remove_bg_job(pi);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001620 pi->stopped_progs = 0;
Eric Andersenbf7df042001-05-23 22:18:35 +00001621 free_pipe(pi, 0);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001622 free(pi);
1623}
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001624#endif /* JOB */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001625
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001626/* Checks to see if any processes have exited -- if they
Eric Andersenbafd94f2001-05-02 16:11:59 +00001627 have, figure out why and see if a job has completed */
Eric Andersenc798b072001-06-22 06:23:03 +00001628static int checkjobs(struct pipe* fg_pipe)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001629{
Eric Andersenc798b072001-06-22 06:23:03 +00001630 int attributes;
1631 int status;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001632#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00001633 int prognum = 0;
1634 struct pipe *pi;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001635#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001636 pid_t childpid;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001637 int rcode = 0;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001638
Eric Andersenc798b072001-06-22 06:23:03 +00001639 attributes = WUNTRACED;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001640 if (fg_pipe == NULL) {
Eric Andersenc798b072001-06-22 06:23:03 +00001641 attributes |= WNOHANG;
1642 }
1643
Denis Vlasenko1359da62007-04-21 23:27:30 +00001644/* Do we do this right?
1645 * bash-3.00# sleep 20 | false
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001646 * <ctrl-Z pressed>
Denis Vlasenko1359da62007-04-21 23:27:30 +00001647 * [3]+ Stopped sleep 20 | false
1648 * bash-3.00# echo $?
1649 * 1 <========== bg pipe is not fully done, but exitcode is already known!
1650 */
1651
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001652//FIXME: non-interactive bash does not continue even if all processes in fg pipe
1653//are stopped. Testcase: "cat | cat" in a script (not on command line)
1654// + killall -STOP cat
1655
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001656 wait_more:
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001657// TODO: safe_waitpid?
Eric Andersenc798b072001-06-22 06:23:03 +00001658 while ((childpid = waitpid(-1, &status, attributes)) > 0) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001659 const int dead = WIFEXITED(status) || WIFSIGNALED(status);
1660
1661#ifdef DEBUG_SHELL_JOBS
1662 if (WIFSTOPPED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001663 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001664 childpid, WSTOPSIG(status), WEXITSTATUS(status));
1665 if (WIFSIGNALED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001666 debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001667 childpid, WTERMSIG(status), WEXITSTATUS(status));
1668 if (WIFEXITED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001669 debug_printf_jobs("pid %d exited, exitcode %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001670 childpid, WEXITSTATUS(status));
1671#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001672 /* Were we asked to wait for fg pipe? */
Eric Andersenc798b072001-06-22 06:23:03 +00001673 if (fg_pipe) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001674 int i;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001675 for (i = 0; i < fg_pipe->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001676 debug_printf_jobs("check pid %d\n", fg_pipe->progs[i].pid);
Eric Andersenc798b072001-06-22 06:23:03 +00001677 if (fg_pipe->progs[i].pid == childpid) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001678 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001679 if (dead) {
1680 fg_pipe->progs[i].pid = 0;
1681 fg_pipe->running_progs--;
Denis Vlasenko05743d72008-02-10 12:10:08 +00001682 if (i == fg_pipe->num_progs - 1)
Denis Vlasenko1359da62007-04-21 23:27:30 +00001683 /* last process gives overall exitstatus */
1684 rcode = WEXITSTATUS(status);
1685 } else {
1686 fg_pipe->progs[i].is_stopped = 1;
1687 fg_pipe->stopped_progs++;
1688 }
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001689 debug_printf_jobs("fg_pipe: running_progs %d stopped_progs %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001690 fg_pipe->running_progs, fg_pipe->stopped_progs);
1691 if (fg_pipe->running_progs - fg_pipe->stopped_progs <= 0) {
1692 /* All processes in fg pipe have exited/stopped */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001693#if ENABLE_HUSH_JOB
Denis Vlasenko1359da62007-04-21 23:27:30 +00001694 if (fg_pipe->running_progs)
1695 insert_bg_job(fg_pipe);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001696#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001697 return rcode;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001698 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001699 /* There are still running processes in the fg pipe */
1700 goto wait_more;
Eric Andersenc798b072001-06-22 06:23:03 +00001701 }
1702 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001703 /* fall through to searching process in bg pipes */
Eric Andersenc798b072001-06-22 06:23:03 +00001704 }
1705
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001706#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001707 /* We asked to wait for bg or orphaned children */
1708 /* No need to remember exitcode in this case */
Eric Andersenc798b072001-06-22 06:23:03 +00001709 for (pi = job_list; pi; pi = pi->next) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001710 prognum = 0;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001711 while (prognum < pi->num_progs) {
1712 if (pi->progs[prognum].pid == childpid)
1713 goto found_pi_and_prognum;
Eric Andersen52a97ca2001-06-22 06:49:26 +00001714 prognum++;
1715 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001716 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001717#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001718
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001719 /* Happens when shell is used as init process (init=/bin/sh) */
1720 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001721 goto wait_more;
Eric Andersenaeb44c42001-05-22 20:29:00 +00001722
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001723#if ENABLE_HUSH_JOB
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001724 found_pi_and_prognum:
Denis Vlasenko1359da62007-04-21 23:27:30 +00001725 if (dead) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001726 /* child exited */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001727 pi->progs[prognum].pid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001728 pi->running_progs--;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001729 if (!pi->running_progs) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001730 printf(JOB_STATUS_FORMAT, pi->jobid,
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001731 "Done", pi->cmdtext);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001732 delete_finished_bg_job(pi);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001733 }
1734 } else {
1735 /* child stopped */
1736 pi->stopped_progs++;
1737 pi->progs[prognum].is_stopped = 1;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001738 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001739#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001740 }
1741
Denis Vlasenko1359da62007-04-21 23:27:30 +00001742 /* wait found no children or failed */
1743
1744 if (childpid && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001745 bb_perror_msg("waitpid");
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001746 return rcode;
Eric Andersenada18ff2001-05-21 16:18:22 +00001747}
1748
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001749#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +00001750static int checkjobs_and_fg_shell(struct pipe* fg_pipe)
1751{
1752 pid_t p;
1753 int rcode = checkjobs(fg_pipe);
1754 /* Job finished, move the shell to the foreground */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001755 p = getpgid(0); /* pgid of our process */
1756 debug_printf_jobs("fg'ing ourself: getpgid(0)=%d\n", (int)p);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001757 if (tcsetpgrp(interactive_fd, p) && errno != ENOTTY)
1758 bb_perror_msg("tcsetpgrp-4a");
1759 return rcode;
1760}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001761#endif
Denis Vlasenko52881e92007-04-21 13:42:52 +00001762
Denis Vlasenko05743d72008-02-10 12:10:08 +00001763/* run_pipe() starts all the jobs, but doesn't wait for anything
Eric Andersenc798b072001-06-22 06:23:03 +00001764 * to finish. See checkjobs().
Eric Andersen25f27032001-04-26 23:22:31 +00001765 *
1766 * return code is normally -1, when the caller has to wait for children
1767 * to finish to determine the exit status of the pipe. If the pipe
1768 * is a simple builtin command, however, the action is done by the
Denis Vlasenko05743d72008-02-10 12:10:08 +00001769 * time run_pipe returns, and the exit code is provided as the
Eric Andersen25f27032001-04-26 23:22:31 +00001770 * return value.
1771 *
1772 * The input of the pipe is always stdin, the output is always
1773 * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
1774 * because it tries to avoid running the command substitution in
1775 * subshell, when that is in fact necessary. The subshell process
1776 * now has its stdout directed to the input of the appropriate pipe,
1777 * so this routine is noticeably simpler.
Denis Vlasenko170435c2007-05-23 13:01:10 +00001778 *
1779 * Returns -1 only if started some children. IOW: we have to
1780 * mask out retvals of builtins etc with 0xff!
Eric Andersen25f27032001-04-26 23:22:31 +00001781 */
Denis Vlasenko05743d72008-02-10 12:10:08 +00001782static int run_pipe(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00001783{
1784 int i;
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001785 int nextin;
1786 int pipefds[2]; /* pipefds[0] is for reading */
Rob Landley53702e52006-07-19 21:43:53 +00001787 struct child_prog *child;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001788 const struct built_in_command *x;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001789 char *p;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001790 /* it is not always needed, but we aim to smaller code */
1791 int squirrel[] = { -1, -1, -1 };
1792 int rcode;
Denis Vlasenko52881e92007-04-21 13:42:52 +00001793 const int single_fg = (pi->num_progs == 1 && pi->followup != PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00001794
Denis Vlasenko05743d72008-02-10 12:10:08 +00001795 debug_printf_exec("run_pipe start: single_fg=%d\n", single_fg);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001796
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001797#if ENABLE_HUSH_JOB
Eric Andersenada18ff2001-05-21 16:18:22 +00001798 pi->pgrp = -1;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001799#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001800 pi->running_progs = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001801 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001802
1803 /* Check if this is a simple builtin (not part of a pipe).
1804 * Builtins within pipes have to fork anyway, and are handled in
1805 * pseudo_exec. "echo foo | read bar" doesn't work on bash, either.
1806 */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001807 child = &(pi->progs[0]);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001808 if (single_fg && child->group && child->subshell == 0) {
Eric Andersen04407e52001-06-07 16:42:05 +00001809 debug_printf("non-subshell grouping\n");
1810 setup_redirects(child, squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001811 debug_printf_exec(": run_list\n");
1812 rcode = run_list(child->group) & 0xff;
Eric Andersen04407e52001-06-07 16:42:05 +00001813 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001814 debug_printf_exec("run_pipe return %d\n", rcode);
1815 return rcode;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001816 }
1817
Denis Vlasenko1359da62007-04-21 23:27:30 +00001818 if (single_fg && child->argv != NULL) {
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001819 char **argv_expanded;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001820 char **argv = child->argv;
1821
1822 for (i = 0; is_assignment(argv[i]); i++)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001823 continue;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001824 if (i != 0 && argv[i] == NULL) {
Eric Andersen78a7c992001-05-15 16:30:25 +00001825 /* assignments, but no command: set the local environment */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001826 for (i = 0; argv[i] != NULL; i++) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001827 debug_printf("local environment set: %s\n", argv[i]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001828 p = expand_string_to_string(argv[i]);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00001829 set_local_var(p, 0);
Eric Andersen78a7c992001-05-15 16:30:25 +00001830 }
1831 return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */
1832 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001833 for (i = 0; is_assignment(argv[i]); i++) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00001834 p = expand_string_to_string(argv[i]);
1835 //sp: child->sp--;
1836 putenv(p);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001837 }
Eric Andersen25f27032001-04-26 23:22:31 +00001838 for (x = bltins; x->cmd; x++) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001839 if (strcmp(argv[i], x->cmd) == 0) {
1840 if (x->function == builtin_exec && argv[i+1] == NULL) {
Eric Andersen83a2ae22001-05-07 17:59:25 +00001841 debug_printf("magic exec\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001842 setup_redirects(child, NULL);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001843 return EXIT_SUCCESS;
1844 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001845 debug_printf("builtin inline %s\n", argv[0]);
Eric Andersen25f27032001-04-26 23:22:31 +00001846 /* XXX setup_redirects acts on file descriptors, not FILEs.
1847 * This is perfect for work that comes after exec().
1848 * Is it really safe for inline use? Experimentally,
1849 * things seem to work with glibc. */
1850 setup_redirects(child, squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001851 debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv[i+1]);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001852 //sp: if (child->sp) /* btw we can do it unconditionally... */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001853 argv_expanded = expand_strvec_to_strvec(argv + i);
1854 rcode = x->function(argv_expanded) & 0xff;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001855 free(argv_expanded);
Eric Andersen25f27032001-04-26 23:22:31 +00001856 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001857 debug_printf_exec("run_pipe return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00001858 return rcode;
1859 }
1860 }
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001861#if ENABLE_FEATURE_SH_STANDALONE
1862 {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +00001863 int a = find_applet_by_name(argv[i]);
1864 if (a >= 0 && APPLET_IS_NOFORK(a)) {
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001865 setup_redirects(child, squirrel);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001866 save_nofork_data(&nofork_save);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001867 argv_expanded = argv + i;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001868 //sp: if (child->sp)
Denis Vlasenko170435c2007-05-23 13:01:10 +00001869 argv_expanded = expand_strvec_to_strvec(argv + i);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00001870 debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv_expanded[0], argv_expanded[1]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00001871 rcode = run_nofork_applet_prime(&nofork_save, a, argv_expanded) & 0xff;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00001872 free(argv_expanded);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001873 restore_redirects(squirrel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001874 debug_printf_exec("run_pipe return %d\n", rcode);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001875 return rcode;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001876 }
1877 }
1878#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001879 }
1880
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001881 /* Disable job control signals for shell (parent) and
1882 * for initial child code after fork */
1883 set_jobctrl_sighandler(SIG_IGN);
1884
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001885 /* Going to fork a child per each pipe member */
1886 pi->running_progs = 0;
1887 nextin = 0;
1888
Eric Andersen25f27032001-04-26 23:22:31 +00001889 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001890 child = &(pi->progs[i]);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001891 if (child->argv)
1892 debug_printf_exec(": pipe member '%s' '%s'...\n", child->argv[0], child->argv[1]);
1893 else
1894 debug_printf_exec(": pipe member with no argv\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001895
1896 /* pipes are inserted between pairs of commands */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001897 pipefds[0] = 0;
1898 pipefds[1] = 1;
1899 if ((i + 1) < pi->num_progs)
1900 xpipe(pipefds);
Eric Andersen25f27032001-04-26 23:22:31 +00001901
Denis Vlasenko05743d72008-02-10 12:10:08 +00001902 child->pid = BB_MMU ? fork() : vfork();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001903 if (!child->pid) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00001904 if (ENABLE_HUSH_JOB)
1905 die_sleep = 0; /* let nofork's xfuncs die */
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001906#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001907 /* Every child adds itself to new process group
Denis Vlasenko05743d72008-02-10 12:10:08 +00001908 * with pgid == pid_of_first_child_in_pipe */
Denis Vlasenko170435c2007-05-23 13:01:10 +00001909 if (run_list_level == 1 && interactive_fd) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00001910 pid_t pgrp;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001911 /* Don't do pgrp restore anymore on fatal signals */
1912 set_fatal_sighandler(SIG_DFL);
Denis Vlasenko05743d72008-02-10 12:10:08 +00001913 pgrp = pi->pgrp;
1914 if (pgrp < 0) /* true for 1st process only */
1915 pgrp = getpid();
1916 if (setpgid(0, pgrp) == 0 && pi->followup != PIPE_BG) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001917 /* We do it in *every* child, not just first,
1918 * to avoid races */
Denis Vlasenko05743d72008-02-10 12:10:08 +00001919 tcsetpgrp(interactive_fd, pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001920 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001921 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001922#endif
Denis Vlasenko8412d792007-10-01 09:59:47 +00001923 xmove_fd(nextin, 0);
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001924 xmove_fd(pipefds[1], 1); /* write end */
1925 if (pipefds[0] > 1)
1926 close(pipefds[0]); /* read end */
Eric Andersen25f27032001-04-26 23:22:31 +00001927 /* Like bash, explicit redirects override pipes,
1928 * and the pipe fd is available for dup'ing. */
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001929 setup_redirects(child, NULL);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001930
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001931 /* Restore default handlers just prior to exec */
1932 set_jobctrl_sighandler(SIG_DFL);
1933 set_misc_sighandler(SIG_DFL);
1934 signal(SIGCHLD, SIG_DFL);
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001935 pseudo_exec(child); /* does not return */
Eric Andersen25f27032001-04-26 23:22:31 +00001936 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001937
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001938 if (child->pid < 0) { /* [v]fork failed */
1939 /* Clearly indicate, was it fork or vfork */
Denis Vlasenko05743d72008-02-10 12:10:08 +00001940 bb_perror_msg(BB_MMU ? "fork" : "vfork");
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001941 } else {
1942 pi->running_progs++;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001943#if ENABLE_HUSH_JOB
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001944 /* Second and next children need to know pid of first one */
1945 if (pi->pgrp < 0)
1946 pi->pgrp = child->pid;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001947#endif
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001948 }
Eric Andersen25f27032001-04-26 23:22:31 +00001949
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001950 if (i)
1951 close(nextin);
1952 if ((i + 1) < pi->num_progs)
1953 close(pipefds[1]); /* write end */
1954 /* Pass read (output) pipe end to next iteration */
Eric Andersen25f27032001-04-26 23:22:31 +00001955 nextin = pipefds[0];
1956 }
Denis Vlasenkod2c450c2008-01-08 20:32:12 +00001957
Denis Vlasenko05743d72008-02-10 12:10:08 +00001958 if (!pi->running_progs) {
1959 debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n");
1960 return 1;
1961 }
1962
1963 debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->running_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00001964 return -1;
1965}
1966
Denis Vlasenko4b924f32007-05-30 00:29:55 +00001967#ifndef debug_print_tree
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001968static void debug_print_tree(struct pipe *pi, int lvl)
1969{
1970 static const char *PIPE[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001971 [PIPE_SEQ] = "SEQ",
1972 [PIPE_AND] = "AND",
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00001973 [PIPE_OR ] = "OR" ,
1974 [PIPE_BG ] = "BG" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001975 };
1976 static const char *RES[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001977 [RES_NONE ] = "NONE" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00001978#if ENABLE_HUSH_IF
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001979 [RES_IF ] = "IF" ,
1980 [RES_THEN ] = "THEN" ,
1981 [RES_ELIF ] = "ELIF" ,
1982 [RES_ELSE ] = "ELSE" ,
1983 [RES_FI ] = "FI" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00001984#endif
1985#if ENABLE_HUSH_LOOPS
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001986 [RES_FOR ] = "FOR" ,
1987 [RES_WHILE] = "WHILE",
1988 [RES_UNTIL] = "UNTIL",
1989 [RES_DO ] = "DO" ,
1990 [RES_DONE ] = "DONE" ,
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001991 [RES_IN ] = "IN" ,
Denis Vlasenko06810332007-05-21 23:30:54 +00001992#endif
1993 [RES_XXXX ] = "XXXX" ,
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001994 [RES_SNTX ] = "SNTX" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001995 };
1996
1997 int pin, prn;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00001998
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001999 pin = 0;
2000 while (pi) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002001 fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "",
2002 pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002003 prn = 0;
2004 while (prn < pi->num_progs) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002005 struct child_prog *child = &pi->progs[prn];
2006 char **argv = child->argv;
2007
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002008 fprintf(stderr, "%*s prog %d", lvl*2, "", prn);
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002009 if (child->group) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002010 fprintf(stderr, " group %s: (argv=%p)\n",
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002011 (child->subshell ? "()" : "{}"),
2012 argv);
2013 debug_print_tree(child->group, lvl+1);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002014 prn++;
2015 continue;
2016 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002017 if (argv) while (*argv) {
2018 fprintf(stderr, " '%s'", *argv);
2019 argv++;
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002020 }
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002021 fprintf(stderr, "\n");
2022 prn++;
2023 }
2024 pi = pi->next;
2025 pin++;
2026 }
2027}
2028#endif
2029
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002030/* NB: called by pseudo_exec, and therefore must not modify any
2031 * global data until exec/_exit (we can be a child after vfork!) */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002032static int run_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002033{
Denis Vlasenko06810332007-05-21 23:30:54 +00002034 struct pipe *rpipe;
2035#if ENABLE_HUSH_LOOPS
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002036 char *for_varname = NULL;
2037 char **for_lcur = NULL;
2038 char **for_list = NULL;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002039 int flag_rep = 0;
Denis Vlasenko06810332007-05-21 23:30:54 +00002040#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002041 int flag_skip = 1;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002042 int rcode = 0; /* probably for gcc only */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002043 int flag_restore = 0;
Denis Vlasenko06810332007-05-21 23:30:54 +00002044#if ENABLE_HUSH_IF
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002045 int if_code = 0, next_if_code = 0; /* need double-buffer to handle elif */
Denis Vlasenko06810332007-05-21 23:30:54 +00002046#else
2047 enum { if_code = 0, next_if_code = 0 };
2048#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002049 reserved_style rword;
2050 reserved_style skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002051
Denis Vlasenko05743d72008-02-10 12:10:08 +00002052 debug_printf_exec("run_list start lvl %d\n", run_list_level + 1);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00002053
Denis Vlasenko06810332007-05-21 23:30:54 +00002054#if ENABLE_HUSH_LOOPS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002055 /* check syntax for "for" */
2056 for (rpipe = pi; rpipe; rpipe = rpipe->next) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002057 if ((rpipe->res_word == RES_IN || rpipe->res_word == RES_FOR)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002058 && (rpipe->next == NULL)
2059 ) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00002060 syntax("malformed for"); /* no IN or no commands after IN */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002061 debug_printf_exec("run_list lvl %d return 1\n", run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002062 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002063 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002064 if ((rpipe->res_word == RES_IN && rpipe->next->res_word == RES_IN && rpipe->next->progs[0].argv != NULL)
2065 || (rpipe->res_word == RES_FOR && rpipe->next->res_word != RES_IN)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002066 ) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002067 /* TODO: what is tested in the first condition? */
Denis Vlasenko90e485c2007-05-23 15:22:50 +00002068 syntax("malformed for"); /* 2nd condition: not followed by IN */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002069 debug_printf_exec("run_list lvl %d return 1\n", run_list_level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002070 return 1;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002071 }
2072 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002073#else
2074 rpipe = NULL;
2075#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002076
2077#if ENABLE_HUSH_JOB
2078 /* Example of nested list: "while true; do { sleep 1 | exit 2; } done".
2079 * We are saving state before entering outermost list ("while...done")
2080 * so that ctrl-Z will correctly background _entire_ outermost list,
2081 * not just a part of it (like "sleep 1 | exit 2") */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002082 if (++run_list_level == 1 && interactive_fd) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002083 if (sigsetjmp(toplevel_jb, 1)) {
2084 /* ctrl-Z forked and we are parent; or ctrl-C.
2085 * Sighandler has longjmped us here */
2086 signal(SIGINT, SIG_IGN);
2087 signal(SIGTSTP, SIG_IGN);
2088 /* Restore level (we can be coming from deep inside
2089 * nested levels) */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002090 run_list_level = 1;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002091#if ENABLE_FEATURE_SH_STANDALONE
2092 if (nofork_save.saved) { /* if save area is valid */
2093 debug_printf_jobs("exiting nofork early\n");
2094 restore_nofork_data(&nofork_save);
2095 }
2096#endif
2097 if (ctrl_z_flag) {
2098 /* ctrl-Z has forked and stored pid of the child in pi->pid.
2099 * Remember this child as background job */
2100 insert_bg_job(pi);
2101 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002102 /* ctrl-C. We just stop doing whatever we were doing */
Denis Vlasenko4daad902007-09-27 10:20:47 +00002103 bb_putchar('\n');
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002104 }
2105 rcode = 0;
2106 goto ret;
2107 }
2108 /* ctrl-Z handler will store pid etc in pi */
2109 toplevel_list = pi;
2110 ctrl_z_flag = 0;
2111#if ENABLE_FEATURE_SH_STANDALONE
2112 nofork_save.saved = 0; /* in case we will run a nofork later */
2113#endif
2114 signal_SA_RESTART(SIGTSTP, handler_ctrl_z);
2115 signal(SIGINT, handler_ctrl_c);
2116 }
Denis Vlasenko05743d72008-02-10 12:10:08 +00002117#endif /* JOB */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002118
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002119 for (; pi; pi = flag_restore ? rpipe : pi->next) {
Denis Vlasenko05743d72008-02-10 12:10:08 +00002120//why? int save_num_progs;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002121 rword = pi->res_word;
Denis Vlasenko06810332007-05-21 23:30:54 +00002122#if ENABLE_HUSH_LOOPS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002123 if (rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002124 flag_restore = 0;
2125 if (!rpipe) {
2126 flag_rep = 0;
2127 rpipe = pi;
2128 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002129 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002130#endif
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002131 debug_printf_exec(": rword=%d if_code=%d next_if_code=%d skip_more=%d\n",
2132 rword, if_code, next_if_code, skip_more_for_this_rword);
2133 if (rword == skip_more_for_this_rword && flag_skip) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002134 if (pi->followup == PIPE_SEQ)
2135 flag_skip = 0;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002136 continue;
2137 }
2138 flag_skip = 1;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002139 skip_more_for_this_rword = RES_XXXX;
Denis Vlasenko06810332007-05-21 23:30:54 +00002140#if ENABLE_HUSH_IF
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002141 if (rword == RES_THEN || rword == RES_ELSE)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002142 if_code = next_if_code;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002143 if (rword == RES_THEN && if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002144 continue;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002145 if (rword == RES_ELSE && !if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002146 continue;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002147 if (rword == RES_ELIF && !if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002148 break;
Denis Vlasenko06810332007-05-21 23:30:54 +00002149#endif
2150#if ENABLE_HUSH_LOOPS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002151 if (rword == RES_FOR && pi->num_progs) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002152 if (!for_lcur) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002153 /* first loop through for */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002154 /* if no variable values after "in" we skip "for" */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002155 if (!pi->next->progs->argv)
2156 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002157 /* create list of variable values */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002158 for_list = expand_strvec_to_strvec(pi->next->progs->argv);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002159 for_lcur = for_list;
2160 for_varname = pi->progs->argv[0];
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002161 pi->progs->argv[0] = NULL;
2162 flag_rep = 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002163 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002164 free(pi->progs->argv[0]);
2165 if (!*for_lcur) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002166 /* for loop is over, clean up */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002167 free(for_list);
2168 for_lcur = NULL;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002169 flag_rep = 0;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002170 pi->progs->argv[0] = for_varname;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002171 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002172 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002173 /* insert next value from for_lcur */
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002174 /* vda: does it need escaping? */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002175 pi->progs->argv[0] = xasprintf("%s=%s", for_varname, *for_lcur++);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002176 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002177 if (rword == RES_IN)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002178 continue;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002179 if (rword == RES_DO) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002180 if (!flag_rep)
2181 continue;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002182 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002183 if (rword == RES_DONE) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002184 if (flag_rep) {
2185 flag_restore = 1;
2186 } else {
2187 rpipe = NULL;
2188 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002189 }
Denis Vlasenko06810332007-05-21 23:30:54 +00002190#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002191 if (pi->num_progs == 0)
2192 continue;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002193//why? save_num_progs = pi->num_progs;
2194 debug_printf_exec(": run_pipe with %d members\n", pi->num_progs);
2195 rcode = run_pipe(pi);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002196 if (rcode != -1) {
Eric Andersen25f27032001-04-26 23:22:31 +00002197 /* We only ran a builtin: rcode was set by the return value
Denis Vlasenko05743d72008-02-10 12:10:08 +00002198 * of run_pipe(), and we don't need to wait for anything. */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002199 } else if (pi->followup == PIPE_BG) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002200 /* What does bash do with attempts to background builtins? */
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002201 /* Even bash 3.2 doesn't do that well with nested bg:
2202 * try "{ { sleep 10; echo DEEP; } & echo HERE; } &".
Denis Vlasenko170435c2007-05-23 13:01:10 +00002203 * I'm NOT treating inner &'s as jobs */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002204#if ENABLE_HUSH_JOB
Denis Vlasenko4b924f32007-05-30 00:29:55 +00002205 if (run_list_level == 1)
Denis Vlasenko170435c2007-05-23 13:01:10 +00002206 insert_bg_job(pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002207#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002208 rcode = EXIT_SUCCESS;
2209 } else {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002210#if ENABLE_HUSH_JOB
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00002211 if (run_list_level == 1 && interactive_fd) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00002212 /* waits for completion, then fg's main shell */
Denis Vlasenko52881e92007-04-21 13:42:52 +00002213 rcode = checkjobs_and_fg_shell(pi);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002214 } else
2215#endif
2216 {
Denis Vlasenko170435c2007-05-23 13:01:10 +00002217 /* this one just waits for completion */
Eric Andersenc798b072001-06-22 06:23:03 +00002218 rcode = checkjobs(pi);
Eric Andersen25f27032001-04-26 23:22:31 +00002219 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002220 debug_printf_exec(": checkjobs returned %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002221 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002222 debug_printf_exec(": setting last_return_code=%d\n", rcode);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002223 last_return_code = rcode;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002224//why? pi->num_progs = save_num_progs;
Denis Vlasenko06810332007-05-21 23:30:54 +00002225#if ENABLE_HUSH_IF
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002226 if (rword == RES_IF || rword == RES_ELIF)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002227 next_if_code = rcode; /* can be overwritten a number of times */
Denis Vlasenko06810332007-05-21 23:30:54 +00002228#endif
2229#if ENABLE_HUSH_LOOPS
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002230 if (rword == RES_WHILE)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002231 flag_rep = !last_return_code;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002232 if (rword == RES_UNTIL)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002233 flag_rep = last_return_code;
Denis Vlasenko06810332007-05-21 23:30:54 +00002234#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002235 if ((rcode == EXIT_SUCCESS && pi->followup == PIPE_OR)
2236 || (rcode != EXIT_SUCCESS && pi->followup == PIPE_AND)
2237 ) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002238 skip_more_for_this_rword = rword;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002239 }
Eric Andersen028b65b2001-06-28 01:10:11 +00002240 checkjobs(NULL);
Eric Andersen25f27032001-04-26 23:22:31 +00002241 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002242
2243#if ENABLE_HUSH_JOB
2244 if (ctrl_z_flag) {
2245 /* ctrl-Z forked somewhere in the past, we are the child,
2246 * and now we completed running the list. Exit. */
2247 exit(rcode);
2248 }
2249 ret:
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00002250 if (!--run_list_level && interactive_fd) {
2251 signal(SIGTSTP, SIG_IGN);
2252 signal(SIGINT, SIG_IGN);
2253 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002254#endif
Denis Vlasenko05743d72008-02-10 12:10:08 +00002255 debug_printf_exec("run_list lvl %d return %d\n", run_list_level + 1, rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002256 return rcode;
2257}
2258
Eric Andersen25f27032001-04-26 23:22:31 +00002259/* return code is the exit status of the pipe */
Eric Andersenbf7df042001-05-23 22:18:35 +00002260static int free_pipe(struct pipe *pi, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002261{
2262 char **p;
2263 struct child_prog *child;
2264 struct redir_struct *r, *rnext;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002265 int a, i, ret_code = 0;
Eric Andersen52a97ca2001-06-22 06:49:26 +00002266
2267 if (pi->stopped_progs > 0)
2268 return ret_code;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002269 debug_printf_clean("%s run pipe: (pid %d)\n", indenter(indent), getpid());
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002270 for (i = 0; i < pi->num_progs; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002271 child = &pi->progs[i];
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002272 debug_printf_clean("%s command %d:\n", indenter(indent), i);
Eric Andersen25f27032001-04-26 23:22:31 +00002273 if (child->argv) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002274 for (a = 0, p = child->argv; *p; a++, p++) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002275 debug_printf_clean("%s argv[%d] = %s\n", indenter(indent), a, *p);
Eric Andersen25f27032001-04-26 23:22:31 +00002276 }
Denis Vlasenkof962a032007-11-23 12:50:54 +00002277 free_strings(child->argv);
2278 child->argv = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002279 } else if (child->group) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002280 debug_printf_clean("%s begin group (subshell:%d)\n", indenter(indent), child->subshell);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002281 ret_code = free_pipe_list(child->group, indent+3);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002282 debug_printf_clean("%s end group\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002283 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002284 debug_printf_clean("%s (nil)\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002285 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002286 for (r = child->redirects; r; r = rnext) {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002287 debug_printf_clean("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->type].descrip);
Eric Andersen25f27032001-04-26 23:22:31 +00002288 if (r->dup == -1) {
Eric Andersen817e73c2001-06-06 17:56:09 +00002289 /* guard against the case >$FOO, where foo is unset or blank */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002290 if (r->glob_word) {
2291 debug_printf_clean(" %s\n", r->glob_word[0]);
2292 free_strings(r->glob_word);
2293 r->glob_word = NULL;
Eric Andersen817e73c2001-06-06 17:56:09 +00002294 }
Eric Andersen25f27032001-04-26 23:22:31 +00002295 } else {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002296 debug_printf_clean("&%d\n", r->dup);
Eric Andersen25f27032001-04-26 23:22:31 +00002297 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002298 rnext = r->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002299 free(r);
2300 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002301 child->redirects = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002302 }
2303 free(pi->progs); /* children are an array, they get freed all at once */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002304 pi->progs = NULL;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002305#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002306 free(pi->cmdtext);
2307 pi->cmdtext = NULL;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002308#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002309 return ret_code;
2310}
2311
Eric Andersenbf7df042001-05-23 22:18:35 +00002312static int free_pipe_list(struct pipe *head, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002313{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002314 int rcode = 0; /* if list has no members */
Eric Andersen25f27032001-04-26 23:22:31 +00002315 struct pipe *pi, *next;
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002316
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002317 for (pi = head; pi; pi = next) {
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002318 debug_printf_clean("%s pipe reserved mode %d\n", indenter(indent), pi->res_word);
Eric Andersenbf7df042001-05-23 22:18:35 +00002319 rcode = free_pipe(pi, indent);
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002320 debug_printf_clean("%s pipe followup code %d\n", indenter(indent), pi->followup);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002321 next = pi->next;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002322 /*pi->next = NULL;*/
Eric Andersen25f27032001-04-26 23:22:31 +00002323 free(pi);
2324 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002325 return rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00002326}
2327
2328/* Select which version we will use */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002329static int run_and_free_list(struct pipe *pi)
Eric Andersen25f27032001-04-26 23:22:31 +00002330{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002331 int rcode = 0;
Denis Vlasenko05743d72008-02-10 12:10:08 +00002332 debug_printf_exec("run_and_free_list entered\n");
2333 if (!fake_mode) {
2334 debug_printf_exec(": run_list with %d members\n", pi->num_progs);
2335 rcode = run_list(pi);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002336 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00002337 /* free_pipe_list has the side effect of clearing memory.
Denis Vlasenko05743d72008-02-10 12:10:08 +00002338 * In the long run that function can be merged with run_list,
Eric Andersen25f27032001-04-26 23:22:31 +00002339 * but doing that now would hobble the debugging effort. */
Denis Vlasenko05743d72008-02-10 12:10:08 +00002340 free_pipe_list(pi, /* indent: */ 0);
2341 debug_printf_exec("run_nad_free_list return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002342 return rcode;
2343}
2344
Denis Vlasenkoff097622007-10-01 10:00:45 +00002345/* Whoever decided to muck with glob internal data is AN IDIOT! */
2346/* uclibc happily changed the way it works (and it has rights to do so!),
2347 all hell broke loose (SEGVs) */
2348
Eric Andersen25f27032001-04-26 23:22:31 +00002349/* The API for glob is arguably broken. This routine pushes a non-matching
2350 * string into the output structure, removing non-backslashed backslashes.
2351 * If someone can prove me wrong, by performing this function within the
2352 * original glob(3) api, feel free to rewrite this routine into oblivion.
Eric Andersen25f27032001-04-26 23:22:31 +00002353 * XXX broken if the last character is '\\', check that before calling.
2354 */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002355static char **globhack(const char *src, char **strings)
Eric Andersen25f27032001-04-26 23:22:31 +00002356{
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002357 int cnt;
Eric Andersen25f27032001-04-26 23:22:31 +00002358 const char *s;
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002359 char *v, *dest;
2360
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002361 for (cnt = 1, s = src; s && *s; s++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002362 if (*s == '\\') s++;
2363 cnt++;
2364 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002365 v = dest = xmalloc(cnt);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002366 for (s = src; s && *s; s++, dest++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002367 if (*s == '\\') s++;
2368 *dest = *s;
2369 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002370 *dest = '\0';
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002371
2372 return add_string_to_strings(strings, v);
Eric Andersen25f27032001-04-26 23:22:31 +00002373}
2374
2375/* XXX broken if the last character is '\\', check that before calling */
2376static int glob_needed(const char *s)
2377{
2378 for (; *s; s++) {
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002379 if (*s == '\\')
2380 s++;
2381 if (strchr("*[?", *s))
2382 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002383 }
2384 return 0;
2385}
2386
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002387static int xglob(o_string *dest, char ***pglob)
Eric Andersen25f27032001-04-26 23:22:31 +00002388{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002389 /* short-circuit for null word */
Eric Andersen25f27032001-04-26 23:22:31 +00002390 /* we can code this better when the debug_printf's are gone */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002391 if (dest->length == 0) {
2392 if (dest->nonnull) {
2393 /* bash man page calls this an "explicit" null */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002394 *pglob = globhack(dest->data, *pglob);
2395 }
2396 return 0;
2397 }
2398
2399 if (glob_needed(dest->data)) {
2400 glob_t globdata;
2401 int gr;
2402
2403 memset(&globdata, 0, sizeof(globdata));
2404 gr = glob(dest->data, 0, NULL, &globdata);
2405 debug_printf("glob returned %d\n", gr);
2406 if (gr == GLOB_NOSPACE)
2407 bb_error_msg_and_die("out of memory during glob");
2408 if (gr == GLOB_NOMATCH) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002409 debug_printf("globhack returned %d\n", gr);
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002410 /* quote removal, or more accurately, backslash removal */
2411 *pglob = globhack(dest->data, *pglob);
Denis Vlasenkof962a032007-11-23 12:50:54 +00002412 globfree(&globdata);
Eric Andersen25f27032001-04-26 23:22:31 +00002413 return 0;
2414 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002415 if (gr != 0) { /* GLOB_ABORTED ? */
2416 bb_error_msg("glob(3) error %d", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002417 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002418 if (globdata.gl_pathv && globdata.gl_pathv[0])
2419 *pglob = add_strings_to_strings(1, *pglob, globdata.gl_pathv);
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002420 globfree(&globdata);
2421 return gr;
Eric Andersen25f27032001-04-26 23:22:31 +00002422 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002423
2424 *pglob = globhack(dest->data, *pglob);
2425 return 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002426}
2427
Denis Vlasenko170435c2007-05-23 13:01:10 +00002428/* expand_strvec_to_strvec() takes a list of strings, expands
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002429 * all variable references within and returns a pointer to
2430 * a list of expanded strings, possibly with larger number
2431 * of strings. (Think VAR="a b"; echo $VAR).
2432 * This new list is allocated as a single malloc block.
2433 * NULL-terminated list of char* pointers is at the beginning of it,
2434 * followed by strings themself.
2435 * Caller can deallocate entire list by single free(list). */
2436
2437/* Helpers first:
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00002438 * count_XXX estimates size of the block we need. It's okay
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002439 * to over-estimate sizes a bit, if it makes code simpler */
2440static int count_ifs(const char *str)
2441{
2442 int cnt = 0;
2443 debug_printf_expand("count_ifs('%s') ifs='%s'", str, ifs);
2444 while (1) {
2445 str += strcspn(str, ifs);
2446 if (!*str) break;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00002447 str++; /* str += strspn(str, ifs); */
2448 cnt++; /* cnt += strspn(str, ifs); - but this code is larger */
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002449 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002450 debug_printf_expand(" return %d\n", cnt);
2451 return cnt;
2452}
2453
2454static void count_var_expansion_space(int *countp, int *lenp, char *arg)
2455{
2456 char first_ch;
2457 int i;
2458 int len = *lenp;
2459 int count = *countp;
2460 const char *val;
2461 char *p;
2462
2463 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL))) {
2464 len += p - arg;
2465 arg = ++p;
2466 p = strchr(p, SPECIAL_VAR_SYMBOL);
2467 first_ch = arg[0];
2468
2469 switch (first_ch & 0x7f) {
2470 /* high bit in 1st_ch indicates that var is double-quoted */
2471 case '$': /* pid */
2472 case '!': /* bg pid */
2473 case '?': /* exitcode */
2474 case '#': /* argc */
2475 len += sizeof(int)*3 + 1; /* enough for int */
2476 break;
2477 case '*':
2478 case '@':
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002479 for (i = 1; global_argv[i]; i++) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002480 len += strlen(global_argv[i]) + 1;
2481 count++;
2482 if (!(first_ch & 0x80))
2483 count += count_ifs(global_argv[i]);
2484 }
2485 break;
2486 default:
2487 *p = '\0';
2488 arg[0] = first_ch & 0x7f;
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002489 if (isdigit(arg[0])) {
2490 i = xatoi_u(arg);
2491 val = NULL;
2492 if (i < global_argc)
2493 val = global_argv[i];
2494 } else
2495 val = lookup_param(arg);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002496 arg[0] = first_ch;
2497 *p = SPECIAL_VAR_SYMBOL;
2498
2499 if (val) {
2500 len += strlen(val) + 1;
2501 if (!(first_ch & 0x80))
2502 count += count_ifs(val);
2503 }
2504 }
2505 arg = ++p;
2506 }
2507
2508 len += strlen(arg) + 1;
2509 count++;
2510 *lenp = len;
2511 *countp = count;
2512}
2513
2514/* Store given string, finalizing the word and starting new one whenever
2515 * we encounter ifs char(s). This is used for expanding variable values.
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002516 * End-of-string does NOT finalize word: think about 'echo -$VAR-' */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002517static int expand_on_ifs(char **list, int n, char **posp, const char *str)
2518{
2519 char *pos = *posp;
2520 while (1) {
2521 int word_len = strcspn(str, ifs);
2522 if (word_len) {
2523 memcpy(pos, str, word_len); /* store non-ifs chars */
2524 pos += word_len;
2525 str += word_len;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002526 }
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002527 if (!*str) /* EOL - do not finalize word */
2528 break;
2529 *pos++ = '\0';
2530 if (n) debug_printf_expand("expand_on_ifs finalized list[%d]=%p '%s' "
2531 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2532 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2533 list[n++] = pos;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002534 str += strspn(str, ifs); /* skip ifs chars */
2535 }
2536 *posp = pos;
2537 return n;
2538}
2539
2540/* Expand all variable references in given string, adding words to list[]
2541 * at n, n+1,... positions. Return updated n (so that list[n] is next one
2542 * to be filled). This routine is extremely tricky: has to deal with
2543 * variables/parameters with whitespace, $* and $@, and constructs like
2544 * 'echo -$*-'. If you play here, you must run testsuite afterwards! */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002545/* NB: another bug is that we cannot detect empty strings yet:
2546 * "" or $empty"" expands to zero words, has to expand to empty word */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002547static int expand_vars_to_list(char **list, int n, char **posp, char *arg, char or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002548{
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002549 /* or_mask is either 0 (normal case) or 0x80
2550 * (expansion of right-hand side of assignment == 1-element expand) */
2551
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002552 char first_ch, ored_ch;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002553 int i;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002554 const char *val;
2555 char *p;
2556 char *pos = *posp;
2557
2558 ored_ch = 0;
2559
2560 if (n) debug_printf_expand("expand_vars_to_list finalized list[%d]=%p '%s' "
2561 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2562 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2563 list[n++] = pos;
2564
2565 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL))) {
2566 memcpy(pos, arg, p - arg);
2567 pos += (p - arg);
2568 arg = ++p;
2569 p = strchr(p, SPECIAL_VAR_SYMBOL);
2570
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002571 first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002572 ored_ch |= first_ch;
2573 val = NULL;
2574 switch (first_ch & 0x7f) {
2575 /* Highest bit in first_ch indicates that var is double-quoted */
2576 case '$': /* pid */
2577 /* FIXME: (echo $$) should still print pid of main shell */
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00002578 val = utoa(getpid()); /* rootpid? */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002579 break;
2580 case '!': /* bg pid */
2581 val = last_bg_pid ? utoa(last_bg_pid) : (char*)"";
2582 break;
2583 case '?': /* exitcode */
2584 val = utoa(last_return_code);
2585 break;
2586 case '#': /* argc */
2587 val = utoa(global_argc ? global_argc-1 : 0);
2588 break;
2589 case '*':
2590 case '@':
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002591 i = 1;
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002592 if (!global_argv[i])
2593 break;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002594 if (!(first_ch & 0x80)) { /* unquoted $* or $@ */
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002595 while (global_argv[i]) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002596 n = expand_on_ifs(list, n, &pos, global_argv[i]);
2597 debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, global_argc-1);
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002598 if (global_argv[i++][0] && global_argv[i]) {
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002599 /* this argv[] is not empty and not last:
2600 * put terminating NUL, start new word */
2601 *pos++ = '\0';
2602 if (n) debug_printf_expand("expand_vars_to_list 2 finalized list[%d]=%p '%s' "
2603 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2604 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2605 list[n++] = pos;
2606 }
2607 }
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002608 } else
2609 /* If or_mask is nonzero, we handle assignment 'a=....$@.....'
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00002610 * and in this case should treat it like '$*' - see 'else...' below */
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002611 if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002612 while (1) {
2613 strcpy(pos, global_argv[i]);
2614 pos += strlen(global_argv[i]);
2615 if (++i >= global_argc)
2616 break;
2617 *pos++ = '\0';
2618 if (n) debug_printf_expand("expand_vars_to_list 3 finalized list[%d]=%p '%s' "
2619 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2620 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
2621 list[n++] = pos;
2622 }
2623 } else { /* quoted $*: add as one word */
Denis Vlasenkoc3c66592007-11-24 00:22:42 +00002624 while (1) {
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002625 strcpy(pos, global_argv[i]);
2626 pos += strlen(global_argv[i]);
Denis Vlasenkocccdc4e2007-11-23 21:08:38 +00002627 if (!global_argv[++i])
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002628 break;
2629 if (ifs[0])
2630 *pos++ = ifs[0];
2631 }
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002632 }
2633 break;
2634 default:
2635 *p = '\0';
2636 arg[0] = first_ch & 0x7f;
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002637 if (isdigit(arg[0])) {
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002638 i = xatoi_u(arg);
Denis Vlasenko764d59d2007-05-14 16:23:23 +00002639 val = NULL;
2640 if (i < global_argc)
2641 val = global_argv[i];
2642 } else
2643 val = lookup_param(arg);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002644 arg[0] = first_ch;
2645 *p = SPECIAL_VAR_SYMBOL;
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002646 if (!(first_ch & 0x80)) { /* unquoted $VAR */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002647 if (val) {
2648 n = expand_on_ifs(list, n, &pos, val);
2649 val = NULL;
2650 }
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00002651 } /* else: quoted $VAR, val will be appended at pos */
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002652 }
2653 if (val) {
2654 strcpy(pos, val);
2655 pos += strlen(val);
2656 }
2657 arg = ++p;
2658 }
2659 debug_printf_expand("expand_vars_to_list adding tail '%s' at %p\n", arg, pos);
2660 strcpy(pos, arg);
2661 pos += strlen(arg) + 1;
2662 if (pos == list[n-1] + 1) { /* expansion is empty */
2663 if (!(ored_ch & 0x80)) { /* all vars were not quoted... */
2664 debug_printf_expand("expand_vars_to_list list[%d] empty, going back\n", n);
2665 pos--;
2666 n--;
2667 }
2668 }
2669
2670 *posp = pos;
2671 return n;
2672}
2673
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002674static char **expand_variables(char **argv, char or_mask)
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002675{
2676 int n;
2677 int count = 1;
2678 int len = 0;
2679 char *pos, **v, **list;
2680
2681 v = argv;
2682 if (!*v) debug_printf_expand("count_var_expansion_space: "
2683 "argv[0]=NULL count=%d len=%d alloc_space=%d\n",
2684 count, len, sizeof(char*) * count + len);
2685 while (*v) {
2686 count_var_expansion_space(&count, &len, *v);
2687 debug_printf_expand("count_var_expansion_space: "
2688 "'%s' count=%d len=%d alloc_space=%d\n",
2689 *v, count, len, sizeof(char*) * count + len);
2690 v++;
2691 }
2692 len += sizeof(char*) * count; /* total to alloc */
2693 list = xmalloc(len);
2694 pos = (char*)(list + count);
2695 debug_printf_expand("list=%p, list[0] should be %p\n", list, pos);
2696 n = 0;
2697 v = argv;
2698 while (*v)
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002699 n = expand_vars_to_list(list, n, &pos, *v++, or_mask);
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002700
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002701 if (n) debug_printf_expand("finalized list[%d]=%p '%s' "
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002702 "strlen=%d next=%p pos=%p\n", n-1, list[n-1], list[n-1],
2703 strlen(list[n-1]), list[n-1] + strlen(list[n-1]) + 1, pos);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002704 list[n] = NULL;
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002705
2706#ifdef DEBUG_EXPAND
2707 {
2708 int m = 0;
2709 while (m <= n) {
2710 debug_printf_expand("list[%d]=%p '%s'\n", m, list[m], list[m]);
2711 m++;
2712 }
2713 debug_printf_expand("used_space=%d\n", pos - (char*)list);
2714 }
2715#endif
Denis Vlasenko170435c2007-05-23 13:01:10 +00002716 if (ENABLE_HUSH_DEBUG)
2717 if (pos - (char*)list > len)
2718 bb_error_msg_and_die("BUG in varexp");
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002719 return list;
2720}
2721
Denis Vlasenko170435c2007-05-23 13:01:10 +00002722static char **expand_strvec_to_strvec(char **argv)
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002723{
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002724 return expand_variables(argv, 0);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002725}
2726
Denis Vlasenko170435c2007-05-23 13:01:10 +00002727static char *expand_string_to_string(const char *str)
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002728{
2729 char *argv[2], **list;
2730
2731 argv[0] = (char*)str;
2732 argv[1] = NULL;
2733 list = expand_variables(argv, 0x80); /* 0x80: make one-element expansion */
Denis Vlasenko170435c2007-05-23 13:01:10 +00002734 if (ENABLE_HUSH_DEBUG)
2735 if (!list[0] || list[1])
2736 bb_error_msg_and_die("BUG in varexp2");
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002737 /* actually, just move string 2*sizeof(char*) bytes back */
2738 strcpy((char*)list, list[0]);
Denis Vlasenko170435c2007-05-23 13:01:10 +00002739 debug_printf_expand("string_to_string='%s'\n", (char*)list);
2740 return (char*)list;
2741}
2742
2743static char* expand_strvec_to_string(char **argv)
2744{
2745 char **list;
2746
2747 list = expand_variables(argv, 0x80);
2748 /* Convert all NULs to spaces */
2749 if (list[0]) {
2750 int n = 1;
2751 while (list[n]) {
2752 if (ENABLE_HUSH_DEBUG)
2753 if (list[n-1] + strlen(list[n-1]) + 1 != list[n])
2754 bb_error_msg_and_die("BUG in varexp3");
2755 list[n][-1] = ' '; /* TODO: or to ifs[0]? */
2756 n++;
2757 }
2758 }
2759 strcpy((char*)list, list[0]);
2760 debug_printf_expand("strvec_to_string='%s'\n", (char*)list);
Denis Vlasenkob6a741f2007-05-17 14:38:17 +00002761 return (char*)list;
2762}
Denis Vlasenko03eb8bf2007-05-14 16:19:34 +00002763
Eric Andersenf72f5622001-05-15 23:21:41 +00002764/* This is used to get/check local shell variables */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002765static struct variable *get_local_var(const char *name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002766{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002767 struct variable *cur;
2768 int len;
Eric Andersenf72f5622001-05-15 23:21:41 +00002769
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002770 if (!name)
Eric Andersenf72f5622001-05-15 23:21:41 +00002771 return NULL;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002772 len = strlen(name);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002773 for (cur = top_var; cur; cur = cur->next) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002774 if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=')
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002775 return cur;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00002776 }
Eric Andersenf72f5622001-05-15 23:21:41 +00002777 return NULL;
2778}
2779
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002780/* str holds "NAME=VAL" and is expected to be malloced.
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002781 * We take ownership of it. */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002782static int set_local_var(char *str, int flg_export)
Eric Andersen78a7c992001-05-15 16:30:25 +00002783{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002784 struct variable *cur;
2785 char *value;
2786 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002787
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002788 value = strchr(str, '=');
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002789 if (!value) { /* not expected to ever happen? */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002790 free(str);
Eric Andersen99785762001-05-22 21:37:48 +00002791 return -1;
2792 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002793
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002794 name_len = value - str + 1; /* including '=' */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002795 cur = top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */
2796 while (1) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002797 if (strncmp(cur->varstr, str, name_len) != 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002798 if (!cur->next) {
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002799 /* Bail out. Note that now cur points
2800 * to last var in linked list */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002801 break;
Eric Andersen20a69a72001-05-15 17:24:44 +00002802 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002803 cur = cur->next;
2804 continue;
Eric Andersen20a69a72001-05-15 17:24:44 +00002805 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002806 /* We found an existing var with this name */
2807 *value = '\0';
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002808 if (cur->flg_read_only) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002809 bb_error_msg("%s: readonly variable", str);
2810 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002811 return -1;
2812 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002813 unsetenv(str); /* just in case */
2814 *value = '=';
2815 if (strcmp(cur->varstr, str) == 0) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002816 free_and_exp:
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002817 free(str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002818 goto exp;
2819 }
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002820 if (cur->max_len >= strlen(str)) {
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002821 /* This one is from startup env, reuse space */
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002822 strcpy(cur->varstr, str);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002823 goto free_and_exp;
2824 }
2825 /* max_len == 0 signifies "malloced" var, which we can
2826 * (and has to) free */
2827 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002828 free(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002829 cur->max_len = 0;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002830 goto set_str_and_exp;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002831 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002832
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002833 /* Not found - create next variable struct */
2834 cur->next = xzalloc(sizeof(*cur));
2835 cur = cur->next;
2836
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002837 set_str_and_exp:
2838 cur->varstr = str;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002839 exp:
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002840 if (flg_export)
2841 cur->flg_export = 1;
2842 if (cur->flg_export)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002843 return putenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002844 return 0;
Eric Andersen20a69a72001-05-15 17:24:44 +00002845}
2846
Eric Andersenf72f5622001-05-15 23:21:41 +00002847static void unset_local_var(const char *name)
Eric Andersen20a69a72001-05-15 17:24:44 +00002848{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002849 struct variable *cur;
2850 struct variable *prev = prev; /* for gcc */
2851 int name_len;
Eric Andersen20a69a72001-05-15 17:24:44 +00002852
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002853 if (!name)
2854 return;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002855 name_len = strlen(name);
2856 cur = top_var;
2857 while (cur) {
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002858 if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002859 if (cur->flg_read_only) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002860 bb_error_msg("%s: readonly variable", name);
Eric Andersen94ac2442001-05-22 19:05:18 +00002861 return;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002862 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002863 /* prev is ok to use here because 1st variable, HUSH_VERSION,
2864 * is ro, and we cannot reach this code on the 1st pass */
2865 prev->next = cur->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002866 unsetenv(cur->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002867 if (!cur->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00002868 free(cur->varstr);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002869 free(cur);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002870 return;
Eric Andersenf72f5622001-05-15 23:21:41 +00002871 }
Denis Vlasenkod76c0492007-05-25 02:16:25 +00002872 prev = cur;
2873 cur = cur->next;
Eric Andersen20a69a72001-05-15 17:24:44 +00002874 }
Eric Andersen78a7c992001-05-15 16:30:25 +00002875}
2876
2877static int is_assignment(const char *s)
2878{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002879 if (!s || !isalpha(*s))
2880 return 0;
2881 s++;
2882 while (isalnum(*s) || *s == '_')
2883 s++;
2884 return *s == '=';
Eric Andersen78a7c992001-05-15 16:30:25 +00002885}
2886
Eric Andersen25f27032001-04-26 23:22:31 +00002887/* the src parameter allows us to peek forward to a possible &n syntax
2888 * for file descriptor duplication, e.g., "2>&1".
2889 * Return code is 0 normally, 1 if a syntax error is detected in src.
2890 * Resource errors (in xmalloc) cause the process to exit */
2891static int setup_redirect(struct p_context *ctx, int fd, redir_type style,
2892 struct in_str *input)
2893{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002894 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00002895 struct redir_struct *redir = child->redirects;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002896 struct redir_struct *last_redir = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002897
2898 /* Create a new redir_struct and drop it onto the end of the linked list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002899 while (redir) {
2900 last_redir = redir;
2901 redir = redir->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002902 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00002903 redir = xzalloc(sizeof(struct redir_struct));
2904 /* redir->next = NULL; */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00002905 /* redir->glob_word = NULL; */
Eric Andersen25f27032001-04-26 23:22:31 +00002906 if (last_redir) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002907 last_redir->next = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002908 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002909 child->redirects = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002910 }
2911
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002912 redir->type = style;
2913 redir->fd = (fd == -1) ? redir_table[style].default_fd : fd;
Eric Andersen25f27032001-04-26 23:22:31 +00002914
2915 debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
2916
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002917 /* Check for a '2>&1' type redirect */
Eric Andersen25f27032001-04-26 23:22:31 +00002918 redir->dup = redirect_dup_num(input);
2919 if (redir->dup == -2) return 1; /* syntax error */
2920 if (redir->dup != -1) {
2921 /* Erik had a check here that the file descriptor in question
Eric Andersen83a2ae22001-05-07 17:59:25 +00002922 * is legit; I postpone that to "run time"
2923 * A "-" representation of "close me" shows up as a -3 here */
Eric Andersen25f27032001-04-26 23:22:31 +00002924 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
2925 } else {
2926 /* We do _not_ try to open the file that src points to,
2927 * since we need to return and let src be expanded first.
2928 * Set ctx->pending_redirect, so we know what to do at the
Denis Vlasenko201c72a2007-05-25 10:00:36 +00002929 * end of the next parsed word. */
Eric Andersen25f27032001-04-26 23:22:31 +00002930 ctx->pending_redirect = redir;
2931 }
2932 return 0;
2933}
2934
Denis Vlasenkoac678ec2007-04-16 22:32:04 +00002935static struct pipe *new_pipe(void)
2936{
Eric Andersen25f27032001-04-26 23:22:31 +00002937 struct pipe *pi;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002938 pi = xzalloc(sizeof(struct pipe));
2939 /*pi->num_progs = 0;*/
2940 /*pi->progs = NULL;*/
2941 /*pi->next = NULL;*/
2942 /*pi->followup = 0; invalid */
2943 if (RES_NONE)
Denis Vlasenko219e88d2007-05-21 10:18:23 +00002944 pi->res_word = RES_NONE;
Eric Andersen25f27032001-04-26 23:22:31 +00002945 return pi;
2946}
2947
2948static void initialize_context(struct p_context *ctx)
2949{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002950 ctx->child = NULL;
Denis Vlasenko1a735862007-05-23 00:32:25 +00002951 ctx->pipe = ctx->list_head = new_pipe();
2952 ctx->pending_redirect = NULL;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00002953 ctx->res_w = RES_NONE;
Denis Vlasenko1a735862007-05-23 00:32:25 +00002954 //only ctx->parse_type is not touched... is this intentional?
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002955 ctx->old_flag = 0;
Denis Vlasenko1a735862007-05-23 00:32:25 +00002956 ctx->stack = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002957 done_command(ctx); /* creates the memory for working child */
2958}
2959
2960/* normal return is 0
2961 * if a reserved word is found, and processed, return 1
2962 * should handle if, then, elif, else, fi, for, while, until, do, done.
2963 * case, function, and select are obnoxious, save those for later.
2964 */
Denis Vlasenko06810332007-05-21 23:30:54 +00002965#if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00002966static int reserved_word(o_string *dest, struct p_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00002967{
2968 struct reserved_combo {
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002969 char literal[7];
2970 unsigned char code;
2971 int flag;
Eric Andersen25f27032001-04-26 23:22:31 +00002972 };
2973 /* Mostly a list of accepted follow-up reserved words.
2974 * FLAG_END means we are done with the sequence, and are ready
2975 * to turn the compound list into a command.
2976 * FLAG_START means the word must start a new compound list.
2977 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002978 static const struct reserved_combo reserved_list[] = {
Denis Vlasenko06810332007-05-21 23:30:54 +00002979#if ENABLE_HUSH_IF
Eric Andersen25f27032001-04-26 23:22:31 +00002980 { "if", RES_IF, FLAG_THEN | FLAG_START },
2981 { "then", RES_THEN, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
2982 { "elif", RES_ELIF, FLAG_THEN },
2983 { "else", RES_ELSE, FLAG_FI },
2984 { "fi", RES_FI, FLAG_END },
Denis Vlasenko06810332007-05-21 23:30:54 +00002985#endif
2986#if ENABLE_HUSH_LOOPS
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002987 { "for", RES_FOR, FLAG_IN | FLAG_START },
Eric Andersen25f27032001-04-26 23:22:31 +00002988 { "while", RES_WHILE, FLAG_DO | FLAG_START },
2989 { "until", RES_UNTIL, FLAG_DO | FLAG_START },
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002990 { "in", RES_IN, FLAG_DO },
Eric Andersen25f27032001-04-26 23:22:31 +00002991 { "do", RES_DO, FLAG_DONE },
2992 { "done", RES_DONE, FLAG_END }
Denis Vlasenko06810332007-05-21 23:30:54 +00002993#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002994 };
Denis Vlasenko80b8b392007-06-25 10:55:35 +00002995
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002996 const struct reserved_combo *r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002997
Denis Vlasenko80b8b392007-06-25 10:55:35 +00002998 for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) {
Denis Vlasenko1a735862007-05-23 00:32:25 +00002999 if (strcmp(dest->data, r->literal) != 0)
3000 continue;
3001 debug_printf("found reserved word %s, code %d\n", r->literal, r->code);
3002 if (r->flag & FLAG_START) {
3003 struct p_context *new;
3004 debug_printf("push stack\n");
Denis Vlasenko06810332007-05-21 23:30:54 +00003005#if ENABLE_HUSH_LOOPS
Denis Vlasenko1a735862007-05-23 00:32:25 +00003006 if (ctx->res_w == RES_IN || ctx->res_w == RES_FOR) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003007 syntax("malformed for"); /* example: 'for if' */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003008 ctx->res_w = RES_SNTX;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003009 b_reset(dest);
Eric Andersenaf44a0e2001-04-27 07:26:12 +00003010 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003011 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003012#endif
3013 new = xmalloc(sizeof(*new));
3014 *new = *ctx; /* physical copy */
3015 initialize_context(ctx);
3016 ctx->stack = new;
3017 } else if (ctx->res_w == RES_NONE || !(ctx->old_flag & (1 << r->code))) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003018 syntax(NULL);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003019 ctx->res_w = RES_SNTX;
Denis Vlasenkoff131b92007-04-10 15:42:06 +00003020 b_reset(dest);
Eric Andersen25f27032001-04-26 23:22:31 +00003021 return 1;
3022 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003023 ctx->res_w = r->code;
3024 ctx->old_flag = r->flag;
3025 if (ctx->old_flag & FLAG_END) {
3026 struct p_context *old;
3027 debug_printf("pop stack\n");
3028 done_pipe(ctx, PIPE_SEQ);
3029 old = ctx->stack;
3030 old->child->group = ctx->list_head;
3031 old->child->subshell = 0;
3032 *ctx = *old; /* physical copy */
3033 free(old);
3034 }
3035 b_reset(dest);
3036 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003037 }
3038 return 0;
3039}
Denis Vlasenko06810332007-05-21 23:30:54 +00003040#else
3041#define reserved_word(dest, ctx) ((int)0)
3042#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003043
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003044/* Normal return is 0.
Eric Andersen25f27032001-04-26 23:22:31 +00003045 * Syntax or xglob errors return 1. */
3046static int done_word(o_string *dest, struct p_context *ctx)
3047{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003048 struct child_prog *child = ctx->child;
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003049 char ***glob_target;
3050 int gr;
Eric Andersen25f27032001-04-26 23:22:31 +00003051
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003052 debug_printf_parse("done_word entered: '%s' %p\n", dest->data, child);
Eric Andersen25f27032001-04-26 23:22:31 +00003053 if (dest->length == 0 && !dest->nonnull) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003054 debug_printf_parse("done_word return 0: true null, ignored\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003055 return 0;
3056 }
3057 if (ctx->pending_redirect) {
Denis Vlasenkoff097622007-10-01 10:00:45 +00003058 glob_target = &ctx->pending_redirect->glob_word;
Eric Andersen25f27032001-04-26 23:22:31 +00003059 } else {
3060 if (child->group) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003061 syntax(NULL);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003062 debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n");
3063 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003064 }
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003065 if (!child->argv && (ctx->parse_type & PARSEFLAG_SEMICOLON)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003066 debug_printf_parse(": checking '%s' for reserved-ness\n", dest->data);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003067 if (reserved_word(dest, ctx)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003068 debug_printf_parse("done_word return %d\n", (ctx->res_w == RES_SNTX));
3069 return (ctx->res_w == RES_SNTX);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003070 }
Eric Andersen25f27032001-04-26 23:22:31 +00003071 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003072 glob_target = &child->argv;
Eric Andersen25f27032001-04-26 23:22:31 +00003073 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003074 gr = xglob(dest, glob_target);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003075 if (gr != 0) {
3076 debug_printf_parse("done_word return 1: xglob returned %d\n", gr);
3077 return 1;
3078 }
Eric Andersen25f27032001-04-26 23:22:31 +00003079
3080 b_reset(dest);
3081 if (ctx->pending_redirect) {
Denis Vlasenkof962a032007-11-23 12:50:54 +00003082 /* NB: don't free_strings(ctx->pending_redirect->glob_word) here */
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003083 if (ctx->pending_redirect->glob_word
3084 && ctx->pending_redirect->glob_word[0]
3085 && ctx->pending_redirect->glob_word[1]
3086 ) {
3087 /* more than one word resulted from globbing redir */
3088 ctx->pending_redirect = NULL;
Manuel Novoa III cad53642003-03-19 09:13:01 +00003089 bb_error_msg("ambiguous redirect");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003090 debug_printf_parse("done_word return 1: ambiguous redirect\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003091 return 1;
3092 }
Denis Vlasenkod65ea392007-10-01 10:02:25 +00003093 ctx->pending_redirect = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003094 }
Denis Vlasenko06810332007-05-21 23:30:54 +00003095#if ENABLE_HUSH_LOOPS
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003096 if (ctx->res_w == RES_FOR) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003097 done_word(dest, ctx);
3098 done_pipe(ctx, PIPE_SEQ);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003099 }
Denis Vlasenko06810332007-05-21 23:30:54 +00003100#endif
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003101 debug_printf_parse("done_word return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003102 return 0;
3103}
3104
3105/* The only possible error here is out of memory, in which case
3106 * xmalloc exits. */
3107static int done_command(struct p_context *ctx)
3108{
3109 /* The child is really already in the pipe structure, so
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003110 * advance the pipe counter and make a new, null child. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003111 struct pipe *pi = ctx->pipe;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003112 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00003113
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003114 if (child) {
3115 if (child->group == NULL
3116 && child->argv == NULL
3117 && child->redirects == NULL
3118 ) {
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003119 debug_printf_parse("done_command: skipping null cmd, num_progs=%d\n", pi->num_progs);
3120 return pi->num_progs;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003121 }
Eric Andersen25f27032001-04-26 23:22:31 +00003122 pi->num_progs++;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003123 debug_printf_parse("done_command: ++num_progs=%d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00003124 } else {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003125 debug_printf_parse("done_command: initializing, num_progs=%d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00003126 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003127
3128 /* Only real trickiness here is that the uncommitted
3129 * child structure is not counted in pi->num_progs. */
Eric Andersen25f27032001-04-26 23:22:31 +00003130 pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1));
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003131 child = &pi->progs[pi->num_progs];
Eric Andersen25f27032001-04-26 23:22:31 +00003132
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003133 memset(child, 0, sizeof(*child));
3134 /*child->redirects = NULL;*/
3135 /*child->argv = NULL;*/
3136 /*child->is_stopped = 0;*/
3137 /*child->group = NULL;*/
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003138 child->family = pi;
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003139 //sp: /*child->sp = 0;*/
Denis Vlasenko262d7652007-05-20 21:52:49 +00003140 //pt: child->parse_type = ctx->parse_type;
Eric Andersen25f27032001-04-26 23:22:31 +00003141
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003142 ctx->child = child;
Eric Andersen25f27032001-04-26 23:22:31 +00003143 /* but ctx->pipe and ctx->list_head remain unchanged */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003144
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003145 return pi->num_progs; /* used only for 0/nonzero check */
Eric Andersen25f27032001-04-26 23:22:31 +00003146}
3147
3148static int done_pipe(struct p_context *ctx, pipe_style type)
3149{
3150 struct pipe *new_p;
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003151 int not_null;
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003152
3153 debug_printf_parse("done_pipe entered, followup %d\n", type);
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003154 not_null = done_command(ctx); /* implicit closure of previous command */
Eric Andersen25f27032001-04-26 23:22:31 +00003155 ctx->pipe->followup = type;
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003156 ctx->pipe->res_word = ctx->res_w;
Denis Vlasenkodd4cb2b2007-05-05 15:11:40 +00003157 /* Without this check, even just <enter> on command line generates
3158 * tree of three NOPs (!). Which is harmless but annoying.
3159 * IOW: it is safe to do it unconditionally. */
3160 if (not_null) {
3161 new_p = new_pipe();
3162 ctx->pipe->next = new_p;
3163 ctx->pipe = new_p;
3164 ctx->child = NULL;
3165 done_command(ctx); /* set up new pipe to accept commands */
3166 }
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003167 debug_printf_parse("done_pipe return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003168 return 0;
3169}
3170
3171/* peek ahead in the in_str to find out if we have a "&n" construct,
3172 * as in "2>&1", that represents duplicating a file descriptor.
3173 * returns either -2 (syntax error), -1 (no &), or the number found.
3174 */
3175static int redirect_dup_num(struct in_str *input)
3176{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003177 int ch, d = 0, ok = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003178 ch = b_peek(input);
3179 if (ch != '&') return -1;
3180
3181 b_getch(input); /* get the & */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003182 ch = b_peek(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003183 if (ch == '-') {
3184 b_getch(input);
3185 return -3; /* "-" represents "close me" */
3186 }
3187 while (isdigit(ch)) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00003188 d = d*10 + (ch-'0');
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003189 ok = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003190 b_getch(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00003191 ch = b_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003192 }
3193 if (ok) return d;
3194
Manuel Novoa III cad53642003-03-19 09:13:01 +00003195 bb_error_msg("ambiguous redirect");
Eric Andersen25f27032001-04-26 23:22:31 +00003196 return -2;
3197}
3198
3199/* If a redirect is immediately preceded by a number, that number is
3200 * supposed to tell which file descriptor to redirect. This routine
3201 * looks for such preceding numbers. In an ideal world this routine
3202 * needs to handle all the following classes of redirects...
3203 * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
3204 * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
3205 * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
3206 * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
3207 * A -1 output from this program means no valid number was found, so the
3208 * caller should use the appropriate default for this redirection.
3209 */
3210static int redirect_opt_num(o_string *o)
3211{
3212 int num;
3213
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003214 if (o->length == 0)
3215 return -1;
3216 for (num = 0; num < o->length; num++) {
3217 if (!isdigit(*(o->data + num))) {
Eric Andersen25f27032001-04-26 23:22:31 +00003218 return -1;
3219 }
3220 }
3221 /* reuse num (and save an int) */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003222 num = atoi(o->data);
Eric Andersen25f27032001-04-26 23:22:31 +00003223 b_reset(o);
3224 return num;
3225}
3226
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003227#if ENABLE_HUSH_TICK
Denis Vlasenko8412d792007-10-01 09:59:47 +00003228/* NB: currently disabled on NOMMU */
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00003229static FILE *generate_stream_from_list(struct pipe *head)
Eric Andersen25f27032001-04-26 23:22:31 +00003230{
3231 FILE *pf;
Eric Andersen25f27032001-04-26 23:22:31 +00003232 int pid, channel[2];
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003233
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +00003234 xpipe(channel);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003235/* *** NOMMU WARNING *** */
3236/* By using vfork here, we suspend parent till child exits or execs.
3237 * If child will not do it before it fills the pipe, it can block forever
3238 * in write(STDOUT_FILENO), and parent (shell) will be also stuck.
3239 */
3240 pid = BB_MMU ? fork() : vfork();
3241 if (pid < 0)
3242 bb_perror_msg_and_die(BB_MMU ? "fork" : "vfork");
3243 if (pid == 0) { /* child */
Denis Vlasenko83177992008-02-11 08:44:36 +00003244 if (ENABLE_HUSH_JOB)
3245 die_sleep = 0; /* let nofork's xfuncs die */
Eric Andersen25f27032001-04-26 23:22:31 +00003246 close(channel[0]);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003247 xmove_fd(channel[1], 1);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003248 /* Prevent it from trying to handle ctrl-z etc */
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003249#if ENABLE_HUSH_JOB
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003250 run_list_level = 1;
Denis Vlasenko27f79ff2007-05-30 00:55:52 +00003251#endif
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003252 /* Process substitution is not considered to be usual
3253 * 'command execution'.
3254 * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. */
3255 /* Not needed, we are relying on it being disabled
3256 * everywhere outside actual command execution. */
3257 /*set_jobctrl_sighandler(SIG_IGN);*/
3258 set_misc_sighandler(SIG_DFL);
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003259 /* Freeing 'head' here would break NOMMU. */
3260 _exit(run_list(head));
Eric Andersen25f27032001-04-26 23:22:31 +00003261 }
Eric Andersen25f27032001-04-26 23:22:31 +00003262 close(channel[1]);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003263 pf = fdopen(channel[0], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00003264 return pf;
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003265 /* 'head' is freed by the caller */
Eric Andersen25f27032001-04-26 23:22:31 +00003266}
3267
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003268/* Return code is exit status of the process that is run. */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003269static int process_command_subs(o_string *dest, struct p_context *ctx,
3270 struct in_str *input, const char *subst_end)
Eric Andersen25f27032001-04-26 23:22:31 +00003271{
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003272 int retcode, ch, eol_cnt;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003273 o_string result = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003274 struct p_context inner;
3275 FILE *p;
3276 struct in_str pipe_str;
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003277
Eric Andersen25f27032001-04-26 23:22:31 +00003278 initialize_context(&inner);
3279
3280 /* recursion to generate command */
3281 retcode = parse_stream(&result, &inner, input, subst_end);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003282 if (retcode != 0)
3283 return retcode; /* syntax error or EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003284 done_word(&result, &inner);
3285 done_pipe(&inner, PIPE_SEQ);
3286 b_free(&result);
3287
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003288 p = generate_stream_from_list(inner.list_head);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003289 if (p == NULL)
3290 return 1;
Denis Vlasenkoa0898172007-10-01 09:59:01 +00003291 close_on_exec_on(fileno(p));
Eric Andersen25f27032001-04-26 23:22:31 +00003292 setup_file_in_str(&pipe_str, p);
3293
3294 /* now send results of command back into original context */
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003295 eol_cnt = 0;
3296 while ((ch = b_getch(&pipe_str)) != EOF) {
3297 if (ch == '\n') {
3298 eol_cnt++;
3299 continue;
3300 }
3301 while (eol_cnt) {
Denis Vlasenkoff097622007-10-01 10:00:45 +00003302 b_addqchr(dest, '\n', dest->o_quote);
Denis Vlasenko3e9aaae2007-05-11 12:56:43 +00003303 eol_cnt--;
3304 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00003305 b_addqchr(dest, ch, dest->o_quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003306 }
3307
3308 debug_printf("done reading from pipe, pclose()ing\n");
3309 /* This is the step that wait()s for the child. Should be pretty
3310 * safe, since we just read an EOF from its stdout. We could try
Denis Vlasenko262d7652007-05-20 21:52:49 +00003311 * to do better, by using wait(), and keeping track of background jobs
Eric Andersen25f27032001-04-26 23:22:31 +00003312 * at the same time. That would be a lot of work, and contrary
3313 * to the KISS philosophy of this program. */
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003314 retcode = fclose(p);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003315 free_pipe_list(inner.list_head, /* indent: */ 0);
Denis Vlasenkoba7cf262007-05-25 14:34:30 +00003316 debug_printf("closed FILE from child, retcode=%d\n", retcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003317 return retcode;
3318}
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003319#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003320
3321static int parse_group(o_string *dest, struct p_context *ctx,
3322 struct in_str *input, int ch)
3323{
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003324 int rcode;
3325 const char *endch = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003326 struct p_context sub;
3327 struct child_prog *child = ctx->child;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003328
3329 debug_printf_parse("parse_group entered\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003330 if (child->argv) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003331 syntax(NULL);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003332 debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n");
3333 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003334 }
3335 initialize_context(&sub);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003336 endch = "}";
3337 if (ch == '(') {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003338 endch = ")";
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003339 child->subshell = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003340 }
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003341 rcode = parse_stream(dest, &sub, input, endch);
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00003342//vda: err chk?
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003343 done_word(dest, &sub); /* finish off the final word in the subcontext */
Eric Andersen25f27032001-04-26 23:22:31 +00003344 done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
3345 child->group = sub.list_head;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003346
3347 debug_printf_parse("parse_group return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00003348 return rcode;
3349 /* child remains "open", available for possible redirects */
3350}
3351
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003352/* Basically useful version until someone wants to get fancier,
Eric Andersen25f27032001-04-26 23:22:31 +00003353 * see the bash man page under "Parameter Expansion" */
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00003354static const char *lookup_param(const char *src)
Eric Andersen25f27032001-04-26 23:22:31 +00003355{
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003356 struct variable *var = get_local_var(src);
3357 if (var)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003358 return strchr(var->varstr, '=') + 1;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003359 return NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00003360}
3361
3362/* return code: 0 for OK, 1 for syntax error */
3363static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input)
3364{
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003365 int ch = b_peek(input); /* first character after the $ */
Denis Vlasenkoff097622007-10-01 10:00:45 +00003366 unsigned char quote_mask = dest->o_quote ? 0x80 : 0;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003367
3368 debug_printf_parse("handle_dollar entered: ch='%c'\n", ch);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003369 if (isalpha(ch)) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003370 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003371 //sp: ctx->child->sp++;
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003372 while (1) {
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003373 debug_printf_parse(": '%c'\n", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003374 b_getch(input);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003375 b_addchr(dest, ch | quote_mask);
3376 quote_mask = 0;
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003377 ch = b_peek(input);
3378 if (!isalnum(ch) && ch != '_')
3379 break;
Eric Andersen25f27032001-04-26 23:22:31 +00003380 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003381 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003382 } else if (isdigit(ch)) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003383 make_one_char_var:
3384 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003385 //sp: ctx->child->sp++;
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003386 debug_printf_parse(": '%c'\n", ch);
3387 b_getch(input);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003388 b_addchr(dest, ch | quote_mask);
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003389 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003390 } else switch (ch) {
Denis Vlasenko602d13c2007-05-13 18:34:53 +00003391 case '$': /* pid */
3392 case '!': /* last bg pid */
3393 case '?': /* last exit code */
3394 case '#': /* number of args */
3395 case '*': /* args */
3396 case '@': /* args */
3397 goto make_one_char_var;
Eric Andersen25f27032001-04-26 23:22:31 +00003398 case '{':
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003399 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003400 //sp: ctx->child->sp++;
Eric Andersen25f27032001-04-26 23:22:31 +00003401 b_getch(input);
3402 /* XXX maybe someone will try to escape the '}' */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003403 while (1) {
3404 ch = b_getch(input);
Denis Vlasenkob0550012007-05-24 12:18:16 +00003405 if (ch == '}')
3406 break;
3407 if (!isalnum(ch) && ch != '_') {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003408 syntax("unterminated ${name}");
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003409 debug_printf_parse("handle_dollar return 1: unterminated ${name}\n");
3410 return 1;
3411 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003412 debug_printf_parse(": '%c'\n", ch);
Denis Vlasenko1f4cf512007-05-16 10:39:24 +00003413 b_addchr(dest, ch | quote_mask);
3414 quote_mask = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00003415 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003416 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00003417 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003418#if ENABLE_HUSH_TICK
Eric Andersen25f27032001-04-26 23:22:31 +00003419 case '(':
Matt Kraai9f8caf12001-05-02 16:26:12 +00003420 b_getch(input);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003421 process_command_subs(dest, ctx, input, ")");
Eric Andersen25f27032001-04-26 23:22:31 +00003422 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003423#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003424 case '-':
3425 case '_':
3426 /* still unhandled, but should be eventually */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003427 bb_error_msg("unhandled syntax: $%c", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003428 return 1;
3429 break;
3430 default:
Denis Vlasenkoff097622007-10-01 10:00:45 +00003431 b_addqchr(dest, '$', dest->o_quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003432 }
Denis Vlasenkoe0a33672007-05-10 23:06:55 +00003433 debug_printf_parse("handle_dollar return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003434 return 0;
3435}
3436
Eric Andersen25f27032001-04-26 23:22:31 +00003437/* return code is 0 for normal exit, 1 for syntax error */
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003438static int parse_stream(o_string *dest, struct p_context *ctx,
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003439 struct in_str *input, const char *end_trigger)
Eric Andersen25f27032001-04-26 23:22:31 +00003440{
"Vladimir N. Oleynik"4ccd2b42006-01-31 09:27:48 +00003441 int ch, m;
Eric Andersen25f27032001-04-26 23:22:31 +00003442 int redir_fd;
3443 redir_type redir_style;
3444 int next;
3445
Denis Vlasenkoff097622007-10-01 10:00:45 +00003446 /* Only double-quote state is handled in the state variable dest->o_quote.
Eric Andersen25f27032001-04-26 23:22:31 +00003447 * A single-quote triggers a bypass of the main loop until its mate is
Denis Vlasenkoff097622007-10-01 10:00:45 +00003448 * found. When recursing, quote state is passed in via dest->o_quote. */
Eric Andersen25f27032001-04-26 23:22:31 +00003449
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003450 debug_printf_parse("parse_stream entered, end_trigger='%s'\n", end_trigger);
3451
Denis Vlasenko1a735862007-05-23 00:32:25 +00003452 while (1) {
Denis Vlasenko1a735862007-05-23 00:32:25 +00003453 m = CHAR_IFS;
3454 next = '\0';
Denis Vlasenko170435c2007-05-23 13:01:10 +00003455 ch = b_getch(input);
Denis Vlasenko1a735862007-05-23 00:32:25 +00003456 if (ch != EOF) {
3457 m = charmap[ch];
3458 if (ch != '\n')
3459 next = b_peek(input);
3460 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003461 debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n",
Denis Vlasenkoff097622007-10-01 10:00:45 +00003462 ch, ch, m, dest->o_quote);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003463 if (m == CHAR_ORDINARY
Denis Vlasenkoff097622007-10-01 10:00:45 +00003464 || (m != CHAR_SPECIAL && dest->o_quote)
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003465 ) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00003466 if (ch == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003467 syntax("unterminated \"");
Denis Vlasenko170435c2007-05-23 13:01:10 +00003468 debug_printf_parse("parse_stream return 1: unterminated \"\n");
3469 return 1;
3470 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00003471 b_addqchr(dest, ch, dest->o_quote);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003472 continue;
3473 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003474 if (m == CHAR_IFS) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003475 if (done_word(dest, ctx)) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003476 debug_printf_parse("parse_stream return 1: done_word!=0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003477 return 1;
Eric Andersenaac75e52001-04-30 18:18:45 +00003478 }
Denis Vlasenko1a735862007-05-23 00:32:25 +00003479 if (ch == EOF)
3480 break;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003481 /* If we aren't performing a substitution, treat
3482 * a newline as a command separator.
3483 * [why we don't handle it exactly like ';'? --vda] */
3484 if (end_trigger && ch == '\n') {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003485 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003486 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003487 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003488 if ((end_trigger && strchr(end_trigger, ch))
Denis Vlasenkoff097622007-10-01 10:00:45 +00003489 && !dest->o_quote && ctx->res_w == RES_NONE
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003490 ) {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003491 debug_printf_parse("parse_stream return 0: end_trigger char found\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003492 return 0;
3493 }
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003494 if (m == CHAR_IFS)
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003495 continue;
3496 switch (ch) {
Eric Andersen25f27032001-04-26 23:22:31 +00003497 case '#':
Denis Vlasenkoff097622007-10-01 10:00:45 +00003498 if (dest->length == 0 && !dest->o_quote) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003499 while (1) {
3500 ch = b_peek(input);
3501 if (ch == EOF || ch == '\n')
3502 break;
3503 b_getch(input);
3504 }
Eric Andersen25f27032001-04-26 23:22:31 +00003505 } else {
Denis Vlasenkoff097622007-10-01 10:00:45 +00003506 b_addqchr(dest, ch, dest->o_quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003507 }
3508 break;
3509 case '\\':
3510 if (next == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003511 syntax("\\<eof>");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003512 debug_printf_parse("parse_stream return 1: \\<eof>\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003513 return 1;
3514 }
Denis Vlasenkoff097622007-10-01 10:00:45 +00003515 b_addqchr(dest, '\\', dest->o_quote);
3516 b_addqchr(dest, b_getch(input), dest->o_quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003517 break;
3518 case '$':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003519 if (handle_dollar(dest, ctx, input) != 0) {
3520 debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n");
3521 return 1;
3522 }
Eric Andersen25f27032001-04-26 23:22:31 +00003523 break;
3524 case '\'':
3525 dest->nonnull = 1;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003526 while (1) {
3527 ch = b_getch(input);
3528 if (ch == EOF || ch == '\'')
3529 break;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003530 b_addchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003531 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003532 if (ch == EOF) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003533 syntax("unterminated '");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003534 debug_printf_parse("parse_stream return 1: unterminated '\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003535 return 1;
3536 }
3537 break;
3538 case '"':
3539 dest->nonnull = 1;
Denis Vlasenkoff097622007-10-01 10:00:45 +00003540 dest->o_quote ^= 1; /* invert */
Eric Andersen25f27032001-04-26 23:22:31 +00003541 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003542#if ENABLE_HUSH_TICK
Eric Andersen25f27032001-04-26 23:22:31 +00003543 case '`':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003544 process_command_subs(dest, ctx, input, "`");
Eric Andersen25f27032001-04-26 23:22:31 +00003545 break;
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003546#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003547 case '>':
3548 redir_fd = redirect_opt_num(dest);
3549 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003550 redir_style = REDIRECT_OVERWRITE;
Eric Andersen25f27032001-04-26 23:22:31 +00003551 if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003552 redir_style = REDIRECT_APPEND;
Eric Andersen25f27032001-04-26 23:22:31 +00003553 b_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003554 }
3555#if 0
3556 else if (next == '(') {
3557 syntax(">(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003558 debug_printf_parse("parse_stream return 1: >(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003559 return 1;
3560 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003561#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003562 setup_redirect(ctx, redir_fd, redir_style, input);
3563 break;
3564 case '<':
3565 redir_fd = redirect_opt_num(dest);
3566 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003567 redir_style = REDIRECT_INPUT;
Eric Andersen25f27032001-04-26 23:22:31 +00003568 if (next == '<') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003569 redir_style = REDIRECT_HEREIS;
Eric Andersen25f27032001-04-26 23:22:31 +00003570 b_getch(input);
3571 } else if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003572 redir_style = REDIRECT_IO;
Eric Andersen25f27032001-04-26 23:22:31 +00003573 b_getch(input);
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003574 }
3575#if 0
3576 else if (next == '(') {
3577 syntax("<(process) not supported");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003578 debug_printf_parse("parse_stream return 1: <(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003579 return 1;
3580 }
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003581#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003582 setup_redirect(ctx, redir_fd, redir_style, input);
3583 break;
3584 case ';':
3585 done_word(dest, ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003586 done_pipe(ctx, PIPE_SEQ);
Eric Andersen25f27032001-04-26 23:22:31 +00003587 break;
3588 case '&':
3589 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003590 if (next == '&') {
Eric Andersen25f27032001-04-26 23:22:31 +00003591 b_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003592 done_pipe(ctx, PIPE_AND);
Eric Andersen25f27032001-04-26 23:22:31 +00003593 } else {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003594 done_pipe(ctx, PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00003595 }
3596 break;
3597 case '|':
3598 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003599 if (next == '|') {
Eric Andersen25f27032001-04-26 23:22:31 +00003600 b_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003601 done_pipe(ctx, PIPE_OR);
Eric Andersen25f27032001-04-26 23:22:31 +00003602 } else {
3603 /* we could pick up a file descriptor choice here
3604 * with redirect_opt_num(), but bash doesn't do it.
3605 * "echo foo 2| cat" yields "foo 2". */
3606 done_command(ctx);
3607 }
3608 break;
3609 case '(':
3610 case '{':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003611 if (parse_group(dest, ctx, input, ch) != 0) {
3612 debug_printf_parse("parse_stream return 1: parse_group returned non-0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003613 return 1;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003614 }
Eric Andersen25f27032001-04-26 23:22:31 +00003615 break;
3616 case ')':
3617 case '}':
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003618 syntax("unexpected }"); /* Proper use of this character is caught by end_trigger */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003619 debug_printf_parse("parse_stream return 1: unexpected '}'\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003620 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003621 default:
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003622 if (ENABLE_HUSH_DEBUG)
3623 bb_error_msg_and_die("BUG: unexpected %c\n", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003624 }
3625 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003626 /* Complain if quote? No, maybe we just finished a command substitution
Eric Andersen25f27032001-04-26 23:22:31 +00003627 * that was quoted. Example:
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003628 * $ echo "`cat foo` plus more"
Eric Andersen25f27032001-04-26 23:22:31 +00003629 * and we just got the EOF generated by the subshell that ran "cat foo"
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003630 * The only real complaint is if we got an EOF when end_trigger != NULL,
Eric Andersen25f27032001-04-26 23:22:31 +00003631 * that is, we were really supposed to get end_trigger, and never got
3632 * one before the EOF. Can't use the standard "syntax error" return code,
3633 * so that parse_stream_outer can distinguish the EOF and exit smoothly. */
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003634 debug_printf_parse("parse_stream return %d\n", -(end_trigger != NULL));
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003635 if (end_trigger)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003636 return -1;
Eric Andersen25f27032001-04-26 23:22:31 +00003637 return 0;
3638}
3639
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003640static void set_in_charmap(const char *set, int code)
Eric Andersen25f27032001-04-26 23:22:31 +00003641{
Denis Vlasenkof5294e12007-04-14 10:09:57 +00003642 while (*set)
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003643 charmap[(unsigned char)*set++] = code;
Eric Andersen25f27032001-04-26 23:22:31 +00003644}
3645
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003646static void update_charmap(void)
Eric Andersen25f27032001-04-26 23:22:31 +00003647{
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003648 /* char *ifs and char charmap[256] are both globals. */
Eric Andersen25f27032001-04-26 23:22:31 +00003649 ifs = getenv("IFS");
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003650 if (ifs == NULL)
3651 ifs = " \t\n";
Eric Andersen25f27032001-04-26 23:22:31 +00003652 /* Precompute a list of 'flow through' behavior so it can be treated
3653 * quickly up front. Computation is necessary because of IFS.
3654 * Special case handling of IFS == " \t\n" is not implemented.
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003655 * The charmap[] array only really needs two bits each,
3656 * and on most machines that would be faster (reduced L1 cache use).
Eric Andersen25f27032001-04-26 23:22:31 +00003657 */
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003658 memset(charmap, CHAR_ORDINARY, sizeof(charmap));
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003659#if ENABLE_HUSH_TICK
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003660 set_in_charmap("\\$\"`", CHAR_SPECIAL);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003661#else
3662 set_in_charmap("\\$\"", CHAR_SPECIAL);
3663#endif
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003664 set_in_charmap("<>;&|(){}#'", CHAR_ORDINARY_IF_QUOTED);
Denis Vlasenko14b5dd92007-05-20 21:51:38 +00003665 set_in_charmap(ifs, CHAR_IFS); /* are ordinary if quoted */
Eric Andersen25f27032001-04-26 23:22:31 +00003666}
3667
Eric Andersenaff114c2004-04-14 17:51:38 +00003668/* most recursion does not come through here, the exception is
Denis Vlasenko170435c2007-05-23 13:01:10 +00003669 * from builtin_source() and builtin_eval() */
3670static int parse_and_run_stream(struct in_str *inp, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003671{
Eric Andersen25f27032001-04-26 23:22:31 +00003672 struct p_context ctx;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003673 o_string temp = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003674 int rcode;
3675 do {
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003676 ctx.parse_type = parse_flag;
Eric Andersen25f27032001-04-26 23:22:31 +00003677 initialize_context(&ctx);
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003678 update_charmap();
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003679 if (!(parse_flag & PARSEFLAG_SEMICOLON) || (parse_flag & PARSEFLAG_REPARSING))
Denis Vlasenko8f6bdb42007-05-16 09:36:55 +00003680 set_in_charmap(";$&|", CHAR_ORDINARY);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003681#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko5a1437d2007-05-24 13:22:47 +00003682 inp->promptmode = 0; /* PS1 */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003683#endif
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003684 /* We will stop & execute after each ';' or '\n'.
3685 * Example: "sleep 9999; echo TEST" + ctrl-C:
3686 * TEST should be printed */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003687 rcode = parse_stream(&temp, &ctx, inp, ";\n");
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003688 if (rcode != 1 && ctx.old_flag != 0) {
Denis Vlasenko90e485c2007-05-23 15:22:50 +00003689 syntax(NULL);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003690 }
3691 if (rcode != 1 && ctx.old_flag == 0) {
3692 done_word(&temp, &ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003693 done_pipe(&ctx, PIPE_SEQ);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00003694 debug_print_tree(ctx.list_head, 0);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003695 debug_printf_exec("parse_stream_outer: run_and_free_list\n");
3696 run_and_free_list(ctx.list_head);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003697 } else {
3698 if (ctx.old_flag != 0) {
3699 free(ctx.stack);
3700 b_reset(&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003701 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003702 temp.nonnull = 0;
Denis Vlasenkoff097622007-10-01 10:00:45 +00003703 temp.o_quote = 0;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003704 inp->p = NULL;
Denis Vlasenko05743d72008-02-10 12:10:08 +00003705 free_pipe_list(ctx.list_head, /* indent: */ 0);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003706 }
Eric Andersena813afc2001-05-24 16:19:36 +00003707 b_free(&temp);
Denis Vlasenko219e88d2007-05-21 10:18:23 +00003708 } while (rcode != -1 && !(parse_flag & PARSEFLAG_EXIT_FROM_LOOP)); /* loop on syntax errors, return on EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003709 return 0;
3710}
3711
Denis Vlasenko170435c2007-05-23 13:01:10 +00003712static int parse_and_run_string(const char *s, int parse_flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003713{
3714 struct in_str input;
3715 setup_string_in_str(&input, s);
Denis Vlasenko170435c2007-05-23 13:01:10 +00003716 return parse_and_run_stream(&input, parse_flag);
Eric Andersen25f27032001-04-26 23:22:31 +00003717}
3718
Denis Vlasenko170435c2007-05-23 13:01:10 +00003719static int parse_and_run_file(FILE *f)
Eric Andersen25f27032001-04-26 23:22:31 +00003720{
3721 int rcode;
3722 struct in_str input;
3723 setup_file_in_str(&input, f);
Denis Vlasenko170435c2007-05-23 13:01:10 +00003724 rcode = parse_and_run_stream(&input, PARSEFLAG_SEMICOLON);
Eric Andersen25f27032001-04-26 23:22:31 +00003725 return rcode;
3726}
3727
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003728#if ENABLE_HUSH_JOB
Eric Andersen6c947d22001-06-25 22:24:38 +00003729/* Make sure we have a controlling tty. If we get started under a job
3730 * aware app (like bash for example), make sure we are now in charge so
3731 * we don't fight over who gets the foreground */
Eric Anderseneaecbf32001-10-31 10:41:31 +00003732static void setup_job_control(void)
Eric Andersen52a97ca2001-06-22 06:49:26 +00003733{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003734 pid_t shell_pgrp;
3735
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003736 saved_task_pgrp = shell_pgrp = getpgrp();
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003737 debug_printf_jobs("saved_task_pgrp=%d\n", saved_task_pgrp);
Denis Vlasenko96e1b382007-09-30 23:50:48 +00003738 close_on_exec_on(interactive_fd);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003739
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003740 /* If we were ran as 'hush &',
3741 * sleep until we are in the foreground. */
3742 while (tcgetpgrp(interactive_fd) != shell_pgrp) {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003743 /* Send TTIN to ourself (should stop us) */
Denis Vlasenkoff131b92007-04-10 15:42:06 +00003744 kill(- shell_pgrp, SIGTTIN);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003745 shell_pgrp = getpgrp();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003746 }
Eric Andersen52a97ca2001-06-22 06:49:26 +00003747
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003748 /* Ignore job-control and misc signals. */
3749 set_jobctrl_sighandler(SIG_IGN);
3750 set_misc_sighandler(SIG_IGN);
3751//huh? signal(SIGCHLD, SIG_IGN);
3752
Denis Vlasenkoa6c467f2007-05-05 15:10:52 +00003753 /* We _must_ restore tty pgrp on fatal signals */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003754 set_fatal_sighandler(sigexit);
Eric Andersen6c947d22001-06-25 22:24:38 +00003755
3756 /* Put ourselves in our own process group. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003757 setpgrp(); /* is the same as setpgid(our_pid, our_pid); */
Eric Andersen6c947d22001-06-25 22:24:38 +00003758 /* Grab control of the terminal. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003759 tcsetpgrp(interactive_fd, getpid());
Eric Andersen6c947d22001-06-25 22:24:38 +00003760}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003761#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00003762
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00003763int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Matt Kraai2d91deb2001-08-01 17:21:35 +00003764int hush_main(int argc, char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00003765{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00003766 static const char version_str[] ALIGN1 = "HUSH_VERSION="HUSH_VER_STR;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003767 static const struct variable const_shell_ver = {
3768 .next = NULL,
3769 .varstr = (char*)version_str,
3770 .max_len = 1, /* 0 can provoke free(name) */
3771 .flg_export = 1,
3772 .flg_read_only = 1,
3773 };
3774
Eric Andersen25f27032001-04-26 23:22:31 +00003775 int opt;
3776 FILE *input;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003777 char **e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003778 struct variable *cur_var;
Eric Andersenbc604a22001-05-16 05:24:03 +00003779
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003780 PTR_TO_GLOBALS = xzalloc(sizeof(G));
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003781
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003782 /* Deal with HUSH_VERSION */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003783 shell_ver = const_shell_ver; /* copying struct here */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003784 top_var = &shell_ver;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003785 unsetenv("HUSH_VERSION"); /* in case it exists in initial env */
3786 /* Initialize our shell local variables with the values
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003787 * currently living in the environment */
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003788 cur_var = top_var;
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003789 e = environ;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003790 if (e) while (*e) {
3791 char *value = strchr(*e, '=');
3792 if (value) { /* paranoia */
3793 cur_var->next = xzalloc(sizeof(*cur_var));
3794 cur_var = cur_var->next;
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003795 cur_var->varstr = *e;
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003796 cur_var->max_len = strlen(*e);
3797 cur_var->flg_export = 1;
3798 }
3799 e++;
3800 }
Denis Vlasenko0a83fc32007-05-25 11:12:32 +00003801 putenv((char *)version_str); /* reinstate HUSH_VERSION */
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003802
Denis Vlasenko38f63192007-01-22 09:03:07 +00003803#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003804 line_input_state = new_line_input_t(FOR_SHELL);
3805#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003806 /* XXX what should these be while sourcing /etc/profile? */
3807 global_argc = argc;
3808 global_argv = argv;
Eric Andersen94ac2442001-05-22 19:05:18 +00003809 /* Initialize some more globals to non-zero values */
3810 set_cwd();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003811#if ENABLE_HUSH_INTERACTIVE
3812#if ENABLE_FEATURE_EDITING
3813 cmdedit_set_initial_prompt();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003814#endif
Eric Andersen94ac2442001-05-22 19:05:18 +00003815 PS2 = "> ";
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003816#endif
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003817
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003818 if (EXIT_SUCCESS) /* otherwise is already done */
3819 last_return_code = EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00003820
3821 if (argv[0] && argv[0][0] == '-') {
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003822 debug_printf("sourcing /etc/profile\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003823 input = fopen("/etc/profile", "r");
3824 if (input != NULL) {
Denis Vlasenkoa0898172007-10-01 09:59:01 +00003825 close_on_exec_on(fileno(input));
Denis Vlasenko170435c2007-05-23 13:01:10 +00003826 parse_and_run_file(input);
Eric Andersena90f20b2001-06-26 23:00:21 +00003827 fclose(input);
3828 }
Eric Andersen25f27032001-04-26 23:22:31 +00003829 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003830 input = stdin;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003831
Eric Andersen25f27032001-04-26 23:22:31 +00003832 while ((opt = getopt(argc, argv, "c:xif")) > 0) {
3833 switch (opt) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003834 case 'c':
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003835 global_argv = argv + optind;
3836 global_argc = argc - optind;
Denis Vlasenko170435c2007-05-23 13:01:10 +00003837 opt = parse_and_run_string(optarg, PARSEFLAG_SEMICOLON);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003838 goto final_return;
3839 case 'i':
Denis Vlasenkoc666f712007-05-16 22:18:54 +00003840 /* Well, we cannot just declare interactiveness,
3841 * we have to have some stuff (ctty, etc) */
3842 /* interactive_fd++; */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003843 break;
3844 case 'f':
Denis Vlasenkoc8be5ee2007-05-17 15:38:46 +00003845 fake_mode = 1;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003846 break;
3847 default:
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003848#ifndef BB_VER
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003849 fprintf(stderr, "Usage: sh [FILE]...\n"
3850 " or: sh -c command [args]...\n\n");
3851 exit(EXIT_FAILURE);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003852#else
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003853 bb_show_usage();
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003854#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003855 }
3856 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003857#if ENABLE_HUSH_JOB
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003858 /* A shell is interactive if the '-i' flag was given, or if all of
Eric Andersen25f27032001-04-26 23:22:31 +00003859 * the following conditions are met:
Denis Vlasenko55b2de72007-04-18 17:21:28 +00003860 * no -c command
Eric Andersen25f27032001-04-26 23:22:31 +00003861 * no arguments remaining or the -s flag given
3862 * standard input is a terminal
3863 * standard output is a terminal
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003864 * Refer to Posix.2, the description of the 'sh' utility. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003865 if (argv[optind] == NULL && input == stdin
3866 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
3867 ) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003868 saved_tty_pgrp = tcgetpgrp(STDIN_FILENO);
3869 debug_printf("saved_tty_pgrp=%d\n", saved_tty_pgrp);
3870 if (saved_tty_pgrp >= 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003871 /* try to dup to high fd#, >= 255 */
3872 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
3873 if (interactive_fd < 0) {
3874 /* try to dup to any fd */
3875 interactive_fd = dup(STDIN_FILENO);
3876 if (interactive_fd < 0)
3877 /* give up */
3878 interactive_fd = 0;
3879 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003880 // TODO: track & disallow any attempts of user
3881 // to (inadvertently) close/redirect it
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003882 }
Eric Andersen25f27032001-04-26 23:22:31 +00003883 }
Denis Vlasenko21f0d4c2007-05-06 14:15:42 +00003884 debug_printf("interactive_fd=%d\n", interactive_fd);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003885 if (interactive_fd) {
Denis Vlasenko08126f62008-02-11 08:39:11 +00003886 fcntl(interactive_fd, F_SETFD, FD_CLOEXEC);
Eric Andersen25f27032001-04-26 23:22:31 +00003887 /* Looks like they want an interactive shell */
Eric Andersen52a97ca2001-06-22 06:49:26 +00003888 setup_job_control();
Denis Vlasenko4ecfcdc2008-02-11 08:32:31 +00003889 /* -1 is special - makes xfuncs longjmp, not exit
Denis Vlasenkoc04163a2008-02-11 08:30:53 +00003890 * (we reset die_sleep = 0 whereever we [v]fork) */
3891 die_sleep = -1;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003892 if (setjmp(die_jmp)) {
3893 /* xfunc has failed! die die die */
3894 hush_exit(xfunc_error_retval);
3895 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003896#if !ENABLE_FEATURE_SH_EXTRA_QUIET
Denis Vlasenkoca525b42007-06-13 12:27:17 +00003897 printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", bb_banner);
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003898 printf("Enter 'help' for a list of built-in commands.\n\n");
3899#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00003900 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003901#elif ENABLE_HUSH_INTERACTIVE
3902/* no job control compiled, only prompt/line editing */
3903 if (argv[optind] == NULL && input == stdin
3904 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
3905 ) {
3906 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
3907 if (interactive_fd < 0) {
3908 /* try to dup to any fd */
3909 interactive_fd = dup(STDIN_FILENO);
3910 if (interactive_fd < 0)
3911 /* give up */
3912 interactive_fd = 0;
3913 }
Denis Vlasenko08126f62008-02-11 08:39:11 +00003914 if (interactive_fd)
3915 fcntl(interactive_fd, F_SETFD, FD_CLOEXEC);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003916 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003917#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003918
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003919 if (argv[optind] == NULL) {
Denis Vlasenko170435c2007-05-23 13:01:10 +00003920 opt = parse_and_run_file(stdin);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003921 } else {
3922 debug_printf("\nrunning script '%s'\n", argv[optind]);
3923 global_argv = argv + optind;
3924 global_argc = argc - optind;
3925 input = xfopen(argv[optind], "r");
Denis Vlasenko459a5ad2008-02-11 08:35:03 +00003926 fcntl(fileno(input), F_SETFD, FD_CLOEXEC);
Denis Vlasenko05743d72008-02-10 12:10:08 +00003927 opt = parse_and_run_file(input);
Eric Andersen25f27032001-04-26 23:22:31 +00003928 }
Eric Andersen25f27032001-04-26 23:22:31 +00003929
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003930 final_return:
3931
Denis Vlasenko38f63192007-01-22 09:03:07 +00003932#if ENABLE_FEATURE_CLEAN_UP
Eric Andersenaeb44c42001-05-22 20:29:00 +00003933 fclose(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003934 if (cwd != bb_msg_unknown)
Eric Andersenaeb44c42001-05-22 20:29:00 +00003935 free((char*)cwd);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003936 cur_var = top_var->next;
3937 while (cur_var) {
3938 struct variable *tmp = cur_var;
3939 if (!cur_var->max_len)
Denis Vlasenko28c0f0f2007-05-25 02:46:01 +00003940 free(cur_var->varstr);
Denis Vlasenkod76c0492007-05-25 02:16:25 +00003941 cur_var = cur_var->next;
3942 free(tmp);
Eric Andersenaeb44c42001-05-22 20:29:00 +00003943 }
Eric Andersen25f27032001-04-26 23:22:31 +00003944#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003945 hush_exit(opt ? opt : last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +00003946}
Denis Vlasenko96702ca2007-11-23 23:28:55 +00003947
3948
3949#if ENABLE_LASH
3950int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
3951int lash_main(int argc, char **argv)
3952{
3953 //bb_error_msg("lash is deprecated, please use hush instead");
3954 return hush_main(argc, argv);
3955}
3956#endif