2025-02-06 13:44:55 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* minishell.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
|
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
/* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */
|
2025-02-06 14:24:17 +01:00
|
|
|
/* Updated: 2025/02/06 14:25:02 by kcolin ### ########.fr */
|
2025-02-06 13:44:55 +01:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
2025-02-06 14:21:01 +01:00
|
|
|
#include <readline/history.h>
|
2025-02-06 13:58:50 +01:00
|
|
|
#include <readline/readline.h>
|
2025-02-06 14:21:01 +01:00
|
|
|
#include <stdbool.h>
|
2025-02-06 13:58:50 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2025-02-06 14:24:17 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** get the prompt to show to the user when getting a command
|
|
|
|
|
**
|
|
|
|
|
** if the terminal is a tty, the prompt is '$ ', else it is ''
|
|
|
|
|
*/
|
|
|
|
|
static const char *get_prompt(void)
|
|
|
|
|
{
|
|
|
|
|
if (isatty(STDIN_FILENO))
|
|
|
|
|
return ("$ ");
|
|
|
|
|
return ("");
|
|
|
|
|
}
|
2025-02-06 13:58:50 +01:00
|
|
|
|
2025-02-06 14:21:01 +01:00
|
|
|
/*
|
|
|
|
|
** 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;
|
|
|
|
|
|
2025-02-06 14:24:17 +01:00
|
|
|
line = readline(get_prompt());
|
2025-02-06 14:21:01 +01:00
|
|
|
if (line != NULL && line[0] != '\0')
|
|
|
|
|
add_history(line);
|
|
|
|
|
return (line);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-06 13:44:55 +01:00
|
|
|
int main(int argc, char *argv[], char **envp)
|
|
|
|
|
{
|
2025-02-06 13:58:50 +01:00
|
|
|
char *line;
|
|
|
|
|
|
2025-02-06 13:44:55 +01:00
|
|
|
(void)argc;
|
|
|
|
|
(void)argv;
|
|
|
|
|
(void)envp;
|
2025-02-06 14:21:01 +01:00
|
|
|
line = get_command();
|
2025-02-06 13:58:50 +01:00
|
|
|
while (line != NULL)
|
|
|
|
|
{
|
2025-02-06 14:21:01 +01:00
|
|
|
printf("%s\n", line); // FIXME
|
2025-02-06 13:58:50 +01:00
|
|
|
free(line);
|
2025-02-06 14:21:01 +01:00
|
|
|
line = get_command();
|
2025-02-06 13:58:50 +01:00
|
|
|
}
|
2025-02-06 13:44:55 +01:00
|
|
|
return (0);
|
|
|
|
|
}
|