Rizin
unix-like reverse engineering framework and cli tools
input.c File Reference
#include <rz_cons.h>
#include <string.h>

Go to the source code of this file.

Macros

#define I   rz_cons_singleton()
 
#define RETURN(x)
 

Functions

RZ_API int rz_cons_controlz (int ch)
 
static int __parseMouseEvent (void)
 
RZ_API int rz_cons_arrow_to_hjkl (int ch)
 
RZ_API int rz_cons_fgets (char *buf, int len, int argc, const char **argv)
 
RZ_API int rz_cons_any_key (const char *msg)
 
void resizeWin (void)
 
RZ_API int rz_cons_readchar_timeout (ut32 usec)
 
RZ_API bool rz_cons_readpush (const char *str, int len)
 
RZ_API void rz_cons_readflush (void)
 
RZ_API void rz_cons_switchbuf (bool active)
 
RZ_API bool rz_cons_readbuffer_readchar (char *ch)
 
RZ_API int rz_cons_readchar (void)
 
RZ_API bool rz_cons_yesno (int def, const char *fmt,...)
 
RZ_API char * rz_cons_input (const char *msg)
 

Variables

volatile sig_atomic_t sigwinchFlag
 

Macro Definition Documentation

◆ I

#define I   rz_cons_singleton()

Definition at line 12 of file input.c.

◆ RETURN

#define RETURN (   x)
Value:
{ \
ret = x; \
goto beach; \
}
int x
Definition: mipsasm.c:20

Function Documentation

◆ __parseMouseEvent()

static int __parseMouseEvent ( void  )
static

Definition at line 30 of file input.c.

30  {
31  char xpos[32];
32  char ypos[32];
33  (void)rz_cons_readchar(); // skip first char
34  int ch2 = rz_cons_readchar();
35 
36  // [32M - mousedown
37  // [35M - mouseup
38  if (ch2 == ';') {
39  int i;
40  // read until next ;
41  for (i = 0; i < sizeof(xpos) - 1; i++) {
42  char ch = rz_cons_readchar();
43  if (ch == ';' || ch == 'M') {
44  break;
45  }
46  xpos[i] = ch;
47  }
48  xpos[i] = 0;
49  for (i = 0; i < sizeof(ypos) - 1; i++) {
50  char ch = rz_cons_readchar();
51  if (ch == ';' || ch == 'M') {
52  break;
53  }
54  ypos[i] = ch;
55  }
56  ypos[i] = 0;
57  rz_cons_set_click(atoi(xpos), atoi(ypos));
58  (void)rz_cons_readchar();
59  // ignored
60  int ch = rz_cons_readchar();
61  if (ch == 27) {
62  ch = rz_cons_readchar(); // '['
63  }
64  if (ch == '[') {
65  do {
66  ch = rz_cons_readchar(); // '3'
67  } while (ch != 'M');
68  }
69  }
70  return 0;
71 }
lzma_index ** i
Definition: index.h:629
RZ_API void rz_cons_set_click(int x, int y)
Definition: cons.c:477
RZ_API int rz_cons_readchar(void)
Definition: input.c:619

References i, rz_cons_readchar(), and rz_cons_set_click().

Referenced by rz_cons_arrow_to_hjkl().

◆ resizeWin()

void resizeWin ( void  )

Definition at line 471 of file cons.c.

471  {
472  if (I.event_resize) {
473  I.event_resize(I.event_data);
474  }
475 }
#define I
Definition: cons.c:22

References I.

Referenced by rz_cons_readchar().

◆ rz_cons_any_key()

RZ_API int rz_cons_any_key ( const char *  msg)

Definition at line 393 of file input.c.

393  {
394  if (msg && *msg) {
395  rz_cons_printf("\n-- %s --\n", msg);
396  } else {
397  rz_cons_print("\n--press any key--\n");
398  }
399  rz_cons_flush();
400  return rz_cons_readchar();
401  // rz_cons_strcat ("\x1b[2J\x1b[0;0H");
402 }
RZ_API int rz_cons_printf(const char *format,...)
Definition: cons.c:1202
RZ_API void rz_cons_flush(void)
Definition: cons.c:959
static struct sockaddr static addrlen static backlog const void msg
Definition: sfsocketcall.h:119

