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
96fi 96fi
97 97
98AC_ARG_WITH([tools], 98AC_ARG_WITH([tools],
99 [AS_HELP_STRING([--with-tools], [Build irecovery tools. (requires readline) [default=yes]])], 99 [AS_HELP_STRING([--with-tools], [Build irecovery tools. [default=yes]])],
100 [], 100 [],
101 [with_tools=yes]) 101 [with_tools=yes])
102 102
103AS_IF([test "x$with_tools" = "xyes"], [ 103AS_IF([test "x$with_tools" = "xyes"], [
104 AC_DEFINE(BUILD_TOOLS, 1, [Define if we are building irecovery tools]) 104 AC_DEFINE(BUILD_TOOLS, 1, [Define if we are building irecovery tools])
105 AC_CHECK_HEADERS([readline/readline.h], [], 105 AC_CHECK_HEADERS([readline/readline.h],
106 [AC_MSG_ERROR([Please install readline development headers])] 106 [AC_DEFINE(HAVE_READLINE, 1, [Define if readline is available])],
107 )] 107 [AC_MSG_NOTICE([NOTE: Building without readline support. If you want readline support, install its development package.])]
108) 108 )
109])
109AM_CONDITIONAL(BUILD_TOOLS, test "x$with_tools" = "xyes") 110AM_CONDITIONAL(BUILD_TOOLS, test "x$with_tools" = "xyes")
110 111
111AC_ARG_WITH([dummy], 112AC_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 @@
31#include <string.h> 31#include <string.h>
32#include <getopt.h> 32#include <getopt.h>
33#include <inttypes.h> 33#include <inttypes.h>
34#include <ctype.h>
34#include <libirecovery.h> 35#include <libirecovery.h>
36#ifdef HAVE_READLINE
35#include <readline/readline.h> 37#include <readline/readline.h>
36#include <readline/history.h> 38#include <readline/history.h>
39#else
40#ifndef WIN32
41#include <termios.h>
42#endif
43#endif
37 44
38#ifdef WIN32 45#ifdef WIN32
39#include <windows.h> 46#include <windows.h>
47#include <conio.h>
40#ifndef sleep 48#ifndef sleep
41#define sleep(n) Sleep(1000 * n) 49#define sleep(n) Sleep(1000 * n)
42#endif 50#endif
@@ -271,15 +279,49 @@ static void parse_command(irecv_client_t client, unsigned char* command, unsigne
271 279
272static void load_command_history() 280static void load_command_history()
273{ 281{
282#ifdef HAVE_READLINE
274 read_history(FILE_HISTORY_PATH); 283 read_history(FILE_HISTORY_PATH);
284#endif
275} 285}
276 286
277static void append_command_to_history(char* cmd) 287static void append_command_to_history(const char* cmd)
278{ 288{
289#ifdef HAVE_READLINE
279 add_history(cmd); 290 add_history(cmd);
280 write_history(FILE_HISTORY_PATH); 291 write_history(FILE_HISTORY_PATH);
292#endif
281} 293}
282 294
295#ifndef HAVE_READLINE
296#ifdef WIN32
297#define BS_CC '\b'
298#else
299#define BS_CC 0x7f
300#define getch getchar
301#endif
302static void get_input(char *buf, int maxlen)
303{
304 int len = 0;
305 int c;
306
307 while ((c = getch())) {
308 if ((c == '\r') || (c == '\n')) {
309 break;
310 }
311 if (isprint(c)) {
312 if (len < maxlen-1)
313 buf[len++] = c;
314 } else if (c == BS_CC) {
315 if (len > 0) {
316 fputs("\b \b", stdout);
317 len--;
318 }
319 }
320 }
321 buf[len] = 0;
322}
323#endif
324
283static void init_shell(irecv_client_t client) 325static void init_shell(irecv_client_t client)
284{ 326{
285 irecv_error_t error = 0; 327 irecv_error_t error = 0;
@@ -294,8 +336,15 @@ static void init_shell(irecv_client_t client)
294 debug("%s\n", irecv_strerror(error)); 336 debug("%s\n", irecv_strerror(error));
295 break; 337 break;
296 } 338 }
297 339#ifdef HAVE_READLINE
298 char* cmd = readline("> "); 340 char* cmd = readline("> ");
341#else
342 char cmdbuf[4096];
343 const char* cmd = &cmdbuf[0];
344 printf("> ");
345 fflush(stdout);
346 get_input(cmdbuf, sizeof(cmdbuf));
347#endif
299 if (cmd && *cmd) { 348 if (cmd && *cmd) {
300 if (_is_breq_command(cmd)) { 349 if (_is_breq_command(cmd)) {
301 error = irecv_send_command_breq(client, cmd, 1); 350 error = irecv_send_command_breq(client, cmd, 1);
@@ -307,8 +356,10 @@ static void init_shell(irecv_client_t client)
307 } 356 }
308 357
309 append_command_to_history(cmd); 358 append_command_to_history(cmd);
310 free(cmd);
311 } 359 }
360#ifdef HAVE_READLINE
361 free(cmd);
362#endif
312 } 363 }
313} 364}
314 365