From 63f0dc8e3ffee77ef08fe929c7dccabf9cb3a8d2 Mon Sep 17 00:00:00 2001 From: pancake Date: Mon, 20 Apr 2015 14:44:54 +0200 Subject: [PATCH] Implement more utf8 terminal check, handle `jbe` in asm.pseudo --- configure-plugins | 5 +++++ libr/cons/utf8.c | 31 +++++++++++++++++++++++++++++-- libr/core/config.c | 14 ++------------ libr/parse/p/parse_x86_pseudo.c | 3 ++- libr/util/regex/test.c | 20 ++++++++++++++------ 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/configure-plugins b/configure-plugins index ae2b0b661d..33a1234772 100755 --- a/configure-plugins +++ b/configure-plugins @@ -5,8 +5,13 @@ # update: 2010-01-14 # +LANG=C +LC_ALL=C LOADLIBS=1 +export LANG +export LC_ALL + list () { for a in $STATIC ; do echo "static $a" ; done for a in $SHARED ; do echo "shared $a" ; done diff --git a/libr/cons/utf8.c b/libr/cons/utf8.c index 43599b4f4f..bcfd1e09eb 100644 --- a/libr/cons/utf8.c +++ b/libr/cons/utf8.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2014 - pancake */ +/* radare - LGPL - Copyright 2015 - pancake */ // Copypasta from http://www.linuxquestions.org/questions/programming-9/get-cursor-position-in-c-947833/ #include @@ -12,6 +12,11 @@ #define RD_EOF -1 #define RD_EIO -2 +/* select utf8 terminal detection method */ +#define UTF8_DETECT_ENV 1 +#define UTF8_DETECT_LOCALE 0 +#define UTF8_DETECT_CURSOR 0 + static inline int rd(const int fd) { unsigned char buffer[4]; ssize_t n; @@ -87,6 +92,7 @@ int current_tty(void) { #endif } +#if UTF8_DETECT_CURSOR /* As the tty for current cursor position. * This function returns 0 if success, errno code otherwise. * Actual errno will be unchanged. @@ -201,9 +207,28 @@ static int cursor_position(const int tty, int *const rowptr, int *const colptr) /* Done. */ return ret; } - +#endif R_API int r_cons_is_utf8() { + int ret = 0; +#if UTF8_DETECT_ENV + char *sval = r_sys_getenv ("LC_CTYPE"); + if (sval) { + r_str_case (sval, 0); + if (!strcmp (sval, "utf-8")) + ret = 1; + free (sval); + } +#endif +#if UTF8_DETECT_LOCALE +#include + const char *ctype = setlocale(LC_CTYPE, NULL); + if ( (ctype != NULL) && (ctype = strchr(ctype, '.')) && ctype++ && + (strcasecmp(ctype, "UTF-8") == 0 || strcasecmp(ctype, "UTF8") == 0)) { + return 1; + } +#endif +#if UTF8_DETECT_CURSOR int row = 0, col = 0; int row2 = 0, col2 = 0; int fd = current_tty(); @@ -221,6 +246,8 @@ R_API int r_cons_is_utf8() { close (fd); write (1, "\r \r", 6); return ((col2-col)==2); +#endif + return ret; } #else R_API int r_cons_is_utf8() { diff --git a/libr/core/config.c b/libr/core/config.c index fc048cc6b7..4999cd713d 100644 --- a/libr/core/config.c +++ b/libr/core/config.c @@ -1272,18 +1272,8 @@ R_API int r_core_config_init(RCore *core) { SETCB("scr.truecolor", "false", &cb_truecolor, "Manage color palette (0: ansi 16, 1: 256, 2: 16M)"); SETCB("scr.color", (core->print->flags&R_PRINT_FLAGS_COLOR)?"true":"false", &cb_color, "Enable/Disable colors"); SETCB("scr.null", "false", &cb_scrnull, "if set shows no output (disable console)"); -#if 0 - { - const char *val; - char *sval = r_sys_getenv ("LC_CTYPE"); - r_str_case (sval, 0); - val = strcmp (sval, "utf-8")? "false": "true"; - free (sval); - r_config_set_cb (cfg, "scr.utf8", val, &cb_utf8); - } -#else - SETCB("scr.utf8", "false", &cb_utf8, "Show UTF-8 characters instead of ANSI"); -#endif + SETCB("scr.utf8", r_cons_is_utf8()?"true":"false", + &cb_utf8, "Show UTF-8 characters instead of ANSI"); SETPREF("scr.histsave", "true", "Always save history on exit"); /* search */ SETCB("search.contiguous", "true", &cb_contiguous, "Accept contiguous/adjacent search hits"); diff --git a/libr/parse/p/parse_x86_pseudo.c b/libr/parse/p/parse_x86_pseudo.c index c2c961f4d0..afe1f83af6 100644 --- a/libr/parse/p/parse_x86_pseudo.c +++ b/libr/parse/p/parse_x86_pseudo.c @@ -56,9 +56,10 @@ static int replace(int argc, const char *argv[], char *newstr) { { "je", "isZero 1)"}, { "jle", "isLessOrEqual 1)"}, { "jge", "isGreaterOrEqual 1)"}, + { "jbe", "isBelowOrEqual 1)"}, { "jne", "notZero 1)"}, { "call", "1 ()"}, - { "test", "if (1 == 2)"}, + { "test", "if (1 == 2"}, { "xor", "1 ^= 2"}, { NULL } }; diff --git a/libr/util/regex/test.c b/libr/util/regex/test.c index 872ac66aef..9165c9e133 100644 --- a/libr/util/regex/test.c +++ b/libr/util/regex/test.c @@ -18,13 +18,21 @@ int _main() { return 0; } -int main() { - RRegex *rx = r_regex_new ("^hi", ""); +int main(int argc, char **argv) { + const char *needle = "^hi"; + const char *haystack_1 = "patata"; + const char *haystack_2 = "hillow"; + if (argc>3) { + needle = argv[1]; + haystack_1 = argv[2]; + haystack_2 = argv[3]; + } else printf ("Using default values\n"); + RRegex *rx = r_regex_new (needle, ""); if (rx) { - int res = r_regex_exec (rx, "patata", 0, 0, 0); - printf ("result (patata) = %d\n", res); - res = r_regex_exec (rx, "hillow", 0, 0, 0); - printf ("result (hillow) = %d\n", res); + int res = r_regex_exec (rx, haystack_1, 0, 0, 0); + printf ("result (%s) = %d\n", haystack_1, res); + res = r_regex_exec (rx, haystack_2, 0, 0, 0); + printf ("result (%s) = %d\n", haystack_2, res); r_regex_free (rx); } else printf ("oops, cannot compile regexp\n"); return 0;