add: get_user_input

This commit is contained in:
starnakin 2023-06-28 20:44:13 +02:00
parent 38c27fd583
commit cc8cb37697
4 changed files with 58 additions and 41 deletions

View File

@ -7,7 +7,7 @@ OBJ_DIR = obj
LIB_DIR = lib LIB_DIR = lib
SOURCES = $(shell find $(SRC_DIR) -type f -name '*.c') SOURCES = $(shell find $(SRC_DIR) -type f -name '*.c')
OBJECTS = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SOURCES)) OBJECTS = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SOURCES))
LIBRARIES = lib/bozolib/bozolib.a LIBRARIES = lib/bozolib/bozolib.a -lreadline
DEPS = $(OBJECTS:.o=.d) DEPS = $(OBJECTS:.o=.d)
all: $(NAME) $(LIBRARIES) all: $(NAME) $(LIBRARIES)

54
src/input/input.c Normal file
View File

@ -0,0 +1,54 @@
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <limits.h>
#include <unistd.h>
#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);
}

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "../../lib/bozolib/bozolib.h"
char* get_prompt(lst** env);
#include "../../lib/bozolib/bozolib.h"
char *get_user_input(lst** env);

View File

@ -1,38 +0,0 @@
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#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;
}