Merge remote-tracking branch 'origin/quinten' into willem

This commit is contained in:
whaffman 2025-02-28 01:11:45 +01:00
commit 2f3f22b563
25 changed files with 523 additions and 317 deletions

View File

@ -4,6 +4,14 @@ A lot of amazing shell stuff
- libreadline-dev - libreadline-dev
- libncurses-dev - libncurses-dev
## Comments on work
- Checked if path is a directory and send appropriate message
- `executor_absolute_path` => extracted some code into helpers to make it norminette compliant
- `executor_child` => refactored putstr_fd into error_msg | checked if file on path is exectuable to show permission denied where applicable
- `lexer_token_next` => extracted code for norminette
- `parser` => **A LOT** of work in all files for norminette compliancy
- `expander_parse_string` => Fixed so that $HOME/test.txt returns the correct path and file
## Edge Cases ## Edge Cases
- `cat $PWD/test` should expand, - `cat $PWD/test` should expand,
- `cd -` : `bash: cd: OLDPWD not set` - `cd -` : `bash: cd: OLDPWD not set`

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/04 16:13:11 by whaffman #+# #+# */ /* Created: 2025/02/04 16:13:11 by whaffman #+# #+# */
/* Updated: 2025/02/06 16:14:44 by qmennen ### ########.fr */ /* Updated: 2025/02/27 16:36:39 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,6 +26,7 @@
# include <signal.h> # include <signal.h>
# include <dirent.h> # include <dirent.h>
# include <termios.h> # include <termios.h>
# include <limits.h>
# include <term.h> # include <term.h>
# include <readline/readline.h> # include <readline/readline.h>

View File

@ -15,9 +15,11 @@
# include "minishell.h" # include "minishell.h"
t_command *parser_command_new(t_minishell *msh, char *cmd); t_command *parser_alloc_command(t_minishell *msh, char *cmd);
char **parser_get_arguments(t_list *list, t_minishell *msh);
t_list *parser_get_commands(t_minishell *msh); t_list *parser_get_commands(t_minishell *msh);
t_list *parser_get_input_redirects(t_list *list); t_list *parser_get_input_redirects(t_list *list);
void parser_create_command(t_minishell *msh, t_command *cmd, t_list **l_tkn);
char **parser_get_arguments(t_list *list, t_minishell *msh);
int parser_validate_command(t_command *command);
#endif #endif

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* redirect.h :+: :+: */ /* redirect.h :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: Quinten <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/23 12:26:29 by Quinten #+# #+# */ /* Created: 2025/02/23 12:26:29 by Quinten #+# #+# */
/* Updated: 2025/02/23 12:26:29 by Quinten ######## odam.nl */ /* Updated: 2025/02/27 18:09:47 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,8 +15,8 @@
# include "minishell.h" # include "minishell.h"
t_redirect *redirect_new(t_minishell *msh, t_token_type type, char *value); t_redirect *redirect_new(t_minishell *msh, t_token_type type, char *value);
void redirect_new_error(t_minishell *msh, t_list **redirects); void redirect_new_error(t_minishell *msh, t_list **redirects, int flag);
void redirect_create(t_minishell *msh, t_list **tokens, t_list **redirects, t_token_type type); int redirect_create(t_minishell *msh, t_list **tokens, t_list **redirects, t_token_type type);
t_list *redirect_get_inputs(t_minishell *msh, t_list *list); t_list *redirect_get_inputs(t_minishell *msh, t_list *list);
t_list *redirect_get_outputs(t_minishell *msh, t_list *list); t_list *redirect_get_outputs(t_minishell *msh, t_list *list);
int redirect_is_valid(t_list *lst, t_token *token, int mode); int redirect_is_valid(t_list *lst, t_token *token, int mode);

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* tokenizer.h :+: :+: */ /* tokenizer.h :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/05 12:36:00 by whaffman #+# #+# */ /* Created: 2025/02/05 12:36:00 by whaffman #+# #+# */
/* Updated: 2025/02/26 15:45:26 by whaffman ######## odam.nl */ /* Updated: 2025/02/27 19:03:09 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,6 +21,7 @@ void ft_lexer_free(t_minishell *msh, t_lexer *lexer);
void lexer_readchar(t_lexer *lexer); void lexer_readchar(t_lexer *lexer);
char *lexer_readword(t_minishell *msh, t_lexer *lexer); char *lexer_readword(t_minishell *msh, t_lexer *lexer);
t_list *ft_parse_input(t_minishell *msh); t_list *ft_parse_input(t_minishell *msh);
char *lexer_parse_quotes(t_minishell *msh, t_lexer *lexer);
/** /**
* Token * Token
*/ */
@ -29,5 +30,7 @@ t_token *token_new(t_minishell *msh, t_token_type type, char *c, int pos);
void ft_token_free(t_minishell *msh, t_token *token); void ft_token_free(t_minishell *msh, t_token *token);
void ft_clear_tokenlist(t_minishell *msh, void *content); void ft_clear_tokenlist(t_minishell *msh, void *content);
t_token *token_parse(t_minishell *msh, t_lexer *lexer); t_token *token_parse(t_minishell *msh, t_lexer *lexer);
char *token_type_convert(t_token_type type);
t_token_type token_char_convert(char c, int is_double);
#endif // TOKENIZER_H #endif // TOKENIZER_H

