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))