Another update from Larry:
[oweals/busybox.git] / shell / hush.c
1 /* 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
12  *      written Dec 2000 and Jan 2001 by Larry Doolittle.
13  *      The execution engine, the builtins, and much of the underlying
14  *      support has been adapted from busybox-0.49pre's lash,
15  *      which is Copyright (C) 2000 by Lineo, Inc., and
16  *      written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>.
17  *      That, in turn, is based in part on ladsh.c, by Michael K. Johnson and
18  *      Erik W. Troan, which they placed in the public domain.  I don't know
19  *      how much of the Johnson/Troan code has survived the repeated rewrites.
20  * Other credits:
21  *      simple_itoa() was lifted from boa-0.93.15
22  *      b_addchr() derived from similar w_addchar function in glibc-2.2
23  *      setup_redirect(), redirect_opt_num(), and big chunks of main()
24  *        and many builtins derived from contributions by Erik Andersen
25  *      miscellaneous bugfixes from Matt Kraai
26  *
27  * There are two big (and related) architecture differences between
28  * this parser and the lash parser.  One is that this version is
29  * actually designed from the ground up to understand nearly all
30  * of the Bourne grammar.  The second, consequential change is that
31  * the parser and input reader have been turned inside out.  Now,
32  * the parser is in control, and asks for input as needed.  The old
33  * way had the input reader in control, and it asked for parsing to
34  * take place as needed.  The new way makes it much easier to properly
35  * handle the recursion implicit in the various substitutions, especially
36  * across continuation lines.
37  *
38  * Bash grammar not implemented: (how many of these were in original sh?)
39  *      $@ (those sure look like weird quoting rules)
40  *      $_
41  *      ! negation operator for pipes
42  *      &> and >& redirection of stdout+stderr
43  *      Brace Expansion
44  *      Tilde Expansion
45  *      fancy forms of Parameter Expansion
46  *      Arithmetic Expansion
47  *      <(list) and >(list) Process Substitution
48  *      reserved words: case, esac, function
49  *      Here Documents ( << word )
50  *      Functions
51  * Major bugs:
52  *      job handling woefully incomplete and buggy
53  *      reserved word execution woefully incomplete and buggy
54  * to-do:
55  *      port selected bugfixes from post-0.49 busybox lash
56  *      finish implementing reserved words
57  *      handle children going into background
58  *      clean up recognition of null pipes
59  *      have builtin_exec set flag to avoid restore_redirects
60  *      figure out if "echo foo}" is fixable
61  *      check setting of global_argc and global_argv
62  *      control-C handling, probably with longjmp
63  *      VAR=value prefix for simple commands
64  *      follow IFS rules more precisely, including update semantics
65  *      write builtin_eval, builtin_ulimit, builtin_umask
66  *      figure out what to do with backslash-newline
67  *      explain why we use signal instead of sigaction
68  *      propagate syntax errors, die on resource errors?
69  *      continuation lines, both explicit and implicit - done?
70  *      memory leak finding and plugging - done?
71  *      more testing, especially quoting rules and redirection
72  *      maybe change map[] to use 2-bit entries
73  *      (eventually) remove all the printf's
74  *
75  * This program is free software; you can redistribute it and/or modify
76  * it under the terms of the GNU General Public License as published by
77  * the Free Software Foundation; either version 2 of the License, or
78  * (at your option) any later version.
79  *
80  * This program is distributed in the hope that it will be useful,
81  * but WITHOUT ANY WARRANTY; without even the implied warranty of
82  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
83  * General Public License for more details.
84  *
85  * You should have received a copy of the GNU General Public License
86  * along with this program; if not, write to the Free Software
87  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
88  */
89 #include <ctype.h>     /* isalpha, isdigit */
90 #include <unistd.h>    /* getpid */
91 #include <stdlib.h>    /* getenv, atoi */
92 #include <string.h>    /* strchr */
93 #include <stdio.h>     /* popen etc. */
94 #include <glob.h>      /* glob, of course */
95 #include <stdarg.h>    /* va_list */
96 #include <errno.h>
97 #include <fcntl.h>
98 #include <getopt.h>    /* should be pretty obvious */
99
100 #include <sys/types.h>
101 #include <sys/wait.h>
102 #include <signal.h>
103
104 /* #include <dmalloc.h> */
105 /* #define DEBUG_SHELL */
106
107 #ifdef BB_VER
108 #include "busybox.h"
109 #include "cmdedit.h"
110 #else
111 #define applet_name "hush"
112 #include "standalone.h"
113 #define shell_main main
114 #define BB_FEATURE_SH_SIMPLE_PROMPT
115 #endif
116
117 typedef enum {
118         REDIRECT_INPUT     = 1,
119         REDIRECT_OVERWRITE = 2,
120         REDIRECT_APPEND    = 3,
121         REDIRECT_HEREIS    = 4,
122         REDIRECT_IO        = 5
123 } redir_type;
124
125 /* The descrip member of this structure is only used to make debugging
126  * output pretty */
127 struct {int mode; int default_fd; char *descrip;} redir_table[] = {
128         { 0,                         0, "()" },
129         { O_RDONLY,                  0, "<"  },
130         { O_CREAT|O_TRUNC|O_WRONLY,  1, ">"  },
131         { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" },
132         { O_RDONLY,                 -1, "<<" },
133         { O_RDWR,                    1, "<>" }
134 };
135
136 typedef enum {
137         PIPE_SEQ = 1,
138         PIPE_AND = 2,
139         PIPE_OR  = 3,
140         PIPE_BG  = 4,
141 } pipe_style;
142
143 /* might eventually control execution */
144 typedef enum {
145         RES_NONE  = 0,
146         RES_IF    = 1,
147         RES_THEN  = 2,
148         RES_ELIF  = 3,
149         RES_ELSE  = 4,
150         RES_FI    = 5,
151         RES_FOR   = 6,
152         RES_WHILE = 7,
153         RES_UNTIL = 8,
154         RES_DO    = 9,
155         RES_DONE  = 10,
156         RES_XXXX  = 11,
157         RES_SNTX  = 12
158 } reserved_style;
159 #define FLAG_END   (1<<RES_NONE)
160 #define FLAG_IF    (1<<RES_IF)
161 #define FLAG_THEN  (1<<RES_THEN)
162 #define FLAG_ELIF  (1<<RES_ELIF)
163 #define FLAG_ELSE  (1<<RES_ELSE)
164 #define FLAG_FI    (1<<RES_FI)
165 #define FLAG_FOR   (1<<RES_FOR)
166 #define FLAG_WHILE (1<<RES_WHILE)
167 #define FLAG_UNTIL (1<<RES_UNTIL)
168 #define FLAG_DO    (1<<RES_DO)
169 #define FLAG_DONE  (1<<RES_DONE)
170 #define FLAG_START (1<<RES_XXXX)
171
172 /* This holds pointers to the various results of parsing */
173 struct p_context {
174         struct child_prog *child;
175         struct pipe *list_head;
176         struct pipe *pipe;
177         struct redir_struct *pending_redirect;
178         reserved_style w;
179         int old_flag;                           /* for figuring out valid reserved words */
180         struct p_context *stack;
181         /* How about quoting status? */
182 };
183
184 struct redir_struct {
185         redir_type type;                        /* type of redirection */
186         int fd;                                         /* file descriptor being redirected */
187         int dup;                                        /* -1, or file descriptor being duplicated */
188         struct redir_struct *next;      /* pointer to the next redirect in the list */ 
189         glob_t word;                            /* *word.gl_pathv is the filename */
190 };
191
192 struct child_prog {
193         pid_t pid;                                      /* 0 if exited */
194         char **argv;                            /* program name and arguments */
195         struct pipe *group;                     /* if non-NULL, first in group or subshell */
196         int subshell;                           /* flag, non-zero if group must be forked */
197         struct redir_struct *redirects; /* I/O redirections */
198         glob_t glob_result;                     /* result of parameter globbing */
199         int is_stopped;                         /* is the program currently running? */
200         struct pipe *family;            /* pointer back to the child's parent pipe */
201 };
202
203 struct pipe {
204         int jobid;                                      /* job number */
205         int num_progs;                          /* total number of programs in job */
206         int running_progs;                      /* number of programs running */
207         char *text;                                     /* name of job */
208         char *cmdbuf;                           /* buffer various argv's point into */
209         pid_t pgrp;                                     /* process group ID for the job */
210         struct child_prog *progs;       /* array of commands in pipe */
211         struct pipe *next;                      /* to track background commands */
212         int stopped_progs;                      /* number of programs alive, but stopped */
213         int job_context;                        /* bitmask defining current context */
214         pipe_style followup;            /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
215         reserved_style r_mode;          /* supports if, for, while, until */
216         struct jobset *job_list;
217 };
218
219 struct jobset {
220         struct pipe *head;                      /* head of list of running jobs */
221         struct pipe *fg;                        /* current foreground job */
222 };
223
224 struct close_me {
225         int fd;
226         struct close_me *next;
227 };
228
229 /* globals, connect us to the outside world
230  * the first three support $?, $#, and $1 */
231 char **global_argv;
232 unsigned int global_argc;
233 unsigned int last_return_code;
234 extern char **environ; /* This is in <unistd.h>, but protected with __USE_GNU */
235  
236 /* Variables we export */
237 unsigned int shell_context;  /* Used in cmdedit.c to reset the
238                               * context when someone hits ^C */
239
240 /* "globals" within this file */
241 static char *ifs=NULL;
242 static char map[256];
243 static int fake_mode=0;
244 static int interactive=0;
245 static struct close_me *close_me_head = NULL;
246 static char *cwd;
247 /* static struct jobset job_list = { NULL, NULL }; */
248 static unsigned int last_bg_pid=0;
249 static char *PS1;
250 static char *PS2 = "> ";
251
252 #define B_CHUNK (100)
253 #define B_NOSPAC 1
254 #define MAX_LINE 256       /* for cwd */
255 #define MAX_READ 256       /* for builtin_read */
256
257 typedef struct {
258         char *data;
259         int length;
260         int maxlen;
261         int quote;
262         int nonnull;
263 } o_string;
264 #define NULL_O_STRING {NULL,0,0,0,0}
265 /* used for initialization:
266         o_string foo = NULL_O_STRING; */
267
268 /* I can almost use ordinary FILE *.  Is open_memstream() universally
269  * available?  Where is it documented? */
270 struct in_str {
271         const char *p;
272         int __promptme;
273         int promptmode;
274         FILE *file;
275         int (*get) (struct in_str *);
276         int (*peek) (struct in_str *);
277 };
278 #define b_getch(input) ((input)->get(input))
279 #define b_peek(input) ((input)->peek(input))
280
281 #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n"
282
283 struct built_in_command {
284         char *cmd;                                      /* name */
285         char *descr;                            /* description */
286         int (*function) (struct child_prog *);  /* function ptr */
287 };
288
289 /* belongs in busybox.h */
290 static inline int max(int a, int b) {
291         return (a>b)?a:b;
292 }
293
294 /* This should be in utility.c */
295 #ifdef DEBUG_SHELL
296 static void debug_printf(const char *format, ...)
297 {
298         va_list args;
299         va_start(args, format);
300         vfprintf(stderr, format, args);
301         va_end(args);
302 }
303 #else
304 static void debug_printf(const char *format, ...) { }
305 #endif
306 #define final_printf debug_printf
307
308 void __syntax(char *file, int line) {
309         fprintf(stderr,"syntax error %s:%d\n",file,line);
310 }
311 #define syntax() __syntax(__FILE__, __LINE__)
312
313 /* Index of subroutines: */
314 /*   function prototypes for builtins */
315 static int builtin_cd(struct child_prog *child);
316 static int builtin_env(struct child_prog *child);
317 static int builtin_exec(struct child_prog *child);
318 static int builtin_exit(struct child_prog *child);
319 static int builtin_export(struct child_prog *child);
320 static int builtin_fg_bg(struct child_prog *child);
321 static int builtin_help(struct child_prog *child);
322 static int builtin_jobs(struct child_prog *child);
323 static int builtin_pwd(struct child_prog *child);
324 static int builtin_read(struct child_prog *child);
325 static int builtin_shift(struct child_prog *child);
326 static int builtin_source(struct child_prog *child);
327 static int builtin_ulimit(struct child_prog *child);
328 static int builtin_umask(struct child_prog *child);
329 static int builtin_unset(struct child_prog *child);
330 /*   o_string manipulation: */
331 static int b_check_space(o_string *o, int len);
332 static int b_addchr(o_string *o, int ch);
333 static void b_reset(o_string *o);
334 static int b_addqchr(o_string *o, int ch, int quote);
335 static int b_adduint(o_string *o, unsigned int i);
336 /*  in_str manipulations: */
337 static int static_get(struct in_str *i);
338 static int static_peek(struct in_str *i);
339 static int file_get(struct in_str *i);
340 static int file_peek(struct in_str *i);
341 static void setup_file_in_str(struct in_str *i, FILE *f);
342 static void setup_string_in_str(struct in_str *i, const char *s);
343 /*  close_me manipulations: */
344 static void mark_open(int fd);
345 static void mark_closed(int fd);
346 static void close_all();
347 /*  "run" the final data structures: */
348 static char *indenter(int i);
349 static int run_list_test(struct pipe *head, int indent);
350 static int run_pipe_test(struct pipe *pi, int indent);
351 /*  really run the final data structures: */
352 static int setup_redirects(struct child_prog *prog, int squirrel[]);
353 static int pipe_wait(struct pipe *pi);
354 static int run_list_real(struct pipe *pi);
355 static void pseudo_exec(struct child_prog *child) __attribute__ ((noreturn));
356 static int run_pipe_real(struct pipe *pi);
357 /*   extended glob support: */
358 static int globhack(const char *src, int flags, glob_t *pglob);
359 static int glob_needed(const char *s);
360 static int xglob(o_string *dest, int flags, glob_t *pglob);
361 /*   data structure manipulation: */
362 static int setup_redirect(struct p_context *ctx, int fd, redir_type style, struct in_str *input);
363 static void initialize_context(struct p_context *ctx);
364 static int done_word(o_string *dest, struct p_context *ctx);
365 static int done_command(struct p_context *ctx);
366 static int done_pipe(struct p_context *ctx, pipe_style type);
367 /*   primary string parsing: */
368 static int redirect_dup_num(struct in_str *input);
369 static int redirect_opt_num(o_string *o);
370 static int process_command_subs(o_string *dest, struct p_context *ctx, struct in_str *input, int subst_end);
371 static int parse_group(o_string *dest, struct p_context *ctx, struct in_str *input, int ch);
372 static void lookup_param(o_string *dest, struct p_context *ctx, o_string *src);
373 static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input);
374 static int parse_string(o_string *dest, struct p_context *ctx, const char *src);
375 static int parse_stream(o_string *dest, struct p_context *ctx, struct in_str *input0, int end_trigger);
376 /*   setup: */
377 static int parse_stream_outer(struct in_str *inp);
378 static int parse_string_outer(const char *s);
379 static int parse_file_outer(FILE *f);
380
381 /* Table of built-in functions.  They can be forked or not, depending on
382  * context: within pipes, they fork.  As simple commands, they do not.
383  * When used in non-forking context, they can change global variables
384  * in the parent shell process.  If forked, of course they can not.
385  * For example, 'unset foo | whatever' will parse and run, but foo will
386  * still be set at the end. */
387 static struct built_in_command bltins[] = {
388         {"bg", "Resume a job in the background", builtin_fg_bg},
389         {"cd", "Change working directory", builtin_cd},
390         {"env", "Print all environment variables", builtin_env},
391         {"exec", "Exec command, replacing this shell with the exec'd process", builtin_exec},
392         {"exit", "Exit from shell()", builtin_exit},
393         {"export", "Set environment variable", builtin_export},
394         {"fg", "Bring job into the foreground", builtin_fg_bg},
395         {"jobs", "Lists the active jobs", builtin_jobs},
396         {"pwd", "Print current directory", builtin_pwd},
397         {"read", "Input environment variable", builtin_read},
398         {"shift", "Shift positional parameters", builtin_shift},
399         {"ulimit","Controls resource limits", builtin_ulimit},
400         {"umask","Sets file creation mask", builtin_umask},
401         {"unset", "Unset environment variable", builtin_unset},
402         {".", "Source-in and run commands in a file", builtin_source},
403         {"help", "List shell built-in commands", builtin_help},
404         {NULL, NULL, NULL}
405 };
406
407 /* built-in 'cd <path>' handler */
408 static int builtin_cd(struct child_prog *child)
409 {
410         char *newdir;
411         if (child->argv[1] == NULL)
412                 newdir = getenv("HOME");
413         else
414                 newdir = child->argv[1];
415         if (chdir(newdir)) {
416                 printf("cd: %s: %s\n", newdir, strerror(errno));
417                 return EXIT_FAILURE;
418         }
419         getcwd(cwd, sizeof(char)*MAX_LINE);
420         return EXIT_SUCCESS;
421 }
422
423 /* built-in 'env' handler */
424 static int builtin_env(struct child_prog *dummy)
425 {
426         char **e = environ;
427         if (e == NULL) return EXIT_FAILURE;
428         for (; *e; e++) {
429                 puts(*e);
430         }
431         return EXIT_SUCCESS;
432 }
433
434 /* built-in 'exec' handler */
435 static int builtin_exec(struct child_prog *child)
436 {
437         if (child->argv[1] == NULL)
438                 return EXIT_SUCCESS;   /* Really? */
439         child->argv++;
440         pseudo_exec(child);
441         /* never returns */
442 }
443
444 /* built-in 'exit' handler */
445 static int builtin_exit(struct child_prog *child)
446 {
447         if (child->argv[1] == NULL)
448                 exit(last_return_code);
449         exit (atoi(child->argv[1]));
450 }
451
452 /* built-in 'export VAR=value' handler */
453 static int builtin_export(struct child_prog *child)
454 {
455         int res;
456
457         if (child->argv[1] == NULL) {
458                 return (builtin_env(child));
459         }
460         res = putenv(child->argv[1]);
461         if (res)
462                 fprintf(stderr, "export: %s\n", strerror(errno));
463         return (res);
464 }
465
466 /* built-in 'fg' and 'bg' handler */
467 static int builtin_fg_bg(struct child_prog *child)
468 {
469         int i, jobNum;
470         struct pipe *job=NULL;
471         
472         if (!child->argv[1] || child->argv[2]) {
473                 error_msg("%s: exactly one argument is expected\n",
474                                 child->argv[0]);
475                 return EXIT_FAILURE;
476         }
477
478         if (sscanf(child->argv[1], "%%%d", &jobNum) != 1) {
479                 error_msg("%s: bad argument '%s'\n",
480                                 child->argv[0], child->argv[1]);
481                 return EXIT_FAILURE;
482         }
483
484         for (job = child->family->job_list->head; job; job = job->next) {
485                 if (job->jobid == jobNum) {
486                         break;
487                 }
488         }
489
490         if (!job) {
491                 error_msg("%s: unknown job %d\n",
492                                 child->argv[0], jobNum);
493                 return EXIT_FAILURE;
494         }
495
496         if (*child->argv[0] == 'f') {
497                 /* Make this job the foreground job */
498                 /* suppress messages when run from /linuxrc mag@sysgo.de */
499                 if (tcsetpgrp(0, job->pgrp) && errno != ENOTTY)
500                         perror_msg("tcsetpgrp"); 
501                 child->family->job_list->fg = job;
502         }
503
504         /* Restart the processes in the job */
505         for (i = 0; i < job->num_progs; i++)
506                 job->progs[i].is_stopped = 0;
507
508         kill(-job->pgrp, SIGCONT);
509
510         job->stopped_progs = 0;
511         return EXIT_SUCCESS;
512 }
513
514 /* built-in 'help' handler */
515 static int builtin_help(struct child_prog *dummy)
516 {
517         struct built_in_command *x;
518
519         printf("\nBuilt-in commands:\n");
520         printf("-------------------\n");
521         for (x = bltins; x->cmd; x++) {
522                 if (x->descr==NULL)
523                         continue;
524                 printf("%s\t%s\n", x->cmd, x->descr);
525         }
526         printf("\n\n");
527         return EXIT_SUCCESS;
528 }
529
530 /* built-in 'jobs' handler */
531 static int builtin_jobs(struct child_prog *child)
532 {
533         struct pipe *job;
534         char *status_string;
535
536         for (job = child->family->job_list->head; job; job = job->next) {
537                 if (job->running_progs == job->stopped_progs)
538                         status_string = "Stopped";
539                 else
540                         status_string = "Running";
541                 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->text);
542         }
543         return EXIT_SUCCESS;
544 }
545
546
547 /* built-in 'pwd' handler */
548 static int builtin_pwd(struct child_prog *dummy)
549 {
550         getcwd(cwd, MAX_LINE);
551         puts(cwd);
552         return EXIT_SUCCESS;
553 }
554
555 /* built-in 'read VAR' handler */
556 static int builtin_read(struct child_prog *child)
557 {
558         int res = 0, len, newlen;
559         char *s;
560         char string[MAX_READ];
561
562         if (child->argv[1]) {
563                 /* argument (VAR) given: put "VAR=" into buffer */
564                 strcpy(string, child->argv[1]);
565                 len = strlen(string);
566                 string[len++] = '=';
567                 string[len]   = '\0';
568                 /* XXX would it be better to go through in_str? */
569                 fgets(&string[len], sizeof(string) - len, stdin);       /* read string */
570                 newlen = strlen(string);
571                 if(newlen > len)
572                         string[--newlen] = '\0';        /* chomp trailing newline */
573                 /*
574                 ** string should now contain "VAR=<value>"
575                 ** copy it (putenv() won't do that, so we must make sure
576                 ** the string resides in a static buffer!)
577                 */
578                 res = -1;
579                 if((s = strdup(string)))
580                         res = putenv(s);
581                 if (res)
582                         fprintf(stderr, "read: %s\n", strerror(errno));
583         }
584         else
585                 fgets(string, sizeof(string), stdin);
586
587         return (res);
588 }
589
590 /* Built-in 'shift' handler */
591 static int builtin_shift(struct child_prog *child)
592 {
593         int n=1;
594         if (child->argv[1]) {
595                 n=atoi(child->argv[1]);
596         }
597         if (n>=0 && n<global_argc) {
598                 /* XXX This probably breaks $0 */
599                 global_argc -= n;
600                 global_argv += n;
601                 return EXIT_SUCCESS;
602         } else {
603                 return EXIT_FAILURE;
604         }
605 }
606
607 /* Built-in '.' handler (read-in and execute commands from file) */
608 static int builtin_source(struct child_prog *child)
609 {
610         FILE *input;
611         int status;
612
613         if (child->argv[1] == NULL)
614                 return EXIT_FAILURE;
615
616         /* XXX search through $PATH is missing */
617         input = fopen(child->argv[1], "r");
618         if (!input) {
619                 fprintf(stderr, "Couldn't open file '%s'\n", child->argv[1]);
620                 return EXIT_FAILURE;
621         }
622
623         /* Now run the file */
624         /* XXX argv and argc are broken; need to save old global_argv
625          * (pointer only is OK!) on this stack frame,
626          * set global_argv=child->argv+1, recurse, and restore. */
627         mark_open(fileno(input));
628         status = parse_file_outer(input);
629         mark_closed(fileno(input));
630         fclose(input);
631         return (status);
632 }
633
634 static int builtin_ulimit(struct child_prog *child)
635 {
636         printf("builtin_ulimit not written\n");
637         return EXIT_FAILURE;
638 }
639
640 static int builtin_umask(struct child_prog *child)
641 {
642         printf("builtin_umask not written\n");
643         return EXIT_FAILURE;
644 }
645
646 /* built-in 'unset VAR' handler */
647 static int builtin_unset(struct child_prog *child)
648 {
649         if (child->argv[1] == NULL) {
650                 fprintf(stderr, "unset: parameter required.\n");
651                 return EXIT_FAILURE;
652         }
653         unsetenv(child->argv[1]);
654         return EXIT_SUCCESS;
655 }
656
657 static int b_check_space(o_string *o, int len)
658 {
659         /* It would be easy to drop a more restrictive policy
660          * in here, such as setting a maximum string length */
661         if (o->length + len > o->maxlen) {
662                 char *old_data = o->data;
663                 /* assert (data == NULL || o->maxlen != 0); */
664                 o->maxlen += max(2*len, B_CHUNK);
665                 o->data = realloc(o->data, 1 + o->maxlen);
666                 if (o->data == NULL) {
667                         free(old_data);
668                 }
669         }
670         return o->data == NULL;
671 }
672
673 static int b_addchr(o_string *o, int ch)
674 {
675         debug_printf("b_addchr: %c %d %p\n", ch, o->length, o);
676         if (b_check_space(o, 1)) return B_NOSPAC;
677         o->data[o->length] = ch;
678         o->length++;
679         o->data[o->length] = '\0';
680         return 0;
681 }
682
683 static void b_reset(o_string *o)
684 {
685         o->length = 0;
686         o->nonnull = 0;
687         if (o->data != NULL) *o->data = '\0';
688 }
689
690 static void b_free(o_string *o)
691 {
692         b_reset(o);
693         if (o->data != NULL) free(o->data);
694         o->data = NULL;
695         o->maxlen = 0;
696 }
697
698 /* My analysis of quoting semantics tells me that state information
699  * is associated with a destination, not a source.
700  */
701 static int b_addqchr(o_string *o, int ch, int quote)
702 {
703         if (quote && strchr("*?[\\",ch)) {
704                 int rc;
705                 rc = b_addchr(o, '\\');
706                 if (rc) return rc;
707         }
708         return b_addchr(o, ch);
709 }
710
711 /* belongs in utility.c */
712 char *simple_itoa(unsigned int i)
713 {
714         /* 21 digits plus null terminator, good for 64-bit or smaller ints */
715         static char local[22];
716         char *p = &local[21];
717         *p-- = '\0';
718         do {
719                 *p-- = '0' + i % 10;
720                 i /= 10;
721         } while (i > 0);
722         return p + 1;
723 }
724
725 static int b_adduint(o_string *o, unsigned int i)
726 {
727         int r;
728         char *p = simple_itoa(i);
729         /* no escape checking necessary */
730         do r=b_addchr(o, *p++); while (r==0 && *p);
731         return r;
732 }
733
734 static int static_get(struct in_str *i)
735 {
736         int ch=*i->p++;
737         if (ch=='\0') return EOF;
738         return ch;
739 }
740
741 static int static_peek(struct in_str *i)
742 {
743         return *i->p;
744 }
745
746 static inline void cmdedit_set_initial_prompt(void)
747 {
748 #ifdef BB_FEATURE_SH_SIMPLE_PROMPT
749         PS1 = NULL;
750 #else
751         PS1 = getenv("PS1");
752         if(PS1==0)
753                 PS1 = "\\w \\$ ";
754 #endif  
755 }
756
757 static inline void setup_prompt_string(int promptmode, char **prompt_str)
758 {
759         debug_printf("setup_prompt_string %d ",promptmode);
760 #ifdef BB_FEATURE_SH_SIMPLE_PROMPT
761         /* Set up the prompt */
762         if (promptmode == 1) {
763                 if (PS1)
764                         free(PS1);
765                 PS1=xmalloc(strlen(cwd)+4);
766                 sprintf(PS1, "%s %s", cwd, ( geteuid() != 0 ) ?  "$ ":"# ");
767                 *prompt_str = PS1;
768         } else {
769                 *prompt_str = PS2;
770         }
771 #else
772         *prompt_str = (promptmode==0)? PS1 : PS2;
773 #endif
774         debug_printf("result %s\n",*prompt_str);
775 }
776
777 static void get_user_input(struct in_str *i)
778 {
779         char *prompt_str;
780         static char the_command[BUFSIZ];
781
782         setup_prompt_string(i->promptmode, &prompt_str);
783 #ifdef BB_FEATURE_COMMAND_EDITING
784         /*
785          ** enable command line editing only while a command line
786          ** is actually being read; otherwise, we'll end up bequeathing
787          ** atexit() handlers and other unwanted stuff to our
788          ** child processes (rob@sysgo.de)
789          */
790         cmdedit_read_input(prompt_str, the_command);
791         cmdedit_terminate();
792 #else
793         fputs(prompt_str, stdout);
794         fflush(stdout);
795         the_command[0]=fgetc(i->file);
796         the_command[1]='\0';
797 #endif
798         i->p = the_command;
799 }
800
801 /* This is the magic location that prints prompts 
802  * and gets data back from the user */
803 static int file_get(struct in_str *i)
804 {
805         int ch;
806
807         ch = 0;
808         /* If there is data waiting, eat it up */
809         if (i->p && *i->p) {
810                 ch=*i->p++;
811         } else {
812                 /* need to double check i->file because we might be doing something
813                  * more complicated by now, like sourcing or substituting. */
814                 if (i->__promptme && interactive && i->file == stdin) {
815                         get_user_input(i);
816                         i->promptmode=2;
817                         i->__promptme = 0;
818                         if (i->p && *i->p) {
819                                 ch=*i->p++;
820                         }
821                 } else {
822                         ch = fgetc(i->file);
823                 }
824
825                 debug_printf("b_getch: got a %d\n", ch);
826         }
827         if (ch == '\n') i->__promptme=1;
828         return ch;
829 }
830
831 /* All the callers guarantee this routine will never be
832  * used right after a newline, so prompting is not needed.
833  */
834 static int file_peek(struct in_str *i)
835 {
836         if (i->p && *i->p) {
837                 return *i->p;
838         } else {
839                 static char buffer[2];
840                 buffer[0] = fgetc(i->file);
841                 buffer[1] = '\0';
842                 i->p = buffer;
843                 debug_printf("b_peek: got a %d\n", *i->p);
844                 return *i->p; 
845         }
846 }
847
848 static void setup_file_in_str(struct in_str *i, FILE *f)
849 {
850         i->peek = file_peek;
851         i->get = file_get;
852         i->__promptme=1;
853         i->promptmode=1;
854         i->file = f;
855         i->p = NULL;
856 }
857
858 static void setup_string_in_str(struct in_str *i, const char *s)
859 {
860         i->peek = static_peek;
861         i->get = static_get;
862         i->__promptme=1;
863         i->promptmode=1;
864         i->p = s;
865 }
866
867 static void mark_open(int fd)
868 {
869         struct close_me *new = xmalloc(sizeof(struct close_me));
870         new->fd = fd;
871         new->next = close_me_head;
872         close_me_head = new;
873 }
874
875 static void mark_closed(int fd)
876 {
877         struct close_me *tmp;
878         if (close_me_head == NULL || close_me_head->fd != fd)
879                 error_msg_and_die("corrupt close_me");
880         tmp = close_me_head;
881         close_me_head = close_me_head->next;
882         free(tmp);
883 }
884
885 static void close_all()
886 {
887         struct close_me *c;
888         for (c=close_me_head; c; c=c->next) {
889                 close(c->fd);
890         }
891         close_me_head = NULL;
892 }
893
894 /* squirrel != NULL means we squirrel away copies of stdin, stdout,
895  * and stderr if they are redirected. */
896 static int setup_redirects(struct child_prog *prog, int squirrel[])
897 {
898         int openfd, mode;
899         struct redir_struct *redir;
900
901         for (redir=prog->redirects; redir; redir=redir->next) {
902                 if (redir->dup == -1) {
903                         mode=redir_table[redir->type].mode;
904                         openfd = open(redir->word.gl_pathv[0], mode, 0666);
905                         if (openfd < 0) {
906                         /* this could get lost if stderr has been redirected, but
907                            bash and ash both lose it as well (though zsh doesn't!) */
908                                 fprintf(stderr,"error opening %s: %s\n", redir->word.gl_pathv[0],
909                                         strerror(errno));
910                                 return 1;
911                         }
912                 } else {
913                         openfd = redir->dup;
914                 }
915
916                 if (openfd != redir->fd) {
917                         if (squirrel && redir->fd < 3) {
918                                 squirrel[redir->fd] = dup(redir->fd);
919                         }
920                         dup2(openfd, redir->fd);
921                         close(openfd);
922                 }
923         }
924         return 0;
925 }
926
927 static void restore_redirects(int squirrel[])
928 {
929         int i, fd;
930         for (i=0; i<3; i++) {
931                 fd = squirrel[i];
932                 if (fd != -1) {
933                         /* No error checking.  I sure wouldn't know what
934                          * to do with an error if I found one! */
935                         dup2(fd, i);
936                         close(fd);
937                 }
938         }
939 }
940
941 /* XXX this definitely needs some more thought, work, and
942  * cribbing from other shells */
943 static int pipe_wait(struct pipe *pi)
944 {
945         int rcode=0, i, pid, running, status;
946         running = pi->num_progs;
947         while (running) {
948                 pid=waitpid(-1, &status, 0);
949                 if (pid < 0) perror_msg_and_die("waitpid");
950                 for (i=0; i < pi->num_progs; i++) {
951                         if (pi->progs[i].pid == pid) {
952                                 if (i==pi->num_progs-1) rcode=WEXITSTATUS(status);
953                                 pi->progs[i].pid = 0;
954                                 running--;
955                                 break;
956                         }
957                 }
958         }
959         return rcode;
960 }
961
962 /* very simple version for testing */
963 static void pseudo_exec(struct child_prog *child)
964 {
965         int rcode;
966         struct built_in_command *x;
967         if (child->argv) {
968                 /*
969                  * Check if the command matches any of the builtins.
970                  * Depending on context, this might be redundant.  But it's
971                  * easier to waste a few CPU cycles than it is to figure out
972                  * if this is one of those cases.
973                  */
974                 for (x = bltins; x->cmd; x++) {
975                         if (strcmp(child->argv[0], x->cmd) == 0 ) {
976                                 debug_printf("builtin exec %s\n", child->argv[0]);
977                                 exit(x->function(child));
978                         }
979                 }
980
981                 /* Check if the command matches any busybox internal commands
982                  * ("applets") here.  
983                  * FIXME: This feature is not 100% safe, since
984                  * BusyBox is not fully reentrant, so we have no guarantee the things
985                  * from the .bss are still zeroed, or that things from .data are still
986                  * at their defaults.  We could exec ourself from /proc/self/exe, but I
987                  * really dislike relying on /proc for things.  We could exec ourself
988                  * from global_argv[0], but if we are in a chroot, we may not be able
989                  * to find ourself... */ 
990 #ifdef BB_FEATURE_SH_STANDALONE_SHELL
991                 {
992                         int argc_l;
993                         char** argv_l=child->argv;
994                         char *name = child->argv[0];
995
996 #ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
997                         /* Following discussions from November 2000 on the busybox mailing
998                          * list, the default configuration, (without
999                          * get_last_path_component()) lets the user force use of an
1000                          * external command by specifying the full (with slashes) filename.
1001                          * If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then applets
1002                          * _aways_ override external commands, so if you want to run
1003                          * /bin/cat, it will use BusyBox cat even if /bin/cat exists on the
1004                          * filesystem and is _not_ busybox.  Some systems may want this,
1005                          * most do not.  */
1006                         name = get_last_path_component(name);
1007 #endif
1008                         /* Count argc for use in a second... */
1009                         for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
1010                         optind = 1;
1011                         debug_printf("running applet %s\n", name);
1012                         run_applet_by_name(name, argc_l, child->argv);
1013                         exit(1);
1014                 }
1015 #endif
1016                 debug_printf("exec of %s\n",child->argv[0]);
1017                 execvp(child->argv[0],child->argv);
1018                 perror("execvp");
1019                 exit(1);
1020         } else if (child->group) {
1021                 debug_printf("runtime nesting to group\n");
1022                 interactive=0;    /* crucial!!!! */
1023                 rcode = run_list_real(child->group);
1024                 /* OK to leak memory by not calling run_list_test,
1025                  * since this process is about to exit */
1026                 exit(rcode);
1027         } else {
1028                 /* Can happen.  See what bash does with ">foo" by itself. */
1029                 debug_printf("trying to pseudo_exec null command\n");
1030                 exit(EXIT_SUCCESS);
1031         }
1032 }
1033
1034 /* run_pipe_real() starts all the jobs, but doesn't wait for anything
1035  * to finish.  See pipe_wait().
1036  *
1037  * return code is normally -1, when the caller has to wait for children
1038  * to finish to determine the exit status of the pipe.  If the pipe
1039  * is a simple builtin command, however, the action is done by the
1040  * time run_pipe_real returns, and the exit code is provided as the
1041  * return value.
1042  *
1043  * The input of the pipe is always stdin, the output is always
1044  * stdout.  The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
1045  * because it tries to avoid running the command substitution in
1046  * subshell, when that is in fact necessary.  The subshell process
1047  * now has its stdout directed to the input of the appropriate pipe,
1048  * so this routine is noticeably simpler.
1049  */
1050 static int run_pipe_real(struct pipe *pi)
1051 {
1052         int i;
1053         int nextin, nextout;
1054         int pipefds[2];                         /* pipefds[0] is for reading */
1055         struct child_prog *child;
1056         struct built_in_command *x;
1057
1058         nextin = 0;
1059         pi->pgrp = 0;
1060
1061         /* Check if this is a simple builtin (not part of a pipe).
1062          * Builtins within pipes have to fork anyway, and are handled in
1063          * pseudo_exec.  "echo foo | read bar" doesn't work on bash, either.
1064          */
1065         if (pi->num_progs == 1 && pi->progs[0].argv != NULL) {
1066                 child = & (pi->progs[0]);
1067                 if (child->group && ! child->subshell) {
1068                         int squirrel[] = {-1, -1, -1};
1069                         int rcode;
1070                         debug_printf("non-subshell grouping\n");
1071                         setup_redirects(child, squirrel);
1072                         /* XXX could we merge code with following builtin case,
1073                          * by creating a pseudo builtin that calls run_list_real? */
1074                         rcode = run_list_real(child->group);
1075                         restore_redirects(squirrel);
1076                         return rcode;
1077                 }
1078                 for (x = bltins; x->cmd; x++) {
1079                         if (strcmp(child->argv[0], x->cmd) == 0 ) {
1080                                 int squirrel[] = {-1, -1, -1};
1081                                 int rcode;
1082                                 debug_printf("builtin inline %s\n", child->argv[0]);
1083                                 /* XXX setup_redirects acts on file descriptors, not FILEs.
1084                                  * This is perfect for work that comes after exec().
1085                                  * Is it really safe for inline use?  Experimentally,
1086                                  * things seem to work with glibc. */
1087                                 setup_redirects(child, squirrel);
1088                                 rcode = x->function(child);
1089                                 restore_redirects(squirrel);
1090                                 return rcode;
1091                         }
1092                 }
1093         }
1094
1095         for (i = 0; i < pi->num_progs; i++) {
1096                 child = & (pi->progs[i]);
1097
1098                 /* pipes are inserted between pairs of commands */
1099                 if ((i + 1) < pi->num_progs) {
1100                         if (pipe(pipefds)<0) perror_msg_and_die("pipe");
1101                         nextout = pipefds[1];
1102                 } else {
1103                         nextout=1;
1104                         pipefds[0] = -1;
1105                 }
1106
1107                 /* XXX test for failed fork()? */
1108                 if (!(child->pid = fork())) {
1109                         close_all();
1110
1111                         if (nextin != 0) {
1112                                 dup2(nextin, 0);
1113                                 close(nextin);
1114                         }
1115                         if (nextout != 1) {
1116                                 dup2(nextout, 1);
1117                                 close(nextout);
1118                         }
1119                         if (pipefds[0]!=-1) {
1120                                 close(pipefds[0]);  /* opposite end of our output pipe */
1121                         }
1122
1123                         /* Like bash, explicit redirects override pipes,
1124                          * and the pipe fd is available for dup'ing. */
1125                         setup_redirects(child,NULL);
1126
1127                         pseudo_exec(child);
1128                 }
1129                 if (interactive) {
1130                         /* Put our child in the process group whose leader is the
1131                          * first process in this pipe. */
1132                         if (pi->pgrp==0) {
1133                                 pi->pgrp = child->pid;
1134                         }
1135                         /* Don't check for errors.  The child may be dead already,
1136                          * in which case setpgid returns error code EACCES. */
1137                         setpgid(child->pid, pi->pgrp);
1138                 }
1139                 /* In the non-interactive case, do nothing.  Leave the children
1140                  * with the process group that they inherited from us. */
1141         
1142                 if (nextin != 0)
1143                         close(nextin);
1144                 if (nextout != 1)
1145                         close(nextout);
1146
1147                 /* If there isn't another process, nextin is garbage 
1148                    but it doesn't matter */
1149                 nextin = pipefds[0];
1150         }
1151         return -1;
1152 }
1153
1154 static int run_list_real(struct pipe *pi)
1155 {
1156         int rcode=0;
1157         int if_code=0, next_if_code=0;  /* need double-buffer to handle elif */
1158         reserved_style rmode, skip_more_in_this_rmode=RES_XXXX;
1159         for (;pi;pi=pi->next) {
1160                 rmode = pi->r_mode;
1161                 debug_printf("rmode=%d  if_code=%d  next_if_code=%d skip_more=%d\n", rmode, if_code, next_if_code, skip_more_in_this_rmode);
1162                 if (rmode == skip_more_in_this_rmode) continue;
1163                 skip_more_in_this_rmode = RES_XXXX;
1164                 if (rmode == RES_THEN || rmode == RES_ELSE) if_code = next_if_code;
1165                 if (rmode == RES_THEN &&  if_code) continue;
1166                 if (rmode == RES_ELSE && !if_code) continue;
1167                 if (rmode == RES_ELIF && !if_code) continue;
1168                 if (pi->num_progs == 0) continue;
1169                 rcode = run_pipe_real(pi);
1170                 if (rcode!=-1) {
1171                         /* We only ran a builtin: rcode was set by the return value
1172                          * of run_pipe_real(), and we don't need to wait for anything. */
1173                 } else if (pi->followup==PIPE_BG) {
1174                         /* XXX check bash's behavior with nontrivial pipes */
1175                         /* XXX compute jobid */
1176                         /* XXX what does bash do with attempts to background builtins? */
1177                         printf("[%d] %d\n", pi->jobid, pi->pgrp);
1178                         last_bg_pid = pi->pgrp;
1179                         rcode = EXIT_SUCCESS;
1180                 } else {
1181                         if (interactive) {
1182                                 /* move the new process group into the foreground */
1183                                 /* suppress messages when run from /linuxrc mag@sysgo.de */
1184                                 signal(SIGTTIN, SIG_IGN);
1185                                 signal(SIGTTOU, SIG_IGN);
1186                                 if (tcsetpgrp(0, pi->pgrp) && errno != ENOTTY)
1187                                         perror_msg("tcsetpgrp");
1188                                 rcode = pipe_wait(pi);
1189                                 if (tcsetpgrp(0, getpid()) && errno != ENOTTY)
1190                                         perror_msg("tcsetpgrp");
1191                                 signal(SIGTTIN, SIG_DFL);
1192                                 signal(SIGTTOU, SIG_DFL);
1193                         } else {
1194                                 rcode = pipe_wait(pi);
1195                         }
1196                 }
1197                 last_return_code=rcode;
1198                 if ( rmode == RES_IF || rmode == RES_ELIF )
1199                         next_if_code=rcode;  /* can be overwritten a number of times */
1200                 if ( (rcode==EXIT_SUCCESS && pi->followup==PIPE_OR) ||
1201                      (rcode!=EXIT_SUCCESS && pi->followup==PIPE_AND) )
1202                         skip_more_in_this_rmode=rmode;
1203                         /* return rcode; */ /* XXX broken if list is part of if/then/else */
1204         }
1205         return rcode;
1206 }
1207
1208 /* broken, of course, but OK for testing */
1209 static char *indenter(int i)
1210 {
1211         static char blanks[]="                                    ";
1212         return &blanks[sizeof(blanks)-i-1];
1213 }
1214
1215 /* return code is the exit status of the pipe */
1216 static int run_pipe_test(struct pipe *pi, int indent)
1217 {
1218         char **p;
1219         struct child_prog *child;
1220         struct redir_struct *r, *rnext;
1221         int a, i, ret_code=0;
1222         char *ind = indenter(indent);
1223         final_printf("%s run pipe: (pid %d)\n",ind,getpid());
1224         for (i=0; i<pi->num_progs; i++) {
1225                 child = &pi->progs[i];
1226                 final_printf("%s  command %d:\n",ind,i);
1227                 if (child->argv) {
1228                         for (a=0,p=child->argv; *p; a++,p++) {
1229                                 final_printf("%s   argv[%d] = %s\n",ind,a,*p);
1230                         }
1231                         globfree(&child->glob_result);
1232                         child->argv=NULL;
1233                 } else if (child->group) {
1234                         final_printf("%s   begin group (subshell:%d)\n",ind, child->subshell);
1235                         ret_code = run_list_test(child->group,indent+3);
1236                         final_printf("%s   end group\n",ind);
1237                 } else {
1238                         final_printf("%s   (nil)\n",ind);
1239                 }
1240                 for (r=child->redirects; r; r=rnext) {
1241                         final_printf("%s   redirect %d%s", ind, r->fd, redir_table[r->type].descrip);
1242                         if (r->dup == -1) {
1243                                 final_printf(" %s\n", *r->word.gl_pathv);
1244                                 globfree(&r->word);
1245                         } else {
1246                                 final_printf("&%d\n", r->dup);
1247                         }
1248                         rnext=r->next;
1249                         free(r);
1250                 }
1251                 child->redirects=NULL;
1252         }
1253         free(pi->progs);   /* children are an array, they get freed all at once */
1254         pi->progs=NULL;
1255         return ret_code;
1256 }
1257
1258 static int run_list_test(struct pipe *head, int indent)
1259 {
1260         int rcode=0;   /* if list has no members */
1261         struct pipe *pi, *next;
1262         char *ind = indenter(indent);
1263         for (pi=head; pi; pi=next) {
1264                 if (pi->num_progs == 0) break;
1265                 final_printf("%s pipe reserved mode %d\n", ind, pi->r_mode);
1266                 rcode = run_pipe_test(pi, indent);
1267                 final_printf("%s pipe followup code %d\n", ind, pi->followup);
1268                 next=pi->next;
1269                 pi->next=NULL;
1270                 free(pi);
1271         }
1272         return rcode;   
1273 }
1274
1275 /* Select which version we will use */
1276 static int run_list(struct pipe *pi)
1277 {
1278         int rcode=0;
1279         if (fake_mode==0) {
1280                 rcode = run_list_real(pi);
1281         } 
1282         /* run_list_test has the side effect of clearing memory
1283          * In the long run that function can be merged with run_list_real,
1284          * but doing that now would hobble the debugging effort. */
1285         run_list_test(pi,0);
1286         return rcode;
1287 }
1288
1289 /* The API for glob is arguably broken.  This routine pushes a non-matching
1290  * string into the output structure, removing non-backslashed backslashes.
1291  * If someone can prove me wrong, by performing this function within the
1292  * original glob(3) api, feel free to rewrite this routine into oblivion.
1293  * Return code (0 vs. GLOB_NOSPACE) matches glob(3).
1294  * XXX broken if the last character is '\\', check that before calling.
1295  */
1296 static int globhack(const char *src, int flags, glob_t *pglob)
1297 {
1298         int cnt, pathc;
1299         const char *s;
1300         char *dest;
1301         for (cnt=1, s=src; *s; s++) {
1302                 if (*s == '\\') s++;
1303                 cnt++;
1304         }
1305         dest = malloc(cnt);
1306         if (!dest) return GLOB_NOSPACE;
1307         if (!(flags & GLOB_APPEND)) {
1308                 pglob->gl_pathv=NULL;
1309                 pglob->gl_pathc=0;
1310                 pglob->gl_offs=0;
1311                 pglob->gl_offs=0;
1312         }
1313         pathc = ++pglob->gl_pathc;
1314         pglob->gl_pathv = realloc(pglob->gl_pathv, (pathc+1)*sizeof(*pglob->gl_pathv));
1315         if (pglob->gl_pathv == NULL) return GLOB_NOSPACE;
1316         pglob->gl_pathv[pathc-1]=dest;
1317         pglob->gl_pathv[pathc]=NULL;
1318         for (s=src; *s; s++, dest++) {
1319                 if (*s == '\\') s++;
1320                 *dest = *s;
1321         }
1322         *dest='\0';
1323         return 0;
1324 }
1325
1326 /* XXX broken if the last character is '\\', check that before calling */
1327 static int glob_needed(const char *s)
1328 {
1329         for (; *s; s++) {
1330                 if (*s == '\\') s++;
1331                 if (strchr("*[?",*s)) return 1;
1332         }
1333         return 0;
1334 }
1335
1336 #if 0
1337 static void globprint(glob_t *pglob)
1338 {
1339         int i;
1340         debug_printf("glob_t at %p:\n", pglob);
1341         debug_printf("  gl_pathc=%d  gl_pathv=%p  gl_offs=%d  gl_flags=%d\n",
1342                 pglob->gl_pathc, pglob->gl_pathv, pglob->gl_offs, pglob->gl_flags);
1343         for (i=0; i<pglob->gl_pathc; i++)
1344                 debug_printf("pglob->gl_pathv[%d] = %p = %s\n", i,
1345                         pglob->gl_pathv[i], pglob->gl_pathv[i]);
1346 }
1347 #endif
1348
1349 static int xglob(o_string *dest, int flags, glob_t *pglob)
1350 {
1351         int gr;
1352
1353         /* short-circuit for null word */
1354         /* we can code this better when the debug_printf's are gone */
1355         if (dest->length == 0) {
1356                 if (dest->nonnull) {
1357                         /* bash man page calls this an "explicit" null */
1358                         gr = globhack(dest->data, flags, pglob);
1359                         debug_printf("globhack returned %d\n",gr);
1360                 } else {
1361                         return 0;
1362                 }
1363         } else if (glob_needed(dest->data)) {
1364                 gr = glob(dest->data, flags, NULL, pglob);
1365                 debug_printf("glob returned %d\n",gr);
1366                 if (gr == GLOB_NOMATCH) {
1367                         /* quote removal, or more accurately, backslash removal */
1368                         gr = globhack(dest->data, flags, pglob);
1369                         debug_printf("globhack returned %d\n",gr);
1370                 }
1371         } else {
1372                 gr = globhack(dest->data, flags, pglob);
1373                 debug_printf("globhack returned %d\n",gr);
1374         }
1375         if (gr == GLOB_NOSPACE) {
1376                 fprintf(stderr,"out of memory during glob\n");
1377                 exit(1);
1378         }
1379         if (gr != 0) { /* GLOB_ABORTED ? */
1380                 fprintf(stderr,"glob(3) error %d\n",gr);
1381         }
1382         /* globprint(glob_target); */
1383         return gr;
1384 }
1385
1386 /* the src parameter allows us to peek forward to a possible &n syntax
1387  * for file descriptor duplication, e.g., "2>&1".
1388  * Return code is 0 normally, 1 if a syntax error is detected in src.
1389  * Resource errors (in xmalloc) cause the process to exit */
1390 static int setup_redirect(struct p_context *ctx, int fd, redir_type style,
1391         struct in_str *input)
1392 {
1393         struct child_prog *child=ctx->child;
1394         struct redir_struct *redir = child->redirects;
1395         struct redir_struct *last_redir=NULL;
1396
1397         /* Create a new redir_struct and drop it onto the end of the linked list */
1398         while(redir) {
1399                 last_redir=redir;
1400                 redir=redir->next;
1401         }
1402         redir = xmalloc(sizeof(struct redir_struct));
1403         redir->next=NULL;
1404         if (last_redir) {
1405                 last_redir->next=redir;
1406         } else {
1407                 child->redirects=redir;
1408         }
1409
1410         redir->type=style;
1411         redir->fd= (fd==-1) ? redir_table[style].default_fd : fd ;
1412
1413         debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
1414
1415         /* Check for a '2>&1' type redirect */ 
1416         redir->dup = redirect_dup_num(input);
1417         if (redir->dup == -2) return 1;  /* syntax error */
1418         if (redir->dup != -1) {
1419                 /* Erik had a check here that the file descriptor in question
1420                  * is legit; I postpone that to "run time" */
1421                 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
1422         } else {
1423                 /* We do _not_ try to open the file that src points to,
1424                  * since we need to return and let src be expanded first.
1425                  * Set ctx->pending_redirect, so we know what to do at the
1426                  * end of the next parsed word.
1427                  */
1428                 ctx->pending_redirect = redir;
1429         }
1430         return 0;
1431 }
1432
1433 struct pipe *new_pipe(void) {
1434         struct pipe *pi;
1435         pi = xmalloc(sizeof(struct pipe));
1436         pi->num_progs = 0;
1437         pi->progs = NULL;
1438         pi->next = NULL;
1439         pi->followup = 0;  /* invalid */
1440         return pi;
1441 }
1442
1443 static void initialize_context(struct p_context *ctx)
1444 {
1445         ctx->pipe=NULL;
1446         ctx->pending_redirect=NULL;
1447         ctx->child=NULL;
1448         ctx->list_head=new_pipe();
1449         ctx->pipe=ctx->list_head;
1450         ctx->w=RES_NONE;
1451         ctx->stack=NULL;
1452         done_command(ctx);   /* creates the memory for working child */
1453 }
1454
1455 /* normal return is 0
1456  * if a reserved word is found, and processed, return 1
1457  * should handle if, then, elif, else, fi, for, while, until, do, done.
1458  * case, function, and select are obnoxious, save those for later.
1459  */
1460 int reserved_word(o_string *dest, struct p_context *ctx)
1461 {
1462         struct reserved_combo {
1463                 char *literal;
1464                 int code;
1465                 long flag;
1466         };
1467         /* Mostly a list of accepted follow-up reserved words.
1468          * FLAG_END means we are done with the sequence, and are ready
1469          * to turn the compound list into a command.
1470          * FLAG_START means the word must start a new compound list.
1471          */
1472         static struct reserved_combo reserved_list[] = {
1473                 { "if",    RES_IF,    FLAG_THEN | FLAG_START },
1474                 { "then",  RES_THEN,  FLAG_ELIF | FLAG_ELSE | FLAG_FI },
1475                 { "elif",  RES_ELIF,  FLAG_THEN },
1476                 { "else",  RES_ELSE,  FLAG_FI   },
1477                 { "fi",    RES_FI,    FLAG_END  },
1478                 { "for",   RES_FOR,   FLAG_DO   | FLAG_START },
1479                 { "while", RES_WHILE, FLAG_DO   | FLAG_START },
1480                 { "until", RES_UNTIL, FLAG_DO   | FLAG_START },
1481                 { "do",    RES_DO,    FLAG_DONE },
1482                 { "done",  RES_DONE,  FLAG_END  }
1483         };
1484         struct reserved_combo *r;
1485         for (r=reserved_list;
1486 #define NRES sizeof(reserved_list)/sizeof(struct reserved_combo)
1487                 r<reserved_list+NRES; r++) {
1488                 if (strcmp(dest->data, r->literal) == 0) {
1489                         debug_printf("found reserved word %s, code %d\n",r->literal,r->code);
1490                         if (r->flag & FLAG_START) {
1491                                 struct p_context *new = xmalloc(sizeof(struct p_context));
1492                                 debug_printf("push stack\n");
1493                                 *new = *ctx;   /* physical copy */
1494                                 initialize_context(ctx);
1495                                 ctx->stack=new;
1496                         } else if ( ctx->w == RES_NONE || ! (ctx->old_flag & (1<<r->code))) {
1497                                 syntax();
1498                                 ctx->w = RES_SNTX;
1499                                 b_reset (dest);
1500                                 return 1;
1501                         }
1502                         ctx->w=r->code;
1503                         ctx->old_flag = r->flag;
1504                         if (ctx->old_flag & FLAG_END) {
1505                                 struct p_context *old;
1506                                 debug_printf("pop stack\n");
1507                                 old = ctx->stack;
1508                                 old->child->group = ctx->list_head;
1509                                 *ctx = *old;   /* physical copy */
1510                                 free(old);
1511                                 ctx->w=RES_NONE;
1512                         }
1513                         b_reset (dest);
1514                         return 1;
1515                 }
1516         }
1517         return 0;
1518 }
1519
1520 /* normal return is 0.
1521  * Syntax or xglob errors return 1. */
1522 static int done_word(o_string *dest, struct p_context *ctx)
1523 {
1524         struct child_prog *child=ctx->child;
1525         glob_t *glob_target;
1526         int gr, flags = 0;
1527
1528         debug_printf("done_word: %s %p\n", dest->data, child);
1529         if (dest->length == 0 && !dest->nonnull) {
1530                 debug_printf("  true null, ignored\n");
1531                 return 0;
1532         }
1533         if (ctx->pending_redirect) {
1534                 glob_target = &ctx->pending_redirect->word;
1535         } else {
1536                 if (child->group) {
1537                         syntax();
1538                         return 1;  /* syntax error, groups and arglists don't mix */
1539                 }
1540                 if (!child->argv) {
1541                         debug_printf("checking %s for reserved-ness\n",dest->data);
1542                         if (reserved_word(dest,ctx)) return ctx->w==RES_SNTX;
1543                 }
1544                 glob_target = &child->glob_result;
1545                 if (child->argv) flags |= GLOB_APPEND;
1546         }
1547         gr = xglob(dest, flags, glob_target);
1548         if (gr != 0) return 1;
1549
1550         b_reset(dest);
1551         if (ctx->pending_redirect) {
1552                 ctx->pending_redirect=NULL;
1553                 if (glob_target->gl_pathc != 1) {
1554                         fprintf(stderr, "ambiguous redirect\n");
1555                         return 1;
1556                 }
1557         } else {
1558                 child->argv = glob_target->gl_pathv;
1559         }
1560         return 0;
1561 }
1562
1563 /* The only possible error here is out of memory, in which case
1564  * xmalloc exits. */
1565 static int done_command(struct p_context *ctx)
1566 {
1567         /* The child is really already in the pipe structure, so
1568          * advance the pipe counter and make a new, null child.
1569          * Only real trickiness here is that the uncommitted
1570          * child structure, to which ctx->child points, is not
1571          * counted in pi->num_progs. */
1572         struct pipe *pi=ctx->pipe;
1573         struct child_prog *prog=ctx->child;
1574
1575         if (prog && prog->group == NULL
1576                  && prog->argv == NULL
1577                  && prog->redirects == NULL) {
1578                 debug_printf("done_command: skipping null command\n");
1579                 return 0;
1580         } else if (prog) {
1581                 pi->num_progs++;
1582                 debug_printf("done_command: num_progs incremented to %d\n",pi->num_progs);
1583         } else {
1584                 debug_printf("done_command: initializing\n");
1585         }
1586         pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1));
1587
1588         prog = pi->progs + pi->num_progs;
1589         prog->redirects = NULL;
1590         prog->argv = NULL;
1591         prog->is_stopped = 0;
1592         prog->group = NULL;
1593         prog->glob_result.gl_pathv = NULL;
1594         prog->family = pi;
1595
1596         ctx->child=prog;
1597         /* but ctx->pipe and ctx->list_head remain unchanged */
1598         return 0;
1599 }
1600
1601 static int done_pipe(struct p_context *ctx, pipe_style type)
1602 {
1603         struct pipe *new_p;
1604         done_command(ctx);  /* implicit closure of previous command */
1605         debug_printf("done_pipe, type %d\n", type);
1606         ctx->pipe->followup = type;
1607         ctx->pipe->r_mode = ctx->w;
1608         new_p=new_pipe();
1609         ctx->pipe->next = new_p;
1610         ctx->pipe = new_p;
1611         ctx->child = NULL;
1612         done_command(ctx);  /* set up new pipe to accept commands */
1613         return 0;
1614 }
1615
1616 /* peek ahead in the in_str to find out if we have a "&n" construct,
1617  * as in "2>&1", that represents duplicating a file descriptor.
1618  * returns either -2 (syntax error), -1 (no &), or the number found.
1619  */
1620 static int redirect_dup_num(struct in_str *input)
1621 {
1622         int ch, d=0, ok=0;
1623         ch = b_peek(input);
1624         if (ch != '&') return -1;
1625
1626         b_getch(input);  /* get the & */
1627         while (ch=b_peek(input),isdigit(ch)) {
1628                 d = d*10+(ch-'0');
1629                 ok=1;
1630                 b_getch(input);
1631         }
1632         if (ok) return d;
1633
1634         fprintf(stderr, "ambiguous redirect\n");
1635         return -2;
1636 }
1637
1638 /* If a redirect is immediately preceded by a number, that number is
1639  * supposed to tell which file descriptor to redirect.  This routine
1640  * looks for such preceding numbers.  In an ideal world this routine
1641  * needs to handle all the following classes of redirects...
1642  *     echo 2>foo     # redirects fd  2 to file "foo", nothing passed to echo
1643  *     echo 49>foo    # redirects fd 49 to file "foo", nothing passed to echo
1644  *     echo -2>foo    # redirects fd  1 to file "foo",    "-2" passed to echo
1645  *     echo 49x>foo   # redirects fd  1 to file "foo",   "49x" passed to echo
1646  * A -1 output from this program means no valid number was found, so the
1647  * caller should use the appropriate default for this redirection.
1648  */
1649 static int redirect_opt_num(o_string *o)
1650 {
1651         int num;
1652
1653         if (o->length==0) return -1;
1654         for(num=0; num<o->length; num++) {
1655                 if (!isdigit(*(o->data+num))) {
1656                         return -1;
1657                 }
1658         }
1659         /* reuse num (and save an int) */
1660         num=atoi(o->data);
1661         b_reset(o);
1662         return num;
1663 }
1664
1665 FILE *generate_stream_from_list(struct pipe *head)
1666 {
1667         FILE *pf;
1668 #if 1
1669         int pid, channel[2];
1670         if (pipe(channel)<0) perror_msg_and_die("pipe");
1671         pid=fork();
1672         if (pid<0) {
1673                 perror_msg_and_die("fork");
1674         } else if (pid==0) {
1675                 close(channel[0]);
1676                 if (channel[1] != 1) {
1677                         dup2(channel[1],1);
1678                         close(channel[1]);
1679                 }
1680 #if 0
1681 #define SURROGATE "surrogate response"
1682                 write(1,SURROGATE,sizeof(SURROGATE));
1683                 exit(run_list(head));
1684 #else
1685                 exit(run_list_real(head));   /* leaks memory */
1686 #endif
1687         }
1688         debug_printf("forked child %d\n",pid);
1689         close(channel[1]);
1690         pf = fdopen(channel[0],"r");
1691         debug_printf("pipe on FILE *%p\n",pf);
1692 #else
1693         run_list_test(head,0);
1694         pf=popen("echo surrogate response","r");
1695         debug_printf("started fake pipe on FILE *%p\n",pf);
1696 #endif
1697         return pf;
1698 }
1699
1700 /* this version hacked for testing purposes */
1701 /* return code is exit status of the process that is run. */
1702 static int process_command_subs(o_string *dest, struct p_context *ctx, struct in_str *input, int subst_end)
1703 {
1704         int retcode;
1705         o_string result=NULL_O_STRING;
1706         struct p_context inner;
1707         FILE *p;
1708         struct in_str pipe_str;
1709         initialize_context(&inner);
1710
1711         /* recursion to generate command */
1712         retcode = parse_stream(&result, &inner, input, subst_end);
1713         if (retcode != 0) return retcode;  /* syntax error or EOF */
1714         done_word(&result, &inner);
1715         done_pipe(&inner, PIPE_SEQ);
1716         b_free(&result);
1717
1718         p=generate_stream_from_list(inner.list_head);
1719         if (p==NULL) return 1;
1720         mark_open(fileno(p));
1721         setup_file_in_str(&pipe_str, p);
1722
1723         /* now send results of command back into original context */
1724         retcode = parse_stream(dest, ctx, &pipe_str, '\0');
1725         /* XXX In case of a syntax error, should we try to kill the child?
1726          * That would be tough to do right, so just read until EOF. */
1727         if (retcode == 1) {
1728                 while (b_getch(&pipe_str)!=EOF) { /* discard */ };
1729         }
1730
1731         debug_printf("done reading from pipe, pclose()ing\n");
1732         /* This is the step that wait()s for the child.  Should be pretty
1733          * safe, since we just read an EOF from its stdout.  We could try
1734          * to better, by using wait(), and keeping track of background jobs
1735          * at the same time.  That would be a lot of work, and contrary
1736          * to the KISS philosophy of this program. */
1737         mark_closed(fileno(p));
1738         retcode=pclose(p);
1739         debug_printf("pclosed, retcode=%d\n",retcode);
1740         /* XXX this process fails to trim a single trailing newline */
1741         return retcode;
1742 }
1743
1744 static int parse_group(o_string *dest, struct p_context *ctx,
1745         struct in_str *input, int ch)
1746 {
1747         int rcode, endch=0;
1748         struct p_context sub;
1749         struct child_prog *child = ctx->child;
1750         if (child->argv) {
1751                 syntax();
1752                 return 1;  /* syntax error, groups and arglists don't mix */
1753         }
1754         initialize_context(&sub);
1755         switch(ch) {
1756                 case '(': endch=')'; child->subshell=1; break;
1757                 case '{': endch='}'; break;
1758                 default: syntax();   /* really logic error */
1759         }
1760         rcode=parse_stream(dest,&sub,input,endch);
1761         done_word(dest,&sub); /* finish off the final word in the subcontext */
1762         done_pipe(&sub, PIPE_SEQ);  /* and the final command there, too */
1763         child->group = sub.list_head;
1764         return rcode;
1765         /* child remains "open", available for possible redirects */
1766 }
1767
1768 /* basically useful version until someone wants to get fancier,
1769  * see the bash man page under "Parameter Expansion" */
1770 static void lookup_param(o_string *dest, struct p_context *ctx, o_string *src)
1771 {
1772         const char *p=NULL;
1773         if (src->data) p = getenv(src->data);
1774         if (p) parse_string(dest, ctx, p);   /* recursion */
1775         b_free(src);
1776 }
1777
1778 /* return code: 0 for OK, 1 for syntax error */
1779 static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input)
1780 {
1781         int i, advance=0;
1782         o_string alt=NULL_O_STRING;
1783         char sep[]=" ";
1784         int ch = input->peek(input);  /* first character after the $ */
1785         debug_printf("handle_dollar: ch=%c\n",ch);
1786         if (isalpha(ch)) {
1787                 while(ch=b_peek(input),isalnum(ch) || ch=='_') {
1788                         b_getch(input);
1789                         b_addchr(&alt,ch);
1790                 }
1791                 lookup_param(dest, ctx, &alt);
1792         } else if (isdigit(ch)) {
1793                 i = ch-'0';  /* XXX is $0 special? */
1794                 if (i<global_argc) {
1795                         parse_string(dest, ctx, global_argv[i]); /* recursion */
1796                 }
1797                 advance = 1;
1798         } else switch (ch) {
1799                 case '$':
1800                         b_adduint(dest,getpid());
1801                         advance = 1;
1802                         break;
1803                 case '!':
1804                         if (last_bg_pid > 0) b_adduint(dest, last_bg_pid);
1805                         advance = 1;
1806                         break;
1807                 case '?':
1808                         b_adduint(dest,last_return_code);
1809                         advance = 1;
1810                         break;
1811                 case '#':
1812                         b_adduint(dest,global_argc ? global_argc-1 : 0);
1813                         advance = 1;
1814                         break;
1815                 case '{':
1816                         b_getch(input);
1817                         /* XXX maybe someone will try to escape the '}' */
1818                         while(ch=b_getch(input),ch!=EOF && ch!='}') {
1819                                 b_addchr(&alt,ch);
1820                         }
1821                         if (ch != '}') {
1822                                 syntax();
1823                                 return 1;
1824                         }
1825                         lookup_param(dest, ctx, &alt);
1826                         break;
1827                 case '(':
1828                         process_command_subs(dest, ctx, input, ')');
1829                         break;
1830                 case '*':
1831                         sep[0]=ifs[0];
1832                         for (i=1; i<global_argc; i++) {
1833                                 parse_string(dest, ctx, global_argv[i]);
1834                                 if (i+1 < global_argc) parse_string(dest, ctx, sep);
1835                         }
1836                         break;
1837                 case '@':
1838                 case '-':
1839                 case '_':
1840                         /* still unhandled, but should be eventually */
1841                         fprintf(stderr,"unhandled syntax: $%c\n",ch);
1842                         return 1;
1843                         break;
1844                 default:
1845                         b_addqchr(dest,'$',dest->quote);
1846         }
1847         /* Eat the character if the flag was set.  If the compiler
1848          * is smart enough, we could substitute "b_getch(input);"
1849          * for all the "advance = 1;" above, and also end up with
1850          * a nice size-optimized program.  Hah!  That'll be the day.
1851          */
1852         if (advance) b_getch(input);
1853         return 0;
1854 }
1855
1856 int parse_string(o_string *dest, struct p_context *ctx, const char *src)
1857 {
1858         struct in_str foo;
1859         setup_string_in_str(&foo, src);
1860         return parse_stream(dest, ctx, &foo, '\0');
1861 }
1862
1863 /* return code is 0 for normal exit, 1 for syntax error */
1864 int parse_stream(o_string *dest, struct p_context *ctx,
1865         struct in_str *input, int end_trigger)
1866 {
1867         unsigned int ch, m;
1868         int redir_fd;
1869         redir_type redir_style;
1870         int next;
1871
1872         /* Only double-quote state is handled in the state variable dest->quote.
1873          * A single-quote triggers a bypass of the main loop until its mate is
1874          * found.  When recursing, quote state is passed in via dest->quote. */
1875
1876         debug_printf("parse_stream, end_trigger=%d\n",end_trigger);
1877         while ((ch=b_getch(input))!=EOF) {
1878                 m = map[ch];
1879                 next = (ch == '\n') ? 0 : b_peek(input);
1880                 debug_printf("parse_stream: ch=%c (%d) m=%d quote=%d\n",
1881                         ch,ch,m,dest->quote);
1882                 if (m==0 || ((m==1 || m==2) && dest->quote)) {
1883                         b_addqchr(dest, ch, dest->quote);
1884                 } else {
1885                         if (m==2) {  /* unquoted IFS */
1886                                 done_word(dest, ctx);
1887                                 if (ch=='\n') done_pipe(ctx,PIPE_SEQ);
1888                         }
1889                         if (ch == end_trigger && !dest->quote && ctx->w==RES_NONE) {
1890                                 debug_printf("leaving parse_stream\n");
1891                                 return 0;
1892                         }
1893 #if 0
1894                         if (ch=='\n') {
1895                                 /* Yahoo!  Time to run with it! */
1896                                 done_pipe(ctx,PIPE_SEQ);
1897                                 run_list(ctx->list_head);
1898                                 initialize_context(ctx);
1899                         }
1900 #endif
1901                         if (m!=2) switch (ch) {
1902                 case '#':
1903                         if (dest->length == 0 && !dest->quote) {
1904                                 while(ch=b_peek(input),ch!=EOF && ch!='\n') { b_getch(input); }
1905                         } else {
1906                                 b_addqchr(dest, ch, dest->quote);
1907                         }
1908                         break;
1909                 case '\\':
1910                         if (next == EOF) {
1911                                 syntax();
1912                                 return 1;
1913                         }
1914                         b_addqchr(dest, '\\', dest->quote);
1915                         b_addqchr(dest, b_getch(input), dest->quote);
1916                         break;
1917                 case '$':
1918                         if (handle_dollar(dest, ctx, input)!=0) return 1;
1919                         break;
1920                 case '\'':
1921                         dest->nonnull = 1;
1922                         while(ch=b_getch(input),ch!=EOF && ch!='\'') {
1923                                 b_addchr(dest,ch);
1924                         }
1925                         if (ch==EOF) {
1926                                 syntax();
1927                                 return 1;
1928                         }
1929                         break;
1930                 case '"':
1931                         dest->nonnull = 1;
1932                         dest->quote = !dest->quote;
1933                         break;
1934                 case '`':
1935                         process_command_subs(dest, ctx, input, '`');
1936                         break;
1937                 case '>':
1938                         redir_fd = redirect_opt_num(dest);
1939                         done_word(dest, ctx);
1940                         redir_style=REDIRECT_OVERWRITE;
1941                         if (next == '>') {
1942                                 redir_style=REDIRECT_APPEND;
1943                                 b_getch(input);
1944                         } else if (next == '(') {
1945                                 syntax();   /* until we support >(list) Process Substitution */
1946                                 return 1;
1947                         }
1948                         setup_redirect(ctx, redir_fd, redir_style, input);
1949                         break;
1950                 case '<':
1951                         redir_fd = redirect_opt_num(dest);
1952                         done_word(dest, ctx);
1953                         redir_style=REDIRECT_INPUT;
1954                         if (next == '<') {
1955                                 redir_style=REDIRECT_HEREIS;
1956                                 b_getch(input);
1957                         } else if (next == '>') {
1958                                 redir_style=REDIRECT_IO;
1959                                 b_getch(input);
1960                         } else if (next == '(') {
1961                                 syntax();   /* until we support <(list) Process Substitution */
1962                                 return 1;
1963                         }
1964                         setup_redirect(ctx, redir_fd, redir_style, input);
1965                         break;
1966                 case ';':
1967                         done_word(dest, ctx);
1968                         done_pipe(ctx,PIPE_SEQ);
1969                         break;
1970                 case '&':
1971                         done_word(dest, ctx);
1972                         if (next=='&') {
1973                                 b_getch(input);
1974                                 done_pipe(ctx,PIPE_AND);
1975                         } else {
1976                                 done_pipe(ctx,PIPE_BG);
1977                         }
1978                         break;
1979                 case '|':
1980                         done_word(dest, ctx);
1981                         if (next=='|') {
1982                                 b_getch(input);
1983                                 done_pipe(ctx,PIPE_OR);
1984                         } else {
1985                                 /* we could pick up a file descriptor choice here
1986                                  * with redirect_opt_num(), but bash doesn't do it.
1987                                  * "echo foo 2| cat" yields "foo 2". */
1988                                 done_command(ctx);
1989                         }
1990                         break;
1991                 case '(':
1992                 case '{':
1993                         if (parse_group(dest, ctx, input, ch)!=0) return 1;
1994                         break;
1995                 case ')':
1996                 case '}':
1997                         syntax();   /* Proper use of this character caught by end_trigger */
1998                         return 1;
1999                         break;
2000                 default:
2001                         syntax();   /* this is really an internal logic error */
2002                         return 1;
2003                         }
2004                 }
2005         }
2006         /* complain if quote?  No, maybe we just finished a command substitution
2007          * that was quoted.  Example:
2008          * $ echo "`cat foo` plus more" 
2009          * and we just got the EOF generated by the subshell that ran "cat foo"
2010          * The only real complaint is if we got an EOF when end_trigger != '\0',
2011          * that is, we were really supposed to get end_trigger, and never got
2012          * one before the EOF.  Can't use the standard "syntax error" return code,
2013          * so that parse_stream_outer can distinguish the EOF and exit smoothly. */
2014         if (end_trigger != '\0') return -1;
2015         return 0;
2016 }
2017
2018 void mapset(const unsigned char *set, int code)
2019 {
2020         const unsigned char *s;
2021         for (s=set; *s; s++) map[*s] = code;
2022 }
2023
2024 void update_ifs_map(void)
2025 {
2026         /* char *ifs and char map[256] are both globals. */
2027         ifs = getenv("IFS");
2028         if (ifs == NULL) ifs=" \t\n";
2029         /* Precompute a list of 'flow through' behavior so it can be treated
2030          * quickly up front.  Computation is necessary because of IFS.
2031          * Special case handling of IFS == " \t\n" is not implemented.
2032          * The map[] array only really needs two bits each, and on most machines
2033          * that would be faster because of the reduced L1 cache footprint.
2034          */
2035         memset(map,0,256);        /* most characters flow through always */
2036         mapset("\\$'\"`", 3);     /* never flow through */
2037         mapset("<>;&|(){}#", 1);  /* flow through if quoted */
2038         mapset(ifs, 2);           /* also flow through if quoted */
2039 }
2040
2041 /* most recursion does not come through here, the exeception is
2042  * from builtin_source() */
2043 int parse_stream_outer(struct in_str *inp)
2044 {
2045
2046         struct p_context ctx;
2047         o_string temp=NULL_O_STRING;
2048         int rcode;
2049         do {
2050                 initialize_context(&ctx);
2051                 update_ifs_map();
2052                 inp->promptmode=1;
2053                 rcode = parse_stream(&temp, &ctx, inp, '\n');
2054                 done_word(&temp, &ctx);
2055                 done_pipe(&ctx,PIPE_SEQ);
2056                 run_list(ctx.list_head);
2057         } while (rcode != -1);   /* loop on syntax errors, return on EOF */
2058         return 0;
2059 }
2060
2061 static int parse_string_outer(const char *s)
2062 {
2063         struct in_str input;
2064         setup_string_in_str(&input, s);
2065         return parse_stream_outer(&input);
2066 }
2067
2068 static int parse_file_outer(FILE *f)
2069 {
2070         int rcode;
2071         struct in_str input;
2072         setup_file_in_str(&input, f);
2073         rcode = parse_stream_outer(&input);
2074         return rcode;
2075 }
2076
2077 int shell_main(int argc, char **argv)
2078 {
2079         int opt;
2080         FILE *input;
2081
2082         last_return_code=EXIT_SUCCESS;
2083
2084         /* XXX what should these be while sourcing /etc/profile? */
2085         global_argc = argc;
2086         global_argv = argv;
2087
2088         if (argv[0] && argv[0][0] == '-') {
2089                 debug_printf("\nsourcing /etc/profile\n");
2090                 input = xfopen("/etc/profile", "r");
2091                 mark_open(fileno(input));
2092                 parse_file_outer(input);
2093                 mark_closed(fileno(input));
2094                 fclose(input);
2095         }
2096         input=stdin;
2097         
2098         /* initialize the cwd -- this is never freed...*/
2099         cwd = xgetcwd(0);
2100 #ifdef BB_FEATURE_COMMAND_EDITING
2101         cmdedit_set_initial_prompt();
2102 #else
2103         PS1 = NULL;
2104 #endif
2105         
2106         while ((opt = getopt(argc, argv, "c:xif")) > 0) {
2107                 switch (opt) {
2108                         case 'c':
2109                                 {
2110                                         global_argv = argv+optind;
2111                                         global_argc = argc-optind;
2112                                         opt = parse_string_outer(optarg);
2113                                         goto final_return;
2114                                 }
2115                                 break;
2116                         case 'i':
2117                                 interactive++;
2118                                 break;
2119                         case 'f':
2120                                 fake_mode++;
2121                                 break;
2122                         default:
2123                                 fprintf(stderr, "Usage: sh [FILE]...\n"
2124                                                 "   or: sh -c command [args]...\n\n");
2125                                 exit(EXIT_FAILURE);
2126                 }
2127         }
2128         /* A shell is interactive if the `-i' flag was given, or if all of
2129          * the following conditions are met:
2130          *        no -c command
2131          *    no arguments remaining or the -s flag given
2132          *    standard input is a terminal
2133          *    standard output is a terminal
2134          *    Refer to Posix.2, the description of the `sh' utility. */
2135         if (argv[optind]==NULL && input==stdin &&
2136                         isatty(fileno(stdin)) && isatty(fileno(stdout))) {
2137                 interactive++;
2138         }
2139
2140         debug_printf("\ninteractive=%d\n", interactive);
2141         if (interactive) {
2142                 /* Looks like they want an interactive shell */
2143                 fprintf(stdout, "\nhush -- the humble shell v0.01 (testing)\n\n");
2144                 opt=parse_file_outer(stdin);
2145                 goto final_return;
2146         }
2147
2148         debug_printf("\nrunning script '%s'\n", argv[optind]);
2149         global_argv = argv+optind;
2150         global_argc = argc-optind;
2151         input = xfopen(argv[optind], "r");
2152         opt = parse_file_outer(input);
2153
2154 #ifdef BB_FEATURE_CLEAN_UP
2155         fclose(input.file);
2156 #endif
2157
2158 final_return:
2159         return(opt?opt:last_return_code);
2160 }