Merge branch 'pda' of https://github.com/jzck/42sh into pda

"conflict resolved"
This commit is contained in:
ariard@student.42.fr 2017-02-21 20:33:12 +01:00
commit 04c4978c35
17 changed files with 84 additions and 159 deletions

View file

@ -191,7 +191,6 @@ lexer/lexer_paren.c\
lexer/lexer_quote.c\
lexer/lexer_sep.c\
lexer/lexer_word.c\
lexer/stack_to_prompt.c\
lexer/token_append.c\
lexer/token_cmp_type.c\
lexer/token_free.c\
@ -219,7 +218,6 @@ main/data_exit.c\
main/data_init.c\
main/data_singleton.c\
main/ft_putast.c\
main/ft_putast2.c\
main/main.c\
main/shell_exit.c\
main/shell_get_avdata.c\
@ -235,12 +233,12 @@ parser/build_tree.c\
parser/error_syntax.c\
parser/eval_sym.c\
parser/ft_parse.c\
parser/parser_init.c\
parser/pop_stack.c\
parser/produce_sym.c\
parser/push_stack.c\
parser/read_stack.c\
parser/tree_wrapper.c\
parser/parser_init.c
parser/tree_wrapper.c
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */
/* Updated: 2017/02/21 14:27:57 by jhalford ### ########.fr */
/* Updated: 2017/02/21 19:51:19 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(int fd, char *prompt);
int readline(int fd, int prompt, char **input);
int ft_completion(int ret);
struct termios *ft_save_termios(int save);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2017/02/21 14:28:48 by jhalford ### ########.fr */
/* Updated: 2017/02/21 18:51:18 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,6 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */
/* Updated: 2017/02/21 20:28:27 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */
/* Updated: 2017/02/20 22:00:03 by jhalford ### ########.fr */
/* Updated: 2017/02/21 20:14:41 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */
/* Updated: 2017/02/20 21:03:48 by ariard ### ########.fr */
/* Updated: 2017/02/21 20:09:54 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/08 14:30:07 by jhalford #+# #+# */
/* Updated: 2017/01/31 15:05:31 by jhalford ### ########.fr */
/* Updated: 2017/02/21 20:09:24 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -1,38 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_to_prompt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 21:25:26 by jhalford #+# #+# */
/* Updated: 2017/02/20 22:13:54 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "lexer.h"
char *stack_to_prompt(t_list *stack)
{
t_flag top;
if (!stack)
return (NULL);
top = *(int*)stack->content;
if (top == BACKSLASH)
return ("> ");
else if (top == QUOTE)
return ("quote> ");
else if (top == DQUOTE)
return ("dquote> ");
else if (top == BQUOTE)
return ("bquote> ");
else if (top == DQUOTE_BQUOTE)
return ("dquote bquote> ");
else if (top == PAREN)
return ("subsh> ");
else if (top == DLESS)
return ("heredoc> ");
else
return ("error> ");
}

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */
/* Updated: 2017/02/21 14:20:16 by jhalford ### ########.fr */
/* Updated: 2017/02/21 20:04:31 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -76,7 +76,7 @@ void ft_init_termios(void)
tcsetattr(0, TCSANOW, &term);
}
void readline_init(char *prompt)
void readline_init(int prompt)
{
static int beg = 0;
@ -92,24 +92,23 @@ void readline_init(char *prompt)
ft_strdel(&STR);
data_singleton()->line.list_cur = data_singleton()->line.list_beg;
POS = 0;
prompt ? ft_putstr(prompt) : ft_prompt();
prompt ? ft_putstr("> ") : ft_prompt();
}
char *readline(int fd, char *prompt)
int readline(int fd, int prompt, char **input)
{
char *input;
if (fd != STDIN)
if (!SH_IS_INTERACTIVE(data_singleton()->opts))
{
get_next_line(fd, &input);
return (input);
if (get_next_line(fd, input) == 0)
return (1);
return (0);
}
readline_init(prompt);
input = ft_read_stdin();
*input = ft_read_stdin();
ft_putchar('\n');
if (!prompt)
input = ft_history_parsing();
*input = ft_history_parsing();
if (tcsetattr(0, TCSANOW, ft_save_termios(0)) == -1)
return (NULL);
return (input);
return (-1);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */
/* Updated: 2017/02/20 22:19:40 by jhalford ### ########.fr */
/* Updated: 2017/02/21 19:20:20 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,10 +15,6 @@
char *ft_putast(void *nodein)
{
t_astnode *node;
struct s_tmp{
char str[5];
t_type type;
};
node = nodein;
if (node->type == TK_THEN)
return ("THEN");

View file

@ -1,59 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tree_type.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */
/* Updated: 2017/02/20 22:08:42 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_putast2(void *nodein)
{
t_astnode *node;
node = nodein;
if (node->type == TK_SEMI)
ft_putendl_fd("SEMI", 2);
else if (node->type == TK_PIPE)
ft_putendl_fd("PIPE", 2);
else if (node->type == TK_COMMAND)
{
ft_putstr_fd("COMMAND: ", 2);
ft_sstrprint_fd(2, node->data.sstr, ',');
ft_putchar_fd('\n', 2);
}
else if (TK_REDIR(node->type))
{
ft_putnbr_fd(node->data.redir.n, 2);
if (node->type == TK_GREATAND || node->type == TK_LESSAND)
{
if (node->type == TK_GREATAND)
ft_putstr_fd(">&:", 2);
else
ft_putstr_fd("<&:", 2);
ft_putnbr_fd(node->data.redir.word.fd, 2);
if (node->data.redir.close)
ft_putstr_fd(" (closed)", 2);
}
else
{
if (node->type == TK_GREAT)
ft_putendl_fd(">:", 2);
else if (node->type == TK_LESS)
ft_putstr_fd("<:", 2);
else if (node->type == TK_DGREAT)
ft_putstr_fd(">>:", 2);
else if (node->type == TK_DLESS)
ft_putstr_fd("<<:", 2);
ft_putstr_fd(node->data.redir.word.word, 2);
}
ft_putchar_fd('\n', 2);
}
else
ft_putendl_fd("OTHER", 2);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/02/21 18:51:41 by ariard ### ########.fr */
/* Updated: 2017/02/21 20:31:43 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,16 +20,24 @@ int handle_instruction(int fd)
t_parser parser;
t_btree *ast;
char *str;
int ret;
lexer_init(&lexer);
parser_init(&parser);
token = NULL;
ast = NULL;
/* str = NULL; */
DG("START: state=%i", parser.state);
while (1)
{
str = readline(fd, stack_to_prompt(lexer.stack));
if (parser.state == UNDEFINED && !str)
return (error_EOF());
if ((ret = readline(fd, get_lexer_stack(lexer), &str)))
{
DG("ret=%i, str=%s, state=%i", ret, str, parser.state);
if (ret == -1)
return (-1);
return (parser.state == UNDEFINED ? error_EOF() : 1);
}
DG("ret=%i, str=%s", ret, str);
ft_strappend(&lexer.str, str);
if (get_lexer_stack(lexer) == BACKSLASH)
pop(&lexer.stack);
@ -40,33 +48,48 @@ int handle_instruction(int fd)
return (1);
//token_print(token);
if (get_lexer_stack(lexer))
continue;
continue ;
if (ft_parse(&ast, &token, &parser))
continue;
continue ;
DG("AFTER PARSING: state=%i", parser.state);
if (parser.state == SUCCESS)
break;
if (parser.state == ERROR)
break ;
else if (parser.state == ERROR)
return (error_syntax(&token));
}
DG("succesful parsing:");
btree_print(STDBUG, ast, &ft_putast);
// if (ft_exec(&ast))
// return (1);
/* if (ft_exec(&ast)) */
/* return (1); */
ft_add_str_in_history(lexer.str);
return (1);
return (0);
}
int get_input_fd()
{
t_data *data;
char *file;
int fds[2];
int fd;
data = data_singleton();
fd = STDIN;
if (SH_IS_INTERACTIVE(data->opts))
return (STDIN);
/* else if (data->opts & SHELL_OPTS_LC) */
/* { */
/* } */
return (fd);
else if (data->opts & SH_OPTS_LC)
{
pipe(fds);
fd = fds[PIPE_READ];
file = shell_get_avdata();
write(fds[PIPE_WRITE], file, ft_strlen(file));
close(fds[PIPE_WRITE]);
fcntl(fd, F_SETFD, FD_CLOEXEC);
return (fd);
}
else if ((file = shell_get_avdata()))
return (open(file, O_RDONLY));
else
return (open(shell_get_avdata(), O_RDONLY));
return (STDIN);
}
int main(int ac, char **av)
@ -75,13 +98,15 @@ int main(int ac, char **av)
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");
DG("{inv}{bol}{gre}start of shell{eoc} JOBC is %s",
SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF");
fd = get_input_fd();
while (handle_instruction(fd))
while (handle_instruction(fd) == 0)
{
// lexer_clean;
// parser_clean;
;
}
shell_exit();
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 17:37:04 by jhalford #+# #+# */
/* Updated: 2017/02/20 21:04:01 by ariard ### ########.fr */
/* Updated: 2017/02/21 20:14:43 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,9 +14,13 @@
void shell_exit(void)
{
// DG("shell_exit()");
t_data *data;
/* DG("shell_exit()"); */
data = data_singleton();
data_exit();
if (SH_HAS_JOBC(data_singleton()->opts))
if (SH_HAS_JOBC(data->opts))
job_kill_all();
tcsetattr(STDIN, TCSANOW, &data_singleton()->jobc.shell_tmodes);
if (SH_IS_INTERACTIVE(data->opts))
tcsetattr(STDIN, TCSANOW, &data_singleton()->jobc.shell_tmodes);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */
/* Updated: 2017/02/20 21:00:32 by ariard ### ########.fr */
/* Updated: 2017/02/21 20:14:44 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,7 +21,7 @@ void shell_init(int ac, char **av)
data = data_singleton();
data->argc = ac;
data->argv = ft_sstrdup(av);
atexit(&shell_exit);
/* atexit(&shell_exit); */
shell_get_opts(ac, av);
if (SH_IS_INTERACTIVE(data->opts))
{

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 20:15:35 by ariard #+# #+# */
/* Updated: 2017/02/21 18:13:47 by ariard ### ########.fr */
/* Updated: 2017/02/21 19:27:59 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,6 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */
/* Updated: 2017/02/21 18:52:33 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,25 +29,27 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser)
{
while (*token)
{
produce_sym(*parser->stack, parser->new_sym, token);
produce_sym(*parser->stack, parser->new_sym, token);
DG("new sym %s", read_state(*parser->new_sym));
if (eval_sym(*parser->stack, *parser->new_sym))
parser->state = ERROR;
else
{
{
aggregate_sym(&parser->stack, parser->new_sym, &parser->state);
push_stack(++parser->stack, *parser->new_sym);
}
ft_putstr("\n");
if (*parser->stack == PROGRAM)
parser->state = SUCCESS;
else
parser->state = UNDEFINED;
build_tree(ast, token);
btree_print(STDBUG, *ast, &ft_putast);
if ((end_instruction(*parser->stack) && !(*token)->next)
|| *parser->stack == PROGRAM)
if ((end_instruction(*parser->stack) && !(*token)->next))
/* || *parser->stack == PROGRAM) */
insert_linebreak(token);
else
ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free);
}
ft_read_stack(parser->stack);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/21 16:14:04 by ariard #+# #+# */
/* Updated: 2017/02/21 16:38:13 by ariard ### ########.fr */
/* Updated: 2017/02/21 20:01:40 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,7 @@
void parser_init(t_parser *parser)
{
parser->state = UNDEFINED;
parser->state = SUCCESS;
parser->new_sym = ft_memalloc(sizeof(t_sym));
parser->stack = ft_memalloc(sizeof(t_sym) * 1000);
push_stack(parser->stack, LINEBREAK);