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