Rizin
unix-like reverse engineering framework and cli tools
rizin.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2020 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_main.h>
5 #include <rz_util.h>
6 
7 static void rz_cmd(int in, int out, const char *cmd) {
8  rz_xwrite(out, cmd, strlen(cmd) + 1);
9  rz_xwrite(out, "\n", 1);
10  int bufsz = (1024 * 64);
11  unsigned char *buf = malloc(bufsz);
12  if (!buf) {
13  return;
14  }
15  while (1) {
16  rz_xread(in, buf, bufsz);
17  buf[bufsz - 1] = '\0';
18  int len = strlen((const char *)buf);
19  if (len < 1) {
20  break;
21  }
22  rz_xwrite(1, buf, len);
23  if (len != bufsz) {
24  break;
25  }
26  }
27  free(buf);
28  rz_xwrite(1, "\n", 1);
29 }
30 
31 static int rz_main_rzpipe(int argc, const char **argv) {
32  int i, rc = 0;
33  char *_in = rz_sys_getenv("RZ_PIPE_IN");
34  char *_out = rz_sys_getenv("RZ_PIPE_OUT");
35  if (_in && _out) {
36  int in = atoi(_in);
37  int out = atoi(_out);
38  for (i = 1; i < argc; i++) {
39  rz_cmd(in, out, argv[i]);
40  }
41  } else {
42  eprintf("Error: RZ_PIPE_(IN|OUT) environment not set\n");
43  eprintf("Usage: rizin -c '!*rzp x' # run commands via rzpipe\n");
44  rc = 1;
45  }
46  free(_in);
47  free(_out);
48  return rc;
49 }
50 
51 int MAIN_NAME(int argc, const ARGV_TYPE **argv) {
52  char **utf8_argv = ARGV_TYPE_TO_UTF8(argc, argv);
53  int ret;
54  if (argc > 0 && strstr(utf8_argv[0], "rzp")) {
55  ret = rz_main_rzpipe(argc, (const char **)utf8_argv);
56  } else {
57  ret = rz_main_rizin(argc, (const char **)utf8_argv);
58  }
59  FREE_UTF8_ARGV(argc, utf8_argv);
60  return ret;
61 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
static void rz_cmd(int in, int out, const char *cmd)
Definition: rizin.c:7
int MAIN_NAME(int argc, const ARGV_TYPE **argv)
Definition: rizin.c:51
static int rz_main_rzpipe(int argc, const char **argv)
Definition: rizin.c:31
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
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 void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
RZ_API int rz_main_rizin(int argc, const char **argv)
Definition: rizin.c:370
void * malloc(size_t size)
Definition: malloc.c:123
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
#define eprintf(x, y...)
Definition: rlcc.c:7
#define ARGV_TYPE_TO_UTF8(argc, argv)
Definition: rz_main.h:30
#define FREE_UTF8_ARGV(argc, utf8_argv)
Definition: rz_main.h:31
#define ARGV_TYPE
Definition: rz_main.h:29
RZ_API char * rz_sys_getenv(const char *key)
Get the value of an environment variable named key or NULL if none exists.
Definition: sys.c:483
#define rz_xread(fd, buf, count)
Definition: rz_types.h:643
#define rz_xwrite(fd, buf, count)
Definition: rz_types.h:642