variables assigning passes initial testing, needs more testing
This commit is contained in:
parent
e66999c24f
commit
cc2fadf41c
6 changed files with 66 additions and 94 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/20 15:02:39 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 timeout;
|
||||||
int fd;
|
int fd;
|
||||||
char **names;
|
char **names;
|
||||||
|
char *input;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct s_readopt
|
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 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);
|
struct termios bt_read_term(int init);
|
||||||
int bt_read_terminit(t_read *data);
|
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_getdelim(t_read *data, char *arg);
|
||||||
int bt_read_getnchars(t_read *data, char *arg);
|
int bt_read_getnchars(t_read *data, char *arg);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/25 16:02:05 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);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bt_read_termexit(void)
|
int bt_read_exit(t_read *data)
|
||||||
{
|
{
|
||||||
struct termios term;
|
struct termios term;
|
||||||
|
|
||||||
|
ft_strdel(&data->input);
|
||||||
term = bt_read_term(0);
|
term = bt_read_term(0);
|
||||||
if (tcsetattr(0, TCSANOW, &term) < 0)
|
if (tcsetattr(0, TCSANOW, &term) < 0)
|
||||||
return (-1);
|
return (-1);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/20 15:01:45 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},
|
{0, 0, 0},
|
||||||
};
|
};
|
||||||
|
|
||||||
void bt_read_init(t_read *data)
|
int bt_read_loop(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 i;
|
int i;
|
||||||
|
int esc;
|
||||||
|
int ret;
|
||||||
|
char buf[2];
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (g_readtab[i].letter)
|
esc = 0;
|
||||||
|
if (data->prompt)
|
||||||
|
ft_printf(data->prompt);
|
||||||
|
while (42)
|
||||||
{
|
{
|
||||||
if (g_readtab[i].letter == letter)
|
if ((ret = read(data->fd, buf, 1)) <= 0)
|
||||||
return (&g_readtab[i]);
|
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++;
|
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;
|
char *input;
|
||||||
int j;
|
char **names;
|
||||||
int k;
|
char *IFS;
|
||||||
t_readopt *opt;
|
char *start;
|
||||||
|
|
||||||
i = 1;
|
input = data->input;
|
||||||
k = 0;
|
names = data->names ? data->names : (char*[]){"REPLY", NULL};
|
||||||
while (av[i])
|
IFS = ft_getenv(data_singleton()->env, "IFS");
|
||||||
|
start = input;
|
||||||
|
while (*start && *names)
|
||||||
{
|
{
|
||||||
j = 0;
|
if (!(names[1]) || !IFS)
|
||||||
if (av[i][j++] == '-')
|
|
||||||
{
|
{
|
||||||
if (av[i][j] == '-' && av[i][j + 1] == 0)
|
builtin_setenv("setenv", (char*[]){"setenv", *names, start}, NULL);
|
||||||
{
|
|
||||||
i++;
|
|
||||||
break ;
|
break ;
|
||||||
}
|
}
|
||||||
while (av[i][j])
|
while (*input && !ft_strchr(IFS, *input))
|
||||||
{
|
input++;
|
||||||
if (!(opt = bt_read_getopt(av[i][j])))
|
while (input && ft_strchr(IFS, *input))
|
||||||
{
|
*(input++) = 0;
|
||||||
ft_dprintf(2, "{red}%s: bad option: %c{eoc}\n", SHELL_NAME, av[i][j]);
|
builtin_setenv("setenv", (char*[]){"setenv", *names, start}, NULL);
|
||||||
return (2);
|
start = input;
|
||||||
|
names++;
|
||||||
}
|
}
|
||||||
data->opts |= opt->flag;
|
|
||||||
if (opt->get)
|
|
||||||
{
|
|
||||||
(*opt->get)(data, av[++i]);
|
|
||||||
break ;
|
|
||||||
}
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
break ;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
data->names = av + i;
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int builtin_read(const char *path, char *const av[], char *const envp[])
|
int builtin_read(const char *path, char *const av[], char *const envp[])
|
||||||
{
|
{
|
||||||
t_read data;
|
t_read data;
|
||||||
int i;
|
|
||||||
char buf[2];
|
|
||||||
char *input;
|
|
||||||
int esc;
|
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
(void)path;
|
(void)path;
|
||||||
(void)envp;
|
(void)envp;
|
||||||
input = NULL;
|
ret = 0;
|
||||||
bt_read_init(&data);
|
if (bt_read_init(&data, (char **)av))
|
||||||
if ((bt_read_parse(&data, (char **)av)))
|
ret = 2;
|
||||||
return (2);
|
else if (bt_read_loop(&data))
|
||||||
DG("read_opts: %b", data.opts);
|
ret = 1;
|
||||||
DG("\ndelim=%c\nnchars=%i\nprompt=%s\ntimeout=%i\nfd=%i",
|
else if (bt_read_assign(&data))
|
||||||
data.delim, data.nchars, data.prompt, data.timeout, data.fd);
|
ret = 1;
|
||||||
ft_sstrprint(data.names, ',');
|
bt_read_exit(&data);
|
||||||
bt_read_terminit(&data);
|
return (ret);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/28 14:25:17 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: sbenning <sbenning@student.42.fr> +#+ +:+ +#+ */
|
/* By: sbenning <sbenning@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/12/08 16:50:26 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/12/06 18:40:58 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue