aboutsummaryrefslogtreecommitdiff
path: root/client/rl.cpp
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2024-05-24 17:47:00 +0200
committerlonkaars <loek@pipeframe.xyz>2024-05-24 17:47:00 +0200
commit1a92ed5075aba4b41fe34422d21a2c66cdf1d4c9 (patch)
treea66d2dae13ab7c214da7b9ccf4701f0b7817c2e5 /client/rl.cpp
parent31c30df2a24a45c69a7c5c2f594fa3a9a835b1fb (diff)
WIP `send` command
Diffstat (limited to 'client/rl.cpp')
-rw-r--r--client/rl.cpp47
1 files changed, 25 insertions, 22 deletions
diff --git a/client/rl.cpp b/client/rl.cpp
index 6073500..3f93e99 100644
--- a/client/rl.cpp
+++ b/client/rl.cpp
@@ -8,13 +8,14 @@
#include "rl.h"
#include "cmd.h"
+#include "parse.h"
void rl_printf(const char *fmt, ...) {
// save line
char* saved_line = rl_copy_text(0, rl_end);
int saved_point = rl_point;
int saved_end = rl_end;
-
+
// clear line
rl_save_prompt();
rl_replace_line("", 0);
@@ -36,34 +37,38 @@ void rl_printf(const char *fmt, ...) {
free(saved_line);
}
-static bool cli_cmd(char* line) {
- char* cmd = strtok(line, " \t\n");
+static void cli_cmd(char* cmd) {
+ char* line = consume_token(cmd, IFS);
+
for (size_t i = 0; i < cmds_length; i++) {
- if (strncmp(cmds[i].name, cmd, strlen(cmd)) != 0) continue;
+ if (strncmp(cmds[i].name, cmd, strlen(cmd)) != 0)
+ continue;
+
cmds[i].handle(line);
- return true;
+ return;
}
- return false;
+
+ printf("unknown command!\n");
}
static char* rl_completion_entries(const char *text, int state) {
- static size_t i = 0;
- if (state == 0) i = 0;
-
- while (i < cmds_length) {
- struct cmd cmd = cmds[i];
- i++;
- if (strncmp(text, cmd.name, strlen(text)) == 0) {
- return strdup(cmd.name);
- }
- }
-
- return NULL;
+ static size_t i = 0;
+ if (state == 0) i = 0;
+
+ while (i < cmds_length) {
+ struct cmd cmd = cmds[i];
+ i++;
+ if (strncmp(text, cmd.name, strlen(text)) == 0) {
+ return strdup(cmd.name);
+ }
+ }
+
+ return NULL;
}
int cli_main() {
char* input = NULL;
- rl_completion_entry_function = rl_completion_entries;
+ rl_completion_entry_function = rl_completion_entries;
while (1) {
if (input != NULL) free(input);
@@ -73,9 +78,7 @@ int cli_main() {
if (*input == '\0') continue; // ignore empty lines
add_history(input);
- if (cli_cmd(input)) continue;
-
- printf("unknown command!\n");
+ cli_cmd(input);
}
return EXIT_SUCCESS;