aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristopher Uhlig2025-01-25 13:01:10 +0100
committerChristopher Uhlig2025-01-25 13:01:10 +0100
commit87b90103930d83d74baa998866b0995cb8887d51 (patch)
tree0eb876341c2794f2165b83c1d339fb3b38e9f711 /src
parent18fb7cb8ae69fc3439266a154aa6b0f947d6805d (diff)
downloadminishell-87b90103930d83d74baa998866b0995cb8887d51.tar.gz
minishell-87b90103930d83d74baa998866b0995cb8887d51.zip
fixed leaks in tokenizer and collectargs also fixed seg for < > and improved value add by $ use
Diffstat (limited to 'src')
-rw-r--r--src/builtins_part_three.c2
-rw-r--r--src/collect_redirs.c26
-rw-r--r--src/debug_tools.c20
-rw-r--r--src/execute_cmd.c11
-rw-r--r--src/format_string.c12
-rw-r--r--src/free_node.c117
-rw-r--r--src/free_token.c29
-rw-r--r--src/get_cmd_path.c29
-rw-r--r--src/main.c2
-rw-r--r--src/new_token.c5
-rw-r--r--src/parse_cmd.c99
-rw-r--r--src/parser.c44
-rw-r--r--src/praise_the_norme.c18
-rw-r--r--src/repl.c43
-rw-r--r--src/tokenizer.c8
15 files changed, 137 insertions, 328 deletions
diff --git a/src/builtins_part_three.c b/src/builtins_part_three.c
index 3b9b100..e7d74a6 100644
--- a/src/builtins_part_three.c
+++ b/src/builtins_part_three.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/18 18:29:24 by chuhlig #+# #+# */
-/* Updated: 2025/01/21 16:15:19 by chuhlig ### ########.fr */
+/* Updated: 2025/01/24 17:42:49 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/collect_redirs.c b/src/collect_redirs.c
index 8f5dadc..860d9bc 100644
--- a/src/collect_redirs.c
+++ b/src/collect_redirs.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */
-/* Updated: 2025/01/23 18:22:04 by chuhlig ### ########.fr */
+/* Updated: 2025/01/25 11:40:52 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,7 +21,7 @@ static int set_heredoc_data(t_token *cur, t_redirection *result,
t_env *env);
t_redirection *collect_redirs(t_token **tokens, t_env *env,
- t_list **create_files)// tokes is possition after pipe or first token
+ t_list **create_files)
{
t_redirection *result;
t_token *cur;
@@ -31,15 +31,14 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env,
result = malloc(sizeof(t_redirection) * 2);
if (result == NULL)
return (free_tokens(*tokens), NULL);
- free(set_redir(&result[0], 0, NULL, env));// no token use
- free(set_redir(&result[1], 0, NULL, env));// no token use
+ free(set_redir(&result[0], 0, NULL, env));
+ free(set_redir(&result[1], 0, NULL, env));
data.create_files = create_files;
data.env = env;
while (cur != NULL)
{
- if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)//could this be a problem with recursion?
- collect_and_check_redir(result, &cur, &data, tokens);// cur=token is just in there to free and connect dont know if its ok
- //return or better said does
+ if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)
+ collect_and_check_redir(result, &cur, &data, tokens);
else if (cur->type == REDIR_TOKEN)
return (free(result), NULL);
else
@@ -57,21 +56,20 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur,
str = ft_strdup((*cur)->next->content.string);
if ((*cur)->content.redir_type == INPUT_LIMITER)
{
- if (!set_heredoc_data(*cur, result, data->env))// set here doc data with token head or first token after pipe
+ if (!set_heredoc_data(*cur, result, data->env))
return ;
}
else if ((*cur)->content.redir_type == INPUT_FILE)
q4fc(data->create_files, set_redir(&result[0], INPUT_FILE,
- format_string(str, data->env, 0), data->env));//here toke list should be not messed up
+ format_string(str, data->env, 0), data->env));
else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE)
q4fc(data->create_files, set_redir(&result[1], OUTPUT_OVERRIDE,
format_string(str, data->env, 0), data->env));
- else if ((*cur)->content.redir_type == OUTPUT_APPEND)
+ else if ((*cur)->content.redir_type == OUTPUT_APPEND)
q4fc(data->create_files, set_redir(&result[1], OUTPUT_APPEND,
format_string(str, data->env, 0), data->env));
- i_love_the_norme(cur, tokens);// takes adress of token head or pos after pipe and the token specifer
-}// i love the norm simple does free token and connect
-//later more detailes
+ i_love_the_norme(cur, tokens);
+}
static t_redirection *set_redir(t_redirection *redir, int type, char *spec,
t_env *env)
@@ -79,8 +77,6 @@ static t_redirection *set_redir(t_redirection *redir, int type, char *spec,
t_redirection *result;
redir->type = type;
- // if (redir->specifier != NULL)
- // free(redir->specifier);
if (spec != NULL)
redir->specifier = format_string(spec, env, ft_atoi("0"));
else
diff --git a/src/debug_tools.c b/src/debug_tools.c
index 2c8ee7e..3c38cd6 100644
--- a/src/debug_tools.c
+++ b/src/debug_tools.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 15:34:14 by dkaiser #+# #+# */
-/* Updated: 2025/01/23 15:21:11 by chuhlig ### ########.fr */
+/* Updated: 2025/01/25 11:39:59 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -35,21 +35,3 @@ void panic(char *msg)
ft_putendl_fd("\e[0m", 1);
}
}
-
-
-
-void print_token_list(t_token *token_list) {
- t_token *current = token_list;
- while (current != NULL) {
- if (current->type == STRING_TOKEN) {
- printf("STRING_TOKEN: %s\n", current->content.string);
- } else if (current->type == REDIR_TOKEN) {
- printf("REDIR_TOKEN: %d\n", current->content.redir_type);
- } else if (current->type == PIPE_TOKEN) {
- printf("PIPE_TOKEN\n");
- } else if (current->type == NEWLINE_TOKEN) {
- printf("NEWLINE_TOKEN\n");
- }
- current = current->next;
- }
-}
diff --git a/src/execute_cmd.c b/src/execute_cmd.c
index 811c92c..9f00522 100644
--- a/src/execute_cmd.c
+++ b/src/execute_cmd.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */
-/* Updated: 2025/01/22 16:53:04 by dkaiser ### ########.fr */
+/* Updated: 2025/01/25 11:41:42 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -17,14 +17,6 @@
static void establish_pipeline(int original_stdin, int original_stdout);
static int exec_cmd(t_cmd *cmd, t_env **env, int original_std[2], int result);
-int invalid_input(char *cmd)
-{
- ft_putstr_fd("minishell: ", STDERR_FILENO);
- ft_putstr_fd(cmd, STDERR_FILENO);
- ft_putstr_fd(": command not found\n", STDERR_FILENO);
- return (127);
-}
-
int is_builtin(char *cmd)
{
return ((ft_strcmp(cmd, "export") == 0) || (ft_strcmp(cmd, "unset") == 0)
@@ -104,7 +96,6 @@ static int exec_cmd(t_cmd *cmd, t_env **env, int original_std[2], int result)
cmd_path = get_cmd_path(cmd->args[i], *env, &result);
if (cmd_path != NULL)
execve(cmd_path, &(cmd->args[i]), env_to_strlst(*env));
- // free(cmd_path);
exit(result);
}
waitpid(pid, &status, 0);
diff --git a/src/format_string.c b/src/format_string.c
index 06e5210..e649fdb 100644
--- a/src/format_string.c
+++ b/src/format_string.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:30:11 by chuhlig #+# #+# */
-/* Updated: 2025/01/23 14:34:35 by chuhlig ### ########.fr */
+/* Updated: 2025/01/25 11:57:27 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -49,11 +49,8 @@ static void append_slice(char **dst, char *src, int start, int end)
i++;
}
result[len + i] = '\0';
- // if (*dst != NULL)
- // free(*dst);
+ free(*dst);
*dst = result;
- // free(src);
- // src = *dst;
}
static void append_var(char **dst, char *src, int *pos, t_env *env)
@@ -73,6 +70,8 @@ static void append_var(char **dst, char *src, int *pos, t_env *env)
value = env_get(env, var);
if (value)
{
+ while (*value == ' ')
+ value++;
result = ft_strjoin(*dst, value);
free(*dst);
*dst = result;
@@ -81,7 +80,8 @@ static void append_var(char **dst, char *src, int *pos, t_env *env)
free(var);
}
-static void handle_dollar_sign(char **result, char *str, int *pos, t_env *env)
+static void handle_dollar_sign(char **result, char *str, int *pos,
+ t_env *env)
{
if (str[*pos + 1] == '?')
{
diff --git a/src/free_node.c b/src/free_node.c
index 733fe1c..e62d6cb 100644
--- a/src/free_node.c
+++ b/src/free_node.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 11:41:46 by dkaiser #+# #+# */
-/* Updated: 2025/01/22 16:00:36 by chuhlig ### ########.fr */
+/* Updated: 2025/01/25 11:39:01 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,96 +16,49 @@ static void free_pipe_node(t_node *node);
static void free_cmd_node(t_node *node);
static void free_file(void *arg);
-// void free_node(t_node *node)
-// {
-// if (node->type == PIPE_NODE)
-// free_pipe_node(node);
-// else if (node->type == CMD_NODE)
-// free_cmd_node(node);
-// else if (node->type == STRING_NODE)
-// free(node->content.string);
-// else
-// panic(UNREACHABLE);
-// free(node);
-// }
-
-// static void free_pipe_node(t_node *node)
-// {
-// free_node(node->content.pipe.left);
-// free_node(node->content.pipe.right);
-// }
-
-// static void free_cmd_node(t_node *node)
-// {
-// int i;
-
-// i = 0;
-// while (node->content.cmd.args != NULL && node->content.cmd.args[i] != NULL)
-// {
-// free(node->content.cmd.args[i]);
-// i++;
-// }
-// free(node->content.cmd.args);
-// if (node->content.cmd.redirs[0].type != 0
-// && node->content.cmd.redirs[0].specifier != NULL)
-// free(node->content.cmd.redirs[0].specifier);
-// if (node->content.cmd.redirs[1].type != 0
-// && node->content.cmd.redirs[0].specifier != NULL)
-// free(node->content.cmd.redirs[1].specifier);
-// if (node->content.cmd.create_files != NULL)
-// ft_lstclear(&node->content.cmd.create_files, free_file);
-// }
-
-// static void free_file(void *arg)
-// {
-// t_redirection *file;
-
-// file = (t_redirection *)arg;
-// free(file->specifier);
-// free(file);
-// }
-
-void free_node(t_node *node)
+void free_node(t_node *node)
{
- if (node->type == PIPE_NODE)
- free_pipe_node(node);
- else if (node->type == CMD_NODE)
- free_cmd_node(node);
- else if (node->type == STRING_NODE)
- free(node->content.string);
- free(node);
+ if (node->type == PIPE_NODE)
+ free_pipe_node(node);
+ else if (node->type == CMD_NODE)
+ free_cmd_node(node);
+ else if (node->type == STRING_NODE)
+ free(node->content.string);
+ free(node);
}
-static void free_pipe_node(t_node *node)
+static void free_pipe_node(t_node *node)
{
- free_node(node->content.pipe.left);
- free_node(node->content.pipe.right);
+ free_node(node->content.pipe.left);
+ free_node(node->content.pipe.right);
}
-static void free_cmd_node(t_node *node)
+static void free_cmd_node(t_node *node)
{
- int i;
+ int i;
- i = 0;
- while (node->content.cmd.args != NULL && node->content.cmd.args[i] != NULL)
- {
- free(node->content.cmd.args[i]);
- i++;
- }
- free(node->content.cmd.args);
- if (node->content.cmd.redirs[0].type != 0 && node->content.cmd.redirs[0].specifier != NULL)
- free(node->content.cmd.redirs[0].specifier);
- if (node->content.cmd.redirs[1].type != 0 && node->content.cmd.redirs[1].specifier != NULL)
- free(node->content.cmd.redirs[1].specifier);
- if (node->content.cmd.create_files != NULL)
- ft_lstclear(&node->content.cmd.create_files, free_file);
+ i = 0;
+ while (node->content.cmd.args != NULL && node->content.cmd.args[i] != NULL)
+ {
+ free(node->content.cmd.args[i]);
+ i++;
+ }
+ free(node->content.cmd.args);
+ if (node->content.cmd.redirs[0].type != 0
+ && node->content.cmd.redirs[0].specifier != NULL)
+ free(node->content.cmd.redirs[0].specifier);
+ if (node->content.cmd.redirs[1].type != 0
+ && node->content.cmd.redirs[1].specifier != NULL)
+ free(node->content.cmd.redirs[1].specifier);
+ if (node->content.cmd.create_files != NULL)
+ ft_lstclear(&node->content.cmd.create_files, free_file);
}
-static void free_file(void *arg)
+static void free_file(void *arg)
{
- t_redirection *file;
+ t_redirection *file;
- file = (t_redirection *)arg;
- free(file->specifier);
- free(file);
-} \ No newline at end of file
+ file = (t_redirection *)arg;
+ free(file->specifier);
+ free(file);
+}
diff --git a/src/free_token.c b/src/free_token.c
index 2292eb0..a63e750 100644
--- a/src/free_token.c
+++ b/src/free_token.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 14:38:57 by dkaiser #+# #+# */
-/* Updated: 2025/01/22 17:28:31 by dkaiser ### ########.fr */
+/* Updated: 2025/01/25 11:36:59 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,12 +19,7 @@ void free_token(t_token *token)
token->previous->next = NULL;
if (token->next != NULL)
token->next->previous = NULL;
- // if (token->previous == NULL && token->next == NULL)
- // {
- // if (token->type == STRING_TOKEN && token->content.string != NULL)
- // free(token->content.string); // Ensure content is freed
- // }
- free(token);//maybe free token
+ free(token);
token = NULL;
}
@@ -34,13 +29,12 @@ void free_token2(t_token *token)
token->previous->next = NULL;
if (token->next != NULL)
token->next->previous = NULL;
- if (token->type == STRING_TOKEN && token->content.string != NULL)
- free(token->content.string); // Ensure content is freed
- free(token);//maybe free token
+ if (token->type == STRING_TOKEN && token->content.string != NULL)
+ free(token->content.string);
+ free(token);
token = NULL;
}
-
void free_token_and_connect(t_token *token)
{
if (token->previous != NULL)
@@ -58,7 +52,7 @@ void free_token_and_connect2(t_token *token)
if (token->next != NULL)
token->next->previous = token->previous;
if (token->type == STRING_TOKEN && token->content.string != NULL)
- free(token->content.string); // Ensure content is freed
+ free(token->content.string);
free(token);
token = NULL;
}
@@ -72,14 +66,3 @@ void free_tokens(t_token *tokens)
}
free_token2(tokens);
}
-// void free_tokens(t_token *tokens)
-// {
-// t_token *tmp;
-
-// while (tokens)
-// {
-// tmp = tokens;
-// tokens = tokens->next;
-// free_token(tmp); // Ensure each token is freed
-// }
-// }
diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c
index f882734..8075a5f 100644
--- a/src/get_cmd_path.c
+++ b/src/get_cmd_path.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */
-/* Updated: 2025/01/22 16:59:48 by dkaiser ### ########.fr */
+/* Updated: 2025/01/25 11:36:28 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -66,8 +66,7 @@ static char *find_in_path(char *cmd, t_env *env, int *return_code)
char *cur_path;
char *cmd_path;
char **path;
- char **path_start;
-
+ char **path_start;
path = get_split_path(env);
path_start = path;
@@ -94,30 +93,6 @@ static char *find_in_path(char *cmd, t_env *env, int *return_code)
return (NULL);
}
-// static char *get_simple_cmd_path(char *cmd, int *return_code)
-// {
-// char *result;
-
-// result = ft_strdup(cmd);
-// if (!result)
-// return (NULL);
-// if (access(result, F_OK) == -1)
-// {
-// free(result);
-// return (error(ENOENT, cmd, 127, return_code));
-// }
-// if (access(result, X_OK) == -1)
-// {
-// free(result);
-// return (error(EACCES, cmd, 126, return_code));
-// }
-// if (is_directory(cmd))
-// {
-// free(result);
-// return (error(EISDIR, cmd, 126, return_code));
-// }
-// return (result);
-// }
static char *get_simple_cmd_path(char *cmd, int *return_code)
{
char *result;
diff --git a/src/main.c b/src/main.c
index e863f3a..6fbe58c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */
-/* Updated: 2025/01/22 01:49:12 by chuhlig ### ########.fr */
+/* Updated: 2025/01/25 10:25:42 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/new_token.c b/src/new_token.c
index 6f49681..c9bd28c 100644
--- a/src/new_token.c
+++ b/src/new_token.c
@@ -6,11 +6,12 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 14:29:44 by dkaiser #+# #+# */
-/* Updated: 2025/01/22 00:41:47 by chuhlig ### ########.fr */
+/* Updated: 2025/01/24 18:46:05 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "token.h"
+#include "libft.h"
t_token *new_token(int type, t_token *previous, t_token *next)
{
@@ -39,7 +40,7 @@ t_token *new_str_token(char *str, t_token *previous, t_token *next)
free(str);
return (NULL);
}
- token->content.string = str;
+ token->content.string = ft_strdup(str);
return (token);
}
diff --git a/src/parse_cmd.c b/src/parse_cmd.c
index a978c99..5f9e36d 100644
--- a/src/parse_cmd.c
+++ b/src/parse_cmd.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */
-/* Updated: 2025/01/23 18:04:50 by chuhlig ### ########.fr */
+/* Updated: 2025/01/25 11:36:09 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,16 +21,10 @@ t_node *parse_cmd(t_token *tokens, t_env **env)
t_list *create_files;
create_files = NULL;
- printf("parse_cmd\n");
- print_token_list(tokens);
- redirs = collect_redirs(&tokens, *env, &create_files);// takes pos next to pipe or first token
- printf("parse_cmdafter collect redir\n");
- print_token_list(tokens);// still existing tokenlist
+ redirs = collect_redirs(&tokens, *env, &create_files);
if (redirs == NULL)
return (NULL);
args = collect_args(&tokens, env);
- printf("parse_cmdafter collect args\n");
- // print_token_list(tokens);//here it gone
if (args == NULL)
{
free(redirs);
@@ -39,64 +33,33 @@ t_node *parse_cmd(t_token *tokens, t_env **env)
return (new_cmd_node(args, redirs, create_files));
}
-// static char **collect_args(t_token **tokens, t_env **env)
-// {
-// t_token *cur;
-// char **result;
-// int i;
-// t_token *next;
-
-// cur = *tokens;
-// i = 0;
-// while (cur != NULL && ++i)
-// cur = cur->next;
-// result = malloc(sizeof(char *) * (i + 1));
-// if (result == NULL)
-// return (free_tokens(*tokens), NULL);
-// cur = *tokens;
-// i = 0;
-// while (cur != NULL && cur->type == STRING_TOKEN)
-// {
-// next = cur->next;
-// if (cur->previous)
-// free_token(cur->previous);
-// result[i] = format_string(cur->content.string, *env, ft_atoi("0"));
-// i++;
-// if (cur->next == NULL)
-// free_token(cur);
-// cur = next;
-// }
-// result[i] = NULL;
-// return (result);
-// }
-
-static char **collect_args(t_token **tokens, t_env **env) {
- t_token *cur;
- char **result;
- int i;
- t_token *next;
+static char **collect_args(t_token **tokens, t_env **env)
+{
+ t_token *cur;
+ char **result;
+ int i;
+ t_token *next;
- cur = *tokens;
- i = 0;
- while (cur != NULL && ++i)
- cur = cur->next;
- result = malloc(sizeof(char *) * (i + 1));
- if (result == NULL)
- return (free_tokens(*tokens), NULL);
- cur = *tokens;
- i = 0;
- while (cur != NULL && cur->type == STRING_TOKEN) {
- next = cur->next;
- result[i] = format_string(cur->content.string, *env, ft_atoi("0"));
- i++;
- if (cur->previous)
- cur->previous->next = cur->next;
- if (cur->next)
- cur->next->previous = cur->previous;
- free_token(cur);
- cur = next;
- }
- result[i] = NULL;
- *tokens = cur; // Update the head of the token list
- return (result);
-}// need to later the rest of the tokenlist
+ cur = *tokens;
+ i = 0;
+ while (cur != NULL && ++i)
+ cur = cur->next;
+ result = malloc(sizeof(char *) * (i + 1));
+ if (result == NULL)
+ return (free_tokens(*tokens), NULL);
+ cur = *tokens;
+ i = 0;
+ while (cur != NULL && cur->type == STRING_TOKEN)
+ {
+ next = cur->next;
+ if (cur->previous)
+ free_token2(cur->previous);
+ result[i] = format_string(cur->content.string, *env, ft_atoi("0"));
+ i++;
+ if (cur->next == NULL)
+ free_token2(cur);
+ cur = next;
+ }
+ result[i] = NULL;
+ return (result);
+}
diff --git a/src/parser.c b/src/parser.c
index 68ab372..8393cda 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/29 15:53:29 by dkaiser #+# #+# */
-/* Updated: 2025/01/23 18:00:18 by chuhlig ### ########.fr */
+/* Updated: 2025/01/25 11:38:47 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,28 +21,30 @@ static t_node *parse_statement(t_token *tokens, t_env **env);
t_node *parse(t_token *tokens, t_env **env)
{
- t_node *result;//
+ t_node *result;
- if ((*tokens).type == PIPE_TOKEN)
+ if ((*tokens).type == PIPE_TOKEN
+ || ((*tokens).type == REDIR_TOKEN && !(*tokens).next))
+ {
result = NULL;
+ free_tokens(tokens);
+ }
else
result = parse_statement(tokens, env);
if (result == NULL)
+ {
printf("Parsing error.\n");
- // if (tokens != NULL)
- // print_token_list(tokens);
+ free_tokens(tokens);
+ }
return (result);
}
static t_node *parse_statement(t_token *tokens, t_env **env)
{
t_token *left_side_tokens;
-
- print_token_list(tokens);//until her is fine
- left_side_tokens = split_at_first(&tokens, PIPE_TOKEN);//by pipe usage parse cmd gets reacls so also check there the token list changes
- //leftside toke has the pos of pipe -> next
- //or if we have pipe we return token head and also change the *tokens to after pipe
- if (left_side_tokens == NULL)// we never return NULL exept token is already null
+
+ left_side_tokens = split_at_first(&tokens, PIPE_TOKEN);
+ if (left_side_tokens == NULL)
{
free_tokens(tokens);
tokens = NULL;
@@ -51,13 +53,12 @@ static t_node *parse_statement(t_token *tokens, t_env **env)
else if (tokens != NULL)
{
return (new_pipe_node(parse_cmd(left_side_tokens, env),
- parse_statement(tokens, env)));//here new pipe node
+ parse_statement(tokens, env)));
}
else
{
- print_token_list(left_side_tokens);
- return (parse_cmd(left_side_tokens, env));//here return is cmd node
- }// here he takt left side token so
+ return (parse_cmd(left_side_tokens, env));
+ }
}
t_token *split_at_first(t_token **tokens, int type)
@@ -65,24 +66,21 @@ t_token *split_at_first(t_token **tokens, int type)
t_token *split;
t_token *result;
- split = find_token_by_type(*tokens, type);//split has the pos of where pipe appears// if no pipe in tokenlist 1st if case
+ split = find_token_by_type(*tokens, type);
if (split == NULL)
{
result = *tokens;
- *tokens = NULL;//we are change to pointing token to NULL
+ *tokens = NULL;
return (result);
}
result = *tokens;
- *tokens = split->next;// is this part enought reconnetion
+ *tokens = split->next;
if (result == split)
result = NULL;
- free_token2(split);//why free here? bv would free pipe node but what is with the connection of the tokenlist does this mess up the connection?
+ free_token2(split);
split = NULL;
- return (result);// at this return return is at tokenlist pos of split next and result is the tokenlist before split
+ return (result);
}
-//free token seems not right here even
-// or at least no the right funtion here
-//
static t_token *find_token_by_type(t_token *tokens, int type)
{
diff --git a/src/praise_the_norme.c b/src/praise_the_norme.c
index 5639b7d..7b72172 100644
--- a/src/praise_the_norme.c
+++ b/src/praise_the_norme.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/20 18:35:41 by dkaiser #+# #+# */
-/* Updated: 2025/01/23 18:31:49 by chuhlig ### ########.fr */
+/* Updated: 2025/01/25 11:34:26 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,19 +16,15 @@ void i_love_the_norme(t_token **cur, t_token **tokens)
{
t_token *next_token;
- next_token = (*cur)->next;//setting next token to the adress of the next token
- free_token_and_connect(*cur);// do i neee a double call here
- //but technically it would remove the redir token
- if (next_token)// if after redir is stuff
+ next_token = (*cur)->next;
+ free_token_and_connect2(*cur);
+ if (next_token)
{
- if (next_token->previous == NULL)// then if is the first token or token after pipe
- *tokens = next_token->next;// how does here come no error
- //anyways i twould the redir adress token to the next token
+ if (next_token->previous == NULL)
+ *tokens = next_token->next;
*cur = next_token->next;
free_token_and_connect(next_token);
}
- else // else makes sense
+ else
*cur = NULL;
}
-
-// takes adress of token head or pos after pipe and the token specifer \ No newline at end of file
diff --git a/src/repl.c b/src/repl.c
index 3988a42..93874a7 100644
--- a/src/repl.c
+++ b/src/repl.c
@@ -6,48 +6,17 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */
-/* Updated: 2025/01/23 17:42:33 by chuhlig ### ########.fr */
+/* Updated: 2025/01/25 12:41:48 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "../include/minishell.h"
#include "token.h"
-t_token *shallow_copy_token(t_token *token)
-{
- if (token == NULL)
- return (NULL);
-
- if (token->type == STRING_TOKEN)
- return (new_str_token(token->content.string, NULL, NULL));
- else if (token->type == REDIR_TOKEN)
- return (new_redir_token(token->content.redir_type, NULL, NULL));
- else
- return (new_redir_token(token->type, NULL, NULL));
-}
-
-t_token *shallow_copy_tokens(t_token *tokens)
-{
- t_token *result;
- t_token *cur;
-
- result = shallow_copy_token(tokens);
- if (!result)
- return (NULL);
- cur = result;
- while (tokens->next != NULL)
- {
- tokens = tokens->next;
- cur->next = shallow_copy_tokens(tokens);
- cur = cur->next;
- }
- return (result);
-}
-
void free_repl(char *input, t_node *ast)
{
free(input);
- if(ast)
+ if (ast)
free_node(ast);
}
@@ -56,7 +25,6 @@ void repl(const char *prompt, t_env **env, int *promptflag)
char *input;
t_token *token_list;
t_node *ast;
- t_token *tokens_copy;
(*promptflag)++;
while (1)
@@ -70,17 +38,16 @@ void repl(const char *prompt, t_env **env, int *promptflag)
break ;
}
if (input[0] == '\0')
+ {
+ free(input);
continue ;
+ }
add_history(input);
token_list = NULL;
tokenizer(input, &token_list, '\0');
- tokens_copy = shallow_copy_tokens(token_list);
- free_tokens(tokens_copy);
- // print_token_list(token_list);
ast = parse(token_list, env);
if (ast)
set_return_code(eval(ast, env), env);
free_repl(input, ast);
- print_token_list(tokens_copy);
}
}
diff --git a/src/tokenizer.c b/src/tokenizer.c
index 451fa50..90cb521 100644
--- a/src/tokenizer.c
+++ b/src/tokenizer.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */
-/* Updated: 2025/01/22 00:49:10 by chuhlig ### ########.fr */
+/* Updated: 2025/01/25 11:30:58 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -60,12 +60,15 @@ void snap_string_token(char *string, int start_of_string, int i,
t_token **token_list)
{
char *line;
+ char *original;
int len;
+ line = NULL;
len = i - start_of_string + 1;
if (len > 0)
{
- line = (char *)malloc(len + 1);
+ line = (char *)malloc((sizeof(char) * len + 1));
+ original = line;
if (!line)
{
exit(EXIT_FAILURE);
@@ -79,6 +82,7 @@ void snap_string_token(char *string, int start_of_string, int i,
*token_list = new_str_token(line, *token_list, NULL);
print_token(*token_list);
}
+ free(original);
}
}