From 967be9e750221ab2ab783f95df79bb26d290a45e Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:07:58 +0200 Subject: add: added projects --- 42sh/src/parser/parser_redir.c | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 42sh/src/parser/parser_redir.c (limited to '42sh/src/parser/parser_redir.c') diff --git a/42sh/src/parser/parser_redir.c b/42sh/src/parser/parser_redir.c new file mode 100644 index 0000000..9a06d0b --- /dev/null +++ b/42sh/src/parser/parser_redir.c @@ -0,0 +1,59 @@ +#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; +} -- cgit v1.2.3