diff options
| author | Christopher Uhlig | 2025-01-20 13:06:34 +0100 |
|---|---|---|
| committer | Christopher Uhlig | 2025-01-20 13:06:34 +0100 |
| commit | 8f5abcdb257393a2e576fe382835e8e060662834 (patch) | |
| tree | 1b923423b22bb59ff10ea07b0700a5a7bdf96f5a /src | |
| parent | 3392f2b811269f174620832d663b09ef4f4e43f3 (diff) | |
| download | minishell-8f5abcdb257393a2e576fe382835e8e060662834.tar.gz minishell-8f5abcdb257393a2e576fe382835e8e060662834.zip | |
merged
Diffstat (limited to 'src')
| -rw-r--r-- | src/builtins_part_one.c | 133 | ||||
| -rw-r--r-- | src/builtins_part_three.c | 76 | ||||
| -rw-r--r-- | src/builtins_part_two.c | 5 | ||||
| -rw-r--r-- | src/collect_redirs.c | 53 | ||||
| -rw-r--r-- | src/create_files.c | 16 | ||||
| -rw-r--r-- | src/debug_tools.c | 6 | ||||
| -rw-r--r-- | src/env_to_strlst.c | 29 | ||||
| -rw-r--r-- | src/execute_cmd.c | 2 | ||||
| -rw-r--r-- | src/format_string.c | 112 | ||||
| -rw-r--r-- | src/free_token.c | 7 | ||||
| -rw-r--r-- | src/get_cmd_path.c | 4 | ||||
| -rw-r--r-- | src/handle_redir.c | 103 | ||||
| -rw-r--r-- | src/interpreter.c | 89 | ||||
| -rw-r--r-- | src/new_node.c | 5 | ||||
| -rw-r--r-- | src/parse_cmd.c | 13 | ||||
| -rw-r--r-- | src/parser.c | 4 | ||||
| -rw-r--r-- | src/repl.c | 6 | ||||
| -rw-r--r-- | src/signal_handling.c | 2 |
18 files changed, 391 insertions, 274 deletions
diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index f3bcfc0..7cbcdeb 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,60 +6,13 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 19:51:59 by chuhlig ### ########.fr */ +/* Updated: 2025/01/18 18:33:33 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" #include <stdio.h> -int echo(char **av) -{ - int i; - int f; - - i = 1; - f = 1; - if (av[1] == NULL || av[1][0] == '\0') - { - write(1, "\n", 1); - return (0); - } - if (ft_strncmp(av[1], "-n", 3) == 0) - { - i++; - f = 0; - } - while (av[i]) - { - write(1, av[i], ft_strlen(av[i])); - i++; - if (av[i]) - write(1, " ", 1); - } - if (f) - write(1, "\n", 1); - return (0); -} - -void exit_shell(t_env **env, int exit_status) -{ - free_envlst(env); - exit(exit_status); -} - -int builtin_exit(char **args, t_env **env) -{ - int exit_status; - - if (args[1]) - exit_status = ft_atoi(args[1]); - else - exit_status = 0; - exit_shell(env, exit_status); - return (exit_status); -} - int unset(char **av, t_env **env) { t_env *current; @@ -69,8 +22,6 @@ int unset(char **av, t_env **env) i = 0; while (av[++i]) { - if (ft_strchr(av[i], '?')) - return (1); current = *env; prev = NULL; while (current) @@ -95,6 +46,8 @@ t_env *check_existing(t_env *env, char *av) { while (env) { + if (ft_strcmp("$", av) == 0) + return (NULL); if (ft_strcmp(env->name, av) == 0) return (env); env = env->next; @@ -102,51 +55,65 @@ t_env *check_existing(t_env *env, char *av) return (NULL); } -int export(char **av, t_env **env) +void export_export(char *av, t_env **env) { char *tmp; t_env *current; - int i; current = NULL; + tmp = ft_strchr(av, '='); + *tmp = '\0'; + current = check_existing(*env, av); + if (current) + free(current->value); + else + { + current = env_new(ft_strdup(av)); + current->next = *env; + *env = current; + } + current->value = ft_strdup(tmp + 1); +} + +int is_valid_identifier(char *str) +{ + int i; + i = 0; - while (av[++i]) + if (!ft_isalpha(str[0]) && str[0] != '_') + return (0); + while (str[i] && str[i] != '=') { - if ((ft_strchr(av[i], '='))) - { - tmp = ft_strchr(av[i], '='); - *tmp = '\0'; - if (ft_strchr(av[i], '?')) - return (1); - current = check_existing(*env, av[i]); - if (current) - free(current->value); - else - { - current = env_new(ft_strdup(av[i])); - current->next = *env; - *env = current; - } - current->value = ft_strdup(tmp + 1); - } - else - return (1); + if (!ft_isalnum(str[i]) && str[i] != '_') + return (0); + i++; } - return (0); + return (1); } -void set_return_code(int return_code, t_env **env) +int export(char **av, t_env **env) { - t_env *cur; + char *equal_sign; + int i; - cur = check_existing(*env, "?"); - if (cur) - free(cur->value); - else + i = 0; + while (av[++i]) { - cur = env_new(ft_strdup("?")); - cur->next = *env; - *env = cur; + equal_sign = ft_strchr(av[i], '='); + if (equal_sign) + *equal_sign = '\0'; + if (!is_valid_identifier(av[i])) + { + write(1, "Minishell $ export: not a valid identifier\n", 43); + if (equal_sign) + *equal_sign = '='; + continue ; + } + if (equal_sign) + { + *equal_sign = '='; + export_export(av[i], env); + } } - cur->value = ft_itoa(return_code); + return (0); } diff --git a/src/builtins_part_three.c b/src/builtins_part_three.c new file mode 100644 index 0000000..7c73e95 --- /dev/null +++ b/src/builtins_part_three.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtins_part_three.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/18 18:29:24 by chuhlig #+# #+# */ +/* Updated: 2025/01/18 18:34:29 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "env.h" + +void exit_shell(t_env **env, int exit_status) +{ + free_envlst(env); + exit(exit_status); +} + +int builtin_exit(char **av, t_env **env) +{ + int exit_status; + + if (av[1]) + exit_status = ft_atoi(av[1]); + else + exit_status = 0; + exit_shell(env, exit_status); + return (exit_status); +} + +void set_return_code(int return_code, t_env **env) +{ + t_env *cur; + + cur = check_existing(*env, "?"); + if (cur) + free(cur->value); + else + { + cur = env_new(ft_strdup("?")); + cur->next = *env; + *env = cur; + } + cur->value = ft_itoa(return_code); +} + +int echo(char **av) +{ + int i; + int f; + + i = 1; + f = 1; + if (av[1] == NULL || av[1][0] == '\0') + { + write(1, "\n", 1); + return (0); + } + if (ft_strncmp(av[1], "-n", 3) == 0) + { + i++; + f = 0; + } + while (av[i]) + { + write(1, av[i], ft_strlen(av[i])); + i++; + if (av[i]) + write(1, " ", 1); + } + if (f) + write(1, "\n", 1); + return (0); +} diff --git a/src/builtins_part_two.c b/src/builtins_part_two.c index 05d6943..9fabc81 100644 --- a/src/builtins_part_two.c +++ b/src/builtins_part_two.c @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/25 20:52:16 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 19:31:17 by chuhlig ### ########.fr */ +/* Updated: 2025/01/18 18:57:12 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,6 +72,7 @@ int cd(t_env **env, char **av) } if (chdir(current->value) == -1) return (1); + update_pwd(env); } else { @@ -101,7 +102,7 @@ int ft_env(t_env *env) { while (env != NULL) { - if (strchr(env->name, '?')) + if (ft_strchr(env->name, '?')) { env = env->next; continue ; diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 171dc06..c170a30 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/16 18:19:36 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 13:00:04 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,8 @@ static t_redirection *set_redir(t_redirection *redir, int type, char *spec, 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) +t_redirection *collect_redirs(t_token **tokens, t_env *env, + t_list **create_files) { t_redirection *result; t_token *cur; @@ -45,28 +46,6 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env, t_list **create_file return (result); } -static t_redirection *set_redir(t_redirection *redir, int type, char *spec, - t_env *env) -{ - t_redirection *result; - - redir->type = type; - if (spec != NULL) - redir->specifier = format_string(spec, env); - else - redir->specifier = spec; - if (redir->type == OUTPUT_APPEND || redir->type == OUTPUT_OVERRIDE) - { - result = malloc(sizeof(t_redirection)); - if (!result) - return (NULL); - result->type = type; - result->specifier = spec; - return (result); - } - return (NULL); -} - static void collect_and_check_redir(t_redirection *result, t_token **cur, t_env *env, t_list **create_files) { @@ -89,7 +68,7 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1], OUTPUT_APPEND, str, env))); next_token = (*cur)->next; - free_token_and_connect(*cur); + // free_token_and_connect(*cur); if (next_token) { *cur = next_token->next; @@ -99,6 +78,30 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, *cur = NULL; } +static t_redirection *set_redir(t_redirection *redir, int type, char *spec, + t_env *env) +{ + t_redirection *result; + + redir->type = type; + if (spec != NULL) + redir->specifier = format_string(spec, env, ft_atoi("0")); + else + redir->specifier = spec; + if (redir->type == OUTPUT_APPEND || redir->type == OUTPUT_OVERRIDE) + { + result = malloc(sizeof(t_redirection)); + if (!result) + return (NULL); + result->type = type; + result->specifier = spec; + return (result); + } + return (NULL); +} + + + static int set_heredoc_data(t_token *cur, t_redirection *result, t_env *env) { char *heredoc_data; diff --git a/src/create_files.c b/src/create_files.c index faee27f..8689f88 100644 --- a/src/create_files.c +++ b/src/create_files.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* create_files.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/16 16:23:51 by dkaiser #+# #+# */ -/* Updated: 2025/01/16 19:16:33 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 14:36:59 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,10 +22,10 @@ void create_files(t_list *files) { dbg("Test"); if (files->content == NULL) - continue; + continue ; file = (t_redirection *)files->content; if (access(file->specifier, F_OK) != -1 && access(file->specifier, W_OK) == -1) - break; + break ; if (file->type == OUTPUT_OVERRIDE) { fd = open(file->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); @@ -36,10 +36,10 @@ void create_files(t_list *files) fd = open(file->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); close(fd); } - /* if (files->next == NULL) */ - /* break; */ - /* if (((t_redirection *) files->next->content)->type == 0) */ - /* break; */ + if (files->next == NULL) + break ; + if (((t_redirection *) files->next->content)->type == 0) + break ; files = files->next; } } diff --git a/src/debug_tools.c b/src/debug_tools.c index de59703..6bee1b0 100644 --- a/src/debug_tools.c +++ b/src/debug_tools.c @@ -3,14 +3,16 @@ /* ::: :::::::: */ /* debug_tools.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 15:34:14 by dkaiser #+# #+# */ -/* Updated: 2024/06/28 15:04:43 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 12:50:36 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "debug_tools.h" +#include <stdio.h> +#include <stdarg.h> void dbg(char *msg) { diff --git a/src/env_to_strlst.c b/src/env_to_strlst.c index a1ab7cc..5806d96 100644 --- a/src/env_to_strlst.c +++ b/src/env_to_strlst.c @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:22:28 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 19:34:10 by chuhlig ### ########.fr */ +/* Updated: 2025/01/18 18:50:49 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,25 +15,32 @@ static char *get_var_assign(t_env *cur); -char **env_to_strlst(t_env *env) +static int getsize(t_env *env) { int size; t_env *cur; - char **result; - int i; size = 0; cur = env; - while (cur != NULL) + while (cur) { - if (ft_strchr(cur->name, '?')) - { - cur = cur->next; - continue ; - } - size++; + if (!ft_strchr(cur->name, '?')) + size++; cur = cur->next; } + return (size); +} + +char **env_to_strlst(t_env *env) +{ + int size; + t_env *cur; + char **result; + int i; + + size = 0; + cur = env; + size = getsize(env); result = malloc(sizeof(char *) * (size + 1)); if (result == NULL) return (NULL); diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 8f5b541..e2b9d66 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/16 18:38:00 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 19:15:46 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/format_string.c b/src/format_string.c index 5f31130..7e64039 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/14 18:06:17 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 20:24:05 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,46 +14,18 @@ #include "libft.h" #include "minishell.h" -static void append_slice(char **dst, char *src, int start, int end); -static void append_var(char **dst, char *src, int *pos, t_env *env); - -char *format_string(char *str, t_env *env) +void append_var_exit_code(char **dst, t_env *env) { + char *exit_code; char *result; - int pos; - int start; - int is_literal; - pos = 0; - start = 0; - is_literal = 0; - result = NULL; - if (str == NULL) - return (NULL); - while (str[pos] != '\0') + exit_code = env_get(env, "?"); + if (exit_code) { - if (str[pos] == '\'') - { - append_slice(&result, str, start, pos); - start = pos + 1; - is_literal = !is_literal; - } - if (str[pos] == '"' && !is_literal) - { - append_slice(&result, str, start, pos); - start = pos + 1; - } - if (str[pos] == '$' && !is_literal) - { - append_slice(&result, str, start, pos); - append_var(&result, str, &pos, env); - start = pos; - continue ; - } - pos++; + result = ft_strjoin(*dst, exit_code); + free(*dst); + *dst = result; } - append_slice(&result, str, start, pos); - return (result); } static void append_slice(char **dst, char *src, int start, int end) @@ -65,9 +37,7 @@ static void append_slice(char **dst, char *src, int start, int end) if (*dst != NULL) len = ft_strlen(*dst); else - { len = 0; - } result = malloc(len + (end - start) + 1); if (!result) return ; @@ -93,27 +63,63 @@ static void append_var(char **dst, char *src, int *pos, t_env *env) i = 0; *pos += 1; - while (src[*pos + i] != '\0' && src[*pos + i] != '\'' && src[*pos - + i] != '"' && src[*pos + i] != '$') - { + while (ft_isalnum(src[*pos + i]) || src[*pos + i] == '_') i++; - } - var = malloc(i + 1); - if (var == NULL) + if (i == 0) return ; - var[i] = '\0'; - i--; - while (i >= 0) - { - var[i] = src[*pos + i]; - i--; - } + var = ft_substr(src, *pos, i); value = env_get(env, var); - if (value != NULL) + if (value) { result = ft_strjoin(*dst, value); free(*dst); *dst = result; } - *pos += ft_strlen(var); + *pos += i; + free(var); +} + +static void handle_dollar_sign(char **result, char *str, int *pos, t_env *env) +{ + if (str[*pos + 1] == '?') + { + append_var_exit_code(result, env); + *pos += 2; + } + else if (ft_isalnum(str[*pos + 1]) || str[*pos + 1] == '_') + append_var(result, str, pos, env); + else + { + append_slice(result, str, *pos, *pos + 1); + (*pos)++; + } +} + +char *format_string(char *str, t_env *env, int is_literal) +{ + char *result; + int pos; + int start; + + pos = 0; + start = 0; + result = NULL; + if (!str) + return (NULL); + while (str[pos]) + { + if (str[pos] == '\'' || (str[pos] == '\"' && !is_literal) + || (str[pos] == '$' && !is_literal)) + { + append_slice(&result, str, start, pos); + if (str[pos] == '$') + handle_dollar_sign(&result, str, &pos, env); + else + is_literal ^= (str[pos++] == '\''); + start = pos; + continue ; + } + pos++; + } + return (append_slice(&result, str, start, pos), result); } diff --git a/src/free_token.c b/src/free_token.c index 9b035ac..512ba23 100644 --- a/src/free_token.c +++ b/src/free_token.c @@ -3,14 +3,15 @@ /* ::: :::::::: */ /* free_token.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 14:38:57 by dkaiser #+# #+# */ -/* Updated: 2024/08/02 14:23:56 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 12:49:48 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "token.h" +#include "debug_tools.h" void free_token(t_token *token) { @@ -19,6 +20,7 @@ void free_token(t_token *token) if (token->next != NULL) token->next->previous = NULL; free(token); + token = NULL; } void free_token_and_connect(t_token *token) @@ -28,6 +30,7 @@ void free_token_and_connect(t_token *token) if (token->next != NULL) token->next->previous = token->previous; free(token); + token = NULL; } void free_tokens(t_token *tokens) diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c index 543540b..c35b3bc 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/15 16:38:39 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 18:56:22 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -86,7 +86,7 @@ static char *find_in_path(char *cmd, t_env *env, int *return_code) } *return_code = 127; printf("%s:", cmd); - ft_putstr_fd(" command not found", 2); + ft_putstr_fd(" command not found", 2);//output is shit even if its fine for tester return (NULL); } diff --git a/src/handle_redir.c b/src/handle_redir.c new file mode 100644 index 0000000..29bba92 --- /dev/null +++ b/src/handle_redir.c @@ -0,0 +1,103 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* handle_redir.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/18 18:34:51 by chuhlig #+# #+# */ +/* Updated: 2025/01/18 18:47:31 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int handle_input_redirection(t_redirection *redir) +{ + int fd; + + if (redir->type == INPUT_FILE) + { + fd = open_file(redir->specifier, O_RDONLY, 0); + if (fd < 0) + return (-1); + dup2(fd, STDIN_FILENO); + close(fd); + } + else if (redir->type == INPUT_LIMITER) + { + fd = open_file("/tmp/heredoc_tmp", O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd < 0) + return (-1); + write(fd, redir->specifier, ft_strlen(redir->specifier)); + close(fd); + fd = open_file("/tmp/heredoc_tmp", O_RDONLY, 0); + if (fd < 0) + return (-1); + dup2(fd, STDIN_FILENO); + close(fd); + } + return (0); +} + +int handle_output_redirection(t_redirection *redir) +{ + int fd; + + if (redir->type == OUTPUT_OVERRIDE) + { + fd = open_file(redir->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd < 0) + return (-1); + dup2(fd, STDOUT_FILENO); + close(fd); + } + else if (redir->type == OUTPUT_APPEND) + { + fd = open_file(redir->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); + if (fd < 0) + return (-1); + dup2(fd, STDOUT_FILENO); + close(fd); + } + return (0); +} + +int handle_redirections(t_redirection *redirs) +{ + if (redirs[0].type == INPUT_FILE || redirs[0].type == INPUT_LIMITER) + { + if (handle_input_redirection(&redirs[0]) < 0) + return (-1); + } + if (redirs[1].type == OUTPUT_OVERRIDE || redirs[1].type == OUTPUT_APPEND) + { + if (handle_output_redirection(&redirs[1]) < 0) + return (-1); + } + return (0); +} + +int handle_pipe_parent(int p[2], t_node *node, t_env **env) +{ + int original_stdin; + int result; + + close(p[1]); + original_stdin = dup(STDIN_FILENO); + dup2(p[0], STDIN_FILENO); + result = eval_rec(node->content.pipe.right, env, p[0]); + dup2(original_stdin, STDIN_FILENO); + close(original_stdin); + close(p[0]); + return (result); +} + +int handle_pipe_child(int p[2], t_node *node, t_env **env, int in_fd) +{ + close(p[0]); + dup2(in_fd, STDIN_FILENO); + dup2(p[1], STDOUT_FILENO); + close(p[1]); + exit(eval_rec(node->content.pipe.left, env, in_fd)); +} diff --git a/src/interpreter.c b/src/interpreter.c index c7fe67c..f31e965 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,100 +6,41 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */ -/* Updated: 2025/01/16 18:44:39 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 12:48:49 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "debug_tools.h" #include "minishell.h" -int handle_redirections(t_redirection *redirs) +int eval_rec(t_node *node, t_env **env, int in_fd); + +int open_file(char *path, int flags, int mode) { int fd; - if (redirs[0].type == INPUT_FILE) - { - fd = open(redirs[0].specifier, O_RDONLY); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDIN_FILENO); - close(fd); - } - else if (redirs[0].type == INPUT_LIMITER) - { - fd = open("/tmp/heredoc_tmp", O_WRONLY | O_TRUNC, 0644); - if (fd < 0) - { - perror("open"); - return (-1); - } - write(fd, redirs[0].specifier, ft_strlen(redirs[0].specifier)); - close(fd); - fd = open("/tmp/heredoc_tmp", O_RDONLY); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDIN_FILENO); - close(fd); - } - if (redirs[1].type == OUTPUT_OVERRIDE) - { - fd = open(redirs[1].specifier, O_WRONLY | O_TRUNC, 0644); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDOUT_FILENO); - close(fd); - } - else if (redirs[1].type == OUTPUT_APPEND) - { - fd = open(redirs[1].specifier, O_WRONLY | O_APPEND, 0644); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDOUT_FILENO); - close(fd); - } - return (0); + fd = open(path, flags, mode); + if (fd < 0) + perror("open"); + return (fd); } int eval_rec(t_node *node, t_env **env, int in_fd) { - pid_t pid; int p[2]; + pid_t pid; int result; - int original_stdin; if (node->type == PIPE_NODE) { - pipe(p); + if (pipe(p) == -1) + return (perror("pipe"), EXIT_FAILURE); pid = fork(); + if (pid == -1) + return (perror("fork"), close(p[0]), close(p[1]), EXIT_FAILURE); if (pid == 0) - { - close(p[0]); - dup2(in_fd, STDIN_FILENO); - dup2(p[1], STDOUT_FILENO); - result = eval_rec(node->content.pipe.left, env, in_fd); - exit(result); - } - else - { - close(p[1]); - original_stdin = dup(STDIN_FILENO); - dup2(p[0], STDIN_FILENO); - result = eval_rec(node->content.pipe.right, env, p[0]); - dup2(original_stdin, STDIN_FILENO); - close(original_stdin); - } + handle_pipe_child(p, node, env, in_fd); + result = handle_pipe_parent(p, node, env); } else if (node->type == CMD_NODE) result = execute_cmd(&node->content.cmd, env); diff --git a/src/new_node.c b/src/new_node.c index 83d9159..bbac154 100644 --- a/src/new_node.c +++ b/src/new_node.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* new_node.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:21:03 by dkaiser #+# #+# */ -/* Updated: 2025/01/16 18:25:54 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 19:01:01 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,6 +51,7 @@ t_node *new_cmd_node(char **args, t_redirection redirs[2], t_list *create_files) node->content.cmd.redirs[1] = redirs[1]; node->content.cmd.create_files = create_files; free(redirs); + redirs = NULL;//1 return (node); } return (NULL); diff --git a/src/parse_cmd.c b/src/parse_cmd.c index 92dfd12..d13bf3f 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/16 19:06:03 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 12:44:44 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ t_node *parse_cmd(t_token *tokens, t_env **env) { char **args; t_redirection *redirs; - t_list *create_files; + t_list *create_files; create_files = NULL; redirs = collect_redirs(&tokens, *env, &create_files); @@ -36,6 +36,7 @@ t_node *parse_cmd(t_token *tokens, t_env **env) static char **collect_args(t_token **tokens, t_env **env) { t_token *cur; + t_token *next;//2 char **result; int i; @@ -50,12 +51,14 @@ static char **collect_args(t_token **tokens, t_env **env) i = 0; while (cur != NULL && cur->type == STRING_TOKEN) { + next = cur->next;//2 if (cur->previous) free_token(cur->previous); - result[i] = format_string(cur->content.string, *env); + result[i] = format_string(cur->content.string, *env, ft_atoi("0")); i++; - cur = cur->next; + // cur = cur->next; + cur = next;//2 } result[i] = NULL; return (result); -} +}
\ No newline at end of file diff --git a/src/parser.c b/src/parser.c index 1375954..aef8d70 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/11 16:06:54 by chuhlig ### ########.fr */ +/* Updated: 2025/01/19 18:59:00 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,7 @@ static t_node *parse_statement(t_token *tokens, t_env **env) if (left_side_tokens == NULL) { free_tokens(tokens); + tokens = NULL;//1 return (NULL); } else if (tokens != NULL) @@ -70,6 +71,7 @@ t_token *split_at_first(t_token **tokens, int type) if (result == split) result = NULL; free_token(split); + split = NULL;//1 return (result); } @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2025/01/14 15:39:58 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 12:45:00 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ void repl(const char *prompt, t_env **env, int *promptflag) while (1) { input = readline(prompt); - if (input == NULL) + if (input == NULL) { if (*promptflag > 1) (*promptflag)--; @@ -41,3 +41,5 @@ void repl(const char *prompt, t_env **env, int *promptflag) free(input); } } + +//echo hi | >./outfiles/outfile01 echo bye >./test_files/invalid_permission
\ No newline at end of file diff --git a/src/signal_handling.c b/src/signal_handling.c index c6273d0..6c6ca1e 100644 --- a/src/signal_handling.c +++ b/src/signal_handling.c @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 15:08:43 by dkaiser #+# #+# */ -/* Updated: 2025/01/14 14:11:29 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 12:15:32 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ |
