summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2024-11-01 20:48:43 +0100
committerGravatar Nikias Bassen2024-11-01 20:48:43 +0100
commit24818b238155dc7797f14b471b63c789e6f3bb1a (patch)
tree682c8d684e7322c513e6eed535b5e2f5f2e6ea85
parent958b108ebf9653a389981676dcfb531e4b596af2 (diff)
downloadlibirecovery-24818b238155dc7797f14b471b63c789e6f3bb1a.tar.gz
libirecovery-24818b238155dc7797f14b471b63c789e6f3bb1a.tar.bz2
Allow building without readline support for irecovery tool
-rw-r--r--configure.ac11
-rw-r--r--tools/irecovery.c57
2 files changed, 60 insertions, 8 deletions
diff --git a/configure.ac b/configure.ac
index 06a27d0..4507e85 100644
--- a/configure.ac
+++ b/configure.ac
@@ -96,16 +96,17 @@ if test "$ac_cv_attribute_constructor" = "yes"; then
fi
AC_ARG_WITH([tools],
- [AS_HELP_STRING([--with-tools], [Build irecovery tools. (requires readline) [default=yes]])],
+ [AS_HELP_STRING([--with-tools], [Build irecovery tools. [default=yes]])],
[],
[with_tools=yes])
AS_IF([test "x$with_tools" = "xyes"], [
AC_DEFINE(BUILD_TOOLS, 1, [Define if we are building irecovery tools])
- AC_CHECK_HEADERS([readline/readline.h], [],
- [AC_MSG_ERROR([Please install readline development headers])]
- )]
-)
+ AC_CHECK_HEADERS([readline/readline.h],
+ [AC_DEFINE(HAVE_READLINE, 1, [Define if readline is available])],
+ [AC_MSG_NOTICE([NOTE: Building without readline support. If you want readline support, install its development package.])]
+ )
+])
AM_CONDITIONAL(BUILD_TOOLS, test "x$with_tools" = "xyes")
AC_ARG_WITH([dummy],
diff --git a/tools/irecovery.c b/tools/irecovery.c
index 61d053a..f527c44 100644
--- a/tools/irecovery.c
+++ b/tools/irecovery.c
@@ -31,12 +31,20 @@
#include <string.h>
#include <getopt.h>
#include <inttypes.h>
+#include <ctype.h>
#include <libirecovery.h>
+#ifdef HAVE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
+#else
+#ifndef WIN32
+#include <termios.h>
+#endif
+#endif
#ifdef WIN32
#include <windows.h>
+#include <conio.h>
#ifndef sleep
#define sleep(n) Sleep(1000 * n)
#endif
@@ -271,15 +279,49 @@ static void parse_command(irecv_client_t client, unsigned char* command, unsigne
static void load_command_history()
{
+#ifdef HAVE_READLINE
read_history(FILE_HISTORY_PATH);
+#endif
}
-static void append_command_to_history(char* cmd)
+static void append_command_to_history(const char* cmd)
{
+#ifdef HAVE_READLINE
add_history(cmd);
write_history(FILE_HISTORY_PATH);
+#endif
}
+#ifndef HAVE_READLINE
+#ifdef WIN32
+#define BS_CC '\b'
+#else
+#define BS_CC 0x7f
+#define getch getchar
+#endif
+static void get_input(char *buf, int maxlen)
+{
+ int len = 0;
+ int c;
+
+ while ((c = getch())) {
+ if ((c == '\r') || (c == '\n')) {
+ break;
+ }
+ if (isprint(c)) {
+ if (len < maxlen-1)
+ buf[len++] = c;
+ } else if (c == BS_CC) {
+ if (len > 0) {
+ fputs("\b \b", stdout);
+ len--;
+ }
+ }
+ }
+ buf[len] = 0;
+}
+#endif
+
static void init_shell(irecv_client_t client)
{
irecv_error_t error = 0;
@@ -294,8 +336,15 @@ static void init_shell(irecv_client_t client)
debug("%s\n", irecv_strerror(error));
break;
}
-
+#ifdef HAVE_READLINE
char* cmd = readline("> ");
+#else
+ char cmdbuf[4096];
+ const char* cmd = &cmdbuf[0];
+ printf("> ");
+ fflush(stdout);
+ get_input(cmdbuf, sizeof(cmdbuf));
+#endif
if (cmd && *cmd) {
if (_is_breq_command(cmd)) {
error = irecv_send_command_breq(client, cmd, 1);
@@ -307,8 +356,10 @@ static void init_shell(irecv_client_t client)
}
append_command_to_history(cmd);
- free(cmd);
}
+#ifdef HAVE_READLINE
+ free(cmd);
+#endif
}
}