diff options
Diffstat (limited to 'toys/pending')
-rw-r--r-- | toys/pending/sh.c | 73 |
1 files changed, 6 insertions, 67 deletions
diff --git a/toys/pending/sh.c b/toys/pending/sh.c index cc3825c6..47086363 100644 --- a/toys/pending/sh.c +++ b/toys/pending/sh.c @@ -219,47 +219,6 @@ GLOBALS( // leading to function loop with run->parse->run static int sh_run(char *new); -#define BUGBUG 0 - -// call with NULL to just dump FDs -static void dump_state(struct sh_function *sp, int err) -{ - struct sh_pipeline *pl; - long i; - int q = 0, fd = open("/proc/self/fd", O_RDONLY); - DIR *dir = fdopendir(fd); - char buf[256]; - - if (sp && sp->expect) { - struct double_list *dl; - - for (dl = sp->expect; dl; dl = (dl->next == sp->expect) ? 0 : dl->next) - dprintf(err, "expecting %s\n", dl->data); - if (sp->pipeline) - dprintf(err, "pipeline count=%d here=%d\n", sp->pipeline->prev->count, - sp->pipeline->prev->here); - } - - if (sp) for (pl = sp->pipeline; pl ; pl = (pl->next == sp->pipeline) ? 0 : pl->next) { - dprintf(err, "<%d> type=%d argc=%d", q++, pl->type, pl->arg->c); - for (i = 0; i<=pl->arg->c; i++) - if (i == pl->arg->c) dprintf(err, " term=%s", pl->arg->v ? pl->arg->v[pl->arg->c] : ""); - else dprintf(err, " arg[%ld]=%s", i, pl->arg->v[i]); - dprintf(err, "\n"); - } - - if (dir) { - struct dirent *dd; - - while ((dd = readdir(dir))) { - if (atoi(dd->d_name)!=fd && 0<readlinkat(fd, dd->d_name, buf,sizeof(buf))) - dprintf(err, "OPEN %d: %s = %s\n", getpid(), dd->d_name, buf); - } - closedir(dir); - } - close(fd); -} - // ordered for greedy matching, so >&; becomes >& ; not > &; // making these const means I need to typecast the const away later to // avoid endless warnings. @@ -629,7 +588,7 @@ static int save_redirect(int **rd, int from, int to) if ((hfd = next_hfd())==-1) return 1; if (hfd != dup2(to, hfd)) hfd = -1; else fcntl(hfd, F_SETFD, FD_CLOEXEC); -if (BUGBUG) dprintf(255, "%d redir from=%d to=%d hfd=%d\n", getpid(), from, to, hfd); + // dup "to" if (from >= 0 && to != dup2(from, to)) { if (hfd >= 0) close(hfd); @@ -637,7 +596,6 @@ if (BUGBUG) dprintf(255, "%d redir from=%d to=%d hfd=%d\n", getpid(), from, to, return 1; } } else { -if (BUGBUG) dprintf(255, "%d schedule close %d\n", getpid(), to); hfd = to; to = -1; } @@ -670,7 +628,6 @@ static int run_subshell(char *str, int len) { pid_t pid; -if (BUGBUG) dprintf(255, "run_subshell %.*s\n", len, str); // The with-mmu path is significantly faster. if (CFG_TOYBOX_FORK) { char *s; @@ -726,13 +683,10 @@ static void unredirect(int *urd) if (!urd) return; - for (i = 0; i<*urd; i++, rr += 2) { -if (BUGBUG) dprintf(255, "%d urd %d %d\n", getpid(), rr[0], rr[1]); - if (rr[0] != -1) { - // No idea what to do about fd exhaustion here, so Steinbach's Guideline. - dup2(rr[0], rr[1]); - close(rr[0]); - } + for (i = 0; i<*urd; i++, rr += 2) if (rr[0] != -1) { + // No idea what to do about fd exhaustion here, so Steinbach's Guideline. + dup2(rr[0], rr[1]); + close(rr[0]); } free(urd); } @@ -1131,8 +1085,6 @@ static int expand_arg_nobrace(struct sh_arg *arg, char *str, unsigned flags, int ii = 0, oo = 0, xx, yy, dd, jj, kk, ll, mm; struct sh_arg deck = {0}; -if (BUGBUG) dprintf(255, "expand %s\n", str); - // Tilde expansion if (!(flags&NO_TILDE) && *str == '~') { struct passwd *pw = 0; @@ -1989,15 +1941,12 @@ static struct sh_process *run_command(struct sh_arg *arg) struct arg_list *delete = 0; struct toy_list *tl; -if (BUGBUG) dprintf(255, "run_command %s\n", arg->v[0]); - // Count leading variable assignments for (envlen = 0; envlen<arg->c; envlen++) { s = varend(arg->v[envlen]); if (s == arg->v[envlen] || *s != '=') break; } -if (BUGBUG) { int i; dprintf(255, "envlen=%d arg->c=%d run=", envlen, arg->c); for (i=0; i<arg->c; i++) dprintf(255, "'%s' ", arg->v[i]); dprintf(255, "\n"); } // perform assignments locally if there's no command if (envlen == arg->c) { while (jj<envlen) { @@ -2034,7 +1983,6 @@ if (BUGBUG) { int i; dprintf(255, "envlen=%d arg->c=%d run=", envlen, arg->c); f // expand arguments and perform redirects pp = expand_redir(arg, envlen, 0); -if (BUGBUG) { int i; dprintf(255, "cooked arg->c=%d run=", arg->c); for (i=0; i<pp->arg.c; i++) dprintf(255, "'%s' ", pp->arg.v[i]); dprintf(255, "\n"); } // Do nothing if nothing to do if (pp->exit || !pp->arg.v); @@ -2189,8 +2137,6 @@ static int parse_line(char *line, struct sh_function *sp) pl->count = 0; arg = pl->arg; -if (BUGBUG>1) dprintf(255, "{%d:%s}\n", pl->type, ex ? ex : (sp->expect ? "*" : "")); - // find arguments of the form [{n}]<<[-] with another one after it for (i = 0; i<arg->c; i++) { s = arg->v[i] + redir_prefix(arg->v[i]); @@ -2224,7 +2170,6 @@ if (BUGBUG>1) dprintf(255, "{%d:%s}\n", pl->type, ex ? ex : (sp->expect ? "*" : // Parse next word and detect overflow (too many nested quotes). if ((end = parse_word(start, 0, 0)) == (void *)1) goto flush; -if (BUGBUG>1) dprintf(255, "[%d:%.*s:%s] ", pl ? pl->type : 0, end ? (int)(end-start) : 0, start, ex ? : ""); // Is this a new pipeline segment? if (!pl) pl = add_pl(sp, &arg); @@ -2710,7 +2655,6 @@ TODO: a | b | c needs subshell for builtins? if (pipe_segments(ctl, pipes, &urd)) break; } -if (BUGBUG) dprintf(255, "%d runtype=%d %s %s\n", getpid(), pl->type, s, ctl); // Is this an executable segment? if (!pl->type) { @@ -3139,8 +3083,6 @@ void sh_main(void) // if (!FLAG(noprofile)) { } -if (BUGBUG) { int fd = open("/dev/tty", O_RDWR); if (fd == -1) fd = open("/dev/console", O_RDWR); if (fd == -1) dup2(2, 255); else dup2(fd, 255); close(fd); } - // Is this an interactive shell? if (FLAG(s) || (!FLAG(c) && !toys.optc)) TT.options |= OPT_S; if (FLAG(i) || (!FLAG(c) && (TT.options&OPT_S) && isatty(0))) @@ -3191,7 +3133,6 @@ if (BUGBUG) { int fd = open("/dev/tty", O_RDWR); if (fd == -1) fd = open("/dev/c // SIGSTOP and SIGTSTP need need SA_RESTART, but child proc should stop } -if (BUGBUG) dprintf(255, "line=%s\n", new); if (sl) { if (*new == 0x7f) error_exit("'%s' is ELF", sl->str); free(sl); @@ -3200,9 +3141,7 @@ if (BUGBUG) dprintf(255, "line=%s\n", new); // TODO if (!isspace(*new)) add_to_history(line); // returns 0 if line consumed, command if it needs more data - prompt = parse_line(new, &scratch); -if (BUGBUG) dprintf(255, "prompt=%d\n", prompt), dump_state(&scratch, 255); - if (prompt != 1) { + if (1 != (prompt = parse_line(new, &scratch))) { // TODO: ./blah.sh one two three: put one two three in scratch.arg if (!prompt) run_function(scratch.pipeline); free_function(&scratch); |