Add simple shell

This commit is contained in:
savalet
2025-02-10 16:43:03 +01:00
parent c8f7a7df46
commit 7ab3034f65
6 changed files with 160 additions and 0 deletions

14
src/common.h Normal file
View File

@@ -0,0 +1,14 @@
/*
** EPITECH PROJECT, 2025
** __
** File description:
** _
*/
#ifndef COMMON_H
#define COMMON_H
enum {
RETURN_SUCCESS = 0,
RETURN_FAILURE = 84
};
#endif /* COMMON_H */

36
src/debug.h Normal file
View File

@@ -0,0 +1,36 @@
/*
** EPITECH PROJECT, 2025
** __
** File description:
** _
*/
#ifndef DEBUG_H
#define DEBUG_H
#include "vt100_esc_codes.h"
#include <stdio.h>
#define OMIT
#ifdef U_DEBUG_MODE
#define HEAD __FILE_NAME__, __LINE__
#define HEAD_FMT_FILE BOLD BLUE "%s" RESET
#define HEAD_FMT_LINE ":" BOLD PURPLE "%d" RESET
#define HEAD_FMT HEAD_FMT_FILE HEAD_FMT_LINE " "
#define ERR(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
#define DEBUG_INTERNAL(fmt, ...) ERR(HEAD_FMT fmt, HEAD, __VA_ARGS__)
#define U_DEBUG(fmt, ...) DEBUG_INTERNAL(fmt, __VA_ARGS__)
#define U_DEBUG_MSG(msg) DEBUG_INTERNAL("%s\n", msg)
#define U_DEBUG_CALL(func, ...) func(__VA_ARGS__)
#else
#define U_DEBUG_CALL(func, ...) OMIT
#define U_DEBUG_MSG(msg) OMIT
#define U_DEBUG(fmt, ...) OMIT
#endif
#endif /* DEBUG_H */

16
src/main.c Normal file
View File

@@ -0,0 +1,16 @@
/*
** EPITECH PROJECT, 2025
** __
** File description:
** _
*/
#include "debug.h"
#include "shell.h"
int main(int ac __attribute__((unused)), char **av __attribute__((unused)),
char **env)
{
U_DEBUG_MSG("Debug mode activated.");
shell(env);
}

54
src/shell.c Normal file
View File

@@ -0,0 +1,54 @@
/*
** EPITECH PROJECT, 2025
** __
** File description:
** _
*/
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "common.h"
#include "debug.h"
#include "env.h"
#include "shell.h"
#include "u_str.h"
static
int shell_loop(void)
{
int is_a_tty = isatty(STDIN_FILENO);
char *buffer = NULL;
size_t buffer_sz;
signal(SIGINT, SIG_IGN);
while (true) {
if (is_a_tty)
WRITE_CONST(STDOUT_FILENO, SHELL_PROMPT);
if (getline(&buffer, &buffer_sz, stdin) == -1)
break;
U_DEBUG("Buffer [%.*s]\n", u_strlen(buffer) - 1, buffer);
if (!u_str_is_alnum(buffer))
continue;
}
free(buffer);
return RETURN_SUCCESS;
}
int shell(char **env)
{
buff_t env_values = { 0 };
env_entry_t *env_entries = NULL;
size_t env_size = count_env_entries(env);
env_values.str = malloc(ENV_BUFF_CAP);
if (env_values.str == NULL)
return RETURN_FAILURE;
env_entries = malloc(sizeof *env_entries * env_size);
parse_env(env, &env_values, env_entries);
U_DEBUG_CALL(debug_env_entries, env_entries, env_size);
return shell_loop();
}

13
src/shell.h Normal file
View File

@@ -0,0 +1,13 @@
/*
** EPITECH PROJECT, 2025
** __
** File description:
** _
*/
#ifndef SHELL_H
#define SHELL_H
#include "vt100_esc_codes.h"
#define SHELL_PROMPT RED "|> " RESET
int shell(char **env);
#endif /* SHELL_H */

27
src/vt100_esc_codes.h Normal file
View File

@@ -0,0 +1,27 @@
/*
** EPITECH PROJECT, 2025
** __
** File description:
** _
*/
#ifndef CR_VT100_ESC_CODES_H
#define CR_VT100_ESC_CODES_H
#define ESC "\033"
#define CFMT(n) ESC "[" #n "m"
// move
#define MOVE_RIGHT(n) ESC "[" #n "C"
// colors
#define RESET CFMT(0)
#define BOLD CFMT(1)
#define RED CFMT(31)
#define GREEN CFMT(32)
#define YELLOW CFMT(33)
#define BLUE CFMT(34)
#define PURPLE CFMT(35)
#define CYAN CFMT(36)
#endif