aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client/i2c.cpp3
-rw-r--r--client/rl.cpp31
-rw-r--r--client/rl.h5
-rw-r--r--client/sock.cpp2
4 files changed, 28 insertions, 13 deletions
diff --git a/client/i2c.cpp b/client/i2c.cpp
index 3655191..2a5e67a 100644
--- a/client/i2c.cpp
+++ b/client/i2c.cpp
@@ -2,6 +2,7 @@
#include <stdlib.h>
#include "i2ctcpv1.h"
+#include "rl.h"
#include "sock.h"
#include "xxd.h"
@@ -37,8 +38,10 @@ static void i2c_handle_cmd_read(uint16_t, const char *, size_t);
void i2c_recv(const char * data, size_t data_size) {
if (i2c_dump_recv) {
+ _rl_printf_start();
printf("[%s] data(0x%02lx):\n", __FUNCTION__, data_size);
xxd(data, data_size);
+ _rl_printf_stop();
}
}
diff --git a/client/rl.cpp b/client/rl.cpp
index 2e74e5f..493f753 100644
--- a/client/rl.cpp
+++ b/client/rl.cpp
@@ -10,24 +10,22 @@
#include "cmd.h"
#include "parse.h"
-void rl_printf(const char *fmt, ...) {
+static char* saved_line;
+static int saved_point, saved_end;
+
+void _rl_printf_start() {
// save line
- char* saved_line = rl_copy_text(0, rl_end);
- int saved_point = rl_point;
- int saved_end = rl_end;
+ saved_line = rl_copy_text(0, rl_end);
+ saved_point = rl_point;
+ 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
+void _rl_printf_stop() {
rl_restore_prompt();
rl_replace_line(saved_line, 0);
rl_point = saved_point;
@@ -37,6 +35,17 @@ void rl_printf(const char *fmt, ...) {
free(saved_line);
}
+void rl_printf(const char *fmt, ...) {
+ _rl_printf_start();
+
+ va_list args;
+ va_start(args, fmt);
+ vprintf(fmt, args);
+ va_end(args);
+
+ _rl_printf_stop();
+}
+
static void cli_cmd(char* cmd) {
cmd += strspn(cmd, IFS); // skip leading whitespace
char* line = consume_token(cmd, IFS);
diff --git a/client/rl.h b/client/rl.h
index d2f8612..4a5b2c1 100644
--- a/client/rl.h
+++ b/client/rl.h
@@ -27,6 +27,11 @@ int cli_main();
* completes.
*/
void rl_printf(const char * fmt, ...);
+//! Store line for printing without disrupting readline prompt
+void _rl_printf_start();
+//! Restore line stored by \c _rl_printf_start()
+void _rl_printf_stop();
+
/**
* \brief Get the index of the word currently under the cursor
*
diff --git a/client/sock.cpp b/client/sock.cpp
index e33a3dc..c292d6a 100644
--- a/client/sock.cpp
+++ b/client/sock.cpp
@@ -79,8 +79,6 @@ void PBSocket::sock_task() {
char buf[80];
ssize_t bytes = read(_fd, buf, sizeof(buf));
- rl_printf("%.*s", bytes, buf);
- continue;
if (bytes == -1) {
rl_printf("error: %s (%d)\n", strerror(errno), errno);
break;