diff --git a/42sh/Makefile b/42sh/Makefile index 64ceac65..45ff43da 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -67,8 +67,13 @@ $(D_OBJ)/%.o: $(D_SRC)/exec/%.c includes/exec.h @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) @echo "Compiling "$<"..." -libft/libft.a: - @$(MAKE) -C libft/ 2>/dev/null +libft/libft.a: libft/src/*/*.c + @echo "libft/libft.a" + @$(MAKE) -C libft 2>/dev/null + +libft: + @echo "libft" + @$(MAKE) -C libft 2>/dev/null clean: $(RM) $(D_OBJ) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 40cff420..afc7afa3 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2016/11/27 22:57:06 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 18:26:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,10 +16,7 @@ # include "minishell.h" # define PIPE_READ 0 # define PIPE_WRITE 1 -# define STDIN 0 -# define STDOUT 1 -typedef long long t_type; typedef struct s_exec t_exec; struct s_exec @@ -36,6 +33,10 @@ int exec_semi(t_btree *ast, t_data *data); int exec_pipe(t_btree *ast, t_data *data); int exec_less(t_btree *ast, t_data *data); int exec_great(t_btree *ast, t_data *data); +int exec_dgreat(t_btree *ast, t_data *data); int exec_command(t_btree *ast, t_data *data); +int ft_cmd_process(char **argv, t_data *data); +int ft_cmd_exec(char *execpath, char **argv, t_data *data); + #endif diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 5da45a13..cc077777 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 15:31:42 by jhalford #+# #+# */ -/* Updated: 2016/11/14 18:22:04 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 18:36:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 8cabd1b7..027851f9 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,12 +6,13 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2016/11/27 16:10:09 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 18:53:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef MINISHELL_H # define MINISHELL_H +# define SHELL_NAME "minishell" # include "libft.h" @@ -24,8 +25,8 @@ # include # include # include +# include -# define SHELL_NAME "minishell" typedef enum e_qstate t_qstate; typedef struct s_data t_data; @@ -45,8 +46,11 @@ struct s_data char *input; int input_pos; + t_list *qstack; t_qstate state_now; t_qstate state_last; + int fdin; + int fdout; }; extern t_stof g_builtins[]; @@ -55,18 +59,13 @@ extern pid_t g_pid; void sig_handler(int signo); int data_init(t_data *data); - -int ft_cmd_process(char **argv, char ***env_p); -int ft_cmd_exec(char *execpath, char **argv, char ***env_p); -char **ft_cmd_getav(char *cmd); - -int ft_builtin(char **av, char ***env); -int builtin_echo(char **av, char ***env); -int builtin_cd(char **av, char ***env); -int builtin_exit(char **av, char ***env); -int builtin_setenv(char **av, char ***env); -int builtin_unsetenv(char **av, char ***env); -int builtin_env(char **av, char ***env); +int ft_builtin(char **av, t_data *data); +int builtin_echo(char **av, t_data *data); +int builtin_cd(char **av, t_data *data); +int builtin_exit(char **av, t_data *data); +int builtin_setenv(char **av, t_data *data); +int builtin_unsetenv(char **av, t_data *data); +int builtin_env(char **av, t_data *data); void ft_expand_dollar(char **av, char **env); char *ft_findexec(char **path, char *file); diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 28a65a88..6f93613c 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 15:31:42 by jhalford #+# #+# */ -/* Updated: 2016/11/14 18:22:04 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 16:25:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin.c b/42sh/src/builtin/builtin.c index cc74c118..773c8feb 100644 --- a/42sh/src/builtin/builtin.c +++ b/42sh/src/builtin/builtin.c @@ -1,5 +1,16 @@ -#include "minishell.h" +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 14:21:34 by jhalford #+# #+# */ +/* Updated: 2016/11/28 14:27:36 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "minishell.h" t_stof g_builtin[] = { {"echo", &builtin_echo}, @@ -11,24 +22,18 @@ t_stof g_builtin[] = { {NULL, NULL}, }; -int ft_builtin(char **av, char ***env_p) +int ft_builtin(char **av, t_data *data) { int i; int ret; - char *exitval[2]; - char **env; i = 0; - env = *env_p; - exitval[0] = ft_strdup("?"); while (g_builtin[i].name) { if (ft_strcmp(g_builtin[i].name, *av) == 0) { - ret = (g_builtin[i].f)(av, env_p); - exitval[1] = ft_strdup(ft_itoa(ret)); - builtin_setenv(exitval, &env); - *env_p = env; + ret = (g_builtin[i].f)(av, data); + builtin_setenv((char*[3]){"?", ft_itoa(ret)}, data); return (1); } i++; diff --git a/42sh/src/builtin/builtin_cd.c b/42sh/src/builtin/builtin_cd.c index 5e4aee1f..236dfbd7 100644 --- a/42sh/src/builtin/builtin_cd.c +++ b/42sh/src/builtin/builtin_cd.c @@ -1,6 +1,6 @@ #include "minishell.h" -#define CDOPT_L 0b001 -#define CDOPT_P 0b010 +#define CDOPT_L 0x001 +#define CDOPT_P 0x002 #define HAS_CDOPT_P(x) (x & CD_OPT_P) #define HAS_CDOPT_L(x) (x & CD_OPT_L) #define CDERR_1 "cd: no such file or directory: %s\n" @@ -48,20 +48,17 @@ static int builtin_cd_opts(char **av, int *opts) return (i); } -int builtin_cd(char **av, char ***env_p) +int builtin_cd(char **av, t_data *data) { int i; int opts; char *target; - char *oldpwd[2]; - char *pwd[2]; opts = 0; i = builtin_cd_opts(av, &opts); - if (!(target = builtin_cd_special(av + i, *env_p))) + if (!(target = builtin_cd_special(av + i, data->env))) return (0); - oldpwd[0] = ft_strdup("OLDPWD"); - oldpwd[1] = getcwd(NULL, 0); + builtin_setenv((char*[3]){"OLDPWD", getcwd(NULL, 0)}, data); if (chdir(target)) { ft_printf(CDERR_1, target); @@ -69,9 +66,6 @@ int builtin_cd(char **av, char ***env_p) } else if (target != av[i]) ft_printf("%s\n", target); - pwd[0] = ft_strdup("PWD"); - pwd[1] = getcwd(NULL, 0); - builtin_setenv(pwd, env_p); - builtin_setenv(oldpwd, env_p); + builtin_setenv((char*[3]){"PWD", getcwd(NULL, 0)}, data); return (0); } diff --git a/42sh/src/builtin/builtin_echo.c b/42sh/src/builtin/builtin_echo.c index 35272ee0..ef8e64a4 100644 --- a/42sh/src/builtin/builtin_echo.c +++ b/42sh/src/builtin/builtin_echo.c @@ -1,8 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_echo.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 14:21:41 by jhalford #+# #+# */ +/* Updated: 2016/11/28 14:22:02 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "minishell.h" -int builtin_echo(char **av, char ***env_p) +int builtin_echo(char **av, t_data *data) { - (void)env_p; + (void)data; av++; while (*av) { diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 668ebcb5..972b9303 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -1,6 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_env.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ +/* Updated: 2016/11/28 14:28:37 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "minishell.h" -int builtin_env(char **av, char ***env_p) +int builtin_env(char **av, t_data *data) { int i; char **env; @@ -8,7 +20,7 @@ int builtin_env(char **av, char ***env_p) i = 1; env = NULL; if (!av[1]) - ft_sstrprint(*env_p, '\n'); + ft_sstrprint(data->env, '\n'); else { while (av[i] && ft_strchr(av[i], '=')) @@ -17,7 +29,7 @@ int builtin_env(char **av, char ***env_p) i++; } if (av[i]) - ft_cmd_process(av + i, env_p); + ft_cmd_process(av + i, data); } return (0); } diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index 0b086272..4202349c 100644 --- a/42sh/src/builtin/builtin_exit.c +++ b/42sh/src/builtin/builtin_exit.c @@ -1,14 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_exit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 14:28:41 by jhalford #+# #+# */ +/* Updated: 2016/11/28 14:30:07 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "minishell.h" -int builtin_exit(char **av, char ***env_p) +int builtin_exit(char **av, t_data *data) { int status; - (void)env_p; if (av[1]) status = ft_atoi(av[1]); else - status = ft_atoi(ft_getenv(*env_p, "?")); + status = ft_atoi(ft_getenv(data->env, "?")); exit(status); return (0); } diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index 3ac7a457..f8ef44cf 100644 --- a/42sh/src/builtin/builtin_setenv.c +++ b/42sh/src/builtin/builtin_setenv.c @@ -1,15 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_setenv.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ +/* Updated: 2016/11/28 14:29:13 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "minishell.h" -int builtin_setenv(char **av, char ***env_p) +int builtin_setenv(char **av, t_data *data) { char *str; char **env; - env = *env_p; + env = data->env; if (ft_strcmp(av[0], "setenv") == 0) av++; if (!av[0]) - ft_sstrprint(*env_p, '\n'); + ft_sstrprint(data->env, '\n'); else { str = ft_str3join(av[0], "=", av[1]); @@ -22,7 +34,7 @@ int builtin_setenv(char **av, char ***env_p) } env++; } - *env_p = ft_sstradd(*env_p, str); + data->env = ft_sstradd(data->env, str); } return (0); } diff --git a/42sh/src/builtin/builtin_unsetenv.c b/42sh/src/builtin/builtin_unsetenv.c index 9e1b4841..ead029cb 100644 --- a/42sh/src/builtin/builtin_unsetenv.c +++ b/42sh/src/builtin/builtin_unsetenv.c @@ -1,12 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_unsetenv.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 14:29:17 by jhalford #+# #+# */ +/* Updated: 2016/11/28 14:29:50 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "minishell.h" -int builtin_unsetenv(char **av, char ***env_p) +int builtin_unsetenv(char **av, t_data *data) { char **env; int i; int j; - env = *env_p; + env = data->env; /* ft_printf("builtin: %s\n", av[0]); */ i = 1; while (av[i]) diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index f2b2c848..e05934b4 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2016/11/27 23:46:57 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 18:34:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,9 +18,9 @@ int exec_command(t_btree *ast, t_data *data) node = ast->item; ft_putstr_fd("befor exec: ", 2); - ft_sstrprint(((t_astnode*)ast->item)->u_data.sstr, ','); - ft_putchar('\n'); - ft_cmd_process(node->u_data.sstr, &data->env); + ft_sstrprint_fd(2, ((t_astnode*)ast->item)->u_data.sstr, ','); + ft_putchar_fd('\n', 2); + ft_cmd_process(node->u_data.sstr, data); ft_putstr_fd("after exec\n", 2); return (0); } diff --git a/42sh/src/exec/exec_dgreat.c b/42sh/src/exec/exec_dgreat.c new file mode 100644 index 00000000..efe0e3e7 --- /dev/null +++ b/42sh/src/exec/exec_dgreat.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_dgreat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 18:15:13 by jhalford #+# #+# */ +/* Updated: 2016/11/28 18:36:11 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" + +int exec_dgreat(t_btree *ast, t_data *data) +{ + t_astnode *node; + int fd; + + node = ast->item; + fd = open(node->u_data.redir.u_word.word, O_WRONLY | O_APPEND | O_CREAT, 0644); + data->fdout = fd; + + ft_exec(ast->left, data); + + data->fdout = STDOUT; + return (0); +} diff --git a/42sh/src/exec/exec_great.c b/42sh/src/exec/exec_great.c index ee07977d..b757ae4d 100644 --- a/42sh/src/exec/exec_great.c +++ b/42sh/src/exec/exec_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */ -/* Updated: 2016/11/14 17:44:57 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 18:35:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,15 @@ int exec_great(t_btree *ast, t_data *data) { - (void)ast; - ft_putendl("exec_great"); + t_astnode *node; + int fd; + + node = ast->item; + fd = open(node->u_data.redir.u_word.word, O_WRONLY | O_TRUNC | O_CREAT, 0644); + data->fdout = fd; + ft_exec(ast->left, data); - ft_exec(ast->right, data); + + data->fdout = STDOUT; return (0); } diff --git a/42sh/src/exec/exec_less.c b/42sh/src/exec/exec_less.c index 2937adfc..2fe9a2d9 100644 --- a/42sh/src/exec/exec_less.c +++ b/42sh/src/exec/exec_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:27:08 by jhalford #+# #+# */ -/* Updated: 2016/11/14 17:45:04 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 18:19:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,13 @@ int exec_less(t_btree *ast, t_data *data) { - (void)ast; - ft_putendl("exec_less"); - ft_exec(ast->left, data); - ft_exec(ast->right, data); - return (0); + t_astnode *node; + int fd; + + node = ast->item; + fd = open(node->u_data.redir.u_word.word, O_RDONLY); + dup2(fd, node->u_data.redir.n); + close(fd); + + return (ft_exec(ast->left, data)); } diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index 1c23f5f0..20344d8c 100644 --- a/42sh/src/exec/exec_pipe.c +++ b/42sh/src/exec/exec_pipe.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 21:13:23 by jhalford #+# #+# */ -/* Updated: 2016/11/27 23:49:35 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 18:10:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,16 +14,22 @@ int exec_pipe(t_btree *ast, t_data *data) { - int filedes[2]; + int fds[2]; + + pipe(fds); + ft_dprintf(2, "pipe %i->%i\n", fds[PIPE_WRITE], fds[PIPE_READ]); + data->fdout = fds[PIPE_WRITE]; - pipe(filedes); - ft_printf("doing dup2\n"); - if ((dup2(filedes[1], 1)) == -1) - return (-1); ft_exec(ast->left, data); - close(filedes[PIPE_WRITE]); - dup2(filedes[0], 0); + + data->fdout = STDOUT; + data->fdin = fds[PIPE_READ]; + ft_exec(ast->right, data); - close(filedes[0]); + + close(fds[PIPE_WRITE]); + close(fds[PIPE_READ]); + data->fdin = STDIN; + data->fdout = STDOUT; return (0); } diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 6bca18c3..dcefd761 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2016/11/27 23:07:42 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 18:16:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ t_exec g_exec[] = {TK_PIPE, &exec_pipe}, {TK_LESS, &exec_less}, {TK_GREAT, &exec_great}, + {TK_DGREAT, &exec_dgreat}, {TK_COMMAND, &exec_command}, {0, 0}, }; diff --git a/42sh/src/lexer/ft_tokenize.c b/42sh/src/lexer/ft_tokenize.c index 24f27ff3..92dd69ba 100644 --- a/42sh/src/lexer/ft_tokenize.c +++ b/42sh/src/lexer/ft_tokenize.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:37:11 by jhalford #+# #+# */ -/* Updated: 2016/11/11 20:37:49 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 16:46:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_default.c b/42sh/src/lexer/lexer_default.c index 7ec6b06f..45e3996e 100644 --- a/42sh/src/lexer/lexer_default.c +++ b/42sh/src/lexer/lexer_default.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer_default.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */ +/* Updated: 2016/11/28 18:36:40 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "lexer.h" int lexer_default(t_list **alst, char *str) diff --git a/42sh/src/lexer/lexer_dquote.c b/42sh/src/lexer/lexer_dquote.c index 07506b13..0b9745ed 100644 --- a/42sh/src/lexer/lexer_dquote.c +++ b/42sh/src/lexer/lexer_dquote.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer_dquote.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 18:36:58 by jhalford #+# #+# */ +/* Updated: 2016/11/28 18:43:39 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "lexer.h" int lexer_dquote(t_list **alst, char *str) @@ -5,7 +17,7 @@ int lexer_dquote(t_list **alst, char *str) t_token *token; token = (*alst)->content; - token->type = WORD; + token->type = TK_WORD; if (*str == '\"') return (ft_tokenize(&(*alst)->next, str + 1, DEFAULT)); if (*str == '\\') diff --git a/42sh/src/lexer/qstate_update.c b/42sh/src/lexer/qstate_update.c index 6183d427..04a94439 100644 --- a/42sh/src/lexer/qstate_update.c +++ b/42sh/src/lexer/qstate_update.c @@ -1,35 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* qstate_update.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 16:56:11 by jhalford #+# #+# */ +/* Updated: 2016/11/28 19:27:44 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "minishell.h" void qstate_update(t_data *data, char c) { - t_qstate *now; - t_qstate *last; + t_qstate *state; + t_list **list; + t_list *tmp; - now = &data->state_now; - last = &data->state_last; - if (*now == Q_NONE) + list = &data->qstack; + state = (*list)->content; + ft_lstadd(list, ft_lstnew(NULL, sizeof(t_qstate))); + if (c == -1) + { + tmp = ft_lstpop(list); + ft_lstdelone(&tmp, &ft_lst_cfree); + return ; + } + else if (*state == Q_NONE) { if (c == '\\') - { - *last = Q_NONE; - *now = Q_BACKSLASH; - } - *now = c == '\'' ? Q_QUOTE : *now; - *now = c == '\"' ? Q_DQUOTE : *now; + *state = Q_BACKSLASH; + else if (c == '\"') + *state = Q_DQUOTE; + else if (c == '\'') + *state = Q_QUOTE; } - else if (*now == Q_QUOTE) - *now = c == '\'' ? Q_NONE : *now; - else if (*now == Q_DQUOTE) + else if (*state == Q_QUOTE) + { + if (c == '\'') + *state = Q_NONE; + } + else if (*state == Q_DQUOTE) { if (c == '\\') - { - *last = Q_DQUOTE; - *now = Q_BACKSLASH; - } - *now = c == '\"' ? Q_NONE : *now; - } - else if (*now == Q_BACKSLASH) - { - *now = *last; + *state = Q_BACKSLASH; + else if (c == '\"') + *state = Q_NONE; } + else if (*state == Q_BACKSLASH) + state = (*list)->next->next->content; + *((t_qstate *)(*list)->content) = *state; } diff --git a/42sh/src/lexer/token_print.c b/42sh/src/lexer/token_print.c index 38c16e90..90871dbe 100644 --- a/42sh/src/lexer/token_print.c +++ b/42sh/src/lexer/token_print.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* token_print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */ +/* Updated: 2016/11/28 14:39:08 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "lexer.h" void token_print(t_list *lst) @@ -7,7 +19,7 @@ void token_print(t_list *lst) while (lst) { token = lst->content; - ft_printf("%#06llx: '%s'\n", token->type, token->data); + ft_dprintf(2, "%#06llx: '%s'\n", token->type, token->data); lst = lst->next; } } diff --git a/42sh/src/line-editing/ft_interactive_sh.c b/42sh/src/line-editing/ft_interactive_sh.c index 502e8b0e..d4506c24 100644 --- a/42sh/src/line-editing/ft_interactive_sh.c +++ b/42sh/src/line-editing/ft_interactive_sh.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:44:21 by jhalford #+# #+# */ -/* Updated: 2016/11/11 14:39:10 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 19:28:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,7 +54,8 @@ int ft_interactive_sh(t_data *data) ft_strdel(&data->input); data->input = ft_memalloc(10); data->input_pos = 0; - data->state_now = Q_NONE; + data->qstack = ft_lstnew(NULL, sizeof(t_qstate)); + *((t_qstate*)data->qstack->content) = Q_NONE; ft_set_termios(data, 1); ft_prompt(); while (1) @@ -72,6 +73,7 @@ int ft_interactive_sh(t_data *data) return (-1); else if (ret == 2) { + ft_lstdel(&data->qstack, &ft_lst_cfree); ft_set_termios(data, 0); return (0); } diff --git a/42sh/src/line-editing/ft_key_default.c b/42sh/src/line-editing/ft_key_default.c index 97f37710..85603e0c 100644 --- a/42sh/src/line-editing/ft_key_default.c +++ b/42sh/src/line-editing/ft_key_default.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_key_basic.c :+: :+: :+: */ +/* ft_key_default.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/10 13:44:23 by jhalford #+# #+# */ -/* Updated: 2016/11/10 13:44:23 by jhalford ### ########.fr */ +/* Created: 2016/11/28 18:45:23 by jhalford #+# #+# */ +/* Updated: 2016/11/28 19:27:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_key_del.c b/42sh/src/line-editing/ft_key_del.c index 9751b075..435d8f3c 100644 --- a/42sh/src/line-editing/ft_key_del.c +++ b/42sh/src/line-editing/ft_key_del.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:44:26 by jhalford #+# #+# */ -/* Updated: 2016/11/10 13:44:26 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 18:54:02 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,11 +18,11 @@ int ft_key_del(t_data *data, char *buf) (void)data; (void)buf; - if (!data->input_pos) return (0); if (data->input[data->input_pos - 1] == '\n') return (0); + qstate_update(data, -1); ft_strcpy(data->input + data->input_pos - 1, data->input + data->input_pos); data->input_pos--; diff --git a/42sh/src/line-editing/ft_key_enter.c b/42sh/src/line-editing/ft_key_enter.c index f473d665..9707d654 100644 --- a/42sh/src/line-editing/ft_key_enter.c +++ b/42sh/src/line-editing/ft_key_enter.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:44:30 by jhalford #+# #+# */ -/* Updated: 2016/11/27 20:23:50 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 19:02:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_prompt.c b/42sh/src/line-editing/ft_prompt.c index 252bf9f7..32aa615e 100644 --- a/42sh/src/line-editing/ft_prompt.c +++ b/42sh/src/line-editing/ft_prompt.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 14:22:34 by jhalford #+# #+# */ -/* Updated: 2016/11/10 14:22:48 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 15:16:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 1f52a346..283a9dc4 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* data_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ +/* Updated: 2016/11/28 19:28:22 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "minishell.h" extern char **environ; @@ -6,6 +18,8 @@ int data_init(t_data *data) { data->env = ft_sstrdup(environ); data->history = NULL; + data->fdin = STDIN; + data->fdout = STDOUT; if (!(data->history = ft_dlstnew(NULL, 0))) return (-1); return (0); diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index 93ad72dc..cfdfe8a0 100644 --- a/42sh/src/main/ft_putast.c +++ b/42sh/src/main/ft_putast.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ -/* Updated: 2016/11/14 18:21:50 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 14:47:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/ft_putast2.c b/42sh/src/main/ft_putast2.c index 8b36d14f..df1e45f6 100644 --- a/42sh/src/main/ft_putast2.c +++ b/42sh/src/main/ft_putast2.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ -/* Updated: 2016/11/14 18:21:50 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 18:35:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,42 +18,42 @@ void ft_putast2(void *nodein) node = nodein; if (node->type == TK_SEMI) - ft_putendl("SEMI"); + ft_putendl_fd("SEMI", 2); else if (node->type == TK_PIPE) - ft_putendl("PIPE"); + ft_putendl_fd("PIPE", 2); else if (node->type == TK_COMMAND) { - ft_putstr("COMMAND: "); - ft_sstrprint(node->u_data.sstr, ','); - ft_putchar('\n'); + ft_putstr_fd("COMMAND: ", 2); + ft_sstrprint_fd(2, node->u_data.sstr, ','); + ft_putchar_fd('\n', 2); } else if (node->type & TK_REDIR) { - ft_putnbr(node->u_data.redir.n); + ft_putnbr_fd(node->u_data.redir.n, 2); if (node->type == TK_GREATAND || node->type == TK_LESSAND) { if (node->type == TK_GREATAND) - ft_putstr(">&:"); + ft_putstr_fd(">&:", 2); else - ft_putstr("<&:"); - ft_putnbr(node->u_data.redir.u_word.fd); + ft_putstr_fd("<&:", 2); + ft_putnbr_fd(node->u_data.redir.u_word.fd, 2); if (node->u_data.redir.close) - ft_putstr(" (closed)"); + ft_putstr_fd(" (closed)", 2); } else { if (node->type == TK_GREAT) - ft_putendl(">:"); + ft_putendl_fd(">:", 2); else if (node->type == TK_LESS) - ft_putstr("<:"); + ft_putstr_fd("<:", 2); else if (node->type == TK_DGREAT) - ft_putstr(">>:"); + ft_putstr_fd(">>:", 2); else if (node->type == TK_DLESS) - ft_putstr("<<:"); - ft_putstr(node->u_data.redir.u_word.word); + ft_putstr_fd("<<:", 2); + ft_putstr_fd(node->u_data.redir.u_word.word, 2); } - ft_putchar('\n'); + ft_putchar_fd('\n', 2); } else - ft_putendl("OTHER"); + ft_putendl_fd("OTHER", 2); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 5209ca6f..46f3fff9 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 21:13:34 by jhalford #+# #+# */ -/* Updated: 2016/11/27 23:17:56 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 19:28:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,29 +18,31 @@ int main(void) t_list *token; t_btree *ast; + token = NULL; if (data_init(&data)) return (1); if (signal(SIGINT, sig_handler) == SIG_ERR) - ft_printf("\ncan't catch SIGINT\n"); + ft_dprintf(STDERR, "\ncan't catch SIGINT\n"); while (1) { if (ft_interactive_sh(&data)) return (1); - ft_printf("command='%s'\n", data.input); + ft_dprintf(STDERR, "command='%s'\n", data.input); if (ft_tokenize(&token, data.input, DEFAULT)) return (1); + if (!token) + continue ; token_print(token); ast = NULL; if (ft_parse(&ast, &token)) return (1); - btree_print(ast, &ft_putast); - ft_printf("\n--- INFIX BREAKDOWN ---\n"); + btree_print(STDERR, ast, &ft_putast); + ft_dprintf(STDERR, "\n--- INFIX BREAKDOWN ---\n"); btree_apply_infix(ast, &ft_putast2); /* ft_lstdel(&token, &token_free); */ token = NULL; if (ft_exec(ast, &data)) return (1); - } return (0); } diff --git a/42sh/src/minishell-exec/ft_cmd.c b/42sh/src/minishell-exec/ft_cmd.c index 6d5d7a77..d6087da9 100644 --- a/42sh/src/minishell-exec/ft_cmd.c +++ b/42sh/src/minishell-exec/ft_cmd.c @@ -6,21 +6,21 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 21:13:18 by jhalford #+# #+# */ -/* Updated: 2016/11/27 22:55:31 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 18:32:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" extern pid_t g_pid; -int ft_cmd_process(char **argv, char ***env_p) +int ft_cmd_process(char **argv, t_data *data) { char **path; char *execpath; - path = ft_strsplit(ft_getenv(*env_p, "PATH"), ':'); - ft_expand_dollar(argv, *env_p); - if (ft_builtin(argv, env_p)) + path = ft_strsplit(ft_getenv(data->env, "PATH"), ':'); + ft_expand_dollar(argv, data->env); + if (ft_builtin(argv, data)) return (0); else if (ft_strchr(argv[0], '/')) execpath = argv[0]; @@ -29,10 +29,10 @@ int ft_cmd_process(char **argv, char ***env_p) ft_dprintf(2, "minishell: command not found: %s\n", argv[0]); return (-1); } - return (ft_cmd_exec(execpath, argv, env_p)); + return (ft_cmd_exec(execpath, argv, data)); } -int ft_cmd_exec(char *execpath, char **argv, char ***env_p) +int ft_cmd_exec(char *execpath, char **argv, t_data *data) { pid_t pid; int status; @@ -43,18 +43,36 @@ int ft_cmd_exec(char *execpath, char **argv, char ***env_p) ft_dprintf(2, "minishell: permission denied: %s\n", argv[0]); return (-1); } + ft_dprintf(2, "gonna fork, in=%i, out=%i\n", data->fdin, data->fdout); if ((pid = fork()) == -1) return (-1); else if (pid == 0) { - environ = ft_sstrdup(*env_p); + if (data->fdin != STDIN) + { + dup2(data->fdin, STDIN); + close(data->fdin); + } + if (data->fdout != STDOUT) + { + dup2(data->fdout, STDOUT); + close(data->fdout); + } + environ = ft_sstrdup(data->env); execve(execpath, argv, environ); } else { g_pid = pid; - wait(&status); - builtin_setenv((char*[2]){"?", ft_itoa(status)}, env_p); + if (data->fdin != STDIN) + close(data->fdin); + if (data->fdout != STDOUT) + close(data->fdout); + else + { + waitpid(pid, &status, 0); + builtin_setenv((char*[3]){"?", ft_itoa(status)}, data); + } } return (0); } diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index a21f9427..0c64b9f8 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 11:30:08 by jhalford #+# #+# */ -/* Updated: 2016/11/27 20:25:43 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 16:38:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_less.c b/42sh/src/parser/parse_less.c index 1038eb94..a0eb6aed 100644 --- a/42sh/src/parser/parse_less.c +++ b/42sh/src/parser/parse_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2016/11/14 17:47:33 by jhalford ### ########.fr */ +/* Updated: 2016/11/28 18:12:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_separator.c b/42sh/src/parser/parse_separator.c index daf59113..c16b4b4b 100644 --- a/42sh/src/parser/parse_separator.c +++ b/42sh/src/parser/parse_separator.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_separator.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/28 16:21:51 by jhalford #+# #+# */ +/* Updated: 2016/11/28 16:21:56 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "parser.h" int parse_separator(t_btree **ast, t_list **start, t_list **lst) diff --git a/42sh/test.c b/42sh/test.c deleted file mode 100644 index 6a6fd131..00000000 --- a/42sh/test.c +++ /dev/null @@ -1,50 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* test.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/27 21:15:49 by jhalford #+# #+# */ -/* Updated: 2016/11/27 23:50:57 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft/includes/libft.h" - -extern char **environ; - -#define PIPE_READ 0 -#define PIPE_WRITE 1 -#define STDIN 0 -#define STDOUT 1 - -int main(void) -{ - pid_t pid; - int fds[2]; - - pipe(fds); - if ((pid = fork()) == 0) - { - /* child */ - dup2(fds[PIPE_WRITE], STDOUT); - /* close(fds[PIPE_READ]); */ - /* close(fds[PIPE_WRITE]); */ - execv("/bin/ls", (char*[2]){"/bin/ls"}); - } - else - wait(NULL); - close(fds[PIPE_WRITE]); - if ((pid = fork()) == 0) - { - /* parent */ - dup2(fds[PIPE_READ], STDIN); - /* close(fds[PIPE_READ]); */ - execv("/usr/bin/wc", (char*[2]){"/usr/bin/wc", "-l"}); - } - else - wait(NULL); - close(fds[PIPE_WRITE]); - close(fds[PIPE_READ]); -}