diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..f266755 --- /dev/null +++ b/src/common.h @@ -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 */ diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 0000000..0e22e16 --- /dev/null +++ b/src/debug.h @@ -0,0 +1,36 @@ +/* +** EPITECH PROJECT, 2025 +** __ +** File description: +** _ +*/ + +#ifndef DEBUG_H + #define DEBUG_H + #include "vt100_esc_codes.h" + #include + + #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 */ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..96b84b1 --- /dev/null +++ b/src/main.c @@ -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); +} diff --git a/src/shell.c b/src/shell.c new file mode 100644 index 0000000..8f406bc --- /dev/null +++ b/src/shell.c @@ -0,0 +1,54 @@ +/* +** EPITECH PROJECT, 2025 +** __ +** File description: +** _ +*/ + +#include +#include +#include +#include +#include + +#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(); +} diff --git a/src/shell.h b/src/shell.h new file mode 100644 index 0000000..1001830 --- /dev/null +++ b/src/shell.h @@ -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 */ diff --git a/src/vt100_esc_codes.h b/src/vt100_esc_codes.h new file mode 100644 index 0000000..a2860b7 --- /dev/null +++ b/src/vt100_esc_codes.h @@ -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