redirection parsing: parse redirections for cmdgroup

This commit is contained in:
Khaïs COLIN 2025-03-19 16:26:45 +01:00
parent c258fa6077
commit 448458b37f
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
5 changed files with 129 additions and 2 deletions

View file

@ -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 \

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdlib.h>
/*
@ -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();
}

View file

@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmdgroup_redirection_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

23
test.sh
View file

@ -102,4 +102,27 @@ expecting <<EOF
╰─ (no redirections)
EOF
when_run <<EOF "redirections are parsed"
echo hello < in hi > out
EOF
expecting <<EOF
╰─ t_cmdgroup
├─ t_cmdlist
│ ├─ num_cmds = 1
│ ╰─ cmd[0]
│ ├─ t_cmdlist_item
│ │ ╰─ t_pipeline
│ │ ├─ num_cmd = 1
│ │ ╰─ cmd[0]
│ │ ╰─ t_simple_cmd
│ │ ├─ words = [echo][hello][hi]
│ │ ╰─ t_redir_list
│ │ ├─ redirection[0]
│ │ │ ╰─ t_redirection
│ │ ╰─ redirection[1]
│ │ ╰─ t_redirection
│ ╰─ t_operator = END
╰─ (no redirections)
EOF
finalize