From cccd4692fab390d0c4fbab3fcae7f4aa55ca9f1a Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sat, 10 Oct 2020 11:43:05 +0200 Subject: Added comment to preprocess, redir, setup, signal, eval_cmd and utils --- src/eval/cmd.c | 19 +++++++++++++++- src/eval/eval.c | 10 ++++----- src/eval/redir.c | 30 ++++++++++++++++++++++++- src/preprocess/filename.c | 11 +++++++++- src/preprocess/interpolation.c | 50 +++++++++++++++++++++++++++++++++++++++++- src/preprocess/preprocess.c | 32 ++++++++++++++++++++++++++- src/setup.c | 35 ++++++++++++++++++++++++++++- src/signal.c | 20 ++++++++++++++++- src/utils.c | 13 ++++++++++- 9 files changed, 206 insertions(+), 14 deletions(-) diff --git a/src/eval/cmd.c b/src/eval/cmd.c index a35a73d..d107ce2 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/10/09 14:31:45 by cacharle ### ########.fr */ +/* Updated: 2020/10/10 11:32:36 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,14 @@ pid_t g_child_pid = -1; +/* +** \brief Function wrapped in fork_wrap. +** If it's a builtin, call the coresponding builtin function +** Otherwise call execve +** \param param Parameters of this function +** \return The status of the builtin or the error code of execve +*/ + static int st_wrapped_cmd(t_fork_param_cmd *param) { int status; @@ -36,6 +44,15 @@ static int st_split_destroy_ret(int ret, char **strs) return (ret); } +/* +** \brief Evaluate a command +** \param fds Input/output file descriptor of the command +** \param env Environment +** \param ast Comment AST node +** \return EVAL_FATAL no allocation error, +** the status of the runned command otherwise +*/ + int eval_cmd(int fds[2], t_env env, t_ast *ast) { t_fork_param_cmd param; diff --git a/src/eval/eval.c b/src/eval/eval.c index 032fc30..f78e8ee 100644 --- a/src/eval/eval.c +++ b/src/eval/eval.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/13 20:38:06 by charles #+# #+# */ -/* Updated: 2020/10/09 16:12:27 by cacharle ### ########.fr */ +/* Updated: 2020/10/10 11:28:12 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,11 +21,9 @@ static int st_replace(int oldfd, int newfd) /* ** \brief Wrap a function in a fork -** \param fds fork read/write file descriptors -** \param passed param of the wrapped function -** \param wrapped function to wrap -** \param child_pid Pointer where to store the child pid -** or NULL if the child should be waited +** \param fds Fork read/write file descriptors +** \param passed Param of the wrapped function +** \param wrapped Function to wrap ** \return The child status code or EVAL_FATAL on error */ diff --git a/src/eval/redir.c b/src/eval/redir.c index 9d88b29..8c154d5 100644 --- a/src/eval/redir.c +++ b/src/eval/redir.c @@ -6,12 +6,21 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/15 11:05:34 by charles #+# #+# */ -/* Updated: 2020/10/09 14:38:16 by cacharle ### ########.fr */ +/* Updated: 2020/10/10 11:40:16 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "eval.h" +/* +** \brief Open a file and close the previous opened file +** if there was one already setup +** \param filename File to open +** \param fd File descriptor to set or replace +** \param oflag Flag passed to the open function +** \return 0 on success, the error status code otherwise +*/ + static int st_open_replace(char *filename, int *fd, int oflag) { if (fd == NULL) @@ -31,6 +40,15 @@ static int st_open_replace(char *filename, int *fd, int oflag) return (0); } +/* +** \brief Call st_open_replace with different argument +** according to the redirection type +** \param filename Name of the file to open +** \param fds Input/output file descriptors +** \param tag Token tag of the redirection +** \return Whatever st_open_replace returns +*/ + static int st_open_replace_dispatch(char *filename, int fds[2], enum e_tok tag) { int *fd; @@ -64,6 +82,16 @@ static int st_tok_lsts_destroy_ret( return (ret); } +/* +** \brief Extract redirections from tokens +** \param redirs List of token of redirection, in the format +** redir token -> n sticked string token -> redir token -> ... +** \param env Environement need for interpolation of redirection filename +** \param fds Input/output file descriptor to setup +** \return 0 on success, +** the command evaluation error status code otherwise +*/ + int redir_extract(t_tok_lst **redirs, t_env env, int fds[2]) { t_tok_lst *after; diff --git a/src/preprocess/filename.c b/src/preprocess/filename.c index 0470554..fa5c3d2 100644 --- a/src/preprocess/filename.c +++ b/src/preprocess/filename.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/10/09 15:29:04 by cacharle #+# #+# */ -/* Updated: 2020/10/09 15:30:03 by cacharle ### ########.fr */ +/* Updated: 2020/10/10 10:49:50 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,15 @@ #include "lexer.h" #include "minishell.h" +/* +** \brief Preprocess the tokens of a filename +** \param tokens List of sicked string tokens +** \param env Environment +** \param filename A pointer where to put the resulting filename +** \return Return 0 on success, 1 on ambiguous redirect, +** EVAL_FATAL if an allocation failed +*/ + int preprocess_filename(t_tok_lst **tokens, t_env env, char **filename) { char **strs; diff --git a/src/preprocess/interpolation.c b/src/preprocess/interpolation.c index c28d545..455cb90 100644 --- a/src/preprocess/interpolation.c +++ b/src/preprocess/interpolation.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/10/09 15:27:46 by cacharle #+# #+# */ -/* Updated: 2020/10/10 10:26:43 by cacharle ### ########.fr */ +/* Updated: 2020/10/10 11:07:57 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,12 @@ #include "lexer.h" #include "minishell.h" +/* +** \brief Split a string into token +** \param str String to split +** \return The list of tokens or NULL on error +*/ + static t_tok_lst *st_field_split(char *str) { t_tok_lst *ret; @@ -45,6 +51,19 @@ static t_tok_lst *st_field_split(char *str) #define MATCH 1 #define AFTER 2 +/* +** \brief Search for a match in the environmnet +** initialize strs variable if found +** \param strs Strings to intialise +** strs[0] string before the match +** strs[1] interpolation match +** strs[2] string after the match +** \param env Environment +** \param str Current string to search in the environment +** \param i Current position in the str +** \return Return true on match, false otherwise +*/ + static bool st_make_strs(char *strs[3], t_env env, char *str, size_t i) { size_t var_len; @@ -62,6 +81,15 @@ static bool st_make_strs(char *strs[3], t_env env, char *str, size_t i) return (true); } +/* +** \brief Merge the fields tokens in the current token +** \param strs before/match/after strings +** \param curr Pointer to list where to merge the fields +** \param fields Fields to merge in curr +** \param len The current position in the string after the interpolation +** \return Returns len because of norm complience trick +*/ + static size_t st_merge_fields_in_curr( char *strs[3], t_tok_lst **curr, t_tok_lst *fields, size_t len) { @@ -80,6 +108,15 @@ static size_t st_merge_fields_in_curr( return (len); } +/* +** \brief Interpolate a non quoted string +** \param strs before/match/after strings +** \param curr Current token +** \param i Current position in string +** \param prev_tag Previous Token tag +** \return The position after the interpolation +*/ + static size_t st_interpolate_non_quoted( char *strs[3], t_tok_lst **curr, size_t i, enum e_tok prev_tag) { @@ -109,6 +146,17 @@ static size_t st_interpolate_non_quoted( return (i); } +/* +** \brief Interpolate the string in a token +** \param ptrs Norm complience trick +** ptrs[0] (char*) current string +** ptrs[1] (t_tok_lst**) current token +** \param i Current position in token +** \param prev_tag Tag of the previous token +** \param env Environment +** \return The position in the token after interpolation +*/ + size_t interpolate( void *ptrs[2], size_t i, enum e_tok prev_tag, t_env env) { diff --git a/src/preprocess/preprocess.c b/src/preprocess/preprocess.c index 3852b8b..7bc50d5 100644 --- a/src/preprocess/preprocess.c +++ b/src/preprocess/preprocess.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 08:58:49 by charles #+# #+# */ -/* Updated: 2020/10/10 10:32:53 by cacharle ### ########.fr */ +/* Updated: 2020/10/10 10:56:41 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,12 @@ #include "lexer.h" #include "minishell.h" +/* +** \brief Concatenate sticked string tokens +** \param tokens List of tokens to concatenate +** \return The resulting list of tokens +*/ + t_tok_lst *st_stick_tokens(t_tok_lst *tokens) { t_tok_lst *curr; @@ -36,6 +42,13 @@ t_tok_lst *st_stick_tokens(t_tok_lst *tokens) return (tokens); } +/* +** \brief Convert a list of tokens to a NULL terminated string array +** \param tokens A pointer to a list of tokens to convert +** \return The string array of arguments +** \note tokens is destroyed +*/ + char **st_tokens_to_argv(t_tok_lst **tokens) { char **ret; @@ -56,6 +69,15 @@ char **st_tokens_to_argv(t_tok_lst **tokens) return (ret); } +/* +** \brief Try to escape the first character of a string +** \param str String to escape +** \param tag Tag of the current token +** (different characters are escaped in different type of strings) +** \return true if the first there was a character to escape, +** false otherwise +*/ + bool escape(char *str, enum e_tok tag) { if (str[0] == '\\' && @@ -68,6 +90,14 @@ bool escape(char *str, enum e_tok tag) return (false); } +/* +** \brief Preprocess (escaping and interpolation) +** tokens an convert then to an argv +** \param tokens List of token to preprocess +** \param env Environment +** \return The arguments on success, NULL on allocation error +*/ + char **preprocess(t_tok_lst **tokens, t_env env) { t_tok_lst *curr; diff --git a/src/setup.c b/src/setup.c index 2830e2d..7affe7f 100644 --- a/src/setup.c +++ b/src/setup.c @@ -6,12 +6,20 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/16 15:46:09 by charles #+# #+# */ -/* Updated: 2020/10/10 08:08:35 by cacharle ### ########.fr */ +/* Updated: 2020/10/10 11:19:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +/* +** \brief Export variable if not already in environment +** \param env Environment where to export +** \param key Key to check for export +** \param value Default value +** \return True if allocation succeded, false otherwise +*/ + bool st_export_default(t_env env, char *key, char *value) { if (env_search(env, key, NULL) != NULL) @@ -19,6 +27,12 @@ bool st_export_default(t_env env, char *key, char *value) return (env_export(env, key, value) != NULL); } +/* +** \brief Setup the environment variables +** \param env Environment to setup +** \return true on success, false otherwise +*/ + bool setup_env(t_env env) { char buf[PATH_MAX + 1]; @@ -35,6 +49,12 @@ bool setup_env(t_env env) return (true); } +/* +** \brief Increment the SHLVL variable +** \param env Environment where to set SHLVL +** \return true on success, false otherwise +*/ + bool setup_shlvl(t_env env) { char shlvl_str[64]; @@ -47,6 +67,12 @@ bool setup_shlvl(t_env env) return (env_export(env, "SHLVL", shlvl_str) != NULL); } +/* +** \brief Initialize the progname global variable +** used in error messages +** \param first_arg argv[0] of minishell +*/ + void setup_progname(char *first_arg) { char *last_slash; @@ -60,6 +86,13 @@ void setup_progname(char *first_arg) g_state.progname = last_slash + 1; } +/* +** \brief Setup minishell (signals, env variables, progname) +** \param first_arg Used in setup_progname +** \param env Environment +** \return true on succes, false otherwise +*/ + bool setup(char *first_arg, t_env env) { signal(SIGINT, signal_sigint); diff --git a/src/signal.c b/src/signal.c index a0633fc..5d88368 100644 --- a/src/signal.c +++ b/src/signal.c @@ -6,13 +6,20 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/07/16 09:16:16 by charles #+# #+# */ -/* Updated: 2020/10/07 15:02:56 by cacharle ### ########.fr */ +/* Updated: 2020/10/10 11:23:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "eval.h" #include "minishell.h" +/* +** \brief Interupt signal handler +** If no child is running, print a prompt +** Otherwise the currently running child +** \param signum Ignored (needed by signal function) +*/ + void signal_sigint(int signum) { (void)signum; @@ -32,6 +39,12 @@ void signal_sigint(int signum) } } +/* +** \brief Quit signal handler +** If a child is running send him a SIGQUIT +** \param signum Ignored (needed by signal function) +*/ + void signal_sigquit(int signum) { if (g_state.killed) @@ -45,6 +58,11 @@ void signal_sigquit(int signum) } } +/* +** \brief Term signal handler, does nothing +** \param signum Ignored (needed by signal function) +*/ + void signal_sigterm(int signum) { (void)signum; diff --git a/src/utils.c b/src/utils.c index 2fffe45..4125eba 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:56:31 by cacharle #+# #+# */ -/* Updated: 2020/10/09 12:40:51 by cacharle ### ########.fr */ +/* Updated: 2020/10/10 11:12:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,11 @@ void print_prompt(void) ft_putstr_fd("\033[0m$ ", STDERR_FILENO); } +/* +** \brief Exit the program on condition +** \param predicate Exit if true +*/ + void exit_if(bool predicate) { if (!predicate) @@ -48,6 +53,12 @@ void exit_if(bool predicate) exit(3); } +/* +** \brief Check if a string only contains spaces and tabs +** \param str String to chec +** \return True if only contains blank, false otherwise +*/ + bool utils_strisblank(char *str) { while (*str != '\0') -- cgit