diff --git a/Makefile b/Makefile index 37a2ed1..bafae2c 100644 --- a/Makefile +++ b/Makefile @@ -61,6 +61,7 @@ srcs = \ src/parser/wordsplit/tokenizing_6_10.c \ src/parser/wordsplit/wordsplit.c \ src/parser/wordsplit/wordsplit_utils.c \ + src/postprocess/redirections/cmdgroup_redirection_parsing.c \ src/postprocess/redirections/redirection.c \ src/postprocess/redirections/redirection_list.c \ src/postprocess/redirections/redirection_parsing.c \ diff --git a/src/minishell.c b/src/minishell.c index 1cd21a6..6742ca9 100644 --- a/src/minishell.c +++ b/src/minishell.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */ -/* Updated: 2025/03/19 16:30:32 by khais ### ########.fr */ +/* Updated: 2025/03/19 16:38:42 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #include "parser/cmdgroup/cmdgroup.h" #include "parser/wordlist/wordlist.h" #include "parser/wordsplit/wordsplit.h" +#include "postprocess/redirections/redirection_parsing.h" #include /* @@ -34,6 +35,9 @@ static t_cmdgroup *parse_command(char *line) return (cmd); } +/* +** debug-print a cmdgroup +*/ static void debug_command(t_cmdgroup *cmd) { t_buffer *indent; @@ -44,6 +48,19 @@ static void debug_command(t_cmdgroup *cmd) cmdgroup_destroy(cmd); } +/* +** Do all the post-processing steps relating to a command. +** +** Currently, this is the following: +** 1. redirection parsing +*/ +static t_cmdgroup *post_process_command(t_cmdgroup *cmd) +{ + if (cmdgroup_parse_redirections(cmd) == NULL) + return (cmdgroup_destroy(cmd), NULL); + return (cmd); +} + int main(int argc, char *argv[], char **envp) { char *line; @@ -56,6 +73,7 @@ int main(int argc, char *argv[], char **envp) while (line != NULL) { cmd = parse_command(line); + cmd = post_process_command(cmd); debug_command(cmd); line = get_command(); } diff --git a/src/postprocess/redirections/cmdgroup_redirection_parsing.c b/src/postprocess/redirections/cmdgroup_redirection_parsing.c new file mode 100644 index 0000000..22e0dd9 --- /dev/null +++ b/src/postprocess/redirections/cmdgroup_redirection_parsing.c @@ -0,0 +1,83 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cmdgroup_redirection_parsing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/19 16:37:09 by khais #+# #+# */ +/* Updated: 2025/03/19 18:40:04 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "redirection_parsing.h" +#include "../../parser/cmdlist/cmdlist_item.h" + +static t_pipeline *pipeline_parse_redirections(t_pipeline *pipeline) +{ + int i; + + if (pipeline == NULL) + return (NULL); + i = 0; + while (i < pipeline->num_cmd) + { + if (parse_redirections(pipeline->cmds[i]) == NULL) + return (NULL); + i++; + } + return (pipeline); +} + +static t_cmdlist_item *cmdlist_item_parse_redirections(t_cmdlist_item *item) +{ + if (item == NULL) + return (NULL); + if (item->type == TYPE_PIPELINE) + { + if (pipeline_parse_redirections(item->inner.pipeline) == NULL) + return (NULL); + } + if (item->type == TYPE_CMDGROUP) + { + if (cmdgroup_parse_redirections(item->inner.cmdgroup) == NULL) + return (NULL); + } + return (item); +} + +/* +** Iterate over all simple_cmd contained withing this cmdlist, and do +** redirection parsing on them. +*/ +static t_cmdlist *cmdlist_parse_redirections(t_cmdlist *cmd) +{ + int i; + + if (cmd == NULL) + return (NULL); + i = 0; + while (i < cmd->num_cmds) + { + if (cmdlist_item_parse_redirections(cmd->cmds[i]) == NULL) + return (NULL); + i++; + } + return (cmd); +} + +/* +** Iterate over all simple_cmd contained within this cmdgroup, and do +** redirection parsing on them. +** +** This DOES NOT parse redirections that are applied to a whole cmdgroup. This +** MUST be done by cmdgroup_from_wordlist. +*/ +t_cmdgroup *cmdgroup_parse_redirections(t_cmdgroup *cmd) +{ + if (cmd == NULL) + return (NULL); + if (cmdlist_parse_redirections(cmd->item) == NULL) + return (NULL); + return (cmd); +} diff --git a/src/postprocess/redirections/redirection_parsing.h b/src/postprocess/redirections/redirection_parsing.h index 75bcc66..b36cd08 100644 --- a/src/postprocess/redirections/redirection_parsing.h +++ b/src/postprocess/redirections/redirection_parsing.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/07 11:59:31 by khais #+# #+# */ -/* Updated: 2025/03/07 14:38:30 by khais ### ########.fr */ +/* Updated: 2025/03/19 16:38:01 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,9 @@ # define REDIRECTION_PARSING_H # include "../../parser/simple_cmd/simple_cmd.h" +# include "../../parser/cmdgroup/cmdgroup.h" t_simple_cmd *parse_redirections(t_simple_cmd *cmd); +t_cmdgroup *cmdgroup_parse_redirections(t_cmdgroup *cmd); #endif // REDIRECTIONS_H diff --git a/test.sh b/test.sh index fa6252d..9957795 100755 --- a/test.sh +++ b/test.sh @@ -102,4 +102,27 @@ expecting < out +EOF +expecting <