References msg, rz_cons_flush(), rz_cons_printf(), and rz_cons_readchar().

Referenced by rtr_visual(), rz_cmd_help(), rz_cons_message(), rz_core_visual_analysis(), rz_core_visual_analysis_classes(), rz_core_visual_asm(), rz_core_visual_bit_editor(), rz_core_visual_classes(), rz_core_visual_cmd(), rz_core_visual_comments(), rz_core_visual_config(), rz_core_visual_debugtraces(), rz_core_visual_debugtraces_help(), rz_core_visual_define(), rz_core_visual_esil(), rz_core_visual_graph(), rz_core_visual_trackflags(), rz_core_visual_view_graph(), rz_core_visual_view_rop(), rz_core_visual_xrefs(), and visual_search().

◆ rz_cons_arrow_to_hjkl()

RZ_API int rz_cons_arrow_to_hjkl ( int  ch)

Definition at line 78 of file input.c.

78  {
79 #if __WINDOWS__
80  if (I->vtmode != RZ_VIRT_TERM_MODE_COMPLETE) {
81  if (is_arrow) {
82  switch (ch) {
83  case VK_DOWN: // key down
84  ch = bCtrl ? 'J' : 'j';
85  break;
86  case VK_RIGHT: // key right
87  ch = bCtrl ? 'L' : 'l';
88  break;
89  case VK_UP: // key up
90  ch = bCtrl ? 'K' : 'k';
91  break;
92  case VK_LEFT: // key left
93  ch = bCtrl ? 'H' : 'h';
94  break;
95  case VK_PRIOR: // key home
96  ch = 'K';
97  break;
98  case VK_NEXT: // key end
99  ch = 'J';
100  break;
101  }
102  }
103  return I->mouse_event && (ut8)ch == UT8_MAX ? 0 : ch;
104  }
105 #endif
106  I->mouse_event = 0;
107  /* emacs */
108  switch ((ut8)ch) {
109  case 0xc3:
111  ch = 'K';
112  break; // emacs repag (alt + v)
113  case 0x16: ch = 'J'; break; // emacs avpag (ctrl + v)
114  case 0x10: ch = 'k'; break; // emacs up (ctrl + p)
115  case 0x0e: ch = 'j'; break; // emacs down (ctrl + n)
116  case 0x06: ch = 'l'; break; // emacs right (ctrl + f)
117  case 0x02: ch = 'h'; break; // emacs left (ctrl + b)
118  }
119  if (ch != 0x1b) {
120  return ch;
121  }
122  ch = rz_cons_readchar();
123  if (!ch) {
124  return 0;
125  }
126  switch (ch) {
127  case 0x1b:
128  ch = 'q'; // XXX: must be 0x1b (RZ_CONS_KEY_ESC)
129  break;
130  case 0x4f: // function keys from f1 to f4
131  ch = rz_cons_readchar();
132 #if defined(__HAIKU__)
133  /* Haiku't don use the '[' char for function keys */
134  if (ch > 'O') { /* only in f1..f12 function keys */
135  ch = 0xf1 + (ch & 0xf);
136  break;
137  }
138  case '[': // 0x5b function keys (2)
139  /* Haiku need ESC + [ for PageUp and PageDown */
140  if (ch < 'A' || ch == '[') {
141  ch = rz_cons_readchar();
142  }
143 #else
144  ch = 0xf1 + (ch & 0xf);
145  break;
146  case '[': // function keys (2)
147  ch = rz_cons_readchar();
148 #endif
149  switch (ch) {
150  case '<': {
151  char pos[8] = { 0 };
152  int p = 0;
153  int x = 0;
154  int y = 0;
155  int sc = 0;
156 
157  char vel[8] = { 0 };
158  int vn = 0;
159  do {
160  ch = rz_cons_readchar();
161  // just for debugging
162  // eprintf ( "%c", ch);
163  if (sc > 0) {
164  if (ch >= '0' && ch <= '9') {
165  pos[p++] = ch;
166  }
167  }
168  if (sc < 1) {
169  vel[vn++] = ch;
170  }
171  if (ch == ';') {
172  if (sc == 1) {
173  pos[p++] = 0;
174  x = atoi(pos);
175  }
176  sc++;
177  p = 0;
178  }
179  } while (ch != 'M' && ch != 'm');
180  int nvel = atoi(vel);
181  switch (nvel) {
182  case 2: // right click
183  if (ch == 'M') {
184  return INT8_MAX;
185  }
186  return -INT8_MAX;
187  case 64: // wheel up
188  return 'k';
189  case 65: // wheel down
190  return 'j';
191  }
192  pos[p++] = 0;
193  y = atoi(pos);
194  rz_cons_set_click(x, y);
195  }
196  return 0;
197  case '[':
198  ch = rz_cons_readchar();
199  switch (ch) {
200  case '2': ch = RZ_CONS_KEY_F11; break;
201  case 'A': ch = RZ_CONS_KEY_F1; break;
202  case 'B': ch = RZ_CONS_KEY_F2; break;
203  case 'C': ch = RZ_CONS_KEY_F3; break;
204  case 'D': ch = RZ_CONS_KEY_F4; break;
205  }
206  break;
207  case '9':
208  // handle mouse wheel
209  // __parseWheelEvent();
210  ch = rz_cons_readchar();
211  // 6 is up
212  // 7 is down
213  I->mouse_event = 1;
214  if (ch == '6') {
215  ch = 'k';
216  } else if (ch == '7') {
217  ch = 'j';
218  } else {
219  // unhandled case
220  ch = 0;
221  }
222  int ch2;
223  do {
224  ch2 = rz_cons_readchar();
225  } while (ch2 != 'M');
226  break;
227  case '3':
228  // handle mouse down /up events (35 vs 32)
230  return 0;
231  break;
232  case '2':
233  ch = rz_cons_readchar();
234  switch (ch) {
235  case 0x7e:
236  ch = RZ_CONS_KEY_F12;
237  break;
238  default:
240  switch (ch) {
241  case '0': ch = RZ_CONS_KEY_F9; break;
242  case '1': ch = RZ_CONS_KEY_F10; break;
243  case '3': ch = RZ_CONS_KEY_F11; break;
244  }
245  break;
246  }
247  break;
248  case '1':
249  ch = rz_cons_readchar();
250  switch (ch) {
251  case '1': ch = RZ_CONS_KEY_F1; break;
252  case '2': ch = RZ_CONS_KEY_F2; break;
253  case '3': ch = RZ_CONS_KEY_F3; break;
254  case '4': ch = RZ_CONS_KEY_F4; break;
255  case '5': ch = RZ_CONS_KEY_F5; break;
256  // case '6': ch = RZ_CONS_KEY_F5; break;
257  case '7': ch = RZ_CONS_KEY_F6; break;
258  case '8': ch = RZ_CONS_KEY_F7; break;
259  case '9': ch = RZ_CONS_KEY_F8; break;
260 #if 0
261  case '5':
262  rz_cons_readchar ();
263  ch = 0xf5;
264  break;
265  case '6':
266  rz_cons_readchar ();
267  ch = 0xf7;
268  break;
269  case '7':
270  rz_cons_readchar ();
271  ch = 0xf6;
272  break;
273  case '8':
274  rz_cons_readchar ();
275  ch = 0xf7;
276  break;
277  case '9':
278  rz_cons_readchar ();
279  ch = 0xf8;
280  break;
281 #endif
282  // Support st/st-256color term and others
283  // for shift+arrows
284  case ';': // arrow+mod
285  ch = rz_cons_readchar();
286  switch (ch) {
287  case '2': // arrow+shift
288  ch = rz_cons_readchar();
289  switch (ch) {
290  case 'A': ch = 'K'; break;
291  case 'B': ch = 'J'; break;
292  case 'C': ch = 'L'; break;
293  case 'D': ch = 'H'; break;
294  }
295  break;
296  // add other modifiers
297  }
298  break;
299  case ':': // arrow+shift
301  ch = rz_cons_readchar();
302  switch (ch) {
303  case 'A': ch = 'K'; break;
304  case 'B': ch = 'J'; break;
305  case 'C': ch = 'L'; break;
306  case 'D': ch = 'H'; break;
307  }
308  break;
309  } // F9-F12 not yet supported!!
310  break;
311  case '5':
312  ch = 'K';
314  break; // repag
315  case '6':
316  ch = 'J';
318  break; // avpag
319  /* arrow keys */
320  case 'A': ch = 'k'; break; // up
321  case 'B': ch = 'j'; break; // down
322  case 'C': ch = 'l'; break; // right
323  case 'D':
324  ch = 'h';
325  break; // left
326  // Support rxvt-unicode term for shift+arrows
327  case 'a': ch = 'K'; break; // shift+up
328  case 'b': ch = 'J'; break; // shift+down
329  case 'c': ch = 'L'; break; // shift+right
330  case 'd': ch = 'H'; break; // shift+left
331  case 'M': ch = __parseMouseEvent(); break;
332  }
333  break;
334  }
335  return ch;
336 }
#define ut8
Definition: dcpu16.h:8
static char sc[]
Definition: egg_cb.c:6
static int __parseMouseEvent(void)
Definition: input.c:30
#define I
Definition: input.c:12
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
#define RZ_CONS_KEY_F12
Definition: rz_cons.h:589
#define RZ_CONS_KEY_F11
Definition: rz_cons.h:588
@ RZ_VIRT_TERM_MODE_COMPLETE
All the sequences goes through VT (Windows Terminal, mintty, all OSs)
Definition: rz_cons.h:451
#define RZ_CONS_KEY_F10
Definition: rz_cons.h:587
#define RZ_CONS_KEY_F6
Definition: rz_cons.h:583
#define RZ_CONS_KEY_F7
Definition: rz_cons.h:584
#define RZ_CONS_KEY_F3
Definition: rz_cons.h:580
#define RZ_CONS_KEY_F2
Definition: rz_cons.h:579
#define RZ_CONS_KEY_F1
Definition: rz_cons.h:578
#define RZ_CONS_KEY_F5
Definition: rz_cons.h:582
#define RZ_CONS_KEY_F4
Definition: rz_cons.h:581
#define RZ_CONS_KEY_F9
Definition: rz_cons.h:586
#define RZ_CONS_KEY_F8
Definition: rz_cons.h:585
#define UT8_MAX
#define INT8_MAX
int pos
Definition: main.c:11

References __parseMouseEvent(), I, INT8_MAX, p, pos, RZ_CONS_KEY_F1, RZ_CONS_KEY_F10, RZ_CONS_KEY_F11, RZ_CONS_KEY_F12, RZ_CONS_KEY_F2, RZ_CONS_KEY_F3, RZ_CONS_KEY_F4, RZ_CONS_KEY_F5, RZ_CONS_KEY_F6, RZ_CONS_KEY_F7, RZ_CONS_KEY_F8, RZ_CONS_KEY_F9, rz_cons_readchar(), rz_cons_set_click(), RZ_VIRT_TERM_MODE_COMPLETE, sc, ut8, UT8_MAX, and x.

Referenced by __create_almighty(), __move_panel_to_dir(), __panels_process(), __vi_mode(), insert_mode_enabled(), rz_cons_less_str(), rz_core_visual_analysis(), rz_core_visual_analysis_classes(), rz_core_visual_bit_editor(), rz_core_visual_browse(), rz_core_visual_classes(), rz_core_visual_cmd(), rz_core_visual_colors(), rz_core_visual_comments(), rz_core_visual_config(), rz_core_visual_debugtraces(), rz_core_visual_define(), rz_core_visual_esil(), rz_core_visual_graph(), rz_core_visual_trackflags(), rz_core_visual_view_graph(), rz_core_visual_view_rop(), rz_core_visual_xrefs(), and rz_diff_hex_visual().

