blob: 4641e59c8165f03b0fa810694d28886e9a799362 [file] [log] [blame]
Eric Andersen25f27032001-04-26 23:22:31 +00001/* vi: set sw=4 ts=4: */
2/*
3 * sh.c -- a prototype Bourne shell grammar parser
4 * Intended to follow the original Thompson and Ritchie
5 * "small and simple is beautiful" philosophy, which
6 * incidentally is a good match to today's BusyBox.
7 *
8 * Copyright (C) 2000,2001 Larry Doolittle <larry@doolittle.boa.org>
9 *
10 * Credits:
11 * The parser routines proper are all original material, first
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
Eric Andersenaac75e52001-04-30 18:18:45 +000048 * reserved words: case, esac, function
Eric Andersen25f27032001-04-26 23:22:31 +000049 * Here Documents ( << word )
50 * Functions
51 * Major bugs:
52 * job handling woefully incomplete and buggy
53 * reserved word execution woefully incomplete and buggy
Eric Andersen25f27032001-04-26 23:22:31 +000054 * 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
Eric Andersen25f27032001-04-26 23:22:31 +000074 *
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> */
Eric Andersen4ed5e372001-05-01 01:49:50 +0000105/* #define DEBUG_SHELL */
Eric Andersen25f27032001-04-26 23:22:31 +0000106
107#ifdef BB_VER
108#include "busybox.h"
109#include "cmdedit.h"
110#else
Eric Andersen25f27032001-04-26 23:22:31 +0000111#define applet_name "hush"
Eric Andersenaf44a0e2001-04-27 07:26:12 +0000112#include "standalone.h"
Eric Andersen25f27032001-04-26 23:22:31 +0000113#define shell_main main
Eric Andersenaf44a0e2001-04-27 07:26:12 +0000114#define BB_FEATURE_SH_SIMPLE_PROMPT
115#endif
Eric Andersen25f27032001-04-26 23:22:31 +0000116
117typedef 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 */
127struct {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
136typedef 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 */
144typedef 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,
Eric Andersenaf44a0e2001-04-27 07:26:12 +0000156 RES_XXXX = 11,
157 RES_SNTX = 12
Eric Andersen25f27032001-04-26 23:22:31 +0000158} 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 */
173struct 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
184struct 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
192struct 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
203struct 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
219struct jobset {
220 struct pipe *head; /* head of list of running jobs */
221 struct pipe *fg; /* current foreground job */
222};
223
224struct 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 */
231char **global_argv;
232unsigned int global_argc;
233unsigned int last_return_code;
234extern char **environ; /* This is in <unistd.h>, but protected with __USE_GNU */
235
236/* Variables we export */
237unsigned int shell_context; /* Used in cmdedit.c to reset the
238 * context when someone hits ^C */
239
240/* "globals" within this file */
241static char *ifs=NULL;
242static char map[256];
243static int fake_mode=0;
244static int interactive=0;
245static struct close_me *close_me_head = NULL;
246static char *cwd;
Eric Andersenaf44a0e2001-04-27 07:26:12 +0000247/* static struct jobset job_list = { NULL, NULL }; */
Eric Andersen25f27032001-04-26 23:22:31 +0000248static unsigned int last_bg_pid=0;
249static char *PS1;
250static 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
257typedef 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? */
270struct 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
283struct 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 */
290static 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
296static 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
304static void debug_printf(const char *format, ...) { }
305#endif
306#define final_printf debug_printf
307
308void __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 */
315static int builtin_cd(struct child_prog *child);
316static int builtin_env(struct child_prog *child);
317static int builtin_exec(struct child_prog *child);
318static int builtin_exit(struct child_prog *child);
319static int builtin_export(struct child_prog *child);
320static int builtin_fg_bg(struct child_prog *child);
321static int builtin_help(struct child_prog *child);
322static int builtin_jobs(struct child_prog *child);
323static int builtin_pwd(struct child_prog *child);
324static int builtin_read(struct child_prog *child);
325static int builtin_shift(struct child_prog *child);
326static int builtin_source(struct child_prog *child);
327static int builtin_ulimit(struct child_prog *child);
328static int builtin_umask(struct child_prog *child);
329static int builtin_unset(struct child_prog *child);
330/* o_string manipulation: */
331static int b_check_space(o_string *o, int len);
332static int b_addchr(o_string *o, int ch);
333static void b_reset(o_string *o);
334static int b_addqchr(o_string *o, int ch, int quote);
335static int b_adduint(o_string *o, unsigned int i);
336/* in_str manipulations: */
337static int static_get(struct in_str *i);
338static int static_peek(struct in_str *i);
339static int file_get(struct in_str *i);
340static int file_peek(struct in_str *i);
341static void setup_file_in_str(struct in_str *i, FILE *f);
342static void setup_string_in_str(struct in_str *i, const char *s);
343/* close_me manipulations: */
344static void mark_open(int fd);
345static void mark_closed(int fd);
346static void close_all();
347/* "run" the final data structures: */
348static char *indenter(int i);
349static int run_list_test(struct pipe *head, int indent);
350static int run_pipe_test(struct pipe *pi, int indent);
351/* really run the final data structures: */
352static int setup_redirects(struct child_prog *prog, int squirrel[]);
353static int pipe_wait(struct pipe *pi);
354static int run_list_real(struct pipe *pi);
355static void pseudo_exec(struct child_prog *child) __attribute__ ((noreturn));
356static int run_pipe_real(struct pipe *pi);
357/* extended glob support: */
358static int globhack(const char *src, int flags, glob_t *pglob);
359static int glob_needed(const char *s);
360static int xglob(o_string *dest, int flags, glob_t *pglob);
361/* data structure manipulation: */
362static int setup_redirect(struct p_context *ctx, int fd, redir_type style, struct in_str *input);
363static void initialize_context(struct p_context *ctx);
364static int done_word(o_string *dest, struct p_context *ctx);
365static int done_command(struct p_context *ctx);
366static int done_pipe(struct p_context *ctx, pipe_style type);
367/* primary string parsing: */
368static int redirect_dup_num(struct in_str *input);
369static int redirect_opt_num(o_string *o);
370static int process_command_subs(o_string *dest, struct p_context *ctx, struct in_str *input, int subst_end);
371static int parse_group(o_string *dest, struct p_context *ctx, struct in_str *input, int ch);
372static void lookup_param(o_string *dest, struct p_context *ctx, o_string *src);
373static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input);
374static int parse_string(o_string *dest, struct p_context *ctx, const char *src);
375static int parse_stream(o_string *dest, struct p_context *ctx, struct in_str *input0, int end_trigger);
376/* setup: */
377static int parse_stream_outer(struct in_str *inp);
378static int parse_string_outer(const char *s);
379static 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. */
387static 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 */
408static 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 */
424static 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 */
435static 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 */
445static int builtin_exit(struct child_prog *child)
446{
447 if (child->argv[1] == NULL)
Eric Andersene67c3ce2001-05-02 02:09:36 +0000448 exit(last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +0000449 exit (atoi(child->argv[1]));
450}
451
452/* built-in 'export VAR=value' handler */
453static 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 */
467static 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 */
515static 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 */
531static 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 */
548static 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 */
556static 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 */
591static 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) */
608static 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
634static int builtin_ulimit(struct child_prog *child)
635{
636 printf("builtin_ulimit not written\n");
637 return EXIT_FAILURE;
638}
639
640static 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 */
647static 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
657static 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
673static 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
683static 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
690static 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 */
701static 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 */
712char *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
725static 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
734static int static_get(struct in_str *i)
735{
736 int ch=*i->p++;
737 if (ch=='\0') return EOF;
738 return ch;
739}
740
741static int static_peek(struct in_str *i)
742{
743 return *i->p;
744}
745
746static 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
757static inline void setup_prompt_string(int promptmode, char **prompt_str)
758{
Eric Andersenaf44a0e2001-04-27 07:26:12 +0000759 debug_printf("setup_prompt_string %d ",promptmode);
Eric Andersen25f27032001-04-26 23:22:31 +0000760#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;
Eric Andersenaf44a0e2001-04-27 07:26:12 +0000773#endif
774 debug_printf("result %s\n",*prompt_str);
Eric Andersen25f27032001-04-26 23:22:31 +0000775}
776
777static void get_user_input(struct in_str *i)
778{
779 char *prompt_str;
Eric Andersen088875f2001-04-27 07:49:41 +0000780 static char the_command[BUFSIZ];
Eric Andersen25f27032001-04-26 23:22:31 +0000781
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 */
803static 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;
Eric Andersene67c3ce2001-05-02 02:09:36 +0000817 i->__promptme = 0;
818 if (i->p && *i->p) {
819 ch=*i->p++;
820 }
Eric Andersen4ed5e372001-05-01 01:49:50 +0000821 } else {
Eric Andersene67c3ce2001-05-02 02:09:36 +0000822 ch = fgetc(i->file);
Eric Andersen25f27032001-04-26 23:22:31 +0000823 }
Eric Andersen4ed5e372001-05-01 01:49:50 +0000824
Eric Andersen25f27032001-04-26 23:22:31 +0000825 debug_printf("b_getch: got a %d\n", ch);
826 }
827 if (ch == '\n') i->__promptme=1;
828 return ch;
829}
830
831/* All the callers guarantee this routine will never be
832 * used right after a newline, so prompting is not needed.
833 */
834static int file_peek(struct in_str *i)
835{
836 if (i->p && *i->p) {
837 return *i->p;
838 } else {
Eric Andersene67c3ce2001-05-02 02:09:36 +0000839 static char buffer[2];
840 buffer[0] = fgetc(i->file);
841 buffer[1] = '\0';
842 i->p = buffer;
Eric Andersen25f27032001-04-26 23:22:31 +0000843 debug_printf("b_peek: got a %d\n", *i->p);
844 return *i->p;
845 }
846}
847
848static void setup_file_in_str(struct in_str *i, FILE *f)
849{
850 i->peek = file_peek;
851 i->get = file_get;
852 i->__promptme=1;
853 i->promptmode=1;
854 i->file = f;
855 i->p = NULL;
856}
857
858static void setup_string_in_str(struct in_str *i, const char *s)
859{
860 i->peek = static_peek;
861 i->get = static_get;
862 i->__promptme=1;
863 i->promptmode=1;
864 i->p = s;
865}
866
867static void mark_open(int fd)
868{
869 struct close_me *new = xmalloc(sizeof(struct close_me));
870 new->fd = fd;
871 new->next = close_me_head;
872 close_me_head = new;
873}
874
875static void mark_closed(int fd)
876{
877 struct close_me *tmp;
878 if (close_me_head == NULL || close_me_head->fd != fd)
879 error_msg_and_die("corrupt close_me");
880 tmp = close_me_head;
881 close_me_head = close_me_head->next;
882 free(tmp);
883}
884
885static void close_all()
886{
887 struct close_me *c;
888 for (c=close_me_head; c; c=c->next) {
889 close(c->fd);
890 }
891 close_me_head = NULL;
892}
893
894/* squirrel != NULL means we squirrel away copies of stdin, stdout,
895 * and stderr if they are redirected. */
896static int setup_redirects(struct child_prog *prog, int squirrel[])
897{
898 int openfd, mode;
899 struct redir_struct *redir;
900
901 for (redir=prog->redirects; redir; redir=redir->next) {
902 if (redir->dup == -1) {
903 mode=redir_table[redir->type].mode;
904 openfd = open(redir->word.gl_pathv[0], mode, 0666);
905 if (openfd < 0) {
906 /* this could get lost if stderr has been redirected, but
907 bash and ash both lose it as well (though zsh doesn't!) */
908 fprintf(stderr,"error opening %s: %s\n", redir->word.gl_pathv[0],
909 strerror(errno));
910 return 1;
911 }
912 } else {
913 openfd = redir->dup;
914 }
915
916 if (openfd != redir->fd) {
917 if (squirrel && redir->fd < 3) {
918 squirrel[redir->fd] = dup(redir->fd);
919 }
920 dup2(openfd, redir->fd);
921 close(openfd);
922 }
923 }
924 return 0;
925}
926
927static void restore_redirects(int squirrel[])
928{
929 int i, fd;
930 for (i=0; i<3; i++) {
931 fd = squirrel[i];
932 if (fd != -1) {
933 /* No error checking. I sure wouldn't know what
934 * to do with an error if I found one! */
935 dup2(fd, i);
936 close(fd);
937 }
938 }
939}
940
941/* XXX this definitely needs some more thought, work, and
942 * cribbing from other shells */
943static int pipe_wait(struct pipe *pi)
944{
945 int rcode=0, i, pid, running, status;
946 running = pi->num_progs;
947 while (running) {
948 pid=waitpid(-1, &status, 0);
949 if (pid < 0) perror_msg_and_die("waitpid");
950 for (i=0; i < pi->num_progs; i++) {
951 if (pi->progs[i].pid == pid) {
952 if (i==pi->num_progs-1) rcode=WEXITSTATUS(status);
953 pi->progs[i].pid = 0;
954 running--;
955 break;
956 }
957 }
958 }
959 return rcode;
960}
961
962/* very simple version for testing */
963static void pseudo_exec(struct child_prog *child)
964{
965 int rcode;
966 struct built_in_command *x;
967 if (child->argv) {
968 /*
969 * Check if the command matches any of the builtins.
970 * Depending on context, this might be redundant. But it's
971 * easier to waste a few CPU cycles than it is to figure out
972 * if this is one of those cases.
973 */
974 for (x = bltins; x->cmd; x++) {
975 if (strcmp(child->argv[0], x->cmd) == 0 ) {
976 debug_printf("builtin exec %s\n", child->argv[0]);
977 exit(x->function(child));
978 }
979 }
Eric Andersenaac75e52001-04-30 18:18:45 +0000980
981 /* Check if the command matches any busybox internal commands
982 * ("applets") here.
983 * FIXME: This feature is not 100% safe, since
984 * BusyBox is not fully reentrant, so we have no guarantee the things
985 * from the .bss are still zeroed, or that things from .data are still
986 * at their defaults. We could exec ourself from /proc/self/exe, but I
987 * really dislike relying on /proc for things. We could exec ourself
988 * from global_argv[0], but if we are in a chroot, we may not be able
989 * to find ourself... */
990#ifdef BB_FEATURE_SH_STANDALONE_SHELL
991 {
992 int argc_l;
993 char** argv_l=child->argv;
994 char *name = child->argv[0];
995
996#ifdef BB_FEATURE_SH_APPLETS_ALWAYS_WIN
997 /* Following discussions from November 2000 on the busybox mailing
998 * list, the default configuration, (without
999 * get_last_path_component()) lets the user force use of an
1000 * external command by specifying the full (with slashes) filename.
1001 * If you enable BB_FEATURE_SH_APPLETS_ALWAYS_WIN, then applets
1002 * _aways_ override external commands, so if you want to run
1003 * /bin/cat, it will use BusyBox cat even if /bin/cat exists on the
1004 * filesystem and is _not_ busybox. Some systems may want this,
1005 * most do not. */
1006 name = get_last_path_component(name);
1007#endif
1008 /* Count argc for use in a second... */
1009 for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
1010 optind = 1;
1011 debug_printf("running applet %s\n", name);
1012 run_applet_by_name(name, argc_l, child->argv);
1013 exit(1);
1014 }
1015#endif
Eric Andersen25f27032001-04-26 23:22:31 +00001016 debug_printf("exec of %s\n",child->argv[0]);
1017 execvp(child->argv[0],child->argv);
1018 perror("execvp");
1019 exit(1);
1020 } else if (child->group) {
1021 debug_printf("runtime nesting to group\n");
1022 interactive=0; /* crucial!!!! */
1023 rcode = run_list_real(child->group);
1024 /* OK to leak memory by not calling run_list_test,
1025 * since this process is about to exit */
1026 exit(rcode);
1027 } else {
1028 /* Can happen. See what bash does with ">foo" by itself. */
1029 debug_printf("trying to pseudo_exec null command\n");
1030 exit(EXIT_SUCCESS);
1031 }
1032}
1033
1034/* run_pipe_real() starts all the jobs, but doesn't wait for anything
1035 * to finish. See pipe_wait().
1036 *
1037 * return code is normally -1, when the caller has to wait for children
1038 * to finish to determine the exit status of the pipe. If the pipe
1039 * is a simple builtin command, however, the action is done by the
1040 * time run_pipe_real returns, and the exit code is provided as the
1041 * return value.
1042 *
1043 * The input of the pipe is always stdin, the output is always
1044 * stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
1045 * because it tries to avoid running the command substitution in
1046 * subshell, when that is in fact necessary. The subshell process
1047 * now has its stdout directed to the input of the appropriate pipe,
1048 * so this routine is noticeably simpler.
1049 */
1050static int run_pipe_real(struct pipe *pi)
1051{
1052 int i;
1053 int nextin, nextout;
1054 int pipefds[2]; /* pipefds[0] is for reading */
1055 struct child_prog *child;
1056 struct built_in_command *x;
1057
1058 nextin = 0;
1059 pi->pgrp = 0;
1060
1061 /* Check if this is a simple builtin (not part of a pipe).
1062 * Builtins within pipes have to fork anyway, and are handled in
1063 * pseudo_exec. "echo foo | read bar" doesn't work on bash, either.
1064 */
1065 if (pi->num_progs == 1 && pi->progs[0].argv != NULL) {
1066 child = & (pi->progs[0]);
1067 if (child->group && ! child->subshell) {
1068 int squirrel[] = {-1, -1, -1};
1069 int rcode;
1070 debug_printf("non-subshell grouping\n");
1071 setup_redirects(child, squirrel);
1072 /* XXX could we merge code with following builtin case,
1073 * by creating a pseudo builtin that calls run_list_real? */
1074 rcode = run_list_real(child->group);
1075 restore_redirects(squirrel);
1076 return rcode;
1077 }
1078 for (x = bltins; x->cmd; x++) {
1079 if (strcmp(child->argv[0], x->cmd) == 0 ) {
1080 int squirrel[] = {-1, -1, -1};
1081 int rcode;
1082 debug_printf("builtin inline %s\n", child->argv[0]);
1083 /* XXX setup_redirects acts on file descriptors, not FILEs.
1084 * This is perfect for work that comes after exec().
1085 * Is it really safe for inline use? Experimentally,
1086 * things seem to work with glibc. */
1087 setup_redirects(child, squirrel);
1088 rcode = x->function(child);
1089 restore_redirects(squirrel);
1090 return rcode;
1091 }
1092 }
1093 }
1094
1095 for (i = 0; i < pi->num_progs; i++) {
1096 child = & (pi->progs[i]);
1097
1098 /* pipes are inserted between pairs of commands */
1099 if ((i + 1) < pi->num_progs) {
1100 if (pipe(pipefds)<0) perror_msg_and_die("pipe");
1101 nextout = pipefds[1];
1102 } else {
1103 nextout=1;
1104 pipefds[0] = -1;
1105 }
1106
1107 /* XXX test for failed fork()? */
1108 if (!(child->pid = fork())) {
1109 close_all();
1110
1111 if (nextin != 0) {
1112 dup2(nextin, 0);
1113 close(nextin);
1114 }
1115 if (nextout != 1) {
1116 dup2(nextout, 1);
1117 close(nextout);
1118 }
1119 if (pipefds[0]!=-1) {
1120 close(pipefds[0]); /* opposite end of our output pipe */
1121 }
1122
1123 /* Like bash, explicit redirects override pipes,
1124 * and the pipe fd is available for dup'ing. */
1125 setup_redirects(child,NULL);
1126
1127 pseudo_exec(child);
1128 }
1129 if (interactive) {
1130 /* Put our child in the process group whose leader is the
1131 * first process in this pipe. */
1132 if (pi->pgrp==0) {
1133 pi->pgrp = child->pid;
1134 }
1135 /* Don't check for errors. The child may be dead already,
1136 * in which case setpgid returns error code EACCES. */
1137 setpgid(child->pid, pi->pgrp);
1138 }
1139 /* In the non-interactive case, do nothing. Leave the children
1140 * with the process group that they inherited from us. */
1141
1142 if (nextin != 0)
1143 close(nextin);
1144 if (nextout != 1)
1145 close(nextout);
1146
1147 /* If there isn't another process, nextin is garbage
1148 but it doesn't matter */
1149 nextin = pipefds[0];
1150 }
1151 return -1;
1152}
1153
1154static int run_list_real(struct pipe *pi)
1155{
1156 int rcode=0;
1157 int if_code=0, next_if_code=0; /* need double-buffer to handle elif */
Eric Andersen4ed5e372001-05-01 01:49:50 +00001158 reserved_style rmode, skip_more_in_this_rmode=RES_XXXX;
Eric Andersen25f27032001-04-26 23:22:31 +00001159 for (;pi;pi=pi->next) {
1160 rmode = pi->r_mode;
Eric Andersen4ed5e372001-05-01 01:49:50 +00001161 debug_printf("rmode=%d if_code=%d next_if_code=%d skip_more=%d\n", rmode, if_code, next_if_code, skip_more_in_this_rmode);
1162 if (rmode == skip_more_in_this_rmode) continue;
1163 skip_more_in_this_rmode = RES_XXXX;
Eric Andersen25f27032001-04-26 23:22:31 +00001164 if (rmode == RES_THEN || rmode == RES_ELSE) if_code = next_if_code;
1165 if (rmode == RES_THEN && if_code) continue;
1166 if (rmode == RES_ELSE && !if_code) continue;
1167 if (rmode == RES_ELIF && !if_code) continue;
Eric Andersen4ed5e372001-05-01 01:49:50 +00001168 if (pi->num_progs == 0) continue;
Eric Andersen25f27032001-04-26 23:22:31 +00001169 rcode = run_pipe_real(pi);
1170 if (rcode!=-1) {
1171 /* We only ran a builtin: rcode was set by the return value
1172 * of run_pipe_real(), and we don't need to wait for anything. */
1173 } else if (pi->followup==PIPE_BG) {
1174 /* XXX check bash's behavior with nontrivial pipes */
1175 /* XXX compute jobid */
1176 /* XXX what does bash do with attempts to background builtins? */
1177 printf("[%d] %d\n", pi->jobid, pi->pgrp);
1178 last_bg_pid = pi->pgrp;
1179 rcode = EXIT_SUCCESS;
1180 } else {
1181 if (interactive) {
1182 /* move the new process group into the foreground */
1183 /* suppress messages when run from /linuxrc mag@sysgo.de */
1184 signal(SIGTTIN, SIG_IGN);
1185 signal(SIGTTOU, SIG_IGN);
1186 if (tcsetpgrp(0, pi->pgrp) && errno != ENOTTY)
1187 perror_msg("tcsetpgrp");
1188 rcode = pipe_wait(pi);
Matt Kraai1c8a59a2001-05-02 15:37:09 +00001189 if (tcsetpgrp(0, getpgrp()) && errno != ENOTTY)
Eric Andersen25f27032001-04-26 23:22:31 +00001190 perror_msg("tcsetpgrp");
1191 signal(SIGTTIN, SIG_DFL);
1192 signal(SIGTTOU, SIG_DFL);
1193 } else {
1194 rcode = pipe_wait(pi);
1195 }
1196 }
1197 last_return_code=rcode;
1198 if ( rmode == RES_IF || rmode == RES_ELIF )
1199 next_if_code=rcode; /* can be overwritten a number of times */
1200 if ( (rcode==EXIT_SUCCESS && pi->followup==PIPE_OR) ||
1201 (rcode!=EXIT_SUCCESS && pi->followup==PIPE_AND) )
Eric Andersen4ed5e372001-05-01 01:49:50 +00001202 skip_more_in_this_rmode=rmode;
1203 /* return rcode; */ /* XXX broken if list is part of if/then/else */
Eric Andersen25f27032001-04-26 23:22:31 +00001204 }
1205 return rcode;
1206}
1207
1208/* broken, of course, but OK for testing */
1209static char *indenter(int i)
1210{
1211 static char blanks[]=" ";
1212 return &blanks[sizeof(blanks)-i-1];
1213}
1214
1215/* return code is the exit status of the pipe */
1216static int run_pipe_test(struct pipe *pi, int indent)
1217{
1218 char **p;
1219 struct child_prog *child;
1220 struct redir_struct *r, *rnext;
1221 int a, i, ret_code=0;
1222 char *ind = indenter(indent);
1223 final_printf("%s run pipe: (pid %d)\n",ind,getpid());
1224 for (i=0; i<pi->num_progs; i++) {
1225 child = &pi->progs[i];
1226 final_printf("%s command %d:\n",ind,i);
1227 if (child->argv) {
1228 for (a=0,p=child->argv; *p; a++,p++) {
1229 final_printf("%s argv[%d] = %s\n",ind,a,*p);
1230 }
1231 globfree(&child->glob_result);
1232 child->argv=NULL;
1233 } else if (child->group) {
1234 final_printf("%s begin group (subshell:%d)\n",ind, child->subshell);
1235 ret_code = run_list_test(child->group,indent+3);
1236 final_printf("%s end group\n",ind);
1237 } else {
1238 final_printf("%s (nil)\n",ind);
1239 }
1240 for (r=child->redirects; r; r=rnext) {
1241 final_printf("%s redirect %d%s", ind, r->fd, redir_table[r->type].descrip);
1242 if (r->dup == -1) {
1243 final_printf(" %s\n", *r->word.gl_pathv);
1244 globfree(&r->word);
1245 } else {
1246 final_printf("&%d\n", r->dup);
1247 }
1248 rnext=r->next;
1249 free(r);
1250 }
1251 child->redirects=NULL;
1252 }
1253 free(pi->progs); /* children are an array, they get freed all at once */
1254 pi->progs=NULL;
1255 return ret_code;
1256}
1257
1258static int run_list_test(struct pipe *head, int indent)
1259{
1260 int rcode=0; /* if list has no members */
1261 struct pipe *pi, *next;
1262 char *ind = indenter(indent);
1263 for (pi=head; pi; pi=next) {
1264 if (pi->num_progs == 0) break;
1265 final_printf("%s pipe reserved mode %d\n", ind, pi->r_mode);
1266 rcode = run_pipe_test(pi, indent);
1267 final_printf("%s pipe followup code %d\n", ind, pi->followup);
1268 next=pi->next;
1269 pi->next=NULL;
1270 free(pi);
1271 }
1272 return rcode;
1273}
1274
1275/* Select which version we will use */
1276static int run_list(struct pipe *pi)
1277{
1278 int rcode=0;
1279 if (fake_mode==0) {
1280 rcode = run_list_real(pi);
1281 }
1282 /* run_list_test has the side effect of clearing memory
1283 * In the long run that function can be merged with run_list_real,
1284 * but doing that now would hobble the debugging effort. */
1285 run_list_test(pi,0);
1286 return rcode;
1287}
1288
1289/* The API for glob is arguably broken. This routine pushes a non-matching
1290 * string into the output structure, removing non-backslashed backslashes.
1291 * If someone can prove me wrong, by performing this function within the
1292 * original glob(3) api, feel free to rewrite this routine into oblivion.
1293 * Return code (0 vs. GLOB_NOSPACE) matches glob(3).
1294 * XXX broken if the last character is '\\', check that before calling.
1295 */
1296static int globhack(const char *src, int flags, glob_t *pglob)
1297{
1298 int cnt, pathc;
1299 const char *s;
1300 char *dest;
Matt Kraaif162e7d2001-05-02 14:48:48 +00001301 for (cnt=1, s=src; s && *s; s++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001302 if (*s == '\\') s++;
1303 cnt++;
1304 }
1305 dest = malloc(cnt);
1306 if (!dest) return GLOB_NOSPACE;
1307 if (!(flags & GLOB_APPEND)) {
1308 pglob->gl_pathv=NULL;
1309 pglob->gl_pathc=0;
1310 pglob->gl_offs=0;
1311 pglob->gl_offs=0;
1312 }
1313 pathc = ++pglob->gl_pathc;
1314 pglob->gl_pathv = realloc(pglob->gl_pathv, (pathc+1)*sizeof(*pglob->gl_pathv));
1315 if (pglob->gl_pathv == NULL) return GLOB_NOSPACE;
1316 pglob->gl_pathv[pathc-1]=dest;
1317 pglob->gl_pathv[pathc]=NULL;
Matt Kraaif162e7d2001-05-02 14:48:48 +00001318 for (s=src; s && *s; s++, dest++) {
Eric Andersen25f27032001-04-26 23:22:31 +00001319 if (*s == '\\') s++;
1320 *dest = *s;
1321 }
1322 *dest='\0';
1323 return 0;
1324}
1325
1326/* XXX broken if the last character is '\\', check that before calling */
1327static int glob_needed(const char *s)
1328{
1329 for (; *s; s++) {
1330 if (*s == '\\') s++;
1331 if (strchr("*[?",*s)) return 1;
1332 }
1333 return 0;
1334}
1335
1336#if 0
1337static void globprint(glob_t *pglob)
1338{
1339 int i;
1340 debug_printf("glob_t at %p:\n", pglob);
1341 debug_printf(" gl_pathc=%d gl_pathv=%p gl_offs=%d gl_flags=%d\n",
1342 pglob->gl_pathc, pglob->gl_pathv, pglob->gl_offs, pglob->gl_flags);
1343 for (i=0; i<pglob->gl_pathc; i++)
1344 debug_printf("pglob->gl_pathv[%d] = %p = %s\n", i,
1345 pglob->gl_pathv[i], pglob->gl_pathv[i]);
1346}
1347#endif
1348
1349static int xglob(o_string *dest, int flags, glob_t *pglob)
1350{
1351 int gr;
1352
1353 /* short-circuit for null word */
1354 /* we can code this better when the debug_printf's are gone */
1355 if (dest->length == 0) {
1356 if (dest->nonnull) {
1357 /* bash man page calls this an "explicit" null */
1358 gr = globhack(dest->data, flags, pglob);
1359 debug_printf("globhack returned %d\n",gr);
1360 } else {
1361 return 0;
1362 }
1363 } else if (glob_needed(dest->data)) {
1364 gr = glob(dest->data, flags, NULL, pglob);
1365 debug_printf("glob returned %d\n",gr);
1366 if (gr == GLOB_NOMATCH) {
1367 /* quote removal, or more accurately, backslash removal */
1368 gr = globhack(dest->data, flags, pglob);
1369 debug_printf("globhack returned %d\n",gr);
1370 }
1371 } else {
1372 gr = globhack(dest->data, flags, pglob);
1373 debug_printf("globhack returned %d\n",gr);
1374 }
1375 if (gr == GLOB_NOSPACE) {
1376 fprintf(stderr,"out of memory during glob\n");
1377 exit(1);
1378 }
1379 if (gr != 0) { /* GLOB_ABORTED ? */
1380 fprintf(stderr,"glob(3) error %d\n",gr);
1381 }
1382 /* globprint(glob_target); */
1383 return gr;
1384}
1385
1386/* the src parameter allows us to peek forward to a possible &n syntax
1387 * for file descriptor duplication, e.g., "2>&1".
1388 * Return code is 0 normally, 1 if a syntax error is detected in src.
1389 * Resource errors (in xmalloc) cause the process to exit */
1390static int setup_redirect(struct p_context *ctx, int fd, redir_type style,
1391 struct in_str *input)
1392{
1393 struct child_prog *child=ctx->child;
1394 struct redir_struct *redir = child->redirects;
1395 struct redir_struct *last_redir=NULL;
1396
1397 /* Create a new redir_struct and drop it onto the end of the linked list */
1398 while(redir) {
1399 last_redir=redir;
1400 redir=redir->next;
1401 }
1402 redir = xmalloc(sizeof(struct redir_struct));
1403 redir->next=NULL;
1404 if (last_redir) {
1405 last_redir->next=redir;
1406 } else {
1407 child->redirects=redir;
1408 }
1409
1410 redir->type=style;
1411 redir->fd= (fd==-1) ? redir_table[style].default_fd : fd ;
1412
1413 debug_printf("Redirect type %d%s\n", redir->fd, redir_table[style].descrip);
1414
1415 /* Check for a '2>&1' type redirect */
1416 redir->dup = redirect_dup_num(input);
1417 if (redir->dup == -2) return 1; /* syntax error */
1418 if (redir->dup != -1) {
1419 /* Erik had a check here that the file descriptor in question
1420 * is legit; I postpone that to "run time" */
1421 debug_printf("Duplicating redirect '%d>&%d'\n", redir->fd, redir->dup);
1422 } else {
1423 /* We do _not_ try to open the file that src points to,
1424 * since we need to return and let src be expanded first.
1425 * Set ctx->pending_redirect, so we know what to do at the
1426 * end of the next parsed word.
1427 */
1428 ctx->pending_redirect = redir;
1429 }
1430 return 0;
1431}
1432
1433struct pipe *new_pipe(void) {
1434 struct pipe *pi;
1435 pi = xmalloc(sizeof(struct pipe));
1436 pi->num_progs = 0;
1437 pi->progs = NULL;
1438 pi->next = NULL;
1439 pi->followup = 0; /* invalid */
1440 return pi;
1441}
1442
1443static void initialize_context(struct p_context *ctx)
1444{
1445 ctx->pipe=NULL;
1446 ctx->pending_redirect=NULL;
1447 ctx->child=NULL;
1448 ctx->list_head=new_pipe();
1449 ctx->pipe=ctx->list_head;
1450 ctx->w=RES_NONE;
1451 ctx->stack=NULL;
1452 done_command(ctx); /* creates the memory for working child */
1453}
1454
1455/* normal return is 0
1456 * if a reserved word is found, and processed, return 1
1457 * should handle if, then, elif, else, fi, for, while, until, do, done.
1458 * case, function, and select are obnoxious, save those for later.
1459 */
1460int reserved_word(o_string *dest, struct p_context *ctx)
1461{
1462 struct reserved_combo {
1463 char *literal;
1464 int code;
1465 long flag;
1466 };
1467 /* Mostly a list of accepted follow-up reserved words.
1468 * FLAG_END means we are done with the sequence, and are ready
1469 * to turn the compound list into a command.
1470 * FLAG_START means the word must start a new compound list.
1471 */
1472 static struct reserved_combo reserved_list[] = {
1473 { "if", RES_IF, FLAG_THEN | FLAG_START },
1474 { "then", RES_THEN, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
1475 { "elif", RES_ELIF, FLAG_THEN },
1476 { "else", RES_ELSE, FLAG_FI },
1477 { "fi", RES_FI, FLAG_END },
1478 { "for", RES_FOR, FLAG_DO | FLAG_START },
1479 { "while", RES_WHILE, FLAG_DO | FLAG_START },
1480 { "until", RES_UNTIL, FLAG_DO | FLAG_START },
1481 { "do", RES_DO, FLAG_DONE },
1482 { "done", RES_DONE, FLAG_END }
1483 };
1484 struct reserved_combo *r;
Matt Kraaif162e7d2001-05-02 14:48:48 +00001485 if (dest->data == NULL)
1486 return 0;
Eric Andersen25f27032001-04-26 23:22:31 +00001487 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))) {
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001499 syntax();
1500 ctx->w = RES_SNTX;
1501 b_reset (dest);
1502 return 1;
Eric Andersen25f27032001-04-26 23:22:31 +00001503 }
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. */
1524static 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);
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001544 if (reserved_word(dest,ctx)) return ctx->w==RES_SNTX;
Eric Andersen25f27032001-04-26 23:22:31 +00001545 }
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. */
1567static 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
1603static 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 */
1622static 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 */
1651static 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
1667FILE *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. */
1704static 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
1746static 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" */
1772static 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 */
1781static 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
1858int 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 */
1866int 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);
Eric Andersenaac75e52001-04-30 18:18:45 +00001886 } else {
1887 if (m==2) { /* unquoted IFS */
1888 done_word(dest, ctx);
1889 if (ch=='\n') done_pipe(ctx,PIPE_SEQ);
1890 }
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001891 if (ch == end_trigger && !dest->quote && ctx->w==RES_NONE) {
Eric Andersenaac75e52001-04-30 18:18:45 +00001892 debug_printf("leaving parse_stream\n");
Eric Andersenaf44a0e2001-04-27 07:26:12 +00001893 return 0;
1894 }
Eric Andersen25f27032001-04-26 23:22:31 +00001895#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
Eric Andersenaac75e52001-04-30 18:18:45 +00001903 if (m!=2) switch (ch) {
Eric Andersen25f27032001-04-26 23:22:31 +00001904 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;
Eric Andersenaac75e52001-04-30 18:18:45 +00002005 }
Eric Andersen25f27032001-04-26 23:22:31 +00002006 }
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
2020void mapset(const unsigned char *set, int code)
2021{
2022 const unsigned char *s;
2023 for (s=set; *s; s++) map[*s] = code;
2024}
2025
2026void 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() */
2045int 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
2063static 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
2070static 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
2079int shell_main(int argc, char **argv)
2080{
2081 int opt;
2082 FILE *input;
2083
Eric Andersene67c3ce2001-05-02 02:09:36 +00002084 last_return_code=EXIT_SUCCESS;
2085
Eric Andersen25f27032001-04-26 23:22:31 +00002086 /* XXX what should these be while sourcing /etc/profile? */
2087 global_argc = argc;
2088 global_argv = argv;
2089
2090 if (argv[0] && argv[0][0] == '-') {
2091 debug_printf("\nsourcing /etc/profile\n");
2092 input = xfopen("/etc/profile", "r");
2093 mark_open(fileno(input));
2094 parse_file_outer(input);
2095 mark_closed(fileno(input));
2096 fclose(input);
2097 }
2098 input=stdin;
2099
2100 /* initialize the cwd -- this is never freed...*/
2101 cwd = xgetcwd(0);
2102#ifdef BB_FEATURE_COMMAND_EDITING
2103 cmdedit_set_initial_prompt();
2104#else
2105 PS1 = NULL;
2106#endif
2107
2108 while ((opt = getopt(argc, argv, "c:xif")) > 0) {
2109 switch (opt) {
2110 case 'c':
2111 {
2112 global_argv = argv+optind;
2113 global_argc = argc-optind;
2114 opt = parse_string_outer(optarg);
Eric Andersene67c3ce2001-05-02 02:09:36 +00002115 goto final_return;
Eric Andersen25f27032001-04-26 23:22:31 +00002116 }
2117 break;
2118 case 'i':
2119 interactive++;
2120 break;
2121 case 'f':
2122 fake_mode++;
2123 break;
2124 default:
2125 fprintf(stderr, "Usage: sh [FILE]...\n"
2126 " or: sh -c command [args]...\n\n");
2127 exit(EXIT_FAILURE);
2128 }
2129 }
2130 /* A shell is interactive if the `-i' flag was given, or if all of
2131 * the following conditions are met:
2132 * no -c command
2133 * no arguments remaining or the -s flag given
2134 * standard input is a terminal
2135 * standard output is a terminal
2136 * Refer to Posix.2, the description of the `sh' utility. */
2137 if (argv[optind]==NULL && input==stdin &&
2138 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
2139 interactive++;
2140 }
Eric Andersene67c3ce2001-05-02 02:09:36 +00002141
2142 debug_printf("\ninteractive=%d\n", interactive);
Eric Andersen25f27032001-04-26 23:22:31 +00002143 if (interactive) {
2144 /* Looks like they want an interactive shell */
2145 fprintf(stdout, "\nhush -- the humble shell v0.01 (testing)\n\n");
Eric Andersene67c3ce2001-05-02 02:09:36 +00002146 opt=parse_file_outer(stdin);
2147 goto final_return;
Eric Andersen25f27032001-04-26 23:22:31 +00002148 }
Eric Andersen25f27032001-04-26 23:22:31 +00002149
2150 debug_printf("\nrunning script '%s'\n", argv[optind]);
2151 global_argv = argv+optind;
2152 global_argc = argc-optind;
2153 input = xfopen(argv[optind], "r");
2154 opt = parse_file_outer(input);
2155
2156#ifdef BB_FEATURE_CLEAN_UP
2157 fclose(input.file);
2158#endif
2159
Eric Andersene67c3ce2001-05-02 02:09:36 +00002160final_return:
2161 return(opt?opt:last_return_code);
Eric Andersen25f27032001-04-26 23:22:31 +00002162}