main refactoring

This commit is contained in:
Jack Halford 2017-03-16 14:20:44 +01:00
parent 1c8148af3f
commit 6efd4c7e1f
16 changed files with 90 additions and 63 deletions

View file

@ -266,6 +266,7 @@ line-editing/resize.c\
main/data_exit.c\
main/data_init.c\
main/data_singleton.c\
main/error_msg.c\
main/ft_putast.c\
main/instruction_free.c\
main/main.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */
/* Updated: 2017/03/15 21:55:22 by jhalford ### ########.fr */
/* Updated: 2017/03/16 14:19:03 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,11 +19,11 @@
# define BT_EXPORT_LP (1 << 0)
# define MATHERR_0 "{red}math : invalid number of arguments{eoc}\n"
# define MATHERR_1 "{red}math : invalid variable name{eoc}\n"
# define MATHERR_2 "{red}math : invalid operator{eoc}\n"
# define MATHERR_3 "{red}math : invalid operand{eoc}\n"
# define MATHERR_4 "{red}math : division by 0{eoc}\n"
# define MATHERR_0 "math : invalid number of arguments\n"
# define MATHERR_1 "math : invalid variable name\n"
# define MATHERR_2 "math : invalid operator\n"
# define MATHERR_3 "math : invalid operand\n"
# define MATHERR_4 "math : division by 0\n"
t_execf *is_builtin(t_process *p);
int builtin_export(const char *path, char *const av[], char *const envp[]);

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */
/* Updated: 2017/03/16 12:10:24 by gwojda ### ########.fr */
/* Updated: 2017/03/16 13:55:48 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -155,7 +155,7 @@ void ft_c(void);
void ft_x(void);
void ft_v(void);
void ft_read_it(int input, size_t *pos, char **str);
int readline(int fd, int prompt, char **input);
int readline(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/03/15 16:35:42 by gwojda ### ########.fr */
/* Updated: 2017/03/16 14:15:02 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -51,10 +51,11 @@ int shell_init(int ac, char **av);
void shell_exit(void);
int data_init(int ac, char **av);
void data_exit(void);
int error_msg(char *msg);
int instruction_free(t_list **token, t_parser *parser,
t_btree **ast);
void content_free(void *data, size_t content_size);
int instruction_free(
t_list **token, t_btree **ast, t_lexer *lexer, t_parser *parser);
/* void content_free(void *data, size_t content_size); */
char *ft_putast(void *node);
void ft_putast2(void *node);

View file

@ -59,10 +59,9 @@ struct s_errormatch
char *error;
};
extern t_aggrematch g_aggrematch[];
extern t_prodmatch g_prodmatch[];
extern t_stackmatch g_stackmatch[];
extern t_errormatch g_errormatch[];
/* extern t_aggrematch g_aggrematch[]; */
/* extern t_prodmatch g_prodmatch[]; */
/* extern t_stackmatch g_stackmatch[]; */
void parser_init(t_parser *parser);
int ft_parse(t_btree **ast, t_list **token, t_parser *parser);

