summaryrefslogtreecommitdiffstats
path: root/tools/irecovery.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/irecovery.c')
-rw-r--r--tools/irecovery.c67
1 files changed, 60 insertions, 7 deletions
diff --git a/tools/irecovery.c b/tools/irecovery.c
index 61d053a..b293324 100644
--- a/tools/irecovery.c
+++ b/tools/irecovery.c
@@ -31,16 +31,22 @@
#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
+#ifdef _WIN32
#include <windows.h>
-#ifndef sleep
+#include <conio.h>
#define sleep(n) Sleep(1000 * n)
#endif
-#endif
#define FILE_HISTORY_PATH ".irecovery"
#define debug(...) if (verbose) fprintf(stderr, __VA_ARGS__)
@@ -271,14 +277,48 @@ 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)
{
@@ -294,8 +334,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 +354,10 @@ static void init_shell(irecv_client_t client)
}
append_command_to_history(cmd);
- free(cmd);
}
+#ifdef HAVE_READLINE
+ free(cmd);
+#endif
}
}
@@ -544,7 +593,11 @@ int main(int argc, char* argv[])
return 0;
case 'V':
- printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION);
+ printf("%s %s", TOOL_NAME, PACKAGE_VERSION);
+#ifdef HAVE_READLINE
+ printf(" (readline)");
+#endif
+ printf("\n");
return 0;
default: