summaryrefslogtreecommitdiffstats
path: root/src/irecovery.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/irecovery.c')
-rw-r--r--src/irecovery.c92
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
26enum { 28enum {
27 kResetDevice, kStartShell, kSendCommand, kSendFile 29 kResetDevice, kStartShell, kSendCommand, kSendFile
28}; 30};
29 31
32static unsigned int exit_shell = 0;
33
30void print_shell_usage() { 34void 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
37void init_shell(irecv_device* device) { 41void 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(;;) { 59int 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
67int 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
75void load_command_history() {
76 read_history(FILE_HISTORY_PATH);
77}
78
79void append_command_to_history(char* cmd) {
80 add_history(cmd);
81 write_history(FILE_HISTORY_PATH);
82}
83
84void 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
79int main(int argc, char** argv) { 111int 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");