builtin_read many changes
This commit is contained in:
parent
d4b7872423
commit
dfaf728660
12 changed files with 53 additions and 96 deletions
|
|
@ -13,10 +13,6 @@
|
||||||
#ifndef BUILTIN_READ_H
|
#ifndef BUILTIN_READ_H
|
||||||
# define BUILTIN_READ_H
|
# define BUILTIN_READ_H
|
||||||
|
|
||||||
/* # include "types.h" */
|
|
||||||
/* # include "builtin.h" */
|
|
||||||
/* # include "minishell.h" */
|
|
||||||
|
|
||||||
# define READ_OPT_LA (1 << 0)
|
# define READ_OPT_LA (1 << 0)
|
||||||
# define READ_OPT_LD (1 << 1)
|
# define READ_OPT_LD (1 << 1)
|
||||||
# define READ_OPT_LE (1 << 2)
|
# define READ_OPT_LE (1 << 2)
|
||||||
|
|
@ -35,12 +31,12 @@ typedef struct s_readopt t_readopt;
|
||||||
struct s_read
|
struct s_read
|
||||||
{
|
{
|
||||||
t_flag opts;
|
t_flag opts;
|
||||||
|
char **names;
|
||||||
char delim;
|
char delim;
|
||||||
int nchars;
|
int nchars;
|
||||||
char *prompt;
|
char *prompt;
|
||||||
int timeout;
|
int timeout;
|
||||||
int fd;
|
int fd;
|
||||||
char **names;
|
|
||||||
char *input;
|
char *input;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@
|
||||||
struct s_data
|
struct s_data
|
||||||
{
|
{
|
||||||
t_flag opts;
|
t_flag opts;
|
||||||
|
char **av_data;
|
||||||
int fd;
|
int fd;
|
||||||
char **env;
|
char **env;
|
||||||
int argc;
|
int argc;
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,6 @@ char/ft_isprint.c\
|
||||||
char/ft_tolower.c\
|
char/ft_tolower.c\
|
||||||
char/ft_toupper.c\
|
char/ft_toupper.c\
|
||||||
cliopts/cliopts_get.c\
|
cliopts/cliopts_get.c\
|
||||||
cliopts/cliopts_getdata.c\
|
|
||||||
cliopts/cliopts_has.c\
|
cliopts/cliopts_has.c\
|
||||||
color/ft_color_mk.c\
|
color/ft_color_mk.c\
|
||||||
color/ft_color_mkif.c\
|
color/ft_color_mkif.c\
|
||||||
|
|
|
||||||
|
|
@ -32,10 +32,10 @@ struct s_cliopts
|
||||||
struct s_data_template
|
struct s_data_template
|
||||||
{
|
{
|
||||||
t_flag flag;
|
t_flag flag;
|
||||||
|
char **av_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
int cliopts_get(char **av, t_cliopts opt_map[], void *data);
|
int cliopts_get(char **av, t_cliopts opt_map[], void *data);
|
||||||
char **cliopts_getdata(char **av);
|
|
||||||
int cliopts_has(char **av, char c);
|
int cliopts_has(char **av, char c);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,6 @@ static int cliopts_parse_short(char ***av, t_cliopts opt_map[], void *data)
|
||||||
++(*av);
|
++(*av);
|
||||||
if ((map->get)(av, data))
|
if ((map->get)(av, data))
|
||||||
return (ERR_SET(E_CO_MISS, *arg));
|
return (ERR_SET(E_CO_MISS, *arg));
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
((t_data_template*)data)->flag |= map->flag_on;
|
((t_data_template*)data)->flag |= map->flag_on;
|
||||||
((t_data_template*)data)->flag &= ~map->flag_off;
|
((t_data_template*)data)->flag &= ~map->flag_off;
|
||||||
|
|
@ -66,7 +65,6 @@ static int cliopts_parse_short(char ***av, t_cliopts opt_map[], void *data)
|
||||||
}
|
}
|
||||||
++(*av);
|
++(*av);
|
||||||
return (0);
|
return (0);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cliopts_parse_long(char ***av, t_cliopts opt_map[], void *data)
|
static int cliopts_parse_long(char ***av, t_cliopts opt_map[], void *data)
|
||||||
|
|
@ -109,7 +107,8 @@ int cliopts_get(char **av, t_cliopts opt_map[], void *data)
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return (0);
|
break ;
|
||||||
}
|
}
|
||||||
|
((t_data_template*)data)->av_data = av;
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* cliopts_getdata.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2017/03/14 20:35:15 by jhalford #+# #+# */
|
|
||||||
/* Updated: 2017/03/14 21:35:19 by jhalford ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "cliopts.h"
|
|
||||||
|
|
||||||
char **cliopts_getdata(char **av)
|
|
||||||
{
|
|
||||||
if (!av)
|
|
||||||
return (NULL);
|
|
||||||
av++;
|
|
||||||
while (*av)
|
|
||||||
{
|
|
||||||
if (ft_strcmp(*av, "--") == 0)
|
|
||||||
return (av + 1);
|
|
||||||
else if ((*av)[0] == '-' && (*av)[1] == '-')
|
|
||||||
av++;
|
|
||||||
else if ((*av)[0] == '-')
|
|
||||||
av++;
|
|
||||||
else
|
|
||||||
return (av);
|
|
||||||
}
|
|
||||||
return (av);
|
|
||||||
}
|
|
||||||
|
|
@ -16,6 +16,7 @@ int bt_read_getdelim(char ***av, t_read *data)
|
||||||
{
|
{
|
||||||
if (!av || !*av)
|
if (!av || !*av)
|
||||||
return (1);
|
return (1);
|
||||||
|
if (data)
|
||||||
data->delim = ***av;
|
data->delim = ***av;
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
@ -24,17 +25,17 @@ int bt_read_getnchars(char ***av, t_read *data)
|
||||||
{
|
{
|
||||||
if (!av || !*av)
|
if (!av || !*av)
|
||||||
return (1);
|
return (1);
|
||||||
|
if (data)
|
||||||
data->nchars = ft_atoi(**av);
|
data->nchars = ft_atoi(**av);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bt_read_getprompt(char ***av, t_read *data)
|
int bt_read_getprompt(char ***av, t_read *data)
|
||||||
{
|
{
|
||||||
DG("getting prompt");
|
|
||||||
if (!av || !*av || !**av)
|
if (!av || !*av || !**av)
|
||||||
return (1);
|
return (1);
|
||||||
|
if (data)
|
||||||
data->prompt = **av;
|
data->prompt = **av;
|
||||||
DG("got prompt [%s]", data->prompt);
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,6 +43,7 @@ int bt_read_gettimeout(char ***av, t_read *data)
|
||||||
{
|
{
|
||||||
if (!av || !*av)
|
if (!av || !*av)
|
||||||
return (1);
|
return (1);
|
||||||
|
if (data)
|
||||||
data->timeout = ft_atoi(**av);
|
data->timeout = ft_atoi(**av);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
@ -50,6 +52,7 @@ int bt_read_getfd(char ***av, t_read *data)
|
||||||
{
|
{
|
||||||
if (!av || !*av)
|
if (!av || !*av)
|
||||||
return (1);
|
return (1);
|
||||||
|
if (data)
|
||||||
data->fd = ft_atoi(**av);
|
data->fd = ft_atoi(**av);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,17 +27,13 @@ int bt_read_terminit(t_read *data)
|
||||||
|
|
||||||
(void)data;
|
(void)data;
|
||||||
term = bt_read_term(1);
|
term = bt_read_term(1);
|
||||||
term.c_lflag &= ~(ICANON | ECHO);
|
term.c_lflag = ECHO | ECHOE | ECHOK | ICANON;
|
||||||
if (data->opts & READ_OPT_LT)
|
term.c_lflag &= data->timeout ? ~ICANON : ~0;
|
||||||
{
|
if (data->opts & READ_OPT_LS)
|
||||||
|
term.c_lflag &= ~ECHO;
|
||||||
term.c_cc[VTIME] = data->timeout * 10;
|
term.c_cc[VTIME] = data->timeout * 10;
|
||||||
term.c_cc[VMIN] = 0;
|
term.c_cc[VMIN] = !data->timeout;
|
||||||
}
|
term.c_cc[VEOL] = data->delim;
|
||||||
else
|
|
||||||
{
|
|
||||||
term.c_cc[VTIME] = 0;
|
|
||||||
term.c_cc[VMIN] = 1;
|
|
||||||
}
|
|
||||||
if (tcsetattr(0, TCSANOW, &term) < 0)
|
if (tcsetattr(0, TCSANOW, &term) < 0)
|
||||||
return (-1);
|
return (-1);
|
||||||
return (0);
|
return (0);
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ int builtin_export(
|
||||||
ft_perror();
|
ft_perror();
|
||||||
if (data.flag & BT_EXPORT_LP)
|
if (data.flag & BT_EXPORT_LP)
|
||||||
return (bt_export_print());
|
return (bt_export_print());
|
||||||
av = cliopts_getdata((char**)av);
|
av = data.av_data;
|
||||||
while (*av)
|
while (*av)
|
||||||
{
|
{
|
||||||
if ((equal = ft_strchr(*av, '=')))
|
if ((equal = ft_strchr(*av, '=')))
|
||||||
|
|
|
||||||
|
|
@ -38,18 +38,18 @@ int bt_read_init(t_read *data, char **av)
|
||||||
data->fd = 0;
|
data->fd = 0;
|
||||||
data->timeout = 0;
|
data->timeout = 0;
|
||||||
data->input = NULL;
|
data->input = NULL;
|
||||||
data->names = NULL;
|
|
||||||
if ((cliopts_get(av, g_read_opts, data)))
|
if ((cliopts_get(av, g_read_opts, data)))
|
||||||
return(ft_perror());
|
return(ft_perror());
|
||||||
if (data->names)
|
DG("opts=%b", data->opts);
|
||||||
DG("%s,%s", data->names[0], data->names[1]);
|
|
||||||
bt_read_terminit(data);
|
bt_read_terminit(data);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bt_read_loop(t_read *data)
|
int bt_read_loop(t_read *data)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int esc;
|
int esc;
|
||||||
|
int ret;
|
||||||
char buf[2];
|
char buf[2];
|
||||||
|
|
||||||
esc = 0;
|
esc = 0;
|
||||||
|
|
@ -58,16 +58,15 @@ int bt_read_loop(t_read *data)
|
||||||
ft_printf(data->prompt);
|
ft_printf(data->prompt);
|
||||||
while (42)
|
while (42)
|
||||||
{
|
{
|
||||||
if (read(data->fd, buf, 1) <= 0)
|
if ((ret = read(data->fd, buf, 1)) <= 0)
|
||||||
return (1);
|
return (ret);
|
||||||
buf[1] = 0;
|
buf[ret] = 0;
|
||||||
if (!(data->opts &READ_OPT_LS))
|
DG("read [%c]", *buf);
|
||||||
ft_putchar(*buf);
|
|
||||||
if (!esc && *buf == data->delim)
|
if (!esc && *buf == data->delim)
|
||||||
break ;
|
break ;
|
||||||
esc = esc ? 0 : !(data->opts & READ_OPT_LR) && (*buf == '\\');
|
esc = esc ? 0 : !(data->opts & READ_OPT_LR) && (*buf == '\\');
|
||||||
ft_strappend(&data->input, buf);
|
ft_strappend(&data->input, buf);
|
||||||
if (*buf == '\n' && !(data->opts & READ_OPT_LR))
|
if (*buf == '\n' && !(data->opts & (READ_OPT_LR | READ_OPT_LS)))
|
||||||
ft_putstr("> ");
|
ft_putstr("> ");
|
||||||
if ((data->opts & READ_OPT_LN) && ++i >= data->nchars)
|
if ((data->opts & READ_OPT_LN) && ++i >= data->nchars)
|
||||||
break ;
|
break ;
|
||||||
|
|
@ -80,25 +79,22 @@ int bt_read_assign(t_read *data)
|
||||||
char *input;
|
char *input;
|
||||||
char **names;
|
char **names;
|
||||||
char *ifs;
|
char *ifs;
|
||||||
char *start;
|
char *tok;
|
||||||
|
|
||||||
|
DG("assigning");
|
||||||
input = data->input;
|
input = data->input;
|
||||||
names = data->names ? data->names : (char*[]){"REPLY", NULL};
|
names = *data->names ? data->names : (char*[]){"REPLY", NULL};
|
||||||
ifs = ft_getenv(data_singleton()->env, "IFS");
|
ifs = ft_getenv(data_singleton()->local_var, "IFS");
|
||||||
start = input;
|
if (!names[1])
|
||||||
while (*start && *names)
|
ifs = NULL;
|
||||||
|
tok = ft_strtok(input, ifs);
|
||||||
|
while (*names)
|
||||||
{
|
{
|
||||||
if (!(names[1]) || !ifs)
|
DG("tok=%s", tok);
|
||||||
{
|
DG("name=%s", *names);
|
||||||
builtin_setenv("setenv", (char*[]){"setenv", *names, start}, NULL);
|
builtin_setenv("setenv", (char*[]){"setenv", *names, tok}, NULL);
|
||||||
break ;
|
ifs = names[1] ? ifs : NULL;
|
||||||
}
|
tok = ft_strtok(NULL, ifs);
|
||||||
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++;
|
names++;
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
|
|
@ -114,10 +110,12 @@ int builtin_read(const char *path, char *const av[], char *const envp[])
|
||||||
ret = 0;
|
ret = 0;
|
||||||
if (bt_read_init(&data, (char **)av))
|
if (bt_read_init(&data, (char **)av))
|
||||||
ret = 2;
|
ret = 2;
|
||||||
else if (bt_read_loop(&data))
|
else if ((ret = bt_read_loop(&data)))
|
||||||
ret = 1;
|
;
|
||||||
else if (bt_read_assign(&data))
|
else if (bt_read_assign(&data))
|
||||||
ret = 1;
|
ret = 1;
|
||||||
|
if (ret == -1)
|
||||||
|
exit(1);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
bt_read_usage();
|
bt_read_usage();
|
||||||
if (ret != 2)
|
if (ret != 2)
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ int data_init(int ac, char **av)
|
||||||
ft_strdel(&shlvl);
|
ft_strdel(&shlvl);
|
||||||
}
|
}
|
||||||
data->comp = NULL;
|
data->comp = NULL;
|
||||||
data->opts = 0;
|
data->opts = SH_INTERACTIVE | SH_OPTS_JOBC;
|
||||||
exec_reset();
|
exec_reset();
|
||||||
data->lst_func = NULL;
|
data->lst_func = NULL;
|
||||||
lexer_init(&data->lexer);
|
lexer_init(&data->lexer);
|
||||||
|
|
|
||||||
|
|
@ -28,19 +28,19 @@ int get_input_fd(char **av)
|
||||||
struct stat buf;
|
struct stat buf;
|
||||||
|
|
||||||
data = data_singleton();
|
data = data_singleton();
|
||||||
|
file = *data->av_data;
|
||||||
if (SH_IS_INTERACTIVE(data->opts))
|
if (SH_IS_INTERACTIVE(data->opts))
|
||||||
return (STDIN);
|
return (STDIN);
|
||||||
else if (data->opts & SH_OPTS_LC)
|
else if (data->opts & SH_OPTS_LC)
|
||||||
{
|
{
|
||||||
pipe(fds);
|
pipe(fds);
|
||||||
file = *cliopts_getdata(av);
|
|
||||||
write(fds[PIPE_WRITE], file, ft_strlen(file));
|
write(fds[PIPE_WRITE], file, ft_strlen(file));
|
||||||
close(fds[PIPE_WRITE]);
|
close(fds[PIPE_WRITE]);
|
||||||
dup2_close(fds[PIPE_READ], (fd = 10));
|
dup2_close(fds[PIPE_READ], (fd = 10));
|
||||||
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||||
return (fd);
|
return (fd);
|
||||||
}
|
}
|
||||||
else if ((file = *cliopts_getdata(av)) && !stat(file, &buf))
|
else if (file && !stat(file, &buf))
|
||||||
{
|
{
|
||||||
fd = -1;
|
fd = -1;
|
||||||
if (S_ISDIR(buf.st_mode))
|
if (S_ISDIR(buf.st_mode))
|
||||||
|
|
@ -85,13 +85,10 @@ int shell_init(int ac, char **av)
|
||||||
|
|
||||||
data = data_singleton();
|
data = data_singleton();
|
||||||
data_init(ac, av);
|
data_init(ac, av);
|
||||||
if (isatty(STDIN) && !*cliopts_getdata(av))
|
|
||||||
{
|
|
||||||
data->opts |= SH_INTERACTIVE;
|
|
||||||
data->opts |= SH_OPTS_JOBC;
|
|
||||||
}
|
|
||||||
if (cliopts_get(av, g_opts, data))
|
if (cliopts_get(av, g_opts, data))
|
||||||
return (ft_perror());
|
return (ft_perror());
|
||||||
|
if (!isatty(STDIN) || *data->av_data)
|
||||||
|
data->opts &= ~(SH_INTERACTIVE | SH_OPTS_JOBC);
|
||||||
data->fd = get_input_fd(av);
|
data->fd = get_input_fd(av);
|
||||||
if (SH_IS_INTERACTIVE(data->opts))
|
if (SH_IS_INTERACTIVE(data->opts))
|
||||||
interactive_settings();
|
interactive_settings();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue