OSX: Multiple libedit/readline fixes (#585)
[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>
3775e9e8 16#include <stdarg.h>
51969283 17#include <readline/readline.h>
9492e0b0 18#include <pthread.h>
3775e9e8 19#endif
7fe9b0b7 20
21#include "ui.h"
22
3fe71039 23double CursorScaleFactor = 1;
b8fdac9e 24int PlotGridX=0, PlotGridY=0, PlotGridXdefault= 64, PlotGridYdefault= 64, CursorCPos= 0, CursorDPos= 0;
7fe9b0b7 25int offline;
3851172d 26int flushAfterWrite = 0; //buzzy
0f321d63 27int GridOffset = 0;
3fe71039 28bool GridLocked = false;
b8fdac9e 29bool showDemod = true;
0f321d63 30
7fe9b0b7 31static char *logfilename = "proxmark3.log";
32
3775e9e8
MF
33#ifndef EXTERNAL_PRINTANDLOG
34// Declared in proxmark3.c
35extern pthread_mutex_t print_lock;
36
7fe9b0b7 37void PrintAndLog(char *fmt, ...)
38{
51969283
M
39 char *saved_line;
40 int saved_point;
9492e0b0 41 va_list argptr, argptr2;
42 static FILE *logfile = NULL;
43 static int logging=1;
7fe9b0b7 44
acf0582d 45 // lock this section to avoid interlacing prints from different threads
9492e0b0 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 }
ed50f7f3 55
a5a83016
MF
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.
ed50f7f3 59#ifdef RL_STATE_READCMD
a5a83016 60 // We are using GNU readline. libedit (OSX) doesn't support this flag.
51969283 61 int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
7fe9b0b7 62
51969283
M
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 }
ed50f7f3 70#endif
51969283 71
9492e0b0 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");
51969283 78
a5a83016
MF
79#ifdef RL_STATE_READCMD
80 // We are using GNU readline. libedit (OSX) doesn't support this flag.
51969283
M
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 }
a5a83016 88#endif
51969283 89
9492e0b0 90 if (logging && logfile) {
91 vfprintf(logfile, fmt, argptr2);
92 fprintf(logfile,"\n");
93 fflush(logfile);
94 }
95 va_end(argptr2);
96
ed77aabe 97 if (flushAfterWrite == 1) //buzzy
98 {
99 fflush(NULL);
100 }
9492e0b0 101 //release lock
102 pthread_mutex_unlock(&print_lock);
7fe9b0b7 103}
3775e9e8 104#endif
9492e0b0 105
7fe9b0b7 106void SetLogFilename(char *fn)
107{
108 logfilename = fn;
109}
Impressum, Datenschutz