diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/lexer.c | 4 | ||||
| -rw-r--r-- | core/ptrbox.c | 98 | ||||
| -rw-r--r-- | core/state.c | 9 |
3 files changed, 109 insertions, 2 deletions
diff --git a/core/lexer.c b/core/lexer.c index 2eb50a1..e93c629 100644 --- a/core/lexer.c +++ b/core/lexer.c @@ -3,6 +3,7 @@ #include <stdlib.h> #include <string.h> #include "defconf/lexer.h" +#include "defconf/ptrbox.h" /* Constants */ #define MAX_KW_LEN 32 @@ -109,7 +110,7 @@ lexer_scan_ident(struct defconf_state *state, int lc) if (!isalnum(c) && c != '_') { lexer_putback(state, c); buf[bufind] = '\0'; - return strdup(buf); + return ptrbox_strdup(&state->ptrbox, buf); } buf[bufind++] = c; @@ -177,7 +178,6 @@ lexer_scan(struct defconf_state *state, struct token *result) /* Scan for an identifier */ if ((ident = lexer_scan_ident(state, c)) != NULL) { if (lexer_check_kw(ident, result) == 0) { - free(ident); return 0; } diff --git a/core/ptrbox.c b/core/ptrbox.c new file mode 100644 index 0000000..f6e2254 --- /dev/null +++ b/core/ptrbox.c @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2026, Apollo Telephone Laboratories. + * Provided under the BSD-3 clause. + */ + +#include <stdlib.h> +#include <string.h> +#include "defconf/ptrbox.h" + +static inline void +ptrbox_insert(struct ptrbox *ptrbox, struct ptrbox_entry *entry) +{ + if (ptrbox == NULL || entry == NULL) { + return; + } + + TAILQ_INSERT_TAIL(&ptrbox->entries, entry, link); + ++ptrbox->entry_count; +} + +/* + * Allocate a new pointer box entry + * + * @data: Data to be associated with pointer box entry + */ +static struct ptrbox_entry * +ptrbox_alloc_entry(void *data) +{ + struct ptrbox_entry *entry; + + if (data == NULL) { + return NULL; + } + + entry = malloc(sizeof(*entry)); + if (entry == NULL) { + return NULL; + } + + entry->data = data; + return entry; +} + +char * +ptrbox_strdup(struct ptrbox *ptrbox, const char *s) +{ + struct ptrbox_entry *entry; + char *dup; + + if (ptrbox == NULL || s == NULL) { + return NULL; + } + + if ((dup = strdup(s)) == NULL) { + return NULL; + } + + if ((entry = ptrbox_alloc_entry(dup)) == NULL) { + free(dup); + return NULL; + } + + ptrbox_insert(ptrbox, entry); + return dup; +} + +int +ptrbox_init(struct ptrbox *ptrbox) +{ + if (ptrbox == NULL) { + return -1; + } + + TAILQ_INIT(&ptrbox->entries); + ptrbox->entry_count = 0; + return 0; +} + +void +ptrbox_destroy(struct ptrbox *ptrbox) +{ + struct ptrbox_entry *entry, *tmp; + + if (ptrbox == NULL) { + return; + } + + entry = TAILQ_FIRST(&ptrbox->entries); + while (entry != NULL) { + if (entry->data != NULL) { + free(entry->data); + } + + tmp = entry; + entry = TAILQ_NEXT(entry, link); + free(tmp); + } +} diff --git a/core/state.c b/core/state.c index ce1c286..c55a5be 100644 --- a/core/state.c +++ b/core/state.c @@ -9,6 +9,8 @@ int defconf_state_init(const char *in_path, struct defconf_state *state) { + int error; + if (in_path == NULL || state == NULL) { return -1; } @@ -19,6 +21,12 @@ defconf_state_init(const char *in_path, struct defconf_state *state) return -1; } + if ((error = ptrbox_init(&state->ptrbox)) < 0) { + fclose(state->in_fp); + printf("fatal: failed to create pointer box\n"); + return error; + } + state->lex_cache = '\0'; return 0; } @@ -32,4 +40,5 @@ defconf_state_destroy(struct defconf_state *state) fclose(state->in_fp); state->in_fp = NULL; + ptrbox_destroy(&state->ptrbox); } |
