redir working

This commit is contained in:
gazhonsepaskwa
2025-02-04 17:29:45 +01:00
parent a9006a671c
commit 272dc2760a
3 changed files with 39 additions and 14 deletions

View File

@@ -60,6 +60,7 @@ void add_to_tab(char ***tab, char *str)
{ {
*tab = malloc(sizeof(char *) * 2); *tab = malloc(sizeof(char *) * 2);
} }
if (tmp)
free(tmp); free(tmp);
(*tab)[i] = ft_strdup(str); (*tab)[i] = ft_strdup(str);
(*tab)[i + 1] = NULL; (*tab)[i + 1] = NULL;

View File

@@ -67,6 +67,14 @@ t_msh *init_msh(char **envp)
return (msh); 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) int main(int ac, char **av, char **envp)
{ {
char *input; char *input;
@@ -78,13 +86,16 @@ int main(int ac, char **av, char **envp)
if (!msh) if (!msh)
return (1); return (1);
input = NULL; input = NULL;
if (ac == 1)
while (1) while (1)
{ {
while (!input || !input[0]) while (!input || !input[0])
input = powerline(); input = powerline();
msh->head = parser(input, msh); interpret_cmd(&input, msh);
msh->ex_code = execute_command(msh->head); }
free(input); else
input = NULL; {
input = ft_strdup(av[1]);
interpret_cmd(&input, msh);
} }
} }

View File

@@ -102,16 +102,22 @@ void create_redir(t_node *cpy, t_ast_n *self)
{ {
t_redir redir; 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); redir = get_redir(cpy);
add_redir(redir, &self->redir); add_redir(redir, &self->redir);
add_to_tab(&self->files, cpy->next->val); add_to_tab(&self->files, cpy->next->val);
cpy = cpy->next; cpy = cpy->next;
while (cpy && cpy->next && get_redir(cpy) == _NR) while (cpy && cpy->next && get_redir(cpy) == _NR)
{
cpy = cpy->next; cpy = cpy->next;
} }
} }
}
t_node *get_cmd(t_node *lst) t_node *get_cmd(t_node *lst)
{ {
@@ -132,14 +138,14 @@ char **get_args(t_node *cmd)
t_node *cpy; t_node *cpy;
cpy = cmd; cpy = cmd;
while (cpy && !get_redir(cpy)) while (cpy && cpy->next && !get_redir(cpy->next))
cpy = 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) void create_cmd(t_ast_n *self, t_node *lst)
{ {
t_node *cpy;
t_node *cmd_node; t_node *cmd_node;
self->state = _CMD; self->state = _CMD;
@@ -149,8 +155,15 @@ void create_cmd(t_ast_n *self, t_node *lst)
cmd_node = get_cmd(lst); cmd_node = get_cmd(lst);
self->cmd = ft_strdup(cmd_node->val); self->cmd = ft_strdup(cmd_node->val);
self->args = get_args(cmd_node); self->args = get_args(cmd_node);
cpy = lst; create_redir(lst, self);
create_redir(cpy, 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]);
} }