diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index 42f2543a..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/09 16:57:22 by jhalford ### ########.fr */ +/* Updated: 2017/01/21 18:42:34 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..177f9176 --- /dev/null +++ b/42sh/includes/builtin_read.h @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_read.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */ +/* Updated: 2017/01/27 18:56:58 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BUILTIN_READ_H +# define BUILTIN_READ_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; +typedef struct s_readopt t_readopt; + +struct s_read +{ + t_flag opts; + char delim; + int nchars; + char *prompt; + int timeout; + int fd; + char **names; + char *input; +}; + +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[]); + +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_exit(t_read *data); + +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/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/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/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); +} diff --git a/42sh/src/builtin/bt_read_term.c b/42sh/src/builtin/bt_read_term.c new file mode 100644 index 00000000..853929c4 --- /dev/null +++ b/42sh/src/builtin/bt_read_term.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bt_read_term.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/25 16:02:05 by jhalford #+# #+# */ +/* Updated: 2017/01/27 19:00:10 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "builtin.h" + +struct termios bt_read_term(int init) +{ + static struct termios term; + + if (init) + tcgetattr(0, &term); + return (term); +} + +int bt_read_terminit(t_read *data) +{ + struct termios term; + + (void)data; + term = bt_read_term(1); + 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); + return (0); +} + +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); + return (0); +} diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c new file mode 100644 index 00000000..586f3627 --- /dev/null +++ b/42sh/src/builtin/builtin_read.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_read.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */ +/* Updated: 2017/01/27 19:40:12 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "builtin_read.h" + +t_readopt g_readtab[] = +{ + /* {'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, bt_read_gettimeout}, + {'u', READ_OPT_LU, bt_read_getfd}, + {0, 0, 0}, +}; + +int bt_read_loop(t_read *data) +{ + int i; + int esc; + char buf[2]; + + i = 0; + esc = 0; + if (data->prompt) + ft_printf(data->prompt); + while (42) + { + if (read(data->fd, buf, 1) <= 0) + return (1); + 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); + if (*buf == '\n' && !(data->opts & READ_OPT_LR)) + ft_putstr("> "); + if ((data->opts & READ_OPT_LN) && ++i >= data->nchars) + break ; + } + ft_putchar('\n'); + return (0); +} + +int bt_read_assign(t_read *data) +{ + char *input; + char **names; + char *IFS; + char *start; + + input = data->input; + names = data->names ? data->names : (char*[]){"REPLY", NULL}; + IFS = ft_getenv(data_singleton()->env, "IFS"); + start = input; + while (*start && *names) + { + if (!(names[1]) || !IFS) + { + builtin_setenv("setenv", (char*[]){"setenv", *names, start}, NULL); + break ; + } + 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++; + } + return (0); +} + +int builtin_read(const char *path, char *const av[], char *const envp[]) +{ + t_read data; + int ret; + + (void)path; + (void)envp; + 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/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index ce8f22c5..eba6e445 100644 --- a/42sh/src/builtin/is_builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -12,7 +12,8 @@ #include "minishell.h" -t_stof g_builtin[] = { +t_stof g_builtin[] = +{ {"echo", &builtin_echo}, {"cd", &builtin_cd}, {"setenv", &builtin_setenv}, @@ -23,6 +24,7 @@ t_stof g_builtin[] = { {"fg", &builtin_fg}, {"bg", &builtin_bg}, {"history", &builtin_history}, + {"read", &builtin_read}, {NULL, NULL}, }; 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_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/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 c08e5c5a..2c422840 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/26 18:32:42 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/main/main.c b/42sh/src/main/main.c index ce0eae68..7cdd2a95 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -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))