]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net> | |
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 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <string.h> | |
15 | #include <pthread.h> | |
16 | #include <unistd.h> | |
17 | #include <readline/readline.h> | |
18 | #include <readline/history.h> | |
19 | #include "proxusb.h" | |
20 | #include "proxmark3.h" | |
21 | #include "proxgui.h" | |
22 | #include "cmdmain.h" | |
23 | ||
24 | struct usb_receiver_arg | |
25 | { | |
26 | int run; | |
27 | }; | |
28 | ||
29 | struct main_loop_arg | |
30 | { | |
31 | int usb_present; | |
32 | char *script_cmds_file; | |
33 | }; | |
34 | ||
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)) { | |
42 | UsbCommandReceived(&cmdbuf); | |
43 | fflush(NULL); | |
44 | } | |
45 | } | |
46 | ||
47 | pthread_exit(NULL); | |
48 | return NULL; | |
49 | } | |
50 | ||
51 | static void *main_loop(void *targ) | |
52 | { | |
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 | } | |
74 | ||
75 | read_history(".history"); | |
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 | ||
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) { | |
114 | break; | |
115 | } | |
116 | ||
117 | CommandReceived(cmd); | |
118 | add_history(cmd); | |
119 | } | |
120 | free(cmd); | |
121 | } else { | |
122 | printf("\n"); | |
123 | break; | |
124 | } | |
125 | } | |
126 | ||
127 | write_history(".history"); | |
128 | ||
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 | } | |
139 | ||
140 | ExitGraphics(); | |
141 | pthread_exit(NULL); | |
142 | return NULL; | |
143 | } | |
144 | ||
145 | int main(int argc, char **argv) | |
146 | { | |
147 | // Make sure to initialize | |
148 | struct main_loop_arg marg = { | |
149 | .usb_present = 0, | |
150 | .script_cmds_file = NULL | |
151 | }; | |
152 | pthread_t main_loop_t; | |
153 | usb_init(); | |
154 | ||
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 | ||
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; | |
181 | } |