Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: ttsubo <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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 #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -60,15 +60,15 @@ 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
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
Expand Down
11 changes: 6 additions & 5 deletions inc/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdbool.h>
# include <stddef.h>
# include "libft.h"
# include "error.h"
# include "free_functions.h"
# include "initialize.h"
# include "libft.h"
# include "struct.h"
# include "free_functions.h"
# include <stdbool.h>
# include <stddef.h>

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
3 changes: 2 additions & 1 deletion inc/parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions src/common/is_quote.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* is_quote.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 == '"');
}
19 changes: 10 additions & 9 deletions src/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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);
}
79 changes: 79 additions & 0 deletions src/parser/trim_quote_tokens.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* trim_quote_tokens.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}