@ -1 +1 @@
Subproject commit 0c1dbf5b89c99612c49d6fd63dd25d0310bad3d9
Subproject commit 04b8d5cd090e86b0f9d8160b01e49d43666deddf

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */
/* Updated: 2017/03/15 20:55:40 by ariard ### ########.fr */
/* Updated: 2017/03/16 14:17:57 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 10:58:49 by ariard #+# #+# */
/* Updated: 2017/03/15 21:02:59 by ariard ### ########.fr */
/* Updated: 2017/03/16 14:18:05 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,7 +26,7 @@ static int get_value(char **var, char **value)
char *temp;
if (!word_is_assignment(var))
return (ft_error_message(MATHERR_1));
return (error_msg(MATHERR_1));
temp = ft_sstrstr(data_singleton()->local_var, *var);
if (temp)
{
@ -54,7 +54,7 @@ static int do_math(char **value, char *operator, char *operand)
else
ope2 = 0;
if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0)
return (ft_error_message(MATHERR_4));
return (error_msg(MATHERR_4));
else
{
ope1 = (operator[0] == '+') ? ope1 + ope2 : ope1;
@ -78,7 +78,7 @@ int builtin_math(const char *path, char *const av[], char *const envp[])
(void)path;
(void)envp;
if (!av || !av[1] || !av[2] || !av[3] || av[4])
return (ft_error_message(MATHERR_0));
return (error_msg(MATHERR_0));
init_math(&var, &value, &operator, &operand);
var = av[1];
if (get_value(&var, &value) == -1)
@ -86,10 +86,10 @@ int builtin_math(const char *path, char *const av[], char *const envp[])
operator = av[2];
if (!(ft_strlen(operator) == 1 && (operator[0] == '+' || operator[0] == '-'
|| operator[0] == '/' || operator[0] == '*' || operator[0] == '%')))
return (ft_error_message(MATHERR_2));
return (error_msg(MATHERR_2));
operand = av[3];
if (!ft_stris(operand, &ft_isdigit))
return (ft_error_message(MATHERR_3));
return (error_msg(MATHERR_3));
if (do_math(&value, operator, operand) == -1)
return (-1);
builtin_setenv("setenv", (char *[]){"local", var, value, 0}, data_singleton()->local_var);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/29 16:04:18 by jhalford #+# #+# */
/* Updated: 2017/03/15 18:06:06 by jhalford ### ########.fr */
/* Updated: 2017/03/16 14:01:26 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:48:10 by jhalford #+# #+# */
/* Updated: 2017/03/15 18:11:49 by jhalford ### ########.fr */
/* Updated: 2017/03/16 13:54:18 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/02 15:17:28 by gwojda #+# #+# */
/* Updated: 2017/03/16 10:17:13 by gwojda ### ########.fr */
/* Updated: 2017/03/16 14:05:00 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,18 +6,18 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */
/* Updated: 2017/03/15 03:52:15 by wescande ### ########.fr */
/* Updated: 2017/03/16 14:06:39 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int readline(int fd, int prompt, char **input)
int readline(int prompt, char **input)
{
int ret;
if (!SH_IS_INTERACTIVE(data_singleton()->opts))
return ((ret = get_next_line(fd, input)) >= 0 ? !ret : ret);
return ((ret = get_next_line(STDIN, input)) >= 0 ? !ret : ret);
readline_init(prompt);
*input = ft_read_stdin();
if (STR)

19
42sh/src/main/error_msg.c Normal file
View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* error_msg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/16 14:14:47 by jhalford #+# #+# */
/* Updated: 2017/03/16 14:17:29 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int error_msg(char *msg)
{
ft_dprintf(2, "{red}%s{eoc}\n", msg);
return (-1);
}

View file

@ -6,13 +6,13 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/15 15:54:59 by gwojda #+# #+# */
/* Updated: 2017/03/15 16:49:10 by gwojda ### ########.fr */
/* Updated: 2017/03/16 14:13:33 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int instruction_free(t_list **token, t_parser *parser, t_btree **ast)
int instruction_free(t_list **token, t_btree **ast, t_lexer *lexer, t_parser *parser)
{
ft_lstdel(token, &token_free);
token = NULL;
@ -20,6 +20,7 @@ int instruction_free(t_list **token, t_parser *parser, t_btree **ast)
btree_del(ast, &ft_lst_cfree);
ft_lstdel(&data_singleton()->heredoc_queue, &redir_free);
ft_lstdel(&data_singleton()->exec.op_stack, &ft_lst_cfree);
ft_strdel(&lexer->str);
free(parser->new_sym);
return (0);
}

View file

@ -6,35 +6,30 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/03/15 17:55:12 by jhalford ### ########.fr */
/* Updated: 2017/03/16 14:10:44 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int handle_instruction(int fd)
static int handle_instruction( t_list **token,
t_btree **ast,
t_lexer lexer,
t_parser parser)
{
t_list *token;
t_list *ltoken;
t_lexer lexer;
t_parser parser;
t_btree *ast;
char *str;
int ret;
lexer_init(&lexer);
parser_init(&parser);
token = NULL;
ast = NULL;
while (1)
{
if ((ret = readline(fd, get_lexer_stack(lexer) ||
if ((ret = readline(get_lexer_stack(lexer) ||
parser.state == UNDEFINED || lexer.state == HEREDOC, &str)))
{
if (ret == -1)
return (-1);
return (parser.state == UNDEFINED ? error_eof(&token,
&parser, &ast) : 1);
return (parser.state == UNDEFINED ? error_eof(token,
&parser, ast) : 1);
}
if (lexer.state == HEREDOC)
{
@ -44,21 +39,21 @@ int handle_instruction(int fd)
ft_strappend(&lexer.str, str);
if (get_lexer_stack(lexer) == BACKSLASH)
pop(&lexer.stack);
ltoken = ft_lstlast(token);
if (lexer_lex(token ? &ltoken : &token, &lexer))
ltoken = ft_lstlast(*token);
if (lexer_lex(token ? &ltoken : token, &lexer))
return (1);
if (get_lexer_stack(lexer) > 2)
continue ;
lexer.state = DEFAULT;
if (get_reserved_words(&token))
if (get_reserved_words(token))
return (1);
token_print(token);
if (insert_newline(&token))
token_print(*token);
if (insert_newline(token))
return (1);
if (ft_parse(&ast, &token, &parser))
if (ft_parse(ast, token, &parser))
continue ;
if (parser.state == ERROR)
error_syntax(&token, &parser, &ast);
error_syntax(token, &parser, ast);
lexer.state = data_singleton()->heredoc_queue ? HEREDOC : 0;
if (lexer.state)
continue;
@ -67,16 +62,14 @@ int handle_instruction(int fd)
else if (parser.state == ERROR && SH_IS_INTERACTIVE(data_singleton()->opts))
{
ft_add_str_in_history(lexer.str);
return (error_syntax(&token, &parser, &ast));
return (error_syntax(token, &parser, ast));
}
}
/* btree_print(STDBUG, ast, &ft_putast); */
if (ft_exec(&ast))
if (ft_exec(ast))
return (2);
instruction_free(&token, &parser, &ast);
if (SH_IS_INTERACTIVE(data_singleton()->opts) && *lexer.str)
ft_add_str_in_history(lexer.str);
ft_strdel(&lexer.str);
return (0);
}
@ -118,6 +111,10 @@ int get_input_fd(char **av)
int main(int ac, char **av)
{
int fd;
t_list *token;
t_lexer lexer;
t_parser parser;
t_btree *ast;
g_argv = av;
setlocale(LC_ALL, "");
@ -126,9 +123,16 @@ int main(int ac, char **av)
return (1);
if ((fd = get_input_fd(av)) < 0)
return (1);
dup2_close(fd, STDIN);
lexer_init(&lexer);
parser_init(&parser);
token = NULL;
ast = NULL;
DG("JOBC is %s, fd=[%i]", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF", fd);
while (handle_instruction(fd) == 0)
;
while (handle_instruction(&token, &ast, lexer, parser) == 0)
{
instruction_free(&token, &ast, &lexer, &parser);
}
builtin_exit(NULL, NULL, NULL);
return (0);
}

View file

@ -6,13 +6,13 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 20:15:35 by ariard #+# #+# */
/* Updated: 2017/03/15 04:12:31 by wescande ### ########.fr */
/* Updated: 2017/03/16 14:12:14 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_errormatch g_errormatch[] =
static t_errormatch g_errormatch[] =
{
{TK_NEWLINE, "newline"},
{TK_SEMI, ";"},
@ -57,6 +57,8 @@ int error_syntax(t_list **lst, t_parser *parser,
t_token *token;
int i;
(void)parser;
(void)ast;
if (!*lst)
return (0);
token = (*lst)->content;
@ -67,19 +69,19 @@ int error_syntax(t_list **lst, t_parser *parser,
{
ft_dprintf(2, "syntax error near unexpected token « %s »\n",
g_errormatch[i].error);
instruction_free(lst, parser, ast);
return (0);
}
i++;
}
ft_dprintf(2, "syntax error near unexpected token « %s »\n", token->data);
instruction_free(lst, parser, ast);
return (0);
}
int error_eof(t_list **lst, t_parser *parser, t_btree **ast)
{
(void)lst;
(void)parser;
(void)ast;
ft_putstr_fd("syntax error near unexpected EOF", 2);
instruction_free(lst, parser, ast);
return (0);
}