#include "utils.h" #include #include void append_str_to_String(String* target, const char* src_str) { size_t len = strlen(src_str); for (size_t i = 0; i < len; i++) { push_to_String(target, src_str[i]); } } char* inner(String* str) { char *out = malloc(str->count + 1); if (!out) abort(); for (size_t i = 0; i < str->count; i++) { out[i] = *get_from_String(str, i); } out[str->count] = '\0'; return out; } void set_map_pair(KeyValMap* map, StrPair pair) { for (int i = 0; i < map->count; i++) { char* key_i = get_from_KeyValMap(map, i)->key; if (key_i == NULL) continue; if (strcmp(key_i, pair.key) == 0) { // TODO: maybe copy instead strcpy( map->blocks[i / map->block_size][i % map->block_size].val, pair.val ); return; } } push_to_KeyValMap(map, pair); } MaybeStr get_map_value(KeyValMap* map, char* key) { printf("%d\n", map->count); for (int i = 0; i < map->count; i++) { char* key_i = get_from_KeyValMap(map, i)->key; if (key_i == NULL) continue; if (strcmp(key_i, key) == 0) { return (MaybeStr){ .some = true, .value = get_from_KeyValMap(map, i)->val }; } } return (MaybeStr){.some = false}; } void unset_map_key(KeyValMap* map, char* key) { for (int i = 0; i < map->count; i++) { char* key_i = get_from_KeyValMap(map, i)->key; if (key_i == NULL) continue; if (strcmp(key_i, key) == 0) { map->blocks[i / map->block_size][i % map->block_size].key = NULL; break; } } } char* get_var(char* varname, ShellState* shstate) { MaybeStr maybe_varval = get_map_value(&shstate->shell_vars, varname); if (maybe_varval.some == true) return maybe_varval.value; return getenv(varname); } bool is_identifier(char* str) { if (str == NULL) return false; if (!isalpha(str[0]) && str[0] != '_') return false; for (int i = 1; str[i] != '\0'; i++) { if (!isalnum(str[i]) && str[i] != '_') return false; } return true; }