From 690445700a54ba78e2d013a4251c7258f57b19c2 Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 21 Jun 2012 10:12:02 +0200 Subject: [PATCH] * Add ^ and $ in cons grep expressions * Minor work on source CL commands, needs review --- configure | 9 ++-- libr/bin/format/mach0/mach0.c | 3 +- libr/bin/p/bin_mach0.c | 11 ++--- libr/cons/grep.c | 32 +++++++++++--- libr/core/cmd_info.c | 15 ++++--- libr/core/cmd_meta.c | 79 +++++++++++++++++++++-------------- libr/include/r_cons.h | 2 + libr/util/base85.c | 2 +- 8 files changed, 97 insertions(+), 56 deletions(-) diff --git a/configure b/configure index 751e72fce9..bcb727729b 100755 --- a/configure +++ b/configure @@ -1,5 +1,5 @@ #!/bin/sh -# This script was automatically generated by ACR v0.9 +# This script was automatically generated by ACR v0.9.1 # @author: pancake # @url: http://www.nopcode.org # @repo: hg clone http://hg.youterm.com/acr @@ -101,7 +101,8 @@ done : ${INSTALL_DIR:=${INSTALL} -d} : ${INSTALL_DATA:=${INSTALL} -m 644} : ${INSTALL_SCRIPT:=${INSTALL} -m 755} -: ${INSTALL_PROGRAM:=${INSTALL} -m 755 -s} +: ${INSTALL_PROGRAM:=${INSTALL} -m 755} +: ${INSTALL_PROGRAM_STRIP:=${INSTALL} -m 755 -s} : ${INSTALL_MAN:=${INSTALL} -m 444} : ${INSTALL_LIB:=${INSTALL} -c} PKGNAME='radare2' ; VERSION='0.9.1hg' ; CONTACT_MAIL="pancake@nopcode.org" ; CONTACT_NAME="pancake" ; CONTACT="pancake " ; @@ -187,7 +188,7 @@ take_environ() { } show_version() { -echo "radare2-0.9.1hg configuration script done with acr v0.9. +echo "radare2-0.9.1hg configuration script done with acr v0.9.1. The 'Free Software Foundation' message is only for autodetection. Originally written by pancake ." exit 0 @@ -287,7 +288,7 @@ parse_options $1 shift done -ENVWORDS="MANDIR INFODIR LIBDIR INCLUDEDIR LOCALSTATEDIR SYSCONFDIR DATADIR LIBEXECDIR SBINDIR BINDIR EPREFIX PREFIX SPREFIX TARGET HOST BUILD INSTALL INSTALL_LIB INSTALL_MAN INSTALL_PROGRAM INSTALL_DIR INSTALL_SCRIPT INSTALL_DATA HOST_OS HOST_CPU BUILD_OS BUILD_CPU TARGET_OS TARGET_CPU PKGNAME VPATH VERSION CONTACT CONTACT_NAME CONTACT_MAIL CC CFLAGS CPPFLAGS LDFLAGS HAVE_LANG_C DEBUGGER HAVE_LIB_DL DL_LIBS LIL_ENDIAN BIG_ENDIAN BYTEORDER HAVE_LIB_MAGIC USE_MAGIC USE_LIB_MAGIC LIBMAGIC HAVE_LIB_EWF EWF_CFLAGS EWF_LDFLAGS HAVE_PKGCFG_LIBEWF HAVE_EWF HAVE_LIB_GMP HAVE_GMP HAVE_LIB_SSL SSL_CFLAGS SSL_LDFLAGS HAVE_PKGCFG_OPENSSL HAVE_OPENSSL HAVE_LILEND HAVE_BIGEND HAVE_LIB_LUA5_1 HAVE_LIB_TCC USERCC USEROSTYPE WITHPIC WITHNONPIC LIBVERSION" +ENVWORDS="MANDIR INFODIR LIBDIR INCLUDEDIR LOCALSTATEDIR SYSCONFDIR DATADIR LIBEXECDIR SBINDIR BINDIR EPREFIX PREFIX SPREFIX TARGET HOST BUILD INSTALL INSTALL_LIB INSTALL_MAN INSTALL_PROGRAM INSTALL_PROGRAM_STRIP INSTALL_DIR INSTALL_SCRIPT INSTALL_DATA HOST_OS HOST_CPU BUILD_OS BUILD_CPU TARGET_OS TARGET_CPU PKGNAME VPATH VERSION CONTACT CONTACT_NAME CONTACT_MAIL CC CFLAGS CPPFLAGS LDFLAGS HAVE_LANG_C DEBUGGER HAVE_LIB_DL DL_LIBS LIL_ENDIAN BIG_ENDIAN BYTEORDER HAVE_LIB_MAGIC USE_MAGIC USE_LIB_MAGIC LIBMAGIC HAVE_LIB_EWF EWF_CFLAGS EWF_LDFLAGS HAVE_PKGCFG_LIBEWF HAVE_EWF HAVE_LIB_GMP HAVE_GMP HAVE_LIB_SSL SSL_CFLAGS SSL_LDFLAGS HAVE_PKGCFG_OPENSSL HAVE_OPENSSL HAVE_LILEND HAVE_BIGEND HAVE_LIB_LUA5_1 HAVE_LIB_TCC USERCC USEROSTYPE WITHPIC WITHNONPIC LIBVERSION" create_environ diff --git a/libr/bin/format/mach0/mach0.c b/libr/bin/format/mach0/mach0.c index 1886f3eb89..8f7a2fc972 100644 --- a/libr/bin/format/mach0/mach0.c +++ b/libr/bin/format/mach0/mach0.c @@ -24,7 +24,8 @@ static int MACH0_(r_bin_mach0_addr_to_offset)(struct MACH0_(r_bin_mach0_obj_t)* } static int MACH0_(r_bin_mach0_init_hdr)(struct MACH0_(r_bin_mach0_obj_t)* bin) { - int magic, len; + ut32 magic; + int len; if (r_buf_read_at (bin->b, 0, (ut8*)&magic, 4) == -1) { eprintf ("Error: read (magic)\n"); diff --git a/libr/bin/p/bin_mach0.c b/libr/bin/p/bin_mach0.c index 049ecd094d..9f18aa034d 100644 --- a/libr/bin/p/bin_mach0.c +++ b/libr/bin/p/bin_mach0.c @@ -158,7 +158,7 @@ static RBinInfo* info(RBinArch *arch) { free (str); } strncpy (ret->rclass, "mach0", R_BIN_SIZEOF_STRINGS); - /* TODO get os*/ + /* TODO get os */ strncpy (ret->os, "darwin", R_BIN_SIZEOF_STRINGS); strncpy (ret->subsystem, "darwin", R_BIN_SIZEOF_STRINGS); if ((str = MACH0_(r_bin_mach0_get_cputype) (arch->bin_obj))) { @@ -183,10 +183,11 @@ static RBinInfo* info(RBinArch *arch) { #if !R_BIN_MACH064 static int check(RBinArch *arch) { - if (arch && arch->buf && arch->buf->buf) - if (!memcmp (arch->buf->buf, "\xce\xfa\xed\xfe", 4) || - !memcmp (arch->buf->buf, "\xfe\xed\xfa\xce", 4)) - return R_TRUE; + if (arch && arch->buf && arch->buf->buf) { + if (!memcmp (arch->buf->buf, "\xce\xfa\xed\xfe", 4) || + !memcmp (arch->buf->buf, "\xfe\xed\xfa\xce", 4)) + return R_TRUE; + } return R_FALSE; } diff --git a/libr/cons/grep.c b/libr/cons/grep.c index 590b4491ce..0e497f3892 100644 --- a/libr/cons/grep.c +++ b/libr/cons/grep.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2011 pancake nibble */ +/* radare - LGPL - Copyright 2009-2012 pancake nibble */ #include #include @@ -9,6 +9,9 @@ R_API void r_cons_grep(const char *str) { char *ptr, *optr, *ptr2, *ptr3; cons = r_cons_singleton (); cons->grep.str = NULL; + cons->grep.neg = 0; + cons->grep.begin = 0; + cons->grep.end = 0; cons->grep.nstrings = 0; cons->grep.tokenfrom = 0; cons->grep.tokento = ST32_MAX; @@ -18,6 +21,10 @@ R_API void r_cons_grep(const char *str) { if (str == NULL || !*str) return; + if (*str == '^') { // neg + cons->grep.begin = 1; + str++; + } if (*str == '!') { // neg cons->grep.neg = 1; str++; @@ -28,6 +35,13 @@ R_API void r_cons_grep(const char *str) { } strncpy (buf, str, sizeof (buf)-1); + { + int len = strlen (buf)-1; + if (len>1 && buf[len]=='$' && buf[len-1]!='\\') { + cons->grep.end = 1; + buf[len] = 0; + } + } ptr = buf; ptr3 = strchr (ptr, '['); // column number if (ptr3) { @@ -131,11 +145,17 @@ R_API int r_cons_grep_line(char *buf, int len) { memcpy (in, buf, len); if (cons->grep.nstrings>0) { - for (i=0; igrep.nstrings; i++) - if (strstr (in, cons->grep.strings[i])) { - hit = !cons->grep.neg; - break; - } + for (i=0; igrep.nstrings; i++) { + char *p = strstr (in, cons->grep.strings[i]); + if (!p) continue; + if (cons->grep.begin) + hit = (p == in)? 1: 0; + else hit = !cons->grep.neg; + // XXX: this can be optimized + if (cons->grep.end && (strlen (cons->grep.strings[i]) != strlen (p))) + hit = 0; + break; + } } else hit = 1; if (hit) { diff --git a/libr/core/cmd_info.c b/libr/core/cmd_info.c index 270806c2d8..5834c97af0 100644 --- a/libr/core/cmd_info.c +++ b/libr/core/cmd_info.c @@ -1,4 +1,3 @@ - /* radare - LGPL - Copyright 2009-2012 // pancake */ static int cmd_info(void *data, const char *input) { @@ -46,13 +45,13 @@ static int cmd_info(void *data, const char *input) { r_cons_printf ( "Usage: i[aeiIsSz]* ; get info from opened file\n" "NOTE: Append a '*' to get the output in radare commands\n" - " ia ; show all info (imports, exports, sections..)\n" - " ii ; imports\n" - " iI ; binary info\n" - " ie ; entrypoint\n" - " is ; symbols\n" - " iS ; sections\n" - " iz ; strings\n"); + " ia ; show all info (imports, exports, sections..)\n" + " ii ; imports\n" + " iI ; binary info\n" + " ie ; entrypoint\n" + " is ; symbols\n" + " iS ; sections\n" + " iz ; strings\n"); break; case '*': break; diff --git a/libr/core/cmd_meta.c b/libr/core/cmd_meta.c index 8e7b3ba20f..07fb74ee76 100644 --- a/libr/core/cmd_meta.c +++ b/libr/core/cmd_meta.c @@ -47,42 +47,59 @@ static int cmd_meta(void *data, const char *input) { } break; case 'l': - { - int num; - char *f, *p, *line, buf[4096]; - f = strdup (input +2); - p = strchr (f, ':'); - if (p) { - *p=0; - num = atoi (p+1); - line = r_file_slurp_line (input+2, num, 0); - if (!line) { - const char *dirsrc = r_config_get (core->config, "dir.source"); - if (dirsrc && *dirsrc) { - f = r_str_concat (strdup (dirsrc), f); - line = r_file_slurp_line (f, num, 0); - } - if (!line) { - eprintf ("Cannot slurp file\n"); - return R_FALSE; - } - } - p = strchr (p+1, ' '); - if (p) { - snprintf (buf, sizeof (buf), "CC %s:%d %s @ %s", - f, num, line, p+1); + // XXX: this should be moved to CL? + if (input[2]=='a') { + ut64 offset; + input++; + if (input[1]=='?') { + eprintf ("Usage: cla [addr]\n"); } else { - snprintf (buf, sizeof (buf), "\"CC %s:%d %s\"", - f, num, line); + char *sl; + if (input[1]==' ') + offset = r_num_math (core->num, input+2); + else offset = core->offset; + sl = r_bin_meta_get_source_line (core->bin, offset); + if (sl) { + r_cons_printf ("%s\n", sl); + free (sl); + } + } + } else { + int num; + char *f, *p, *line, buf[4096]; + f = strdup (input +2); + p = strchr (f, ':'); + if (p) { + *p=0; + num = atoi (p+1); + line = r_file_slurp_line (input+2, num, 0); + if (!line) { + const char *dirsrc = r_config_get (core->config, "dir.source"); + if (dirsrc && *dirsrc) { + f = r_str_concat (strdup (dirsrc), f); + line = r_file_slurp_line (f, num, 0); + } + if (!line) { + eprintf ("Cannot slurp file\n"); + return R_FALSE; + } + } + p = strchr (p+1, ' '); + if (p) { + snprintf (buf, sizeof (buf), "CC %s:%d %s @ %s", + f, num, line, p+1); + } else { + snprintf (buf, sizeof (buf), "\"CC %s:%d %s\"", + f, num, line); + } + r_core_cmd0 (core, buf); + free (line); + free (f); } - r_core_cmd0 (core, buf); - free (line); - free (f); - } } break; case 'L': // debug information of current offset - ret = r_bin_meta_get_line (core->bin, core->offset, file, 1023, &line); + ret = r_bin_meta_get_line (core->bin, core->offset, file, sizeof (file)-1, &line); if (ret) { r_cons_printf ("file %s\nline %d\n", file, line); ret = (line<5)? 5-line: 5; diff --git a/libr/include/r_cons.h b/libr/include/r_cons.h index 784dcc222a..13b7e69657 100644 --- a/libr/include/r_cons.h +++ b/libr/include/r_cons.h @@ -40,6 +40,8 @@ typedef struct r_cons_grep_t { int tokenfrom; int tokento; int neg; + int begin; + int end; } RConsGrep; typedef void (*RConsEvent)(void *); diff --git a/libr/util/base85.c b/libr/util/base85.c index 733a80c920..606a8693aa 100644 --- a/libr/util/base85.c +++ b/libr/util/base85.c @@ -205,7 +205,7 @@ void ascii85_decode(FILE *fp, int delims, int ignore_garbage) { int main(int argc, char *argv[]) { extern char *optarg; extern int optind, optopt, opterr; - int opt, long_optind, decode = 0, ignore_garbage = 0, delims = 1, wrap = 76, y_abbr = 0; + int opt, long_optind, decode = 0, ignore_garbage = 0, delims = 0, wrap = 76, y_abbr = 0; FILE *fp = stdin; char *opts = "dinw:yh"; struct option long_opts[] = {