From cc8cb3769769b4a6e269dc396170686ef284706f Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 28 Jun 2023 20:44:13 +0200 Subject: [PATCH] add: get_user_input --- Makefile | 2 +- src/input/input.c | 54 ++++++++++++++++++++++++++ src/{prompt/prompt.h => input/input.h} | 5 ++- src/prompt/prompt.c | 38 ------------------ 4 files changed, 58 insertions(+), 41 deletions(-) create mode 100644 src/input/input.c rename src/{prompt/prompt.h => input/input.h} (60%) delete mode 100644 src/prompt/prompt.c diff --git a/Makefile b/Makefile index b947093..fce1490 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ OBJ_DIR = obj LIB_DIR = lib SOURCES = $(shell find $(SRC_DIR) -type f -name '*.c') OBJECTS = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SOURCES)) -LIBRARIES = lib/bozolib/bozolib.a +LIBRARIES = lib/bozolib/bozolib.a -lreadline DEPS = $(OBJECTS:.o=.d) all: $(NAME) $(LIBRARIES) diff --git a/src/input/input.c b/src/input/input.c new file mode 100644 index 0000000..ead8b10 --- /dev/null +++ b/src/input/input.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include +#include "../../lib/bozolib/bozolib.h" +#include "../env/env.h" + +static char* get_prompt(lst** env) +{ + char* out; + int size; + char cwd[PATH_MAX]; + if (getcwd(cwd, sizeof(cwd)) == NULL) + { + dprintf(2, "chemin inconnu"); + return NULL; + } + char* computer = get_env_variable(env, "COMPUTER"); + char* user = get_env_variable(env, "USER"); + char* home = get_env_variable(env, "HOME"); + if (computer == NULL) + computer = ""; + if (user == NULL) + user = ""; + else if (strncmp(home, cwd, strlen(home)) == 0) + { + strcpy(cwd, "~"); + strcat(cwd, cwd + strlen(home)); + } + size = snprintf(NULL, 0, "%s@%s>%s $ ",user, computer, cwd) + 10; + out = malloc(size*sizeof(char)); + if(out == NULL) + return NULL; + sprintf(out, "%s@%s > %s $ ",user, computer, cwd); + return out; +} + +char *get_user_input(lst** env) +{ + char *prompt; + char *input; + + prompt = get_prompt(env); + if (prompt == NULL) + return NULL; + input = readline(prompt); + if (input != NULL && str_contain_only(input, "\t ") == 0) + add_history(input); + free(prompt); + if (input == NULL) + printf("exit"); + return (input); +} diff --git a/src/prompt/prompt.h b/src/input/input.h similarity index 60% rename from src/prompt/prompt.h rename to src/input/input.h index 559c65e..d12d998 100644 --- a/src/prompt/prompt.h +++ b/src/input/input.h @@ -1,4 +1,5 @@ #pragma once -#include "../../lib/bozolib/bozolib.h" -char* get_prompt(lst** env); +#include "../../lib/bozolib/bozolib.h" + +char *get_user_input(lst** env); diff --git a/src/prompt/prompt.c b/src/prompt/prompt.c deleted file mode 100644 index 8c74edf..0000000 --- a/src/prompt/prompt.c +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include -#include "../../lib/bozolib/bozolib.h" -#include "../env/env.h" - -char* get_prompt(lst** env) -{ - char* out; - int size; - char cwd[PATH_MAX]; - if (getcwd(cwd, sizeof(cwd)) == NULL) - { - dprintf(2, "chemin inconnu"); - return NULL; - } - char* computer = get_env_variable(env, "COMPUTER"); - char* user = get_env_variable(env, "USER"); - char* home = get_env_variable(env, "HOME"); - if (computer == NULL) - computer = ""; - if (user == NULL) - user = ""; - else if (strncmp(home, cwd, strlen(home)) == 0) - { - strcpy(cwd, "~"); - strcat(cwd, cwd + strlen(home)); - } - size = snprintf(NULL, 0, "%s@%s>%s $",user, computer, cwd); - out = malloc(size*sizeof(char)); - if(out == NULL) - { - return NULL; - } - sprintf(out, "%s@%s > %s $",user, computer, cwd); - - return out; -}