]> git.zerfleddert.de Git - proxmark3-svn/blob - client/proxmark3.c
strip trailing space in a more strictly correct place
[proxmark3-svn] / client / proxmark3.c
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 <readline/readline.h>
17 #include <readline/history.h>
18 #include "proxusb.h"
19 #include "proxmark3.h"
20 #include "proxgui.h"
21 #include "cmdmain.h"
22
23 struct usb_receiver_arg
24 {
25 int run;
26 };
27
28 struct main_loop_arg
29 {
30 int usb_present;
31 };
32
33 static void *usb_receiver(void *targ)
34 {
35 struct usb_receiver_arg *arg = (struct usb_receiver_arg*)targ;
36 UsbCommand cmdbuf;
37
38 while (arg->run) {
39 if (ReceiveCommandPoll(&cmdbuf)) {
40 for (int i = 0; i < strlen(PROXPROMPT); i++)
41 putchar(0x08);
42 UsbCommandReceived(&cmdbuf);
43 printf(PROXPROMPT);
44 fflush(NULL);
45 }
46 }
47
48 pthread_exit(NULL);
49 return NULL;
50 }
51
52 static void *main_loop(void *targ)
53 {
54 struct main_loop_arg *arg = (struct main_loop_arg*)targ;
55 struct usb_receiver_arg rarg;
56 char *cmd = NULL;
57 pthread_t reader_thread;
58
59 if (arg->usb_present == 1) {
60 rarg.run=1;
61 pthread_create(&reader_thread, NULL, &usb_receiver, &rarg);
62 }
63
64 while(1) {
65 cmd = readline(PROXPROMPT);
66 if (cmd) {
67 while(cmd[strlen(cmd) - 1] == ' ')
68 cmd[strlen(cmd) - 1] = 0x00;
69 if (cmd[0] != 0x00) {
70 CommandReceived(cmd);
71 add_history(cmd);
72 }
73 free(cmd);
74 } else {
75 printf("\n");
76 break;
77 }
78 }
79
80 if (arg->usb_present == 1) {
81 rarg.run = 0;
82 pthread_join(reader_thread, NULL);
83 }
84
85 ExitGraphics();
86 pthread_exit(NULL);
87 return NULL;
88 }
89
90 int main(int argc, char **argv)
91 {
92 struct main_loop_arg marg;
93 pthread_t main_loop_t;
94 usb_init();
95
96 if (!OpenProxmark(1)) {
97 fprintf(stderr,"PROXMARK3: NOT FOUND!\n");
98 marg.usb_present = 0;
99 offline = 1;
100 } else {
101 marg.usb_present = 1;
102 offline = 0;
103 }
104
105 pthread_create(&main_loop_t, NULL, &main_loop, &marg);
106 InitGraphics(argc, argv);
107
108 MainGraphics();
109
110 pthread_join(main_loop_t, NULL);
111
112 if (marg.usb_present == 1) {
113 CloseProxmark();
114 }
115 return 0;
116 }
Impressum, Datenschutz