Rizin
unix-like reverse engineering framework and cli tools
rtr_shell.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2019 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 static bool rtr_visual(RzCore *core, TextLog T, const char *cmd) {
5  bool autorefresh = false;
6  if (cmd) {
8  for (;;) {
9  char *ret;
11  ret = rtrcmd(T, cmd);
12  rz_cons_println(ret);
13  free(ret);
14  rz_cons_flush();
15  if (rz_cons_is_breaked()) {
16  break;
17  }
18  rz_sys_sleep(1);
19  }
21  } else {
22  const char *cmds[] = { "px", "pd", "pxa", "dr", "sr SP;pxa", NULL };
23  int cmdidx = 0;
24  char *ret, ch;
25  free(rtrcmd(T, "e scr.color=true"));
26  free(rtrcmd(T, "e scr.html=false"));
27  for (;;) {
29  ret = rtrcmd(T, cmds[cmdidx]);
30  if (ret) {
31  rz_cons_println(ret);
32  free(ret);
33  }
34  rz_cons_flush();
35  if (autorefresh) {
36  rz_cons_printf("(auto-refresh)\n");
37  rz_cons_flush();
39  rz_sys_sleep(1);
40  if (rz_cons_is_breaked()) {
41  autorefresh = false;
42  ch = rz_cons_readchar();
43  } else {
45  continue;
46  }
48  } else {
49  ch = rz_cons_readchar();
50  }
51 #if 0
52 TODO:
53  i insert hex/string/asm
54  0-9 follow jumps
55 #endif
56  switch (ch) {
57  case '?':
59  rz_cons_printf("Remote Visual keys:\n"
60  " hjkl : move\n"
61  " HJKL : move faster\n"
62  " +-*/ : change block size\n"
63  " pP : rotate print modes\n"
64  " T : enter TextLog chat console\n"
65  " @ : enter auto-refresh mode\n"
66  " i : insert hexpair\n"
67  " q : quit this mode and go back to the shell\n"
68  " sS : step / step over\n"
69  " . : seek entry or pc\n");
70  rz_cons_flush();
72  break;
73  case 'i': {
74 #if __UNIX__
75 #define COLORFLAGS (core->print->flags & RZ_PRINT_FLAGS_COLOR)
76 #else
77 #define COLORFLAGS 0
78 #endif
79  char buf[1024];
80  if (COLORFLAGS) {
82  } else {
83  rz_line_set_prompt(":> ");
84  }
85  showcursor(core, true);
86  rz_cons_fgets(buf + 3, sizeof(buf) - 3, 0, NULL);
87  memcpy(buf, "wx ", 3);
88  if (buf[3]) {
89  char *res = rtrcmd(T, buf);
90  if (res) {
91  rz_cons_println(res);
92  free(res);
93  }
94  rz_cons_flush();
95  }
96  } break;
97  case 's':
98  free(rtrcmd(T, "ds;.dr*"));
99  break;
100  case 'S':
101  free(rtrcmd(T, "dso;.dr*"));
102  break;
103  case '.':
104  free(rtrcmd(T, "s entry0;dr?rip;?? sr PC"));
105  break;
106  case ':': {
107  int ret;
108  eprintf("Press <enter> to return to Visual mode.\n");
109  do {
110  char buf[1024];
111 #if __UNIX__
113 #else
114  rz_line_set_prompt(":> ");
115 #endif
116  showcursor(core, true);
117  rz_cons_fgets(buf, sizeof(buf), 0, NULL);
118  if (*buf) {
120  char *res = rtrcmd(T, buf);
121  if (res) {
122  rz_cons_println(res);
123  free(res);
124  }
125  rz_cons_flush();
126  ret = true;
127  } else {
128  ret = false;
129  // rz_cons_any_key ();
130  rz_cons_clear00();
131  showcursor(core, false);
132  }
133  } while (ret);
134  } break;
135  case '@': autorefresh = true; break;
136  case 'j':
137  if (cmdidx == 1) {
138  free(rtrcmd(T, "so"));
139  break;
140  } else {
141  free(rtrcmd(T, "sd +16"));
142  break;
143  }
144  break;
145  case 'k': free(rtrcmd(T, "sd -16")); break;
146  case 'h': free(rtrcmd(T, "sd -1")); break;
147  case 'l': free(rtrcmd(T, "sd +1")); break;
148  case 'J':
149  if (cmdidx == 1) {
150  free(rtrcmd(T, "4so"));
151  } else {
152  free(rtrcmd(T, "sd +32"));
153  }
154  break;
155  case 'K': free(rtrcmd(T, "sd -32")); break;
156  case 'H': free(rtrcmd(T, "sd -2")); break;
157  case 'L': free(rtrcmd(T, "sd +2")); break;
158  case '+': free(rtrcmd(T, "b+1")); break;
159  case '*': free(rtrcmd(T, "b+16")); break;
160  case '-': free(rtrcmd(T, "b-1")); break;
161  case '/': free(rtrcmd(T, "b-16")); break;
162  case 'p':
163  cmdidx++;
164  if (!cmds[cmdidx]) {
165  cmdidx = 0;
166  }
167  break;
168  case 'P':
169  cmdidx--;
170  if (cmdidx < 0) {
171  cmdidx = 2;
172  }
173  break;
174  case 'q': return false;
175  }
176  }
177  }
178  return true;
179 }
180 
181 // XXX: this needs to be moved to use the standard shell like in !=! and support visual+panels
182 static void __rtr_shell(RzCore *core, int nth) {
183  char *proto = "http";
184  char *host = "";
185  char *port = "";
186  char *file = "";
187  char prompt[64], prompt2[64], *str, *ptr;
188  int len;
189  const char *res;
190  RzSocket *s = NULL;
191 
192  TextLog T = { host, port, file };
193  snprintf(prompt, sizeof(prompt), "[%s://%s:%s/%s]> ",
194  proto, host, port, file);
195  snprintf(prompt2, sizeof(prompt2), "[%s:%s]$ ", host, port);
196  for (;;) {
198  res = rz_line_readline();
199  if (!res || !*res) {
200  break;
201  }
202  if (*res == 'q') {
203  break;
204  }
205  if (!strcmp(res, "!sh")) {
206  for (;;) {
207  rz_line_set_prompt(prompt2);
208  res = rz_line_readline();
209  if (!res || !*res || !strcmp(res, "exit")) {
210  break;
211  }
212  ptr = rz_str_uri_encode(res);
213  char *uri = rz_str_newf("http://%s:%s/%s!%s", host, port, file, res);
214  str = rz_socket_http_get(uri, NULL, &len);
215  if (str) {
216  str[len] = 0;
217  res = strstr(str, "\n\n");
218  if (res) {
219  res = strstr(res + 1, "\n\n");
220  }
221  res = res ? res + 2 : str;
222  const char *tail = (res[strlen(res) - 1] == '\n') ? "" : "\n";
223  printf("%s%s", res, tail);
225  free(str);
226  }
227  free(ptr);
228  free(uri);
229  }
230  } else if (res[0] == 'v' || res[0] == 'V') {
231  if (res[1] == ' ') {
232  rtr_visual(core, T, res + 1);
233  } else {
234  rtr_visual(core, T, NULL);
235  }
236  } else {
237  char *cmdline = rz_str_newf("%d %s", nth, res);
238  rz_core_rtr_cmd(core, cmdline);
239  rz_cons_flush();
240  rz_line_hist_add(res);
241  }
242  }
243  rz_socket_free(s);
244 }
size_t len
Definition: 6502dis.c:15
#define T(op)
static void showcursor(RzCore *core, int x)
Definition: agraph.c:188
lzma_index ** i
Definition: index.h:629
RZ_API void rz_cons_clear00(void)
Definition: cons.c:778
RZ_API void rz_cons_break_pop(void)
Definition: cons.c:361
RZ_API void rz_cons_break_push(RzConsBreak cb, void *user)
Definition: cons.c:357
RZ_API int rz_cons_printf(const char *format,...)
Definition: cons.c:1202
RZ_API void rz_cons_flush(void)
Definition: cons.c:959
RZ_API bool rz_cons_is_breaked(void)
Definition: cons.c:373
RZ_API void rz_cons_println(const char *str)
Definition: cons.c:233
#define NULL
Definition: cris-opc.c:27
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
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 cmd
Definition: sflib.h:79
RZ_API const char * rz_line_readline(void)
Definition: dietline.c:913
RZ_API int rz_line_hist_add(const char *line)
Definition: dietline.c:382
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API int rz_cons_any_key(const char *msg)
Definition: input.c:393
RZ_API int rz_cons_fgets(char *buf, int len, int argc, const char **argv)
Definition: input.c:339
RZ_API int rz_cons_readchar(void)
Definition: input.c:619
voidpf void * buf
Definition: ioapi.h:138
snprintf
Definition: kernel.h:364
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
RZ_API void rz_line_set_prompt(const char *prompt)
Definition: line.c:56
static void insert(char *dst, const char *src)
Definition: filter.c:39
static const char hex[16]
Definition: print.c:21
#define eprintf(x, y...)
Definition: rlcc.c:7
static RzSocket * s
Definition: rtr.c:28
static char * rtrcmd(TextLog T, const char *str)
Definition: rtr.c:81
RZ_API void rz_core_rtr_cmd(RzCore *core, const char *input)
Definition: rtr.c:850
static void __rtr_shell(RzCore *core, int nth)
Definition: rtr_shell.c:182
#define COLORFLAGS
static bool rtr_visual(RzCore *core, TextLog T, const char *cmd)
Definition: rtr_shell.c:4
#define Color_RESET
Definition: rz_cons.h:617
RZ_API char * rz_socket_http_get(const char *url, int *code, int *rlen)
Definition: socket_http.c:287
RZ_API int rz_socket_free(RzSocket *s)
Definition: socket.c:453
RZ_API char * rz_str_uri_encode(const char *buf)
Definition: str.c:2860
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API int rz_sys_sleep(int secs)
Sleep for secs seconds.
Definition: sys.c:300
Definition: rtr.c:40
Definition: gzappend.c:170
static char * prompt(const char *str, const char *txt)
Definition: vmenus.c:30