2018-02-28 00:24:03 +00:00
|
|
|
/* radare2 - LGPL - Copyright 2014-2018 - pancake, Judge_Dredd */
|
2014-02-11 01:32:37 +00:00
|
|
|
|
|
|
|
#include <r_cons.h>
|
2016-06-29 10:33:31 +00:00
|
|
|
#include <r_regex.h>
|
2015-04-12 12:59:13 +00:00
|
|
|
#include <r_util.h>
|
2018-11-14 12:15:50 +00:00
|
|
|
#include "pager_private.h"
|
2015-02-11 14:22:39 +00:00
|
|
|
|
2015-10-29 11:54:19 +00:00
|
|
|
R_API int r_cons_less_str(const char *str, const char *exitkeys) {
|
2016-01-14 22:27:09 +00:00
|
|
|
static int in_help = false;
|
|
|
|
static const char *r_cons_less_help = \
|
|
|
|
" u/space - page up/down\n"
|
|
|
|
" jk - line down/up\n"
|
|
|
|
" gG - begin/end buffer\n"
|
|
|
|
" / - search in buffer\n"
|
2017-09-26 09:53:16 +00:00
|
|
|
" _ - enter the hud mode\n"
|
2016-01-14 22:27:09 +00:00
|
|
|
" n/p - next/prev search result\n"
|
|
|
|
" q - quit\n"
|
|
|
|
" ? - show this help\n"
|
|
|
|
"\n";
|
2016-01-27 02:14:19 +00:00
|
|
|
int lines_count = 0;
|
2015-02-08 17:42:02 +00:00
|
|
|
RRegex *rx = NULL;
|
2015-04-21 11:26:54 +00:00
|
|
|
int w, h, ch, to, ui = 1, from = 0, i;
|
2015-02-09 23:05:18 +00:00
|
|
|
const char *sreg;
|
2016-01-27 02:14:19 +00:00
|
|
|
RList **mla;
|
2015-04-21 11:26:54 +00:00
|
|
|
|
2016-11-02 21:59:32 +00:00
|
|
|
if (!str || !*str) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-09-26 09:53:16 +00:00
|
|
|
// rcons kills str after flushing the buffer, so we must keep a copy
|
|
|
|
char *ostr = strdup (str);
|
|
|
|
if (!ostr) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-02-09 23:05:18 +00:00
|
|
|
char *p = strdup (str);
|
2016-11-02 21:59:32 +00:00
|
|
|
if (!p) {
|
2017-09-26 09:53:16 +00:00
|
|
|
free (ostr);
|
2016-11-02 21:59:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-11-14 12:15:50 +00:00
|
|
|
int *lines = pager_splitlines (p, &lines_count);
|
2016-11-02 21:59:32 +00:00
|
|
|
if (lines_count < 1) {
|
2016-01-27 02:14:19 +00:00
|
|
|
mla = NULL;
|
|
|
|
} else {
|
|
|
|
mla = calloc (lines_count, sizeof (RList *));
|
2016-05-24 06:03:15 +00:00
|
|
|
if (!mla) {
|
|
|
|
free (p);
|
2017-09-26 09:53:16 +00:00
|
|
|
free (ostr);
|
2016-05-24 06:04:34 +00:00
|
|
|
free (lines);
|
2016-05-24 06:03:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-01-27 02:14:19 +00:00
|
|
|
}
|
2016-06-29 10:33:31 +00:00
|
|
|
for (i = 0; i < lines_count; i++) {
|
2016-01-27 02:14:19 +00:00
|
|
|
mla[i] = r_list_new ();
|
2016-06-29 10:33:31 +00:00
|
|
|
}
|
2015-09-14 00:08:31 +00:00
|
|
|
r_cons_set_raw (true);
|
|
|
|
r_cons_show_cursor (false);
|
2014-02-11 01:32:37 +00:00
|
|
|
r_cons_reset ();
|
2018-12-11 09:28:01 +00:00
|
|
|
h = 0;
|
2014-02-11 01:32:37 +00:00
|
|
|
while (ui) {
|
2015-04-12 12:59:13 +00:00
|
|
|
w = r_cons_get_size (&h);
|
2018-09-17 10:15:12 +00:00
|
|
|
to = R_MIN (lines_count, from + h);
|
2016-11-02 21:59:32 +00:00
|
|
|
if (from + 3 > lines_count) {
|
2016-05-16 00:59:42 +00:00
|
|
|
from = lines_count - 3;
|
2016-11-02 21:59:32 +00:00
|
|
|
}
|
|
|
|
if (from < 0) {
|
|
|
|
from = 0;
|
|
|
|
}
|
2018-11-14 12:15:50 +00:00
|
|
|
pager_printpage (p, lines, mla, from, to, w);
|
2014-02-11 01:32:37 +00:00
|
|
|
ch = r_cons_readchar ();
|
2015-10-29 11:54:19 +00:00
|
|
|
if (exitkeys && strchr (exitkeys, ch)) {
|
2015-11-01 04:47:16 +00:00
|
|
|
for (i = 0; i < lines_count; i++) {
|
2016-01-27 02:14:19 +00:00
|
|
|
r_list_free (mla[i]);
|
2015-11-01 04:47:16 +00:00
|
|
|
}
|
2015-11-05 09:34:26 +00:00
|
|
|
free (p);
|
2017-09-26 09:53:16 +00:00
|
|
|
free (mla);
|
|
|
|
free (ostr);
|
2015-11-08 22:38:42 +00:00
|
|
|
free (lines);
|
2015-10-29 11:54:19 +00:00
|
|
|
return ch;
|
|
|
|
}
|
2014-02-11 01:32:37 +00:00
|
|
|
ch = r_cons_arrow_to_hjkl (ch);
|
|
|
|
switch (ch) {
|
2017-09-26 09:53:16 +00:00
|
|
|
case '_':
|
|
|
|
r_cons_hud_string (ostr);
|
|
|
|
break;
|
2016-01-14 22:27:09 +00:00
|
|
|
case '?':
|
|
|
|
if (!in_help) {
|
|
|
|
in_help = true;
|
|
|
|
r_cons_less_str (r_cons_less_help, NULL);
|
|
|
|
in_help = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
from -= h;
|
2016-11-02 21:59:32 +00:00
|
|
|
if (from < 0) {
|
|
|
|
from = 0;
|
|
|
|
}
|
2016-01-14 22:27:09 +00:00
|
|
|
break;
|
2014-02-11 01:32:37 +00:00
|
|
|
case ' ': from += h; break;
|
|
|
|
case 'g': from = 0; break;
|
2018-09-17 10:15:12 +00:00
|
|
|
case 'G': from = lines_count-h; break;
|
2015-03-23 15:18:35 +00:00
|
|
|
case -1: // EOF
|
2018-10-23 09:55:09 +00:00
|
|
|
case '\x03': // ^C
|
2014-02-11 01:32:37 +00:00
|
|
|
case 'q': ui = 0; break;
|
2014-03-09 20:38:37 +00:00
|
|
|
case '\r':
|
|
|
|
case '\n':
|
2014-02-11 01:32:37 +00:00
|
|
|
case 'j': from++; break;
|
|
|
|
case 'J': from+=h; break;
|
2018-09-13 08:17:26 +00:00
|
|
|
case 'k':
|
|
|
|
if (from > 0) {
|
|
|
|
from--;
|
|
|
|
}
|
|
|
|
break;
|
2014-03-09 23:53:25 +00:00
|
|
|
case 'K': from = (from>=h)? from-h: 0;
|
2014-02-11 01:32:37 +00:00
|
|
|
break;
|
2015-02-08 17:42:02 +00:00
|
|
|
case '/': /* search */
|
2016-01-14 22:27:09 +00:00
|
|
|
r_cons_reset_colors ();
|
|
|
|
r_line_set_prompt ("/");
|
|
|
|
sreg = r_line_readline ();
|
2018-11-14 12:15:50 +00:00
|
|
|
from = R_MIN (lines_count - 1, from);
|
2015-02-08 17:42:02 +00:00
|
|
|
/* repeat last search if empty string is provided */
|
2016-01-14 22:27:09 +00:00
|
|
|
if (sreg[0]) { /* prepare for a new search */
|
2018-09-13 08:17:26 +00:00
|
|
|
if (rx) {
|
|
|
|
r_regex_free (rx);
|
|
|
|
}
|
2018-11-14 12:15:50 +00:00
|
|
|
rx = r_regex_new (sreg, "");
|
2015-02-11 14:22:39 +00:00
|
|
|
} else { /* we got an empty string */
|
2018-11-14 12:15:50 +00:00
|
|
|
from = pager_next_match (from, mla, lines_count);
|
2015-02-11 14:22:39 +00:00
|
|
|
break;
|
2015-02-08 17:42:02 +00:00
|
|
|
}
|
2018-09-13 08:17:26 +00:00
|
|
|
if (!rx) {
|
|
|
|
break;
|
|
|
|
}
|
2015-02-11 14:22:39 +00:00
|
|
|
/* find all occurences */
|
2018-11-14 12:15:50 +00:00
|
|
|
if (pager_all_matches (p, rx, mla, lines, lines_count)) {
|
|
|
|
from = pager_next_match (from, mla, lines_count);
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2015-02-11 14:22:39 +00:00
|
|
|
break;
|
|
|
|
case 'n': /* next match */
|
|
|
|
/* search already performed */
|
2016-06-29 10:33:31 +00:00
|
|
|
if (rx) {
|
2018-11-14 12:15:50 +00:00
|
|
|
from = pager_next_match (from, mla, lines_count);
|
2016-06-29 10:33:31 +00:00
|
|
|
}
|
2015-02-11 14:22:39 +00:00
|
|
|
break;
|
2018-08-05 21:14:04 +00:00
|
|
|
case 'N':
|
2015-02-11 14:22:39 +00:00
|
|
|
case 'p': /* previous match */
|
2016-06-29 10:33:31 +00:00
|
|
|
if (rx) {
|
2018-11-14 12:15:50 +00:00
|
|
|
from = pager_prev_match (from, mla);
|
2016-06-29 10:33:31 +00:00
|
|
|
}
|
2015-02-11 14:22:39 +00:00
|
|
|
break;
|
2014-02-11 01:32:37 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-29 11:54:19 +00:00
|
|
|
for (i = 0; i < lines_count; i++) {
|
2016-01-14 22:27:09 +00:00
|
|
|
r_list_free (mla[i]);
|
2015-10-29 11:54:19 +00:00
|
|
|
}
|
|
|
|
free (mla);
|
2018-07-05 09:40:59 +00:00
|
|
|
r_regex_free (rx);
|
2014-02-11 01:32:37 +00:00
|
|
|
free (lines);
|
2014-04-29 22:55:07 +00:00
|
|
|
free (p);
|
2015-10-29 11:54:19 +00:00
|
|
|
r_cons_reset_colors ();
|
2015-09-14 00:08:31 +00:00
|
|
|
r_cons_set_raw (false);
|
|
|
|
r_cons_show_cursor (true);
|
2017-09-26 09:53:16 +00:00
|
|
|
free (ostr);
|
2015-10-29 11:54:19 +00:00
|
|
|
return 0;
|
2014-02-11 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_cons_less() {
|
2018-06-23 10:10:13 +00:00
|
|
|
r_cons_less_str (r_cons_singleton ()->context->buffer, NULL);
|
2014-02-11 01:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
main (int argc, char **argv) {
|
|
|
|
char *s = r_file_slurp (argv[1], NULL);
|
|
|
|
r_cons_new ();
|
|
|
|
r_cons_less (s);
|
|
|
|
}
|
|
|
|
#endif
|