diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-02-06 10:14:48 -0500 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-02-06 10:14:48 -0500 |
commit | 906b120e249c7d73fcd03477bafbdd80ce61b7eb (patch) | |
tree | f728224fa9629085c259714810d6c8033691a7e0 | |
parent | bf3a29fbe20b0b2bd0339c76c3b279cea63b59f2 (diff) | |
download | stasis-906b120e249c7d73fcd03477bafbdd80ce61b7eb.tar.gz |
Fix addressing issue...
* Store the address of the pointer to template variable, instead of the pointer. Whoops!
* Pre-declare all template pointers as early as possible to make them available to the entire program
* Comment tpl_*() prototypes
-rw-r--r-- | include/template.h | 26 | ||||
-rw-r--r-- | src/main.c | 18 | ||||
-rw-r--r-- | src/template.c | 11 |
3 files changed, 48 insertions, 7 deletions
diff --git a/include/template.h b/include/template.h index 36d3c2a..24ca98f 100644 --- a/include/template.h +++ b/include/template.h @@ -7,10 +7,32 @@ #include "omc.h" -void tpl_register(char *key, char *ptr); +/** + * Map a text value to a pointer in memory + * + * @param key in-text variable name + * @param ptr pointer to string + */ +void tpl_register(char *key, char **ptr); + +/** + * Free the template engine + */ void tpl_free(); + +/** + * Retrieve the value of a key mapped by the template engine + * @param key string registered by `tpl_register` + * @return a pointer to value, or NULL if the key is not present + */ char *tpl_getval(char *key); -char *tpl_render(char *str); +/** + * Replaces occurrences of all registered key value pairs in `str` + * @param str the text data to render + * @return a rendered copy of `str`, or NULL. + * The caller is responsible for free()ing memory allocated by this function + */ +char *tpl_render(char *str); #endif //OMC_TEMPLATE_H @@ -188,6 +188,24 @@ int main(int argc, char *argv[], char *arge[]) { msg(OMC_MSG_L1, "Initializing\n"); + // Expose variables for use with the template engine + // NOTE: These pointers are populated by delivery_init() so please avoid using + // tpl_render() until then. + tpl_register("meta.name", &ctx.meta.name); + tpl_register("meta.version", &ctx.meta.version); + tpl_register("meta.codename", &ctx.meta.codename); + tpl_register("meta.mission", &ctx.meta.mission); + tpl_register("meta.python", &ctx.meta.python); + tpl_register("meta.python_compact", &ctx.meta.python_compact); + tpl_register("info.release_name", &ctx.info.release_name); + tpl_register("conda.installer_baseurl", &ctx.conda.installer_baseurl); + tpl_register("conda.installer_name", &ctx.conda.installer_name); + tpl_register("conda.installer_version", &ctx.conda.installer_version); + tpl_register("conda.installer_arch", &ctx.conda.installer_arch); + tpl_register("conda.installer_platform", &ctx.conda.installer_platform); + tpl_register("system.arch", &ctx.system.arch); + tpl_register("system.platform", &ctx.system.platform[DELIVERY_PLATFORM_RELEASE]); + // Set up PREFIX/etc directory information // The user may manipulate the base directory path with OMC_SYSCONFDIR // environment variable diff --git a/src/template.c b/src/template.c index 335da85..fe7ef11 100644 --- a/src/template.c +++ b/src/template.c @@ -12,12 +12,12 @@ struct tpl_item { char *key; - char *ptr; + char **ptr; }; struct tpl_item *tpl_pool[1024] = {0}; unsigned tpl_pool_used = 0; -void tpl_register(char *key, char *ptr) { +void tpl_register(char *key, char **ptr) { struct tpl_item *item = NULL; item = calloc(1, sizeof(*item)); if (!item) { @@ -47,7 +47,7 @@ char *tpl_getval(char *key) { for (size_t i = 0; i < tpl_pool_used; i++) { if (tpl_pool[i]->key) { if (!strcmp(tpl_pool[i]->key, key)) { - result = tpl_pool[i]->ptr; + result = *tpl_pool[i]->ptr; break; } } @@ -78,8 +78,9 @@ char *tpl_render(char *str) { return NULL; } size_t output_bytes = strlen(str); - char *output; - char *b_close, *pos; + char *output = NULL; + char *b_close = NULL; + char *pos = NULL; pos = str; output = calloc(output_bytes + 1, sizeof(*output)); |