From ac411d36cc4f8b1ba35f4bb89a73a309d794ce11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Wed, 9 Apr 2025 13:45:56 +0200 Subject: [PATCH] debug: special command can turn debug mode on and off Using the .debug command, debug mode can be toggled. It starts turned off. When it is on, each command, before being executed, is debug-printed using the tree-printing facilities. --- src/get_command.c | 22 +++++++++++++++++++--- src/get_command.h | 6 ++++-- src/minishell.c | 25 ++++++++++++++----------- src/minishell.h | 5 +++-- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/get_command.c b/src/get_command.c index a6aea96..a3a30ab 100644 --- a/src/get_command.c +++ b/src/get_command.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/19 18:03:11 by khais #+# #+# */ -/* Updated: 2025/02/19 18:04:06 by khais ### ########.fr */ +/* Updated: 2025/04/09 13:59:20 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ #include #include #include "libft.h" +#include "get_command.h" /* ** remove one '\n' from the end of the string, if it exists @@ -34,13 +35,28 @@ static char *strip_newline(char *str) return (str); } +static char *handle_special_command(char *line, t_minishell *app) +{ + if (line == NULL) + return (NULL); + if (ft_strcmp(".debug", line) == 0) + { + app->debug = !app->debug; + ft_printf("[dbg: %d]\n", (int)app->debug); + line[0] = '\0'; + } + return (line); +} + /* ** get a command line using readline. ** ** returned buffer must be freed by caller. ** will add command to history if appropriate. +** +** Also handles special commands, which are not further processed. */ -char *get_command(void) +char *get_command(t_minishell *app) { char *line; @@ -50,5 +66,5 @@ char *get_command(void) line = strip_newline(get_next_line(STDIN_FILENO)); if (line != NULL && line[0] != '\0') add_history(line); - return (line); + return (handle_special_command(line, app)); } diff --git a/src/get_command.h b/src/get_command.h index 0772ecd..25d45ad 100644 --- a/src/get_command.h +++ b/src/get_command.h @@ -6,13 +6,15 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/19 18:02:19 by khais #+# #+# */ -/* Updated: 2025/02/19 18:03:06 by khais ### ########.fr */ +/* Updated: 2025/04/09 13:51:41 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef GET_COMMAND_H # define GET_COMMAND_H -char *get_command(void); +# include "minishell.h" + +char *get_command(t_minishell *app); #endif diff --git a/src/minishell.c b/src/minishell.c index b706ffb..52e6da4 100644 --- a/src/minishell.c +++ b/src/minishell.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */ -/* Updated: 2025/04/15 11:56:50 by khais ### ########.fr */ +/* Updated: 2025/04/15 14:36:03 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,6 +39,17 @@ static void execute_command(t_simple_cmd *cmd, t_minishell *app) set_interactive_mode_sig_handling(); } +static void debug_command(t_cmd *cmd, t_minishell *app) +{ + t_buffer *indent; + + if (app->debug == false) + return ; + indent = ft_buffer_new(); + cmd_debug(cmd, indent, true); + ft_buffer_free(indent); +} + /* ** Do all the post-processing steps relating to a command. */ @@ -62,15 +73,6 @@ static void app_init(t_minishell *app, char **envp) app->env = env_from_envp(envp); } -static void debug_command(t_cmd *cmd) -{ - t_buffer *indent; - - indent = ft_buffer_new(); - cmd_debug(cmd, indent, true); - ft_buffer_free(indent); -} - int main(int argc, char *argv[], char **envp) { char *line; @@ -81,9 +83,10 @@ int main(int argc, char *argv[], char **envp) (void)argv; set_interactive_mode_sig_handling(); app_init(&app, envp); + app.debug = true; line = "echo coucou"; cmd = minishell_parse(&app, line); - debug_command(cmd); + debug_command(cmd, &app); cmd_destroy(cmd); env_destroy(app.env); return (0); diff --git a/src/minishell.h b/src/minishell.h index 68ef6e3..cea5010 100644 --- a/src/minishell.h +++ b/src/minishell.h @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/14 15:00/22 by khais #+# #+# */ -/* Updated: 2025/04/14 15:00:22 by khais ### ########.fr */ +/* Created: 2025/04/09 13:49:28 by khais #+# #+# */ +/* Updated: 2025/04/15 12:01:54 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -118,6 +118,7 @@ typedef struct s_minishell t_env *env; int lines_read; int last_return_value; + bool debug; } t_minishell; t_redirect *t_redirect_add_back(t_redirect **init, t_redirect *back);