From a8b4e5d6df529c79faab9bdfb47196494df8e801 Mon Sep 17 00:00:00 2001 From: Jean HERAIL Date: Tue, 27 Jan 2026 21:10:24 +0100 Subject: [PATCH] fix(execution): reimplemented the builtins --- check_flemme.sh | 2 +- src/execution/execution.c | 25 --------- src/execution/execution_helpers.c | 92 ++++++++++++++++++++++++++++++- 3 files changed, 91 insertions(+), 28 deletions(-) diff --git a/check_flemme.sh b/check_flemme.sh index 0543001..12398ae 100755 --- a/check_flemme.sh +++ b/check_flemme.sh @@ -32,7 +32,7 @@ run_cmd "Running autoreconf" autoreconf --force --verbose --install if [[ "$(uname)" == "Darwin" ]]; then run_cmd "Configuring for MacOS" ./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -I/opt/homebrew/include' LDFLAGS='-L/opt/homebrew/lib' else - run_cmd "Configuring for Linux" ./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -g -fsanitize=address' + run_cmd "Configuring for Linux" ./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -g' fi run_cmd "Cleaning build" make clean diff --git a/src/execution/execution.c b/src/execution/execution.c index 82fcacf..90bb8eb 100644 --- a/src/execution/execution.c +++ b/src/execution/execution.c @@ -14,32 +14,7 @@ #include "../utils/ast/ast.h" #include "../utils/hash_map/hash_map.h" -// --- Helpers --- -/** - * @brief converts a linked list of command arguments to an argv array. Don't - * forget to free the result - * - * @param command_list Linked list of command arguments - * @return char** Array of command arguments suitable for execvp. Terminated by - * NULL - */ - -/** - * @brief Tries to execute a builtin command if the command matches a builtin - * - * @param argv Array of command arguments - * @return int Exit status of the builtin command, or -1 if not a builtin - */ - -// --- Execution Core --- - -/** - * @brief Executes a command represented by an ast_command structure - * - * @param command The command to execute - * @return int The exit status of the command - */ // Refactored: delegates to helpers in execution_helpers.c #include "execution_helpers.h" diff --git a/src/execution/execution_helpers.c b/src/execution/execution_helpers.c index 8041a3c..e82cbfe 100644 --- a/src/execution/execution_helpers.c +++ b/src/execution/execution_helpers.c @@ -208,9 +208,97 @@ int exec_ast_redir(struct ast_redir *redir, struct hash_map *vars) return ret; } -// Dummy try_builtin for linking -static int try_builtin(char **argv) +// --- Builtins --- + +static int builtin_echo(char **argv) +{ + bool newline = true; + int i = 1; + + if (argv[1] && strcmp(argv[1], "-n") == 0) + { + newline = false; + i++; + } + + for (; argv[i]; i++) + { + printf("%s", argv[i]); + if (argv[i + 1]) + printf(" "); + } + if (newline) + printf("\n"); + + fflush(stdout); + return 0; +} + + +static int builtin_true(char **argv) { (void)argv; + return 0; +} + +static int builtin_false(char **argv) +{ + (void)argv; + return 1; +} + +static int builtin_exit(char **argv) +{ + int exit_val = 0; + if (argv[1]) + exit_val = atoi(argv[1]); + exit(exit_val); + return exit_val; +} + + +static int builtin_cd(char **argv) +{ + const char *path = argv[1]; + if (!path) + { + path = getenv("HOME"); + if (!path) + { + fprintf(stderr, "cd: HOME not set\n"); + return 1; + } + } + if (chdir(path) != 0) + { + perror("cd"); + return 1; + } + return 0; +} + + +/** + * @brief Tries to execute a builtin command if the command matches a builtin + * + * @param argv Array of command arguments + * @return int Exit status of the builtin command, or -1 if not a builtin + */ +static int try_builtin(char **argv) +{ + if (!argv || !argv[0]) + return 0; + + if (strcmp(argv[0], "echo") == 0) + return builtin_echo(argv); + if (strcmp(argv[0], "true") == 0) + return builtin_true(argv); + if (strcmp(argv[0], "false") == 0) + return builtin_false(argv); + if (strcmp(argv[0], "exit") == 0) + return builtin_exit(argv); + if (strcmp(argv[0], "cd") == 0) + return builtin_cd(argv); + return -1; }