◆ rz_cons_controlz()

RZ_API int rz_cons_controlz ( int  ch)

Definition at line 14 of file input.c.

14  {
15 #if __UNIX__
16  if (ch == 0x1a) {
17  rz_cons_show_cursor(true);
18  rz_cons_enable_mouse(false);
19  rz_sys_stop();
20  return 0;
21  }
22 #endif
23  return ch;
24 }
RZ_API bool rz_cons_enable_mouse(const bool enable)
Definition: cons.c:500
RZ_API void rz_cons_show_cursor(int cursor)
Definition: cons.c:1581
RZ_API bool rz_sys_stop(void)
Send SIGTSTP signal to every process in this process group.
Definition: sys.c:1864

References rz_cons_enable_mouse(), rz_cons_show_cursor(), and rz_sys_stop().

Referenced by rz_cons_readchar(), and rz_line_readchar_utf8().

◆ rz_cons_fgets()

RZ_API int rz_cons_fgets ( char *  buf,
int  len,
int  argc,
const char **  argv 
)

Definition at line 339 of file input.c.

339  {
340 #define RETURN(x) \
341  { \
342  ret = x; \
343  goto beach; \
344  }
345  RzCons *cons = rz_cons_singleton();
346  int ret = 0, color = cons->context->pal.input && *cons->context->pal.input;
347  if (cons->echo) {
348  rz_cons_set_raw(false);
349  rz_cons_show_cursor(true);
350  }
351 #if 0
352  int mouse = rz_cons_enable_mouse (false);
353  rz_cons_enable_mouse (false);
354  rz_cons_flush ();
355 #endif
356  errno = 0;
357  if (cons->user_fgets) {
358  RETURN(cons->user_fgets(buf, len, cons->user_fgets_user));
359  }
360  printf("%s", cons->line->prompt);
361  fflush(stdout);
362  *buf = '\0';
363  if (color) {
364  const char *p = cons->context->pal.input;
365  if (RZ_STR_ISNOTEMPTY(p)) {
366  fwrite(p, strlen(p), 1, stdout);
367  fflush(stdout);
368  }
369  }
370  if (!fgets(buf, len, cons->fdin)) {
371  if (color) {
373  fflush(stdout);
374  }
375  RETURN(-1);
376  }
377  if (feof(cons->fdin)) {
378  if (color) {
380  }
381  RETURN(-2);
382  }
384  if (color) {
386  }
387  ret = strlen(buf);
388 beach:
389  // rz_cons_enable_mouse (mouse);
390  return ret;
391 }
size_t len
Definition: 6502dis.c:15
RZ_API void rz_cons_set_raw(bool is_raw)
Definition: cons.c:1617
RZ_API RzCons * rz_cons_singleton(void)
Definition: cons.c:300
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
#define RETURN(x)
voidpf void * buf
Definition: ioapi.h:138
#define Color_RESET
Definition: rz_cons.h:617
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68
RZ_API RZ_BORROW char * rz_str_trim_tail(RZ_NONNULL char *str)
Removes whitespace characters (space, tab, newline etc.) from the end of a string and replaces them w...
Definition: str_trim.c:125
RzConsPrintablePalette pal
Definition: rz_cons.h:491
struct rz_line_t * line
Definition: rz_cons.h:553
int echo
Definition: rz_cons.h:509
void * user_fgets_user
Definition: rz_cons.h:521
RzConsContext * context
Definition: rz_cons.h:502
FILE * fdin
Definition: rz_cons.h:517
int(* user_fgets)(char *buf, int len, void *user)
Definition: rz_cons.h:520
char * prompt
Definition: rz_cons.h:1103
static int color
Definition: visual.c:20

