From 1486c3b12448668f60a2403d8cbc3209c9f78f8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Wed, 19 Mar 2025 12:12:51 +0100 Subject: [PATCH] treeprint refactor: use simpler types --- report.txt | 136 ++++++++++++++++++ src/minishell.c | 4 +- src/parser/cmdgroup/cmdgroup.c | 4 +- src/parser/cmdgroup/cmdgroup.h | 4 +- src/parser/cmdlist/cmdlist.h | 4 +- src/parser/cmdlist/cmdlist_debug.c | 6 +- src/parser/wordlist/wordlist_debug.c | 2 +- .../redirections/redirection_list.c | 4 +- .../redirections/redirection_list.h | 4 +- src/treedrawing.c | 20 +-- src/treedrawing.h | 6 +- 11 files changed, 165 insertions(+), 29 deletions(-) create mode 100644 report.txt diff --git a/report.txt b/report.txt new file mode 100644 index 0000000..f157d5d --- /dev/null +++ b/report.txt @@ -0,0 +1,136 @@ +++++++++++++++++++++++++++++++++++++++++++++++++++++ +redirections are parsed +++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++ Failed test: +echo hello < in hi > out +++++ Got output: + ╰─ 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 + │ │ │ ├─ t_redir_type = REDIR_INPUT + │ │ │ ╰─ marker = [in] + │ │ ╰─ redirection[1] + │ │ ╰─ t_redirection + │ │ ├─ t_redir_type = REDIR_OUTPUT + │ │ ╰─ marker = [out] + │ ╰─ t_operator = END + ╰─ (no redirections) +++++ But expected: + ╰─ 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) +++++ Diff: +(red is got, green is expected) +14,15d13 +< │ │ │ ├─ t_redir_type = REDIR_INPUT +< │ │ │ ╰─ marker = [in] +18,19d15 +< │ │ ├─ t_redir_type = REDIR_OUTPUT +< │ │ ╰─ marker = [out] +++++ Run this to debug: +echo 'echo hello < in hi > out' | ./minishell +++++++++++++++++++++++++++++++++++++++++++++++++++++ +multiple redirections are parsed +++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++ Failed test: +echo << HERE_DOC hello>outfile < infile < infile2 >> append +++++ Got output: + ╰─ 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] + │ │ ╰─ t_redir_list + │ │ ├─ redirection[0] + │ │ │ ╰─ t_redirection + │ │ │ ├─ t_redir_type = REDIR_HERE_DOC + │ │ │ ╰─ marker = [HERE_DOC] + │ │ ├─ redirection[1] + │ │ │ ╰─ t_redirection + │ │ │ ├─ t_redir_type = REDIR_OUTPUT + │ │ │ ╰─ marker = [outfile] + │ │ ├─ redirection[2] + │ │ │ ╰─ t_redirection + │ │ │ ├─ t_redir_type = REDIR_INPUT + │ │ │ ╰─ marker = [infile] + │ │ ├─ redirection[3] + │ │ │ ╰─ t_redirection + │ │ │ ├─ t_redir_type = REDIR_INPUT + │ │ │ ╰─ marker = [infile2] + │ │ ╰─ redirection[4] + │ │ ╰─ t_redirection + │ │ ├─ t_redir_type = REDIR_APPEND + │ │ ╰─ marker = [append] + │ ╰─ t_operator = END + ╰─ (no redirections) +++++ But expected: + +++++ Diff: +(red is got, green is expected) +1,33c1 +< ╰─ 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] +< │ │ ╰─ t_redir_list +< │ │ ├─ redirection[0] +< │ │ │ ╰─ t_redirection +< │ │ │ ├─ t_redir_type = REDIR_HERE_DOC +< │ │ │ ╰─ marker = [HERE_DOC] +< │ │ ├─ redirection[1] +< │ │ │ ╰─ t_redirection +< │ │ │ ├─ t_redir_type = REDIR_OUTPUT +< │ │ │ ╰─ marker = [outfile] +< │ │ ├─ redirection[2] +< │ │ │ ╰─ t_redirection +< │ │ │ ├─ t_redir_type = REDIR_INPUT +< │ │ │ ╰─ marker = [infile] +< │ │ ├─ redirection[3] +< │ │ │ ╰─ t_redirection +< │ │ │ ├─ t_redir_type = REDIR_INPUT +< │ │ │ ╰─ marker = [infile2] +< │ │ ╰─ redirection[4] +< │ │ ╰─ t_redirection +< │ │ ├─ t_redir_type = REDIR_APPEND +< │ │ ╰─ marker = [append] +< │ ╰─ t_operator = END +< ╰─ (no redirections) +--- +>  +++++ Run this to debug: +echo 'echo << HERE_DOC hello>outfile < infile < infile2 >> append' | ./minishell diff --git a/src/minishell.c b/src/minishell.c index 3682c0c..d927087 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/18 12:33:21 by khais ### ########.fr */ +/* Updated: 2025/03/19 12:12:39 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ int main(int argc, char *argv[], char **envp) cmd = cmdgroup_from_wordlist(words); wordlist_destroy(words); indent = ft_buffer_new(); - cmdgroup_debug(cmd, &indent, true); + cmdgroup_debug(cmd, indent, true); ft_buffer_free(indent); cmdgroup_destroy(cmd); line = get_command(); diff --git a/src/parser/cmdgroup/cmdgroup.c b/src/parser/cmdgroup/cmdgroup.c index 64f96fa..e70370b 100644 --- a/src/parser/cmdgroup/cmdgroup.c +++ b/src/parser/cmdgroup/cmdgroup.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/11 15:18:02 by khais #+# #+# */ -/* Updated: 2025/03/19 11:26:34 by khais ### ########.fr */ +/* Updated: 2025/03/19 12:10:19 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,7 +38,7 @@ void cmdgroup_destroy(t_cmdgroup *cmd) free(cmd); } -void cmdgroup_debug(t_cmdgroup *cmd, t_buffer **leader, bool is_last) +void cmdgroup_debug(t_cmdgroup *cmd, t_buffer *leader, bool is_last) { if (cmd == NULL) return ; diff --git a/src/parser/cmdgroup/cmdgroup.h b/src/parser/cmdgroup/cmdgroup.h index aedb177..8fad0ea 100644 --- a/src/parser/cmdgroup/cmdgroup.h +++ b/src/parser/cmdgroup/cmdgroup.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/11 15:11:57 by khais #+# #+# */ -/* Updated: 2025/03/19 11:26:22 by khais ### ########.fr */ +/* Updated: 2025/03/19 12:10:37 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,6 @@ typedef struct s_cmdgroup t_cmdgroup *cmdgroup_from_wordlist(t_wordlist *words); void cmdgroup_destroy(t_cmdgroup *cmd); -void cmdgroup_debug(t_cmdgroup *cmd, t_buffer **indent, bool is_last); +void cmdgroup_debug(t_cmdgroup *cmd, t_buffer *leader, bool is_last); #endif // CMDGROUP_H diff --git a/src/parser/cmdlist/cmdlist.h b/src/parser/cmdlist/cmdlist.h index c4c1f9e..dd5c05c 100644 --- a/src/parser/cmdlist/cmdlist.h +++ b/src/parser/cmdlist/cmdlist.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/24 17:45:01 by khais #+# #+# */ -/* Updated: 2025/03/18 15:10:28 by khais ### ########.fr */ +/* Updated: 2025/03/19 12:12:12 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -86,6 +86,6 @@ typedef struct s_cmdlist t_cmdlist *cmdlist_from_wordlist(const t_wordlist *wordlist); void cmdlist_destroy(t_cmdlist *cmd); -void cmdlist_debug(t_cmdlist *cmd, t_buffer **indent, bool is_last); +void cmdlist_debug(t_cmdlist *cmd, t_buffer *leader, bool is_last); #endif // CMDLIST_H diff --git a/src/parser/cmdlist/cmdlist_debug.c b/src/parser/cmdlist/cmdlist_debug.c index 8c7e185..d2eaf11 100644 --- a/src/parser/cmdlist/cmdlist_debug.c +++ b/src/parser/cmdlist/cmdlist_debug.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/18 15:52:28 by khais #+# #+# */ -/* Updated: 2025/03/18 15:52:45 by khais ### ########.fr */ +/* Updated: 2025/03/19 12:12:20 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ #include "libft.h" static void cmdlist_array_debug(t_cmdlist *cmd, - int i, t_buffer **leader, bool is_last) + int i, t_buffer *leader, bool is_last) { indent(leader, is_last); ft_printf("cmd[%d]\n", i); @@ -25,7 +25,7 @@ static void cmdlist_array_debug(t_cmdlist *cmd, dedent(leader, is_last); } -void cmdlist_debug(t_cmdlist *cmd, t_buffer **leader, bool is_last) +void cmdlist_debug(t_cmdlist *cmd, t_buffer *leader, bool is_last) { int i; bool last; diff --git a/src/parser/wordlist/wordlist_debug.c b/src/parser/wordlist/wordlist_debug.c index 6cbe7fc..7bce06e 100644 --- a/src/parser/wordlist/wordlist_debug.c +++ b/src/parser/wordlist/wordlist_debug.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/24 18:20:00 by khais #+# #+# */ -/* Updated: 2025/02/24 18:20:09 by khais ### ########.fr */ +/* Updated: 2025/03/19 12:09:56 by khais ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/postprocess/redirections/redirection_list.c b/src/postprocess/redirections/redirection_list.c index 56f4538..19d3570 100644 --- a/src/postprocess/redirections/redirection_list.c +++ b/src/postprocess/redirections/redirection_list.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/07 14:29:53 by khais #+# #+# */ -/* Updated: 2025/03/18 15:33:10 by khais ### ########.fr */ +/* Updated: 2025/03/19 12:10:48 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -67,7 +67,7 @@ t_redir_list *redir_list_push(t_redir_list *lst, t_redirection *item) return (start); } -void redir_list_debug(t_redir_list *lst, t_buffer **leader, bool is_last) +void redir_list_debug(t_redir_list *lst, t_buffer *leader, bool is_last) { indent(leader, is_last); ft_printf("%s\n", "t_redir_list"); diff --git a/src/postprocess/redirections/redirection_list.h b/src/postprocess/redirections/redirection_list.h index 1c4b9bf..a445b60 100644 --- a/src/postprocess/redirections/redirection_list.h +++ b/src/postprocess/redirections/redirection_list.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/07 14:29:32 by khais #+# #+# */ -/* Updated: 2025/03/18 15:34:17 by khais ### ########.fr */ +/* Updated: 2025/03/19 12:10:54 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,6 @@ typedef struct s_redirection_list t_redir_list *redir_list_push(t_redir_list *lst, t_redirection *item); void redir_list_debug(t_redir_list *lst, - t_buffer **indent, bool is_last); + t_buffer *leader, bool is_last); #endif // REDIRECTION_LIST_H diff --git a/src/treedrawing.c b/src/treedrawing.c index 2978eeb..957b1c2 100644 --- a/src/treedrawing.c +++ b/src/treedrawing.c @@ -6,41 +6,41 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/18 15:24:50 by khais #+# #+# */ -/* Updated: 2025/03/18 15:33:39 by khais ### ########.fr */ +/* Updated: 2025/03/19 12:09:47 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "treedrawing.h" #include "libft.h" -void indent(t_buffer **leader, bool is_last) +void indent(t_buffer *leader, bool is_last) { - ft_printf("%s", (*leader)->buffer); + ft_printf("%s", leader->buffer); if (is_last) { ft_printf("%s", CORNER); - *leader = ft_buffer_push_buf(*leader, SPACE, ft_strlen(SPACE)); + ft_buffer_push_buf(leader, SPACE, ft_strlen(SPACE)); } else { ft_printf("%s", CROSS); - *leader = ft_buffer_push_buf(*leader, VERTICAL, ft_strlen(VERTICAL)); + ft_buffer_push_buf(leader, VERTICAL, ft_strlen(VERTICAL)); } } -void dedent(t_buffer **leader, bool is_last) +void dedent(t_buffer *leader, bool is_last) { size_t length_to_remove; - size_t idx; + ssize_t idx; if (is_last) length_to_remove = ft_strlen(SPACE); else length_to_remove = ft_strlen(VERTICAL); - idx = (*leader)->length - length_to_remove; + idx = leader->length - length_to_remove; if (idx >= 0) { - (*leader)->buffer[idx] = '\0'; - (*leader)->length = idx; + leader->buffer[idx] = '\0'; + leader->length = idx; } } diff --git a/src/treedrawing.h b/src/treedrawing.h index 44abee2..34b2c69 100644 --- a/src/treedrawing.h +++ b/src/treedrawing.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/18 12:24:25 by khais #+# #+# */ -/* Updated: 2025/03/18 15:46:36 by khais ### ########.fr */ +/* Updated: 2025/03/19 12:10:05 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ # define VERTICAL " │ " # define SPACE " " -void indent(t_buffer **indent, bool is_last); -void dedent(t_buffer **indent, bool is_last); +void indent(t_buffer *indent, bool is_last); +void dedent(t_buffer *indent, bool is_last); #endif // TREEDRAWING_H