aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2019-11-28 18:29:17 -0500
committerrsiddharth <s@ricketyspace.net>2020-04-17 20:56:35 -0400
commit63c5da4f5f646d4c989e7760be46de86e3b01efe (patch)
tree4b41d5b24d75a4b56e500ed45015b86237cd66d2
parentdf712efd0127a8f145fcec39b695485270367783 (diff)
downloadnserver-63c5da4f5f646d4c989e7760be46de86e3b01efe.tar.gz
nserver-63c5da4f5f646d4c989e7760be46de86e3b01efe.tar.bz2
nserver-63c5da4f5f646d4c989e7760be46de86e3b01efe.tar.xz
nserver-63c5da4f5f646d4c989e7760be46de86e3b01efe.zip
nserver: ncmd.h: Add find_function.
* nserver/src/ncmd.c (test_find_function): New function. (all_tests): Add test_find_function. * nserver/src/ncmd.h (FUNCTIONS): New enum. (find_function): New function declaration. * nserver/tests/ncmd_tests.c (find_function): New function definition.
-rw-r--r--nserver/src/ncmd.c53
-rw-r--r--nserver/src/ncmd.h12
-rw-r--r--nserver/tests/ncmd_tests.c51
3 files changed, 116 insertions, 0 deletions
diff --git a/nserver/src/ncmd.c b/nserver/src/ncmd.c
index 8c90831..ad57e2b 100644
--- a/nserver/src/ncmd.c
+++ b/nserver/src/ncmd.c
@@ -98,3 +98,56 @@ struct bstrList *cmd_parts(char *cmd)
return NULL;
}
+int find_function(struct bstrList *cmd_parts)
+{
+ // functions.
+ struct tagbstring fcreate = bsStatic("/create");
+ struct tagbstring fsample = bsStatic("/sample");
+ struct tagbstring fmean = bsStatic("/mean");
+ struct tagbstring fdump = bsStatic("/dump");
+ struct tagbstring fdelete = bsStatic("/delete");
+ struct tagbstring flist = bsStatic("/list");
+
+ check(cmd_parts != NULL, "cmd_parts is NULL");
+ check(cmd_parts->qty > 0, "qty check failed");
+
+ bstring cmd_name = cmd_parts->entry[0];
+ check(blength(cmd_name) > 0, "cmd_name check failed");
+
+ // trim cmd name
+ int rc = btrimws(cmd_name);
+ check(rc == BSTR_OK, "cmd name trim failed");
+
+ // find function for cmd_name
+ if (bstricmp(cmd_name, &fcreate) == 0) {
+ check(cmd_parts->qty == 2, "cmd invalid");
+
+ return NS_CREATE;
+ } else if (bstricmp(cmd_name, &fsample) == 0) {
+ check(cmd_parts->qty == 3, "cmd invalid");
+
+ return NS_SAMPLE;
+ } else if (bstricmp(cmd_name, &fmean) == 0) {
+ check(cmd_parts->qty == 2, "cmd invalid");
+
+ return NS_MEAN;
+ } else if (bstricmp(cmd_name, &fdump) == 0) {
+ check(cmd_parts->qty == 2, "cmd invalid");
+
+ return NS_DUMP;
+ } else if (bstricmp(cmd_name, &fdelete) == 0) {
+ check(cmd_parts->qty == 2, "cmd invalid");
+
+ return NS_DELETE;
+ } else if (bstricmp(cmd_name, &flist) == 0) {
+ check(cmd_parts->qty == 1, "cmd invalid");
+
+ return NS_LIST;
+ } else {
+ return NS_NOP;
+ }
+
+ error:
+ return NS_NOP;
+}
+
diff --git a/nserver/src/ncmd.h b/nserver/src/ncmd.h
index c1cf9a0..80ca45a 100644
--- a/nserver/src/ncmd.h
+++ b/nserver/src/ncmd.h
@@ -9,9 +9,21 @@
#define CMD_MIN_SIZE 5
#define CMD_MAX_SIZE 120
+enum FUNCTIONS {
+ NS_CREATE,
+ NS_SAMPLE,
+ NS_MEAN,
+ NS_DUMP,
+ NS_DELETE,
+ NS_LIST,
+ NS_NOP = -1
+};
+
int sanitize(char *cmd);
char *check_cmd(char *cmd);
struct bstrList *cmd_parts(char *cmd);
+int find_function(struct bstrList *cmd_parts);
+
#endif
diff --git a/nserver/tests/ncmd_tests.c b/nserver/tests/ncmd_tests.c
index 0331000..0bbcbb6 100644
--- a/nserver/tests/ncmd_tests.c
+++ b/nserver/tests/ncmd_tests.c
@@ -135,6 +135,56 @@ char *test_cmd_parts()
return NULL;
}
+char *test_find_function()
+{
+ struct bstrList *parts = NULL;
+ int funk = 0;
+
+ char *bacon = "/create bacon";
+ parts = cmd_parts(bacon);
+ mu_assert(parts != NULL, "cmd_parts failed");
+ funk = find_function(parts);
+ mu_assert(funk == NS_CREATE, "find function create failed");
+
+ char *ham = "/create ham";
+ parts = cmd_parts(ham);
+ mu_assert(parts != NULL, "cmd_parts failed");
+ funk = find_function(parts);
+ mu_assert(funk == NS_CREATE, "find function create failed");
+
+ char *sample = "/sample bacon 42";
+ parts = cmd_parts(sample);
+ mu_assert(parts != NULL, "cmd_parts failed");
+ funk = find_function(parts);
+ mu_assert(funk == NS_SAMPLE, "find function sample failed");
+
+ char *mean = "/mean bacon";
+ parts = cmd_parts(mean);
+ mu_assert(parts != NULL, "cmd_parts failed");
+ funk = find_function(parts);
+ mu_assert(funk == NS_MEAN, "find function mean failed");
+
+ char *dump = "/dump bacon";
+ parts = cmd_parts(dump);
+ mu_assert(parts != NULL, "cmd_parts failed");
+ funk = find_function(parts);
+ mu_assert(funk == NS_DUMP, "find function dump failed");
+
+ char *delete = "/delete bacon";
+ parts = cmd_parts(delete);
+ mu_assert(parts != NULL, "cmd_parts failed");
+ funk = find_function(parts);
+ mu_assert(funk == NS_DELETE, "find function delete failed");
+
+ char *list = "/list";
+ parts = cmd_parts(list);
+ mu_assert(parts != NULL, "cmp_parts failed");
+ funk = find_function(parts);
+ mu_assert(funk == NS_LIST, "find function list failed");
+
+ return NULL;
+}
+
char *all_tests()
{
mu_suite_start();
@@ -142,6 +192,7 @@ char *all_tests()
mu_run_test(test_sanitize);
mu_run_test(test_check_cmd);
mu_run_test(test_cmd_parts);
+ mu_run_test(test_find_function);
return NULL;
}