read: add commands to history

This commit is contained in:
Khaïs COLIN 2025-02-06 14:21:01 +01:00
parent d613644edd
commit 8bfdb04630
No known key found for this signature in database

View file

@ -6,15 +6,32 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/06 13:44:06 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 <stdbool.h> #include <readline/history.h>
#include <readline/readline.h> #include <readline/readline.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
/*
** 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) int main(int argc, char *argv[], char **envp)
{ {
char *line; char *line;
@ -22,12 +39,12 @@ int main(int argc, char *argv[], char **envp)
(void)argc; (void)argc;
(void)argv; (void)argv;
(void)envp; (void)envp;
line = readline("$ "); line = get_command();
while (line != NULL) while (line != NULL)
{ {
printf("%s", line); // FIXME printf("%s\n", line); // FIXME
free(line); free(line);
line = readline("$ "); line = get_command();
} }
return (0); return (0);
} }