Implement RCons.echo() and use it from r2.cmd("echo64") ##cons (#14635)

Required for r2pipe programs to buffer output to be processed by r2
This commit is contained in:
radare 2019-07-24 13:59:57 +02:00 committed by GitHub
parent bf53723c4c
commit 672fe95049
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 54 additions and 9 deletions

View File

@ -838,6 +838,26 @@ static bool lastMatters() {
!I.context->grep.json && !I.is_html);
}
R_API void r_cons_echo(const char *msg) {
static RStrBuf *echodata = NULL; // TODO: move into RConsInstance? maybe nope
if (msg) {
if (echodata) {
r_strbuf_append (echodata, msg);
r_strbuf_append (echodata, "\n");
} else {
echodata = r_strbuf_new (msg);
}
} else {
if (echodata) {
char *data = r_strbuf_drain (echodata);
r_cons_strcat (data);
r_cons_newline ();
echodata = NULL;
free (data);
}
}
}
R_API void r_cons_flush(void) {
const char *tee = I.teefile;
if (I.noflush) {

View File

@ -1949,6 +1949,10 @@ static int cmd_system(void *data, const char *input) {
cmd_autocomplete (core, input + 2);
} else if (input[1] == '?') {
cmd_help_exclamation (core);
} else if (input[1] == '*') {
char *cmd = r_str_trim_dup (input + 1);
(void)r_core_cmdf (core, "\"#!pipe %s\"", cmd);
free (cmd);
} else {
if (r_sandbox_enable (0)) {
eprintf ("This command is disabled in sandbox mode\n");
@ -4509,6 +4513,7 @@ R_API char *r_core_cmd_str(RCore *core, const char *cmd) {
static_str = r_cons_get_buffer ();
retstr = strdup (static_str? static_str: "");
r_cons_pop ();
r_cons_echo (NULL);
return retstr;
}

View File

@ -288,6 +288,23 @@ done:
}
}
R_API void r_core_echo(RCore *core, const char *input) {
if (!strncmp (input, "64 ", 3)) {
char *buf = strdup (input);
r_base64_decode ((ut8*)buf, input + 3, -1);
if (*buf) {
r_cons_echo (buf);
}
free (buf);
} else {
char *p = strchr (input, ' ');
if (p) {
r_cons_strcat (p + 1);
r_cons_newline ();
}
}
}
static int cmd_eval(void *data, const char *input) {
RCore *core = (RCore *)data;
switch (input[0]) {
@ -399,11 +416,7 @@ static int cmd_eval(void *data, const char *input) {
case '*': r_cons_pal_list (1, NULL); break; // "ec*"
case 'h': // echo
if (input[2] == 'o') {
char *p = strchr (input, ' ');
if (p) {
r_cons_strcat (p + 1);
r_cons_newline ();
}
r_core_echo (core, input + 3);
} else {
r_cons_pal_list ('h', NULL);
}

View File

@ -382,6 +382,7 @@ R_API void r_core_clippy(const char *msg) {
free (s);
}
static int cmd_help(void *data, const char *input) {
RCore *core = (RCore *)data;
RIOMap *map;

View File

@ -19,7 +19,6 @@ static ut64 letter_divs[R_CORE_ASMQJMPS_LEN_LETTERS - 1] = {
R_CORE_ASMQJMPS_LETTERS
};
extern int r_is_heap (void *p);
extern bool r_core_is_project (RCore *core, const char *name);
static int on_fcn_new(RAnal *_anal, void* _user, RAnalFunction *fcn) {
@ -3069,6 +3068,8 @@ R_API int r_core_prompt(RCore *r, int sync) {
return true;
}
extern void r_core_echo(RCore *core, const char *input);
R_API int r_core_prompt_exec(RCore *r) {
int ret = r_core_cmd (r, r->cmdqueue, true);
//int ret = r_core_cmd (r, r->cmdqueue, true);
@ -3077,6 +3078,7 @@ R_API int r_core_prompt_exec(RCore *r) {
r_sys_tts (buf, true);
r->cons->use_tts = false;
}
r_cons_echo (NULL);
r_cons_flush ();
if (r->cons && r->cons->line && r->cons->line->zerosep) {
r_cons_zero ();

View File

@ -1077,13 +1077,13 @@ char *__apply_filter_cmd(RCore *core, RPanel *panel) {
}
char *__handleCmdStrCache(RCore *core, RPanel *panel, bool force_cache) {
char *out;
char *cmd = __apply_filter_cmd (core, panel);
bool b = core->print->cur_enabled && __getCurPanel (core->panels) != panel;
if (b) {
core->print->cur_enabled = false;
}
out = r_core_cmd_str (core, cmd);
char *out = r_core_cmd_str (core, cmd);
r_cons_echo (NULL);
if (force_cache) {
panel->model->cache = true;
}

View File

@ -704,6 +704,7 @@ R_API int r_core_visual_prompt(RCore *core) {
} else if (*buf) {
r_line_hist_add (buf);
r_core_cmd (core, buf, 0);
r_cons_echo (NULL);
r_cons_flush ();
ret = true;
} else {

View File

@ -819,6 +819,7 @@ R_API char *r_cons_editor(const char *file, const char *str);
R_API void r_cons_reset(void);
R_API void r_cons_reset_colors(void);
R_API void r_cons_print_clear(void);
R_API void r_cons_echo(const char *msg);
R_API void r_cons_zero(void);
R_API void r_cons_highlight(const char *word);
R_API void r_cons_clear(void);

View File

@ -888,6 +888,7 @@ typedef struct r_core_task_t {
typedef void (*RCoreTaskOneShot)(void *);
R_API void r_core_echo(RCore *core, const char *msg);
R_API RCoreTask *r_core_task_get(RCore *core, int id);
R_API RCoreTask *r_core_task_get_incref(RCore *core, int id);
R_API void r_core_task_print(RCore *core, RCoreTask *task, int mode);

View File

@ -37,6 +37,7 @@ R_API const char *r_time_to_string (ut64 ts);
R_API int r_sys_fork(void);
// nocleanup = false => exit(); true => _exit()
R_API void r_sys_exit(int status, bool nocleanup);
R_API bool r_is_heap (void *p);
R_API bool r_sys_stop(void);
R_API char *r_sys_pid_to_path(int pid);
R_API int r_sys_run(const ut8 *buf, int len);

View File

@ -947,7 +947,7 @@ R_API int r_sys_run(const ut8 *buf, int len) {
return ret;
}
R_API int r_is_heap (void *p) {
R_API bool r_is_heap (void *p) {
void *q = malloc (8);
ut64 mask = UT64_MAX;
ut64 addr = (ut64)(size_t)q;