From d6db63a7b78966156b3d0b95824d9800aacb36f4 Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Sat, 28 Aug 2021 13:56:23 +0600 Subject: [PATCH] Use libedit for monitor when available --- src/unix/unix.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/unix/unix.c b/src/unix/unix.c index 35bb22f3b..bc5ea3b86 100644 --- a/src/unix/unix.c +++ b/src/unix/unix.c @@ -708,6 +708,14 @@ bool process_media_commands_3(uint8_t* id, char* fn, uint8_t* wp, int cmdargc) || fn[strlen(fn) - 1] == '"') fn[strlen(fn) - 1] = '\0'; return err; } +char* (*f_readline)(const char*) = NULL; +int (*f_add_history)(const char *) = NULL; + +#ifdef __APPLE__ +#define LIBEDIT_LIBRARY "libedit.dylib" +#else +#define LIBEDIT_LIBRARY "libedit.so" +#endif void monitor_thread(void* param) { @@ -715,12 +723,28 @@ void monitor_thread(void* param) { char* line = NULL; size_t n; + void* libedithandle = dlopen(LIBEDIT_LIBRARY, RTLD_LOCAL | RTLD_LAZY); + if (libedithandle) + { + f_readline = dlsym(libedithandle, "readline"); + f_add_history = dlsym(libedithandle, "add_history"); + if (!f_readline) + { + fprintf(stderr, "readline in libedit not found, line editing will be limited.\n"); + } + } + else fprintf(stderr, "libedit not found, line editing will be limited.\n"); printf("86Box monitor console.\n"); while (!exit_event) { if (feof(stdin)) break; - printf("(86Box) "); - getline(&line, &n, stdin); + if (f_readline) + line = f_readline("(86Box) "); + else + { + printf("(86Box) "); + getline(&line, &n, stdin); + } if (line) { int cmdargc = 0; @@ -733,6 +757,7 @@ void monitor_thread(void* param) line = NULL; continue; } + if (f_add_history) f_add_history(line); memset(xargv, 0, sizeof(xargv)); while(1) {