]> git.zerfleddert.de Git - proxmark3-svn/blob - client/ui.c
dc122d2f17acc04cd405d093dc4bb540e83c7355
[proxmark3-svn] / client / ui.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 // 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 // Declared in proxmark3.c
35 extern pthread_mutex_t print_lock;
36
37 void PrintAndLog(char *fmt, ...)
38 {
39 char *saved_line;
40 int saved_point;
41 va_list argptr, argptr2;
42 static FILE *logfile = NULL;
43 static int logging=1;
44
45 // lock this section to avoid interlacing prints from different threads
46 pthread_mutex_lock(&print_lock);
47
48 if (logging && !logfile) {
49 logfile=fopen(logfilename, "a");
50 if (!logfile) {
51 fprintf(stderr, "Can't open logfile, logging disabled!\n");
52 logging=0;
53 }
54 }
55
56 // If there is an incoming message from the hardware (eg: lf hid read) in
57 // the background (while the prompt is displayed and accepting user input),
58 // stash the prompt and bring it back later.
59 #ifdef RL_STATE_READCMD
60 // We are using GNU readline. libedit (OSX) doesn't support this flag.
61 int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
62
63 if (need_hack) {
64 saved_point = rl_point;
65 saved_line = rl_copy_text(0, rl_end);
66 rl_save_prompt();
67 rl_replace_line("", 0);
68 rl_redisplay();
69 }
70 #endif
71
72 va_start(argptr, fmt);
73 va_copy(argptr2, argptr);
74 vprintf(fmt, argptr);
75 printf(" "); // cleaning prompt
76 va_end(argptr);
77 printf("\n");
78
79 #ifdef RL_STATE_READCMD
80 // We are using GNU readline. libedit (OSX) doesn't support this flag.
81 if (need_hack) {
82 rl_restore_prompt();
83 rl_replace_line(saved_line, 0);
84 rl_point = saved_point;
85 rl_redisplay();
86 free(saved_line);
87 }
88 #endif
89
90 if (logging && logfile) {
91 vfprintf(logfile, fmt, argptr2);
92 fprintf(logfile,"\n");
93 fflush(logfile);
94 }
95 va_end(argptr2);
96
97 if (flushAfterWrite == 1) //buzzy
98 {
99 fflush(NULL);
100 }
101 //release lock
102 pthread_mutex_unlock(&print_lock);
103 }
104 #endif
105
106 void SetLogFilename(char *fn)
107 {
108 logfilename = fn;
109 }
Impressum, Datenschutz