From 985745f881edb8bff6e5795b60d510685f430f4f Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 3 Feb 2017 13:21:06 +0100 Subject: [PATCH 1/6] starting to fixi redirections --- 42sh/Makefile | 1 + 42sh/includes/parser.h | 3 ++- 42sh/src/exec/exec_great.c | 2 +- 42sh/src/exec/ft_exec.c | 2 +- 42sh/src/main/main.c | 2 +- 42sh/src/parser/ft_parse.c | 12 +++--------- 42sh/src/parser/parse_dgreat.c | 2 +- 42sh/src/parser/parse_great.c | 2 +- 42sh/src/parser/parse_redir.c | 34 ++++++++++++++++++++++++++++++++++ 9 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 42sh/src/parser/parse_redir.c diff --git a/42sh/Makefile b/42sh/Makefile index 8d286750..8a4eda07 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -187,6 +187,7 @@ parser/parse_great.c\ parser/parse_greatand.c\ parser/parse_less.c\ parser/parse_lessand.c\ +parser/parse_redir.c\ parser/parse_separator.c\ parser/parse_subshell.c\ parser/parse_word.c diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 3a6f5392..0bbc3553 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ -/* Updated: 2017/02/02 14:03:15 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 19:03:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,6 +60,7 @@ extern t_parser g_parser[]; int ft_parse(t_btree **ast, t_list **token); int parse_separator(t_btree **ast, t_list **start, t_list **lst); +int parse_redir(t_btree **ast, t_list **start, t_list **lst); int parse_less(t_btree **ast, t_list **start, t_list **lst); int parse_great(t_btree **ast, t_list **start, t_list **lst); int parse_dless(t_btree **ast, t_list **start, t_list **lst); diff --git a/42sh/src/exec/exec_great.c b/42sh/src/exec/exec_great.c index 6b410e7b..fa307617 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/12/13 17:14:19 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 19:06:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index e257cd9e..1b5f4c59 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: 2017/02/02 15:04:50 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 19:06:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 8fb8d026..931f4a63 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/02/02 15:45:17 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 18:57:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index beb0cfaa..fbcb39a2 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */ -/* Updated: 2017/02/02 15:45:33 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 19:02:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,12 +18,7 @@ t_parser g_parser[] = {TK_AND_IF | TK_OR_IF, &parse_separator}, {TK_AMP, &parse_separator}, {TK_PIPE, &parse_separator}, - {TK_LESS, &parse_less}, - {TK_GREAT, &parse_great}, - {TK_DLESS, &parse_dless}, - {TK_DGREAT, &parse_dgreat}, - {TK_LESSAND, &parse_lessand}, - {TK_GREATAND, &parse_greatand}, + {TK_REDIR, &parse_redir}, {TK_SUBSHELL, &parse_subshell}, {TK_WORD, &parse_word}, {0, 0}, @@ -48,8 +43,7 @@ int ft_parse(t_btree **ast, t_list **start) if ((lst = ft_lst_find(*start, &g_parser[i].type, &token_cmp_type))) { if (g_parser[i].f) - (*g_parser[i].f)(ast, start, &lst); - return (0); + return ((*g_parser[i].f)(ast, start, &lst)); } i++; } diff --git a/42sh/src/parser/parse_dgreat.c b/42sh/src/parser/parse_dgreat.c index 15d7b579..dcfe9a5d 100644 --- a/42sh/src/parser/parse_dgreat.c +++ b/42sh/src/parser/parse_dgreat.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/01/10 14:44:12 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 19:02:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_great.c b/42sh/src/parser/parse_great.c index 490965dc..f81c0de4 100644 --- a/42sh/src/parser/parse_great.c +++ b/42sh/src/parser/parse_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/01/10 14:45:50 by jhalford ### ########.fr */ +/* Updated: 2017/02/02 19:05:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_redir.c b/42sh/src/parser/parse_redir.c new file mode 100644 index 00000000..e937b1a3 --- /dev/null +++ b/42sh/src/parser/parse_redir.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_redir.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/02 18:58:27 by jhalford #+# #+# */ +/* Updated: 2017/02/02 19:04:32 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +int parse_redir(t_btree **ast, t_list **start, t_list **lst) +{ + t_token *tok; + + tok = (*lst)->content; + if (tok->type == TK_LESS) + return (parse_less(ast, start, lst)); + else if (tok->type == TK_GREAT) + return (parse_great(ast, start, lst)); + else if (tok->type == TK_DLESS) + return (parse_dless(ast, start, lst)); + else if (tok->type == TK_DGREAT) + return (parse_dgreat(ast, start, lst)); + else if (tok->type == TK_LESSAND) + return (parse_lessand(ast, start, lst)); + else if (tok->type == TK_GREATAND) + return (parse_greatand(ast, start, lst)); + else + return (-42); +} From 62be4bc4707158fe3a0e6a8a140722e2a8c810c6 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 3 Feb 2017 14:11:34 +0100 Subject: [PATCH 2/6] new architecture for redirection inside of fork(), chained redirects dont work properly yet --- 42sh/Makefile | 5 +- 42sh/includes/exec.h | 9 ++- 42sh/includes/lexer.h | 2 +- 42sh/includes/parser.h | 13 +---- 42sh/includes/types.h | 32 ++++++----- 42sh/src/exec/exec_command.c | 3 +- 42sh/src/exec/exec_dgreat.c | 27 --------- 42sh/src/exec/exec_less.c | 30 ---------- 42sh/src/exec/{exec_great.c => exec_redir.c} | 14 ++--- 42sh/src/exec/ft_exec.c | 6 +- 42sh/src/exec/process_do_redirection.c | 58 ++++++++++++++++++++ 42sh/src/exec/process_redirect.c | 10 +++- 42sh/src/job-control/process_free.c | 3 +- 42sh/src/lexer/ft_tokenize.c | 2 +- 42sh/src/main/data_init.c | 3 +- 42sh/src/parser/ft_parse.c | 2 +- 42sh/src/parser/parse_great.c | 2 +- 42sh/src/parser/parse_greatand.c | 3 +- 42sh/src/parser/parse_lessand.c | 3 +- 42sh/src/parser/parse_redir.c | 2 +- 20 files changed, 118 insertions(+), 111 deletions(-) delete mode 100644 42sh/src/exec/exec_dgreat.c delete mode 100644 42sh/src/exec/exec_less.c rename 42sh/src/exec/{exec_great.c => exec_redir.c} (72%) create mode 100644 42sh/src/exec/process_do_redirection.c diff --git a/42sh/Makefile b/42sh/Makefile index 8a4eda07..48bc5be4 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -44,15 +44,14 @@ exec/ast_free.c\ exec/exec_ampersand.c\ exec/exec_and_if.c\ exec/exec_command.c\ -exec/exec_dgreat.c\ -exec/exec_great.c\ -exec/exec_less.c\ exec/exec_or_if.c\ exec/exec_pipe.c\ +exec/exec_redir.c\ exec/exec_semi.c\ exec/ft_exec.c\ exec/ft_findexec.c\ exec/launch_process.c\ +exec/process_do_redirection.c\ exec/process_redirect.c\ exec/process_reset.c\ exec/process_setexec.c\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index aaee3466..0616b685 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: 2017/01/12 13:18:27 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 14:01:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,6 +47,7 @@ struct s_process pid_t pid; int fdin; int fdout; + t_list *redirs; int toclose; int status; t_flag attributes; @@ -77,16 +78,14 @@ int exec_ampersand(t_btree **ast); int exec_or_if(t_btree **ast); int exec_and_if(t_btree **ast); int exec_pipe(t_btree **ast); - -int exec_less(t_btree **ast); -int exec_great(t_btree **ast); -int exec_dgreat(t_btree **ast); +int exec_redir(t_btree **ast); int exec_command(t_btree **ast); int launch_process(t_process *p); int process_setexec(t_type type, t_process *p); int process_setgroup(t_process *p, pid_t pid); int process_redirect(t_process *p); +void process_do_redirection(t_redir *redir); void process_setsig(void); void process_free(void *content, size_t content_size); void process_reset(void); diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 0b4f3424..a3508bc6 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/02/02 15:36:09 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:25:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 0bbc3553..f5eae463 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ -/* Updated: 2017/02/02 19:03:31 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 14:01:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,14 +15,6 @@ # include "minishell.h" -typedef struct s_parser t_parser; -typedef struct s_ld t_ld; -typedef struct s_astnode t_astnode; -typedef struct s_redir t_redir; -typedef union u_astdata t_astdata; -typedef union u_word t_word; -typedef long long t_type; - struct s_parser { t_type type; @@ -37,9 +29,10 @@ union u_word struct s_redir { + t_flag type; int n; - int close; t_word word; + int close; }; union u_astdata diff --git a/42sh/includes/types.h b/42sh/includes/types.h index af8071d0..c2aea9ff 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -6,29 +6,33 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ -/* Updated: 2017/01/10 12:45:35 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 14:02:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef TYPES_H # define TYPES_H -typedef long long t_type; -typedef struct s_line t_line; -typedef struct s_comp t_comp; -typedef struct s_exec t_exec; -typedef struct s_jobc t_jobc; -typedef enum e_mode t_mode; +typedef struct s_data t_data; -typedef struct s_data t_data; -typedef enum e_qstate t_qstate; - -typedef struct s_job t_job; -typedef struct s_jobc t_jobc; -typedef struct s_execmap t_execmap; -typedef struct s_process t_process; typedef long long t_type; typedef long long t_flag; +typedef struct s_line t_line; +typedef struct s_comp t_comp; +typedef struct s_exec t_exec; +typedef struct s_jobc t_jobc; +typedef enum e_mode t_mode; + +typedef struct s_parser t_parser; +typedef struct s_ld t_ld; +typedef struct s_astnode t_astnode; +typedef struct s_redir t_redir; +typedef union u_astdata t_astdata; +typedef union u_word t_word; + +typedef struct s_job t_job; +typedef struct s_execmap t_execmap; +typedef struct s_process t_process; typedef int (t_execf)(const char *path, char *const argv[], char *const envp[]); t_data *data_singleton(); diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index e0f5e71b..b9db339a 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: 2017/02/02 15:45:32 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:45:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,6 +66,7 @@ int exec_command(t_btree **ast) p->av = NULL; p->pid = 0; p->attributes &= ~(PROCESS_STATE_MASK | PROCESS_TYPE_MASK); + p->redirs = NULL; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/exec_dgreat.c b/42sh/src/exec/exec_dgreat.c deleted file mode 100644 index 9897f54d..00000000 --- a/42sh/src/exec/exec_dgreat.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* exec_dgreat.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/28 18:15:13 by jhalford #+# #+# */ -/* Updated: 2016/12/13 17:13:58 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "exec.h" - -int exec_dgreat(t_btree **ast) -{ - t_astnode *node; - int fd; - - node = (*ast)->item; - fd = open(node->data.redir.word.word, O_WRONLY | O_APPEND | O_CREAT, 0644); - data_singleton()->exec.process.fdout = fd; - ft_exec(&(*ast)->left); - data_singleton()->exec.process.fdout = STDOUT; - btree_delone(ast, &ast_free); - return (0); -} diff --git a/42sh/src/exec/exec_less.c b/42sh/src/exec/exec_less.c deleted file mode 100644 index f27be538..00000000 --- a/42sh/src/exec/exec_less.c +++ /dev/null @@ -1,30 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* exec_less.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/14 17:27:08 by jhalford #+# #+# */ -/* Updated: 2016/12/13 17:14:46 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "exec.h" - -int exec_less(t_btree **ast) -{ - t_astnode *node; - int fd; - - node = (*ast)->item; - fd = open(node->data.redir.word.word, O_RDONLY); - data_singleton()->exec.process.fdin = fd; - /* ft_strappend(&data->exec.process.command, "<"); */ - /* ft_strappend(&data->exec.process.command, node->data.redir.word.word); */ - ft_exec(&(*ast)->left); - data_singleton()->exec.process.fdin = STDIN; - /* data->exec.process.command = NULL; */ - btree_delone(ast, &ast_free); - return (0); -} diff --git a/42sh/src/exec/exec_great.c b/42sh/src/exec/exec_redir.c similarity index 72% rename from 42sh/src/exec/exec_great.c rename to 42sh/src/exec/exec_redir.c index fa307617..d7becf4b 100644 --- a/42sh/src/exec/exec_great.c +++ b/42sh/src/exec/exec_redir.c @@ -1,27 +1,27 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* exec_great.c :+: :+: :+: */ +/* exec_redir.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */ -/* Updated: 2017/02/02 19:06:38 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:58:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec.h" -int exec_great(t_btree **ast) +int exec_redir(t_btree **ast) { t_astnode *node; - int fd; + t_process *p; + p = &data_singleton()->exec.process; node = (*ast)->item; - fd = open(node->data.redir.word.word, O_WRONLY | O_TRUNC | O_CREAT, 0644); - data_singleton()->exec.process.fdout = fd; + node->data.redir.type = node->type; + ft_lsteadd(&p->redirs, ft_lstnew(&node->data.redir,sizeof(node->data.redir))); ft_exec(&(*ast)->left); - data_singleton()->exec.process.fdout = STDOUT; btree_delone(ast, &ast_free); return (0); } diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 1b5f4c59..6bf0e751 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: 2017/02/02 19:06:47 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:45:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,7 @@ t_execmap g_execmap[] = {TK_SEMI, &exec_semi}, {TK_AMP, &exec_ampersand}, {TK_PIPE, &exec_pipe}, - {TK_LESS, &exec_less}, - {TK_GREAT, &exec_great}, - {TK_DGREAT, &exec_dgreat}, + {TK_REDIR, &exec_redir}, {TK_COMMAND | TK_SUBSHELL, &exec_command}, {0, 0}, }; diff --git a/42sh/src/exec/process_do_redirection.c b/42sh/src/exec/process_do_redirection.c new file mode 100644 index 00000000..b0ce8954 --- /dev/null +++ b/42sh/src/exec/process_do_redirection.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* process_do_redirection.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/03 13:46:40 by jhalford #+# #+# */ +/* Updated: 2017/02/03 14:10:01 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void process_do_redirection(t_redir *redir) +{ + int fdin; + int fdout; + + if (redir->type & TK_GREAT) + { + fdin = redir->n; + fdout = open(redir->word.word, O_WRONLY | O_TRUNC | O_CREAT, 0644); + DG("opened [%s] in fd[%i]", redir->word.word, fdout); + } + else if (redir->type & TK_DGREAT) + { + fdin = redir->n; + fdout = open(redir->word.word, O_WRONLY | O_APPEND | O_CREAT, 0644); + } + else if (redir->type & TK_LESS) + { + fdin = open(redir->word.word, O_WRONLY | O_TRUNC | O_CREAT, 0644); + fdout = redir->n; + } + else if (redir->type & (TK_LESSAND | TK_GREATAND)) + { + if (redir->close) + { + DG("gonna close(%i)", redir->n); + close(redir->n); + return ; + } + else + { + fdin = redir->type & TK_LESSAND ? redir->word.fd : redir->n; + fdout = redir->type & TK_LESSAND ? redir->n : redir->word.fd; + } + } + else + { + DG("redir->type not well set !"); + return ; + } + DG("gonna dup2(%i,%i)", fdout, fdin); + dup2(fdout, fdin); + close(fdout); +} diff --git a/42sh/src/exec/process_redirect.c b/42sh/src/exec/process_redirect.c index 77830bb0..4f3396ca 100644 --- a/42sh/src/exec/process_redirect.c +++ b/42sh/src/exec/process_redirect.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */ -/* Updated: 2017/01/10 14:31:15 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:46:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,14 @@ int process_redirect(t_process *p) { + t_list *redirs; + + redirs = p->redirs; + while (redirs) + { + process_do_redirection(redirs->content); + redirs = redirs->next; + } if (p->toclose != STDIN) close(p->toclose); if (p->fdin != STDIN) diff --git a/42sh/src/job-control/process_free.c b/42sh/src/job-control/process_free.c index f0f77c0a..ce23bf71 100644 --- a/42sh/src/job-control/process_free.c +++ b/42sh/src/job-control/process_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/02/02 15:45:25 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:59:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,5 +20,6 @@ void process_free(void *content, size_t content_size) p = content; ft_strdel(&p->path); ft_sstrfree(p->av); + ft_lstdel(&p->redirs, ft_lst_cfree); free(p); } diff --git a/42sh/src/lexer/ft_tokenize.c b/42sh/src/lexer/ft_tokenize.c index a2334864..cfdfe43d 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: 2017/02/02 15:34:45 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:26:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 5a0791ae..5b9a0fd9 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ -/* Updated: 2017/01/19 16:26:35 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 13:44:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,7 @@ int data_init(void) data->exec.process.fdout = STDOUT; data->exec.process.pid = 0; data->exec.process.attributes = PROCESS_PIPESTART | PROCESS_PIPEEND; + data->exec.process.redirs = NULL; data->exec.aol_status = NULL; data->exec.aol_search = 0; diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index fbcb39a2..ea512479 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */ -/* Updated: 2017/02/02 19:02:32 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:43:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_great.c b/42sh/src/parser/parse_great.c index f81c0de4..dffaf1a7 100644 --- a/42sh/src/parser/parse_great.c +++ b/42sh/src/parser/parse_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2017/02/02 19:05:52 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 14:07:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parse_greatand.c b/42sh/src/parser/parse_greatand.c index b4d6f657..7751fb83 100644 --- a/42sh/src/parser/parse_greatand.c +++ b/42sh/src/parser/parse_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2016/12/01 16:37:58 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 14:09:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ int parse_greatand(t_btree **ast, t_list **start, t_list **lst) node->type = TK_GREATAND; tok = (*lst)->content; and = ft_strchr(tok->data, '&'); + node->data.redir.n = *tok->data == '>' ? 1 : ft_atoi(tok->data); node->data.redir.word.fd = ft_atoi(and + 1); node->data.redir.close = tok->data[ft_strlen(tok->data) - 1] == '-' ? 1 : 0; diff --git a/42sh/src/parser/parse_lessand.c b/42sh/src/parser/parse_lessand.c index 8f5b0b9b..2a047610 100644 --- a/42sh/src/parser/parse_lessand.c +++ b/42sh/src/parser/parse_lessand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */ -/* Updated: 2016/12/01 16:37:54 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 14:07:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ int parse_lessand(t_btree **ast, t_list **start, t_list **lst) node->type = TK_LESSAND; tok = (*lst)->content; and = ft_strchr(tok->data, '&'); + node->data.redir.n = *tok->data == '<' ? 1 : ft_atoi(tok->data); node->data.redir.word.fd = ft_atoi(and + 1); node->data.redir.close = tok->data[ft_strlen(tok->data) - 1] == '-' ? 1 : 0; diff --git a/42sh/src/parser/parse_redir.c b/42sh/src/parser/parse_redir.c index e937b1a3..95625fd0 100644 --- a/42sh/src/parser/parse_redir.c +++ b/42sh/src/parser/parse_redir.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/02 18:58:27 by jhalford #+# #+# */ -/* Updated: 2017/02/02 19:04:32 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 13:58:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From 09cb6f78437cf19bd6d5688fbbe59a8f397ba4a0 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 3 Feb 2017 14:41:28 +0100 Subject: [PATCH 3/6] seems to pass initial testing for chained redirections, havent tried very far --- 42sh/src/exec/process_do_redirection.c | 28 ++++++++++---------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/42sh/src/exec/process_do_redirection.c b/42sh/src/exec/process_do_redirection.c index b0ce8954..dacd34b4 100644 --- a/42sh/src/exec/process_do_redirection.c +++ b/42sh/src/exec/process_do_redirection.c @@ -6,27 +6,28 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:46:40 by jhalford #+# #+# */ -/* Updated: 2017/02/03 14:10:01 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 14:41:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +int fd_is_valid(int fd) +{ + return (fcntl(fd, F_GETFD) != -1 || errno != EBADF); +} + void process_do_redirection(t_redir *redir) { int fdin; int fdout; - if (redir->type & TK_GREAT) + if (redir->type & (TK_GREAT | TK_DGREAT)) { fdin = redir->n; - fdout = open(redir->word.word, O_WRONLY | O_TRUNC | O_CREAT, 0644); - DG("opened [%s] in fd[%i]", redir->word.word, fdout); - } - else if (redir->type & TK_DGREAT) - { - fdin = redir->n; - fdout = open(redir->word.word, O_WRONLY | O_APPEND | O_CREAT, 0644); + fdout = open(redir->word.word, O_WRONLY | O_CREAT + (redir->type & TK_GREAT) ? O_TRUNC : O_APPEND, + 0644); } else if (redir->type & TK_LESS) { @@ -37,7 +38,6 @@ void process_do_redirection(t_redir *redir) { if (redir->close) { - DG("gonna close(%i)", redir->n); close(redir->n); return ; } @@ -47,12 +47,6 @@ void process_do_redirection(t_redir *redir) fdout = redir->type & TK_LESSAND ? redir->n : redir->word.fd; } } - else - { - DG("redir->type not well set !"); - return ; - } - DG("gonna dup2(%i,%i)", fdout, fdin); - dup2(fdout, fdin); + fd_is_valid(fdout) ? dup2(fdout, fdin) : close(fdin); close(fdout); } From f4efbdd31388130d7234671e47fd9f0b46e59ff3 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 3 Feb 2017 14:56:30 +0100 Subject: [PATCH 4/6] hotfix, forget to check compilation --- 42sh/includes/types.h | 2 +- 42sh/src/exec/process_do_redirection.c | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/42sh/includes/types.h b/42sh/includes/types.h index c2aea9ff..72e30a72 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ -/* Updated: 2017/02/03 14:02:56 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 14:54:04 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/process_do_redirection.c b/42sh/src/exec/process_do_redirection.c index dacd34b4..2cb1c02b 100644 --- a/42sh/src/exec/process_do_redirection.c +++ b/42sh/src/exec/process_do_redirection.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:46:40 by jhalford #+# #+# */ -/* Updated: 2017/02/03 14:41:01 by jhalford ### ########.fr */ +/* Updated: 2017/02/03 14:55:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ void process_do_redirection(t_redir *redir) { fdin = redir->n; fdout = open(redir->word.word, O_WRONLY | O_CREAT - (redir->type & TK_GREAT) ? O_TRUNC : O_APPEND, + | (redir->type & TK_GREAT) ? O_TRUNC : O_APPEND, 0644); } else if (redir->type & TK_LESS) @@ -47,6 +47,11 @@ void process_do_redirection(t_redir *redir) fdout = redir->type & TK_LESSAND ? redir->n : redir->word.fd; } } + else + { + ft_dprintf(2, "{red}%s: redirection error.", SHELL_NAME); + return ; + } fd_is_valid(fdout) ? dup2(fdout, fdin) : close(fdin); close(fdout); } From c0918e8e6b5412507d9abeb7c46a4375d73dcf85 Mon Sep 17 00:00:00 2001 From: gwojda Date: Fri, 3 Feb 2017 15:04:54 +0100 Subject: [PATCH 5/6] quoting + backslash + backquotes + parentheses + accolades revu : normalement ca marche. On va tenter d'ajouter la completion --- 42sh/Makefile | 1 + 42sh/includes/ft_readline.h | 37 ++++++++-- 42sh/src/line-editing/check_backslash.c | 93 ++++++++++++++++++++----- 42sh/src/line-editing/completion.c | 34 +++++++++ 42sh/src/line-editing/get_touch.c | 4 +- 42sh/src/line-editing/get_touch_toolz.c | 6 +- 42sh/src/line-editing/history.c | 4 +- 42sh/src/line-editing/history_parsing.c | 3 +- 42sh/src/line-editing/reader.c | 5 +- 42sh/src/line-editing/readline.c | 5 +- 10 files changed, 156 insertions(+), 36 deletions(-) create mode 100644 42sh/src/line-editing/completion.c diff --git a/42sh/Makefile b/42sh/Makefile index 0b60f83d..9f764f0a 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -143,6 +143,7 @@ lexer/token_init.c\ lexer/token_print.c\ line-editing/builtin_history.c\ line-editing/check_backslash.c\ +line-editing/completion.c\ line-editing/control_c_and_d.c\ line-editing/copy_cut_paste.c\ line-editing/ft_split_whitespaces.c\ diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h index c79f8815..ac99a2a2 100644 --- a/42sh/includes/ft_readline.h +++ b/42sh/includes/ft_readline.h @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */ -/* Updated: 2017/02/02 18:28:44 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 15:04:38 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,6 +52,27 @@ # define TOUCHE_F5 892427035 # define TOUCHE_F6 925981467 +# define PROMPT_QUOTES "quote> " +# define PROMPT_DQUOTES "dquote> " +# define PROMPT_BQUOTES "bquote> " +# define PROMPT_ACCOLADE "cursh> " +# define PROMPT_BRACKET "subsh> " +# define PROMPT_BSLASH "> " + +# define SIZE_PROMPT_QUOTES 7 +# define SIZE_PROMPT_DQUOTES 8 +# define SIZE_PROMPT_BQUOTES 8 +# define SIZE_PROMPT_ACCOLADE 7 +# define SIZE_PROMPT_BRACKET 7 +# define SIZE_PROMPT_BSLASH 2 + +# define IS_QUOTES 1 << 0 +# define IS_BQUOTES 1 << 1 +# define IS_DQUOTES 1 << 2 +# define IS_ACCOLADE 1 << 3 +# define IS_BRACKET 1 << 4 +# define IS_BSLASH 1 << 5 + # define HIST 1 # define ERROR_CNTL_R 1 @@ -82,8 +103,15 @@ typedef struct s_key void (*f)(void); } t_key; -extern t_key g_keys[]; +typedef struct s_prompt_type +{ + char key; + int value; + char *new_prompt; +} t_prompt_type; +extern t_key g_keys[]; +extern t_prompt_type g_prompt_tab[]; void ft_putnc(char c, int n); int ft_nbr_len(int nbr); @@ -109,10 +137,10 @@ void ft_realloc_str_history(char **str, size_t pos, int nb_his, int len); void ft_realloc_str_history_2(char **str, size_t pos, char *s); long long ft_pow(int nbr, int power); void ft_realloc_str_history_3(char **str, size_t pos, char *s); -void ft_check_backslash(char **str); char *ft_strget_history(char *str); int ft_nb_last_line(char *str, size_t pos); int ft_put(int nb); +void ft_check_line(void); char *ft_read_stdin(void); void ft_end(void); @@ -139,9 +167,8 @@ void ft_v(void); void ft_history_parsing(void); void ft_read_it(int input, size_t *pos, char **str); int ft_readline(void); +int ft_completion(int ret); void ft_check_heredoc(char **str); -void ft_check_quotes(char **s); - #endif diff --git a/42sh/src/line-editing/check_backslash.c b/42sh/src/line-editing/check_backslash.c index 1ab0256e..247d6828 100644 --- a/42sh/src/line-editing/check_backslash.c +++ b/42sh/src/line-editing/check_backslash.c @@ -6,30 +6,85 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 13:32:52 by gwojda #+# #+# */ -/* Updated: 2017/02/02 16:01:25 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 14:09:20 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void ft_check_backslash(char **str) +t_prompt_type g_prompt_tab[] = { - char *tmp1; - char *tmp2; + {IS_QUOTES ,SIZE_PROMPT_QUOTES ,PROMPT_QUOTES}, + {IS_BQUOTES ,SIZE_PROMPT_BQUOTES ,PROMPT_BQUOTES}, + {IS_DQUOTES ,SIZE_PROMPT_DQUOTES ,PROMPT_DQUOTES}, + {IS_ACCOLADE ,SIZE_PROMPT_ACCOLADE ,PROMPT_ACCOLADE}, + {IS_BRACKET ,SIZE_PROMPT_BRACKET ,PROMPT_BRACKET}, + {IS_BSLASH ,SIZE_PROMPT_BSLASH ,PROMPT_BSLASH}, + {0 ,0 ,0}, +}; - if (!*str || !**str) - return ; - if ((*str)[ft_strlen(*str) - 1] == '\\') - { - ft_putstr("> "); - data_singleton()->line.prompt_size = 2; - tmp1 = *str; - tmp2 = ft_strjoin(tmp1, "\n"); - free(tmp1); - tmp1 = ft_read_stdin(); - *str = ft_strjoin(tmp2, tmp1); - free(tmp1); - free(tmp2); - ft_putchar('\n'); - } +void ft_read_more(short c) +{ + char *str_tmp; + char *str_tmp2; + int i; + + i = 0; + str_tmp2 = data_singleton()->line.input; + str_tmp = ft_strjoin(str_tmp2, "\n"); + free(str_tmp2); + data_singleton()->line.input = NULL; + data_singleton()->line.pos = 0; + while (g_prompt_tab[i].key && !(g_prompt_tab[i].key & c)) + ++i; + data_singleton()->line.prompt_size = g_prompt_tab[i].value; + ft_printf("\n%s", g_prompt_tab[i].new_prompt); + str_tmp2 = ft_read_stdin(); + str_tmp2 = ft_strjoin(str_tmp, data_singleton()->line.input); + free(str_tmp); + free(data_singleton()->line.input); + data_singleton()->line.input = str_tmp2; + ft_check_line(); +} + +void ft_check_this_char(char c, short *status) +{ + int i; + char stats[] = {'\'', '`', '\"', '{', '(', '\\', '\0'}; + + i = 0; + while (stats[i] && stats[i] != c) + ++i; + if (!stats[i]) + return ; + if (((1 << i) & ~(*status))) + { + if (((1 << i) > *status && + (*status == 0 && !(IS_QUOTES & *status) && (!(IS_DQUOTES & *status)))) + || (((1 << i) == IS_BQUOTES) && !(IS_QUOTES & *status))) + (*status) = (*status) | (1 << i); + } + else + (*status) = (*status) ^ (1 << i); +} + +void ft_check_line(void) +{ + int i; + char *str; + short status; + + i = 0; + status = 0; + str = data_singleton()->line.input; + while (str[i]) + { + if (IS_BSLASH & status) + status = status ^ IS_BSLASH; + else + ft_check_this_char(str[i], &status); + ++i; + } + if (status) + ft_read_more(status); } diff --git a/42sh/src/line-editing/completion.c b/42sh/src/line-editing/completion.c new file mode 100644 index 00000000..beb64572 --- /dev/null +++ b/42sh/src/line-editing/completion.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* completion.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/03 14:15:55 by gwojda #+# #+# */ +/* Updated: 2017/02/03 15:03:17 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int ft_completion(int ret) +{ + size_t tmp; + + if ((ret != TOUCHE_TAB && ret != 10) + || (ret == 10 && !(data_singleton()->comp))) + return (0); + data_singleton()->line.pos = tmp; + if (ret == 10) + ft_puttermcaps("cd"); + if (data_singleton()->comp || ret == TOUCHE_TAB) + completion(ret); + if (ret == 10) + { + ft_current_str(data_singleton()->line.input, tmp); + ft_get_next_str(data_singleton()->line.input, &tmp); + data_singleton()->line.pos = tmp; + } + return (1); +} diff --git a/42sh/src/line-editing/get_touch.c b/42sh/src/line-editing/get_touch.c index 2ecde44c..cd607266 100644 --- a/42sh/src/line-editing/get_touch.c +++ b/42sh/src/line-editing/get_touch.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 16:28:49 by gwojda #+# #+# */ -/* Updated: 2017/02/02 18:27:07 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 14:46:22 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,6 +54,8 @@ char *ft_read_stdin(void) ret = 0; j = 0; read(0, &ret, sizeof(int)); + if (ft_completion(ret)) + continue ; while (g_key[j].value && g_key[j].value != ret) ++j; if (g_key[j].value) diff --git a/42sh/src/line-editing/get_touch_toolz.c b/42sh/src/line-editing/get_touch_toolz.c index e5937fac..f99820dc 100644 --- a/42sh/src/line-editing/get_touch_toolz.c +++ b/42sh/src/line-editing/get_touch_toolz.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 16:42:54 by gwojda #+# #+# */ -/* Updated: 2017/02/02 15:22:35 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 12:08:45 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -73,13 +73,15 @@ void ft_found_prev_word(void) void ft_found_next_word(void) { - int i; + int i; char *str; size_t *pos; str = data_singleton()->line.input; pos = &data_singleton()->line.pos; i = 0; + if (!str) + return ; while (str[i + *pos] && str[i + *pos] == ' ') { ft_putchar(str[i + *pos]); diff --git a/42sh/src/line-editing/history.c b/42sh/src/line-editing/history.c index 295af73e..cd587a51 100644 --- a/42sh/src/line-editing/history.c +++ b/42sh/src/line-editing/history.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/02 15:22:19 by gwojda #+# #+# */ -/* Updated: 2017/02/02 18:04:16 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 11:55:36 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ void ft_history_down(void) str = &data_singleton()->line.input; pos = &data_singleton()->line.pos; head = data_singleton()->line.list_cur; - if (!head || !*str) + if (!head) return ; if (*str) { diff --git a/42sh/src/line-editing/history_parsing.c b/42sh/src/line-editing/history_parsing.c index bd3f67d4..1da1f4dd 100644 --- a/42sh/src/line-editing/history_parsing.c +++ b/42sh/src/line-editing/history_parsing.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/25 11:39:47 by gwojda #+# #+# */ -/* Updated: 2017/02/02 16:20:26 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 11:57:07 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,7 +59,6 @@ static void ft_history_parsing_2(void) data_singleton()->line.input = ft_read_stdin(); ft_putchar('\n'); data_singleton()->line.opt = data_singleton()->line.opt | ~HIST; - ft_check_quotes(&data_singleton()->line.input); ft_check_heredoc(&data_singleton()->line.input); ft_history_parsing(); } diff --git a/42sh/src/line-editing/reader.c b/42sh/src/line-editing/reader.c index 4a3e17d3..22d41fd2 100644 --- a/42sh/src/line-editing/reader.c +++ b/42sh/src/line-editing/reader.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 16:52:57 by gwojda #+# #+# */ -/* Updated: 2017/01/25 19:07:39 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 11:40:10 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,6 +61,7 @@ void ft_read_it(int input, size_t *pos, char **str) return ; ft_read_it_2(input, t); ft_read_it_3(str, t, pos, &j); - ft_putstr((*str) + (*pos) - j); +// ft_current_str((*str) + (*pos) - j, *pos); +// ft_get_next_str((*str) + (*pos) - j, pos); ft_putnc('\b', ft_strlen((*str)) - ((*pos))); } diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index f48668b4..d2e0a2f1 100644 --- a/42sh/src/line-editing/readline.c +++ b/42sh/src/line-editing/readline.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/02/02 18:03:23 by gwojda ### ########.fr */ +/* Updated: 2017/02/03 12:33:51 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -82,10 +82,9 @@ int ft_readline(void) data_singleton()->line.pos = 0; ft_prompt(); data_singleton()->line.input = ft_read_stdin(); + ft_check_line(); ft_putchar('\n'); - ft_check_quotes(&data_singleton()->line.input); ft_check_heredoc(&data_singleton()->line.input); - ft_check_backslash(&data_singleton()->line.input); ft_history_parsing(); if (data_singleton()->line.input) { From 3e2f3eae76133226a6ca098adc3bcb561df5be49 Mon Sep 17 00:00:00 2001 From: gwojda Date: Fri, 3 Feb 2017 15:05:28 +0100 Subject: [PATCH 6/6] quoting + backslash + backquotes + parentheses + accolades revu : normalement ca marche. On va tenter d'ajouter la completion --- 42sh/src/line-editing/quotes_gest.c | 88 ----------------------------- 1 file changed, 88 deletions(-) delete mode 100644 42sh/src/line-editing/quotes_gest.c diff --git a/42sh/src/line-editing/quotes_gest.c b/42sh/src/line-editing/quotes_gest.c deleted file mode 100644 index c4be15f9..00000000 --- a/42sh/src/line-editing/quotes_gest.c +++ /dev/null @@ -1,88 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* quotes_gest.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: gwojda +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/01/05 12:20:19 by gwojda #+# #+# */ -/* Updated: 2017/02/02 17:53:41 by gwojda ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -int ft_check_quotes_num(char *s) -{ - int i; - char simp; - char doub; - - i = 0; - simp = 0; - doub = 0; - while (s[i]) - { - if (s[i] == '\'' && ((i && s[i - 1] != '\\') || !i)) - { - simp = 1; - ++i; - while (s[i] && !(s[i] == '\'' && ((i && s[i - 1] != '\\') || !i))) - ++i; - if (s[i] == '\'' && ((i && s[i - 1] != '\\') || !i)) - simp = 0; - } - else if (s[i] == '"' && ((i && s[i - 1] != '\\') || !i)) - { - doub = 1; - ++i; - while (s[i] && !(s[i] == '"' && ((i && s[i - 1] != '\\') || !i))) - ++i; - if (s[i] == '"' && ((i && s[i - 1] != '\\') || !i)) - doub = 0; - } - if (s[i]) - ++i; - } - if (doub) - return (2); - else if (simp) - return (1); - return (0); -} - -void ft_check_quotes(char **s) -{ - int ret; - char *tmp; - char *tmp2; - int prompt_size_mem; - t_list_history *head; - - head = data_singleton()->line.list_beg; - if (!*s) - return ; - while ((ret = (ft_check_quotes_num(*s)))) - { - if (ret == 1) - ft_putstr("squote> "); - else if (ret == 2) - ft_putstr("dquote> "); - tmp = *s; - *s = ft_strjoin(*s, "\n"); - ft_strdel(&tmp); - tmp = *s; - prompt_size_mem = data_singleton()->line.prompt_size; - data_singleton()->line.input = NULL; - data_singleton()->line.pos = 0; - tmp2 = ft_read_stdin(); - data_singleton()->line.prompt_size = prompt_size_mem; - ft_putchar('\n'); - if (!tmp2) - continue ; - *s = ft_strjoin(tmp, tmp2); - data_singleton()->line.input = *s; - ft_strdel(&tmp); - ft_strdel(&tmp2); - } -}