diff options
Diffstat (limited to 'nserver/src/ncmd.c')
-rw-r--r-- | nserver/src/ncmd.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/nserver/src/ncmd.c b/nserver/src/ncmd.c index 51459ad..8c90831 100644 --- a/nserver/src/ncmd.c +++ b/nserver/src/ncmd.c @@ -45,3 +45,56 @@ char *check_cmd(char *cmd) err = "internal error\n"; return err; } + + +struct bstrList *cmd_parts(char *cmd) +{ + bstring bcmd = NULL; + struct bstrList *parts_tmp = NULL; + + bcmd = bfromcstr(cmd); + check(bcmd != NULL, "bstring creation failed"); + + parts_tmp = bsplit(bcmd, ' '); + check(parts_tmp != NULL, "cmp split failed"); + check(parts_tmp->qty > 0, "qty check failed"); + + struct bstrList *parts = bstrListCreate(); + check(parts != NULL, "parts create failed"); + + int rc = bstrListAlloc(parts, parts_tmp->qty); + check(rc == BSTR_OK, "parts alloc failed"); + check(parts->qty == 0, "qty check failed"); + + bstring part = NULL; + int index = 0; + for (int i = 0; i < parts_tmp->qty; i++) { + check(parts->qty <= parts->mlen, "parts capacity check failed"); + + part = parts_tmp->entry[i]; + + if (blength(part) == 0) { + continue; + } + + parts->entry[index++] = bstrcpy(part); + parts->qty++; + } + + // Clean up. + bdestroy(bcmd); + bstrListDestroy(parts_tmp); + + return parts; + error: + // Clean up. + if (bcmd) { + bdestroy(bcmd); + } + if (parts_tmp) { + bstrListDestroy(parts_tmp); + } + + return NULL; +} + |