From 6dd19a75addd85632f6fb1735624a2af05b2585e Mon Sep 17 00:00:00 2001 From: "william.valenduc" Date: Thu, 22 Jan 2026 13:10:23 +0000 Subject: [PATCH] feat(execution): set $? var --- src/execution/execution.c | 1 + src/main.c | 10 +++++++--- src/utils/string_utils/string_utils.c | 9 +++++++++ src/utils/string_utils/string_utils.h | 9 +++++++++ src/utils/vars/vars.c | 12 ++++++++++-- src/utils/vars/vars.h | 6 ++++++ 6 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/execution/execution.c b/src/execution/execution.c index a98ff2f..d1d0e97 100644 --- a/src/execution/execution.c +++ b/src/execution/execution.c @@ -216,6 +216,7 @@ int execution(struct ast *ast, struct hash_map *vars) switch (ast->type) { + case AST_VOID: case AST_END: { return 0; } diff --git a/src/main.c b/src/main.c index 10644ac..a03cfa9 100644 --- a/src/main.c +++ b/src/main.c @@ -84,10 +84,14 @@ int main(int argc, char **argv) // Main parse-execute loop while (command_ast != NULL && command_ast->type != AST_END) { - // Execute AST - return_code = execution(command_ast, vars); + if (command_ast->type != AST_VOID) + { + // Execute AST + return_code = execution(command_ast, vars); - // set $? variable + // set $? variable + set_var_int(vars, "?", return_code); + } ast_free(&command_ast); diff --git a/src/utils/string_utils/string_utils.c b/src/utils/string_utils/string_utils.c index e5b7040..15ad87e 100644 --- a/src/utils/string_utils/string_utils.c +++ b/src/utils/string_utils/string_utils.c @@ -1,6 +1,7 @@ #include "string_utils.h" #include +#include #include #include @@ -42,3 +43,11 @@ char *insert_into(char *dest, const char *src, size_t pos, size_t len) return realloc(dest, new_len + 1); return dest; } + +void int_to_str(int value, char *buffer) +{ + if (buffer == NULL) + return; + + snprintf(buffer, 11, "%d", value); +} diff --git a/src/utils/string_utils/string_utils.h b/src/utils/string_utils/string_utils.h index 7f0e622..36f23ac 100644 --- a/src/utils/string_utils/string_utils.h +++ b/src/utils/string_utils/string_utils.h @@ -18,4 +18,13 @@ char *trim_blank_left(char *str); */ char *insert_into(char *dest, const char *src, size_t pos, size_t len); +/** + * Converts an integer to its string representation. + * @param value The integer value to convert. + * @param buffer A character array where the resulting string will be stored. + * The buffer must be at least 11 bytes long to accommodate the largest + * 32-bit integer and the null terminator. + */ +void int_to_str(int value, char *buffer); + #endif /* STRING_UTILS_H */ diff --git a/src/utils/vars/vars.c b/src/utils/vars/vars.c index 290566d..a69d349 100644 --- a/src/utils/vars/vars.c +++ b/src/utils/vars/vars.c @@ -9,6 +9,7 @@ #include #include "../hash_map/hash_map.h" +#include "../string_utils/string_utils.h" #define VARS_INITIAL_SIZE 16 @@ -16,8 +17,8 @@ static void vars_default(struct hash_map *vars) { set_var_copy(vars, "?", "0"); pid_t pid = getpid(); - char pid_str[20]; - snprintf(pid_str, sizeof(pid_str), "%d", pid); + char pid_str[11]; + int_to_str(pid, pid_str); set_var_copy(vars, "$", pid_str); } @@ -71,3 +72,10 @@ bool set_var_copy(struct hash_map *vars, const char *key, const char *value) free(value_copy); return res; } + +bool set_var_int(struct hash_map *vars, const char *key, int value) +{ + char value_str[11]; + int_to_str(value, value_str); + return set_var_copy(vars, key, value_str); +} diff --git a/src/utils/vars/vars.h b/src/utils/vars/vars.h index 9e5fa4c..19ffae1 100644 --- a/src/utils/vars/vars.h +++ b/src/utils/vars/vars.h @@ -40,4 +40,10 @@ bool set_var(struct hash_map *vars, const char *key, const char *value, */ bool set_var_copy(struct hash_map *vars, const char *key, const char *value); +/** + * Set the value of a variable to an integer. Behavior is similar to + * set_var_copy. + */ +bool set_var_int(struct hash_map *vars, const char *key, int value); + #endif /* ! VARS_H */