Move non-native RLang plugins into r2-bindings

This commit is contained in:
pancake 2013-12-14 02:19:00 +01:00
parent 2620f22e9b
commit c3f57f233f
18 changed files with 87 additions and 84 deletions

View File

@ -6,6 +6,7 @@
R_LIB_VERSION(r_lang);
#include "p/vala.c" // hardcoded
#include "p/c.c" // hardcoded
RLang *__lang = NULL;
@ -14,7 +15,6 @@ R_API void r_lang_plugin_free (RLangPlugin *p) {
p->fini (__lang);
}
R_API RLang *r_lang_new() {
RLang *lang = R_NEW (RLang);
if (lang) {
@ -23,6 +23,7 @@ R_API RLang *r_lang_new() {
lang->langs->free = (RListFree)r_lang_plugin_free;
lang->defs = r_list_new ();
lang->defs->free = (RListFree)r_lang_def_free;
r_lang_add (lang, &r_lang_plugin_c);
r_lang_add (lang, &r_lang_plugin_vala);
}
return lang;

View File

@ -1,64 +1 @@
BINDEPS=foo
include ../../config.mk
CFLAGS+=-I../../include -Wall -DPREFIX=\"${PREFIX}\"
ifeq ($(OSTYPE),darwin)
CFLAGS+=-undefined dynamic_lookup
endif
LUAPKG=$(shell pkg-config --list-all|awk '/lua-/{print $$1;}')
ifneq (${LUAPKG},)
CFLAGS+=$(shell pkg-config --cflags ${LUAPKG})
LUA_LDFLAGS+=$(shell pkg-config --libs ${LUAPKG})
endif
BINDEPS=
LANGS=$(shell ./getlangs.sh ${EXT_SO})
#LANGS=lang_python.${EXT_SO} lang_perl.${EXT_SO}
#LANGS+=lang_ruby.so
ifeq ($(HAVE_LIB_TCC),1)
LANGS+=lang_tcc.${EXT_SO}
endif
ifeq ($(HAVE_LIB_LUA5_1),1)
LANGS+=lang_lua.${EXT_SO}
endif
all: ${LANGS}
@echo "LANG ${LANGS}"
ifeq ($(OSTYPE),windows)
lang_python.${EXT_SO}:
${CC} ${CFLAGS} -I${HOME}/.wine/drive_c/Python27/include \
-L${HOME}/.wine/drive_c/Python27/libs -L../../core/ -lr_core \
${LDFLAGS_LIB} -shared -o lang_python.${EXT_SO} python.c -lpython27
else
PYCFG=../../../r2-bindings/python-config-wrapper
PYCFLAGS=$(shell ${PYCFG} --cflags)
PYLDFLAGS=$(shell ${PYCFG} --libs) -L$(shell ${PYCFG} --prefix)/lib
lang_python.${EXT_SO}:
${CC} python.c ${CFLAGS} ${PYCFLAGS} ${PYLDFLAGS} \
${LDFLAGS} ${LDFLAGS_LIB} -fPIC -o lang_python.${EXT_SO}
endif
ifeq ($(HAVE_LIB_TCC),1)
lang_tcc.${EXT_SO}: tcc.o
-${CC} ${CFLAGS} -fPIC ${LDFLAGS_LIB} -o lang_tcc.${EXT_SO} tcc.c ${LDFLAGS_LINKPATH}.. -ldl -ltcc
endif
lang_lua.${EXT_SO}: lua.o
-${CC} ${CFLAGS} -fPIC ${LDFLAGS_LIB} -o lang_lua.${EXT_SO} lua.c ${LDFLAGS_LINKPATH}.. ${LUA_LDFLAGS}
lang_ruby.${EXT_SO}:
-env CFLAGS="${CFLAGS}" ruby mkruby.rb
lang_perl.${EXT_SO}:
-${CC} ${CFLAGS} -I/usr/lib/perl/5.10/CORE/ \
-fPIC ${LDFLAGS_LIB} -o lang_perl.${EXT_SO} perl.c ${LDFLAGS_LINKPATH}.. \
`perl -MExtUtils::Embed -e ccopts | sed -e 's/-arch [^\s]* //g'` \
`perl -MExtUtils::Embed -e ldopts | sed -e 's/-arch [^\s]* //g'` -lncurses
mrproper clean:
-rm -f *.${EXT_SO} *.${EXT_AR} *.o
-rm -rf *.dSYM
all:

71
libr/lang/p/c.c Normal file
View File

@ -0,0 +1,71 @@
/* radare - LGPL - Copyright 2011-2013 pancake */
/* vala extension for libr (radare2) */
// TODO: add cache directory (~/.r2/cache)
#include "r_lib.h"
#include "r_core.h"
#include "r_lang.h"
static int lang_c_file(RLang *lang, const char *file) {
void *lib;
char *p, name[512], buf[512];
if (!strstr (file, ".c"))
sprintf (name, "%s.c", file);
else strcpy (name, file);
if (!r_file_exists (name)) {
eprintf ("file not found (%s)\n", name);
return R_FALSE;
}
if (system (buf) != 0)
return R_FALSE;
p = strstr (name, ".c"); if (p) *p=0;
// TODO: use CC environ if possible
snprintf (buf, sizeof (buf), "gcc -fPIC -shared %s -o lib%s."R_LIB_EXT
" $(pkg-config --cflags --libs r_core)", file, name);
if (system (buf) != 0)
return R_FALSE;
snprintf (buf, sizeof (buf), "./lib%s."R_LIB_EXT, name);
lib = r_lib_dl_open (buf);
if (lib!= NULL) {
void (*fcn)(RCore *);
fcn = r_lib_dl_sym (lib, "entry");
if (fcn) fcn (lang->user);
else eprintf ("Cannot find 'entry' symbol in library\n");
r_lib_dl_close (lib);
} else eprintf ("Cannot open library\n");
r_file_rm (buf); // remove lib
return 0;
}
static int lang_c_init(void *user) {
// TODO: check if "valac" is found in path
return R_TRUE;
}
static int lang_c_run(RLang *lang, const char *code, int len) {
FILE *fd = fopen (".tmp.c", "w");
if (fd) {
fputs ("#include <r_core.h>\n\nvoid entry(RCore *core) {\n", fd);
fputs (code, fd);
fputs (";\n}\n", fd);
fclose (fd);
lang_c_file (lang, ".tmp.c");
r_file_rm (".tmp.c");
} else eprintf ("Cannot open .tmp.c\n");
return R_TRUE;
}
static struct r_lang_plugin_t r_lang_plugin_c = {
.name = "C",
.ext = "c",
.desc = "C language extension",
.help = NULL,
.run = lang_c_run,
.init = (void*)lang_c_init,
.fini = NULL,
.run_file = (void*)lang_c_file,
.set_argv = NULL,
};

View File

@ -1,13 +1,12 @@
/* radare - LGPL - Copyright 2011-2012 pancake<nopcode.org> */
/* radare - LGPL - Copyright 2011-2013 pancake */
/* vala extension for libr (radare2) */
// TODO: add support for Genie
// TODO: add cache directory (~/.r2/cache)
#include "r_lib.h"
#include "r_core.h"
#include "r_lang.h"
static int r_vala_file(RLang *lang, const char *file) {
static int lang_vala_file(RLang *lang, const char *file) {
void *lib;
char *p, name[512], buf[512];
char *vapidir;
@ -32,6 +31,7 @@ static int r_vala_file(RLang *lang, const char *file) {
return R_FALSE;
p = strstr (name, ".vala"); if (p) *p=0;
p = strstr (name, ".gs"); if (p) *p=0;
// TODO: use CC environ if possible
snprintf (buf, sizeof (buf), "gcc -fPIC -shared %s.c -o lib%s."R_LIB_EXT
" $(pkg-config --cflags --libs r_core gobject-2.0)", name, name);
if (system (buf) != 0)
@ -52,19 +52,19 @@ static int r_vala_file(RLang *lang, const char *file) {
return 0;
}
static int init(void *user) {
static int lang_vala_init(void *user) {
// TODO: check if "valac" is found in path
return R_TRUE;
}
static int vala_run(RLang *lang, const char *code, int len) {
static int lang_vala_run(RLang *lang, const char *code, int len) {
FILE *fd = fopen (".tmp.vala", "w");
if (fd) {
fputs ("using Radare;\n\npublic static void entry(RCore core) {\n", fd);
fputs (code, fd);
fputs (";\n}\n", fd);
fclose (fd);
r_vala_file (lang, ".tmp.vala");
lang_vala_file (lang, ".tmp.vala");
r_file_rm (".tmp.vala");
} else eprintf ("Cannot open .tmp.vala\n");
return R_TRUE;
@ -75,16 +75,9 @@ static struct r_lang_plugin_t r_lang_plugin_vala = {
.ext = "vala",
.desc = "VALA language extension",
.help = NULL,
.run = vala_run,
.init = (void*)init,
.run = lang_vala_run,
.init = (void*)lang_vala_init,
.fini = NULL,
.run_file = (void*)r_vala_file,
.run_file = (void*)lang_vala_file,
.set_argv = NULL,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_LANG,
.data = &r_lang_plugin_vala,
};
#endif

View File

@ -120,6 +120,7 @@ io.ptrace
io.procpid
io.shm
io.zip
lang.vala
parse.mreplace
parse.att2intel
parse.mips_pseudo

View File

@ -1,12 +1,11 @@
/* tcc extension for libr (radare2) */
/* tcc extension for libr (radare2) - copyright pancake 2011-2013 */
#include "r_lib.h"
#include "r_lang.h"
#include <libtcc.h>
/* TODO: store the state globally or so.. */
static int r_lang_tcc_run(struct r_lang_t *lang, const char *code, int len)
{
static int r_lang_tcc_run(struct r_lang_t *lang, const char *code, int len) {
TCCState *ts = tcc_new ();
/* TODO: set defined vars as global */
//list_for_each(lang->defs) {

View File

@ -31,4 +31,5 @@ if [ "$1" = "-n" ]; then
fi
${PYCFG} $@ | sed -e 's/-arch [^\s]*//g' | \
sed 's, s ,,g' | \
sed s,-Wstrict-prototypes,,g 2>/dev/null