/* * test parser main */ #include #if MSDOS|OS2 #include #endif #include "kw.h" #include "tsh.h" #include "execute.h" #include "system.h" char ignoreeof = 0; /* * run() executes a command */ /* lower 8 bits are user status; upper 8 bits are system status */ int status; run(ptr) register CMD *ptr; { struct iterator *iter; while (ptr && status >= 0) { if (ptr->c_text) { switch (ptr->c_type) { case IF: case ELIF: pipeline(ptr->c_expr); if (status >= 0) run(status ? ptr->c_false : ptr->c_true); break; case THEN: case DO: run(ptr->c_next); return status; case ELSE: run(ptr->c_next); return status; case WHILE: while (pipeline(ptr->c_expr) == 0 && run(ptr->c_body) >= 0) ; break; case FOR: if (iter = foreach(ptr)) { while (fornext(iter)) run(ptr->c_body); forgone(iter); } break; default: pipeline(ptr); break; } } ptr = ptr->c_next; } return status; } /* display */ /* * run a command and attach it to the next command in the pipeline */ pipeline(ptr) register CMD *ptr; { struct pipedesc pipe; register mode = RUN_SYNC; register run_pipe; int save; if (run_pipe=(ptr->c_listop == PIPE)) { if (makepipe(&pipe) != 0) /* fail if we can't make */ return status; /* a pipeline */ if (multitask()) /* if we're using real */ mode = RUN_PIPE; /* pipes, this process runs */ /* in the background */ save = dup(1); /* point ourselves at the pipe */ dup2(pipe.p_wrd,1); } else if (ptr->c_listop == BG) /* else only run in bg if & */ mode = RUN_ASYNC; runcommand(ptr, mode); if (run_pipe) { close(1); dup2(save, 1); close(save); } if (status >= 0) { switch (ptr->c_listop) { case OR: /* execute rest of pipeline only if this command failed */ if (status == 0) goto done; break; case AND: /* execute rest of pipeline only if this command worked */ if (status != 0) goto done; break; /* ... case BG is implied - all background processes by default * succeed */ /* ... case SEMI is run unless there's a system error. */ } if (run_pipe) { if (kickpipe(&pipe) != 0) goto done; save = dup(0); dup2(pipe.p_rdd, 0); } run(ptr->c_list); if (run_pipe) { dup2(save, 0); close(save); } } done: if (run_pipe) freepipe(&pipe); return status; } /* pipeline */ /* * restore the universe to sanity before we exit */ shellexit(st) { #if UNIX system("stty echo -raw"); #endif exit(st); } /* * main(): parse test main */ main(argc, argv, envp) char **argv, **envp; { register CMD *ptr; extern int mmcount; register first; #if UNIX system("stty raw -echo opost onlcr"); #else sysinit(); #endif getenviron(envp); reset("tsh>"); while (1) { first = mmcount; if (ptr = parse()) { if (ptr->c_type == EXIT) break; status = 0; run(ptr); garbage(ptr); } if (first != mmcount) fprintf(stderr, "first is %d, current is %d\n", first, mmcount); if (eof()) { puts("EOF"); break; } if (eos()) { jobstatus(); reset("tsh>"); } } shellexit(0); } /* main */