Implement ~$!! as a tac replacement and clarify the ~$! use ##shell

This commit is contained in:
pancake 2022-07-30 19:50:50 -04:00 committed by GitHub
parent 14df487086
commit be93a9c4b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 130 additions and 21 deletions

View File

@ -1168,11 +1168,8 @@ static bool esil_lsl(RAnalEsil *esil) {
if (num2 > sizeof (ut64) * 8) {
ERR ("esil_lsl: shift is too big");
} else {
if (num2 > 63) {
r_anal_esil_pushnum (esil, 0);
} else {
r_anal_esil_pushnum (esil, num << num2);
}
const ut64 shift = (num2 > 63)? 0: num << num2;
r_anal_esil_pushnum (esil, shift);
ret = true;
}
} else {

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2021 - pancake, nibble */
/* radare - LGPL - Copyright 2009-2022 - pancake, nibble */
#include <r_cons.h>
#include <r_util/r_print.h>
@ -23,7 +23,9 @@ static const char *help_detail_tilde[] = {
"modifier:", "", "",
" &", "", "all words must match to grep the line",
" $[n]", "", "sort numerically / alphabetically the Nth column",
" $!", "", "sort in inverse order",
" $", "", "sort in alphabetic order",
" $!", "", "inverse alphabetical sort",
" $!!", "", "reverse the lines (like the `tac` tool)",
" ,", "", "token to define another keyword",
" +", "", "case insensitive grep (grep -i)",
" ^", "", "words must be placed at the beginning of line",
@ -180,13 +182,17 @@ static void parse_grep_expression(const char *str) {
break;
case '$':
ptr++;
grep->sort = 0;
if (*ptr == '!') {
if (ptr[1] == '!') {
grep->sort = -1;
ptr++;
}
grep->sort_invert = true;
ptr++;
} else {
grep->sort_invert = false;
}
grep->sort = atoi (ptr);
while (IS_DIGIT (*ptr)) {
ptr++;
}
@ -763,7 +769,7 @@ R_API void r_cons_grepbuf(void) {
}
cons->context->buffer_len = ob_len;
if (grep->sort != -1) {
if (grep->sort != -1 || grep->sort_invert) {
#define INSERT_LINES(list)\
do {\
r_list_foreach (list, iter, str) {\
@ -783,7 +789,9 @@ R_API void r_cons_grepbuf(void) {
RConsContext *ctx = cons->context;
ctx->sorted_column = grep->sort;
r_list_sort (ctx->sorted_lines, cmp);
if (grep->sort != -1) {
r_list_sort (ctx->sorted_lines, cmp);
}
if (grep->sort_invert) {
r_list_reverse (ctx->sorted_lines);
}
@ -907,7 +915,18 @@ R_API int r_cons_grep_line(char *buf, int len) {
}
free (in);
free (out);
if (grep->sort != -1) {
if (grep->sort_invert && grep->sort == -1) {
char ch = buf[len];
buf[len] = 0;
if (!ctx->sorted_lines) {
ctx->sorted_lines = r_list_newf (free);
}
if (!ctx->unsorted_lines) {
ctx->unsorted_lines = r_list_newf (free);
}
r_list_append (ctx->sorted_lines, strdup (buf));
buf[len] = ch;
} else if (grep->sort != -1) {
char ch = buf[len];
buf[len] = 0;
if (!ctx->sorted_lines) {

View File

@ -341,23 +341,23 @@ static RNumCalcToken get_token(RNum *num, RNumCalc *nc) {
int i = 0;
#define stringValueAppend(x) { \
const size_t max = sizeof (nc->string_value) - 1; \
if (i < max) nc->string_value[i++] = x; \
else nc->string_value[max] = 0; \
if (i < max) { nc->string_value[i++] = x; } \
else { nc->string_value[max] = 0; } \
}
stringValueAppend(ch);
stringValueAppend (ch);
if (ch == '[') {
while (cin_get (num, nc, &ch) && ch != ']') {
if (i > R_NUMCALC_STRSZ - 1) {
error (num, nc, "string too long");
return 0;
}
stringValueAppend(ch);
stringValueAppend (ch);
}
if (ch != ']') {
error (num, nc, "cannot find closing ]");
return 0;
}
stringValueAppend(ch);
stringValueAppend (ch);
} else if (ch == ']') {
error (num, nc, "cannot find opening [");
return 0;
@ -367,7 +367,7 @@ static RNumCalcToken get_token(RNum *num, RNumCalc *nc) {
error (num, nc, "string too long");
return 0;
}
stringValueAppend(ch);
stringValueAppend (ch);
}
}
stringValueAppend (0);

View File

@ -436,7 +436,6 @@ R_API ut64 r_num_get(RNum *num, const char *str) {
}
R_API ut64 r_num_math(RNum *num, const char *str) {
ut64 ret;
const char *err = NULL;
if (R_STR_ISEMPTY (str)) {
return 0LL;
@ -444,7 +443,7 @@ R_API ut64 r_num_math(RNum *num, const char *str) {
if (num) {
num->dbz = 0;
}
ret = r_num_calc (num, str, &err); // TODO: rename r_num_calc to r_num_math_err()
ut64 ret = r_num_calc (num, str, &err); // TODO: rename r_num_calc to r_num_math_err()
if (err) {
R_LOG_DEBUG ("(%s) in (%s)", err, str);
}
@ -632,7 +631,7 @@ R_API char* r_num_as_string(RNum *___, ut64 n, bool printable_only) {
int stri, ret = 0, off = 0;
int len = sizeof (ut64);
ut64 num = n;
str[stri=0] = 0;
str[stri = 0] = 0;
while (len--) {
char ch = (num & 0xff);
if (ch >= 32 && ch < 127) {
@ -744,7 +743,7 @@ static ut64 r_num_tailff(RNum *num, const char *hex) {
}
free (p);
}
ut64 left = ((UT64_MAX >>i) << i);
ut64 left = ((UT64_MAX >> i) << i);
return left | n;
}

View File

@ -1,3 +1,85 @@
NAME=?e ~$! invert tac
FILE=bins/elf/crackme
CMDS=<<EOF
iS
?e --
iS~$!!
EOF
EXPECT=<<EOF
[Sections]
nth paddr size vaddr vsize perm name
-------------------------------------------------
0 0x00000000 0x0 0x00000000 0x0 ----
1 0x00000200 0x1c 0x00400200 0x1c -r-- .interp
2 0x0000021c 0x20 0x0040021c 0x20 -r-- .note.ABI-tag
3 0x0000023c 0x24 0x0040023c 0x24 -r-- .note.gnu.build-id
4 0x00000260 0x3c 0x00400260 0x3c -r-- .hash
5 0x000002a0 0x28 0x004002a0 0x28 -r-- .gnu.hash
6 0x000002c8 0xf0 0x004002c8 0xf0 -r-- .dynsym
7 0x000003b8 0x65 0x004003b8 0x65 -r-- .dynstr
8 0x0000041e 0x14 0x0040041e 0x14 -r-- .gnu.version
9 0x00000438 0x20 0x00400438 0x20 -r-- .gnu.version_r
10 0x00000458 0x48 0x00400458 0x48 -r-- .rela.dyn
11 0x000004a0 0x90 0x004004a0 0x90 -r-- .rela.plt
12 0x00000530 0x18 0x00400530 0x18 -r-x .init
13 0x00000548 0x70 0x00400548 0x70 -r-x .plt
14 0x000005c0 0x248 0x004005c0 0x248 -r-x .text
15 0x00000808 0xe 0x00400808 0xe -r-x .fini
16 0x00000818 0x3a 0x00400818 0x3a -r-- .rodata
17 0x00000854 0x24 0x00400854 0x24 -r-- .eh_frame_hdr
18 0x00000878 0x7c 0x00400878 0x7c -r-- .eh_frame
19 0x000008f8 0x10 0x006008f8 0x10 -rw- .ctors
20 0x00000908 0x10 0x00600908 0x10 -rw- .dtors
21 0x00000918 0x8 0x00600918 0x8 -rw- .jcr
22 0x00000920 0x1a0 0x00600920 0x1a0 -rw- .dynamic
23 0x00000ac0 0x8 0x00600ac0 0x8 -rw- .got
24 0x00000ac8 0x48 0x00600ac8 0x48 -rw- .got.plt
25 0x00000b10 0x10 0x00600b10 0x10 -rw- .data
26 0x00000b20 0x0 0x00600b20 0x28 -rw- .bss
27 0x00000b20 0x1c 0x00000000 0x1c ---- .comment
28 0x00000b3c 0xfe 0x00000000 0xfe ---- .shstrtab
29 0x00001400 0x6a8 0x00000000 0x6a8 ---- .symtab
30 0x00001aa8 0x269 0x00000000 0x269 ---- .strtab
--
30 0x00001aa8 0x269 0x00000000 0x269 ---- .strtab
29 0x00001400 0x6a8 0x00000000 0x6a8 ---- .symtab
28 0x00000b3c 0xfe 0x00000000 0xfe ---- .shstrtab
27 0x00000b20 0x1c 0x00000000 0x1c ---- .comment
26 0x00000b20 0x0 0x00600b20 0x28 -rw- .bss
25 0x00000b10 0x10 0x00600b10 0x10 -rw- .data
24 0x00000ac8 0x48 0x00600ac8 0x48 -rw- .got.plt
23 0x00000ac0 0x8 0x00600ac0 0x8 -rw- .got
22 0x00000920 0x1a0 0x00600920 0x1a0 -rw- .dynamic
21 0x00000918 0x8 0x00600918 0x8 -rw- .jcr
20 0x00000908 0x10 0x00600908 0x10 -rw- .dtors
19 0x000008f8 0x10 0x006008f8 0x10 -rw- .ctors
18 0x00000878 0x7c 0x00400878 0x7c -r-- .eh_frame
17 0x00000854 0x24 0x00400854 0x24 -r-- .eh_frame_hdr
16 0x00000818 0x3a 0x00400818 0x3a -r-- .rodata
15 0x00000808 0xe 0x00400808 0xe -r-x .fini
14 0x000005c0 0x248 0x004005c0 0x248 -r-x .text
13 0x00000548 0x70 0x00400548 0x70 -r-x .plt
12 0x00000530 0x18 0x00400530 0x18 -r-x .init
11 0x000004a0 0x90 0x004004a0 0x90 -r-- .rela.plt
10 0x00000458 0x48 0x00400458 0x48 -r-- .rela.dyn
9 0x00000438 0x20 0x00400438 0x20 -r-- .gnu.version_r
8 0x0000041e 0x14 0x0040041e 0x14 -r-- .gnu.version
7 0x000003b8 0x65 0x004003b8 0x65 -r-- .dynstr
6 0x000002c8 0xf0 0x004002c8 0xf0 -r-- .dynsym
5 0x000002a0 0x28 0x004002a0 0x28 -r-- .gnu.hash
4 0x00000260 0x3c 0x00400260 0x3c -r-- .hash
3 0x0000023c 0x24 0x0040023c 0x24 -r-- .note.gnu.build-id
2 0x0000021c 0x20 0x0040021c 0x20 -r-- .note.ABI-tag
1 0x00000200 0x1c 0x00400200 0x1c -r-- .interp
0 0x00000000 0x0 0x00000000 0x0 ----
-------------------------------------------------
nth paddr size vaddr vsize perm name
[Sections]
EOF
RUN
NAME=?e ~
FILE=malloc://1024
CMDS=?e jeje~jojo
@ -394,19 +476,31 @@ EOF
RUN
NAME=sort lines
FILE=malloc://1024
CMDS=<<EOF
?e -- alphabetical sort --
?e 3\n2\n4\n1~$
?e -- inverted alphabetical sort --
?e 3\n2\n4\n1~$!
?e -- invert (like tac) --
?e 3\n2\n4\n1~$!!
EOF
EXPECT=<<EOF
-- alphabetical sort --
1
2
3
4
-- inverted alphabetical sort --
4
3
2
1
-- invert (like tac) --
1
4
2
3
EOF
RUN