]>
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 | // UI utilities | |
10 | //----------------------------------------------------------------------------- | |
11 | ||
7fe9b0b7 | 12 | #include <stdarg.h> |
51969283 | 13 | #include <stdlib.h> |
7fe9b0b7 | 14 | #include <stdio.h> |
0f321d63 | 15 | #include <stdbool.h> |
51969283 | 16 | #include <readline/readline.h> |
9492e0b0 | 17 | #include <pthread.h> |
7fe9b0b7 | 18 | |
19 | #include "ui.h" | |
20 | ||
3fe71039 | 21 | double CursorScaleFactor = 1; |
b8fdac9e | 22 | int PlotGridX=0, PlotGridY=0, PlotGridXdefault= 64, PlotGridYdefault= 64, CursorCPos= 0, CursorDPos= 0; |
7fe9b0b7 | 23 | int offline; |
afdcb8c1 | 24 | bool flushAfterWrite = false; //buzzy |
0f321d63 | 25 | int GridOffset = 0; |
3fe71039 | 26 | bool GridLocked = false; |
b8fdac9e | 27 | bool showDemod = true; |
0f321d63 | 28 | |
9492e0b0 | 29 | extern pthread_mutex_t print_lock; |
30 | ||
7fe9b0b7 | 31 | static char *logfilename = "proxmark3.log"; |
32 | ||
33 | void PrintAndLog(char *fmt, ...) | |
34 | { | |
51969283 M |
35 | char *saved_line; |
36 | int saved_point; | |
9492e0b0 | 37 | va_list argptr, argptr2; |
38 | static FILE *logfile = NULL; | |
39 | static int logging=1; | |
7fe9b0b7 | 40 | |
acf0582d | 41 | // lock this section to avoid interlacing prints from different threads |
9492e0b0 | 42 | pthread_mutex_lock(&print_lock); |
43 | ||
44 | if (logging && !logfile) { | |
45 | logfile=fopen(logfilename, "a"); | |
46 | if (!logfile) { | |
47 | fprintf(stderr, "Can't open logfile, logging disabled!\n"); | |
48 | logging=0; | |
49 | } | |
50 | } | |
ed50f7f3 MF |
51 | |
52 | #ifdef RL_STATE_READCMD | |
53 | // We are using GNU readline. | |
51969283 | 54 | int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0; |
7fe9b0b7 | 55 | |
51969283 M |
56 | if (need_hack) { |
57 | saved_point = rl_point; | |
58 | saved_line = rl_copy_text(0, rl_end); | |
59 | rl_save_prompt(); | |
60 | rl_replace_line("", 0); | |
61 | rl_redisplay(); | |
62 | } | |
ed50f7f3 MF |
63 | #else |
64 | // We are using libedit (OSX), which doesn't support this flag. | |
ed50f7f3 | 65 | #endif |
51969283 | 66 | |
9492e0b0 | 67 | va_start(argptr, fmt); |
68 | va_copy(argptr2, argptr); | |
69 | vprintf(fmt, argptr); | |
70 | printf(" "); // cleaning prompt | |
71 | va_end(argptr); | |
72 | printf("\n"); | |
51969283 | 73 | |
afdcb8c1 MF |
74 | // This needs to be wrapped in ifdefs, as this if optimisation is disabled, |
75 | // this block won't be removed, and it'll fail at the linker. | |
76 | #ifdef RL_STATE_READCMD | |
51969283 M |
77 | if (need_hack) { |
78 | rl_restore_prompt(); | |
79 | rl_replace_line(saved_line, 0); | |
80 | rl_point = saved_point; | |
81 | rl_redisplay(); | |
82 | free(saved_line); | |
83 | } | |
afdcb8c1 | 84 | #endif |
51969283 | 85 | |
9492e0b0 | 86 | if (logging && logfile) { |
87 | vfprintf(logfile, fmt, argptr2); | |
88 | fprintf(logfile,"\n"); | |
89 | fflush(logfile); | |
90 | } | |
91 | va_end(argptr2); | |
92 | ||
ed77aabe | 93 | if (flushAfterWrite == 1) //buzzy |
94 | { | |
95 | fflush(NULL); | |
96 | } | |
9492e0b0 | 97 | //release lock |
98 | pthread_mutex_unlock(&print_lock); | |
7fe9b0b7 | 99 | } |
100 | ||
9492e0b0 | 101 | |
7fe9b0b7 | 102 | void SetLogFilename(char *fn) |
103 | { | |
104 | logfilename = fn; | |
105 | } | |
afdcb8c1 MF |
106 | |
107 | void SetFlushAfterWrite(bool flush_after_write) { | |
108 | flushAfterWrite = flush_after_write; | |
109 | } | |
110 |