builtin_Read good
This commit is contained in:
parent
2873c44c72
commit
3570d7e53d
6 changed files with 24 additions and 29 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */
|
/* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/03/25 01:24:42 by wescande ### ########.fr */
|
/* Updated: 2017/03/25 04:03:51 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -31,7 +31,7 @@ typedef struct s_readopt t_readopt;
|
||||||
|
|
||||||
struct s_read
|
struct s_read
|
||||||
{
|
{
|
||||||
t_flag opts;
|
t_flag flag;
|
||||||
char **names;
|
char **names;
|
||||||
char delim;
|
char delim;
|
||||||
int nchars;
|
int nchars;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/04 16:31:18 by wescande #+# #+# */
|
/* Created: 2017/01/04 16:31:18 by wescande #+# #+# */
|
||||||
/* Updated: 2017/03/24 18:38:35 by wescande ### ########.fr */
|
/* Updated: 2017/03/25 03:22:10 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -67,12 +67,6 @@ int word_is_assignment(char **content);
|
||||||
void *tab_esc_copy(void *content);
|
void *tab_esc_copy(void *content);
|
||||||
char *get_output(char *command);
|
char *get_output(char *command);
|
||||||
|
|
||||||
/*
|
|
||||||
** return TRUE if path file is a directory.
|
|
||||||
*/
|
|
||||||
|
|
||||||
int is_directory(const char *path);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** return TRUE if char at str_pos in ini_str is escape.
|
** return TRUE if char at str_pos in ini_str is escape.
|
||||||
** five the possibility to set if the char is esc or not.
|
** five the possibility to set if the char is esc or not.
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/14 20:04:04 by jhalford #+# #+# */
|
/* Created: 2017/03/14 20:04:04 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/03/25 02:02:09 by wescande ### ########.fr */
|
/* Updated: 2017/03/25 04:06:39 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -77,7 +77,7 @@ static int cliopts_parse_short(
|
||||||
if ((map->get)(tmp, data))
|
if ((map->get)(tmp, data))
|
||||||
return (ERR_SET(E_CO_MISS, *arg));
|
return (ERR_SET(E_CO_MISS, *arg));
|
||||||
if (map->arg_required)
|
if (map->arg_required)
|
||||||
break ;
|
arg += ft_strlen(arg) - 1;
|
||||||
}
|
}
|
||||||
((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;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/21 18:00:03 by jhalford #+# #+# */
|
/* Created: 2017/01/21 18:00:03 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/03/25 01:27:06 by wescande ### ########.fr */
|
/* Updated: 2017/03/25 04:00:30 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -18,6 +18,7 @@ int bt_read_getdelim(char *opt_arg, t_read *data)
|
||||||
return (1);
|
return (1);
|
||||||
if (data)
|
if (data)
|
||||||
data->delim = *opt_arg;
|
data->delim = *opt_arg;
|
||||||
|
DG("delim=%c", data->delim);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/25 16:02:05 by jhalford #+# #+# */
|
/* Created: 2017/01/25 16:02:05 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/03/22 19:21:35 by jhalford ### ########.fr */
|
/* Updated: 2017/03/25 04:20:00 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -26,15 +26,13 @@ int bt_read_terminit(t_read *data)
|
||||||
struct termios term;
|
struct termios term;
|
||||||
|
|
||||||
term = bt_read_term(1);
|
term = bt_read_term(1);
|
||||||
if (!(data->opts & BT_READ_INTER))
|
if (!(data->flag & BT_READ_INTER))
|
||||||
return (0);
|
return (0);
|
||||||
term.c_lflag = ECHO | ECHOE | ECHOK | ICANON;
|
term.c_lflag = ECHO | ECHOE | ECHOK | ICANON;
|
||||||
{
|
term.c_lflag &= data->timeout ? ~ICANON : ~0;
|
||||||
term.c_lflag &= data->timeout ? ~ICANON : ~0;
|
term.c_cc[VTIME] = data->timeout * 10;
|
||||||
term.c_cc[VTIME] = data->timeout * 10;
|
term.c_cc[VMIN] = data->timeout ? 0 : 1;
|
||||||
term.c_cc[VMIN] = !data->timeout;
|
if (data->flag & BT_READ_LS)
|
||||||
}
|
|
||||||
if (data->opts & BT_READ_LS)
|
|
||||||
term.c_lflag &= ~ECHO;
|
term.c_lflag &= ~ECHO;
|
||||||
term.c_cc[VEOL] = data->delim;
|
term.c_cc[VEOL] = data->delim;
|
||||||
if (tcsetattr(0, TCSANOW, &term) < 0)
|
if (tcsetattr(0, TCSANOW, &term) < 0)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */
|
/* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/03/25 01:28:33 by wescande ### ########.fr */
|
/* Updated: 2017/03/25 04:19:34 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -29,7 +29,7 @@ t_cliopts g_read_opts[] =
|
||||||
|
|
||||||
int bt_read_init(t_read *data, char **av)
|
int bt_read_init(t_read *data, char **av)
|
||||||
{
|
{
|
||||||
data->opts = 0;
|
data->flag = 0;
|
||||||
data->delim = '\n';
|
data->delim = '\n';
|
||||||
data->nchars = -1;
|
data->nchars = -1;
|
||||||
data->prompt = NULL;
|
data->prompt = NULL;
|
||||||
|
|
@ -37,11 +37,11 @@ int bt_read_init(t_read *data, char **av)
|
||||||
data->timeout = 0;
|
data->timeout = 0;
|
||||||
data->input = NULL;
|
data->input = NULL;
|
||||||
if (isatty(STDIN))
|
if (isatty(STDIN))
|
||||||
data->opts |= BT_READ_INTER;
|
data->flag |= BT_READ_INTER;
|
||||||
if (bt_read_terminit(data) < 0)
|
|
||||||
exit(1);
|
|
||||||
if ((cliopts_get(av, g_read_opts, data)))
|
if ((cliopts_get(av, g_read_opts, data)))
|
||||||
return (ft_perror("read"));
|
return (ft_perror("read"));
|
||||||
|
if (bt_read_terminit(data) < 0)
|
||||||
|
exit(1);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,8 +54,9 @@ int bt_read_loop(t_read *data)
|
||||||
|
|
||||||
esc = 0;
|
esc = 0;
|
||||||
i = 0;
|
i = 0;
|
||||||
if (data->prompt && (data->opts & BT_READ_INTER))
|
if (data->prompt && (data->flag & BT_READ_INTER))
|
||||||
ft_printf(data->prompt);
|
ft_printf(data->prompt);
|
||||||
|
DG("nchars; %i, opts=%b", data->nchars, data->flag);
|
||||||
while (42)
|
while (42)
|
||||||
{
|
{
|
||||||
if ((ret = read(data->fd, buf, 1)) <= 0)
|
if ((ret = read(data->fd, buf, 1)) <= 0)
|
||||||
|
|
@ -63,12 +64,13 @@ int bt_read_loop(t_read *data)
|
||||||
buf[ret] = 0;
|
buf[ret] = 0;
|
||||||
if (!esc && *buf == data->delim)
|
if (!esc && *buf == data->delim)
|
||||||
break ;
|
break ;
|
||||||
esc = esc ? 0 : !(data->opts & BT_READ_LR) && (*buf == '\\');
|
esc = esc ? 0 : !(data->flag & BT_READ_LR) && (*buf == '\\');
|
||||||
ft_strappend(&data->input, buf);
|
ft_strappend(&data->input, buf);
|
||||||
if (*buf == '\n' && !(data->opts &
|
if (*buf == '\n' && !(data->flag &
|
||||||
(BT_READ_LR | BT_READ_LS | BT_READ_INTER)))
|
(BT_READ_LR | BT_READ_LS | BT_READ_INTER)))
|
||||||
ft_putstr("> ");
|
ft_putstr("> ");
|
||||||
if ((data->opts & BT_READ_LN) && ++i >= data->nchars)
|
DG("%i/%i", i, data->nchars);
|
||||||
|
if ((data->flag & BT_READ_LN) && ++i >= data->nchars)
|
||||||
break ;
|
break ;
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue