From 32d3159ceb55ead598e6ac3b3ed70ddd2d0cf0fb Mon Sep 17 00:00:00 2001 From: Riccardo Schirone Date: Thu, 24 Aug 2023 10:03:03 +0200 Subject: [PATCH] shell: make ?x commands a parsing failure We have moved the `?x` commands to `%x`, thus now no command should start with `?`. This patch makes the parser fail to parse `?x` strings. --- librz/core/cmd/cmd.c | 6 ++-- .../corpus/cmd_substitution.txt | 10 +++--- .../rizin-shell-parser/corpus/comments.txt | 6 ++-- .../rizin-shell-parser/corpus/escape_args.txt | 4 +-- .../corpus/grep_commands.txt | 6 ++-- .../corpus/iter_commands.txt | 4 +-- .../rizin-shell-parser/corpus/quoted_args.txt | 14 ++++---- .../corpus/simple_commands.txt | 36 +++++++++++++------ .../corpus/special_commands.txt | 2 +- subprojects/rizin-shell-parser/src/scanner.c | 11 +++--- 10 files changed, 57 insertions(+), 42 deletions(-) diff --git a/librz/core/cmd/cmd.c b/librz/core/cmd/cmd.c index d52b1b05a0d..4ab4a5f00f3 100644 --- a/librz/core/cmd/cmd.c +++ b/librz/core/cmd/cmd.c @@ -3972,11 +3972,13 @@ DEFINE_HANDLE_TS_FCN_AND_SYMBOL(help_stmt) { if (node_str_len >= 2 && !strcmp(node_string + node_str_len - 2, "?*")) { node_string[node_str_len - 2] = 0; const char *argv[2] = { NULL, node_string }; - return rz_cmd_help_search_handler(state->core, 2, argv, RZ_OUTPUT_MODE_STANDARD); + int argc = node_str_len > 2 ? 2 : 1; + return rz_cmd_help_search_handler(state->core, argc, argv, RZ_OUTPUT_MODE_STANDARD); } else if (node_str_len >= 3 && !strcmp(node_string + node_str_len - 3, "?*j")) { node_string[node_str_len - 3] = 0; const char *argv[2] = { NULL, node_string }; - return rz_cmd_help_search_handler(state->core, 2, argv, RZ_OUTPUT_MODE_JSON); + int argc = node_str_len > 2 ? 2 : 1; + return rz_cmd_help_search_handler(state->core, argc, argv, RZ_OUTPUT_MODE_JSON); } TSNode command = ts_node_child_by_field_name(node, "command", strlen("command")); diff --git a/subprojects/rizin-shell-parser/corpus/cmd_substitution.txt b/subprojects/rizin-shell-parser/corpus/cmd_substitution.txt index 6dd16a2bcfb..5f510fb20b6 100644 --- a/subprojects/rizin-shell-parser/corpus/cmd_substitution.txt +++ b/subprojects/rizin-shell-parser/corpus/cmd_substitution.txt @@ -2,7 +2,7 @@ Command substitution used as simple arg $( ======================================= -?e $(p8 10) +echo $(p8 10) --- @@ -17,7 +17,7 @@ Command substitution used as simple arg $( Command substitution with multiple statements ======================================= -?e $(p8 10; p8 4 @ 0xdeadbeef) +echo $(p8 10; p8 4 @ 0xdeadbeef) --- @@ -38,7 +38,7 @@ Command substitution with multiple statements Command substitution used as simple arg ` ======================================= -?e `p8 10` +echo `p8 10` --- @@ -54,7 +54,7 @@ Command substitution used as simple arg ` Nested command substitution ======================================= -?e $(p8 $(?e 10)) +echo $(p8 $(echo 10)) --- @@ -71,7 +71,7 @@ Nested command substitution Nested command substitution 2 ======================================= -?e Hello$(?e Wor$(?e ld)) +echo Hello$(echo Wor$(echo ld)) --- diff --git a/subprojects/rizin-shell-parser/corpus/comments.txt b/subprojects/rizin-shell-parser/corpus/comments.txt index f51f94c0994..0b987780e4e 100644 --- a/subprojects/rizin-shell-parser/corpus/comments.txt +++ b/subprojects/rizin-shell-parser/corpus/comments.txt @@ -57,9 +57,9 @@ p8 4 # something #! identified as comment if not command name ============================================ -?e #!this is a comment -?e #this is also a comment -?e # and this too +echo #!this is a comment +echo #this is also a comment +echo # and this too --- diff --git a/subprojects/rizin-shell-parser/corpus/escape_args.txt b/subprojects/rizin-shell-parser/corpus/escape_args.txt index 23aa0c7ac68..553d3cb4cab 100644 --- a/subprojects/rizin-shell-parser/corpus/escape_args.txt +++ b/subprojects/rizin-shell-parser/corpus/escape_args.txt @@ -16,7 +16,7 @@ pd 10\@test\>name Use newlines in echo ==================== -?e "Hello\nWorld" +echo "Hello\nWorld" --- @@ -30,7 +30,7 @@ Use newlines in echo Escape hash =========== -?e Hello\#World +echo Hello\#World --- diff --git a/subprojects/rizin-shell-parser/corpus/grep_commands.txt b/subprojects/rizin-shell-parser/corpus/grep_commands.txt index 565b6a43889..db97a1ca977 100644 --- a/subprojects/rizin-shell-parser/corpus/grep_commands.txt +++ b/subprojects/rizin-shell-parser/corpus/grep_commands.txt @@ -28,9 +28,9 @@ afl~$ Grep with cmd substitution as specifier ======================================= -pd 10~`?e mov` -pd 10~mo`?e v` -pd 10~mo$(?e v) +pd 10~`echo mov` +pd 10~mo`echo v` +pd 10~mo$(echo v) --- diff --git a/subprojects/rizin-shell-parser/corpus/iter_commands.txt b/subprojects/rizin-shell-parser/corpus/iter_commands.txt index 2d6c594a5fd..9ea7e2a0583 100644 --- a/subprojects/rizin-shell-parser/corpus/iter_commands.txt +++ b/subprojects/rizin-shell-parser/corpus/iter_commands.txt @@ -66,7 +66,7 @@ p8 @@/x 9090 Iter interpret cmd ================== -p8 @@c:?e hello +p8 @@c:echo hello --- @@ -81,7 +81,7 @@ p8 @@c:?e hello Iter interpret offsetssizes cmd =============================== -p8 @@@c:?e hello 10 +p8 @@@c:echo hello 10 --- diff --git a/subprojects/rizin-shell-parser/corpus/quoted_args.txt b/subprojects/rizin-shell-parser/corpus/quoted_args.txt index 70988ac1735..b4c6215748d 100644 --- a/subprojects/rizin-shell-parser/corpus/quoted_args.txt +++ b/subprojects/rizin-shell-parser/corpus/quoted_args.txt @@ -2,7 +2,7 @@ Echo with (double) quoted args ============================== -?e "This;is.one@string" +echo "This;is.one@string" --- @@ -14,7 +14,7 @@ Echo with (double) quoted args Echo with (single) quoted args ============================== -?e 'This;is.one@string' +echo 'This;is.one@string' --- @@ -27,7 +27,7 @@ Echo with (single) quoted args Legacy command - all quoted =========================== -"?e This;is.one@string" +"echo This;is.one@string" --- @@ -39,7 +39,7 @@ Legacy command - all quoted Double quoted arg with cmd substitution ======================================= -?e "This is $(?e "a command")" +echo "This is $(echo "a command")" --- @@ -56,9 +56,9 @@ Double quoted arg with cmd substitution Quoted hash ================== -?e "#" -?e " #$(pdf)" -?e '#' +echo "#" +echo " #$(pdf)" +echo '#' --- diff --git a/subprojects/rizin-shell-parser/corpus/simple_commands.txt b/subprojects/rizin-shell-parser/corpus/simple_commands.txt index 02e05b52d40..e3f50b3a780 100644 --- a/subprojects/rizin-shell-parser/corpus/simple_commands.txt +++ b/subprojects/rizin-shell-parser/corpus/simple_commands.txt @@ -54,6 +54,20 @@ af 0xdeadbeef (args (arg (arg_identifier))))) +========================= +?v not valid +========================= + +?v 3 + +--- + +(statements + (help_stmt + (cmd_identifier)) + (ERROR)) + + ====================== Percentage mark statements ====================== @@ -160,7 +174,7 @@ pf??? (help_stmt (cmd_identifier)) (help_stmt (cmd_identifier)) (help_stmt (cmd_identifier)) - (arged_stmt (cmd_identifier)) + (help_stmt (cmd_identifier)) (help_stmt (cmd_identifier)) (help_stmt (cmd_identifier)) (help_stmt (cmd_identifier)) @@ -173,7 +187,7 @@ pf??? Command with concatenation args =============================== -?e Hello" World"'!' And All +echo Hello" World"'!' And All --- @@ -193,7 +207,7 @@ Command with concatenation args Tasks command ============= -& ?e Hello +& echo Hello && 1 &= & @@ -215,7 +229,7 @@ Tasks command Arg with (...) ============== -?e Hello(World) +echo Hello(World) --- @@ -235,7 +249,7 @@ Arg with (...) Expressions with (...) ============================ -?v (100 + (2 - 3)) * 2 +%v (100 + (2 - 3)) * 2 --- @@ -263,13 +277,13 @@ Expressions with (...) Arg with $ ========== -?v $$ +%v $$ s $ -?v $* -?v $$$ test -?v $alias -?e hello$alias -?e test +%v $* +%v $$$ test +%v $alias +echo hello$alias +echo test --- diff --git a/subprojects/rizin-shell-parser/corpus/special_commands.txt b/subprojects/rizin-shell-parser/corpus/special_commands.txt index e9627d06bb0..8a465863e70 100644 --- a/subprojects/rizin-shell-parser/corpus/special_commands.txt +++ b/subprojects/rizin-shell-parser/corpus/special_commands.txt @@ -268,7 +268,7 @@ Interpreter statements Pointer type statements with substitution ======================================= -*entr$(?e y0)=$(?v $$) +*entr$(echo y0)=$(%v $$) --- diff --git a/subprojects/rizin-shell-parser/src/scanner.c b/subprojects/rizin-shell-parser/src/scanner.c index 5df51c9bcfb..133c5850da9 100644 --- a/subprojects/rizin-shell-parser/src/scanner.c +++ b/subprojects/rizin-shell-parser/src/scanner.c @@ -60,11 +60,11 @@ static bool is_interpret_cmd(const char *s) { static bool is_special_start(const int32_t ch) { return ch == '*' || ch == '(' || ch == '@' || ch == '|' || ch == '>' || - ch == '.' || ch == '|' || ch == '~' || ch == '!'; + ch == '.' || ch == '|' || ch == '~' || ch == '!' || ch == '?'; } static bool is_start_of_command(const int32_t ch) { - return iswalpha (ch) || ch == '$' || ch == '?' || ch == ':' || ch == '+' || + return iswalpha (ch) || ch == '$' || ch == ':' || ch == '+' || ch == '=' || ch == '/' || ch == '_' || ch == '#' || ch == '\\' || ch == '-' || ch == '<' || ch == '&' || ch == '%' || is_special_start (ch); } @@ -180,10 +180,9 @@ bool tree_sitter_rzcmd_external_scanner_scan(void *payload, TSLexer *lexer, cons if (is_comment (res)) { return false; } - // ?? is not considered an help command, just a regular one - if ((res[i_res - 1] == '?' && strcmp (res, "??") != 0) || - (i_res > 2 && is_recursive_help (res[i_res - 2], res[i_res - 1])) || - (i_res > 3 && is_recursive_help_json (res[i_res - 3], res[i_res - 2], res[i_res - 1]))) { + if ((res[i_res - 1] == '?') || + (i_res >= 2 && is_recursive_help(res[i_res - 2], res[i_res - 1])) || + (i_res >= 3 && is_recursive_help_json(res[i_res - 3], res[i_res - 2], res[i_res - 1]))) { if (i_res == 1) { return false; }