diff --git a/libr/core/cmd.c b/libr/core/cmd.c index 5d75ecd4c0..ac4515d1d0 100644 --- a/libr/core/cmd.c +++ b/libr/core/cmd.c @@ -1325,7 +1325,7 @@ R_API int r_core_cmd(RCore *core, const char *cstr, int log) { } R_API int r_core_cmd_lines(RCore *core, const char *lines) { - int ret = R_TRUE; + int r, ret = R_TRUE; char *nl, *data, *odata; if (!lines || !*lines) return R_TRUE; @@ -1334,8 +1334,8 @@ R_API int r_core_cmd_lines(RCore *core, const char *lines) { if (nl) { do { *nl = '\0'; - ret = r_core_cmd (core, data, 0); - if (ret == -1) { + r = r_core_cmd (core, data, 0); + if (r == -1) { ret = R_FALSE; break; } @@ -1349,9 +1349,8 @@ R_API int r_core_cmd_lines(RCore *core, const char *lines) { data = nl+1; } while ((nl = strchr (data, '\n'))); } - if (data && *data) { + if (data && *data) r_core_cmd (core, data, 0); - } free (odata); return ret; } @@ -1360,11 +1359,10 @@ R_API int r_core_cmd_file(RCore *core, const char *file) { int ret = R_TRUE; char *nl, *data, *odata; data = r_file_abspath (file); + if (!data) return R_FALSE; odata = r_file_slurp (data, NULL); free (data); - if (!odata) { - return -1; - } + if (!odata) return R_FALSE; if (!r_core_cmd_lines (core, odata)) { eprintf ("Failed to run script '%s'\n", file); return R_FALSE; diff --git a/libr/core/cmd_write.c b/libr/core/cmd_write.c index babc74d434..b02d0b1c72 100644 --- a/libr/core/cmd_write.c +++ b/libr/core/cmd_write.c @@ -14,10 +14,20 @@ static int cmd_write(void *data, const char *input) { switch (*input) { case 'p': + if (input[1]=='-' || (input[1]==' '&&input[2]=='-')) { + const char *tmpfile = ".tmp"; + char *out = r_core_editor (core, NULL); + if (out) { + // XXX hacky .. patch should support str, not only file + r_file_dump (tmpfile, out, strlen (out)); + r_core_patch (core, tmpfile); + r_file_rm (tmpfile); + } + } else if (input[1]==' ' && input[2]) { r_core_patch (core, input+2); } else { - eprintf ("Usage: wp [rapatch-file]\n" + eprintf ("Usage: wp [-|r2patch-file]\n" "TODO: rapatch format documentation here\n"); } break; @@ -152,6 +162,13 @@ static int cmd_write(void *data, const char *input) { break; case 'f': arg = (const char *)(input+((input[1]==' ')?2:1)); + if (!strcmp (arg, "-")) { + char *out = r_core_editor (core, NULL); + if (out) { + r_io_write_at (core->io, core->offset, out, strlen (out)); + free (out); + } + } else if ((buf = (ut8*) r_file_slurp (arg, &size))) { r_io_set_fd (core->io, core->file->fd); r_io_write_at (core->io, core->offset, buf, size); @@ -162,6 +179,20 @@ static int cmd_write(void *data, const char *input) { break; case 'F': arg = (const char *)(input+((input[1]==' ')?2:1)); + if (!strcmp (arg, "-")) { + int len; + char *out, *in = r_core_editor (core, NULL); + if (in) { + out = strdup (in); + if (out) { + len = r_hex_str2bin (in, out); + if (len>0) + r_io_write_at (core->io, core->offset, out, len); + free (out); + } + free (in); + } + } else if ((buf = r_file_slurp_hexpairs (arg, &size))) { r_io_set_fd (core->io, core->file->fd); r_io_write_at (core->io, core->offset, buf, size); @@ -453,10 +484,11 @@ static int cmd_write(void *data, const char *input) { " wo? hex write in block with operation. 'wo?' fmi\n" " wm f0ff set binary mask hexpair to be used as cyclic write mask\n" " ws pstring write 1 byte for length and then the string\n" - " wf file write contents of file at current offset\n" - " wF file write contents of hexpairs file here\n" + " wf -|file write contents of file at current offset\n" + " wF -|file write contents of hexpairs file here\n" + " wp -|file apply radare patch file. See wp? fmi\n" " wt file [sz] write to file (from current seek, blocksize or sz bytes)\n" - " wp file apply radare patch file. See wp? fmi\n"); + ); //TODO: add support for offset+seek // " wf file o s ; write contents of file from optional offset 'o' and size 's'.\n" break; diff --git a/libr/core/patch.c b/libr/core/patch.c index 34d80a0641..c8adca646d 100644 --- a/libr/core/patch.c +++ b/libr/core/patch.c @@ -30,6 +30,10 @@ R_API int r_core_patch (RCore *core, const char *patch) { fgets (str, sizeof (str), fd); if (*str=='#' || *str=='\n' || *str=='\r') continue; + if (*str==':') { + r_core_cmd0 (core, str+1); + continue; + } if (*str=='.' || *str=='!') { r_core_cmd0 (core, str); continue; @@ -37,7 +41,7 @@ R_API int r_core_patch (RCore *core, const char *patch) { p = strchr (str+1, ' '); if (p) { *p = 0; - for (++p;*p==' ';p++); // XXX: skipsspaces here + for (++p; *p==' '; p++); // XXX: skipsspaces here switch (*p) { case '{': { char *s, *off = strdup (str); @@ -82,7 +86,7 @@ R_API int r_core_patch (RCore *core, const char *patch) { p2 = strchr (p+1,'"'); if (p2) *p2=0; r_core_cmdf (core, "s %s", str); - r_core_cmdf (core, "\" %s\"", p+1); + r_core_cmdf (core, "\"w %s\"", p+1); break; case ':': r_core_cmdf (core, "s %s", str); diff --git a/libr/util/hex.c b/libr/util/hex.c index 14994b20c0..cddc21685a 100644 --- a/libr/util/hex.c +++ b/libr/util/hex.c @@ -64,6 +64,25 @@ R_API int r_hex_str2bin(const char *in, ut8 *out) { if (!memcmp (in, "0x", 2)) in += 2; for (ptr = in; ; ptr++) { + /* comments */ + if (*ptr=='#') { + while (*ptr && *ptr != '\n') ptr++; + if (!ptr[0]) + break; + ptr--; + continue; + } + if (*ptr == '/' && ptr[1]=='*') { + while (*ptr && ptr[1]) { + if (*ptr == '*' && ptr[1]=='/') + break; + ptr++; + } + if (!ptr[0] || !ptr[1]) + break; + ptr++; + continue; + } /* ignored chars */ if (*ptr==':' || *ptr=='\n' || *ptr=='\t' || *ptr=='\r' || *ptr==' ') continue;