From b16d4b834d95d5cc0757d09e74fe6042e55f5440 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Thu, 10 Sep 2020 15:08:28 +0200 Subject: Added better error handling in eval --- src/builtin/exit.c | 2 +- src/builtin/unset.c | 6 ++-- src/env.c | 4 +-- src/error.c | 28 ++++++++++++++++++- src/eval/cmd.c | 80 ++++++++++++++++++++++++++--------------------------- src/eval/local.c | 2 +- src/eval/op.c | 18 ++++++------ src/eval/redir.c | 27 +++++++++--------- src/lexer/lexer.c | 6 ++-- src/main.c | 20 ++++++++------ src/signal.c | 4 +-- 11 files changed, 111 insertions(+), 86 deletions(-) (limited to 'src') diff --git a/src/builtin/exit.c b/src/builtin/exit.c index 640fc01..52a4462 100644 --- a/src/builtin/exit.c +++ b/src/builtin/exit.c @@ -24,7 +24,7 @@ int builtin_exit(char **argv, t_env env) (void)env; if (argv[1] == NULL) - status = g_last_status_code; + status = g_last_status; else { errno = 0; diff --git a/src/builtin/unset.c b/src/builtin/unset.c index 367f063..30facd3 100644 --- a/src/builtin/unset.c +++ b/src/builtin/unset.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:51 by charles #+# #+# */ -/* Updated: 2020/07/19 18:47:36 by charles ### ########.fr */ +/* Updated: 2020/09/10 13:49:58 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,11 +31,11 @@ int builtin_unset(char **argv, t_env env) { errorf("unset: `%s': not a valid identifier\n", argv[i]); status = 1; - continue; // put invalid identifier + continue ; } found_index = env_search_index(env, argv[i]); if (found_index == -1) - continue; + continue ; ft_vecremove(env, found_index, free); } return (status); diff --git a/src/env.c b/src/env.c index 1766a50..19ead42 100644 --- a/src/env.c +++ b/src/env.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */ -/* Updated: 2020/09/09 16:56:39 by charles ### ########.fr */ +/* Updated: 2020/09/10 14:19:42 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -105,7 +105,7 @@ char *env_search_first_match(t_env env, const char *haystack) while (ft_isalnum(haystack[len]) || haystack[len] == '_') len++; if (haystack[0] == '?') - return (ft_itoa(g_last_status_code)); // FIXME leak (static buffer) + return (ft_itoa(g_last_status)); // FIXME leak (static buffer) if (len == 0) return (NULL); i = -1; diff --git a/src/error.c b/src/error.c index e19a9ed..6c24635 100644 --- a/src/error.c +++ b/src/error.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 11:02:52 by charles #+# #+# */ -/* Updated: 2020/08/27 10:44:08 by charles ### ########.fr */ +/* Updated: 2020/09/10 14:48:19 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,3 +49,29 @@ void verrorf(const char *format, va_list ap) } } } + +static int g_error_to_status[] = { + [ERR_NONE] = 0, + [ERR_AMBIGUOUS_REDIR] = 1, + [ERR_OPEN] = 1, + [ERR_CMD_NOT_FOUND] = 127, + [ERR_SYNTAX] = 2, + [ERR_IS_DIRECTORY] = 126, + [ERR_ERRNO] = 126, +}; + +int error_get_status(int status) +{ + return (g_error_to_status[status]); +} + +void error_set_status(int status) +{ + if (status == ERR_FATAL) + exit(1); + /* printf("%d\n", status); */ + if (status < ERR_NONE) + g_last_status = status; + else + g_last_status = g_error_to_status[status]; +} diff --git a/src/eval/cmd.c b/src/eval/cmd.c index 5130a45..107a2a6 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,14 +6,14 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/09/09 17:40:54 by charles ### ########.fr */ +/* Updated: 2020/09/10 15:05:54 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "eval.h" pid_t g_child_pid = -1; -int g_last_status_code = 0; +int g_last_status = 0; void token_debug(void *v); /* @@ -33,19 +33,19 @@ int fork_wrap( pid_t child_pid; if ((child_pid = fork()) == -1) - return (-1); + return (ERR_FATAL); if (child_pid == 0) { - if ((fds[FDS_READ] != MS_NO_FD && dup2(fds[FDS_READ], STDIN_FILENO) == -1) || - (fds[FDS_WRITE] != MS_NO_FD && dup2(fds[FDS_WRITE], STDOUT_FILENO) == -1)) + if ((fds[FD_READ] != FD_NONE && dup2(fds[FD_READ], STDIN_FILENO) == -1) || + (fds[FD_WRITE] != FD_NONE && dup2(fds[FD_WRITE], STDOUT_FILENO) == -1)) exit(EXIT_FAILURE); - if ((status = wrapped(passed)) == -1) + if ((status = wrapped(passed)) == ERR_FATAL) exit(EXIT_FAILURE); exit(status); } g_child_pid = child_pid; wait(&child_pid); - close(fds[FDS_WRITE]); + close(fds[FD_WRITE]); // also read end? return (WEXITSTATUS(child_pid)); } @@ -53,38 +53,41 @@ int fork_wrap( int forked_cmd(void *void_param) { t_fork_param_cmd *param; - int ret; + int status; + struct stat statbuf; param = void_param; ft_vecpop(param->env_local, NULL); if (ft_vecswallow_at(param->env, param->env->size - 1, param->env_local) == NULL) { ft_vecdestroy(param->env_local, free); - return (-1); + return (ERR_FATAL); } if (param->builtin != NULL) return (param->builtin->func(param->argv, param->env)); else { + if (stat(param->exec_path, &statbuf) == -1) + { + errorf("%s: %s\n", param->exec_path, strerror(errno)); + return (error_get_status(ERR_ERRNO)); + } + if (S_ISDIR(statbuf.st_mode)) + { + errorf("%s: Is a directory\n", param->exec_path); + return (error_get_status(ERR_IS_DIRECTORY)); + } + errno = 0; - ret = execve(param->exec_path, param->argv, (char**)param->env->data); - if (ret == -1) + status = execve(param->exec_path, param->argv, (char**)param->env->data); + if (status == -1) { if (errno == ENOEXEC) - return (0); - struct stat statbuf; - if (stat(param->exec_path, &statbuf) != -1 && S_ISDIR(statbuf.st_mode)) - { - errorf("%s: Is a directory\n", param->exec_path); - ret = 126; - } - else - { - errorf("%s: %s\n", param->exec_path, strerror(errno)); - ret = 126; - } + return (error_get_status(ERR_NONE)); + errorf("%s: %s\n", param->exec_path, strerror(errno)); + return (error_get_status(ERR_ERRNO)); } - return (ret); + return (status); } } @@ -92,16 +95,18 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) { t_fork_param_cmd param; char **argv; + int status; - if (!redir_extract(&ast->redirs, env, fds)) + if ((status = redir_extract(&ast->redirs, env, fds)) != ERR_NONE) { ast->redirs = NULL; - return (-1); + return (status); } ast->redirs = NULL; if ((param.env_local = env_from_array((char*[]){NULL})) == NULL) - return (-1); - variable_extract(&ast->cmd_argv, env, param.env_local); + return (ERR_FATAL); + if (!variable_extract(&ast->cmd_argv, env, param.env_local)) + return (ERR_FATAL); /* char **strs = preprocess(&start, env); */ /* */ @@ -115,17 +120,16 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) /* ft_vecdestroy(param.env_local, free); */ /* return (-1); */ /* } */ - /* g_last_status_code = 0; */ + /* g_last_status = 0; */ /* return (0); */ /* } */ if ((argv = preprocess(&ast->cmd_argv, env)) == NULL) { ast->cmd_argv = NULL; - return (-1); + return (ERR_FATAL); } - // can have no command (e.g `< file`) if (argv[0] == NULL) return (0); param.builtin = builtin_search_func(argv[0]); @@ -135,23 +139,19 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) param.exec_path = exec_search_path(path, env_search(env, "PATH"), argv[0]); if (param.exec_path == NULL) { - g_last_status_code = 127; errorf("%s: command not found\n", argv[0]); ft_split_destroy(argv); - return (-1); // return error status + return (ERR_CMD_NOT_FOUND); } } else if (!param.builtin->forked) - { - g_last_status_code = param.builtin->func(argv, env); - return (g_last_status_code); - } + return (param.builtin->func(argv, env)); param.argv = argv; param.env = env; - int ret = fork_wrap(fds, ¶m, &forked_cmd); - g_last_status_code = ret; + status = fork_wrap(fds, ¶m, &forked_cmd); + /* g_last_status = status; */ ft_split_destroy(argv); ft_vecdestroy(param.env_local, free); - return (ret); + return (status); } diff --git a/src/eval/local.c b/src/eval/local.c index 13719cd..089a0c7 100644 --- a/src/eval/local.c +++ b/src/eval/local.c @@ -62,7 +62,7 @@ /* ft_vecdestroy(param.env_local, free); */ /* return (-1); */ /* } */ - /* g_last_status_code = 0; */ + /* g_last_status = 0; */ /* return (0); */ /* } */ diff --git a/src/eval/op.c b/src/eval/op.c index 36340be..e06c959 100644 --- a/src/eval/op.c +++ b/src/eval/op.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 15:27:22 by charles #+# #+# */ -/* Updated: 2020/08/28 16:54:39 by charles ### ########.fr */ +/* Updated: 2020/09/10 14:15:08 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,15 +19,15 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast) int right_fds[2]; int p[2]; - left_fds[FDS_READ] = fds[FDS_READ]; - left_fds[FDS_WRITE] = MS_NO_FD; - right_fds[FDS_READ] = MS_NO_FD; - right_fds[FDS_WRITE] = fds[FDS_WRITE]; + left_fds[FD_READ] = fds[FD_READ]; + left_fds[FD_WRITE] = FD_NONE; + right_fds[FD_READ] = FD_NONE; + right_fds[FD_WRITE] = fds[FD_WRITE]; if (ast->op.sep == TAG_PIPE) { pipe(p); - left_fds[FDS_WRITE] = p[FDS_WRITE]; - right_fds[FDS_READ] = p[FDS_READ]; + left_fds[FD_WRITE] = p[FD_WRITE]; + right_fds[FD_READ] = p[FD_READ]; } if ((status = eval(left_fds, env, path, ast->op.left)) == -1) return (-1); @@ -51,7 +51,7 @@ int eval_parent(int fds[2], t_env env, t_path path, t_ast *ast) t_fork_param_parent param; if (!redir_extract(&ast->redirs, env, fds)) - return (-1); + return (ERR_FATAL); param.fds[0] = fds[0]; param.fds[1] = fds[1]; param.env = env; @@ -69,5 +69,5 @@ int eval(int fds[2], t_env env, t_path path, t_ast *ast) return (eval_op(fds, env, path, ast)); if (ast->tag == AST_CMD) return (eval_cmd(fds, env, path, ast)); - return (-1); + return (ERR_FATAL); } diff --git a/src/eval/redir.c b/src/eval/redir.c index e8796a5..9beef17 100644 --- a/src/eval/redir.c +++ b/src/eval/redir.c @@ -6,15 +6,15 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/15 11:05:34 by charles #+# #+# */ -/* Updated: 2020/08/28 17:14:19 by charles ### ########.fr */ +/* Updated: 2020/09/10 14:25:08 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "eval.h" -static bool st_open_replace(int *fd, char *filename, int oflag) +static int st_open_replace(int *fd, char *filename, int oflag) { - if (*fd != MS_NO_FD) + if (*fd != FD_NONE) close(*fd); if (oflag & O_CREAT) *fd = open(filename, oflag, 0644); @@ -22,15 +22,14 @@ static bool st_open_replace(int *fd, char *filename, int oflag) *fd = open(filename, oflag); if (*fd == -1) { - g_last_status_code = 1; errorf("%s: %s\n", filename, strerror(errno)); free(filename); - return (false); + return (ERR_OPEN); } - return (true); + return (ERR_NONE); } -bool redir_extract( +int redir_extract( t_tok_lst **redirs, t_env env, int fds[2]) @@ -40,10 +39,10 @@ bool redir_extract( char *filename; if (*redirs == NULL) - return (true); + return (ERR_NONE); if (!((*redirs)->tag & TAG_IS_REDIR) || (*redirs)->next == NULL || !((*redirs)->next->tag & TAG_IS_STR)) - return (false); + return (ERR_FATAL); curr = (*redirs)->next; after = NULL; while (curr != NULL && curr->tag & TAG_IS_STR) @@ -59,18 +58,18 @@ bool redir_extract( { tok_lst_destroy(redirs, free); tok_lst_destroy(&after, free); - return (false); + return (ERR_FATAL); } if (((*redirs)->tag == TAG_REDIR_IN - && !st_open_replace(&fds[FDS_READ], filename, O_RDONLY)) + && st_open_replace(&fds[FD_READ], filename, O_RDONLY) != ERR_NONE) || ((*redirs)->tag == TAG_REDIR_OUT - && !st_open_replace(&fds[FDS_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC)) + && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC) != ERR_NONE) || ((*redirs)->tag == TAG_REDIR_APPEND - && !st_open_replace(&fds[FDS_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND))) + && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND) != ERR_NONE)) { tok_lst_destroy(redirs, free); tok_lst_destroy(&after, free); - return (false); + return (ERR_FATAL); } tok_lst_destroy(redirs, free); free(filename); diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index befc9b8..122ab75 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/07/16 08:18:25 by nahaddac #+# #+# */ -/* Updated: 2020/09/10 08:33:29 by nahaddac ### ########.fr */ +/* Updated: 2020/09/10 13:58:08 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,9 +22,7 @@ int len_until_sep(char *input) { if (input[i] == '\\') { - i +=2; - if (input[i] == '\\') - ; + i += 2; if (input[i] == ' ') { while(input[++i] == ' ') diff --git a/src/main.c b/src/main.c index 17dee77..71d49bb 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/09/09 16:19:00 by charles ### ########.fr */ +/* Updated: 2020/09/10 14:33:22 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,7 +69,7 @@ int main(int argc, char **argv, char **envp) // } // env_export(env, "_", env_exec_path); - g_last_status_code = 0; + g_last_status = 0; signal(SIGINT, signal_sigint); signal(SIGQUIT, signal_sigquit); signal(SIGTERM, signal_sigterm); @@ -104,9 +104,9 @@ int main(int argc, char **argv, char **envp) /* ft_lstiter(parser_out->ast->cmd_argv, token_debug); */ /* printf("===redirs===\n"); */ /* ft_lstiter(parser_out->ast->redirs, token_debug); */ - int fds[2] = {MS_NO_FD, MS_NO_FD}; - int eval_out = eval(fds, env, path, parser_out->ast); - (void)eval_out; + int fds[2] = {FD_NONE, FD_NONE}; + int status = eval(fds, env, path, parser_out->ast); + error_set_status(status); } else { @@ -134,9 +134,11 @@ int main(int argc, char **argv, char **envp) continue; } - int fds[2] = {MS_NO_FD, MS_NO_FD}; - int eval_out = eval(fds, env, path, parser_out->ast); - (void)eval_out; + int fds[2] = {FD_NONE, FD_NONE}; + int status = eval(fds, env, path, parser_out->ast); + if (status == ERR_FATAL) + exit(1); + error_set_status(status); print_prompt(); } if (ret != FTGL_EOF) @@ -145,5 +147,5 @@ int main(int argc, char **argv, char **envp) ft_htdestroy(path, free); ft_vecdestroy(env, free); - return (g_last_status_code); + return (g_last_status); } diff --git a/src/signal.c b/src/signal.c index e40df26..0dec80c 100644 --- a/src/signal.c +++ b/src/signal.c @@ -16,7 +16,7 @@ void signal_sigint(int signum) { (void)signum; - g_last_status_code = 130; + g_last_status = 130; if (g_child_pid != -1) { kill(g_child_pid, SIGINT); @@ -32,7 +32,7 @@ void signal_sigint(int signum) void signal_sigquit(int signum) { (void)signum; - g_last_status_code = 131; + g_last_status = 131; if (g_child_pid != -1) { kill(g_child_pid, SIGQUIT); -- cgit From 98990d5195e93154abbfd16eaa9d1fcc3572bc5c Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Thu, 10 Sep 2020 19:46:07 +0200 Subject: Added errorf_ret helper, Removing error to status (just status code and fatal error) --- src/builtin/cd.c | 17 ++++------------- src/builtin/exit.c | 12 +++--------- src/error.c | 40 ++++++++++++++++------------------------ src/eval/cmd.c | 43 ++++++++++++++++--------------------------- src/eval/op.c | 14 ++++++++------ src/eval/redir.c | 20 ++++++++++---------- src/main.c | 13 ++++++++----- src/parser/parsed.c | 6 +++--- 8 files changed, 68 insertions(+), 97 deletions(-) (limited to 'src') diff --git a/src/builtin/cd.c b/src/builtin/cd.c index d7115e5..5f7cdd8 100644 --- a/src/builtin/cd.c +++ b/src/builtin/cd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:20 by charles #+# #+# */ -/* Updated: 2020/07/19 19:05:25 by charles ### ########.fr */ +/* Updated: 2020/09/10 19:40:31 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,27 +24,18 @@ int builtin_cd(char **argv, t_env env) (void)env; if (argv[1] != NULL && argv[2] != NULL) - { - errorf("cd: too many arguments\n"); - return (1); - } + return (errorf_ret(1, "cd: too many arguments\n")); if (argv[1] != NULL && argv[1][0] == '\0') return (0); if (argv[1] == NULL) { if ((home = env_search(env, "HOME")) == NULL) - { - errorf("cd: HOME not set\n"); - return (1); - } + return (errorf_ret(1, "cd: HOME not set\n")); argv[1] = home; } errno = 0; if (chdir(argv[1]) == -1) - { - errorf("cd: %s: %s\n", argv[1], strerror(errno)); - return (1); - } + return (errorf_ret(1, "cd: %s: %s\n", argv[1], strerror(errno))); if (!(getcwd(buf, PATH_MAX))) return (1); if (env_export(env, "PWD", buf) == NULL) diff --git a/src/builtin/exit.c b/src/builtin/exit.c index 52a4462..a24efad 100644 --- a/src/builtin/exit.c +++ b/src/builtin/exit.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:16 by charles #+# #+# */ -/* Updated: 2020/08/20 17:31:18 by charles ### ########.fr */ +/* Updated: 2020/09/10 19:41:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,15 +32,9 @@ int builtin_exit(char **argv, t_env env) while (ft_isblank(*after)) after++; if (*after != '\0' || errno == ERANGE) - { - errorf("exit: %s: numeric argument required\n", argv[1]); - return (2); - } + return (errorf_ret(2, "exit: %s: numeric argument required\n", argv[1])); if (argv[2] != NULL) - { - errorf("exit: too many arguments\n"); - return (1); - } + return (errorf_ret(1, "exit: too many arguments\n")); } exit(status % 256); return (0); diff --git a/src/error.c b/src/error.c index 6c24635..2e56117 100644 --- a/src/error.c +++ b/src/error.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 11:02:52 by charles #+# #+# */ -/* Updated: 2020/09/10 14:48:19 by charles ### ########.fr */ +/* Updated: 2020/09/10 20:29:41 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ ** \note NULL arguments are ignored */ -void errorf(const char *format, ...) +void errorf(const char *format, ...) { va_list ap; @@ -28,7 +28,11 @@ void errorf(const char *format, ...) va_end(ap); } -void verrorf(const char *format, va_list ap) +/* +** \brief errorf with an argument pointer (ap) instead of arguments +*/ + +void verrorf(const char *format, va_list ap) { char *arg; @@ -50,28 +54,16 @@ void verrorf(const char *format, va_list ap) } } -static int g_error_to_status[] = { - [ERR_NONE] = 0, - [ERR_AMBIGUOUS_REDIR] = 1, - [ERR_OPEN] = 1, - [ERR_CMD_NOT_FOUND] = 127, - [ERR_SYNTAX] = 2, - [ERR_IS_DIRECTORY] = 126, - [ERR_ERRNO] = 126, -}; +/* +** \brief errorf helper to return an status code and print the error +*/ -int error_get_status(int status) +int errorf_ret(int status, const char *format, ...) { - return (g_error_to_status[status]); -} + va_list ap; -void error_set_status(int status) -{ - if (status == ERR_FATAL) - exit(1); - /* printf("%d\n", status); */ - if (status < ERR_NONE) - g_last_status = status; - else - g_last_status = g_error_to_status[status]; + va_start(ap, format); + verrorf(format, ap); + va_end(ap); + return (status); } diff --git a/src/eval/cmd.c b/src/eval/cmd.c index 107a2a6..36e5a00 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/09/10 15:05:54 by charles ### ########.fr */ +/* Updated: 2020/09/10 20:32:13 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,20 +33,19 @@ int fork_wrap( pid_t child_pid; if ((child_pid = fork()) == -1) - return (ERR_FATAL); + return (EVAL_FATAL); if (child_pid == 0) { if ((fds[FD_READ] != FD_NONE && dup2(fds[FD_READ], STDIN_FILENO) == -1) || (fds[FD_WRITE] != FD_NONE && dup2(fds[FD_WRITE], STDOUT_FILENO) == -1)) exit(EXIT_FAILURE); - if ((status = wrapped(passed)) == ERR_FATAL) + if ((status = wrapped(passed)) == EVAL_FATAL) // FIXME detect fatal in child (pipe ?) exit(EXIT_FAILURE); exit(status); } g_child_pid = child_pid; wait(&child_pid); close(fds[FD_WRITE]); - // also read end? return (WEXITSTATUS(child_pid)); } @@ -61,31 +60,25 @@ int forked_cmd(void *void_param) if (ft_vecswallow_at(param->env, param->env->size - 1, param->env_local) == NULL) { ft_vecdestroy(param->env_local, free); - return (ERR_FATAL); + return (EVAL_FATAL); } if (param->builtin != NULL) return (param->builtin->func(param->argv, param->env)); else { if (stat(param->exec_path, &statbuf) == -1) - { - errorf("%s: %s\n", param->exec_path, strerror(errno)); - return (error_get_status(ERR_ERRNO)); - } + return (errorf_ret(126, "%s: %s\n", param->exec_path, strerror(errno))); if (S_ISDIR(statbuf.st_mode)) - { - errorf("%s: Is a directory\n", param->exec_path); - return (error_get_status(ERR_IS_DIRECTORY)); - } - + return (errorf_ret(126, "%s: Is a directory\n", param->exec_path)); + if (!(statbuf.st_mode & 0444)) + return (errorf_ret(126, "%s: %s\n", param->exec_path, strerror(EACCES))); errno = 0; status = execve(param->exec_path, param->argv, (char**)param->env->data); if (status == -1) { if (errno == ENOEXEC) - return (error_get_status(ERR_NONE)); - errorf("%s: %s\n", param->exec_path, strerror(errno)); - return (error_get_status(ERR_ERRNO)); + return (0); + return (errorf_ret(126, "%s: %s\n", param->exec_path, strerror(errno))); } return (status); } @@ -97,16 +90,12 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) char **argv; int status; - if ((status = redir_extract(&ast->redirs, env, fds)) != ERR_NONE) - { - ast->redirs = NULL; + if ((status = redir_extract(&ast->redirs, env, fds)) != 0) return (status); - } - ast->redirs = NULL; if ((param.env_local = env_from_array((char*[]){NULL})) == NULL) - return (ERR_FATAL); + return (EVAL_FATAL); if (!variable_extract(&ast->cmd_argv, env, param.env_local)) - return (ERR_FATAL); + return (EVAL_FATAL); /* char **strs = preprocess(&start, env); */ /* */ @@ -127,7 +116,7 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) if ((argv = preprocess(&ast->cmd_argv, env)) == NULL) { ast->cmd_argv = NULL; - return (ERR_FATAL); + return (EVAL_FATAL); } if (argv[0] == NULL) @@ -141,7 +130,7 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) { errorf("%s: command not found\n", argv[0]); ft_split_destroy(argv); - return (ERR_CMD_NOT_FOUND); + return (127); } } else if (!param.builtin->forked) @@ -150,8 +139,8 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) param.argv = argv; param.env = env; status = fork_wrap(fds, ¶m, &forked_cmd); - /* g_last_status = status; */ ft_split_destroy(argv); ft_vecdestroy(param.env_local, free); + g_last_status = status; return (status); } diff --git a/src/eval/op.c b/src/eval/op.c index e06c959..9a37736 100644 --- a/src/eval/op.c +++ b/src/eval/op.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 15:27:22 by charles #+# #+# */ -/* Updated: 2020/09/10 14:15:08 by charles ### ########.fr */ +/* Updated: 2020/09/10 20:28:57 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,8 +29,9 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast) left_fds[FD_WRITE] = p[FD_WRITE]; right_fds[FD_READ] = p[FD_READ]; } - if ((status = eval(left_fds, env, path, ast->op.left)) == -1) - return (-1); + if ((status = eval(left_fds, env, path, ast->op.left)) == EVAL_FATAL) + return (EVAL_FATAL); + g_last_status = status; if ((ast->op.sep == TAG_AND && status != 0) || (ast->op.sep == TAG_PIPE && status != 0) || (ast->op.sep == TAG_OR && status == 0)) @@ -49,9 +50,10 @@ int wrapped_eval(void *void_param) int eval_parent(int fds[2], t_env env, t_path path, t_ast *ast) { t_fork_param_parent param; + int status; - if (!redir_extract(&ast->redirs, env, fds)) - return (ERR_FATAL); + if ((status = redir_extract(&ast->redirs, env, fds)) != 0) + return (status); param.fds[0] = fds[0]; param.fds[1] = fds[1]; param.env = env; @@ -69,5 +71,5 @@ int eval(int fds[2], t_env env, t_path path, t_ast *ast) return (eval_op(fds, env, path, ast)); if (ast->tag == AST_CMD) return (eval_cmd(fds, env, path, ast)); - return (ERR_FATAL); + return (EVAL_FATAL); } diff --git a/src/eval/redir.c b/src/eval/redir.c index 9beef17..39e202d 100644 --- a/src/eval/redir.c +++ b/src/eval/redir.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/15 11:05:34 by charles #+# #+# */ -/* Updated: 2020/09/10 14:25:08 by charles ### ########.fr */ +/* Updated: 2020/09/10 20:25:59 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,9 +24,9 @@ static int st_open_replace(int *fd, char *filename, int oflag) { errorf("%s: %s\n", filename, strerror(errno)); free(filename); - return (ERR_OPEN); + return (1); } - return (ERR_NONE); + return (0); } int redir_extract( @@ -39,10 +39,10 @@ int redir_extract( char *filename; if (*redirs == NULL) - return (ERR_NONE); + return (0); if (!((*redirs)->tag & TAG_IS_REDIR) || (*redirs)->next == NULL || !((*redirs)->next->tag & TAG_IS_STR)) - return (ERR_FATAL); + return (EVAL_FATAL); curr = (*redirs)->next; after = NULL; while (curr != NULL && curr->tag & TAG_IS_STR) @@ -58,18 +58,18 @@ int redir_extract( { tok_lst_destroy(redirs, free); tok_lst_destroy(&after, free); - return (ERR_FATAL); + return (EVAL_FATAL); } if (((*redirs)->tag == TAG_REDIR_IN - && st_open_replace(&fds[FD_READ], filename, O_RDONLY) != ERR_NONE) + && st_open_replace(&fds[FD_READ], filename, O_RDONLY) != 0) || ((*redirs)->tag == TAG_REDIR_OUT - && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC) != ERR_NONE) + && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_TRUNC) != 0) || ((*redirs)->tag == TAG_REDIR_APPEND - && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND) != ERR_NONE)) + && st_open_replace(&fds[FD_WRITE], filename, O_WRONLY | O_CREAT | O_APPEND) != 0)) { tok_lst_destroy(redirs, free); tok_lst_destroy(&after, free); - return (ERR_FATAL); + return (EVAL_FATAL); } tok_lst_destroy(redirs, free); free(filename); diff --git a/src/main.c b/src/main.c index 71d49bb..e8275f5 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/09/10 14:33:22 by charles ### ########.fr */ +/* Updated: 2020/09/10 20:26:19 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,6 @@ void tok_lst_debug(t_tok_lst *tokens); ** TODO ** $? ** concurrent pipeline -** cmd variable preprocess ** signal on whole line instead of single command ** env local to current minishell process */ @@ -106,7 +105,10 @@ int main(int argc, char **argv, char **envp) /* ft_lstiter(parser_out->ast->redirs, token_debug); */ int fds[2] = {FD_NONE, FD_NONE}; int status = eval(fds, env, path, parser_out->ast); - error_set_status(status); + if (status == EVAL_FATAL) + exit(1); + g_last_status = status; + /* error_set_status(status); */ } else { @@ -136,9 +138,10 @@ int main(int argc, char **argv, char **envp) int fds[2] = {FD_NONE, FD_NONE}; int status = eval(fds, env, path, parser_out->ast); - if (status == ERR_FATAL) + if (status == EVAL_FATAL) exit(1); - error_set_status(status); + g_last_status = status; + /* error_set_status(status); */ print_prompt(); } if (ret != FTGL_EOF) diff --git a/src/parser/parsed.c b/src/parser/parsed.c index 4e6d6f2..c577b32 100644 --- a/src/parser/parsed.c +++ b/src/parser/parsed.c @@ -6,13 +6,13 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/08/27 20:27:42 by charles #+# #+# */ -/* Updated: 2020/08/28 10:44:39 by charles ### ########.fr */ +/* Updated: 2020/09/10 19:31:46 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "parser.h" -t_parsed *parsed_new(t_ast *ast, t_tok_lst *rest) +t_parsed *parsed_new(t_ast *ast, t_tok_lst *rest) { t_parsed *ret; @@ -24,7 +24,7 @@ t_parsed *parsed_new(t_ast *ast, t_tok_lst *rest) return ret; } -t_parsed *parsed_error(const char *format, ...) +t_parsed *parsed_error(const char *format, ...) { t_parsed *err; va_list ap; -- cgit From 9446bb78646bef4e3449a59a9e01118b025402bf Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Fri, 11 Sep 2020 19:18:17 +0200 Subject: Removing command local variable --- src/eval/cmd.c | 35 +++++++---------------------------- src/eval/variable.c | 42 ------------------------------------------ src/lexer/tok_lst.c | 20 +------------------- src/main.c | 4 ++-- 4 files changed, 10 insertions(+), 91 deletions(-) delete mode 100644 src/eval/variable.c (limited to 'src') diff --git a/src/eval/cmd.c b/src/eval/cmd.c index 36e5a00..44f22b6 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/09/10 20:32:13 by charles ### ########.fr */ +/* Updated: 2020/09/11 19:10:20 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,12 +56,12 @@ int forked_cmd(void *void_param) struct stat statbuf; param = void_param; - ft_vecpop(param->env_local, NULL); - if (ft_vecswallow_at(param->env, param->env->size - 1, param->env_local) == NULL) - { - ft_vecdestroy(param->env_local, free); - return (EVAL_FATAL); - } + /* ft_vecpop(param->env_local, NULL); */ + /* if (ft_vecswallow_at(param->env, param->env->size - 1, param->env_local) == NULL) */ + /* { */ + /* ft_vecdestroy(param->env_local, free); */ + /* return (EVAL_FATAL); */ + /* } */ if (param->builtin != NULL) return (param->builtin->func(param->argv, param->env)); else @@ -92,26 +92,6 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) if ((status = redir_extract(&ast->redirs, env, fds)) != 0) return (status); - if ((param.env_local = env_from_array((char*[]){NULL})) == NULL) - return (EVAL_FATAL); - if (!variable_extract(&ast->cmd_argv, env, param.env_local)) - return (EVAL_FATAL); - - /* char **strs = preprocess(&start, env); */ - /* */ - /* if (env_export(env_local, id, strs[0]) == NULL) */ - /* return (-1); */ - /* if (ast->cmd_argv == NULL) // FIXME special env not passed to child processes */ - /* { */ - /* ft_vecpop(param.env_local, NULL); */ - /* if (ft_vecswallow_at(env, env->size - 1, param.env_local) == NULL) */ - /* { */ - /* ft_vecdestroy(param.env_local, free); */ - /* return (-1); */ - /* } */ - /* g_last_status = 0; */ - /* return (0); */ - /* } */ if ((argv = preprocess(&ast->cmd_argv, env)) == NULL) { @@ -140,7 +120,6 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) param.env = env; status = fork_wrap(fds, ¶m, &forked_cmd); ft_split_destroy(argv); - ft_vecdestroy(param.env_local, free); g_last_status = status; return (status); } diff --git a/src/eval/variable.c b/src/eval/variable.c deleted file mode 100644 index 2b6d7cf..0000000 --- a/src/eval/variable.c +++ /dev/null @@ -1,42 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* variable.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/09/09 17:12:14 by charles #+# #+# */ -/* Updated: 2020/09/09 18:22:43 by charles ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "eval.h" - -bool variable_extract(t_tok_lst **argv, t_env env, t_env env_local) -{ - char *key; - t_tok_lst *value_tokens; - char **strs; - - if (*argv == NULL || !((*argv)->tag & TAG_STR) - || !utils_start_with_valid_identifier((*argv)->content)) - return (true); - - key = (*argv)->content; - (*argv)->content = ft_strchr(key, '='); - *(*argv)->content = '\0'; - (*argv)->content++; - (*argv)->content = ft_strdup((*argv)->content); - - /* printf("|%s| |%s|\n", key, (*argv)->content); */ - /* if (*(*argv)->content == '\0') */ - /* { */ - /* ft_lstpop_front((t_ftlst**)argv, NULL); */ - /* return (true); */ - /* } */ - value_tokens = tok_lst_take_sticked(argv); - strs = preprocess(&value_tokens, env); - if (env_export(env_local, key, strs[0]) == NULL) - return (false); - return (variable_extract(argv, env, env_local)); -} diff --git a/src/lexer/tok_lst.c b/src/lexer/tok_lst.c index 83f50bf..debeb9a 100644 --- a/src/lexer/tok_lst.c +++ b/src/lexer/tok_lst.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/08/27 09:32:58 by charles #+# #+# */ -/* Updated: 2020/09/09 18:10:26 by charles ### ########.fr */ +/* Updated: 2020/09/11 19:11:20 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,21 +70,3 @@ t_tok_lst *tok_lst_uncons(t_tok_lst **tokens) poped->next = NULL; return (poped); } - -t_tok_lst *tok_lst_take_sticked(t_tok_lst **tokens) -{ - t_tok_lst *start; - t_tok_lst *curr; - - if (*tokens == NULL) - return (NULL); - start = *tokens; - curr = *tokens; - while (curr->tag & TAG_STICK && curr->tag & TAG_IS_STR) - curr = curr->next; - /* if (curr->tag & TAG_IS_STR) */ - /* curr = curr->next; */ - *tokens = curr->next; - curr->next = NULL; - return (start); -} diff --git a/src/main.c b/src/main.c index e8275f5..5089a6f 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/09/10 20:26:19 by charles ### ########.fr */ +/* Updated: 2020/09/11 19:18:00 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,9 +29,9 @@ void tok_lst_debug(t_tok_lst *tokens); /* ** TODO ** $? +** pipe make 2 new children ** concurrent pipeline ** signal on whole line instead of single command -** env local to current minishell process */ bool env_set_default(t_env env, char *key, char *value) -- cgit From 65006d0c14d3efa647b3c866ab54bdb1749fa31d Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sat, 12 Sep 2020 17:12:41 +0200 Subject: Added concurrent pipeline (not working with minishell_test for some obscure reason) --- src/builtin/cd.c | 8 ++++---- src/builtin/echo.c | 2 +- src/builtin/export.c | 16 ++++++++++------ src/eval/cmd.c | 37 ++++++++++++++++++++++++------------- src/eval/op.c | 38 ++++++++++++++++++++++++++++++++------ src/main.c | 5 +++-- src/utils.c | 2 +- 7 files changed, 75 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/builtin/cd.c b/src/builtin/cd.c index 5f7cdd8..de1eeb9 100644 --- a/src/builtin/cd.c +++ b/src/builtin/cd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:20 by charles #+# #+# */ -/* Updated: 2020/09/10 19:40:31 by charles ### ########.fr */ +/* Updated: 2020/09/12 11:09:49 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ int builtin_cd(char **argv, t_env env) { - char buf[PATH_MAX]; + char buf[PATH_MAX + 1]; char *home; (void)env; @@ -36,9 +36,9 @@ int builtin_cd(char **argv, t_env env) errno = 0; if (chdir(argv[1]) == -1) return (errorf_ret(1, "cd: %s: %s\n", argv[1], strerror(errno))); - if (!(getcwd(buf, PATH_MAX))) + if (getcwd(buf, PATH_MAX) == NULL) return (1); if (env_export(env, "PWD", buf) == NULL) - return (2); // FIXME malloc error recognition in builtins and cmd + return (EVAL_FATAL); return (0); } diff --git a/src/builtin/echo.c b/src/builtin/echo.c index 75a350c..9ad427a 100644 --- a/src/builtin/echo.c +++ b/src/builtin/echo.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:10:47 by charles #+# #+# */ -/* Updated: 2020/06/15 14:26:30 by charles ### ########.fr */ +/* Updated: 2020/09/12 15:24:34 by charles ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/builtin/export.c b/src/builtin/export.c index e19756d..d394af6 100644 --- a/src/builtin/export.c +++ b/src/builtin/export.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 17:11:34 by charles #+# #+# */ -/* Updated: 2020/08/28 17:49:47 by charles ### ########.fr */ +/* Updated: 2020/09/12 11:06:47 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,16 +21,20 @@ static void st_put_declare_x(char *s) { char *equal_ptr; - // could use errorf on stdout if (s == NULL) return ; - ft_putstr("declare -x "); + ft_putstr("export "); if ((equal_ptr = ft_strchr(s, '=')) == NULL) equal_ptr = ft_strchr(s, '\0'); write(STDOUT_FILENO, s, equal_ptr - s); ft_putchar('='); ft_putchar('"'); - ft_putstr(equal_ptr + 1); + while (*++equal_ptr != '\0') + { + if (*equal_ptr == '"') + ft_putchar('\\'); + ft_putchar(*equal_ptr); + } ft_putchar('"'); ft_putchar('\n'); } @@ -38,7 +42,7 @@ static void st_put_declare_x(char *s) int builtin_export(char **argv, t_env env) { int status; - size_t i; + size_t i; char *equal_ptr; if (argv[1] == NULL) @@ -62,7 +66,7 @@ int builtin_export(char **argv, t_env env) continue; } if (env_export(env, argv[i], equal_ptr == NULL ? "" : equal_ptr + 1) == NULL) - return (127); // malloc error + return (EVAL_FATAL); } return (status); } diff --git a/src/eval/cmd.c b/src/eval/cmd.c index 44f22b6..9745632 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/09/11 19:10:20 by charles ### ########.fr */ +/* Updated: 2020/09/12 17:04:23 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ pid_t g_child_pid = -1; int g_last_status = 0; + void token_debug(void *v); /* @@ -25,16 +26,22 @@ void token_debug(void *v); */ int fork_wrap( - int fds[2], - void *passed, - int (*wrapped)(void *param)) + int fds[2], + void *passed, + int (*wrapped)(void *param), + pid_t *child_pid +) { int status; - pid_t child_pid; + bool waiting; + pid_t pid; - if ((child_pid = fork()) == -1) + waiting = child_pid == NULL; + if (waiting) + child_pid = &pid; + if ((*child_pid = fork()) == -1) return (EVAL_FATAL); - if (child_pid == 0) + if (*child_pid == 0) { if ((fds[FD_READ] != FD_NONE && dup2(fds[FD_READ], STDIN_FILENO) == -1) || (fds[FD_WRITE] != FD_NONE && dup2(fds[FD_WRITE], STDOUT_FILENO) == -1)) @@ -43,10 +50,15 @@ int fork_wrap( exit(EXIT_FAILURE); exit(status); } - g_child_pid = child_pid; - wait(&child_pid); - close(fds[FD_WRITE]); - return (WEXITSTATUS(child_pid)); + g_child_pid = *child_pid; + if (waiting) + { + waitpid(*child_pid, child_pid, 0); + close(fds[FD_WRITE]); + /* close(fds[FD_READ]); */ + return (WEXITSTATUS(*child_pid)); + } + return (0); } int forked_cmd(void *void_param) @@ -104,7 +116,6 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) param.builtin = builtin_search_func(argv[0]); if (param.builtin == NULL) { - // check env local for PATH param.exec_path = exec_search_path(path, env_search(env, "PATH"), argv[0]); if (param.exec_path == NULL) { @@ -118,7 +129,7 @@ int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast) param.argv = argv; param.env = env; - status = fork_wrap(fds, ¶m, &forked_cmd); + status = fork_wrap(fds, ¶m, &forked_cmd, NULL); ft_split_destroy(argv); g_last_status = status; return (status); diff --git a/src/eval/op.c b/src/eval/op.c index 9a37736..a39e380 100644 --- a/src/eval/op.c +++ b/src/eval/op.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 15:27:22 by charles #+# #+# */ -/* Updated: 2020/09/10 20:28:57 by charles ### ########.fr */ +/* Updated: 2020/09/12 17:00:11 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,26 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast) pipe(p); left_fds[FD_WRITE] = p[FD_WRITE]; right_fds[FD_READ] = p[FD_READ]; + + pid_t left_pid; + pid_t right_pid; + eval_forked(left_fds, env, path, ast->op.left, &left_pid); + eval_forked(right_fds, env, path, ast->op.right, &right_pid); + + pid_t finished; + finished = wait(NULL); + close(p[FD_READ]); + close(p[FD_WRITE]); + if (finished == left_pid) + { + /* waitpid(right_pid, &right_pid, 0); */ + } + else if (finished == right_pid) + { + kill(left_pid, SIGKILL); + /* waitpid(left_pid, &left_pid, 0); */ + } + return (0); } if ((status = eval(left_fds, env, path, ast->op.left)) == EVAL_FATAL) return (EVAL_FATAL); @@ -41,7 +61,7 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast) int wrapped_eval(void *void_param) { - t_fork_param_parent *param; + t_fork_param_args *param; param = void_param; return (eval(param->fds, param->env, param->path, param->ast)); @@ -49,18 +69,24 @@ int wrapped_eval(void *void_param) int eval_parent(int fds[2], t_env env, t_path path, t_ast *ast) { - t_fork_param_parent param; int status; if ((status = redir_extract(&ast->redirs, env, fds)) != 0) return (status); + ast->tag ^= AST_PARENT; + return (eval_forked(fds, env, path, ast->parent_ast, NULL)); +} + +int eval_forked(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid) +{ + t_fork_param_args param; + param.fds[0] = fds[0]; param.fds[1] = fds[1]; param.env = env; param.path = path; - ast->tag ^= AST_PARENT; - param.ast = ast->parent_ast; - return (fork_wrap(fds, ¶m, wrapped_eval)); + param.ast = ast; + return (fork_wrap(fds, ¶m, wrapped_eval, child_pid)); } int eval(int fds[2], t_env env, t_path path, t_ast *ast) diff --git a/src/main.c b/src/main.c index 5089a6f..62e1d2b 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/09/11 19:18:00 by charles ### ########.fr */ +/* Updated: 2020/09/12 17:04:06 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -108,7 +108,8 @@ int main(int argc, char **argv, char **envp) if (status == EVAL_FATAL) exit(1); g_last_status = status; - /* error_set_status(status); */ + exit(status); + return (0); } else { diff --git a/src/utils.c b/src/utils.c index 55c190b..9fd47c0 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:56:31 by cacharle #+# #+# */ -/* Updated: 2020/09/09 15:41:04 by charles ### ########.fr */ +/* Updated: 2020/09/12 16:30:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -- cgit