Fix #19830 - implement pushd/popd commands ##shell

This commit is contained in:
pancake 2022-05-03 11:31:36 +02:00
parent 34061b85e2
commit 86e7fd1a14
7 changed files with 111 additions and 7 deletions

View File

@ -66,12 +66,13 @@ static int analop(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len, RAn
op->size = print_insn_s390 ((bfd_vma)Offset, &disasm_obj);
if (op->size < 1) {
op->mnemonic = strdup ("invalid");
op->type = R_ANAL_OP_TYPE_ILL;
op->size = 2;
} else {
op->mnemonic = r_strbuf_drain (buf_global);
buf_global = NULL;
}
r_buf_free (buf_global);
r_strbuf_free (buf_global);
buf_global = NULL;
return op->size;
}

View File

@ -5441,6 +5441,42 @@ static int cmd_print(void *data, const char *input) {
/* hijack here for now, idk how to more cleanly integrate it */
return cmd_pdu (core, input + 2);
}
if (!strncmp (input, "ushd", 4)) { // "pushd"
bool halp = true;
const char *arg = strchr (input, ' ');
if (arg) {
arg = r_str_trim_head_ro (arg + 1);
if (*arg) {
halp = false;
if (r_syscmd_pushd (arg)) {
r_core_return_code (core, 0);
} else {
r_core_return_code (core, 1);
}
}
}
if (halp) {
eprintf ("Usage: pushd [dir]\n");
r_core_return_code (core, 1);
}
return 0;
}
if (!strncmp (input, "opd", 3)) { // "popd"
bool all = strstr (input, "-a");
bool halp = strstr (input, "-h");
if (halp) {
eprintf ("Usage: popd [-a]\n");
r_core_return_code (core, 1);
} else {
if (all) {
r_syscmd_popalld ();
} else {
r_syscmd_popd ();
}
r_core_return_code (core, 0);
}
return 0;
}
r_print_init_rowoffsets (core->print);
off = UT64_MAX;
@ -6827,7 +6863,7 @@ static int cmd_print(void *data, const char *input) {
}
break;
case 'o': // "po"
cmd_print_op(core, input);
cmd_print_op (core, input);
break;
case 'x': // "px"
if (input[1] == '-' && input[2] == '-') {

View File

@ -139,6 +139,9 @@ R_API bool r_sys_tts(const char *txt, bool bg);
/* syscmd */
R_API char *r_syscmd_ls(const char *input, int w);
R_API char *r_syscmd_cat(const char *file);
R_API bool r_syscmd_pushd(const char *dir);
R_API bool r_syscmd_popd(void);
R_API void r_syscmd_popalld(void);
R_API bool r_syscmd_mkdir(const char *dir);
R_API char *r_syscmd_mktemp(const char *dir);
R_API bool r_syscmd_mv(const char *input);

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2021 - pancake */
/* radare - LGPL - Copyright 2009-2022 - pancake */
#include <r_core.h>
#include <r_types.h>
@ -1217,6 +1217,7 @@ R_API int r_main_rabin2(int argc, const char **argv) {
pj_free (pj);
r_cons_flush ();
r_core_fini (&core);
r_syscmd_popalld ();
return 0;
}

View File

@ -9,6 +9,8 @@
#define FMT_QUIET 'q'
#define FMT_EMOJI 'e'
R_TH_LOCAL RList *dirstack = NULL;
static char *showfile(char *res, const int nth, const char *fpath, const char *name, int printfmt, bool needs_newline) {
#if __UNIX__
struct stat sb;
@ -572,6 +574,48 @@ R_API bool r_syscmd_mkdir(const char *dir) {
return true;
}
R_API bool r_syscmd_pushd(const char *input) {
if (!dirstack) {
dirstack = r_list_newf (free);
}
char *cwd = r_sys_getdir ();
if (!cwd) {
eprintf ("Where am I?\n");
return false;
}
bool suc = r_sys_chdir (input);
if (suc) {
r_list_push (dirstack, cwd);
} else {
eprintf ("Cannot chdir\n");
}
return suc;
}
R_API bool r_syscmd_popd(void) {
if (!dirstack) {
return false;
}
char *d = r_list_pop (dirstack);
if (d) {
r_sys_chdir (d);
eprintf ("%s\n", d);
free (d);
}
if (r_list_empty (dirstack)) {
r_list_free (dirstack);
dirstack = NULL;
return false;
}
return true;
}
R_API void r_syscmd_popalld(void) {
while (r_syscmd_popd ()) {
// wait for it
}
}
R_API bool r_syscmd_mv(const char *input) {
if (strlen (input) < 3) {
eprintf ("Usage: mv src dst\n");

View File

@ -787,14 +787,12 @@ R_API ut64 r_num_tail_base(RNum *num, ut64 addr, ut64 off) {
R_API ut64 r_num_tail(RNum *num, ut64 addr, const char *hex) {
ut64 mask = 0LL;
ut64 n = 0;
char *p;
int i;
while (*hex && (*hex == ' ' || *hex == '.')) {
hex++;
}
i = strlen (hex) * 4;
p = malloc (strlen (hex) + 10);
int i = strlen (hex) * 4;
char *p = malloc (strlen (hex) + 10);
if (p) {
strcpy (p, "0x");
strcpy (p + 2, hex);

View File

@ -1,3 +1,24 @@
NAME=pushd;popd
FILE=-
CMDS=<<EOF
cd /
pushd bin
pushd ../dev
pwd
popd
popd
pwd
EOF
EXPECT=<<EOF
/dev
/
EOF
EXPECT_ERR=<<EOF
/bin
/
EOF
RUN
NAME=mkdir; ls
FILE=-
CMDS=<<EOF