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