aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/builtin/cd.c5
-rw-r--r--src/builtin/echo.c5
-rw-r--r--src/builtin/env.c5
-rw-r--r--src/builtin/exit.c4
-rw-r--r--src/builtin/export.c5
-rw-r--r--src/builtin/pwd.c4
-rw-r--r--src/builtin/unset.c5
-rw-r--r--src/env.c21
-rw-r--r--src/eval/eval.c (renamed from src/eval.c)49
-rw-r--r--src/eval/read.c89
-rw-r--r--src/eval/val.c69
-rw-r--r--src/main.c13
-rw-r--r--src/parse/ast.c63
-rw-r--r--src/parse/lexer.c5
-rw-r--r--src/parse/parse.c5
-rw-r--r--src/path.c24
-rw-r--r--src/utils.c (renamed from src/util.c)10
17 files changed, 331 insertions, 50 deletions
diff --git a/src/builtin/cd.c b/src/builtin/cd.c
index f82a876..fe806ec 100644
--- a/src/builtin/cd.c
+++ b/src/builtin/cd.c
@@ -1,3 +1,8 @@
+/**
+** \file cd.c
+** \brief `cd` builtin
+*/
+
#include "minishell.h"
int ms_cd(t_env env, char **argv)
diff --git a/src/builtin/echo.c b/src/builtin/echo.c
index b2bcee9..817da28 100644
--- a/src/builtin/echo.c
+++ b/src/builtin/echo.c
@@ -1,3 +1,8 @@
+/**
+** \file echo.c
+** \brief `echo` builtin
+*/
+
#include "minishell.h"
int ms_echo(char **argv)
diff --git a/src/builtin/env.c b/src/builtin/env.c
index 9e962ec..226b5b9 100644
--- a/src/builtin/env.c
+++ b/src/builtin/env.c
@@ -1,3 +1,8 @@
+/**
+** \file env.c
+** \brief `env` builtin
+*/
+
#include "minishell.h"
void st_print_env_variable(t_ftht_content *content)
diff --git a/src/builtin/exit.c b/src/builtin/exit.c
index e69de29..f4cc4fd 100644
--- a/src/builtin/exit.c
+++ b/src/builtin/exit.c
@@ -0,0 +1,4 @@
+/**
+** \file exit.c
+** \brief `exit` builtin
+*/
diff --git a/src/builtin/export.c b/src/builtin/export.c
index df27655..77d46c1 100644
--- a/src/builtin/export.c
+++ b/src/builtin/export.c
@@ -1,3 +1,8 @@
+/**
+** \file export.c
+** \brief `export` builtin
+*/
+
#include "minishell.h"
/* int ms_export(t_env env, char **argv) */
diff --git a/src/builtin/pwd.c b/src/builtin/pwd.c
index 2836852..326f834 100644
--- a/src/builtin/pwd.c
+++ b/src/builtin/pwd.c
@@ -1,3 +1,7 @@
+/**
+** \file pwd.c
+** \brief `pwd` builtin
+*/
#include "minishell.h"
int ms_pwd(void)
diff --git a/src/builtin/unset.c b/src/builtin/unset.c
index bc4327b..4d7c628 100644
--- a/src/builtin/unset.c
+++ b/src/builtin/unset.c
@@ -1,3 +1,8 @@
+/**
+** \file unset.c
+** \brief `unset` builtin
+*/
+
#include "minishell.h"
int ms_unset(t_env env, char **argv)
diff --git a/src/env.c b/src/env.c
index 04ab561..c019cb4 100644
--- a/src/env.c
+++ b/src/env.c
@@ -10,10 +10,25 @@
/* */
/* ************************************************************************** */
+/**
+** \file env.c
+** \brief Environment hash table manipulation
+*/
+
#include "minishell.h"
+/**
+** \brief Number of buckets of an environment hash table
+*/
+
#define MS_ENV_HT_SIZE 2048
+/**
+** \brief Convert array of string to environment hash table
+** \param envp array of string (each in the format `name=value`)
+** \return Environment hash table or NULL on error
+*/
+
t_env ms_env_from_array(char **envp)
{
t_env env;
@@ -42,6 +57,12 @@ t_env ms_env_from_array(char **envp)
return (env);
}
+/**
+** \brief Convert environment to array of string
+** \param env Environment hash table
+** \return Array of string on NULL on error
+*/
+
char **ms_env_to_array(t_env env)
{
(void)env;
diff --git a/src/eval.c b/src/eval/eval.c
index 907949d..8fca2c1 100644
--- a/src/eval.c
+++ b/src/eval/eval.c
@@ -1,48 +1,10 @@
-#include "minishell.h"
-
-t_val *val_new_redir(t_val_type type, char *filename)
-{
- t_val *val;
-
- if ((val = val_new(type)) == NULL)
- return (NULL);
- if (type == VAL_REDIR_IN)
- val->data.fd = open(filename, O_RDONLY);
- else if (type == VAL_REDIR_OUT)
- val->data.fd = open(filename, O_WRONLY);
- else if (type == VAL_REDIR_APPEND)
- val->data.fd = open(filename, O_RDWR);
- if (val->data.fd < 0)
- {
- free(val);
- return (NULL);
- }
- return (val);
-}
-
-t_val *val_new_err(char *msg)
-{
- t_val *val;
-
- if ((val == malloc(sizeof(t_val))) == NULL)
- return (NULL);
- val->type = VAL_ERR;
- if ((val->data.str = ft_strdup(msg)) == NULL)
- return (NULL);
- return (val);
-}
+/**
+** \file eval.c
+** \brief Evaluation of an AST
+*/
-t_val *read_ast(t_ast *ast)
-{
- t_val *val;
+#include "minishell.h"
- if (ast->tag == TAG_REDIR_IN)
- {
- val = val_new();
- val->type = VAL_REDIR_IN;
- val->value.fd = open(ast->content, O_RDONLY);
- }
-}
static bool check_node(t_ast *ast)
{
@@ -129,6 +91,7 @@ static char **get_args(t_ast *ast)
*/
}
+//reduce?
int ms_eval(t_path path, t_env env, t_ast *ast)
{
int status;
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);
+}
diff --git a/src/main.c b/src/main.c
index 11e4517..52b5baa 100644
--- a/src/main.c
+++ b/src/main.c
@@ -10,8 +10,21 @@
/* */
/* ************************************************************************** */
+/**
+** \file main.c
+** \brief Minishell entrypoint
+*/
+
#include "minishell.h"
+/**
+** \brief Program entrypoint
+** \param argc Number of arguments in `argv`
+** \param argv Array of string, argument of the program
+** \param envp NULL terminated array of string representing the environment
+** \return 0 if ok, 1 otherwise
+*/
+
int main(int argc, char **argv, const char **envp)
{
t_path path;
diff --git a/src/parse/ast.c b/src/parse/ast.c
new file mode 100644
index 0000000..f566832
--- /dev/null
+++ b/src/parse/ast.c
@@ -0,0 +1,63 @@
+/**
+** \file ast.c
+** \brief AST manipulation
+*/
+
+#include "ms_parse.h"
+
+/**
+** \brief Create a new AST node with default values
+** \param tag Tag of the node
+** \return The allocated node
+*/
+
+t_ast *ms_ast_new(t_tag tag)
+{
+ t_ast *ast;
+
+ if ((ast = (t_ast*)malloc(sizeof(t_ast))) == NULL)
+ return (NULL);
+ ast->tag = tag;
+ ast->content = NULL;
+ ast->children_num = 0;
+ ast->children = NULL;
+ return (ast);
+}
+
+/**
+** \brief Destroy an allocated AST
+** \warning Assumes that `content`, `children` and the node itself have been malloc'd
+** \param ast AST to destroy
+*/
+
+void ms_ast_destroy(t_ast *ast)
+{
+ int i;
+
+ if (ast == NULL)
+ return ;
+ i = -1;
+ while (++i < ast->children_num)
+ ms_ast_destroy(ast->children[i]);
+ free(ast->children);
+ free(ast->content);
+ free(ast);
+}
+
+/**
+** \brief Iterate over an AST node's childs
+** \param f Function applied to each child, take `arg` has his first argument.
+** \param arg Pointer that will be passed to `f`, to keep information between iterations
+*/
+
+void ms_ast_iter(
+ t_ast *ast,
+ void (*f)(void *f_arg, t_ast *children),
+ void *arg)
+{
+ int i;
+
+ i = -1;
+ while (++i < ast->children_num)
+ f(arg, ast->children[i]);
+}
diff --git a/src/parse/lexer.c b/src/parse/lexer.c
index b5caba2..d8f1254 100644
--- a/src/parse/lexer.c
+++ b/src/parse/lexer.c
@@ -1,3 +1,8 @@
+/**
+** \file lexer.c
+** \brief Lexer
+*/
+
#include "minishell.h"
char **ms_lexer(char *input)
diff --git a/src/parse/parse.c b/src/parse/parse.c
index bdd0144..41b82c2 100644
--- a/src/parse/parse.c
+++ b/src/parse/parse.c
@@ -1,3 +1,8 @@
+/**
+** \file parse.c
+** \brief Parser
+*/
+
#include "minishell.h"
t_ast *ms_parse(char *input)
diff --git a/src/path.c b/src/path.c
index 3ac12d9..c2b6582 100644
--- a/src/path.c
+++ b/src/path.c
@@ -10,12 +10,24 @@
/* */
/* ************************************************************************** */
+/**
+** \file path.c
+** \brief Path hash table manipulation
+*/
+
#include "minishell.h"
+/**
+** \brief Number of buckets of a path hash table
+*/
+
#define MS_PATH_HT_SIZE 4096
-/*
-** Update `path` with all files in the directory named `dirname`.
+/**
+** \brief Update `path` with all files in the directory named `dirname`.
+** \param path Path hash table
+** \param dirname directory name
+** \return Same path or NULL on error
*/
static t_path st_path_dir_update(t_path path, char *dirname)
@@ -38,9 +50,11 @@ static t_path st_path_dir_update(t_path path, char *dirname)
return (path);
}
-/*
-** Update (or create if `path` is NULL) `path` according to `path_str`
-** (each directory is separated by a ':').
+/**
+** \brief Update the path
+** \param path Path hash table or NULL to create a new one
+** \param path_var PATH environment variable where each directory is separated by a ':'
+** \return The updated/created path hash table or NULL on error
*/
t_path ms_path_update(t_path path, char *path_var)
diff --git a/src/util.c b/src/utils.c
index 188b762..2b655bf 100644
--- a/src/util.c
+++ b/src/utils.c
@@ -10,10 +10,16 @@
/* */
/* ************************************************************************** */
+/**
+** \file utils.c
+** \brief Various functions
+*/
+
#include "minishell.h"
-/*
-** delete a hash table entry containing a allocated string key and value
+/**
+** \brief Delete function for a entry containing a allocated key and value
+** \param content Hash table entry content
*/
void ms_ht_del_str_entry(t_ftht_content *content)