feat(parser): redirections

This commit is contained in:
Matteo Flebus 2026-01-27 19:56:33 +01:00
parent 04ff7376eb
commit 8a5c589742
17 changed files with 78 additions and 108 deletions

View file

@ -46,7 +46,7 @@ void ast_free(struct ast **node)
default:
perror("WARNING: Internal error: failed to free an AST node (Unknown "
"type)");
"type)");
return;
}

View file

@ -7,11 +7,11 @@
#include "ast_end.h"
#include "ast_if.h"
#include "ast_list.h"
#include "ast_neg.h"
#include "ast_pipe.h"
#include "ast_redir.h"
#include "ast_void.h"
#include "ast_word.h"
#include "ast_pipe.h"
#include "ast_neg.h"
/**
* Prints the Graphviz DOT representation of the given AST to stdout.

View file

@ -1,8 +1,8 @@
#ifndef AST_BASE_H
#define AST_BASE_H
#include <stdlib.h>
#include <stdbool.h>
#include <stdlib.h>
enum ast_type
{

View file

@ -6,12 +6,12 @@
struct ast_neg
{
bool negation; // True negates the child's output
struct ast* child;
struct ast *child;
};
bool ast_is_neg(struct ast *node);
struct ast_neg *ast_get_neg(struct ast *node);
struct ast *ast_create_neg(bool negation, struct ast* child);
void ast_free_neg(struct ast_neg* node);
struct ast *ast_create_neg(bool negation, struct ast *child);
void ast_free_neg(struct ast_neg *node);
#endif /* ! AST_NEG_H */

View file

@ -5,14 +5,14 @@
struct ast_pipe
{
struct ast* left;
struct ast* right;
struct ast *left;
struct ast *right;
// Output of left will be redirected to right stdin
};
bool ast_is_pipe(struct ast *node);
struct ast_pipe *ast_get_pipe(struct ast *node);
struct ast *ast_create_pipe(struct ast* left, struct ast* right);
void ast_free_pipe(struct ast_pipe* node);
struct ast *ast_create_pipe(struct ast *left, struct ast *right);
void ast_free_pipe(struct ast_pipe *node);
#endif /* ! AST_PIPE_H */

View file

@ -14,13 +14,12 @@ struct ast_redir *ast_get_redir(struct ast *node)
return NULL;
}
struct ast *ast_create_redir(struct ast *child, char *filename, int io_number,
struct ast *ast_create_redir(char *filename, int io_number,
enum ast_redir_type type)
{
struct ast_redir *redir = malloc(sizeof(struct ast_redir));
if (!redir)
return NULL;
redir->child = child;
redir->filename =
filename; // Takes ownership? Usually yes in simple ASTs, or dup. Let's
// assume pointer copy for now, but user must manage memory.
@ -34,7 +33,6 @@ void ast_free_redir(struct ast_redir *redir)
{
if (!redir)
return;
ast_free(&redir->child);
free(redir->filename);
free(redir);
}

View file

@ -17,7 +17,6 @@ enum ast_redir_type
struct ast_redir
{
struct ast *child;
char *filename;
int io_number; // The FD being redirected (default -1 if not specified,
// implies 0 or 1 based on type)
@ -26,7 +25,7 @@ struct ast_redir
bool ast_is_redir(struct ast *node);
struct ast_redir *ast_get_redir(struct ast *node);
struct ast *ast_create_redir(struct ast *child, char *filename, int io_number,
struct ast *ast_create_redir(char *filename, int io_number,
enum ast_redir_type type);
void ast_free_redir(struct ast_redir *redir);

View file

@ -1,8 +1,8 @@
#define _POSIX_C_SOURCE 200809L
#include "ast_word.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -17,6 +17,7 @@ struct ast *ast_create_word(char *word)
struct ast *res = ast_create(AST_WORD, ast_node);
if (res == NULL)
{
free(ast_node->word);
free(ast_node);
return NULL;
}

View file

@ -23,7 +23,7 @@ struct ast_word *ast_get_word(struct ast *node);
/**
* Creates a new AST node representing a command.
*/
struct ast *ast_create_word(char* word);
struct ast *ast_create_word(char *word);
/*
* @brief: frees the given ast_command and sets the pointer to NULL.