diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/irecovery.c | 57 |
1 files changed, 54 insertions, 3 deletions
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 | ||
| 272 | static void load_command_history() | 280 | static 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 | ||
| 277 | static void append_command_to_history(char* cmd) | 287 | static 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 | ||
| 302 | static 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 | |||
| 283 | static void init_shell(irecv_client_t client) | 325 | static 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 | ||
