]>
Commit | Line | Data |
---|---|---|
a553f267 | 1 | //----------------------------------------------------------------------------- |
212ef3a0 | 2 | // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net> |
a553f267 | 3 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> |
4 | // | |
5 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
6 | // at your option, any later version. See the LICENSE.txt file for the text of | |
7 | // the license. | |
8 | //----------------------------------------------------------------------------- | |
9 | // Main binary | |
10 | //----------------------------------------------------------------------------- | |
11 | ||
6658905f | 12 | #include <stdio.h> |
590f8ff9 | 13 | #include <stdlib.h> |
6658905f | 14 | #include <string.h> |
7fe9b0b7 | 15 | #include <pthread.h> |
8556b852 | 16 | #include <unistd.h> |
6658905f | 17 | #include <readline/readline.h> |
18 | #include <readline/history.h> | |
7fe9b0b7 | 19 | #include "proxusb.h" |
6658905f | 20 | #include "proxmark3.h" |
21 | #include "proxgui.h" | |
7fe9b0b7 | 22 | #include "cmdmain.h" |
6658905f | 23 | |
7fe9b0b7 | 24 | struct usb_receiver_arg |
25 | { | |
26 | int run; | |
6658905f | 27 | }; |
28 | ||
7fe9b0b7 | 29 | struct main_loop_arg |
30 | { | |
31 | int usb_present; | |
1f947c4b | 32 | char *script_cmds_file; |
a60612db | 33 | }; |
34 | ||
7fe9b0b7 | 35 | static void *usb_receiver(void *targ) |
36 | { | |
37 | struct usb_receiver_arg *arg = (struct usb_receiver_arg*)targ; | |
38 | UsbCommand cmdbuf; | |
39 | ||
40 | while (arg->run) { | |
41 | if (ReceiveCommandPoll(&cmdbuf)) { | |
7fe9b0b7 | 42 | UsbCommandReceived(&cmdbuf); |
7fe9b0b7 | 43 | fflush(NULL); |
44 | } | |
45 | } | |
46 | ||
47 | pthread_exit(NULL); | |
4cd41f34 | 48 | return NULL; |
6658905f | 49 | } |
50 | ||
51 | static void *main_loop(void *targ) | |
52 | { | |
1f947c4b | 53 | struct main_loop_arg *arg = (struct main_loop_arg*)targ; |
54 | struct usb_receiver_arg rarg; | |
55 | char *cmd = NULL; | |
56 | pthread_t reader_thread; | |
57 | ||
58 | if (arg->usb_present == 1) { | |
59 | rarg.run=1; | |
60 | pthread_create(&reader_thread, NULL, &usb_receiver, &rarg); | |
61 | } | |
62 | ||
63 | FILE *script_file = NULL; | |
64 | char script_cmd_buf[256]; | |
65 | ||
66 | if (arg->script_cmds_file) | |
67 | { | |
68 | script_file = fopen(arg->script_cmds_file, "r"); | |
69 | if (script_file) | |
70 | { | |
71 | printf("using 'scripting' commands file %s\n", arg->script_cmds_file); | |
72 | } | |
73 | } | |
7fe9b0b7 | 74 | |
8556b852 | 75 | read_history(".history"); |
1f947c4b | 76 | while(1) |
77 | { | |
78 | // If there is a script file | |
79 | if (script_file) | |
80 | { | |
81 | if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file)) | |
82 | { | |
83 | fclose(script_file); | |
84 | script_file = NULL; | |
85 | } | |
86 | else | |
87 | { | |
88 | char *nl; | |
89 | nl = strrchr(script_cmd_buf, '\r'); | |
90 | if (nl) *nl = '\0'; | |
91 | nl = strrchr(script_cmd_buf, '\n'); | |
92 | if (nl) *nl = '\0'; | |
93 | ||
94 | if ((cmd = (char*) malloc(strlen(script_cmd_buf))) != NULL) | |
95 | { | |
96 | memset(cmd, 0, strlen(script_cmd_buf)); | |
97 | strcpy(cmd, script_cmd_buf); | |
98 | printf("%s\n", cmd); | |
99 | } | |
100 | } | |
101 | } | |
102 | ||
103 | if (!script_file) | |
104 | { | |
105 | cmd = readline(PROXPROMPT); | |
106 | } | |
107 | ||
8556b852 M |
108 | if (cmd) { |
109 | while(cmd[strlen(cmd) - 1] == ' ') | |
110 | cmd[strlen(cmd) - 1] = 0x00; | |
111 | ||
112 | if (cmd[0] != 0x00) { | |
113 | if (strncmp(cmd, "quit", 4) == 0) { | |
8556b852 M |
114 | break; |
115 | } | |
116 | ||
117 | CommandReceived(cmd); | |
118 | add_history(cmd); | |
119 | } | |
120 | free(cmd); | |
121 | } else { | |
122 | printf("\n"); | |
123 | break; | |
124 | } | |
125 | } | |
7fe9b0b7 | 126 | |
51969283 M |
127 | write_history(".history"); |
128 | ||
1f947c4b | 129 | if (arg->usb_present == 1) { |
130 | rarg.run = 0; | |
131 | pthread_join(reader_thread, NULL); | |
132 | } | |
133 | ||
134 | if (script_file) | |
135 | { | |
136 | fclose(script_file); | |
137 | script_file = NULL; | |
138 | } | |
7fe9b0b7 | 139 | |
1f947c4b | 140 | ExitGraphics(); |
141 | pthread_exit(NULL); | |
142 | return NULL; | |
6658905f | 143 | } |
144 | ||
145 | int main(int argc, char **argv) | |
146 | { | |
1f947c4b | 147 | // Make sure to initialize |
148 | struct main_loop_arg marg = { | |
149 | .usb_present = 0, | |
150 | .script_cmds_file = NULL | |
151 | }; | |
7fe9b0b7 | 152 | pthread_t main_loop_t; |
153 | usb_init(); | |
154 | ||
1f947c4b | 155 | // If the user passed the filename of the 'script' to execute, get it |
156 | if (argc > 1 && argv[1]) | |
157 | { | |
158 | marg.script_cmds_file = argv[1]; | |
159 | } | |
160 | ||
7fe9b0b7 | 161 | if (!OpenProxmark(1)) { |
162 | fprintf(stderr,"PROXMARK3: NOT FOUND!\n"); | |
163 | marg.usb_present = 0; | |
164 | offline = 1; | |
165 | } else { | |
166 | marg.usb_present = 1; | |
167 | offline = 0; | |
168 | } | |
169 | ||
170 | pthread_create(&main_loop_t, NULL, &main_loop, &marg); | |
171 | InitGraphics(argc, argv); | |
172 | ||
173 | MainGraphics(); | |
174 | ||
175 | pthread_join(main_loop_t, NULL); | |
176 | ||
177 | if (marg.usb_present == 1) { | |
178 | CloseProxmark(); | |
179 | } | |
180 | return 0; | |
6658905f | 181 | } |