Merge branch 'quinten'

This commit is contained in:
Quinten Mennen 2025-02-19 16:38:29 +01:00
commit fbe2c51d41
23 changed files with 451 additions and 99 deletions

View File

@ -22,7 +22,7 @@ LIBFT = $(LIBFT_PATH)/libft.a
OBJ_PATH = obj OBJ_PATH = obj
VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils:src/executor:src/parser:src/signal VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils:src/executor:src/parser:src/expander:src/debug:src/signal
SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c"))
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))

20
inc/debug.h Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* debug.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/18 20:06:53 by qmennen #+# #+# */
/* Updated: 2025/02/18 20:10:49 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef DEBUG_H
# define DEBUG_H
# include "minishell.h"
void print_commands(void *param);
#endif

25
inc/expander.h Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expander.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/18 19:00:51 by qmennen #+# #+# */
/* Updated: 2025/02/19 15:19:50 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef EXPANDER_H
# define EXPANDER_H
#include "minishell.h"
t_environment *expander_get_var(const char *s, int idx, t_minishell *minishell);
t_list *expander_parse_variables(const char *s, t_minishell *minishell);
char *expander_allocate_memory(const char *s, t_list *variables);
char *expander_parse_string(char *s, t_minishell *minishell);
int expander_character_valid(const char c);
int expander_expand_dollar(char *src, char *dest, int *j, t_list *variables);
#endif

View File

@ -22,6 +22,8 @@
# include "tokenizer.h" # include "tokenizer.h"
# include "executor.h" # include "executor.h"
# include "parser.h" # include "parser.h"
# include "expander.h"
# include "debug.h"
# include "utils.h" # include "utils.h"
# define TRUE 1 # define TRUE 1

View File

@ -16,7 +16,7 @@
# include "minishell.h" # include "minishell.h"
t_command *parser_command_new(char *cmd); t_command *parser_command_new(char *cmd);
char **parser_get_arguments(t_list *list); char **parser_get_arguments(t_list *list, t_minishell *minishell);
t_list *parser_get_commands(t_list *list); t_list *parser_get_commands(t_minishell *minishell);
#endif #endif

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* tokenizer.h :+: :+: */ /* tokenizer.h :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@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/05 12:36:01 by whaffman ######## odam.nl */ /* Updated: 2025/02/18 17:02:17 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,16 +18,16 @@
*/ */
t_lexer *ft_lexer_new(const char *input); t_lexer *ft_lexer_new(const char *input);
void ft_lexer_free(t_lexer *lexer); void ft_lexer_free(t_lexer *lexer);
void ft_lexer_readchar(t_lexer *lexer); void lexer_readchar(t_lexer *lexer);
char *ft_lexer_readword(t_lexer *lexer); char *lexer_readword(t_lexer *lexer);
t_list *ft_parse_input(t_lexer *lexer); t_list *ft_parse_input(t_lexer *lexer);
/** /**
* Token * Token
*/ */
t_token *ft_token_next(t_lexer *lexer); t_token *ft_token_next(t_lexer *lexer);
t_token *ft_token_new(t_token_type type, char *c, int pos); t_token *token_new(t_token_type type, char *c, int pos);
void ft_token_free(t_token *token); void ft_token_free(t_token *token);
void ft_clear_tokenlist(void *content); void ft_clear_tokenlist(void *content);
t_token *ft_parse_token(t_lexer *lexer); t_token *token_parse(t_lexer *lexer);
#endif // TOKENIZER_H #endif // TOKENIZER_H

View File