24
sources.mk Normal file
View File

@ -0,0 +1,24 @@
VPATH = src:src/prompt:src/utils:src/lexer:src/token:src/environment:src/executor:src/parser:src/debug:src/expander:src/builtin:src/signal:src/redirect:
SOURCES = history_load.c history_write.c prompt.c print_banner.c check_malloc.c \
free_command_list.c free_freelist.c free_lexer.c free_minishell.c \
free_safe.c free_token.c free_token_list.c ft_lstclear_safe.c \
ft_lstnew_safe.c ft_strdup_safe.c ft_strjoin_safe.c malloc_safe.c \
error_msg.c free_minishell_line.c ft_substr_safe.c init_minishell.c \
lexer_read_char.c lexer_new.c lexer_parse_input.c lexer_read_word.c \
lexer_token_next.c token_new.c token_parse.c environment_add.c \
environment_del.c environment_free.c environment_free_list.c \
environment_get.c environment_get_arr.c environment_parse.c \
environment_print.c executor_close_fds.c executor_absolute_path.c \
executor_count_fds.c executor_create_pipes.c \
executor_create_redirects.c executor_open_fds.c \
executor_execute_pipeline.c executor_fork.c executor_child.c \
parser_create_command.c parser_get_arguments.c parser_alloc_command.c \
parser_get_commands.c parser_validate_command.c print_commands.c \
print_freelist.c expander_is_character.c expander_expand_dollar.c \
expander_allocate_memory.c expander_get_variable.c \
expander_parse_variables.c expander_parse_string.c is_builtin.c \
simple_builtins.c builtin_cd.c builtin_echo.c builtin_env.c \
builtin_exit.c builtin_export.c builtin_pwd.c builtin_router.c \
builtin_unset.c signal.c signal_init.c.c redirect_get_inputs.c \
redirect_get_outputs.c redirect_new.c redirect_new_error.c \
redirect_process_heredoc.c redirect_valid_type.c main.c \

View File

@ -18,8 +18,8 @@ int builtin_export(t_minishell *msh, t_command *cmd)
char **arr; char **arr;
int i; int i;
i = 1; i = 0;
while (cmd->args[i] != NULL) while (cmd->args[++i] != NULL)
{ {
arr = ft_split(cmd->args[i], '='); arr = ft_split(cmd->args[i], '=');
if (arr[1] == NULL) if (arr[1] == NULL)
@ -37,7 +37,6 @@ int builtin_export(t_minishell *msh, t_command *cmd)
else else
environment_add(msh, arr[0], arr[1]); environment_add(msh, arr[0], arr[1]);
ft_free_arr(arr); ft_free_arr(arr);
i++;
} }
return (SUCCESS); return (SUCCESS);
} }

