diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/builtins_part_three.c | 2 | ||||
| -rw-r--r-- | src/builtins_part_two.c | 6 | ||||
| -rw-r--r-- | src/collect_redirs.c | 11 | ||||
| -rw-r--r-- | src/create_files.c | 8 | ||||
| -rw-r--r-- | src/debug_tools.c | 2 | ||||
| -rw-r--r-- | src/error.c | 9 | ||||
| -rw-r--r-- | src/execute_cmd.c | 4 | ||||
| -rw-r--r-- | src/format_string.c | 10 | ||||
| -rw-r--r-- | src/free_node.c | 20 | ||||
| -rw-r--r-- | src/free_token.c | 30 | ||||
| -rw-r--r-- | src/get_cmd_path.c | 14 | ||||
| -rw-r--r-- | src/interpreter.c | 5 | ||||
| -rw-r--r-- | src/main.c | 4 | ||||
| -rw-r--r-- | src/new_token.c | 6 | ||||
| -rw-r--r-- | src/parse_cmd.c | 22 | ||||
| -rw-r--r-- | src/parser.c | 17 | ||||
| -rw-r--r-- | src/praise_the_norme.c | 6 | ||||
| -rw-r--r-- | src/repl.c | 26 | ||||
| -rw-r--r-- | src/tokenizer.c | 8 |
19 files changed, 141 insertions, 69 deletions
diff --git a/src/builtins_part_three.c b/src/builtins_part_three.c index 5f6fa31..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/20 17:08:17 by chuhlig ### ########.fr */ +/* Updated: 2025/01/24 17:42:49 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/builtins_part_two.c b/src/builtins_part_two.c index e461861..ede48de 100644 --- a/src/builtins_part_two.c +++ b/src/builtins_part_two.c @@ -15,17 +15,14 @@ void update_oldpwd(t_env **env) { t_env *current; - t_env *prev; char cwd[1028]; char *tmp; - prev = NULL; current = *env; while (current) { if (ft_strncmp(current->name, "OLDPWD", 6) == 0) break ; - prev = current; current = current->next; } getcwd(cwd, sizeof(cwd)); @@ -37,17 +34,14 @@ void update_oldpwd(t_env **env) void update_pwd(t_env **env) { t_env *current; - t_env *prev; char cwd[1028]; char *tmp; - prev = NULL; current = *env; while (current) { if (ft_strncmp(current->name, "PWD", 3) == 0) break ; - prev = current; current = current->next; } getcwd(cwd, sizeof(cwd)); diff --git a/src/collect_redirs.c b/src/collect_redirs.c index f274053..c1b7047 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/20 18:39:24 by dkaiser ### ########.fr */ +/* Updated: 2025/01/25 14:37:06 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,8 +31,8 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env, result = malloc(sizeof(t_redirection) * 2); if (result == NULL) return (free_tokens(*tokens), NULL); - set_redir(&result[0], 0, NULL, env); - set_redir(&result[1], 0, NULL, env); + 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) @@ -44,8 +44,6 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env, else cur = cur->next; } - if (cur && cur->type == REDIR_TOKEN) - return (free(result), NULL); return (result); } @@ -55,7 +53,7 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, char *str; if ((*cur)->content.redir_type != INPUT_LIMITER) - str = ft_strdup((*cur)->next->content.string); + str = (*cur)->next->content.string; if ((*cur)->content.redir_type == INPUT_LIMITER) { if (!set_heredoc_data(*cur, result, data->env)) @@ -71,6 +69,7 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, q4fc(data->create_files, set_redir(&result[1], OUTPUT_APPEND, format_string(str, data->env, 0), data->env)); i_love_the_norme(cur, tokens); + free(str); } static t_redirection *set_redir(t_redirection *redir, int type, char *spec, diff --git a/src/create_files.c b/src/create_files.c index eee2860..8c04d8f 100644 --- a/src/create_files.c +++ b/src/create_files.c @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/16 16:23:51 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 18:30:40 by dkaiser ### ########.fr */ +/* Updated: 2025/01/21 13:17:47 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,7 +52,11 @@ static int cant_write(char *filename) static void create_file(char *filename, int mode) { - close(open(filename, O_WRONLY | O_CREAT | mode, 0644)); + int fd; + + fd = open(filename, O_WRONLY | O_CREAT | mode, 0644); + if (fd != -1) + close(fd); } void q4fc(t_list **queue, t_redirection *redir) diff --git a/src/debug_tools.c b/src/debug_tools.c index 6bee1b0..8668fad 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/20 12:50:36 by chuhlig ### ########.fr */ +/* Updated: 2025/01/25 11:39:59 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/error.c b/src/error.c index 2ca60b2..eebc21a 100644 --- a/src/error.c +++ b/src/error.c @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/15 16:35:53 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 18:12:40 by dkaiser ### ########.fr */ +/* Updated: 2025/01/25 14:58:52 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,9 +22,14 @@ void *error(int err_code, char *err_text, int exit_code, int *ret_code) return (NULL); } -void command_not_found_error(char *cmd) +void *command_not_found_error(char *cmd, int *return_code, char *cmd_path, + char **split_path) { ft_printf("%s:", cmd); ft_putstr_fd(" command not found", 2); ft_printf("\n"); + *return_code = 127; + free(cmd_path); + ft_free_split(split_path); + return (NULL); } diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 1438f9c..adfcb37 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/20 20:04:31 by chuhlig ### ########.fr */ +/* Updated: 2025/01/25 14:42:39 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,6 +49,8 @@ int execute_cmd(t_cmd *cmd, t_env **env) int original_std[2]; int result; + if (cmd->args == NULL || cmd->args[0] == NULL) + return (EXIT_FAILURE); original_std[1] = dup(STDOUT_FILENO); original_std[0] = dup(STDIN_FILENO); create_files(cmd->create_files); diff --git a/src/format_string.c b/src/format_string.c index 7e64039..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/19 20:24:05 by chuhlig ### ########.fr */ +/* Updated: 2025/01/25 11:57:27 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,8 +49,7 @@ 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; } @@ -71,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; @@ -79,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 6eae059..e62d6cb 100644 --- a/src/free_node.c +++ b/src/free_node.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* free_node.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:41:46 by dkaiser #+# #+# */ -/* Updated: 2024/08/11 12:26:20 by dkaiser ### ########.fr */ +/* Updated: 2025/01/25 11:39:01 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ 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) { @@ -23,8 +24,6 @@ void free_node(t_node *node) free_cmd_node(node); else if (node->type == STRING_NODE) free(node->content.string); - else - panic(UNREACHABLE); free(node); } @@ -49,6 +48,17 @@ static void free_cmd_node(t_node *node) && 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) + && 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) +{ + t_redirection *file; + + file = (t_redirection *)arg; + free(file->specifier); + free(file); } diff --git a/src/free_token.c b/src/free_token.c index 512ba23..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/20 12:49:48 by chuhlig ### ########.fr */ +/* Updated: 2025/01/25 11:36:59 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,18 @@ void free_token(t_token *token) token = NULL; } +void free_token2(t_token *token) +{ + if (token->previous != NULL) + 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); + free(token); + token = NULL; +} + void free_token_and_connect(t_token *token) { if (token->previous != NULL) @@ -33,12 +45,24 @@ void free_token_and_connect(t_token *token) token = NULL; } +void free_token_and_connect2(t_token *token) +{ + if (token->previous != NULL) + token->previous->next = token->next; + if (token->next != NULL) + token->next->previous = token->previous; + if (token->type == STRING_TOKEN && token->content.string != NULL) + free(token->content.string); + free(token); + token = NULL; +} + void free_tokens(t_token *tokens) { while (tokens->next != NULL) { tokens = tokens->next; - free_token(tokens->previous); + free_token2(tokens->previous); } - free_token(tokens); + free_token2(tokens); } diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c index 70043a7..aae80cd 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/20 18:12:33 by dkaiser ### ########.fr */ +/* Updated: 2025/01/25 14:58:45 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,8 +66,10 @@ static char *find_in_path(char *cmd, t_env *env, int *return_code) char *cur_path; char *cmd_path; char **path; + char **path_start; path = get_split_path(env); + path_start = path; cmd_path = NULL; while (*path) { @@ -75,18 +77,16 @@ static char *find_in_path(char *cmd, t_env *env, int *return_code) free(cmd_path); cur_path = ft_strjoin(*path, "/"); if (!cur_path) - return (NULL); + return (ft_free_split(path_start), NULL); cmd_path = ft_strjoin(cur_path, cmd); free(cur_path); if (!cmd_path) - return (NULL); + return (ft_free_split(path_start), NULL); if (access(cmd_path, X_OK) != -1) - return (cmd_path); + return (ft_free_split(path_start), cmd_path); path++; } - *return_code = 127; - command_not_found_error(cmd); - return (NULL); + return (command_not_found_error(cmd, return_code, cmd_path, path_start)); } static char *get_simple_cmd_path(char *cmd, int *return_code) diff --git a/src/interpreter.c b/src/interpreter.c index 9cd5292..979a3cf 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */ -/* Updated: 2025/01/20 20:07:11 by chuhlig ### ########.fr */ +/* Updated: 2025/01/22 15:20:18 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,10 @@ int eval_rec(t_node *node, t_env **env, int in_fd) else if (node->type == CMD_NODE) result = execute_cmd(&node->content.cmd, env); else + { result = EXIT_FAILURE; + free_node(node); + } return (result); } @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */ -/* Updated: 2025/01/14 18:41:15 by chuhlig ### ########.fr */ +/* Updated: 2025/01/25 15:41:04 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ int main(int argc, char *argv[], char *envp[]) return (1); getenvlst(&env, envp); set_return_code(0, &env); - repl("Minishell $ ", &env, &promptflag); + repl("Minishell $ ", &env); free_envlst(&env); return (0); } diff --git a/src/new_token.c b/src/new_token.c index bd65b9d..c5497b4 100644 --- a/src/new_token.c +++ b/src/new_token.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "token.h" +#include "libft.h" t_token *new_token(int type, t_token *previous, t_token *next) { @@ -35,8 +36,11 @@ t_token *new_str_token(char *str, t_token *previous, t_token *next) token = new_token(STRING_TOKEN, previous, next); if (token == NULL) + { + 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 6505384..578601c 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,13 +6,14 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 19:09:21 by chuhlig ### ########.fr */ +/* Updated: 2025/01/25 15:00:55 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" static char **collect_args(t_token **tokens, t_env **env); +static void setup_vars(t_token **tokens, t_token **cur, int *i); t_node *parse_cmd(t_token *tokens, t_env **env) { @@ -40,24 +41,29 @@ static char **collect_args(t_token **tokens, t_env **env) int i; t_token *next; - cur = *tokens; - i = 0; + setup_vars(tokens, &cur, &i); 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; + setup_vars(tokens, &cur, &i); 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++; + free_token2(cur->previous); + result[i++] = format_string(cur->content.string, *env, ft_atoi("0")); + if (cur->next == NULL) + free_token2(cur); cur = next; } result[i] = NULL; return (result); } + +static void setup_vars(t_token **tokens, t_token **cur, int *i) +{ + *cur = *tokens; + *i = 0; +} diff --git a/src/parser.c b/src/parser.c index 13ab10d..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/20 19:13:31 by chuhlig ### ########.fr */ +/* Updated: 2025/01/25 11:38:47 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,17 +19,24 @@ static t_token *find_token_by_type(t_token *tokens, int type); t_token *split_at_first(t_token **tokens, int type); static t_node *parse_statement(t_token *tokens, t_env **env); -t_list *parse(t_token *tokens, t_env **env) +t_node *parse(t_token *tokens, t_env **env) { 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"); - return (ft_lstnew(result)); + free_tokens(tokens); + } + return (result); } static t_node *parse_statement(t_token *tokens, t_env **env) @@ -70,7 +77,7 @@ t_token *split_at_first(t_token **tokens, int type) *tokens = split->next; if (result == split) result = NULL; - free_token(split); + free_token2(split); split = NULL; return (result); } diff --git a/src/praise_the_norme.c b/src/praise_the_norme.c index a22843b..7b72172 100644 --- a/src/praise_the_norme.c +++ b/src/praise_the_norme.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* praise_the_norme.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/20 18:35:41 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 18:39:31 by dkaiser ### ########.fr */ +/* Updated: 2025/01/25 11:34:26 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ void i_love_the_norme(t_token **cur, t_token **tokens) t_token *next_token; next_token = (*cur)->next; - free_token_and_connect(*cur); + free_token_and_connect2(*cur); if (next_token) { if (next_token->previous == NULL) @@ -6,18 +6,25 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 17:58:43 by dkaiser ### ########.fr */ +/* Updated: 2025/01/25 15:39:36 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "../include/minishell.h" #include "token.h" -void repl(const char *prompt, t_env **env, int *promptflag) +static void free_repl(char *input, t_node *ast) +{ + free(input); + if (ast) + free_node(ast); +} + +void repl(const char *prompt, t_env **env) { char *input; t_token *token_list; - t_list *lines; + t_node *ast; (*promptflag)++; while (1) @@ -25,19 +32,20 @@ void repl(const char *prompt, t_env **env, int *promptflag) input = readline(prompt); if (input == NULL) { - if (*promptflag > 1) - (*promptflag)--; printf("exit\n"); break ; } if (input[0] == '\0') + { + free(input); continue ; + } add_history(input); token_list = NULL; tokenizer(input, &token_list, '\0'); - lines = parse(token_list, env); - if (lines) - set_return_code(eval(lines->content, env), env); - free(input); + ast = parse(token_list, env); + if (ast) + set_return_code(eval(ast, env), env); + free_repl(input, ast); } } diff --git a/src/tokenizer.c b/src/tokenizer.c index e202de6..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: 2024/10/17 14:21:26 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); } } |
