]> git.zerfleddert.de Git - proxmark3-svn/blame - client/ui.c
Merge pull request #969 from pwpiwi/gcc10_fixes
[proxmark3-svn] / client / ui.c
CommitLineData
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
3775e9e8
MF
12#include <stdbool.h>
13#ifndef EXTERNAL_PRINTANDLOG
51969283 14#include <stdlib.h>
7fe9b0b7 15#include <stdio.h>
79d9ddc5 16#include <string.h>
3775e9e8 17#include <stdarg.h>
51969283 18#include <readline/readline.h>
9492e0b0 19#include <pthread.h>
6b882a39 20#include "util.h"
3775e9e8 21#endif
7fe9b0b7 22
23#include "ui.h"
24
3fe71039 25double CursorScaleFactor = 1;
b8fdac9e 26int PlotGridX=0, PlotGridY=0, PlotGridXdefault= 64, PlotGridYdefault= 64, CursorCPos= 0, CursorDPos= 0;
61aaee35 27bool flushAfterWrite = false; //buzzy
0f321d63 28int GridOffset = 0;
3fe71039 29bool GridLocked = false;
b8fdac9e 30bool showDemod = true;
0f321d63 31
7fe9b0b7 32static char *logfilename = "proxmark3.log";
33
3775e9e8 34#ifndef EXTERNAL_PRINTANDLOG
f5ecd97b 35static pthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER;
3775e9e8 36
6b882a39
OM
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 {
994f21fe 103 snprintf(buffer2, sizeof(buffer2), "%s%.*s", prefix, MAX_PRINT_BUFFER - 20, buffer);
6b882a39
OM
104 PrintAndLog(buffer2);
105 }
106}
107
7fe9b0b7 108void PrintAndLog(char *fmt, ...)
109{
51969283
M
110 char *saved_line;
111 int saved_point;
9492e0b0 112 va_list argptr, argptr2;
113 static FILE *logfile = NULL;
114 static int logging=1;
7fe9b0b7 115
acf0582d 116 // lock this section to avoid interlacing prints from different threads
9492e0b0 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 }
ed50f7f3 126
a5a83016
MF
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.
ed50f7f3 130#ifdef RL_STATE_READCMD
a5a83016 131 // We are using GNU readline. libedit (OSX) doesn't support this flag.
51969283 132 int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
7fe9b0b7 133
51969283
M
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 }
ed50f7f3 141#endif
51969283 142
9492e0b0 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");
51969283 149
a5a83016
MF
150#ifdef RL_STATE_READCMD
151 // We are using GNU readline. libedit (OSX) doesn't support this flag.
51969283
M
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 }
a5a83016 159#endif
51969283 160
9492e0b0 161 if (logging && logfile) {
162 vfprintf(logfile, fmt, argptr2);
163 fprintf(logfile,"\n");
164 fflush(logfile);
165 }
166 va_end(argptr2);
167
61aaee35 168 if (flushAfterWrite) //buzzy
ed77aabe 169 {
170 fflush(NULL);
171 }
9492e0b0 172 //release lock
173 pthread_mutex_unlock(&print_lock);
7fe9b0b7 174}
3775e9e8 175#endif
9492e0b0 176
7fe9b0b7 177void SetLogFilename(char *fn)
178{
179 logfilename = fn;
180}
61aaee35 181
182void SetFlushAfterWrite(bool flush_after_write) {
183 flushAfterWrite = flush_after_write;
184}
185
Impressum, Datenschutz