diff --git a/Makefile b/Makefile index b33993a..39c781a 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: ttsubo +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2025/04/03 12:55:20 by ttsubo #+# #+# # -# Updated: 2025/05/04 19:49:07 by ttsubo ### ########.fr # +# Updated: 2025/05/05 18:42:27 by ttsubo ### ########.fr # # # # **************************************************************************** # @@ -60,7 +60,7 @@ endef # 2.Add the source code when it is added ROOT_SRC = main.c minish_signal.c initialize.c error.c -COMMON_SRC = validate_key.c error_mes.c free_functions.c free_functions_2.c +COMMON_SRC = validate_key.c error_mes.c is_quote.c free_functions.c free_functions_2.c INVOKE_CMD_SRC = create_envp.c exec_pipeline.c execute_cmd.c \ execute_cmd_helper.c invoke_command.c pipeline_helper.c \ redirect.c fds.c wait_pipeline.c @@ -68,7 +68,7 @@ TOKENIZER_SRC = tokenizer.c tokenizer_error.c read_token.c \ is_quote_closed.c get_token_capa.c is_redirect_validate.c PARSER_SRC = allocate_cmds.c parser.c parser_utils.c setup_cmds.c \ allocate_cmds_utils.c allocate_cmds_utils_2.c \ - expand_env.c expand_tokens.c + expand_env.c expand_tokens.c trim_quote_tokens.c BUILTIN_SRC = cd.c exit.c pwd.c echo.c env.c unset.c export.c \ env_utils.c env_utils_2.c env_utils_3.c builtin_utils.c \ export_exec.c export_print_sorted_env.c export_error.c split_key_value.c diff --git a/inc/common/common.h b/inc/common/common.h index 247b40d..a341b36 100644 --- a/inc/common/common.h +++ b/inc/common/common.h @@ -6,24 +6,25 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/29 11:44:19 by ttsubo #+# #+# */ -/* Updated: 2025/05/04 19:17:09 by ttsubo ### ########.fr */ +/* Updated: 2025/05/05 18:38:08 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef COMMON_H # define COMMON_H -# include -# include -# include "libft.h" # include "error.h" +# include "free_functions.h" # include "initialize.h" +# include "libft.h" # include "struct.h" -# include "free_functions.h" +# include +# include bool is_key_start(int c); bool is_key_char(int c); bool is_valid_key(char *str); +bool is_quote(char c); void error_mes(char *name, char *mes); #endif \ No newline at end of file diff --git a/inc/parser/parser.h b/inc/parser/parser.h index f339e03..e064b88 100644 --- a/inc/parser/parser.h +++ b/inc/parser/parser.h @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 14:14:59 by ttsubo #+# #+# */ -/* Updated: 2025/05/04 19:19:21 by ttsubo ### ########.fr */ +/* Updated: 2025/05/05 18:38:15 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,7 @@ t_cmd **calloc_cmds(char **tokens); t_cmd *calloc_cmd(void); char **calloc_argv(char **tokens); char *calloc_arg(char *token); +char **trim_quote_tokens(char **tokens); t_cmd **parser(char **tokens, t_minish *minish); char **expand_tokens(char **tokens, t_minish *minish); diff --git a/src/common/is_quote.c b/src/common/is_quote.c new file mode 100644 index 0000000..c00eccf --- /dev/null +++ b/src/common/is_quote.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* is_quote.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ttsubo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/05 14:07:03 by ttsubo #+# #+# */ +/* Updated: 2025/05/05 14:15:15 by ttsubo ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "common.h" + +bool is_quote(char c) +{ + return (c == '\'' || c == '"'); +} diff --git a/src/parser/parser.c b/src/parser/parser.c index 87ad077..cb11a5f 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 14:14:32 by ttsubo #+# #+# */ -/* Updated: 2025/05/04 19:20:38 by ttsubo ### ########.fr */ +/* Updated: 2025/05/05 18:39:13 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,18 +20,19 @@ */ t_cmd **parser(char **tokens, t_minish *minish) { - char **expanded_tokens; + char **formatted_tokens; t_cmd **cmds; - expanded_tokens = expand_tokens(tokens, minish); - if (!expanded_tokens) + formatted_tokens = expand_tokens(tokens, minish); + if (!formatted_tokens) return (NULL); - cmds = allocate_cmds(expanded_tokens); + formatted_tokens = trim_quote_tokens(formatted_tokens); + cmds = allocate_cmds(formatted_tokens); if (!cmds) - return (free_strs(&expanded_tokens), NULL); - cmds = setup_cmds(cmds, expanded_tokens); + return (free_strs(&formatted_tokens), NULL); + cmds = setup_cmds(cmds, formatted_tokens); if (!cmds) - return (free_strs(&expanded_tokens), NULL); - free_strs(&expanded_tokens); + return (free_strs(&formatted_tokens), NULL); + free_strs(&formatted_tokens); return (cmds); } diff --git a/src/parser/trim_quote_tokens.c b/src/parser/trim_quote_tokens.c new file mode 100644 index 0000000..22a3ff3 --- /dev/null +++ b/src/parser/trim_quote_tokens.c @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* trim_quote_tokens.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ttsubo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/05 14:52:11 by ttsubo #+# #+# */ +/* Updated: 2025/05/05 14:52:43 by ttsubo ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +static void _copy_quoted_content(char *token, char *result, size_t *pt_i, + size_t *pr_i) +{ + size_t t_i; + size_t r_i; + char quote; + + t_i = *pt_i; + r_i = *pr_i; + quote = token[t_i++]; + while (token[t_i] && token[t_i] != quote) + result[r_i++] = token[t_i++]; + if (token[t_i] == quote) + t_i++; + else + { + r_i = *pr_i; + result[r_i++] = quote; + t_i = *pt_i; + while (token[t_i]) + result[r_i++] = token[t_i++]; + } + *pt_i = t_i; + *pr_i = r_i; +} + +static void _exec(char *token, char *result, size_t *pt_i, size_t *pr_i) +{ + if (is_quote(token[*pt_i])) + _copy_quoted_content(token, result, pt_i, pr_i); + else + result[(*pr_i)++] = token[(*pt_i)++]; +} + +static char *_trim_quote_token(char *token) +{ + size_t t_i; + size_t r_i; + char *result; + + if (!token) + return (NULL); + result = ft_calloc(ft_strlen(token) + 1, sizeof(char)); + if (!result) + return (NULL); + t_i = 0; + r_i = 0; + while (token[t_i]) + _exec(token, result, &t_i, &r_i); + free(token); + return (result); +} + +char **trim_quote_tokens(char **tokens) +{ + size_t i; + + i = 0; + while (tokens[i]) + { + tokens[i] = _trim_quote_token(tokens[i]); + i++; + } + return (tokens); +}