blob: 78531e864f27f7da2a950763d28060619e977f70 [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?)
40 * $@ (those sure look like weird quoting rules)
41 * $_
42 * ! negation operator for pipes
43 * &> and >& redirection of stdout+stderr
44 * Brace Expansion
45 * Tilde Expansion
46 * fancy forms of Parameter Expansion
Eric Andersen78a7c992001-05-15 16:30:25 +000047 * aliases
Eric Andersen25f27032001-04-26 23:22:31 +000048 * Arithmetic Expansion
49 * <(list) and >(list) Process Substitution
Eric Andersen83a2ae22001-05-07 17:59:25 +000050 * reserved words: case, esac, select, function
Eric Andersen25f27032001-04-26 23:22:31 +000051 * Here Documents ( << word )
52 * Functions
53 * Major bugs:
Denis Vlasenkob81b3df2007-04-28 16:48:04 +000054 * job handling woefully incomplete and buggy (improved --vda)
Eric Andersen25f27032001-04-26 23:22:31 +000055 * reserved word execution woefully incomplete and buggy
Eric Andersen25f27032001-04-26 23:22:31 +000056 * to-do:
Eric Andersen83a2ae22001-05-07 17:59:25 +000057 * port selected bugfixes from post-0.49 busybox lash - done?
58 * finish implementing reserved words: for, while, until, do, done
59 * change { and } from special chars to reserved words
60 * builtins: break, continue, eval, return, set, trap, ulimit
61 * test magic exec
Eric Andersen25f27032001-04-26 23:22:31 +000062 * handle children going into background
63 * clean up recognition of null pipes
Eric Andersen25f27032001-04-26 23:22:31 +000064 * check setting of global_argc and global_argv
65 * control-C handling, probably with longjmp
Eric Andersen25f27032001-04-26 23:22:31 +000066 * follow IFS rules more precisely, including update semantics
Eric Andersen25f27032001-04-26 23:22:31 +000067 * figure out what to do with backslash-newline
68 * explain why we use signal instead of sigaction
69 * propagate syntax errors, die on resource errors?
70 * continuation lines, both explicit and implicit - done?
71 * memory leak finding and plugging - done?
72 * more testing, especially quoting rules and redirection
Eric Andersen78a7c992001-05-15 16:30:25 +000073 * document how quoting rules not precisely followed for variable assignments
Eric Andersen25f27032001-04-26 23:22:31 +000074 * maybe change map[] to use 2-bit entries
75 * (eventually) remove all the printf's
Eric Andersen25f27032001-04-26 23:22:31 +000076 *
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000077 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen25f27032001-04-26 23:22:31 +000078 */
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000079
80#include "busybox.h"
Eric Andersen25f27032001-04-26 23:22:31 +000081#include <glob.h> /* glob, of course */
Eric Andersen25f27032001-04-26 23:22:31 +000082#include <getopt.h> /* should be pretty obvious */
Eric Andersen25f27032001-04-26 23:22:31 +000083/* #include <dmalloc.h> */
Denis Vlasenkod01ff132007-05-02 21:40:23 +000084
85
86/* If you comment out one of these below, it will be #defined later
87 * to perform debug printfs to stderr: */
Denis Vlasenko400c5b62007-05-04 13:07:27 +000088#define debug_printf(...) do {} while (0)
89/* Finer-grained debug switches */
90#define debug_printf_jobs(...) do {} while (0)
91#define debug_printf_exec(...) do {} while (0)
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +000092#define debug_printf_parse(...) do {} while (0)
Denis Vlasenko400c5b62007-05-04 13:07:27 +000093#define debug_print_tree(a, b) do {} while (0)
Denis Vlasenkod01ff132007-05-02 21:40:23 +000094
95
96#ifndef debug_printf
97#define debug_printf(...) fprintf(stderr, __VA_ARGS__)
98/* broken, of course, but OK for testing */
99static const char *indenter(int i)
100{
101 static const char blanks[] = " ";
102 return &blanks[sizeof(blanks) - i - 1];
103}
104#endif
105#define final_printf debug_printf
106
107#ifndef debug_printf_jobs
108#define debug_printf_jobs(...) fprintf(stderr, __VA_ARGS__)
109#define DEBUG_SHELL_JOBS 1
110#endif
111
112#ifndef debug_printf_exec
113#define debug_printf_exec(...) fprintf(stderr, __VA_ARGS__)
114#endif
115
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000116#ifndef debug_printf_parse
117#define debug_printf_parse(...) fprintf(stderr, __VA_ARGS__)
118#endif
119
Eric Andersen25f27032001-04-26 23:22:31 +0000120
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000121#if !ENABLE_HUSH_INTERACTIVE
122#undef ENABLE_FEATURE_EDITING
123#define ENABLE_FEATURE_EDITING 0
124#undef ENABLE_FEATURE_EDITING_FANCY_PROMPT
125#define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0
126#endif
"Robert P. J. Day"f3501602006-07-01 12:19:39 +0000127
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000128#define SPECIAL_VAR_SYMBOL 03
129#define FLAG_EXIT_FROM_LOOP 1
130#define FLAG_PARSE_SEMICOLON (1 << 1) /* symbol ';' is special for parser */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000131#define FLAG_REPARSING (1 << 2) /* >=2nd pass */
Eric Andersen25f27032001-04-26 23:22:31 +0000132
133typedef enum {
134 REDIRECT_INPUT = 1,
135 REDIRECT_OVERWRITE = 2,
136 REDIRECT_APPEND = 3,
137 REDIRECT_HEREIS = 4,
138 REDIRECT_IO = 5
139} redir_type;
140
141/* The descrip member of this structure is only used to make debugging
142 * output pretty */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000143static const struct {
144 int mode;
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000145 signed char default_fd;
146 char descrip[3];
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +0000147} redir_table[] = {
Eric Andersen25f27032001-04-26 23:22:31 +0000148 { 0, 0, "()" },
149 { O_RDONLY, 0, "<" },
150 { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
151 { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" },
152 { O_RDONLY, -1, "<<" },
153 { O_RDWR, 1, "<>" }
154};
155
156typedef enum {
157 PIPE_SEQ = 1,
158 PIPE_AND = 2,
159 PIPE_OR = 3,
160 PIPE_BG = 4,
161} pipe_style;
162
163/* might eventually control execution */
164typedef enum {
165 RES_NONE = 0,
166 RES_IF = 1,
167 RES_THEN = 2,
168 RES_ELIF = 3,
169 RES_ELSE = 4,
170 RES_FI = 5,
171 RES_FOR = 6,
172 RES_WHILE = 7,
173 RES_UNTIL = 8,
174 RES_DO = 9,
175 RES_DONE = 10,
Eric Andersenaf44a0e2001-04-27 07:26:12 +0000176 RES_XXXX = 11,
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000177 RES_IN = 12,
178 RES_SNTX = 13
Eric Andersen25f27032001-04-26 23:22:31 +0000179} reserved_style;
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000180enum {
181 FLAG_END = (1 << RES_NONE ),
182 FLAG_IF = (1 << RES_IF ),
183 FLAG_THEN = (1 << RES_THEN ),
184 FLAG_ELIF = (1 << RES_ELIF ),
185 FLAG_ELSE = (1 << RES_ELSE ),
186 FLAG_FI = (1 << RES_FI ),
187 FLAG_FOR = (1 << RES_FOR ),
188 FLAG_WHILE = (1 << RES_WHILE),
189 FLAG_UNTIL = (1 << RES_UNTIL),
190 FLAG_DO = (1 << RES_DO ),
191 FLAG_DONE = (1 << RES_DONE ),
192 FLAG_IN = (1 << RES_IN ),
193 FLAG_START = (1 << RES_XXXX ),
194};
Eric Andersen25f27032001-04-26 23:22:31 +0000195
196/* This holds pointers to the various results of parsing */
197struct p_context {
198 struct child_prog *child;
199 struct pipe *list_head;
200 struct pipe *pipe;
201 struct redir_struct *pending_redirect;
202 reserved_style w;
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000203 int old_flag; /* for figuring out valid reserved words */
Eric Andersen25f27032001-04-26 23:22:31 +0000204 struct p_context *stack;
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000205 int type; /* define type of parser : ";$" common or special symbol */
Eric Andersen25f27032001-04-26 23:22:31 +0000206 /* How about quoting status? */
207};
208
209struct redir_struct {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000210 struct redir_struct *next; /* pointer to the next redirect in the list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000211 redir_type type; /* type of redirection */
212 int fd; /* file descriptor being redirected */
213 int dup; /* -1, or file descriptor being duplicated */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000214 glob_t word; /* *word.gl_pathv is the filename */
Eric Andersen25f27032001-04-26 23:22:31 +0000215};
216
217struct child_prog {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000218 pid_t pid; /* 0 if exited */
219 char **argv; /* program name and arguments */
220 struct pipe *group; /* if non-NULL, first in group or subshell */
221 int subshell; /* flag, non-zero if group must be forked */
222 struct redir_struct *redirects; /* I/O redirections */
223 glob_t glob_result; /* result of parameter globbing */
224 int is_stopped; /* is the program currently running? */
225 struct pipe *family; /* pointer back to the child's parent pipe */
226 int sp; /* number of SPECIAL_VAR_SYMBOL */
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000227 int type;
Eric Andersen25f27032001-04-26 23:22:31 +0000228};
229
230struct pipe {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000231 struct pipe *next;
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000232 int num_progs; /* total number of programs in job */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000233 int running_progs; /* number of programs running (not exited) */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000234 char *cmdbuf; /* buffer various argv's point into */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000235#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000236 int jobid; /* job number */
237 char *cmdtext; /* name of job */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000238 pid_t pgrp; /* process group ID for the job */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000239#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000240 struct child_prog *progs; /* array of commands in pipe */
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000241 int stopped_progs; /* number of programs alive, but stopped */
242 int job_context; /* bitmask defining current context */
243 pipe_style followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
244 reserved_style r_mode; /* supports if, for, while, until */
Eric Andersen25f27032001-04-26 23:22:31 +0000245};
246
Eric Andersen25f27032001-04-26 23:22:31 +0000247struct close_me {
Eric Andersen25f27032001-04-26 23:22:31 +0000248 struct close_me *next;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000249 int fd;
Eric Andersen25f27032001-04-26 23:22:31 +0000250};
251
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000252struct variables {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000253 struct variables *next;
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000254 const char *name;
255 const char *value;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000256 int flg_export;
257 int flg_read_only;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000258};
259
Eric Andersen25f27032001-04-26 23:22:31 +0000260/* globals, connect us to the outside world
261 * the first three support $?, $#, and $1 */
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +0000262static char **global_argv;
"Vladimir N. Oleynik"4ccd2b42006-01-31 09:27:48 +0000263static int global_argc;
264static int last_return_code;
Eric Andersen25f27032001-04-26 23:22:31 +0000265extern char **environ; /* This is in <unistd.h>, but protected with __USE_GNU */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000266
Eric Andersen25f27032001-04-26 23:22:31 +0000267/* "globals" within this file */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000268enum {
269 MAP_ORDINARY = 0,
270 MAP_FLOWTROUGH_IF_QUOTED = 1,
271 MAP_IFS_IF_UNQUOTED = 2, /* flow through if quoted too */
272 MAP_NEVER_FLOWTROUGH = 3,
273};
"Vladimir N. Oleynik"4ccd2b42006-01-31 09:27:48 +0000274static unsigned char map[256];
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000275static const char *ifs;
Eric Andersenbc604a22001-05-16 05:24:03 +0000276static int fake_mode;
Eric Andersenbc604a22001-05-16 05:24:03 +0000277static struct close_me *close_me_head;
Eric Andersencfa88ec2001-05-11 18:08:16 +0000278static const char *cwd;
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000279static unsigned last_bg_pid;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000280#if !ENABLE_HUSH_INTERACTIVE
281enum { interactive_fd = 0 };
282#else
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000283/* 'interactive_fd' is a fd# open to ctty, if we have one
284 * _AND_ if we decided to mess with job control */
285static int interactive_fd;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000286#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000287static pid_t saved_task_pgrp;
288static pid_t saved_tty_pgrp;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000289static int last_jobid;
290static struct pipe *job_list;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000291#endif
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +0000292static const char *PS1;
293static const char *PS2;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000294#endif
295
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000296#define HUSH_VER_STR "0.02"
297static struct variables shell_ver = { NULL, "HUSH_VERSION", HUSH_VER_STR, 1, 1 };
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +0000298static struct variables *top_vars = &shell_ver;
Eric Andersen25f27032001-04-26 23:22:31 +0000299
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000300#define B_CHUNK 100
Eric Andersen25f27032001-04-26 23:22:31 +0000301#define B_NOSPAC 1
Eric Andersen25f27032001-04-26 23:22:31 +0000302
303typedef struct {
304 char *data;
305 int length;
306 int maxlen;
307 int quote;
308 int nonnull;
309} o_string;
310#define NULL_O_STRING {NULL,0,0,0,0}
311/* used for initialization:
312 o_string foo = NULL_O_STRING; */
313
314/* I can almost use ordinary FILE *. Is open_memstream() universally
315 * available? Where is it documented? */
316struct in_str {
317 const char *p;
Matt Kraaibdd4ece2001-05-23 17:43:00 +0000318 char peek_buf[2];
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000319#if ENABLE_HUSH_INTERACTIVE
Eric Andersen25f27032001-04-26 23:22:31 +0000320 int __promptme;
321 int promptmode;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000322#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000323 FILE *file;
324 int (*get) (struct in_str *);
325 int (*peek) (struct in_str *);
326};
327#define b_getch(input) ((input)->get(input))
328#define b_peek(input) ((input)->peek(input))
329
330#define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
331
332struct built_in_command {
Denis Vlasenko1359da62007-04-21 23:27:30 +0000333 const char *cmd; /* name */
334 const char *descr; /* description */
335 int (*function) (char **argv); /* function ptr */
Eric Andersen25f27032001-04-26 23:22:31 +0000336};
337
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000338static void __syntax(int line)
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000339{
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000340 bb_error_msg("syntax error hush.c:%d", line);
Eric Andersen25f27032001-04-26 23:22:31 +0000341}
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000342#define syntax() __syntax(__LINE__)
Eric Andersen25f27032001-04-26 23:22:31 +0000343
344/* Index of subroutines: */
345/* function prototypes for builtins */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000346static int builtin_cd(char **argv);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000347static int builtin_eval(char **argv);
348static int builtin_exec(char **argv);
349static int builtin_exit(char **argv);
350static int builtin_export(char **argv);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000351#if ENABLE_HUSH_JOB
Denis Vlasenko1359da62007-04-21 23:27:30 +0000352static int builtin_fg_bg(char **argv);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000353static int builtin_jobs(char **argv);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000354#endif
355static int builtin_help(char **argv);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000356static int builtin_pwd(char **argv);
357static int builtin_read(char **argv);
358static int builtin_set(char **argv);
359static int builtin_shift(char **argv);
360static int builtin_source(char **argv);
361static int builtin_umask(char **argv);
362static int builtin_unset(char **argv);
363static int builtin_not_written(char **argv);
Eric Andersen25f27032001-04-26 23:22:31 +0000364/* o_string manipulation: */
365static int b_check_space(o_string *o, int len);
366static int b_addchr(o_string *o, int ch);
367static void b_reset(o_string *o);
368static int b_addqchr(o_string *o, int ch, int quote);
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000369static int b_adduint(o_string *o, unsigned i);
Eric Andersen25f27032001-04-26 23:22:31 +0000370/* in_str manipulations: */
371static int static_get(struct in_str *i);
372static int static_peek(struct in_str *i);
373static int file_get(struct in_str *i);
374static int file_peek(struct in_str *i);
375static void setup_file_in_str(struct in_str *i, FILE *f);
376static void setup_string_in_str(struct in_str *i, const char *s);
377/* close_me manipulations: */
378static void mark_open(int fd);
379static void mark_closed(int fd);
Eric Anderseneaecbf32001-10-31 10:41:31 +0000380static void close_all(void);
Eric Andersen25f27032001-04-26 23:22:31 +0000381/* "run" the final data structures: */
Eric Andersenbf7df042001-05-23 22:18:35 +0000382static int free_pipe_list(struct pipe *head, int indent);
383static int free_pipe(struct pipe *pi, int indent);
Eric Andersen25f27032001-04-26 23:22:31 +0000384/* really run the final data structures: */
385static int setup_redirects(struct child_prog *prog, int squirrel[]);
Eric Andersen25f27032001-04-26 23:22:31 +0000386static int run_list_real(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000387static void pseudo_exec_argv(char **argv) ATTRIBUTE_NORETURN;
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000388static void pseudo_exec(struct child_prog *child) ATTRIBUTE_NORETURN;
Eric Andersen25f27032001-04-26 23:22:31 +0000389static int run_pipe_real(struct pipe *pi);
390/* extended glob support: */
391static int globhack(const char *src, int flags, glob_t *pglob);
392static int glob_needed(const char *s);
393static int xglob(o_string *dest, int flags, glob_t *pglob);
Eric Andersen78a7c992001-05-15 16:30:25 +0000394/* variable assignment: */
Eric Andersen78a7c992001-05-15 16:30:25 +0000395static int is_assignment(const char *s);
Eric Andersen25f27032001-04-26 23:22:31 +0000396/* data structure manipulation: */
397static int setup_redirect(struct p_context *ctx, int fd, redir_type style, struct in_str *input);
398static void initialize_context(struct p_context *ctx);
399static int done_word(o_string *dest, struct p_context *ctx);
400static int done_command(struct p_context *ctx);
401static int done_pipe(struct p_context *ctx, pipe_style type);
402/* primary string parsing: */
403static int redirect_dup_num(struct in_str *input);
404static int redirect_opt_num(o_string *o);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000405static int process_command_subs(o_string *dest, struct p_context *ctx, struct in_str *input, const char *subst_end);
Eric Andersen25f27032001-04-26 23:22:31 +0000406static int parse_group(o_string *dest, struct p_context *ctx, struct in_str *input, int ch);
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000407static const char *lookup_param(const char *src);
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000408static char *make_string(char **inp);
Eric Andersen25f27032001-04-26 23:22:31 +0000409static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input);
410static int parse_string(o_string *dest, struct p_context *ctx, const char *src);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +0000411static 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 +0000412/* setup: */
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000413static int parse_stream_outer(struct in_str *inp, int flag);
414static int parse_string_outer(const char *s, int flag);
Eric Andersen25f27032001-04-26 23:22:31 +0000415static int parse_file_outer(FILE *f);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000416/* job management: */
Eric Andersenc798b072001-06-22 06:23:03 +0000417static int checkjobs(struct pipe* fg_pipe);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000418#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +0000419static int checkjobs_and_fg_shell(struct pipe* fg_pipe);
Eric Andersenbafd94f2001-05-02 16:11:59 +0000420static void insert_bg_job(struct pipe *pi);
421static void remove_bg_job(struct pipe *pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000422static void delete_finished_bg_job(struct pipe *pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000423#else
424int checkjobs_and_fg_shell(struct pipe* fg_pipe); /* never called */
425#endif
Eric Andersenf72f5622001-05-15 23:21:41 +0000426/* local variable support */
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000427static char **make_list_in(char **inp, char *name);
428static char *insert_var_value(char *inp);
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000429static const char *get_local_var(const char *var);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000430static int set_local_var(const char *s, int flg_export);
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000431static void unset_local_var(const char *name);
Eric Andersen25f27032001-04-26 23:22:31 +0000432
433/* Table of built-in functions. They can be forked or not, depending on
434 * context: within pipes, they fork. As simple commands, they do not.
435 * When used in non-forking context, they can change global variables
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000436 * in the parent shell process. If forked, of course they cannot.
Eric Andersen25f27032001-04-26 23:22:31 +0000437 * For example, 'unset foo | whatever' will parse and run, but foo will
438 * still be set at the end. */
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +0000439static const struct built_in_command bltins[] = {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000440#if ENABLE_HUSH_JOB
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000441 { "bg", "Resume a job in the background", builtin_fg_bg },
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000442#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000443 { "break", "Exit for, while or until loop", builtin_not_written },
444 { "cd", "Change working directory", builtin_cd },
445 { "continue", "Continue for, while or until loop", builtin_not_written },
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000446 { "eval", "Construct and run shell command", builtin_eval },
447 { "exec", "Exec command, replacing this shell with the exec'd process",
448 builtin_exec },
449 { "exit", "Exit from shell()", builtin_exit },
450 { "export", "Set environment variable", builtin_export },
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000451#if ENABLE_HUSH_JOB
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000452 { "fg", "Bring job into the foreground", builtin_fg_bg },
453 { "jobs", "Lists the active jobs", builtin_jobs },
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000454#endif
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000455 { "pwd", "Print current directory", builtin_pwd },
456 { "read", "Input environment variable", builtin_read },
457 { "return", "Return from a function", builtin_not_written },
458 { "set", "Set/unset shell local variables", builtin_set },
459 { "shift", "Shift positional parameters", builtin_shift },
460 { "trap", "Trap signals", builtin_not_written },
461 { "ulimit","Controls resource limits", builtin_not_written },
462 { "umask","Sets file creation mask", builtin_umask },
463 { "unset", "Unset environment variable", builtin_unset },
464 { ".", "Source-in and run commands in a file", builtin_source },
465 { "help", "List shell built-in commands", builtin_help },
466 { NULL, NULL, NULL }
Eric Andersen25f27032001-04-26 23:22:31 +0000467};
468
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000469#if ENABLE_HUSH_JOB
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000470
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000471/* move to libbb? */
472static void signal_SA_RESTART(int sig, void (*handler)(int))
473{
474 struct sigaction sa;
475 sa.sa_handler = handler;
476 sa.sa_flags = SA_RESTART;
477 sigemptyset(&sa.sa_mask);
478 sigaction(sig, &sa, NULL);
479}
480
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000481/* Signals are grouped, we handle them in batches */
482static void set_fatal_sighandler(void (*handler)(int))
483{
484 signal(SIGILL , handler);
485 signal(SIGTRAP, handler);
486 signal(SIGABRT, handler);
487 signal(SIGFPE , handler);
488 signal(SIGBUS , handler);
489 signal(SIGSEGV, handler);
490 /* bash 3.2 seems to handle these just like 'fatal' ones */
491 signal(SIGHUP , handler);
492 signal(SIGPIPE, handler);
493 signal(SIGALRM, handler);
494}
495static void set_jobctrl_sighandler(void (*handler)(int))
496{
497 signal(SIGTSTP, handler);
498 signal(SIGTTIN, handler);
499 signal(SIGTTOU, handler);
500}
501static void set_misc_sighandler(void (*handler)(int))
502{
503 signal(SIGINT , handler);
504 signal(SIGQUIT, handler);
505 signal(SIGTERM, handler);
506}
507/* SIGCHLD is special and handled separately */
508
509static void set_every_sighandler(void (*handler)(int))
510{
511 set_fatal_sighandler(handler);
512 set_jobctrl_sighandler(handler);
513 set_misc_sighandler(handler);
514 signal(SIGCHLD, handler);
515}
516
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000517static struct pipe *toplevel_list;
518static sigjmp_buf toplevel_jb;
519smallint ctrl_z_flag;
520#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000521struct nofork_save_area nofork_save;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000522#endif
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000523
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000524static void handler_ctrl_c(int sig)
525{
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000526 debug_printf_jobs("got sig %d\n", sig);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000527// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000528 siglongjmp(toplevel_jb, 1);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +0000529}
530
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000531static void handler_ctrl_z(int sig)
532{
533 pid_t pid;
534
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000535 debug_printf_jobs("got tty sig %d in pid %d\n", sig, getpid());
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000536 pid = fork();
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000537 if (pid < 0) /* can't fork. Pretend there were no ctrl-Z */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000538 return;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000539 ctrl_z_flag = 1;
540//vda: wrong!!
541// toplevel_list->running_progs = 1;
542// toplevel_list->stopped_progs = 0;
543//
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000544 if (!pid) { /* child */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000545 setpgrp();
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000546 debug_printf_jobs("set pgrp for child %d ok\n", getpid());
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000547 set_every_sighandler(SIG_DFL);
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000548 raise(SIGTSTP); /* resend TSTP so that child will be stopped */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000549 debug_printf_jobs("returning in child\n");
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000550 /* return to nofork, it will eventually exit now,
551 * not return back to shell */
552 return;
553 }
554 /* parent */
555 /* finish filling up pipe info */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000556 toplevel_list->pgrp = pid; /* child is in its own pgrp */
557 toplevel_list->progs[0].pid = pid;
558//vda: wrong!!
559// toplevel_list->running_progs = 1;
560// toplevel_list->stopped_progs = 0;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000561 /* parent needs to longjmp out of running nofork.
562 * we will "return" exitcode 0, with child put in background */
563// as usual we can have all kinds of nasty problems with leaked malloc data here
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +0000564 debug_printf_jobs("siglongjmp in parent\n");
565 siglongjmp(toplevel_jb, 1);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000566}
567
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000568/* Restores tty foreground process group, and exits.
569 * May be called as signal handler for fatal signal
570 * (will faithfully resend signal to itself, producing correct exit state)
571 * or called directly with -EXITCODE.
572 * We also call it if xfunc is exiting. */
573static void sigexit(int sig) ATTRIBUTE_NORETURN;
574static void sigexit(int sig)
575{
576 sigset_t block_all;
577
578 /* Disable all signals: job control, SIGPIPE, etc. */
579 sigfillset(&block_all);
580 sigprocmask(SIG_SETMASK, &block_all, NULL);
581
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000582 if (interactive_fd)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000583 tcsetpgrp(interactive_fd, saved_tty_pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000584
585 /* Not a signal, just exit */
586 if (sig <= 0)
587 _exit(- sig);
588
589 /* Enable only this sig and kill ourself with it */
590 signal(sig, SIG_DFL);
591 sigdelset(&block_all, sig);
592 sigprocmask(SIG_SETMASK, &block_all, NULL);
593 raise(sig);
594 _exit(1); /* Should not reach it */
595}
596
597/* Restores tty foreground process group, and exits. */
598static void hush_exit(int exitcode) ATTRIBUTE_NORETURN;
599static void hush_exit(int exitcode)
600{
601 fflush(NULL); /* flush all streams */
602 sigexit(- (exitcode & 0xff));
603}
604
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000605#else /* !JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000606
607#define set_fatal_sighandler(handler) ((void)0)
608#define set_jobctrl_sighandler(handler) ((void)0)
609#define set_misc_sighandler(handler) ((void)0)
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000610#define hush_exit(e) exit(e)
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000611
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000612#endif /* JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000613
614
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000615static const char *set_cwd(void)
616{
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000617 if (cwd == bb_msg_unknown)
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000618 cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000619 cwd = xrealloc_getcwd_or_warn((char *)cwd);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000620 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000621 cwd = bb_msg_unknown;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000622 return cwd;
623}
624
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000625/* built-in 'eval' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000626static int builtin_eval(char **argv)
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000627{
628 char *str = NULL;
629 int rcode = EXIT_SUCCESS;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000630
Denis Vlasenko1359da62007-04-21 23:27:30 +0000631 if (argv[1]) {
632 str = make_string(argv + 1);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000633 parse_string_outer(str, FLAG_EXIT_FROM_LOOP |
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000634 FLAG_PARSE_SEMICOLON);
635 free(str);
636 rcode = last_return_code;
637 }
638 return rcode;
639}
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000640
Eric Andersen25f27032001-04-26 23:22:31 +0000641/* built-in 'cd <path>' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000642static int builtin_cd(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000643{
644 char *newdir;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000645 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000646 newdir = getenv("HOME");
647 else
Denis Vlasenko1359da62007-04-21 23:27:30 +0000648 newdir = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000649 if (chdir(newdir)) {
650 printf("cd: %s: %s\n", newdir, strerror(errno));
651 return EXIT_FAILURE;
652 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000653 set_cwd();
Eric Andersen25f27032001-04-26 23:22:31 +0000654 return EXIT_SUCCESS;
655}
656
Eric Andersen25f27032001-04-26 23:22:31 +0000657/* built-in 'exec' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000658static int builtin_exec(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000659{
Denis Vlasenko1359da62007-04-21 23:27:30 +0000660 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000661 return EXIT_SUCCESS; /* Really? */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000662 pseudo_exec_argv(argv + 1);
Eric Andersen25f27032001-04-26 23:22:31 +0000663 /* never returns */
664}
665
666/* built-in 'exit' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000667static int builtin_exit(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000668{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000669// TODO: bash does it ONLY on top-level sh exit (+interacive only?)
670 //puts("exit"); /* bash does it */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000671// TODO: warn if we have background jobs: "There are stopped jobs"
672// On second consecutive 'exit', exit anyway.
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000673
Denis Vlasenko1359da62007-04-21 23:27:30 +0000674 if (argv[1] == NULL)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000675 hush_exit(last_return_code);
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000676 /* mimic bash: exit 123abc == exit 255 + error msg */
677 xfunc_error_retval = 255;
678 /* bash: exit -2 == exit 254, no error msg */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000679 hush_exit(xatoi(argv[1]) & 0xff);
Eric Andersen25f27032001-04-26 23:22:31 +0000680}
681
682/* built-in 'export VAR=value' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000683static int builtin_export(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000684{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000685 int res = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000686 char *name = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000687
Eric Andersenf72f5622001-05-15 23:21:41 +0000688 if (name == NULL) {
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000689 // TODO:
690 // ash emits: export VAR='VAL'
691 // bash: declare -x VAR="VAL"
692 // (both also escape as needed (quotes, $, etc))
693 char **e = environ;
694 if (e)
695 while (*e)
696 puts(*e++);
697 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000698 }
Eric Andersenf72f5622001-05-15 23:21:41 +0000699
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000700 name = strdup(name);
Eric Andersenf72f5622001-05-15 23:21:41 +0000701
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000702 if (name) {
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000703 const char *value = strchr(name, '=');
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000704
Eric Andersen94ac2442001-05-22 19:05:18 +0000705 if (!value) {
706 char *tmp;
707 /* They are exporting something without an =VALUE */
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000708
Eric Andersen94ac2442001-05-22 19:05:18 +0000709 value = get_local_var(name);
710 if (value) {
711 size_t ln = strlen(name);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000712
Eric Andersen94ac2442001-05-22 19:05:18 +0000713 tmp = realloc(name, ln+strlen(value)+2);
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000714 if (tmp == NULL)
Eric Andersen94ac2442001-05-22 19:05:18 +0000715 res = -1;
716 else {
717 sprintf(tmp+ln, "=%s", value);
718 name = tmp;
719 }
720 } else {
721 /* bash does not return an error when trying to export
722 * an undefined variable. Do likewise. */
723 res = 1;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000724 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000725 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000726 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000727 if (res < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000728 bb_perror_msg("export");
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000729 else if (res == 0)
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000730 res = set_local_var(name, 1);
731 else
732 res = 0;
733 free(name);
734 return res;
Eric Andersen25f27032001-04-26 23:22:31 +0000735}
736
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000737#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +0000738/* built-in 'fg' and 'bg' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000739static int builtin_fg_bg(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000740{
Eric Andersen0fcd4472001-05-02 20:12:03 +0000741 int i, jobnum;
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000742 struct pipe *pi;
Eric Andersen25f27032001-04-26 23:22:31 +0000743
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000744 if (!interactive_fd)
Eric Andersenc798b072001-06-22 06:23:03 +0000745 return EXIT_FAILURE;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000746 /* If they gave us no args, assume they want the last backgrounded task */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000747 if (!argv[1]) {
Eric Andersenc798b072001-06-22 06:23:03 +0000748 for (pi = job_list; pi; pi = pi->next) {
749 if (pi->jobid == last_jobid) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000750 goto found;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000751 }
752 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000753 bb_error_msg("%s: no current job", argv[0]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000754 return EXIT_FAILURE;
755 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000756 if (sscanf(argv[1], "%%%d", &jobnum) != 1) {
757 bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000758 return EXIT_FAILURE;
759 }
760 for (pi = job_list; pi; pi = pi->next) {
761 if (pi->jobid == jobnum) {
762 goto found;
Eric Andersen25f27032001-04-26 23:22:31 +0000763 }
764 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000765 bb_error_msg("%s: %d: no such job", argv[0], jobnum);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000766 return EXIT_FAILURE;
767 found:
Denis Vlasenko52881e92007-04-21 13:42:52 +0000768 // TODO: bash prints a string representation
769 // of job being foregrounded (like "sleep 1 | cat")
Denis Vlasenko1359da62007-04-21 23:27:30 +0000770 if (*argv[0] == 'f') {
Eric Andersen028b65b2001-06-28 01:10:11 +0000771 /* Put the job into the foreground. */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000772 tcsetpgrp(interactive_fd, pi->pgrp);
Eric Andersen25f27032001-04-26 23:22:31 +0000773 }
774
775 /* Restart the processes in the job */
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000776 debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_progs, pi->pgrp);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000777 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000778 debug_printf_jobs("reviving pid %d\n", pi->progs[i].pid);
Eric Andersen0fcd4472001-05-02 20:12:03 +0000779 pi->progs[i].is_stopped = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000780 }
781 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +0000782
Denis Vlasenkobb81c582007-01-30 22:32:09 +0000783 i = kill(- pi->pgrp, SIGCONT);
784 if (i < 0) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000785 if (errno == ESRCH) {
Denis Vlasenko1359da62007-04-21 23:27:30 +0000786 delete_finished_bg_job(pi);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000787 return EXIT_SUCCESS;
Eric Andersen028b65b2001-06-28 01:10:11 +0000788 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000789 bb_perror_msg("kill (SIGCONT)");
Eric Andersen028b65b2001-06-28 01:10:11 +0000790 }
791 }
Eric Andersen25f27032001-04-26 23:22:31 +0000792
Denis Vlasenko1359da62007-04-21 23:27:30 +0000793 if (*argv[0] == 'f') {
794 remove_bg_job(pi);
Denis Vlasenko52881e92007-04-21 13:42:52 +0000795 return checkjobs_and_fg_shell(pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000796 }
Eric Andersen25f27032001-04-26 23:22:31 +0000797 return EXIT_SUCCESS;
798}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000799#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000800
801/* built-in 'help' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000802static int builtin_help(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +0000803{
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +0000804 const struct built_in_command *x;
Eric Andersen25f27032001-04-26 23:22:31 +0000805
806 printf("\nBuilt-in commands:\n");
807 printf("-------------------\n");
808 for (x = bltins; x->cmd; x++) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000809 if (x->descr == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000810 continue;
811 printf("%s\t%s\n", x->cmd, x->descr);
812 }
813 printf("\n\n");
814 return EXIT_SUCCESS;
815}
816
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000817#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +0000818/* built-in 'jobs' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000819static int builtin_jobs(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +0000820{
821 struct pipe *job;
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000822 const char *status_string;
Eric Andersen25f27032001-04-26 23:22:31 +0000823
Eric Andersenc798b072001-06-22 06:23:03 +0000824 for (job = job_list; job; job = job->next) {
Eric Andersen25f27032001-04-26 23:22:31 +0000825 if (job->running_progs == job->stopped_progs)
826 status_string = "Stopped";
827 else
828 status_string = "Running";
Eric Andersen52a97ca2001-06-22 06:49:26 +0000829
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000830 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
Eric Andersen25f27032001-04-26 23:22:31 +0000831 }
832 return EXIT_SUCCESS;
833}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000834#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000835
Eric Andersen25f27032001-04-26 23:22:31 +0000836/* built-in 'pwd' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000837static int builtin_pwd(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +0000838{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000839 puts(set_cwd());
Eric Andersen25f27032001-04-26 23:22:31 +0000840 return EXIT_SUCCESS;
841}
842
843/* built-in 'read VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000844static int builtin_read(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000845{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000846 int res;
Eric Andersen25f27032001-04-26 23:22:31 +0000847
Denis Vlasenko1359da62007-04-21 23:27:30 +0000848 if (argv[1]) {
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000849 char string[BUFSIZ];
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000850 char *var = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +0000851
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000852 string[0] = '\0'; /* In case stdin has only EOF */
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000853 /* read string */
854 fgets(string, sizeof(string), stdin);
855 chomp(string);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000856 var = malloc(strlen(argv[1]) + strlen(string) + 2);
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000857 if (var) {
Denis Vlasenko1359da62007-04-21 23:27:30 +0000858 sprintf(var, "%s=%s", argv[1], string);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000859 res = set_local_var(var, 0);
860 } else
Eric Andersen94ac2442001-05-22 19:05:18 +0000861 res = -1;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000862 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000863 bb_perror_msg("read");
Eric Andersen94ac2442001-05-22 19:05:18 +0000864 free(var); /* So not move up to avoid breaking errno */
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000865 return res;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000866 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000867 do res = getchar(); while (res != '\n' && res != EOF);
868 return 0;
Eric Andersen25f27032001-04-26 23:22:31 +0000869}
870
Eric Andersenf72f5622001-05-15 23:21:41 +0000871/* built-in 'set VAR=value' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000872static int builtin_set(char **argv)
Eric Andersenf72f5622001-05-15 23:21:41 +0000873{
Denis Vlasenko1359da62007-04-21 23:27:30 +0000874 char *temp = argv[1];
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000875 struct variables *e;
Eric Andersenf72f5622001-05-15 23:21:41 +0000876
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000877 if (temp == NULL)
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000878 for (e = top_vars; e; e = e->next)
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000879 printf("%s=%s\n", e->name, e->value);
880 else
881 set_local_var(temp, 0);
882
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000883 return EXIT_SUCCESS;
Eric Andersenf72f5622001-05-15 23:21:41 +0000884}
885
886
Eric Andersen25f27032001-04-26 23:22:31 +0000887/* Built-in 'shift' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000888static int builtin_shift(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000889{
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000890 int n = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000891 if (argv[1]) {
892 n = atoi(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +0000893 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000894 if (n >= 0 && n < global_argc) {
Eric Andersen25f27032001-04-26 23:22:31 +0000895 /* XXX This probably breaks $0 */
896 global_argc -= n;
897 global_argv += n;
898 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000899 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +0000900 return EXIT_FAILURE;
Eric Andersen25f27032001-04-26 23:22:31 +0000901}
902
903/* Built-in '.' handler (read-in and execute commands from file) */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000904static int builtin_source(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000905{
906 FILE *input;
907 int status;
908
Denis Vlasenko1359da62007-04-21 23:27:30 +0000909 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000910 return EXIT_FAILURE;
911
912 /* XXX search through $PATH is missing */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000913 input = fopen(argv[1], "r");
Eric Andersen25f27032001-04-26 23:22:31 +0000914 if (!input) {
Denis Vlasenko1359da62007-04-21 23:27:30 +0000915 bb_error_msg("cannot open '%s'", argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +0000916 return EXIT_FAILURE;
917 }
918
919 /* Now run the file */
920 /* XXX argv and argc are broken; need to save old global_argv
921 * (pointer only is OK!) on this stack frame,
Denis Vlasenko1359da62007-04-21 23:27:30 +0000922 * set global_argv=argv+1, recurse, and restore. */
Eric Andersen25f27032001-04-26 23:22:31 +0000923 mark_open(fileno(input));
924 status = parse_file_outer(input);
925 mark_closed(fileno(input));
926 fclose(input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000927 return status;
Eric Andersen25f27032001-04-26 23:22:31 +0000928}
929
Denis Vlasenko1359da62007-04-21 23:27:30 +0000930static int builtin_umask(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000931{
Eric Andersen83a2ae22001-05-07 17:59:25 +0000932 mode_t new_umask;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000933 const char *arg = argv[1];
Eric Andersen83a2ae22001-05-07 17:59:25 +0000934 char *end;
935 if (arg) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000936 new_umask = strtoul(arg, &end, 8);
937 if (*end != '\0' || end == arg) {
Eric Andersen83a2ae22001-05-07 17:59:25 +0000938 return EXIT_FAILURE;
939 }
940 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000941 new_umask = umask(0);
942 printf("%.3o\n", (unsigned) new_umask);
Eric Andersen83a2ae22001-05-07 17:59:25 +0000943 }
944 umask(new_umask);
945 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000946}
947
948/* built-in 'unset VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000949static int builtin_unset(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000950{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000951 /* bash returned already true */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000952 unset_local_var(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +0000953 return EXIT_SUCCESS;
954}
955
Denis Vlasenko1359da62007-04-21 23:27:30 +0000956static int builtin_not_written(char **argv)
Eric Andersen83a2ae22001-05-07 17:59:25 +0000957{
Denis Vlasenko1359da62007-04-21 23:27:30 +0000958 printf("builtin_%s not written\n", argv[0]);
Eric Andersen83a2ae22001-05-07 17:59:25 +0000959 return EXIT_FAILURE;
960}
961
Eric Andersen25f27032001-04-26 23:22:31 +0000962static int b_check_space(o_string *o, int len)
963{
964 /* It would be easy to drop a more restrictive policy
965 * in here, such as setting a maximum string length */
966 if (o->length + len > o->maxlen) {
967 char *old_data = o->data;
Denis Vlasenkof5294e12007-04-14 10:09:57 +0000968 /* assert(data == NULL || o->maxlen != 0); */
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000969 o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK);
Eric Andersen25f27032001-04-26 23:22:31 +0000970 o->data = realloc(o->data, 1 + o->maxlen);
971 if (o->data == NULL) {
972 free(old_data);
973 }
974 }
975 return o->data == NULL;
976}
977
978static int b_addchr(o_string *o, int ch)
979{
980 debug_printf("b_addchr: %c %d %p\n", ch, o->length, o);
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000981 if (b_check_space(o, 1))
982 return B_NOSPAC;
Eric Andersen25f27032001-04-26 23:22:31 +0000983 o->data[o->length] = ch;
984 o->length++;
985 o->data[o->length] = '\0';
986 return 0;
987}
988
989static void b_reset(o_string *o)
990{
991 o->length = 0;
992 o->nonnull = 0;
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000993 if (o->data != NULL)
994 *o->data = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +0000995}
996
997static void b_free(o_string *o)
998{
999 b_reset(o);
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001000 free(o->data);
Eric Andersen25f27032001-04-26 23:22:31 +00001001 o->data = NULL;
1002 o->maxlen = 0;
1003}
1004
1005/* My analysis of quoting semantics tells me that state information
1006 * is associated with a destination, not a source.
1007 */
1008static int b_addqchr(o_string *o, int ch, int quote)
1009{
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00001010 if (quote && strchr("*?[\\", ch)) {
Eric Andersen25f27032001-04-26 23:22:31 +00001011 int rc;
1012 rc = b_addchr(o, '\\');
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001013 if (rc)
1014 return rc;
Eric Andersen25f27032001-04-26 23:22:31 +00001015 }
1016 return b_addchr(o, ch);
1017}
1018
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001019static int b_adduint(o_string *o, unsigned i)
Eric Andersen25f27032001-04-26 23:22:31 +00001020{
1021 int r;
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00001022 char buf[sizeof(unsigned)*3 + 1];
1023 char *p = buf;
1024 *(utoa_to_buf(i, buf, sizeof(buf))) = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00001025 /* no escape checking necessary */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00001026 do r = b_addchr(o, *p++); while (r == 0 && *p);
Eric Andersen25f27032001-04-26 23:22:31 +00001027 return r;
1028}
1029
1030static int static_get(struct in_str *i)
1031{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001032 int ch = *i->p++;
1033 if (ch == '\0') return EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001034 return ch;
1035}
1036
1037static int static_peek(struct in_str *i)
1038{
1039 return *i->p;
1040}
1041
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001042#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001043#if ENABLE_FEATURE_EDITING
Rob Landley88621d72006-08-29 19:41:06 +00001044static void cmdedit_set_initial_prompt(void)
Eric Andersen25f27032001-04-26 23:22:31 +00001045{
Denis Vlasenko38f63192007-01-22 09:03:07 +00001046#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001047 PS1 = NULL;
1048#else
1049 PS1 = getenv("PS1");
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001050 if (PS1 == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +00001051 PS1 = "\\w \\$ ";
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001052#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001053}
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001054#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001055
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001056static const char* setup_prompt_string(int promptmode)
Eric Andersen25f27032001-04-26 23:22:31 +00001057{
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001058 const char *prompt_str;
1059 debug_printf("setup_prompt_string %d ", promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001060#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001061 /* Set up the prompt */
1062 if (promptmode == 1) {
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001063 char *ns;
1064 free((char*)PS1);
1065 ns = xmalloc(strlen(cwd)+4);
1066 sprintf(ns, "%s %s", cwd, (geteuid() != 0) ? "$ " : "# ");
1067 prompt_str = ns;
1068 PS1 = ns;
Eric Andersen25f27032001-04-26 23:22:31 +00001069 } else {
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001070 prompt_str = PS2;
Eric Andersen25f27032001-04-26 23:22:31 +00001071 }
1072#else
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001073 prompt_str = (promptmode == 1) ? PS1 : PS2;
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001074#endif
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001075 debug_printf("result %s\n", prompt_str);
1076 return prompt_str;
Eric Andersen25f27032001-04-26 23:22:31 +00001077}
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001078#endif /* ENABLE_HUSH_INTERACTIVE */
Eric Andersen25f27032001-04-26 23:22:31 +00001079
Denis Vlasenko38f63192007-01-22 09:03:07 +00001080#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001081static line_input_t *line_input_state;
1082#endif
1083
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001084#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0937be52007-04-28 16:47:08 +00001085static int get_user_input(struct in_str *i)
Eric Andersen25f27032001-04-26 23:22:31 +00001086{
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001087 static char the_command[ENABLE_FEATURE_EDITING ? BUFSIZ : 2];
1088
Denis Vlasenko0937be52007-04-28 16:47:08 +00001089 int r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001090 const char *prompt_str;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001091 prompt_str = setup_prompt_string(i->promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001092#if ENABLE_FEATURE_EDITING
Eric Andersen25f27032001-04-26 23:22:31 +00001093 /*
1094 ** enable command line editing only while a command line
1095 ** is actually being read; otherwise, we'll end up bequeathing
1096 ** atexit() handlers and other unwanted stuff to our
1097 ** child processes (rob@sysgo.de)
1098 */
Denis Vlasenko0937be52007-04-28 16:47:08 +00001099 r = read_line_input(prompt_str, the_command, BUFSIZ, line_input_state);
Eric Andersen25f27032001-04-26 23:22:31 +00001100#else
1101 fputs(prompt_str, stdout);
1102 fflush(stdout);
Denis Vlasenko0937be52007-04-28 16:47:08 +00001103 the_command[0] = r = fgetc(i->file);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001104 the_command[1] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00001105#endif
Eric Andersen4f6753e2001-05-31 17:17:12 +00001106 fflush(stdout);
Eric Andersen25f27032001-04-26 23:22:31 +00001107 i->p = the_command;
Denis Vlasenko0937be52007-04-28 16:47:08 +00001108 return r; /* < 0 == EOF. Not meaningful otherwise */
Eric Andersen25f27032001-04-26 23:22:31 +00001109}
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001110#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001111
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001112/* This is the magic location that prints prompts
Eric Andersen25f27032001-04-26 23:22:31 +00001113 * and gets data back from the user */
1114static int file_get(struct in_str *i)
1115{
1116 int ch;
1117
1118 ch = 0;
1119 /* If there is data waiting, eat it up */
1120 if (i->p && *i->p) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001121 ch = *i->p++;
Eric Andersen25f27032001-04-26 23:22:31 +00001122 } else {
1123 /* need to double check i->file because we might be doing something
1124 * more complicated by now, like sourcing or substituting. */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001125#if ENABLE_HUSH_INTERACTIVE
1126 if (interactive_fd && i->__promptme && i->file == stdin) {
Denis Vlasenko0937be52007-04-28 16:47:08 +00001127 while (!i->p || !(interactive_fd && i->p[0])) {
1128 if (get_user_input(i) < 0)
1129 return EOF;
Eric Andersen4f6753e2001-05-31 17:17:12 +00001130 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001131 i->promptmode = 2;
Eric Andersene67c3ce2001-05-02 02:09:36 +00001132 i->__promptme = 0;
1133 if (i->p && *i->p) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001134 ch = *i->p++;
Eric Andersene67c3ce2001-05-02 02:09:36 +00001135 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001136 } else
1137#endif
1138 {
Eric Andersene67c3ce2001-05-02 02:09:36 +00001139 ch = fgetc(i->file);
Eric Andersen25f27032001-04-26 23:22:31 +00001140 }
Eric Andersen25f27032001-04-26 23:22:31 +00001141 debug_printf("b_getch: got a %d\n", ch);
1142 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001143#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001144 if (ch == '\n')
1145 i->__promptme = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001146#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001147 return ch;
1148}
1149
1150/* All the callers guarantee this routine will never be
1151 * used right after a newline, so prompting is not needed.
1152 */
1153static int file_peek(struct in_str *i)
1154{
1155 if (i->p && *i->p) {
1156 return *i->p;
Eric Andersen25f27032001-04-26 23:22:31 +00001157 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001158 i->peek_buf[0] = fgetc(i->file);
1159 i->peek_buf[1] = '\0';
1160 i->p = i->peek_buf;
1161 debug_printf("b_peek: got a %d\n", *i->p);
1162 return *i->p;
Eric Andersen25f27032001-04-26 23:22:31 +00001163}
1164
1165static void setup_file_in_str(struct in_str *i, FILE *f)
1166{
1167 i->peek = file_peek;
1168 i->get = file_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001169#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001170 i->__promptme = 1;
1171 i->promptmode = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001172#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001173 i->file = f;
1174 i->p = NULL;
1175}
1176
1177static void setup_string_in_str(struct in_str *i, const char *s)
1178{
1179 i->peek = static_peek;
1180 i->get = static_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001181#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkobb81c582007-01-30 22:32:09 +00001182 i->__promptme = 1;
1183 i->promptmode = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001184#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001185 i->p = s;
1186}
1187
1188static void mark_open(int fd)
1189{
1190 struct close_me *new = xmalloc(sizeof(struct close_me));
1191 new->fd = fd;
1192 new->next = close_me_head;
1193 close_me_head = new;
1194}
1195
1196static void mark_closed(int fd)
1197{
1198 struct close_me *tmp;
1199 if (close_me_head == NULL || close_me_head->fd != fd)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001200 bb_error_msg_and_die("corrupt close_me");
Eric Andersen25f27032001-04-26 23:22:31 +00001201 tmp = close_me_head;
1202 close_me_head = close_me_head->next;
1203 free(tmp);
1204}
1205
Eric Anderseneaecbf32001-10-31 10:41:31 +00001206static void close_all(void)
Eric Andersen25f27032001-04-26 23:22:31 +00001207{
1208 struct close_me *c;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001209 for (c = close_me_head; c; c = c->next) {
Eric Andersen25f27032001-04-26 23:22:31 +00001210 close(c->fd);
1211 }
1212 close_me_head = NULL;
1213}
1214
1215/* squirrel != NULL means we squirrel away copies of stdin, stdout,
1216 * and stderr if they are redirected. */
1217static int setup_redirects(struct child_prog *prog, int squirrel[])
1218{
1219 int openfd, mode;
1220 struct redir_struct *redir;
1221
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001222 for (redir = prog->redirects; redir; redir = redir->next) {
Eric Andersen817e73c2001-06-06 17:56:09 +00001223 if (redir->dup == -1 && redir->word.gl_pathv == NULL) {
1224 /* something went wrong in the parse. Pretend it didn't happen */
1225 continue;
1226 }
Eric Andersen25f27032001-04-26 23:22:31 +00001227 if (redir->dup == -1) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001228 mode = redir_table[redir->type].mode;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001229 openfd = open_or_warn(redir->word.gl_pathv[0], mode);
Eric Andersen25f27032001-04-26 23:22:31 +00001230 if (openfd < 0) {
1231 /* this could get lost if stderr has been redirected, but
1232 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersen25f27032001-04-26 23:22:31 +00001233 return 1;
1234 }
1235 } else {
1236 openfd = redir->dup;
1237 }
1238
1239 if (openfd != redir->fd) {
1240 if (squirrel && redir->fd < 3) {
1241 squirrel[redir->fd] = dup(redir->fd);
1242 }
Eric Andersen83a2ae22001-05-07 17:59:25 +00001243 if (openfd == -3) {
1244 close(openfd);
1245 } else {
1246 dup2(openfd, redir->fd);
Matt Kraaic616e532001-06-05 16:50:08 +00001247 if (redir->dup == -1)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001248 close(openfd);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001249 }
Eric Andersen25f27032001-04-26 23:22:31 +00001250 }
1251 }
1252 return 0;
1253}
1254
1255static void restore_redirects(int squirrel[])
1256{
1257 int i, fd;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001258 for (i = 0; i < 3; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001259 fd = squirrel[i];
1260 if (fd != -1) {
1261 /* No error checking. I sure wouldn't know what
1262 * to do with an error if I found one! */
1263 dup2(fd, i);
1264 close(fd);
1265 }
1266 }
1267}
1268
Eric Andersenada18ff2001-05-21 16:18:22 +00001269/* never returns */
Eric Andersen94ac2442001-05-22 19:05:18 +00001270/* XXX no exit() here. If you don't exec, use _exit instead.
1271 * The at_exit handlers apparently confuse the calling process,
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001272 * in particular stdin handling. Not sure why? -- because of vfork! (vda) */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001273static void pseudo_exec_argv(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001274{
Eric Andersen78a7c992001-05-15 16:30:25 +00001275 int i, rcode;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001276 char *p;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001277 const struct built_in_command *x;
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001278
Denis Vlasenko1359da62007-04-21 23:27:30 +00001279 for (i = 0; is_assignment(argv[i]); i++) {
1280 debug_printf("pid %d environment modification: %s\n",
1281 getpid(), argv[i]);
1282 // FIXME: vfork case??
1283 p = insert_var_value(argv[i]);
1284 putenv(strdup(p));
1285 if (p != argv[i])
1286 free(p);
1287 }
1288 argv += i;
1289 /* If a variable is assigned in a forest, and nobody listens,
1290 * was it ever really set?
1291 */
1292 if (argv[0] == NULL) {
1293 _exit(EXIT_SUCCESS);
1294 }
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001295
Denis Vlasenko1359da62007-04-21 23:27:30 +00001296 /*
1297 * Check if the command matches any of the builtins.
1298 * Depending on context, this might be redundant. But it's
1299 * easier to waste a few CPU cycles than it is to figure out
1300 * if this is one of those cases.
1301 */
1302 for (x = bltins; x->cmd; x++) {
1303 if (strcmp(argv[0], x->cmd) == 0) {
1304 debug_printf("builtin exec %s\n", argv[0]);
1305 rcode = x->function(argv);
1306 fflush(stdout);
1307 _exit(rcode);
Eric Andersen94ac2442001-05-22 19:05:18 +00001308 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001309 }
Eric Andersen78a7c992001-05-15 16:30:25 +00001310
Denis Vlasenko1359da62007-04-21 23:27:30 +00001311 /* Check if the command matches any busybox internal commands
1312 * ("applets") here.
1313 * FIXME: This feature is not 100% safe, since
1314 * BusyBox is not fully reentrant, so we have no guarantee the things
1315 * from the .bss are still zeroed, or that things from .data are still
1316 * at their defaults. We could exec ourself from /proc/self/exe, but I
1317 * really dislike relying on /proc for things. We could exec ourself
1318 * from global_argv[0], but if we are in a chroot, we may not be able
1319 * to find ourself... */
Denis Vlasenko80d14be2007-04-10 23:03:30 +00001320#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko1359da62007-04-21 23:27:30 +00001321 debug_printf("running applet %s\n", argv[0]);
1322 run_applet_and_exit(argv[0], argv);
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001323// is it ok that run_applet_and_exit() does exit(), not _exit()?
1324// NB: IIRC on NOMMU we are after _vfork_, not fork!
Eric Andersenaac75e52001-04-30 18:18:45 +00001325#endif
Denis Vlasenko1359da62007-04-21 23:27:30 +00001326 debug_printf("exec of %s\n", argv[0]);
1327 execvp(argv[0], argv);
1328 bb_perror_msg("cannot exec '%s'", argv[0]);
1329 _exit(1);
1330}
1331
1332static void pseudo_exec(struct child_prog *child)
1333{
Denis Vlasenkof2fffd02007-05-02 23:39:04 +00001334// FIXME: buggy wrt NOMMU! Must not modify any global data
1335// until it does exec/_exit, but currently it does.
Denis Vlasenko1359da62007-04-21 23:27:30 +00001336 int rcode;
1337
1338 if (child->argv) {
1339 pseudo_exec_argv(child->argv);
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001340 }
1341
1342 if (child->group) {
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001343 // FIXME: do not modify globals! Think vfork!
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001344#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001345 interactive_fd = 0; /* crucial!!!! */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001346#endif
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001347 debug_printf_exec("pseudo_exec: run_list_real\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001348 rcode = run_list_real(child->group);
Eric Andersenbf7df042001-05-23 22:18:35 +00001349 /* OK to leak memory by not calling free_pipe_list,
Eric Andersen25f27032001-04-26 23:22:31 +00001350 * since this process is about to exit */
Eric Andersen94ac2442001-05-22 19:05:18 +00001351 _exit(rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00001352 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001353
1354 /* Can happen. See what bash does with ">foo" by itself. */
1355 debug_printf("trying to pseudo_exec null command\n");
1356 _exit(EXIT_SUCCESS);
Eric Andersen25f27032001-04-26 23:22:31 +00001357}
1358
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001359#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001360static const char *get_cmdtext(struct pipe *pi)
1361{
1362 char **argv;
1363 char *p;
1364 int len;
1365
1366 /* This is subtle. ->cmdtext is created only on first backgrounding.
1367 * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...)
1368 * On subsequent bg argv can be trashed, but we won't use it */
1369 if (pi->cmdtext)
1370 return pi->cmdtext;
1371 argv = pi->progs[0].argv;
1372 if (!argv || !argv[0])
1373 return (pi->cmdtext = xzalloc(1));
1374
1375 len = 0;
1376 do len += strlen(*argv) + 1; while (*++argv);
1377 pi->cmdtext = p = xmalloc(len);
1378 argv = pi->progs[0].argv;
1379 do {
1380 len = strlen(*argv);
1381 memcpy(p, *argv, len);
1382 p += len;
1383 *p++ = ' ';
1384 } while (*++argv);
1385 p[-1] = '\0';
1386 return pi->cmdtext;
1387}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001388#endif
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001389
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001390#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00001391static void insert_bg_job(struct pipe *pi)
1392{
1393 struct pipe *thejob;
1394
1395 /* Linear search for the ID of the job to use */
1396 pi->jobid = 1;
Eric Andersenc798b072001-06-22 06:23:03 +00001397 for (thejob = job_list; thejob; thejob = thejob->next)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001398 if (thejob->jobid >= pi->jobid)
1399 pi->jobid = thejob->jobid + 1;
1400
1401 /* add thejob to the list of running jobs */
Eric Andersenc798b072001-06-22 06:23:03 +00001402 if (!job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001403 thejob = job_list = xmalloc(sizeof(*thejob));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001404 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001405 for (thejob = job_list; thejob->next; thejob = thejob->next)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001406 continue;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001407 thejob->next = xmalloc(sizeof(*thejob));
1408 thejob = thejob->next;
1409 }
1410
1411 /* physically copy the struct job */
Eric Andersen0fcd4472001-05-02 20:12:03 +00001412 memcpy(thejob, pi, sizeof(struct pipe));
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001413 thejob->progs = xmalloc(sizeof(pi->progs[0]) * pi->num_progs);
1414 memcpy(thejob->progs, pi->progs, sizeof(pi->progs[0]) * pi->num_progs);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001415 thejob->next = NULL;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001416 /*seems to be wrong:*/
1417 /*thejob->running_progs = thejob->num_progs;*/
1418 /*thejob->stopped_progs = 0;*/
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001419 thejob->cmdtext = xstrdup(get_cmdtext(pi));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001420
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001421 /* we don't wait for background thejobs to return -- append it
Eric Andersenbafd94f2001-05-02 16:11:59 +00001422 to the list of backgrounded thejobs and leave it alone */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001423 printf("[%d] %d %s\n", thejob->jobid, thejob->progs[0].pid, thejob->cmdtext);
Eric Andersen1a6d39b2001-05-08 05:11:54 +00001424 last_bg_pid = thejob->progs[0].pid;
Eric Andersenc798b072001-06-22 06:23:03 +00001425 last_jobid = thejob->jobid;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001426}
1427
Eric Andersenbafd94f2001-05-02 16:11:59 +00001428static void remove_bg_job(struct pipe *pi)
1429{
1430 struct pipe *prev_pipe;
1431
Eric Andersenc798b072001-06-22 06:23:03 +00001432 if (pi == job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001433 job_list = pi->next;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001434 } else {
Eric Andersenc798b072001-06-22 06:23:03 +00001435 prev_pipe = job_list;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001436 while (prev_pipe->next != pi)
1437 prev_pipe = prev_pipe->next;
1438 prev_pipe->next = pi->next;
1439 }
Eric Andersen028b65b2001-06-28 01:10:11 +00001440 if (job_list)
1441 last_jobid = job_list->jobid;
1442 else
1443 last_jobid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001444}
Eric Andersen028b65b2001-06-28 01:10:11 +00001445
Denis Vlasenko1359da62007-04-21 23:27:30 +00001446/* remove a backgrounded job */
1447static void delete_finished_bg_job(struct pipe *pi)
1448{
1449 remove_bg_job(pi);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001450 pi->stopped_progs = 0;
Eric Andersenbf7df042001-05-23 22:18:35 +00001451 free_pipe(pi, 0);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001452 free(pi);
1453}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001454#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001455
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001456/* Checks to see if any processes have exited -- if they
Eric Andersenbafd94f2001-05-02 16:11:59 +00001457 have, figure out why and see if a job has completed */
Eric Andersenc798b072001-06-22 06:23:03 +00001458static int checkjobs(struct pipe* fg_pipe)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001459{
Eric Andersenc798b072001-06-22 06:23:03 +00001460 int attributes;
1461 int status;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001462#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00001463 int prognum = 0;
1464 struct pipe *pi;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001465#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001466 pid_t childpid;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001467 int rcode = 0;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001468
Eric Andersenc798b072001-06-22 06:23:03 +00001469 attributes = WUNTRACED;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001470 if (fg_pipe == NULL) {
Eric Andersenc798b072001-06-22 06:23:03 +00001471 attributes |= WNOHANG;
1472 }
1473
Denis Vlasenko1359da62007-04-21 23:27:30 +00001474/* Do we do this right?
1475 * bash-3.00# sleep 20 | false
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001476 * <ctrl-Z pressed>
Denis Vlasenko1359da62007-04-21 23:27:30 +00001477 * [3]+ Stopped sleep 20 | false
1478 * bash-3.00# echo $?
1479 * 1 <========== bg pipe is not fully done, but exitcode is already known!
1480 */
1481
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001482//FIXME: non-interactive bash does not continue even if all processes in fg pipe
1483//are stopped. Testcase: "cat | cat" in a script (not on command line)
1484// + killall -STOP cat
1485
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001486 wait_more:
Eric Andersenc798b072001-06-22 06:23:03 +00001487 while ((childpid = waitpid(-1, &status, attributes)) > 0) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001488 const int dead = WIFEXITED(status) || WIFSIGNALED(status);
1489
1490#ifdef DEBUG_SHELL_JOBS
1491 if (WIFSTOPPED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001492 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001493 childpid, WSTOPSIG(status), WEXITSTATUS(status));
1494 if (WIFSIGNALED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001495 debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001496 childpid, WTERMSIG(status), WEXITSTATUS(status));
1497 if (WIFEXITED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001498 debug_printf_jobs("pid %d exited, exitcode %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001499 childpid, WEXITSTATUS(status));
1500#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001501 /* Were we asked to wait for fg pipe? */
Eric Andersenc798b072001-06-22 06:23:03 +00001502 if (fg_pipe) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001503 int i;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001504 for (i = 0; i < fg_pipe->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001505 debug_printf_jobs("check pid %d\n", fg_pipe->progs[i].pid);
Eric Andersenc798b072001-06-22 06:23:03 +00001506 if (fg_pipe->progs[i].pid == childpid) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001507 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001508 if (dead) {
1509 fg_pipe->progs[i].pid = 0;
1510 fg_pipe->running_progs--;
1511 if (i == fg_pipe->num_progs-1)
1512 /* last process gives overall exitstatus */
1513 rcode = WEXITSTATUS(status);
1514 } else {
1515 fg_pipe->progs[i].is_stopped = 1;
1516 fg_pipe->stopped_progs++;
1517 }
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001518 debug_printf_jobs("fg_pipe: running_progs %d stopped_progs %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001519 fg_pipe->running_progs, fg_pipe->stopped_progs);
1520 if (fg_pipe->running_progs - fg_pipe->stopped_progs <= 0) {
1521 /* All processes in fg pipe have exited/stopped */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001522#if ENABLE_HUSH_JOB
Denis Vlasenko1359da62007-04-21 23:27:30 +00001523 if (fg_pipe->running_progs)
1524 insert_bg_job(fg_pipe);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001525#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001526 return rcode;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001527 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001528 /* There are still running processes in the fg pipe */
1529 goto wait_more;
Eric Andersenc798b072001-06-22 06:23:03 +00001530 }
1531 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001532 /* fall through to searching process in bg pipes */
Eric Andersenc798b072001-06-22 06:23:03 +00001533 }
1534
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001535#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001536 /* We asked to wait for bg or orphaned children */
1537 /* No need to remember exitcode in this case */
Eric Andersenc798b072001-06-22 06:23:03 +00001538 for (pi = job_list; pi; pi = pi->next) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001539 prognum = 0;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001540 while (prognum < pi->num_progs) {
1541 if (pi->progs[prognum].pid == childpid)
1542 goto found_pi_and_prognum;
Eric Andersen52a97ca2001-06-22 06:49:26 +00001543 prognum++;
1544 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001545 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001546#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001547
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001548 /* Happens when shell is used as init process (init=/bin/sh) */
1549 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001550 goto wait_more;
Eric Andersenaeb44c42001-05-22 20:29:00 +00001551
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001552#if ENABLE_HUSH_JOB
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001553 found_pi_and_prognum:
Denis Vlasenko1359da62007-04-21 23:27:30 +00001554 if (dead) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001555 /* child exited */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001556 pi->progs[prognum].pid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001557 pi->running_progs--;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001558 if (!pi->running_progs) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001559 printf(JOB_STATUS_FORMAT, pi->jobid,
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001560 "Done", pi->cmdtext);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001561 delete_finished_bg_job(pi);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001562 }
1563 } else {
1564 /* child stopped */
1565 pi->stopped_progs++;
1566 pi->progs[prognum].is_stopped = 1;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001567 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001568#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001569 }
1570
Denis Vlasenko1359da62007-04-21 23:27:30 +00001571 /* wait found no children or failed */
1572
1573 if (childpid && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001574 bb_perror_msg("waitpid");
Matt Kraai80abc452001-05-02 21:48:17 +00001575
Eric Andersenbafd94f2001-05-02 16:11:59 +00001576 /* move the shell to the foreground */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001577 //if (interactive_fd && tcsetpgrp(interactive_fd, getpgid(0)))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001578 // bb_perror_msg("tcsetpgrp-2");
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001579 return rcode;
Eric Andersenada18ff2001-05-21 16:18:22 +00001580}
1581
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001582#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +00001583static int checkjobs_and_fg_shell(struct pipe* fg_pipe)
1584{
1585 pid_t p;
1586 int rcode = checkjobs(fg_pipe);
1587 /* Job finished, move the shell to the foreground */
1588 p = getpgid(0);
1589 debug_printf("fg'ing ourself: getpgid(0)=%d\n", (int)p);
1590 if (tcsetpgrp(interactive_fd, p) && errno != ENOTTY)
1591 bb_perror_msg("tcsetpgrp-4a");
1592 return rcode;
1593}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001594#endif
Denis Vlasenko52881e92007-04-21 13:42:52 +00001595
Eric Andersen25f27032001-04-26 23:22:31 +00001596/* run_pipe_real() starts all the jobs, but doesn't wait for anything
Eric Andersenc798b072001-06-22 06:23:03 +00001597 * to finish. See checkjobs().
Eric Andersen25f27032001-04-26 23:22:31 +00001598 *
1599 * return code is normally -1, when the caller has to wait for children
1600 * to finish to determine the exit status of the pipe. If the pipe
1601 * is a simple builtin command, however, the action is done by the
1602 * time run_pipe_real returns, and the exit code is provided as the
1603 * return value.
1604 *
1605 * The input of the pipe is always stdin, the output is always
1606 * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
1607 * because it tries to avoid running the command substitution in
1608 * subshell, when that is in fact necessary. The subshell process
1609 * now has its stdout directed to the input of the appropriate pipe,
1610 * so this routine is noticeably simpler.
1611 */
1612static int run_pipe_real(struct pipe *pi)
1613{
1614 int i;
1615 int nextin, nextout;
1616 int pipefds[2]; /* pipefds[0] is for reading */
Rob Landley53702e52006-07-19 21:43:53 +00001617 struct child_prog *child;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001618 const struct built_in_command *x;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001619 char *p;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001620 /* it is not always needed, but we aim to smaller code */
1621 int squirrel[] = { -1, -1, -1 };
1622 int rcode;
Denis Vlasenko52881e92007-04-21 13:42:52 +00001623 const int single_fg = (pi->num_progs == 1 && pi->followup != PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00001624
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001625 debug_printf_exec("run_pipe_real start:\n");
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001626
Eric Andersen25f27032001-04-26 23:22:31 +00001627 nextin = 0;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001628#if ENABLE_HUSH_JOB
Eric Andersenada18ff2001-05-21 16:18:22 +00001629 pi->pgrp = -1;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001630#endif
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001631 pi->running_progs = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001632 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001633
1634 /* Check if this is a simple builtin (not part of a pipe).
1635 * Builtins within pipes have to fork anyway, and are handled in
1636 * pseudo_exec. "echo foo | read bar" doesn't work on bash, either.
1637 */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001638 child = &(pi->progs[0]);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001639 if (single_fg && child->group && child->subshell == 0) {
Eric Andersen04407e52001-06-07 16:42:05 +00001640 debug_printf("non-subshell grouping\n");
1641 setup_redirects(child, squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001642 debug_printf_exec(": run_list_real\n");
Eric Andersen04407e52001-06-07 16:42:05 +00001643 rcode = run_list_real(child->group);
1644 restore_redirects(squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001645 debug_printf_exec("run_pipe_real return %d\n", rcode);
Eric Andersen04407e52001-06-07 16:42:05 +00001646 return rcode;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001647 }
1648
Denis Vlasenko1359da62007-04-21 23:27:30 +00001649 if (single_fg && child->argv != NULL) {
1650 char **argv = child->argv;
1651
1652 for (i = 0; is_assignment(argv[i]); i++)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001653 continue;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001654 if (i != 0 && argv[i] == NULL) {
Eric Andersen78a7c992001-05-15 16:30:25 +00001655 /* assignments, but no command: set the local environment */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001656 for (i = 0; argv[i] != NULL; i++) {
Eric Andersen99785762001-05-22 21:37:48 +00001657 /* Ok, this case is tricky. We have to decide if this is a
1658 * local variable, or an already exported variable. If it is
1659 * already exported, we have to export the new value. If it is
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001660 * not exported, we need only set this as a local variable.
Eric Andersen99785762001-05-22 21:37:48 +00001661 * This junk is all to decide whether or not to export this
1662 * variable. */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001663 int export_me = 0;
Eric Andersen99785762001-05-22 21:37:48 +00001664 char *name, *value;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001665 name = xstrdup(argv[i]);
Eric Andersen04407e52001-06-07 16:42:05 +00001666 debug_printf("Local environment set: %s\n", name);
Eric Andersen99785762001-05-22 21:37:48 +00001667 value = strchr(name, '=');
1668 if (value)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001669 *value = 0;
1670 if (get_local_var(name)) {
1671 export_me = 1;
Eric Andersen99785762001-05-22 21:37:48 +00001672 }
1673 free(name);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001674 p = insert_var_value(argv[i]);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001675 set_local_var(p, export_me);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001676 if (p != argv[i])
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001677 free(p);
Eric Andersen78a7c992001-05-15 16:30:25 +00001678 }
1679 return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */
1680 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001681 for (i = 0; is_assignment(argv[i]); i++) {
1682 p = insert_var_value(argv[i]);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001683 putenv(strdup(p));
Denis Vlasenko1359da62007-04-21 23:27:30 +00001684 if (p != argv[i]) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001685 child->sp--;
1686 free(p);
1687 }
1688 }
1689 if (child->sp) {
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001690 char *str;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001691
Denis Vlasenko1359da62007-04-21 23:27:30 +00001692 str = make_string(argv + i);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001693 debug_printf_exec(": parse_string_outer '%s'\n", str);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001694 parse_string_outer(str, FLAG_EXIT_FROM_LOOP | FLAG_REPARSING);
1695 free(str);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001696 debug_printf_exec("run_pipe_real return %d\n", last_return_code);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001697 return last_return_code;
1698 }
Eric Andersen25f27032001-04-26 23:22:31 +00001699 for (x = bltins; x->cmd; x++) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001700 if (strcmp(argv[i], x->cmd) == 0) {
1701 if (x->function == builtin_exec && argv[i+1] == NULL) {
Eric Andersen83a2ae22001-05-07 17:59:25 +00001702 debug_printf("magic exec\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001703 setup_redirects(child, NULL);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001704 return EXIT_SUCCESS;
1705 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001706 debug_printf("builtin inline %s\n", argv[0]);
Eric Andersen25f27032001-04-26 23:22:31 +00001707 /* XXX setup_redirects acts on file descriptors, not FILEs.
1708 * This is perfect for work that comes after exec().
1709 * Is it really safe for inline use? Experimentally,
1710 * things seem to work with glibc. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001711// TODO: fflush(NULL)?
Eric Andersen25f27032001-04-26 23:22:31 +00001712 setup_redirects(child, squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001713 debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv[i+1]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001714 rcode = x->function(argv + i);
Eric Andersen25f27032001-04-26 23:22:31 +00001715 restore_redirects(squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001716 debug_printf_exec("run_pipe_real return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00001717 return rcode;
1718 }
1719 }
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001720#if ENABLE_FEATURE_SH_STANDALONE
1721 {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001722 const struct bb_applet *a = find_applet_by_name(argv[i]);
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001723 if (a && a->nofork) {
1724 setup_redirects(child, squirrel);
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001725 debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", argv[i], argv[i+1]);
1726 save_nofork_data(&nofork_save);
1727 rcode = run_nofork_applet_prime(&nofork_save, a, argv);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001728 restore_redirects(squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001729 debug_printf_exec("run_pipe_real return %d\n", rcode);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001730 return rcode;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001731 }
1732 }
1733#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001734 }
1735
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001736 /* Going to fork a child per each pipe member */
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001737 pi->running_progs = 0;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001738
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001739 /* Disable job control signals for shell (parent) and
1740 * for initial child code after fork */
1741 set_jobctrl_sighandler(SIG_IGN);
1742
Eric Andersen25f27032001-04-26 23:22:31 +00001743 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001744 child = &(pi->progs[i]);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001745 debug_printf_exec(": pipe member '%s' '%s'...\n", child->argv[0], child->argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001746
1747 /* pipes are inserted between pairs of commands */
1748 if ((i + 1) < pi->num_progs) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001749 if (pipe(pipefds) < 0)
1750 bb_perror_msg_and_die("pipe");
Eric Andersen25f27032001-04-26 23:22:31 +00001751 nextout = pipefds[1];
1752 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001753 nextout = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00001754 pipefds[0] = -1;
1755 }
1756
1757 /* XXX test for failed fork()? */
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001758#if BB_MMU
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001759 child->pid = fork();
Eric Andersen72f9a422001-10-28 05:12:20 +00001760#else
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001761 child->pid = vfork();
Eric Andersen72f9a422001-10-28 05:12:20 +00001762#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001763 if (!child->pid) { /* child */
1764 /* Every child adds itself to new process group
1765 * with pgid == pid of first child in pipe */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001766#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001767 if (interactive_fd) {
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001768 /* Don't do pgrp restore anymore on fatal signals */
1769 set_fatal_sighandler(SIG_DFL);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001770 if (pi->pgrp < 0) /* true for 1st process only */
1771 pi->pgrp = getpid();
1772 if (setpgid(0, pi->pgrp) == 0 && pi->followup != PIPE_BG) {
1773 /* We do it in *every* child, not just first,
1774 * to avoid races */
1775 tcsetpgrp(interactive_fd, pi->pgrp);
1776 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001777 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001778#endif
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00001779 // in non-interactive case fatal sigs are already SIG_DFL
Eric Andersen25f27032001-04-26 23:22:31 +00001780 close_all();
Eric Andersen25f27032001-04-26 23:22:31 +00001781 if (nextin != 0) {
1782 dup2(nextin, 0);
1783 close(nextin);
1784 }
1785 if (nextout != 1) {
1786 dup2(nextout, 1);
1787 close(nextout);
1788 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00001789 if (pipefds[0] != -1) {
Eric Andersen25f27032001-04-26 23:22:31 +00001790 close(pipefds[0]); /* opposite end of our output pipe */
1791 }
Eric Andersen25f27032001-04-26 23:22:31 +00001792 /* Like bash, explicit redirects override pipes,
1793 * and the pipe fd is available for dup'ing. */
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001794 setup_redirects(child, NULL);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001795
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001796 /* Restore default handlers just prior to exec */
1797 set_jobctrl_sighandler(SIG_DFL);
1798 set_misc_sighandler(SIG_DFL);
1799 signal(SIGCHLD, SIG_DFL);
Eric Andersen25f27032001-04-26 23:22:31 +00001800 pseudo_exec(child);
1801 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001802
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001803 pi->running_progs++;
1804
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001805#if ENABLE_HUSH_JOB
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00001806 /* Second and next children need to know pid of first one */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001807 if (pi->pgrp < 0)
Eric Andersen0fcd4472001-05-02 20:12:03 +00001808 pi->pgrp = child->pid;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001809#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001810
Eric Andersen0fcd4472001-05-02 20:12:03 +00001811 /* Don't check for errors. The child may be dead already,
1812 * in which case setpgid returns error code EACCES. */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001813 //why we do it at all?? child does it itself
1814 //if (interactive_fd)
1815 // setpgid(child->pid, pi->pgrp);
Eric Andersen0fcd4472001-05-02 20:12:03 +00001816
Eric Andersen25f27032001-04-26 23:22:31 +00001817 if (nextin != 0)
1818 close(nextin);
1819 if (nextout != 1)
1820 close(nextout);
1821
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001822 /* If there isn't another process, nextin is garbage
Eric Andersen25f27032001-04-26 23:22:31 +00001823 but it doesn't matter */
1824 nextin = pipefds[0];
1825 }
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001826 debug_printf_exec("run_pipe_real return -1\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001827 return -1;
1828}
1829
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001830#ifndef debug_print_tree
1831static void debug_print_tree(struct pipe *pi, int lvl)
1832{
1833 static const char *PIPE[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001834 [PIPE_SEQ] = "SEQ",
1835 [PIPE_AND] = "AND",
1836 [PIPE_OR ] = "OR",
1837 [PIPE_BG ] = "BG",
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001838 };
1839 static const char *RES[] = {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001840 [RES_NONE ] = "NONE" ,
1841 [RES_IF ] = "IF" ,
1842 [RES_THEN ] = "THEN" ,
1843 [RES_ELIF ] = "ELIF" ,
1844 [RES_ELSE ] = "ELSE" ,
1845 [RES_FI ] = "FI" ,
1846 [RES_FOR ] = "FOR" ,
1847 [RES_WHILE] = "WHILE",
1848 [RES_UNTIL] = "UNTIL",
1849 [RES_DO ] = "DO" ,
1850 [RES_DONE ] = "DONE" ,
1851 [RES_XXXX ] = "XXXX" ,
1852 [RES_IN ] = "IN" ,
1853 [RES_SNTX ] = "SNTX" ,
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001854 };
1855
1856 int pin, prn;
1857 char **argv;
1858 pin = 0;
1859 while (pi) {
1860 fprintf(stderr, "%*spipe %d r_mode=%s followup=%d %s\n", lvl*2, "",
1861 pin, RES[pi->r_mode], pi->followup, PIPE[pi->followup]);
1862 prn = 0;
1863 while (prn < pi->num_progs) {
1864 fprintf(stderr, "%*s prog %d", lvl*2, "", prn);
1865 if (pi->progs[prn].group) {
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001866 fprintf(stderr, " group %s: (argv=%p)\n",
1867 (pi->subshell ? "()" : "{}"),
1868 pi->progs[prn].argv);
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001869 debug_print_tree(pi->progs[prn].group, lvl+1);
1870 prn++;
1871 continue;
1872 }
1873 argv = pi->progs[prn].argv;
1874 if (argv) while (*argv) {
1875 fprintf(stderr, " '%s'", *argv);
1876 argv++;
1877 }
1878 fprintf(stderr, "\n");
1879 prn++;
1880 }
1881 pi = pi->next;
1882 pin++;
1883 }
1884}
1885#endif
1886
Denis Vlasenkof2fffd02007-05-02 23:39:04 +00001887// NB: called by pseudo_exec, and therefore must not modify any
1888// global data until exec/_exit (we can be a child after vfork!)
Eric Andersen25f27032001-04-26 23:22:31 +00001889static int run_list_real(struct pipe *pi)
1890{
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001891#if ENABLE_HUSH_JOB
1892 static int level;
1893#else
1894 enum { level = 0 };
1895#endif
1896
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001897 char *save_name = NULL;
1898 char **list = NULL;
1899 char **save_list = NULL;
1900 struct pipe *rpipe;
1901 int flag_rep = 0;
1902 int save_num_progs;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001903 int flag_skip = 1;
1904 int rcode = 0; /* probaly for gcc only */
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001905 int flag_restore = 0;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001906 int if_code = 0, next_if_code = 0; /* need double-buffer to handle elif */
1907 reserved_style rmode, skip_more_in_this_rmode = RES_XXXX;
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001908
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001909 debug_printf_exec("run_list_real start lvl %d\n", level + 1);
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001910
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001911 /* check syntax for "for" */
1912 for (rpipe = pi; rpipe; rpipe = rpipe->next) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001913 if ((rpipe->r_mode == RES_IN || rpipe->r_mode == RES_FOR)
1914 && (rpipe->next == NULL)
1915 ) {
1916 syntax();
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001917 debug_printf_exec("run_list_real lvl %d return 1\n", level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001918 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001919 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001920 if ((rpipe->r_mode == RES_IN && rpipe->next->r_mode == RES_IN && rpipe->next->progs->argv != NULL)
1921 || (rpipe->r_mode == RES_FOR && rpipe->next->r_mode != RES_IN)
1922 ) {
1923 syntax();
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001924 debug_printf_exec("run_list_real lvl %d return 1\n", level);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001925 return 1;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001926 }
1927 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001928
1929#if ENABLE_HUSH_JOB
1930 /* Example of nested list: "while true; do { sleep 1 | exit 2; } done".
1931 * We are saving state before entering outermost list ("while...done")
1932 * so that ctrl-Z will correctly background _entire_ outermost list,
1933 * not just a part of it (like "sleep 1 | exit 2") */
1934 if (++level == 1 && interactive_fd) {
1935 if (sigsetjmp(toplevel_jb, 1)) {
1936 /* ctrl-Z forked and we are parent; or ctrl-C.
1937 * Sighandler has longjmped us here */
1938 signal(SIGINT, SIG_IGN);
1939 signal(SIGTSTP, SIG_IGN);
1940 /* Restore level (we can be coming from deep inside
1941 * nested levels) */
1942 level = 1;
1943#if ENABLE_FEATURE_SH_STANDALONE
1944 if (nofork_save.saved) { /* if save area is valid */
1945 debug_printf_jobs("exiting nofork early\n");
1946 restore_nofork_data(&nofork_save);
1947 }
1948#endif
1949 if (ctrl_z_flag) {
1950 /* ctrl-Z has forked and stored pid of the child in pi->pid.
1951 * Remember this child as background job */
1952 insert_bg_job(pi);
1953 } else {
1954 /* ctrl-C. We just stop doing whatever we was doing */
1955 putchar('\n');
1956 }
1957 rcode = 0;
1958 goto ret;
1959 }
1960 /* ctrl-Z handler will store pid etc in pi */
1961 toplevel_list = pi;
1962 ctrl_z_flag = 0;
1963#if ENABLE_FEATURE_SH_STANDALONE
1964 nofork_save.saved = 0; /* in case we will run a nofork later */
1965#endif
1966 signal_SA_RESTART(SIGTSTP, handler_ctrl_z);
1967 signal(SIGINT, handler_ctrl_c);
1968 }
1969#endif
1970
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001971 for (; pi; pi = (flag_restore != 0) ? rpipe : pi->next) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001972 if (pi->r_mode == RES_WHILE || pi->r_mode == RES_UNTIL
1973 || pi->r_mode == RES_FOR
1974 ) {
1975 flag_restore = 0;
1976 if (!rpipe) {
1977 flag_rep = 0;
1978 rpipe = pi;
1979 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001980 }
Eric Andersen25f27032001-04-26 23:22:31 +00001981 rmode = pi->r_mode;
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00001982 debug_printf_exec(": rmode=%d if_code=%d next_if_code=%d skip_more=%d\n",
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001983 rmode, if_code, next_if_code, skip_more_in_this_rmode);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001984 if (rmode == skip_more_in_this_rmode && flag_skip) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001985 if (pi->followup == PIPE_SEQ)
1986 flag_skip = 0;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001987 continue;
1988 }
1989 flag_skip = 1;
Eric Andersen4ed5e372001-05-01 01:49:50 +00001990 skip_more_in_this_rmode = RES_XXXX;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001991 if (rmode == RES_THEN || rmode == RES_ELSE)
1992 if_code = next_if_code;
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00001993 if (rmode == RES_THEN && if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001994 continue;
1995 if (rmode == RES_ELSE && !if_code)
1996 continue;
1997 if (rmode == RES_ELIF && !if_code)
1998 break;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001999 if (rmode == RES_FOR && pi->num_progs) {
2000 if (!list) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002001 /* if no variable values after "in" we skip "for" */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002002 if (!pi->next->progs->argv)
2003 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002004 /* create list of variable values */
2005 list = make_list_in(pi->next->progs->argv,
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002006 pi->progs->argv[0]);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002007 save_list = list;
2008 save_name = pi->progs->argv[0];
2009 pi->progs->argv[0] = NULL;
2010 flag_rep = 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002011 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002012 if (!*list) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002013 free(pi->progs->argv[0]);
2014 free(save_list);
2015 list = NULL;
2016 flag_rep = 0;
2017 pi->progs->argv[0] = save_name;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002018 pi->progs->glob_result.gl_pathv[0] = pi->progs->argv[0];
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002019 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002020 }
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002021 /* insert new value from list for variable */
2022 if (pi->progs->argv[0])
2023 free(pi->progs->argv[0]);
2024 pi->progs->argv[0] = *list++;
2025 pi->progs->glob_result.gl_pathv[0] = pi->progs->argv[0];
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002026 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002027 if (rmode == RES_IN)
2028 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002029 if (rmode == RES_DO) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002030 if (!flag_rep)
2031 continue;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002032 }
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002033 if (rmode == RES_DONE) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002034 if (flag_rep) {
2035 flag_restore = 1;
2036 } else {
2037 rpipe = NULL;
2038 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002039 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002040 if (pi->num_progs == 0)
2041 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002042 save_num_progs = pi->num_progs; /* save number of programs */
Denis Vlasenkod01ff132007-05-02 21:40:23 +00002043 debug_printf_exec(": run_pipe_real with %d members\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00002044 rcode = run_pipe_real(pi);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002045 if (rcode != -1) {
Eric Andersen25f27032001-04-26 23:22:31 +00002046 /* We only ran a builtin: rcode was set by the return value
2047 * of run_pipe_real(), and we don't need to wait for anything. */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002048 } else if (pi->followup == PIPE_BG) {
Eric Andersen25f27032001-04-26 23:22:31 +00002049 /* XXX check bash's behavior with nontrivial pipes */
2050 /* XXX compute jobid */
2051 /* XXX what does bash do with attempts to background builtins? */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002052#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00002053 insert_bg_job(pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002054#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002055 rcode = EXIT_SUCCESS;
2056 } else {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002057#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002058 if (interactive_fd) {
Denis Vlasenko52881e92007-04-21 13:42:52 +00002059 rcode = checkjobs_and_fg_shell(pi);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002060 } else
2061#endif
2062 {
Eric Andersenc798b072001-06-22 06:23:03 +00002063 rcode = checkjobs(pi);
Eric Andersen25f27032001-04-26 23:22:31 +00002064 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002065 debug_printf_exec(": checkjobs returned %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002066 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002067 debug_printf_exec(": setting last_return_code=%d\n", rcode);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002068 last_return_code = rcode;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002069 pi->num_progs = save_num_progs; /* restore number of programs */
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002070 if (rmode == RES_IF || rmode == RES_ELIF)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002071 next_if_code = rcode; /* can be overwritten a number of times */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002072 if (rmode == RES_WHILE)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002073 flag_rep = !last_return_code;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002074 if (rmode == RES_UNTIL)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002075 flag_rep = last_return_code;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002076 if ((rcode == EXIT_SUCCESS && pi->followup == PIPE_OR)
2077 || (rcode != EXIT_SUCCESS && pi->followup == PIPE_AND)
2078 ) {
2079 skip_more_in_this_rmode = rmode;
2080 }
Eric Andersen028b65b2001-06-28 01:10:11 +00002081 checkjobs(NULL);
Eric Andersen25f27032001-04-26 23:22:31 +00002082 }
Denis Vlasenkoac0e5ab2007-05-04 21:37:27 +00002083
2084#if ENABLE_HUSH_JOB
2085 if (ctrl_z_flag) {
2086 /* ctrl-Z forked somewhere in the past, we are the child,
2087 * and now we completed running the list. Exit. */
2088 exit(rcode);
2089 }
2090 ret:
2091 level--;
2092#endif
2093 debug_printf_exec("run_list_real lvl %d return %d\n", level + 1, rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002094 return rcode;
2095}
2096
Eric Andersen25f27032001-04-26 23:22:31 +00002097/* return code is the exit status of the pipe */
Eric Andersenbf7df042001-05-23 22:18:35 +00002098static int free_pipe(struct pipe *pi, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002099{
2100 char **p;
2101 struct child_prog *child;
2102 struct redir_struct *r, *rnext;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002103 int a, i, ret_code = 0;
Eric Andersen52a97ca2001-06-22 06:49:26 +00002104
2105 if (pi->stopped_progs > 0)
2106 return ret_code;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002107 final_printf("%s run pipe: (pid %d)\n", indenter(indent), getpid());
2108 for (i = 0; i < pi->num_progs; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002109 child = &pi->progs[i];
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002110 final_printf("%s command %d:\n", indenter(indent), i);
Eric Andersen25f27032001-04-26 23:22:31 +00002111 if (child->argv) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002112 for (a = 0, p = child->argv; *p; a++, p++) {
2113 final_printf("%s argv[%d] = %s\n", indenter(indent), a, *p);
Eric Andersen25f27032001-04-26 23:22:31 +00002114 }
2115 globfree(&child->glob_result);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002116 child->argv = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002117 } else if (child->group) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002118 final_printf("%s begin group (subshell:%d)\n", indenter(indent), child->subshell);
2119 ret_code = free_pipe_list(child->group, indent+3);
2120 final_printf("%s end group\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002121 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002122 final_printf("%s (nil)\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002123 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002124 for (r = child->redirects; r; r = rnext) {
Rob Landley88621d72006-08-29 19:41:06 +00002125 final_printf("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->type].descrip);
Eric Andersen25f27032001-04-26 23:22:31 +00002126 if (r->dup == -1) {
Eric Andersen817e73c2001-06-06 17:56:09 +00002127 /* guard against the case >$FOO, where foo is unset or blank */
2128 if (r->word.gl_pathv) {
2129 final_printf(" %s\n", *r->word.gl_pathv);
2130 globfree(&r->word);
2131 }
Eric Andersen25f27032001-04-26 23:22:31 +00002132 } else {
2133 final_printf("&%d\n", r->dup);
2134 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002135 rnext = r->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002136 free(r);
2137 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002138 child->redirects = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002139 }
2140 free(pi->progs); /* children are an array, they get freed all at once */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002141 pi->progs = NULL;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002142#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002143 free(pi->cmdtext);
2144 pi->cmdtext = NULL;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002145#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002146 return ret_code;
2147}
2148
Eric Andersenbf7df042001-05-23 22:18:35 +00002149static int free_pipe_list(struct pipe *head, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002150{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002151 int rcode = 0; /* if list has no members */
Eric Andersen25f27032001-04-26 23:22:31 +00002152 struct pipe *pi, *next;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002153 for (pi = head; pi; pi = next) {
Rob Landley88621d72006-08-29 19:41:06 +00002154 final_printf("%s pipe reserved mode %d\n", indenter(indent), pi->r_mode);
Eric Andersenbf7df042001-05-23 22:18:35 +00002155 rcode = free_pipe(pi, indent);
Rob Landley88621d72006-08-29 19:41:06 +00002156 final_printf("%s pipe followup code %d\n", indenter(indent), pi->followup);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002157 next = pi->next;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002158 /*pi->next = NULL;*/
Eric Andersen25f27032001-04-26 23:22:31 +00002159 free(pi);
2160 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002161 return rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00002162}
2163
2164/* Select which version we will use */
2165static int run_list(struct pipe *pi)
2166{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002167 int rcode = 0;
2168 if (fake_mode == 0) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +00002169 debug_printf_exec("run_list: run_list_real with %d members\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00002170 rcode = run_list_real(pi);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002171 }
Eric Andersenbf7df042001-05-23 22:18:35 +00002172 /* free_pipe_list has the side effect of clearing memory
Eric Andersen25f27032001-04-26 23:22:31 +00002173 * In the long run that function can be merged with run_list_real,
2174 * but doing that now would hobble the debugging effort. */
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002175 free_pipe_list(pi, 0);
Eric Andersen25f27032001-04-26 23:22:31 +00002176 return rcode;
2177}
2178
2179/* The API for glob is arguably broken. This routine pushes a non-matching
2180 * string into the output structure, removing non-backslashed backslashes.
2181 * If someone can prove me wrong, by performing this function within the
2182 * original glob(3) api, feel free to rewrite this routine into oblivion.
2183 * Return code (0 vs. GLOB_NOSPACE) matches glob(3).
2184 * XXX broken if the last character is '\\', check that before calling.
2185 */
2186static int globhack(const char *src, int flags, glob_t *pglob)
2187{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002188 int cnt = 0, pathc;
Eric Andersen25f27032001-04-26 23:22:31 +00002189 const char *s;
2190 char *dest;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002191 for (cnt = 1, s = src; s && *s; s++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002192 if (*s == '\\') s++;
2193 cnt++;
2194 }
2195 dest = malloc(cnt);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002196 if (!dest)
2197 return GLOB_NOSPACE;
Eric Andersen25f27032001-04-26 23:22:31 +00002198 if (!(flags & GLOB_APPEND)) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002199 pglob->gl_pathv = NULL;
2200 pglob->gl_pathc = 0;
2201 pglob->gl_offs = 0;
2202 pglob->gl_offs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002203 }
2204 pathc = ++pglob->gl_pathc;
2205 pglob->gl_pathv = realloc(pglob->gl_pathv, (pathc+1)*sizeof(*pglob->gl_pathv));
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002206 if (pglob->gl_pathv == NULL)
2207 return GLOB_NOSPACE;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002208 pglob->gl_pathv[pathc-1] = dest;
2209 pglob->gl_pathv[pathc] = NULL;
2210 for (s = src; s && *s; s++, dest++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002211 if (*s == '\\') s++;
2212 *dest = *s;
2213 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002214 *dest = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00002215 return 0;
2216}
2217
2218/* XXX broken if the last character is '\\', check that before calling */
2219static int glob_needed(const char *s)
2220{
2221 for (; *s; s++) {
2222 if (*s == '\\') s++;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002223 if (strchr("*[?", *s)) return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002224 }
2225 return 0;
2226}
2227
Eric Andersen25f27032001-04-26 23:22:31 +00002228static int xglob(o_string *dest, int flags, glob_t *pglob)
2229{
2230 int gr;
2231
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002232 /* short-circuit for null word */
Eric Andersen25f27032001-04-26 23:22:31 +00002233 /* we can code this better when the debug_printf's are gone */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002234 if (dest->length == 0) {
2235 if (dest->nonnull) {
2236 /* bash man page calls this an "explicit" null */
2237 gr = globhack(dest->data, flags, pglob);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002238 debug_printf("globhack returned %d\n", gr);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002239 } else {
Eric Andersen25f27032001-04-26 23:22:31 +00002240 return 0;
2241 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002242 } else if (glob_needed(dest->data)) {
Eric Andersen25f27032001-04-26 23:22:31 +00002243 gr = glob(dest->data, flags, NULL, pglob);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002244 debug_printf("glob returned %d\n", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002245 if (gr == GLOB_NOMATCH) {
2246 /* quote removal, or more accurately, backslash removal */
2247 gr = globhack(dest->data, flags, pglob);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002248 debug_printf("globhack returned %d\n", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002249 }
2250 } else {
2251 gr = globhack(dest->data, flags, pglob);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002252 debug_printf("globhack returned %d\n", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002253 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002254 if (gr == GLOB_NOSPACE)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002255 bb_error_msg_and_die("out of memory during glob");
Eric Andersen25f27032001-04-26 23:22:31 +00002256 if (gr != 0) { /* GLOB_ABORTED ? */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002257 bb_error_msg("glob(3) error %d", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002258 }
2259 /* globprint(glob_target); */
2260 return gr;
2261}
2262
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002263static char **make_list_in(char **inp, char *name)
2264{
2265 int len, i;
2266 int name_len = strlen(name);
2267 int n = 0;
2268 char **list;
2269 char *p1, *p2, *p3;
2270
2271 /* create list of variable values */
2272 list = xmalloc(sizeof(*list));
2273 for (i = 0; inp[i]; i++) {
2274 p3 = insert_var_value(inp[i]);
2275 p1 = p3;
2276 while (*p1) {
2277 if ((*p1 == ' ')) {
2278 p1++;
2279 continue;
2280 }
2281 p2 = strchr(p1, ' ');
2282 if (p2) {
2283 len = p2 - p1;
2284 } else {
2285 len = strlen(p1);
2286 p2 = p1 + len;
2287 }
2288 /* we use n + 2 in realloc for list, because we add
2289 * new element and then we will add NULL element */
2290 list = xrealloc(list, sizeof(*list) * (n + 2));
2291 list[n] = xmalloc(2 + name_len + len);
2292 strcpy(list[n], name);
2293 strcat(list[n], "=");
2294 strncat(list[n], p1, len);
2295 list[n++][name_len + len + 1] = '\0';
2296 p1 = p2;
2297 }
2298 if (p3 != inp[i]) free(p3);
2299 }
2300 list[n] = NULL;
2301 return list;
2302}
2303
2304static char *insert_var_value(char *inp)
2305{
2306 int res_str_len = 0;
2307 int len;
2308 int done = 0;
2309 char *p, *res_str = NULL;
2310 const char *p1;
2311
2312 while ((p = strchr(inp, SPECIAL_VAR_SYMBOL))) {
2313 if (p != inp) {
2314 len = p - inp;
2315 res_str = xrealloc(res_str, (res_str_len + len));
2316 strncpy((res_str + res_str_len), inp, len);
2317 res_str_len += len;
2318 }
2319 inp = ++p;
2320 p = strchr(inp, SPECIAL_VAR_SYMBOL);
2321 *p = '\0';
2322 p1 = lookup_param(inp);
2323 if (p1) {
2324 len = res_str_len + strlen(p1);
2325 res_str = xrealloc(res_str, (1 + len));
2326 strcpy((res_str + res_str_len), p1);
2327 res_str_len = len;
2328 }
2329 *p = SPECIAL_VAR_SYMBOL;
2330 inp = ++p;
2331 done = 1;
2332 }
2333 if (done) {
2334 res_str = xrealloc(res_str, (1 + res_str_len + strlen(inp)));
2335 strcpy((res_str + res_str_len), inp);
2336 while ((p = strchr(res_str, '\n'))) {
2337 *p = ' ';
2338 }
2339 }
2340 return (res_str == NULL) ? inp : res_str;
2341}
2342
Eric Andersenf72f5622001-05-15 23:21:41 +00002343/* This is used to get/check local shell variables */
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00002344static const char *get_local_var(const char *s)
Eric Andersenf72f5622001-05-15 23:21:41 +00002345{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002346 struct variables *cur;
Eric Andersenf72f5622001-05-15 23:21:41 +00002347
2348 if (!s)
2349 return NULL;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002350 for (cur = top_vars; cur; cur = cur->next)
2351 if (strcmp(cur->name, s) == 0)
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002352 return cur->value;
Eric Andersenf72f5622001-05-15 23:21:41 +00002353 return NULL;
2354}
2355
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002356/* This is used to set local shell variables
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002357 flg_export == 0 if only local (not exporting) variable
2358 flg_export == 1 if "new" exporting environ
2359 flg_export > 1 if current startup environ (not call putenv()) */
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002360static int set_local_var(const char *s, int flg_export)
Eric Andersen78a7c992001-05-15 16:30:25 +00002361{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002362 char *name, *value;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002363 int result = 0;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002364 struct variables *cur;
Eric Andersen20a69a72001-05-15 17:24:44 +00002365
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002366 name = strdup(s);
Eric Andersen20a69a72001-05-15 17:24:44 +00002367
2368 /* Assume when we enter this function that we are already in
2369 * NAME=VALUE format. So the first order of business is to
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002370 * split 's' on the '=' into 'name' and 'value' */
Eric Andersen20a69a72001-05-15 17:24:44 +00002371 value = strchr(name, '=');
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002372 /*if (value == 0 && ++value == 0) ??? -vda */
2373 if (value == NULL || value[1] == '\0') {
Eric Andersen99785762001-05-22 21:37:48 +00002374 free(name);
2375 return -1;
2376 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002377 *value++ = '\0';
Eric Andersen20a69a72001-05-15 17:24:44 +00002378
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002379 for (cur = top_vars; cur; cur = cur->next) {
2380 if (strcmp(cur->name, name) == 0)
Eric Andersen99785762001-05-22 21:37:48 +00002381 break;
2382 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002383
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002384 if (cur) {
2385 if (strcmp(cur->value, value) == 0) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002386 if (flg_export > 0 && cur->flg_export == 0)
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00002387 cur->flg_export = flg_export;
Eric Andersen99785762001-05-22 21:37:48 +00002388 else
2389 result++;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002390 } else if (cur->flg_read_only) {
2391 bb_error_msg("%s: readonly variable", name);
2392 result = -1;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002393 } else {
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002394 if (flg_export > 0 || cur->flg_export > 1)
2395 cur->flg_export = 1;
2396 free((char*)cur->value);
Eric Andersen99785762001-05-22 21:37:48 +00002397
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002398 cur->value = strdup(value);
Eric Andersen99785762001-05-22 21:37:48 +00002399 }
2400 } else {
2401 cur = malloc(sizeof(struct variables));
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002402 if (!cur) {
Eric Andersen99785762001-05-22 21:37:48 +00002403 result = -1;
2404 } else {
2405 cur->name = strdup(name);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002406 if (cur->name) {
Eric Andersen99785762001-05-22 21:37:48 +00002407 free(cur);
2408 result = -1;
2409 } else {
2410 struct variables *bottom = top_vars;
2411 cur->value = strdup(value);
2412 cur->next = 0;
2413 cur->flg_export = flg_export;
2414 cur->flg_read_only = 0;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002415 while (bottom->next)
2416 bottom = bottom->next;
Eric Andersen99785762001-05-22 21:37:48 +00002417 bottom->next = cur;
Eric Andersen20a69a72001-05-15 17:24:44 +00002418 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002419 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002420 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002421
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002422 if (result == 0 && cur->flg_export == 1) {
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002423 *(value-1) = '=';
2424 result = putenv(name);
2425 } else {
Eric Andersen94ac2442001-05-22 19:05:18 +00002426 free(name);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002427 if (result > 0) /* equivalent to previous set */
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002428 result = 0;
2429 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002430 return result;
2431}
2432
Eric Andersenf72f5622001-05-15 23:21:41 +00002433static void unset_local_var(const char *name)
Eric Andersen20a69a72001-05-15 17:24:44 +00002434{
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002435 struct variables *cur, *next;
Eric Andersen20a69a72001-05-15 17:24:44 +00002436
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002437 if (!name)
2438 return;
2439 for (cur = top_vars; cur; cur = cur->next) {
2440 if (strcmp(cur->name, name) == 0) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002441 if (cur->flg_read_only) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002442 bb_error_msg("%s: readonly variable", name);
Eric Andersen94ac2442001-05-22 19:05:18 +00002443 return;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002444 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002445 if (cur->flg_export)
2446 unsetenv(cur->name);
2447 free((char*)cur->name);
2448 free((char*)cur->value);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002449 next = top_vars;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002450 while (next->next != cur)
2451 next = next->next;
2452 next->next = cur->next;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002453 free(cur);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002454 return;
Eric Andersenf72f5622001-05-15 23:21:41 +00002455 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002456 }
Eric Andersen78a7c992001-05-15 16:30:25 +00002457}
2458
2459static int is_assignment(const char *s)
2460{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002461 if (!s || !isalpha(*s))
2462 return 0;
2463 s++;
2464 while (isalnum(*s) || *s == '_')
2465 s++;
2466 return *s == '=';
Eric Andersen78a7c992001-05-15 16:30:25 +00002467}
2468
Eric Andersen25f27032001-04-26 23:22:31 +00002469/* the src parameter allows us to peek forward to a possible &n syntax
2470 * for file descriptor duplication, e.g., "2>&1".
2471 * Return code is 0 normally, 1 if a syntax error is detected in src.
2472 * Resource errors (in xmalloc) cause the process to exit */
2473static int setup_redirect(struct p_context *ctx, int fd, redir_type style,
2474 struct in_str *input)
2475{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002476 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00002477 struct redir_struct *redir = child->redirects;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002478 struct redir_struct *last_redir = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002479
2480 /* Create a new redir_struct and drop it onto the end of the linked list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002481 while (redir) {
2482 last_redir = redir;
2483 redir = redir->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002484 }
2485 redir = xmalloc(sizeof(struct redir_struct));
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002486 redir->next = NULL;
2487 redir->word.gl_pathv = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002488 if (last_redir) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002489 last_redir->next = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002490 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002491 child->redirects = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002492 }
2493
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002494 redir->type = style;
2495 redir->fd = (fd == -1) ? redir_table[style].default_fd : fd;
Eric Andersen25f27032001-04-26 23:22:31 +00002496
2497 debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
2498
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002499 /* Check for a '2>&1' type redirect */
Eric Andersen25f27032001-04-26 23:22:31 +00002500 redir->dup = redirect_dup_num(input);
2501 if (redir->dup == -2) return 1; /* syntax error */
2502 if (redir->dup != -1) {
2503 /* Erik had a check here that the file descriptor in question
Eric Andersen83a2ae22001-05-07 17:59:25 +00002504 * is legit; I postpone that to "run time"
2505 * A "-" representation of "close me" shows up as a -3 here */
Eric Andersen25f27032001-04-26 23:22:31 +00002506 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
2507 } else {
2508 /* We do _not_ try to open the file that src points to,
2509 * since we need to return and let src be expanded first.
2510 * Set ctx->pending_redirect, so we know what to do at the
2511 * end of the next parsed word.
2512 */
2513 ctx->pending_redirect = redir;
2514 }
2515 return 0;
2516}
2517
Denis Vlasenkoac678ec2007-04-16 22:32:04 +00002518static struct pipe *new_pipe(void)
2519{
Eric Andersen25f27032001-04-26 23:22:31 +00002520 struct pipe *pi;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002521 pi = xzalloc(sizeof(struct pipe));
2522 /*pi->num_progs = 0;*/
2523 /*pi->progs = NULL;*/
2524 /*pi->next = NULL;*/
2525 /*pi->followup = 0; invalid */
2526 if (RES_NONE)
2527 pi->r_mode = RES_NONE;
Eric Andersen25f27032001-04-26 23:22:31 +00002528 return pi;
2529}
2530
2531static void initialize_context(struct p_context *ctx)
2532{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002533 ctx->pipe = NULL;
2534 ctx->pending_redirect = NULL;
2535 ctx->child = NULL;
2536 ctx->list_head = new_pipe();
2537 ctx->pipe = ctx->list_head;
2538 ctx->w = RES_NONE;
2539 ctx->stack = NULL;
2540 ctx->old_flag = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002541 done_command(ctx); /* creates the memory for working child */
2542}
2543
2544/* normal return is 0
2545 * if a reserved word is found, and processed, return 1
2546 * should handle if, then, elif, else, fi, for, while, until, do, done.
2547 * case, function, and select are obnoxious, save those for later.
2548 */
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00002549static int reserved_word(o_string *dest, struct p_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00002550{
2551 struct reserved_combo {
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002552 char literal[7];
2553 unsigned char code;
2554 int flag;
Eric Andersen25f27032001-04-26 23:22:31 +00002555 };
2556 /* Mostly a list of accepted follow-up reserved words.
2557 * FLAG_END means we are done with the sequence, and are ready
2558 * to turn the compound list into a command.
2559 * FLAG_START means the word must start a new compound list.
2560 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002561 static const struct reserved_combo reserved_list[] = {
Eric Andersen25f27032001-04-26 23:22:31 +00002562 { "if", RES_IF, FLAG_THEN | FLAG_START },
2563 { "then", RES_THEN, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
2564 { "elif", RES_ELIF, FLAG_THEN },
2565 { "else", RES_ELSE, FLAG_FI },
2566 { "fi", RES_FI, FLAG_END },
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002567 { "for", RES_FOR, FLAG_IN | FLAG_START },
Eric Andersen25f27032001-04-26 23:22:31 +00002568 { "while", RES_WHILE, FLAG_DO | FLAG_START },
2569 { "until", RES_UNTIL, FLAG_DO | FLAG_START },
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002570 { "in", RES_IN, FLAG_DO },
Eric Andersen25f27032001-04-26 23:22:31 +00002571 { "do", RES_DO, FLAG_DONE },
2572 { "done", RES_DONE, FLAG_END }
2573 };
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00002574 enum { NRES = sizeof(reserved_list)/sizeof(reserved_list[0]) };
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002575 const struct reserved_combo *r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002576
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002577 for (r = reserved_list; r < reserved_list+NRES; r++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002578 if (strcmp(dest->data, r->literal) == 0) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002579 debug_printf("found reserved word %s, code %d\n", r->literal, r->code);
Eric Andersen25f27032001-04-26 23:22:31 +00002580 if (r->flag & FLAG_START) {
2581 struct p_context *new = xmalloc(sizeof(struct p_context));
2582 debug_printf("push stack\n");
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002583 if (ctx->w == RES_IN || ctx->w == RES_FOR) {
2584 syntax();
2585 free(new);
2586 ctx->w = RES_SNTX;
2587 b_reset(dest);
2588 return 1;
2589 }
Eric Andersen25f27032001-04-26 23:22:31 +00002590 *new = *ctx; /* physical copy */
2591 initialize_context(ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002592 ctx->stack = new;
2593 } else if (ctx->w == RES_NONE || !(ctx->old_flag & (1 << r->code))) {
Eric Andersenaf44a0e2001-04-27 07:26:12 +00002594 syntax();
2595 ctx->w = RES_SNTX;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002596 b_reset(dest);
Eric Andersenaf44a0e2001-04-27 07:26:12 +00002597 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002598 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002599 ctx->w = r->code;
Eric Andersen25f27032001-04-26 23:22:31 +00002600 ctx->old_flag = r->flag;
2601 if (ctx->old_flag & FLAG_END) {
2602 struct p_context *old;
2603 debug_printf("pop stack\n");
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002604 done_pipe(ctx, PIPE_SEQ);
Eric Andersen25f27032001-04-26 23:22:31 +00002605 old = ctx->stack;
2606 old->child->group = ctx->list_head;
Eric Andersen04407e52001-06-07 16:42:05 +00002607 old->child->subshell = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002608 *ctx = *old; /* physical copy */
2609 free(old);
Eric Andersen25f27032001-04-26 23:22:31 +00002610 }
Denis Vlasenkoff131b92007-04-10 15:42:06 +00002611 b_reset(dest);
Eric Andersen25f27032001-04-26 23:22:31 +00002612 return 1;
2613 }
2614 }
2615 return 0;
2616}
2617
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002618/* Normal return is 0.
Eric Andersen25f27032001-04-26 23:22:31 +00002619 * Syntax or xglob errors return 1. */
2620static int done_word(o_string *dest, struct p_context *ctx)
2621{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002622 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00002623 glob_t *glob_target;
2624 int gr, flags = 0;
2625
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002626 debug_printf_parse("done_word: '%s' %p\n", dest->data, child);
Eric Andersen25f27032001-04-26 23:22:31 +00002627 if (dest->length == 0 && !dest->nonnull) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002628 debug_printf_parse("done_word return 0: true null, ignored\n");
Eric Andersen25f27032001-04-26 23:22:31 +00002629 return 0;
2630 }
2631 if (ctx->pending_redirect) {
2632 glob_target = &ctx->pending_redirect->word;
2633 } else {
2634 if (child->group) {
2635 syntax();
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002636 debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n");
2637 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002638 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002639 if (!child->argv && (ctx->type & FLAG_PARSE_SEMICOLON)) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002640 debug_printf_parse(": checking %s for reserved-ness\n", dest->data);
2641 if (reserved_word(dest, ctx)) {
2642 debug_printf_parse("done_word return %d\n", (ctx->w == RES_SNTX));
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002643 return (ctx->w == RES_SNTX);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002644 }
Eric Andersen25f27032001-04-26 23:22:31 +00002645 }
2646 glob_target = &child->glob_result;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002647 if (child->argv) flags |= GLOB_APPEND;
Eric Andersen25f27032001-04-26 23:22:31 +00002648 }
2649 gr = xglob(dest, flags, glob_target);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002650 if (gr != 0) {
2651 debug_printf_parse("done_word return 1: xglob returned %d\n", gr);
2652 return 1;
2653 }
Eric Andersen25f27032001-04-26 23:22:31 +00002654
2655 b_reset(dest);
2656 if (ctx->pending_redirect) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002657 ctx->pending_redirect = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002658 if (glob_target->gl_pathc != 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002659 bb_error_msg("ambiguous redirect");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002660 debug_printf_parse("done_word return 1: ambiguous redirect\n");
Eric Andersen25f27032001-04-26 23:22:31 +00002661 return 1;
2662 }
2663 } else {
2664 child->argv = glob_target->gl_pathv;
2665 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002666 if (ctx->w == RES_FOR) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002667 done_word(dest, ctx);
2668 done_pipe(ctx, PIPE_SEQ);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002669 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002670 debug_printf_parse("done_word return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00002671 return 0;
2672}
2673
2674/* The only possible error here is out of memory, in which case
2675 * xmalloc exits. */
2676static int done_command(struct p_context *ctx)
2677{
2678 /* The child is really already in the pipe structure, so
2679 * advance the pipe counter and make a new, null child.
2680 * Only real trickiness here is that the uncommitted
2681 * child structure, to which ctx->child points, is not
2682 * counted in pi->num_progs. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002683 struct pipe *pi = ctx->pipe;
2684 struct child_prog *prog = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00002685
2686 if (prog && prog->group == NULL
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00002687 && prog->argv == NULL
2688 && prog->redirects == NULL
2689 ) {
Eric Andersen25f27032001-04-26 23:22:31 +00002690 debug_printf("done_command: skipping null command\n");
2691 return 0;
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00002692 }
2693 if (prog) {
Eric Andersen25f27032001-04-26 23:22:31 +00002694 pi->num_progs++;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002695 debug_printf("done_command: num_progs incremented to %d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00002696 } else {
2697 debug_printf("done_command: initializing\n");
2698 }
2699 pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1));
2700
2701 prog = pi->progs + pi->num_progs;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002702 memset(prog, 0, sizeof(*prog));
2703 /*prog->redirects = NULL;*/
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +00002704 /*prog->argv = NULL; */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002705 /*prog->is_stopped = 0;*/
2706 /*prog->group = NULL;*/
2707 /*prog->glob_result.gl_pathv = NULL;*/
Eric Andersen25f27032001-04-26 23:22:31 +00002708 prog->family = pi;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002709 /*prog->sp = 0;*/
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002710 ctx->child = prog;
2711 prog->type = ctx->type;
Eric Andersen25f27032001-04-26 23:22:31 +00002712
Eric Andersen25f27032001-04-26 23:22:31 +00002713 /* but ctx->pipe and ctx->list_head remain unchanged */
2714 return 0;
2715}
2716
2717static int done_pipe(struct p_context *ctx, pipe_style type)
2718{
2719 struct pipe *new_p;
2720 done_command(ctx); /* implicit closure of previous command */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002721 debug_printf_parse("done_pipe, type %d\n", type);
Eric Andersen25f27032001-04-26 23:22:31 +00002722 ctx->pipe->followup = type;
2723 ctx->pipe->r_mode = ctx->w;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002724 new_p = new_pipe();
Eric Andersen25f27032001-04-26 23:22:31 +00002725 ctx->pipe->next = new_p;
2726 ctx->pipe = new_p;
2727 ctx->child = NULL;
2728 done_command(ctx); /* set up new pipe to accept commands */
2729 return 0;
2730}
2731
2732/* peek ahead in the in_str to find out if we have a "&n" construct,
2733 * as in "2>&1", that represents duplicating a file descriptor.
2734 * returns either -2 (syntax error), -1 (no &), or the number found.
2735 */
2736static int redirect_dup_num(struct in_str *input)
2737{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002738 int ch, d = 0, ok = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002739 ch = b_peek(input);
2740 if (ch != '&') return -1;
2741
2742 b_getch(input); /* get the & */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002743 ch = b_peek(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00002744 if (ch == '-') {
2745 b_getch(input);
2746 return -3; /* "-" represents "close me" */
2747 }
2748 while (isdigit(ch)) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00002749 d = d*10 + (ch-'0');
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002750 ok = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002751 b_getch(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00002752 ch = b_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00002753 }
2754 if (ok) return d;
2755
Manuel Novoa III cad53642003-03-19 09:13:01 +00002756 bb_error_msg("ambiguous redirect");
Eric Andersen25f27032001-04-26 23:22:31 +00002757 return -2;
2758}
2759
2760/* If a redirect is immediately preceded by a number, that number is
2761 * supposed to tell which file descriptor to redirect. This routine
2762 * looks for such preceding numbers. In an ideal world this routine
2763 * needs to handle all the following classes of redirects...
2764 * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
2765 * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
2766 * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
2767 * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
2768 * A -1 output from this program means no valid number was found, so the
2769 * caller should use the appropriate default for this redirection.
2770 */
2771static int redirect_opt_num(o_string *o)
2772{
2773 int num;
2774
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002775 if (o->length == 0)
2776 return -1;
2777 for (num = 0; num < o->length; num++) {
2778 if (!isdigit(*(o->data + num))) {
Eric Andersen25f27032001-04-26 23:22:31 +00002779 return -1;
2780 }
2781 }
2782 /* reuse num (and save an int) */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002783 num = atoi(o->data);
Eric Andersen25f27032001-04-26 23:22:31 +00002784 b_reset(o);
2785 return num;
2786}
2787
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00002788static FILE *generate_stream_from_list(struct pipe *head)
Eric Andersen25f27032001-04-26 23:22:31 +00002789{
2790 FILE *pf;
Eric Andersen25f27032001-04-26 23:22:31 +00002791 int pid, channel[2];
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002792 if (pipe(channel) < 0) bb_perror_msg_and_die("pipe");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002793#if BB_MMU
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002794 pid = fork();
Eric Andersen72f9a422001-10-28 05:12:20 +00002795#else
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002796 pid = vfork();
Eric Andersen72f9a422001-10-28 05:12:20 +00002797#endif
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002798 if (pid < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002799 bb_perror_msg_and_die("fork");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002800 } else if (pid == 0) {
Eric Andersen25f27032001-04-26 23:22:31 +00002801 close(channel[0]);
2802 if (channel[1] != 1) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00002803 dup2(channel[1], 1);
Eric Andersen25f27032001-04-26 23:22:31 +00002804 close(channel[1]);
2805 }
Eric Andersen94ac2442001-05-22 19:05:18 +00002806 _exit(run_list_real(head)); /* leaks memory */
Eric Andersen25f27032001-04-26 23:22:31 +00002807 }
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002808 debug_printf("forked child %d\n", pid);
Eric Andersen25f27032001-04-26 23:22:31 +00002809 close(channel[1]);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002810 pf = fdopen(channel[0], "r");
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002811 debug_printf("pipe on FILE *%p\n", pf);
Eric Andersen25f27032001-04-26 23:22:31 +00002812 return pf;
2813}
2814
2815/* this version hacked for testing purposes */
2816/* return code is exit status of the process that is run. */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002817static int process_command_subs(o_string *dest, struct p_context *ctx,
2818 struct in_str *input, const char *subst_end)
Eric Andersen25f27032001-04-26 23:22:31 +00002819{
2820 int retcode;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002821 o_string result = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00002822 struct p_context inner;
2823 FILE *p;
2824 struct in_str pipe_str;
2825 initialize_context(&inner);
2826
2827 /* recursion to generate command */
2828 retcode = parse_stream(&result, &inner, input, subst_end);
2829 if (retcode != 0) return retcode; /* syntax error or EOF */
2830 done_word(&result, &inner);
2831 done_pipe(&inner, PIPE_SEQ);
2832 b_free(&result);
2833
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002834 p = generate_stream_from_list(inner.list_head);
2835 if (p == NULL) return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002836 mark_open(fileno(p));
2837 setup_file_in_str(&pipe_str, p);
2838
2839 /* now send results of command back into original context */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002840 retcode = parse_stream(dest, ctx, &pipe_str, NULL);
Eric Andersen25f27032001-04-26 23:22:31 +00002841 /* XXX In case of a syntax error, should we try to kill the child?
2842 * That would be tough to do right, so just read until EOF. */
2843 if (retcode == 1) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002844 while (b_getch(&pipe_str) != EOF)
2845 /* discard */;
Eric Andersen25f27032001-04-26 23:22:31 +00002846 }
2847
2848 debug_printf("done reading from pipe, pclose()ing\n");
2849 /* This is the step that wait()s for the child. Should be pretty
2850 * safe, since we just read an EOF from its stdout. We could try
2851 * to better, by using wait(), and keeping track of background jobs
2852 * at the same time. That would be a lot of work, and contrary
2853 * to the KISS philosophy of this program. */
2854 mark_closed(fileno(p));
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002855 retcode = pclose(p);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002856 free_pipe_list(inner.list_head, 0);
2857 debug_printf("pclosed, retcode=%d\n", retcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002858 /* XXX this process fails to trim a single trailing newline */
2859 return retcode;
2860}
2861
2862static int parse_group(o_string *dest, struct p_context *ctx,
2863 struct in_str *input, int ch)
2864{
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002865 int rcode;
2866 const char *endch = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002867 struct p_context sub;
2868 struct child_prog *child = ctx->child;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002869
2870 debug_printf_parse("parse_group entered\n");
Eric Andersen25f27032001-04-26 23:22:31 +00002871 if (child->argv) {
2872 syntax();
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002873 debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n");
2874 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002875 }
2876 initialize_context(&sub);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00002877 switch (ch) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002878 case '(':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002879 endch = ")";
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002880 child->subshell = 1;
2881 break;
2882 case '{':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002883 endch = "}";
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002884 break;
2885 default:
2886 syntax(); /* really logic error */
Eric Andersen25f27032001-04-26 23:22:31 +00002887 }
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002888 rcode = parse_stream(dest, &sub, input, endch);
2889 done_word(dest, &sub); /* finish off the final word in the subcontext */
Eric Andersen25f27032001-04-26 23:22:31 +00002890 done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
2891 child->group = sub.list_head;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002892
2893 debug_printf_parse("parse_group return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002894 return rcode;
2895 /* child remains "open", available for possible redirects */
2896}
2897
2898/* basically useful version until someone wants to get fancier,
2899 * see the bash man page under "Parameter Expansion" */
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00002900static const char *lookup_param(const char *src)
Eric Andersen25f27032001-04-26 23:22:31 +00002901{
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00002902 const char *p = NULL;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002903 if (src) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002904 p = getenv(src);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002905 if (!p)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002906 p = get_local_var(src);
Eric Andersen20a69a72001-05-15 17:24:44 +00002907 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002908 return p;
Eric Andersen25f27032001-04-26 23:22:31 +00002909}
2910
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002911/* Make new string for parser */
2912static char* make_string(char ** inp)
2913{
2914 char *p;
2915 char *str = NULL;
2916 int n;
2917 int len = 2;
2918
2919 for (n = 0; inp[n]; n++) {
2920 p = insert_var_value(inp[n]);
2921 str = xrealloc(str, (len + strlen(p)));
2922 if (n) {
2923 strcat(str, " ");
2924 } else {
2925 *str = '\0';
2926 }
2927 strcat(str, p);
2928 len = strlen(str) + 3;
2929 if (p != inp[n]) free(p);
2930 }
2931 len = strlen(str);
2932 str[len] = '\n';
2933 str[len+1] = '\0';
2934 return str;
2935}
2936
Eric Andersen25f27032001-04-26 23:22:31 +00002937/* return code: 0 for OK, 1 for syntax error */
2938static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input)
2939{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002940 int i, advance = 0;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002941 char sep[] = " ";
Eric Andersen25f27032001-04-26 23:22:31 +00002942 int ch = input->peek(input); /* first character after the $ */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002943 debug_printf("handle_dollar: ch=%c\n", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00002944 if (isalpha(ch)) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002945 b_addchr(dest, SPECIAL_VAR_SYMBOL);
2946 ctx->child->sp++;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002947 while (ch = b_peek(input), isalnum(ch) || ch == '_') {
Eric Andersen25f27032001-04-26 23:22:31 +00002948 b_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002949 b_addchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00002950 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002951 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00002952 } else if (isdigit(ch)) {
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002953 i = ch - '0'; /* XXX is $0 special? */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002954 if (i < global_argc) {
Eric Andersen25f27032001-04-26 23:22:31 +00002955 parse_string(dest, ctx, global_argv[i]); /* recursion */
2956 }
2957 advance = 1;
2958 } else switch (ch) {
2959 case '$':
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002960 b_adduint(dest, getpid());
Eric Andersen25f27032001-04-26 23:22:31 +00002961 advance = 1;
2962 break;
2963 case '!':
2964 if (last_bg_pid > 0) b_adduint(dest, last_bg_pid);
2965 advance = 1;
2966 break;
2967 case '?':
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002968 b_adduint(dest, last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +00002969 advance = 1;
2970 break;
2971 case '#':
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002972 b_adduint(dest, global_argc ? global_argc-1 : 0);
Eric Andersen25f27032001-04-26 23:22:31 +00002973 advance = 1;
2974 break;
2975 case '{':
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002976 b_addchr(dest, SPECIAL_VAR_SYMBOL);
2977 ctx->child->sp++;
Eric Andersen25f27032001-04-26 23:22:31 +00002978 b_getch(input);
2979 /* XXX maybe someone will try to escape the '}' */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002980 while (1) {
2981 ch = b_getch(input);
2982 if (ch == EOF || ch == '}')
2983 break;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002984 b_addchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00002985 }
2986 if (ch != '}') {
2987 syntax();
2988 return 1;
2989 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002990 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00002991 break;
2992 case '(':
Matt Kraai9f8caf12001-05-02 16:26:12 +00002993 b_getch(input);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002994 process_command_subs(dest, ctx, input, ")");
Eric Andersen25f27032001-04-26 23:22:31 +00002995 break;
2996 case '*':
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002997 sep[0] = ifs[0];
2998 for (i = 1; i < global_argc; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002999 parse_string(dest, ctx, global_argv[i]);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003000 if (i+1 < global_argc)
3001 parse_string(dest, ctx, sep);
Eric Andersen25f27032001-04-26 23:22:31 +00003002 }
3003 break;
3004 case '@':
3005 case '-':
3006 case '_':
3007 /* still unhandled, but should be eventually */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003008 bb_error_msg("unhandled syntax: $%c", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003009 return 1;
3010 break;
3011 default:
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003012 b_addqchr(dest,'$', dest->quote);
Eric Andersen25f27032001-04-26 23:22:31 +00003013 }
3014 /* Eat the character if the flag was set. If the compiler
3015 * is smart enough, we could substitute "b_getch(input);"
3016 * for all the "advance = 1;" above, and also end up with
3017 * a nice size-optimized program. Hah! That'll be the day.
3018 */
3019 if (advance) b_getch(input);
3020 return 0;
3021}
3022
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003023static int parse_string(o_string *dest, struct p_context *ctx, const char *src)
Eric Andersen25f27032001-04-26 23:22:31 +00003024{
3025 struct in_str foo;
3026 setup_string_in_str(&foo, src);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003027 return parse_stream(dest, ctx, &foo, NULL);
Eric Andersen25f27032001-04-26 23:22:31 +00003028}
3029
3030/* return code is 0 for normal exit, 1 for syntax error */
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003031static int parse_stream(o_string *dest, struct p_context *ctx,
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003032 struct in_str *input, const char *end_trigger)
Eric Andersen25f27032001-04-26 23:22:31 +00003033{
"Vladimir N. Oleynik"4ccd2b42006-01-31 09:27:48 +00003034 int ch, m;
Eric Andersen25f27032001-04-26 23:22:31 +00003035 int redir_fd;
3036 redir_type redir_style;
3037 int next;
3038
3039 /* Only double-quote state is handled in the state variable dest->quote.
3040 * A single-quote triggers a bypass of the main loop until its mate is
3041 * found. When recursing, quote state is passed in via dest->quote. */
3042
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003043 debug_printf_parse("parse_stream entered, end_trigger='%s'\n", end_trigger);
3044
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003045 while ((ch = b_getch(input)) != EOF) {
Eric Andersen25f27032001-04-26 23:22:31 +00003046 m = map[ch];
3047 next = (ch == '\n') ? 0 : b_peek(input);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003048 debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n",
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003049 ch, ch, m, dest->quote);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003050 if (m == MAP_ORDINARY
3051 || (m != MAP_NEVER_FLOWTROUGH && dest->quote)
3052 ) {
Eric Andersen25f27032001-04-26 23:22:31 +00003053 b_addqchr(dest, ch, dest->quote);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003054 continue;
3055 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003056 if (m == MAP_IFS_IF_UNQUOTED) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003057 if (done_word(dest, ctx)) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003058 debug_printf_parse("parse_stream return 1\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003059 return 1;
Eric Andersenaac75e52001-04-30 18:18:45 +00003060 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003061 /* If we aren't performing a substitution, treat
3062 * a newline as a command separator.
3063 * [why we don't handle it exactly like ';'? --vda] */
3064 if (end_trigger && ch == '\n') {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003065 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003066 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003067 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003068 if ((end_trigger && strchr(end_trigger, ch))
3069 && !dest->quote && ctx->w == RES_NONE
3070 ) {
3071 debug_printf_parse("parse_stream return 0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003072 return 0;
3073 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003074 if (m == MAP_IFS_IF_UNQUOTED)
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003075 continue;
3076 switch (ch) {
Eric Andersen25f27032001-04-26 23:22:31 +00003077 case '#':
3078 if (dest->length == 0 && !dest->quote) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003079 while (1) {
3080 ch = b_peek(input);
3081 if (ch == EOF || ch == '\n')
3082 break;
3083 b_getch(input);
3084 }
Eric Andersen25f27032001-04-26 23:22:31 +00003085 } else {
3086 b_addqchr(dest, ch, dest->quote);
3087 }
3088 break;
3089 case '\\':
3090 if (next == EOF) {
3091 syntax();
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003092 debug_printf_parse("parse_stream return 1\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003093 return 1;
3094 }
3095 b_addqchr(dest, '\\', dest->quote);
3096 b_addqchr(dest, b_getch(input), dest->quote);
3097 break;
3098 case '$':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003099 if (handle_dollar(dest, ctx, input) != 0) {
3100 debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n");
3101 return 1;
3102 }
Eric Andersen25f27032001-04-26 23:22:31 +00003103 break;
3104 case '\'':
3105 dest->nonnull = 1;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003106 while (1) {
3107 ch = b_getch(input);
3108 if (ch == EOF || ch == '\'')
3109 break;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003110 b_addchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003111 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003112 if (ch == EOF) {
Eric Andersen25f27032001-04-26 23:22:31 +00003113 syntax();
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003114 debug_printf_parse("parse_stream return 1\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003115 return 1;
3116 }
3117 break;
3118 case '"':
3119 dest->nonnull = 1;
3120 dest->quote = !dest->quote;
3121 break;
3122 case '`':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003123 process_command_subs(dest, ctx, input, "`");
Eric Andersen25f27032001-04-26 23:22:31 +00003124 break;
3125 case '>':
3126 redir_fd = redirect_opt_num(dest);
3127 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003128 redir_style = REDIRECT_OVERWRITE;
Eric Andersen25f27032001-04-26 23:22:31 +00003129 if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003130 redir_style = REDIRECT_APPEND;
Eric Andersen25f27032001-04-26 23:22:31 +00003131 b_getch(input);
3132 } else if (next == '(') {
3133 syntax(); /* until we support >(list) Process Substitution */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003134 debug_printf_parse("parse_stream return 1: >(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003135 return 1;
3136 }
3137 setup_redirect(ctx, redir_fd, redir_style, input);
3138 break;
3139 case '<':
3140 redir_fd = redirect_opt_num(dest);
3141 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003142 redir_style = REDIRECT_INPUT;
Eric Andersen25f27032001-04-26 23:22:31 +00003143 if (next == '<') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003144 redir_style = REDIRECT_HEREIS;
Eric Andersen25f27032001-04-26 23:22:31 +00003145 b_getch(input);
3146 } else if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003147 redir_style = REDIRECT_IO;
Eric Andersen25f27032001-04-26 23:22:31 +00003148 b_getch(input);
3149 } else if (next == '(') {
3150 syntax(); /* until we support <(list) Process Substitution */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003151 debug_printf_parse("parse_stream return 1: <(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003152 return 1;
3153 }
3154 setup_redirect(ctx, redir_fd, redir_style, input);
3155 break;
3156 case ';':
3157 done_word(dest, ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003158 done_pipe(ctx, PIPE_SEQ);
Eric Andersen25f27032001-04-26 23:22:31 +00003159 break;
3160 case '&':
3161 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003162 if (next == '&') {
Eric Andersen25f27032001-04-26 23:22:31 +00003163 b_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003164 done_pipe(ctx, PIPE_AND);
Eric Andersen25f27032001-04-26 23:22:31 +00003165 } else {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003166 done_pipe(ctx, PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00003167 }
3168 break;
3169 case '|':
3170 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003171 if (next == '|') {
Eric Andersen25f27032001-04-26 23:22:31 +00003172 b_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003173 done_pipe(ctx, PIPE_OR);
Eric Andersen25f27032001-04-26 23:22:31 +00003174 } else {
3175 /* we could pick up a file descriptor choice here
3176 * with redirect_opt_num(), but bash doesn't do it.
3177 * "echo foo 2| cat" yields "foo 2". */
3178 done_command(ctx);
3179 }
3180 break;
3181 case '(':
3182 case '{':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003183 if (parse_group(dest, ctx, input, ch) != 0) {
3184 debug_printf_parse("parse_stream return 1: parse_group returned non-0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003185 return 1;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003186 }
Eric Andersen25f27032001-04-26 23:22:31 +00003187 break;
3188 case ')':
3189 case '}':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003190 syntax(); /* Proper use of this character is caught by end_trigger */
3191 debug_printf_parse("parse_stream return 1: unexpected '}'\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003192 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003193 default:
3194 syntax(); /* this is really an internal logic error */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003195 debug_printf_parse("parse_stream return 1: internal logic error\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003196 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003197 }
3198 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003199 /* Complain if quote? No, maybe we just finished a command substitution
Eric Andersen25f27032001-04-26 23:22:31 +00003200 * that was quoted. Example:
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003201 * $ echo "`cat foo` plus more"
Eric Andersen25f27032001-04-26 23:22:31 +00003202 * and we just got the EOF generated by the subshell that ran "cat foo"
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003203 * The only real complaint is if we got an EOF when end_trigger != NULL,
Eric Andersen25f27032001-04-26 23:22:31 +00003204 * that is, we were really supposed to get end_trigger, and never got
3205 * one before the EOF. Can't use the standard "syntax error" return code,
3206 * so that parse_stream_outer can distinguish the EOF and exit smoothly. */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003207 debug_printf_parse("parse_stream return -%d\n", end_trigger != NULL);
3208 if (end_trigger)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003209 return -1;
Eric Andersen25f27032001-04-26 23:22:31 +00003210 return 0;
3211}
3212
Eric Andersena68ea1c2006-01-30 22:48:39 +00003213static void mapset(const char *set, int code)
Eric Andersen25f27032001-04-26 23:22:31 +00003214{
Denis Vlasenkof5294e12007-04-14 10:09:57 +00003215 while (*set)
3216 map[(unsigned char)*set++] = code;
Eric Andersen25f27032001-04-26 23:22:31 +00003217}
3218
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00003219static void update_ifs_map(void)
Eric Andersen25f27032001-04-26 23:22:31 +00003220{
3221 /* char *ifs and char map[256] are both globals. */
3222 ifs = getenv("IFS");
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003223 if (ifs == NULL) ifs = " \t\n";
Eric Andersen25f27032001-04-26 23:22:31 +00003224 /* Precompute a list of 'flow through' behavior so it can be treated
3225 * quickly up front. Computation is necessary because of IFS.
3226 * Special case handling of IFS == " \t\n" is not implemented.
3227 * The map[] array only really needs two bits each, and on most machines
3228 * that would be faster because of the reduced L1 cache footprint.
3229 */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003230 memset(map, MAP_ORDINARY, sizeof(map)); /* most chars flow through always */
3231 mapset("\\$'\"`", MAP_NEVER_FLOWTROUGH);
3232 mapset("<>;&|(){}#", MAP_FLOWTROUGH_IF_QUOTED);
3233 mapset(ifs, MAP_IFS_IF_UNQUOTED); /* also flow through if quoted */
Eric Andersen25f27032001-04-26 23:22:31 +00003234}
3235
Eric Andersenaff114c2004-04-14 17:51:38 +00003236/* most recursion does not come through here, the exception is
Eric Andersen25f27032001-04-26 23:22:31 +00003237 * from builtin_source() */
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003238static int parse_stream_outer(struct in_str *inp, int flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003239{
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003240// FIXME: '{ true | exit 3; echo $? }' is parsed as a whole,
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00003241// as a result $? is replaced by 0, not 3!
3242// Need to stop & execute stuff at ';', not parse till EOL!
3243
Eric Andersen25f27032001-04-26 23:22:31 +00003244 struct p_context ctx;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003245 o_string temp = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003246 int rcode;
3247 do {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003248 ctx.type = flag;
Eric Andersen25f27032001-04-26 23:22:31 +00003249 initialize_context(&ctx);
3250 update_ifs_map();
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003251 if (!(flag & FLAG_PARSE_SEMICOLON) || (flag & FLAG_REPARSING))
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003252 mapset(";$&|", MAP_ORDINARY);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003253#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003254 inp->promptmode = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003255#endif
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003256 rcode = parse_stream(&temp, &ctx, inp, ";\n");
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003257 if (rcode != 1 && ctx.old_flag != 0) {
3258 syntax();
3259 }
3260 if (rcode != 1 && ctx.old_flag == 0) {
3261 done_word(&temp, &ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003262 done_pipe(&ctx, PIPE_SEQ);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00003263 debug_printf_exec("parse_stream_outer: run_list\n");
Denis Vlasenko400c5b62007-05-04 13:07:27 +00003264 debug_print_tree(ctx.list_head, 0);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003265 run_list(ctx.list_head);
3266 } else {
3267 if (ctx.old_flag != 0) {
3268 free(ctx.stack);
3269 b_reset(&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003270 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003271 temp.nonnull = 0;
3272 temp.quote = 0;
3273 inp->p = NULL;
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003274 free_pipe_list(ctx.list_head, 0);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003275 }
Eric Andersena813afc2001-05-24 16:19:36 +00003276 b_free(&temp);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003277 } while (rcode != -1 && !(flag & FLAG_EXIT_FROM_LOOP)); /* loop on syntax errors, return on EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003278 return 0;
3279}
3280
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003281static int parse_string_outer(const char *s, int flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003282{
3283 struct in_str input;
3284 setup_string_in_str(&input, s);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003285 return parse_stream_outer(&input, flag);
Eric Andersen25f27032001-04-26 23:22:31 +00003286}
3287
3288static int parse_file_outer(FILE *f)
3289{
3290 int rcode;
3291 struct in_str input;
3292 setup_file_in_str(&input, f);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003293 rcode = parse_stream_outer(&input, FLAG_PARSE_SEMICOLON);
Eric Andersen25f27032001-04-26 23:22:31 +00003294 return rcode;
3295}
3296
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003297#if ENABLE_HUSH_JOB
Eric Andersen6c947d22001-06-25 22:24:38 +00003298/* Make sure we have a controlling tty. If we get started under a job
3299 * aware app (like bash for example), make sure we are now in charge so
3300 * we don't fight over who gets the foreground */
Eric Anderseneaecbf32001-10-31 10:41:31 +00003301static void setup_job_control(void)
Eric Andersen52a97ca2001-06-22 06:49:26 +00003302{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003303 pid_t shell_pgrp;
3304
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003305 saved_task_pgrp = shell_pgrp = getpgrp();
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003306 debug_printf("saved_task_pgrp=%d\n", saved_task_pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003307 fcntl(interactive_fd, F_SETFD, FD_CLOEXEC);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003308
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003309 /* If we were ran as 'hush &',
3310 * sleep until we are in the foreground. */
3311 while (tcgetpgrp(interactive_fd) != shell_pgrp) {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003312 /* Send TTIN to ourself (should stop us) */
Denis Vlasenkoff131b92007-04-10 15:42:06 +00003313 kill(- shell_pgrp, SIGTTIN);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003314 shell_pgrp = getpgrp();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003315 }
Eric Andersen52a97ca2001-06-22 06:49:26 +00003316
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003317 /* Ignore job-control and misc signals. */
3318 set_jobctrl_sighandler(SIG_IGN);
3319 set_misc_sighandler(SIG_IGN);
3320//huh? signal(SIGCHLD, SIG_IGN);
3321
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003322 /* We _must_ restore tty pgrp fatal signals */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003323 set_fatal_sighandler(sigexit);
Eric Andersen6c947d22001-06-25 22:24:38 +00003324
3325 /* Put ourselves in our own process group. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003326 setpgrp(); /* is the same as setpgid(our_pid, our_pid); */
Eric Andersen6c947d22001-06-25 22:24:38 +00003327 /* Grab control of the terminal. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003328 tcsetpgrp(interactive_fd, getpid());
Eric Andersen6c947d22001-06-25 22:24:38 +00003329}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003330#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00003331
Denis Vlasenko06af2162007-02-03 17:28:39 +00003332int hush_main(int argc, char **argv);
Matt Kraai2d91deb2001-08-01 17:21:35 +00003333int hush_main(int argc, char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00003334{
3335 int opt;
3336 FILE *input;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003337 char **e;
Eric Andersenbc604a22001-05-16 05:24:03 +00003338
Denis Vlasenko38f63192007-01-22 09:03:07 +00003339#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003340 line_input_state = new_line_input_t(FOR_SHELL);
3341#endif
3342
Eric Andersen25f27032001-04-26 23:22:31 +00003343 /* XXX what should these be while sourcing /etc/profile? */
3344 global_argc = argc;
3345 global_argv = argv;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003346
Matt Kraai2d91deb2001-08-01 17:21:35 +00003347 /* (re?) initialize globals. Sometimes hush_main() ends up calling
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003348 * hush_main(), therefore we cannot rely on the BSS to zero out this
Eric Andersen94ac2442001-05-22 19:05:18 +00003349 * stuff. Reset these to 0 every time. */
3350 ifs = NULL;
Eric Andersenaeb44c42001-05-22 20:29:00 +00003351 /* map[] is taken care of with call to update_ifs_map() */
Eric Andersen94ac2442001-05-22 19:05:18 +00003352 fake_mode = 0;
Eric Andersen94ac2442001-05-22 19:05:18 +00003353 close_me_head = NULL;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003354#if ENABLE_HUSH_INTERACTIVE
3355 interactive_fd = 0;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003356#endif
3357#if ENABLE_HUSH_JOB
Eric Andersen94ac2442001-05-22 19:05:18 +00003358 last_bg_pid = 0;
Eric Andersen52a97ca2001-06-22 06:49:26 +00003359 job_list = NULL;
Eric Andersenc798b072001-06-22 06:23:03 +00003360 last_jobid = 0;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003361#endif
Eric Andersen94ac2442001-05-22 19:05:18 +00003362
3363 /* Initialize some more globals to non-zero values */
3364 set_cwd();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003365#if ENABLE_HUSH_INTERACTIVE
3366#if ENABLE_FEATURE_EDITING
3367 cmdedit_set_initial_prompt();
3368#else
3369 PS1 = NULL;
3370#endif
Eric Andersen94ac2442001-05-22 19:05:18 +00003371 PS2 = "> ";
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003372#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003373 /* initialize our shell local variables with the values
Eric Andersen94ac2442001-05-22 19:05:18 +00003374 * currently living in the environment */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003375 e = environ;
3376 if (e)
3377 while (*e)
3378 set_local_var(*e++, 2); /* without call putenv() */
Eric Andersen94ac2442001-05-22 19:05:18 +00003379
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003380 last_return_code = EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00003381
3382 if (argv[0] && argv[0][0] == '-') {
3383 debug_printf("\nsourcing /etc/profile\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003384 input = fopen("/etc/profile", "r");
3385 if (input != NULL) {
Eric Andersena90f20b2001-06-26 23:00:21 +00003386 mark_open(fileno(input));
3387 parse_file_outer(input);
3388 mark_closed(fileno(input));
3389 fclose(input);
3390 }
Eric Andersen25f27032001-04-26 23:22:31 +00003391 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003392 input = stdin;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003393
Eric Andersen25f27032001-04-26 23:22:31 +00003394 while ((opt = getopt(argc, argv, "c:xif")) > 0) {
3395 switch (opt) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003396 case 'c':
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003397 global_argv = argv + optind;
3398 global_argc = argc - optind;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003399 opt = parse_string_outer(optarg, FLAG_PARSE_SEMICOLON);
3400 goto final_return;
3401 case 'i':
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003402 // Well, we cannot just declare interactiveness,
3403 // we have to have some stuff (ctty, etc)
3404 /*interactive_fd++;*/
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003405 break;
3406 case 'f':
3407 fake_mode++;
3408 break;
3409 default:
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003410#ifndef BB_VER
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003411 fprintf(stderr, "Usage: sh [FILE]...\n"
3412 " or: sh -c command [args]...\n\n");
3413 exit(EXIT_FAILURE);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003414#else
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003415 bb_show_usage();
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003416#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003417 }
3418 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003419#if ENABLE_HUSH_JOB
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003420 /* A shell is interactive if the '-i' flag was given, or if all of
Eric Andersen25f27032001-04-26 23:22:31 +00003421 * the following conditions are met:
Denis Vlasenko55b2de72007-04-18 17:21:28 +00003422 * no -c command
Eric Andersen25f27032001-04-26 23:22:31 +00003423 * no arguments remaining or the -s flag given
3424 * standard input is a terminal
3425 * standard output is a terminal
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003426 * Refer to Posix.2, the description of the 'sh' utility. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003427 if (argv[optind] == NULL && input == stdin
3428 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
3429 ) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003430 saved_tty_pgrp = tcgetpgrp(STDIN_FILENO);
3431 debug_printf("saved_tty_pgrp=%d\n", saved_tty_pgrp);
3432 if (saved_tty_pgrp >= 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003433 /* try to dup to high fd#, >= 255 */
3434 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
3435 if (interactive_fd < 0) {
3436 /* try to dup to any fd */
3437 interactive_fd = dup(STDIN_FILENO);
3438 if (interactive_fd < 0)
3439 /* give up */
3440 interactive_fd = 0;
3441 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003442 // TODO: track & disallow any attempts of user
3443 // to (inadvertently) close/redirect it
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003444 }
Eric Andersen25f27032001-04-26 23:22:31 +00003445 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003446 debug_printf("\ninteractive_fd=%d\n", interactive_fd);
3447 if (interactive_fd) {
Eric Andersen25f27032001-04-26 23:22:31 +00003448 /* Looks like they want an interactive shell */
Eric Andersen52a97ca2001-06-22 06:49:26 +00003449 setup_job_control();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003450 /* Make xfuncs do cleanup on exit */
3451 die_sleep = -1; /* flag */
3452 if (setjmp(die_jmp)) {
3453 /* xfunc has failed! die die die */
3454 hush_exit(xfunc_error_retval);
3455 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003456#if !ENABLE_FEATURE_SH_EXTRA_QUIET
Denis Vlasenkoef36ead2007-05-02 15:34:47 +00003457 printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", BB_BANNER);
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003458 printf("Enter 'help' for a list of built-in commands.\n\n");
3459#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00003460 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003461#elif ENABLE_HUSH_INTERACTIVE
3462/* no job control compiled, only prompt/line editing */
3463 if (argv[optind] == NULL && input == stdin
3464 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
3465 ) {
3466 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
3467 if (interactive_fd < 0) {
3468 /* try to dup to any fd */
3469 interactive_fd = dup(STDIN_FILENO);
3470 if (interactive_fd < 0)
3471 /* give up */
3472 interactive_fd = 0;
3473 }
3474 }
3475
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003476#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003477
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003478 if (argv[optind] == NULL) {
3479 opt = parse_file_outer(stdin);
Eric Andersene67c3ce2001-05-02 02:09:36 +00003480 goto final_return;
Eric Andersen25f27032001-04-26 23:22:31 +00003481 }
Eric Andersen25f27032001-04-26 23:22:31 +00003482
3483 debug_printf("\nrunning script '%s'\n", argv[optind]);
Denis Vlasenko4c978632007-02-03 03:31:13 +00003484 global_argv = argv + optind;
3485 global_argc = argc - optind;
Rob Landleyd921b2e2006-08-03 15:41:12 +00003486 input = xfopen(argv[optind], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00003487 opt = parse_file_outer(input);
3488
Denis Vlasenko38f63192007-01-22 09:03:07 +00003489#if ENABLE_FEATURE_CLEAN_UP
Eric Andersenaeb44c42001-05-22 20:29:00 +00003490 fclose(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003491 if (cwd != bb_msg_unknown)
Eric Andersenaeb44c42001-05-22 20:29:00 +00003492 free((char*)cwd);
3493 {
3494 struct variables *cur, *tmp;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003495 for (cur = top_vars; cur; cur = tmp) {
Eric Andersenaeb44c42001-05-22 20:29:00 +00003496 tmp = cur->next;
3497 if (!cur->flg_read_only) {
Denis Vlasenko4c978632007-02-03 03:31:13 +00003498 free((char*)cur->name);
3499 free((char*)cur->value);
Eric Andersenaeb44c42001-05-22 20:29:00 +00003500 free(cur);
3501 }
3502 }
3503 }
Eric Andersen25f27032001-04-26 23:22:31 +00003504#endif
3505
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003506 final_return:
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003507 hush_exit(opt ? opt : last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +00003508}