diff --git a/libr/bin/bin.c b/libr/bin/bin.c index 6ca7b4b2db..5eecc77e08 100644 --- a/libr/bin/bin.c +++ b/libr/bin/bin.c @@ -1498,14 +1498,17 @@ R_API RList *r_bin_get_libs(RBin *bin) { R_API RList * r_bin_patch_relocs(RBin *bin) { static bool first = true; RBinObject *o = r_bin_cur_object (bin); - if (!o) return NULL; - + if (!o) { + return NULL; + } //r_bin_object_set_items set o->relocs but there we don't have access to io //so we need to be run from bin_relocs, free the previous reloc and get the patched ones if (first && o->plugin && o->plugin->patch_relocs) { RList *tmp = o->plugin->patch_relocs (bin); first = false; - if (!tmp) return o->relocs; + if (!tmp) { + return o->relocs; + } r_list_free (o->relocs); o->relocs = tmp; REBASE_PADDR (o, o->relocs, RBinReloc); diff --git a/libr/bin/filter.c b/libr/bin/filter.c index 8a28b3edc8..7bc2872449 100644 --- a/libr/bin/filter.c +++ b/libr/bin/filter.c @@ -48,9 +48,13 @@ R_API void r_bin_filter_name(Sdb *db, ut64 vaddr, char *name, int maxlen) { R_API void r_bin_filter_sym(Sdb *db, ut64 vaddr, RBinSymbol *sym) { char *name; - if (!db || !sym) return; + if (!db || !sym) { + return; + } name = sym->name; - if (!name) return; + if (!name) { + return; + } const char *uname = sdb_fmt (0, "%" PFMT64x ".%s", vaddr, name); ut32 vhash = sdb_hash (uname); // vaddr hash - unique ut32 hash = sdb_hash (name); // name hash - if dupped and not in unique hash must insert @@ -65,7 +69,10 @@ R_API void r_bin_filter_sym(Sdb *db, ut64 vaddr, RBinSymbol *sym) { } if (count > 1) { char *nstr = r_str_newf ("%s_%d", sym->name, count - 1); - free (sym->name); + //this leaks but for security reasons until refactored + //the problem is only with ELF's relocs though + //complains go to alvarofe + //free (sym->name); sym->name = nstr; } } diff --git a/libr/bin/format/elf/elf_specs.h b/libr/bin/format/elf/elf_specs.h index 89b5471b7e..84a915983c 100644 --- a/libr/bin/format/elf/elf_specs.h +++ b/libr/bin/format/elf/elf_specs.h @@ -58,7 +58,7 @@ #ifndef _INCLUDE_ELF_SPECS_H #define _INCLUDE_ELF_SPECS_H -#define ELF_STRING_LENGTH 256 +#define ELF_STRING_LENGTH 256 // not strictly ELF, but close enough: #define CGCMAG "\177CGC" diff --git a/libr/bin/p/bin_mach0.c b/libr/bin/p/bin_mach0.c index 56fd512c0d..b96c6710b5 100644 --- a/libr/bin/p/bin_mach0.c +++ b/libr/bin/p/bin_mach0.c @@ -283,7 +283,6 @@ static RList* relocs(RBinFile *arch) { if (arch && arch->o) { bin = arch->o->bin_obj; } - if (!obj || !obj->bin_obj || !(ret = r_list_newf (free))) return NULL; ret->free = free; diff --git a/libr/cons/cons.c b/libr/cons/cons.c index 603ec691f9..c67cb13e05 100644 --- a/libr/cons/cons.c +++ b/libr/cons/cons.c @@ -26,6 +26,16 @@ typedef struct { RConsGrep *grep; } RConsStack; +typedef struct { + bool breaked; + void *data; + RConsEvent event_interrupt; +} RConsBreakStack; + +static void break_stack_free(void *ptr) { + RConsBreakStack *b = (RConsBreakStack*)ptr; + free (b); +} static void cons_stack_free(void *ptr) { RConsStack *s = (RConsStack *)ptr; @@ -165,13 +175,49 @@ R_API RCons *r_cons_singleton () { return &I; } -R_API void r_cons_break(void (*cb)(void *u), void *user) { +R_API void r_cons_break_clear() { I.breaked = false; - I.event_interrupt = cb; - I.data = user; +} + +R_API void r_cons_break_push(RConsBreak cb, void *user) { + if (I.break_stack) { + //if we don't have any element in the stack start the signal + RConsBreakStack *b = R_NEW0 (RConsBreakStack); + if (!b) return; + if (r_stack_is_empty (I.break_stack)) { #if __UNIX__ || __CYGWIN__ - signal (SIGINT, break_signal); + signal (SIGINT, break_signal); #endif + I.breaked = false; + } + //save the actual state + b->event_interrupt = I.event_interrupt; + b->data = I.data; + r_stack_push (I.break_stack, b); + //configure break + I.event_interrupt = cb; + I.data = user; + } +} + +R_API void r_cons_break_pop() { + //restore old state + if (I.break_stack) { + RConsBreakStack *b = NULL; + r_print_set_interrupted (I.breaked); + b = r_stack_pop (I.break_stack); + if (b) { + I.event_interrupt = b->event_interrupt; + I.data = b->data; + break_stack_free (b); + } else { + //there is not more elements in the stack +#if __UNIX__ || __CYGWIN__ + signal (SIGINT, SIG_IGN); +#endif + I.breaked = false; + } + } } R_API bool r_cons_is_breaked() { @@ -184,6 +230,14 @@ R_API void r_cons_break_end() { #if __UNIX__ || __CYGWIN__ signal (SIGINT, SIG_IGN); #endif + if (!r_stack_is_empty (I.break_stack)) { + //free all the stack + r_stack_free (I.break_stack); + //create another one + I.break_stack = r_stack_newf (6, break_stack_free); + I.data = NULL; + I.event_interrupt = NULL; + } } #if __WINDOWS__ && !__CYGWIN__ @@ -285,6 +339,7 @@ R_API RCons *r_cons_new() { I.truecolor = 0; I.mouse = 0; I.cons_stack = r_stack_newf (6, cons_stack_free); + I.break_stack = r_stack_newf (6, break_stack_free); r_cons_pal_null (); r_cons_pal_init (NULL); r_cons_rgb_init (); @@ -307,6 +362,7 @@ R_API RCons *r_cons_free() { I.buffer = NULL; } r_stack_free (I.cons_stack); + r_stack_free (I.break_stack); return NULL; } @@ -566,7 +622,7 @@ R_API void r_cons_flush() { char *nl = strchr (ptr, '\n'); int len = I.buffer_len; I.buffer[I.buffer_len] = 0; - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); while (nl && !r_cons_is_breaked ()) { r_cons_write (ptr, nl - ptr + 1); if (!(i % pagesize)) { @@ -577,7 +633,7 @@ R_API void r_cons_flush() { i++; } r_cons_write (ptr, I.buffer + len - ptr); - r_cons_break_end (); + r_cons_break_pop (); } else { r_cons_write (I.buffer, I.buffer_len); } diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index 433aae1420..5eed280bfc 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -72,19 +72,28 @@ R_API int r_line_dietline_init() { static int r_line_readchar_utf8(unsigned char *s, int slen) { // TODO: add support for w32 int ret, len; - for (len = 0; len+20) break; + } + if ((s[len] & 0xc0) != 0x80) { + continue; + } + if (len > 0) { + break; + } } len++; s[len] = 0; @@ -114,14 +123,16 @@ do_it_again: SetConsoleMode (h, 0); // RAW *vch = 0; ret = ReadConsoleInput (h, irInBuf, 128, &out); - if (ret<1) { + if (ret < 1) { return 0; } for (i = 0; i < out; i++) { - if (irInBuf[i].EventType!=KEY_EVENT) + if (irInBuf[i].EventType!=KEY_EVENT) { continue; - if (!irInBuf[i].Event.KeyEvent.bKeyDown) + } + if (!irInBuf[i].Event.KeyEvent.bKeyDown) { continue; + } *buf = irInBuf[i].Event.KeyEvent.uChar.AsciiChar; bCtrl = irInBuf[i].Event.KeyEvent.dwControlKeyState & 8; if (irInBuf[i].Event.KeyEvent.uChar.AsciiChar) { @@ -139,8 +150,9 @@ do_it_again: } } SetConsoleMode (h, mode); - if (buf[0]==0 && *vch==0) + if (buf[0] == 0 && *vch == 0) { goto do_it_again; + } return buf[0]; } #endif @@ -149,7 +161,7 @@ static int r_line_readchar() { *buf = '\0'; #if __WINDOWS__ && !__CYGWIN__ #if 1 // new implementation for read input at windows by skuater. If something fail set this to 0 - int dummy=0; + int dummy = 0; return r_line_readchar_win(&dummy); #endif BOOL ret; @@ -166,8 +178,9 @@ do_it_again: SetConsoleMode (h, 0); // RAW ret = ReadConsole (h, buf, 1, &out, NULL); // wine hack-around - if (!ret && read (0, buf, 1) != 1) + if (!ret && read (0, buf, 1) != 1) { return -1; + } SetConsoleMode (h, mode); #else do { @@ -175,42 +188,51 @@ do_it_again: ret = read (0, buf, 1); buf[0] = r_cons_controlz (buf[0]); // VTE HOME/END support - if (buf[0]==79) { - if (read (0, buf, 1) != 1) + if (buf[0] == 79) { + if (read (0, buf, 1) != 1) { return -1; - if (buf[0]==70) { + } + if (buf[0] == 70) { return 5; - } else if (buf[0]==72) { + } else if (buf[0] == 72) { return 1; } return 0; } - if (ret == -1) return 0; // read no char - if (!buf[0] || ret == 0) return -1; // eof + if (ret == -1) { + return 0; // read no char + } + if (!buf[0] || !ret) { + return -1; // eof + } // TODO: add support for other invalid chars - if (*buf==0xc2 || *buf==0xc3) { - read (0, buf+1, 1); + if (*buf == 0xc2 || *buf == 0xc3) { + read (0, buf + 1, 1); *buf = '\0'; } } while (*buf == '\0'); #endif #if ONLY_VALID_CHARS - if (!is_valid_char (buf[0])) + if (!is_valid_char (buf[0])) { goto do_it_again; + } #endif return buf[0]; } R_API int r_line_hist_add(const char *line) { - if (!I.history.data) + if (!I.history.data) { inithist (); - if (I.history.top >= I.history.size) + } + if (I.history.top >= I.history.size) { I.history.top = I.history.index = 0; // workaround + } /* ignore dup */ if (I.history.index > 0) { const char *data = I.history.data[I.history.index - 1]; - if (data && !strcmp (line, data)) + if (data && !strcmp (line, data)) { return false; + } } if (line && *line) { // && I.history.index < I.history.size) { I.history.data[I.history.top++] = strdup (line); @@ -221,12 +243,14 @@ R_API int r_line_hist_add(const char *line) { } static int r_line_hist_up() { - if (I.hist_up) + if (I.hist_up) { return I.hist_up (I.user); - if (!I.history.data) + } + if (!I.history.data) { inithist (); - if (I.history.index>0) { - strncpy (I.buffer.data, I.history.data[--I.history.index], R_LINE_BUFSIZE-1); + } + if (I.history.index > 0) { + strncpy (I.buffer.data, I.history.data[--I.history.index], R_LINE_BUFSIZE - 1); I.buffer.index = I.buffer.length = strlen (I.buffer.data); return true; } @@ -234,21 +258,23 @@ static int r_line_hist_up() { } static int r_line_hist_down() { - if (I.hist_down) + if (I.hist_down) { return I.hist_down (I.user); + } I.buffer.index = 0; - if (!I.history.data) + if (!I.history.data) { inithist (); - if (I.history.index.history or so */ R_API int r_line_hist_load(const char *file) { FILE *fd; - char buf[R_LINE_BUFSIZE], - *path = r_str_home (file); - if (!path) + char buf[R_LINE_BUFSIZE], *path = r_str_home (file); + if (!path) { return false; + } if (!(fd = fopen (path, "r"))) { free (path); return false; } while (fgets (buf, sizeof (buf), fd) != NULL) { - buf[strlen (buf)-1] = 0; + buf[strlen (buf) - 1] = 0; r_line_hist_add (buf); } fclose (fd); @@ -321,13 +356,15 @@ R_API int r_line_hist_save(const char *file) { fd = fopen (path, "w"); if (fd != NULL) { if (I.history.data) { - for (i=0; i 0) { int tt = largv0; - if (*t != ' ') p[tt++] = ' '; + if (*t != ' ') { + p[tt++] = ' '; + } memmove (p + tt, t, len_t); } memcpy (p, argv[0], largv0); p[largv0] = ' '; - if (len_t == 0) p[largv0 + 1] = '\0'; + if (!len_t) { + p[largv0 + 1] = '\0'; + } I.buffer.length = strlen (I.buffer.data); I.buffer.index = (p - I.buffer.data) + largv0 + 1; } @@ -396,7 +437,9 @@ R_API void r_line_autocomplete() { // try to autocomplete argument for (i = 0; i < argc; i++) { j = 0; - if (!argv[i]) break; + if (!argv[i]) { + break; + } while (argv[i][j] == root[j] && root[j] != '\0') j++; if (j < min_common_len) { min_common_len = j; @@ -409,7 +452,7 @@ R_API void r_line_autocomplete() { p[tt + len_t] = '\0'; } memmove (p, root, min_common_len); - if (len_t == 0) p[min_common_len] = '\0'; + if (!len_t) p[min_common_len] = '\0'; I.buffer.length = strlen (I.buffer.data); I.buffer.index = (p - I.buffer.data) + min_common_len; } @@ -454,8 +497,8 @@ R_API const char *r_line_readline_cb_win(RLineReadCallback cb, void *user) { static int gcomp_idx = 0; static int gcomp = 0; signed char buf[10]; - int ch, i=0; /* grep completion */ - int vch=0; + int ch, i = 0; /* grep completion */ + int vch = 0; char *tmp_ed_cmd, prev = 0; HANDLE hClipBoard; char *clipText; @@ -464,13 +507,14 @@ R_API const char *r_line_readline_cb_win(RLineReadCallback cb, void *user) { I.buffer.data[0] = '\0'; if (I.contents) { memmove (I.buffer.data, I.contents, - R_MIN (strlen (I.contents)+1, R_LINE_BUFSIZE-1)); + R_MIN (strlen (I.contents) + 1, R_LINE_BUFSIZE - 1)); I.buffer.data[R_LINE_BUFSIZE-1] = '\0'; I.buffer.index = I.buffer.length = strlen (I.contents); } if (I.disable) { - if (!fgets (I.buffer.data, R_LINE_BUFSIZE-1, stdin)) + if (!fgets (I.buffer.data, R_LINE_BUFSIZE - 1, stdin)) { return NULL; + } I.buffer.data[strlen (I.buffer.data)] = '\0'; return (*I.buffer.data)? I.buffer.data : r_line_nullstr; } @@ -483,71 +527,72 @@ R_API const char *r_line_readline_cb_win(RLineReadCallback cb, void *user) { printf ("\x1b[0K\r%s%s", I.prompt, I.buffer.data); fflush (stdout); } - r_cons_singleton()->breaked = false; + r_cons_break_push (NULL, NULL); for (;;) { -#if 0 - if (I.echo) { - printf(" ("); - for(i=1;i= columns) break; - printf("%s ", argv[i]); - } - } - printf(")"); - fflush(stdout); - } -#endif - I.buffer.data[I.buffer.length]='\0'; + I.buffer.data[I.buffer.length] = '\0'; if (cb && !cb (user, I.buffer.data)) { I.buffer.data[0] = 0; I.buffer.length = 0; } ch = r_line_readchar_win (&vch); - //eprintf("des ch=%c(%d) vch=%d\n",ch,vch); - if (ch == -1) return NULL; + if (ch == -1) { + r_cons_break_pop (); + return NULL; + } buf[0] = ch; - if (I.echo) + if (I.echo) { r_cons_clear_line (0); + } columns = r_cons_get_size (NULL)-2; - if (columns<1) + if (columns < 1) { columns = 40; - if (I.echo) + } + if (I.echo) { printf ("\r%*c\r", columns, ' '); - if (I.echo) + } + if (I.echo) { printf ("\r\x1b[2K\r"); //%*c\r", columns, ' '); + } /* process special at vch codes first*/ switch (vch) { case 37: // left arrow I.buffer.index = I.buffer.index? I.buffer.index-1: 0; break; case 38: // up arrow - if (gcomp) gcomp_idx++; - else if (r_line_hist_up ()==-1) - return NULL; + if (gcomp) { + gcomp_idx++; + } else if (r_line_hist_up () == -1) { + r_cons_break_pop (); + return NULL; + } break; case 39: // right arrow - I.buffer.index = I.buffer.index0) - gcomp_idx--; - } else if (r_line_hist_down ()==-1) + if (gcomp_idx > 0) { + gcomp_idx--; + } + } else if (r_line_hist_down () == -1) { + r_cons_break_pop (); return NULL; + } break; /* ctrl+arrows */ case 137:// ctrl+left arrow // previous word - for (i=I.buffer.index; i>0; i--) { - if (I.buffer.data[i] == ' ') { - I.buffer.index = i-1; - break; - } + for (i = I.buffer.index; i > 0; i--) { + if (I.buffer.data[i] == ' ') { + I.buffer.index = i - 1; + break; + } + } + if (I.buffer.data[i] != ' ') { + I.buffer.index = 0; } - if (I.buffer.data[i] != ' ') - I.buffer.index = 0; break; case 138:// ctrl+up arrow //first @@ -555,14 +600,15 @@ R_API const char *r_line_readline_cb_win(RLineReadCallback cb, void *user) { break; case 139:// ctrl+right arrow // next word - for (i=I.buffer.index; ibreaked = true; + gcomp = 0; goto _end; case 4: // ^D if (!I.buffer.data[0]) { /* eof */ - if (I.echo) printf ("^D\n"); + if (I.echo) { + printf ("^D\n"); + } r_cons_set_raw (false); + r_cons_break_pop (); return NULL; } - if (I.buffer.index0) { - for (i=I.buffer.index-1; i>0&&I.buffer.data[i]==' '; i--); - for (; i&&I.buffer.data[i]!=' '; i--); - if (!i) for (; i>0&&I.buffer.data[i]==' '; i--); - if (i>0) i++; else if (i<0) i=0; - if (I.buffer.index>I.buffer.length) + if (I.buffer.index > 0) { + for (i = I.buffer.index - 1; i > 0 && I.buffer.data[i] == ' '; i--) { + /*nothing to see here*/ + } + for (; i && I.buffer.data[i] != ' '; i--) { + /*nothing to see here*/ + } + if (!i) { + for (; i > 0 && I.buffer.data[i] == ' '; i--) { + /*nothing to see here*/ + } + } + if (i > 0) { + i++; + } else if (i < 0) { + i = 0; + } + if (I.buffer.index > I.buffer.length) { I.buffer.length = I.buffer.index; - memmove (I.buffer.data+i, - I.buffer.data+I.buffer.index, - I.buffer.length-I.buffer.index+1); + } + memmove (I.buffer.data + i, I.buffer.data + I.buffer.index, + I.buffer.length - I.buffer.index + 1); I.buffer.length = strlen (I.buffer.data); - I.buffer.index = i; + I.buffer.index = i; } break; case 24: // ^X -- do nothing but store in prev = *buf @@ -707,42 +786,51 @@ R_API const char *r_line_readline_cb_win(RLineReadCallback cb, void *user) { if (I.buffer.length < R_LINE_BUFSIZE) { I.buffer.index = I.buffer.length; strcat (I.buffer.data, I.clipboard); - } else I.buffer.length -= strlen (I.clipboard); + } else { + I.buffer.length -= strlen (I.clipboard); + } } break; case 14: // ^n if (gcomp) { - if (gcomp_idx>0) + if (gcomp_idx > 0) gcomp_idx--; - } else r_line_hist_down (); + } else { + r_line_hist_down (); + } break; case 16: // ^p if (gcomp) { gcomp_idx++; - } else r_line_hist_up (); + } else { + r_line_hist_up (); + } break; case 8: case 127: if (I.buffer.index < I.buffer.length) { - if (I.buffer.index>0) { + if (I.buffer.index > 0) { int len = 0; // TODO: WIP len = 1; I.buffer.index--; - memmove (I.buffer.data+I.buffer.index, - I.buffer.data+I.buffer.index+len, - strlen (I.buffer.data+I.buffer.index)); + memmove (I.buffer.data + I.buffer.index, + I.buffer.data + I.buffer.index + len, + strlen (I.buffer.data + I.buffer.index)); I.buffer.length -= len; I.buffer.data[I.buffer.length] = 0; } } else { // OK I.buffer.index = --I.buffer.length; - if (I.buffer.length<0) I.buffer.length=0; - I.buffer.data[I.buffer.length]='\0'; + if (I.buffer.length < 0) { + I.buffer.length = 0; + } + I.buffer.data[I.buffer.length] = '\0'; } - if (I.buffer.index<0) + if (I.buffer.index < 0) { I.buffer.index = 0; + } break; /* tab */ case 9: // tab @@ -750,38 +838,29 @@ R_API const char *r_line_readline_cb_win(RLineReadCallback cb, void *user) { break; /* enter */ case 13: - if (gcomp && I.buffer.length>0) { + if (gcomp && I.buffer.length > 0) { strncpy (I.buffer.data, gcomp_line, R_LINE_BUFSIZE-1); - I.buffer.data[R_LINE_BUFSIZE-1] = '\0'; + I.buffer.data[R_LINE_BUFSIZE - 1] = '\0'; I.buffer.length = strlen (gcomp_line); } gcomp_idx = gcomp = 0; goto _end; -#if 0 - // force command fit - for(i=1;iI.buffer.index; i--) + } + if (I.buffer.index < I.buffer.length) { + for (i = ++I.buffer.length; i > I.buffer.index; i--) { I.buffer.data[i] = I.buffer.data[i-1]; + } I.buffer.data[I.buffer.index] = buf[0]; } else { - I.buffer.data[I.buffer.length]=buf[0]; + I.buffer.data[I.buffer.length] = buf[0]; I.buffer.length++; - if (I.buffer.length>(R_LINE_BUFSIZE-1)) + if (I.buffer.length > (R_LINE_BUFSIZE - 1)) { I.buffer.length--; - I.buffer.data[I.buffer.length]='\0'; + } + I.buffer.data[I.buffer.length] = '\0'; } I.buffer.index++; break; @@ -790,42 +869,43 @@ R_API const char *r_line_readline_cb_win(RLineReadCallback cb, void *user) { if (I.echo) { if (gcomp) { gcomp_line = ""; - //if (I.buffer.length == 0) - // gcomp = 0; - if (I.history.data != NULL) - for (i=0; icols) { + if (I.buffer.index > cols) { printf ("< "); i = I.buffer.index-cols; - if (i>sizeof (I.buffer.data)) { - i = sizeof (I.buffer.data)-1; + if (i > sizeof (I.buffer.data)) { + i = sizeof (I.buffer.data) - 1; } - } else i = 0; - len = I.buffer.index-i; - if (len>0 && (i+len)<=I.buffer.length) - fwrite (I.buffer.data+i, 1, len, stdout); + } else { + i = 0; + } + len = I.buffer.index - i; + if (len > 0 && (i + len) <= I.buffer.length) { + fwrite (I.buffer.data + i, 1, len, stdout); + } } fflush (stdout); } } _end: + r_cons_break_pop (); r_cons_set_raw (0); if (I.echo) { printf ("\r%s%s\n", I.prompt, I.buffer.data); @@ -834,7 +914,6 @@ _end: // should be here or not? if (!memcmp (I.buffer.data, "!history", 8)) { - //if (I.buffer.data[0]=='!' && I.buffer.data[1]=='\0') { r_line_hist_list (); return r_line_nullstr; } @@ -856,20 +935,21 @@ R_API const char *r_line_readline_cb(RLineReadCallback cb, void *user) { #if USE_UTF8 int utflen; #endif - int ch, i=0; /* grep completion */ + int ch, i = 0; /* grep completion */ char *tmp_ed_cmd, prev = 0; I.buffer.index = I.buffer.length = 0; I.buffer.data[0] = '\0'; if (I.contents) { memmove (I.buffer.data, I.contents, - R_MIN (strlen (I.contents)+1, R_LINE_BUFSIZE-1)); + R_MIN (strlen (I.contents) + 1, R_LINE_BUFSIZE - 1)); I.buffer.data[R_LINE_BUFSIZE-1] = '\0'; I.buffer.index = I.buffer.length = strlen (I.contents); } if (I.disable) { - if (!fgets (I.buffer.data, R_LINE_BUFSIZE-1, stdin)) + if (!fgets (I.buffer.data, R_LINE_BUFSIZE-1, stdin)) { return NULL; + } I.buffer.data[strlen (I.buffer.data)] = '\0'; return (*I.buffer.data)? I.buffer.data : r_line_nullstr; } @@ -882,23 +962,30 @@ R_API const char *r_line_readline_cb(RLineReadCallback cb, void *user) { printf ("\x1b[0K\r%s%s", I.prompt, I.buffer.data); fflush (stdout); } - r_cons_singleton()->breaked = false; + r_cons_break_push (NULL, NULL); for (;;) { + if (r_cons_is_breaked ()) { + eprintf ("CATCH\n"); + break; + } I.buffer.data[I.buffer.length] = '\0'; if (cb && !cb (user, I.buffer.data)) { I.buffer.data[0] = 0; I.buffer.length = 0; } #if USE_UTF8 - utflen = r_line_readchar_utf8 ( - (ut8*)buf, sizeof (buf)); - if (utflen <1) { + utflen = r_line_readchar_utf8 ((ut8*)buf, sizeof (buf)); + if (utflen < 1) { + r_cons_break_pop (); return NULL; } buf[utflen] = 0; #else ch = r_line_readchar (); - if (ch == -1) return NULL; + if (ch == -1) { + r_cons_break_pop (); + return NULL; + } buf[0] = ch; #endif if (I.echo) { @@ -925,21 +1012,25 @@ R_API const char *r_line_readline_cb(RLineReadCallback cb, void *user) { break; case 2: // ^b // emacs left #if USE_UTF8 - { - char *s = I.buffer.data+I.buffer.index-1; - utflen = 1; - while (s>I.buffer.data && (*s & 0xc0) == 0x80) { - utflen++; - s--; - } - } - I.buffer.index = I.buffer.index? I.buffer.index-utflen: 0; + { + char *s = I.buffer.data+I.buffer.index-1; + utflen = 1; + while (s > I.buffer.data && (*s & 0xc0) == 0x80) { + utflen++; + s--; + } + } + I.buffer.index = I.buffer.index + ? I.buffer.index - utflen + : 0; #else - I.buffer.index = I.buffer.index? I.buffer.index-1: 0; + I.buffer.index = I.buffer.index + ? I.buffer.index - 1 + : 0; #endif break; case 5: // ^E - if (prev == 24) { // ^X = 0x18 + if (prev == 24) { // ^X = 0x18 I.buffer.data[I.buffer.length] = 0; // probably unnecessary tmp_ed_cmd = I.editor_cb (I.user, I.buffer.data); if (tmp_ed_cmd) { @@ -949,31 +1040,40 @@ R_API const char *r_line_readline_cb(RLineReadCallback cb, void *user) { I.buffer.index = I.buffer.length; strncpy (I.buffer.data, tmp_ed_cmd, R_LINE_BUFSIZE-1); I.buffer.data[R_LINE_BUFSIZE-1] = '\0'; - } else I.buffer.length -= strlen (tmp_ed_cmd); + } else { + I.buffer.length -= strlen (tmp_ed_cmd); + } free (tmp_ed_cmd); } - } else I.buffer.index = I.buffer.length; + } else { + I.buffer.index = I.buffer.length; + } break; case 3: // ^C - if (I.echo) + if (I.echo) { eprintf ("^C\n"); + } I.buffer.index = I.buffer.length = 0; *I.buffer.data = '\0'; - r_cons_singleton()->breaked = true; + gcomp = 0; goto _end; case 4: // ^D if (!I.buffer.data[0]) { /* eof */ - if (I.echo) + if (I.echo) { printf ("^D\n"); + } r_cons_set_raw (false); + r_cons_break_pop (); return NULL; } - if (I.buffer.index0) { + if (I.buffer.index > 0) { char *s; do { I.buffer.index--; @@ -1030,18 +1135,31 @@ R_API const char *r_line_readline_cb(RLineReadCallback cb, void *user) { I.buffer.index = 0; break; case 23: // ^W ^w - if (I.buffer.index>0) { - for (i=I.buffer.index-1; i>0&&I.buffer.data[i]==' '; i--); - for (; i&&I.buffer.data[i]!=' '; i--); - if (!i) for (; i>0&&I.buffer.data[i]==' '; i--); - if (i>0) i++; else if (i<0) i=0; - if (I.buffer.index>I.buffer.length) + if (I.buffer.index > 0) { + for (i = I.buffer.index - 1; i > 0 && I.buffer.data[i] == ' '; i--) { + /*nothing to see here*/ + } + for (; i && I.buffer.data[i] != ' '; i--) { + /*nothing to see here*/ + } + if (!i) { + for (; i > 0 && I.buffer.data[i] == ' '; i--) { + /*nothing to see here*/ + } + } + if (i > 0) { + i++; + } else if (i < 0) { + i = 0; + } + if (I.buffer.index > I.buffer.length) { I.buffer.length = I.buffer.index; - memmove (I.buffer.data+i, - I.buffer.data+I.buffer.index, - I.buffer.length-I.buffer.index+1); + } + memmove (I.buffer.data + i, + I.buffer.data + I.buffer.index, + I.buffer.length - I.buffer.index + 1); I.buffer.length = strlen (I.buffer.data); - I.buffer.index = i; + I.buffer.index = i; } break; case 24: // ^X -- do nothing but store in prev = *buf @@ -1053,170 +1171,200 @@ R_API const char *r_line_readline_cb(RLineReadCallback cb, void *user) { if (I.buffer.length < R_LINE_BUFSIZE) { I.buffer.index = I.buffer.length; strcat (I.buffer.data, I.clipboard); - } else I.buffer.length -= strlen (I.clipboard); + } else { + I.buffer.length -= strlen (I.clipboard); + } } break; case 14: // ^n if (gcomp) { - if (gcomp_idx>0) + if (gcomp_idx > 0) { gcomp_idx--; - } else r_line_hist_down (); + } + } else { + r_line_hist_down (); + } break; case 16: // ^p if (gcomp) { gcomp_idx++; - } else r_line_hist_up (); + } else { + r_line_hist_up (); + } break; case 27: //esc-5b-41-00-00 buf[0] = r_line_readchar(); switch (buf[0]) { - case -1: return NULL; - case 1: // begin - I.buffer.index = 0; - break; - case 5: // end - I.buffer.index = I.buffer.length; - break; - case 'B': - case 'b': - // previous word - for (i = I.buffer.index - 2; i >= 0; i--) { - if (I.buffer.data[i] == ' ' && I.buffer.data[i + 1] != ' ') { - I.buffer.index = i + 1; - break; - } - } - if (i < 0) - I.buffer.index = 0; - break; - case 'F': - case 'f': - // next word - for (i = I.buffer.index + 1; i < I.buffer.length; i++) { - if (I.buffer.data[i] != ' ' && I.buffer.data[i - 1] == ' ') { - I.buffer.index = i; - break; - } - } - if (i >= I.buffer.length) - I.buffer.index = I.buffer.length; - break; - default: - buf[1] = r_line_readchar(); - if (buf[1] == -1) - return NULL; - if (buf[0]==0x5b) { // [ - switch (buf[1]) { - case 0x33: // supr - if (I.buffer.index0) - gcomp_idx--; - } else if (r_line_hist_down ()==-1) - return NULL; - break; - case 0x43: // C --> right arrow + case -1: + r_cons_break_pop (); + return NULL; + case 1: // begin + I.buffer.index = 0; + break; + case 5: // end + I.buffer.index = I.buffer.length; + break; + case 'B': + case 'b': + // previous word + for (i = I.buffer.index - 2; i >= 0; i--) { + if (I.buffer.data[i] == ' ' && I.buffer.data[i + 1] != ' ') { + I.buffer.index = i + 1; + break; + } + } + if (i < 0) { + I.buffer.index = 0; + } + break; + case 'F': + case 'f': + // next word + for (i = I.buffer.index + 1; i < I.buffer.length; i++) { + if (I.buffer.data[i] != ' ' && I.buffer.data[i - 1] == ' ') { + I.buffer.index = i; + break; + } + } + if (i >= I.buffer.length) { + I.buffer.index = I.buffer.length; + } + break; + default: + buf[1] = r_line_readchar(); + if (buf[1] == -1) { + r_cons_break_pop (); + return NULL; + } + if (buf[0] == 0x5b) { // [ + switch (buf[1]) { + case 0x33: // supr + if (I.buffer.index < I.buffer.length) { + memmove (I.buffer.data + I.buffer.index, + I.buffer.data + I.buffer.index + 1, + strlen (I.buffer.data + I.buffer.index + 1) + 1); + } + buf[1] = r_line_readchar (); + if (buf[1] == -1) { + r_cons_break_pop (); + return NULL; + } + break; + /* arrows */ + case 0x41: + if (gcomp) { + gcomp_idx++; + } else if (r_line_hist_up () == -1) { + r_cons_break_pop (); + return NULL; + } + break; + case 0x42: + if (gcomp) { + if (gcomp_idx > 0) { + gcomp_idx--; + } + } else if (r_line_hist_down () == -1) { + r_cons_break_pop (); + return NULL; + } + break; + case 0x43: // C --> right arrow #if USE_UTF8 - { - char *s = I.buffer.data+I.buffer.index+1; - utflen = 1; - while ((*s & 0xc0) == 0x80) { - utflen++; - s++; - } - I.buffer.index = I.buffer.index left arrow + break; + case 0x44: // D --> left arrow #if USE_UTF8 - { - char *s = I.buffer.data+I.buffer.index-1; - utflen = 1; - while (s>I.buffer.data && (*s & 0xc0) == 0x80) { - utflen++; - s--; - } - } - I.buffer.index = I.buffer.index? I.buffer.index-utflen: 0; + { + char *s = I.buffer.data+I.buffer.index-1; + utflen = 1; + while (s > I.buffer.data && (*s & 0xc0) == 0x80) { + utflen++; + s--; + } + } + I.buffer.index = I.buffer.index + ? I.buffer.index - utflen + : 0; #else - I.buffer.index = I.buffer.index? I.buffer.index-1: 0; + I.buffer.index = I.buffer.index + ? I.buffer.index - 1 + : 0; #endif - break; - case 0x31: // control + arrow - ch = r_cons_readchar (); - if (ch == 0x7e) { // HOME in screen/tmux - // corresponding END is 0x34 below (the 0x7e is ignored there) - I.buffer.index = 0; - break; - } - r_cons_readchar (); - ch = r_cons_readchar (); - switch (ch) { - case 0x41: - //first - I.buffer.index = 0; - break; - case 0x44: - // previous word - for (i=I.buffer.index; i>0; i--) { - if (I.buffer.data[i] == ' ') { - I.buffer.index = i-1; - break; - } - } - if (I.buffer.data[i] != ' ') - I.buffer.index = 0; - break; - case 0x42: - //end - I.buffer.index = I.buffer.length; - break; - case 0x43: - // next word - for (i=I.buffer.index; i 0; i--) { + if (I.buffer.data[i] == ' ') { + I.buffer.index = i-1; + break; + } + } + if (I.buffer.data[i] != ' ') { + I.buffer.index = 0; + } + break; + case 0x42: + //end + I.buffer.index = I.buffer.length; + break; + case 0x43: + // next word + for (i = I.buffer.index; i < I.buffer.length; i++) { + if (I.buffer.data[i] == ' ') { + I.buffer.index = i+1; + break; + } + } + if (I.buffer.data[i] != ' ') { + I.buffer.index = I.buffer.length; + } + break; + } + r_cons_set_raw (1); + break; + case 0x37: // HOME xrvt-unicode + r_cons_readchar (); + case 0x48: // HOME + I.buffer.index = 0; + break; + case 0x34: // END + case 0x38: // END xrvt-unicode + r_cons_readchar (); + case 0x46: // END + I.buffer.index = I.buffer.length; + break; + } + } } break; case 8: @@ -1277,9 +1425,10 @@ R_API const char *r_line_readline_cb(RLineReadCallback cb, void *user) { gcomp_idx = gcomp = 0; goto _end; default: - if (gcomp) + if (gcomp) { gcomp++; - if (I.buffer.indexcols) { + if (I.buffer.index > cols) { printf ("< "); i = I.buffer.index-cols; - if (i>sizeof (I.buffer.data)) { + if (i > sizeof (I.buffer.data)) { i = sizeof (I.buffer.data)-1; } - } else i = 0; + } else { + i = 0; + } len = I.buffer.index-i; - if (len>0 && (i+len)<=I.buffer.length) + if (len > 0 && (i + len) <= I.buffer.length) { fwrite (I.buffer.data+i, 1, len, stdout); + } } fflush (stdout); } } _end: + r_cons_break_pop (); r_cons_set_raw (0); if (I.echo) { printf ("\r%s%s\n", I.prompt, I.buffer.data); diff --git a/libr/cons/input.c b/libr/cons/input.c index 82b82ea2aa..b21a95f5db 100644 --- a/libr/cons/input.c +++ b/libr/cons/input.c @@ -240,7 +240,9 @@ R_API int r_cons_fgets(char *buf, int len, int argc, const char **argv) { RETURN (-1); } if (feof (cons->fdin)) { - if (color) printf (Color_RESET); + if (color) { + printf (Color_RESET); + } RETURN (-2); } buf[strlen (buf)-1] = '\0'; diff --git a/libr/core/anal.c b/libr/core/anal.c index afce210a93..bac4853f8f 100755 --- a/libr/core/anal.c +++ b/libr/core/anal.c @@ -2353,7 +2353,7 @@ R_API int r_core_anal_search(RCore *core, ut64 from, ut64 to, ut64 ref) { free (buf); return -1; } - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); if (core->blocksize > OPSZ) { if (bckwrds) { if (from + core->blocksize > to) { @@ -2370,7 +2370,6 @@ R_API int r_core_anal_search(RCore *core, ut64 from, ut64 to, ut64 ref) { if (r_cons_is_breaked ()) { break; } - r_cons_break (NULL, NULL); // TODO: this can be probably enhaced ret = r_io_read_at (core->io, at, buf, core->blocksize); if (ret != core->blocksize) { @@ -2383,7 +2382,6 @@ R_API int r_core_anal_search(RCore *core, ut64 from, ut64 to, ut64 ref) { if (r_cons_is_breaked ()) { break; } - r_cons_break (NULL, NULL); r_anal_op_fini (&op); if (!r_anal_op (core->anal, &op, at + i, buf + i, core->blocksize - i)) { @@ -2453,7 +2451,7 @@ R_API int r_core_anal_search(RCore *core, ut64 from, ut64 to, ut64 ref) { } else { eprintf ("error: block size too small\n"); } - r_cons_break_end (); + r_cons_break_pop (); free (buf); r_anal_op_fini (&op); return count; @@ -2488,21 +2486,18 @@ R_API int r_core_anal_search_xrefs(RCore *core, ut64 from, ut64 to, int rad) { r_cons_printf ("{"); } r_io_use_desc (core->io, core->file->desc); - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); at = from; - while (at < to && !r_cons_singleton()->breaked) { + while (at < to && !r_cons_is_breaked ()) { int i, ret; ret = r_io_read_at (core->io, at, buf, core->blocksize); if (ret != core->blocksize && at+ret-OPSZ < to) { break; } i = 0; - while (at+i < to && i < ret-OPSZ) { + while (at + i < to && i < ret-OPSZ && !r_cons_is_breaked ()) { RAnalRefType type; ut64 xref_from, xref_to; - if (r_cons_singleton()->breaked) { - break; - } xref_from = at+i; r_anal_op_fini (&op); ret = r_anal_op (core->anal, &op, at+i, buf+i, core->blocksize-i); @@ -2571,9 +2566,7 @@ R_API int r_core_anal_search_xrefs(RCore *core, ut64 from, ut64 to, int rad) { RIOSection *s; r_list_foreach (core->io->sections, iter, s) { if (xref_to >= s->vaddr && xref_to < s->vaddr + s->vsize) { - if (s->vaddr != 0) { - break; - } + if (s->vaddr) break; } } if (!iter) { @@ -2612,8 +2605,7 @@ R_API int r_core_anal_search_xrefs(RCore *core, ut64 from, ut64 to, int rad) { case R_ANAL_REF_TYPE_DATA: cmd = "axd"; break; default: cmd = "ax"; break; } - r_cons_printf ("%s 0x%08"PFMT64x" 0x%08"PFMT64x"\n", - cmd, xref_to, xref_from); + r_cons_printf ("%s 0x%08"PFMT64x" 0x%08"PFMT64x"\n", cmd, xref_to, xref_from); if (cfg_anal_strings) { char *str_flagname = is_string_at (core, xref_to, &len); if (str_flagname) { @@ -2627,16 +2619,14 @@ R_API int r_core_anal_search_xrefs(RCore *core, ut64 from, ut64 to, int rad) { } } } - count++; } at += i; } - r_cons_break_end (); + r_cons_break_pop (); free (buf); r_anal_op_fini (&op); - if (rad == 'j') { r_cons_printf ("}\n"); } @@ -2677,7 +2667,7 @@ R_API int r_core_anal_all(RCore *core) { r_core_cmd0 (core, "af"); } - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); /* Main */ if ((binmain = r_bin_get_sym (core->bin, R_BIN_SYM_MAIN)) != NULL) { ut64 addr = r_bin_get_vaddr (core->bin, binmain->paddr, binmain->vaddr); @@ -2692,7 +2682,7 @@ R_API int r_core_anal_all(RCore *core) { /* Symbols (Imports are already analyzed by rabin2 on init) */ if ((list = r_bin_get_symbols (core->bin)) != NULL) { r_list_foreach (list, iter, symbol) { - if (core->cons->breaked) { + if (r_cons_is_breaked ()) { break; } if (isValidSymbol (symbol)) { @@ -2706,18 +2696,21 @@ R_API int r_core_anal_all(RCore *core) { if (anal_vars) { /* Set fcn type to R_ANAL_FCN_TYPE_SYM for symbols */ r_list_foreach (core->anal->fcns, iter, fcni) { - if (core->cons->breaked) + if (r_cons_is_breaked ()) { break; + } if (r_config_get_i (core->config, "anal.vars")) { r_anal_var_delete_all (core->anal, fcni->addr, 'r'); r_anal_var_delete_all (core->anal, fcni->addr, 'b'); r_anal_var_delete_all (core->anal, fcni->addr, 's'); fcn_callconv (core, fcni); } - if (!strncmp (fcni->name, "sym.", 4) || !strncmp (fcni->name, "main", 4)) + if (!strncmp (fcni->name, "sym.", 4) || !strncmp (fcni->name, "main", 4)) { fcni->type = R_ANAL_FCN_TYPE_SYM; + } } } + r_cons_break_pop (); return true; } @@ -2871,7 +2864,7 @@ R_API void r_core_anal_stats_free (RCoreAnalStats *s) { free (s); } -R_API RList* r_core_anal_cycles (RCore *core, int ccl) { +R_API RList* r_core_anal_cycles(RCore *core, int ccl) { ut64 addr = core->offset; int depth = 0; RAnalOp *op = NULL; @@ -2882,7 +2875,8 @@ R_API RList* r_core_anal_cycles (RCore *core, int ccl) { return NULL; } cf = r_anal_cycle_frame_new (); - while (cf && !core->cons->breaked) { + r_cons_break_push (NULL, NULL); + while (cf && !r_cons_is_breaked ()) { if ((op = r_core_anal_op (core, addr)) && (op->cycles) && (ccl > 0)) { r_cons_clear_line (1); eprintf ("%i -- ", ccl); @@ -3034,7 +3028,7 @@ R_API RList* r_core_anal_cycles (RCore *core, int ccl) { } r_anal_op_free (op); } - if (core->cons->breaked) { + if (r_cons_is_breaked ()) { while (cf) { ch = r_list_pop (cf->hooks); while (ch) { @@ -3046,6 +3040,7 @@ R_API RList* r_core_anal_cycles (RCore *core, int ccl) { cf = prev; } } + r_cons_break_pop (); return hooks; } @@ -3189,7 +3184,7 @@ static int esilbreak_mem_read(RAnalEsil *esil, ut64 addr, ut8 *buf, int len) { } static bool esil_anal_stop = false; -static void cccb(void*u) { +static void cccb(void *u) { esil_anal_stop = true; eprintf ("^C\n"); } @@ -3293,7 +3288,7 @@ R_API void r_core_anal_esil(RCore *core, const char *str, const char *target) { return; } esil_anal_stop = false; - r_cons_break (cccb, core); + r_cons_break_push (cccb, core); int opalign = r_anal_archinfo (core->anal, R_ANAL_ARCHINFO_ALIGN); int in = r_syscall_get_swi (core->anal->syscall); @@ -3352,10 +3347,8 @@ R_API void r_core_anal_esil(RCore *core, const char *str, const char *target) { } (void)r_anal_esil_parse (ESIL, esilstr); // looks like ^C is handled by esil_parse !!!! - r_cons_break (cccb, core); //r_anal_esil_dumpstack (ESIL); r_anal_esil_stack_free (ESIL); - switch (op.type) { case R_ANAL_OP_TYPE_LEA: if ((target && op.ptr == ntarget) || !target) { @@ -3457,5 +3450,5 @@ R_API void r_core_anal_esil(RCore *core, const char *str, const char *target) { } free (buf); free (op.mnemonic); - r_cons_break_end (); + r_cons_break_pop (); } diff --git a/libr/core/anal_tp.c b/libr/core/anal_tp.c index d27d383463..e27433d966 100644 --- a/libr/core/anal_tp.c +++ b/libr/core/anal_tp.c @@ -67,13 +67,20 @@ static void type_match(RCore *core, ut64 addr, char *name) { const char *bp_name = r_reg_get_name (anal->reg, R_REG_NAME_BP); ut64 sp = r_reg_getv (anal->reg, sp_name); ut64 bp = r_reg_getv (anal->reg, bp_name); + r_cons_break_push (NULL, NULL); for (i = 0; i < max; i++) { + if (r_cons_is_breaked ()) { + goto out_function; + } char *type = r_anal_type_func_args_type (anal, fcn_name, i); - const char *name =r_anal_type_func_args_name (anal, fcn_name, i); + const char *name = r_anal_type_func_args_name (anal, fcn_name, i); const char *place = r_anal_cc_arg (anal, cc, i + 1); if (!strcmp (place, "stack")) { // type_match_stack (); for (j = idx; j >= 0; j--) { + if (r_cons_is_breaked ()) { + goto out_function; + } ut64 write_addr = sdb_num_get (trace, sdb_fmt (-1, "%d.mem.write", j), 0); if (write_addr == sp + size) { ut64 instr_addr = sdb_num_get (trace, sdb_fmt (-1, "%d.addr", j), 0); @@ -84,7 +91,7 @@ static void type_match(RCore *core, ut64 addr, char *name) { for (i2 = 0; i2 < array_size; i2++) { if (bp_name) { int bp_idx = sdb_array_get_num (trace, tmp, i2, 0) - bp; - if ((v =r_anal_var_get (anal, addr, R_ANAL_VAR_KIND_BPV, 1, bp_idx))) { + if ((v = r_anal_var_get (anal, addr, R_ANAL_VAR_KIND_BPV, 1, bp_idx))) { r_anal_var_retype (anal, addr, 1, bp_idx, R_ANAL_VAR_KIND_BPV, type, -1, v->name); r_anal_var_free (v); } @@ -104,13 +111,19 @@ static void type_match(RCore *core, ut64 addr, char *name) { free (type); int k; for ( k = max -1; k >=i; k--) { + if (r_cons_is_breaked ()) { + goto out_function; + } type = r_anal_type_func_args_type (anal, fcn_name, k); - name =r_anal_type_func_args_name (anal, fcn_name, k); + name = r_anal_type_func_args_name (anal, fcn_name, k); place = r_anal_cc_arg (anal, cc, k + 1); if (strcmp (place ,"stack_rev")) { break; } for (j = idx; j >= 0; j--) { + if (r_cons_is_breaked ()) { + goto out_function; + } ut64 write_addr = sdb_num_get (trace, sdb_fmt (-1, "%d.mem.write", j), 0); if (write_addr == sp + size) { ut64 instr_addr = sdb_num_get (trace, sdb_fmt (-1, "%d.addr", j), 0); @@ -121,7 +134,7 @@ static void type_match(RCore *core, ut64 addr, char *name) { for (i2 = 0; i2 < array_size; i2++) { if (bp_name) { int bp_idx = sdb_array_get_num (trace, tmp, i2, 0) - bp; - if ((v =r_anal_var_get (anal, addr, R_ANAL_VAR_KIND_BPV, 1, bp_idx))) { + if ((v = r_anal_var_get (anal, addr, R_ANAL_VAR_KIND_BPV, 1, bp_idx))) { r_anal_var_retype (anal, addr, 1, bp_idx, R_ANAL_VAR_KIND_BPV, type, -1, v->name); r_anal_var_free (v); } @@ -142,6 +155,9 @@ static void type_match(RCore *core, ut64 addr, char *name) { } else { // type_match_reg (); for (j = idx; j >= 0; j--) { + if (r_cons_is_breaked ()) { + goto out_function; + } if (sdb_array_contains (trace, sdb_fmt (-1, "%d.reg.write", j), place, 0)) { ut64 instr_addr = sdb_num_get (trace, sdb_fmt (-1, "%d.addr", j), 0); r_meta_set_string (core->anal, R_META_TYPE_COMMENT, instr_addr, @@ -149,15 +165,18 @@ static void type_match(RCore *core, ut64 addr, char *name) { char *tmp = sdb_fmt (-1, "%d.mem.read", j); int i2, array_size = sdb_array_size (trace, tmp); for (i2 = 0; i2 < array_size; i2++) { + if (r_cons_is_breaked ()) { + goto out_function; + } if (bp_name) { int bp_idx = sdb_array_get_num (trace, tmp, i2, 0) - bp; - if ((v =r_anal_var_get (anal, addr, R_ANAL_VAR_KIND_BPV, 1, bp_idx))) { + if ((v = r_anal_var_get (anal, addr, R_ANAL_VAR_KIND_BPV, 1, bp_idx))) { r_anal_var_retype (anal, addr, 1, bp_idx, R_ANAL_VAR_KIND_BPV, type, -1, v->name); r_anal_var_free (v); } } int sp_idx = sdb_array_get_num (trace, tmp, i2, 0) - sp; - if ((v =r_anal_var_get (anal, addr, R_ANAL_VAR_KIND_SPV, 1, sp_idx))) { + if ((v = r_anal_var_get (anal, addr, R_ANAL_VAR_KIND_SPV, 1, sp_idx))) { r_anal_var_retype (anal, addr, 1, sp_idx, R_ANAL_VAR_KIND_SPV, type, -1, v->name); r_anal_var_free (v); } @@ -168,6 +187,8 @@ static void type_match(RCore *core, ut64 addr, char *name) { } free (type); } +out_function: + r_cons_break_pop (); free (fcn_name); } @@ -219,23 +240,16 @@ R_API void r_core_anal_type_match(RCore *core, RAnalFunction *fcn) { ut64 addr = fcn->addr; r_reg_setv (core->dbg->reg, pc, fcn->addr); r_debug_reg_sync (core->dbg, R_REG_TYPE_ALL, true); - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); while (!r_cons_is_breaked ()) { RAnalOp *op = r_core_anal_op (core, addr); int loop_count = sdb_num_get (core->anal->esil->db_trace, sdb_fmt (-1, "0x%"PFMT64x".count", addr), 0); if (loop_count > LOOP_MAX) { -#if 0 - eprintf ("Unfortunately your evilly engineered %s function trapped my most innocent `aftm` in an infinite loop.\n", fcn->name); - eprintf ("I kept trace log for you to review and find out how bad things were going to happen by yourself.\n"); - eprintf ("You can view this log by `ate`. Meanwhile, I will train on how to behave with such behaviour without bothering you.\n"); -#endif - r_anal_emul_restore (core, esil_var); - return; + goto out; } sdb_num_set (core->anal->esil->db_trace, sdb_fmt (-1, "0x%"PFMT64x".count", addr), loop_count + 1, 0); if (!op || op->type == R_ANAL_OP_TYPE_RET) { - r_anal_emul_restore (core, esil_var); - return; + goto out; } if (op->type == R_ANAL_OP_TYPE_CALL) { RAnalFunction *fcn_call = r_anal_get_fcn_in (core->anal, op->jump, -1); @@ -258,10 +272,12 @@ R_API void r_core_anal_type_match(RCore *core, RAnalFunction *fcn) { } else { r_core_esil_step (core, UT64_MAX, NULL); r_anal_op_free (op); + } r_core_cmd0 (core, ".ar*"); addr = r_reg_getv (core->anal->reg, pc); } - r_cons_break_end (); +out: + r_cons_break_pop (); r_anal_emul_restore (core, esil_var); } diff --git a/libr/core/asm.c b/libr/core/asm.c index 798bb383dd..17c08a817c 100644 --- a/libr/core/asm.c +++ b/libr/core/asm.c @@ -68,14 +68,16 @@ R_API RList *r_core_asm_strsearch(RCore *core, const char *input, ut64 from, ut6 int tokcount, matchcount, count = 0; int matches = 0; - if (!*input) + if (!*input) { return NULL; + } if (core->blocksize <= OPSZ) { eprintf ("error: block size too small\n"); return NULL; } - if (!(buf = (ut8 *)calloc (core->blocksize, 1))) + if (!(buf = (ut8 *)calloc (core->blocksize, 1))) { return NULL; + } if (!(ptr = strdup (input))) { free (buf); return NULL; @@ -86,21 +88,22 @@ R_API RList *r_core_asm_strsearch(RCore *core, const char *input, ut64 from, ut6 return NULL; } tokens[0] = NULL; - for (tokcount=0; tokcount<(sizeof (tokens) / sizeof (char*)) - 1; tokcount++) { + for (tokcount = 0; tokcount < (sizeof (tokens) / sizeof (char*)) - 1; tokcount++) { tok = strtok (tokcount? NULL: ptr, ";"); - if (!tok) - break; + if (!tok) break; tokens[tokcount] = r_str_trim_head_tail (tok); } tokens[tokcount] = NULL; - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); for (at = from, matchcount = 0; at < to; at += core->blocksize-OPSZ) { matches = 0; - if (r_cons_singleton ()->breaked) + if (r_cons_is_breaked ()) { break; + } ret = r_io_read_at (core->io, at, buf, core->blocksize); - if (ret != core->blocksize) + if (ret != core->blocksize) { break; + } idx = 0, matchcount = 0; while (idx < core->blocksize) { ut64 addr = at + idx; @@ -108,33 +111,37 @@ R_API RList *r_core_asm_strsearch(RCore *core, const char *input, ut64 from, ut6 op.buf_asm[0] = 0; op.buf_hex[0] = 0; if (!(len = r_asm_disassemble (core->assembler, &op, buf+idx, core->blocksize-idx))) { - idx = (matchcount)? tidx+1: idx+1; + idx = (matchcount)? tidx + 1: idx + 1; matchcount = 0; continue; } matches = true; - if (!strcmp (op.buf_asm, "unaligned")) - matches = false; - if (!strcmp (op.buf_asm, "invalid")) + if (!strcmp (op.buf_asm, "unaligned")) { + matches = false; + } + if (!strcmp (op.buf_asm, "invalid")) { matches = false; + } if (matches && tokens[matchcount]) { - if (!regexp) matches = strstr(op.buf_asm, tokens[matchcount]) != NULL; - else { + if (!regexp) { + matches = strstr(op.buf_asm, tokens[matchcount]) != NULL; + } else { rx = r_regex_new (tokens[matchcount], ""); matches = r_regex_exec (rx, op.buf_asm, 0, 0, 0) == 0; r_regex_free (rx); } } - if (align && align>1) { + if (align && align > 1) { if (addr % align) { matches = false; } } if (matches) { code = r_str_concatf (code, "%s; ", op.buf_asm); - if (matchcount == tokcount-1) { - if (tokcount == 1) + if (matchcount == tokcount - 1) { + if (tokcount == 1) { tidx = idx; + } if (!(hit = r_core_asm_hit_new ())) { r_list_purge (hits); free (hits); @@ -152,15 +159,15 @@ R_API RList *r_core_asm_strsearch(RCore *core, const char *input, ut64 from, ut6 r_list_append (hits, hit); R_FREE (code); matchcount = 0; - idx = tidx+1; + idx = tidx + 1; if (maxhits) { - count ++; + count++; if (count >= maxhits) { //eprintf ("Error: search.maxhits reached\n"); goto beach; } } - } else if (matchcount == 0) { + } else if (!matchcount) { tidx = idx; matchcount++; idx += len; @@ -169,19 +176,20 @@ R_API RList *r_core_asm_strsearch(RCore *core, const char *input, ut64 from, ut6 idx += len; } } else { - idx = matchcount? tidx+1: idx+1; + idx = matchcount? tidx + 1: idx + 1; R_FREE (code); matchcount = 0; } } - at += OPSZ; } + r_cons_break_pop (); r_asm_set_pc (core->assembler, toff); beach: free (buf); free (ptr); free (code); + r_cons_break_pop (); return hits; } diff --git a/libr/core/cbin.c b/libr/core/cbin.c index cc8ed0111c..125fe0205a 100644 --- a/libr/core/cbin.c +++ b/libr/core/cbin.c @@ -306,7 +306,7 @@ static void _print_strings(RCore *r, RList *list, int mode, int va) { } if (IS_MODE_SET (mode) && r_config_get_i (r->config, "bin.strings")) { r_flag_space_set (r->flags, "strings"); - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); } r_list_foreach (list, iter, string) { const char *section_name, *type_string; @@ -329,7 +329,7 @@ static void _print_strings(RCore *r, RList *list, int mode, int va) { type_string = r_bin_string_type (string->type); if (IS_MODE_SET (mode)) { char *f_name, *str; - if (r_cons_singleton()->breaked) { + if (r_cons_is_breaked ()) { break; } r_meta_add (r->anal, R_META_TYPE_STRING, addr, addr + string->size, string->string); @@ -389,7 +389,7 @@ static void _print_strings(RCore *r, RList *list, int mode, int va) { r_cons_printf ("]"); } if (IS_MODE_SET (mode)) { - r_cons_break_end (); + r_cons_break_pop (); } } @@ -741,11 +741,11 @@ static int bin_dwarf(RCore *core, int mode) { free (da); } } - r_cons_break (NULL, NULL); if (!list) { return false; } + r_cons_break_push (NULL, NULL); /* cache file:line contents */ const char *lastFile = NULL; int *lastFileLines = NULL; @@ -760,7 +760,7 @@ static int bin_dwarf(RCore *core, int mode) { /* we should need to store all this in sdb, or do a filecontentscache in libr/util */ r_list_foreach (list, iter, row) { - if (r_cons_singleton()->breaked) { + if (r_cons_is_breaked ()) { break; } if (mode) { @@ -821,7 +821,7 @@ static int bin_dwarf(RCore *core, int mode) { r_cons_printf ("0x%08"PFMT64x"\t%s\t%d\n", row->address, row->file, row->line); } } - r_cons_break_end (); + r_cons_break_pop (); R_FREE (lastFileContents); R_FREE (lastFileContents2); r_list_free (list); @@ -1127,8 +1127,9 @@ static int bin_relocs(RCore *r, int mode, int va) { relocs = r_bin_patch_relocs (r->bin); if (!relocs) { relocs = r_bin_get_relocs (r->bin); - if (!relocs) + if (!relocs) { return false; + } } if (IS_MODE_RAD (mode)) { diff --git a/libr/core/cio.c b/libr/core/cio.c index 0541bcd559..91bacb90b4 100644 --- a/libr/core/cio.c +++ b/libr/core/cio.c @@ -84,12 +84,14 @@ R_API int r_core_dump(RCore *core, const char *file, ut64 addr, ut64 size, int a fclose (fd); return false; } - r_cons_break (NULL, NULL); - for (i = 0; ibreaked) + r_cons_break_push (NULL, NULL); + for (i = 0; i < size; i += bs) { + if (r_cons_is_breaked ()) { break; - if ((i + bs) > size) + } + if ((i + bs) > size) { bs = size - i; + } r_io_read_at (core->io, addr + i, buf, bs); if (fwrite (buf, bs, 1, fd) < 1) { eprintf ("write error\n"); @@ -97,7 +99,7 @@ R_API int r_core_dump(RCore *core, const char *file, ut64 addr, ut64 size, int a } } eprintf ("dumped 0x%"PFMT64x" bytes\n", i); - r_cons_break_end (); + r_cons_break_pop (); fclose (fd); free (buf); return true; diff --git a/libr/core/cmd.c b/libr/core/cmd.c index 8a2718b5b1..d4ce25c72f 100644 --- a/libr/core/cmd.c +++ b/libr/core/cmd.c @@ -568,7 +568,9 @@ static int cmd_interpret(void *data, const char *input) { case '-': if (input[1]=='?') { r_cons_printf ("Usage: '-' '.-' '. -' do the same\n"); - } else r_core_run_script (core, "-"); + } else { + r_core_run_script (core, "-"); + } break; case ' ': if (!r_core_run_script (core, input + 1)) { @@ -616,10 +618,12 @@ static int cmd_interpret(void *data, const char *input) { if (filter) { *filter = '~'; } - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); if (ptr) { for (;;) { - if (r_cons_singleton()->breaked) break; + if (r_cons_is_breaked ()) { + break; + } eol = strchr (ptr, '\n'); if (eol) *eol = '\0'; if (*ptr) { @@ -631,7 +635,7 @@ static int cmd_interpret(void *data, const char *input) { ptr = eol + 1; } } - r_cons_break_end (); + r_cons_break_pop (); free (str); free (inp); break; @@ -2174,8 +2178,7 @@ R_API int r_core_cmd_foreach(RCore *core, const char *cmd, char *each) { oseek = core->offset; ostr = str = strdup (each); - //r_cons_break(); - + r_cons_break_push (NULL, NULL); //pop on return switch (each[0]) { case '?':{ const char* help_msg[] = { @@ -2210,11 +2213,14 @@ R_API int r_core_cmd_foreach(RCore *core, const char *cmd, char *each) { r_core_block_size (core, bb->size); r_core_seek (core, bb->addr, 1); r_core_cmd (core, cmd, 0); + if (r_cons_is_breaked ()) { + break; + } } } free (ostr); r_core_block_size (core, bs); - return false; + goto out_finish; } break; case 'i': // "@@i" - function instructions @@ -2230,11 +2236,14 @@ R_API int r_core_cmd_foreach(RCore *core, const char *cmd, char *each) { ut64 addr = bb->addr + bb->op_pos[i]; r_core_seek (core, addr, 1); r_core_cmd (core, cmd, 0); + if (r_cons_is_breaked ()) { + break; + } } } } free (ostr); - return false; + goto out_finish; } break; case 'f': // "@@f" @@ -2246,11 +2255,14 @@ R_API int r_core_cmd_foreach(RCore *core, const char *cmd, char *each) { if (each[2] && strstr (fcn->name, each + 2)) { r_core_seek (core, fcn->addr, 1); r_core_cmd (core, cmd, 0); + if (r_cons_is_breaked ()) { + break; + } } } } free (ostr); - return false; + goto out_finish; } else { RAnalFunction *fcn; RListIter *iter; @@ -2268,11 +2280,14 @@ R_API int r_core_cmd_foreach(RCore *core, const char *cmd, char *each) { r_cons_pop (); r_cons_strcat (buf); free (buf); + if (r_cons_is_breaked ()) { + break; + } } core->cons->grep = grep; } free (ostr); - return false; + goto out_finish; } break; case 't': @@ -2291,7 +2306,7 @@ R_API int r_core_cmd_foreach(RCore *core, const char *cmd, char *each) { } r_debug_select (core->dbg, pid, pid); free (ostr); - return false; + goto out_finish; } break; case 'c': @@ -2343,14 +2358,18 @@ R_API int r_core_cmd_foreach(RCore *core, const char *cmd, char *each) { if (out) { each = out; do { - while (*each==' ') each++; - if (!*each) break; + while (*each == ' ') each++; + if (!*each) { + break; + } str = strchr (each, ' '); if (str) { *str = '\0'; addr = r_num_math (core->num, each); *str = ' '; - } else addr = r_num_math (core->num, each); + } else { + addr = r_num_math (core->num, each); + } //eprintf ("; 0x%08"PFMT64x":\n", addr); each = str+1; r_core_seek (core, addr, 1); @@ -2362,19 +2381,18 @@ R_API int r_core_cmd_foreach(RCore *core, const char *cmd, char *each) { } break; case '.': - if (each[1]=='(') { + if (each[1] == '(') { char cmd2[1024]; - // TODO: use r_cons_break() here // XXX whats this 999 ? i = 0; - r_cons_break (NULL, NULL); - for (core->rcmd->macro.counter=0;i<999;core->rcmd->macro.counter++) { - if (r_cons_singleton ()->breaked) + for (core->rcmd->macro.counter = 0;i < 999; core->rcmd->macro.counter++) { + if (r_cons_is_breaked ()) { break; + } r_cmd_macro_call (&core->rcmd->macro, each+2); - if (!core->rcmd->macro.brk_value) + if (!core->rcmd->macro.brk_value) { break; - + } addr = core->rcmd->macro._brk_value; sprintf (cmd2, "%s @ 0x%08"PFMT64x"", cmd, addr); eprintf ("0x%08"PFMT64x" (%s)\n", addr, cmd2); @@ -2382,17 +2400,17 @@ R_API int r_core_cmd_foreach(RCore *core, const char *cmd, char *each) { r_core_cmd (core, cmd2, 0); i++; } - r_cons_break_end(); } else { char buf[1024]; char cmd2[1024]; - FILE *fd = r_sandbox_fopen (each+1, "r"); + FILE *fd = r_sandbox_fopen (each + 1, "r"); if (fd) { core->rcmd->macro.counter=0; while (!feof (fd)) { buf[0] = '\0'; - if (!fgets (buf, sizeof (buf), fd)) + if (!fgets (buf, sizeof (buf), fd)) { break; + } addr = r_num_math (core->num, buf); eprintf ("0x%08"PFMT64x": %s\n", addr, cmd); sprintf (cmd2, "%s @ 0x%08"PFMT64x"", cmd, addr); @@ -2426,7 +2444,7 @@ R_API int r_core_cmd_foreach(RCore *core, const char *cmd, char *each) { /* for all flags in current flagspace */ // XXX: dont ask why, but this only works with _prev.. r_list_foreach (core->flags->flags, iter, flag) { - if (r_cons_singleton()->breaked) { + if (r_cons_is_breaked ()) { break; } /* filter per flag spaces */ @@ -2446,7 +2464,6 @@ R_API int r_core_cmd_foreach(RCore *core, const char *cmd, char *each) { free (buf); } } - r_cons_break (NULL, NULL); core->flags->space_idx = flagspace; core->rcmd->macro.counter++ ; free (word); @@ -2454,13 +2471,16 @@ R_API int r_core_cmd_foreach(RCore *core, const char *cmd, char *each) { } } } - r_cons_break_end (); + r_cons_break_pop (); // XXX: use r_core_seek here core->offset = oseek; free (word); free (ostr); return true; +out_finish: + r_cons_break_pop (); + return false; } R_API int r_core_cmd(RCore *core, const char *cstr, int log) { @@ -2552,15 +2572,20 @@ R_API int r_core_cmd_lines(RCore *core, const char *lines) { int r, ret = true; char *nl, *data, *odata; - if (!lines || !*lines) return true; + if (!lines || !*lines) { + return true; + } data = odata = strdup (lines); - if (!odata) return false; + if (!odata) { + return false; + } nl = strchr (odata, '\n'); if (nl) { - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); do { - if (core->cons->breaked) { + if (r_cons_is_breaked ()) { free (odata); + r_cons_break_pop (); return ret; } *nl = '\0'; @@ -2572,18 +2597,21 @@ R_API int r_core_cmd_lines(RCore *core, const char *lines) { } r_cons_flush (); if (data[0]=='q') { - if (data[1]=='!') + if (data[1] == '!') { ret = -1; - else eprintf ("'q': quit ignored. Use 'q!'\n"); + } else { + eprintf ("'q': quit ignored. Use 'q!'\n"); + } data = nl + 1; break; } data = nl+1; } while ((nl = strchr (data, '\n'))); - r_cons_break_end (); + r_cons_break_pop (); } - if (ret>=0 && data && *data) + if (ret >= 0 && data && *data) { r_core_cmd (core, data, 0); + } free (odata); return ret; } diff --git a/libr/core/cmd_anal.c b/libr/core/cmd_anal.c index a08e2f7679..eaa1e417a7 100644 --- a/libr/core/cmd_anal.c +++ b/libr/core/cmd_anal.c @@ -42,7 +42,7 @@ static bool anal_is_bad_call(RCore *core, ut64 from, ut64 to, ut64 addr, ut8 *bu } #endif -static void type_cmd_help (RCore *core) { +static void type_cmd_help(RCore *core) { const char *help_msg[] = { "Usage:", "aftm", "", "afta", "", "Setup memory and analyse do type matching analysis for all functions", @@ -60,6 +60,7 @@ static void type_cmd(RCore *core, const char *input) { } RListIter *it; ut64 seek; + r_cons_break_push (NULL, NULL); switch (*input) { case 'a': // "afta" seek = core->offset; @@ -71,6 +72,9 @@ static void type_cmd(RCore *core, const char *input) { r_core_seek (core, fcn->addr, true); r_anal_esil_set_pc (core->anal->esil, fcn->addr); r_core_anal_type_match (core, fcn); + if (r_cons_is_breaked ()) { + break; + } } if (!io_cache) { r_config_set_i (core->config, "io.cache", io_cache); @@ -89,6 +93,7 @@ static void type_cmd(RCore *core, const char *input) { type_cmd_help (core); break; } + r_cons_break_pop (); } static int cc_print(void *p, const char *k, const char *v) { @@ -2058,10 +2063,11 @@ R_API int r_core_esil_step(RCore *core, ut64 until_addr, const char *until_expr) RAnalEsil *esil = core->anal->esil; const char *name = r_reg_get_name (core->anal->reg, R_REG_NAME_PC); ut64 addr = r_reg_getv (core->anal->reg, name); + r_cons_break_push (NULL, NULL); repeat: - if (r_cons_singleton ()->breaked) { + if (r_cons_is_breaked ()) { eprintf ("[+] ESIL emulation interrupted at 0x%08" PFMT64x "\n", addr); - return 0; + goto out_return_zero; } if (!esil) { int romem = r_config_get_i (core->config, "esil.romem"); @@ -2071,7 +2077,7 @@ repeat: int stacksize = r_config_get_i (core->config, "esil.stacksize"); int nonull = r_config_get_i (core->config, "esil.nonull"); if (!(core->anal->esil = r_anal_esil_new (stacksize, iotrap))) { - return 0; + goto out_return_zero; } esil = core->anal->esil; r_anal_esil_setup (esil, core->anal, romem, stats, nonull); // setup io @@ -2096,14 +2102,14 @@ repeat: } if (r_anal_pin_call (core->anal, addr)) { eprintf ("esil pin called\n"); - return 1; + goto out_return_one; } if (esil->exectrap) { if (!(r_io_section_get_rwx (core->io, addr) & R_IO_EXEC)) { esil->trap = R_ANAL_TRAP_EXEC_ERR; esil->trap_code = addr; eprintf ("[ESIL] Trap, trying to execute on non-executable memory\n"); - return 1; + goto out_return_one; } } r_io_read_at (core->io, addr, code, sizeof (code)); @@ -2123,7 +2129,7 @@ repeat: esil->trap = R_ANAL_TRAP_EXEC_ERR; esil->trap_code = addr; eprintf ("[ESIL] Trap, trying to execute a branch in a delay slot\n"); - return 1; + goto out_return_one; } } @@ -2177,25 +2183,34 @@ repeat: if (until_addr != UT64_MAX) { if (r_reg_getv (core->anal->reg, name) == until_addr) { eprintf ("ADDR BREAK\n"); - return 0; - } else goto repeat; + goto out_return_zero; + } else { + goto repeat; + } } // check esil if (esil->trap) { if (core->anal->esil->verbose) { eprintf ("TRAP\n"); } - return 0; + goto out_return_zero; } if (until_expr) { if (r_anal_esil_condition (core->anal->esil, until_expr)) { if (core->anal->esil->verbose) { eprintf ("ESIL BREAK!\n"); } - return 0; - } else goto repeat; + goto out_return_zero; + } else { + goto repeat; + } } +out_return_one: + r_cons_break_pop (); return 1; +out_return_zero: + r_cons_break_pop (); + return 0; } static void cmd_address_info(RCore *core, const char *addrstr, int fmt) { @@ -3149,15 +3164,15 @@ static void cmd_anal_aftertraps(RCore *core, const char *input) { } } addr_end = addr + len; - r_cons_break (NULL, NULL); if (!(buf = malloc (4096))) { return; } bufi = 0; int trapcount = 0; int nopcount = 0; + r_cons_break_push (NULL, NULL); while (addr < addr_end) { - if (core->cons->breaked) { + if (r_cons_is_breaked ()) { break; } // TODO: too many ioreads here @@ -3189,10 +3204,11 @@ static void cmd_anal_aftertraps(RCore *core, const char *input) { } else { op.size = minop; } - addr += (op.size > 0)? op.size: 1; - bufi += (op.size > 0)? op.size: 1; + addr += (op.size > 0)? op.size : 1; + bufi += (op.size > 0)? op.size : 1; r_anal_op_fini (&op); } + r_cons_break_pop (); free (buf); } @@ -3252,13 +3268,13 @@ static void cmd_anal_calls(RCore *core, const char *input) { } } addr_end = addr + len; - r_cons_break (NULL, NULL); if (!(buf = malloc (4096))) { return; } bufi = 0; + r_cons_break_push (NULL, NULL); while (addr < addr_end) { - if (core->cons->breaked) { + if (r_cons_is_breaked ()) { break; } // TODO: too many ioreads here @@ -3296,6 +3312,7 @@ static void cmd_anal_calls(RCore *core, const char *input) { bufi += (op.size > 0)? op.size: 1; r_anal_op_fini (&op); } + r_cons_break_pop (); free (buf); } @@ -4747,30 +4764,35 @@ static int cmd_anal_all(RCore *core, const char *input) { eprintf ("Usage: See aa? for more help\n"); } else { bool done_aav = false; - r_cons_break (NULL, NULL); ut64 curseek = core->offset; rowlog (core, "Analyze all flags starting with sym. and entry0 (aa)"); + r_cons_break_push (NULL, NULL); r_core_anal_all (core); rowlog_done (core); - if (core->cons->breaked) { + if (r_cons_is_breaked ()) { goto jacuzzi; } r_cons_clear_line (1); - r_cons_break_end (); if (*input == 'a') { // "aaa" int c = r_config_get_i (core->config, "anal.calls"); if (strstr (r_config_get (core->config, "asm.arch"), "arm")) { rowlog (core, "\nAnalyze value pointers (aav)"); done_aav = true; r_core_cmd0 (core, "aav"); + if (r_cons_is_breaked ()) { + goto jacuzzi; + } r_core_cmd0 (core, "aav $S+$SS+1"); } r_config_set_i (core->config, "anal.calls", 1); r_core_cmd0 (core, "s $S"); rowlog (core, "Analyze len bytes of instructions for references (aar)"); + if (r_cons_is_breaked ()) { + goto jacuzzi; + } (void)r_core_anal_refs (core, input + 1); // "aar" rowlog_done (core); - if (core->cons->breaked) { + if (r_cons_is_breaked ()) { goto jacuzzi; } rowlog (core, "Analyze function calls (aac)"); @@ -4779,7 +4801,7 @@ static int cmd_anal_all(RCore *core, const char *input) { // rowlog (core, "Analyze data refs as code (LEA)"); // (void) cmd_anal_aad (core, NULL); // "aad" rowlog_done (core); - if (core->cons->breaked) { + if (r_cons_is_breaked ()) { goto jacuzzi; } if (input[1] == 'a') { // "aaaa" @@ -4800,6 +4822,9 @@ static int cmd_anal_all(RCore *core, const char *input) { } r_config_set_i (core->config, "anal.calls", c); rowlog (core, "Constructing a function name for fcn.* and sym.func.* functions (aan)"); + if (r_cons_is_breaked ()) { + goto jacuzzi; + } if (r_config_get_i (core->config, "anal.autoname")) { r_core_anal_autoname_all_fcns (core); } @@ -4809,14 +4834,11 @@ static int cmd_anal_all(RCore *core, const char *input) { rowlog_done (core); } rowlog_done (core); - if (core->cons->breaked) { - goto jacuzzi; - } r_core_cmd0 (core, "s-"); } jacuzzi: flag_every_function (core); - // r_core_cmd0 (core, "aai"); + r_cons_break_pop (); } break; case 't': { @@ -5005,8 +5027,6 @@ static int cmd_anal(void *data, const char *input) { //"ax", " [-cCd] [f] [t]", "manage code/call/data xrefs", NULL }; - r_cons_break (NULL, NULL); - switch (input[0]) { case 'p': // "ap" { @@ -5043,7 +5063,9 @@ static int cmd_anal(void *data, const char *input) { if (len > 0) core_anal_bytes (core, buf, len, 0, input[1]); free (buf); - } else eprintf ("Usage: ab [hexpair-bytes]\n abj [hexpair-bytes] (json)"); + } else { + eprintf ("Usage: ab [hexpair-bytes]\n abj [hexpair-bytes] (json)"); + } break; case 'i': cmd_anal_info (core, input + 1); break; // "ai" case 'r': cmd_anal_reg (core, input + 1); break; // "ar" @@ -5055,7 +5077,6 @@ static int cmd_anal(void *data, const char *input) { break; case 'f': // "af" if (!cmd_anal_fcn (core, input)) { - r_cons_break_end (); return false; } break; @@ -5073,7 +5094,6 @@ static int cmd_anal(void *data, const char *input) { break; case 'x': if (!cmd_anal_refs (core, input + 1)) { - r_cons_break_end (); return false; } break; @@ -5100,9 +5120,7 @@ static int cmd_anal(void *data, const char *input) { r_config_set_i (core->config, "asm.lines", false); r_config_set_i (core->config, "asm.xrefs", false); - r_cons_break (NULL, NULL); hooks = r_core_anal_cycles (core, ccl); //analyse - r_cons_break_end (); r_cons_clear_line (1); r_list_foreach (hooks, iter, hook) { instr_tmp = r_core_disassemble_instr (core, hook->addr, 1); @@ -5158,9 +5176,11 @@ static int cmd_anal(void *data, const char *input) { cmd_anal_hint (core, input + 1); break; case '!': - if (core->anal && core->anal->cur && core->anal->cur->cmd_ext) + if (core->anal && core->anal->cur && core->anal->cur->cmd_ext) { return core->anal->cur->cmd_ext (core->anal, input + 1); - else r_cons_printf ("No plugins for this analysis plugin\n"); + } else { + r_cons_printf ("No plugins for this analysis plugin\n"); + } break; default: r_core_cmd_help (core, help_msg); @@ -5171,12 +5191,11 @@ static int cmd_anal(void *data, const char *input) { NULL); break; } - if (tbs != core->blocksize) + if (tbs != core->blocksize) { r_core_block_size (core, tbs); - if (core->cons->breaked) { - r_cons_clear_line (1); - eprintf ("Interrupted\n"); } - r_cons_break_end (); + if (r_cons_is_breaked ()) { + r_cons_clear_line (1); + } return 0; } diff --git a/libr/core/cmd_api.c b/libr/core/cmd_api.c index f8ecf1917c..c78751c22a 100644 --- a/libr/core/cmd_api.c +++ b/libr/core/cmd_api.c @@ -570,7 +570,6 @@ R_API char *r_cmd_macro_label_process(RCmdMacro *mac, RCmdMacroLabel *labels, in /* TODO: add support for spaced arguments */ R_API int r_cmd_macro_call(RCmdMacro *mac, const char *name) { - RCons *cons; char *args; int nargs = 0; char *str, *ptr, *ptr2; @@ -610,39 +609,37 @@ R_API int r_cmd_macro_call(RCmdMacro *mac, const char *name) { ptr = strchr (str, ','); if (ptr) *ptr =0; - cons = r_cons_singleton (); - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); r_list_foreach (mac->macros, iter, m) { if (!strcmp (str, m->name)) { char *ptr = m->code; char *end = strchr (ptr, '\n'); - if (m->nargs != 0 && nargs != m->nargs) { - eprintf ("Macro '%s' expects %d args, not %d\n", - m->name, m->nargs, nargs); + eprintf ("Macro '%s' expects %d args, not %d\n", m->name, m->nargs, nargs); macro_level --; free (str); + r_cons_break_pop (); return false; } - mac->brk = 0; do { if (end) *end = '\0'; - if (cons->breaked) { + if (r_cons_is_breaked ()) { eprintf ("Interrupted at (%s)\n", ptr); - if (end) *end = '\n'; + if (end) { + *end = '\n'; + } free (str); + r_cons_break_pop (); return false; } r_cons_flush (); - /* Label handling */ ptr2 = r_cmd_macro_label_process (mac, &(labels[0]), &labels_n, ptr); if (!ptr2) { eprintf ("Oops. invalid label name\n"); break; - } else - if (ptr != ptr2) { // && end) { + } else if (ptr != ptr2) { ptr = ptr2; if (end) *end ='\n'; end = strchr (ptr, '\n'); @@ -655,8 +652,9 @@ R_API int r_cmd_macro_call(RCmdMacro *mac, const char *name) { // TODO: handle quit? r == 0?? // quit, exits the macro. like a break value = mac->num->value; - if (r <0) { + if (r < 0) { free (str); + r_cons_break_pop (); return r; } } @@ -666,23 +664,24 @@ R_API int r_cmd_macro_call(RCmdMacro *mac, const char *name) { } else { macro_level --; free (str); - return true; + goto out_clean; } /* Fetch next command */ end = strchr (ptr, '\n'); } while (!mac->brk); - if (mac->brk) { macro_level--; free (str); - return true; + goto out_clean; } } } eprintf ("No macro named '%s'\n", str); macro_level--; free (str); +out_clean: + r_cons_break_pop (); return true; } diff --git a/libr/core/cmd_debug.c b/libr/core/cmd_debug.c index 7db214a0b7..5b57b3e425 100644 --- a/libr/core/cmd_debug.c +++ b/libr/core/cmd_debug.c @@ -194,17 +194,17 @@ static void dot_trace_traverse(RCore *core, RTree *t, int fmt) { static int step_until(RCore *core, ut64 addr) { ut64 off = r_debug_reg_get (core->dbg, "PC"); - if (off == 0LL) { + if (!off) { eprintf ("Cannot 'drn pc'\n"); return false; } - if (addr == 0LL) { + if (!addr) { eprintf ("Cannot continue until address 0\n"); return false; } - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); do { - if (r_cons_singleton ()->breaked) { + if (r_cons_is_breaked ()) { core->break_loop = true; break; } @@ -216,7 +216,7 @@ static int step_until(RCore *core, ut64 addr) { off = r_debug_reg_get (core->dbg, "PC"); // check breakpoint here } while (off != addr); - r_cons_break_end(); + r_cons_break_pop (); return true; } @@ -226,9 +226,9 @@ static int step_until_esil(RCore *core, const char *esilstr) { eprintf ("Not initialized %p. Run 'aei' first.\n", core->anal->esil); return false; } - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); for (;;) { - if (r_cons_singleton ()->breaked) { + if (r_cons_is_breaked ()) { core->break_loop = true; break; } @@ -243,7 +243,7 @@ static int step_until_esil(RCore *core, const char *esilstr) { break; } } - r_cons_break_end(); + r_cons_break_pop (); return true; } @@ -258,12 +258,14 @@ static int step_until_inst(RCore *core, const char *instr) { eprintf ("Wrong state\n"); return false; } - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); for (;;) { - if (r_cons_singleton ()->breaked) + if (r_cons_is_breaked ()) { break; - if (r_debug_is_dead (core->dbg)) + } + if (r_debug_is_dead (core->dbg)) { break; + } r_debug_step (core->dbg, 1); r_debug_reg_sync (core->dbg, R_REG_TYPE_ALL, false); /* TODO: disassemble instruction and strstr */ @@ -273,14 +275,14 @@ static int step_until_inst(RCore *core, const char *instr) { r_io_read_at (core->io, pc, buf, sizeof (buf)); ret = r_asm_disassemble (core->assembler, &asmop, buf, sizeof (buf)); eprintf ("0x%08"PFMT64x" %d %s\n", pc, ret, asmop.buf_asm); - if (ret>0) { + if (ret > 0) { if (strstr (asmop.buf_asm, instr)) { eprintf ("Stop.\n"); break; } } } - r_cons_break_end(); + r_cons_break_pop (); return true; } @@ -295,12 +297,14 @@ static int step_until_flag(RCore *core, const char *instr) { eprintf ("Wrong state\n"); return false; } - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); for (;;) { - if (r_cons_singleton ()->breaked) + if (r_cons_is_breaked ()) { break; - if (r_debug_is_dead (core->dbg)) + } + if (r_debug_is_dead (core->dbg)) { break; + } r_debug_step (core->dbg, 1); r_debug_reg_sync (core->dbg, R_REG_TYPE_ALL, false); pc = r_debug_reg_get (core->dbg, "PC"); @@ -314,21 +318,21 @@ static int step_until_flag(RCore *core, const char *instr) { } } beach: - r_cons_break_end(); + r_cons_break_pop (); return true; } /* until end of frame */ static int step_until_eof(RCore *core) { ut64 off, now = r_debug_reg_get (core->dbg, "SP"); - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); do { // XXX (HACK!) r_debug_step_over (core->dbg, 1); off = r_debug_reg_get (core->dbg, "SP"); // check breakpoint here } while (off <= now); - r_cons_break_end(); + r_cons_break_pop (); return true; } @@ -2492,10 +2496,8 @@ static void debug_trace_calls (RCore *core, const char *input) { eprintf ("No process to debug."); return; } - if (*input == ' ') { ut64 first_n; - while (*input == ' ') input++; first_n = r_num_math (core->num, input); input = strchr (input, ' '); @@ -2512,27 +2514,24 @@ static void debug_trace_calls (RCore *core, const char *input) { final_addr = first_n; } } - core->dbg->trace->enabled = 0; - r_cons_break (static_debug_stop, core->dbg); + r_cons_break_push (static_debug_stop, core->dbg); r_reg_arena_swap (core->dbg->reg, true); - if (final_addr != UT64_MAX) { int hwbp = r_config_get_i (core->config, "dbg.hwbp"); - bp_final = r_debug_bp_add (core->dbg, final_addr, hwbp, NULL, 0); if (!bp_final) { eprintf ("Cannot set breakpoint at final address (%"PFMT64x")\n", final_addr); } } - do_debug_trace_calls (core, from, to, final_addr); - if (bp_final) + if (bp_final) { r_bp_del (core->dbg->bp, final_addr); + } _core = core; trace_traverse (core->dbg->tree); core->dbg->trace->enabled = t; - r_cons_break_end(); + r_cons_break_pop (); } static void r_core_debug_esil (RCore *core, const char *input) { @@ -2788,18 +2787,18 @@ static bool cmd_dcu (RCore *core, const char *input) { return false; } if (dcu_range) { - // TODO : handle ^C here - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); do { - if (r_cons_is_breaked ()) + if (r_cons_is_breaked ()) { break; + } r_debug_step (core->dbg, 1); r_debug_reg_sync (core->dbg, R_REG_TYPE_GPR, false); pc = r_debug_reg_get (core->dbg, "PC"); eprintf ("Continue 0x%08"PFMT64x" > 0x%08"PFMT64x" < 0x%08"PFMT64x"\n", from, pc, to); } while (pc < from || pc > to); - r_cons_break_end (); + r_cons_break_pop (); } else { ut64 addr = from; eprintf ("Continue until 0x%08"PFMT64x" using %d bpsize\n", addr, core->dbg->bpsize); @@ -2909,19 +2908,20 @@ static int cmd_debug_continue (RCore *core, const char *input) { int n = 0; int t = core->dbg->trace->enabled; core->dbg->trace->enabled = 0; - r_cons_break (static_debug_stop, core->dbg); + r_cons_break_push (static_debug_stop, core->dbg); do { r_debug_step (core->dbg, 1); r_debug_reg_sync (core->dbg, R_REG_TYPE_GPR, false); pc = r_debug_reg_get (core->dbg, "PC"); eprintf (" %d %"PFMT64x"\r", n++, pc); s = r_io_section_vget (core->io, pc); - if (r_cons_singleton ()->breaked) + if (r_cons_is_breaked ()) { break; + } } while (!s); eprintf ("\n"); core->dbg->trace->enabled = t; - r_cons_break_end(); + r_cons_break_pop (); return 1; } case 'u': @@ -2995,10 +2995,11 @@ static int cmd_debug_step (RCore *core, const char *input) { case 'i': // "dsi" if (input[2] == ' ') { int n = 0; - r_cons_break (static_debug_stop, core->dbg); + r_cons_break_push (static_debug_stop, core->dbg); do { - if (r_cons_singleton ()->breaked) + if (r_cons_is_breaked ()) { break; + } r_debug_step (core->dbg, 1); if (r_debug_is_dead (core->dbg)) { core->break_loop = true; @@ -3007,6 +3008,7 @@ static int cmd_debug_step (RCore *core, const char *input) { r_core_cmd0 (core, ".dr*"); n++; } while (!r_num_conditional (core->num, input + 3)); + r_cons_break_pop (); eprintf ("Stopped after %d instructions\n", n); } else { eprintf ("3 Missing argument\n"); @@ -3268,10 +3270,10 @@ static int cmd_debug(void *data, const char *input) { eprintf ("TODO: transplant process\n"); break; case 'c': // "dc" - r_cons_break (static_debug_stop, core->dbg); + r_cons_break_push (static_debug_stop, core->dbg); (void)cmd_debug_continue (core, input); follow = r_config_get_i (core->config, "dbg.follow"); - r_cons_break_end (); + r_cons_break_pop (); break; case 'm': // "dm" cmd_debug_map (core, input + 1); @@ -3483,15 +3485,17 @@ static int cmd_debug(void *data, const char *input) { break; case 'w': - r_cons_break (static_debug_stop, core->dbg); - for (;!r_cons_singleton ()->breaked;) { + r_cons_break_push (static_debug_stop, core->dbg); + for (;!r_cons_is_breaked ();) { int pid = atoi (input + 1); //int opid = core->dbg->pid = pid; int res = r_debug_kill (core->dbg, pid, 0, 0); - if (!res) break; + if (!res) { + break; + } r_sys_usleep (200); } - r_cons_break_end (); + r_cons_break_pop (); break; case 'k': r_core_debug_kill (core, input + 1); diff --git a/libr/core/cmd_print.c b/libr/core/cmd_print.c index 7350b69cf5..1c73be960e 100644 --- a/libr/core/cmd_print.c +++ b/libr/core/cmd_print.c @@ -1208,11 +1208,11 @@ static int pdi(RCore *core, int nb_opcodes, int nb_bytes, int fmt) { r_core_block_read (core); } } - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); #define isTheEnd (nb_opcodes? nb_bytes? (jbreaked) { + if (r_cons_is_breaked ()) { err = 1; break; } @@ -1223,8 +1223,9 @@ static int pdi(RCore *core, int nb_opcodes, int nb_bytes, int fmt) { if (fmt != 'e') { // pie item = r_flag_get_i (core->flags, core->offset + i); if (item) { - if (show_offset) + if (show_offset) { r_cons_printf ("0x%08"PFMT64x" ", core->offset + i); + } r_cons_printf (" %s:\n", item->name); } } // do not show flags in pie @@ -1239,14 +1240,15 @@ static int pdi(RCore *core, int nb_opcodes, int nb_bytes, int fmt) { if (ret < 1) { err = 1; ret = asmop.size; - if (ret<1) ret = 1; + if (ret < 1) ret = 1; if (show_bytes) { r_cons_printf ("%14s%02x ", "", core->block[i]); } r_cons_println ("invalid"); //???"); } else { - if (show_bytes) + if (show_bytes) { r_cons_printf ("%16s ", asmop.buf_hex); + } ret = asmop.size; if (decode || esil) { RAnalOp analop = {0}; @@ -1304,7 +1306,7 @@ static int pdi(RCore *core, int nb_opcodes, int nb_bytes, int fmt) { break; #endif } - r_cons_break_end (); + r_cons_break_pop (); core->offset = old_offset; return err; } @@ -2360,7 +2362,7 @@ static int cmd_print(void *data, const char *input) { } switch (*input) { case 'w': // "pw" - if (input[1]=='n') { + if (input[1] == 'n') { cmd_print_pwn (core); } else if (input[1]=='d') { if (!r_sandbox_enable (0)) { @@ -3072,15 +3074,21 @@ static int cmd_print(void *data, const char *input) { RAsmOp asmop; int j, ret; const ut8 *buf = core->block; - if (l==0) l= len; - r_cons_break (NULL, NULL); - for (i=j=0; iblocksize && jassembler, &asmop, buf+i, len-i); - if (r_cons_singleton ()->breaked) break; - r_cons_printf ("%d\n", ret); - if (ret<1) ret = 1; + if (!l) { + l= len; } - r_cons_break_end (); + r_cons_break_push (NULL, NULL); + for (i = j = 0; i < core->blocksize && j < l; i += ret, j++ ) { + ret = r_asm_disassemble (core->assembler, &asmop, buf + i, len - i); + if (r_cons_is_breaked ()) { + break; + } + r_cons_printf ("%d\n", ret); + if (ret < 1) { + ret = 1; + } + } + r_cons_break_pop (); pd_result = 0; } break; @@ -3553,7 +3561,7 @@ static int cmd_print(void *data, const char *input) { core->print->flags &= ~R_PRINT_FLAGS_HEADER; } } - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); switch (input[1]) { case '/': r_core_print_examine (core, input+2); @@ -3587,8 +3595,8 @@ static int cmd_print(void *data, const char *input) { break; case 'a': // "pxa" if (l != 0) { - if (len%16) { - len += 16-(len%16); + if (len % 16) { + len += 16 - (len % 16); } annotated_hexdump (core, input + 2, len); } @@ -3611,12 +3619,12 @@ static int cmd_print(void *data, const char *input) { " _R ret\n" " == cmp/test\n" " XX invalid\n"); - } else if (l != 0) { + } else if (l) { cmd_print_pxA (core, len, input+1); } break; case 'b': // "pxb" - if (l != 0) { + if (l) { ut32 n; int i, c; char buf[32]; @@ -3630,7 +3638,7 @@ static int cmd_print(void *data, const char *input) { r_str_bits (buf, core->block+i, 8, NULL); SPLIT_BITS (buf); r_cons_printf ("%s.%s ", buf, buf+5); - if (c==3) { + if (c == 3) { const ut8 *b = core->block + i-3; #define K(x) (b[3-x]<<(8*x)) n = K (0) | K (1) | K (2) | K (3); @@ -3697,56 +3705,56 @@ static int cmd_print(void *data, const char *input) { } break; case 'W': // "pxW" - if (l != 0) { - len = len - (len%4); - for (i=0; iprint; - RFlagItem *f; - ut32 v = r_read_ble32 (core->block + i, core->print->big_endian); - if (p && p->colorfor) { - a = p->colorfor (p->user, v); - if (a && *a) { - b = Color_RESET; + if (l) { + len = len - (len % 4); + for (i = 0; i < len; i += 4) { + const char *a, *b; + char *fn; + RPrint *p = core->print; + RFlagItem *f; + ut32 v = r_read_ble32 (core->block + i, core->print->big_endian); + if (p && p->colorfor) { + a = p->colorfor (p->user, v); + if (a && *a) { + b = Color_RESET; + } else { + a = b = ""; + } } else { a = b = ""; } - } else { - a = b = ""; - } - f = r_flag_get_at (core->flags, v); - fn = NULL; - if (f) { - st64 delta = (v - f->offset); - if (delta >= 0 && delta < 8192) { - if (v == f->offset) { - fn = strdup (f->name); - } else { - fn = r_str_newf ("%s+%d", - f->name, v-f->offset); + f = r_flag_get_at (core->flags, v); + fn = NULL; + if (f) { + st64 delta = (v - f->offset); + if (delta >= 0 && delta < 8192) { + if (v == f->offset) { + fn = strdup (f->name); + } else { + fn = r_str_newf ("%s+%d", + f->name, v-f->offset); + } } } + r_cons_printf ("0x%08"PFMT64x" %s0x%08"PFMT64x"%s %s\n", + (ut64)core->offset+i, a, (ut64)v, b, fn? fn: ""); + free (fn); } - r_cons_printf ("0x%08"PFMT64x" %s0x%08"PFMT64x"%s %s\n", - (ut64)core->offset+i, a, (ut64)v, b, fn? fn: ""); - free (fn); - } } break; case 'r': // "pxr" - if (l != 0) { + if (l) { if (input[2] == 'j') { int base = core->anal->bits; r_cons_printf ("["); const char *comma = ""; const ut8 *buf = core->block; int withref = 0; - for (i=0; i< core->blocksize; i+= (base/4)) { + for (i = 0; i < core->blocksize; i+= (base/4)) { ut64 addr = core->offset + i; ut64 *foo = (ut64*)(buf+i); ut64 val = *foo; - if (base==32) val &= UT32_MAX; + if (base == 32) val &= UT32_MAX; r_cons_printf ("%s{\"addr\":%"PFMT64d",\"value\":%" \ PFMT64d, comma, addr, val); comma = ","; @@ -3762,7 +3770,9 @@ static int cmd_print(void *data, const char *input) { withref = 1; } } - if (!withref) r_cons_printf ("}"); + if (!withref) { + r_cons_printf ("}"); + } } r_cons_printf ("]\n"); } else { @@ -3772,18 +3782,18 @@ static int cmd_print(void *data, const char *input) { if (bitsize == 16) bitsize = 32; core->print->cols = 1; core->print->flags |= R_PRINT_FLAGS_REFS; - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); r_print_hexdump (core->print, core->offset, core->block, len, bitsize, bitsize / 8); - r_cons_break_end (); + r_cons_break_pop (); core->print->flags &= ~R_PRINT_FLAGS_REFS; core->print->cols = ocols; } } break; case 'h': - if (l != 0) { + if (l) { r_print_hexdump (core->print, core->offset, core->block, len, 32, 2); } @@ -3799,8 +3809,14 @@ static int cmd_print(void *data, const char *input) { ut64 v = (ut64)r_read_ble16 (core->block + i, p->big_endian); if (p && p->colorfor) { a = p->colorfor (p->user, v); - if (a && *a) { b = Color_RESET; } else { a = b = ""; } - } else { a = b = ""; } + if (a && *a) { + b = Color_RESET; + } else { + a = b = ""; + } + } else { + a = b = ""; + } f = r_flag_get_at (core->flags, v); fn = NULL; if (f) { @@ -3808,7 +3824,9 @@ static int cmd_print(void *data, const char *input) { if (delta>=0 && delta<8192) { if (v == f->offset) { fn = strdup (f->name); - } else fn = r_str_newf ("%s+%d", f->name, v-f->offset); + } else { + fn = r_str_newf ("%s+%d", f->name, v-f->offset); + } } } r_cons_printf ("0x%08"PFMT64x" %s0x%04"PFMT64x"%s %s\n", @@ -3818,45 +3836,52 @@ static int cmd_print(void *data, const char *input) { } break; case 'q': - if (l != 0) { + if (l) { r_print_hexdump (core->print, core->offset, core->block, len, 64, 8); } break; case 'Q': // TODO. show if flag name, or inside function - if (l != 0) { - len = len - (len % 8); - for (i = 0; i < len; i += 8) { - const char *a, *b; - char *fn; - RPrint *p = core->print; - RFlagItem *f; - ut64 v = r_read_ble64 (core->block + i, p->big_endian); - if (p && p->colorfor) { - a = p->colorfor (p->user, v); - if (a && *a) { b = Color_RESET; } else { a = b = ""; } - } else { a = b = ""; } - f = r_flag_get_at (core->flags, v); - fn = NULL; - if (f) { - st64 delta = (v - f->offset); - if (delta>=0 && delta<8192) { - if (v == f->offset) { - fn = strdup (f->name); - } else fn = r_str_newf ("%s+%d", f->name, v-f->offset); + if (l) { + len = len - (len % 8); + for (i = 0; i < len; i += 8) { + const char *a, *b; + char *fn; + RPrint *p = core->print; + RFlagItem *f; + ut64 v = r_read_ble64 (core->block + i, p->big_endian); + if (p && p->colorfor) { + a = p->colorfor (p->user, v); + if (a && *a) { + b = Color_RESET; + } else { + a = b = ""; + } + } else { + a = b = ""; } + f = r_flag_get_at (core->flags, v); + fn = NULL; + if (f) { + st64 delta = (v - f->offset); + if (delta>=0 && delta<8192) { + if (v == f->offset) { + fn = strdup (f->name); + } else { + fn = r_str_newf ("%s+%d", f->name, v-f->offset); + } + } + } + r_cons_printf ("0x%08"PFMT64x" %s0x%016"PFMT64x"%s %s\n", + (ut64)core->offset+i, a, v, b, fn? fn: ""); + free (fn); } - r_cons_printf ("0x%08"PFMT64x" %s0x%016"PFMT64x"%s %s\n", - (ut64)core->offset+i, a, v, b, fn? fn: ""); - free (fn); - } } break; case 's': - if (l != 0) { + if (l) { core->print->flags |= R_PRINT_FLAGS_SPARSE; - r_print_hexdump (core->print, core->offset, - core->block, len, 16, 1); + r_print_hexdump (core->print, core->offset, core->block, len, 16, 1); core->print->flags &= (((ut32)-1) & (~R_PRINT_FLAGS_SPARSE)); } break; @@ -3932,11 +3957,11 @@ static int cmd_print(void *data, const char *input) { cols = 1; } for (i = 0; i < len; i += cols) { - r_print_addr (core->print, core->offset+i); + r_print_addr (core->print, core->offset + i); for (j = i; j < i + cols; j += 1) { ut8 *p = (ut8*)core->block + j; - if (jprint->cols*len; + len = core->print->cols * len; /* faltrhou */ default: - if (l != 0) { + if (l) { ut64 from = r_config_get_i (core->config, "diff.from"); ut64 to = r_config_get_i (core->config, "diff.to"); - if (from == to && from == 0) { + if (from == to && !from) { if (!r_core_block_size (core, len)) { len = core->blocksize; } - r_print_hexdump (core->print, core->offset, - core->block, len, 16, 1); + r_print_hexdump (core->print, core->offset, + core->block, len, 16, 1); } else { r_core_print_cmp (core, from, to); } @@ -3970,29 +3995,35 @@ static int cmd_print(void *data, const char *input) { } break; } - r_cons_break_end (); + r_cons_break_pop (); break; case '2': // "p2" if (l != 0) { - if (input[1] == '?') + if (input[1] == '?') { r_cons_printf ("|Usage: p2 [number of bytes representing tiles]\n" "NOTE: Only full tiles will be printed\n"); - else r_print_2bpp_tiles (core->print, core->block, len/16); + } else { + r_print_2bpp_tiles (core->print, core->block, len/16); + } } break; case '6': if (l != 0) { int malen = (core->blocksize*4)+1; ut8 *buf = malloc (malen); - if (!buf) break; + if (!buf) { + break; + } memset (buf, 0, malen); switch (input[1]) { case 'd': - if (input[2] == '?') + if (input[2] == '?') { r_cons_printf ("|Usage: p6d [len] base 64 decode\n"); - else if (r_base64_decode (buf, (const char *)core->block, len)) + } else if (r_base64_decode (buf, (const char *)core->block, len)) { r_cons_println ((const char*)buf); - else eprintf ("r_base64_decode: invalid stream\n"); + } else { + eprintf ("r_base64_decode: invalid stream\n"); + } break; case 'e': if (input[2] == '?') { @@ -4015,7 +4046,7 @@ static int cmd_print(void *data, const char *input) { case '8': // "p8" if (input[1] == '?') { r_cons_printf("|Usage: p8[fj] [len] 8bit hexpair list of bytes (see pcj)\n"); - } else if (l != 0) { + } else if (l) { if (!r_core_block_size (core, len)) { len = core->blocksize; } @@ -4054,10 +4085,12 @@ static int cmd_print(void *data, const char *input) { int rows = (h/12); int i, j; char *s; - if (rows<1) rows = 1; - c = r_cons_canvas_new (w, rows*11); - for (i = 0; ioffset += len; r_core_read_at (core, core->offset, core->block, len); @@ -4081,24 +4114,39 @@ static int cmd_print(void *data, const char *input) { case ' ': case '\0': //len must be multiple of 4 since r_mem_copyendian move data in fours - sizeof(ut32) - if (len < sizeof (ut32)) eprintf ("You should change the block size: b %d\n", (int)sizeof (ut32)); - if (len % sizeof (ut32) != 0) len = len - (len % sizeof (ut32)); - for (l=0; lprint, core->block+l, sizeof (ut32)); + if (len < sizeof (ut32)) { + eprintf ("You should change the block size: b %d\n", (int)sizeof (ut32)); + } + if (len % sizeof (ut32)) { + len = len - (len % sizeof (ut32)); + } + for (l = 0; l < len; l += sizeof (ut32)) { + r_print_date_unix (core->print, core->block + l, sizeof (ut32)); + } break; case 'd': //len must be multiple of 4 since r_print_date_dos read buf+3 //if block size is 1 or 5 for example it reads beyond the buffer - if (len < sizeof (ut32)) eprintf ("You should change the block size: b %d\n", (int)sizeof (ut32)); - if (len % sizeof (ut32) != 0) len = len - (len % sizeof (ut32)); - for (l=0; lprint, core->block+l, sizeof (ut32)); + if (len < sizeof (ut32)) { + eprintf ("You should change the block size: b %d\n", (int)sizeof (ut32)); + } + if (len % sizeof (ut32)) { + len = len - (len % sizeof (ut32)); + } + for (l = 0; l < len; l += sizeof (ut32)) { + r_print_date_dos (core->print, core->block + l, sizeof (ut32)); + } break; case 'n': - if (len < sizeof (ut64)) eprintf ("You should change the block size: b %d\n", (int)sizeof (ut64)); - if (len % sizeof (ut64) != 0) len = len - (len % sizeof (ut64)); - for (l=0; lprint, core->block+l, sizeof (ut64)); + if (len < sizeof (ut64)) { + eprintf ("You should change the block size: b %d\n", (int)sizeof (ut64)); + } + if (len % sizeof (ut64)) { + len = len - (len % sizeof (ut64)); + } + for (l = 0; l < len; l += sizeof (ut64)) { + r_print_date_w32 (core->print, core->block + l, sizeof (ut64)); + } break; case '?':{ const char* help_msg[] = { diff --git a/libr/core/cmd_search.c b/libr/core/cmd_search.c index e8cd21938a..891bd90586 100644 --- a/libr/core/cmd_search.c +++ b/libr/core/cmd_search.c @@ -118,29 +118,29 @@ static void cmd_search_bin(RCore *core, ut64 from, ut64 to) { ut8 buf[1024]; int size, sz = sizeof (buf); - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); while (from < to) { - if (r_cons_singleton()->breaked) { + if (r_cons_is_breaked ()) { break; } r_io_read_at (core->io, from, buf, sz); plug = r_bin_get_binplugin_by_bytes (core->bin, buf, sz); if (plug) { - r_cons_printf ("0x%08"PFMT64x" %s\n", - from, plug->name); + r_cons_printf ("0x%08"PFMT64x" %s\n", from, plug->name); // TODO: load the bin and calculate its size if (plug->size) { r_bin_load_io_at_offset_as_sz (core->bin, core->file->desc, 0, 0, 0, core->offset, plug->name, 4096); size = plug->size (core->bin->cur); - if (size>0) + if (size > 0) { r_cons_printf ("size %d\n", size); + } } } from ++; } - r_cons_break_end (); + r_cons_break_pop (); } R_API int cmd_search_value_in_range(RCore *core, ut64 from, ut64 to, ut64 vmin, ut64 vmax, int vsize) { @@ -160,17 +160,17 @@ R_API int cmd_search_value_in_range(RCore *core, ut64 from, ut64 to, ut64 vmin, eprintf ("Error: vmin must be lower than vmax\n"); return -1; } + r_cons_break_push (NULL, NULL); while (from < to) { memset (buf, 0, sizeof (buf)); // probably unnecessary (void)r_io_read_at (core->io, from, buf, sizeof (buf)); if (r_cons_is_breaked ()) { goto beach; } - for (i=0; i < sizeof (buf) - vsize; i++) { + for (i = 0; i < sizeof (buf) - vsize; i++) { void *v = (buf + i); ut64 addr = from + i; if (r_cons_is_breaked ()) { - eprintf ("BEACH\n"); goto beach; } if (align && (addr) % align) { @@ -178,10 +178,10 @@ R_API int cmd_search_value_in_range(RCore *core, ut64 from, ut64 to, ut64 vmin, } match = false; switch (vsize) { - case 1: n = *(ut8*)(v); match = (buf[i]>=vmin && buf[i]<=vmax); break; - case 2: v16 = *((ut16*)(v)); match = (v16>=vmin && v16<=vmax); n = v16; break; - case 4: v32 = *((ut32 *)(v)); match = (v32>=vmin && v32<=vmax); n = v32; break; - case 8: v64 = *((ut64 *)(v)); match = (v64>=vmin && v64<=vmax); n = v64; break; + case 1: n = *(ut8*)(v); match = (buf[i] >= vmin && buf[i] <= vmax); break; + case 2: v16 = *((ut16*)(v)); match = (v16 >= vmin && v16 <= vmax); n = v16; break; + case 4: v32 = *((ut32 *)(v)); match = (v32 >= vmin && v32 <= vmax); n = v32; break; + case 8: v64 = *((ut64 *)(v)); match = (v64 >= vmin && v64 <= vmax); n = v64; break; default: eprintf ("Unknown vsize\n"); return -1; } if (match && !vinfun) { @@ -211,7 +211,7 @@ R_API int cmd_search_value_in_range(RCore *core, ut64 from, ut64 to, ut64 vmin, from += sizeof (buf); } beach: - r_cons_break_end (); + r_cons_break_pop (); return hitctr; } @@ -1230,10 +1230,11 @@ static int r_core_search_rop(RCore *core, ut64 from, ut64 to, int opt, const cha maplist = true; } - if (json) + if (json) { r_cons_printf ("["); + } + r_cons_break_push (NULL, NULL); - r_cons_break (NULL, NULL); r_list_foreach (list, itermap, map) { from = map->from; to = map->to; @@ -1250,10 +1251,13 @@ static int r_core_search_rop(RCore *core, ut64 from, ut64 to, int opt, const cha from = search_from; } - if (from>to) { + if (from > to) { eprintf ("Invalid range 0x%"PFMT64x" - 0x%"PFMT64x"\n", from, to); continue; } + if (r_cons_is_breaked ()) { + break; + } delta = to - from; if (delta < 1) { delta = from - to; @@ -1279,7 +1283,7 @@ static int r_core_search_rop(RCore *core, ut64 from, ut64 to, int opt, const cha (void)r_io_read_at (core->io, from, buf, delta); // Find the end gadgets. - for (i = 0; i+32 < delta; i += increment) { + for (i = 0; i + 32 < delta; i += increment) { RAnalOp end_gadget = {0}; // Disassemble one. if (r_anal_op (core->anal, &end_gadget, from+i, buf+i, @@ -1304,8 +1308,9 @@ static int r_core_search_rop(RCore *core, ut64 from, ut64 to, int opt, const cha r_list_append (end_list, (void*)epair); } } - if (r_cons_singleton()->breaked) + if (r_cons_is_breaked ()) { break; + } // Right now we have a list of all of the end/stop gadgets. // We can just construct gadgets from a little bit before them. } @@ -1321,8 +1326,9 @@ static int r_core_search_rop(RCore *core, ut64 from, ut64 to, int opt, const cha ropdepth = increment == 1 ? max_instr * max_inst_size_x86 /* wow, x86 is long */ : max_instr * increment; - if (r_cons_singleton()->breaked) + if (r_cons_is_breaked ()) { break; + } struct endlist_pair *end_gadget = (struct endlist_pair *)r_list_pop(end_list); next = end_gadget->instr_offset; prev = 0; @@ -1334,9 +1340,10 @@ static int r_core_search_rop(RCore *core, ut64 from, ut64 to, int opt, const cha } else { if (i < prev) i = prev; } - if (i <0) i = 0; - if (r_cons_singleton()->breaked) + if (i < 0) i = 0; + if (r_cons_is_breaked ()) { break; + } if (i >= next) { // We've exhausted the first end-gadget section, // move to the next one. @@ -1346,13 +1353,13 @@ static int r_core_search_rop(RCore *core, ut64 from, ut64 to, int opt, const cha end_gadget = (struct endlist_pair *)r_list_pop(end_list); next = end_gadget->instr_offset; i = next - ropdepth; - if (i <0) i = 0; + if (i < 0) i = 0; } else { break; } } if (i >= end) { // read by chunk of 4k - r_core_read_at (core, from+i, buf+i, + r_core_read_at (core, from + i, buf + i, R_MIN ((delta-i), 4096)); end = i + 2048; } @@ -1361,10 +1368,9 @@ static int r_core_search_rop(RCore *core, ut64 from, ut64 to, int opt, const cha RList * hitlist; r_asm_set_pc (core->assembler, from+i); hitlist = construct_rop_gadget (core, - from+i, buf, i, grep, regexp, - rx_list, end_gadget, badstart, &max_count); - if (!hitlist) - continue; + from + i, buf, i, grep, regexp, + rx_list, end_gadget, badstart, &max_count); + if (!hitlist) continue; if (align && (0 != ((from + i) % align))) { continue; } @@ -1385,13 +1391,14 @@ static int r_core_search_rop(RCore *core, ut64 from, ut64 to, int opt, const cha r_list_purge (badstart); free (buf); } - if (r_cons_singleton ()->breaked) + if (r_cons_is_breaked ()) { eprintf ("\n"); - r_cons_break_end (); + } + r_cons_break_pop (); - if (json) + if (json) { r_cons_printf ("]\n"); - + } r_list_free (list); r_list_free (rx_list); r_list_free (end_list); @@ -1429,11 +1436,12 @@ static void do_esil_search(RCore *core, struct search_parameters *param, const c int hit_combo = 0; char *res; ut64 nres, addr = param->from; - r_cons_break (NULL, NULL); - if (!core->anal->esil) + if (!core->anal->esil) { core->anal->esil = r_anal_esil_new (stacksize, iotrap); - if (!core->anal->esil) + } + if (!core->anal->esil) { return; + } /* hook addrinfo */ core->anal->esil->cb.user = core; r_anal_esil_set_op (core->anal->esil, "AddrInfo", esil_addrinfo); @@ -1441,7 +1449,9 @@ static void do_esil_search(RCore *core, struct search_parameters *param, const c r_anal_esil_setup (core->anal->esil, core->anal, 1, 0, nonull); r_anal_esil_stack_free (core->anal->esil); core->anal->esil->verbose = 0; - for (; addrto; addr++) { + + r_cons_break_push (NULL, NULL); + for (; addr < param->to; addr++) { if (core->search->align) { if ((addr % core->search->align)) { continue; @@ -1458,7 +1468,7 @@ static void do_esil_search(RCore *core, struct search_parameters *param, const c // inheap r_anal_esil_set_op (core->anal->esil, "AddressInfo", esil_search_address_info); #endif - if (r_cons_singleton ()->breaked) { + if (r_cons_is_breaked ()) { eprintf ("Breaked at 0x%08"PFMT64x"\n", addr); break; } @@ -1504,8 +1514,10 @@ static void do_esil_search(RCore *core, struct search_parameters *param, const c } } r_config_set_i (core->config, "search.kwidx", kwidx +1); - r_cons_break_end (); - } else eprintf ("Usage: /E [esil-expr]\n"); + r_cons_break_pop (); + } else { + eprintf ("Usage: /E [esil-expr]\n"); + } r_cons_clear_line (1); } @@ -1564,15 +1576,15 @@ static void do_anal_search(RCore *core, struct search_parameters *param, const c input = r_str_chop_ro (input); buf = malloc (bsize); maxhits = (int)r_config_get_i (core->config, "search.count"); - r_cons_break (NULL, NULL); - for (i = 0, at = param->from; at < param->to; at++,i++) { - if (r_cons_singleton()->breaked) { + r_cons_break_push (NULL, NULL); + for (i = 0, at = param->from; at < param->to; at++, i++) { + if (r_cons_is_breaked ()) { break; } if (i >= (bsize - 32)) { i = 0; } - if (i == 0) { + if (!i) { r_core_read_at (core, at, buf, bsize); } ret = r_anal_op (core->anal, &aop, at, buf + i, bsize - i); @@ -1636,7 +1648,7 @@ static void do_anal_search(RCore *core, struct search_parameters *param, const c if (mode == 'j') { r_cons_println ("]\n"); } - r_cons_break_end (); + r_cons_break_pop (); free (buf); } @@ -1655,8 +1667,9 @@ static void do_asm_search(RCore *core, struct search_parameters *param, const ch } else { outmode = *(end_cmd - 1); } - if (outmode != 'j') + if (outmode != 'j') { json = 0; + } if (!strncmp (param->mode, "dbg.", 4) || !strncmp (param->mode, "io.sections", 11)) { param->boundaries = r_core_get_boundaries (core, param->mode, ¶m->from, ¶m->to); @@ -1680,17 +1693,17 @@ static void do_asm_search(RCore *core, struct search_parameters *param, const ch if (json) { r_cons_print ("["); } - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); r_list_foreach (param->boundaries, itermap, map) { param->from = map->from; param->to = map->to; - if (r_cons_singleton()->breaked) { + if (r_cons_is_breaked ()) { break; } if (maxhits && count >= maxhits) { break; } - if (outmode == 0) { + if (!outmode) { hits = NULL; } else { hits = r_core_asm_strsearch (core, input+2, @@ -1698,8 +1711,9 @@ static void do_asm_search(RCore *core, struct search_parameters *param, const ch } if (hits) { r_list_foreach (hits, iter, hit) { - if (r_cons_singleton()->breaked) + if (r_cons_is_breaked ()) { break; + } switch (outmode) { case 'j': if (count > 0) r_cons_printf (","); @@ -1734,7 +1748,7 @@ static void do_asm_search(RCore *core, struct search_parameters *param, const ch } } if (json) r_cons_printf ("]"); - r_cons_break_end (); + r_cons_break_pop (); if (maplist) { param->boundaries->free = free; @@ -1747,10 +1761,12 @@ static void do_string_search(RCore *core, struct search_parameters *param) { ut64 at; ut8 *buf; int ret; - - if (json) r_cons_printf("["); int oraise = core->io->raised; int bufsz; + + if (json) { + r_cons_printf("["); + } RListIter *iter; RIOMap *map; if (!searchflags && !json) { @@ -1758,9 +1774,10 @@ static void do_string_search(RCore *core, struct search_parameters *param) { } core->search->inverse = param->inverse; searchcount = r_config_get_i (core->config, "search.count"); - if (searchcount) + if (searchcount) { searchcount++; - if (core->search->n_kws>0 || param->crypto_search) { + } + if (core->search->n_kws > 0 || param->crypto_search) { RSearchKeyword aeskw; if (param->crypto_search) { memset (&aeskw, 0, sizeof (aeskw)); @@ -1773,7 +1790,6 @@ static void do_string_search(RCore *core, struct search_parameters *param) { // REMOVE OLD FLAGS r_core_cmdf (core, "f-%s*", r_config_get (core->config, "search.prefix")); r_search_set_callback (core->search, &__cb_hit, core); cmdhit = r_config_get (core->config, "cmd.hit"); - r_cons_break (NULL, NULL); // XXX required? imho nor_io_set_fd (core->io, core->file->fd); if (!param->boundaries) { RIOMap *map = R_NEW0 (RIOMap); @@ -1786,12 +1802,15 @@ static void do_string_search(RCore *core, struct search_parameters *param) { } buf = (ut8 *)malloc (core->blocksize); bufsz = core->blocksize; + r_cons_break_push (NULL, NULL); r_list_foreach (param->boundaries, iter, map) { int fd; param->from = map->from; param->to = map->to; searchhits = 0; - + if (r_cons_is_breaked ()) { + break; + } if (param->to < param->from) { eprintf ("invalid from/to values\n"); break; @@ -1818,7 +1837,9 @@ static void do_string_search(RCore *core, struct search_parameters *param) { if ((param->to - bufsz) <= param->from) { at = param->from; param->do_bckwrd_srch = false; - } else at = param->to - bufsz; + } else { + at = param->to - bufsz; + } } else { at = param->from; } @@ -1826,18 +1847,14 @@ static void do_string_search(RCore *core, struct search_parameters *param) { bckwrds search -> check later */ for (; (!param->bckwrds && at < param->to) || param->bckwrds;) { print_search_progress (at, param->to, searchhits); - if (r_cons_singleton ()->breaked) { + if (r_cons_is_breaked ()) { eprintf ("\n\n"); break; } - // avoid searching beyond limits if ((at + bufsz) > param->to) { bufsz = param->to - at; } - - //ret = r_core_read_at (core, at, buf, bufsz); - // ret = r_io_read_at (core->io, at, buf, bufsz); if (param->use_mread) { // what about a config var to choose which io api to use? ret = r_io_mread (core->io, fd, at, buf, bufsz); @@ -1845,21 +1862,16 @@ static void do_string_search(RCore *core, struct search_parameters *param) { r_io_seek (core->io, at, R_IO_SEEK_SET); ret = r_io_read (core->io, buf, bufsz); } - /* - if (ignorecase) { - int i; - for (i=0; icrypto_search) { int delta = 0; - if (param->aes_search) + if (param->aes_search) { delta = r_search_aes_update (core->search, at, buf, ret); - else if (param->rsa_search) + } else if (param->rsa_search) { delta = r_search_rsa_update (core->search, at, buf, ret); + } if (delta != -1) { if (!r_search_hit_new (core->search, &aeskw, at+delta)) { break; @@ -1887,7 +1899,6 @@ static void do_string_search(RCore *core, struct search_parameters *param) { } } print_search_progress (at, param->to, searchhits); - r_cons_break_end (); r_cons_clear_line (1); core->num->value = searchhits; if (searchflags && (searchcount>0) && !json) { @@ -1906,6 +1917,7 @@ static void do_string_search(RCore *core, struct search_parameters *param) { } } } + r_cons_break_pop (); free (buf); if (maplist) { param->boundaries->free = free; @@ -1913,13 +1925,17 @@ static void do_string_search(RCore *core, struct search_parameters *param) { param->boundaries = NULL; } r_io_raise (core->io, oraise); - } else eprintf ("No keywords defined\n"); + } else { + eprintf ("No keywords defined\n"); + } /* Crazy party counter (kill me please) */ - if ((searchhits == 0 ) && (core->search->n_kws > 0)) + if (!searchhits && core->search->n_kws > 0) { core->search->n_kws--; - - if (json) r_cons_printf("]"); + } + if (json) { + r_cons_printf("]"); + } } static void rop_kuery(void *data, const char *input) { @@ -2249,14 +2265,15 @@ reread: } break; case 'm': // "/m" dosearch = false; - if (input[1]==' ' || input[1]=='\0') { + if (input[1] == ' ' || input[1] == '\0') { int ret; const char *file = input[1]? input+2: NULL; ut64 addr = param.from; - r_cons_break (NULL, NULL); - for (; addrbreaked) + r_cons_break_push (NULL, NULL); + for (; addr < param.to; addr++) { + if (r_cons_is_breaked ()) { break; + } ret = r_core_magic_at (core, file, addr, 99, false); if (ret == -1) { // something went terribly wrong. @@ -2265,19 +2282,21 @@ reread: addr += ret-1; } r_cons_clear_line (1); - r_cons_break_end (); - } else eprintf ("Usage: /m [file]\n"); + r_cons_break_pop (); + } else { + eprintf ("Usage: /m [file]\n"); + } r_cons_clear_line (1); break; case 'p': // "/p" { if (input[param_offset-1]) { int ps = atoi (input+param_offset); - if (ps>1) { - r_cons_break (NULL, NULL); + if (ps > 1) { + r_cons_break_push (NULL, NULL); r_search_pattern_size (core->search, ps); r_search_pattern (core->search, param.from, param.to); - r_cons_break_end (); + r_cons_break_pop (); break; } } diff --git a/libr/core/cmd_seek.c b/libr/core/cmd_seek.c index c22bc88d5c..e99473cac4 100644 --- a/libr/core/cmd_seek.c +++ b/libr/core/cmd_seek.c @@ -90,15 +90,17 @@ R_API int r_core_lines_initcache (RCore *core, ut64 start_addr, ut64 end_addr) { line_count = start_addr ? 0 : 1; core->print->lines_cache[0] = start_addr ? 0 : baddr; - r_cons_break (NULL, NULL); buf = malloc (bsz); - if (!buf) return -1; + if (!buf) { + return -1; + } + r_cons_break_push (NULL, NULL); while (off < end_addr) { - if (r_cons_singleton ()->breaked) { + if (r_cons_is_breaked ()) { break; } r_io_read_at (core->io, off, (ut8*)buf, bsz); - for (i=0; iprint->lines_cache[line_count] = start_addr ? off+i+1 : off+i+1+baddr; line_count++; @@ -117,11 +119,11 @@ R_API int r_core_lines_initcache (RCore *core, ut64 start_addr, ut64 end_addr) { off += bsz; } free (buf); - r_cons_break_end (); + r_cons_break_pop (); return line_count; beach: free (buf); - r_cons_break_end(); + r_cons_break_pop (); return -1; } diff --git a/libr/core/cmd_zign.c b/libr/core/cmd_zign.c index af6b57c27c..b322b7e9f4 100644 --- a/libr/core/cmd_zign.c +++ b/libr/core/cmd_zign.c @@ -31,7 +31,7 @@ static void fcn_zig_search(RCore *core, ut64 ini, ut64 fin) { eprintf ("Ranges are: 0x%08"PFMT64x" 0x%08"PFMT64x"\n", ini, fin); old_fs = core->flags->space_idx; r_cons_printf ("fs sign\n"); - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); if (r_io_read_at (core->io, ini, buf, len) == len) { ut64 align = r_config_get_i (core->config, "search.align"); for (idx = 0; idx < len; idx++) { @@ -52,7 +52,7 @@ static void fcn_zig_search(RCore *core, ut64 ini, ut64 fin) { eprintf ("Cannot read %"PFMT64d" bytes at 0x%08"PFMT64x"\n", len, ini); } r_cons_printf ("fs %s\n", (old_fs == -1) ? "*" : core->flags->spaces[old_fs]); - r_cons_break_end (); + r_cons_break_pop (); free (buf); core->sign->matches = count; } else { @@ -101,7 +101,7 @@ static int cmd_zign(void *data, const char *input) { switch (*input) { case 'B': - if (input[1]==' ' && input[2]) { + if (input[1] == ' ' && input[2]) { ut8 buf[128]; ut64 addr = core->offset; int size = 32; @@ -118,16 +118,23 @@ static int cmd_zign(void *data, const char *input) { name = flag->name; r_cons_printf ("zb %s ", name); len = R_MIN (size, sizeof (buf)); - for (i=0; ifdout; int minzlen = r_config_get_i (core->config, "zign.min"); int maxzlen = r_config_get_i (core->config, "zign.max"); @@ -143,13 +150,17 @@ static int cmd_zign(void *data, const char *input) { r_cons_strcat ("# Signatures\n"); } r_cons_printf ("zn %s\n", input + 2); + r_cons_break_push (NULL, NULL); r_list_foreach (core->anal->fcns, iter, fcni) { RAnalOp *op = NULL; int zlen, len, oplen, idx = 0; ut8 *buf; - + if (r_cons_is_breaked ()) { + break; + } len = r_anal_fcn_realsize (fcni); if (!(buf = calloc (1, len))) { + r_cons_break_pop (); return false; } /* XXX this is wrong. we must read for each basic block not the whole function length */ @@ -159,6 +170,7 @@ static int cmd_zign(void *data, const char *input) { name = flag->name; if (!(op = r_anal_op_new ())) { free (buf); + r_cons_break_pop (); return false; } zlen = 0; @@ -166,10 +178,11 @@ static int cmd_zign(void *data, const char *input) { zlen = len; } else { while (idx < len) { - if ((oplen = r_anal_op (core->anal, op, fcni->addr + idx, buf + idx, len - idx)) < 1) { + oplen = r_anal_op (core->anal, op, fcni->addr + idx, buf + idx, len - idx); + if (oplen < 1) { break; } - if (op->nopcode != 0) { + if (op->nopcode) { int left = R_MAX (oplen - op->nopcode, 0); memset (buf + idx + op->nopcode, 0, left); } @@ -181,7 +194,7 @@ static int cmd_zign(void *data, const char *input) { r_cons_printf ("zb %s ", name); for (i = 0; i < len; i++) { /* XXX assuming buf[i] == 0 is wrong because mask != data */ - if (buf[i] == 0) { + if (!buf[i]) { r_cons_printf (".."); } else { r_cons_printf ("%02x", buf[i]); @@ -204,27 +217,32 @@ static int cmd_zign(void *data, const char *input) { free (buf); r_anal_op_free (op); } + r_cons_break_pop (); r_cons_strcat ("zn-\n"); if (ptr) { r_cons_flush (); r_cons_singleton ()->fdout = fdold; close (fd); } - } else eprintf ("Usage: zg libc [libc.sig]\n"); + } else { + eprintf ("Usage: zg libc [libc.sig]\n"); + } break; case 'n': - if (!input[1]) + if (!input[1]) { r_cons_println (core->sign->ns); - else if (!strcmp ("-", input+1)) + } else if (!strcmp ("-", input + 1)) { r_sign_ns (core->sign, ""); - else r_sign_ns (core->sign, input+2); + } else { + r_sign_ns (core->sign, input + 2); + } break; case 'a': case 'b': case 'h': case 'f': case 'p': - if (*(input+1) == '\0' || *(input+2) == '\0') + if (*(input + 1) == '\0' || *(input + 2) == '\0') eprintf ("Usage: z%c [name] [arg]\n", *input); else{ ptr = strchr (input+3, ' '); @@ -236,8 +254,9 @@ static int cmd_zign(void *data, const char *input) { break; case 'c': item = r_sign_check (core->sign, core->block, core->blocksize); - if (item) + if (item) { r_cons_printf ("f sign.%s @ 0x%08"PFMT64x"\n", item->name, core->offset); + } break; case '-': if (input[1] == '*') { @@ -261,7 +280,6 @@ static int cmd_zign(void *data, const char *input) { eprintf ("Usage: z%c [ini] [end]\n", *input); return false; } - char *ptr = strchr (input+2, ' '); if (ptr) { *ptr = '\0'; @@ -327,7 +345,6 @@ static int cmd_zign(void *data, const char *input) { int old_fs; RListIter *it; ut8 *buf; - if (r_list_empty (core->anal->fcns)) { eprintf("No functions found, please run some analysis before.\n"); return false; @@ -339,9 +356,6 @@ static int cmd_zign(void *data, const char *input) { return false; } fcni = (RAnalFunction*)it->data; - if (r_cons_singleton ()->breaked) { - break; - } len = r_anal_fcn_realsize (fcni); if (!(buf = malloc (len))) { return false; @@ -357,7 +371,6 @@ static int cmd_zign(void *data, const char *input) { } } free (buf); - r_cons_break_end (); core->sign->matches += count; } break; diff --git a/libr/core/core.c b/libr/core/core.c index c196f2bd8c..15914b9dbe 100644 --- a/libr/core/core.c +++ b/libr/core/core.c @@ -1066,8 +1066,9 @@ R_API int r_core_fgets(char *buf, int len) { const char *ptr; RLine *rli = r_line_singleton (); buf[0] = '\0'; - if (rli->completion.argv != radare_argv) + if (rli->completion.argv != radare_argv) { r_line_free_autocomplete (rli); + } rli->completion.argc = CMDS; rli->completion.argv = radare_argv; rli->completion.run = autocomplete; @@ -1077,7 +1078,7 @@ R_API int r_core_fgets(char *buf, int len) { } strncpy (buf, ptr, len); buf[len - 1] = 0; - return strlen (buf)+1; + return strlen (buf) + 1; } /*-----------------------------------*/ @@ -1627,7 +1628,7 @@ R_API RCore *r_core_free(RCore *c) { R_API void r_core_prompt_loop(RCore *r) { int ret; do { - if (r_core_prompt (r, false)<1) { + if (r_core_prompt (r, false) < 1) { break; } // if (lock) r_th_lock_enter (lock); @@ -1761,7 +1762,6 @@ R_API int r_core_prompt(RCore *r, int sync) { rnv = r->num->value; set_prompt (r); - ret = r_cons_fgets (line, sizeof (line), 0, NULL); if (ret == -2) { return R_CORE_CMD_EXIT; // ^D @@ -1900,24 +1900,25 @@ R_API int r_core_serve(RCore *core, RIODesc *file) { fd = rior->fd; eprintf ("RAP Server started (rap.loop=%s)\n", r_config_get (core->config, "rap.loop")); + + r_cons_break_push (rap_break, rior); reaccept: core->io->plugin = NULL; - r_cons_break (rap_break, rior); - while (!core->cons->breaked) { + while (!r_cons_is_breaked ()) { c = r_socket_accept (fd); if (!c) { break; } - if (core->cons->breaked) { - return -1; + if (r_cons_is_breaked ()) { + goto out_of_function; } if (!c) { eprintf ("rap: cannot accept\n"); r_socket_free (c); - return -1; + goto out_of_function; } eprintf ("rap: client connected\n"); - for (;!core->cons->breaked;) { + for (;!r_cons_is_breaked ();) { if (!r_socket_read (c, &cmd, 1)) { eprintf ("rap: connection closed\n"); if (r_config_get_i (core->config, "rap.loop")) { @@ -1925,7 +1926,7 @@ reaccept: r_socket_free (c); goto reaccept; } - return -1; + goto out_of_function; } switch ((ut8)cmd) { case RMT_OPEN: @@ -1962,78 +1963,13 @@ reaccept: pipefd = -1; eprintf ("Cannot open file (%s)\n", ptr); r_socket_close (c); - return -1; //XXX: Close conection and goto accept + goto out_of_function; //XXX: Close conection and goto accept } } buf[0] = RMT_OPEN | RMT_REPLY; r_write_be32 (buf + 1, pipefd); r_socket_write (c, buf, 5); r_socket_flush (c); -#if 0 - /* Write meta info */ - RMetaItem *d; - r_list_foreach (core->anal->meta->data, iter, d) { - if (d->type == R_META_TYPE_COMMENT) - snprintf ((char *)buf, sizeof (buf), "%s %s @ 0x%08"PFMT64x, - r_meta_type_to_string (d->type), d->str, d->from); - else - snprintf ((char *)buf, sizeof (buf), - "%s %d %s @ 0x%08"PFMT64x, - r_meta_type_to_string (d->type), - (int)(d->to-d->from), d->str, d->from); - i = strlen ((char *)buf); - r_mem_copyendian ((ut8 *)&j, (ut8 *)&i, 4, !LE); - r_socket_write (c, (ut8 *)&j, 4); - r_socket_write (c, buf, i); - r_socket_flush (c); - } -#endif -#if 0 - RIOSection *s; - r_list_foreach_prev (core->io->sections, iter, s) { - snprintf ((char *)buf, sizeof (buf), - "S 0x%08"PFMT64x" 0x%08"PFMT64x" 0x%08"PFMT64x" 0x%08"PFMT64x" %s %d", - s->offset, s->vaddr, s->size, s->vsize, s->name, s->rwx); - i = strlen ((char *)buf); - r_mem_copyendian ((ut8 *)&j, (ut8 *)&i, 4, !LE); - r_socket_write (c, (ut8 *)&j, 4); - r_socket_write (c, buf, i); - r_socket_flush (c); - } -#endif -#if 0 - int fs = -1; - RFlagItem *flag; - r_list_foreach_prev (core->flags->flags, iter, flag) { - if (fs == -1 || flag->space != fs) { - fs = flag->space; - snprintf ((char *)buf, sizeof (buf), - "fs %s", r_flag_space_get_i (core->flags, fs)); - i = strlen ((char *)buf); - r_mem_copyendian ((ut8 *)&j, (ut8 *)&i, 4, !LE); - r_socket_write (c, (ut8 *)&j, 4); - r_socket_write (c, buf, i); - } - snprintf ((char *)buf, sizeof (buf), - "f %s %"PFMT64d" 0x%08"PFMT64x, - flag->name, flag->size, flag->offset); - i = strlen ((char *)buf); - r_mem_copyendian ((ut8 *)&j, (ut8 *)&i, 4, !LE); - r_socket_write (c, (ut8 *)&j, 4); - r_socket_write (c, buf, i); - r_socket_flush (c); - } - - snprintf ((char *)buf, sizeof (buf), "s 0x%"PFMT64x, core->offset); - i = strlen ((char *)buf); - r_mem_copyendian ((ut8 *)&j, (ut8 *)&i, 4, !LE); - r_socket_write (c, (ut8 *)&j, 4); - r_socket_write (c, buf, i); - - i = 0; - r_socket_write (c, (ut8 *)&i, 4); - r_socket_flush (c); -#endif free (ptr); ptr = NULL; break; @@ -2063,7 +1999,7 @@ reaccept: eprintf ("Cannot read %d bytes\n", i); r_socket_free (c); // TODO: reply error here - return -1; + goto out_of_function; } break; case RMT_CMD: @@ -2188,12 +2124,13 @@ reaccept: r_socket_close (c); free (ptr); ptr = NULL; - return -1; + goto out_of_function; } } - r_cons_break_end (); eprintf ("client: disconnected\n"); } +out_of_function: + r_cons_break_pop (); return -1; } diff --git a/libr/core/disasm.c b/libr/core/disasm.c index bc2cc34afd..9343218550 100644 --- a/libr/core/disasm.c +++ b/libr/core/disasm.c @@ -3461,18 +3461,19 @@ toro: } ds_print_esil_anal_init (ds); - r_cons_break (NULL, NULL); inc = 0; if (!ds->l) { len = ds->l = core->blocksize; } + r_cons_break_push (NULL, NULL); r_anal_build_range_on_hints (core->anal); for (i = idx = ret = 0; idx < len && ds->lines < ds->l; idx += inc, i++, ds->index += inc, ds->lines++) { ds->at = ds->addr + idx; ds->vat = p2v (ds, ds->at); - if (core->cons && core->cons->breaked) { + if (r_cons_is_breaked ()) { dorepeat = 0; + r_cons_break_pop (); return 0; //break; } r_core_seek_archbits (core, ds->at); // slow but safe @@ -3514,8 +3515,6 @@ toro: r_io_read_at (core->io, ds->addr, buf, len); inc = 0; //delta; idx = 0; - // r_cons_printf ("delta %d fsize %d\n", delta, f->size); - // inc = 1; continue; } } @@ -3564,6 +3563,7 @@ toro: } if (ds->retry) { ds->retry = 0; + r_cons_break_pop (); goto retry; } ds_atabs_option (ds); @@ -3695,7 +3695,7 @@ toro: if (nbuf == buf) { R_FREE (buf); } - r_cons_break_end (); + r_cons_break_pop (); #if HASRETRY if (!ds->cbytes && ds->lines < ds->l && dorepeat) { @@ -3796,8 +3796,8 @@ R_API int r_core_print_disasm_instructions(RCore *core, int nb_bytes, int nb_opc if (!ds->l) { ds->l = ds->len; } - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); //build ranges to map addr with bits r_anal_build_range_on_hints (core->anal); #define isTheEnd (nb_opcodes? jvat = p2v (ds, ds->at); hasanal = false; r_core_seek_archbits (core, ds->at); - if (r_cons_singleton ()->breaked) { + if (r_cons_is_breaked ()) { break; } ds->hint = r_core_hint_begin (core, ds->hint, ds->at); @@ -3918,7 +3918,7 @@ R_API int r_core_print_disasm_instructions(RCore *core, int nb_bytes, int nb_opc ds->hint = NULL; } } - r_cons_break_end (); + r_cons_break_pop (); if (ds->oldbits) { r_config_set_i (core->config, "asm.bits", ds->oldbits); ds->oldbits = 0; @@ -4197,12 +4197,12 @@ R_API int r_core_print_disasm_all(RCore *core, ut64 addr, int l, int len, int mo if (mode == 'j') { r_cons_printf ("["); } - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); for (i = 0; i < l; i++) { ds->at = addr + i; ds->vat = p2v (ds, ds->at); r_asm_set_pc (core->assembler, ds->vat); - if (r_cons_singleton ()->breaked) { + if (r_cons_is_breaked ()) { break; } ret = r_asm_disassemble (core->assembler, &asmop, buf + i, l - i); @@ -4271,7 +4271,7 @@ R_API int r_core_print_disasm_all(RCore *core, ut64 addr, int l, int len, int mo } } } - r_cons_break_end (); + r_cons_break_pop (); if (buf != core->block) { free (buf); } @@ -4348,7 +4348,7 @@ R_API int r_core_print_fcn_disasm(RPrint *p, RCore *core, ut64 addr, int l, int core->inc = 0; core->cons->vline = r_config_get_i (core->config, "scr.utf8")? r_vline_u: r_vline_a; i = idx = 0; - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); ds_print_esil_anal_init (ds); if (core->io && core->io->debug) { @@ -4372,7 +4372,7 @@ R_API int r_core_print_fcn_disasm(RPrint *p, RCore *core, ut64 addr, int l, int // XXX - why is it necessary to set this everytime? r_asm_set_pc (core->assembler, ds->at); if (ds->lines >= ds->l) break; - if (r_cons_singleton ()->breaked) break; + if (r_cons_is_breaked ()) break; ds_update_ref_lines (ds); /* show type links */ @@ -4382,22 +4382,19 @@ R_API int r_core_print_fcn_disasm(RPrint *p, RCore *core, ut64 addr, int l, int ret = ds_disassemble (ds, buf+idx, len - bb_size_consumed); ds_atabs_option (ds); // TODO: store previous oplen in core->dec - if (core->inc == 0) { + if (!core->inc) { core->inc = ds->oplen; } - r_anal_op_fini (&ds->analop); - - if (!ds->lastfail) + if (!ds->lastfail) { r_anal_op (core->anal, &ds->analop, ds->at+bb_size_consumed, buf+idx, len-bb_size_consumed); - + } if (ret < 1) { r_strbuf_init (&ds->analop.esil); ds->analop.type = R_ANAL_OP_TYPE_ILL; } - ds_instruction_mov_lea (ds, idx); ds_control_flow_comments (ds); ds_adistrick_comments (ds); @@ -4455,7 +4452,7 @@ R_API int r_core_print_fcn_disasm(RPrint *p, RCore *core, ut64 addr, int l, int ds_print_comments_right (ds); ds_show_refs (ds); ds_print_esil_anal (ds); - if ( !(ds->show_comments && ds->show_comment_right && ds->comment)) { + if (!(ds->show_comments && ds->show_comment_right && ds->comment)) { r_cons_newline (); } if (ds->line) { @@ -4477,7 +4474,7 @@ R_API int r_core_print_fcn_disasm(RPrint *p, RCore *core, ut64 addr, int l, int i++; } free (buf); - r_cons_break_end (); + r_cons_break_pop (); ds_print_esil_anal_fini (ds); if (ds->oldbits) { diff --git a/libr/core/rtr.c b/libr/core/rtr.c index f11dc84250..3ee9f2bd9d 100644 --- a/libr/core/rtr.c +++ b/libr/core/rtr.c @@ -155,10 +155,10 @@ beach: free (oldprompt); } -static bool rtr_visual (RCore *core, TextLog T, const char *cmd) { +static bool rtr_visual(RCore *core, TextLog T, const char *cmd) { bool autorefresh = false; if (cmd) { - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); for (;;) { char *ret; r_cons_clear00 (); @@ -166,11 +166,12 @@ static bool rtr_visual (RCore *core, TextLog T, const char *cmd) { r_cons_println (ret); free (ret); r_cons_flush (); - if (r_cons_singleton ()->breaked) + if (r_cons_is_breaked ()) { break; + } r_sys_sleep (1); } - r_cons_break_end (); + r_cons_break_pop (); } else { const char *cmds[] = { "px", "pd", "pxa", "dr", "sr SP;pxa", NULL }; int cmdidx = 0; @@ -188,16 +189,16 @@ static bool rtr_visual (RCore *core, TextLog T, const char *cmd) { if (autorefresh) { r_cons_printf ("(auto-refresh)\n"); r_cons_flush (); - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); r_sys_sleep (1); - if (r_cons_singleton ()->breaked) { + if (r_cons_is_breaked ()) { autorefresh = false; ch = r_cons_readchar (); } else { - r_cons_break_end (); + r_cons_break_pop (); continue; } - r_cons_break_end (); + r_cons_break_pop (); } else { ch = r_cons_readchar (); } @@ -382,7 +383,7 @@ static void activateDieTime (RCore *core) { } // return 1 on error -static int r_core_rtr_http_run (RCore *core, int launch, const char *path) { +static int r_core_rtr_http_run(RCore *core, int launch, const char *path) { RConfig *newcfg = NULL, *origcfg = NULL; char headers[128] = {0}; RSocketHTTPRequest *rs; @@ -488,9 +489,8 @@ static int r_core_rtr_http_run (RCore *core, int launch, const char *path) { core->block = newblk; // TODO: handle mutex lock/unlock here - while (!r_cons_singleton ()->breaked) { - r_cons_break ((RConsBreak)r_core_rtr_http_stop, core); - + r_cons_break_push ((RConsBreak)r_core_rtr_http_stop, core); + while (!r_cons_is_breaked ()) { /* restore environment */ core->config = origcfg; r_config_set (origcfg, "scr.html", r_config_get (origcfg, "scr.html")); @@ -538,7 +538,9 @@ static int r_core_rtr_http_run (RCore *core, int launch, const char *path) { //eprintf ("Firewall (%s)\n", allows); int i, count = r_str_split (allows, ','); p = strchr (peer, ':'); - if (p) *p = 0; + if (p) { + *p = 0; + } for (i = 0; i < count; i++) { allows_host = r_str_word_get0 (allows, i); //eprintf ("--- (%s) (%s)\n", host, peer); @@ -566,10 +568,11 @@ static int r_core_rtr_http_run (RCore *core, int launch, const char *path) { http_logf (core, "[HTTP] %s %s\n", peer, rs->path); free (peer); } - if (r_config_get_i (core->config, "http.dirlist")) - if (r_file_is_directory (rs->path)) + if (r_config_get_i (core->config, "http.dirlist")) { + if (r_file_is_directory (rs->path)) { dir = strdup (rs->path); - + } + } if (r_config_get_i (core->config, "http.cors")) { strcpy (headers, "Access-Control-Allow-Origin: *\n" "Access-Control-Allow-Headers: Origin, " @@ -611,8 +614,7 @@ static int r_core_rtr_http_run (RCore *core, int launch, const char *path) { free (path); } } else { - r_socket_http_response (rs, 403, - "Permission denied\n", 0, NULL); + r_socket_http_response (rs, 403, "Permission denied\n", 0, NULL); } } else if (!strncmp (rs->path, "/cmd/", 5)) { char *cmd = rs->path + 5; @@ -631,7 +633,7 @@ static int r_core_rtr_http_run (RCore *core, int launch, const char *path) { httpref_enabled = false; } - while (*cmd=='/') cmd++; + while (*cmd == '/') cmd++; if (httpref_enabled && (!rs->referer || (refstr && !strstr (rs->referer, refstr)))) { r_socket_http_response (rs, 503, "", 0, headers); } else { @@ -669,7 +671,6 @@ static int r_core_rtr_http_run (RCore *core, int launch, const char *path) { // eprintf ("CMD (%s)\n", cmd); out = r_core_cmd_str_pipe (core, cmd); } - // eprintf ("\nOUT LEN = %d\n", strlen (out)); if (out) { char *res = r_str_uri_encode (out); char *newheaders = r_str_newf ( @@ -724,9 +725,15 @@ static int r_core_rtr_http_run (RCore *core, int launch, const char *path) { char *f = r_file_slurp (path, &sz); if (f) { const char *ct = NULL; - if (strstr (path, ".js")) ct = "Content-Type: application/javascript\n"; - if (strstr (path, ".css")) ct = "Content-Type: text/css\n"; - if (strstr (path, ".html")) ct = "Content-Type: text/html\n"; + if (strstr (path, ".js")) { + ct = "Content-Type: application/javascript\n"; + } + if (strstr (path, ".css")) { + ct = "Content-Type: text/css\n"; + } + if (strstr (path, ".html")) { + ct = "Content-Type: text/html\n"; + } char *hdr = r_str_newf ("%s%s", ct, headers); r_socket_http_response (rs, 200, f, sz, hdr); free (hdr); @@ -796,7 +803,7 @@ the_end: r_config_set (core->config, "http.allow", allow); r_config_set (core->config, "http.ui", httpui); } - r_cons_break_end (); + r_cons_break_pop (); core->http_up = false; r_socket_free (s); r_config_free (newcfg); @@ -1400,8 +1407,11 @@ R_API int r_core_rtr_cmds (RCore *core, const char *port) { eprintf ("Listening for commands on port %s\n", port); listenport = port; + r_cons_break_push ((RConsBreak)r_core_rtr_http_stop, core); for (;;) { - r_cons_break ((RConsBreak)r_core_rtr_http_stop, core); + if (r_cons_is_breaked ()) { + break; + } ch = r_socket_accept (s); buf[0] = 0; ret = r_socket_read (ch, buf, sizeof (buf) - 1); @@ -1409,10 +1419,10 @@ R_API int r_core_rtr_cmds (RCore *core, const char *port) { buf[ret] = 0; for (i = 0; buf[i]; i++) { if (buf[i] == '\n') - buf[i] = buf[i+1]? ';': '\0'; + buf[i] = buf[i + 1]? ';': '\0'; } - if (!r_config_get_i (core->config, "scr.prompt") \ - && !strcmp ((char*)buf, "q!")) + if (!r_config_get_i (core->config, "scr.prompt") && + !strcmp ((char *)buf, "q!")) break; str = r_core_cmd_str (core, (const char *)buf); if (str && *str) { @@ -1422,12 +1432,10 @@ R_API int r_core_rtr_cmds (RCore *core, const char *port) { } free (str); } - if (r_cons_singleton()->breaked) - break; r_socket_close (ch); r_socket_free (ch); - r_cons_break_end (); } + r_cons_break_pop (); r_socket_free (s); r_socket_free (ch); return 0; diff --git a/libr/core/task.c b/libr/core/task.c index 8605c9466c..54108082d2 100644 --- a/libr/core/task.c +++ b/libr/core/task.c @@ -35,9 +35,9 @@ R_API void r_core_task_list (RCore *core, int mode) { R_API void r_core_task_join (RCore *core, RCoreTask *task) { RListIter *iter; if( task) { - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); r_th_wait (task->msg->th); - r_cons_break_end (); + r_cons_break_pop (); } else { r_list_foreach_prev (core->tasks, iter, task) { r_th_wait (task->msg->th); diff --git a/libr/core/visual.c b/libr/core/visual.c index ef676ef84e..386a8842cb 100644 --- a/libr/core/visual.c +++ b/libr/core/visual.c @@ -31,8 +31,9 @@ static const char **printfmt = printfmtSingle; static int visual_repeat_thread(RThread *th) { RCore *core = th->user; int i = 0; + r_cons_break_push (NULL, NULL); for (;;) { - if (core->cons->breaked) { + if (r_cons_is_breaked ()) { break; } visual_refresh (core); @@ -42,6 +43,7 @@ static int visual_repeat_thread(RThread *th) { r_cons_flush (); r_sys_sleep (1); } + r_cons_break_pop (); r_th_kill (th, 1); return 0; } @@ -117,14 +119,16 @@ static void visual_repeat(RCore *core) { #endif } else { RThread *th = r_th_new (visual_repeat_thread, core, 0); - if (!th) return; + if (!th) { + return; + } r_th_start (th, 1); - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); r_cons_any_key (NULL); eprintf ("^C \n"); core->cons->breaked = true; r_th_wait (th); - r_cons_break_end (); + r_cons_break_pop (); } } #endif diff --git a/libr/core/vmenus.c b/libr/core/vmenus.c index b9304fe7cf..07cbf93625 100644 --- a/libr/core/vmenus.c +++ b/libr/core/vmenus.c @@ -2713,10 +2713,9 @@ repeat: } //depth = 0; } - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); r_core_cmdf (core, "af @ 0x%08" PFMT64x, off); // required for thumb autodetection - //r_core_anal_fcn (core, off, UT64_MAX, R_ANAL_REF_TYPE_NULL, depth); - r_cons_break_end (); + r_cons_break_pop (); if (funsize) { RAnalFunction *f = r_anal_get_fcn_in (core->anal, off, -1); r_anal_fcn_set_size (f, funsize); diff --git a/libr/debug/debug.c b/libr/debug/debug.c index 88a91a462f..4c427c4315 100644 --- a/libr/debug/debug.c +++ b/libr/debug/debug.c @@ -907,9 +907,8 @@ R_API int r_debug_continue_kill(RDebug *dbg, int sig) { if (!dbg) { return false; } - #if __WINDOWS__ - r_cons_break (w32_break_process, dbg); + r_cons_break_push (w32_break_process, dbg); #endif repeat: if (r_debug_is_dead (dbg)) { @@ -917,12 +916,14 @@ repeat: } if (dbg->h && dbg->h->cont) { /* handle the stage-2 of breakpoints */ - if (!r_debug_recoil (dbg, R_DBG_RECOIL_CONTINUE)) + if (!r_debug_recoil (dbg, R_DBG_RECOIL_CONTINUE)) { +#if __WINDOWS__ + r_cons_break_pop (); +#endif return false; - + } /* tell the inferior to go! */ ret = dbg->h->cont (dbg, dbg->pid, dbg->tid, sig); - //XXX(jjd): why? //dbg->reason.signum = 0; reason = r_debug_wait (dbg, &bp); @@ -954,6 +955,9 @@ repeat: /* if continuing killed the inferior, we won't be able to get * the registers.. */ if (reason == R_DEBUG_REASON_DEAD || r_debug_is_dead (dbg)) { +#if __WINDOWS__ + r_cons_break_pop (); +#endif return false; } @@ -997,7 +1001,11 @@ repeat: } } } +#if __WINDOWS__ + r_cons_break_pop (); +#endif return ret; + } R_API int r_debug_continue(RDebug *dbg) { diff --git a/libr/debug/esil.c b/libr/debug/esil.c index 31f0cbcc38..a288422f6b 100644 --- a/libr/debug/esil.c +++ b/libr/debug/esil.c @@ -268,15 +268,16 @@ R_API int r_debug_esil_stepi (RDebug *d) { R_API ut64 r_debug_esil_step(RDebug *dbg, ut32 count) { count++; has_match = 0; - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); do { - if (r_cons_is_breaked ()) + if (r_cons_is_breaked ()) { break; + } if (has_match) { eprintf ("EsilBreak match at 0x%08"PFMT64x"\n", opc); break; } - if (count>0) { + if (count > 0) { count--; if (!count) { //eprintf ("Limit reached\n"); @@ -284,7 +285,7 @@ R_API ut64 r_debug_esil_step(RDebug *dbg, ut32 count) { } } } while (r_debug_esil_stepi (dbg)); - r_cons_break_end (); + r_cons_break_pop (); return opc; } diff --git a/libr/debug/p/debug_bochs.c b/libr/debug/p/debug_bochs.c index ceea96818d..4476dfab1e 100644 --- a/libr/debug/p/debug_bochs.c +++ b/libr/debug/p/debug_bochs.c @@ -12,7 +12,7 @@ static char *saveRegs; static ut64 ripStop = 0LL; typedef struct { - libbochs_t desc; + libbochs_t desc; } RIOBochs; static libbochs_t *desc = NULL; @@ -25,8 +25,9 @@ static int r_debug_bochs_breakpoint (RBreakpointItem *bp, int set, void *user) { ut64 a; int n,i,lenRec; //eprintf ("bochs_breakpoint\n"); - if (!bp) + if (!bp) { return false; + } if (set) { //eprintf("[set] bochs_breakpoint %016"PFMT64x"\n",bp->addr); sprintf (cmd, "lb 0x%x", (ut32)bp->addr); @@ -55,8 +56,9 @@ static int r_debug_bochs_breakpoint (RBreakpointItem *bp, int set, void *user) { n = r_num_get (NULL,num); a = r_num_get (NULL,addr); //eprintf("parseado %x %016"PFMT64x"\n",n,a); - if (a == bp->addr) - break; + if (a == bp->addr) { + break; + } } i += 48; } while (desc->data[i] != '<' && idata, "Next at"))) { if ((ini = strstr (x, "[0x"))) { @@ -332,7 +335,7 @@ static int r_debug_bochs_detach(RDebug *dbg, int pid) { static const char *r_debug_bochs_reg_profile(RDebug *dbg) { int bits = dbg->anal->bits; - + if (bits == 16 || bits == 32 || bits == 64) { return strdup ( "=PC csip\n" @@ -349,7 +352,7 @@ static const char *r_debug_bochs_reg_profile(RDebug *dbg) { "seg ds 2 0x03E 0 \n" "seg fs 2 0x040 0 \n" "seg gs 2 0x042 0 \n" - + "gpr rax 8 0x078 0 \n" "gpr eax 4 0x078 0 \n" "gpr ax 2 0x078 0 \n" diff --git a/libr/debug/p/debug_native.c b/libr/debug/p/debug_native.c index ee025340cf..359cfdcb54 100755 --- a/libr/debug/p/debug_native.c +++ b/libr/debug/p/debug_native.c @@ -221,12 +221,12 @@ static int r_debug_native_continue_syscall (RDebug *dbg, int pid, int num) { /* Callback to trigger SIGINT signal */ static void r_debug_native_stop(RDebug *dbg) { r_debug_kill (dbg, dbg->pid, dbg->tid, SIGINT); - r_cons_break_end(); + r_cons_break_pop (); } /* TODO: specify thread? */ /* TODO: must return true/false */ -static int r_debug_native_continue (RDebug *dbg, int pid, int tid, int sig) { +static int r_debug_native_continue(RDebug *dbg, int pid, int tid, int sig) { #if __WINDOWS__ && !__CYGWIN__ if (ContinueDebugEvent (pid, tid, DBG_CONTINUE) == 0) { print_lasterr ((char *)__FUNCTION__, "ContinueDebugEvent"); @@ -237,14 +237,15 @@ static int r_debug_native_continue (RDebug *dbg, int pid, int tid, int sig) { #elif __APPLE__ bool ret; ret = xnu_continue (dbg, pid, tid, sig); - if (!ret) + if (!ret) { return -1; + } return tid; #elif __BSD__ void *data = (void*)(size_t)((sig != -1) ? sig : dbg->reason.signum); ut64 pc = r_debug_reg_get (dbg, "PC"); return ptrace (PTRACE_CONT, pid, (void*)(size_t)pc, (int)(size_t)data) == 0; -#elif __CYGWIN__ +#elif __CYGWIN__ #warning "r_debug_native_continue not supported on this platform" return -1; #else @@ -256,7 +257,7 @@ static int r_debug_native_continue (RDebug *dbg, int pid, int tid, int sig) { //eprintf ("continuing with signal %d ...\n", contsig); /* SIGINT handler for attached processes: dbg.consbreak (disabled by default) */ if (dbg->consbreak) { - r_cons_break ((RConsBreak)r_debug_native_stop, dbg); + r_cons_break_push ((RConsBreak)r_debug_native_stop, dbg); } return ptrace (PTRACE_CONT, pid, NULL, contsig) == 0; #endif diff --git a/libr/debug/p/debug_wind.c b/libr/debug/p/debug_wind.c index cb9976311c..0c97ce77c1 100644 --- a/libr/debug/p/debug_wind.c +++ b/libr/debug/p/debug_wind.c @@ -73,8 +73,8 @@ static int r_debug_wind_wait (RDebug *dbg, int pid) { kd_packet_t *pkt; kd_stc_64 *stc; int ret; - r_cons_break (wstatic_debug_break, dbg); dbreak = 0; + r_cons_break_push (wstatic_debug_break, dbg); for (;;) { ret = wind_wait_packet (wctx, KD_PACKET_TYPE_STATE_CHANGE, &pkt); if (dbreak) { @@ -82,8 +82,9 @@ static int r_debug_wind_wait (RDebug *dbg, int pid) { wind_break (wctx); continue; } - if (ret != KD_E_OK || !pkt) + if (ret != KD_E_OK || !pkt) { break; + } stc = (kd_stc_64 *)pkt->data; // Handle exceptions only if (stc->state == STATE_EXCEPTION) { @@ -94,9 +95,12 @@ static int r_debug_wind_wait (RDebug *dbg, int pid) { dbg->reason.signum = stc->state; free (pkt); break; - } else wind_continue (wctx); + } else { + wind_continue (wctx); + } free (pkt); } + r_cons_break_pop (); // TODO : Set the faulty process as target return true; diff --git a/libr/flag/flag.c b/libr/flag/flag.c index 2485d2f01e..d95bbf49c4 100644 --- a/libr/flag/flag.c +++ b/libr/flag/flag.c @@ -77,7 +77,7 @@ static int set_name(RFlagItem *item, const char *name) { r_name_filter (item->name, 0); // TODO: name_filter should be chopping already item->namehash = r_str_hash64 (item->name); free (item->realname); - item->realname = item->name; + item->realname = strdup (item->name); return true; } @@ -413,7 +413,9 @@ R_API RFlagItem *r_flag_set(RFlag *f, const char *name, ut64 off, ut32 size) { RList *list; /* contract fail */ - if (!name || !*name) return NULL; + if (!name || !*name) { + return NULL; + } item = r_flag_get (f, name); if (item) { diff --git a/libr/include/r_cons.h b/libr/include/r_cons.h index 5125a2f8df..f329a8cf88 100644 --- a/libr/include/r_cons.h +++ b/libr/include/r_cons.h @@ -209,6 +209,7 @@ r_cons_click_clear(); typedef struct r_cons_t { RConsGrep grep; RStack *cons_stack; + RStack *break_stack; char *buffer; //int line; int buffer_len; @@ -423,7 +424,6 @@ R_API RCons *r_cons_free (void); R_API char *r_cons_lastline (void); typedef void (*RConsBreak)(void *); -R_API void r_cons_break(RConsBreak cb, void *user); R_API void r_cons_break_end(void); R_API bool r_cons_is_breaked(); @@ -437,6 +437,9 @@ R_API int r_cons_w32_print(const ut8 *ptr, int len, int empty); R_API void r_cons_push(); R_API void r_cons_pop(); +R_API void r_cons_break_pop(); +R_API void r_cons_break_push(RConsBreak cb, void*user); +R_API void r_cons_break_clear(); /* control */ R_API char *r_cons_editor (const char *file, const char *str); diff --git a/libr/include/r_util/r_stack.h b/libr/include/r_util/r_stack.h index c41a0d726a..4f64ffab7a 100644 --- a/libr/include/r_util/r_stack.h +++ b/libr/include/r_util/r_stack.h @@ -12,9 +12,9 @@ typedef struct r_stack_t { R_API RStack *r_stack_new(ut32 n); R_API void r_stack_free(RStack *s); +R_API bool r_stack_is_empty(RStack *s); R_API RStack *r_stack_newf(ut32 n, RStackFree f); R_API int r_stack_push(RStack *s, void *el); R_API void *r_stack_pop(RStack *s); -R_API int r_stack_is_empty(RStack *s); R_API unsigned int r_stack_size(RStack *s); #endif // R_STACK_H diff --git a/libr/lang/p/pipe.c b/libr/lang/p/pipe.c index 455fe373d6..355a78a5da 100644 --- a/libr/lang/p/pipe.c +++ b/libr/lang/p/pipe.c @@ -38,14 +38,14 @@ DWORD WINAPI ThreadFunction(LPVOID lpParam) { BOOL bSuccess = FALSE; int i, res = 0; DWORD dwRead, dwWritten; - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); res = ConnectNamedPipe (hPipeInOut, NULL); if (!res) { eprintf ("ConnectNamedPipe failed\n"); return FALSE; } do { - if (r_cons_singleton ()->breaked) { + if (r_cons_is_breaked ()) { TerminateProcess(hproc,0); break; } @@ -85,7 +85,7 @@ DWORD WINAPI ThreadFunction(LPVOID lpParam) { } } } while(!bStopThread); - r_cons_break_end (); + r_cons_break_pop (); return TRUE; } #else @@ -113,7 +113,7 @@ static int lang_pipe_run(RLang *lang, const char *code, int len) { child = r_sys_fork (); if (child == -1) { /* error */ - } else if (child == 0) { + } else if (!child) { /* children */ r_sandbox_system (code, 1); write (input[1], "", 1); @@ -126,22 +126,20 @@ static int lang_pipe_run(RLang *lang, const char *code, int len) { } else { /* parent */ char *res, buf[1024]; - /* Close pipe ends not required in the parent */ close (output[1]); close (input[0]); - - r_cons_break (NULL, NULL); + r_cons_break_push (NULL, NULL); for (;;) { - if (r_cons_singleton ()->breaked) { + if (r_cons_is_breaked ()) { break; } memset (buf, 0, sizeof (buf)); ret = read (output[0], buf, sizeof (buf)-1); - if (ret <1 || !buf[0]) { + if (ret < 1 || !buf[0]) { break; } - buf[sizeof (buf)-1] = 0; + buf[sizeof (buf) - 1] = 0; res = lang->cmd_str ((RCore*)lang->user, buf); //eprintf ("%d %s\n", ret, buf); if (res) { @@ -152,22 +150,26 @@ static int lang_pipe_run(RLang *lang, const char *code, int len) { write (input[1], "", 1); // NULL byte } } + r_cons_break_pop (); /* workaround to avoid stdin closed */ - if (safe_in != -1) + if (safe_in != -1) { close (safe_in); + } safe_in = open (ttyname(0), O_RDONLY); if (safe_in != -1) { dup2 (safe_in, 0); - } else eprintf ("Cannot open ttyname(0) %s\n", ttyname(0)); - r_cons_break_end (); + } else { + eprintf ("Cannot open ttyname(0) %s\n", ttyname(0)); + } } close (input[0]); close (input[1]); close (output[0]); close (output[1]); - if (safe_in != -1) + if (safe_in != -1) { close (safe_in); + } waitpid (child, NULL, 0); return true; #else diff --git a/libr/util/print.c b/libr/util/print.c index 3a302839e1..efd4219a8e 100644 --- a/libr/util/print.c +++ b/libr/util/print.c @@ -785,35 +785,38 @@ R_API void r_print_hexdump(RPrint *p, ut64 addr, const ut8 *buf, int len, int ba } if (use_sparse) { if (check_sparse (buf+i, inc, sparse_char)) { - if (i+inc>=len || check_sparse (buf+i+inc, inc, sparse_char)) { + if (i + inc >= len || check_sparse (buf+i+inc, inc, sparse_char)) { if (i+inc+inc>=len || check_sparse (buf+i+inc+inc, inc, sparse_char)) { sparse_char = buf[j]; last_sparse++; - if (last_sparse==2) { + if (last_sparse == 2) { printfmt (" ...\n"); continue; } - if (last_sparse>2) continue; + if (last_sparse > 2) continue; } } - } else last_sparse = 0; + } else { + last_sparse = 0; + } } - if (use_offset) - r_print_addr (p, addr+j); - printfmt ((col==1)? "|": " "); + if (use_offset) { + r_print_addr (p, addr + j); + } + printfmt ((col == 1)? "|": " "); for (j = i; j < i + inc; j++) { if (j >= len) { if (col == 1) { - if (j+1 >= inc + i) { - printfmt (j%2?" |":"| "); + if (j + 1 >= inc + i) { + printfmt (j % 2?" |":"| "); } else { - printfmt (j%2?" ":" "); + printfmt (j % 2?" ":" "); } } else { if (base == 10) { - printfmt (j%2?" ":" "); + printfmt (j % 2?" ":" "); } else { - printfmt (j%2?" ":" "); + printfmt (j % 2?" ":" "); } } continue; @@ -869,8 +872,8 @@ R_API void r_print_hexdump(RPrint *p, ut64 addr, const ut8 *buf, int len, int ba break; } r_print_byte (p, fmt, j, buf[j]); - if (j%2 || !pairs) { - if (col==1) { + if (j % 2 || !pairs) { + if (col == 1) { if (j + 1 < inc + i) { printfmt (" "); } else { @@ -889,7 +892,7 @@ R_API void r_print_hexdump(RPrint *p, ut64 addr, const ut8 *buf, int len, int ba } if (col == 2) printfmt("|"); if (p && p->flags & R_PRINT_FLAGS_REFS) { - ut64 *foo = (ut64*)(buf+i); + ut64 *foo = (ut64*)(buf + i); ut64 addr = *foo; if (base == 32) { addr &= UT32_MAX; diff --git a/libr/util/stack.c b/libr/util/stack.c index 35e6763921..a39bc89cd5 100644 --- a/libr/util/stack.c +++ b/libr/util/stack.c @@ -52,6 +52,7 @@ R_API int r_stack_push(RStack *s, void *el) { return true; } + //the caller should be take care of the object returned R_API void *r_stack_pop(RStack *s) { void *res; @@ -63,7 +64,7 @@ R_API void *r_stack_pop(RStack *s) { return res; } -R_API int r_stack_is_empty(RStack *s) { +R_API bool r_stack_is_empty(RStack *s) { return s->top == -1; } diff --git a/libr/util/str.c b/libr/util/str.c index 835536c492..0e38d21137 100644 --- a/libr/util/str.c +++ b/libr/util/str.c @@ -670,7 +670,7 @@ R_API char *r_str_newlen(const char *str, int len) { // specification. R_API char *r_str_newf(const char *fmt, ...) { int ret, ret2; - char *p, string[1024]; + char *tmp, *p, string[1024]; va_list ap, ap2; va_start (ap, fmt); va_start (ap2, fmt); @@ -681,7 +681,7 @@ R_API char *r_str_newf(const char *fmt, ...) { } ret = vsnprintf (string, sizeof (string) - 1, fmt, ap); if (ret < 1 || ret >= sizeof (string)) { - p = malloc (ret + 2); + p = calloc (1, ret + 3); if (!p) { va_end (ap2); va_end (ap); @@ -694,14 +694,14 @@ R_API char *r_str_newf(const char *fmt, ...) { va_end (ap); return NULL; } - fmt = r_str_new (p); + tmp = r_str_new (p); free (p); } else { - fmt = r_str_new (string); + tmp = r_str_new (string); } va_end (ap2); va_end (ap); - return (char*)fmt; + return tmp; } // TODO: rename to r_str_trim_inplace() or something like that