1 /* radare - LPGL - Copyright 2017-2020 condret */
2
3 #include <r_lib.h>
4 #include <r_core.h>
5 #include <r_lang.h>
6
lang_lib_init(RLang * user)7 static bool lang_lib_init(RLang *user) {
8 return true;
9 }
10
lang_lib_file_run(RLang * user,const char * file)11 static bool lang_lib_file_run(RLang *user, const char *file) {
12 char *libpath;
13 void *lib;
14 if (!(libpath = r_str_new (file))) {
15 return false;
16 }
17 if (!r_str_startswith (libpath, "/") && !r_str_startswith (libpath, "./")) {
18 libpath = r_str_prepend (libpath, "./");
19 }
20 if (!r_file_exists (libpath)) {
21 if (!r_str_endswith (libpath, R_LIB_EXT)) {
22 libpath = r_str_appendf (libpath, ".%s", R_LIB_EXT);
23 }
24 }
25 if (!r_file_exists (libpath)) {
26 free (libpath);
27 return false;
28 }
29
30 lib = r_lib_dl_open (libpath);
31 if (lib) {
32 void (*fcn)(RCore *);
33 fcn = r_lib_dl_sym (lib, "entry");
34 if (fcn) {
35 fcn (user->user);
36 } else {
37 eprintf ("Cannot find 'entry' symbol in library\n");
38 }
39 r_lib_dl_close (lib);
40 }
41 free (libpath);
42 return true;
43 }
44
45 static RLangPlugin r_lang_plugin_lib = {
46 .name = "lib",
47 .ext = R_LIB_EXT,
48 .desc = "Load libs directly into r2",
49 .license = "LGPL",
50 .init = lang_lib_init,
51 .run_file = lang_lib_file_run,
52 };
53