feat(execution): set $? var
This commit is contained in:
parent
f20a02ddab
commit
6dd19a75ad
6 changed files with 42 additions and 5 deletions
|
|
@ -216,6 +216,7 @@ int execution(struct ast *ast, struct hash_map *vars)
|
|||
|
||||
switch (ast->type)
|
||||
{
|
||||
case AST_VOID:
|
||||
case AST_END: {
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
10
src/main.c
10
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);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "string_utils.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.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 dest;
|
||||
}
|
||||
|
||||
void int_to_str(int value, char *buffer)
|
||||
{
|
||||
if (buffer == NULL)
|
||||
return;
|
||||
|
||||
snprintf(buffer, 11, "%d", value);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue