From 8bfdb04630baf910ada5a4d9b5e827ba4aa44809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Thu, 6 Feb 2025 14:21:01 +0100 Subject: [PATCH] read: add commands to history --- src/minishell.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/minishell.c b/src/minishell.c index f81240a..5941a44 100644 --- a/src/minishell.c +++ b/src/minishell.c @@ -6,15 +6,32 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */ -/* Updated: 2025/02/06 14:11:37 by kcolin ### ########.fr */ +/* Updated: 2025/02/06 14:19:04 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ -#include +#include #include +#include #include #include +/* +** get a command line using readline. +** +** returned buffer must be freed by caller. +** will add command to history if appropriate. +*/ +static char *get_command(void) +{ + char *line; + + line = readline("$ "); + if (line != NULL && line[0] != '\0') + add_history(line); + return (line); +} + int main(int argc, char *argv[], char **envp) { char *line; @@ -22,12 +39,12 @@ int main(int argc, char *argv[], char **envp) (void)argc; (void)argv; (void)envp; - line = readline("$ "); + line = get_command(); while (line != NULL) { - printf("%s", line); // FIXME + printf("%s\n", line); // FIXME free(line); - line = readline("$ "); + line = get_command(); } return (0); }