Rizin
unix-like reverse engineering framework and cli tools
cpipe.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2019 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
8 #include <rz_lib.h>
9 #include <rz_core.h>
10 #include <rz_lang.h>
11 #include "pipe_helper.h"
12 
13 #if __UNIX__
14 static int lang_cpipe_file(RzLang *lang, const char *file) {
15  char *a, *cc, *p, name[512];
16  const char *libpath, *libname;
17 
18  if (strlen(file) > (sizeof(name) - 10)) {
19  return false;
20  }
21  if (!strstr(file, ".c")) {
22  sprintf(name, "%s.c", file);
23  } else {
24  strcpy(name, file);
25  }
26  if (!rz_file_exists(name)) {
27  eprintf("file not found (%s)\n", name);
28  return false;
29  }
30 
31  a = (char *)rz_str_lchr(name, '/');
32  if (a) {
33  *a = 0;
34  libpath = name;
35  libname = a + 1;
36  } else {
37  libpath = ".";
38  libname = name;
39  }
40  p = strstr(name, ".c");
41  if (p) {
42  *p = 0;
43  }
44  cc = rz_sys_getenv("CC");
45  if (RZ_STR_ISEMPTY(cc)) {
46  free(cc);
47  cc = strdup("gcc");
48  }
49  char *libdir = rz_path_libdir();
50  char *pkgconf_path = rz_file_path_join(libdir, "pkgconfig");
51  char *file_esc = rz_str_escape_sh(file);
52  char *libpath_esc = rz_str_escape_sh(libpath);
53  char *libname_esc = rz_str_escape_sh(libname);
54  char *buf = rz_str_newf("%s \"%s\" -o \"%s/bin%s\""
55  " $(PKG_CONFIG_PATH=%s pkg-config --cflags --libs rz_socket)",
56  cc, file_esc, libpath_esc, libname_esc, pkgconf_path);
57  free(libname_esc);
58  free(libpath_esc);
59  free(file_esc);
60  free(pkgconf_path);
61  free(cc);
62  if (rz_sys_system(buf) == 0) {
63  char *o_ld_path = rz_sys_getenv("LD_LIBRARY_PATH");
64  rz_sys_setenv("LD_LIBRARY_PATH", libdir);
65  char *binfile = rz_str_newf("%s/bin%s", libpath, libname);
66  lang_pipe_run(lang, binfile, -1);
67  rz_file_rm(binfile);
68  rz_sys_setenv("LD_LIBRARY_PATH", o_ld_path);
69  free(o_ld_path);
70  free(binfile);
71  }
72  free(libdir);
73  free(buf);
74  return 0;
75 }
76 
77 static int lang_cpipe_init(void *user) {
78  // TODO: check if C compiler is found in path
79  return true;
80 }
81 
82 static int lang_cpipe_run(RzLang *lang, const char *code, int len) {
83  FILE *fd = rz_sys_fopen(".tmp.c", "w");
84  if (!fd) {
85  eprintf("Cannot open .tmp.c\n");
86  return false;
87  }
88  fputs("#include <rz_socket.h>\n\n"
89  "#define RZP(x,y...) rzpipe_cmdf(rzp,x,##y)\n"
90  "int main() {\n"
91  " RzPipe *rzp = rzpipe_open(NULL);",
92  fd);
93  fputs(code, fd);
94  fputs("\n}\n", fd);
95  fclose(fd);
96  lang_cpipe_file(lang, ".tmp.c");
97  rz_file_rm(".tmp.c");
98  return true;
99 }
100 
101 RzLangPlugin rz_lang_plugin_cpipe = {
102  .name = "cpipe",
103  .ext = "c2",
104  .desc = "rzpipe scripting in C",
105  .license = "LGPL",
106  .run = lang_cpipe_run,
107  .init = (void *)lang_cpipe_init,
108  .fini = NULL,
109  .run_file = (void *)lang_cpipe_file,
110 };
111 
112 #ifndef RZ_PLUGIN_INCORE
115  .data = &rz_lang_plugin_cpipe,
116  .version = RZ_VERSION
117 };
118 #endif
119 
120 #else
121 #ifdef _MSC_VER
122 #pragma message("Warning: cpipe RzLangPlugin is not implemented on this platform")
123 #else
124 #warning cpipe RzLangPlugin is not implemented on this platform
125 #endif
126 #endif
size_t len
Definition: 6502dis.c:15
RZ_API RzLibStruct rizin_plugin
static bool fini(void *user)
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
sprintf
Definition: kernel.h:365
void * p
Definition: libc.cpp:67
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")
string FILE
Definition: benchmark.py:21
const char * name
Definition: op.c:541
RZ_IPI int lang_pipe_run(RzLang *lang, const char *code, int len)
Definition: pipe_helper.c:132
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API bool rz_file_exists(const char *str)
Definition: file.c:192
RZ_API bool rz_file_rm(const char *file)
Definition: file.c:865
RZ_API RZ_OWN char * rz_file_path_join(RZ_NONNULL const char *s1, RZ_NULLABLE const char *s2)
Concatenate two paths to create a new one with s1+s2 with the correct path separator.
Definition: file.c:1312
@ RZ_LIB_TYPE_LANG
Definition: rz_lib.h:71
RZ_API RZ_OWN char * rz_path_libdir(void)
Return the directory where the Rizin libraries are placed.
Definition: path.c:155
RZ_API const char * rz_str_lchr(const char *str, char chr)
Definition: str.c:669
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
RZ_API char * rz_str_escape_sh(const char *buf)
Definition: str.c:1560
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
RZ_API int rz_sys_setenv(const char *key, const char *value)
Set an environment variable in the calling process.
Definition: sys.c:405
RZ_API FILE * rz_sys_fopen(const char *path, const char *mode)
Definition: sys.c:1815
RZ_API int rz_sys_system(const char *command)
Definition: sys.c:1658
#define RZ_VERSION
Definition: rz_version.h:8
#define a(i)
Definition: sha256.c:41
Definition: inftree9.h:24
Definition: gzappend.c:170
Definition: z80asm.h:102
const char * name
Definition: rz_lang.h:28
static const z80_opcode fd[]
Definition: z80_tab.h:997