some refactoring of readline.c

This commit is contained in:
Jack Halford 2017-02-10 05:30:17 +01:00
parent 6ec9f9295e
commit 1167a23ca3

View file

@ -42,7 +42,7 @@ void ft_init_history(void)
close(fd); close(fd);
} }
struct termios *ft_save_stats_term(int save) struct termios *ft_save_termios(int save)
{ {
static struct termios *term_save = NULL; static struct termios *term_save = NULL;
@ -54,40 +54,40 @@ struct termios *ft_save_stats_term(int save)
return (term_save); return (term_save);
} }
struct termios *ft_stats_term_termcaps(void) void ft_init_termios(void)
{ {
static struct termios *term = NULL; struct termios term;
if (!term) tcgetattr(0, &term);
term.c_lflag &= ~(ECHO | ICANON | ISIG);
term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &term);
}
void readline_init(char *prompt)
{ {
ft_save_termios(1);
ft_init_line(); ft_init_line();
ft_init_history(); ft_init_history();
term = (struct termios *)malloc(sizeof(struct termios)); ft_init_termios();
tcgetattr(0, term); if (STR)
(*term).c_lflag &= ~(ECHO | ICANON | ISIG); ft_strdel(&STR);
(*term).c_cc[VMIN] = 1; data_singleton()->line.list_cur = data_singleton()->line.list_beg;
(*term).c_cc[VTIME] = 0; POS = 0;
} prompt ? ft_putstr(prompt) : ft_prompt();
return (term);
} }
char *readline(char *prompt) char *readline(char *prompt)
{ {
char *input; char *input;
ft_save_stats_term(1); readline_init(prompt);
if (tcsetattr(0, TCSANOW, ft_stats_term_termcaps()) == -1)
return (NULL);
if (STR)
ft_strdel(&STR);
data_singleton()->line.list_cur = data_singleton()->line.list_beg;
POS = 0;
prompt ? ft_putstr(prompt) : ft_prompt();
input = ft_read_stdin(); input = ft_read_stdin();
ft_putchar('\n'); ft_putchar('\n');
/* ft_check_line(); */ /* ft_check_line(); */
/* ft_check_heredoc(&STR); */ /* ft_check_heredoc(&STR); */
if (tcsetattr(0, TCSANOW, ft_save_stats_term(0)) == -1) if (tcsetattr(0, TCSANOW, ft_save_termios(0)) == -1)
return (NULL); return (NULL);
return (input); return (input);