redir working
This commit is contained in:
25
srcs/main.c
25
srcs/main.c
@@ -67,6 +67,14 @@ t_msh *init_msh(char **envp)
|
||||
return (msh);
|
||||
}
|
||||
|
||||
void interpret_cmd(char **input, t_msh *msh)
|
||||
{
|
||||
msh->head = parser(*input, msh);
|
||||
msh->ex_code = execute_command(msh->head);
|
||||
free(*input);
|
||||
*input = NULL;
|
||||
}
|
||||
|
||||
int main(int ac, char **av, char **envp)
|
||||
{
|
||||
char *input;
|
||||
@@ -78,13 +86,16 @@ int main(int ac, char **av, char **envp)
|
||||
if (!msh)
|
||||
return (1);
|
||||
input = NULL;
|
||||
while (1)
|
||||
if (ac == 1)
|
||||
while (1)
|
||||
{
|
||||
while (!input || !input[0])
|
||||
input = powerline();
|
||||
interpret_cmd(&input, msh);
|
||||
}
|
||||
else
|
||||
{
|
||||
while (!input || !input[0])
|
||||
input = powerline();
|
||||
msh->head = parser(input, msh);
|
||||
msh->ex_code = execute_command(msh->head);
|
||||
free(input);
|
||||
input = NULL;
|
||||
input = ft_strdup(av[1]);
|
||||
interpret_cmd(&input, msh);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,14 +102,20 @@ void create_redir(t_node *cpy, t_ast_n *self)
|
||||
{
|
||||
t_redir redir;
|
||||
|
||||
while (cpy && cpy->next && get_redir(cpy) == _NR)
|
||||
while (cpy)
|
||||
{
|
||||
while (cpy && get_redir(cpy) == _NR)
|
||||
cpy = cpy->next;
|
||||
if (!cpy)
|
||||
break;
|
||||
redir = get_redir(cpy);
|
||||
add_redir(redir, &self->redir);
|
||||
add_to_tab(&self->files, cpy->next->val);
|
||||
cpy = cpy->next;
|
||||
while (cpy && cpy->next && get_redir(cpy) == _NR)
|
||||
{
|
||||
cpy = cpy->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,14 +138,14 @@ char **get_args(t_node *cmd)
|
||||
t_node *cpy;
|
||||
|
||||
cpy = cmd;
|
||||
while (cpy && !get_redir(cpy))
|
||||
while (cpy && cpy->next && !get_redir(cpy->next))
|
||||
cpy = cpy->next;
|
||||
return (lltotab(cmd, cpy));
|
||||
// create the arg list by skipping the redir and the command but everything else is an arg (ex : ls > test.txt -la)
|
||||
return (lltotab(cmd, cpy->next));
|
||||
}
|
||||
|
||||
void create_cmd(t_ast_n *self, t_node *lst)
|
||||
{
|
||||
t_node *cpy;
|
||||
t_node *cmd_node;
|
||||
|
||||
self->state = _CMD;
|
||||
@@ -149,8 +155,15 @@ void create_cmd(t_ast_n *self, t_node *lst)
|
||||
cmd_node = get_cmd(lst);
|
||||
self->cmd = ft_strdup(cmd_node->val);
|
||||
self->args = get_args(cmd_node);
|
||||
cpy = lst;
|
||||
create_redir(cpy, self);
|
||||
create_redir(lst, self);
|
||||
// debug
|
||||
int i = -1;
|
||||
while (self->files && self->files[++i])
|
||||
ft_debug("redir : [%d]%s\n",self->redir[i], self->files[i]);
|
||||
ft_debug("====\n");
|
||||
i = -1;
|
||||
while (self->args && self->args[++i])
|
||||
ft_debug("args : %s\n",self->args[i], self->args[i]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user