View File

@ -12,15 +12,10 @@
#include "minishell.h" #include "minishell.h"
char *executor_absolute_path(t_minishell *msh, char *cmd) static char *resolve_relative(t_minishell *msh, char *cmd)
{ {
char **path;
t_environment *path_env;
char *executable; char *executable;
int i;
if (cmd[0] == '/' || cmd[0] == '.')
{
if (access(cmd, F_OK) == 0) if (access(cmd, F_OK) == 0)
{ {
executable = ft_strdup_safe(msh, cmd); executable = ft_strdup_safe(msh, cmd);
@ -29,7 +24,31 @@ char *executor_absolute_path(t_minishell *msh, char *cmd)
return (executable); return (executable);
} }
return (NULL); return (NULL);
} }
static char *resolve_path(t_minishell *msh, char *path, char *cmd)
{
char *executable;
executable = malloc_safe(msh, ft_strlen(path) + ft_strlen(cmd) + 2);
ft_strlcpy(executable, path, ft_strlen(path) + 1);
ft_strlcat(executable, "/", ft_strlen(path) + 2);
ft_strlcat(executable, cmd, ft_strlen(path) + ft_strlen(cmd) + 2);
if (access(executable, F_OK) == 0)
return (executable);
free_safe(msh, (void **)&executable);
return (NULL);
}
char *executor_absolute_path(t_minishell *msh, char *cmd)
{
char **path;
t_environment *path_env;
char *executable;
int i;
if (cmd[0] == '/' || cmd[0] == '.')
return (resolve_relative(msh, cmd));
path_env = environment_get(msh, "PATH"); path_env = environment_get(msh, "PATH");
if (!path_env) if (!path_env)
return (NULL); return (NULL);
@ -37,16 +56,9 @@ char *executor_absolute_path(t_minishell *msh, char *cmd)
i = 0; i = 0;
while (path[i] != NULL) while (path[i] != NULL)
{ {
executable = malloc_safe(msh, ft_strlen(path[i]) + ft_strlen(cmd) + 2); executable = resolve_path(msh, path[i], cmd);
ft_strlcpy(executable, path[i], ft_strlen(path[i]) + 1); if (executable)
ft_strlcat(executable, "/", ft_strlen(path[i]) + 2);
ft_strlcat(executable, cmd, ft_strlen(path[i]) + ft_strlen(cmd) + 2);
if (access(executable, F_OK) == 0)
{
ft_free_arr(path);
return (executable); return (executable);
}
free_safe(msh, (void **)&executable);
i++; i++;
} }
return (ft_free_arr(path), NULL); return (ft_free_arr(path), NULL);

View File

@ -6,12 +6,24 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/12 21:25:10 by willem #+# #+# */ /* Created: 2025/02/12 21:25:10 by willem #+# #+# */
/* Updated: 2025/02/26 18:20:34 by qmennen ### ########.fr */ /* Updated: 2025/02/27 18:12:48 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
static int is_dir(char *path)
{
struct stat path_stats;
if (stat(path, &path_stats) < 0)
{
error_msg("is_dir", "path could not be read");
return (0);
}
return (S_ISDIR(path_stats.st_mode));
}
void executor_child(t_minishell *msh, t_command *command) void executor_child(t_minishell *msh, t_command *command)
{ {
char *path; char *path;
@ -22,12 +34,17 @@ void executor_child(t_minishell *msh, t_command *command)
dup2(command->fd_out, 1); dup2(command->fd_out, 1);
executor_close_fds(command->n_fds); executor_close_fds(command->n_fds);
path = executor_absolute_path(msh, command->args[0]); path = executor_absolute_path(msh, command->args[0]);
if (path == NULL) if (path == NULL || access(path, F_OK | X_OK) != 0)
{
error_msg("minishell", "command not found");
return ;
}
if (is_dir(path))
{ {
ft_putstr_fd(RED BOLD, 2); ft_putstr_fd(RED BOLD, 2);
ft_putstr_fd("minishell: ", 2);
ft_putstr_fd(command->args[0], 2); ft_putstr_fd(command->args[0], 2);
ft_putstr_fd(": " RESET "command not found\n", 2); ft_putstr_fd(": " RESET "Is a directory\n", 2);
return ;
} }
execve(path, command->args, environment_get_arr(msh)); execve(path, command->args, environment_get_arr(msh));
} }

View File

@ -40,9 +40,9 @@ char *expander_parse_string(char *s, t_minishell *msh)
current = variables; current = variables;
i = 0; i = 0;
j = 0; j = 0;
while (s[i] && current) while (s[i])
{ {
if (s[i] == '$' && s[i + 1]) if (s[i] == '$' && s[i + 1] && current)
{ {
i++; i++;
i += expander_expand_dollar(s + i, string, &j, current); i += expander_expand_dollar(s + i, string, &j, current);

View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_parse_quotes.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/27 19:02:37 by qmennen #+# #+# */
/* Updated: 2025/02/27 19:26:35 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *lexer_parse_quotes(t_minishell *msh, t_lexer *lexer)
{
int start;
int len;
char qc;
char *word;
qc = lexer->current_char;
word = NULL;
lexer_readchar(lexer);
start = lexer->pos;
while (lexer->current_char != '\0' && lexer->current_char != qc)
lexer_readchar(lexer);
len = lexer->pos - start;
word = malloc_safe(msh, sizeof(char) * len + 1);
ft_strlcpy(word, lexer->input + start, len + 1);
if (lexer->current_char == qc)
lexer_readchar(lexer);
else
{
free_safe(msh, (void **)&word);
return (NULL);
}
return (word);
}

View File

@ -1,41 +1,55 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* lexer_read_word.c :+: :+: */ /* lexer_read_word.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/05 19:03:47 by qmennen #+# #+# */ /* Created: 2025/02/05 19:03:47 by qmennen #+# #+# */
/* Updated: 2025/02/26 16:13:59 by whaffman ######## odam.nl */ /* Updated: 2025/02/27 19:30:09 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
static char *parse_quotes(t_minishell *msh, t_lexer *lexer) static int match_quotes(t_minishell *msh, t_lexer *lexer)
{ {
int start; char c;
int len; int i;
char qc; int d_qts;
char *word; int s_qts;
qc = lexer->current_char; i = lexer->pos;
word = NULL; c = lexer->input[i];
lexer_readchar(lexer); d_qts = 0;
start = lexer->pos; s_qts = 0;
while (lexer->current_char != '\0' && lexer->current_char != qc) //TODO: Normalize this validation. Happens a lot
lexer_readchar(lexer); while (ft_isprint(c) && c != '<' && c != '>' && c != '|' && c != '\0' && !ft_isspace(c))
len = lexer->pos - start;
word = malloc_safe(msh, sizeof(char) * len + 1);
ft_strlcpy(word, lexer->input + start, len + 1);
if (lexer->current_char == qc)
lexer_readchar(lexer);
else
{ {
free_safe(msh, (void **)&word); if (lexer->input[i] == '\'')
return (NULL); s_qts++;
if (lexer->input[i] == '\'')
d_qts++;
i++;
c = lexer->input[i];
} }
return (word); return ((s_qts % 2) == 0 && (d_qts % 2) == 0);
}
static int calculate_word_len(t_minishell *msh, t_lexer *lexer)
{
int i;
int len;
len = 1;
i = lexer->pos;
while (ft_isprint(lexer->input[i]) && lexer->input[i] != '<' && lexer->input[i]!= '>' && lexer->input[i] != '|' && lexer->input[i] != '\0' && !ft_isspace(lexer->input[i]))
{
if(lexer->input[i] != '\'' && lexer->input[i] != '"')
len++;
i++;
}
return (len);
} }
char *lexer_readword(t_minishell *msh, t_lexer *lexer) char *lexer_readword(t_minishell *msh, t_lexer *lexer)
@ -43,21 +57,28 @@ char *lexer_readword(t_minishell *msh, t_lexer *lexer)
int start; int start;
int len; int len;
char *word; char *word;
char c;
start = lexer->pos; start = lexer->pos;
if (lexer->current_char == '"' || lexer->current_char == '\'') if (lexer->current_char == '"' || lexer->current_char == '\'')
{ {
return (parse_quotes(msh, lexer)); return (lexer_parse_quotes(msh, lexer));
} }
while (ft_isprint(lexer->current_char) && lexer->current_char != '<' if (!match_quotes(msh, lexer))
&& lexer->current_char != '>' && lexer->current_char != '|' return (NULL);
&& lexer->current_char != '\0' && !ft_isspace(lexer->current_char) len = calculate_word_len(msh, lexer);
&& lexer->current_char != '"' && lexer->current_char != '\'') word = malloc_safe(msh, sizeof(char) * len);
c = lexer->current_char;
len = 0;
while (ft_isprint(c) && c != '<' && c != '>' && c != '|' && c != '\0' && !ft_isspace(c))
{ {
if(c != '\'' && c != '"')
word[len++] = lexer->current_char;
lexer_readchar(lexer); lexer_readchar(lexer);
c = lexer->current_char;
} }
len = lexer->pos - start; word[len] = 0;
word = malloc_safe(msh, sizeof(char) * len + 1);
ft_strlcpy(word, lexer->input + start, len + 1);
return (word); return (word);
} }

View File

@ -11,7 +11,6 @@
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#include "typedef.h"
static t_token_type get_word_type(char c) static t_token_type get_word_type(char c)
{ {
@ -23,6 +22,21 @@ 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)
{
t_token_type word_type;
t_token *token;
char *word;
word_type = get_word_type(lexer->current_char);
word = lexer_readword(msh, lexer);
if (!word)
return (token_new(msh, T_ERROR, &(lexer->current_char), pos));
token = token_new(msh, word_type, word, pos);
free_safe(msh, (void **)&word);
return (token);
}
/** /**
* @brief Retrieves the next token from the lexer. * @brief Retrieves the next token from the lexer.
* *
@ -45,32 +59,23 @@ static t_token_type get_word_type(char c)
t_token *ft_token_next(t_minishell *msh, t_lexer *lexer) t_token *ft_token_next(t_minishell *msh, t_lexer *lexer)
{ {
t_token *token; t_token *token;
t_token_type word_type;
char *word;
int current_pos; int current_pos;
char c;
token = NULL; token = NULL;
while (ft_isspace(lexer->current_char)) while (ft_isspace(lexer->current_char))
lexer_readchar(lexer); lexer_readchar(lexer);
current_pos = lexer->pos; current_pos = lexer->pos;
if (lexer->current_char == '\0') c = lexer->current_char;
if (c == '\0')
token = token_new(msh, T_EOF, NULL, current_pos); token = token_new(msh, T_EOF, NULL, current_pos);
else if (lexer->current_char == '<' || lexer->current_char == '>' else if (c == '<' || c == '>'
|| lexer->current_char == '|') || c == '|')
token = token_parse(msh, lexer); token = token_parse(msh, lexer);
else if (ft_isprint(lexer->current_char)) else if (ft_isprint(lexer->current_char))
{ token = process_word(msh, lexer, current_pos);
word_type = get_word_type(lexer->current_char);
word = lexer_readword(msh, lexer);
if (!word)
return (token_new(msh, T_ERROR, &lexer->current_char, current_pos));
token = token_new(msh, word_type, word, current_pos);
free_safe(msh, (void **)&word);
}
else else
{
token = token_new(msh, T_ERROR, NULL, current_pos); token = token_new(msh, T_ERROR, NULL, current_pos);
printf("token->type: %d\n", token->type); c = lexer->current_char;
}
return (token); return (token);
} }

View File

@ -12,7 +12,7 @@
#include "minishell.h" #include "minishell.h"
t_command *parser_command_new(t_minishell *msh, char *cmd) t_command *parser_alloc_command(t_minishell *msh, char *cmd)
{ {
t_command *command; t_command *command;

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_create_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/27 13:35:02 by qmennen #+# #+# */
/* Updated: 2025/02/27 13:36:48 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void parser_create_command(t_minishell *msh, t_command *cmd, t_list **l_tkn)
{
cmd->args = parser_get_arguments(*l_tkn, msh);
cmd->redirect_in = redirect_get_inputs(msh, *l_tkn);
cmd->redirect_out = redirect_get_outputs(msh, *l_tkn);
}

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 16:20:09 by qmennen #+# #+# */ /* Created: 2025/02/11 16:20:09 by qmennen #+# #+# */
/* Updated: 2025/02/26 18:35:27 by qmennen ### ########.fr */ /* Updated: 2025/02/27 18:58:48 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,13 +34,23 @@ static int count_cmds(t_list *list)
static int parser_should_expand(t_list *value) static int parser_should_expand(t_list *value)
{ {
t_token *token; t_token *token;
int i;
token = (t_token *)value->content; token = (t_token *)value->content;
if (!token) if (!token)
return (0); return (0);
return ((token->type == T_DQWORD && ft_strchr(token->value, '$')) i = 0;
|| (token->type == T_WORD if (token->type == T_DQWORD)
&& token->value[0] == '$' {
while (token->value[i])
{
if (token->value[i] == '$' && !expander_character_valid(token->value[i + 1]))
return (0);
i++;
}
return (1);
}
return ((token->type == T_WORD && token->value[0] == '$'
&& token->value[1])); && token->value[1]));
} }

View File

@ -6,32 +6,17 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */ /* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */
/* Updated: 2025/02/18 20:36:01 by qmennen ### ########.fr */ /* Updated: 2025/02/27 16:07:54 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#include "utils.h"
static int is_command_token(t_token *token) static int is_command_token(t_token *token)
{ {
return (token->type < 3 || redirect_token_type(token)); return (token->type < 3 || redirect_token_type(token));
} }
static int validate_redirects(t_list *lst)
{
t_list *token;
token = lst;
while (token)
{
if (((t_redirect *)token->content)->type == T_ERROR)
return (0);
token = token->next;
}
return (1);
}
t_list *parser_get_commands(t_minishell *msh) t_list *parser_get_commands(t_minishell *msh)
{ {
t_list *command_list; t_list *command_list;
@ -46,14 +31,10 @@ t_list *parser_get_commands(t_minishell *msh)
while (current) while (current)
{ {
token = (t_token *) current->content; token = (t_token *) current->content;
command = parser_command_new(msh, ft_strdup_safe(msh, token->value)); command = parser_alloc_command(msh, ft_strdup_safe(msh, token->value));
command->args = parser_get_arguments(current, msh); parser_create_command(msh, command, &current);
command->redirect_in = redirect_get_inputs(msh, current); if (! parser_validate_command(command))
command->redirect_out = redirect_get_outputs(msh, current);
if (!validate_redirects(command->redirect_in))
{
break ; break ;
}
ft_lstadd_back(&command_list, ft_lstnew_safe(msh, command)); ft_lstadd_back(&command_list, ft_lstnew_safe(msh, command));
while (current && is_command_token((t_token *)current->content)) while (current && is_command_token((t_token *)current->content))
current = current->next; current = current->next;

View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_validate_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/27 16:06:33 by qmennen #+# #+# */
/* Updated: 2025/02/27 16:08:49 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int validate_redirects(t_list *lst)
{
t_list *token;
token = lst;
while (token)
{
if (((t_redirect *)token->content)->type == T_ERROR)
return (0);
token = token->next;
}
return (1);
}
int parser_validate_command(t_command *command)
{
int r_in;
int r_out;
r_in = validate_redirects(command->redirect_in);
r_out = validate_redirects(command->redirect_out);
return (r_in && r_out);
}

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/23 12:29:05 by Quinten #+# #+# */ /* Created: 2025/02/23 12:29:05 by Quinten #+# #+# */
/* Updated: 2025/02/26 17:38:59 by qmennen ### ########.fr */ /* Updated: 2025/02/27 18:09:15 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -39,10 +39,9 @@ t_list *redirect_get_inputs(t_minishell *msh, t_list *list)
} }
check_heredoc(msh, current, token); check_heredoc(msh, current, token);
flag = redirect_is_valid(current, token, F_OK | R_OK); flag = redirect_is_valid(current, token, F_OK | R_OK);
redirect_create(msh, &current, &redirects, token->type); flag && (redirect_create(msh, &current, &redirects, token->type));
current = current->next; current = current->next;
} }
if (flag <= 0) redirect_new_error(msh, &redirects, flag);
redirect_new_error(msh, &redirects);
return (redirects); return (redirects);
} }

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* redirect_get_outputs.c :+: :+: */ /* redirect_get_outputs.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: Quinten <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/23 12:37:24 by Quinten #+# #+# */ /* Created: 2025/02/23 12:37:24 by Quinten #+# #+# */
/* Updated: 2025/02/23 12:37:24 by Quinten ######## odam.nl */ /* Updated: 2025/02/27 18:12:04 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,7 +23,7 @@ t_list *redirect_get_outputs(t_minishell *msh, t_list *list)
redirects = NULL; redirects = NULL;
current = list; current = list;
token = (t_token *)current->content; token = (t_token *)current->content;
while (current && flag && !redirect_is_delimiter(token)) while (current && !redirect_is_delimiter(token))
{ {
token = (t_token *)current->content; token = (t_token *)current->content;
if (token->type != T_REDIRECT_OUT && token->type != T_APPEND_OUT) if (token->type != T_REDIRECT_OUT && token->type != T_APPEND_OUT)
@ -32,10 +32,9 @@ t_list *redirect_get_outputs(t_minishell *msh, t_list *list)
continue ; continue ;
} }
flag = redirect_is_valid(current, token, -1); flag = redirect_is_valid(current, token, -1);
redirect_create(msh, &current, &redirects, token->type); flag && (redirect_create(msh, &current, &redirects, token->type));
current = current->next; current = current->next;
} }
if (flag <= 0) redirect_new_error(msh, &redirects, flag);
redirect_new_error(msh, &redirects);
return (redirects); return (redirects);
} }

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* redirect_new.c :+: :+: */ /* redirect_new.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: Quinten <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/23 12:27:33 by Quinten #+# #+# */ /* Created: 2025/02/23 12:27:33 by Quinten #+# #+# */
/* Updated: 2025/02/23 12:27:33 by Quinten ######## odam.nl */ /* Updated: 2025/02/27 18:11:25 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,7 +24,7 @@ t_redirect *redirect_new(t_minishell *msh, t_token_type type, char *value)
return (result); return (result);
} }
void redirect_create(t_minishell *msh, t_list **tokens, t_list **redirects, int redirect_create(t_minishell *msh, t_list **tokens, t_list **redirects,
t_token_type type) t_token_type type)
{ {
t_list *new; t_list *new;
@ -41,4 +41,5 @@ t_token_type type)
ft_lstadd_back(redirects, new); ft_lstadd_back(redirects, new);
if (tokens) if (tokens)
*tokens = (*tokens)->next; *tokens = (*tokens)->next;
return (1);
} }

View File

@ -6,17 +6,19 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/26 17:37:12 by qmennen #+# #+# */ /* Created: 2025/02/26 17:37:12 by qmennen #+# #+# */
/* Updated: 2025/02/26 17:44:49 by qmennen ### ########.fr */ /* Updated: 2025/02/27 18:07:25 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void redirect_new_error(t_minishell *msh, t_list **redirects) void redirect_new_error(t_minishell *msh, t_list **redirects, int flag)
{ {
t_list *new; t_list *new;
t_redirect *redir; t_redirect *redir;
if (flag)
return ;
redir = redirect_new(msh, T_ERROR, NULL); redir = redirect_new(msh, T_ERROR, NULL);
new = ft_lstnew_safe(msh, redir); new = ft_lstnew_safe(msh, redir);
ft_lstadd_back(redirects, new); ft_lstadd_back(redirects, new);

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* token_char_convert.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/27 18:25:34 by qmennen #+# #+# */
/* Updated: 2025/02/27 18:28:13 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
# include "minishell.h"
t_token_type token_char_convert(char c, int is_double)
{
if (c == '<')
{
if (is_double)
return (T_HEREDOC);
return (T_REDIRECT_IN);
}
else if (c == '>')
{
if (is_double)
return (T_APPEND_OUT);
return (T_REDIRECT_OUT);
}
else if (c == '&' && is_double)
return (T_AND);
else if (c == '|')
{
if (is_double)
return (T_OR);
return (T_PIPE);
}
return (T_ERROR);
}

View File

@ -1,61 +1,17 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* token_parse.c :+: :+: */ /* token_parse.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/05 19:10:17 by qmennen #+# #+# */ /* Created: 2025/02/05 19:10:17 by qmennen #+# #+# */
/* Updated: 2025/02/26 16:15:19 by whaffman ######## odam.nl */ /* Updated: 2025/02/27 18:27:40 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
static t_token_type token_from_char(char c, int is_double)
{
if (c == '<')
{
if (is_double)
return (T_HEREDOC);
return (T_REDIRECT_IN);
}
else if (c == '>')
{
if (is_double)
return (T_APPEND_OUT);
return (T_REDIRECT_OUT);
}
else if (c == '&' && is_double)
return (T_AND);
else if (c == '|')
{
if (is_double)
return (T_OR);
return (T_PIPE);
}
return (T_ERROR);
}
static char *char_from_type(t_token_type type)
{
if (type == T_HEREDOC)
return ("<<");
else if (type == T_REDIRECT_IN)
return ("<");
else if (type == T_APPEND_OUT)
return (">>");
else if (type == T_REDIRECT_OUT)
return (">");
else if (type == T_AND)
return ("&&");
else if (type == T_OR)
return ("||");
else if (type == T_PIPE)
return ("|");
return (NULL);
}
t_token *token_parse(t_minishell *msh, t_lexer *lexer) t_token *token_parse(t_minishell *msh, t_lexer *lexer)
{ {
int is_double; int is_double;
@ -65,8 +21,8 @@ t_token *token_parse(t_minishell *msh, t_lexer *lexer)
c = lexer->current_char; c = lexer->current_char;
is_double = lexer->input[lexer->pos + 1] == c; is_double = lexer->input[lexer->pos + 1] == c;
type = token_from_char(c, is_double); type = token_char_convert(c, is_double);
token = token_new(msh, type, char_from_type(type), lexer->pos); token = token_new(msh, type, token_type_convert(type), lexer->pos);
if (is_double) if (is_double)
lexer_readchar(lexer); lexer_readchar(lexer);
lexer_readchar(lexer); lexer_readchar(lexer);

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* token_type_convert.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/27 18:25:34 by qmennen #+# #+# */
/* Updated: 2025/02/27 18:26:40 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
# include "minishell.h"
char *token_type_convert(t_token_type type)
{
if (type == T_HEREDOC)
return ("<<");
else if (type == T_REDIRECT_IN)
return ("<");
else if (type == T_APPEND_OUT)
return (">>");
else if (type == T_REDIRECT_OUT)
return (">");
else if (type == T_AND)
return ("&&");
else if (type == T_OR)
return ("||");
else if (type == T_PIPE)
return ("|");
return (NULL);
}