cleanup
This commit is contained in:
parent
bed9f1d6c2
commit
3da58bcb52
9 changed files with 88 additions and 89 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */
|
/* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */
|
||||||
/* Updated: 2017/03/16 13:55:48 by jhalford ### ########.fr */
|
/* Updated: 2017/03/16 15:13:25 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -74,6 +74,7 @@ typedef struct s_list_history
|
||||||
typedef struct s_line
|
typedef struct s_line
|
||||||
{
|
{
|
||||||
char *input;
|
char *input;
|
||||||
|
int fd;
|
||||||
char *copy_tmp;
|
char *copy_tmp;
|
||||||
size_t pos;
|
size_t pos;
|
||||||
int prompt_size;
|
int prompt_size;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
|
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/03/16 15:04:04 by jhalford ### ########.fr */
|
/* Updated: 2017/03/16 15:41:33 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -35,6 +35,7 @@
|
||||||
struct s_data
|
struct s_data
|
||||||
{
|
{
|
||||||
t_flag opts;
|
t_flag opts;
|
||||||
|
int fd;
|
||||||
char **env;
|
char **env;
|
||||||
char **local_var;
|
char **local_var;
|
||||||
int argc;
|
int argc;
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,7 @@ int pop_stack(t_list **stack, t_sym erase_sym);
|
||||||
int pop_heredoc(t_list **lst);
|
int pop_heredoc(t_list **lst);
|
||||||
|
|
||||||
int error_syntax(t_list **token, t_parser *parser, t_btree **ast);
|
int error_syntax(t_list **token, t_parser *parser, t_btree **ast);
|
||||||
int error_eof(t_list **token, t_parser *parser, t_btree **ast);
|
int error_eof(void);
|
||||||
|
|
||||||
int ft_read_stack(t_sym *stack);
|
int ft_read_stack(t_sym *stack);
|
||||||
char *read_state(t_sym current);
|
char *read_state(t_sym current);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/07 17:34:44 by gwojda #+# #+# */
|
/* Created: 2017/03/07 17:34:44 by gwojda #+# #+# */
|
||||||
/* Updated: 2017/03/16 14:58:45 by jhalford ### ########.fr */
|
/* Updated: 2017/03/16 15:32:03 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -41,5 +41,5 @@ void readline_init(int has_prompt)
|
||||||
data_singleton()->line.list_cur = data_singleton()->line.list_beg;
|
data_singleton()->line.list_cur = data_singleton()->line.list_beg;
|
||||||
POS = 0;
|
POS = 0;
|
||||||
has_prompt ? ft_prompt() : ft_putstr("> ");
|
has_prompt ? ft_prompt() : ft_putstr("> ");
|
||||||
data_singleton()->line.is_prompt = prompt ? 0 : 1;
|
data_singleton()->line.is_prompt = has_prompt ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */
|
/* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */
|
||||||
/* Updated: 2017/03/16 14:58:47 by jhalford ### ########.fr */
|
/* Updated: 2017/03/16 15:22:11 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/10 11:36:39 by jhalford #+# #+# */
|
/* Created: 2017/01/10 11:36:39 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/03/09 15:22:54 by jhalford ### ########.fr */
|
/* Updated: 2017/03/16 15:12:53 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,46 +6,46 @@
|
||||||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
|
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/03/16 15:02:12 by jhalford ### ########.fr */
|
/* Updated: 2017/03/16 15:45:57 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
static int do_lexer_routine(t_list **token, t_lexer *lexer)
|
static int do_lexer_routine(t_list **token, t_lexer *lexer, t_parser *parser, char *str)
|
||||||
{
|
{
|
||||||
t_list *ltoken;
|
t_list *ltoken;
|
||||||
|
|
||||||
if (lexer->state == HEREDOC)
|
if (lexer->state == HEREDOC || parser->state == UNDEFINED)
|
||||||
{
|
{
|
||||||
ft_strappend(&lexer->str, (char[]){'\n', 0});
|
ft_strappend(&lexer->str, (char[]){'\n', 0});
|
||||||
lexer->pos++;
|
lexer->pos++;
|
||||||
}
|
}
|
||||||
ft_strappend(&lexer->str, str);
|
ft_strappend(&lexer->str, str);
|
||||||
if (get_lexer_stack(lexer) == BACKSLASH)
|
if (get_lexer_stack(*lexer) == BACKSLASH)
|
||||||
pop(&lexer->stack);
|
pop(&lexer->stack);
|
||||||
ltoken = ft_lstlast(*token);
|
ltoken = ft_lstlast(*token);
|
||||||
if (lexer_lex(token ? <oken : token, lexer) < 1)
|
if (lexer_lex(token ? <oken : token, lexer) < 1)
|
||||||
exit(1);
|
exit(1);
|
||||||
if (get_lexer_stack(lexer) > 2)
|
if (get_lexer_stack(*lexer) > 2)
|
||||||
return (1);
|
return (1);
|
||||||
lexer->state = DEFAULT;
|
lexer->state = DEFAULT;
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int do_parser_routine(t_list **token, t_lexer *lexer, t_parser *parser)
|
static int do_parser_routine(t_list **token, t_btree **ast, t_lexer *lexer, t_parser *parser)
|
||||||
{
|
{
|
||||||
if (get_reserved_words(token))
|
if (get_reserved_words(token))
|
||||||
return (1);
|
return (1);
|
||||||
token_print(*token);
|
token_print(*token);
|
||||||
if (insert_newline(token))
|
if (insert_newline(token))
|
||||||
return (1);
|
return (1);
|
||||||
ft_parse(ast, token, &parser);
|
ft_parse(ast, token, parser);
|
||||||
if ((lexer->state = data_singleton()->heredoc_queue ? HEREDOC : DEFAULT))
|
if ((lexer->state = data_singleton()->heredoc_queue ? HEREDOC : DEFAULT))
|
||||||
return (0);
|
return (0);
|
||||||
if (parse->state == ERROR)
|
if (parser->state == ERROR)
|
||||||
{
|
{
|
||||||
error_syntax(token, &parser, ast);
|
error_syntax(token, parser, ast);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
else if (parser->state == SUCCESS)
|
else if (parser->state == SUCCESS)
|
||||||
|
|
@ -53,103 +53,68 @@ static int do_parser_routine(t_list **token, t_lexer *lexer, t_parser *parser)
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int do_readline_routine(t_list **token, t_lexer *lexer, t_parser *parser)
|
static int do_readline_routine(t_lexer *lexer, t_parser *parser, char **str)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
int prompt;
|
int has_prompt;
|
||||||
char *str;
|
|
||||||
|
|
||||||
has_prompt = !(get_lexer_stack(lexer) || parser->state == UNDEFINED || lexer->state == HEREDOC);
|
has_prompt = !(get_lexer_stack(*lexer) || parser->state == UNDEFINED || lexer->state == HEREDOC);
|
||||||
ret = readline(has_prompt, &str);
|
ret = readline(has_prompt, str);
|
||||||
{
|
if (ret == -1)
|
||||||
if (ret == -1)
|
exit(1);
|
||||||
return (-1);
|
if (ret == 1 && parser->state == UNDEFINED)
|
||||||
return (parser.state == UNDEFINED ? error_eof(token,
|
error_eof();
|
||||||
&parser, ast) : 1);
|
return (ret);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int handle_instruction( t_list **token,
|
static int handle_instruction(t_lexer *lexer, t_parser *parser)
|
||||||
t_btree **ast,
|
|
||||||
t_lexer *lexer,
|
|
||||||
t_parser *parser)
|
|
||||||
{
|
{
|
||||||
|
t_list *token;
|
||||||
|
t_btree *ast;
|
||||||
|
int ret;
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
token = NULL;
|
||||||
|
ast = NULL;
|
||||||
|
str = NULL;
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
if (do_readline_routine(lexer, parser) > 0)
|
if ((ret = do_readline_routine(lexer, parser, &str)) > 0)
|
||||||
if (do_lexer_routine(token, lexer) > 0)
|
return (ret);
|
||||||
|
if (do_lexer_routine(&token, lexer, parser, str) > 0)
|
||||||
continue ;
|
continue ;
|
||||||
if (do_parser_routine(token, lexer, parser) > 0)
|
if (do_parser_routine(&token, &ast, lexer, parser) > 0)
|
||||||
break ;
|
break ;
|
||||||
}
|
}
|
||||||
/* btree_print(STDBUG, ast, &ft_putast); */
|
/* btree_print(STDBUG, ast, &ft_putast); */
|
||||||
if (parser->state == SUCCESS && ft_exec(ast) < 0)
|
if (parser->state == SUCCESS && ft_exec(&ast) < 0)
|
||||||
exit(1);
|
exit(1);
|
||||||
if (SH_IS_INTERACTIVE(data_singleton()->opts) && *lexer->str)
|
if (SH_IS_INTERACTIVE(data_singleton()->opts) && *lexer->str)
|
||||||
ft_add_str_in_history(lexer->str);
|
ft_add_str_in_history(lexer->str);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_input_fd(char **av)
|
|
||||||
{
|
|
||||||
t_data *data;
|
|
||||||
char *file;
|
|
||||||
int fds[2];
|
|
||||||
int fd;
|
|
||||||
struct stat buf;
|
|
||||||
|
|
||||||
data = data_singleton();
|
|
||||||
if (SH_IS_INTERACTIVE(data->opts))
|
|
||||||
return (STDIN);
|
|
||||||
else if (data->opts & SH_OPTS_LC)
|
|
||||||
{
|
|
||||||
pipe(fds);
|
|
||||||
dup2_close(fds[PIPE_READ], 10);
|
|
||||||
fd = 10;
|
|
||||||
file = *cliopts_getdata(av);
|
|
||||||
write(fds[PIPE_WRITE], file, ft_strlen(file));
|
|
||||||
close(fds[PIPE_WRITE]);
|
|
||||||
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
||||||
return (fd);
|
|
||||||
}
|
|
||||||
else if ((file = *cliopts_getdata(av)) && !stat(file, &buf))
|
|
||||||
{
|
|
||||||
fd = -1;
|
|
||||||
if (S_ISDIR(buf.st_mode))
|
|
||||||
ft_printf("{red}%s: %s: is a directory\n{eoc}", av[0], file);
|
|
||||||
else if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0)
|
|
||||||
ft_printf("{red}%s: %s: No such file or directory\n{eoc}", av[0], file);
|
|
||||||
if (fd > 0 && !dup2_close(fd, 10) && (fd = 10))
|
|
||||||
return (fd);
|
|
||||||
}
|
|
||||||
return (STDIN);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int ac, char **av)
|
int main(int ac, char **av)
|
||||||
{
|
{
|
||||||
int fd;
|
int ret;
|
||||||
t_list *token;
|
|
||||||
t_lexer lexer;
|
t_lexer lexer;
|
||||||
t_parser parser;
|
t_parser parser;
|
||||||
t_btree *ast;
|
|
||||||
|
|
||||||
g_argv = av;
|
g_argv = av;
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
DG("{inv}{bol}{gre}start of shell{eoc}");
|
DG("{inv}{bol}{gre}start of shell{eoc}");
|
||||||
if (shell_init(ac, av))
|
if (shell_init(ac, av))
|
||||||
return (1);
|
return (1);
|
||||||
if ((fd = get_input_fd(av)) < 0)
|
|
||||||
return (1);
|
|
||||||
dup2_close(fd, STDIN);
|
|
||||||
lexer_init(&lexer);
|
lexer_init(&lexer);
|
||||||
parser_init(&parser);
|
parser_init(&parser);
|
||||||
token = NULL;
|
DG("JOBC is %s", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF");
|
||||||
ast = NULL;
|
while (1)
|
||||||
DG("JOBC is %s, fd=[%i]", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF", fd);
|
|
||||||
while (handle_instruction(&token, &ast, lexer, parser) == 0)
|
|
||||||
{
|
{
|
||||||
instruction_free(&token, &ast, &lexer, &parser);
|
ret = handle_instruction(&lexer, &parser);
|
||||||
|
/* instruction_free(&token, &ast, &lexer, &parser); */
|
||||||
|
if (ret == 1)
|
||||||
|
break ;
|
||||||
}
|
}
|
||||||
builtin_exit(NULL, NULL, NULL);
|
builtin_exit(NULL, NULL, NULL);
|
||||||
return (0);
|
return (0);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */
|
/* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/03/15 13:55:49 by gwojda ### ########.fr */
|
/* Updated: 2017/03/16 15:31:31 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -19,6 +19,41 @@ static t_cliopts g_opts[] =
|
||||||
{0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int get_input_fd(char **av)
|
||||||
|
{
|
||||||
|
t_data *data;
|
||||||
|
char *file;
|
||||||
|
int fds[2];
|
||||||
|
int fd;
|
||||||
|
struct stat buf;
|
||||||
|
|
||||||
|
data = data_singleton();
|
||||||
|
if (SH_IS_INTERACTIVE(data->opts))
|
||||||
|
return (STDIN);
|
||||||
|
else if (data->opts & SH_OPTS_LC)
|
||||||
|
{
|
||||||
|
pipe(fds);
|
||||||
|
dup2_close(fds[PIPE_READ], 10);
|
||||||
|
fd = 10;
|
||||||
|
file = *cliopts_getdata(av);
|
||||||
|
write(fds[PIPE_WRITE], file, ft_strlen(file));
|
||||||
|
close(fds[PIPE_WRITE]);
|
||||||
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||||
|
return (fd);
|
||||||
|
}
|
||||||
|
else if ((file = *cliopts_getdata(av)) && !stat(file, &buf))
|
||||||
|
{
|
||||||
|
fd = -1;
|
||||||
|
if (S_ISDIR(buf.st_mode))
|
||||||
|
ft_printf("{red}%s: %s: is a directory\n{eoc}", av[0], file);
|
||||||
|
else if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0)
|
||||||
|
ft_printf("{red}%s: %s: No such file or directory\n{eoc}", av[0], file);
|
||||||
|
if (fd > 0 && !dup2_close(fd, 10) && (fd = 10))
|
||||||
|
return (fd);
|
||||||
|
}
|
||||||
|
return (STDIN);
|
||||||
|
}
|
||||||
|
|
||||||
static int interactive_settings(void)
|
static int interactive_settings(void)
|
||||||
{
|
{
|
||||||
int *shell_pgid;
|
int *shell_pgid;
|
||||||
|
|
@ -64,7 +99,7 @@ int shell_init(int ac, char **av)
|
||||||
return (ft_perror());
|
return (ft_perror());
|
||||||
if (SH_IS_INTERACTIVE(data->opts))
|
if (SH_IS_INTERACTIVE(data->opts))
|
||||||
interactive_settings();
|
interactive_settings();
|
||||||
else
|
if ((data->fd = get_input_fd(av)) < 0)
|
||||||
DG("not interactive");
|
return (1);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/02/09 20:15:35 by ariard #+# #+# */
|
/* Created: 2017/02/09 20:15:35 by ariard #+# #+# */
|
||||||
/* Updated: 2017/03/16 14:12:14 by jhalford ### ########.fr */
|
/* Updated: 2017/03/16 15:34:39 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -77,11 +77,8 @@ int error_syntax(t_list **lst, t_parser *parser,
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int error_eof(t_list **lst, t_parser *parser, t_btree **ast)
|
int error_eof(void)
|
||||||
{
|
{
|
||||||
(void)lst;
|
|
||||||
(void)parser;
|
|
||||||
(void)ast;
|
|
||||||
ft_putstr_fd("syntax error near unexpected EOF", 2);
|
ft_putstr_fd("syntax error near unexpected EOF", 2);
|
||||||
return (0);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue