Smart color handling:

* if on Windows, no color, as usual
* if on Linux, color only on real term
  * no color when > foo
  * no color in the logfile
This commit is contained in:
Philippe Teuwen 2019-04-30 00:41:52 +02:00
commit a91d0a7b19
10 changed files with 101 additions and 112 deletions

View file

@ -15,7 +15,7 @@
#include "proxmark3.h" #include "proxmark3.h"
#include "apps.h" #include "apps.h"
#include "parity.h" #include "parity.h"
#include "commonutil.h" #include "util.h"
#include "string.h" #include "string.h"
#include "iso14443a.h" #include "iso14443a.h"
#include "crapto1/crapto1.h" #include "crapto1/crapto1.h"

View file

@ -12,18 +12,13 @@
#define __UTIL_H #define __UTIL_H
#include "common.h" #include "common.h"
#include "commonutil.h"
#include "proxmark3.h" #include "proxmark3.h"
#include "string.h" #include "string.h"
#include "BigBuf.h" #include "BigBuf.h"
#include "ticks.h" #include "ticks.h"
// Basic macros // Basic macros
# define _BLUE_(s) "\x1b[34m" s "\x1b[0m "
# define _RED_(s) "\x1b[31m" s "\x1b[0m "
# define _GREEN_(s) "\x1b[32m" s "\x1b[0m "
# define _YELLOW_(s) "\x1b[33m" s "\x1b[0m "
# define _MAGENTA_(s) "\x1b[35m" s "\x1b[0m "
# define _CYAN_(s) "\x1b[36m" s "\x1b[0m "
#ifndef SHORT_COIL #ifndef SHORT_COIL
#define SHORT_COIL() LOW(GPIO_SSC_DOUT) #define SHORT_COIL() LOW(GPIO_SSC_DOUT)

View file

@ -11,6 +11,10 @@
#include "comms.h" #include "comms.h"
#include "crc16.h" #include "crc16.h"
#if defined(__linux__) || (__APPLE__)
#include <sys/stat.h>
#include <unistd.h>
#endif
//#define COMMS_DEBUG //#define COMMS_DEBUG
//#define COMMS_DEBUG_RAW //#define COMMS_DEBUG_RAW
@ -249,48 +253,6 @@ static int getReply(PacketResponseNG *packet) {
return 1; return 1;
} }
static void memcpy_filtered(void *dest, const void *src, size_t n, bool filter) {
#if defined(__linux__) || (__APPLE__)
memcpy(dest, src, n);
#else
if (filter) {
// Filter out ANSI sequences on these OS
uint8_t *rdest = (uint8_t *)dest;
uint8_t *rsrc = (uint8_t *)src;
uint16_t si = 0;
for (uint16_t i = 0; i < n; i++) {
if ((rsrc[i] == '\x1b')
&& (i < n - 1)
&& (rsrc[i + 1] >= 0x40)
&& (rsrc[i + 1] <= 0x5F)) { // entering ANSI sequence
i++;
if ((rsrc[i] == '[') && (i < n - 1)) { // entering CSI sequence
i++;
while ((i < n - 1) && (rsrc[i] >= 0x30) && (rsrc[i] <= 0x3F)) { // parameter bytes
i++;
}
while ((i < n - 1) && (rsrc[i] >= 0x20) && (rsrc[i] <= 0x2F)) { // intermediate bytes
i++;
}
if ((rsrc[i] >= 0x40) && (rsrc[i] <= 0x7F)) { // final byte
continue;
}
} else {
continue;
}
}
rdest[si++] = rsrc[i];
}
} else {
memcpy(dest, src, n);
}
#endif
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Entry point into our code: called whenever we received a packet over USB // Entry point into our code: called whenever we received a packet over USB
// that we weren't necessarily expecting, for example a debug print. // that we weren't necessarily expecting, for example a debug print.
@ -303,6 +265,13 @@ static void PacketResponseReceived(PacketResponseNG *packet) {
// we got a packet, reset WaitForResponseTimeout timeout // we got a packet, reset WaitForResponseTimeout timeout
timeout_start_time = msclock(); timeout_start_time = msclock();
bool filter_ansi = true;
#if defined(__linux__) || (__APPLE__)
struct stat tmp_stat;
if ((fstat (STDOUT_FILENO, &tmp_stat) == 0) && (S_ISCHR (tmp_stat.st_mode)) && isatty(STDIN_FILENO))
filter_ansi = false;
#endif
switch (packet->cmd) { switch (packet->cmd) {
// First check if we are handling a debug message // First check if we are handling a debug message
case CMD_DEBUG_PRINT_STRING: { case CMD_DEBUG_PRINT_STRING: {
@ -320,11 +289,11 @@ static void PacketResponseReceived(PacketResponseNG *packet) {
struct d *data = (struct d *)&packet->data.asBytes; struct d *data = (struct d *)&packet->data.asBytes;
len = packet->length - sizeof(data->flag); len = packet->length - sizeof(data->flag);
flag = data->flag; flag = data->flag;
memcpy_filtered(s, data->buf, len, flag & FLAG_ANSI); memcpy_filter_ansi(s, data->buf, len, (flag & FLAG_ANSI) && filter_ansi);
} else { } else {
len = MIN(packet->oldarg[0], USB_CMD_DATA_SIZE); len = MIN(packet->oldarg[0], USB_CMD_DATA_SIZE);
flag = packet->oldarg[1]; flag = packet->oldarg[1];
memcpy_filtered(s, packet->data.asBytes, len, flag & FLAG_ANSI); memcpy_filter_ansi(s, packet->data.asBytes, len, (flag & FLAG_ANSI) && filter_ansi);
} }
if (flag & FLAG_LOG) { if (flag & FLAG_LOG) {

View file

@ -12,7 +12,6 @@
#include <jansson.h> #include <jansson.h>
#include "tlv.h" #include "tlv.h"
#include "commonutil.h"
typedef struct { typedef struct {
tlv_tag_t Tag; tlv_tag_t Tag;

View file

@ -16,7 +16,6 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <cbor.h> #include <cbor.h>
#include "commonutil.h"
#include "util.h" #include "util.h"
const char *GetCOSEAlgName(int id); const char *GetCOSEAlgName(int id);

View file

@ -30,25 +30,25 @@
#include "usart.h" #include "usart.h"
static void showBanner(void) { static void showBanner(void) {
printf("\n\n"); PrintAndLogEx(NORMAL, "\n");
#if defined(__linux__) || (__APPLE__) #if defined(__linux__) || (__APPLE__)
printf(_BLUE_("██████╗ ███╗ ███╗ ████╗ ") " ...iceman fork\n"); PrintAndLogEx(NORMAL, _BLUE_("██████╗ ███╗ ███╗ ████╗ ") " ...iceman fork");
printf(_BLUE_("██╔══██╗████╗ ████║ ══█║") " ...dedicated to " _BLUE_("RDV40") "\n"); PrintAndLogEx(NORMAL, _BLUE_("██╔══██╗████╗ ████║ ══█║") " ...dedicated to " _BLUE_("RDV40"));
printf(_BLUE_("██████╔╝██╔████╔██║ ████╔╝") "\n"); PrintAndLogEx(NORMAL, _BLUE_("██████╔╝██╔████╔██║ ████╔╝"));
printf(_BLUE_("██╔═══╝ ██║╚██╔╝██║ ══█║") " iceman@icesql.net\n"); PrintAndLogEx(NORMAL, _BLUE_("██╔═══╝ ██║╚██╔╝██║ ══█║") " iceman@icesql.net");
printf(_BLUE_("██║ ██║ ╚═╝ ██║ ████╔╝") " https://github.com/rfidresearchgroup/proxmark3/\n"); PrintAndLogEx(NORMAL, _BLUE_("██║ ██║ ╚═╝ ██║ ████╔╝") " https://github.com/rfidresearchgroup/proxmark3/");
printf(_BLUE_("╚═╝ ╚═╝ ╚═╝ ╚═══╝ ") "pre-release v4.0\n"); PrintAndLogEx(NORMAL, _BLUE_("╚═╝ ╚═╝ ╚═╝ ╚═══╝ ") "pre-release v4.0");
#else #else
printf("======. ===. ===. ====. ...iceman fork\n"); PrintAndLogEx(NORMAL, "======. ===. ===. ====. ...iceman fork");
printf("==...==.====. ====. ..=. ...dedicated to RDV40\n"); PrintAndLogEx(NORMAL, "==...==.====. ====. ..=. ...dedicated to RDV40");
printf("======..==.====.==. ====..\n"); PrintAndLogEx(NORMAL, "======..==.====.==. ====..");
printf("==..... ==..==..==. ..=. iceman@icesql.net\n"); PrintAndLogEx(NORMAL, "==..... ==..==..==. ..=. iceman@icesql.net");
printf("==. ==. ... ==. ====.. https://github.com/rfidresearchgroup/proxmark3/\n"); PrintAndLogEx(NORMAL, "==. ==. ... ==. ====.. https://github.com/rfidresearchgroup/proxmark3/");
printf("... ... ... ..... pre-release v4.0\n"); PrintAndLogEx(NORMAL, "... ... ... ..... pre-release v4.0");
#endif #endif
printf("\nSupport iceman on patreon, https://www.patreon.com/iceman1001/"); PrintAndLogEx(NORMAL, "\nSupport iceman on patreon, https://www.patreon.com/iceman1001/");
// printf("\nMonero: 43mNJLpgBVaTvyZmX9ajcohpvVkaRy1kbZPm8tqAb7itZgfuYecgkRF36rXrKFUkwEGeZedPsASRxgv4HPBHvJwyJdyvQuP"); // printf("\nMonero: 43mNJLpgBVaTvyZmX9ajcohpvVkaRy1kbZPm8tqAb7itZgfuYecgkRF36rXrKFUkwEGeZedPsASRxgv4HPBHvJwyJdyvQuP");
printf("\n\n\n"); PrintAndLogEx(NORMAL, "\n");
fflush(stdout); fflush(stdout);
} }

View file

@ -16,6 +16,10 @@
#endif #endif
#include "ui.h" #include "ui.h"
#if defined(__linux__) || (__APPLE__)
#include <unistd.h>
#include <sys/stat.h>
#endif
double CursorScaleFactor = 1; double CursorScaleFactor = 1;
int PlotGridX = 0, PlotGridY = 0, PlotGridXdefault = 64, PlotGridYdefault = 64; int PlotGridX = 0, PlotGridY = 0, PlotGridXdefault = 64, PlotGridYdefault = 64;
@ -138,10 +142,11 @@ void PrintAndLogEx(logLevel_t level, const char *fmt, ...) {
void PrintAndLog(const char *fmt, ...) { void PrintAndLog(const char *fmt, ...) {
char *saved_line; char *saved_line;
int saved_point; int saved_point;
va_list argptr, argptr2; va_list argptr;
static FILE *logfile = NULL; static FILE *logfile = NULL;
static int logging = 1; static int logging = 1;
char buffer[MAX_PRINT_BUFFER] = {0};
char buffer2[MAX_PRINT_BUFFER] = {0};
// lock this section to avoid interlacing prints from different threads // lock this section to avoid interlacing prints from different threads
pthread_mutex_lock(&print_lock); pthread_mutex_lock(&print_lock);
@ -171,10 +176,18 @@ void PrintAndLog(const char *fmt, ...) {
#endif #endif
va_start(argptr, fmt); va_start(argptr, fmt);
va_copy(argptr2, argptr); vsnprintf(buffer, sizeof(buffer), fmt, argptr);
vprintf(fmt, argptr);
printf(" "); // cleaning prompt
va_end(argptr); va_end(argptr);
bool filter_ansi = true;
#if defined(__linux__) || (__APPLE__)
struct stat tmp_stat;
if ((fstat (STDOUT_FILENO, &tmp_stat) == 0) && (S_ISCHR (tmp_stat.st_mode)) && isatty(STDIN_FILENO))
filter_ansi = false;
#endif
memcpy_filter_ansi(buffer2, buffer, sizeof(buffer), filter_ansi);
printf("%s", buffer2);
printf(" "); // cleaning prompt
printf("\n"); printf("\n");
#ifdef RL_STATE_READCMD #ifdef RL_STATE_READCMD
@ -189,11 +202,14 @@ void PrintAndLog(const char *fmt, ...) {
#endif #endif
if (logging && logfile) { if (logging && logfile) {
vfprintf(logfile, fmt, argptr2); if (filter_ansi) { // already done
fprintf(logfile, "\n"); fprintf(logfile, "%s\n", buffer2);
} else {
memcpy_filter_ansi(buffer, buffer2, sizeof(buffer2), true);
fprintf(logfile, "%s\n", buffer);
}
fflush(logfile); fflush(logfile);
} }
va_end(argptr2);
if (flushAfterWrite) if (flushAfterWrite)
fflush(stdout); fflush(stdout);
@ -210,6 +226,44 @@ void SetFlushAfterWrite(bool value) {
flushAfterWrite = value; flushAfterWrite = value;
} }
void memcpy_filter_ansi(void *dest, const void *src, size_t n, bool filter) {
if (filter) {
// Filter out ANSI sequences on these OS
uint8_t *rdest = (uint8_t *)dest;
uint8_t *rsrc = (uint8_t *)src;
uint16_t si = 0;
for (uint16_t i = 0; i < n; i++) {
if ((rsrc[i] == '\x1b')
&& (i < n - 1)
&& (rsrc[i + 1] >= 0x40)
&& (rsrc[i + 1] <= 0x5F)) { // entering ANSI sequence
i++;
if ((rsrc[i] == '[') && (i < n - 1)) { // entering CSI sequence
i++;
while ((i < n - 1) && (rsrc[i] >= 0x30) && (rsrc[i] <= 0x3F)) { // parameter bytes
i++;
}
while ((i < n - 1) && (rsrc[i] >= 0x20) && (rsrc[i] <= 0x2F)) { // intermediate bytes
i++;
}
if ((rsrc[i] >= 0x40) && (rsrc[i] <= 0x7F)) { // final byte
continue;
}
} else {
continue;
}
}
rdest[si++] = rsrc[i];
}
} else {
memcpy(dest, src, n);
}
}
void iceIIR_Butterworth(int *data, const size_t len) { void iceIIR_Butterworth(int *data, const size_t len) {
int *output = (int *) calloc(sizeof(int) * len, sizeof(uint8_t)); int *output = (int *) calloc(sizeof(int) * len, sizeof(uint8_t));

View file

@ -39,6 +39,7 @@ void PrintAndLogOptions(const char *str[][2], size_t size, size_t space);
void PrintAndLogEx(logLevel_t level, const char *fmt, ...); void PrintAndLogEx(logLevel_t level, const char *fmt, ...);
void SetLogFilename(char *fn); void SetLogFilename(char *fn);
void SetFlushAfterWrite(bool value); void SetFlushAfterWrite(bool value);
void memcpy_filter_ansi(void *dest, const void *src, size_t n, bool filter);
extern double CursorScaleFactor; extern double CursorScaleFactor;
extern int PlotGridX, PlotGridY, PlotGridXdefault, PlotGridYdefault, GridOffset; extern int PlotGridX, PlotGridY, PlotGridXdefault, PlotGridYdefault, GridOffset;

View file

@ -19,6 +19,7 @@
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include "ui.h" // PrintAndLog #include "ui.h" // PrintAndLog
#include "commonutil.h"
#ifdef ANDROID #ifdef ANDROID
#include <endian.h> #include <endian.h>
@ -112,42 +113,6 @@
# define FILE_PATH_SIZE 1000 # define FILE_PATH_SIZE 1000
#endif #endif
#if defined(__linux__) || (__APPLE__)
# define _BLUE_(s) "\x1b[34m" s "\x1b[0m "
#else
# define _BLUE_(s) s " "
#endif
#if defined(__linux__) || (__APPLE__)
# define _RED_(s) "\x1b[31m" s "\x1b[0m "
#else
# define _RED_(s) s " "
#endif
#if defined(__linux__) || (__APPLE__)
# define _GREEN_(s) "\x1b[32m" s "\x1b[0m "
#else
# define _GREEN_(s) s " "
#endif
#if defined(__linux__) || (__APPLE__)
# define _YELLOW_(s) "\x1b[33m" s "\x1b[0m "
#else
# define _YELLOW_(s) s " "
#endif
#if defined(__linux__) || (__APPLE__)
# define _MAGENTA_(s) "\x1b[35m" s "\x1b[0m "
#else
# define _MAGENTA_(s) s " "
#endif
#if defined(__linux__) || (__APPLE__)
# define _CYAN_(s) "\x1b[36m" s "\x1b[0m "
#else
# define _CYAN_(s) s " "
#endif
#ifndef DropField #ifndef DropField
#define DropField() { \ #define DropField() { \
clearCommandBuffer(); SendCommandOLD(CMD_READER_ISO_14443a, 0, 0, 0, NULL, 0); \ clearCommandBuffer(); SendCommandOLD(CMD_READER_ISO_14443a, 0, 0, 0, NULL, 0); \

View file

@ -53,4 +53,11 @@ void lsl(uint8_t *data, size_t len);
int32_t le24toh(uint8_t data[3]); int32_t le24toh(uint8_t data[3]);
void htole24(uint32_t val, uint8_t data[3]); void htole24(uint32_t val, uint8_t data[3]);
# define _BLUE_(s) "\x1b[34m" s "\x1b[0m "
# define _RED_(s) "\x1b[31m" s "\x1b[0m "
# define _GREEN_(s) "\x1b[32m" s "\x1b[0m "
# define _YELLOW_(s) "\x1b[33m" s "\x1b[0m "
# define _MAGENTA_(s) "\x1b[35m" s "\x1b[0m "
# define _CYAN_(s) "\x1b[36m" s "\x1b[0m "
#endif #endif