]>
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 | // UI utilities | |
10 | //----------------------------------------------------------------------------- | |
11 | ||
12 | #include <stdbool.h> | |
13 | #ifndef EXTERNAL_PRINTANDLOG | |
14 | #include <stdlib.h> | |
15 | #include <stdio.h> | |
16 | #include <stdarg.h> | |
17 | #include <readline/readline.h> | |
18 | #include <pthread.h> | |
19 | #endif | |
20 | ||
21 | #include "ui.h" | |
22 | ||
23 | double CursorScaleFactor = 1; | |
24 | int PlotGridX=0, PlotGridY=0, PlotGridXdefault= 64, PlotGridYdefault= 64, CursorCPos= 0, CursorDPos= 0; | |
25 | int offline; | |
26 | int flushAfterWrite = 0; //buzzy | |
27 | int GridOffset = 0; | |
28 | bool GridLocked = false; | |
29 | bool showDemod = true; | |
30 | ||
31 | static char *logfilename = "proxmark3.log"; | |
32 | ||
33 | #ifndef EXTERNAL_PRINTANDLOG | |
34 | static pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER; | |
35 | ||
36 | void PrintAndLog(char *fmt, ...) | |
37 | { | |
38 | char *saved_line; | |
39 | int saved_point; | |
40 | va_list argptr, argptr2; | |
41 | static FILE *logfile = NULL; | |
42 | static int logging=1; | |
43 | ||
44 | // lock this section to avoid interlacing prints from different threads | |
45 | pthread_mutex_lock(&print_lock); | |
46 | ||
47 | if (logging && !logfile) { | |
48 | logfile=fopen(logfilename, "a"); | |
49 | if (!logfile) { | |
50 | fprintf(stderr, "Can't open logfile, logging disabled!\n"); | |
51 | logging=0; | |
52 | } | |
53 | } | |
54 | ||
55 | // If there is an incoming message from the hardware (eg: lf hid read) in | |
56 | // the background (while the prompt is displayed and accepting user input), | |
57 | // stash the prompt and bring it back later. | |
58 | #ifdef RL_STATE_READCMD | |
59 | // We are using GNU readline. libedit (OSX) doesn't support this flag. | |
60 | int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0; | |
61 | ||
62 | if (need_hack) { | |
63 | saved_point = rl_point; | |
64 | saved_line = rl_copy_text(0, rl_end); | |
65 | rl_save_prompt(); | |
66 | rl_replace_line("", 0); | |
67 | rl_redisplay(); | |
68 | } | |
69 | #endif | |
70 | ||
71 | va_start(argptr, fmt); | |
72 | va_copy(argptr2, argptr); | |
73 | vprintf(fmt, argptr); | |
74 | printf(" "); // cleaning prompt | |
75 | va_end(argptr); | |
76 | printf("\n"); | |
77 | ||
78 | #ifdef RL_STATE_READCMD | |
79 | // We are using GNU readline. libedit (OSX) doesn't support this flag. | |
80 | if (need_hack) { | |
81 | rl_restore_prompt(); | |
82 | rl_replace_line(saved_line, 0); | |
83 | rl_point = saved_point; | |
84 | rl_redisplay(); | |
85 | free(saved_line); | |
86 | } | |
87 | #endif | |
88 | ||
89 | if (logging && logfile) { | |
90 | vfprintf(logfile, fmt, argptr2); | |
91 | fprintf(logfile,"\n"); | |
92 | fflush(logfile); | |
93 | } | |
94 | va_end(argptr2); | |
95 | ||
96 | if (flushAfterWrite == 1) //buzzy | |
97 | { | |
98 | fflush(NULL); | |
99 | } | |
100 | //release lock | |
101 | pthread_mutex_unlock(&print_lock); | |
102 | } | |
103 | #endif | |
104 | ||
105 | void SetLogFilename(char *fn) | |
106 | { | |
107 | logfilename = fn; | |
108 | } |