execution functions changes: they now take reference of nodes, they now

delete nodes after operations (suffix).
This commit is contained in:
Jack Halford 2016-12-05 13:15:50 +01:00
parent 768d996b7a
commit d24633c9e6
23 changed files with 127 additions and 80 deletions

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2016/12/03 15:30:31 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:22:40 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,22 +22,22 @@ typedef struct s_execfunc t_execfunc;
struct s_execfunc
{
t_type type;
int (*f)(t_btree *ast, t_data *data);
int (*f)(t_btree **ast, t_data *data);
};
extern t_execfunc g_execfunc[];
int ft_exec(t_btree *ast, t_data *data);
int ft_exec(t_btree **ast, t_data *data);
int exec_semi(t_btree *ast, t_data *data);
int exec_or_if(t_btree *ast, t_data *data);
int exec_and_if(t_btree *ast, t_data *data);
int exec_pipe(t_btree *ast, t_data *data);
int exec_semi(t_btree **ast, t_data *data);
int exec_or_if(t_btree **ast, t_data *data);
int exec_and_if(t_btree **ast, t_data *data);
int exec_pipe(t_btree **ast, t_data *data);
int exec_less(t_btree *ast, t_data *data);
int exec_great(t_btree *ast, t_data *data);
int exec_dgreat(t_btree *ast, t_data *data);
int exec_command(t_btree *ast, t_data *data);
int exec_less(t_btree **ast, t_data *data);
int exec_great(t_btree **ast, t_data *data);
int exec_dgreat(t_btree **ast, t_data *data);
int exec_command(t_btree **ast, t_data *data);
void fd_redirect(t_data *data);
void fd_reset(t_data *data);
@ -45,4 +45,6 @@ void fd_reset(t_data *data);
int ft_cmd_process(char **argv, t_data *data);
int ft_cmd_exec(char *execpath, char **argv, t_data *data);
void ast_free(void *data, size_t content_size);
#endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2016/12/01 18:01:14 by jhalford ### ########.fr */
/* Updated: 2016/12/05 13:14:50 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,19 +15,21 @@
# include "minishell.h"
# define TK_LESS 0x00000001
# define TK_GREAT 0x00000002
# define TK_DLESS 0x00000004
# define TK_DGREAT 0x00000008
# define TK_LESSAND 0x00000010
# define TK_GREATAND 0x00000020
# define TK_SEMI 0x00000040
# define TK_PIPE 0x00000080
# define TK_AND_IF 0x00000100
# define TK_OR_IF 0x00000200
# define TK_AMP 0x00000400
# define TK_WORD 0x00100000
# define TK_COMMAND 0x00200000
typedef long long t_type;
# define TK_LESS 1 << 0
# define TK_GREAT 1 << 1
# define TK_DLESS 1 << 2
# define TK_DGREAT 1 << 3
# define TK_LESSAND 1 << 4
# define TK_GREATAND 1 << 5
# define TK_SEMI 1 << 6
# define TK_PIPE 1 << 7
# define TK_AND_IF 1 << 8
# define TK_OR_IF 1 << 9
# define TK_AMP 1 << 10
# define TK_WORD 1 << 11
# define TK_COMMAND 1 << 12
# define TK_REDIR (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20)

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2016/12/03 15:27:13 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:32:11 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */
/* Updated: 2016/12/03 12:08:15 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:33:54 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,6 +20,7 @@ typedef struct s_astnode t_astnode;
typedef struct s_redir t_redir;
typedef union u_astdata t_astdata;
typedef union u_word t_word;
typedef long long t_type;
struct s_parser
{

31
42sh/src/exec/ast_free.c Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ast_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 11:50:51 by jhalford #+# #+# */
/* Updated: 2016/12/05 12:07:38 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ast_free(void *data, size_t content_size)
{
t_astnode *node;
(void)content_size;
node = data;
if (node->type == TK_COMMAND)
{
if (node->data.sstr)
ft_sstrfree(node->data.sstr);
}
else if (node->type == TK_LESS || node->type == TK_GREAT || node->type == TK_DGREAT)
{
ft_printf("gonna del word of redirection at %p\n", node->data.redir.word.word);
ft_strdel(&node->data.redir.word.word);
}
}

View file

@ -6,13 +6,13 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 20:52:28 by jhalford #+# #+# */
/* Updated: 2016/12/03 15:22:12 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:17:33 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_and_if(t_btree *ast, t_data *data)
int exec_and_if(t_btree **ast, t_data *data)
{
if (data->exec.aol_status == NULL
|| (data->exec.aol_search == TK_AND_IF
@ -20,14 +20,15 @@ int exec_and_if(t_btree *ast, t_data *data)
|| (data->exec.aol_search == TK_OR_IF
&& *data->exec.aol_status != '0'))
{
ft_exec(ast->left, data);
ft_exec(&(*ast)->left, data);
data->exec.aol_status = ft_getenv(data->env, "?");
}
data->exec.aol_search = TK_AND_IF;
if (*data->exec.aol_status == '0'
|| ((t_astnode*)ast->right->item)->type != TK_COMMAND)
ft_exec(ast->right, data);
|| ((t_astnode*)(*ast)->right->item)->type != TK_COMMAND)
ft_exec(&(*ast)->right, data);
data->exec.aol_status = NULL;
data->exec.aol_search = 0;
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,17 +6,18 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */
/* Updated: 2016/12/03 11:56:11 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:12:31 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_command(t_btree *ast, t_data *data)
int exec_command(t_btree **ast, t_data *data)
{
t_astnode *node;
node = ast->item;
node = (*ast)->item;
ft_cmd_process(node->data.sstr, data);
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,21 +6,22 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 18:15:13 by jhalford #+# #+# */
/* Updated: 2016/12/03 15:22:33 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:13:45 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_dgreat(t_btree *ast, t_data *data)
int exec_dgreat(t_btree **ast, t_data *data)
{
t_astnode *node;
int fd;
node = ast->item;
node = (*ast)->item;
fd = open(node->data.redir.word.word, O_WRONLY | O_APPEND | O_CREAT, 0644);
data->exec.fdout = fd;
ft_exec(ast->left, data);
ft_exec(&(*ast)->left, data);
data->exec.fdout = STDOUT;
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,21 +6,22 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:27:51 by jhalford #+# #+# */
/* Updated: 2016/12/03 15:22:43 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:13:28 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_great(t_btree *ast, t_data *data)
int exec_great(t_btree **ast, t_data *data)
{
t_astnode *node;
int fd;
node = ast->item;
node = (*ast)->item;
fd = open(node->data.redir.word.word, O_WRONLY | O_TRUNC | O_CREAT, 0644);
data->exec.fdout = fd;
ft_exec(ast->left, data);
ft_exec(&(*ast)->left, data);
data->exec.fdout = STDOUT;
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,21 +6,22 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:27:08 by jhalford #+# #+# */
/* Updated: 2016/12/03 15:22:51 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:12:59 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_less(t_btree *ast, t_data *data)
int exec_less(t_btree **ast, t_data *data)
{
t_astnode *node;
int fd;
node = ast->item;
node = (*ast)->item;
fd = open(node->data.redir.word.word, O_RDONLY);
data->exec.fdin = fd;
ft_exec(ast->left, data);
ft_exec(&(*ast)->left, data);
data->exec.fdin = STDIN;
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,13 +6,13 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 21:06:17 by jhalford #+# #+# */
/* Updated: 2016/12/03 15:23:28 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:17:55 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_or_if(t_btree *ast, t_data *data)
int exec_or_if(t_btree **ast, t_data *data)
{
if (data->exec.aol_status == NULL
|| (data->exec.aol_search == TK_AND_IF
@ -20,14 +20,15 @@ int exec_or_if(t_btree *ast, t_data *data)
|| (data->exec.aol_search == TK_OR_IF
&& *data->exec.aol_status != '0'))
{
ft_exec(ast->left, data);
ft_exec(&(*ast)->left, data);
data->exec.aol_status = ft_getenv(data->env, "?");
}
data->exec.aol_search = TK_OR_IF;
if (*data->exec.aol_status != '0'
|| ((t_astnode*)ast->right->item)->type != TK_COMMAND)
ft_exec(ast->right, data);
|| ((t_astnode*)(*ast)->right->item)->type != TK_COMMAND)
ft_exec(&(*ast)->right, data);
data->exec.aol_status = NULL;
data->exec.aol_search = 0;
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,28 +6,29 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 21:13:23 by jhalford #+# #+# */
/* Updated: 2016/12/03 15:23:49 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:14:13 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_pipe(t_btree *ast, t_data *data)
int exec_pipe(t_btree **ast, t_data *data)
{
int fds[2];
pipe(fds);
ft_dprintf(2, "pipe %i->%i\n", fds[PIPE_WRITE], fds[PIPE_READ]);
data->exec.fdout = fds[PIPE_WRITE];
ft_exec(ast->left, data);
ft_exec(&(*ast)->left, data);
if (data->exec.fdout != STDOUT)
close(data->exec.fdout);
data->exec.fdout = STDOUT;
data->exec.fdin = fds[PIPE_READ];
ft_exec(ast->right, data);
ft_exec(&(*ast)->right, data);
close(fds[PIPE_WRITE]);
close(fds[PIPE_READ]);
data->exec.fdin = STDIN;
data->exec.fdout = STDOUT;
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,15 +6,16 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 20:52:05 by jhalford #+# #+# */
/* Updated: 2016/11/30 20:52:09 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:14:37 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
int exec_semi(t_btree *ast, t_data *data)
int exec_semi(t_btree **ast, t_data *data)
{
ft_exec(ast->left, data);
ft_exec(ast->right, data);
ft_exec(&(*ast)->left, data);
ft_exec(&(*ast)->right, data);
btree_delone(ast, &ast_free);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */
/* Updated: 2016/12/03 15:25:00 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:18:45 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,14 +25,14 @@ t_execfunc g_execfunc[] =
{0, 0},
};
int ft_exec(t_btree *ast, t_data *data)
int ft_exec(t_btree **ast, t_data *data)
{
t_astnode *item;
int i;
i = 0;
item = ast->item;
if (!ast)
item = (*ast)->item;
if (!*ast)
return (0);
while (g_execfunc[i].type)
{

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:08:55 by jhalford #+# #+# */
/* Updated: 2016/11/30 17:52:00 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:29:50 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:07:30 by jhalford #+# #+# */
/* Updated: 2016/12/03 12:07:30 by jhalford ### ########.fr */
/* Updated: 2016/12/05 11:53:04 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */
/* Updated: 2016/12/03 12:08:14 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:50:03 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,11 +15,17 @@
void token_print(t_list *lst)
{
t_token *token;
int i;
t_type type;
while (lst)
{
i = 1;
token = lst->content;
ft_dprintf(2, "%#010llx: '%s'\n", token->type, token->data);
type = token->type;
while (type >> (i++ + 2))
;
ft_dprintf(2, "%02i '%s'\n", i, token->data);
lst = lst->next;
}
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:14:12 by jhalford #+# #+# */
/* Updated: 2016/12/03 15:37:29 by jhalford ### ########.fr */
/* Updated: 2016/12/03 17:03:55 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,7 @@
t_stof g_keys[] = {
{FT_KEY_C_K, NULL},
{FT_KEY_TAB, NULL},
{FT_KEY_TAB, &ft_key_default},
{FT_KEY_C_H, &ft_line_start},
{FT_KEY_C_L, &ft_line_end},
{FT_KEY_C_U, &ft_clear_line},

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */
/* Updated: 2016/12/01 16:44:24 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:35:35 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 21:13:34 by jhalford #+# #+# */
/* Updated: 2016/12/03 15:33:21 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:11:27 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,6 +19,7 @@ int main(void)
t_btree *ast;
token = NULL;
ast = NULL;
if (data_init(&data))
return (1);
if (signal(SIGINT, sig_handler) == SIG_ERR)
@ -34,14 +35,12 @@ int main(void)
if (!token)
continue ;
token_print(token);
ast = NULL;
if (ft_parse(&ast, &token))
return (1);
btree_print(STDERR, ast, &ft_putast);
/* ft_dprintf(STDERR, "\n--- INFIX BREAKDOWN ---\n"); */
/* btree_apply_infix(ast, &ft_putast2); */
/* ft_lstdel(&token, &token_free); */
if (ft_exec(ast, &data))
if (ft_exec(&ast, &data))
return (1);
}
return (0);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/30 17:14:58 by jhalford #+# #+# */
/* Updated: 2016/12/03 13:50:36 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:33:01 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */
/* Updated: 2016/12/03 13:32:06 by jhalford ### ########.fr */
/* Updated: 2016/12/05 12:12:11 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,10 +28,8 @@ int parse_great(t_btree **ast, t_list **start, t_list **lst)
return (1);
node->data.redir.n = ft_atoi(tok->data);
node->data.redir.word.word = ft_strdup(next_tok->data);
ft_lst_delif(start, (*lst)->content, &ft_addrcmp, &token_free);
ft_lst_delif(start, (*lst)->next->content, &ft_addrcmp, &token_free);
ft_parse(&(*ast)->left, start);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 12:49:45 by jhalford #+# #+# */
/* Updated: 2016/12/01 16:36:44 by jhalford ### ########.fr */
/* Updated: 2016/12/05 11:58:48 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */