diff --git a/caller.c b/caller.c index 7aa6875..da44324 100755 --- a/caller.c +++ b/caller.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ -/* Updated: 2022/05/05 20:52:22 by narnaud ### ########.fr */ +/* Updated: 2022/05/06 11:22:52 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,3 +70,46 @@ void exe(t_datas *datas, t_command *cmd) } } } + +pid_t caller(t_datas *datas, t_command *cmd) +{ + pid_t pid; + + pid = fork(); + if (!pid) + { + if (cmd->fd[0]) + dup2(cmd->fd[0], STDIN_FILENO); + if (cmd->fd[1]) + dup2(cmd->fd[1], STDOUT_FILENO); + if (builtin_call(datas, cmd) == -1) + exe(datas, cmd); + exit(EXIT_SUCCESS); + } + return pid; +} + +int piper(t_datas *datas, t_command *cmd) +{ + int pip[2]; + int status; + pid_t pid; + + if (cmd->fd[1] == 0 && cmd->next && cmd->next->fd[0] == 0) + { + pipe(pip); + cmd->fd[1] = pip[1]; + cmd->next->fd[0] = pip[0]; + } + pid = caller(datas, cmd); + if (cmd->fd[1]) + close(cmd->fd[1]); + if (cmd->next) + piper(datas, cmd->next); + waitpid(pid, &status, 0); + handle_status(datas, status); + if (cmd->fd[0]) + close(cmd->fd[0]); + return (1); +} + diff --git a/lexer.c b/lexer.c index 5985ccc..110b2f5 100755 --- a/lexer.c +++ b/lexer.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */ -/* Updated: 2022/05/05 15:41:06 by mea ### ########.fr */ +/* Updated: 2022/05/06 11:19:26 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -92,6 +92,7 @@ int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i) char *var_name; char *value; char **env; + i = 1; if ((*line)[1] == '?') { @@ -152,3 +153,35 @@ int check_state(t_lexer *lex, char **line) } return (0); } + +t_token *lexer(t_datas *datas, char *line) +{ + t_lexer *lex; + char *tmp; + int tmp_i; + + lex = ft_calloc(1, sizeof *lex); + lex->state = ROOT_ST; + tmp = ft_calloc(1024, sizeof *tmp); + tmp_i = 0; + while (*line) + { + if (check_state(lex, &line)) + continue; + if (lex->state != S_QUOTE_ST && *line == '$') + tmp_i = replace_var(datas, &line, tmp, tmp_i); + if (check_register(lex, &line, tmp)) + { + tmp_i = 0; + ft_bzero(tmp, 1024); + continue ; + } + tmp[tmp_i] = *line; + line++; + tmp_i++; + } + create_token(lex, tmp); + return (lex->tokens); +} + + diff --git a/minishell.c b/minishell.c index b7999ff..6625f79 100755 --- a/minishell.c +++ b/minishell.c @@ -6,125 +6,11 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ -/* Updated: 2022/05/05 20:53:48 by narnaud ### ########.fr */ +/* Updated: 2022/05/06 11:23:19 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ # include "minishell.h" - -void handle_status(t_datas *datas, int status) -{ - if (WIFSIGNALED(status)) - { - datas->exit_code = 128 + WTERMSIG(status); - if (datas->exit_code == 131) - printf("Quit: 3\n"); - } - else - datas->exit_code = WEXITSTATUS(status); -} - -pid_t caller(t_datas *datas, t_command *cmd) -{ - pid_t pid; - - pid = fork(); - if (!pid) - { - if (cmd->fd[0]) - dup2(cmd->fd[0], STDIN_FILENO); - if (cmd->fd[1]) - dup2(cmd->fd[1], STDOUT_FILENO); - if (builtin_call(datas, cmd) == -1) - exe(datas, cmd); - exit(EXIT_SUCCESS); - } - return pid; -} - -int piper(t_datas *datas, t_command *cmd) -{ - int pip[2]; - int status; - pid_t pid; - - if (cmd->fd[1] == 0 && cmd->next && cmd->next->fd[0] == 0) - { - pipe(pip); - cmd->fd[1] = pip[1]; - cmd->next->fd[0] = pip[0]; - } - pid = caller(datas, cmd); - if (cmd->fd[1]) - close(cmd->fd[1]); - if (cmd->next) - piper(datas, cmd->next); - waitpid(pid, &status, 0); - handle_status(datas, status); - if (cmd->fd[0]) - close(cmd->fd[0]); - return (1); -} - -t_command *parser(t_datas *datas, t_token *tok, t_command *prev) -{ - int i; - t_command *cmd; - - if (!tok) - return (NULL); - cmd = ft_calloc(1, sizeof(t_command)); - if (prev) - cmd->prev = prev; - i = 0; - cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); - while(tok && tok->type != PIPE) - { - if (tok->type) - update_redir(datas, cmd, tok); - else - cmd->argv[i++] = tok->value; - tok = tok->next; - } - if (DEBUG) - printf("######\n->cmd : %s, in_fd : %d, out_fd : %d\n", \ - cmd->argv[0], cmd->fd[0], cmd->fd[1]); - cmd->argv[i] = NULL; - if (tok && tok->type == PIPE) - cmd->next = parser(datas, tok->next, cmd); - return(cmd); -} - -t_token *lexer(t_datas *datas, char *line) -{ - t_lexer *lex; - char *tmp; - int tmp_i; - - lex = ft_calloc(1, sizeof *lex); - lex->state = ROOT_ST; - tmp = ft_calloc(1024, sizeof *tmp); - tmp_i = 0; - while (*line) - { - if (check_state(lex, &line)) - continue; - if (lex->state != S_QUOTE_ST && *line == '$') - tmp_i = replace_var(datas, &line, tmp, tmp_i); - if (check_register(lex, &line, tmp)) - { - tmp_i = 0; - ft_bzero(tmp, 1024); - continue ; - } - tmp[tmp_i] = *line; - line++; - tmp_i++; - } - create_token(lex, tmp); - return (lex->tokens); -} - int main(int argc, char **argv, char **envp) { t_command *cmd; diff --git a/minishell.h b/minishell.h index 7bab911..c5d08fd 100755 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ -/* Updated: 2022/05/05 20:46:00 by narnaud ### ########.fr */ +/* Updated: 2022/05/06 11:38:29 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,6 +52,7 @@ int is_empty(char *line); void halt(int ret_code); void termios(int ctl); void sigs_handler(int sig_num); +void handle_status(t_datas *datas, int status); // ----------------------------------Builtins.c int close_minishell(int exit_code); @@ -69,6 +70,9 @@ int ft_export(t_datas *datas, t_command *cmd); void exe(t_datas *datas, t_command *cmd); int builtin_call(t_datas *datas, t_command *cmd); int is_builtin(char *cmd); +pid_t caller(t_datas *datas, t_command *cmd); +int piper(t_datas *datas, t_command *cmd); + // ----------------------------------Parser.c typedef enum e_type @@ -79,17 +83,18 @@ typedef enum e_type ADD, IN, HD, -} t_type; +} t_type; typedef struct s_token { char *value; t_type type; struct s_token *next; -} t_token; - -size_t count_arguments(t_token *tok); -void update_redir(t_datas *datas, t_command *cmd, t_token *tok); +} t_token; +char *expend_str(t_datas *datas, char *line); +size_t count_arguments(t_token *tok); +void update_redir(t_datas *datas, t_command *cmd, t_token *tok); +t_command *parser(t_datas *datas, t_token *tok, t_command *prev); // ----------------------------------Lexer.c @@ -99,7 +104,7 @@ typedef enum e_state ROOT_ST, S_QUOTE_ST, D_QUOTE_ST, -} t_state; +} t_state; typedef struct s_lex { @@ -108,12 +113,12 @@ typedef struct s_lex t_type next_type; t_token *tokens; int n_elem; -} t_lexer; +} t_lexer; -int create_token(t_lexer *lex, char str[]); -int check_register(t_lexer *lex, char **line, char *tmp); -int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i); -int check_state(t_lexer *lex, char **line); -char *expend_str(t_datas *datas, char *line); +int create_token(t_lexer *lex, char str[]); +int check_register(t_lexer *lex, char **line, char *tmp); +int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i); +int check_state(t_lexer *lex, char **line); +t_token *lexer(t_datas *datas, char *line); #endif diff --git a/parser.c b/parser.c index 134d113..493df7a 100755 --- a/parser.c +++ b/parser.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 16:09:25 by narnaud #+# #+# */ -/* Updated: 2022/05/05 14:30:11 by mea ### ########.fr */ +/* Updated: 2022/05/06 11:21:18 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -92,4 +92,33 @@ void update_redir(t_datas *datas, t_command *cmd, t_token *tok) } } +t_command *parser(t_datas *datas, t_token *tok, t_command *prev) +{ + int i; + t_command *cmd; + + if (!tok) + return (NULL); + cmd = ft_calloc(1, sizeof(t_command)); + if (prev) + cmd->prev = prev; + i = 0; + cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); + while(tok && tok->type != PIPE) + { + if (tok->type) + update_redir(datas, cmd, tok); + else + cmd->argv[i++] = tok->value; + tok = tok->next; + } + if (DEBUG) + printf("######\n->cmd : %s, in_fd : %d, out_fd : %d\n", \ + cmd->argv[0], cmd->fd[0], cmd->fd[1]); + cmd->argv[i] = NULL; + if (tok && tok->type == PIPE) + cmd->next = parser(datas, tok->next, cmd); + return(cmd); +} + diff --git a/utils.c b/utils.c index 2d4da7e..6aa053a 100755 --- a/utils.c +++ b/utils.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/03 08:57:53 by narnaud #+# #+# */ -/* Updated: 2022/05/05 16:33:21 by mea ### ########.fr */ +/* Updated: 2022/05/06 11:39:05 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,10 +23,39 @@ int is_empty(char *line) return (1); } +void handle_status(t_datas *datas, int status) +{ + if (WIFSIGNALED(status)) + { + datas->exit_code = 128 + WTERMSIG(status); + if (datas->exit_code == 131) + printf("Quit: 3\n"); + } + else + datas->exit_code = WEXITSTATUS(status); +} + void nothing(int sig_num) { (void)sig_num; } + +void sigs_handler(int sig_num) +{ + if (sig_num == SIGINT) + printf("\n"); + rl_on_new_line(); + rl_replace_line("", 0); + rl_redisplay(); + return ; +} + +void halt(int ret_code) +{ + printf("exit\n"); + exit(ret_code); +} + void termios(int ctl) { struct termios termios_p; @@ -51,18 +80,4 @@ void termios(int ctl) tcsetattr(tty, TCSANOW, &termios_p); } -void sigs_handler(int sig_num) -{ - if (sig_num == SIGINT) - printf("\n"); - rl_on_new_line(); - rl_replace_line("", 0); - rl_redisplay(); - return ; -} -void halt(int ret_code) -{ - printf("exit\n"); - exit(ret_code); -}