Skip to content

Commit

Permalink
Support internal files in the tac command ##shell
Browse files Browse the repository at this point in the history
  • Loading branch information
radare authored and trufae committed Mar 13, 2024
1 parent 98de4be commit 7c6c8a4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
11 changes: 3 additions & 8 deletions libr/core/cmd_cmp.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1149,16 +1149,11 @@ static int cmd_cmp(void *data, const char *input) {
case 'a': // "ca"
if (input[1] == 't') { // "cat"
const char *path = r_str_trim_head_ro (input + 2);
if (*path == '$' && !path[1]) {
R_LOG_ERROR ("No alias name given");
} else if (*path == '$') {
RCmdAliasVal *v = r_cmd_alias_get (core->rcmd, path + 1);
if (v) {
char *v_str = r_cmd_alias_val_strdup (v);
if (*path == '$') {
char *v_str = r_core_slurp (core, path, NULL);
if (v_str) {
r_cons_println (v_str);
free (v_str);
} else {
R_LOG_ERROR ("No such alias \"$%s\"", path + 1);
}
} else if (*path) {
if (r_fs_check (core->fs, path)) {
Expand Down
40 changes: 32 additions & 8 deletions libr/core/cmd_type.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,26 @@ static void showFormat(RCore *core, const char *name, int mode) {
}
}

R_API char *r_core_slurp(RCore *core, const char *path, size_t *len) {
if (*path == '$') {
if (!path[1]) {
R_LOG_ERROR ("No alias name given");
return NULL;
}
RCmdAliasVal *v = r_cmd_alias_get (core->rcmd, path + 1);
if (!v) {
R_LOG_ERROR ("No such alias \"$%s\"", path + 1);
return NULL;
}
char *r = r_cmd_alias_val_strdup (v);
if (len) {
*len = strlen (r);
}
return r;
}
return r_file_slurp (path, len);
}

static int cmd_tac(void *data, const char *_input) { // "tac"
RCore *core = (RCore *) data;
char *input = strdup (_input);
Expand All @@ -332,15 +352,19 @@ static int cmd_tac(void *data, const char *_input) { // "tac"
break;
default: // "tac"
if (R_STR_ISNOTEMPTY (arg)) {
char *filedata = r_file_slurp (arg, NULL);
RList *lines = r_str_split_list (filedata, "\n", 0);
RListIter *iter;
char *line;
r_list_foreach_prev (lines, iter, line) {
r_cons_printf ("%s\n", line);
char *filedata = r_core_slurp (core, arg, NULL);
if (filedata) {
RList *lines = r_str_split_list (filedata, "\n", 0);
RListIter *iter;
char *line;
r_list_foreach_prev (lines, iter, line) {
r_cons_printf ("%s\n", line);
}
r_list_free (lines);
free (filedata);
} else {
R_LOG_ERROR ("File not found");
}
r_list_free (lines);
free (filedata);
} else {
r_core_cmd_help_match (core, help_msg_t, "tac");
}
Expand Down
2 changes: 2 additions & 0 deletions libr/include/r_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ R_API RIODesc *r_core_file_open(RCore *core, const char *file, int flags, ut64 l
R_API RIODesc *r_core_file_open_many(RCore *r, const char *file, int flags, ut64 loadaddr);
R_API bool r_core_file_close_all_but(RCore *core);

R_API char *r_core_slurp(RCore *core, const char *path, size_t *len);

R_API int r_core_setup_debugger(RCore *r, const char *debugbackend, bool attach);
R_API int r_core_seek_delta(RCore *core, st64 addr);
R_API bool r_core_extend_at(RCore *core, ut64 addr, int size);
Expand Down

0 comments on commit 7c6c8a4

Please sign in to comment.