Implement py command and add stdin slurp support for js- too ##lang

This commit is contained in:
pancake 2022-12-08 21:08:30 +01:00 committed by pancake
parent 0f45f2cadd
commit 59c2811e4f
2 changed files with 60 additions and 0 deletions

View File

@ -102,6 +102,7 @@ static RCoreHelpMessage help_msg_j = {
"j:", "?e", "run '?e' command and show the result stats in json",
"ji:", "[cmd]", "run command and indent it as json like (cmd~{})",
"js", " [expr]", "run given javascript expression",
"js-", "", "read from stdin until ^D",
"js:", "[file]", "interpret javascript file",
"join", " f1 f2", "join the contents of two files",
NULL
@ -1574,6 +1575,23 @@ static int cmd_join(void *data, const char *input) { // "join"
if (input[0] == 's') {
if (input[1] == ':') {
r_core_cmdf (core, ". %s", input + 1);
} else if (input[1] == '-') {
if (r_config_get_b (core->config, "scr.interactive")) {
int sz;
char *data = r_stdin_slurp (&sz);
if (data) {
char *code = r_str_newf ("(function() { %s })()", data);
if (r_lang_use (core->lang, "mujs")) {
r_lang_run (core->lang, code, sz);
} else {
R_LOG_ERROR ("Requires mujs");
}
free (code);
free (data);
}
} else {
R_LOG_ERROR ("requires scr.interactive");
}
} else if (input[1] == ' ') {
if (r_lang_use (core->lang, "mujs")) {
r_lang_run (core->lang, input + 1, -1);

View File

@ -191,6 +191,7 @@ static const char *help_msg_p[] = {
"pv", "[?][ejh] [mode]", "show value of given size (1, 2, 4, 8)",
"pwd", "", "display current working directory",
"px", "[?][owq] [len]", "hexdump of N bytes (o=octal, w=32bit, q=64bit)",
"py", "([-:file]) [expr]", "print clipboard (yp) run python script (py:file) oneliner `py print(1)` or stdin slurp `py-`",
"pz", "[?] [len]", "print zoom view (see pz? for help)",
NULL
};
@ -7194,6 +7195,47 @@ static int cmd_print(void *data, const char *input) {
free (res);
}
break;
case 'y': // "py"
switch (input[1]) {
case '?':
r_core_cmd_help_match (core, help_msg_p, "py", false);
break;
case '-':
if (r_config_get_b (core->config, "scr.interactive")) {
int sz;
char *data = r_stdin_slurp (&sz);
if (data) {
const char *const fn = ".tmp.py";
r_file_dump (fn, (ut8*)data, sz, false);
r_core_cmd_callf (core, ". %s", fn);
r_file_rm (fn);
free (data);
}
} else {
R_LOG_ERROR ("requires interactive shell");
}
break;
case ':':
r_core_cmd_callf (core, "#!python %s", input + 2);
break;
case ' ':
{
char *data = (char *)r_str_trim_head_ro (input + 2);
int sz = strlen (data);
if (R_STR_ISNOTEMPTY (data)) {
const char *const fn = ".tmp.py";
if (r_file_dump (fn, (ut8*)data, sz, false)) {
r_core_cmd_callf (core, ". %s", fn);
}
r_file_rm (fn);
}
}
break;
case 0:
r_core_cmd_call (core, "yp");
break;
}
break;
case 'o': // "po"
cmd_print_op (core, input);
break;