From ee7ddc5e14d57337a1f4054ddac46f9ab8fd2439 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 20 Jan 2017 19:34:18 +0100 Subject: [PATCH 1/9] builtin_read first commit --- 42sh/includes/builtin.h | 3 +- 42sh/includes/builtin_read.h | 55 +++++++++++++++++ 42sh/includes/lexer.h | 2 +- 42sh/src/builtin/builtin_read.c | 103 ++++++++++++++++++++++++++++++++ 42sh/src/builtin/is_builtin.c | 3 +- 42sh/src/main/main.c | 2 +- 6 files changed, 164 insertions(+), 4 deletions(-) create mode 100644 42sh/includes/builtin_read.h create mode 100644 42sh/src/builtin/builtin_read.c diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index 42f2543a..248a41ac 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:21:56 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:57:22 by jhalford ### ########.fr */ +/* Updated: 2017/01/20 15:11:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ # include "types.h" # include "libft.h" +# include "builtin_read.h" t_execf *is_builtin(t_process *p); int builtin_env(const char *path, char *const argv[], char *const envp[]); diff --git a/42sh/includes/builtin_read.h b/42sh/includes/builtin_read.h new file mode 100644 index 00000000..c51f201d --- /dev/null +++ b/42sh/includes/builtin_read.h @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_read.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */ +/* Updated: 2017/01/20 19:32:45 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BUILTIN_H +# define BUILTIN_H + +# include "types.h" +# include "libft.h" +# include "builtin.h" +# include "minishell.h" + +# define READ_OPT_LA (1 << 0) +# define READ_OPT_LD (1 << 1) +# define READ_OPT_LE (1 << 2) +# define READ_OPT_LI (1 << 3) +# define READ_OPT_LN (1 << 4) +# define READ_OPT_UN (1 << 5) +# define READ_OPT_LP (1 << 6) +# define READ_OPT_LR (1 << 7) +# define READ_OPT_LS (1 << 8) +# define READ_OPT_LT (1 << 9) +# define READ_OPT_LU (1 << 10) + +typedef struct s_read t_read; + +struct s_read +{ + char delim; + int nchars; + char *prompt; + int timeout; + int fd; +}; + +struct s_readopt +{ + char letter; + t_flag flag; + int (*get)(t_read *data, char *arg); +}; + +extern t_readopt g_readtab[]; + +int builtin_read(const char *path, char *const av[], char *const envp[]); + +#endif diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 03276719..32641bb4 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/01/12 14:57:41 by jhalford ### ########.fr */ +/* Updated: 2017/01/20 15:24:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c new file mode 100644 index 00000000..f9137519 --- /dev/null +++ b/42sh/src/builtin/builtin_read.c @@ -0,0 +1,103 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_read.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */ +/* Updated: 2017/01/20 19:32:44 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "builtin_read.h" + +t_readopt g_readtab[] = +{ + {'a', READ_OPT_LA, NULL}, + {'d', READ_OPT_LD, NULL}, + {'e', READ_OPT_LE, NULL}, + {'i', READ_OPT_LI, NULL}, + {'n', READ_OPT_LN, NULL}, + {'N', READ_OPT_UN, NULL}, + {'p', READ_OPT_LP, NULL}, + {'r', READ_OPT_LR, NULL}, + {'s', READ_OPT_LS, NULL}, + {'t', READ_OPT_LT, NULL}, + {'u', READ_OPT_LU, NULL}, + {0, 0, 0}, +}; + +void bt_read_init(t_read *data) +{ + data.delim = '\n'; + data.nchars = -1; + data.prompt = NULL; + data.timeout = -1; + data.fd = 0; +} + +t_readopt bt_read_getopt(char letter) +{ + int i; + + i = 0; + while (g_readtab[i].letter) + { + if (g_readtab[i].letter == letter) + return (g_readtab[i]); + } + return (NULL); +} + +int bt_read_parse(t_read *data, char **av) +{ + int i; + int j; + int k; + t_readopt opt; + + while (av[i]) + { + j = 0; + if (av[i][j++] == '-') + { + if (av[i][j] == '-') + { + i++; + break ; + } + while (av[i][j]) + { + if (!(opt = bt_read_getopt(av[i][j]))) + { + ft_dprintf(2, "%s: bad option: %c", SHELL_NAME, av[i][j]); + return (2); + } + data->opts |= opt.flag; + if (data->get) + { + (*data->get)(data, av[i + 1]); + break ; + } + j++; + } + } + i++; + } + if (av[i]) + bt_read_getnames(()) + return (0); +} + +int builtin_read(const char *path, char *const av[], char *const envp[]) +{ + t_read data; + int i; + + (void)path; + (void)envp; + bt_read_init(&data); + if ((i = bt_read_parse(&data, (char **)av))) + return (i); +} diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index 7dbdda0c..7b1e781e 100644 --- a/42sh/src/builtin/is_builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */ -/* Updated: 2017/01/09 16:58:13 by jhalford ### ########.fr */ +/* Updated: 2017/01/20 15:01:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ t_stof g_builtin[] = { {"jobs", &builtin_jobs}, {"fg", &builtin_fg}, {"bg", &builtin_bg}, + {"read", &builtin_read}, {NULL, NULL}, }; diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 27e00034..fafa3727 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/01/12 14:02:30 by jhalford ### ########.fr */ +/* Updated: 2017/01/20 15:23:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From 8e3f84cef1668b3f986d1da211c9d0265cae29dc Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sun, 22 Jan 2017 18:23:21 +0100 Subject: [PATCH 2/9] finished parsing options, read loop done. few options are missing still, also have a break problem : read doesnt stop reading even when i dopnt want anymore input... --- 42sh/includes/builtin.h | 2 +- 42sh/includes/builtin_read.h | 17 +++- 42sh/includes/minishell.h | 2 +- 42sh/libft | 2 +- 42sh/src/builtin/bt_read_get.c | 43 ++++++++++ 42sh/src/builtin/builtin_read.c | 103 +++++++++++++++++------- 42sh/src/builtin/is_builtin.c | 5 +- 42sh/src/lexer/lexer_number.c | 3 +- 42sh/src/lexer/token_append.c | 10 ++- 42sh/src/line-editing/curs_setup.c | 2 +- 42sh/src/line-editing/curs_term_setup.c | 2 +- 42sh/src/line-editing/ft_readline.c | 2 +- 42sh/src/main/main.c | 2 +- 13 files changed, 147 insertions(+), 48 deletions(-) create mode 100644 42sh/src/builtin/bt_read_get.c diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index 248a41ac..f2324223 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:21:56 by jhalford #+# #+# */ -/* Updated: 2017/01/20 15:11:37 by jhalford ### ########.fr */ +/* Updated: 2017/01/21 18:42:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/builtin_read.h b/42sh/includes/builtin_read.h index c51f201d..feeac781 100644 --- a/42sh/includes/builtin_read.h +++ b/42sh/includes/builtin_read.h @@ -6,12 +6,12 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */ -/* Updated: 2017/01/20 19:32:45 by jhalford ### ########.fr */ +/* Updated: 2017/01/22 18:04:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef BUILTIN_H -# define BUILTIN_H +#ifndef BUILTIN_READ_H +# define BUILTIN_READ_H # include "types.h" # include "libft.h" @@ -30,15 +30,18 @@ # define READ_OPT_LT (1 << 9) # define READ_OPT_LU (1 << 10) -typedef struct s_read t_read; +typedef struct s_read t_read; +typedef struct s_readopt t_readopt; struct s_read { + t_flag opts; char delim; int nchars; char *prompt; int timeout; int fd; + char **names; }; struct s_readopt @@ -52,4 +55,10 @@ extern t_readopt g_readtab[]; int builtin_read(const char *path, char *const av[], char *const envp[]); +int bt_read_getdelim(t_read *data, char *arg); +int bt_read_getnchars(t_read *data, char *arg); +int bt_read_getprompt(t_read *data, char *arg); +int bt_read_gettimeout(t_read *data, char *arg); +int bt_read_getfd(t_read *data, char *arg); + #endif diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 02b05202..f02c55b9 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/01/11 17:17:16 by jhalford ### ########.fr */ +/* Updated: 2017/01/21 18:05:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft b/42sh/libft index ab92f0e5..4c497745 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit ab92f0e5d817c9d726a8ccf2f11c084ba446bbdf +Subproject commit 4c4977452745481166749b813e8db51dcf0caf44 diff --git a/42sh/src/builtin/bt_read_get.c b/42sh/src/builtin/bt_read_get.c new file mode 100644 index 00000000..a7364b71 --- /dev/null +++ b/42sh/src/builtin/bt_read_get.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bt_read_get.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/21 18:00:03 by jhalford #+# #+# */ +/* Updated: 2017/01/22 16:24:55 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "builtin_read.h" + +int bt_read_getdelim(t_read *data, char *arg) +{ + data->delim = *arg; + return (0); +} + +int bt_read_getnchars(t_read *data, char *arg) +{ + data->nchars = ft_atoi(arg); + return (0); +} + +int bt_read_getprompt(t_read *data, char *arg) +{ + data->prompt = arg; + return (0); +} + +int bt_read_gettimeout(t_read *data, char *arg) +{ + data->timeout = ft_atoi(arg); + return (0); +} + +int bt_read_getfd(t_read *data, char *arg) +{ + data->fd = ft_atoi(arg); + return (0); +} diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index f9137519..6377dd48 100644 --- a/42sh/src/builtin/builtin_read.c +++ b/42sh/src/builtin/builtin_read.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */ -/* Updated: 2017/01/20 19:32:44 by jhalford ### ########.fr */ +/* Updated: 2017/01/22 18:21:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,30 +14,31 @@ t_readopt g_readtab[] = { - {'a', READ_OPT_LA, NULL}, - {'d', READ_OPT_LD, NULL}, - {'e', READ_OPT_LE, NULL}, - {'i', READ_OPT_LI, NULL}, - {'n', READ_OPT_LN, NULL}, - {'N', READ_OPT_UN, NULL}, - {'p', READ_OPT_LP, NULL}, + /* {'a', READ_OPT_LA, NULL}, */ + {'d', READ_OPT_LD, bt_read_getdelim}, + /* {'e', READ_OPT_LE, NULL}, */ + /* {'i', READ_OPT_LI, NULL}, */ + {'n', READ_OPT_LN, bt_read_getnchars}, + /* {'N', READ_OPT_UN, bt_read_getnchars}, */ + {'p', READ_OPT_LP, bt_read_getprompt}, {'r', READ_OPT_LR, NULL}, {'s', READ_OPT_LS, NULL}, - {'t', READ_OPT_LT, NULL}, - {'u', READ_OPT_LU, NULL}, + {'t', READ_OPT_LT, bt_read_gettimeout}, + {'u', READ_OPT_LU, bt_read_getfd}, {0, 0, 0}, }; -void bt_read_init(t_read *data) +void bt_read_init(t_read *data) { - data.delim = '\n'; - data.nchars = -1; - data.prompt = NULL; - data.timeout = -1; - data.fd = 0; + data->opts = 0; + data->delim = '\n'; + data->nchars = -1; + data->prompt = NULL; + data->timeout = -1; + data->fd = 0; } -t_readopt bt_read_getopt(char letter) +t_readopt *bt_read_getopt(char letter) { int i; @@ -45,48 +46,56 @@ t_readopt bt_read_getopt(char letter) while (g_readtab[i].letter) { if (g_readtab[i].letter == letter) - return (g_readtab[i]); + return (&g_readtab[i]); + i++; } return (NULL); } -int bt_read_parse(t_read *data, char **av) +int bt_read_parse(t_read *data, char **av) { int i; int j; int k; - t_readopt opt; + t_readopt *opt; + i = 1; + k = 0; while (av[i]) { j = 0; + DG("check 1"); if (av[i][j++] == '-') { - if (av[i][j] == '-') + if (av[i][j] == '-' && av[i][j + 1] == 0) { + DG("check 2"); i++; break ; } + DG("check 3"); while (av[i][j]) { if (!(opt = bt_read_getopt(av[i][j]))) { - ft_dprintf(2, "%s: bad option: %c", SHELL_NAME, av[i][j]); + ft_dprintf(2, "{red}%s: bad option: %c{eoc}\n", SHELL_NAME, av[i][j]); return (2); } - data->opts |= opt.flag; - if (data->get) + data->opts |= opt->flag; + if (opt->get) { - (*data->get)(data, av[i + 1]); + (*opt->get)(data, av[++i]); break ; } j++; } + DG("check 4"); } + else + break ; i++; } - if (av[i]) - bt_read_getnames(()) + data->names = av + i; return (0); } @@ -94,10 +103,46 @@ int builtin_read(const char *path, char *const av[], char *const envp[]) { t_read data; int i; + char buf[5]; + char *input; + int esc; (void)path; (void)envp; + input = NULL; bt_read_init(&data); - if ((i = bt_read_parse(&data, (char **)av))) - return (i); + if ((bt_read_parse(&data, (char **)av))) + return (2); + DG("read_opts: %b", data.opts); + DG("\ndelim=%c\nnchars=%i\nprompt=%s\ntimeout=%i\nfd=%i", + data.delim, data.nchars, data.prompt, data.timeout, data.fd); + ft_sstrprint(data.names, ','); + i = 0; + esc = 0; + if (data.prompt) + ft_printf(data.prompt); + while (42) + { + if (read(0, buf, 1) < 0) + return (1); + buf[1] = 0; + if (!esc && *buf == data.delim) + { + DG("CHECK 1"); + break ; + } + if (*buf == '\n' && !(data.opts & READ_OPT_LR)) + ft_putstr("> "); + esc = esc ? 0 : !(data.opts & READ_OPT_LR) && (*buf == '\\'); + ft_strappend(&input, buf); + i++; + DG("%i:%i", data.opts & READ_OPT_LN, i >= data.nchars); + if ((data.opts & READ_OPT_LN) && i >= data.nchars) + { + DG("CHECK 2"); + break ; + } + } + DG("input=%s", input); + return (0); } diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index 7b1e781e..4e4af5a6 100644 --- a/42sh/src/builtin/is_builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -6,13 +6,14 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */ -/* Updated: 2017/01/20 15:01:34 by jhalford ### ########.fr */ +/* Updated: 2017/01/21 18:42:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_stof g_builtin[] = { +t_stof g_builtin[] = +{ {"echo", &builtin_echo}, {"cd", &builtin_cd}, {"setenv", &builtin_setenv}, diff --git a/42sh/src/lexer/lexer_number.c b/42sh/src/lexer/lexer_number.c index b5cffe2d..722f1c1e 100644 --- a/42sh/src/lexer/lexer_number.c +++ b/42sh/src/lexer/lexer_number.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:45 by jhalford #+# #+# */ -/* Updated: 2017/01/10 14:29:46 by jhalford ### ########.fr */ +/* Updated: 2017/01/22 17:40:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,6 @@ int lexer_number(t_list **alst, char *str) t_lexstate state; token = (*alst)->content; - DG("*str=%c", *str); if ((state = get_lexer_state(str))) return (ft_tokenize(alst, str, state)); if (*str == '>') diff --git a/42sh/src/lexer/token_append.c b/42sh/src/lexer/token_append.c index c08e5c5a..2d2bbae1 100644 --- a/42sh/src/lexer/token_append.c +++ b/42sh/src/lexer/token_append.c @@ -6,19 +6,21 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/11 17:18:42 by jhalford #+# #+# */ -/* Updated: 2016/11/11 17:47:15 by jhalford ### ########.fr */ +/* Updated: 2017/01/22 16:44:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "lexer.h" +#define TOKEN_INCR 10 + int token_append(t_token *token, char c) { if ((int)ft_strlen(token->data) >= token->size) { - token->data = (char *)ft_realloc(token->data, token->size + 10); - token->size += 10; + token->data = (char *)ft_realloc(token->data, token->size + TOKEN_INCR); + token->size += TOKEN_INCR - 1; } - ft_strcat(token->data, (char[2]){c, '\0'}); + ft_strcat(token->data, (char[]){c, 0}); return (0); } diff --git a/42sh/src/line-editing/curs_setup.c b/42sh/src/line-editing/curs_setup.c index c9842a54..7fcc6a7d 100644 --- a/42sh/src/line-editing/curs_setup.c +++ b/42sh/src/line-editing/curs_setup.c @@ -6,7 +6,7 @@ /* By: sbenning +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/08 16:36:43 by sbenning #+# #+# */ -/* Updated: 2017/01/11 13:55:42 by jhalford ### ########.fr */ +/* Updated: 2017/01/21 18:39:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/curs_term_setup.c b/42sh/src/line-editing/curs_term_setup.c index 9a18bc72..ad10548b 100644 --- a/42sh/src/line-editing/curs_term_setup.c +++ b/42sh/src/line-editing/curs_term_setup.c @@ -6,7 +6,7 @@ /* By: sbenning +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/08 16:50:26 by sbenning #+# #+# */ -/* Updated: 2017/01/11 13:55:40 by jhalford ### ########.fr */ +/* Updated: 2017/01/21 18:40:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_readline.c b/42sh/src/line-editing/ft_readline.c index 90620a39..857935ed 100644 --- a/42sh/src/line-editing/ft_readline.c +++ b/42sh/src/line-editing/ft_readline.c @@ -6,7 +6,7 @@ /* By: sbenning +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/08 18:03:48 by sbenning #+# #+# */ -/* Updated: 2017/01/11 17:35:14 by jhalford ### ########.fr */ +/* Updated: 2017/01/22 18:15:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index fafa3727..d398462b 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/01/20 15:23:46 by jhalford ### ########.fr */ +/* Updated: 2017/01/21 18:04:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From d3fd7bbdedfe944db49b21304f0debc15d13b4c0 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Wed, 25 Jan 2017 18:13:15 +0100 Subject: [PATCH 3/9] termios stuff added --- 42sh/includes/builtin_read.h | 18 +++++---- 42sh/src/builtin/bt_read_term.c | 49 +++++++++++++++++++++++++ 42sh/src/builtin/builtin_read.c | 25 +++++-------- 42sh/src/line-editing/curs_term_setup.c | 2 +- 42sh/src/line-editing/ft_readline.c | 2 +- 42sh/src/main/main.c | 4 +- 6 files changed, 73 insertions(+), 27 deletions(-) create mode 100644 42sh/src/builtin/bt_read_term.c diff --git a/42sh/includes/builtin_read.h b/42sh/includes/builtin_read.h index feeac781..bda0efe1 100644 --- a/42sh/includes/builtin_read.h +++ b/42sh/includes/builtin_read.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */ -/* Updated: 2017/01/22 18:04:23 by jhalford ### ########.fr */ +/* Updated: 2017/01/25 16:14:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,12 +53,16 @@ struct s_readopt extern t_readopt g_readtab[]; -int builtin_read(const char *path, char *const av[], char *const envp[]); +int builtin_read(const char *path, char *const av[], char *const envp[]); -int bt_read_getdelim(t_read *data, char *arg); -int bt_read_getnchars(t_read *data, char *arg); -int bt_read_getprompt(t_read *data, char *arg); -int bt_read_gettimeout(t_read *data, char *arg); -int bt_read_getfd(t_read *data, char *arg); +struct termios *bt_read_term(int init); +int bt_read_terminit(t_read *data); +int bt_read_termexit(void); + +int bt_read_getdelim(t_read *data, char *arg); +int bt_read_getnchars(t_read *data, char *arg); +int bt_read_getprompt(t_read *data, char *arg); +int bt_read_gettimeout(t_read *data, char *arg); +int bt_read_getfd(t_read *data, char *arg); #endif diff --git a/42sh/src/builtin/bt_read_term.c b/42sh/src/builtin/bt_read_term.c new file mode 100644 index 00000000..c1313c16 --- /dev/null +++ b/42sh/src/builtin/bt_read_term.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bt_read_term.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/25 16:02:05 by jhalford #+# #+# */ +/* Updated: 2017/01/25 16:14:30 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "builtin.h" + +struct termios *bt_read_term(int init) +{ + static struct termios term; + + if (init) + { + if (tcgetattr(0, &term) < 0) + return (NULL); + } + return (&term); +} + +int bt_read_terminit(t_read *data) +{ + struct termios *term; + + (void)data; + term = bt_read_term(1); + term->c_lflag &= ~(ECHO | ICANON); + term->c_cc[VTIME] = 0; + term->c_cc[VMIN] = 1; + if (tcsetattr(0, TCSANOW, term) < 0) + return (-1); + return (0); +} + +int bt_read_termexit(void) +{ + struct termios *term; + + term = bt_read_term(0); + if (tcsetattr(0, TCSANOW, term) < 0) + return (-1); + return (0); +} diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index 6377dd48..b7298ddd 100644 --- a/42sh/src/builtin/builtin_read.c +++ b/42sh/src/builtin/builtin_read.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */ -/* Updated: 2017/01/22 18:21:50 by jhalford ### ########.fr */ +/* Updated: 2017/01/25 16:19:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,16 +64,13 @@ int bt_read_parse(t_read *data, char **av) while (av[i]) { j = 0; - DG("check 1"); if (av[i][j++] == '-') { if (av[i][j] == '-' && av[i][j + 1] == 0) { - DG("check 2"); i++; break ; } - DG("check 3"); while (av[i][j]) { if (!(opt = bt_read_getopt(av[i][j]))) @@ -89,7 +86,6 @@ int bt_read_parse(t_read *data, char **av) } j++; } - DG("check 4"); } else break ; @@ -103,7 +99,7 @@ int builtin_read(const char *path, char *const av[], char *const envp[]) { t_read data; int i; - char buf[5]; + char buf[2]; char *input; int esc; @@ -117,32 +113,29 @@ int builtin_read(const char *path, char *const av[], char *const envp[]) DG("\ndelim=%c\nnchars=%i\nprompt=%s\ntimeout=%i\nfd=%i", data.delim, data.nchars, data.prompt, data.timeout, data.fd); ft_sstrprint(data.names, ','); + bt_read_terminit(&data); i = 0; esc = 0; if (data.prompt) ft_printf(data.prompt); while (42) { - if (read(0, buf, 1) < 0) + if (read(data.fd, buf, 1) < 0) return (1); buf[1] = 0; if (!esc && *buf == data.delim) - { - DG("CHECK 1"); break ; - } - if (*buf == '\n' && !(data.opts & READ_OPT_LR)) - ft_putstr("> "); esc = esc ? 0 : !(data.opts & READ_OPT_LR) && (*buf == '\\'); ft_strappend(&input, buf); + ft_putchar(*buf); i++; - DG("%i:%i", data.opts & READ_OPT_LN, i >= data.nchars); + if (*buf == '\n' && !(data.opts & READ_OPT_LR)) + ft_putstr("> "); if ((data.opts & READ_OPT_LN) && i >= data.nchars) - { - DG("CHECK 2"); break ; - } } + ft_putchar('\n'); DG("input=%s", input); + bt_read_termexit(); return (0); } diff --git a/42sh/src/line-editing/curs_term_setup.c b/42sh/src/line-editing/curs_term_setup.c index ad10548b..396503f0 100644 --- a/42sh/src/line-editing/curs_term_setup.c +++ b/42sh/src/line-editing/curs_term_setup.c @@ -6,7 +6,7 @@ /* By: sbenning +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/08 16:50:26 by sbenning #+# #+# */ -/* Updated: 2017/01/21 18:40:44 by jhalford ### ########.fr */ +/* Updated: 2017/01/25 15:04:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_readline.c b/42sh/src/line-editing/ft_readline.c index 857935ed..ff729bde 100644 --- a/42sh/src/line-editing/ft_readline.c +++ b/42sh/src/line-editing/ft_readline.c @@ -6,7 +6,7 @@ /* By: sbenning +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/08 18:03:48 by sbenning #+# #+# */ -/* Updated: 2017/01/22 18:15:18 by jhalford ### ########.fr */ +/* Updated: 2017/01/25 15:04:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index d398462b..52b34a99 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/01/21 18:04:06 by jhalford ### ########.fr */ +/* Updated: 2017/01/25 17:58:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,7 @@ int shell_single_command(char *command) token_print(token); if (ft_parse(&ast, &token)) return (1); - /* btree_print(STDBUG, ast, &ft_putast); */ + btree_print(STDBUG, ast, &ft_putast); /* /1* ft_dprintf(STDBUG, "\n--- INFIX BREAKDOWN ---\n"); *1/ */ /* /1* btree_apply_infix(ast, &ft_putast2); *1/ */ if (ft_exec(&ast)) From 9d2e01de25097ee4309613a05ecbf95d9a5c005f Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 26 Jan 2017 18:41:07 +0100 Subject: [PATCH 4/9] some progress on c_cc[VTIME] understading, almost there --- 42sh/includes/builtin_read.h | 4 ++-- 42sh/src/builtin/bt_read_term.c | 36 +++++++++++++++++++------------- 42sh/src/builtin/builtin_read.c | 8 ++++--- 42sh/src/lexer/lexer_backslash.c | 2 +- 42sh/src/lexer/lexer_delim.c | 2 +- 42sh/src/lexer/lexer_word.c | 2 +- 42sh/src/lexer/token_append.c | 2 +- 42sh/src/main/main.c | 2 +- 8 files changed, 33 insertions(+), 25 deletions(-) diff --git a/42sh/includes/builtin_read.h b/42sh/includes/builtin_read.h index bda0efe1..ab660d46 100644 --- a/42sh/includes/builtin_read.h +++ b/42sh/includes/builtin_read.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */ -/* Updated: 2017/01/25 16:14:16 by jhalford ### ########.fr */ +/* Updated: 2017/01/26 15:23:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,7 +55,7 @@ extern t_readopt g_readtab[]; int builtin_read(const char *path, char *const av[], char *const envp[]); -struct termios *bt_read_term(int init); +struct termios bt_read_term(int init); int bt_read_terminit(t_read *data); int bt_read_termexit(void); diff --git a/42sh/src/builtin/bt_read_term.c b/42sh/src/builtin/bt_read_term.c index c1313c16..cb57f1a9 100644 --- a/42sh/src/builtin/bt_read_term.c +++ b/42sh/src/builtin/bt_read_term.c @@ -6,44 +6,50 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/25 16:02:05 by jhalford #+# #+# */ -/* Updated: 2017/01/25 16:14:30 by jhalford ### ########.fr */ +/* Updated: 2017/01/26 18:39:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "builtin.h" -struct termios *bt_read_term(int init) +struct termios bt_read_term(int init) { static struct termios term; - + if (init) - { - if (tcgetattr(0, &term) < 0) - return (NULL); - } - return (&term); + tcgetattr(0, &term); + return (term); } int bt_read_terminit(t_read *data) { - struct termios *term; + struct termios term; (void)data; term = bt_read_term(1); - term->c_lflag &= ~(ECHO | ICANON); - term->c_cc[VTIME] = 0; - term->c_cc[VMIN] = 1; - if (tcsetattr(0, TCSANOW, term) < 0) + term.c_lflag &= ~(ECHO | ICANON); + if (data->opts & READ_OPT_LT) + { + term.c_cc[VTIME] = data->timeout * 10; + term.c_cc[VMIN] = 0; + } + else + { + term.c_cc[VTIME] = 0; + term.c_cc[VMIN] = 1; + } + if (tcsetattr(0, TCSANOW, &term) < 0) return (-1); + DG("c_cc[VTIME]=%i", term.c_cc[VTIME]); return (0); } int bt_read_termexit(void) { - struct termios *term; + struct termios term; term = bt_read_term(0); - if (tcsetattr(0, TCSANOW, term) < 0) + if (tcsetattr(0, TCSANOW, &term) < 0) return (-1); return (0); } diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index b7298ddd..9f2b128c 100644 --- a/42sh/src/builtin/builtin_read.c +++ b/42sh/src/builtin/builtin_read.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */ -/* Updated: 2017/01/25 16:19:50 by jhalford ### ########.fr */ +/* Updated: 2017/01/26 18:39:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -102,6 +102,7 @@ int builtin_read(const char *path, char *const av[], char *const envp[]) char buf[2]; char *input; int esc; + int ret; (void)path; (void)envp; @@ -120,9 +121,10 @@ int builtin_read(const char *path, char *const av[], char *const envp[]) ft_printf(data.prompt); while (42) { - if (read(data.fd, buf, 1) < 0) + if ((ret = read(data.fd, buf, 1) <= 0)) return (1); - buf[1] = 0; + DG("got *buf=%c, ret=%i", *buf, ret); + buf[ret] = 0; if (!esc && *buf == data.delim) break ; esc = esc ? 0 : !(data.opts & READ_OPT_LR) && (*buf == '\\'); diff --git a/42sh/src/lexer/lexer_backslash.c b/42sh/src/lexer/lexer_backslash.c index 9a6f2438..8439d549 100644 --- a/42sh/src/lexer/lexer_backslash.c +++ b/42sh/src/lexer/lexer_backslash.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:56:49 by jhalford #+# #+# */ -/* Updated: 2016/12/03 12:35:13 by jhalford ### ########.fr */ +/* Updated: 2017/01/26 16:30:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_delim.c b/42sh/src/lexer/lexer_delim.c index 7542b57c..f0ceb531 100644 --- a/42sh/src/lexer/lexer_delim.c +++ b/42sh/src/lexer/lexer_delim.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:58:44 by jhalford #+# #+# */ -/* Updated: 2017/01/11 15:45:53 by jhalford ### ########.fr */ +/* Updated: 2017/01/26 16:30:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_word.c b/42sh/src/lexer/lexer_word.c index 53ed4746..8844b7db 100644 --- a/42sh/src/lexer/lexer_word.c +++ b/42sh/src/lexer/lexer_word.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:07:11 by jhalford #+# #+# */ -/* Updated: 2017/01/11 15:38:03 by jhalford ### ########.fr */ +/* Updated: 2017/01/26 15:56:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/token_append.c b/42sh/src/lexer/token_append.c index 2d2bbae1..2c422840 100644 --- a/42sh/src/lexer/token_append.c +++ b/42sh/src/lexer/token_append.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/11 17:18:42 by jhalford #+# #+# */ -/* Updated: 2017/01/22 16:44:15 by jhalford ### ########.fr */ +/* Updated: 2017/01/26 18:32:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 52b34a99..e90ec878 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/01/25 17:58:18 by jhalford ### ########.fr */ +/* Updated: 2017/01/26 15:16:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From 7d57b47f5efb74db7ec99136ea76c5f87ea31ae6 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 26 Jan 2017 20:32:39 +0100 Subject: [PATCH 5/9] end of day, still doesnt work properly --- 42sh/includes/builtin_read.h | 2 +- 42sh/src/builtin/bt_read_term.c | 2 +- 42sh/src/builtin/builtin_read.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/42sh/includes/builtin_read.h b/42sh/includes/builtin_read.h index ab660d46..86c38d2a 100644 --- a/42sh/includes/builtin_read.h +++ b/42sh/includes/builtin_read.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */ -/* Updated: 2017/01/26 15:23:11 by jhalford ### ########.fr */ +/* Updated: 2017/01/26 20:25:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/bt_read_term.c b/42sh/src/builtin/bt_read_term.c index cb57f1a9..69bb5643 100644 --- a/42sh/src/builtin/bt_read_term.c +++ b/42sh/src/builtin/bt_read_term.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/25 16:02:05 by jhalford #+# #+# */ -/* Updated: 2017/01/26 18:39:29 by jhalford ### ########.fr */ +/* Updated: 2017/01/26 20:27:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index 9f2b128c..953254ef 100644 --- a/42sh/src/builtin/builtin_read.c +++ b/42sh/src/builtin/builtin_read.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */ -/* Updated: 2017/01/26 18:39:19 by jhalford ### ########.fr */ +/* Updated: 2017/01/26 20:32:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From e66999c24f96676be5702f5ac9d917b78a9cebb5 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 27 Jan 2017 17:11:11 +0100 Subject: [PATCH 6/9] -t works fine, gonna do the IFS split next --- 42sh/src/builtin/bt_read_term.c | 4 ++-- 42sh/src/builtin/builtin_read.c | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/42sh/src/builtin/bt_read_term.c b/42sh/src/builtin/bt_read_term.c index 69bb5643..65c87ff6 100644 --- a/42sh/src/builtin/bt_read_term.c +++ b/42sh/src/builtin/bt_read_term.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/25 16:02:05 by jhalford #+# #+# */ -/* Updated: 2017/01/26 20:27:00 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 17:07:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,7 +40,7 @@ int bt_read_terminit(t_read *data) } if (tcsetattr(0, TCSANOW, &term) < 0) return (-1); - DG("c_cc[VTIME]=%i", term.c_cc[VTIME]); + /* DG("c_cc[VTIME]=%i", term.c_cc[VTIME]); */ return (0); } diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index 953254ef..6dae6f74 100644 --- a/42sh/src/builtin/builtin_read.c +++ b/42sh/src/builtin/builtin_read.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */ -/* Updated: 2017/01/26 20:32:28 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 17:10:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,7 +34,6 @@ void bt_read_init(t_read *data) data->delim = '\n'; data->nchars = -1; data->prompt = NULL; - data->timeout = -1; data->fd = 0; } @@ -121,9 +120,9 @@ int builtin_read(const char *path, char *const av[], char *const envp[]) ft_printf(data.prompt); while (42) { - if ((ret = read(data.fd, buf, 1) <= 0)) + if ((ret = read(data.fd, buf, 1)) <= 0) return (1); - DG("got *buf=%c, ret=%i", *buf, ret); + /* DG("got *buf=%c, ret=%i", *buf, ret); */ buf[ret] = 0; if (!esc && *buf == data.delim) break ; From cc2fadf41c83eb7b765ca67ac4449842ef4ef22e Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 27 Jan 2017 19:40:36 +0100 Subject: [PATCH 7/9] variables assigning passes initial testing, needs more testing --- 42sh/includes/builtin_read.h | 7 +- 42sh/src/builtin/bt_read_term.c | 5 +- 42sh/src/builtin/builtin_read.c | 142 +++++++++--------------- 42sh/src/builtin/builtin_setenv.c | 2 +- 42sh/src/line-editing/curs_term_setup.c | 2 +- 42sh/src/main/main.c | 2 +- 6 files changed, 66 insertions(+), 94 deletions(-) diff --git a/42sh/includes/builtin_read.h b/42sh/includes/builtin_read.h index 86c38d2a..177f9176 100644 --- a/42sh/includes/builtin_read.h +++ b/42sh/includes/builtin_read.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */ -/* Updated: 2017/01/26 20:25:11 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 18:56:58 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,6 +42,7 @@ struct s_read int timeout; int fd; char **names; + char *input; }; struct s_readopt @@ -55,9 +56,11 @@ extern t_readopt g_readtab[]; int builtin_read(const char *path, char *const av[], char *const envp[]); +int bt_read_init(t_read *data, char **av); + struct termios bt_read_term(int init); int bt_read_terminit(t_read *data); -int bt_read_termexit(void); +int bt_read_exit(t_read *data); int bt_read_getdelim(t_read *data, char *arg); int bt_read_getnchars(t_read *data, char *arg); diff --git a/42sh/src/builtin/bt_read_term.c b/42sh/src/builtin/bt_read_term.c index 65c87ff6..ae5d40a9 100644 --- a/42sh/src/builtin/bt_read_term.c +++ b/42sh/src/builtin/bt_read_term.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/25 16:02:05 by jhalford #+# #+# */ -/* Updated: 2017/01/27 17:07:48 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 19:00:10 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,10 +44,11 @@ int bt_read_terminit(t_read *data) return (0); } -int bt_read_termexit(void) +int bt_read_exit(t_read *data) { struct termios term; + ft_strdel(&data->input); term = bt_read_term(0); if (tcsetattr(0, TCSANOW, &term) < 0) return (-1); diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index 6dae6f74..176acb3a 100644 --- a/42sh/src/builtin/builtin_read.c +++ b/42sh/src/builtin/builtin_read.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */ -/* Updated: 2017/01/27 17:10:43 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 19:40:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,115 +28,83 @@ t_readopt g_readtab[] = {0, 0, 0}, }; -void bt_read_init(t_read *data) -{ - data->opts = 0; - data->delim = '\n'; - data->nchars = -1; - data->prompt = NULL; - data->fd = 0; -} - -t_readopt *bt_read_getopt(char letter) +int bt_read_loop(t_read *data) { int i; + int esc; + int ret; + char buf[2]; i = 0; - while (g_readtab[i].letter) + esc = 0; + if (data->prompt) + ft_printf(data->prompt); + while (42) { - if (g_readtab[i].letter == letter) - return (&g_readtab[i]); + if ((ret = read(data->fd, buf, 1)) <= 0) + return (1); + /* DG("got *buf=%c, ret=%i", *buf, ret); */ + buf[ret] = 0; + if (!esc && *buf == data->delim) + break ; + esc = esc ? 0 : !(data->opts & READ_OPT_LR) && (*buf == '\\'); + ft_strappend(&data->input, buf); + if (!(data->opts & READ_OPT_LS)) + ft_putchar(*buf); i++; + if (*buf == '\n' && !(data->opts & READ_OPT_LR)) + ft_putstr("> "); + if ((data->opts & READ_OPT_LN) && i >= data->nchars) + break ; } - return (NULL); + ft_putchar('\n'); + DG("input=%s", data->input); + return (0); } -int bt_read_parse(t_read *data, char **av) +int bt_read_assign(t_read *data) { - int i; - int j; - int k; - t_readopt *opt; + char *input; + char **names; + char *IFS; + char *start; - i = 1; - k = 0; - while (av[i]) + input = data->input; + names = data->names ? data->names : (char*[]){"REPLY", NULL}; + IFS = ft_getenv(data_singleton()->env, "IFS"); + start = input; + while (*start && *names) { - j = 0; - if (av[i][j++] == '-') + if (!(names[1]) || !IFS) { - if (av[i][j] == '-' && av[i][j + 1] == 0) - { - i++; - break ; - } - while (av[i][j]) - { - if (!(opt = bt_read_getopt(av[i][j]))) - { - ft_dprintf(2, "{red}%s: bad option: %c{eoc}\n", SHELL_NAME, av[i][j]); - return (2); - } - data->opts |= opt->flag; - if (opt->get) - { - (*opt->get)(data, av[++i]); - break ; - } - j++; - } - } - else + builtin_setenv("setenv", (char*[]){"setenv", *names, start}, NULL); break ; - i++; + } + while (*input && !ft_strchr(IFS, *input)) + input++; + while (input && ft_strchr(IFS, *input)) + *(input++) = 0; + builtin_setenv("setenv", (char*[]){"setenv", *names, start}, NULL); + start = input; + names++; } - data->names = av + i; return (0); } int builtin_read(const char *path, char *const av[], char *const envp[]) { t_read data; - int i; - char buf[2]; - char *input; - int esc; int ret; (void)path; (void)envp; - input = NULL; - bt_read_init(&data); - if ((bt_read_parse(&data, (char **)av))) - return (2); - DG("read_opts: %b", data.opts); - DG("\ndelim=%c\nnchars=%i\nprompt=%s\ntimeout=%i\nfd=%i", - data.delim, data.nchars, data.prompt, data.timeout, data.fd); - ft_sstrprint(data.names, ','); - bt_read_terminit(&data); - i = 0; - esc = 0; - if (data.prompt) - ft_printf(data.prompt); - while (42) - { - if ((ret = read(data.fd, buf, 1)) <= 0) - return (1); - /* DG("got *buf=%c, ret=%i", *buf, ret); */ - buf[ret] = 0; - if (!esc && *buf == data.delim) - break ; - esc = esc ? 0 : !(data.opts & READ_OPT_LR) && (*buf == '\\'); - ft_strappend(&input, buf); - ft_putchar(*buf); - i++; - if (*buf == '\n' && !(data.opts & READ_OPT_LR)) - ft_putstr("> "); - if ((data.opts & READ_OPT_LN) && i >= data.nchars) - break ; - } - ft_putchar('\n'); - DG("input=%s", input); - bt_read_termexit(); - return (0); + ret = 0; + if (bt_read_init(&data, (char **)av)) + ret = 2; + else if (bt_read_loop(&data)) + ret = 1; + else if (bt_read_assign(&data)) + ret = 1; + bt_read_exit(&data); + return (ret); } diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index 98f92890..3c566d6b 100644 --- a/42sh/src/builtin/builtin_setenv.c +++ b/42sh/src/builtin/builtin_setenv.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ -/* Updated: 2017/01/09 15:53:07 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 19:00:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/curs_term_setup.c b/42sh/src/line-editing/curs_term_setup.c index 396503f0..369ad41b 100644 --- a/42sh/src/line-editing/curs_term_setup.c +++ b/42sh/src/line-editing/curs_term_setup.c @@ -6,7 +6,7 @@ /* By: sbenning +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/08 16:50:26 by sbenning #+# #+# */ -/* Updated: 2017/01/25 15:04:17 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 18:55:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index e90ec878..33365380 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/01/26 15:16:00 by jhalford ### ########.fr */ +/* Updated: 2017/01/27 18:18:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From f236e8e734cea52721a255ddaacc3a0011b6ed35 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 27 Jan 2017 20:08:56 +0100 Subject: [PATCH 8/9] refactoring for clarity of bt_read* family --- 42sh/src/builtin/bt_read_parse.c | 89 ++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 42sh/src/builtin/bt_read_parse.c diff --git a/42sh/src/builtin/bt_read_parse.c b/42sh/src/builtin/bt_read_parse.c new file mode 100644 index 00000000..b23672f1 --- /dev/null +++ b/42sh/src/builtin/bt_read_parse.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bt_read_parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/27 18:25:22 by jhalford #+# #+# */ +/* Updated: 2017/01/27 19:40:14 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "builtin_read.h" + +static t_readopt *bt_read_getopt(char letter) +{ + int i; + + i = 0; + while (g_readtab[i].letter) + { + if (g_readtab[i].letter == letter) + return (&g_readtab[i]); + i++; + } + return (NULL); +} + +int bt_read_parse(t_read *data, char **av) +{ + int i; + int j; + int k; + t_readopt *opt; + + i = 1; + k = 0; + while (av[i]) + { + j = 0; + if (av[i][j++] == '-') + { + if (av[i][j] == '-' && av[i][j + 1] == 0) + { + i++; + break ; + } + while (av[i][j]) + { + if (!(opt = bt_read_getopt(av[i][j]))) + { + ft_dprintf(2, "{red}%s: bad option: %c{eoc}\n", SHELL_NAME, av[i][j]); + return (2); + } + data->opts |= opt->flag; + if (opt->get) + { + (*opt->get)(data, av[++i]); + break ; + } + j++; + } + } + else + break ; + i++; + } + data->names = av[i] ? av + i : NULL; + DG("read_opts: %b", data->opts); + DG("\ndelim=%c\nnchars=%i\nprompt=%s\ntimeout=%i\nfd=%i", + data->delim, data->nchars, data->prompt, data->timeout, data->fd); + return (0); +} + +int bt_read_init(t_read *data, char **av) +{ + data->opts = 0; + data->delim = '\n'; + data->nchars = -1; + data->prompt = NULL; + data->fd = 0; + data->input = NULL; + if ((bt_read_parse(data, av))) + return (1); + if (data->names) + DG("%s,%s", data->names[0], data->names[1]); + bt_read_terminit(data); + return (0); +} From 076d3c3d5be8cb514a034781bcef2db424ad2f6e Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sat, 28 Jan 2017 14:44:00 +0100 Subject: [PATCH 9/9] remise a la norme avant de merge --- 42sh/libft | 2 +- 42sh/src/builtin/bt_read_term.c | 1 - 42sh/src/builtin/builtin_read.c | 10 +++------- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/42sh/libft b/42sh/libft index 4c497745..ab92f0e5 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit 4c4977452745481166749b813e8db51dcf0caf44 +Subproject commit ab92f0e5d817c9d726a8ccf2f11c084ba446bbdf diff --git a/42sh/src/builtin/bt_read_term.c b/42sh/src/builtin/bt_read_term.c index ae5d40a9..853929c4 100644 --- a/42sh/src/builtin/bt_read_term.c +++ b/42sh/src/builtin/bt_read_term.c @@ -40,7 +40,6 @@ int bt_read_terminit(t_read *data) } if (tcsetattr(0, TCSANOW, &term) < 0) return (-1); - /* DG("c_cc[VTIME]=%i", term.c_cc[VTIME]); */ return (0); } diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index 176acb3a..586f3627 100644 --- a/42sh/src/builtin/builtin_read.c +++ b/42sh/src/builtin/builtin_read.c @@ -32,7 +32,6 @@ int bt_read_loop(t_read *data) { int i; int esc; - int ret; char buf[2]; i = 0; @@ -41,24 +40,21 @@ int bt_read_loop(t_read *data) ft_printf(data->prompt); while (42) { - if ((ret = read(data->fd, buf, 1)) <= 0) + if (read(data->fd, buf, 1) <= 0) return (1); - /* DG("got *buf=%c, ret=%i", *buf, ret); */ - buf[ret] = 0; + buf[1] = 0; if (!esc && *buf == data->delim) break ; esc = esc ? 0 : !(data->opts & READ_OPT_LR) && (*buf == '\\'); ft_strappend(&data->input, buf); if (!(data->opts & READ_OPT_LS)) ft_putchar(*buf); - i++; if (*buf == '\n' && !(data->opts & READ_OPT_LR)) ft_putstr("> "); - if ((data->opts & READ_OPT_LN) && i >= data->nchars) + if ((data->opts & READ_OPT_LN) && ++i >= data->nchars) break ; } ft_putchar('\n'); - DG("input=%s", data->input); return (0); }