#include #include "parser_functions.h" #include "parser_utils.h" struct ast *parse_redirection(struct lexer *lexer, enum parser_state *state) { // The caller (prefix grammar in this case) will have to attach its // own ast as the left child of the redir ast struct ast *redir = ast_create(AST_REDIRECTION); if (redir == NULL) { QUICK_CLEANUP } struct token next = lexer_peek(lexer); union ast_caster cast; cast.ast = redir; if (next.type == TOKEN_IONUMBER) { cast.ast_r->redirect.fd = next.value; lexer_pop(lexer); next = lexer_peek(lexer); } else { cast.ast_r->redirect.fd = string_create("1"); } if (next.type != TOKEN_REDIR) { cleanup(redir, state); return NULL; } cast.ast_r->redirect.redir = next.value; if (next.value->data[0] == '<' && cast.ast_r->redirect.fd->data[0] == '1') { cast.ast_r->redirect.fd->data[0] = '0'; } lexer_pop(lexer); next = lexer_peek(lexer); if (!(ISWORD(next.type))) { cleanup(redir, state); errx(2, "redir: bad grammar (bad redir)."); } litteral_reserved_word(&next); cast.ast_r->redirect.file = next.value; lexer_pop(lexer); ((struct ast_redirection *)redir)->redirect = cast.ast_r->redirect; return redir; }