From 3f3f28dea3f0b770a485363b0407a12180814899 Mon Sep 17 00:00:00 2001 From: Hoommus Date: Wed, 10 Jul 2019 20:06:54 +0300 Subject: [PATCH 1/2] Patched that pipe error --- Makefile | 2 +- src/job_control/context_switch.c | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index e46d32d..11ff088 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ LIB_NAME = libftprintf.a SHELL_SRC = main.c init.c memory.c auxilia.c \ service_routines.c args_parsing.c string_hash.c \ shell_environ.c shell_environ_tools.c shell_environ_vector.c \ - shell_environ_memory.c \ + shell_environ_memory.c init_term.c \ syscall_wrappers.c \ auxiliary_main.c init_hashtable.c diff --git a/src/job_control/context_switch.c b/src/job_control/context_switch.c index f1974f7..a50866e 100644 --- a/src/job_control/context_switch.c +++ b/src/job_control/context_switch.c @@ -106,8 +106,9 @@ static void duplicate_fds(t_context *new, const t_context *context, list = context->fd_list; while (list) { - if (list->current >= 0 && (tmp = ft_memalloc(sizeof(struct s_fd_lst)))) + if (list->current >= 0) { + tmp = ft_memalloc(sizeof(struct s_fd_lst)); if (list->label) tmp->label = ft_strdup("cloned"); tmp->original = list->original; @@ -118,11 +119,13 @@ static void duplicate_fds(t_context *new, const t_context *context, new_list = tmp; } else - new->fd_list->next = tmp; + { + new_list->next = tmp; + new_list = new_list->next; + } } list = list->next; } - new->fd_list = new_list; } /* From fe723a74c5406e5757a89c2d98c5e88c243ce4ad Mon Sep 17 00:00:00 2001 From: Hoommus Date: Wed, 10 Jul 2019 20:19:06 +0300 Subject: [PATCH 2/2] Patched everything plus pipeline hangs --- include/shell_job_control.h | 1 + src/ast/exec_and_if.c | 5 +- src/ast/exec_brace_group.c | 4 +- src/ast/exec_pipeline.c | 8 ++-- src/ast/exec_subshell.c | 6 ++- src/features/glob/tests.c | 74 +++++++++++++++++++++++++++++ src/init.c | 2 +- src/init_hashtable.c | 19 +++++--- src/job_control/jc_queue_forknrun.c | 5 +- src/main.c | 4 +- 10 files changed, 108 insertions(+), 20 deletions(-) create mode 100644 src/features/glob/tests.c diff --git a/include/shell_job_control.h b/include/shell_job_control.h index 7c91cb7..fe41c97 100644 --- a/include/shell_job_control.h +++ b/include/shell_job_control.h @@ -125,6 +125,7 @@ void jc_format_job(const t_job *job); int forknrun(t_job *job, t_proc *proc, char *path, bool is_async); void close_redundant_fds(t_context *context); +void close_foreign_fds(t_proc *processes, t_proc *current); /* ** Subshells diff --git a/src/ast/exec_and_if.c b/src/ast/exec_and_if.c index f3138b3..136dabc 100644 --- a/src/ast/exec_and_if.c +++ b/src/ast/exec_and_if.c @@ -31,7 +31,7 @@ static NINT exec_and_if_async(const t_node *body) exit(status); } -static int exec_and_if_regular(const t_node *body) +static int exec_and_if_regular(const t_node *body, t_job *job) { int status_left; int status_right; @@ -40,6 +40,7 @@ static int exec_and_if_regular(const t_node *body) status_left = exec_node(body->left, NULL, false); if (status_left == 0) status_right = exec_node(body->right, NULL, false); + jc_job_dealloc(&job); return (status_right); } @@ -87,7 +88,7 @@ int exec_and_if(const t_node *node, if (is_async) f = fork(); if (f == -2) - status = exec_and_if_regular(node); + status = exec_and_if_regular(node, job); else if (f <= 0) status = decide_what_to_do(f, node); else diff --git a/src/ast/exec_brace_group.c b/src/ast/exec_brace_group.c index 4c2c61c..c36d06a 100644 --- a/src/ast/exec_brace_group.c +++ b/src/ast/exec_brace_group.c @@ -6,7 +6,7 @@ /* By: vtarasiu +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/06 15:51:01 by vtarasiu #+# #+# */ -/* Updated: 2019/07/07 19:09:18 by vtarasiu ### ########.fr */ +/* Updated: 2019/07/10 17:35:23 by vtarasiu ### ########.fr */ /* */ /* ************************************************************************** */ @@ -107,5 +107,7 @@ int exec_brace_group(const t_node *node, decide_what_to_do(f, job, body, context); else run_parent(f, job, context, is_async); + if (!new_context) + context_deep_free(&context); return (status); } diff --git a/src/ast/exec_pipeline.c b/src/ast/exec_pipeline.c index 823108a..691e7c9 100644 --- a/src/ast/exec_pipeline.c +++ b/src/ast/exec_pipeline.c @@ -23,8 +23,8 @@ static int exec_pipeline_terminator(const t_node *node, int status; if (context_right == NULL) - context_right = context_duplicate(g_term->context_original, XDUP_TERM); - context_left = context_duplicate(g_term->context_original, XDUP_TERM); + context_right = context_duplicate(g_term->context_original, XDUP_ENV | XDUP_FDS); + context_left = context_duplicate(g_term->context_original, XDUP_ENV | XDUP_FDS); pipe(pp); context_remove_ofd(context_right, 0); context_remove_ofd(context_left, 1); @@ -54,9 +54,9 @@ static int exec_pipeline_inner(const t_node *node, node->right->node_type == NODE_SUBSHELL)) return (exec_pipeline_terminator(node, context_right, is_async)); if (context_right == NULL) - context_right = context_duplicate(g_term->context_original, XDUP_TERM); + context_right = context_duplicate(g_term->context_original, XDUP_ENV | XDUP_FDS); pipe(pp); - context_left = context_duplicate(g_term->context_original, XDUP_TERM); + context_left = context_duplicate(g_term->context_original, XDUP_ENV | XDUP_FDS); context_remove_ofd(context_right, 0); context_remove_ofd(context_left, 1); context_add_fd(context_right, 0, pp[0], "pipe"); diff --git a/src/ast/exec_subshell.c b/src/ast/exec_subshell.c index 779e71b..b391c51 100644 --- a/src/ast/exec_subshell.c +++ b/src/ast/exec_subshell.c @@ -6,7 +6,7 @@ /* By: vtarasiu +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/19 16:39:00 by vtarasiu #+# #+# */ -/* Updated: 2019/07/07 19:09:18 by vtarasiu ### ########.fr */ +/* Updated: 2019/07/10 17:36:59 by vtarasiu ### ########.fr */ /* */ /* ************************************************************************** */ @@ -97,7 +97,7 @@ int exec_subshell(const t_node *node, status = 0; context = new_context ? new_context - : context_duplicate(g_term->context_original, XDUP_TERM); + : context_duplicate(g_term->context_original, XDUP_ALL); f = -2; job = ft_memalloc(sizeof(t_job)); jc_enable_subshell(); @@ -109,6 +109,8 @@ int exec_subshell(const t_node *node, decide_what_to_do(f, job, body, context); else run_parent(f, job, context, is_async); + if (!new_context) + context_deep_free(&context); jc_unlock_subshell_state(g_term->shell_pgid); jc_disable_subshell(); return (status); diff --git a/src/features/glob/tests.c b/src/features/glob/tests.c new file mode 100644 index 0000000..cf3ce1f --- /dev/null +++ b/src/features/glob/tests.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: vtarasiu +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/30 17:13:49 by mvladymy #+# #+# */ +/* Updated: 2019/07/10 14:29:09 by vtarasiu ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include +#include +#include + +#include "ft_glob.h" +#include "glob_tests.h" + +#include +#include +#include + +void glob_test(char *pattern, size_t offset, int flags) +{ + glob_t gl; + int ret; + size_t i; + + gl.gl_offs = offset; + ret = ft_glob(pattern, flags, 0, &gl); + printf("offset: %zu path number: %zu\n", gl.gl_offs, gl.gl_pathc); + i = 0; + while (i < gl.gl_offs + gl.gl_pathc) + printf("\t%s\n", gl.gl_pathv[i++]); + printf("return code: %d\n", ret); + ft_globfree(&gl); +} + +void range_test_match(char ch, char *pattern, bool assert) +{ + size_t a; + bool find; + + a = 0; + find = range_match(ch, pattern, &a); + printf("%lc %s %zu (`%c' in \"%s\")\n", ((assert == find) ? L'✅' : L'❌'), + ((find) ? "true " : "false"), + a, ch, pattern); +} + +void glob_match_test(char *path, char *pattern, bool assert) +{ + bool match; + + match = dir_match(path, pattern, 0, 0); + printf("%lc %s (\"%s\" and \"%s\")\n", ((assert == match) ? L'✅' : L'❌'), + ((match) ? "true " : "false"), + path, pattern); +} + +int main(void) +{ + setlocale(LC_ALL, ""); + range(); + range2(); + match(); + match2(); + match3(); +} diff --git a/src/init.c b/src/init.c index df56f3f..5403872 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: vtarasiu +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2018/11/16 12:28:13 by vtarasiu #+# #+# */ -/* Updated: 2019/07/09 01:28:22 by vtarasiu ### ########.fr */ +/* Updated: 2019/07/10 17:09:36 by vtarasiu ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/init_hashtable.c b/src/init_hashtable.c index 636bbc4..d9999c1 100644 --- a/src/init_hashtable.c +++ b/src/init_hashtable.c @@ -6,7 +6,7 @@ /* By: vtarasiu +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/07 12:46:16 by vtarasiu #+# #+# */ -/* Updated: 2019/07/07 15:00:28 by vtarasiu ### ########.fr */ +/* Updated: 2019/07/10 17:06:53 by vtarasiu ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,13 @@ t_hash_tab *g_hash_table; -void populate_from_dir(const char *dirpath, DIR *dir) +void populate_from_dir(const char *const dirpath, DIR *dir) { struct dirent *dirent; struct stat s; char path[2048]; size_t prefix_len; + char *swap; prefix_len = ft_strlen(dirpath) + 1; ft_bzero(path, sizeof(path)); @@ -35,7 +36,9 @@ void populate_from_dir(const char *dirpath, DIR *dir) !stat(path, &s) && !access(path, F_OK | X_OK)) { - hash_add(g_hash_table, dirent->d_name, path); + swap = ft_strdup(dirent->d_name); + hash_add(g_hash_table, swap, path); + free(swap); } } } @@ -47,17 +50,21 @@ void init_hashtable(void) char **split; DIR *dir; - hash_init(&g_hash_table, 1600); + hash_init(&g_hash_table, 1800); var = get_env_v(g_term->context_current->environ, "PATH"); if (!var || !var->value || ft_strlen(var->value) == 0) return ; i = 0; + split = NULL; split = ft_strsplit(var->value, ':'); while (split && split[i]) { - if ((dir = opendir(split[i]))) + dir = NULL; + if (split[i] && split[i][0] && (dir = opendir(split[i]))) + { populate_from_dir(split[i], dir); - closedir(dir); + closedir(dir); + } i++; } free_array((void **)split); diff --git a/src/job_control/jc_queue_forknrun.c b/src/job_control/jc_queue_forknrun.c index f1ef96b..c69ea24 100644 --- a/src/job_control/jc_queue_forknrun.c +++ b/src/job_control/jc_queue_forknrun.c @@ -6,7 +6,7 @@ /* By: vtarasiu +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/25 18:31:57 by vtarasiu #+# #+# */ -/* Updated: 2019/07/09 01:50:18 by vtarasiu ### ########.fr */ +/* Updated: 2019/07/10 17:41:18 by vtarasiu ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,12 @@ #include "shell_job_control.h" #include "shell_builtins.h" -static void close_foreign_fds(t_proc *processes, +void close_foreign_fds(t_proc *processes, t_proc *current) { struct s_fd_lst *list; + (void)current; while (processes) { if (processes != current) diff --git a/src/main.c b/src/main.c index 37ca30e..81db0f4 100644 --- a/src/main.c +++ b/src/main.c @@ -114,12 +114,12 @@ void run_file(const char *path) int main(int argc, char **argv) { init_shell_context(); + init_variables(); + init_hashtable(); init_files(); init_buffer_vector(MAX_INPUT); history_load(g_term->history_file); argv += parse_args(argc, argv); - init_variables(); - init_hashtable(); jc_init(g_term->context_current); print_messages(); setup_signal_handlers();