feat(expansion) 10291 expansion

This commit is contained in:
william.valenduc 2026-01-19 17:55:16 +00:00
parent 2ebf56dde7
commit b443cd1876
4 changed files with 75 additions and 14 deletions

View file

@ -2,8 +2,11 @@
#include "vars.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "../hash_map/hash_map.h"
@ -14,6 +17,26 @@ struct hash_map *vars_init(void)
return hash_map_init(VARS_INITIAL_SIZE);
}
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);
set_var_copy(vars, "$$", pid_str);
}
short short_random(void)
{
static bool seeded = false;
if (!seeded)
{
srand((unsigned)time(NULL));
seeded = true;
}
return (short)(rand() & 0x7FFF); // force 16 bits positive
}
char *get_var(const struct hash_map *vars, const char *key)
{
return (char *)hash_map_get(vars, key);

View file

@ -10,6 +10,16 @@
*/
struct hash_map *vars_init(void);
/**
* Set default variables.
*/
void vars_default(struct hash_map *vars);
/**
* Generate a random short integer (16 bits positive [0-32767]).
*/
short short_random(void);
/**
* Get the value of a variable, NULL if not found.
*/
@ -26,7 +36,8 @@ char *get_var_or_env(const struct hash_map *vars, const char *key);
* the hash_map and need to be on the heap. Returns true on success, false on
* failure.
*/
bool set_var(struct hash_map *vars, const char *key, const char *value);
bool set_var(struct hash_map *vars, const char *key, const char *value,
bool *updated);
/**
* Same as set_var, but makes copies of key and value so you don't have to worry