diff options
| author | 2010-05-16 12:45:00 -0400 | |
|---|---|---|
| committer | 2010-05-16 12:45:00 -0400 | |
| commit | e7cc5716d941ee2c1ec554926e76448092d9e0c5 (patch) | |
| tree | a55f73699d385670b0f1106e6378fc2ac3a554b6 /src/irecovery.c | |
| parent | ebaf0a72d826a4c8f09d965cd2863d1848a999db (diff) | |
| download | libirecovery-e7cc5716d941ee2c1ec554926e76448092d9e0c5.tar.gz libirecovery-e7cc5716d941ee2c1ec554926e76448092d9e0c5.tar.bz2 | |
Added send and receive callbacks in libirecovery and added history saving into irecovery.c
Diffstat (limited to 'src/irecovery.c')
| -rw-r--r-- | src/irecovery.c | 92 |
1 files changed, 63 insertions, 29 deletions
diff --git a/src/irecovery.c b/src/irecovery.c index ae828af..2ab0aaa 100644 --- a/src/irecovery.c +++ b/src/irecovery.c | |||
| @@ -23,44 +23,76 @@ | |||
| 23 | #include <readline/readline.h> | 23 | #include <readline/readline.h> |
| 24 | #include <readline/history.h> | 24 | #include <readline/history.h> |
| 25 | 25 | ||
| 26 | #define FILE_HISTORY_PATH "~/.irecovery/history" | ||
| 27 | |||
| 26 | enum { | 28 | enum { |
| 27 | kResetDevice, kStartShell, kSendCommand, kSendFile | 29 | kResetDevice, kStartShell, kSendCommand, kSendFile |
| 28 | }; | 30 | }; |
| 29 | 31 | ||
| 32 | static unsigned int exit_shell = 0; | ||
| 33 | |||
| 30 | void print_shell_usage() { | 34 | void print_shell_usage() { |
| 31 | printf("Usage:\n"); | 35 | printf("Usage:\n"); |
| 32 | printf("\t:f <file>\tSend file to device.\n"); | 36 | printf("\t/upload <file>\tSend file to device.\n"); |
| 33 | printf("\t:h\t\tShow this help.\n"); | 37 | printf("\t/help\t\tShow this help.\n"); |
| 34 | printf("\t:q\t\tQuit interactive shell.\n"); | 38 | printf("\t/exit\t\tExit interactive shell.\n"); |
| 35 | } | 39 | } |
| 36 | 40 | ||
| 37 | void init_shell(irecv_device* device) { | 41 | void parse_command(irecv_device_t* device, unsigned char* command, unsigned int size) { |
| 38 | int ret; | 42 | char* cmd = strtok(command, " "); |
| 43 | if(!strcmp(command, "/exit")) { | ||
| 44 | exit_shell = 1; | ||
| 45 | } else | ||
| 46 | |||
| 47 | if(!strcmp(command, "/help")) { | ||
| 48 | print_shell_usage(); | ||
| 49 | } else | ||
| 50 | |||
| 51 | if(!strcmp(command, "/upload")) { | ||
| 52 | char* filename = strtok(0, " "); | ||
| 53 | if(filename != NULL) { | ||
| 54 | irecv_send_file(device, filename); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 39 | 58 | ||
| 40 | for(;;) { | 59 | int recv_callback(irecv_device_t* device, unsigned char* data, unsigned int size) { |
| 41 | char* cmd = readline("iRecovery> "); | 60 | int i = 0; |
| 42 | if(cmd && *cmd) { | 61 | for(i = 0; i < size; i++) { |
| 43 | add_history(cmd); | 62 | printf("%c", data[i]); |
| 44 | if(cmd[0] == ':') { | 63 | } |
| 45 | strtok(cmd, " "); | 64 | return size; |
| 46 | char* arg = strtok(0, " "); | 65 | } |
| 47 | |||
| 48 | if(cmd[1] == 'q') { | ||
| 49 | free(cmd); | ||
| 50 | break; | ||
| 51 | } else if(cmd[1] == 'h') { | ||
| 52 | print_shell_usage(); | ||
| 53 | } else if(cmd[1] == 'f') { | ||
| 54 | ret = irecv_send_file(device, arg); | ||
| 55 | // TODO: error messages | ||
| 56 | } | ||
| 57 | } else { | ||
| 58 | ret = irecv_send_command(device, cmd); | ||
| 59 | // TODO: error messages | ||
| 60 | } | ||
| 61 | 66 | ||
| 67 | int send_callback(irecv_device_t* device, unsigned char* command, unsigned int size) { | ||
| 68 | if(command[0] == '/') { | ||
| 69 | parse_command(device, command, size); | ||
| 70 | return 0; | ||
| 71 | } | ||
| 72 | return size; | ||
| 73 | } | ||
| 74 | |||
| 75 | void load_command_history() { | ||
| 76 | read_history(FILE_HISTORY_PATH); | ||
| 77 | } | ||
| 78 | |||
| 79 | void append_command_to_history(char* cmd) { | ||
| 80 | add_history(cmd); | ||
| 81 | write_history(FILE_HISTORY_PATH); | ||
| 82 | } | ||
| 83 | |||
| 84 | void init_shell(irecv_device_t* device) { | ||
| 85 | load_command_history(); | ||
| 86 | irecv_set_sender(device, &send_callback); | ||
| 87 | irecv_set_receiver(device, &recv_callback); | ||
| 88 | while(!exit_shell) { | ||
| 89 | char* cmd = readline("> "); | ||
| 90 | if(cmd && *cmd) { | ||
| 91 | irecv_send_command(device, cmd); | ||
| 92 | append_command_to_history(cmd); | ||
| 62 | free(cmd); | 93 | free(cmd); |
| 63 | } | 94 | } |
| 95 | irecv_update(device); | ||
| 64 | } | 96 | } |
| 65 | } | 97 | } |
| 66 | 98 | ||
| @@ -78,13 +110,14 @@ void print_usage() { | |||
| 78 | 110 | ||
| 79 | int main(int argc, char** argv) { | 111 | int main(int argc, char** argv) { |
| 80 | int opt = 0; | 112 | int opt = 0; |
| 113 | int debug = 0; | ||
| 81 | int action = 0; | 114 | int action = 0; |
| 82 | char* argument = NULL; | 115 | char* argument = NULL; |
| 83 | if(argc == 1) print_usage(); | 116 | if(argc == 1) print_usage(); |
| 84 | while ((opt = getopt(argc, argv, "dhrsc:f:")) > 0) { | 117 | while ((opt = getopt(argc, argv, "dhrsc:f:")) > 0) { |
| 85 | switch (opt) { | 118 | switch (opt) { |
| 86 | case 'd': | 119 | case 'd': |
| 87 | irecv_set_debug(1); | 120 | debug = 1; |
| 88 | break; | 121 | break; |
| 89 | 122 | ||
| 90 | case 'h': | 123 | case 'h': |
| @@ -115,11 +148,12 @@ int main(int argc, char** argv) { | |||
| 115 | } | 148 | } |
| 116 | } | 149 | } |
| 117 | 150 | ||
| 118 | irecv_device* device = NULL; | 151 | irecv_device_t* device = irecv_init(); |
| 119 | if(irecv_init(&device) < 0) { | 152 | if(device == NULL) { |
| 120 | fprintf(stderr, "Unable to initialize libirecovery\n"); | 153 | fprintf(stderr, "Unable to initialize libirecovery\n"); |
| 121 | return -1; | 154 | return -1; |
| 122 | } | 155 | } |
| 156 | if(debug) irecv_set_debug(device, 1); | ||
| 123 | 157 | ||
| 124 | if(irecv_open(device) < 0) { | 158 | if(irecv_open(device) < 0) { |
| 125 | fprintf(stderr, "Unable to open device\n"); | 159 | fprintf(stderr, "Unable to open device\n"); |
