diff options
author | Joseph Hunkeler <jhunkeler@users.noreply.github.com> | 2024-07-06 09:49:51 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-06 09:49:51 -0400 |
commit | 8ce824ac4b2f526331093a7150e643700efd4d20 (patch) | |
tree | 99e27dee24e82d78941ded4c510e1bac57c3f015 /src/template.c | |
parent | abe87056faa6ed02aff3bbf77c1fd78b713a0864 (diff) | |
download | stasis-8ce824ac4b2f526331093a7150e643700efd4d20.tar.gz |
Add github.c and github.h (#9)
* Add github.c and github.h
* Implements get_github_release_notes()
* Remove unused variables
* Fix circular dependency on tplfunc_frame
* Remove predeclaration of tplfunc_frame
* tpl_register_func accepts pointer to void instead
* tpl_register_func sets maximum number of arguments
* Frame is generated within tpl_register_func
* Improve template function error handling and return/output management
* Remove redundant extern statement
* Include github.h and template_func_proto.h in core.h
* Expose get_github_release_notes_tplfunc_entrypoint function to template engine
* Add template_func_proto.c and template_func_proto.h
* Replace free() with guard variant
* Fix test_template::test_tpl_register_func
* Fix tests
* Fix tests
* cmd should be at least PATH_MAX in size.
* Magic number caused failure to install conda with a long installation path
* Implement get_github_release_notes_auto function that bases release note data off test contexts
* Disable overwriting releases by default
* Add automatic release note generation function call to release_notes.md.in
* Fix test_tpl_register_func()
* Add enough space for tar command plus a path
* Fix circular include
* Github functions do not require access to core.h anyway
* Add comments to union
* Update README to mention template function availability
* Add EnvCtl structure
* Add runtime checks to avoid running all the way to the end only to be met with a configuration error.
* Rename GITHUB to GH
* Development docs pre-rough-draft
Diffstat (limited to 'src/template.c')
-rw-r--r-- | src/template.c | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/src/template.c b/src/template.c index 819fe92..8e1357c 100644 --- a/src/template.c +++ b/src/template.c @@ -25,10 +25,14 @@ extern void tpl_reset() { tpl_pool_func_used = 0; } -void tpl_register_func(char *key, struct tplfunc_frame *frame) { - (void) key; // TODO: placeholder - tpl_pool_func[tpl_pool_func_used] = calloc(1, sizeof(*tpl_pool_func[tpl_pool_func_used])); - memcpy(tpl_pool_func[tpl_pool_func_used], frame, sizeof(*frame)); +void tpl_register_func(char *key, void *tplfunc_ptr, int argc, void *data_in) { + struct tplfunc_frame *frame = calloc(1, sizeof(*frame)); + frame->key = strdup(key); + frame->argc = argc; + frame->func = tplfunc_ptr; + frame->data_in = data_in; + + tpl_pool_func[tpl_pool_func_used] = frame; tpl_pool_func_used++; } @@ -220,7 +224,7 @@ char *tpl_render(char *str) { strcpy(func_name_temp, type_stop + 1); char *param_begin = strchr(func_name_temp, '('); if (!param_begin) { - fprintf(stderr, "offset %zu: function name must be followed by a '('\n", off); + fprintf(stderr, "At position %zu in %s\nfunction name must be followed by a '('\n", off, key); guard_free(output); return NULL; } @@ -228,7 +232,7 @@ char *tpl_render(char *str) { param_begin++; char *param_end = strrchr(param_begin, ')'); if (!param_end) { - fprintf(stderr, "offset %zu: function arguments must be closed with a ')'\n", off); + fprintf(stderr, "At position %zu in %s\nfunction arguments must be closed with a ')'\n", off, key); guard_free(output); return NULL; } @@ -239,8 +243,8 @@ char *tpl_render(char *str) { for (params_count = 0; params[params_count] != NULL; params_count++); struct tplfunc_frame *frame = tpl_getfunc(k); - if (params_count > frame->argc) { - fprintf(stderr, "offset %zu: Too many arguments for function: %s()\n", off, frame->key); + if (params_count > frame->argc || params_count < frame->argc) { + fprintf(stderr, "At position %zu in %s\nIncorrect number of arguments for function: %s (expected %d, got %d)\n", off, key, frame->key, frame->argc, params_count); value = strdup(""); } else { for (size_t p = 0; p < sizeof(frame->argv) / sizeof(*frame->argv) && params[p] != NULL; p++) { @@ -248,10 +252,13 @@ char *tpl_render(char *str) { strip(params[p]); frame->argv[p].t_char_ptr = params[p]; } - char func_result[100]; - char *fres = func_result; - frame->func(frame, fres); - value = strdup(fres); + char *func_result = NULL; + int func_status = 0; + if ((func_status = frame->func(frame, &func_result))) { + fprintf(stderr, "%s returned non-zero status: %d\n", frame->key, func_status); + } + value = strdup(func_result ? func_result : ""); + guard_free(func_result); } GENERIC_ARRAY_FREE(params); } else { |