From 6a04be09ed8f575676bdc593ae2866ef62416d23 Mon Sep 17 00:00:00 2001 From: Lazula <26179473+Lazula@users.noreply.github.com> Date: Thu, 23 Sep 2021 07:07:20 -0500 Subject: [PATCH] Add r_core_cmd_tobuf() --- libr/core/cmd.c | 28 ++++++++++++++++++++++++++++ libr/include/r_core.h | 1 + 2 files changed, 29 insertions(+) diff --git a/libr/core/cmd.c b/libr/core/cmd.c index a020643726..dc68a0d693 100644 --- a/libr/core/cmd.c +++ b/libr/core/cmd.c @@ -5543,6 +5543,34 @@ R_API char *r_core_cmd_str(RCore *core, const char *cmd) { return retstr; } +/* get command output in raw bytes */ +R_API RBuffer *r_core_cmd_tobuf(RCore *core, const char *cmd) { + r_cons_push (); + core->cons->context->noflush = true; + + core->cons->context->cmd_str_depth++; + if (r_core_cmd0 (core, cmd) == -1) { + //eprintf ("Invalid command: %s\n", cmd); + if (--core->cons->context->cmd_str_depth == 0) { + core->cons->context->noflush = false; + r_cons_flush (); + } + r_cons_pop (); + return NULL; + } + + if (--core->cons->context->cmd_str_depth == 0) { + core->cons->context->noflush = false; + } + + r_cons_filter (); + RBuffer *out = r_buf_new_with_bytes ((const ut8*)r_cons_get_buffer (), r_cons_get_buffer_len ()); + + r_cons_pop (); + r_cons_echo (NULL); + return out; +} + /* run cmd in the main task synchronously */ R_API int r_core_cmd_task_sync(RCore *core, const char *cmd, bool log) { RCoreTask *task = core->tasks.main_task; diff --git a/libr/include/r_core.h b/libr/include/r_core.h index 7a73408bc8..4a01c57f67 100644 --- a/libr/include/r_core.h +++ b/libr/include/r_core.h @@ -433,6 +433,7 @@ R_API int r_core_cmd_pipe(RCore *core, char *radare_cmd, char *shell_cmd); R_API char *r_core_cmd_str(RCore *core, const char *cmd); R_API char *r_core_cmd_strf(RCore *core, const char *fmt, ...) R_PRINTF_CHECK(2, 3); R_API char *r_core_cmd_str_pipe(RCore *core, const char *cmd); +R_API RBuffer *r_core_cmd_tobuf(RCore *core, const char *cmd); R_API int r_core_cmd_file(RCore *core, const char *file); R_API int r_core_cmd_lines(RCore *core, const char *lines); R_API int r_core_cmd_command(RCore *core, const char *command);