aboutsummaryrefslogtreecommitdiff
path: root/client/rl.cpp
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2024-05-22 19:32:21 +0200
committerlonkaars <loek@pipeframe.xyz>2024-05-22 19:32:21 +0200
commit53d27ebf10225274a50dc4a7c2343d4efce55a8a (patch)
treebdf1a5d9b3900d86a999bc396e7815bd7372a549 /client/rl.cpp
parentdb8906d54cd9afbc57f0b40a0d618335c552f704 (diff)
clean up command handling
Diffstat (limited to 'client/rl.cpp')
-rw-r--r--client/rl.cpp27
1 files changed, 12 insertions, 15 deletions
diff --git a/client/rl.cpp b/client/rl.cpp
index 32a4df0..b016370 100644
--- a/client/rl.cpp
+++ b/client/rl.cpp
@@ -7,7 +7,7 @@
#include <readline/history.h>
#include "rl.h"
-#include "sock.h"
+#include "cmd.h"
void rl_printf(const char *fmt, ...) {
// save line
@@ -36,9 +36,13 @@ void rl_printf(const char *fmt, ...) {
free(saved_line);
}
-void cmd_test() {
- const char* data = "Hello world!";
- i2c_send(0x39, (char*) data, strlen(data));
+static bool cli_cmd(char* line) {
+ for (size_t i = 0; i < cmds_length; i++) {
+ if (strcmp(line, cmds[i].name) != 0) continue;
+ cmds[i].handle(line);
+ return true;
+ }
+ return false;
}
int cli_main() {
@@ -47,18 +51,11 @@ int cli_main() {
if (input != NULL) free(input);
input = readline(CLI_PROMPT);
- // exit on ^D or ^C (EOF)
- if (input == NULL) return EXIT_SUCCESS;
+ if (input == NULL) return EXIT_SUCCESS; // exit on ^D (EOF)
+ if (*input == '\0') continue; // ignore empty lines
+ add_history(input);
- // add non-empty line to history
- if (*input) add_history(input);
-
- if (strcmp(input, "exit") == 0) return EXIT_SUCCESS;
-
- if (strcmp(input, "test") == 0) {
- cmd_test();
- continue;
- }
+ if (cli_cmd(input)) continue;
printf("unknown command!\n");
}