Another hush update from Larry:
[oweals/busybox.git] / 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(EXIT_SUCCESS);
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                 } else {
818                         static char buffer;
819                         buffer = fgetc(i->file);
820                         i->p = &buffer;
821                 }
822
823                 i->__promptme = 0;
824
825                 if (i->p && *i->p) {
826                         ch=*i->p++;
827                 }
828                 debug_printf("b_getch: got a %d\n", ch);
829         }
830         if (ch == '\n') i->__promptme=1;
831         return ch;
832 }
833
834 /* All the callers guarantee this routine will never be
835  * used right after a newline, so prompting is not needed.
836  */
837 static int file_peek(struct in_str *i)
838 {
839         if (i->p && *i->p) {
840                 return *i->p;
841         } else {
842                 static char buffer;
843                 buffer = fgetc(i->file);
844                 i->p = &buffer;
845                 debug_printf("b_peek: got a %d\n", *i->p);
846                 return *i->p; 
847         }
848 }
849
850 static void setup_file_in_str(struct in_str *i, FILE *f)
851 {
852         i->peek = file_peek;
853         i->get = file_get;
854         i->__promptme=1;
855         i->promptmode=1;
856         i->file = f;
857         i->p = NULL;
858 }
859
860 static void setup_string_in_str(struct in_str *i, const char *s)
861 {
862         i->peek = static_peek;
863         i->get = static_get;
864         i->__promptme=1;
865         i->promptmode=1;
866         i->p = s;
867 }
868
869 static void mark_open(int fd)
870 {
871         struct close_me *new = xmalloc(sizeof(struct close_me));
872         new->fd = fd;
873         new->next = close_me_head;
874         close_me_head = new;
875 }
876
877 static void mark_closed(int fd)
878 {
879         struct close_me *tmp;
880         if (close_me_head == NULL || close_me_head->fd != fd)
881                 error_msg_and_die("corrupt close_me");
882         tmp = close_me_head;
883         close_me_head = close_me_head->next;
884         free(tmp);
885 }
886
887 static void close_all()
888 {
889         struct close_me *c;
890         for (c=close_me_head; c; c=c->next) {
891                 close(c->fd);
892         }
893         close_me_head = NULL;
894 }
895
896 /* squirrel != NULL means we squirrel away copies of stdin, stdout,
897  * and stderr if they are redirected. */
898 static int setup_redirects(struct child_prog *prog, int squirrel[])
899 {
900         int openfd, mode;
901         struct redir_struct *redir;
902
903         for (redir=prog->redirects; redir; redir=redir->next) {
904                 if (redir->dup == -1) {
905                         mode=redir_table[redir->type].mode;
906                         openfd = open(redir->word.gl_pathv[0], mode, 0666);
907                         if (openfd < 0) {
908                         /* this could get lost if stderr has been redirected, but
909                            bash and ash both lose it as well (though zsh doesn't!) */
910                                 fprintf(stderr,"error opening %s: %s\n", redir->word.gl_pathv[0],
911                                         strerror(errno));
912                                 return 1;
913                         }
914                 } else {
915                         openfd = redir->dup;
916                 }
917
918                 if (openfd != redir->fd) {
919                         if (squirrel && redir->fd < 3) {
920                                 squirrel[redir->fd] = dup(redir->fd);
921                         }
922                         dup2(openfd, redir->fd);
923                         close(openfd);
924                 }
925         }
926         return 0;
927 }
928
929 static void restore_redirects(int squirrel[])
930 {
931         int i, fd;
932         for (i=0; i<3; i++) {
933                 fd = squirrel[i];
934                 if (fd != -1) {
935                         /* No error checking.  I sure wouldn't know what
936                          * to do with an error if I found one! */
937                         dup2(fd, i);
938                         close(fd);
939                 }
940         }
941 }
942
943 /* XXX this definitely needs some more thought, work, and
944  * cribbing from other shells */
945 static int pipe_wait(struct pipe *pi)
946 {
947         int rcode=0, i, pid, running, status;
948         running = pi->num_progs;
949         while (running) {
950                 pid=waitpid(-1, &status, 0);
951                 if (pid < 0) perror_msg_and_die("waitpid");
952                 for (i=0; i < pi->num_progs; i++) {
953                         if (pi->progs[i].pid == pid) {
954                                 if (i==pi->num_progs-1) rcode=WEXITSTATUS(status);
955                                 pi->progs[i].pid = 0;
956                                 running--;
957                                 break;
958                         }
959                 }
960         }
961         return rcode;
962 }
963
964 /* very simple version for testing */
965 static void pseudo_exec(struct child_prog *child)
966 {
967         int rcode;
968         struct built_in_command *x;
969         if (child->argv) {
970                 /*
971                  * Check if the command matches any of the builtins.
972                  * Depending on context, this might be redundant.  But it's
973                  * easier to waste a few CPU cycles than it is to figure out
974                  * if this is one of those cases.
975                  */
976                 for (x = bltins; x->cmd; x++) {
977                         if (strcmp(child->argv[0], x->cmd) == 0 ) {
978                                 debug_printf("builtin exec %s\n", child->argv[0]);
979                                 exit(x->function(child));
980                         }
981                 }
982
983                 /* Check if the command matches any busybox internal commands
984                  * ("applets") here.  
985                  * FIXME: This feature is not 100% safe, since
986                  * BusyBox is not fully reentrant, so we have no guarantee the things
987                  * from the .bss are still zeroed, or that things from .data are still
988                  * at their defaults.  We could exec ourself from /proc/self/exe, but I
989                  * really dislike relying on /proc for things.  We could exec ourself
990                  * from global_argv[0], but if we are in a chroot, we may not be able
991                  * to find ourself... */ 
992 #ifdef BB_FEATURE_SH_STANDALONE_SHELL
993                 {
994                         int argc_l;
995                         char** argv_l=child->argv;
996                         char *name = child->argv[0];
997
998 #ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
999                         /* Following discussions from November 2000 on the busybox mailing
1000                          * list, the default configuration, (without
1001                          * get_last_path_component()) lets the user force use of an
1002                          * external command by specifying the full (with slashes) filename.
1003                          * If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then applets
1004                          * _aways_ override external commands, so if you want to run
1005                          * /bin/cat, it will use BusyBox cat even if /bin/cat exists on the
1006                          * filesystem and is _not_ busybox.  Some systems may want this,
1007                          * most do not.  */
1008                         name = get_last_path_component(name);
1009 #endif
1010                         /* Count argc for use in a second... */
1011                         for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
1012                         optind = 1;
1013                         debug_printf("running applet %s\n", name);
1014                         run_applet_by_name(name, argc_l, child->argv);
1015                         exit(1);
1016                 }
1017 #endif
1018                 debug_printf("exec of %s\n",child->argv[0]);
1019                 execvp(child->argv[0],child->argv);
1020                 perror("execvp");
1021                 exit(1);
1022         } else if (child->group) {
1023                 debug_printf("runtime nesting to group\n");
1024                 interactive=0;    /* crucial!!!! */
1025                 rcode = run_list_real(child->group);
1026                 /* OK to leak memory by not calling run_list_test,
1027                  * since this process is about to exit */
1028                 exit(rcode);
1029         } else {
1030                 /* Can happen.  See what bash does with ">foo" by itself. */
1031                 debug_printf("trying to pseudo_exec null command\n");
1032                 exit(EXIT_SUCCESS);
1033         }
1034 }
1035
1036 /* run_pipe_real() starts all the jobs, but doesn't wait for anything
1037  * to finish.  See pipe_wait().
1038  *
1039  * return code is normally -1, when the caller has to wait for children
1040  * to finish to determine the exit status of the pipe.  If the pipe
1041  * is a simple builtin command, however, the action is done by the
1042  * time run_pipe_real returns, and the exit code is provided as the
1043  * return value.
1044  *
1045  * The input of the pipe is always stdin, the output is always
1046  * stdout.  The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
1047  * because it tries to avoid running the command substitution in
1048  * subshell, when that is in fact necessary.  The subshell process
1049  * now has its stdout directed to the input of the appropriate pipe,
1050  * so this routine is noticeably simpler.
1051  */
1052 static int run_pipe_real(struct pipe *pi)
1053 {
1054         int i;
1055         int nextin, nextout;
1056         int pipefds[2];                         /* pipefds[0] is for reading */
1057         struct child_prog *child;
1058         struct built_in_command *x;
1059
1060         nextin = 0;
1061         pi->pgrp = 0;
1062
1063         /* Check if this is a simple builtin (not part of a pipe).
1064          * Builtins within pipes have to fork anyway, and are handled in
1065          * pseudo_exec.  "echo foo | read bar" doesn't work on bash, either.
1066          */
1067         if (pi->num_progs == 1 && pi->progs[0].argv != NULL) {
1068                 child = & (pi->progs[0]);
1069                 if (child->group && ! child->subshell) {
1070                         int squirrel[] = {-1, -1, -1};
1071                         int rcode;
1072                         debug_printf("non-subshell grouping\n");
1073                         setup_redirects(child, squirrel);
1074                         /* XXX could we merge code with following builtin case,
1075                          * by creating a pseudo builtin that calls run_list_real? */
1076                         rcode = run_list_real(child->group);
1077                         restore_redirects(squirrel);
1078                         return rcode;
1079                 }
1080                 for (x = bltins; x->cmd; x++) {
1081                         if (strcmp(child->argv[0], x->cmd) == 0 ) {
1082                                 int squirrel[] = {-1, -1, -1};
1083                                 int rcode;
1084                                 debug_printf("builtin inline %s\n", child->argv[0]);
1085                                 /* XXX setup_redirects acts on file descriptors, not FILEs.
1086                                  * This is perfect for work that comes after exec().
1087                                  * Is it really safe for inline use?  Experimentally,
1088                                  * things seem to work with glibc. */
1089                                 setup_redirects(child, squirrel);
1090                                 rcode = x->function(child);
1091                                 restore_redirects(squirrel);
1092                                 return rcode;
1093                         }
1094                 }
1095         }
1096
1097         for (i = 0; i < pi->num_progs; i++) {
1098                 child = & (pi->progs[i]);
1099
1100                 /* pipes are inserted between pairs of commands */
1101                 if ((i + 1) < pi->num_progs) {
1102                         if (pipe(pipefds)<0) perror_msg_and_die("pipe");
1103                         nextout = pipefds[1];
1104                 } else {
1105                         nextout=1;
1106                         pipefds[0] = -1;
1107                 }
1108
1109                 /* XXX test for failed fork()? */
1110                 if (!(child->pid = fork())) {
1111                         close_all();
1112
1113                         if (nextin != 0) {
1114                                 dup2(nextin, 0);
1115                                 close(nextin);
1116                         }
1117                         if (nextout != 1) {
1118                                 dup2(nextout, 1);
1119                                 close(nextout);
1120                         }
1121                         if (pipefds[0]!=-1) {
1122                                 close(pipefds[0]);  /* opposite end of our output pipe */
1123                         }
1124
1125                         /* Like bash, explicit redirects override pipes,
1126                          * and the pipe fd is available for dup'ing. */
1127                         setup_redirects(child,NULL);
1128
1129                         pseudo_exec(child);
1130                 }
1131                 if (interactive) {
1132                         /* Put our child in the process group whose leader is the
1133                          * first process in this pipe. */
1134                         if (pi->pgrp==0) {
1135                                 pi->pgrp = child->pid;
1136                         }
1137                         /* Don't check for errors.  The child may be dead already,
1138                          * in which case setpgid returns error code EACCES. */
1139                         setpgid(child->pid, pi->pgrp);
1140                 }
1141                 /* In the non-interactive case, do nothing.  Leave the children
1142                  * with the process group that they inherited from us. */
1143         
1144                 if (nextin != 0)
1145                         close(nextin);
1146                 if (nextout != 1)
1147                         close(nextout);
1148
1149                 /* If there isn't another process, nextin is garbage 
1150                    but it doesn't matter */
1151                 nextin = pipefds[0];
1152         }
1153         return -1;
1154 }
1155
1156 static int run_list_real(struct pipe *pi)
1157 {
1158         int rcode=0;
1159         int if_code=0, next_if_code=0;  /* need double-buffer to handle elif */
1160         reserved_style rmode, skip_more_in_this_rmode=RES_XXXX;
1161         for (;pi;pi=pi->next) {
1162                 rmode = pi->r_mode;
1163                 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);
1164                 if (rmode == skip_more_in_this_rmode) continue;
1165                 skip_more_in_this_rmode = RES_XXXX;
1166                 if (rmode == RES_THEN || rmode == RES_ELSE) if_code = next_if_code;
1167                 if (rmode == RES_THEN &&  if_code) continue;
1168                 if (rmode == RES_ELSE && !if_code) continue;
1169                 if (rmode == RES_ELIF && !if_code) continue;
1170                 if (pi->num_progs == 0) continue;
1171                 rcode = run_pipe_real(pi);
1172                 if (rcode!=-1) {
1173                         /* We only ran a builtin: rcode was set by the return value
1174                          * of run_pipe_real(), and we don't need to wait for anything. */
1175                 } else if (pi->followup==PIPE_BG) {
1176                         /* XXX check bash's behavior with nontrivial pipes */
1177                         /* XXX compute jobid */
1178                         /* XXX what does bash do with attempts to background builtins? */
1179                         printf("[%d] %d\n", pi->jobid, pi->pgrp);
1180                         last_bg_pid = pi->pgrp;
1181                         rcode = EXIT_SUCCESS;
1182                 } else {
1183                         if (interactive) {
1184                                 /* move the new process group into the foreground */
1185                                 /* suppress messages when run from /linuxrc mag@sysgo.de */
1186                                 signal(SIGTTIN, SIG_IGN);
1187                                 signal(SIGTTOU, SIG_IGN);
1188                                 if (tcsetpgrp(0, pi->pgrp) && errno != ENOTTY)
1189                                         perror_msg("tcsetpgrp");
1190                                 rcode = pipe_wait(pi);
1191                                 if (tcsetpgrp(0, getpid()) && errno != ENOTTY)
1192                                         perror_msg("tcsetpgrp");
1193                                 signal(SIGTTIN, SIG_DFL);
1194                                 signal(SIGTTOU, SIG_DFL);
1195                         } else {
1196                                 rcode = pipe_wait(pi);
1197                         }
1198                 }
1199                 last_return_code=rcode;
1200                 if ( rmode == RES_IF || rmode == RES_ELIF )
1201                         next_if_code=rcode;  /* can be overwritten a number of times */
1202                 if ( (rcode==EXIT_SUCCESS && pi->followup==PIPE_OR) ||
1203                      (rcode!=EXIT_SUCCESS && pi->followup==PIPE_AND) )
1204                         skip_more_in_this_rmode=rmode;
1205                         /* return rcode; */ /* XXX broken if list is part of if/then/else */
1206         }
1207         return rcode;
1208 }
1209
1210 /* broken, of course, but OK for testing */
1211 static char *indenter(int i)
1212 {
1213         static char blanks[]="                                    ";
1214         return &blanks[sizeof(blanks)-i-1];
1215 }
1216
1217 /* return code is the exit status of the pipe */
1218 static int run_pipe_test(struct pipe *pi, int indent)
1219 {
1220         char **p;
1221         struct child_prog *child;
1222         struct redir_struct *r, *rnext;
1223         int a, i, ret_code=0;
1224         char *ind = indenter(indent);
1225         final_printf("%s run pipe: (pid %d)\n",ind,getpid());
1226         for (i=0; i<pi->num_progs; i++) {
1227                 child = &pi->progs[i];
1228                 final_printf("%s  command %d:\n",ind,i);
1229                 if (child->argv) {
1230                         for (a=0,p=child->argv; *p; a++,p++) {
1231                                 final_printf("%s   argv[%d] = %s\n",ind,a,*p);
1232                         }
1233                         globfree(&child->glob_result);
1234                         child->argv=NULL;
1235                 } else if (child->group) {
1236                         final_printf("%s   begin group (subshell:%d)\n",ind, child->subshell);
1237                         ret_code = run_list_test(child->group,indent+3);
1238                         final_printf("%s   end group\n",ind);
1239                 } else {
1240                         final_printf("%s   (nil)\n",ind);
1241                 }
1242                 for (r=child->redirects; r; r=rnext) {
1243                         final_printf("%s   redirect %d%s", ind, r->fd, redir_table[r->type].descrip);
1244                         if (r->dup == -1) {
1245                                 final_printf(" %s\n", *r->word.gl_pathv);
1246                                 globfree(&r->word);
1247                         } else {
1248                                 final_printf("&%d\n", r->dup);
1249                         }
1250                         rnext=r->next;
1251                         free(r);
1252                 }
1253                 child->redirects=NULL;
1254         }
1255         free(pi->progs);   /* children are an array, they get freed all at once */
1256         pi->progs=NULL;
1257         return ret_code;
1258 }
1259
1260 static int run_list_test(struct pipe *head, int indent)
1261 {
1262         int rcode=0;   /* if list has no members */
1263         struct pipe *pi, *next;
1264         char *ind = indenter(indent);
1265         for (pi=head; pi; pi=next) {
1266                 if (pi->num_progs == 0) break;
1267                 final_printf("%s pipe reserved mode %d\n", ind, pi->r_mode);
1268                 rcode = run_pipe_test(pi, indent);
1269                 final_printf("%s pipe followup code %d\n", ind, pi->followup);
1270                 next=pi->next;
1271                 pi->next=NULL;
1272                 free(pi);
1273         }
1274         return rcode;   
1275 }
1276
1277 /* Select which version we will use */
1278 static int run_list(struct pipe *pi)
1279 {
1280         int rcode=0;
1281         if (fake_mode==0) {
1282                 rcode = run_list_real(pi);
1283         } 
1284         /* run_list_test has the side effect of clearing memory
1285          * In the long run that function can be merged with run_list_real,
1286          * but doing that now would hobble the debugging effort. */
1287         run_list_test(pi,0);
1288         return rcode;
1289 }
1290
1291 /* The API for glob is arguably broken.  This routine pushes a non-matching
1292  * string into the output structure, removing non-backslashed backslashes.
1293  * If someone can prove me wrong, by performing this function within the
1294  * original glob(3) api, feel free to rewrite this routine into oblivion.
1295  * Return code (0 vs. GLOB_NOSPACE) matches glob(3).
1296  * XXX broken if the last character is '\\', check that before calling.
1297  */
1298 static int globhack(const char *src, int flags, glob_t *pglob)
1299 {
1300         int cnt, pathc;
1301         const char *s;
1302         char *dest;
1303         for (cnt=1, s=src; *s; s++) {
1304                 if (*s == '\\') s++;
1305                 cnt++;
1306         }
1307         dest = malloc(cnt);
1308         if (!dest) return GLOB_NOSPACE;
1309         if (!(flags & GLOB_APPEND)) {
1310                 pglob->gl_pathv=NULL;
1311                 pglob->gl_pathc=0;
1312                 pglob->gl_offs=0;
1313                 pglob->gl_offs=0;
1314         }
1315         pathc = ++pglob->gl_pathc;
1316         pglob->gl_pathv = realloc(pglob->gl_pathv, (pathc+1)*sizeof(*pglob->gl_pathv));
1317         if (pglob->gl_pathv == NULL) return GLOB_NOSPACE;
1318         pglob->gl_pathv[pathc-1]=dest;
1319         pglob->gl_pathv[pathc]=NULL;
1320         for (s=src; *s; s++, dest++) {
1321                 if (*s == '\\') s++;
1322                 *dest = *s;
1323         }
1324         *dest='\0';
1325         return 0;
1326 }
1327
1328 /* XXX broken if the last character is '\\', check that before calling */
1329 static int glob_needed(const char *s)
1330 {
1331         for (; *s; s++) {
1332                 if (*s == '\\') s++;
1333                 if (strchr("*[?",*s)) return 1;
1334         }
1335         return 0;
1336 }
1337
1338 #if 0
1339 static void globprint(glob_t *pglob)
1340 {
1341         int i;
1342         debug_printf("glob_t at %p:\n", pglob);
1343         debug_printf("  gl_pathc=%d  gl_pathv=%p  gl_offs=%d  gl_flags=%d\n",
1344                 pglob->gl_pathc, pglob->gl_pathv, pglob->gl_offs, pglob->gl_flags);
1345         for (i=0; i<pglob->gl_pathc; i++)
1346                 debug_printf("pglob->gl_pathv[%d] = %p = %s\n", i,
1347                         pglob->gl_pathv[i], pglob->gl_pathv[i]);
1348 }
1349 #endif
1350
1351 static int xglob(o_string *dest, int flags, glob_t *pglob)
1352 {
1353         int gr;
1354
1355         /* short-circuit for null word */
1356         /* we can code this better when the debug_printf's are gone */
1357         if (dest->length == 0) {
1358                 if (dest->nonnull) {
1359                         /* bash man page calls this an "explicit" null */
1360                         gr = globhack(dest->data, flags, pglob);
1361                         debug_printf("globhack returned %d\n",gr);
1362                 } else {
1363                         return 0;
1364                 }
1365         } else if (glob_needed(dest->data)) {
1366                 gr = glob(dest->data, flags, NULL, pglob);
1367                 debug_printf("glob returned %d\n",gr);
1368                 if (gr == GLOB_NOMATCH) {
1369                         /* quote removal, or more accurately, backslash removal */
1370                         gr = globhack(dest->data, flags, pglob);
1371                         debug_printf("globhack returned %d\n",gr);
1372                 }
1373         } else {
1374                 gr = globhack(dest->data, flags, pglob);
1375                 debug_printf("globhack returned %d\n",gr);
1376         }
1377         if (gr == GLOB_NOSPACE) {
1378                 fprintf(stderr,"out of memory during glob\n");
1379                 exit(1);
1380         }
1381         if (gr != 0) { /* GLOB_ABORTED ? */
1382                 fprintf(stderr,"glob(3) error %d\n",gr);
1383         }
1384         /* globprint(glob_target); */
1385         return gr;
1386 }
1387
1388 /* the src parameter allows us to peek forward to a possible &n syntax
1389  * for file descriptor duplication, e.g., "2>&1".
1390  * Return code is 0 normally, 1 if a syntax error is detected in src.
1391  * Resource errors (in xmalloc) cause the process to exit */
1392 static int setup_redirect(struct p_context *ctx, int fd, redir_type style,
1393         struct in_str *input)
1394 {
1395         struct child_prog *child=ctx->child;
1396         struct redir_struct *redir = child->redirects;
1397         struct redir_struct *last_redir=NULL;
1398
1399         /* Create a new redir_struct and drop it onto the end of the linked list */
1400         while(redir) {
1401                 last_redir=redir;
1402                 redir=redir->next;
1403         }
1404         redir = xmalloc(sizeof(struct redir_struct));
1405         redir->next=NULL;
1406         if (last_redir) {
1407                 last_redir->next=redir;
1408         } else {
1409                 child->redirects=redir;
1410         }
1411
1412         redir->type=style;
1413         redir->fd= (fd==-1) ? redir_table[style].default_fd : fd ;
1414
1415         debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
1416
1417         /* Check for a '2>&1' type redirect */ 
1418         redir->dup = redirect_dup_num(input);
1419         if (redir->dup == -2) return 1;  /* syntax error */
1420         if (redir->dup != -1) {
1421                 /* Erik had a check here that the file descriptor in question
1422                  * is legit; I postpone that to "run time" */
1423                 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
1424         } else {
1425                 /* We do _not_ try to open the file that src points to,
1426                  * since we need to return and let src be expanded first.
1427                  * Set ctx->pending_redirect, so we know what to do at the
1428                  * end of the next parsed word.
1429                  */
1430                 ctx->pending_redirect = redir;
1431         }
1432         return 0;
1433 }
1434
1435 struct pipe *new_pipe(void) {
1436         struct pipe *pi;
1437         pi = xmalloc(sizeof(struct pipe));
1438         pi->num_progs = 0;
1439         pi->progs = NULL;
1440         pi->next = NULL;
1441         pi->followup = 0;  /* invalid */
1442         return pi;
1443 }
1444
1445 static void initialize_context(struct p_context *ctx)
1446 {
1447         ctx->pipe=NULL;
1448         ctx->pending_redirect=NULL;
1449         ctx->child=NULL;
1450         ctx->list_head=new_pipe();
1451         ctx->pipe=ctx->list_head;
1452         ctx->w=RES_NONE;
1453         ctx->stack=NULL;
1454         done_command(ctx);   /* creates the memory for working child */
1455 }
1456
1457 /* normal return is 0
1458  * if a reserved word is found, and processed, return 1
1459  * should handle if, then, elif, else, fi, for, while, until, do, done.
1460  * case, function, and select are obnoxious, save those for later.
1461  */
1462 int reserved_word(o_string *dest, struct p_context *ctx)
1463 {
1464         struct reserved_combo {
1465                 char *literal;
1466                 int code;
1467                 long flag;
1468         };
1469         /* Mostly a list of accepted follow-up reserved words.
1470          * FLAG_END means we are done with the sequence, and are ready
1471          * to turn the compound list into a command.
1472          * FLAG_START means the word must start a new compound list.
1473          */
1474         static struct reserved_combo reserved_list[] = {
1475                 { "if",    RES_IF,    FLAG_THEN | FLAG_START },
1476                 { "then",  RES_THEN,  FLAG_ELIF | FLAG_ELSE | FLAG_FI },
1477                 { "elif",  RES_ELIF,  FLAG_THEN },
1478                 { "else",  RES_ELSE,  FLAG_FI   },
1479                 { "fi",    RES_FI,    FLAG_END  },
1480                 { "for",   RES_FOR,   FLAG_DO   | FLAG_START },
1481                 { "while", RES_WHILE, FLAG_DO   | FLAG_START },
1482                 { "until", RES_UNTIL, FLAG_DO   | FLAG_START },
1483                 { "do",    RES_DO,    FLAG_DONE },
1484                 { "done",  RES_DONE,  FLAG_END  }
1485         };
1486         struct reserved_combo *r;
1487         for (r=reserved_list;
1488 #define NRES sizeof(reserved_list)/sizeof(struct reserved_combo)
1489                 r<reserved_list+NRES; r++) {
1490                 if (strcmp(dest->data, r->literal) == 0) {
1491                         debug_printf("found reserved word %s, code %d\n",r->literal,r->code);
1492                         if (r->flag & FLAG_START) {
1493                                 struct p_context *new = xmalloc(sizeof(struct p_context));
1494                                 debug_printf("push stack\n");
1495                                 *new = *ctx;   /* physical copy */
1496                                 initialize_context(ctx);
1497                                 ctx->stack=new;
1498                         } else if ( ctx->w == RES_NONE || ! (ctx->old_flag & (1<<r->code))) {
1499                                 syntax();
1500                                 ctx->w = RES_SNTX;
1501                                 b_reset (dest);
1502                                 return 1;
1503                         }
1504                         ctx->w=r->code;
1505                         ctx->old_flag = r->flag;
1506                         if (ctx->old_flag & FLAG_END) {
1507                                 struct p_context *old;
1508                                 debug_printf("pop stack\n");
1509                                 old = ctx->stack;
1510                                 old->child->group = ctx->list_head;
1511                                 *ctx = *old;   /* physical copy */
1512                                 free(old);
1513                                 ctx->w=RES_NONE;
1514                         }
1515                         b_reset (dest);
1516                         return 1;
1517                 }
1518         }
1519         return 0;
1520 }
1521
1522 /* normal return is 0.
1523  * Syntax or xglob errors return 1. */
1524 static int done_word(o_string *dest, struct p_context *ctx)
1525 {
1526         struct child_prog *child=ctx->child;
1527         glob_t *glob_target;
1528         int gr, flags = 0;
1529
1530         debug_printf("done_word: %s %p\n", dest->data, child);
1531         if (dest->length == 0 && !dest->nonnull) {
1532                 debug_printf("  true null, ignored\n");
1533                 return 0;
1534         }
1535         if (ctx->pending_redirect) {
1536                 glob_target = &ctx->pending_redirect->word;
1537         } else {
1538                 if (child->group) {
1539                         syntax();
1540                         return 1;  /* syntax error, groups and arglists don't mix */
1541                 }
1542                 if (!child->argv) {
1543                         debug_printf("checking %s for reserved-ness\n",dest->data);
1544                         if (reserved_word(dest,ctx)) return ctx->w==RES_SNTX;
1545                 }
1546                 glob_target = &child->glob_result;
1547                 if (child->argv) flags |= GLOB_APPEND;
1548         }
1549         gr = xglob(dest, flags, glob_target);
1550         if (gr != 0) return 1;
1551
1552         b_reset(dest);
1553         if (ctx->pending_redirect) {
1554                 ctx->pending_redirect=NULL;
1555                 if (glob_target->gl_pathc != 1) {
1556                         fprintf(stderr, "ambiguous redirect\n");
1557                         return 1;
1558                 }
1559         } else {
1560                 child->argv = glob_target->gl_pathv;
1561         }
1562         return 0;
1563 }
1564
1565 /* The only possible error here is out of memory, in which case
1566  * xmalloc exits. */
1567 static int done_command(struct p_context *ctx)
1568 {
1569         /* The child is really already in the pipe structure, so
1570          * advance the pipe counter and make a new, null child.
1571          * Only real trickiness here is that the uncommitted
1572          * child structure, to which ctx->child points, is not
1573          * counted in pi->num_progs. */
1574         struct pipe *pi=ctx->pipe;
1575         struct child_prog *prog=ctx->child;
1576
1577         if (prog && prog->group == NULL
1578                  && prog->argv == NULL
1579                  && prog->redirects == NULL) {
1580                 debug_printf("done_command: skipping null command\n");
1581                 return 0;
1582         } else if (prog) {
1583                 pi->num_progs++;
1584                 debug_printf("done_command: num_progs incremented to %d\n",pi->num_progs);
1585         } else {
1586                 debug_printf("done_command: initializing\n");
1587         }
1588         pi->progs = xrealloc(pi->progs, sizeof(*pi->progs) * (pi->num_progs+1));
1589
1590         prog = pi->progs + pi->num_progs;
1591         prog->redirects = NULL;
1592         prog->argv = NULL;
1593         prog->is_stopped = 0;
1594         prog->group = NULL;
1595         prog->glob_result.gl_pathv = NULL;
1596         prog->family = pi;
1597
1598         ctx->child=prog;
1599         /* but ctx->pipe and ctx->list_head remain unchanged */
1600         return 0;
1601 }
1602
1603 static int done_pipe(struct p_context *ctx, pipe_style type)
1604 {
1605         struct pipe *new_p;
1606         done_command(ctx);  /* implicit closure of previous command */
1607         debug_printf("done_pipe, type %d\n", type);
1608         ctx->pipe->followup = type;
1609         ctx->pipe->r_mode = ctx->w;
1610         new_p=new_pipe();
1611         ctx->pipe->next = new_p;
1612         ctx->pipe = new_p;
1613         ctx->child = NULL;
1614         done_command(ctx);  /* set up new pipe to accept commands */
1615         return 0;
1616 }
1617
1618 /* peek ahead in the in_str to find out if we have a "&n" construct,
1619  * as in "2>&1", that represents duplicating a file descriptor.
1620  * returns either -2 (syntax error), -1 (no &), or the number found.
1621  */
1622 static int redirect_dup_num(struct in_str *input)
1623 {
1624         int ch, d=0, ok=0;
1625         ch = b_peek(input);
1626         if (ch != '&') return -1;
1627
1628         b_getch(input);  /* get the & */
1629         while (ch=b_peek(input),isdigit(ch)) {
1630                 d = d*10+(ch-'0');
1631                 ok=1;
1632                 b_getch(input);
1633         }
1634         if (ok) return d;
1635
1636         fprintf(stderr, "ambiguous redirect\n");
1637         return -2;
1638 }
1639
1640 /* If a redirect is immediately preceded by a number, that number is
1641  * supposed to tell which file descriptor to redirect.  This routine
1642  * looks for such preceding numbers.  In an ideal world this routine
1643  * needs to handle all the following classes of redirects...
1644  *     echo 2>foo     # redirects fd  2 to file "foo", nothing passed to echo
1645  *     echo 49>foo    # redirects fd 49 to file "foo", nothing passed to echo
1646  *     echo -2>foo    # redirects fd  1 to file "foo",    "-2" passed to echo
1647  *     echo 49x>foo   # redirects fd  1 to file "foo",   "49x" passed to echo
1648  * A -1 output from this program means no valid number was found, so the
1649  * caller should use the appropriate default for this redirection.
1650  */
1651 static int redirect_opt_num(o_string *o)
1652 {
1653         int num;
1654
1655         if (o->length==0) return -1;
1656         for(num=0; num<o->length; num++) {
1657                 if (!isdigit(*(o->data+num))) {
1658                         return -1;
1659                 }
1660         }
1661         /* reuse num (and save an int) */
1662         num=atoi(o->data);
1663         b_reset(o);
1664         return num;
1665 }
1666
1667 FILE *generate_stream_from_list(struct pipe *head)
1668 {
1669         FILE *pf;
1670 #if 1
1671         int pid, channel[2];
1672         if (pipe(channel)<0) perror_msg_and_die("pipe");
1673         pid=fork();
1674         if (pid<0) {
1675                 perror_msg_and_die("fork");
1676         } else if (pid==0) {
1677                 close(channel[0]);
1678                 if (channel[1] != 1) {
1679                         dup2(channel[1],1);
1680                         close(channel[1]);
1681                 }
1682 #if 0
1683 #define SURROGATE "surrogate response"
1684                 write(1,SURROGATE,sizeof(SURROGATE));
1685                 exit(run_list(head));
1686 #else
1687                 exit(run_list_real(head));   /* leaks memory */
1688 #endif
1689         }
1690         debug_printf("forked child %d\n",pid);
1691         close(channel[1]);
1692         pf = fdopen(channel[0],"r");
1693         debug_printf("pipe on FILE *%p\n",pf);
1694 #else
1695         run_list_test(head,0);
1696         pf=popen("echo surrogate response","r");
1697         debug_printf("started fake pipe on FILE *%p\n",pf);
1698 #endif
1699         return pf;
1700 }
1701
1702 /* this version hacked for testing purposes */
1703 /* return code is exit status of the process that is run. */
1704 static int process_command_subs(o_string *dest, struct p_context *ctx, struct in_str *input, int subst_end)
1705 {
1706         int retcode;
1707         o_string result=NULL_O_STRING;
1708         struct p_context inner;
1709         FILE *p;
1710         struct in_str pipe_str;
1711         initialize_context(&inner);
1712
1713         /* recursion to generate command */
1714         retcode = parse_stream(&result, &inner, input, subst_end);
1715         if (retcode != 0) return retcode;  /* syntax error or EOF */
1716         done_word(&result, &inner);
1717         done_pipe(&inner, PIPE_SEQ);
1718         b_free(&result);
1719
1720         p=generate_stream_from_list(inner.list_head);
1721         if (p==NULL) return 1;
1722         mark_open(fileno(p));
1723         setup_file_in_str(&pipe_str, p);
1724
1725         /* now send results of command back into original context */
1726         retcode = parse_stream(dest, ctx, &pipe_str, '\0');
1727         /* XXX In case of a syntax error, should we try to kill the child?
1728          * That would be tough to do right, so just read until EOF. */
1729         if (retcode == 1) {
1730                 while (b_getch(&pipe_str)!=EOF) { /* discard */ };
1731         }
1732
1733         debug_printf("done reading from pipe, pclose()ing\n");
1734         /* This is the step that wait()s for the child.  Should be pretty
1735          * safe, since we just read an EOF from its stdout.  We could try
1736          * to better, by using wait(), and keeping track of background jobs
1737          * at the same time.  That would be a lot of work, and contrary
1738          * to the KISS philosophy of this program. */
1739         mark_closed(fileno(p));
1740         retcode=pclose(p);
1741         debug_printf("pclosed, retcode=%d\n",retcode);
1742         /* XXX this process fails to trim a single trailing newline */
1743         return retcode;
1744 }
1745
1746 static int parse_group(o_string *dest, struct p_context *ctx,
1747         struct in_str *input, int ch)
1748 {
1749         int rcode, endch=0;
1750         struct p_context sub;
1751         struct child_prog *child = ctx->child;
1752         if (child->argv) {
1753                 syntax();
1754                 return 1;  /* syntax error, groups and arglists don't mix */
1755         }
1756         initialize_context(&sub);
1757         switch(ch) {
1758                 case '(': endch=')'; child->subshell=1; break;
1759                 case '{': endch='}'; break;
1760                 default: syntax();   /* really logic error */
1761         }
1762         rcode=parse_stream(dest,&sub,input,endch);
1763         done_word(dest,&sub); /* finish off the final word in the subcontext */
1764         done_pipe(&sub, PIPE_SEQ);  /* and the final command there, too */
1765         child->group = sub.list_head;
1766         return rcode;
1767         /* child remains "open", available for possible redirects */
1768 }
1769
1770 /* basically useful version until someone wants to get fancier,
1771  * see the bash man page under "Parameter Expansion" */
1772 static void lookup_param(o_string *dest, struct p_context *ctx, o_string *src)
1773 {
1774         const char *p=NULL;
1775         if (src->data) p = getenv(src->data);
1776         if (p) parse_string(dest, ctx, p);   /* recursion */
1777         b_free(src);
1778 }
1779
1780 /* return code: 0 for OK, 1 for syntax error */
1781 static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input)
1782 {
1783         int i, advance=0;
1784         o_string alt=NULL_O_STRING;
1785         char sep[]=" ";
1786         int ch = input->peek(input);  /* first character after the $ */
1787         debug_printf("handle_dollar: ch=%c\n",ch);
1788         if (isalpha(ch)) {
1789                 while(ch=b_peek(input),isalnum(ch) || ch=='_') {
1790                         b_getch(input);
1791                         b_addchr(&alt,ch);
1792                 }
1793                 lookup_param(dest, ctx, &alt);
1794         } else if (isdigit(ch)) {
1795                 i = ch-'0';  /* XXX is $0 special? */
1796                 if (i<global_argc) {
1797                         parse_string(dest, ctx, global_argv[i]); /* recursion */
1798                 }
1799                 advance = 1;
1800         } else switch (ch) {
1801                 case '$':
1802                         b_adduint(dest,getpid());
1803                         advance = 1;
1804                         break;
1805                 case '!':
1806                         if (last_bg_pid > 0) b_adduint(dest, last_bg_pid);
1807                         advance = 1;
1808                         break;
1809                 case '?':
1810                         b_adduint(dest,last_return_code);
1811                         advance = 1;
1812                         break;
1813                 case '#':
1814                         b_adduint(dest,global_argc ? global_argc-1 : 0);
1815                         advance = 1;
1816                         break;
1817                 case '{':
1818                         b_getch(input);
1819                         /* XXX maybe someone will try to escape the '}' */
1820                         while(ch=b_getch(input),ch!=EOF && ch!='}') {
1821                                 b_addchr(&alt,ch);
1822                         }
1823                         if (ch != '}') {
1824                                 syntax();
1825                                 return 1;
1826                         }
1827                         lookup_param(dest, ctx, &alt);
1828                         break;
1829                 case '(':
1830                         process_command_subs(dest, ctx, input, ')');
1831                         break;
1832                 case '*':
1833                         sep[0]=ifs[0];
1834                         for (i=1; i<global_argc; i++) {
1835                                 parse_string(dest, ctx, global_argv[i]);
1836                                 if (i+1 < global_argc) parse_string(dest, ctx, sep);
1837                         }
1838                         break;
1839                 case '@':
1840                 case '-':
1841                 case '_':
1842                         /* still unhandled, but should be eventually */
1843                         fprintf(stderr,"unhandled syntax: $%c\n",ch);
1844                         return 1;
1845                         break;
1846                 default:
1847                         b_addqchr(dest,'$',dest->quote);
1848         }
1849         /* Eat the character if the flag was set.  If the compiler
1850          * is smart enough, we could substitute "b_getch(input);"
1851          * for all the "advance = 1;" above, and also end up with
1852          * a nice size-optimized program.  Hah!  That'll be the day.
1853          */
1854         if (advance) b_getch(input);
1855         return 0;
1856 }
1857
1858 int parse_string(o_string *dest, struct p_context *ctx, const char *src)
1859 {
1860         struct in_str foo;
1861         setup_string_in_str(&foo, src);
1862         return parse_stream(dest, ctx, &foo, '\0');
1863 }
1864
1865 /* return code is 0 for normal exit, 1 for syntax error */
1866 int parse_stream(o_string *dest, struct p_context *ctx,
1867         struct in_str *input, int end_trigger)
1868 {
1869         unsigned int ch, m;
1870         int redir_fd;
1871         redir_type redir_style;
1872         int next;
1873
1874         /* Only double-quote state is handled in the state variable dest->quote.
1875          * A single-quote triggers a bypass of the main loop until its mate is
1876          * found.  When recursing, quote state is passed in via dest->quote. */
1877
1878         debug_printf("parse_stream, end_trigger=%d\n",end_trigger);
1879         while ((ch=b_getch(input))!=EOF) {
1880                 m = map[ch];
1881                 next = (ch == '\n') ? 0 : b_peek(input);
1882                 debug_printf("parse_stream: ch=%c (%d) m=%d quote=%d\n",
1883                         ch,ch,m,dest->quote);
1884                 if (m==0 || ((m==1 || m==2) && dest->quote)) {
1885                         b_addqchr(dest, ch, dest->quote);
1886                 } else {
1887                         if (m==2) {  /* unquoted IFS */
1888                                 done_word(dest, ctx);
1889                                 if (ch=='\n') done_pipe(ctx,PIPE_SEQ);
1890                         }
1891                         if (ch == end_trigger && !dest->quote && ctx->w==RES_NONE) {
1892                                 debug_printf("leaving parse_stream\n");
1893                                 return 0;
1894                         }
1895 #if 0
1896                         if (ch=='\n') {
1897                                 /* Yahoo!  Time to run with it! */
1898                                 done_pipe(ctx,PIPE_SEQ);
1899                                 run_list(ctx->list_head);
1900                                 initialize_context(ctx);
1901                         }
1902 #endif
1903                         if (m!=2) switch (ch) {
1904                 case '#':
1905                         if (dest->length == 0 && !dest->quote) {
1906                                 while(ch=b_peek(input),ch!=EOF && ch!='\n') { b_getch(input); }
1907                         } else {
1908                                 b_addqchr(dest, ch, dest->quote);
1909                         }
1910                         break;
1911                 case '\\':
1912                         if (next == EOF) {
1913                                 syntax();
1914                                 return 1;
1915                         }
1916                         b_addqchr(dest, '\\', dest->quote);
1917                         b_addqchr(dest, b_getch(input), dest->quote);
1918                         break;
1919                 case '$':
1920                         if (handle_dollar(dest, ctx, input)!=0) return 1;
1921                         break;
1922                 case '\'':
1923                         dest->nonnull = 1;
1924                         while(ch=b_getch(input),ch!=EOF && ch!='\'') {
1925                                 b_addchr(dest,ch);
1926                         }
1927                         if (ch==EOF) {
1928                                 syntax();
1929                                 return 1;
1930                         }
1931                         break;
1932                 case '"':
1933                         dest->nonnull = 1;
1934                         dest->quote = !dest->quote;
1935                         break;
1936                 case '`':
1937                         process_command_subs(dest, ctx, input, '`');
1938                         break;
1939                 case '>':
1940                         redir_fd = redirect_opt_num(dest);
1941                         done_word(dest, ctx);
1942                         redir_style=REDIRECT_OVERWRITE;
1943                         if (next == '>') {
1944                                 redir_style=REDIRECT_APPEND;
1945                                 b_getch(input);
1946                         } else if (next == '(') {
1947                                 syntax();   /* until we support >(list) Process Substitution */
1948                                 return 1;
1949                         }
1950                         setup_redirect(ctx, redir_fd, redir_style, input);
1951                         break;
1952                 case '<':
1953                         redir_fd = redirect_opt_num(dest);
1954                         done_word(dest, ctx);
1955                         redir_style=REDIRECT_INPUT;
1956                         if (next == '<') {
1957                                 redir_style=REDIRECT_HEREIS;
1958                                 b_getch(input);
1959                         } else if (next == '>') {
1960                                 redir_style=REDIRECT_IO;
1961                                 b_getch(input);
1962                         } else if (next == '(') {
1963                                 syntax();   /* until we support <(list) Process Substitution */
1964                                 return 1;
1965                         }
1966                         setup_redirect(ctx, redir_fd, redir_style, input);
1967                         break;
1968                 case ';':
1969                         done_word(dest, ctx);
1970                         done_pipe(ctx,PIPE_SEQ);
1971                         break;
1972                 case '&':
1973                         done_word(dest, ctx);
1974                         if (next=='&') {
1975                                 b_getch(input);
1976                                 done_pipe(ctx,PIPE_AND);
1977                         } else {
1978                                 done_pipe(ctx,PIPE_BG);
1979                         }
1980                         break;
1981                 case '|':
1982                         done_word(dest, ctx);
1983                         if (next=='|') {
1984                                 b_getch(input);
1985                                 done_pipe(ctx,PIPE_OR);
1986                         } else {
1987                                 /* we could pick up a file descriptor choice here
1988                                  * with redirect_opt_num(), but bash doesn't do it.
1989                                  * "echo foo 2| cat" yields "foo 2". */
1990                                 done_command(ctx);
1991                         }
1992                         break;
1993                 case '(':
1994                 case '{':
1995                         if (parse_group(dest, ctx, input, ch)!=0) return 1;
1996                         break;
1997                 case ')':
1998                 case '}':
1999                         syntax();   /* Proper use of this character caught by end_trigger */
2000                         return 1;
2001                         break;
2002                 default:
2003                         syntax();   /* this is really an internal logic error */
2004                         return 1;
2005                         }
2006                 }
2007         }
2008         /* complain if quote?  No, maybe we just finished a command substitution
2009          * that was quoted.  Example:
2010          * $ echo "`cat foo` plus more" 
2011          * and we just got the EOF generated by the subshell that ran "cat foo"
2012          * The only real complaint is if we got an EOF when end_trigger != '\0',
2013          * that is, we were really supposed to get end_trigger, and never got
2014          * one before the EOF.  Can't use the standard "syntax error" return code,
2015          * so that parse_stream_outer can distinguish the EOF and exit smoothly. */
2016         if (end_trigger != '\0') return -1;
2017         return 0;
2018 }
2019
2020 void mapset(const unsigned char *set, int code)
2021 {
2022         const unsigned char *s;
2023         for (s=set; *s; s++) map[*s] = code;
2024 }
2025
2026 void update_ifs_map(void)
2027 {
2028         /* char *ifs and char map[256] are both globals. */
2029         ifs = getenv("IFS");
2030         if (ifs == NULL) ifs=" \t\n";
2031         /* Precompute a list of 'flow through' behavior so it can be treated
2032          * quickly up front.  Computation is necessary because of IFS.
2033          * Special case handling of IFS == " \t\n" is not implemented.
2034          * The map[] array only really needs two bits each, and on most machines
2035          * that would be faster because of the reduced L1 cache footprint.
2036          */
2037         memset(map,0,256);        /* most characters flow through always */
2038         mapset("\\$'\"`", 3);     /* never flow through */
2039         mapset("<>;&|(){}#", 1);  /* flow through if quoted */
2040         mapset(ifs, 2);           /* also flow through if quoted */
2041 }
2042
2043 /* most recursion does not come through here, the exeception is
2044  * from builtin_source() */
2045 int parse_stream_outer(struct in_str *inp)
2046 {
2047
2048         struct p_context ctx;
2049         o_string temp=NULL_O_STRING;
2050         int rcode;
2051         do {
2052                 initialize_context(&ctx);
2053                 update_ifs_map();
2054                 inp->promptmode=1;
2055                 rcode = parse_stream(&temp, &ctx, inp, '\n');
2056                 done_word(&temp, &ctx);
2057                 done_pipe(&ctx,PIPE_SEQ);
2058                 run_list(ctx.list_head);
2059         } while (rcode != -1);   /* loop on syntax errors, return on EOF */
2060         return 0;
2061 }
2062
2063 static int parse_string_outer(const char *s)
2064 {
2065         struct in_str input;
2066         setup_string_in_str(&input, s);
2067         return parse_stream_outer(&input);
2068 }
2069
2070 static int parse_file_outer(FILE *f)
2071 {
2072         int rcode;
2073         struct in_str input;
2074         setup_file_in_str(&input, f);
2075         rcode = parse_stream_outer(&input);
2076         return rcode;
2077 }
2078
2079 int shell_main(int argc, char **argv)
2080 {
2081         int opt;
2082         FILE *input;
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                                         exit(opt);
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         if (interactive) {
2141                 /* Looks like they want an interactive shell */
2142                 fprintf(stdout, "\nhush -- the humble shell v0.01 (testing)\n\n");
2143                 exit(parse_file_outer(stdin));
2144         }
2145         debug_printf("\ninteractive=%d\n", interactive);
2146
2147         debug_printf("\nrunning script '%s'\n", argv[optind]);
2148         global_argv = argv+optind;
2149         global_argc = argc-optind;
2150         input = xfopen(argv[optind], "r");
2151         opt = parse_file_outer(input);
2152
2153 #ifdef BB_FEATURE_CLEAN_UP
2154         fclose(input.file);
2155 #endif
2156
2157         return(opt);
2158 }