blob: c51ed1a514267e32d7225ef01014ad6de1e80ef0 [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
471#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000472/* move to libbb? */
473static void signal_SA_RESTART(int sig, void (*handler)(int))
474{
475 struct sigaction sa;
476 sa.sa_handler = handler;
477 sa.sa_flags = SA_RESTART;
478 sigemptyset(&sa.sa_mask);
479 sigaction(sig, &sa, NULL);
480}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000481#endif
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000482
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000483/* Signals are grouped, we handle them in batches */
484static void set_fatal_sighandler(void (*handler)(int))
485{
486 signal(SIGILL , handler);
487 signal(SIGTRAP, handler);
488 signal(SIGABRT, handler);
489 signal(SIGFPE , handler);
490 signal(SIGBUS , handler);
491 signal(SIGSEGV, handler);
492 /* bash 3.2 seems to handle these just like 'fatal' ones */
493 signal(SIGHUP , handler);
494 signal(SIGPIPE, handler);
495 signal(SIGALRM, handler);
496}
497static void set_jobctrl_sighandler(void (*handler)(int))
498{
499 signal(SIGTSTP, handler);
500 signal(SIGTTIN, handler);
501 signal(SIGTTOU, handler);
502}
503static void set_misc_sighandler(void (*handler)(int))
504{
505 signal(SIGINT , handler);
506 signal(SIGQUIT, handler);
507 signal(SIGTERM, handler);
508}
509/* SIGCHLD is special and handled separately */
510
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000511#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000512static void set_every_sighandler(void (*handler)(int))
513{
514 set_fatal_sighandler(handler);
515 set_jobctrl_sighandler(handler);
516 set_misc_sighandler(handler);
517 signal(SIGCHLD, handler);
518}
519
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000520static struct pipe *nofork_pipe;
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000521struct nofork_save_area nofork_save;
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000522static sigjmp_buf nofork_jb;
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
528 siglongjmp(nofork_jb, 1);
529}
530
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000531static void handler_ctrl_z(int sig)
532{
533 pid_t pid;
534
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000535 debug_printf_jobs("got tty sig %d\n", sig);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000536 pid = fork();
537 if (pid < 0) /* can't fork. Pretend there were no Ctrl-Z */
538 return;
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000539 debug_printf_jobs("bg'ing nofork\n");
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000540 nofork_save.saved = 0; /* flag the fact that Ctrl-Z was handled */
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000541 nofork_pipe->running_progs = 1;
542 nofork_pipe->stopped_progs = 0;
543 if (!pid) { /* child */
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000544 debug_printf_jobs("setting pgrp for child\n");
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000545 setpgrp();
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000546 set_every_sighandler(SIG_DFL);
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000547 raise(SIGTSTP); /* resend TSTP so that child will be stopped */
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000548 debug_printf_jobs("returning to child\n");
Denis Vlasenkoa6a17852007-04-28 16:42:11 +0000549 /* return to nofork, it will eventually exit now,
550 * not return back to shell */
551 return;
552 }
553 /* parent */
554 /* finish filling up pipe info */
555 nofork_pipe->pgrp = pid; /* child is in its own pgrp */
556 nofork_pipe->progs[0].pid = pid;
557 nofork_pipe->running_progs = 1;
558 nofork_pipe->stopped_progs = 0;
559 /* parent needs to longjmp out of running nofork.
560 * we will "return" exitcode 0, with child put in background */
561// as usual we can have all kinds of nasty problems with leaked malloc data here
562 siglongjmp(nofork_jb, 1);
563}
564
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000565#endif
566
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000567/* Restores tty foreground process group, and exits.
568 * May be called as signal handler for fatal signal
569 * (will faithfully resend signal to itself, producing correct exit state)
570 * or called directly with -EXITCODE.
571 * We also call it if xfunc is exiting. */
572static void sigexit(int sig) ATTRIBUTE_NORETURN;
573static void sigexit(int sig)
574{
575 sigset_t block_all;
576
577 /* Disable all signals: job control, SIGPIPE, etc. */
578 sigfillset(&block_all);
579 sigprocmask(SIG_SETMASK, &block_all, NULL);
580
Denis Vlasenko87cb2db2007-04-21 10:00:01 +0000581 if (interactive_fd)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000582 tcsetpgrp(interactive_fd, saved_tty_pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000583
584 /* Not a signal, just exit */
585 if (sig <= 0)
586 _exit(- sig);
587
588 /* Enable only this sig and kill ourself with it */
589 signal(sig, SIG_DFL);
590 sigdelset(&block_all, sig);
591 sigprocmask(SIG_SETMASK, &block_all, NULL);
592 raise(sig);
593 _exit(1); /* Should not reach it */
594}
595
596/* Restores tty foreground process group, and exits. */
597static void hush_exit(int exitcode) ATTRIBUTE_NORETURN;
598static void hush_exit(int exitcode)
599{
600 fflush(NULL); /* flush all streams */
601 sigexit(- (exitcode & 0xff));
602}
603
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000604#else /* !JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000605
606#define set_fatal_sighandler(handler) ((void)0)
607#define set_jobctrl_sighandler(handler) ((void)0)
608#define set_misc_sighandler(handler) ((void)0)
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000609#define hush_exit(e) exit(e)
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000610
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000611#endif /* JOB */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000612
613
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000614static const char *set_cwd(void)
615{
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000616 if (cwd == bb_msg_unknown)
Denis Vlasenko7cced6e2007-04-12 17:08:53 +0000617 cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */
Denis Vlasenko6ca04442007-02-11 16:19:28 +0000618 cwd = xrealloc_getcwd_or_warn((char *)cwd);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000619 if (!cwd)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000620 cwd = bb_msg_unknown;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000621 return cwd;
622}
623
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000624/* built-in 'eval' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000625static int builtin_eval(char **argv)
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000626{
627 char *str = NULL;
628 int rcode = EXIT_SUCCESS;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000629
Denis Vlasenko1359da62007-04-21 23:27:30 +0000630 if (argv[1]) {
631 str = make_string(argv + 1);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000632 parse_string_outer(str, FLAG_EXIT_FROM_LOOP |
Eric Andersen4c9b68f2002-04-13 12:33:41 +0000633 FLAG_PARSE_SEMICOLON);
634 free(str);
635 rcode = last_return_code;
636 }
637 return rcode;
638}
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000639
Eric Andersen25f27032001-04-26 23:22:31 +0000640/* built-in 'cd <path>' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000641static int builtin_cd(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000642{
643 char *newdir;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000644 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000645 newdir = getenv("HOME");
646 else
Denis Vlasenko1359da62007-04-21 23:27:30 +0000647 newdir = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000648 if (chdir(newdir)) {
649 printf("cd: %s: %s\n", newdir, strerror(errno));
650 return EXIT_FAILURE;
651 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000652 set_cwd();
Eric Andersen25f27032001-04-26 23:22:31 +0000653 return EXIT_SUCCESS;
654}
655
Eric Andersen25f27032001-04-26 23:22:31 +0000656/* built-in 'exec' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000657static int builtin_exec(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000658{
Denis Vlasenko1359da62007-04-21 23:27:30 +0000659 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000660 return EXIT_SUCCESS; /* Really? */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000661 pseudo_exec_argv(argv + 1);
Eric Andersen25f27032001-04-26 23:22:31 +0000662 /* never returns */
663}
664
665/* built-in 'exit' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000666static int builtin_exit(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000667{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000668// TODO: bash does it ONLY on top-level sh exit (+interacive only?)
669 //puts("exit"); /* bash does it */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000670// TODO: warn if we have background jobs: "There are stopped jobs"
671// On second consecutive 'exit', exit anyway.
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000672
Denis Vlasenko1359da62007-04-21 23:27:30 +0000673 if (argv[1] == NULL)
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000674 hush_exit(last_return_code);
Denis Vlasenko5f786c22007-04-20 08:35:45 +0000675 /* mimic bash: exit 123abc == exit 255 + error msg */
676 xfunc_error_retval = 255;
677 /* bash: exit -2 == exit 254, no error msg */
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000678 hush_exit(xatoi(argv[1]) & 0xff);
Eric Andersen25f27032001-04-26 23:22:31 +0000679}
680
681/* built-in 'export VAR=value' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000682static int builtin_export(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000683{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000684 int res = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000685 char *name = argv[1];
Eric Andersen25f27032001-04-26 23:22:31 +0000686
Eric Andersenf72f5622001-05-15 23:21:41 +0000687 if (name == NULL) {
Denis Vlasenkof2fffd02007-05-02 23:39:04 +0000688 // TODO:
689 // ash emits: export VAR='VAL'
690 // bash: declare -x VAR="VAL"
691 // (both also escape as needed (quotes, $, etc))
692 char **e = environ;
693 if (e)
694 while (*e)
695 puts(*e++);
696 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000697 }
Eric Andersenf72f5622001-05-15 23:21:41 +0000698
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000699 name = strdup(name);
Eric Andersenf72f5622001-05-15 23:21:41 +0000700
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000701 if (name) {
Denis Vlasenko15d78fb2007-01-30 22:28:21 +0000702 const char *value = strchr(name, '=');
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000703
Eric Andersen94ac2442001-05-22 19:05:18 +0000704 if (!value) {
705 char *tmp;
706 /* They are exporting something without an =VALUE */
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000707
Eric Andersen94ac2442001-05-22 19:05:18 +0000708 value = get_local_var(name);
709 if (value) {
710 size_t ln = strlen(name);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000711
Eric Andersen94ac2442001-05-22 19:05:18 +0000712 tmp = realloc(name, ln+strlen(value)+2);
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000713 if (tmp == NULL)
Eric Andersen94ac2442001-05-22 19:05:18 +0000714 res = -1;
715 else {
716 sprintf(tmp+ln, "=%s", value);
717 name = tmp;
718 }
719 } else {
720 /* bash does not return an error when trying to export
721 * an undefined variable. Do likewise. */
722 res = 1;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000723 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000724 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000725 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000726 if (res < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000727 bb_perror_msg("export");
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000728 else if (res == 0)
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000729 res = set_local_var(name, 1);
730 else
731 res = 0;
732 free(name);
733 return res;
Eric Andersen25f27032001-04-26 23:22:31 +0000734}
735
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000736#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +0000737/* built-in 'fg' and 'bg' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000738static int builtin_fg_bg(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000739{
Eric Andersen0fcd4472001-05-02 20:12:03 +0000740 int i, jobnum;
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000741 struct pipe *pi;
Eric Andersen25f27032001-04-26 23:22:31 +0000742
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000743 if (!interactive_fd)
Eric Andersenc798b072001-06-22 06:23:03 +0000744 return EXIT_FAILURE;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000745 /* If they gave us no args, assume they want the last backgrounded task */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000746 if (!argv[1]) {
Eric Andersenc798b072001-06-22 06:23:03 +0000747 for (pi = job_list; pi; pi = pi->next) {
748 if (pi->jobid == last_jobid) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000749 goto found;
Eric Andersen0fcd4472001-05-02 20:12:03 +0000750 }
751 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000752 bb_error_msg("%s: no current job", argv[0]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000753 return EXIT_FAILURE;
754 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000755 if (sscanf(argv[1], "%%%d", &jobnum) != 1) {
756 bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000757 return EXIT_FAILURE;
758 }
759 for (pi = job_list; pi; pi = pi->next) {
760 if (pi->jobid == jobnum) {
761 goto found;
Eric Andersen25f27032001-04-26 23:22:31 +0000762 }
763 }
Denis Vlasenko1359da62007-04-21 23:27:30 +0000764 bb_error_msg("%s: %d: no such job", argv[0], jobnum);
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000765 return EXIT_FAILURE;
766 found:
Denis Vlasenko52881e92007-04-21 13:42:52 +0000767 // TODO: bash prints a string representation
768 // of job being foregrounded (like "sleep 1 | cat")
Denis Vlasenko1359da62007-04-21 23:27:30 +0000769 if (*argv[0] == 'f') {
Eric Andersen028b65b2001-06-28 01:10:11 +0000770 /* Put the job into the foreground. */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +0000771 tcsetpgrp(interactive_fd, pi->pgrp);
Eric Andersen25f27032001-04-26 23:22:31 +0000772 }
773
774 /* Restart the processes in the job */
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000775 debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_progs, pi->pgrp);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000776 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +0000777 debug_printf_jobs("reviving pid %d\n", pi->progs[i].pid);
Eric Andersen0fcd4472001-05-02 20:12:03 +0000778 pi->progs[i].is_stopped = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000779 }
780 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +0000781
Denis Vlasenkobb81c582007-01-30 22:32:09 +0000782 i = kill(- pi->pgrp, SIGCONT);
783 if (i < 0) {
Denis Vlasenko8a28e622007-04-14 11:16:29 +0000784 if (errno == ESRCH) {
Denis Vlasenko1359da62007-04-21 23:27:30 +0000785 delete_finished_bg_job(pi);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000786 return EXIT_SUCCESS;
Eric Andersen028b65b2001-06-28 01:10:11 +0000787 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000788 bb_perror_msg("kill (SIGCONT)");
Eric Andersen028b65b2001-06-28 01:10:11 +0000789 }
790 }
Eric Andersen25f27032001-04-26 23:22:31 +0000791
Denis Vlasenko1359da62007-04-21 23:27:30 +0000792 if (*argv[0] == 'f') {
793 remove_bg_job(pi);
Denis Vlasenko52881e92007-04-21 13:42:52 +0000794 return checkjobs_and_fg_shell(pi);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000795 }
Eric Andersen25f27032001-04-26 23:22:31 +0000796 return EXIT_SUCCESS;
797}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000798#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000799
800/* built-in 'help' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000801static int builtin_help(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +0000802{
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +0000803 const struct built_in_command *x;
Eric Andersen25f27032001-04-26 23:22:31 +0000804
805 printf("\nBuilt-in commands:\n");
806 printf("-------------------\n");
807 for (x = bltins; x->cmd; x++) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000808 if (x->descr == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000809 continue;
810 printf("%s\t%s\n", x->cmd, x->descr);
811 }
812 printf("\n\n");
813 return EXIT_SUCCESS;
814}
815
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +0000816#if ENABLE_HUSH_JOB
Eric Andersen25f27032001-04-26 23:22:31 +0000817/* built-in 'jobs' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000818static int builtin_jobs(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +0000819{
820 struct pipe *job;
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000821 const char *status_string;
Eric Andersen25f27032001-04-26 23:22:31 +0000822
Eric Andersenc798b072001-06-22 06:23:03 +0000823 for (job = job_list; job; job = job->next) {
Eric Andersen25f27032001-04-26 23:22:31 +0000824 if (job->running_progs == job->stopped_progs)
825 status_string = "Stopped";
826 else
827 status_string = "Running";
Eric Andersen52a97ca2001-06-22 06:49:26 +0000828
Denis Vlasenko3ac0e002007-04-28 16:45:22 +0000829 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
Eric Andersen25f27032001-04-26 23:22:31 +0000830 }
831 return EXIT_SUCCESS;
832}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +0000833#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000834
Eric Andersen25f27032001-04-26 23:22:31 +0000835/* built-in 'pwd' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000836static int builtin_pwd(char **argv ATTRIBUTE_UNUSED)
Eric Andersen25f27032001-04-26 23:22:31 +0000837{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000838 puts(set_cwd());
Eric Andersen25f27032001-04-26 23:22:31 +0000839 return EXIT_SUCCESS;
840}
841
842/* built-in 'read VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000843static int builtin_read(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000844{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000845 int res;
Eric Andersen25f27032001-04-26 23:22:31 +0000846
Denis Vlasenko1359da62007-04-21 23:27:30 +0000847 if (argv[1]) {
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000848 char string[BUFSIZ];
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000849 char *var = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +0000850
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000851 string[0] = '\0'; /* In case stdin has only EOF */
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000852 /* read string */
853 fgets(string, sizeof(string), stdin);
854 chomp(string);
Denis Vlasenko1359da62007-04-21 23:27:30 +0000855 var = malloc(strlen(argv[1]) + strlen(string) + 2);
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000856 if (var) {
Denis Vlasenko1359da62007-04-21 23:27:30 +0000857 sprintf(var, "%s=%s", argv[1], string);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000858 res = set_local_var(var, 0);
859 } else
Eric Andersen94ac2442001-05-22 19:05:18 +0000860 res = -1;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000861 if (res)
Rob Landley5483de12006-06-20 21:35:26 +0000862 bb_perror_msg("read");
Eric Andersen94ac2442001-05-22 19:05:18 +0000863 free(var); /* So not move up to avoid breaking errno */
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000864 return res;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000865 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000866 do res = getchar(); while (res != '\n' && res != EOF);
867 return 0;
Eric Andersen25f27032001-04-26 23:22:31 +0000868}
869
Eric Andersenf72f5622001-05-15 23:21:41 +0000870/* built-in 'set VAR=value' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000871static int builtin_set(char **argv)
Eric Andersenf72f5622001-05-15 23:21:41 +0000872{
Denis Vlasenko1359da62007-04-21 23:27:30 +0000873 char *temp = argv[1];
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000874 struct variables *e;
Eric Andersenf72f5622001-05-15 23:21:41 +0000875
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000876 if (temp == NULL)
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000877 for (e = top_vars; e; e = e->next)
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000878 printf("%s=%s\n", e->name, e->value);
879 else
880 set_local_var(temp, 0);
881
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000882 return EXIT_SUCCESS;
Eric Andersenf72f5622001-05-15 23:21:41 +0000883}
884
885
Eric Andersen25f27032001-04-26 23:22:31 +0000886/* Built-in 'shift' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000887static int builtin_shift(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000888{
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000889 int n = 1;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000890 if (argv[1]) {
891 n = atoi(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +0000892 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000893 if (n >= 0 && n < global_argc) {
Eric Andersen25f27032001-04-26 23:22:31 +0000894 /* XXX This probably breaks $0 */
895 global_argc -= n;
896 global_argv += n;
897 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000898 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +0000899 return EXIT_FAILURE;
Eric Andersen25f27032001-04-26 23:22:31 +0000900}
901
902/* Built-in '.' handler (read-in and execute commands from file) */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000903static int builtin_source(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000904{
905 FILE *input;
906 int status;
907
Denis Vlasenko1359da62007-04-21 23:27:30 +0000908 if (argv[1] == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +0000909 return EXIT_FAILURE;
910
911 /* XXX search through $PATH is missing */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000912 input = fopen(argv[1], "r");
Eric Andersen25f27032001-04-26 23:22:31 +0000913 if (!input) {
Denis Vlasenko1359da62007-04-21 23:27:30 +0000914 bb_error_msg("cannot open '%s'", argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +0000915 return EXIT_FAILURE;
916 }
917
918 /* Now run the file */
919 /* XXX argv and argc are broken; need to save old global_argv
920 * (pointer only is OK!) on this stack frame,
Denis Vlasenko1359da62007-04-21 23:27:30 +0000921 * set global_argv=argv+1, recurse, and restore. */
Eric Andersen25f27032001-04-26 23:22:31 +0000922 mark_open(fileno(input));
923 status = parse_file_outer(input);
924 mark_closed(fileno(input));
925 fclose(input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000926 return status;
Eric Andersen25f27032001-04-26 23:22:31 +0000927}
928
Denis Vlasenko1359da62007-04-21 23:27:30 +0000929static int builtin_umask(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000930{
Eric Andersen83a2ae22001-05-07 17:59:25 +0000931 mode_t new_umask;
Denis Vlasenko1359da62007-04-21 23:27:30 +0000932 const char *arg = argv[1];
Eric Andersen83a2ae22001-05-07 17:59:25 +0000933 char *end;
934 if (arg) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000935 new_umask = strtoul(arg, &end, 8);
936 if (*end != '\0' || end == arg) {
Eric Andersen83a2ae22001-05-07 17:59:25 +0000937 return EXIT_FAILURE;
938 }
939 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000940 new_umask = umask(0);
941 printf("%.3o\n", (unsigned) new_umask);
Eric Andersen83a2ae22001-05-07 17:59:25 +0000942 }
943 umask(new_umask);
944 return EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +0000945}
946
947/* built-in 'unset VAR' handler */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000948static int builtin_unset(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +0000949{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +0000950 /* bash returned already true */
Denis Vlasenko1359da62007-04-21 23:27:30 +0000951 unset_local_var(argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +0000952 return EXIT_SUCCESS;
953}
954
Denis Vlasenko1359da62007-04-21 23:27:30 +0000955static int builtin_not_written(char **argv)
Eric Andersen83a2ae22001-05-07 17:59:25 +0000956{
Denis Vlasenko1359da62007-04-21 23:27:30 +0000957 printf("builtin_%s not written\n", argv[0]);
Eric Andersen83a2ae22001-05-07 17:59:25 +0000958 return EXIT_FAILURE;
959}
960
Eric Andersen25f27032001-04-26 23:22:31 +0000961static int b_check_space(o_string *o, int len)
962{
963 /* It would be easy to drop a more restrictive policy
964 * in here, such as setting a maximum string length */
965 if (o->length + len > o->maxlen) {
966 char *old_data = o->data;
Denis Vlasenkof5294e12007-04-14 10:09:57 +0000967 /* assert(data == NULL || o->maxlen != 0); */
Denis Vlasenkoef36ead2007-05-02 15:34:47 +0000968 o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK);
Eric Andersen25f27032001-04-26 23:22:31 +0000969 o->data = realloc(o->data, 1 + o->maxlen);
970 if (o->data == NULL) {
971 free(old_data);
972 }
973 }
974 return o->data == NULL;
975}
976
977static int b_addchr(o_string *o, int ch)
978{
979 debug_printf("b_addchr: %c %d %p\n", ch, o->length, o);
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000980 if (b_check_space(o, 1))
981 return B_NOSPAC;
Eric Andersen25f27032001-04-26 23:22:31 +0000982 o->data[o->length] = ch;
983 o->length++;
984 o->data[o->length] = '\0';
985 return 0;
986}
987
988static void b_reset(o_string *o)
989{
990 o->length = 0;
991 o->nonnull = 0;
Denis Vlasenko0c886c62007-01-30 22:30:09 +0000992 if (o->data != NULL)
993 *o->data = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +0000994}
995
996static void b_free(o_string *o)
997{
998 b_reset(o);
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000999 free(o->data);
Eric Andersen25f27032001-04-26 23:22:31 +00001000 o->data = NULL;
1001 o->maxlen = 0;
1002}
1003
1004/* My analysis of quoting semantics tells me that state information
1005 * is associated with a destination, not a source.
1006 */
1007static int b_addqchr(o_string *o, int ch, int quote)
1008{
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00001009 if (quote && strchr("*?[\\", ch)) {
Eric Andersen25f27032001-04-26 23:22:31 +00001010 int rc;
1011 rc = b_addchr(o, '\\');
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001012 if (rc)
1013 return rc;
Eric Andersen25f27032001-04-26 23:22:31 +00001014 }
1015 return b_addchr(o, ch);
1016}
1017
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001018static int b_adduint(o_string *o, unsigned i)
Eric Andersen25f27032001-04-26 23:22:31 +00001019{
1020 int r;
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00001021 char buf[sizeof(unsigned)*3 + 1];
1022 char *p = buf;
1023 *(utoa_to_buf(i, buf, sizeof(buf))) = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00001024 /* no escape checking necessary */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00001025 do r = b_addchr(o, *p++); while (r == 0 && *p);
Eric Andersen25f27032001-04-26 23:22:31 +00001026 return r;
1027}
1028
1029static int static_get(struct in_str *i)
1030{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001031 int ch = *i->p++;
1032 if (ch == '\0') return EOF;
Eric Andersen25f27032001-04-26 23:22:31 +00001033 return ch;
1034}
1035
1036static int static_peek(struct in_str *i)
1037{
1038 return *i->p;
1039}
1040
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001041#if ENABLE_HUSH_INTERACTIVE
Rob Landley88621d72006-08-29 19:41:06 +00001042static void cmdedit_set_initial_prompt(void)
Eric Andersen25f27032001-04-26 23:22:31 +00001043{
Denis Vlasenko38f63192007-01-22 09:03:07 +00001044#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001045 PS1 = NULL;
1046#else
1047 PS1 = getenv("PS1");
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001048 if (PS1 == NULL)
Eric Andersen25f27032001-04-26 23:22:31 +00001049 PS1 = "\\w \\$ ";
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001050#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001051}
1052
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001053static const char* setup_prompt_string(int promptmode)
Eric Andersen25f27032001-04-26 23:22:31 +00001054{
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001055 const char *prompt_str;
1056 debug_printf("setup_prompt_string %d ", promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001057#if !ENABLE_FEATURE_EDITING_FANCY_PROMPT
Eric Andersen25f27032001-04-26 23:22:31 +00001058 /* Set up the prompt */
1059 if (promptmode == 1) {
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001060 char *ns;
1061 free((char*)PS1);
1062 ns = xmalloc(strlen(cwd)+4);
1063 sprintf(ns, "%s %s", cwd, (geteuid() != 0) ? "$ " : "# ");
1064 prompt_str = ns;
1065 PS1 = ns;
Eric Andersen25f27032001-04-26 23:22:31 +00001066 } else {
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001067 prompt_str = PS2;
Eric Andersen25f27032001-04-26 23:22:31 +00001068 }
1069#else
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001070 prompt_str = (promptmode == 1) ? PS1 : PS2;
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001071#endif
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001072 debug_printf("result %s\n", prompt_str);
1073 return prompt_str;
Eric Andersen25f27032001-04-26 23:22:31 +00001074}
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001075#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001076
Denis Vlasenko38f63192007-01-22 09:03:07 +00001077#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00001078static line_input_t *line_input_state;
1079#endif
1080
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001081#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0937be52007-04-28 16:47:08 +00001082static int get_user_input(struct in_str *i)
Eric Andersen25f27032001-04-26 23:22:31 +00001083{
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001084 static char the_command[ENABLE_FEATURE_EDITING ? BUFSIZ : 2];
1085
Denis Vlasenko0937be52007-04-28 16:47:08 +00001086 int r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001087 const char *prompt_str;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00001088 prompt_str = setup_prompt_string(i->promptmode);
Denis Vlasenko38f63192007-01-22 09:03:07 +00001089#if ENABLE_FEATURE_EDITING
Eric Andersen25f27032001-04-26 23:22:31 +00001090 /*
1091 ** enable command line editing only while a command line
1092 ** is actually being read; otherwise, we'll end up bequeathing
1093 ** atexit() handlers and other unwanted stuff to our
1094 ** child processes (rob@sysgo.de)
1095 */
Denis Vlasenko0937be52007-04-28 16:47:08 +00001096 r = read_line_input(prompt_str, the_command, BUFSIZ, line_input_state);
Eric Andersen25f27032001-04-26 23:22:31 +00001097#else
1098 fputs(prompt_str, stdout);
1099 fflush(stdout);
Denis Vlasenko0937be52007-04-28 16:47:08 +00001100 the_command[0] = r = fgetc(i->file);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001101 the_command[1] = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00001102#endif
Eric Andersen4f6753e2001-05-31 17:17:12 +00001103 fflush(stdout);
Eric Andersen25f27032001-04-26 23:22:31 +00001104 i->p = the_command;
Denis Vlasenko0937be52007-04-28 16:47:08 +00001105 return r; /* < 0 == EOF. Not meaningful otherwise */
Eric Andersen25f27032001-04-26 23:22:31 +00001106}
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001107#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001108
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001109/* This is the magic location that prints prompts
Eric Andersen25f27032001-04-26 23:22:31 +00001110 * and gets data back from the user */
1111static int file_get(struct in_str *i)
1112{
1113 int ch;
1114
1115 ch = 0;
1116 /* If there is data waiting, eat it up */
1117 if (i->p && *i->p) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001118 ch = *i->p++;
Eric Andersen25f27032001-04-26 23:22:31 +00001119 } else {
1120 /* need to double check i->file because we might be doing something
1121 * more complicated by now, like sourcing or substituting. */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001122#if ENABLE_HUSH_INTERACTIVE
1123 if (interactive_fd && i->__promptme && i->file == stdin) {
Denis Vlasenko0937be52007-04-28 16:47:08 +00001124 while (!i->p || !(interactive_fd && i->p[0])) {
1125 if (get_user_input(i) < 0)
1126 return EOF;
Eric Andersen4f6753e2001-05-31 17:17:12 +00001127 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001128 i->promptmode = 2;
Eric Andersene67c3ce2001-05-02 02:09:36 +00001129 i->__promptme = 0;
1130 if (i->p && *i->p) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001131 ch = *i->p++;
Eric Andersene67c3ce2001-05-02 02:09:36 +00001132 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001133 } else
1134#endif
1135 {
Eric Andersene67c3ce2001-05-02 02:09:36 +00001136 ch = fgetc(i->file);
Eric Andersen25f27032001-04-26 23:22:31 +00001137 }
Eric Andersen25f27032001-04-26 23:22:31 +00001138 debug_printf("b_getch: got a %d\n", ch);
1139 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001140#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001141 if (ch == '\n')
1142 i->__promptme = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001143#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001144 return ch;
1145}
1146
1147/* All the callers guarantee this routine will never be
1148 * used right after a newline, so prompting is not needed.
1149 */
1150static int file_peek(struct in_str *i)
1151{
1152 if (i->p && *i->p) {
1153 return *i->p;
Eric Andersen25f27032001-04-26 23:22:31 +00001154 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001155 i->peek_buf[0] = fgetc(i->file);
1156 i->peek_buf[1] = '\0';
1157 i->p = i->peek_buf;
1158 debug_printf("b_peek: got a %d\n", *i->p);
1159 return *i->p;
Eric Andersen25f27032001-04-26 23:22:31 +00001160}
1161
1162static void setup_file_in_str(struct in_str *i, FILE *f)
1163{
1164 i->peek = file_peek;
1165 i->get = file_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001166#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001167 i->__promptme = 1;
1168 i->promptmode = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001169#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001170 i->file = f;
1171 i->p = NULL;
1172}
1173
1174static void setup_string_in_str(struct in_str *i, const char *s)
1175{
1176 i->peek = static_peek;
1177 i->get = static_get;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001178#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkobb81c582007-01-30 22:32:09 +00001179 i->__promptme = 1;
1180 i->promptmode = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001181#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001182 i->p = s;
1183}
1184
1185static void mark_open(int fd)
1186{
1187 struct close_me *new = xmalloc(sizeof(struct close_me));
1188 new->fd = fd;
1189 new->next = close_me_head;
1190 close_me_head = new;
1191}
1192
1193static void mark_closed(int fd)
1194{
1195 struct close_me *tmp;
1196 if (close_me_head == NULL || close_me_head->fd != fd)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001197 bb_error_msg_and_die("corrupt close_me");
Eric Andersen25f27032001-04-26 23:22:31 +00001198 tmp = close_me_head;
1199 close_me_head = close_me_head->next;
1200 free(tmp);
1201}
1202
Eric Anderseneaecbf32001-10-31 10:41:31 +00001203static void close_all(void)
Eric Andersen25f27032001-04-26 23:22:31 +00001204{
1205 struct close_me *c;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001206 for (c = close_me_head; c; c = c->next) {
Eric Andersen25f27032001-04-26 23:22:31 +00001207 close(c->fd);
1208 }
1209 close_me_head = NULL;
1210}
1211
1212/* squirrel != NULL means we squirrel away copies of stdin, stdout,
1213 * and stderr if they are redirected. */
1214static int setup_redirects(struct child_prog *prog, int squirrel[])
1215{
1216 int openfd, mode;
1217 struct redir_struct *redir;
1218
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001219 for (redir = prog->redirects; redir; redir = redir->next) {
Eric Andersen817e73c2001-06-06 17:56:09 +00001220 if (redir->dup == -1 && redir->word.gl_pathv == NULL) {
1221 /* something went wrong in the parse. Pretend it didn't happen */
1222 continue;
1223 }
Eric Andersen25f27032001-04-26 23:22:31 +00001224 if (redir->dup == -1) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001225 mode = redir_table[redir->type].mode;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001226 openfd = open_or_warn(redir->word.gl_pathv[0], mode);
Eric Andersen25f27032001-04-26 23:22:31 +00001227 if (openfd < 0) {
1228 /* this could get lost if stderr has been redirected, but
1229 bash and ash both lose it as well (though zsh doesn't!) */
Eric Andersen25f27032001-04-26 23:22:31 +00001230 return 1;
1231 }
1232 } else {
1233 openfd = redir->dup;
1234 }
1235
1236 if (openfd != redir->fd) {
1237 if (squirrel && redir->fd < 3) {
1238 squirrel[redir->fd] = dup(redir->fd);
1239 }
Eric Andersen83a2ae22001-05-07 17:59:25 +00001240 if (openfd == -3) {
1241 close(openfd);
1242 } else {
1243 dup2(openfd, redir->fd);
Matt Kraaic616e532001-06-05 16:50:08 +00001244 if (redir->dup == -1)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001245 close(openfd);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001246 }
Eric Andersen25f27032001-04-26 23:22:31 +00001247 }
1248 }
1249 return 0;
1250}
1251
1252static void restore_redirects(int squirrel[])
1253{
1254 int i, fd;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001255 for (i = 0; i < 3; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001256 fd = squirrel[i];
1257 if (fd != -1) {
1258 /* No error checking. I sure wouldn't know what
1259 * to do with an error if I found one! */
1260 dup2(fd, i);
1261 close(fd);
1262 }
1263 }
1264}
1265
Eric Andersenada18ff2001-05-21 16:18:22 +00001266/* never returns */
Eric Andersen94ac2442001-05-22 19:05:18 +00001267/* XXX no exit() here. If you don't exec, use _exit instead.
1268 * The at_exit handlers apparently confuse the calling process,
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001269 * in particular stdin handling. Not sure why? -- because of vfork! (vda) */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001270static void pseudo_exec_argv(char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00001271{
Eric Andersen78a7c992001-05-15 16:30:25 +00001272 int i, rcode;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001273 char *p;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001274 const struct built_in_command *x;
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001275
Denis Vlasenko1359da62007-04-21 23:27:30 +00001276 for (i = 0; is_assignment(argv[i]); i++) {
1277 debug_printf("pid %d environment modification: %s\n",
1278 getpid(), argv[i]);
1279 // FIXME: vfork case??
1280 p = insert_var_value(argv[i]);
1281 putenv(strdup(p));
1282 if (p != argv[i])
1283 free(p);
1284 }
1285 argv += i;
1286 /* If a variable is assigned in a forest, and nobody listens,
1287 * was it ever really set?
1288 */
1289 if (argv[0] == NULL) {
1290 _exit(EXIT_SUCCESS);
1291 }
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001292
Denis Vlasenko1359da62007-04-21 23:27:30 +00001293 /*
1294 * Check if the command matches any of the builtins.
1295 * Depending on context, this might be redundant. But it's
1296 * easier to waste a few CPU cycles than it is to figure out
1297 * if this is one of those cases.
1298 */
1299 for (x = bltins; x->cmd; x++) {
1300 if (strcmp(argv[0], x->cmd) == 0) {
1301 debug_printf("builtin exec %s\n", argv[0]);
1302 rcode = x->function(argv);
1303 fflush(stdout);
1304 _exit(rcode);
Eric Andersen94ac2442001-05-22 19:05:18 +00001305 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001306 }
Eric Andersen78a7c992001-05-15 16:30:25 +00001307
Denis Vlasenko1359da62007-04-21 23:27:30 +00001308 /* Check if the command matches any busybox internal commands
1309 * ("applets") here.
1310 * FIXME: This feature is not 100% safe, since
1311 * BusyBox is not fully reentrant, so we have no guarantee the things
1312 * from the .bss are still zeroed, or that things from .data are still
1313 * at their defaults. We could exec ourself from /proc/self/exe, but I
1314 * really dislike relying on /proc for things. We could exec ourself
1315 * from global_argv[0], but if we are in a chroot, we may not be able
1316 * to find ourself... */
Denis Vlasenko80d14be2007-04-10 23:03:30 +00001317#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko1359da62007-04-21 23:27:30 +00001318 debug_printf("running applet %s\n", argv[0]);
1319 run_applet_and_exit(argv[0], argv);
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001320// is it ok that run_applet_and_exit() does exit(), not _exit()?
1321// NB: IIRC on NOMMU we are after _vfork_, not fork!
Eric Andersenaac75e52001-04-30 18:18:45 +00001322#endif
Denis Vlasenko1359da62007-04-21 23:27:30 +00001323 debug_printf("exec of %s\n", argv[0]);
1324 execvp(argv[0], argv);
1325 bb_perror_msg("cannot exec '%s'", argv[0]);
1326 _exit(1);
1327}
1328
1329static void pseudo_exec(struct child_prog *child)
1330{
Denis Vlasenkof2fffd02007-05-02 23:39:04 +00001331// FIXME: buggy wrt NOMMU! Must not modify any global data
1332// until it does exec/_exit, but currently it does.
Denis Vlasenko1359da62007-04-21 23:27:30 +00001333 int rcode;
1334
1335 if (child->argv) {
1336 pseudo_exec_argv(child->argv);
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001337 }
1338
1339 if (child->group) {
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00001340 // FIXME: do not modify globals! Think vfork!
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001341#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001342 interactive_fd = 0; /* crucial!!!! */
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001343#endif
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001344 debug_printf_exec("pseudo_exec: run_list_real\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001345 rcode = run_list_real(child->group);
Eric Andersenbf7df042001-05-23 22:18:35 +00001346 /* OK to leak memory by not calling free_pipe_list,
Eric Andersen25f27032001-04-26 23:22:31 +00001347 * since this process is about to exit */
Eric Andersen94ac2442001-05-22 19:05:18 +00001348 _exit(rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00001349 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001350
1351 /* Can happen. See what bash does with ">foo" by itself. */
1352 debug_printf("trying to pseudo_exec null command\n");
1353 _exit(EXIT_SUCCESS);
Eric Andersen25f27032001-04-26 23:22:31 +00001354}
1355
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001356#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001357static const char *get_cmdtext(struct pipe *pi)
1358{
1359 char **argv;
1360 char *p;
1361 int len;
1362
1363 /* This is subtle. ->cmdtext is created only on first backgrounding.
1364 * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...)
1365 * On subsequent bg argv can be trashed, but we won't use it */
1366 if (pi->cmdtext)
1367 return pi->cmdtext;
1368 argv = pi->progs[0].argv;
1369 if (!argv || !argv[0])
1370 return (pi->cmdtext = xzalloc(1));
1371
1372 len = 0;
1373 do len += strlen(*argv) + 1; while (*++argv);
1374 pi->cmdtext = p = xmalloc(len);
1375 argv = pi->progs[0].argv;
1376 do {
1377 len = strlen(*argv);
1378 memcpy(p, *argv, len);
1379 p += len;
1380 *p++ = ' ';
1381 } while (*++argv);
1382 p[-1] = '\0';
1383 return pi->cmdtext;
1384}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001385#endif
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001386
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001387#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00001388static void insert_bg_job(struct pipe *pi)
1389{
1390 struct pipe *thejob;
1391
1392 /* Linear search for the ID of the job to use */
1393 pi->jobid = 1;
Eric Andersenc798b072001-06-22 06:23:03 +00001394 for (thejob = job_list; thejob; thejob = thejob->next)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001395 if (thejob->jobid >= pi->jobid)
1396 pi->jobid = thejob->jobid + 1;
1397
1398 /* add thejob to the list of running jobs */
Eric Andersenc798b072001-06-22 06:23:03 +00001399 if (!job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001400 thejob = job_list = xmalloc(sizeof(*thejob));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001401 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001402 for (thejob = job_list; thejob->next; thejob = thejob->next)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001403 continue;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001404 thejob->next = xmalloc(sizeof(*thejob));
1405 thejob = thejob->next;
1406 }
1407
1408 /* physically copy the struct job */
Eric Andersen0fcd4472001-05-02 20:12:03 +00001409 memcpy(thejob, pi, sizeof(struct pipe));
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001410 thejob->progs = xmalloc(sizeof(pi->progs[0]) * pi->num_progs);
1411 memcpy(thejob->progs, pi->progs, sizeof(pi->progs[0]) * pi->num_progs);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001412 thejob->next = NULL;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001413 /*seems to be wrong:*/
1414 /*thejob->running_progs = thejob->num_progs;*/
1415 /*thejob->stopped_progs = 0;*/
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001416 thejob->cmdtext = xstrdup(get_cmdtext(pi));
Eric Andersenbafd94f2001-05-02 16:11:59 +00001417
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001418 /* we don't wait for background thejobs to return -- append it
Eric Andersenbafd94f2001-05-02 16:11:59 +00001419 to the list of backgrounded thejobs and leave it alone */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001420 printf("[%d] %d %s\n", thejob->jobid, thejob->progs[0].pid, thejob->cmdtext);
Eric Andersen1a6d39b2001-05-08 05:11:54 +00001421 last_bg_pid = thejob->progs[0].pid;
Eric Andersenc798b072001-06-22 06:23:03 +00001422 last_jobid = thejob->jobid;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001423}
1424
Eric Andersenbafd94f2001-05-02 16:11:59 +00001425static void remove_bg_job(struct pipe *pi)
1426{
1427 struct pipe *prev_pipe;
1428
Eric Andersenc798b072001-06-22 06:23:03 +00001429 if (pi == job_list) {
Eric Andersen52a97ca2001-06-22 06:49:26 +00001430 job_list = pi->next;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001431 } else {
Eric Andersenc798b072001-06-22 06:23:03 +00001432 prev_pipe = job_list;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001433 while (prev_pipe->next != pi)
1434 prev_pipe = prev_pipe->next;
1435 prev_pipe->next = pi->next;
1436 }
Eric Andersen028b65b2001-06-28 01:10:11 +00001437 if (job_list)
1438 last_jobid = job_list->jobid;
1439 else
1440 last_jobid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001441}
Eric Andersen028b65b2001-06-28 01:10:11 +00001442
Denis Vlasenko1359da62007-04-21 23:27:30 +00001443/* remove a backgrounded job */
1444static void delete_finished_bg_job(struct pipe *pi)
1445{
1446 remove_bg_job(pi);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001447 pi->stopped_progs = 0;
Eric Andersenbf7df042001-05-23 22:18:35 +00001448 free_pipe(pi, 0);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001449 free(pi);
1450}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001451#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001452
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001453/* Checks to see if any processes have exited -- if they
Eric Andersenbafd94f2001-05-02 16:11:59 +00001454 have, figure out why and see if a job has completed */
Eric Andersenc798b072001-06-22 06:23:03 +00001455static int checkjobs(struct pipe* fg_pipe)
Eric Andersenbafd94f2001-05-02 16:11:59 +00001456{
Eric Andersenc798b072001-06-22 06:23:03 +00001457 int attributes;
1458 int status;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001459#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00001460 int prognum = 0;
1461 struct pipe *pi;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001462#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001463 pid_t childpid;
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001464 int rcode = 0;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001465
Eric Andersenc798b072001-06-22 06:23:03 +00001466 attributes = WUNTRACED;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001467 if (fg_pipe == NULL) {
Eric Andersenc798b072001-06-22 06:23:03 +00001468 attributes |= WNOHANG;
1469 }
1470
Denis Vlasenko1359da62007-04-21 23:27:30 +00001471/* Do we do this right?
1472 * bash-3.00# sleep 20 | false
1473 * <Ctrl-Z pressed>
1474 * [3]+ Stopped sleep 20 | false
1475 * bash-3.00# echo $?
1476 * 1 <========== bg pipe is not fully done, but exitcode is already known!
1477 */
1478
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001479//FIXME: non-interactive bash does not continue even if all processes in fg pipe
1480//are stopped. Testcase: "cat | cat" in a script (not on command line)
1481// + killall -STOP cat
1482
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001483 wait_more:
Eric Andersenc798b072001-06-22 06:23:03 +00001484 while ((childpid = waitpid(-1, &status, attributes)) > 0) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001485 const int dead = WIFEXITED(status) || WIFSIGNALED(status);
1486
1487#ifdef DEBUG_SHELL_JOBS
1488 if (WIFSTOPPED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001489 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001490 childpid, WSTOPSIG(status), WEXITSTATUS(status));
1491 if (WIFSIGNALED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001492 debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001493 childpid, WTERMSIG(status), WEXITSTATUS(status));
1494 if (WIFEXITED(status))
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001495 debug_printf_jobs("pid %d exited, exitcode %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001496 childpid, WEXITSTATUS(status));
1497#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001498 /* Were we asked to wait for fg pipe? */
Eric Andersenc798b072001-06-22 06:23:03 +00001499 if (fg_pipe) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001500 int i;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001501 for (i = 0; i < fg_pipe->num_progs; i++) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001502 debug_printf_jobs("check pid %d\n", fg_pipe->progs[i].pid);
Eric Andersenc798b072001-06-22 06:23:03 +00001503 if (fg_pipe->progs[i].pid == childpid) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001504 /* printf("process %d exit %d\n", i, WEXITSTATUS(status)); */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001505 if (dead) {
1506 fg_pipe->progs[i].pid = 0;
1507 fg_pipe->running_progs--;
1508 if (i == fg_pipe->num_progs-1)
1509 /* last process gives overall exitstatus */
1510 rcode = WEXITSTATUS(status);
1511 } else {
1512 fg_pipe->progs[i].is_stopped = 1;
1513 fg_pipe->stopped_progs++;
1514 }
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001515 debug_printf_jobs("fg_pipe: running_progs %d stopped_progs %d\n",
Denis Vlasenko1359da62007-04-21 23:27:30 +00001516 fg_pipe->running_progs, fg_pipe->stopped_progs);
1517 if (fg_pipe->running_progs - fg_pipe->stopped_progs <= 0) {
1518 /* All processes in fg pipe have exited/stopped */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001519#if ENABLE_HUSH_JOB
Denis Vlasenko1359da62007-04-21 23:27:30 +00001520 if (fg_pipe->running_progs)
1521 insert_bg_job(fg_pipe);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001522#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001523 return rcode;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001524 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001525 /* There are still running processes in the fg pipe */
1526 goto wait_more;
Eric Andersenc798b072001-06-22 06:23:03 +00001527 }
1528 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001529 /* fall through to searching process in bg pipes */
Eric Andersenc798b072001-06-22 06:23:03 +00001530 }
1531
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001532#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001533 /* We asked to wait for bg or orphaned children */
1534 /* No need to remember exitcode in this case */
Eric Andersenc798b072001-06-22 06:23:03 +00001535 for (pi = job_list; pi; pi = pi->next) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001536 prognum = 0;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001537 while (prognum < pi->num_progs) {
1538 if (pi->progs[prognum].pid == childpid)
1539 goto found_pi_and_prognum;
Eric Andersen52a97ca2001-06-22 06:49:26 +00001540 prognum++;
1541 }
Eric Andersenbafd94f2001-05-02 16:11:59 +00001542 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001543#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001544
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001545 /* Happens when shell is used as init process (init=/bin/sh) */
1546 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001547 goto wait_more;
Eric Andersenaeb44c42001-05-22 20:29:00 +00001548
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001549#if ENABLE_HUSH_JOB
Denis Vlasenko5f786c22007-04-20 08:35:45 +00001550 found_pi_and_prognum:
Denis Vlasenko1359da62007-04-21 23:27:30 +00001551 if (dead) {
Eric Andersenbafd94f2001-05-02 16:11:59 +00001552 /* child exited */
Eric Andersenbafd94f2001-05-02 16:11:59 +00001553 pi->progs[prognum].pid = 0;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001554 pi->running_progs--;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001555 if (!pi->running_progs) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001556 printf(JOB_STATUS_FORMAT, pi->jobid,
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001557 "Done", pi->cmdtext);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001558 delete_finished_bg_job(pi);
Eric Andersenbafd94f2001-05-02 16:11:59 +00001559 }
1560 } else {
1561 /* child stopped */
1562 pi->stopped_progs++;
1563 pi->progs[prognum].is_stopped = 1;
Eric Andersenbafd94f2001-05-02 16:11:59 +00001564 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001565#endif
Eric Andersenbafd94f2001-05-02 16:11:59 +00001566 }
1567
Denis Vlasenko1359da62007-04-21 23:27:30 +00001568 /* wait found no children or failed */
1569
1570 if (childpid && errno != ECHILD)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001571 bb_perror_msg("waitpid");
Matt Kraai80abc452001-05-02 21:48:17 +00001572
Eric Andersenbafd94f2001-05-02 16:11:59 +00001573 /* move the shell to the foreground */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001574 //if (interactive_fd && tcsetpgrp(interactive_fd, getpgid(0)))
Manuel Novoa III cad53642003-03-19 09:13:01 +00001575 // bb_perror_msg("tcsetpgrp-2");
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001576 return rcode;
Eric Andersenada18ff2001-05-21 16:18:22 +00001577}
1578
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001579#if ENABLE_HUSH_JOB
Denis Vlasenko52881e92007-04-21 13:42:52 +00001580static int checkjobs_and_fg_shell(struct pipe* fg_pipe)
1581{
1582 pid_t p;
1583 int rcode = checkjobs(fg_pipe);
1584 /* Job finished, move the shell to the foreground */
1585 p = getpgid(0);
1586 debug_printf("fg'ing ourself: getpgid(0)=%d\n", (int)p);
1587 if (tcsetpgrp(interactive_fd, p) && errno != ENOTTY)
1588 bb_perror_msg("tcsetpgrp-4a");
1589 return rcode;
1590}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001591#endif
Denis Vlasenko52881e92007-04-21 13:42:52 +00001592
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001593#if ENABLE_FEATURE_SH_STANDALONE
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001594/* run_pipe_real's helper */
1595static int run_single_fg_nofork(struct pipe *pi, const struct bb_applet *a,
1596 char **argv)
1597{
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001598#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001599 int rcode;
1600 /* TSTP handler will store pid etc in pi */
1601 nofork_pipe = pi;
1602 save_nofork_data(&nofork_save);
1603 if (sigsetjmp(nofork_jb, 1) == 0) {
1604 signal_SA_RESTART(SIGTSTP, handler_ctrl_z);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +00001605 signal(SIGINT, handler_ctrl_c);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001606 rcode = run_nofork_applet_prime(&nofork_save, a, argv);
1607 if (--nofork_save.saved != 0) {
1608 /* Ctrl-Z forked, we are child */
1609 exit(rcode);
1610 }
1611 return rcode;
1612 }
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +00001613 /* Ctrl-Z forked, we are parent; or Ctrl-C.
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001614 * Sighandler has longjmped us here */
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +00001615 signal(SIGINT, SIG_IGN);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001616 signal(SIGTSTP, SIG_IGN);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001617 debug_printf_jobs("Exiting nofork early\n");
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001618 restore_nofork_data(&nofork_save);
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +00001619 if (nofork_save.saved == 0) /* Ctrl-Z, not Ctrl-C */
1620 insert_bg_job(pi);
1621 else
1622 putchar('\n'); /* bash does this on Ctrl-C */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001623 return 0;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001624#else
1625 return run_nofork_applet(a, argv);
1626#endif
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001627}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001628#endif
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001629
Eric Andersen25f27032001-04-26 23:22:31 +00001630/* run_pipe_real() starts all the jobs, but doesn't wait for anything
Eric Andersenc798b072001-06-22 06:23:03 +00001631 * to finish. See checkjobs().
Eric Andersen25f27032001-04-26 23:22:31 +00001632 *
1633 * return code is normally -1, when the caller has to wait for children
1634 * to finish to determine the exit status of the pipe. If the pipe
1635 * is a simple builtin command, however, the action is done by the
1636 * time run_pipe_real returns, and the exit code is provided as the
1637 * return value.
1638 *
1639 * The input of the pipe is always stdin, the output is always
1640 * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
1641 * because it tries to avoid running the command substitution in
1642 * subshell, when that is in fact necessary. The subshell process
1643 * now has its stdout directed to the input of the appropriate pipe,
1644 * so this routine is noticeably simpler.
1645 */
1646static int run_pipe_real(struct pipe *pi)
1647{
1648 int i;
1649 int nextin, nextout;
1650 int pipefds[2]; /* pipefds[0] is for reading */
Rob Landley53702e52006-07-19 21:43:53 +00001651 struct child_prog *child;
"Vladimir N. Oleynik"485d7cb2005-10-17 09:48:57 +00001652 const struct built_in_command *x;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001653 char *p;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001654 /* it is not always needed, but we aim to smaller code */
1655 int squirrel[] = { -1, -1, -1 };
1656 int rcode;
Denis Vlasenko52881e92007-04-21 13:42:52 +00001657 const int single_fg = (pi->num_progs == 1 && pi->followup != PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00001658
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001659 debug_printf_exec("run_pipe_real start:\n");
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001660
Eric Andersen25f27032001-04-26 23:22:31 +00001661 nextin = 0;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001662#if ENABLE_HUSH_JOB
Eric Andersenada18ff2001-05-21 16:18:22 +00001663 pi->pgrp = -1;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001664#endif
Denis Vlasenko1359da62007-04-21 23:27:30 +00001665 pi->running_progs = 0;
1666 pi->stopped_progs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001667
1668 /* Check if this is a simple builtin (not part of a pipe).
1669 * Builtins within pipes have to fork anyway, and are handled in
1670 * pseudo_exec. "echo foo | read bar" doesn't work on bash, either.
1671 */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001672 child = &(pi->progs[0]);
Denis Vlasenko52881e92007-04-21 13:42:52 +00001673 if (single_fg && child->group && child->subshell == 0) {
Eric Andersen04407e52001-06-07 16:42:05 +00001674 debug_printf("non-subshell grouping\n");
1675 setup_redirects(child, squirrel);
1676 /* XXX could we merge code with following builtin case,
1677 * by creating a pseudo builtin that calls run_list_real? */
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001678 debug_printf_exec(": run_list_real\n");
Eric Andersen04407e52001-06-07 16:42:05 +00001679 rcode = run_list_real(child->group);
1680 restore_redirects(squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001681 debug_printf_exec("run_pipe_real return %d\n", rcode);
Eric Andersen04407e52001-06-07 16:42:05 +00001682 return rcode;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001683 }
1684
Denis Vlasenko1359da62007-04-21 23:27:30 +00001685 if (single_fg && child->argv != NULL) {
1686 char **argv = child->argv;
1687
1688 for (i = 0; is_assignment(argv[i]); i++)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001689 continue;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001690 if (i != 0 && argv[i] == NULL) {
Eric Andersen78a7c992001-05-15 16:30:25 +00001691 /* assignments, but no command: set the local environment */
Denis Vlasenko1359da62007-04-21 23:27:30 +00001692 for (i = 0; argv[i] != NULL; i++) {
Eric Andersen99785762001-05-22 21:37:48 +00001693 /* Ok, this case is tricky. We have to decide if this is a
1694 * local variable, or an already exported variable. If it is
1695 * already exported, we have to export the new value. If it is
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001696 * not exported, we need only set this as a local variable.
Eric Andersen99785762001-05-22 21:37:48 +00001697 * This junk is all to decide whether or not to export this
1698 * variable. */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001699 int export_me = 0;
Eric Andersen99785762001-05-22 21:37:48 +00001700 char *name, *value;
Denis Vlasenko1359da62007-04-21 23:27:30 +00001701 name = xstrdup(argv[i]);
Eric Andersen04407e52001-06-07 16:42:05 +00001702 debug_printf("Local environment set: %s\n", name);
Eric Andersen99785762001-05-22 21:37:48 +00001703 value = strchr(name, '=');
1704 if (value)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001705 *value = 0;
1706 if (get_local_var(name)) {
1707 export_me = 1;
Eric Andersen99785762001-05-22 21:37:48 +00001708 }
1709 free(name);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001710 p = insert_var_value(argv[i]);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001711 set_local_var(p, export_me);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001712 if (p != argv[i])
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001713 free(p);
Eric Andersen78a7c992001-05-15 16:30:25 +00001714 }
1715 return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */
1716 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001717 for (i = 0; is_assignment(argv[i]); i++) {
1718 p = insert_var_value(argv[i]);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001719 putenv(strdup(p));
Denis Vlasenko1359da62007-04-21 23:27:30 +00001720 if (p != argv[i]) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001721 child->sp--;
1722 free(p);
1723 }
1724 }
1725 if (child->sp) {
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001726 char *str;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001727
Denis Vlasenko1359da62007-04-21 23:27:30 +00001728 str = make_string(argv + i);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001729 debug_printf_exec(": parse_string_outer '%s'\n", str);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001730 parse_string_outer(str, FLAG_EXIT_FROM_LOOP | FLAG_REPARSING);
1731 free(str);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001732 debug_printf_exec("run_pipe_real return %d\n", last_return_code);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001733 return last_return_code;
1734 }
Eric Andersen25f27032001-04-26 23:22:31 +00001735 for (x = bltins; x->cmd; x++) {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001736 if (strcmp(argv[i], x->cmd) == 0) {
1737 if (x->function == builtin_exec && argv[i+1] == NULL) {
Eric Andersen83a2ae22001-05-07 17:59:25 +00001738 debug_printf("magic exec\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001739 setup_redirects(child, NULL);
Eric Andersen83a2ae22001-05-07 17:59:25 +00001740 return EXIT_SUCCESS;
1741 }
Denis Vlasenko1359da62007-04-21 23:27:30 +00001742 debug_printf("builtin inline %s\n", argv[0]);
Eric Andersen25f27032001-04-26 23:22:31 +00001743 /* XXX setup_redirects acts on file descriptors, not FILEs.
1744 * This is perfect for work that comes after exec().
1745 * Is it really safe for inline use? Experimentally,
1746 * things seem to work with glibc. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001747// TODO: fflush(NULL)?
Eric Andersen25f27032001-04-26 23:22:31 +00001748 setup_redirects(child, squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001749 debug_printf_exec(": builtin '%s' '%s'...\n", x->cmd, argv[i+1]);
Denis Vlasenko1359da62007-04-21 23:27:30 +00001750 rcode = x->function(argv + i);
Eric Andersen25f27032001-04-26 23:22:31 +00001751 restore_redirects(squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001752 debug_printf_exec("run_pipe_real return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00001753 return rcode;
1754 }
1755 }
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001756#if ENABLE_FEATURE_SH_STANDALONE
1757 {
Denis Vlasenko1359da62007-04-21 23:27:30 +00001758 const struct bb_applet *a = find_applet_by_name(argv[i]);
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001759 if (a && a->nofork) {
1760 setup_redirects(child, squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001761 debug_printf_exec(": run_single_fg_nofork '%s' '%s'...\n", argv[i], argv[i+1]);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001762 rcode = run_single_fg_nofork(pi, a, argv + i);
1763 restore_redirects(squirrel);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001764 debug_printf_exec("run_pipe_real return %d\n", rcode);
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001765 return rcode;
Denis Vlasenkof5294e12007-04-14 10:09:57 +00001766 }
1767 }
1768#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001769 }
1770
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001771 /* Going to fork a child per each pipe member */
1772
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001773 /* Disable job control signals for shell (parent) and
1774 * for initial child code after fork */
1775 set_jobctrl_sighandler(SIG_IGN);
1776
Eric Andersen25f27032001-04-26 23:22:31 +00001777 for (i = 0; i < pi->num_progs; i++) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001778 child = &(pi->progs[i]);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001779 debug_printf_exec(": pipe member '%s' '%s'...\n", child->argv[0], child->argv[1]);
Eric Andersen25f27032001-04-26 23:22:31 +00001780
1781 /* pipes are inserted between pairs of commands */
1782 if ((i + 1) < pi->num_progs) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001783 if (pipe(pipefds) < 0)
1784 bb_perror_msg_and_die("pipe");
Eric Andersen25f27032001-04-26 23:22:31 +00001785 nextout = pipefds[1];
1786 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001787 nextout = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00001788 pipefds[0] = -1;
1789 }
1790
1791 /* XXX test for failed fork()? */
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001792#if BB_MMU
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001793 child->pid = fork();
Eric Andersen72f9a422001-10-28 05:12:20 +00001794#else
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001795 child->pid = vfork();
Eric Andersen72f9a422001-10-28 05:12:20 +00001796#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001797 if (!child->pid) { /* child */
1798 /* Every child adds itself to new process group
1799 * with pgid == pid of first child in pipe */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001800#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001801 if (interactive_fd) {
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00001802 /* Don't do pgrp restore anymore on fatal signals */
1803 set_fatal_sighandler(SIG_DFL);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001804 if (pi->pgrp < 0) /* true for 1st process only */
1805 pi->pgrp = getpid();
1806 if (setpgid(0, pi->pgrp) == 0 && pi->followup != PIPE_BG) {
1807 /* We do it in *every* child, not just first,
1808 * to avoid races */
1809 tcsetpgrp(interactive_fd, pi->pgrp);
1810 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001811 }
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001812#endif
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00001813 // in non-interactive case fatal sigs are already SIG_DFL
Eric Andersen25f27032001-04-26 23:22:31 +00001814 close_all();
Eric Andersen25f27032001-04-26 23:22:31 +00001815 if (nextin != 0) {
1816 dup2(nextin, 0);
1817 close(nextin);
1818 }
1819 if (nextout != 1) {
1820 dup2(nextout, 1);
1821 close(nextout);
1822 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00001823 if (pipefds[0] != -1) {
Eric Andersen25f27032001-04-26 23:22:31 +00001824 close(pipefds[0]); /* opposite end of our output pipe */
1825 }
Eric Andersen25f27032001-04-26 23:22:31 +00001826 /* Like bash, explicit redirects override pipes,
1827 * and the pipe fd is available for dup'ing. */
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00001828 setup_redirects(child, NULL);
Eric Andersen52a97ca2001-06-22 06:49:26 +00001829
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001830 /* Restore default handlers just prior to exec */
1831 set_jobctrl_sighandler(SIG_DFL);
1832 set_misc_sighandler(SIG_DFL);
1833 signal(SIGCHLD, SIG_DFL);
Eric Andersen25f27032001-04-26 23:22:31 +00001834 pseudo_exec(child);
1835 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001836
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001837 pi->running_progs++;
1838
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00001839#if ENABLE_HUSH_JOB
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00001840 /* Second and next children need to know pid of first one */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001841 if (pi->pgrp < 0)
Eric Andersen0fcd4472001-05-02 20:12:03 +00001842 pi->pgrp = child->pid;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00001843#endif
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001844
Eric Andersen0fcd4472001-05-02 20:12:03 +00001845 /* Don't check for errors. The child may be dead already,
1846 * in which case setpgid returns error code EACCES. */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00001847 //why we do it at all?? child does it itself
1848 //if (interactive_fd)
1849 // setpgid(child->pid, pi->pgrp);
Eric Andersen0fcd4472001-05-02 20:12:03 +00001850
Eric Andersen25f27032001-04-26 23:22:31 +00001851 if (nextin != 0)
1852 close(nextin);
1853 if (nextout != 1)
1854 close(nextout);
1855
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001856 /* If there isn't another process, nextin is garbage
Eric Andersen25f27032001-04-26 23:22:31 +00001857 but it doesn't matter */
1858 nextin = pipefds[0];
1859 }
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001860 debug_printf_exec("run_pipe_real return -1\n");
Eric Andersen25f27032001-04-26 23:22:31 +00001861 return -1;
1862}
1863
Denis Vlasenko400c5b62007-05-04 13:07:27 +00001864#ifndef debug_print_tree
1865static void debug_print_tree(struct pipe *pi, int lvl)
1866{
1867 static const char *PIPE[] = {
1868 [PIPE_SEQ] = "SEQ",
1869 [PIPE_AND] = "AND",
1870 [PIPE_OR ] = "OR",
1871 [PIPE_BG ] = "BG",
1872 };
1873 static const char *RES[] = {
1874 [RES_NONE ] = "NONE" ,
1875 [RES_IF ] = "IF" ,
1876 [RES_THEN ] = "THEN" ,
1877 [RES_ELIF ] = "ELIF" ,
1878 [RES_ELSE ] = "ELSE" ,
1879 [RES_FI ] = "FI" ,
1880 [RES_FOR ] = "FOR" ,
1881 [RES_WHILE] = "WHILE",
1882 [RES_UNTIL] = "UNTIL",
1883 [RES_DO ] = "DO" ,
1884 [RES_DONE ] = "DONE" ,
1885 [RES_XXXX ] = "XXXX" ,
1886 [RES_IN ] = "IN" ,
1887 [RES_SNTX ] = "SNTX" ,
1888 };
1889
1890 int pin, prn;
1891 char **argv;
1892 pin = 0;
1893 while (pi) {
1894 fprintf(stderr, "%*spipe %d r_mode=%s followup=%d %s\n", lvl*2, "",
1895 pin, RES[pi->r_mode], pi->followup, PIPE[pi->followup]);
1896 prn = 0;
1897 while (prn < pi->num_progs) {
1898 fprintf(stderr, "%*s prog %d", lvl*2, "", prn);
1899 if (pi->progs[prn].group) {
1900 fprintf(stderr, " group: (argv=%p)\n", pi->progs[prn].argv);
1901 debug_print_tree(pi->progs[prn].group, lvl+1);
1902 prn++;
1903 continue;
1904 }
1905 argv = pi->progs[prn].argv;
1906 if (argv) while (*argv) {
1907 fprintf(stderr, " '%s'", *argv);
1908 argv++;
1909 }
1910 fprintf(stderr, "\n");
1911 prn++;
1912 }
1913 pi = pi->next;
1914 pin++;
1915 }
1916}
1917#endif
1918
Denis Vlasenkof2fffd02007-05-02 23:39:04 +00001919// NB: called by pseudo_exec, and therefore must not modify any
1920// global data until exec/_exit (we can be a child after vfork!)
Eric Andersen25f27032001-04-26 23:22:31 +00001921static int run_list_real(struct pipe *pi)
1922{
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001923 char *save_name = NULL;
1924 char **list = NULL;
1925 char **save_list = NULL;
1926 struct pipe *rpipe;
1927 int flag_rep = 0;
1928 int save_num_progs;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001929 int rcode = 0, flag_skip = 1;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001930 int flag_restore = 0;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001931 int if_code = 0, next_if_code = 0; /* need double-buffer to handle elif */
1932 reserved_style rmode, skip_more_in_this_rmode = RES_XXXX;
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001933
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001934 debug_printf_exec("run_list_real start:\n");
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00001935
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001936 /* check syntax for "for" */
1937 for (rpipe = pi; rpipe; rpipe = rpipe->next) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001938 if ((rpipe->r_mode == RES_IN || rpipe->r_mode == RES_FOR)
1939 && (rpipe->next == NULL)
1940 ) {
1941 syntax();
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001942 debug_printf_exec("run_list_real return 1\n");
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001943 return 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001944 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001945 if ((rpipe->r_mode == RES_IN && rpipe->next->r_mode == RES_IN && rpipe->next->progs->argv != NULL)
1946 || (rpipe->r_mode == RES_FOR && rpipe->next->r_mode != RES_IN)
1947 ) {
1948 syntax();
Denis Vlasenkod01ff132007-05-02 21:40:23 +00001949 debug_printf_exec("run_list_real return 1\n");
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001950 return 1;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001951 }
1952 }
1953 for (; pi; pi = (flag_restore != 0) ? rpipe : pi->next) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001954 if (pi->r_mode == RES_WHILE || pi->r_mode == RES_UNTIL
1955 || pi->r_mode == RES_FOR
1956 ) {
1957 flag_restore = 0;
1958 if (!rpipe) {
1959 flag_rep = 0;
1960 rpipe = pi;
1961 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001962 }
Eric Andersen25f27032001-04-26 23:22:31 +00001963 rmode = pi->r_mode;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001964 debug_printf("rmode=%d if_code=%d next_if_code=%d skip_more=%d\n",
1965 rmode, if_code, next_if_code, skip_more_in_this_rmode);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001966 if (rmode == skip_more_in_this_rmode && flag_skip) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001967 if (pi->followup == PIPE_SEQ)
1968 flag_skip = 0;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001969 continue;
1970 }
1971 flag_skip = 1;
Eric Andersen4ed5e372001-05-01 01:49:50 +00001972 skip_more_in_this_rmode = RES_XXXX;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001973 if (rmode == RES_THEN || rmode == RES_ELSE)
1974 if_code = next_if_code;
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00001975 if (rmode == RES_THEN && if_code)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001976 continue;
1977 if (rmode == RES_ELSE && !if_code)
1978 continue;
1979 if (rmode == RES_ELIF && !if_code)
1980 break;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001981 if (rmode == RES_FOR && pi->num_progs) {
1982 if (!list) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001983 /* if no variable values after "in" we skip "for" */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001984 if (!pi->next->progs->argv)
1985 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001986 /* create list of variable values */
1987 list = make_list_in(pi->next->progs->argv,
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001988 pi->progs->argv[0]);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001989 save_list = list;
1990 save_name = pi->progs->argv[0];
1991 pi->progs->argv[0] = NULL;
1992 flag_rep = 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001993 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00001994 if (!*list) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00001995 free(pi->progs->argv[0]);
1996 free(save_list);
1997 list = NULL;
1998 flag_rep = 0;
1999 pi->progs->argv[0] = save_name;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002000 pi->progs->glob_result.gl_pathv[0] = pi->progs->argv[0];
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002001 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002002 }
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002003 /* insert new value from list for variable */
2004 if (pi->progs->argv[0])
2005 free(pi->progs->argv[0]);
2006 pi->progs->argv[0] = *list++;
2007 pi->progs->glob_result.gl_pathv[0] = pi->progs->argv[0];
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002008 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002009 if (rmode == RES_IN)
2010 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002011 if (rmode == RES_DO) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002012 if (!flag_rep)
2013 continue;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002014 }
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002015 if (rmode == RES_DONE) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002016 if (flag_rep) {
2017 flag_restore = 1;
2018 } else {
2019 rpipe = NULL;
2020 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002021 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002022 if (pi->num_progs == 0)
2023 continue;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002024 save_num_progs = pi->num_progs; /* save number of programs */
Denis Vlasenkod01ff132007-05-02 21:40:23 +00002025 debug_printf_exec(": run_pipe_real with %d members\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00002026 rcode = run_pipe_real(pi);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002027 if (rcode != -1) {
Eric Andersen25f27032001-04-26 23:22:31 +00002028 /* We only ran a builtin: rcode was set by the return value
2029 * of run_pipe_real(), and we don't need to wait for anything. */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002030 } else if (pi->followup == PIPE_BG) {
Eric Andersen25f27032001-04-26 23:22:31 +00002031 /* XXX check bash's behavior with nontrivial pipes */
2032 /* XXX compute jobid */
2033 /* XXX what does bash do with attempts to background builtins? */
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002034#if ENABLE_HUSH_JOB
Eric Andersenbafd94f2001-05-02 16:11:59 +00002035 insert_bg_job(pi);
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002036#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002037 rcode = EXIT_SUCCESS;
2038 } else {
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002039#if ENABLE_HUSH_JOB
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00002040 if (interactive_fd) {
Denis Vlasenko52881e92007-04-21 13:42:52 +00002041 rcode = checkjobs_and_fg_shell(pi);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002042 } else
2043#endif
2044 {
Eric Andersenc798b072001-06-22 06:23:03 +00002045 rcode = checkjobs(pi);
Eric Andersen25f27032001-04-26 23:22:31 +00002046 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002047 debug_printf_exec("checkjobs returned %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002048 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002049 debug_printf_exec("setting last_return_code=%d\n", rcode);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002050 last_return_code = rcode;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002051 pi->num_progs = save_num_progs; /* restore number of programs */
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002052 if (rmode == RES_IF || rmode == RES_ELIF)
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002053 next_if_code = rcode; /* can be overwritten a number of times */
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002054 if (rmode == RES_WHILE)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002055 flag_rep = !last_return_code;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002056 if (rmode == RES_UNTIL)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002057 flag_rep = last_return_code;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002058 if ((rcode == EXIT_SUCCESS && pi->followup == PIPE_OR)
2059 || (rcode != EXIT_SUCCESS && pi->followup == PIPE_AND)
2060 ) {
2061 skip_more_in_this_rmode = rmode;
2062 }
Eric Andersen028b65b2001-06-28 01:10:11 +00002063 checkjobs(NULL);
Eric Andersen25f27032001-04-26 23:22:31 +00002064 }
Denis Vlasenkod01ff132007-05-02 21:40:23 +00002065 debug_printf_exec("run_list_real return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002066 return rcode;
2067}
2068
Eric Andersen25f27032001-04-26 23:22:31 +00002069/* return code is the exit status of the pipe */
Eric Andersenbf7df042001-05-23 22:18:35 +00002070static int free_pipe(struct pipe *pi, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002071{
2072 char **p;
2073 struct child_prog *child;
2074 struct redir_struct *r, *rnext;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002075 int a, i, ret_code = 0;
Eric Andersen52a97ca2001-06-22 06:49:26 +00002076
2077 if (pi->stopped_progs > 0)
2078 return ret_code;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002079 final_printf("%s run pipe: (pid %d)\n", indenter(indent), getpid());
2080 for (i = 0; i < pi->num_progs; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002081 child = &pi->progs[i];
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002082 final_printf("%s command %d:\n", indenter(indent), i);
Eric Andersen25f27032001-04-26 23:22:31 +00002083 if (child->argv) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002084 for (a = 0, p = child->argv; *p; a++, p++) {
2085 final_printf("%s argv[%d] = %s\n", indenter(indent), a, *p);
Eric Andersen25f27032001-04-26 23:22:31 +00002086 }
2087 globfree(&child->glob_result);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002088 child->argv = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002089 } else if (child->group) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002090 final_printf("%s begin group (subshell:%d)\n", indenter(indent), child->subshell);
2091 ret_code = free_pipe_list(child->group, indent+3);
2092 final_printf("%s end group\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002093 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002094 final_printf("%s (nil)\n", indenter(indent));
Eric Andersen25f27032001-04-26 23:22:31 +00002095 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002096 for (r = child->redirects; r; r = rnext) {
Rob Landley88621d72006-08-29 19:41:06 +00002097 final_printf("%s redirect %d%s", indenter(indent), r->fd, redir_table[r->type].descrip);
Eric Andersen25f27032001-04-26 23:22:31 +00002098 if (r->dup == -1) {
Eric Andersen817e73c2001-06-06 17:56:09 +00002099 /* guard against the case >$FOO, where foo is unset or blank */
2100 if (r->word.gl_pathv) {
2101 final_printf(" %s\n", *r->word.gl_pathv);
2102 globfree(&r->word);
2103 }
Eric Andersen25f27032001-04-26 23:22:31 +00002104 } else {
2105 final_printf("&%d\n", r->dup);
2106 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002107 rnext = r->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002108 free(r);
2109 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002110 child->redirects = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002111 }
2112 free(pi->progs); /* children are an array, they get freed all at once */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002113 pi->progs = NULL;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00002114#if ENABLE_HUSH_JOB
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002115 free(pi->cmdtext);
2116 pi->cmdtext = NULL;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00002117#endif
Eric Andersen25f27032001-04-26 23:22:31 +00002118 return ret_code;
2119}
2120
Eric Andersenbf7df042001-05-23 22:18:35 +00002121static int free_pipe_list(struct pipe *head, int indent)
Eric Andersen25f27032001-04-26 23:22:31 +00002122{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002123 int rcode = 0; /* if list has no members */
Eric Andersen25f27032001-04-26 23:22:31 +00002124 struct pipe *pi, *next;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002125 for (pi = head; pi; pi = next) {
Rob Landley88621d72006-08-29 19:41:06 +00002126 final_printf("%s pipe reserved mode %d\n", indenter(indent), pi->r_mode);
Eric Andersenbf7df042001-05-23 22:18:35 +00002127 rcode = free_pipe(pi, indent);
Rob Landley88621d72006-08-29 19:41:06 +00002128 final_printf("%s pipe followup code %d\n", indenter(indent), pi->followup);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002129 next = pi->next;
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002130 /*pi->next = NULL;*/
Eric Andersen25f27032001-04-26 23:22:31 +00002131 free(pi);
2132 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002133 return rcode;
Eric Andersen25f27032001-04-26 23:22:31 +00002134}
2135
2136/* Select which version we will use */
2137static int run_list(struct pipe *pi)
2138{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002139 int rcode = 0;
2140 if (fake_mode == 0) {
Denis Vlasenkod01ff132007-05-02 21:40:23 +00002141 debug_printf_exec("run_list: run_list_real with %d members\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00002142 rcode = run_list_real(pi);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002143 }
Eric Andersenbf7df042001-05-23 22:18:35 +00002144 /* free_pipe_list has the side effect of clearing memory
Eric Andersen25f27032001-04-26 23:22:31 +00002145 * In the long run that function can be merged with run_list_real,
2146 * but doing that now would hobble the debugging effort. */
Denis Vlasenko400c5b62007-05-04 13:07:27 +00002147 free_pipe_list(pi, 0);
Eric Andersen25f27032001-04-26 23:22:31 +00002148 return rcode;
2149}
2150
2151/* The API for glob is arguably broken. This routine pushes a non-matching
2152 * string into the output structure, removing non-backslashed backslashes.
2153 * If someone can prove me wrong, by performing this function within the
2154 * original glob(3) api, feel free to rewrite this routine into oblivion.
2155 * Return code (0 vs. GLOB_NOSPACE) matches glob(3).
2156 * XXX broken if the last character is '\\', check that before calling.
2157 */
2158static int globhack(const char *src, int flags, glob_t *pglob)
2159{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002160 int cnt = 0, pathc;
Eric Andersen25f27032001-04-26 23:22:31 +00002161 const char *s;
2162 char *dest;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002163 for (cnt = 1, s = src; s && *s; s++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002164 if (*s == '\\') s++;
2165 cnt++;
2166 }
2167 dest = malloc(cnt);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002168 if (!dest)
2169 return GLOB_NOSPACE;
Eric Andersen25f27032001-04-26 23:22:31 +00002170 if (!(flags & GLOB_APPEND)) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002171 pglob->gl_pathv = NULL;
2172 pglob->gl_pathc = 0;
2173 pglob->gl_offs = 0;
2174 pglob->gl_offs = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002175 }
2176 pathc = ++pglob->gl_pathc;
2177 pglob->gl_pathv = realloc(pglob->gl_pathv, (pathc+1)*sizeof(*pglob->gl_pathv));
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002178 if (pglob->gl_pathv == NULL)
2179 return GLOB_NOSPACE;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002180 pglob->gl_pathv[pathc-1] = dest;
2181 pglob->gl_pathv[pathc] = NULL;
2182 for (s = src; s && *s; s++, dest++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002183 if (*s == '\\') s++;
2184 *dest = *s;
2185 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002186 *dest = '\0';
Eric Andersen25f27032001-04-26 23:22:31 +00002187 return 0;
2188}
2189
2190/* XXX broken if the last character is '\\', check that before calling */
2191static int glob_needed(const char *s)
2192{
2193 for (; *s; s++) {
2194 if (*s == '\\') s++;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002195 if (strchr("*[?", *s)) return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002196 }
2197 return 0;
2198}
2199
Eric Andersen25f27032001-04-26 23:22:31 +00002200static int xglob(o_string *dest, int flags, glob_t *pglob)
2201{
2202 int gr;
2203
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002204 /* short-circuit for null word */
Eric Andersen25f27032001-04-26 23:22:31 +00002205 /* we can code this better when the debug_printf's are gone */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002206 if (dest->length == 0) {
2207 if (dest->nonnull) {
2208 /* bash man page calls this an "explicit" null */
2209 gr = globhack(dest->data, flags, pglob);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002210 debug_printf("globhack returned %d\n", gr);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002211 } else {
Eric Andersen25f27032001-04-26 23:22:31 +00002212 return 0;
2213 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002214 } else if (glob_needed(dest->data)) {
Eric Andersen25f27032001-04-26 23:22:31 +00002215 gr = glob(dest->data, flags, NULL, pglob);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002216 debug_printf("glob returned %d\n", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002217 if (gr == GLOB_NOMATCH) {
2218 /* quote removal, or more accurately, backslash removal */
2219 gr = globhack(dest->data, flags, pglob);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002220 debug_printf("globhack returned %d\n", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002221 }
2222 } else {
2223 gr = globhack(dest->data, flags, pglob);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002224 debug_printf("globhack returned %d\n", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002225 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002226 if (gr == GLOB_NOSPACE)
Manuel Novoa III cad53642003-03-19 09:13:01 +00002227 bb_error_msg_and_die("out of memory during glob");
Eric Andersen25f27032001-04-26 23:22:31 +00002228 if (gr != 0) { /* GLOB_ABORTED ? */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002229 bb_error_msg("glob(3) error %d", gr);
Eric Andersen25f27032001-04-26 23:22:31 +00002230 }
2231 /* globprint(glob_target); */
2232 return gr;
2233}
2234
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002235static char **make_list_in(char **inp, char *name)
2236{
2237 int len, i;
2238 int name_len = strlen(name);
2239 int n = 0;
2240 char **list;
2241 char *p1, *p2, *p3;
2242
2243 /* create list of variable values */
2244 list = xmalloc(sizeof(*list));
2245 for (i = 0; inp[i]; i++) {
2246 p3 = insert_var_value(inp[i]);
2247 p1 = p3;
2248 while (*p1) {
2249 if ((*p1 == ' ')) {
2250 p1++;
2251 continue;
2252 }
2253 p2 = strchr(p1, ' ');
2254 if (p2) {
2255 len = p2 - p1;
2256 } else {
2257 len = strlen(p1);
2258 p2 = p1 + len;
2259 }
2260 /* we use n + 2 in realloc for list, because we add
2261 * new element and then we will add NULL element */
2262 list = xrealloc(list, sizeof(*list) * (n + 2));
2263 list[n] = xmalloc(2 + name_len + len);
2264 strcpy(list[n], name);
2265 strcat(list[n], "=");
2266 strncat(list[n], p1, len);
2267 list[n++][name_len + len + 1] = '\0';
2268 p1 = p2;
2269 }
2270 if (p3 != inp[i]) free(p3);
2271 }
2272 list[n] = NULL;
2273 return list;
2274}
2275
2276static char *insert_var_value(char *inp)
2277{
2278 int res_str_len = 0;
2279 int len;
2280 int done = 0;
2281 char *p, *res_str = NULL;
2282 const char *p1;
2283
2284 while ((p = strchr(inp, SPECIAL_VAR_SYMBOL))) {
2285 if (p != inp) {
2286 len = p - inp;
2287 res_str = xrealloc(res_str, (res_str_len + len));
2288 strncpy((res_str + res_str_len), inp, len);
2289 res_str_len += len;
2290 }
2291 inp = ++p;
2292 p = strchr(inp, SPECIAL_VAR_SYMBOL);
2293 *p = '\0';
2294 p1 = lookup_param(inp);
2295 if (p1) {
2296 len = res_str_len + strlen(p1);
2297 res_str = xrealloc(res_str, (1 + len));
2298 strcpy((res_str + res_str_len), p1);
2299 res_str_len = len;
2300 }
2301 *p = SPECIAL_VAR_SYMBOL;
2302 inp = ++p;
2303 done = 1;
2304 }
2305 if (done) {
2306 res_str = xrealloc(res_str, (1 + res_str_len + strlen(inp)));
2307 strcpy((res_str + res_str_len), inp);
2308 while ((p = strchr(res_str, '\n'))) {
2309 *p = ' ';
2310 }
2311 }
2312 return (res_str == NULL) ? inp : res_str;
2313}
2314
Eric Andersenf72f5622001-05-15 23:21:41 +00002315/* This is used to get/check local shell variables */
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00002316static const char *get_local_var(const char *s)
Eric Andersenf72f5622001-05-15 23:21:41 +00002317{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002318 struct variables *cur;
Eric Andersenf72f5622001-05-15 23:21:41 +00002319
2320 if (!s)
2321 return NULL;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002322 for (cur = top_vars; cur; cur = cur->next)
2323 if (strcmp(cur->name, s) == 0)
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002324 return cur->value;
Eric Andersenf72f5622001-05-15 23:21:41 +00002325 return NULL;
2326}
2327
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002328/* This is used to set local shell variables
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002329 flg_export == 0 if only local (not exporting) variable
2330 flg_export == 1 if "new" exporting environ
2331 flg_export > 1 if current startup environ (not call putenv()) */
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002332static int set_local_var(const char *s, int flg_export)
Eric Andersen78a7c992001-05-15 16:30:25 +00002333{
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002334 char *name, *value;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002335 int result = 0;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002336 struct variables *cur;
Eric Andersen20a69a72001-05-15 17:24:44 +00002337
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002338 name = strdup(s);
Eric Andersen20a69a72001-05-15 17:24:44 +00002339
2340 /* Assume when we enter this function that we are already in
2341 * NAME=VALUE format. So the first order of business is to
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002342 * split 's' on the '=' into 'name' and 'value' */
Eric Andersen20a69a72001-05-15 17:24:44 +00002343 value = strchr(name, '=');
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002344 /*if (value == 0 && ++value == 0) ??? -vda */
2345 if (value == NULL || value[1] == '\0') {
Eric Andersen99785762001-05-22 21:37:48 +00002346 free(name);
2347 return -1;
2348 }
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002349 *value++ = '\0';
Eric Andersen20a69a72001-05-15 17:24:44 +00002350
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002351 for (cur = top_vars; cur; cur = cur->next) {
2352 if (strcmp(cur->name, name) == 0)
Eric Andersen99785762001-05-22 21:37:48 +00002353 break;
2354 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002355
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002356 if (cur) {
2357 if (strcmp(cur->value, value) == 0) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002358 if (flg_export > 0 && cur->flg_export == 0)
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00002359 cur->flg_export = flg_export;
Eric Andersen99785762001-05-22 21:37:48 +00002360 else
2361 result++;
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002362 } else if (cur->flg_read_only) {
2363 bb_error_msg("%s: readonly variable", name);
2364 result = -1;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002365 } else {
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002366 if (flg_export > 0 || cur->flg_export > 1)
2367 cur->flg_export = 1;
2368 free((char*)cur->value);
Eric Andersen99785762001-05-22 21:37:48 +00002369
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002370 cur->value = strdup(value);
Eric Andersen99785762001-05-22 21:37:48 +00002371 }
2372 } else {
2373 cur = malloc(sizeof(struct variables));
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002374 if (!cur) {
Eric Andersen99785762001-05-22 21:37:48 +00002375 result = -1;
2376 } else {
2377 cur->name = strdup(name);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002378 if (cur->name) {
Eric Andersen99785762001-05-22 21:37:48 +00002379 free(cur);
2380 result = -1;
2381 } else {
2382 struct variables *bottom = top_vars;
2383 cur->value = strdup(value);
2384 cur->next = 0;
2385 cur->flg_export = flg_export;
2386 cur->flg_read_only = 0;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002387 while (bottom->next)
2388 bottom = bottom->next;
Eric Andersen99785762001-05-22 21:37:48 +00002389 bottom->next = cur;
Eric Andersen20a69a72001-05-15 17:24:44 +00002390 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002391 }
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002392 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002393
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002394 if (result == 0 && cur->flg_export == 1) {
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002395 *(value-1) = '=';
2396 result = putenv(name);
2397 } else {
Eric Andersen94ac2442001-05-22 19:05:18 +00002398 free(name);
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002399 if (result > 0) /* equivalent to previous set */
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002400 result = 0;
2401 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002402 return result;
2403}
2404
Eric Andersenf72f5622001-05-15 23:21:41 +00002405static void unset_local_var(const char *name)
Eric Andersen20a69a72001-05-15 17:24:44 +00002406{
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002407 struct variables *cur, *next;
Eric Andersen20a69a72001-05-15 17:24:44 +00002408
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002409 if (!name)
2410 return;
2411 for (cur = top_vars; cur; cur = cur->next) {
2412 if (strcmp(cur->name, name) == 0) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002413 if (cur->flg_read_only) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002414 bb_error_msg("%s: readonly variable", name);
Eric Andersen94ac2442001-05-22 19:05:18 +00002415 return;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002416 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002417 if (cur->flg_export)
2418 unsetenv(cur->name);
2419 free((char*)cur->name);
2420 free((char*)cur->value);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002421 next = top_vars;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002422 while (next->next != cur)
2423 next = next->next;
2424 next->next = cur->next;
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00002425 free(cur);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002426 return;
Eric Andersenf72f5622001-05-15 23:21:41 +00002427 }
Eric Andersen20a69a72001-05-15 17:24:44 +00002428 }
Eric Andersen78a7c992001-05-15 16:30:25 +00002429}
2430
2431static int is_assignment(const char *s)
2432{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002433 if (!s || !isalpha(*s))
2434 return 0;
2435 s++;
2436 while (isalnum(*s) || *s == '_')
2437 s++;
2438 return *s == '=';
Eric Andersen78a7c992001-05-15 16:30:25 +00002439}
2440
Eric Andersen25f27032001-04-26 23:22:31 +00002441/* the src parameter allows us to peek forward to a possible &n syntax
2442 * for file descriptor duplication, e.g., "2>&1".
2443 * Return code is 0 normally, 1 if a syntax error is detected in src.
2444 * Resource errors (in xmalloc) cause the process to exit */
2445static int setup_redirect(struct p_context *ctx, int fd, redir_type style,
2446 struct in_str *input)
2447{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002448 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00002449 struct redir_struct *redir = child->redirects;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002450 struct redir_struct *last_redir = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002451
2452 /* Create a new redir_struct and drop it onto the end of the linked list */
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002453 while (redir) {
2454 last_redir = redir;
2455 redir = redir->next;
Eric Andersen25f27032001-04-26 23:22:31 +00002456 }
2457 redir = xmalloc(sizeof(struct redir_struct));
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002458 redir->next = NULL;
2459 redir->word.gl_pathv = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002460 if (last_redir) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002461 last_redir->next = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002462 } else {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002463 child->redirects = redir;
Eric Andersen25f27032001-04-26 23:22:31 +00002464 }
2465
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002466 redir->type = style;
2467 redir->fd = (fd == -1) ? redir_table[style].default_fd : fd;
Eric Andersen25f27032001-04-26 23:22:31 +00002468
2469 debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
2470
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002471 /* Check for a '2>&1' type redirect */
Eric Andersen25f27032001-04-26 23:22:31 +00002472 redir->dup = redirect_dup_num(input);
2473 if (redir->dup == -2) return 1; /* syntax error */
2474 if (redir->dup != -1) {
2475 /* Erik had a check here that the file descriptor in question
Eric Andersen83a2ae22001-05-07 17:59:25 +00002476 * is legit; I postpone that to "run time"
2477 * A "-" representation of "close me" shows up as a -3 here */
Eric Andersen25f27032001-04-26 23:22:31 +00002478 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
2479 } else {
2480 /* We do _not_ try to open the file that src points to,
2481 * since we need to return and let src be expanded first.
2482 * Set ctx->pending_redirect, so we know what to do at the
2483 * end of the next parsed word.
2484 */
2485 ctx->pending_redirect = redir;
2486 }
2487 return 0;
2488}
2489
Denis Vlasenkoac678ec2007-04-16 22:32:04 +00002490static struct pipe *new_pipe(void)
2491{
Eric Andersen25f27032001-04-26 23:22:31 +00002492 struct pipe *pi;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002493 pi = xzalloc(sizeof(struct pipe));
2494 /*pi->num_progs = 0;*/
2495 /*pi->progs = NULL;*/
2496 /*pi->next = NULL;*/
2497 /*pi->followup = 0; invalid */
2498 if (RES_NONE)
2499 pi->r_mode = RES_NONE;
Eric Andersen25f27032001-04-26 23:22:31 +00002500 return pi;
2501}
2502
2503static void initialize_context(struct p_context *ctx)
2504{
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002505 ctx->pipe = NULL;
2506 ctx->pending_redirect = NULL;
2507 ctx->child = NULL;
2508 ctx->list_head = new_pipe();
2509 ctx->pipe = ctx->list_head;
2510 ctx->w = RES_NONE;
2511 ctx->stack = NULL;
2512 ctx->old_flag = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002513 done_command(ctx); /* creates the memory for working child */
2514}
2515
2516/* normal return is 0
2517 * if a reserved word is found, and processed, return 1
2518 * should handle if, then, elif, else, fi, for, while, until, do, done.
2519 * case, function, and select are obnoxious, save those for later.
2520 */
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00002521static int reserved_word(o_string *dest, struct p_context *ctx)
Eric Andersen25f27032001-04-26 23:22:31 +00002522{
2523 struct reserved_combo {
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002524 char literal[7];
2525 unsigned char code;
2526 int flag;
Eric Andersen25f27032001-04-26 23:22:31 +00002527 };
2528 /* Mostly a list of accepted follow-up reserved words.
2529 * FLAG_END means we are done with the sequence, and are ready
2530 * to turn the compound list into a command.
2531 * FLAG_START means the word must start a new compound list.
2532 */
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002533 static const struct reserved_combo reserved_list[] = {
Eric Andersen25f27032001-04-26 23:22:31 +00002534 { "if", RES_IF, FLAG_THEN | FLAG_START },
2535 { "then", RES_THEN, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
2536 { "elif", RES_ELIF, FLAG_THEN },
2537 { "else", RES_ELSE, FLAG_FI },
2538 { "fi", RES_FI, FLAG_END },
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002539 { "for", RES_FOR, FLAG_IN | FLAG_START },
Eric Andersen25f27032001-04-26 23:22:31 +00002540 { "while", RES_WHILE, FLAG_DO | FLAG_START },
2541 { "until", RES_UNTIL, FLAG_DO | FLAG_START },
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002542 { "in", RES_IN, FLAG_DO },
Eric Andersen25f27032001-04-26 23:22:31 +00002543 { "do", RES_DO, FLAG_DONE },
2544 { "done", RES_DONE, FLAG_END }
2545 };
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00002546 enum { NRES = sizeof(reserved_list)/sizeof(reserved_list[0]) };
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002547 const struct reserved_combo *r;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002548
Denis Vlasenko0c886c62007-01-30 22:30:09 +00002549 for (r = reserved_list; r < reserved_list+NRES; r++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002550 if (strcmp(dest->data, r->literal) == 0) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002551 debug_printf("found reserved word %s, code %d\n", r->literal, r->code);
Eric Andersen25f27032001-04-26 23:22:31 +00002552 if (r->flag & FLAG_START) {
2553 struct p_context *new = xmalloc(sizeof(struct p_context));
2554 debug_printf("push stack\n");
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002555 if (ctx->w == RES_IN || ctx->w == RES_FOR) {
2556 syntax();
2557 free(new);
2558 ctx->w = RES_SNTX;
2559 b_reset(dest);
2560 return 1;
2561 }
Eric Andersen25f27032001-04-26 23:22:31 +00002562 *new = *ctx; /* physical copy */
2563 initialize_context(ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002564 ctx->stack = new;
2565 } else if (ctx->w == RES_NONE || !(ctx->old_flag & (1 << r->code))) {
Eric Andersenaf44a0e2001-04-27 07:26:12 +00002566 syntax();
2567 ctx->w = RES_SNTX;
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002568 b_reset(dest);
Eric Andersenaf44a0e2001-04-27 07:26:12 +00002569 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002570 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002571 ctx->w = r->code;
Eric Andersen25f27032001-04-26 23:22:31 +00002572 ctx->old_flag = r->flag;
2573 if (ctx->old_flag & FLAG_END) {
2574 struct p_context *old;
2575 debug_printf("pop stack\n");
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002576 done_pipe(ctx, PIPE_SEQ);
Eric Andersen25f27032001-04-26 23:22:31 +00002577 old = ctx->stack;
2578 old->child->group = ctx->list_head;
Eric Andersen04407e52001-06-07 16:42:05 +00002579 old->child->subshell = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002580 *ctx = *old; /* physical copy */
2581 free(old);
Eric Andersen25f27032001-04-26 23:22:31 +00002582 }
Denis Vlasenkoff131b92007-04-10 15:42:06 +00002583 b_reset(dest);
Eric Andersen25f27032001-04-26 23:22:31 +00002584 return 1;
2585 }
2586 }
2587 return 0;
2588}
2589
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002590/* Normal return is 0.
Eric Andersen25f27032001-04-26 23:22:31 +00002591 * Syntax or xglob errors return 1. */
2592static int done_word(o_string *dest, struct p_context *ctx)
2593{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002594 struct child_prog *child = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00002595 glob_t *glob_target;
2596 int gr, flags = 0;
2597
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002598 debug_printf_parse("done_word: '%s' %p\n", dest->data, child);
Eric Andersen25f27032001-04-26 23:22:31 +00002599 if (dest->length == 0 && !dest->nonnull) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002600 debug_printf_parse("done_word return 0: true null, ignored\n");
Eric Andersen25f27032001-04-26 23:22:31 +00002601 return 0;
2602 }
2603 if (ctx->pending_redirect) {
2604 glob_target = &ctx->pending_redirect->word;
2605 } else {
2606 if (child->group) {
2607 syntax();
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002608 debug_printf_parse("done_word return 1: syntax error, groups and arglists don't mix\n");
2609 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002610 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002611 if (!child->argv && (ctx->type & FLAG_PARSE_SEMICOLON)) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002612 debug_printf_parse(": checking %s for reserved-ness\n", dest->data);
2613 if (reserved_word(dest, ctx)) {
2614 debug_printf_parse("done_word return %d\n", (ctx->w == RES_SNTX));
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002615 return (ctx->w == RES_SNTX);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002616 }
Eric Andersen25f27032001-04-26 23:22:31 +00002617 }
2618 glob_target = &child->glob_result;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002619 if (child->argv) flags |= GLOB_APPEND;
Eric Andersen25f27032001-04-26 23:22:31 +00002620 }
2621 gr = xglob(dest, flags, glob_target);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002622 if (gr != 0) {
2623 debug_printf_parse("done_word return 1: xglob returned %d\n", gr);
2624 return 1;
2625 }
Eric Andersen25f27032001-04-26 23:22:31 +00002626
2627 b_reset(dest);
2628 if (ctx->pending_redirect) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002629 ctx->pending_redirect = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002630 if (glob_target->gl_pathc != 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002631 bb_error_msg("ambiguous redirect");
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002632 debug_printf_parse("done_word return 1: ambiguous redirect\n");
Eric Andersen25f27032001-04-26 23:22:31 +00002633 return 1;
2634 }
2635 } else {
2636 child->argv = glob_target->gl_pathv;
2637 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002638 if (ctx->w == RES_FOR) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002639 done_word(dest, ctx);
2640 done_pipe(ctx, PIPE_SEQ);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002641 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002642 debug_printf_parse("done_word return 0\n");
Eric Andersen25f27032001-04-26 23:22:31 +00002643 return 0;
2644}
2645
2646/* The only possible error here is out of memory, in which case
2647 * xmalloc exits. */
2648static int done_command(struct p_context *ctx)
2649{
2650 /* The child is really already in the pipe structure, so
2651 * advance the pipe counter and make a new, null child.
2652 * Only real trickiness here is that the uncommitted
2653 * child structure, to which ctx->child points, is not
2654 * counted in pi->num_progs. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002655 struct pipe *pi = ctx->pipe;
2656 struct child_prog *prog = ctx->child;
Eric Andersen25f27032001-04-26 23:22:31 +00002657
2658 if (prog && prog->group == NULL
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00002659 && prog->argv == NULL
2660 && prog->redirects == NULL
2661 ) {
Eric Andersen25f27032001-04-26 23:22:31 +00002662 debug_printf("done_command: skipping null command\n");
2663 return 0;
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00002664 }
2665 if (prog) {
Eric Andersen25f27032001-04-26 23:22:31 +00002666 pi->num_progs++;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002667 debug_printf("done_command: num_progs incremented to %d\n", pi->num_progs);
Eric Andersen25f27032001-04-26 23:22:31 +00002668 } else {
2669 debug_printf("done_command: initializing\n");
2670 }
2671 pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1));
2672
2673 prog = pi->progs + pi->num_progs;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002674 memset(prog, 0, sizeof(*prog));
2675 /*prog->redirects = NULL;*/
Denis Vlasenkob5eaabb2007-04-28 16:45:59 +00002676 /*prog->argv = NULL; */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002677 /*prog->is_stopped = 0;*/
2678 /*prog->group = NULL;*/
2679 /*prog->glob_result.gl_pathv = NULL;*/
Eric Andersen25f27032001-04-26 23:22:31 +00002680 prog->family = pi;
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00002681 /*prog->sp = 0;*/
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002682 ctx->child = prog;
2683 prog->type = ctx->type;
Eric Andersen25f27032001-04-26 23:22:31 +00002684
Eric Andersen25f27032001-04-26 23:22:31 +00002685 /* but ctx->pipe and ctx->list_head remain unchanged */
2686 return 0;
2687}
2688
2689static int done_pipe(struct p_context *ctx, pipe_style type)
2690{
2691 struct pipe *new_p;
2692 done_command(ctx); /* implicit closure of previous command */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002693 debug_printf_parse("done_pipe, type %d\n", type);
Eric Andersen25f27032001-04-26 23:22:31 +00002694 ctx->pipe->followup = type;
2695 ctx->pipe->r_mode = ctx->w;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002696 new_p = new_pipe();
Eric Andersen25f27032001-04-26 23:22:31 +00002697 ctx->pipe->next = new_p;
2698 ctx->pipe = new_p;
2699 ctx->child = NULL;
2700 done_command(ctx); /* set up new pipe to accept commands */
2701 return 0;
2702}
2703
2704/* peek ahead in the in_str to find out if we have a "&n" construct,
2705 * as in "2>&1", that represents duplicating a file descriptor.
2706 * returns either -2 (syntax error), -1 (no &), or the number found.
2707 */
2708static int redirect_dup_num(struct in_str *input)
2709{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002710 int ch, d = 0, ok = 0;
Eric Andersen25f27032001-04-26 23:22:31 +00002711 ch = b_peek(input);
2712 if (ch != '&') return -1;
2713
2714 b_getch(input); /* get the & */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002715 ch = b_peek(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00002716 if (ch == '-') {
2717 b_getch(input);
2718 return -3; /* "-" represents "close me" */
2719 }
2720 while (isdigit(ch)) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00002721 d = d*10 + (ch-'0');
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002722 ok = 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002723 b_getch(input);
Eric Andersen83a2ae22001-05-07 17:59:25 +00002724 ch = b_peek(input);
Eric Andersen25f27032001-04-26 23:22:31 +00002725 }
2726 if (ok) return d;
2727
Manuel Novoa III cad53642003-03-19 09:13:01 +00002728 bb_error_msg("ambiguous redirect");
Eric Andersen25f27032001-04-26 23:22:31 +00002729 return -2;
2730}
2731
2732/* If a redirect is immediately preceded by a number, that number is
2733 * supposed to tell which file descriptor to redirect. This routine
2734 * looks for such preceding numbers. In an ideal world this routine
2735 * needs to handle all the following classes of redirects...
2736 * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
2737 * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
2738 * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
2739 * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
2740 * A -1 output from this program means no valid number was found, so the
2741 * caller should use the appropriate default for this redirection.
2742 */
2743static int redirect_opt_num(o_string *o)
2744{
2745 int num;
2746
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002747 if (o->length == 0)
2748 return -1;
2749 for (num = 0; num < o->length; num++) {
2750 if (!isdigit(*(o->data + num))) {
Eric Andersen25f27032001-04-26 23:22:31 +00002751 return -1;
2752 }
2753 }
2754 /* reuse num (and save an int) */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002755 num = atoi(o->data);
Eric Andersen25f27032001-04-26 23:22:31 +00002756 b_reset(o);
2757 return num;
2758}
2759
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00002760static FILE *generate_stream_from_list(struct pipe *head)
Eric Andersen25f27032001-04-26 23:22:31 +00002761{
2762 FILE *pf;
Eric Andersen25f27032001-04-26 23:22:31 +00002763 int pid, channel[2];
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002764 if (pipe(channel) < 0) bb_perror_msg_and_die("pipe");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00002765#if BB_MMU
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002766 pid = fork();
Eric Andersen72f9a422001-10-28 05:12:20 +00002767#else
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002768 pid = vfork();
Eric Andersen72f9a422001-10-28 05:12:20 +00002769#endif
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002770 if (pid < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00002771 bb_perror_msg_and_die("fork");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002772 } else if (pid == 0) {
Eric Andersen25f27032001-04-26 23:22:31 +00002773 close(channel[0]);
2774 if (channel[1] != 1) {
Denis Vlasenko7d4c44e2007-04-16 22:34:39 +00002775 dup2(channel[1], 1);
Eric Andersen25f27032001-04-26 23:22:31 +00002776 close(channel[1]);
2777 }
Eric Andersen94ac2442001-05-22 19:05:18 +00002778 _exit(run_list_real(head)); /* leaks memory */
Eric Andersen25f27032001-04-26 23:22:31 +00002779 }
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002780 debug_printf("forked child %d\n", pid);
Eric Andersen25f27032001-04-26 23:22:31 +00002781 close(channel[1]);
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002782 pf = fdopen(channel[0], "r");
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002783 debug_printf("pipe on FILE *%p\n", pf);
Eric Andersen25f27032001-04-26 23:22:31 +00002784 return pf;
2785}
2786
2787/* this version hacked for testing purposes */
2788/* return code is exit status of the process that is run. */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002789static int process_command_subs(o_string *dest, struct p_context *ctx,
2790 struct in_str *input, const char *subst_end)
Eric Andersen25f27032001-04-26 23:22:31 +00002791{
2792 int retcode;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002793 o_string result = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00002794 struct p_context inner;
2795 FILE *p;
2796 struct in_str pipe_str;
2797 initialize_context(&inner);
2798
2799 /* recursion to generate command */
2800 retcode = parse_stream(&result, &inner, input, subst_end);
2801 if (retcode != 0) return retcode; /* syntax error or EOF */
2802 done_word(&result, &inner);
2803 done_pipe(&inner, PIPE_SEQ);
2804 b_free(&result);
2805
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002806 p = generate_stream_from_list(inner.list_head);
2807 if (p == NULL) return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002808 mark_open(fileno(p));
2809 setup_file_in_str(&pipe_str, p);
2810
2811 /* now send results of command back into original context */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002812 retcode = parse_stream(dest, ctx, &pipe_str, NULL);
Eric Andersen25f27032001-04-26 23:22:31 +00002813 /* XXX In case of a syntax error, should we try to kill the child?
2814 * That would be tough to do right, so just read until EOF. */
2815 if (retcode == 1) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002816 while (b_getch(&pipe_str) != EOF)
2817 /* discard */;
Eric Andersen25f27032001-04-26 23:22:31 +00002818 }
2819
2820 debug_printf("done reading from pipe, pclose()ing\n");
2821 /* This is the step that wait()s for the child. Should be pretty
2822 * safe, since we just read an EOF from its stdout. We could try
2823 * to better, by using wait(), and keeping track of background jobs
2824 * at the same time. That would be a lot of work, and contrary
2825 * to the KISS philosophy of this program. */
2826 mark_closed(fileno(p));
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002827 retcode = pclose(p);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002828 free_pipe_list(inner.list_head, 0);
2829 debug_printf("pclosed, retcode=%d\n", retcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002830 /* XXX this process fails to trim a single trailing newline */
2831 return retcode;
2832}
2833
2834static int parse_group(o_string *dest, struct p_context *ctx,
2835 struct in_str *input, int ch)
2836{
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002837 int rcode;
2838 const char *endch = NULL;
Eric Andersen25f27032001-04-26 23:22:31 +00002839 struct p_context sub;
2840 struct child_prog *child = ctx->child;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002841
2842 debug_printf_parse("parse_group entered\n");
Eric Andersen25f27032001-04-26 23:22:31 +00002843 if (child->argv) {
2844 syntax();
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002845 debug_printf_parse("parse_group return 1: syntax error, groups and arglists don't mix\n");
2846 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00002847 }
2848 initialize_context(&sub);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00002849 switch (ch) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002850 case '(':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002851 endch = ")";
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002852 child->subshell = 1;
2853 break;
2854 case '{':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002855 endch = "}";
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002856 break;
2857 default:
2858 syntax(); /* really logic error */
Eric Andersen25f27032001-04-26 23:22:31 +00002859 }
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002860 rcode = parse_stream(dest, &sub, input, endch);
2861 done_word(dest, &sub); /* finish off the final word in the subcontext */
Eric Andersen25f27032001-04-26 23:22:31 +00002862 done_pipe(&sub, PIPE_SEQ); /* and the final command there, too */
2863 child->group = sub.list_head;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002864
2865 debug_printf_parse("parse_group return %d\n", rcode);
Eric Andersen25f27032001-04-26 23:22:31 +00002866 return rcode;
2867 /* child remains "open", available for possible redirects */
2868}
2869
2870/* basically useful version until someone wants to get fancier,
2871 * see the bash man page under "Parameter Expansion" */
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00002872static const char *lookup_param(const char *src)
Eric Andersen25f27032001-04-26 23:22:31 +00002873{
Denis Vlasenko15d78fb2007-01-30 22:28:21 +00002874 const char *p = NULL;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002875 if (src) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002876 p = getenv(src);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002877 if (!p)
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002878 p = get_local_var(src);
Eric Andersen20a69a72001-05-15 17:24:44 +00002879 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002880 return p;
Eric Andersen25f27032001-04-26 23:22:31 +00002881}
2882
Denis Vlasenko87cb2db2007-04-21 10:00:01 +00002883/* Make new string for parser */
2884static char* make_string(char ** inp)
2885{
2886 char *p;
2887 char *str = NULL;
2888 int n;
2889 int len = 2;
2890
2891 for (n = 0; inp[n]; n++) {
2892 p = insert_var_value(inp[n]);
2893 str = xrealloc(str, (len + strlen(p)));
2894 if (n) {
2895 strcat(str, " ");
2896 } else {
2897 *str = '\0';
2898 }
2899 strcat(str, p);
2900 len = strlen(str) + 3;
2901 if (p != inp[n]) free(p);
2902 }
2903 len = strlen(str);
2904 str[len] = '\n';
2905 str[len+1] = '\0';
2906 return str;
2907}
2908
Eric Andersen25f27032001-04-26 23:22:31 +00002909/* return code: 0 for OK, 1 for syntax error */
2910static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input)
2911{
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002912 int i, advance = 0;
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00002913 char sep[] = " ";
Eric Andersen25f27032001-04-26 23:22:31 +00002914 int ch = input->peek(input); /* first character after the $ */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002915 debug_printf("handle_dollar: ch=%c\n", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00002916 if (isalpha(ch)) {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002917 b_addchr(dest, SPECIAL_VAR_SYMBOL);
2918 ctx->child->sp++;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002919 while (ch = b_peek(input), isalnum(ch) || ch == '_') {
Eric Andersen25f27032001-04-26 23:22:31 +00002920 b_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002921 b_addchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00002922 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002923 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00002924 } else if (isdigit(ch)) {
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002925 i = ch - '0'; /* XXX is $0 special? */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002926 if (i < global_argc) {
Eric Andersen25f27032001-04-26 23:22:31 +00002927 parse_string(dest, ctx, global_argv[i]); /* recursion */
2928 }
2929 advance = 1;
2930 } else switch (ch) {
2931 case '$':
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002932 b_adduint(dest, getpid());
Eric Andersen25f27032001-04-26 23:22:31 +00002933 advance = 1;
2934 break;
2935 case '!':
2936 if (last_bg_pid > 0) b_adduint(dest, last_bg_pid);
2937 advance = 1;
2938 break;
2939 case '?':
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002940 b_adduint(dest, last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +00002941 advance = 1;
2942 break;
2943 case '#':
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002944 b_adduint(dest, global_argc ? global_argc-1 : 0);
Eric Andersen25f27032001-04-26 23:22:31 +00002945 advance = 1;
2946 break;
2947 case '{':
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002948 b_addchr(dest, SPECIAL_VAR_SYMBOL);
2949 ctx->child->sp++;
Eric Andersen25f27032001-04-26 23:22:31 +00002950 b_getch(input);
2951 /* XXX maybe someone will try to escape the '}' */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002952 while (1) {
2953 ch = b_getch(input);
2954 if (ch == EOF || ch == '}')
2955 break;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002956 b_addchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00002957 }
2958 if (ch != '}') {
2959 syntax();
2960 return 1;
2961 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00002962 b_addchr(dest, SPECIAL_VAR_SYMBOL);
Eric Andersen25f27032001-04-26 23:22:31 +00002963 break;
2964 case '(':
Matt Kraai9f8caf12001-05-02 16:26:12 +00002965 b_getch(input);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002966 process_command_subs(dest, ctx, input, ")");
Eric Andersen25f27032001-04-26 23:22:31 +00002967 break;
2968 case '*':
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002969 sep[0] = ifs[0];
2970 for (i = 1; i < global_argc; i++) {
Eric Andersen25f27032001-04-26 23:22:31 +00002971 parse_string(dest, ctx, global_argv[i]);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00002972 if (i+1 < global_argc)
2973 parse_string(dest, ctx, sep);
Eric Andersen25f27032001-04-26 23:22:31 +00002974 }
2975 break;
2976 case '@':
2977 case '-':
2978 case '_':
2979 /* still unhandled, but should be eventually */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002980 bb_error_msg("unhandled syntax: $%c", ch);
Eric Andersen25f27032001-04-26 23:22:31 +00002981 return 1;
2982 break;
2983 default:
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00002984 b_addqchr(dest,'$', dest->quote);
Eric Andersen25f27032001-04-26 23:22:31 +00002985 }
2986 /* Eat the character if the flag was set. If the compiler
2987 * is smart enough, we could substitute "b_getch(input);"
2988 * for all the "advance = 1;" above, and also end up with
2989 * a nice size-optimized program. Hah! That'll be the day.
2990 */
2991 if (advance) b_getch(input);
2992 return 0;
2993}
2994
Denis Vlasenko5f786c22007-04-20 08:35:45 +00002995static int parse_string(o_string *dest, struct p_context *ctx, const char *src)
Eric Andersen25f27032001-04-26 23:22:31 +00002996{
2997 struct in_str foo;
2998 setup_string_in_str(&foo, src);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00002999 return parse_stream(dest, ctx, &foo, NULL);
Eric Andersen25f27032001-04-26 23:22:31 +00003000}
3001
3002/* return code is 0 for normal exit, 1 for syntax error */
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003003static int parse_stream(o_string *dest, struct p_context *ctx,
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003004 struct in_str *input, const char *end_trigger)
Eric Andersen25f27032001-04-26 23:22:31 +00003005{
"Vladimir N. Oleynik"4ccd2b42006-01-31 09:27:48 +00003006 int ch, m;
Eric Andersen25f27032001-04-26 23:22:31 +00003007 int redir_fd;
3008 redir_type redir_style;
3009 int next;
3010
3011 /* Only double-quote state is handled in the state variable dest->quote.
3012 * A single-quote triggers a bypass of the main loop until its mate is
3013 * found. When recursing, quote state is passed in via dest->quote. */
3014
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003015 debug_printf_parse("parse_stream entered, end_trigger='%s'\n", end_trigger);
3016
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003017 while ((ch = b_getch(input)) != EOF) {
Eric Andersen25f27032001-04-26 23:22:31 +00003018 m = map[ch];
3019 next = (ch == '\n') ? 0 : b_peek(input);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003020 debug_printf_parse(": ch=%c (%d) m=%d quote=%d\n",
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003021 ch, ch, m, dest->quote);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003022 if (m == MAP_ORDINARY
3023 || (m != MAP_NEVER_FLOWTROUGH && dest->quote)
3024 ) {
Eric Andersen25f27032001-04-26 23:22:31 +00003025 b_addqchr(dest, ch, dest->quote);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003026 continue;
3027 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003028 if (m == MAP_IFS_IF_UNQUOTED) {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003029 if (done_word(dest, ctx)) {
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003030 debug_printf_parse("parse_stream return 1\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003031 return 1;
Eric Andersenaac75e52001-04-30 18:18:45 +00003032 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003033 /* If we aren't performing a substitution, treat
3034 * a newline as a command separator.
3035 * [why we don't handle it exactly like ';'? --vda] */
3036 if (end_trigger && ch == '\n') {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003037 done_pipe(ctx, PIPE_SEQ);
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003038 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003039 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003040 if ((end_trigger && strchr(end_trigger, ch))
3041 && !dest->quote && ctx->w == RES_NONE
3042 ) {
3043 debug_printf_parse("parse_stream return 0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003044 return 0;
3045 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003046 if (m == MAP_IFS_IF_UNQUOTED)
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003047 continue;
3048 switch (ch) {
Eric Andersen25f27032001-04-26 23:22:31 +00003049 case '#':
3050 if (dest->length == 0 && !dest->quote) {
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003051 while (1) {
3052 ch = b_peek(input);
3053 if (ch == EOF || ch == '\n')
3054 break;
3055 b_getch(input);
3056 }
Eric Andersen25f27032001-04-26 23:22:31 +00003057 } else {
3058 b_addqchr(dest, ch, dest->quote);
3059 }
3060 break;
3061 case '\\':
3062 if (next == EOF) {
3063 syntax();
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003064 debug_printf_parse("parse_stream return 1\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003065 return 1;
3066 }
3067 b_addqchr(dest, '\\', dest->quote);
3068 b_addqchr(dest, b_getch(input), dest->quote);
3069 break;
3070 case '$':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003071 if (handle_dollar(dest, ctx, input) != 0) {
3072 debug_printf_parse("parse_stream return 1: handle_dollar returned non-0\n");
3073 return 1;
3074 }
Eric Andersen25f27032001-04-26 23:22:31 +00003075 break;
3076 case '\'':
3077 dest->nonnull = 1;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003078 while (1) {
3079 ch = b_getch(input);
3080 if (ch == EOF || ch == '\'')
3081 break;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003082 b_addchr(dest, ch);
Eric Andersen25f27032001-04-26 23:22:31 +00003083 }
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003084 if (ch == EOF) {
Eric Andersen25f27032001-04-26 23:22:31 +00003085 syntax();
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003086 debug_printf_parse("parse_stream return 1\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003087 return 1;
3088 }
3089 break;
3090 case '"':
3091 dest->nonnull = 1;
3092 dest->quote = !dest->quote;
3093 break;
3094 case '`':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003095 process_command_subs(dest, ctx, input, "`");
Eric Andersen25f27032001-04-26 23:22:31 +00003096 break;
3097 case '>':
3098 redir_fd = redirect_opt_num(dest);
3099 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003100 redir_style = REDIRECT_OVERWRITE;
Eric Andersen25f27032001-04-26 23:22:31 +00003101 if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003102 redir_style = REDIRECT_APPEND;
Eric Andersen25f27032001-04-26 23:22:31 +00003103 b_getch(input);
3104 } else if (next == '(') {
3105 syntax(); /* until we support >(list) Process Substitution */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003106 debug_printf_parse("parse_stream return 1: >(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003107 return 1;
3108 }
3109 setup_redirect(ctx, redir_fd, redir_style, input);
3110 break;
3111 case '<':
3112 redir_fd = redirect_opt_num(dest);
3113 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003114 redir_style = REDIRECT_INPUT;
Eric Andersen25f27032001-04-26 23:22:31 +00003115 if (next == '<') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003116 redir_style = REDIRECT_HEREIS;
Eric Andersen25f27032001-04-26 23:22:31 +00003117 b_getch(input);
3118 } else if (next == '>') {
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003119 redir_style = REDIRECT_IO;
Eric Andersen25f27032001-04-26 23:22:31 +00003120 b_getch(input);
3121 } else if (next == '(') {
3122 syntax(); /* until we support <(list) Process Substitution */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003123 debug_printf_parse("parse_stream return 1: <(process) not supported\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003124 return 1;
3125 }
3126 setup_redirect(ctx, redir_fd, redir_style, input);
3127 break;
3128 case ';':
3129 done_word(dest, ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003130 done_pipe(ctx, PIPE_SEQ);
Eric Andersen25f27032001-04-26 23:22:31 +00003131 break;
3132 case '&':
3133 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003134 if (next == '&') {
Eric Andersen25f27032001-04-26 23:22:31 +00003135 b_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003136 done_pipe(ctx, PIPE_AND);
Eric Andersen25f27032001-04-26 23:22:31 +00003137 } else {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003138 done_pipe(ctx, PIPE_BG);
Eric Andersen25f27032001-04-26 23:22:31 +00003139 }
3140 break;
3141 case '|':
3142 done_word(dest, ctx);
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003143 if (next == '|') {
Eric Andersen25f27032001-04-26 23:22:31 +00003144 b_getch(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003145 done_pipe(ctx, PIPE_OR);
Eric Andersen25f27032001-04-26 23:22:31 +00003146 } else {
3147 /* we could pick up a file descriptor choice here
3148 * with redirect_opt_num(), but bash doesn't do it.
3149 * "echo foo 2| cat" yields "foo 2". */
3150 done_command(ctx);
3151 }
3152 break;
3153 case '(':
3154 case '{':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003155 if (parse_group(dest, ctx, input, ch) != 0) {
3156 debug_printf_parse("parse_stream return 1: parse_group returned non-0\n");
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003157 return 1;
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003158 }
Eric Andersen25f27032001-04-26 23:22:31 +00003159 break;
3160 case ')':
3161 case '}':
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003162 syntax(); /* Proper use of this character is caught by end_trigger */
3163 debug_printf_parse("parse_stream return 1: unexpected '}'\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003164 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003165 default:
3166 syntax(); /* this is really an internal logic error */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003167 debug_printf_parse("parse_stream return 1: internal logic error\n");
Eric Andersen25f27032001-04-26 23:22:31 +00003168 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00003169 }
3170 }
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003171 /* Complain if quote? No, maybe we just finished a command substitution
Eric Andersen25f27032001-04-26 23:22:31 +00003172 * that was quoted. Example:
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003173 * $ echo "`cat foo` plus more"
Eric Andersen25f27032001-04-26 23:22:31 +00003174 * and we just got the EOF generated by the subshell that ran "cat foo"
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003175 * The only real complaint is if we got an EOF when end_trigger != NULL,
Eric Andersen25f27032001-04-26 23:22:31 +00003176 * that is, we were really supposed to get end_trigger, and never got
3177 * one before the EOF. Can't use the standard "syntax error" return code,
3178 * so that parse_stream_outer can distinguish the EOF and exit smoothly. */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003179 debug_printf_parse("parse_stream return -%d\n", end_trigger != NULL);
3180 if (end_trigger)
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003181 return -1;
Eric Andersen25f27032001-04-26 23:22:31 +00003182 return 0;
3183}
3184
Eric Andersena68ea1c2006-01-30 22:48:39 +00003185static void mapset(const char *set, int code)
Eric Andersen25f27032001-04-26 23:22:31 +00003186{
Denis Vlasenkof5294e12007-04-14 10:09:57 +00003187 while (*set)
3188 map[(unsigned char)*set++] = code;
Eric Andersen25f27032001-04-26 23:22:31 +00003189}
3190
"Vladimir N. Oleynik"19c37012005-09-22 14:33:15 +00003191static void update_ifs_map(void)
Eric Andersen25f27032001-04-26 23:22:31 +00003192{
3193 /* char *ifs and char map[256] are both globals. */
3194 ifs = getenv("IFS");
Denis Vlasenkoc72c1ed2007-01-30 22:31:26 +00003195 if (ifs == NULL) ifs = " \t\n";
Eric Andersen25f27032001-04-26 23:22:31 +00003196 /* Precompute a list of 'flow through' behavior so it can be treated
3197 * quickly up front. Computation is necessary because of IFS.
3198 * Special case handling of IFS == " \t\n" is not implemented.
3199 * The map[] array only really needs two bits each, and on most machines
3200 * that would be faster because of the reduced L1 cache footprint.
3201 */
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003202 memset(map, MAP_ORDINARY, sizeof(map)); /* most chars flow through always */
3203 mapset("\\$'\"`", MAP_NEVER_FLOWTROUGH);
3204 mapset("<>;&|(){}#", MAP_FLOWTROUGH_IF_QUOTED);
3205 mapset(ifs, MAP_IFS_IF_UNQUOTED); /* also flow through if quoted */
Eric Andersen25f27032001-04-26 23:22:31 +00003206}
3207
Eric Andersenaff114c2004-04-14 17:51:38 +00003208/* most recursion does not come through here, the exception is
Eric Andersen25f27032001-04-26 23:22:31 +00003209 * from builtin_source() */
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003210static int parse_stream_outer(struct in_str *inp, int flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003211{
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003212// FIXME: '{ true | exit 3; echo $? }' is parsed as a whole,
Denis Vlasenko4ac530c2007-05-02 15:35:45 +00003213// as a result $? is replaced by 0, not 3!
3214// Need to stop & execute stuff at ';', not parse till EOL!
3215
Eric Andersen25f27032001-04-26 23:22:31 +00003216 struct p_context ctx;
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003217 o_string temp = NULL_O_STRING;
Eric Andersen25f27032001-04-26 23:22:31 +00003218 int rcode;
3219 do {
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003220 ctx.type = flag;
Eric Andersen25f27032001-04-26 23:22:31 +00003221 initialize_context(&ctx);
3222 update_ifs_map();
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003223 if (!(flag & FLAG_PARSE_SEMICOLON) || (flag & FLAG_REPARSING))
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003224 mapset(";$&|", MAP_ORDINARY);
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003225#if ENABLE_HUSH_INTERACTIVE
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003226 inp->promptmode = 1;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003227#endif
Denis Vlasenkoe725bfe2007-05-03 22:45:39 +00003228 rcode = parse_stream(&temp, &ctx, inp, ";\n");
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003229 if (rcode != 1 && ctx.old_flag != 0) {
3230 syntax();
3231 }
3232 if (rcode != 1 && ctx.old_flag == 0) {
3233 done_word(&temp, &ctx);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003234 done_pipe(&ctx, PIPE_SEQ);
Denis Vlasenkod01ff132007-05-02 21:40:23 +00003235 debug_printf_exec("parse_stream_outer: run_list\n");
Denis Vlasenko400c5b62007-05-04 13:07:27 +00003236 debug_print_tree(ctx.list_head, 0);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003237 run_list(ctx.list_head);
3238 } else {
3239 if (ctx.old_flag != 0) {
3240 free(ctx.stack);
3241 b_reset(&temp);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003242 }
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003243 temp.nonnull = 0;
3244 temp.quote = 0;
3245 inp->p = NULL;
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003246 free_pipe_list(ctx.list_head, 0);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003247 }
Eric Andersena813afc2001-05-24 16:19:36 +00003248 b_free(&temp);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003249 } while (rcode != -1 && !(flag & FLAG_EXIT_FROM_LOOP)); /* loop on syntax errors, return on EOF */
Eric Andersen25f27032001-04-26 23:22:31 +00003250 return 0;
3251}
3252
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003253static int parse_string_outer(const char *s, int flag)
Eric Andersen25f27032001-04-26 23:22:31 +00003254{
3255 struct in_str input;
3256 setup_string_in_str(&input, s);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003257 return parse_stream_outer(&input, flag);
Eric Andersen25f27032001-04-26 23:22:31 +00003258}
3259
3260static int parse_file_outer(FILE *f)
3261{
3262 int rcode;
3263 struct in_str input;
3264 setup_file_in_str(&input, f);
Eric Andersen4c9b68f2002-04-13 12:33:41 +00003265 rcode = parse_stream_outer(&input, FLAG_PARSE_SEMICOLON);
Eric Andersen25f27032001-04-26 23:22:31 +00003266 return rcode;
3267}
3268
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003269#if ENABLE_HUSH_JOB
Eric Andersen6c947d22001-06-25 22:24:38 +00003270/* Make sure we have a controlling tty. If we get started under a job
3271 * aware app (like bash for example), make sure we are now in charge so
3272 * we don't fight over who gets the foreground */
Eric Anderseneaecbf32001-10-31 10:41:31 +00003273static void setup_job_control(void)
Eric Andersen52a97ca2001-06-22 06:49:26 +00003274{
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003275 pid_t shell_pgrp;
3276
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003277 saved_task_pgrp = shell_pgrp = getpgrp();
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003278 debug_printf("saved_task_pgrp=%d\n", saved_task_pgrp);
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003279 fcntl(interactive_fd, F_SETFD, FD_CLOEXEC);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003280
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003281 /* If we were ran as 'hush &',
3282 * sleep until we are in the foreground. */
3283 while (tcgetpgrp(interactive_fd) != shell_pgrp) {
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003284 /* Send TTIN to ourself (should stop us) */
Denis Vlasenkoff131b92007-04-10 15:42:06 +00003285 kill(- shell_pgrp, SIGTTIN);
Denis Vlasenkoa6a17852007-04-28 16:42:11 +00003286 shell_pgrp = getpgrp();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003287 }
Eric Andersen52a97ca2001-06-22 06:49:26 +00003288
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003289 /* Ignore job-control and misc signals. */
3290 set_jobctrl_sighandler(SIG_IGN);
3291 set_misc_sighandler(SIG_IGN);
3292//huh? signal(SIGCHLD, SIG_IGN);
3293
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003294 /* We _must_ restore tty pgrp fatal signals */
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003295 set_fatal_sighandler(sigexit);
Eric Andersen6c947d22001-06-25 22:24:38 +00003296
3297 /* Put ourselves in our own process group. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003298 setpgrp(); /* is the same as setpgid(our_pid, our_pid); */
Eric Andersen6c947d22001-06-25 22:24:38 +00003299 /* Grab control of the terminal. */
Denis Vlasenko3ac0e002007-04-28 16:45:22 +00003300 tcsetpgrp(interactive_fd, getpid());
Eric Andersen6c947d22001-06-25 22:24:38 +00003301}
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003302#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00003303
Denis Vlasenko06af2162007-02-03 17:28:39 +00003304int hush_main(int argc, char **argv);
Matt Kraai2d91deb2001-08-01 17:21:35 +00003305int hush_main(int argc, char **argv)
Eric Andersen25f27032001-04-26 23:22:31 +00003306{
3307 int opt;
3308 FILE *input;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003309 char **e;
Eric Andersenbc604a22001-05-16 05:24:03 +00003310
Denis Vlasenko38f63192007-01-22 09:03:07 +00003311#if ENABLE_FEATURE_EDITING
Denis Vlasenko8e1c7152007-01-22 07:21:38 +00003312 line_input_state = new_line_input_t(FOR_SHELL);
3313#endif
3314
Eric Andersen25f27032001-04-26 23:22:31 +00003315 /* XXX what should these be while sourcing /etc/profile? */
3316 global_argc = argc;
3317 global_argv = argv;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003318
Matt Kraai2d91deb2001-08-01 17:21:35 +00003319 /* (re?) initialize globals. Sometimes hush_main() ends up calling
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003320 * hush_main(), therefore we cannot rely on the BSS to zero out this
Eric Andersen94ac2442001-05-22 19:05:18 +00003321 * stuff. Reset these to 0 every time. */
3322 ifs = NULL;
Eric Andersenaeb44c42001-05-22 20:29:00 +00003323 /* map[] is taken care of with call to update_ifs_map() */
Eric Andersen94ac2442001-05-22 19:05:18 +00003324 fake_mode = 0;
Eric Andersen94ac2442001-05-22 19:05:18 +00003325 close_me_head = NULL;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003326#if ENABLE_HUSH_INTERACTIVE
3327 interactive_fd = 0;
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003328#endif
3329#if ENABLE_HUSH_JOB
Eric Andersen94ac2442001-05-22 19:05:18 +00003330 last_bg_pid = 0;
Eric Andersen52a97ca2001-06-22 06:49:26 +00003331 job_list = NULL;
Eric Andersenc798b072001-06-22 06:23:03 +00003332 last_jobid = 0;
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003333#endif
Eric Andersen94ac2442001-05-22 19:05:18 +00003334
3335 /* Initialize some more globals to non-zero values */
3336 set_cwd();
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003337#if ENABLE_HUSH_INTERACTIVE
3338#if ENABLE_FEATURE_EDITING
3339 cmdedit_set_initial_prompt();
3340#else
3341 PS1 = NULL;
3342#endif
Eric Andersen94ac2442001-05-22 19:05:18 +00003343 PS2 = "> ";
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003344#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003345 /* initialize our shell local variables with the values
Eric Andersen94ac2442001-05-22 19:05:18 +00003346 * currently living in the environment */
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003347 e = environ;
3348 if (e)
3349 while (*e)
3350 set_local_var(*e++, 2); /* without call putenv() */
Eric Andersen94ac2442001-05-22 19:05:18 +00003351
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003352 last_return_code = EXIT_SUCCESS;
Eric Andersen25f27032001-04-26 23:22:31 +00003353
3354 if (argv[0] && argv[0][0] == '-') {
3355 debug_printf("\nsourcing /etc/profile\n");
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003356 input = fopen("/etc/profile", "r");
3357 if (input != NULL) {
Eric Andersena90f20b2001-06-26 23:00:21 +00003358 mark_open(fileno(input));
3359 parse_file_outer(input);
3360 mark_closed(fileno(input));
3361 fclose(input);
3362 }
Eric Andersen25f27032001-04-26 23:22:31 +00003363 }
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003364 input = stdin;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003365
Eric Andersen25f27032001-04-26 23:22:31 +00003366 while ((opt = getopt(argc, argv, "c:xif")) > 0) {
3367 switch (opt) {
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003368 case 'c':
Denis Vlasenko5f786c22007-04-20 08:35:45 +00003369 global_argv = argv + optind;
3370 global_argc = argc - optind;
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003371 opt = parse_string_outer(optarg, FLAG_PARSE_SEMICOLON);
3372 goto final_return;
3373 case 'i':
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003374 // Well, we cannot just declare interactiveness,
3375 // we have to have some stuff (ctty, etc)
3376 /*interactive_fd++;*/
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003377 break;
3378 case 'f':
3379 fake_mode++;
3380 break;
3381 default:
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003382#ifndef BB_VER
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003383 fprintf(stderr, "Usage: sh [FILE]...\n"
3384 " or: sh -c command [args]...\n\n");
3385 exit(EXIT_FAILURE);
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003386#else
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003387 bb_show_usage();
Eric Andersen9ffb7dd2001-05-19 03:00:46 +00003388#endif
Eric Andersen25f27032001-04-26 23:22:31 +00003389 }
3390 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003391#if ENABLE_HUSH_JOB
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003392 /* A shell is interactive if the '-i' flag was given, or if all of
Eric Andersen25f27032001-04-26 23:22:31 +00003393 * the following conditions are met:
Denis Vlasenko55b2de72007-04-18 17:21:28 +00003394 * no -c command
Eric Andersen25f27032001-04-26 23:22:31 +00003395 * no arguments remaining or the -s flag given
3396 * standard input is a terminal
3397 * standard output is a terminal
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003398 * Refer to Posix.2, the description of the 'sh' utility. */
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003399 if (argv[optind] == NULL && input == stdin
3400 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
3401 ) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003402 saved_tty_pgrp = tcgetpgrp(STDIN_FILENO);
3403 debug_printf("saved_tty_pgrp=%d\n", saved_tty_pgrp);
3404 if (saved_tty_pgrp >= 0) {
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003405 /* try to dup to high fd#, >= 255 */
3406 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
3407 if (interactive_fd < 0) {
3408 /* try to dup to any fd */
3409 interactive_fd = dup(STDIN_FILENO);
3410 if (interactive_fd < 0)
3411 /* give up */
3412 interactive_fd = 0;
3413 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003414 // TODO: track & disallow any attempts of user
3415 // to (inadvertently) close/redirect it
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003416 }
Eric Andersen25f27032001-04-26 23:22:31 +00003417 }
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003418 debug_printf("\ninteractive_fd=%d\n", interactive_fd);
3419 if (interactive_fd) {
Eric Andersen25f27032001-04-26 23:22:31 +00003420 /* Looks like they want an interactive shell */
Eric Andersen52a97ca2001-06-22 06:49:26 +00003421 setup_job_control();
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003422 /* Make xfuncs do cleanup on exit */
3423 die_sleep = -1; /* flag */
3424 if (setjmp(die_jmp)) {
3425 /* xfunc has failed! die die die */
3426 hush_exit(xfunc_error_retval);
3427 }
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003428#if !ENABLE_FEATURE_SH_EXTRA_QUIET
Denis Vlasenkoef36ead2007-05-02 15:34:47 +00003429 printf("\n\n%s hush - the humble shell v"HUSH_VER_STR"\n", BB_BANNER);
Denis Vlasenko2f1bb362007-04-21 10:01:14 +00003430 printf("Enter 'help' for a list of built-in commands.\n\n");
3431#endif
Eric Andersenada18ff2001-05-21 16:18:22 +00003432 }
Denis Vlasenkoe3f2f892007-04-28 16:48:27 +00003433#elif ENABLE_HUSH_INTERACTIVE
3434/* no job control compiled, only prompt/line editing */
3435 if (argv[optind] == NULL && input == stdin
3436 && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)
3437 ) {
3438 interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255);
3439 if (interactive_fd < 0) {
3440 /* try to dup to any fd */
3441 interactive_fd = dup(STDIN_FILENO);
3442 if (interactive_fd < 0)
3443 /* give up */
3444 interactive_fd = 0;
3445 }
3446 }
3447
Denis Vlasenkob81b3df2007-04-28 16:48:04 +00003448#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003449
Denis Vlasenkobb81c582007-01-30 22:32:09 +00003450 if (argv[optind] == NULL) {
3451 opt = parse_file_outer(stdin);
Eric Andersene67c3ce2001-05-02 02:09:36 +00003452 goto final_return;
Eric Andersen25f27032001-04-26 23:22:31 +00003453 }
Eric Andersen25f27032001-04-26 23:22:31 +00003454
3455 debug_printf("\nrunning script '%s'\n", argv[optind]);
Denis Vlasenko4c978632007-02-03 03:31:13 +00003456 global_argv = argv + optind;
3457 global_argc = argc - optind;
Rob Landleyd921b2e2006-08-03 15:41:12 +00003458 input = xfopen(argv[optind], "r");
Eric Andersen25f27032001-04-26 23:22:31 +00003459 opt = parse_file_outer(input);
3460
Denis Vlasenko38f63192007-01-22 09:03:07 +00003461#if ENABLE_FEATURE_CLEAN_UP
Eric Andersenaeb44c42001-05-22 20:29:00 +00003462 fclose(input);
Denis Vlasenkofbf6dea2007-04-13 19:56:56 +00003463 if (cwd != bb_msg_unknown)
Eric Andersenaeb44c42001-05-22 20:29:00 +00003464 free((char*)cwd);
3465 {
3466 struct variables *cur, *tmp;
Denis Vlasenko0c886c62007-01-30 22:30:09 +00003467 for (cur = top_vars; cur; cur = tmp) {
Eric Andersenaeb44c42001-05-22 20:29:00 +00003468 tmp = cur->next;
3469 if (!cur->flg_read_only) {
Denis Vlasenko4c978632007-02-03 03:31:13 +00003470 free((char*)cur->name);
3471 free((char*)cur->value);
Eric Andersenaeb44c42001-05-22 20:29:00 +00003472 free(cur);
3473 }
3474 }
3475 }
Eric Andersen25f27032001-04-26 23:22:31 +00003476#endif
3477
Denis Vlasenkof03dbed2007-04-13 19:55:50 +00003478 final_return:
Denis Vlasenko54e7ffb2007-04-21 00:03:36 +00003479 hush_exit(opt ? opt : last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +00003480}