diff --git a/Makefile b/Makefile index 80cefda..e46ce2d 100755 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ NAME = minishell -LIBFT = 42-libft/libft.a -SRCS = minishell.c lexer.c parser.c caller.c built-in.c utils.c +LIBFT = libft.a +SRCS = minishell.c lexer.c parser.c utils.c OBJS = ${SRCS:.c=.o} READLINE_INC = ~/.brew/opt/readline/include @@ -10,8 +10,8 @@ $(NAME): $(OBJS) gcc ${OBJS} ${LIB} -o ${NAME} $(LIBFT): - ${MAKE} -C ./42-libft - cp ./42-libft/libft.a . + ${MAKE} -C ./libft + cp ./libft/libft.a . all: $(LIBFT) $(NAME) @@ -22,6 +22,8 @@ clean: rm -rf ${OBJS} fclean: clean + ${MAKE} -C ./libft fclean + rm libft.a rm -rf ${NAME} re: fclean all diff --git a/README.md b/README.md index b7ba04b..8d6b780 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,13 @@ # Minishell + +## TODO : + + + +## Notes : + + + +## Issues : + diff --git a/lexer.c b/lexer.c index ac96686..1edd571 100644 --- a/lexer.c +++ b/lexer.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */ -/* Updated: 2022/05/02 17:05:17 by narnaud ### ########.fr */ +/* Updated: 2022/05/03 09:19:36 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,9 +47,9 @@ int check_register(t_lexer *lex, char **line, char *tmp) if (lex->state != ROOT_ST) return (0); spaces = 0; - if (is_space(**line)) + if (ft_isspace(**line)) { - while (is_space(**line)) + while (ft_isspace(**line)) (*line)++; spaces = 1; } @@ -59,7 +59,7 @@ int check_register(t_lexer *lex, char **line, char *tmp) lex->next_type = ADD; else lex->next_type = OUT; - return (create_token(*lex, tmp)); + return (create_token(lex, tmp)); } else if (**line == '<' && (*line)++) { @@ -67,19 +67,18 @@ int check_register(t_lexer *lex, char **line, char *tmp) lex->next_type = HD; else lex->next_type = IN; - return (create_token(*lex, tmp)); + return (create_token(lex, tmp)); } else if (**line == '|') { - (*line)++; lex->next_type = PIPE; - create_token(*lex, tmp); - return (create_token(*lex, "|")); + create_token(lex, tmp); + return (create_token(lex, "|")); } if (!spaces) return (0); - return (create_token(l-*ex, tmp)); + return (create_token(lex, tmp)); } int replace_var(char **line, char *tmp, int tmp_i) diff --git a/libft/Makefile b/libft/Makefile index 08b2fe0..d2dd293 100755 --- a/libft/Makefile +++ b/libft/Makefile @@ -1,7 +1,7 @@ NAME = libft.a SRCS = is/ft_isalpha.c is/ft_isdigit.c is/ft_isascii.c is/ft_isprint.c \ - is/ft_isalnum.c + is/ft_isalnum.c is/ft_isspace.c SRCS += str/ft_len.c str/ft_lcpy.c str/ft_lcat.c str/ft_chr.c \ str/ft_rchr.c str/ft_ncmp.c str/ft_nstr.c str/ft_dup.c \ diff --git a/libft/is/ft_isspace.c b/libft/is/ft_isspace.c index 9cbbcba..139f0a3 100644 --- a/libft/is/ft_isspace.c +++ b/libft/is/ft_isspace.c @@ -6,13 +6,13 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/24 10:05:51 by narnaud #+# #+# */ -/* Updated: 2022/03/24 10:06:15 by narnaud ### ########.fr */ +/* Updated: 2022/05/03 09:47:28 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "../libft.h" -int ft_isspace(char c) +int ft_isspace(int c) { if (c > 8 && c < 14) return (1); diff --git a/libft/libft.h b/libft/libft.h index 91a6acc..2f6e695 100755 --- a/libft/libft.h +++ b/libft/libft.h @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/19 14:42:45 by narnaud #+# #+# */ -/* Updated: 2022/03/25 13:31:43 by narnaud ### ########.fr */ +/* Updated: 2022/05/03 09:48:13 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,6 +44,7 @@ int ft_isascii(int ch); int ft_isdigit(int ch); int ft_isprint(int ch); int ft_isalnum(int ch); +int ft_isspace(int ch); void *ft_memchr(const void *b, int c, size_t n); int ft_memcmp(const void *s1, const void *s2, size_t n); void *ft_memcpy(void *dst, const void *src, size_t n); diff --git a/minishell.c b/minishell.c index 733f430..ce91e2f 100644 --- a/minishell.c +++ b/minishell.c @@ -6,46 +6,38 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ -/* Updated: 2022/05/02 17:43:27 by narnaud ### ########.fr */ +/* Updated: 2022/05/03 10:39:02 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ # include "minishell.h" -t_command *parser(t_token *tok) +t_command *parser(t_token *tok, t_command *prev) { int i; t_command *cmd; - t_command *ret; + if (!tok) + return (NULL); cmd = ft_calloc(1, sizeof(t_command)); - ret = cmd; - while (tok->value) + if (prev) + cmd->prev = prev; + i = 0; + cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); + while(tok && tok->type != PIPE) { - i = 0; - cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); - while(tok->type != PIPE) - { - if (tok->type) - update_redir(cmd, tok); - else - cmd->argv[i++] = tok->value; - if (!tok->next) - break ; - tok = tok->next; - } - //printf("Input : %d\n", cmd->input); - //printf("Output : %d\n", cmd->output); - cmd->argv[i] = NULL; - if (tok->next) - tok = tok->next; + if (tok->type) + update_redir(cmd, tok); else - break ; - cmd->next = ft_calloc(1, sizeof(t_command)); - cmd->next->prev = cmd; - cmd = cmd->next; + cmd->argv[i++] = tok->value; + if (DEBUG) + printf("token : %s, type: %d\n", tok->value, tok->type); + tok = tok->next; } - return (ret); + cmd->argv[i] = NULL; + if (tok && tok->type == PIPE) + cmd->next = parser(tok->next, cmd); + return (cmd); } t_token *lexer(char *line) @@ -54,19 +46,21 @@ t_token *lexer(char *line) char *tmp; int tmp_i; - lex = ft_calloc(1, sizeof *ret); - lex->state = IN_ROOT; + 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) == 0) + if (DEBUG) + printf("%c\n", *line); + if (check_state(lex, &line)) continue; if (lex->state != S_QUOTE_ST && *line == '$') tmp_i = replace_var(&line, tmp, tmp_i); if (check_register(lex, &line, tmp)) { - i = 0; + tmp_i = 0; ft_bzero(tmp, 1024); continue ; } @@ -81,16 +75,17 @@ t_token *lexer(char *line) int launcher(t_env env) { char *line; - int ret; + (void)env; + // TODO: handle ret line = readline("$ "); if (line == NULL) halt(EXIT_FAILURE); if (is_empty(line)) return (EXIT_FAILURE); - ret = piper(parser(lexer(line))); + parser(lexer(line), NULL); add_history(line); - return (ret); + return (0); } int main(int argc, char **argv, char**envp) @@ -99,6 +94,7 @@ int main(int argc, char **argv, char**envp) (void)argv; (void)envp; t_env env; + env.null = 0; while (1) launcher(env); clear_history(); diff --git a/minishell.h b/minishell.h index 90c45e2..a5d5fd5 100644 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ -/* Updated: 2022/05/02 16:45:28 by narnaud ### ########.fr */ +/* Updated: 2022/05/03 09:34:27 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ # define PIPE_MAX_SIZE 8192 # define PATHS_MAX_SIZE 126 # define PROMPT_PREFIX 0 +# define DEBUG 1 # include "libft/libft.h" # include @@ -32,14 +33,26 @@ char **g_env; +// ----------------------------------Utils.c + +void halt(int ret_code); +int is_empty(char *line); +int init_file(char *filename, int mode); // ----------------------------------Piper.c +typedef struct s_env +{ + int null; +} t_env; + typedef struct s_command { char **argv; char **envp; int argc; int fd[2]; + int input; + int output; char *heredoc; struct s_command *next; struct s_command *prev; @@ -48,6 +61,16 @@ typedef struct s_command // ----------------------------------Parser.c +typedef enum e_type +{ + WORD, + PIPE, + OUT, + ADD, + IN, + HD, +} t_type; + typedef struct s_token { char *value; @@ -55,8 +78,8 @@ typedef struct s_token struct s_token *next; } t_token; -size_t count_arguments(t_token tok); -void update_redir(t_command *cmd, t_token tok); +size_t count_arguments(t_token *tok); +void update_redir(t_command *cmd, t_token *tok); // ----------------------------------Lexer.c @@ -68,16 +91,6 @@ typedef enum e_state D_QUOTE_ST, } t_state; -typedef enum e_type -{ - WORD, - PIPE, - OUT, - ADD, - IN, - HD, -} t_type; - typedef struct s_lex { t_state state; @@ -90,6 +103,9 @@ typedef struct s_lex int create_token(t_lexer *lex, char str[]); int check_register(t_lexer *lex, char **line, char *tmp); int replace_var(char **line, char *tmp, int tmp_i); -int check_state(t_lexer *lex, char *line, int i); +int check_state(t_lexer *lex, char **line); + + + #endif diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..cc271f4 --- /dev/null +++ b/utils.c @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/05/03 08:57:53 by narnaud #+# #+# */ +/* Updated: 2022/05/03 09:00:40 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#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) + { + 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); +}