Rizin
unix-like reverse engineering framework and cli tools
less.c File Reference
#include <rz_cons.h>
#include <rz_regex.h>
#include <rz_util.h>
#include "pager_private.h"

Go to the source code of this file.

Functions

RZ_API int rz_cons_less_str (const char *str, const char *exitkeys)
 
RZ_API void rz_cons_less (void)
 

Function Documentation

◆ rz_cons_less()

RZ_API void rz_cons_less ( void  )

Definition at line 171 of file less.c.

171  {
173 }
RZ_API RzCons * rz_cons_singleton(void)
Definition: cons.c:300
#define NULL
Definition: cris-opc.c:27
RZ_API int rz_cons_less_str(const char *str, const char *exitkeys)
Definition: less.c:10
RzConsContext * context
Definition: rz_cons.h:502

References rz_cons_context_t::buffer, rz_cons_t::context, NULL, rz_cons_less_str(), and rz_cons_singleton().

Referenced by rz_core_visual_graph().

◆ rz_cons_less_str()

RZ_API int rz_cons_less_str ( const char *  str,
const char *  exitkeys 
)

Definition at line 10 of file less.c.

10  {
12  if (!rz_cons_is_interactive()) {
13  eprintf("Internal less requires scr.interactive=true.\n");
14  return 0;
15  }
16 
17  static int in_help = false;
18  static const char *rz_cons_less_help =
19  " u/space - page up/down\n"
20  " jk - line down/up\n"
21  " gG - begin/end buffer\n"
22  " / - search in buffer\n"
23  " _ - enter the hud mode\n"
24  " n/p - next/prev search result\n"
25  " q - quit\n"
26  " ? - show this help\n"
27  "\n";
28  int lines_count = 0;
29  RzRegex *rx = NULL;
30  int w, h, ch, to, ui = 1, from = 0, i;
31  const char *sreg;
32  RzList **mla;
33 
34  // rcons kills str after flushing the buffer, so we must keep a copy
35  char *ostr = strdup(str);
36  if (!ostr) {
37  return 0;
38  }
39  char *p = strdup(str);
40  if (!p) {
41  free(ostr);
42  return 0;
43  }
44  int *lines = pager_splitlines(p, &lines_count);
45  if (lines_count < 1) {
46  mla = NULL;
47  } else {
48  mla = calloc(lines_count, sizeof(RzList *));
49  if (!mla) {
50  free(p);
51  free(ostr);
52  free(lines);
53  return 0;
54  }
55  }
56  for (i = 0; i < lines_count; i++) {
57  mla[i] = rz_list_new();
58  }
59  rz_cons_set_raw(true);
60  rz_cons_show_cursor(false);
61  rz_cons_reset();
62  h = 0;
63  while (ui) {
64  w = rz_cons_get_size(&h);
65  to = RZ_MIN(lines_count, from + h);
66  if (from + 3 > lines_count) {
67  from = lines_count - 3;
68  }
69  if (from < 0) {
70  from = 0;
71  }
73  ch = rz_cons_readchar();
74  if (exitkeys && strchr(exitkeys, ch)) {
75  for (i = 0; i < lines_count; i++) {
76  rz_list_free(mla[i]);
77  }
78  free(p);
79  free(mla);
80  free(ostr);
81  free(lines);
82  return ch;
83  }
84  ch = rz_cons_arrow_to_hjkl(ch);
85  switch (ch) {
86  case '_':
87  rz_cons_hud_string(ostr);
88  break;
89  case '?':
90  if (!in_help) {
91  in_help = true;
92  (void)rz_cons_less_str(rz_cons_less_help, NULL);
93  in_help = false;
94  }
95  break;
96  case 'u':
97  from -= h;
98  if (from < 0) {
99  from = 0;
100  }
101  break;
102  case ' ': from += h; break;
103  case 'g': from = 0; break;
104  case 'G': from = lines_count - h; break;
105  case -1: // EOF
106  case '\x03': // ^C
107  case 'q': ui = 0; break;
108  case '\r':
109  case '\n':
110  case 'j': from++; break;
111  case 'J': from += h; break;
112  case 'k':
113  if (from > 0) {
114  from--;
115  }
116  break;
117  case 'K':
118  from = (from >= h) ? from - h : 0;
119  break;
120  case '/': /* search */
122  rz_line_set_prompt("/");
123  sreg = rz_line_readline();
124  from = RZ_MIN(lines_count - 1, from);
125  /* repeat last search if empty string is provided */
126  if (sreg[0]) { /* prepare for a new search */
127  if (rx) {
128  rz_regex_free(rx);
129  }
130  rx = rz_regex_new(sreg, "");
131  } else { /* we got an empty string */
132  from = pager_next_match(from, mla, lines_count);
133  break;
134  }
135  if (!rx) {
136  break;
137  }
138  /* find all occurrences */
139  if (pager_all_matches(p, rx, mla, lines, lines_count)) {
140  from = pager_next_match(from, mla, lines_count);
141  }
142  break;
143  case 'n': /* next match */
144  /* search already performed */
145  if (rx) {
146  from = pager_next_match(from, mla, lines_count);
147  }
148  break;
149  case 'N':
150  case 'p': /* previous match */
151  if (rx) {
153  }
154  break;
155  }
156  }
157  for (i = 0; i < lines_count; i++) {
158  rz_list_free(mla[i]);
159  }
160  free(mla);
161  rz_regex_free(rx);
162  free(lines);
163  free(p);
165  rz_cons_set_raw(false);
166  rz_cons_show_cursor(true);
167  free(ostr);
168  return 0;
169 }
lzma_index ** i
Definition: index.h:629
static RzILOpEffect * mla(cs_insn *insn, bool is_thumb)
Definition: arm_il32.c:1217
static RzBinSourceLineInfo * lines(RzBinFile *bf)
Definition: bin_symbols.c:427
RZ_API void rz_cons_set_raw(bool is_raw)
Definition: cons.c:1617
RZ_API int rz_cons_get_size(int *rows)
Definition: cons.c:1446
RZ_API void rz_cons_reset_colors(void)
Definition: cons.c:783
RZ_API bool rz_cons_is_interactive(void)
Definition: cons.c:365
RZ_API void rz_cons_show_cursor(int cursor)
Definition: cons.c:1581
RZ_API void rz_cons_reset(void)
Definition: cons.c:804
#define w
Definition: crypto_rc6.c:13
RZ_API const char * rz_line_readline(void)
Definition: dietline.c:913
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API char * rz_cons_hud_string(const char *s)
Definition: hud.c:22
RZ_API int rz_cons_arrow_to_hjkl(int ch)
Definition: input.c:78
RZ_API int rz_cons_readchar(void)
Definition: input.c:619
void * p
Definition: libc.cpp:67
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
RZ_API void rz_line_set_prompt(const char *prompt)
Definition: line.c:56
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
RZ_IPI void pager_printpage(const char *line, int *index, RzList **mla, int from, int to, int w)
Definition: pager.c:48
RZ_IPI bool pager_all_matches(const char *s, RzRegex *rx, RzList **mla, int *lines, int lcount)
Definition: pager.c:101
RZ_IPI int * pager_splitlines(char *s, int *lines_count)
Definition: pager.c:136
RZ_IPI int pager_prev_match(int from, RzList **mla)
Definition: pager.c:88
RZ_IPI int pager_next_match(int from, RzList **mla, int lcount)
Definition: pager.c:74
#define eprintf(x, y...)
Definition: rlcc.c:7
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API RzRegex * rz_regex_new(const char *pattern, const char *cflags)
Definition: regcomp.c:183
RZ_API void rz_regex_free(RzRegex *)
Definition: regcomp.c:249
#define RZ_MIN(x, y)
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr socklen_t static fromlen const void const struct sockaddr to
Definition: sfsocketcall.h:125
#define h(i)
Definition: sha256.c:48

References calloc(), eprintf, free(), from, h, i, lines(), mla(), NULL, p, pager_all_matches(), pager_next_match(), pager_prev_match(), pager_printpage(), pager_splitlines(), rz_cons_arrow_to_hjkl(), rz_cons_get_size(), rz_cons_hud_string(), rz_cons_is_interactive(), rz_cons_readchar(), rz_cons_reset(), rz_cons_reset_colors(), rz_cons_set_raw(), rz_cons_show_cursor(), rz_line_readline(), rz_line_set_prompt(), rz_list_free(), rz_list_new(), RZ_MIN, rz_regex_free(), rz_regex_new(), rz_return_val_if_fail, cmd_descs_generate::str, strdup(), to, and w.

Referenced by insert_mode_enabled(), rz_cons_flush(), rz_cons_grepbuf(), rz_cons_less(), and visual_help().