42sh/src/utils/ast/ast_redir.c

41 lines
996 B
C
Raw Normal View History

#include "utils/ast/ast_redir.h"
#include <stdlib.h>
bool ast_is_redir(struct ast *node)
{
return node != NULL && node->type == AST_REDIR;
}
struct ast_redir *ast_get_redir(struct ast *node)
{
if (ast_is_redir(node))
return (struct ast_redir *)node->data;
return NULL;
}
struct ast *ast_create_redir(struct ast *child, 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.
redir->io_number = io_number;
redir->type = type;
return ast_create(AST_REDIR, redir);
}
void ast_free_redir(struct ast_redir *redir)
{
if (!redir)
return;
ast_free(&redir->child);
free(redir->filename);
free(redir);
}