From 98c18a13d25075e1bc876885bb95267ed9f2a1f8 Mon Sep 17 00:00:00 2001 From: Ákos Kőrösi Date: Wed, 13 May 2026 00:09:06 +0200 Subject: outline state management; change maybe macro --- exec.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------- lex.h | 4 +-- parse.h | 2 +- typeutils.h | 4 +-- 4 files changed, 78 insertions(+), 17 deletions(-) diff --git a/exec.c b/exec.c index e2fbbb3..ac88463 100644 --- a/exec.c +++ b/exec.c @@ -1,39 +1,100 @@ #include "lex.h" #include "parse.h" #include "typeutils.h" +#include +typedef struct { + char* key; + char* val; +} StrPair; + +DECLARE_VEC(StrPair, KeyValMap) +DECLARE_MAYBE(char*, MaybeStr) -DECLARE_VEC(char*, StdStrVec) +void set_map_pair(KeyValMap* map, char* key, char* val) { +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) { + // TODO: maybe copy instead + strcpy( + map->blocks[i / map->block_size][i % map->block_size].val, + val + ); + break; + } + } +} + +MaybeStr get_map_value(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) { + 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; + } + } +} +typedef struct { + KeyValMap aliases; + KeyValMap shell_vars; +} ShellState; + + +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); +} + typedef struct { QuotationMode qmode; char* content; } ExpandedChunk; -ExpandedChunk expand_chunk(Chunk chunk) { + + + +ExpandedChunk expand_chunk(Chunk chunk, ShellState* shstate) { char* content; if (chunk.qmode == SINGLE_Q || chunk.kind == LITERAL) { if (chunk.qmode == SINGLE_Q && chunk.kind != LITERAL) exit(69); content = chunk.as.literal_str; } else if (chunk.kind == VAR_EXP) { - // TODO: we need a ShellState to query + content = get_var(chunk.as.varname, shstate); + } else if (chunk.kind == REGION_EXP) { + exit(7); // Not supported yet; arithm } + return (ExpandedChunk){.qmode = chunk.qmode, .content = content}; +} - match (chunk.kind) { - case L - } - switch (chunk.qmode) { - case SINGLE_Q: - +char* expand_token(Token token, ShellState* shstate) { - } + // TODO: make some kind of extractor for the Vec-s that returns a regular T*-type NULL-term array + // TODO: we need to split strs on spaces + exit(1); } - -void run_pipeline(Pipeline pl) { +void run_pipeline(Pipeline pl, ShellState* shstate) { } diff --git a/lex.h b/lex.h index 57c71bb..e5d843f 100644 --- a/lex.h +++ b/lex.h @@ -62,7 +62,7 @@ typedef struct { DECLARE_VEC(Token, TokenVec) -DECLARE_MAYBE(SpecialToken) +DECLARE_MAYBE(SpecialToken, MaybeSpecialToken) typedef enum { CMDS, @@ -74,7 +74,7 @@ struct Region { TokenVec tokens; }; -DECLARE_MAYBE(Chunk); +DECLARE_MAYBE(Chunk, MaybeChunk); DECLARE_VEC(Region, RegionVec) diff --git a/parse.h b/parse.h index 5837a4f..f44ae1b 100644 --- a/parse.h +++ b/parse.h @@ -35,7 +35,7 @@ typedef enum { // TODO: add the |& thingy } Connector; -DECLARE_MAYBE(Connector) +DECLARE_MAYBE(Connector, MaybeConnector) typedef struct { Command cmd; diff --git a/typeutils.h b/typeutils.h index e7aee6f..925857b 100644 --- a/typeutils.h +++ b/typeutils.h @@ -96,8 +96,8 @@ static inline char* inner(String* str) { } -#define DECLARE_MAYBE(T) \ +#define DECLARE_MAYBE(T, Name) \ typedef struct { \ bool some; \ T value; \ - } Maybe##T; + } Name; -- cgit v1.2.3