@ -16,9 +16,13 @@
typedef enum e_token_type typedef enum e_token_type
{ {
T_WORD, T_WORD,
T_DQWORD,
T_SQWORD,
T_PIPE, T_PIPE,
T_REDIRECT_IN, T_REDIRECT_IN,
T_REDIRECT_OUT, T_REDIRECT_OUT,
T_AND,
T_OR,
T_APPEND_OUT, T_APPEND_OUT,
T_HEREDOC, T_HEREDOC,
T_EOF, T_EOF,

View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_commands.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/18 20:06:37 by qmennen #+# #+# */
/* Updated: 2025/02/18 20:11:45 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void print_commands(void *param)
{
t_command *command;
int i;
command = (t_command *)param;
if (!command)
return ;
printf("command: %s\n", command->command);
printf("args: ");
i = 0;
while (command->args[i++])
printf("%s ", command->args[i]);
printf("\n");
}

View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expander_allocate_memory.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 13:57:19 by qmennen #+# #+# */
/* Updated: 2025/02/19 13:57:36 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *expander_allocate_memory(const char *s, t_list *variables)
{
int size;
t_list *current;
t_environment *env;
char *string;
size = 0;
current = variables;
while (current)
{
if (current->content == NULL)
{
current = current->next;
continue;
}
env = (t_environment *)current->content;
size += (ft_strlen(env->value) - ft_strlen(env->name));
current = current->next;
}
size += ft_strlen(s);
string = malloc(size);
if (!string)
perror("expander malloc");
return (string);
}

View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expander_expand_dollar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 15:05:58 by qmennen #+# #+# */
/* Updated: 2025/02/19 15:22:40 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int get_var_len(const char *source, int idx)
{
int i;
i = 0;
while (expander_character_valid(source[idx + i]))
i++;
return (i);
}
int expander_expand_dollar(char *src, char *dest, int *j, t_list *variables)
{
int i;
int v_len;
t_environment *env;
i = 0;
v_len = 0;
if (variables && variables->content)
{
env = (t_environment *)variables->content;
v_len = ft_strlen(env->name);
while (env->value[i])
{
dest[(*j)++] = env->value[i++];
}
}
else
v_len = get_var_len(src, (*j) + 1);
return (v_len);
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expander_get_variable.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 13:59:03 by qmennen #+# #+# */
/* Updated: 2025/02/19 13:59:14 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_environment *expander_get_var(const char *s, int idx, t_minishell *minishell)
{
int i;
t_environment *env;
char *name;
i = 0;
while (expander_character_valid(s[idx + i]))
i++;
name = ft_substr(s, idx, i);
if (!name || !*name)
return (NULL);
env = environment_get(minishell->environment, name);
free(name);
return (env);
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expander_is_character.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 13:15:11 by qmennen #+# #+# */
/* Updated: 2025/02/19 13:15:55 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int expander_character_valid(const char c)
{
return (ft_isalnum(c) || c == '_');
}

View File

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expander_parse_string.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/18 19:00:35 by qmennen #+# #+# */
/* Updated: 2025/02/19 15:20:04 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void free_variables(t_list *variables)
{
t_list *current;
t_list *last;
current = variables;
while (current)
{
last = current;
current = current->next;
free(last);
}
}
char *expander_parse_string(char *s, t_minishell *minishell)
{
t_list *variables;
t_list *current;
char *string;
int i;
int j;
variables = expander_parse_variables(s, minishell);
string = expander_allocate_memory(s, variables);
i = 0;
j = 0;
current = variables;
while (s[i])
{
if (s[i] == '$' && s[i + 1])
{
i++;
i += expander_expand_dollar(s, string, &j, current);
if (current)
current = current->next;
}
else
string[j++] = s[i++];
}
string[j] = 0;
free_variables(variables);
return (string);
}

View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expander_parse_variables.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 13:58:13 by qmennen #+# #+# */
/* Updated: 2025/02/19 15:07:23 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_list *expander_parse_variables(const char *s, t_minishell *minishell)
{
int i;
t_list *var_list;
t_environment *env;
i = 0;
var_list = NULL;
while (s[i])
{
if (s[i] == '$')
{
env = expander_get_var(s, i + 1, minishell);
if (env)
ft_lstadd_back(&var_list, ft_lstnew(env));
else
ft_lstadd_back(&var_list, ft_lstnew(NULL));
}
i++;
}
return (var_list);
}

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/05 19:09:20 by qmennen #+# #+# */ /* Created: 2025/02/05 19:09:20 by qmennen #+# #+# */
/* Updated: 2025/02/05 19:09:26 by qmennen ### ########.fr */ /* Updated: 2025/02/18 17:18:10 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -1,18 +1,18 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* lexer_readchar.c :+: :+: :+: */ /* lexer_read_char.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/05 19:04:53 by qmennen #+# #+# */ /* Created: 2025/02/05 19:04:53 by qmennen #+# #+# */
/* Updated: 2025/02/05 19:05:06 by qmennen ### ########.fr */ /* Updated: 2025/02/18 17:02:17 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void ft_lexer_readchar(t_lexer *lexer) void lexer_readchar(t_lexer *lexer)
{ {
if ((size_t)lexer->n_pos > ft_strlen(lexer->input)) if ((size_t)lexer->n_pos > ft_strlen(lexer->input))
{ {

View File

@ -6,7 +6,7 @@
/* 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/05 19:28:17 by qmennen ### ########.fr */ /* Updated: 2025/02/19 14:12:19 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,15 +21,15 @@ static char *parse_quotes(t_lexer *lexer)
qc = lexer->current_char; qc = lexer->current_char;
word = NULL; word = NULL;
ft_lexer_readchar(lexer); lexer_readchar(lexer);
start = lexer->pos; start = lexer->pos;
while (lexer->current_char != '\0' && lexer->current_char != qc) while (lexer->current_char != '\0' && lexer->current_char != qc)
ft_lexer_readchar(lexer); lexer_readchar(lexer);
len = lexer->pos - start; len = lexer->pos - start;
word = malloc(sizeof(char) * len + 1); word = malloc(sizeof(char) * len + 1);
ft_strlcpy(word, lexer->input + start, len + 1); ft_strlcpy(word, lexer->input + start, len + 1);
if (lexer->current_char == qc) if (lexer->current_char == qc)
ft_lexer_readchar(lexer); lexer_readchar(lexer);
else else
{ {
free(word); free(word);
@ -38,7 +38,7 @@ static char *parse_quotes(t_lexer *lexer)
return (word); return (word);
} }
char *ft_lexer_readword(t_lexer *lexer) char *lexer_readword(t_lexer *lexer)
{ {
int start; int start;
int len; int len;
@ -54,7 +54,7 @@ char *ft_lexer_readword(t_lexer *lexer)
&& lexer->current_char != '\0' && !ft_isspace(lexer->current_char) && lexer->current_char != '\0' && !ft_isspace(lexer->current_char)
&& lexer->current_char != '"' && lexer->current_char != '\'') && lexer->current_char != '"' && lexer->current_char != '\'')
{ {
ft_lexer_readchar(lexer); lexer_readchar(lexer);
} }
len = lexer->pos - start; len = lexer->pos - start;
word = malloc(sizeof(char) * len + 1); word = malloc(sizeof(char) * len + 1);

View File

@ -6,11 +6,22 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/04 16:07:58 by qmennen #+# #+# */ /* Created: 2025/02/04 16:07:58 by qmennen #+# #+# */
/* Updated: 2025/02/05 19:25:35 by qmennen ### ########.fr */ /* Updated: 2025/02/18 17:02:17 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#include "typedef.h"
static t_token_type get_word_type(char c)
{
if (c == '\'')
return (T_SQWORD);
else if (c == '"')
return (T_DQWORD);
else
return (T_WORD);
}
/** /**
* @brief Retrieves the next token from the lexer. * @brief Retrieves the next token from the lexer.
@ -33,28 +44,30 @@
*/ */
t_token *ft_token_next(t_lexer *lexer) t_token *ft_token_next(t_lexer *lexer)
{ {
t_token *token; t_token *token;
char *word; t_token_type word_type;
int current_pos; char *word;
int current_pos;
token = NULL; token = NULL;
while (ft_isspace(lexer->current_char)) while (ft_isspace(lexer->current_char))
ft_lexer_readchar(lexer); lexer_readchar(lexer);
current_pos = lexer->pos; current_pos = lexer->pos;
if (lexer->current_char == '\0') if (lexer->current_char == '\0')
token = ft_token_new(T_EOF, NULL, current_pos); token = token_new(T_EOF, NULL, current_pos);
else if (lexer->current_char == '<' || lexer->current_char == '>' else if (lexer->current_char == '<' || lexer->current_char == '>'
|| lexer->current_char == '|') || lexer->current_char == '|')
token = ft_parse_token(lexer); token = token_parse(lexer);
else if (ft_isprint(lexer->current_char)) else if (ft_isprint(lexer->current_char))
{ {
word = ft_lexer_readword(lexer); word_type = get_word_type(lexer->current_char);
word = lexer_readword(lexer);
if (!word) if (!word)
return (ft_token_new(T_ERROR, &lexer->current_char, current_pos)); return (token_new(T_ERROR, &lexer->current_char, current_pos));
token = ft_token_new(T_WORD, word, current_pos); token = token_new(word_type, word, current_pos);
free(word); free(word);
} }
else else
token = ft_token_new(T_ERROR, NULL, current_pos); token = token_new(T_ERROR, NULL, current_pos);
return (token); return (token);
} }

View File

@ -41,7 +41,7 @@ int main(int argc, char **argv, char **envp)
minishell->lexer = ft_lexer_new(minishell->line); minishell->lexer = ft_lexer_new(minishell->line);
minishell->tokens = ft_parse_input(minishell->lexer); minishell->tokens = ft_parse_input(minishell->lexer);
ft_lstiter(minishell->tokens, token_print); ft_lstiter(minishell->tokens, token_print);
minishell->commands = parser_get_commands(minishell->tokens); minishell->commands = parser_get_commands(minishell);
simple_builtins(minishell); simple_builtins(minishell);
free_minishell_line(minishell); free_minishell_line(minishell);
ft_lstclear(&minishell->commands, free_command_list); ft_lstclear(&minishell->commands, free_command_list);

View File

@ -16,12 +16,14 @@ static int count_cmds(t_list *list)
{ {
int cmds; int cmds;
t_list *current; t_list *current;
t_token *token;
cmds = 0; cmds = 0;
current = list; current = list;
while (current) while (current)
{ {
if (((t_token *)current->content)->type != T_WORD) token = ((t_token *)current->content);
if (token->type >= 3)
break ; break ;
cmds++; cmds++;
current = current->next; current = current->next;
@ -29,7 +31,18 @@ static int count_cmds(t_list *list)
return (cmds); return (cmds);
} }
char **parser_get_arguments(t_list *list) static int parser_should_expand(t_list *value)
{
t_token *token;
token = (t_token *)value->content;
if (!token)
return (0);
return (token->type == T_DQWORD || (token->type == T_WORD &&
token->value[0] == '$' && token->value[1]));
}
char **parser_get_arguments(t_list *list, t_minishell *minishell)
{ {
t_list *current; t_list *current;
char **args; char **args;
@ -47,7 +60,12 @@ char **parser_get_arguments(t_list *list)
i = -1; i = -1;
while ((++i) < cmds && current) while ((++i) < cmds && current)
{ {
if (((t_token *)current->content)->type == T_WORD) if (parser_should_expand(current))
{
args[i] = expander_parse_string(((t_token *)current->content)->value, minishell);
}
else if (((t_token *)current->content)->type == T_WORD ||
((t_token *)current->content)->type == T_SQWORD)
args[i] = ft_strdup(((t_token *)current->content)->value); args[i] = ft_strdup(((t_token *)current->content)->value);
current = current->next; current = current->next;
} }

View File

@ -1,35 +1,18 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* parser_get_commands.c :+: :+: */ /* parser_get_commands.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* 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/12 20:49:00 by willem ######## odam.nl */ /* Updated: 2025/02/18 20:36:01 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
// static void print_cmds(void *content) t_list *parser_get_commands(t_minishell *minishell)
// {
// t_command *cmd;
// int i;
// cmd = (t_command *)content;
// printf("cmd: %s\n", cmd->command);
// printf("args: ");
// i = 0;
// while (cmd->args[i])
// {
// printf("%s ", cmd->args[i]);
// i++;
// }
// printf("\n");
// }
t_list *parser_get_commands(t_list *list)
{ {
t_list *command_list; t_list *command_list;
t_list *current; t_list *current;
@ -37,20 +20,20 @@ t_list *parser_get_commands(t_list *list)
t_token *token; t_token *token;
command_list = NULL; command_list = NULL;
if (!list) if (!minishell->tokens)
return (NULL); return (NULL);
current = list; current = minishell->tokens;
while (current) while (current)
{ {
token = (t_token *) current->content; token = (t_token *) current->content;
command = parser_command_new(ft_strdup(token->value)); command = parser_command_new(ft_strdup(token->value));
command->args = parser_get_arguments(current); command->args = parser_get_arguments(current, minishell);
ft_lstadd_back(&command_list, ft_lstnew(command)); ft_lstadd_back(&command_list, ft_lstnew(command));
while (current && ((t_token *)current->content)->type == T_WORD) while (current && ((t_token *)current->content)->type < 3)
current = current->next; current = current->next;
if (current && ((t_token *)current->content)->type != T_WORD) if (current && ((t_token *)current->content)->type >= 3)
current = current->next; current = current->next;
} }
//ft_lstiter(command_list, print_cmds); // ft_lstiter(command_list, print_commands);
return (command_list); return (command_list);
} }

View File

@ -6,13 +6,13 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/05 19:10:49 by qmennen #+# #+# */ /* Created: 2025/02/05 19:10:49 by qmennen #+# #+# */
/* Updated: 2025/02/05 19:11:32 by qmennen ### ########.fr */ /* Updated: 2025/02/18 17:01:52 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
t_token *ft_token_new(t_token_type type, char *c, int pos) t_token *token_new(t_token_type type, char *c, int pos)
{ {
t_token *token; t_token *token;

View File

@ -6,39 +6,69 @@
/* 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/11 17:38:51 by qmennen ### ########.fr */ /* Updated: 2025/02/18 17:02:17 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
t_token *ft_parse_token(t_lexer *lexer) static t_token_type token_from_char(char c, int is_double)
{ {
t_token *token; 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);
}
token = NULL; static char *char_from_type(t_token_type type)
if (lexer->current_char == '|') {
{ if (type == T_HEREDOC)
token = ft_token_new(T_PIPE, "|", lexer->pos); return ("<<");
} else if (type == T_REDIRECT_IN)
else if (lexer->current_char == '<' && lexer->input[lexer->pos + 1] == '<') return ("<");
{ else if (type == T_APPEND_OUT)
token = ft_token_new(T_HEREDOC, "<<", lexer->pos); return (">>");
ft_lexer_readchar(lexer); else if (type == T_REDIRECT_OUT)
} return (">");
else if (lexer->current_char == '<') else if (type == T_AND)
{ return ("&&");
token = ft_token_new(T_REDIRECT_IN, "<", lexer->pos); else if (type == T_OR)
} return ("||");
else if (lexer->current_char == '>' && lexer->input[lexer->pos + 1] == '>') else if (type == T_PIPE)
{ return ("|");
token = ft_token_new(T_APPEND_OUT, ">>", lexer->pos); return (NULL);
ft_lexer_readchar(lexer); }
}
else if (lexer->current_char == '>') t_token *token_parse(t_lexer *lexer)
{ {
token = ft_token_new(T_REDIRECT_OUT, ">", lexer->pos); int is_double;
} char c;
ft_lexer_readchar(lexer); t_token *token;
t_token_type type;
c = lexer->current_char;
is_double = lexer->input[lexer->pos + 1] == c;
type = token_from_char(c, is_double);
token = token_new(type, char_from_type(type), lexer->pos);
if (is_double)
lexer_readchar(lexer);
lexer_readchar(lexer);
return (token); return (token);
} }