aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client/CMakeLists.txt2
-rw-r--r--client/examples/puzbus-hello-world.cpp67
-rw-r--r--client/main.cpp68
-rw-r--r--client/rl.c55
-rw-r--r--client/rl.h18
5 files changed, 153 insertions, 57 deletions
diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt
index bcef4c0..d77b65b 100644
--- a/client/CMakeLists.txt
+++ b/client/CMakeLists.txt
@@ -10,11 +10,13 @@ include(../proto/include.cmake)
add_executable(main
main.cpp
+ rl.c
)
target_link_libraries(main
puzbus
mpack
+ readline # this is such a common library that I did not bother adding it as a submodule
)
diff --git a/client/examples/puzbus-hello-world.cpp b/client/examples/puzbus-hello-world.cpp
new file mode 100644
index 0000000..dcc965b
--- /dev/null
+++ b/client/examples/puzbus-hello-world.cpp
@@ -0,0 +1,67 @@
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <unistd.h>
+
+#include "puzbusv1.h"
+
+int send_message() {
+ const char* data = "Test message data!";
+ struct pb_msg output = {
+ .addr = 0x39,
+ .data = (char*) data,
+ .length = strlen(data),
+ };
+
+ char* packed;
+ size_t size;
+ if (!pb_write(&output, &packed, &size)) {
+ printf("error writing!\n");
+ return EXIT_FAILURE;
+ }
+
+ fwrite(packed, sizeof(packed[0]), size, stdout);
+ fflush(stdout);
+
+ return EXIT_SUCCESS;
+}
+
+int read_message() {
+ freopen(NULL, "rb", stdin); // allow binary on stdin
+ struct pb_msg input;
+
+ char buf[4]; // extremely small buffer to test chunked message parsing
+ size_t bytes = 0;
+
+ while ((bytes = fread(buf, sizeof(buf[0]), sizeof(buf), stdin)) > 0) {
+ int ret = pb_read(&input, buf, bytes);
+
+ // header read error
+ if (ret < 0) {
+ printf("error reading!\n");
+ return EXIT_FAILURE;
+ }
+
+ // continue reading if more bytes needed...
+ if (ret > 0) continue;
+
+ // message read completely!
+ printf("address: 0x%02x\n", input.addr);
+ printf("data: \"%.*s\"\n", input.length, input.data);
+ free(input.data);
+ return EXIT_SUCCESS;
+ }
+
+ // if we reach this point, data was read but it did not contain a complete
+ // message, and is thus considered a failure
+ return EXIT_FAILURE;
+}
+
+int main() {
+ if (!isatty(fileno(stdout))) return send_message();
+ if (!isatty(fileno(stdin))) return read_message();
+
+ printf("please pipe some data in or out to use this program\n");
+ return EXIT_SUCCESS;
+}
+
diff --git a/client/main.cpp b/client/main.cpp
index dcc965b..3d3a68c 100644
--- a/client/main.cpp
+++ b/client/main.cpp
@@ -1,67 +1,21 @@
#include <cstdio>
#include <cstdlib>
-#include <cstring>
-#include <unistd.h>
+#include <cstdint>
-#include "puzbusv1.h"
+#include "rl.h"
-int send_message() {
- const char* data = "Test message data!";
- struct pb_msg output = {
- .addr = 0x39,
- .data = (char*) data,
- .length = strlen(data),
- };
-
- char* packed;
- size_t size;
- if (!pb_write(&output, &packed, &size)) {
- printf("error writing!\n");
+int main(int argc, char** argv) {
+ if (argc < 2) {
+ printf("usage: %s addr [port]\n", argv[0]);
return EXIT_FAILURE;
}
- fwrite(packed, sizeof(packed[0]), size, stdout);
- fflush(stdout);
-
- return EXIT_SUCCESS;
-}
-
-int read_message() {
- freopen(NULL, "rb", stdin); // allow binary on stdin
- struct pb_msg input;
-
- char buf[4]; // extremely small buffer to test chunked message parsing
- size_t bytes = 0;
-
- while ((bytes = fread(buf, sizeof(buf[0]), sizeof(buf), stdin)) > 0) {
- int ret = pb_read(&input, buf, bytes);
-
- // header read error
- if (ret < 0) {
- printf("error reading!\n");
- return EXIT_FAILURE;
- }
-
- // continue reading if more bytes needed...
- if (ret > 0) continue;
-
- // message read completely!
- printf("address: 0x%02x\n", input.addr);
- printf("data: \"%.*s\"\n", input.length, input.data);
- free(input.data);
- return EXIT_SUCCESS;
- }
-
- // if we reach this point, data was read but it did not contain a complete
- // message, and is thus considered a failure
- return EXIT_FAILURE;
-}
+ // parse arguments
+ char* addr_str = argv[1];
+ uint16_t port = 9191;
+ if (argc >= 3) port = atoi(argv[2]);
-int main() {
- if (!isatty(fileno(stdout))) return send_message();
- if (!isatty(fileno(stdin))) return read_message();
-
- printf("please pipe some data in or out to use this program\n");
- return EXIT_SUCCESS;
+ // enter main CLI (using GNU readline for comfyness)
+ return cli_main();
}
diff --git a/client/rl.c b/client/rl.c
new file mode 100644
index 0000000..fb26057
--- /dev/null
+++ b/client/rl.c
@@ -0,0 +1,55 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdarg.h>
+
+#include <readline/readline.h>
+#include <readline/history.h>
+
+#include "rl.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);
+ rl_redisplay();
+
+ // printf
+ va_list args;
+ va_start(args, fmt);
+ vprintf(fmt, args);
+ va_end(args);
+
+ // restore line
+ rl_restore_prompt();
+ rl_replace_line(saved_line, 0);
+ rl_point = saved_point;
+ rl_end = saved_end;
+ rl_redisplay();
+
+ free(saved_line);
+}
+
+int cli_main() {
+ char* input = NULL;
+ while (1) {
+ if (input != NULL) free(input);
+ input = readline(CLI_PROMPT);
+
+ // exit on ^D or ^C (EOF)
+ if (input == NULL) return EXIT_SUCCESS;
+
+ // add non-empty line to history
+ if (*input) add_history(input);
+
+ if (strcmp(input, "exit") == 0) return EXIT_SUCCESS;
+ }
+
+ return EXIT_SUCCESS;
+}
+
diff --git a/client/rl.h b/client/rl.h
new file mode 100644
index 0000000..7eef4da
--- /dev/null
+++ b/client/rl.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#define COLOR_OFF "\x1B[0m"
+#define COLOR_BLUE "\x1B[0;94m"
+
+#define CLI_PROMPT COLOR_BLUE "pbc" COLOR_OFF "% "
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int cli_main();
+void rl_printf(const char *fmt, ...);
+
+#ifdef __cplusplus
+}
+#endif
+