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/preprocess/filename.c | 11 +++++++++- src/preprocess/interpolation.c | 50 +++++++++++++++++++++++++++++++++++++++++- src/preprocess/preprocess.c | 32 ++++++++++++++++++++++++++- 3 files changed, 90 insertions(+), 3 deletions(-) (limited to 'src/preprocess') 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; -- cgit