References color, Color_RESET, rz_cons_t::context, rz_cons_t::echo, rz_cons_t::fdin, rz_cons_printable_palette_t::input, len, rz_cons_t::line, p, rz_cons_context_t::pal, printf(), rz_line_t::prompt, RETURN, rz_cons_enable_mouse(), rz_cons_flush(), rz_cons_set_raw(), rz_cons_show_cursor(), rz_cons_singleton(), RZ_STR_ISNOTEMPTY, rz_str_trim_tail(), rz_cons_t::user_fgets, and rz_cons_t::user_fgets_user.

Referenced by __handleComment(), __panel_prompt(), __prompt(), add_comment(), config_visual_hit(), handleHints(), prompt(), prompt_read(), rtr_visual(), rz_cmd_help(), rz_cmd_kuery(), rz_cons_input(), rz_core_prompt(), rz_core_visual_analysis(), rz_core_visual_analysis_classes(), rz_core_visual_bit_editor(), rz_core_visual_classes(), rz_core_visual_cmd(), rz_core_visual_colors(), rz_core_visual_config_hud(), rz_core_visual_define(), rz_core_visual_esil(), rz_core_visual_graph(), rz_core_visual_offset(), rz_core_visual_prompt(), rz_core_visual_trackflags(), rz_core_visual_view_graph(), rz_core_visual_view_rop(), visual_offset(), visual_prompt(), and visual_search().

