From f1da67974f0da2a220b0fca4a75c9dd3305de964 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 26 Apr 2024 14:03:25 +0200 Subject: [PATCH] Implement exitable skeleton of a shell The minishell is now able to accept input and access history through the GNU Readline library. The minishell itself prints prompt with currect directory. It also handles the "exit" command as well as ctrl-D so that it can exit. It prints "exit" on exit like bash. --- Makefile | 5 ++++- src/main.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 7204d11..9166aa6 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,9 @@ CFLAGS = -std=c99 -Wall -Wextra -Werror -Wpedantic RM := rm -f +EXT_LIBS := readline +LINKS := $(addprefix -l, $(EXT_LIBS)) + SUBPROJECTS := Libft INCDIR := inc @@ -25,7 +28,7 @@ debug : CFLAGS += -g debug : $(NAME) $(NAME) : $(OBJECTS) Libft/libft.a - $(CC) $(CFLAGS) -o $@ $^ + $(CC) $(CFLAGS) -o $@ $^ $(LINKS) Libft/libft.a : | Libft/Makefile ifneq (,$(findstring debug, $(MAKECMDGOALS))) diff --git a/src/main.c b/src/main.c index c4af877..59d0bf7 100644 --- a/src/main.c +++ b/src/main.c @@ -6,11 +6,69 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/26 13:11:47 by ljiriste #+# #+# */ -/* Updated: 2024/04/26 13:12:09 by ljiriste ### ########.fr */ +/* Updated: 2024/04/26 14:13:15 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ +#include "libft.h" +#include +#include // getcwd +#include // readline +#include // readline +#include // readline + +// A string is "almost_empty" if it: +// is a NULL pointer +// is an empty string +// only contains whitespace +int almost_empty(const char *str) +{ + if (!str) + return (1); + while (*str) + { + if (!ft_isspace(*str)) + return (0); + ++str; + } + return (1); +} + +char *rl_get_line(void) +{ + char *cwd; + char *prompt; + char *line; + + cwd = getcwd(NULL, 0); + prompt = ft_strjoin(cwd, "$ "); + free(cwd); + line = readline(prompt); + free(prompt); + if (!almost_empty(line)) + add_history(line); + return (line); +} + +void handle_input(__attribute__((unused)) const char *input) +{ + return ; +} + int main(void) { - return (0); + char *line; + + while (1) + { + line = rl_get_line(); + if (!line || !ft_strcmp(line, "exit")) + { + rl_clear_history(); + ft_printf("exit\n"); + return (0); + } + handle_input(line); + free(line); + } } -- 2.30.2