Merge remote-tracking branch 'origin/quinten' into willem
This commit is contained in:
commit
98ede770a5
@ -24,5 +24,7 @@ void parser_create_command(t_minishell *msh,
|
|||||||
char **parser_get_arguments(t_list *list, t_minishell *msh);
|
char **parser_get_arguments(t_list *list, t_minishell *msh);
|
||||||
int parser_validate_command(t_command *command);
|
int parser_validate_command(t_command *command);
|
||||||
int parser_count_arguments(t_list *list);
|
int parser_count_arguments(t_list *list);
|
||||||
|
char *parser_concatenate(t_minishell *msh, char *str1, char *str2);
|
||||||
|
char *parser_process_token(t_minishell *msh, t_list *prev, t_list *t_head);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/05 13:11:41 by qmennen #+# #+# */
|
/* Created: 2025/03/05 13:11:41 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/03/05 13:11:58 by qmennen ### ########.fr */
|
/* Updated: 2025/03/05 20:54:45 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -16,5 +16,6 @@
|
|||||||
# include "minishell.h"
|
# include "minishell.h"
|
||||||
|
|
||||||
t_token *token_from_list(t_list *list);
|
t_token *token_from_list(t_list *list);
|
||||||
|
t_list *token_list_index(t_list *tokens, int index);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -27,5 +27,4 @@ int builtin_router(t_minishell *msh, t_command *cmd)
|
|||||||
if (is_builtin(cmd->args[0]) < 0)
|
if (is_builtin(cmd->args[0]) < 0)
|
||||||
return (FALSE);
|
return (FALSE);
|
||||||
return (builtin_fn[is_builtin(cmd->args[0])](msh, cmd));
|
return (builtin_fn[is_builtin(cmd->args[0])](msh, cmd));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ void environment_print(t_minishell *msh, int export)
|
|||||||
t_environment *env;
|
t_environment *env;
|
||||||
|
|
||||||
env_list = msh->environment;
|
env_list = msh->environment;
|
||||||
while(env_list != NULL)
|
while (env_list != NULL)
|
||||||
{
|
{
|
||||||
env = (t_environment *)env_list->content;
|
env = (t_environment *)env_list->content;
|
||||||
if (export)
|
if (export)
|
||||||
|
|||||||
@ -24,7 +24,7 @@ static int is_dir(char *path)
|
|||||||
return (S_ISDIR(path_stats.st_mode));
|
return (S_ISDIR(path_stats.st_mode));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int validate_executable_path(char *path)
|
static int validate_executable_path(char *path)
|
||||||
{
|
{
|
||||||
if (access(path, X_OK) < 0)
|
if (access(path, X_OK) < 0)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -34,7 +34,7 @@ static int execute_builtin(t_minishell *msh, t_command *cmd)
|
|||||||
return (exit_status);
|
return (exit_status);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int executor_execute_command(t_minishell *msh, t_command *cmd)
|
static int executor_execute_command(t_minishell *msh, t_command *cmd)
|
||||||
{
|
{
|
||||||
if (is_builtin(cmd->args[0]) >= 0)
|
if (is_builtin(cmd->args[0]) >= 0)
|
||||||
msh->exit_status = execute_builtin(msh, cmd);
|
msh->exit_status = execute_builtin(msh, cmd);
|
||||||
|
|||||||
@ -41,6 +41,5 @@ t_list *ft_parse_input(t_minishell *msh)
|
|||||||
break ;
|
break ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ft_token_free(msh, token);
|
|
||||||
return (list);
|
return (list);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,7 +30,6 @@ static int calculate_word_len(t_minishell *msh, t_lexer *lexer)
|
|||||||
i = lexer->pos;
|
i = lexer->pos;
|
||||||
while (is_word_char(lexer->input[i]))
|
while (is_word_char(lexer->input[i]))
|
||||||
{
|
{
|
||||||
|
|
||||||
len++;
|
len++;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
@ -67,8 +66,6 @@ char *lexer_readword(t_minishell *msh, t_lexer *lexer)
|
|||||||
qts = (c == '"' || c == '\'');
|
qts = (c == '"' || c == '\'');
|
||||||
if (qts)
|
if (qts)
|
||||||
return (lexer_parse_quotes(msh, lexer));
|
return (lexer_parse_quotes(msh, lexer));
|
||||||
// if (!match_quotes(msh, lexer, qts))
|
|
||||||
// return (NULL);
|
|
||||||
len = calculate_word_len(msh, lexer);
|
len = calculate_word_len(msh, lexer);
|
||||||
word = read_word(msh, lexer, len);
|
word = read_word(msh, lexer, len);
|
||||||
return (word);
|
return (word);
|
||||||
|
|||||||
@ -22,7 +22,7 @@ static t_token_type get_word_type(char c)
|
|||||||
return (T_WORD);
|
return (T_WORD);
|
||||||
}
|
}
|
||||||
|
|
||||||
static t_token * process_word(t_minishell *msh, t_lexer *lexer, int pos)
|
static t_token *process_word(t_minishell *msh, t_lexer *lexer, int pos)
|
||||||
{
|
{
|
||||||
t_token_type word_type;
|
t_token_type word_type;
|
||||||
t_token *token;
|
t_token *token;
|
||||||
|
|||||||
@ -22,7 +22,6 @@ t_command *parser_alloc_command(t_minishell *msh)
|
|||||||
command->fd_out = 1;
|
command->fd_out = 1;
|
||||||
command->redirect_in = NULL;
|
command->redirect_in = NULL;
|
||||||
command->redirect_out = NULL;
|
command->redirect_out = NULL;
|
||||||
// command->environment = NULL;
|
|
||||||
command->n_fds = 0;
|
command->n_fds = 0;
|
||||||
return (command);
|
return (command);
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/parser/parser_concatenate.c
Normal file
23
src/parser/parser_concatenate.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parser_concatenate.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/03/05 20:14:54 by qmennen #+# #+# */
|
||||||
|
/* Updated: 2025/03/05 20:18:30 by qmennen ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
char *parser_concatenate(t_minishell *msh, char *str1, char *str2)
|
||||||
|
{
|
||||||
|
char *cat;
|
||||||
|
|
||||||
|
cat = malloc_safe(msh, ft_strlen(str1) + ft_strlen(str2) + 1);
|
||||||
|
ft_strlcpy(cat, str1, ft_strlen(str1) + 1);
|
||||||
|
ft_strlcat(cat, str2, ft_strlen(str2) + ft_strlen(str1) + 1);
|
||||||
|
return (cat);
|
||||||
|
}
|
||||||
@ -25,6 +25,8 @@ int parser_count_arguments(t_list *list)
|
|||||||
while (current)
|
while (current)
|
||||||
{
|
{
|
||||||
token = ((t_token *)current->content);
|
token = ((t_token *)current->content);
|
||||||
|
if (redirect_is_delimiter(token))
|
||||||
|
break ;
|
||||||
if (token->type < 3 && (!prev || ((t_token *)prev->content)->type < 3))
|
if (token->type < 3 && (!prev || ((t_token *)prev->content)->type < 3))
|
||||||
cmds++;
|
cmds++;
|
||||||
prev = current;
|
prev = current;
|
||||||
@ -32,5 +34,3 @@ int parser_count_arguments(t_list *list)
|
|||||||
}
|
}
|
||||||
return (cmds);
|
return (cmds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -11,93 +11,46 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
#include "typedef.h"
|
||||||
|
|
||||||
static int parser_should_expand(t_list *value)
|
static int parser_should_concact(t_minishell *msh, int argi, t_list *cur, t_list *prev)
|
||||||
{
|
{
|
||||||
t_token *token;
|
t_token *c_tkn;
|
||||||
int i;
|
t_token *p_tkn;
|
||||||
|
char lexer_char;
|
||||||
|
|
||||||
token = (t_token *)value->content;
|
if (argi < 1)
|
||||||
if (!token)
|
|
||||||
return (0);
|
return (0);
|
||||||
i = 0;
|
p_tkn = token_from_list(prev);
|
||||||
if (token->type == T_DQWORD || token->type == T_WORD)
|
c_tkn = token_from_list(cur);
|
||||||
{
|
lexer_char = 0;
|
||||||
while (token->value[i])
|
if (!p_tkn || !c_tkn || c_tkn->position <= 0)
|
||||||
{
|
|
||||||
if (token->value[i] == '$'
|
|
||||||
&& (expander_character_valid(token->value[i + 1]) || token->value[i + 1] == '?'))
|
|
||||||
return (1);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
lexer_char = msh->lexer->input[c_tkn->position - 1];
|
||||||
return (0);
|
// Determine exact separation position
|
||||||
}
|
return (c_tkn->type < 3 && p_tkn->type < 3 && !ft_isspace(lexer_char));
|
||||||
|
|
||||||
static int parser_should_concact(t_minishell *msh, int argi, t_list *prev)
|
|
||||||
{
|
|
||||||
t_token *prev_tkn;
|
|
||||||
char c;
|
|
||||||
|
|
||||||
prev_tkn = token_from_list(prev);
|
|
||||||
c = 0;
|
|
||||||
if (!prev_tkn || argi < 1)
|
|
||||||
return (0);
|
|
||||||
if (prev_tkn->position > 0)
|
|
||||||
c = msh->lexer->input[prev_tkn->position - 1];
|
|
||||||
return (ft_strcmp(prev_tkn->value, "") == 0 && c && !ft_isspace(c));
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *parser_process_token(t_minishell *msh, char **args, t_list *prev, t_list *t_head)
|
|
||||||
{
|
|
||||||
char *str;
|
|
||||||
t_token *token;
|
|
||||||
|
|
||||||
if (!t_head)
|
|
||||||
return (NULL);
|
|
||||||
token = (t_token *)t_head->content;
|
|
||||||
str = NULL;
|
|
||||||
if (ft_strcmp(token->value, "") == 0)
|
|
||||||
return (NULL);
|
|
||||||
if (parser_should_expand(t_head))
|
|
||||||
str = expander_parse_string(token->value, msh);
|
|
||||||
if (!str)
|
|
||||||
str = ft_strdup_safe(msh, token->value);
|
|
||||||
if (str && token->type == T_WORD && ft_strchr(str, '"'))
|
|
||||||
str = parser_sanitize_string(msh, str, '"');
|
|
||||||
else if (str && token->type == T_WORD && ft_strchr(str, '\''))
|
|
||||||
str = parser_sanitize_string(msh, str, '\'');
|
|
||||||
return (str);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char **parser_get_arguments(t_list *list, t_minishell *msh)
|
char **parser_get_arguments(t_list *list, t_minishell *msh)
|
||||||
{
|
{
|
||||||
t_list *current;
|
t_list *current;
|
||||||
t_list *prev;
|
t_list *prev;
|
||||||
|
char *str;
|
||||||
char **args;
|
char **args;
|
||||||
int argc;
|
int argc;
|
||||||
int i;
|
int i;
|
||||||
char *str;
|
|
||||||
char *cat;
|
|
||||||
|
|
||||||
argc = parser_count_arguments(list);
|
argc = parser_count_arguments(list);
|
||||||
args = malloc_safe(msh, (argc + 1) * sizeof(char *));
|
args = malloc_safe(msh, (argc + 1) * sizeof(char *));
|
||||||
current = list;
|
current = list;
|
||||||
i = 0;
|
i = 0;
|
||||||
prev = NULL;
|
|
||||||
while (argc > 0 && current)
|
while (argc > 0 && current)
|
||||||
{
|
{
|
||||||
if (token_from_list(current)->type < 3 && (!prev || token_from_list(prev)->type < 3))
|
if (token_from_list(current)->type < 3)
|
||||||
{
|
{
|
||||||
str = parser_process_token(msh, args, prev, current);
|
str = parser_process_token(msh, token_list_index(list, i - 1), current);
|
||||||
if (parser_should_concact(msh, i, prev))
|
if (parser_should_concact(msh, i, current, prev))
|
||||||
{
|
args[i - 1] = parser_concatenate(msh, args[i - 1], str);
|
||||||
cat = malloc_safe(msh, ft_strlen(str) + ft_strlen(args[i - 1]) + 1);
|
|
||||||
ft_strlcpy(cat, args[i - 1], ft_strlen(args[i - 1]) + 1);
|
|
||||||
ft_strlcat(cat, str, ft_strlen(str) + ft_strlen(args[i - 1]) + 1);
|
|
||||||
args[i - 1] = cat;
|
|
||||||
}
|
|
||||||
else if (str)
|
else if (str)
|
||||||
args[i++] = str;
|
args[i++] = str;
|
||||||
argc--;
|
argc--;
|
||||||
|
|||||||
62
src/parser/parser_process_token.c
Normal file
62
src/parser/parser_process_token.c
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parser_process_token.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/03/05 21:12:15 by qmennen #+# #+# */
|
||||||
|
/* Updated: 2025/03/05 21:42:44 by qmennen ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
static int parser_should_expand(t_list *value)
|
||||||
|
{
|
||||||
|
t_token *token;
|
||||||
|
char *t_val;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
token = (t_token *)value->content;
|
||||||
|
if (!token)
|
||||||
|
return (0);
|
||||||
|
i = 0;
|
||||||
|
if (token->type != T_DQWORD && token->type != T_WORD)
|
||||||
|
return (0);
|
||||||
|
while (token->value[i])
|
||||||
|
{
|
||||||
|
t_val = token->value;
|
||||||
|
if (t_val[i] == '$' && expander_character_valid(t_val[i + 1]))
|
||||||
|
return (1);
|
||||||
|
else if (t_val[i] == '$' && t_val[i + 1] == '?')
|
||||||
|
return (1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: Make an exception for this `echo hey""you "" test`. here echo actually interprets "" as an extra space. Just check if its empty and surrounded by spaces or sth. Can't be asked rn
|
||||||
|
char *parser_process_token(t_minishell *msh, t_list *prev, t_list *t_head)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
t_token *token;
|
||||||
|
t_token *p_token;
|
||||||
|
|
||||||
|
if (!t_head)
|
||||||
|
return (NULL);
|
||||||
|
token = (t_token *)t_head->content;
|
||||||
|
p_token = (t_token *)prev->content;
|
||||||
|
str = NULL;
|
||||||
|
if (ft_strcmp(token->value, "") == 0)
|
||||||
|
return (NULL);
|
||||||
|
if (parser_should_expand(t_head))
|
||||||
|
str = expander_parse_string(token->value, msh);
|
||||||
|
if (!str)
|
||||||
|
str = ft_strdup_safe(msh, token->value);
|
||||||
|
if (str && token->type == T_WORD && ft_strchr(str, '"'))
|
||||||
|
str = parser_sanitize_string(msh, str, '"');
|
||||||
|
else if (str && token->type == T_WORD && ft_strchr(str, '\''))
|
||||||
|
str = parser_sanitize_string(msh, str, '\'');
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
@ -20,7 +20,7 @@ void history_write(char *line)
|
|||||||
fd = open(".minishell_history", O_WRONLY | O_APPEND | O_CREAT, 0644);
|
fd = open(".minishell_history", O_WRONLY | O_APPEND | O_CREAT, 0644);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return ;
|
return ;
|
||||||
// if (*line)
|
if (*line)
|
||||||
// ft_putendl_fd(line, fd);
|
ft_putendl_fd(line, fd);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|||||||
25
src/token/token_list_index.c
Normal file
25
src/token/token_list_index.c
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* token_list_index.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/03/05 20:53:34 by qmennen #+# #+# */
|
||||||
|
/* Updated: 2025/03/05 21:00:01 by qmennen ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
t_list *token_list_index(t_list *tokens, int index)
|
||||||
|
{
|
||||||
|
t_list *current;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
current = tokens;
|
||||||
|
while ((i++) < index && current)
|
||||||
|
current = current->next;
|
||||||
|
return (current);
|
||||||
|
}
|
||||||
@ -15,8 +15,9 @@
|
|||||||
void *malloc_safe(t_minishell *msh, size_t size)
|
void *malloc_safe(t_minishell *msh, size_t size)
|
||||||
{
|
{
|
||||||
void *ptr;
|
void *ptr;
|
||||||
|
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
ptr = malloc(size);
|
ptr = malloc(size);
|
||||||
check_malloc(msh, ptr);
|
check_malloc(msh, ptr);
|
||||||
return (ptr);
|
return (ptr);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user