]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/ui.c
Merge pull request #969 from pwpiwi/gcc10_fixes
[proxmark3-svn] / client / ui.c
... / ...
CommitLineData
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 <string.h>
17#include <stdarg.h>
18#include <readline/readline.h>
19#include <pthread.h>
20#include "util.h"
21#endif
22
23#include "ui.h"
24
25double CursorScaleFactor = 1;
26int PlotGridX=0, PlotGridY=0, PlotGridXdefault= 64, PlotGridYdefault= 64, CursorCPos= 0, CursorDPos= 0;
27bool flushAfterWrite = false; //buzzy
28int GridOffset = 0;
29bool GridLocked = false;
30bool showDemod = true;
31
32static char *logfilename = "proxmark3.log";
33
34#ifndef EXTERNAL_PRINTANDLOG
35static pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER;
36
37void PrintAndLogEx(logLevel_t level, char *fmt, ...) {
38
39 // skip debug messages if client debugging is turned off i.e. 'DATA SETDEBUG 0'
40// if (g_debugMode == 0 && level == DEBUG)
41// return;
42
43 char buffer[MAX_PRINT_BUFFER] = {0};
44 char buffer2[MAX_PRINT_BUFFER] = {0};
45 char prefix[20] = {0};
46 char *token = NULL;
47 int size = 0;
48 // {NORMAL, SUCCESS, INFO, FAILED, WARNING, ERR, DEBUG}
49 static char *prefixes[7] = { "", "", "INFO: ", "FAILED: ", "WARNING: ", "ERROR: ", "#: "};
50
51 switch( level ) {
52 case FAILED:
53 strncpy(prefix,_RED_(FAILED: ), sizeof(prefix)-1);
54 break;
55 case DEBUG:
56 strncpy(prefix,_BLUE_(#: ), sizeof(prefix)-1);
57 break;
58 case SUCCESS:
59 strncpy(prefix,_GREEN_( ), sizeof(prefix)-1);
60 break;
61 case WARNING:
62 strncpy(prefix,_CYAN_(WARNING: ), sizeof(prefix)-1);
63 break;
64 default:
65 strncpy(prefix, prefixes[level], sizeof(prefix)-1);
66 break;
67 }
68
69 va_list args;
70 va_start(args, fmt);
71 vsnprintf(buffer, sizeof(buffer), fmt, args);
72 va_end(args);
73
74 // no prefixes for normal
75 if ( level == NORMAL ) {
76 PrintAndLog(buffer);
77 return;
78 }
79
80 if (strchr(buffer, '\n')) {
81
82 const char delim[2] = "\n";
83
84 // line starts with newline
85 if (buffer[0] == '\n')
86 PrintAndLog("");
87
88 token = strtok(buffer, delim);
89
90 while (token != NULL) {
91
92 size = strlen(buffer2);
93
94 if (strlen(token))
95 snprintf(buffer2+size, sizeof(buffer2)-size, "%s%s\n", prefix, token);
96 else
97 snprintf(buffer2+size, sizeof(buffer2)-size, "\n");
98
99 token = strtok(NULL, delim);
100 }
101 PrintAndLog(buffer2);
102 } else {
103 snprintf(buffer2, sizeof(buffer2), "%s%.*s", prefix, MAX_PRINT_BUFFER - 20, buffer);
104 PrintAndLog(buffer2);
105 }
106}
107
108void PrintAndLog(char *fmt, ...)
109{
110 char *saved_line;
111 int saved_point;
112 va_list argptr, argptr2;
113 static FILE *logfile = NULL;
114 static int logging=1;
115
116 // lock this section to avoid interlacing prints from different threads
117 pthread_mutex_lock(&print_lock);
118
119 if (logging && !logfile) {
120 logfile=fopen(logfilename, "a");
121 if (!logfile) {
122 fprintf(stderr, "Can't open logfile, logging disabled!\n");
123 logging=0;
124 }
125 }
126
127 // If there is an incoming message from the hardware (eg: lf hid read) in
128 // the background (while the prompt is displayed and accepting user input),
129 // stash the prompt and bring it back later.
130#ifdef RL_STATE_READCMD
131 // We are using GNU readline. libedit (OSX) doesn't support this flag.
132 int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
133
134 if (need_hack) {
135 saved_point = rl_point;
136 saved_line = rl_copy_text(0, rl_end);
137 rl_save_prompt();
138 rl_replace_line("", 0);
139 rl_redisplay();
140 }
141#endif
142
143 va_start(argptr, fmt);
144 va_copy(argptr2, argptr);
145 vprintf(fmt, argptr);
146 printf(" "); // cleaning prompt
147 va_end(argptr);
148 printf("\n");
149
150#ifdef RL_STATE_READCMD
151 // We are using GNU readline. libedit (OSX) doesn't support this flag.
152 if (need_hack) {
153 rl_restore_prompt();
154 rl_replace_line(saved_line, 0);
155 rl_point = saved_point;
156 rl_redisplay();
157 free(saved_line);
158 }
159#endif
160
161 if (logging && logfile) {
162 vfprintf(logfile, fmt, argptr2);
163 fprintf(logfile,"\n");
164 fflush(logfile);
165 }
166 va_end(argptr2);
167
168 if (flushAfterWrite) //buzzy
169 {
170 fflush(NULL);
171 }
172 //release lock
173 pthread_mutex_unlock(&print_lock);
174}
175#endif
176
177void SetLogFilename(char *fn)
178{
179 logfilename = fn;
180}
181
182void SetFlushAfterWrite(bool flush_after_write) {
183 flushAfterWrite = flush_after_write;
184}
185
Impressum, Datenschutz