◆ rz_cons_input()

RZ_API char* rz_cons_input ( const char *  msg)

Definition at line 696 of file input.c.

696  {
697  char *oprompt = rz_line_get_prompt();
698  if (!oprompt) {
699  return NULL;
700  }
701  char buf[1024];
702  if (msg) {
704  } else {
705  rz_line_set_prompt("");
706  }
707  buf[0] = 0;
708  rz_cons_fgets(buf, sizeof(buf), 0, NULL);
709  rz_line_set_prompt(oprompt);
710  free(oprompt);
711  return strdup(buf);
712 }
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API int rz_cons_fgets(char *buf, int len, int argc, const char **argv)
Definition: input.c:339
RZ_API void rz_line_set_prompt(const char *prompt)
Definition: line.c:56
RZ_API RZ_OWN char * rz_line_get_prompt(void)
Definition: line.c:64
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")

References free(), msg, NULL, rz_cons_fgets(), rz_line_get_prompt(), rz_line_set_prompt(), and strdup().

Referenced by __show_status_input(), agraph_follow_innodes(), rz_core_visual_define(), and visual_comma().

◆ rz_cons_readbuffer_readchar()

RZ_API bool rz_cons_readbuffer_readchar ( char *  ch)

Definition at line 609 of file input.c.

609  {
610  if (I->input->readbuffer_length <= 0) {
611  return false;
612  }
613  *ch = *I->input->readbuffer;
614  I->input->readbuffer_length--;
615  memmove(I->input->readbuffer, I->input->readbuffer + 1, I->input->readbuffer_length);
616  return true;
617 }

References I.

Referenced by rz_cons_readchar(), and rz_cons_readchar_timeout().

◆ rz_cons_readchar()

RZ_API int rz_cons_readchar ( void  )

Definition at line 619 of file input.c.

