diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2020-12-16 16:58:58 +0100 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2021-03-26 18:53:25 +0100 |
commit | e3db069e103960a93b33ee5f0de57bb0594c6096 (patch) | |
tree | 981c815aef8f696242e93796cb17f295167463cb | |
parent | 4cdda939de174c8fa8c6e701b3b8acd7309e6358 (diff) | |
download | polecat-e3db069e103960a93b33ee5f0de57bb0594c6096.tar.gz polecat-e3db069e103960a93b33ee5f0de57bb0594c6096.zip |
allow functions to return an allocated string value
to simplify the logic all literal string values are allocated before usage
-rw-r--r-- | src/lutris.c | 26 | ||||
-rw-r--r-- | src/lutris.h | 2 |
2 files changed, 16 insertions, 12 deletions
diff --git a/src/lutris.c b/src/lutris.c index ca48391..bdecaff 100644 --- a/src/lutris.c +++ b/src/lutris.c @@ -16,15 +16,18 @@ static const struct Command lutris_commands[] = { { .name = "info", .func = lutris_info, .description = "show information about a lutris script" }, }; -void func() +char* func() { - printf("I got called because $func was found\n"); + char* str = malloc(5); + strncpy(str, "FUNC", 4+1); + + return str; } COMMAND(lutris, debug) { struct list_t variables[] = { - { .key = "string", .type = value_string, .value.str = "output" }, + { .key = "string", .type = value_string, .value.str = "STRING" }, { .key = "func", .type = value_function, .value.func = func }, }; @@ -552,7 +555,7 @@ size_t parseVar(char** pvar, struct list_t* variables, size_t variable_count) char* var = *pvar; char* head = var; char* tail, *end; - char* buf, *key; + char* buf, *value; size_t varcount = 0; size_t offset = 0; @@ -563,7 +566,7 @@ size_t parseVar(char** pvar, struct list_t* variables, size_t variable_count) { // ensure sanity by clearing variables buf = NULL; - key = NULL; + value = NULL; end = strlen(var) + var; @@ -591,11 +594,12 @@ size_t parseVar(char** pvar, struct list_t* variables, size_t variable_count) switch (variables[i].type) { case value_string: - key = variables[i].value.str; + value = malloc(strlen(variables[i].value.str) + 1); + strncpy(value, variables[i].value.str, strlen(variables[i].value.str) + 1); break; case value_function: - variables[i].value.func(); + value = variables[i].value.func(); break; default: @@ -607,23 +611,23 @@ size_t parseVar(char** pvar, struct list_t* variables, size_t variable_count) free(buf); } - if (!key) continue; + if (!value) continue; // copy everything from variable key end to end of string buf = malloc(end-tail+1); strncpy(buf, tail, end-tail+1); - size_t varsize = (head-var) + strlen(key) + (end-tail) + 1; + size_t varsize = (head-var) + strlen(value) + (end-tail) + 1; var = realloc(var, varsize); // end of the string up until the variable // we cannot fetch this after the realloc because it points to completly different memory making pointer math impossible var[bufend] = '\0'; - strncat(var, key, varsize - strlen(var) - 1); + strncat(var, value, varsize - strlen(var) - 1); strncat(var, buf, varsize - strlen(var) - 1); // cleanup + free(value); free(buf); - buf = NULL; varcount++; diff --git a/src/lutris.h b/src/lutris.h index 643c8ff..aa6874d 100644 --- a/src/lutris.h +++ b/src/lutris.h @@ -112,7 +112,7 @@ struct list_t { union { char* str; - void (*func)(); + char* (*func)(); } value; }; |