Author | Jamozed <[email protected]> |
Date | 2021-11-25 12:02:17 |
Commit | b17c84602b6573995399964a6deb7cdf9bb58642 |
Parent | 3cb5034f56e2538fe7e9ff0b92bd865221c4d83f |
Prune misguided redirect code
Diffstat
M | src/esh.h | | | 2 | +- |
M | src/eval.c | | | 2 | +- |
M | src/exec.c | | | 36 | ++++++------------------------------ |
M | src/parse.c | | | 39 | ++------------------------------------- |
M | src/parse.h | | | 7 | ++++--- |
5 files changed, 14 insertions, 72 deletions
diff --git a/src/esh.h b/src/esh.h index 34d2578..c6d9db9 100644 --- a/src/esh.h +++ b/src/esh.h @@ -39,7 +39,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. extern bool Lflag, Pflag; extern int _loop, _ret; -extern int execute(ast a, int fdi, int fd[2]); +extern int execute(ast a); extern void eval(char *src, UINT len); extern void eval_file(const char *file); diff --git a/src/eval.c b/src/eval.c index f72bb12..61496ee 100644 --- a/src/eval.c +++ b/src/eval.c @@ -49,7 +49,7 @@ extern void eval(char *src, UINT len) { a = parse(&l); if (!a) { return; } if (Pflag) { ast_debug(a); continue; } - _ret = execute(a, fileno(stdin), NULL); + _ret = execute(a); } } diff --git a/src/exec.c b/src/exec.c index 93a76ae..b0209a9 100644 --- a/src/exec.c +++ b/src/exec.c @@ -52,19 +52,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. #include <string.h> static int execute_comm(ast a, int fdi, int fd[2]); -static int execute_pipe(ast a, int fdi, int fd[2]); -static int execute_rdin(ast a, int fdi); -static int execute_rout(ast a, int fdi); -static int execute_rerr(ast a, int fdi); +static int execute_pipe(ast a, int fdi); /* Execute a program. */ -int execute(ast a, int fdi, int fd[2]) { +int execute(ast a) { switch (a->k) { - case AK_COMM: {} return execute_comm(a, fdi, fd); - case AK_PIPE: {} return execute_pipe(a, fdi, fd); - // case AK_RDIN: {} return execute_rdin(a, fdi); - case AK_ROUT: {} return execute_rout(a, fdi); - // case AK_RERR: {} return execute_rerr(a, fdi); + case AK_COMM: {} return execute_comm(a, fileno(stdin), NULL); + case AK_PIPE: {} return execute_pipe(a, fileno(stdin)); default: { warn("%s: Unimplemented AST", ast_ks[a->k]); } return -1; } } @@ -105,25 +99,7 @@ static int execute_comm(ast a, int fdi, int fd[2]) { } /* Execute a pipe statement. */ -static int execute_pipe(ast a, int fdi, int fd[2]) { +static int execute_pipe(ast a, int fdi) { int pd[2]; if (pipe(pd) == -1) { warn("PIPE: %s", serr()); return -1; } - execute(a->lc, fdi, pd); return execute(a->rc, pd[0], fd); -} - -/* Execute a redirect stdin statement. */ -static int execute_rdin(ast a, int fdi) { - -} - -/* Execute a redirect stdout statement. */ -static int execute_rout(ast a, int fdi) { - int fd[2] = { 0 }; fd[1] = open(a->rc->s, O_WRONLY | O_CREAT | O_TRUNC); - if (fd[1] == -1) { warn("%s: %s", a->rc->s, serr()); return -1; } - - return execute(a->lc, fdi, fd); -} - -/* Execute a redirect stderr statement. */ -static int execute_rerr(ast a, int fdi) { - + execute_comm(a->lc, fdi, pd); return execute_comm(a->rc, pd[0], NULL); } diff --git a/src/parse.c b/src/parse.c index 753b722..fc9c33a 100644 --- a/src/parse.c +++ b/src/parse.c @@ -44,26 +44,18 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. #include <stdlib.h> char *ast_ks[] = { - "NULL", "WORD", "COMM", - "PIPE", "RDIN", "ROUT", "RERR" + "NULL", "COMM", "PIPE", }; -static ast parse_word(lex *l); static ast parse_comm(lex *l); static ast parse_pipe(lex *l, ast lc); -static ast parse_rdin(lex *l, ast lc); -static ast parse_rout(lex *l, ast lc); -static ast parse_rerr(lex *l, ast lc); /* Initialise an AST node. */ ast ast_init(void) { return assert_calloc(1, sizeof (struct ast_s)); } /* Uninitialise an AST node and its children. */ void ast_free(ast a) { - if (a) { - ast_free(a->lc); ast_free(a->rc); stack_free(&a->c); - if (a->k == AK_WORD) { free(a->s); } free(a); - } + if (a) { ast_free(a->lc); ast_free(a->rc); stack_free(&a->c); free(a); } } #define T (lex_peek(l)) /* Current Token */ @@ -82,19 +74,10 @@ ast parse(lex *l) { for (;;) switch (lex_next(l).k) { case TK_PIPE: { lhs = parse_pipe(l, lhs); } continue; - case TK_RDIN: { lhs = parse_rdin(l, lhs); } continue; - case TK_ROUT: { lhs = parse_rout(l, lhs); } continue; - case TK_RERR: { lhs = parse_rerr(l, lhs); } continue; default: {} return lhs; } } -/* Parse a single word. */ -static ast parse_word(lex *l) { - ast a = ast_init(); a->k = AK_WORD; - a->s = lex_next(l).s; return a; -} - /* Parse a command statement. */ static ast parse_comm(lex *l) { ast a = ast_init(); a->k = AK_COMM; @@ -113,24 +96,6 @@ static ast parse_pipe(lex *l, ast lc) { a->lc = lc; a->rc = parse_comm(l); return a; } -/* Parse a redirect stdin statement. */ -static ast parse_rdin(lex *l, ast lc) { - ast a = ast_init(); a->k = AK_RDIN; - a->lc = lc; a->rc = parse_word(l); return a; -} - -/* Parse a redirect stdout statement. */ -static ast parse_rout(lex *l, ast lc) { - ast a = ast_init(); a->k = AK_ROUT; - a->lc = lc; a->rc = parse_word(l); return a; -} - -/* Parse a redirect stderr statement. */ -static ast parse_rerr(lex *l, ast lc) { - ast a = ast_init(); a->k = AK_RERR; - a->lc = lc; a->rc = parse_word(l); return a; -} - /* Print parser debug output with an indent. */ static void ast_debug_indent(ast a, UINT i) { for (UINT j = 0; j != i; ++j) { printf("\t"); } diff --git a/src/parse.h b/src/parse.h index 697a7e6..41dbcec 100644 --- a/src/parse.h +++ b/src/parse.h @@ -38,13 +38,14 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. #include "util/util.h" typedef enum { - AK_NULL, AK_WORD, AK_COMM, - AK_PIPE, AK_RDIN, AK_ROUT, AK_RERR, + AK_NULL, AK_COMM, AK_PIPE, } ast_k; typedef struct ast_s *ast; -struct ast_s { ast_k k; char *s; ast lc, rc; stack c; }; +struct ast_s { + ast_k k; char *s; ast lc, rc; stack c; +}; extern char *ast_ks[];