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.
This commit is contained in:
Khaïs COLIN 2025-04-09 13:45:56 +02:00
parent 18014cda98
commit ac411d36cc
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
4 changed files with 40 additions and 18 deletions

View file

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

View file

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

View file

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

View file

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