nserver

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 76c8c714cdf91c1841ab2a50e2bc2de5399aa820
parent 17cdf409917335a299e70d03973aea05ded781e3
Author: rsiddharth <s@ricketyspace.net>
Date:   Mon,  9 Dec 2019 19:05:07 -0500

nserver: Update call_function.

* nserver/src/ncmd.c (call_function): Dynmically allocate space for
msg.
* nserver/tests/ncmd_tests.c (test_call_function): Updat test.

Diffstat:
nserver/src/ncmd.c | 65++++++++++++++++++++++++++++++++++++++---------------------------
nserver/tests/ncmd_tests.c | 9+++++++++
2 files changed, 47 insertions(+), 27 deletions(-)

diff --git a/nserver/src/ncmd.c b/nserver/src/ncmd.c @@ -156,105 +156,116 @@ int check_args(struct bstrList *cmd_parts, int argc) return -1; } -/** - * TODO: - * - msg needs to be dynamically allocated. - * - error msgs need to be consistent. - */ char *call_function(int func, struct bstrList *cmd_parts) { char *msg = NULL; + int msg_sz = 200; + msg = (char *) calloc(msg_sz + 1, sizeof(char)); + check_mem(msg); + if (func < 0 || cmd_parts == NULL || cmd_parts->qty < 1) { - msg = "function call failed: internal error"; + strncpy(msg, "error: args invalid\n", msg_sz); return msg; } - int rc = 0; switch (func) { case NS_CREATE: if(check_args(cmd_parts, 2) != 0) { - msg = "create failed: command invalid\n"; + strncpy(msg, "error: command invalid\n", msg_sz); break; } if (sscreate(bdata(cmd_parts->entry[1])) != 0) { - msg = "create failed: internal error"; + strncpy(msg, "error: create failed\n", msg_sz); break; } - msg = "OK\n"; + strncpy(msg, "OK\n", msg_sz); break; case NS_SAMPLE: if(check_args(cmd_parts, 3) != 0) { - msg = "sample failed: command invalid\n"; + strncpy(msg, "error: command invalid\n", msg_sz); break; } double sample = strtod(bdata(cmd_parts->entry[2]), NULL); if (sssample(bdata(cmd_parts->entry[1]), sample) != 0) { - msg = "sample failed: internal error"; + strncpy(msg, "error: sample failed\n", msg_sz); break; } - msg = "OK\n"; + strncpy(msg, "OK\n", msg_sz); break; case NS_MEAN: if(check_args(cmd_parts, 2) != 0) { - msg = "mean failed: command invalid\n"; + strncpy(msg, "error: command invalid\n", msg_sz); break; } double mean = ssmean(bdata(cmd_parts->entry[1])); if (mean < 0) { - msg = "mean failed: internal error"; + strncpy(msg, "error: mean failed\n", msg_sz); break; } - msg = (char *) calloc(20, sizeof(char)); if (sprintf(msg, "Mean: %.2f\n", mean) < 0) { - msg = "mean failed: internal error"; + strncpy(msg, "error: mean failed\n", msg_sz); } break; case NS_DUMP: if(check_args(cmd_parts, 2) != 0) { - msg = "dump failed: command invalid\n"; + strncpy(msg, "error: command invalid\n", msg_sz); break; } char *dump = ssdump(bdata(cmd_parts->entry[1])); if (dump == NULL) { - msg = "dump failed: internal error"; + strncpy(msg, "error: dump failed\n", msg_sz); break; } + + // Clean up msg. + free(msg); + msg = dump; break; case NS_DELETE: if(check_args(cmd_parts, 2) != 0) { - msg = "delete failed: command invalid\n"; + strncpy(msg, "error: command invalid\n", msg_sz); break; } if (ssdelete(bdata(cmd_parts->entry[1])) != 0) { - msg = "delete failed: internal error"; + strncpy(msg, "error: delete failed\n", msg_sz); break; } - msg = "OK\n"; + strncpy(msg, "OK\n", msg_sz); break; case NS_LIST: if(check_args(cmd_parts, 1) != 0) { - msg = "list failed: command invalid\n"; + strncpy(msg, "error: command invalid\n", msg_sz); break; } - msg = sslist(); - if (msg == NULL) { - msg = "list failed: internal error"; + char *list = sslist(); + if (list == NULL) { + strncpy(msg, "error: list failed\n", msg_sz); + break; } + + // Clean up msg. + free(msg); + + msg = list; break; default: - msg = "error: function not found"; + strncpy(msg, "error: function not found\n", msg_sz); break; } return msg; + error: + return NULL; +} + } diff --git a/nserver/tests/ncmd_tests.c b/nserver/tests/ncmd_tests.c @@ -219,48 +219,56 @@ char *test_call_function() mu_assert(parts != NULL, "cmd_parts failed"); msg = call_function(NS_CREATE, parts); mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + free(msg); char *ham = "/create ham"; parts = cmd_parts(ham); mu_assert(parts != NULL, "cmd_parts failed"); msg = call_function(NS_CREATE, parts); mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + free(msg); char *beef = "/create beef"; parts = cmd_parts(beef); mu_assert(parts != NULL, "cmd_parts failed"); msg = call_function(NS_CREATE, parts); mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + free(msg); char *bacon_sample = "/sample bacon 4.2"; parts = cmd_parts(bacon_sample); mu_assert(parts != NULL, "cmd_parts failed"); msg = call_function(NS_SAMPLE, parts); mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + free(msg); bacon_sample = "/Sample bacon 6.9"; parts = cmd_parts(bacon_sample); mu_assert(parts != NULL, "cmd_parts failed"); msg = call_function(NS_SAMPLE, parts); mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + free(msg); char *bacon_mean = "/mean bacon"; parts = cmd_parts(bacon_mean); mu_assert(parts != NULL, "cmd_parts failed"); msg = call_function(NS_MEAN, parts); mu_assert(strcmp(msg, "Mean: 5.55\n") == 0, "call function failed"); + free(msg); char *bacon_dump = "/dump bacon"; parts = cmd_parts(bacon_dump); mu_assert(parts != NULL, "cmd_parts failed"); msg = call_function(NS_DUMP, parts); mu_assert(strcmp(msg, "sum: 11.100000, sumsq: 65.250000, n: 2, min: 4.200000, max: 6.900000, mean: 5.550000, stddev: 1.909188\n") == 0, "call function failed"); + free(msg); char *bacon_delete = "/delete bacon"; parts = cmd_parts(bacon_delete); mu_assert(parts != NULL, "cmd_parts failed"); msg = call_function(NS_DELETE, parts); mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + free(msg); char *list = "/list"; parts = cmd_parts(list); @@ -269,6 +277,7 @@ char *test_call_function() mu_assert(strcmp(msg, "ham\nbeef\n") == 0 || strcmp(msg, "beef\nham\n") == 0, "call function failed"); + free(msg); return NULL; }