42sh/src/parser/grammar_advanced.c

47 lines
1,017 B
C

#include "grammar_advanced.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "grammar_basic.h"
bool
struct ast *parse_redirection(struct lexer_context *ctx)
{
struct token *token = PEEK_TOKEN();
int io_number = -1;
if (token->type == TOKEN_IONUMBER)
{
io_number = atoi(token->data);
POP_TOKEN();
token = PEEK_TOKEN();
}
if (!is_token_redir(token))
{
perror("Syntax error: expected a redirection token but got something "
"else");
return NULL;
}
// char *redir_op = strdup(token->data);
POP_TOKEN();
token = PEEK_TOKEN();
if (token->type != TOKEN_WORD)
{
perror("Syntax error: expected a word after redirection");
// free(redir_op);
return NULL;
}
char *target = strdup(token->data);
POP_TOKEN();
return ast_create_redir(io_number, redir_op, target);
}
struct ast *parse_prefix(struct lexer_context *ctx)
{
return parse_redirection(ctx);
}