treeprint refactor: use simpler types

This commit is contained in:
Khaïs COLIN 2025-03-19 12:12:51 +01:00
parent 3edff91107
commit 1486c3b124
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
11 changed files with 165 additions and 29 deletions

136
report.txt Normal file
View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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