From 941099778b59da6b904c284e8a82affe4766124b Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 30 Mar 2020 22:27:16 +0200 Subject: Added doc --- src/eval/eval.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/eval/read.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ src/eval/val.c | 69 +++++++++++++++++++++++++++++++++++++ 3 files changed, 261 insertions(+) create mode 100644 src/eval/eval.c create mode 100644 src/eval/read.c create mode 100644 src/eval/val.c (limited to 'src/eval') diff --git a/src/eval/eval.c b/src/eval/eval.c new file mode 100644 index 0000000..8fca2c1 --- /dev/null +++ b/src/eval/eval.c @@ -0,0 +1,103 @@ +/** +** \file eval.c +** \brief Evaluation of an AST +*/ + +#include "minishell.h" + + +static bool check_node(t_ast *ast) +{ + +} + +static int eval_sep(t_ast *ast) +{ + if (ast->children_num != 2) + return (-1); + if (ast->tag == TAG_ENDCMD) + { + ms_eval(path, env, ast->children[0]); + return (ms_eval(path, env, ast->children[1])); + } + if (ast->tag == TAG_AND) + { + status = ms_eval(path, env, ast->children[0]); + if (status == 0) + return (ms_eval(path, env, ast->children[1])); + else + return (status); + } + if (ast->tag == TAG_OR) + { + status = ms_eval(path, env, ast->children[0]); + if (status != 0) + return (ms_eval(path, env, ast->children[1])); + else + return (status); + } + return (-1); +} + +static char **get_args(t_ast *ast) +{ + int i; + int counter; + char **argv; + + if (ast->tag != TAG_CMD) + return (NULL); + counter = 0; + i = -1; + while (++i < ast->children_num) + if (ast->children[i]->tag == TAG_ARG) + counter++; + if ((argv = (char**)ft_calloc(counter + 1, sizeof(char*))) == NULL) + return (NULL); + counter = 0; + i = -1; + while (++i < ast->children_num) + { + if (ast->children[i]->tag != TAG_ARG) + continue ; + if ((argv[counter] = ft_strdup(ast->children[i]->content)) == NULL) + { + ft_split_destroy(argv); + return (NULL); + } + } + return (argv); + + /* maybe + int i; + char *tmp; + t_vec *vec; + + if ((vec = ft_vecnew(sizeof(char*))) == NULL) + return (NULL); + i = -1; + while (++i < ast->children_num) + { + if (ast->children[i]->tag != TAG_ARG) + continue ; + if ((tmp = ft_strdup(ast->children[i]->content)) == NULL + || ft_vecpush(vec, tmp) == NULL)) + { + ft_vecdestroy(vec, free); + return (NULL); + } + } + return (vec); + */ +} + +//reduce? +int ms_eval(t_path path, t_env env, t_ast *ast) +{ + int status; + + if (ast->tag == TAG_ENDCMD || ast->tag == TAG_AND + || ast->tag == TAG_OR || ast->tag == TAG_PIPE) + return (eval_sep(ast)); + +} diff --git a/src/eval/read.c b/src/eval/read.c new file mode 100644 index 0000000..1364fca --- /dev/null +++ b/src/eval/read.c @@ -0,0 +1,89 @@ +/** +** \file read.c +** \brief Convert AST to value tree +*/ + +#include "ms_evalue.h" + +static t_value *read_cmd_args(t_value *value_cmd, t_ast *cmd) +{ + +} + +/* static void arg_count_iterator(int *counter, t_ast *child) */ +/* { */ +/* if (child == TAG_STRING) */ +/* (*counter)++; */ +/* } */ +/* */ +/* static void arg_add_iterator(t_value *value_cmd, t_ast *child) */ +/* { */ +/* if (child != TAG_STRING) */ +/* return ; */ +/* value_cmd_push( */ +/* } */ + +/** +** \brief Convert a command AST to a value +** \param cmd Command AST +** \return Converted value +*/ + +static t_value *read_cmd(t_ast *cmd) +{ + int i; + int arg_num; + t_value *value_cmd; + + if (ast->children_num < 1 || ast->children[0]->type != TAG_STRING) + return (value_new_string(VAL_ERR, "Empty command"); + arg_num = 0; + i = -1; + while (++i < ast->children_num) + if (ast->children[i]->type == TAG_STRING) + arg_num++; + if ((value_cmd = value_new_cmd(arg_num)) == NULL) + return (NULL); + i = -1; + while (++i < ast->children_num) + if (ast->children[i]->type == TAG_STRING) + { + if ((value_cmd->args[arg_num++] = + ft_strdup(ast->children[i]->contents)) == NULL) + return (NULL); + } + while (i-- > 0) + if (ast->children[i]->type != TAG_STRING) + { + if (ast->children[i]->tag == TAG_REDIR_IN + && value_cmd->in == NULL) + value_cmd->in = value_new_redir(VAL_REDIR_IN); // check null + else if (ast->children[i]->tag == TAG_REDIR_OUT + && value_cmd->out == NULL)) + value_cmd->out = value_new_redir(VAL_REDIR_OUT); + else if (ast->children[i]->tag == TAG_REDIR_APPEND + && value_cmd->append == NULL)) + value_cmd->append = value_new_redir(VAL_REDIR_APPEND); + } + return (value_cmd); +} + +/** +** \brief Convert an AST to value +** \param ast AST to convert +** \return Converted value +*/ + +t_value *eval_read(t_ast *ast) +{ + t_value *value; + + /* if (ast->tag == TAG_REDIR_IN) */ + /* return (value_new_redir(VAL_REDIR_IN)); */ + /* else if (ast->tag == TAG_REDIR_OUT) */ + /* return (value_new_redir(VAL_REDIR_OUT)); */ + /* else if (ast->tag == TAG_REDIR_APPEND) */ + /* return (value_new_redir(VAL_REDIR_APPEND)); */ + else if (ast->tag == TAG_CMD) + return (read_cmd(ast)); +} diff --git a/src/eval/val.c b/src/eval/val.c new file mode 100644 index 0000000..3293972 --- /dev/null +++ b/src/eval/val.c @@ -0,0 +1,69 @@ +/** +** \file val.c +** \brief Evaluation value manipulation +*/ + +#include "ms_evalue.h" + +/** +** \brief Allocate memory for a t_value and set his type +** \param type Type of valueue +** \return The allocated value +*/ + +t_value *value_new(t_value_type type) +{ + t_value *value; + + if ((value = (t_value*)malloc(sizeof(t_value*))) == NULL) + return (NULL); + value->type = type; + return (value); +} + +/** +** \brief Create a new redirection value from a filename +** \param type Type of redirection and value +** \param filename Name of the file to open +** \return Redirection value +** \warning Undefined behavior on none redirection type. +*/ + +t_value *value_new_redir(t_value_type type, char *filename) +{ + t_value *value; + + if ((value = value_new(type)) == NULL) + return (NULL); + if (type == VAL_REDIR_IN) + value->data.fd = open(filename, O_RDONLY); + else if (type == VAL_REDIR_OUT) + value->data.fd = open(filename, O_WRONLY | O_CREAT); + else if (type == VAL_REDIR_APPEND) + value->data.fd = open(filename, O_APPEND | O_CREAT); + if (value->data.fd < 0) + { + free(value); + return (NULL); + } + return (value); +} + +/** +** \brief Create a new string value (i.e error, arg, cmd) +** \param type String value type +** \param str String data +** \return String value +*/ + +t_value *value_new_string(t_value_type type, char *str) +{ + t_value *value; + + if ((value == malloc(sizeof(t_value))) == NULL) + return (NULL); + value->type = type; + if ((value->data.str = ft_strdup(str)) == NULL) + return (NULL); + return (value); +} -- cgit