619  {
620  char buf[2], ch;
621  buf[0] = -1;
622  if (rz_cons_readbuffer_readchar(&ch)) {
623  return ch;
624  }
625  rz_cons_set_raw(1);
626 #if __WINDOWS__
627  return __cons_readchar_w32(0);
628 #else
629  void *bed = rz_cons_sleep_begin();
630 
631  // Blocks until either stdin has something to read or a signal happens.
632  // This serves to check if the terminal window was resized. It avoids the race
633  // condition that could happen if we did not use pselect or select in case SIGWINCH
634  // was handled immediately before the blocking call (select or read). The race is
635  // prevented from happening by having SIGWINCH blocked process-wide except for in
636  // pselect (that is what pselect is for).
637  fd_set readfds;
638  sigset_t sigmask;
639  FD_ZERO(&readfds);
641  rz_signal_sigmask(0, NULL, &sigmask);
642  sigdelset(&sigmask, SIGWINCH);
643  while (pselect(STDIN_FILENO + 1, &readfds, NULL, NULL, NULL, &sigmask) == -1) {
644  if (errno == EBADF) {
645  eprintf("rz_cons_readchar (): EBADF\n");
646  return -1;
647  }
648  if (sigwinchFlag) {
649  sigwinchFlag = 0;
650  resizeWin();
651  }
652  }
653 
654  ssize_t ret = read(STDIN_FILENO, buf, 1);
655  rz_cons_sleep_end(bed);
656  if (ret != 1) {
657  return -1;
658  }
659  if (I->input->bufactive) {
660  rz_cons_set_raw(0);
661  }
662  return rz_cons_controlz(buf[0]);
663 #endif
664 }
RZ_API void * rz_cons_sleep_begin(void)
Definition: cons.c:443
RZ_API void rz_cons_sleep_end(void *user)
Definition: cons.c:450
volatile sig_atomic_t sigwinchFlag
RZ_API int rz_cons_controlz(int ch)
Definition: input.c:14
RZ_API bool rz_cons_readbuffer_readchar(char *ch)
Definition: input.c:609
void resizeWin(void)
Definition: cons.c:471
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set readfds
Definition: sflib.h:108
#define eprintf(x, y...)
Definition: rlcc.c:7
#define FD_ZERO(set)
Definition: sftypes.h:204
#define FD_SET(d, set)
Definition: sftypes.h:212
#define EBADF
Definition: sftypes.h:119
int ssize_t
Definition: sftypes.h:39
int sigset_t
Definition: sftypes.h:63
#define STDIN_FILENO
Definition: private.h:37
#define SIGWINCH
Definition: win.h:88
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115

References EBADF, eprintf, FD_SET, FD_ZERO, I, NULL, read(), readfds, resizeWin(), rz_cons_controlz(), rz_cons_readbuffer_readchar(), rz_cons_set_raw(), rz_cons_sleep_begin(), rz_cons_sleep_end(), SIGWINCH, sigwinchFlag, and STDIN_FILENO.

Referenced by __create_almighty(), __handle_tab(), __handle_visual_mark(), __panels_process(), __parseMouseEvent(), __show_status(), __vi_mode(), agraph_follow_innodes(), goto_asmqjmps(), insert_mode_enabled(), rtr_visual(), rz_cons_any_key(), rz_cons_arrow_to_hjkl(), rz_cons_less_str(), rz_cons_readchar_timeout(), rz_core_visual(), rz_core_visual_analysis(), rz_core_visual_analysis_classes(), rz_core_visual_bit_editor(), rz_core_visual_browse(), rz_core_visual_classes(), rz_core_visual_cmd(), rz_core_visual_colors(), rz_core_visual_comments(), rz_core_visual_config(), rz_core_visual_debugtraces(), rz_core_visual_define(), rz_core_visual_esil(), rz_core_visual_graph(), rz_core_visual_trackflags(), rz_core_visual_view_graph(), rz_core_visual_view_rop(), rz_core_visual_xrefs(), rz_diff_hex_visual(), rz_line_readchar_utf8(), rz_line_readline_cb(), and visual_help().

◆ rz_cons_readchar_timeout()

RZ_API int rz_cons_readchar_timeout ( ut32  usec)

Definition at line 560 of file input.c.

