From 661cb0eb8b4b0da771a2153d04715228ebd26ae1 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 21 Feb 2017 14:35:14 +0100 Subject: [PATCH] get_fd --- 42sh/includes/ft_readline.h | 4 +- 42sh/includes/minishell.h | 2 +- 42sh/includes/parser.h | 2 +- 42sh/src/exec/exec_command.c | 2 +- 42sh/src/line-editing/readline.c | 9 ++++- 42sh/src/main/main.c | 64 ++++++++++++-------------------- 6 files changed, 35 insertions(+), 48 deletions(-) diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h index 2522979b..9c6f2810 100644 --- a/42sh/includes/ft_readline.h +++ b/42sh/includes/ft_readline.h @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */ -/* Updated: 2017/02/20 20:21:45 by ariard ### ########.fr */ +/* Updated: 2017/02/21 14:27:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -178,7 +178,7 @@ void ft_c(void); void ft_x(void); void ft_v(void); void ft_read_it(int input, size_t *pos, char **str); -char *readline(char *); +char *readline(int fd, char *prompt); int ft_completion(int ret); struct termios *ft_save_termios(int save); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index ea79fd25..1b77b4c1 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/02/20 22:39:18 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 14:28:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index 625551d7..f21a70b1 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ -/* Updated: 2017/02/20 22:39:45 by jhalford ### ########.fr */ +/* Updated: 2017/02/20 22:43:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 9b42f3f9..558804af 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/02/20 22:21:35 by jhalford ### ########.fr */ +/* Updated: 2017/02/20 22:43:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index 5d9795ec..a90789c9 100644 --- a/42sh/src/line-editing/readline.c +++ b/42sh/src/line-editing/readline.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/02/16 12:45:32 by gwojda ### ########.fr */ +/* Updated: 2017/02/21 14:20:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -95,10 +95,15 @@ void readline_init(char *prompt) prompt ? ft_putstr(prompt) : ft_prompt(); } -char *readline(char *prompt) +char *readline(int fd, char *prompt) { char *input; + if (fd != STDIN) + { + get_next_line(fd, &input); + return (input); + } readline_init(prompt); input = ft_read_stdin(); ft_putchar('\n'); diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 5bce1e37..cf710d51 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,54 +6,25 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/02/20 21:52:34 by jhalford ### ########.fr */ +/* Updated: 2017/02/21 14:29:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int non_interactive_shell(char *command) -{ - t_list *token; - t_lexer lexer; - t_btree *ast; - - lexer_init(&lexer); - lexer.str = command; - token = NULL; - ast = NULL; - while (lexer.str[lexer.pos]) - { - if (lexer.stack && *(int*)lexer.stack->content == BACKSLASH) - pop(&lexer.stack); - do { - lexer_lex(&token, &lexer); - } while (lexer.str[lexer.pos] == '\n'); - if (!token) - return (0); -// if (bquotes_expand(&token)) -// return (1); - //token_print(token); - if (ft_parse(&ast, &token)) - return (1); - if (ft_exec(&ast)) - return (1); - } - return (0); -} - -int interactive_shell() +int handle_instruction(int fd) { t_list *token; t_list *ltoken; t_lexer lexer; t_btree *ast; + char *str; lexer_init(&lexer); token = NULL; ast = NULL; do { - char *str = readline(stack_to_prompt(lexer.stack)); + str = readline(fd, stack_to_prompt(lexer.stack)); ft_strappend(&lexer.str, str); if (get_lexer_stack(lexer) == BACKSLASH) pop(&lexer.stack); @@ -64,8 +35,6 @@ int interactive_shell() return (1); //token_print(token); } while (get_lexer_stack(lexer)); -// if (bquotes_expand(&token)) -// return (1); if (!token) return (0); ft_add_str_in_history(lexer.str); @@ -78,20 +47,33 @@ int interactive_shell() return (0); } -int main(int ac, char **av) +int get_input_fd() { t_data *data; data = data_singleton(); + if (SH_IS_INTERACTIVE(data->opts)) + return (STDIN); + /* else if (data->opts & SHELL_OPTS_LC) */ + /* { */ + /* } */ + else + { + return (open(shell_get_avdata(), O_RDONLY)); + } +} + +int main(int ac, char **av) +{ + int fd; + setlocale(LC_ALL, ""); shell_init(ac, av); // DG("{inv}{bol}{gre}start of shell{eoc} JOBC is %s", SH_HAS_JOBC(data->opts)?"ON":"OFF"); - if (SH_IS_INTERACTIVE(data->opts)) + fd = get_input_fd(); + while (handle_instruction(fd)) { - while (1) - interactive_shell(); + ; } - else - non_interactive_shell(shell_get_avdata()); return (0); }