From c4edead900fdd064e6a36aa52bbcd1b19aa7d565 Mon Sep 17 00:00:00 2001 From: narnaud Date: Tue, 3 May 2022 22:07:52 +0200 Subject: [PATCH] -added: signals restaured, -rm: some useless things --- Makefile | 4 +- README.md | 18 ++++++--- caller.c | 39 +++++++++++--------- minishell.c | 41 +++++++++------------ minishell.h | 7 +--- old_caller.c | 102 --------------------------------------------------- old_export.c | 90 --------------------------------------------- old_parser.c | 100 -------------------------------------------------- old_utils.c | 94 ----------------------------------------------- parser.c | 20 ++++++++-- utils.c | 54 ++------------------------- 11 files changed, 74 insertions(+), 495 deletions(-) delete mode 100755 old_caller.c delete mode 100755 old_export.c delete mode 100755 old_parser.c delete mode 100755 old_utils.c diff --git a/Makefile b/Makefile index ba5e415..d9a88ef 100755 --- a/Makefile +++ b/Makefile @@ -8,14 +8,14 @@ OBJS = ${SRCS:.c=.o} LIB = -lreadline -lhistory -L. -lft -$(NAME): $(OBJS) +$(NAME): $(LIBFT) $(OBJS) gcc ${OBJS} ${LIB} -o ${NAME} $(LIBFT): ${MAKE} -C ./libft cp ./libft/libft.a . -all: $(LIBFT) $(NAME) +all: $(NAME) %.o: %.c gcc -Werror -Wextra -Wall -g -c $< # -I ${READLINE_INC} #for rl_replace_line diff --git a/README.md b/README.md index 8d6b780..b108159 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,19 @@ -# Minishell - +#Minishell +--- ## TODO : - - +- free stack, +- handle environment, +- finish built-ins, +- handle multiples heredocs, +- better env management +- j'ai oubliƩ ><' +- ... ? ## Notes : - ## Issues : - +- cat | cat | ls crash on linux ? +- segfault on echo $PATH (g_env bad restaured) +- diff --git a/caller.c b/caller.c index f3c7eef..8289787 100644 --- a/caller.c +++ b/caller.c @@ -14,19 +14,19 @@ int command_call(char *argv[]) { - if (ft_strncmp(argv[0], "echo", 4) == 0) + if (ft_strncmp(argv[0], "echo", 5) == 0) return (ft_echo(argv)); - else if (ft_strncmp(argv[0], "pwd", 3) == 0) + else if (ft_strncmp(argv[0], "pwd", 4) == 0) return (ft_pwd(argv)); - else if (ft_strncmp(argv[0], "cd", 2) == 0) + else if (ft_strncmp(argv[0], "cd", 3) == 0) return (ft_cd(argv)); - else if (ft_strncmp(argv[0], "export", 6) == 0) + else if (ft_strncmp(argv[0], "export", 7) == 0) return (ft_export(argv)); - else if (ft_strncmp(argv[0], "unset", 5) == 0) + else if (ft_strncmp(argv[0], "unset", 6) == 0) return (ft_unset(argv)); - else if (ft_strncmp(argv[0], "env", 3) == 0) + else if (ft_strncmp(argv[0], "env", 4) == 0) return (ft_env(argv)); - else if (ft_strncmp(argv[0], "exit", 4) == 0) + else if (ft_strncmp(argv[0], "exit", 5) == 0) return (close_minishell(0)); else { @@ -41,17 +41,20 @@ void exe(char *argv[], char *const envp[]) char *path; size_t path_length; - execve(argv[0], argv, envp); - path_dirs = ft_split(getenv("PATH"), ':'); - while (path_dirs) + if (ft_strchr(argv[0], '/')) + execve(argv[0], argv, envp); + else { - path_length = ft_strlen(*path_dirs) + ft_strlen(argv[0]) + 2; - path = ft_calloc(path_length, sizeof(char)); - ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs)); - ft_memcpy(path + ft_strlen(*path_dirs),"/", 1); - ft_memcpy(path + ft_strlen(*path_dirs) + 1, argv[0], ft_strlen(argv[0])); - free(path); - execve(path, argv, envp); - path_dirs++; + path_dirs = ft_split(getenv("PATH"), ':'); + while (path_dirs) + { + path_length = ft_strlen(*path_dirs) + ft_strlen(argv[0]) + 2; + path = ft_calloc(path_length, sizeof(char)); + ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs)); + ft_memcpy(path + ft_strlen(*path_dirs),"/", 1); + ft_memcpy(path + ft_strlen(*path_dirs) + 1, argv[0], ft_strlen(argv[0])); + execve(path, argv, envp); + path_dirs++; + } } } diff --git a/minishell.c b/minishell.c index f629fda..9fa53c8 100644 --- a/minishell.c +++ b/minishell.c @@ -16,7 +16,6 @@ char **g_env; int caller(t_command *cmd) { - int cmd_ret; pid_t pid; int pip[2]; int status; @@ -30,16 +29,15 @@ int caller(t_command *cmd) pid = fork(); if (!pid) { - cmd_ret = command_call(cmd->argv); - if (cmd_ret == -1) + if (command_call(cmd->argv) == -1) { if (cmd->fd[0]) dup2(cmd->fd[0], STDIN_FILENO); if (cmd->fd[1]) dup2(cmd->fd[1], STDOUT_FILENO); exe(cmd->argv, cmd->envp); - exit(1); } + exit(1); } if (cmd->fd[1]) close(cmd->fd[1]); @@ -48,7 +46,7 @@ int caller(t_command *cmd) waitpid(pid, &status, 0); if (cmd->fd[0]) close(cmd->fd[0]); - return (cmd_ret); + return (1); } t_command *parser(t_token *tok, t_command *prev) @@ -111,31 +109,26 @@ t_token *lexer(char *line) return (lex->tokens); } -int launcher(t_env env) -{ - char *line; - - (void)env; - // TODO: handle ret - line = readline("$ "); - if (line == NULL) - halt(EXIT_FAILURE); - if (is_empty(line)) - return (EXIT_FAILURE); - caller(parser(lexer(line), NULL)); - add_history(line); - return (0); -} - int main(int argc, char **argv, char**envp) { + char *line; if (argc < 2) (void)argv; g_env = envp; - t_env env; - env.null = 0; + signal(SIGINT, sigs_handler); + signal(SIGQUIT, sigs_handler); while (1) - launcher(env); + { + termios(0); + line = readline("$ "); + termios(1); + if (line == NULL) + halt(EXIT_FAILURE); + if (is_empty(line)) + continue ; + caller(parser(lexer(line), NULL)); + add_history(line); + } clear_history(); return (EXIT_SUCCESS); } diff --git a/minishell.h b/minishell.h index 9297ae6..f289669 100644 --- a/minishell.h +++ b/minishell.h @@ -49,13 +49,10 @@ int ft_export(char **argv); void halt(int ret_code); int is_empty(char *line); -int init_file(char *filename, int mode); +void termios(int ctl); +void sigs_handler(int sig_num); // ----------------------------------Caller.c -typedef struct s_env -{ - int null; -} t_env; typedef struct s_command { diff --git a/old_caller.c b/old_caller.c deleted file mode 100755 index f6cf3bb..0000000 --- a/old_caller.c +++ /dev/null @@ -1,102 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* caller.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: narnaud +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/02/16 16:46:53 by narnaud #+# #+# */ -/* Updated: 2022/03/24 17:15:24 by narnaud ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -int command_call(char *argv[], int input_fd, int output_fd) -{ - if (ft_strncmp(argv[0], "echo", 4) == 0) - return (ft_echo(argv, output_fd)); - else if (ft_strncmp(argv[0], "pwd", 3) == 0) - return (ft_pwd(argv, input_fd, output_fd)); - else if (ft_strncmp(argv[0], "cd", 2) == 0) - return (ft_cd(argv, input_fd, output_fd)); - else if (ft_strncmp(argv[0], "export", 6) == 0) - return (ft_export(argv, input_fd, output_fd)); - else if (ft_strncmp(argv[0], "unset", 5) == 0) - return (ft_unset(argv, input_fd, output_fd)); - else if (ft_strncmp(argv[0], "env", 3) == 0) - return (ft_env(argv, input_fd, output_fd)); - else if (ft_strncmp(argv[0], "exit", 4) == 0) - return (close_minishell(0)); - else - { - errno = EINVAL; - return (-1); - } -} - -void exe(char *argv[], char *const envp[]) -{ - char **path_dirs; - char *path; - size_t path_length; - - - execve(argv[0], argv, envp); - path_dirs = ft_split(getenv("PATH"), ':'); - while (path_dirs) - { - path_length = ft_strlen(*path_dirs) + ft_strlen(argv[0]) + 2; - path = ft_calloc(path_length, sizeof(char)); - ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs)); - ft_memcpy(path + ft_strlen(*path_dirs),"/", 1); - ft_memcpy(path + ft_strlen(*path_dirs) + 1, argv[0], ft_strlen(argv[0])); - free(path); - execve(path, argv, envp); - path_dirs++; - } -} - -int caller(t_command *command, int input_fd, int output_fd) -{ - pid_t pid; - int cmd_ret; - int status; - - if (command->input != 0) - input_fd = command->fd[0]; - if (command->output != 1) - output_fd = command->fd[1]; - else if (!command->next) - output_fd = 1; - cmd_ret = command_call(command->argv, input_fd, output_fd); - if (cmd_ret == -1 && errno == EINVAL) - { - pid = fork(); - termios(1); - if (pid == 0) - { - if (command->prev || command->input || command->heredoc) - { - dup2(input_fd, STDIN_FILENO); - close(input_fd); - } - if (command->next || command->output) - dup2(output_fd, STDOUT_FILENO); - exe(command->argv, command->envp); - } - //signal(SIGINT, NULL); - //signal(SIGQUIT, NULL); - waitpid(pid, &status, 0); - //signal(SIGINT, sigs_handler); - //signal(SIGQUIT, sigs_handler); - termios(0); - if (command->output) - close(command->output); - if (WIFSIGNALED(status)) - return (-1); - if (!WIFEXITED(status)) - printf("Minishell: command not found: '%s'\n", command->argv[0]); - } - return (0); -} diff --git a/old_export.c b/old_export.c deleted file mode 100755 index 48dcf16..0000000 --- a/old_export.c +++ /dev/null @@ -1,90 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* export.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: narnaud +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */ -/* Updated: 2022/04/14 07:45:21 by narnaud ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -int ft_unset(char **argv, int input_fd, int output_fd) -{ - (void)output_fd; - (void)input_fd; - if (!argv[1]) - { - //read(input_fd, PIPE_MAX_SIZE); - return (1); - } - return (0); -} - -int ft_env(char **argv, int input_fd, int output_fd) -{ - (void)output_fd; - (void)input_fd; - (void)argv; - int i; - - i = 0; - while (g_env[i]) - { - printf("%s\n", g_env[i]); - i++; - } - return (0); -} - -int is_valid_identifier(char *ident) -{ - if (ft_isalpha(ident[0]) || ident[0] == '_') - { - while (*ident) - { - if (!ft_isalpha(*ident) && !ft_isdigit(*ident) && *ident != '_') - return (0); - ident++; - } - return (1); - } - return (0); -} - -int ft_export(char **argv, int input_fd, int output_fd) -{ - char **new; - char **env; - int i; - - (void)output_fd; - (void)input_fd; - i = 0; - new = ft_split(argv[1], '='); - if (!is_valid_identifier(new[0])) - { - printf ("export: not an identifier: %s\n", new[0]); - return (0); - } - while (g_env[i]) - { - env = ft_split(g_env[i], '='); - if (!ft_strncmp(new[0], env[0], ft_strlen(new[0]) + 1)) - { - g_env[i] = argv[1]; - return (1); - } - i++; - } - g_env[i] = argv[1]; - g_env[i + 1] = NULL; - return (1); -} - - - - diff --git a/old_parser.c b/old_parser.c deleted file mode 100755 index 3d8b819..0000000 --- a/old_parser.c +++ /dev/null @@ -1,100 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parser.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: narnaud +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/01/06 07:52:29 by narnaud #+# #+# */ -/* Updated: 2022/04/14 09:19:12 by narnaud ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - - -size_t count_arguments(t_token tok) -{ - size_t ret; - - ret = 0; - while (tok.value) - { - if (tok.type == 0 && tok.value[0] != '|') - ret++; - if (tok.next) - tok = *(tok.next); - else - break ; - } - return (ret); -} - -void update_redir(t_command *cmd, t_token tok) -{ - if (tok.type == OUT) - { - if (cmd->fd[1]) - close(cmd->fd[1]); - cmd->output = init_file(tok.value, O_TRUNC | O_WRONLY); - } - else if (tok.type == ADD) - { - if (cmd->fd[1]) - close(cmd->fd[1]); - cmd->fd[1] = init_file(tok.value, O_APPEND | O_WRONLY); - } - else if (tok.type == IN) - { - if (cmd->fd[0]) - close(cmd->fd[0]); - cmd->fd[0] = init_file(tok.value, O_RDONLY); - } - else if (tok.type == HD) - { - if (cmd->fd[0]) - close(cmd->fd[0]); - cmd->fd[0] = 0; - cmd->heredoc = tok.value; - } -} - -t_command *parser(t_token tok) -{ - int i; - t_command *cmd; - t_command *ret; - - cmd = ft_calloc(1, sizeof(t_command)); - ret = cmd; - while (tok.value) - { - i = 0; - cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); - while(tok.value[0] != '|') - { - if (tok.type) - update_redir(cmd, tok); - else - { - cmd->argv[i] = tok.value; - i++; - } - if (tok.next) - tok = *(tok.next); - else - break ; - } - //printf("Input : %d\n", cmd->input); - //printf("Output : %d\n", cmd->output); - cmd->argv[i] = NULL; - if (tok.next) - tok = *(tok.next); - else - break ; - cmd->next = ft_calloc(1, sizeof(t_command)); - cmd->next->prev = cmd; - cmd = cmd->next; - } - return (ret); -} diff --git a/old_utils.c b/old_utils.c deleted file mode 100755 index b9e2847..0000000 --- a/old_utils.c +++ /dev/null @@ -1,94 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* utils.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: narnaud +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/01/07 11:54:34 by narnaud #+# #+# */ -/* Updated: 2022/03/22 15:38:08 by narnaud ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -char **get_commands(char *line) -{ - char **split; - - split = ft_split(line, '|'); - return (split); -} - -int is_empty(char *line) -{ - while (*line) - { - if (*line <= 9 || (*line >= 13 && *line != 32)) - return (0); - line++; - } - return (1); -} - -int msg_n_ret(char *msg, int ret) -{ - printf("%s", msg); - return (ret); -} - -int init_file(char *filename, int mode) -{ - return (open(filename, mode | O_CREAT, 0644)); -} - -int is_space(char ch) -{ - if (ch == 32 || (ch > 8 && ch < 14)) - return (1); - else - return (0); -} - -void termios(int ctl) -{ - struct termios termios_p; - int tty; - - tty = ttyslot(); - if (tcgetattr(tty, &termios_p) < 0) - { - perror("stdin"); - exit (EXIT_FAILURE); - } - //termios_p.c_oflag &= CRDLY; - //termios_p.c_lflag &= ICANON; - if (ctl) - { - termios_p.c_lflag |= ECHOCTL; - termios_p.c_cc[VQUIT] = 28; - } - else - { - termios_p.c_lflag &= ~(ECHOCTL); - termios_p.c_cc[VQUIT] = 0; - } - tcsetattr(tty, TCSANOW, &termios_p); -} - -void sigs_handler(int sig_num) -{ - if (sig_num == SIGQUIT) - printf("QUIT: 3\n"); - else if (sig_num == SIGINT) - printf("\n"); - rl_on_new_line(); - rl_replace_line("", 0); - rl_redisplay(); - return ; -} -void halt(int ret_code) -{ - printf("exit"); - exit(ret_code); -} diff --git a/parser.c b/parser.c index fa62f86..7eeefb5 100644 --- a/parser.c +++ b/parser.c @@ -12,6 +12,20 @@ #include "minishell.h" +void push_heredoc(char *str, int fd) +{ + char *line; + + while(1) + { + line = readline(">"); + if (!ft_strncmp(line, str, ft_strlen(str) + 1)) + break; + ft_putstr_fd(line, fd); + write(fd, "\n", 1); + } +} + size_t count_arguments(t_token *tok) { size_t ret; @@ -32,19 +46,19 @@ void update_redir(t_command *cmd, t_token *tok) { if (cmd->fd[1]) close(cmd->fd[1]); - cmd->fd[1] = init_file(tok->value, O_TRUNC | O_WRONLY); + cmd->fd[1] = open(tok->value, O_CREAT | O_TRUNC | O_WRONLY, 0644); } else if (tok->type == ADD) { if (cmd->fd[1]) close(cmd->fd[1]); - cmd->fd[1] = init_file(tok->value, O_APPEND | O_WRONLY); + cmd->fd[1] = open(tok->value, O_CREAT | O_APPEND | O_WRONLY, 0644); } else if (tok->type == IN) { if (cmd->fd[0]) close(cmd->fd[0]); - cmd->fd[0] = init_file(tok->value, O_RDONLY); + cmd->fd[0] = open(tok->value, O_RDONLY); } else if (tok->type == HD) { diff --git a/utils.c b/utils.c index cc271f4..36ce084 100644 --- a/utils.c +++ b/utils.c @@ -12,28 +12,6 @@ #include "minishell.h" -void push_heredoc(char *str, int fd) -{ - char *line; - - while(1) - { - line = readline(">"); - if (!ft_strncmp(line, str, ft_strlen(str) + 1)) - break; - ft_putstr_fd(line, fd); - write(fd, "\n", 1); - } -} - -char **get_commands(char *line) -{ - char **split; - - split = ft_split(line, '|'); - return (split); -} - int is_empty(char *line) { while (*line) @@ -45,25 +23,6 @@ int is_empty(char *line) return (1); } -int msg_n_ret(char *msg, int ret) -{ - printf("%s", msg); - return (ret); -} - -int init_file(char *filename, int mode) -{ - return (open(filename, mode | O_CREAT, 0644)); -} - -int is_space(char ch) -{ - if (ch == 32 || (ch > 8 && ch < 14)) - return (1); - else - return (0); -} - void termios(int ctl) { struct termios termios_p; @@ -71,22 +30,14 @@ void termios(int ctl) tty = ttyslot(); if (tcgetattr(tty, &termios_p) < 0) - { - perror("stdin"); - exit (EXIT_FAILURE); - } + exit ((perror("stdin"), EXIT_FAILURE)); //termios_p.c_oflag &= CRDLY; //termios_p.c_lflag &= ICANON; if (ctl) - { termios_p.c_lflag |= ECHOCTL; - termios_p.c_cc[VQUIT] = 28; - } else - { termios_p.c_lflag &= ~(ECHOCTL); - termios_p.c_cc[VQUIT] = 0; - } + termios_p.c_cc[VQUIT] = ctl * 28; tcsetattr(tty, TCSANOW, &termios_p); } @@ -101,6 +52,7 @@ void sigs_handler(int sig_num) rl_redisplay(); return ; } + void halt(int ret_code) { printf("exit");