560  {
561  char ch;
562  if (rz_cons_readbuffer_readchar(&ch)) {
563  return ch;
564  }
565 #if __UNIX__
566  struct timeval tv;
567  fd_set fdset, errset;
568  FD_ZERO(&fdset);
569  FD_ZERO(&errset);
570  FD_SET(0, &fdset);
571  tv.tv_sec = 0; // usec / 1000;
572  tv.tv_usec = 1000 * usec;
573  rz_cons_set_raw(1);
574  if (select(1, &fdset, NULL, &errset, &tv) == 1) {
575  return rz_cons_readchar();
576  }
577  rz_cons_set_raw(0);
578  // timeout
579  return -1;
580 #else
581  return __cons_readchar_w32(usec);
582 #endif
583 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval tv
Definition: sflib.h:79
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz select
Definition: sflib.h:108

References FD_SET, FD_ZERO, NULL, rz_cons_readbuffer_readchar(), rz_cons_readchar(), rz_cons_set_raw(), select, and tv.

Referenced by rz_line_readline_cb().

◆ rz_cons_readflush()

RZ_API void rz_cons_readflush ( void  )

Definition at line 596 of file input.c.

596  {
597  RZ_FREE(I->input->readbuffer);
598  I->input->readbuffer_length = 0;
599 }
#define RZ_FREE(x)
Definition: rz_types.h:369

References I, and RZ_FREE.

◆ rz_cons_readpush()

RZ_API bool rz_cons_readpush ( const char *  str,
int  len 
)

Definition at line 585 of file input.c.

585  {
586  char *res = (len + I->input->readbuffer_length > 0) ? realloc(I->input->readbuffer, len + I->input->readbuffer_length) : NULL;
587  if (res) {
588  I->input->readbuffer = res;
589  memmove(I->input->readbuffer + I->input->readbuffer_length, str, len);
590  I->input->readbuffer_length += len;
591  return true;
592  }
593  return false;
594 }
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144

References I, len, NULL, realloc(), and cmd_descs_generate::str.

Referenced by rz_core_visual(), and rz_push_escaped_handler().

◆ rz_cons_switchbuf()

RZ_API void rz_cons_switchbuf ( bool  active)

Definition at line 601 of file input.c.

601  {
602  I->input->bufactive = active;
603 }

References I.

Referenced by __handle_console(), __handle_menu(), __handle_tab_key(), __handle_window_mode(), __handle_zoom_mode(), and __panels_process().

◆ rz_cons_yesno()

RZ_API bool rz_cons_yesno ( int  def,
const char *  fmt,
  ... 
)

Definition at line 666 of file input.c.

666  {
667  va_list ap;
668  ut8 key = (ut8)def;
669  va_start(ap, fmt);
670 
671  if (!rz_cons_is_interactive()) {
672  va_end(ap);
673  return def == 'y';
674  }
675  vfprintf(stderr, fmt, ap);
676  va_end(ap);
677  fflush(stderr);
678  rz_cons_set_raw(true);
679  char buf[] = " ?\n";
680  if (read(0, buf + 1, 1) == 1) {
681  key = (ut8)buf[1];
682  if (write(2, buf, 3) == 3) {
683  if (key == 'Y') {
684  key = 'y';
685  }
686  rz_cons_set_raw(false);
687  if (key == '\n' || key == '\r') {
688  key = def;
689  }
690  return key == 'y';
691  }
692  }
693  return false;
694 }
RZ_API bool rz_cons_is_interactive(void)
Definition: cons.c:365
static static fork write
Definition: sflib.h:33
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
int def(FILE *source, FILE *dest, int level)
Definition: zpipe.c:36

References def(), key, read(), rz_cons_is_interactive(), rz_cons_set_raw(), ut8, and write.

Referenced by __show_status_yesno(), agraph_refresh(), check_function_modified(), DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), rz_cmd_help(), rz_cons_flush(), rz_core_cmd_subst(), rz_core_visual_asm(), rz_core_visual_graph(), rz_main_rizin(), rz_w32_identify_window(), and w32_list_heaps_blocks().

Variable Documentation

◆ sigwinchFlag

volatile sig_atomic_t sigwinchFlag
extern

Referenced by rz_cons_readchar().