feat(execution): set $? var

This commit is contained in:
william.valenduc 2026-01-22 13:10:23 +00:00
parent f20a02ddab
commit 6dd19a75ad
6 changed files with 42 additions and 5 deletions

View file

@ -216,6 +216,7 @@ int execution(struct ast *ast, struct hash_map *vars)
switch (ast->type) switch (ast->type)
{ {
case AST_VOID:
case AST_END: { case AST_END: {
return 0; return 0;
} }

View file

@ -84,10 +84,14 @@ int main(int argc, char **argv)
// Main parse-execute loop // Main parse-execute loop
while (command_ast != NULL && command_ast->type != AST_END) while (command_ast != NULL && command_ast->type != AST_END)
{ {
// Execute AST if (command_ast->type != AST_VOID)
return_code = execution(command_ast, vars); {
// Execute AST
return_code = execution(command_ast, vars);
// set $? variable // set $? variable
set_var_int(vars, "?", return_code);
}
ast_free(&command_ast); ast_free(&command_ast);

View file

@ -1,6 +1,7 @@
#include "string_utils.h" #include "string_utils.h"
#include <ctype.h> #include <ctype.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -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 realloc(dest, new_len + 1);
return dest; return dest;
} }
void int_to_str(int value, char *buffer)
{
if (buffer == NULL)
return;
snprintf(buffer, 11, "%d", value);
}

View file

@ -18,4 +18,13 @@ char *trim_blank_left(char *str);
*/ */
char *insert_into(char *dest, const char *src, size_t pos, size_t len); 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 */ #endif /* STRING_UTILS_H */

View file

@ -9,6 +9,7 @@
#include <unistd.h> #include <unistd.h>
#include "../hash_map/hash_map.h" #include "../hash_map/hash_map.h"
#include "../string_utils/string_utils.h"
#define VARS_INITIAL_SIZE 16 #define VARS_INITIAL_SIZE 16
@ -16,8 +17,8 @@ static void vars_default(struct hash_map *vars)
{ {
set_var_copy(vars, "?", "0"); set_var_copy(vars, "?", "0");
pid_t pid = getpid(); pid_t pid = getpid();
char pid_str[20]; char pid_str[11];
snprintf(pid_str, sizeof(pid_str), "%d", pid); int_to_str(pid, pid_str);
set_var_copy(vars, "$", 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); free(value_copy);
return res; 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);
}

View file

@ -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); 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 */ #endif /* ! VARS_H */