* Initial import of the ruby plugin for r_lang

- Imported mkruby and chkruby from r1
  - Some minor changes in r_lang for it
This commit is contained in:
pancake 2009-02-11 15:04:30 -08:00
parent 008112edba
commit 906cc45eda
8 changed files with 193 additions and 9 deletions

View File

@ -21,7 +21,7 @@ all:
install:
# libraries
@mkdir -p ${PREFIX}/lib
@for a in `find * | grep -e '\.so$$'` ; do \
@for a in `find * | grep -e '\.so$$' | grep lib` ; do \
echo " $$a "; cp $$a ${PREFIX}/lib ; done
# object archives
@for a in `find * | grep -e '\.a$$'` ; do \

View File

@ -172,7 +172,7 @@ static int cmd_help(void *data, const char *input)
" |[cmd] ; run this command thru the io pipe (no args=list)\n"
" #[algo] [len] ; calculate hash checksum of current block\n"
" q [ret] ; quit r\n"
"Append '?' to every char command to get detailed help\n"
"Append '?' to any char command to get detailed help\n"
"");
break;
}

View File

@ -7,6 +7,9 @@
struct r_lang_handle_t {
char *name;
char *desc;
char *help;
int (*init)();
int (*fini)();
int (*run)(void *user, const char *code, int len);
int (*run_file)(void *user, const char *file);
int (*set_argv)(void *user, int argc, char **argv);

View File

@ -10,8 +10,15 @@ int r_lang_init(struct r_lang_t *lang)
return R_TRUE;
}
void r_lang_set_user_ptr(struct r_lang_t *lang, void *user)
{
lang->user = user;
}
int r_lang_add(struct r_lang_t *lang, struct r_lang_handle_t *foo)
{
if (foo->init)
foo->init(lang->user);
list_add_tail(&(foo->list), &(lang->langs));
return R_TRUE;
}
@ -39,11 +46,6 @@ int r_lang_set(struct r_lang_t *lang, const char *name)
return R_FALSE;
}
void r_lang_set_user_ptr(struct r_lang_t *lang, void *user)
{
lang->user = user;
}
int r_lang_set_argv(struct r_lang_t *lang, int argc, char **argv)
{
if (lang->cur && lang->cur->set_argv)
@ -86,7 +88,9 @@ int r_lang_prompt(struct r_lang_t *lang)
buf[strlen(buf)-1]='\0';
if (!strcmp(buf, "q"))
return R_TRUE;
r_lang_run(lang, buf, strlen(buf));
if (!strcmp(buf, "?"))
printf(lang->cur->help);
else r_lang_run(lang, buf, strlen(buf));
}
return R_TRUE;
}

View File

@ -1,13 +1,16 @@
CFLAGS=-I../../include -Wall
BINDEPS=
all: lua.so dummy.so
all: lua.so dummy.so ruby.so
@true
lua.so: lua.o
${CC} ${CFLAGS} -fPIC -shared -o lua.so lua.c -Wl,-R..
@#strip -s lua.so
ruby.so:
-ruby mkruby.rb
dummy.so: dummy.o
${CC} ${CFLAGS} -fPIC -shared -o dummy.so dummy.c -Wl,-R..
@#strip -s lua.so

38
libr/lang/p/chkruby.rb Normal file
View File

@ -0,0 +1,38 @@
# setup with ruby!
require 'mkmf'
INCDIR = Config::CONFIG['rubylibdir'] + "/"+Config::CONFIG['arch']
LIBDIR = Config::CONFIG['LIBRUBY_ARG_SHARED']
#LIBS = Config::CONFIG['LIBS'] -lpthread...
LIBNAM = Config::CONFIG['RUBY_INSTALL_NAME']
#LDSHARED=compilername -shared..."
rb_so = '_test.so'
rb_c = '_test.c'
inc = '../..'
$cc=ENV["CC"]
if $cc == nil then
$cc="cc"
end
tobeup=false
begin
d0 = File.stat(rb_so).mtime.to_i
d1 = File.stat(rb_c).mtime.to_i
if d1 > d0 then
tobeup=true
end
rescue
tobeup=true
end
ret = false
if tobeup then
system("echo 'main(){}' > #{rb_c}");
$line="#{$cc} -I #{inc} -I#{INCDIR} #{rb_c} -shared #{LIBDIR}" \
" -l#{LIBNAM} #{ENV['CFLAGS']} #{ENV['LDFLAGS']} -o #{rb_so}"
ret = system($line)
system("rm -f #{rb_so} #{rb_c}");
end
exit ret

46
libr/lang/p/mkruby.rb Normal file
View File

@ -0,0 +1,46 @@
# setup with ruby!
require 'mkmf'
INCDIR = Config::CONFIG['rubylibdir'] + "/"+Config::CONFIG['arch']
LIBDIR = Config::CONFIG['LIBRUBY_ARG_SHARED']
#LIBS = Config::CONFIG['LIBS'] -lpthread...
LIBNAM = Config::CONFIG['RUBY_INSTALL_NAME']
#LDSHARED=compilername -shared..."
rb_so = 'ruby.so'
rb_c = 'ruby.c'
rb_o = 'ruby.o'
inc = '../../include'
if ARGV[0] != nil; rb_c = ARGV[0] end
if ARGV[1] != nil; rb_so = ARGV[1] end
if ARGV[2] != nil; inc = ARGV[2] end
$cc=ENV["CC"]
if $cc == nil then
$cc="cc"
end
tobeup=false
begin
d0 = File.stat(rb_so).mtime.to_i
d1 = File.stat(rb_c).mtime.to_i
if d1 > d0 then
tobeup=true
end
rescue
tobeup=true
end
def runline(line)
puts line
system(line)
end
if tobeup then
# runline("#{$cc} -I #{inc} -I#{INCDIR} #{rb_c} -fPIC -c #{LIBDIR}" \
# " -l#{LIBNAM} #{ENV['CFLAGS']} #{ENV['LDFLAGS']} -o #{rb_o}")
runline("#{$cc} -I #{inc} -I#{INCDIR} #{rb_c} -fPIC -shared #{LIBDIR}" \
" -l#{LIBNAM} #{ENV['CFLAGS']} #{ENV['LDFLAGS']} -o #{rb_so}")
end
exit 0

90
libr/lang/p/ruby.c Normal file
View File

@ -0,0 +1,90 @@
/* radare - LGPL - Copyright 2009 pancake<nopcode.org> */
/* ruby extension for libr (radare2) */
#include "r_lib.h"
#include "r_lang.h"
#include "ruby.h"
#define LIBDIR "/usr/lib"
#define RUBYAPI LIBDIR"/ruby1.8/radare.rb"
/* for the basic r_core_cmd calls */
#include "r_core.h"
static struct r_core_t *core = NULL;
static VALUE radare_ruby_cmd(VALUE self, VALUE string)
{
const char *retstr;
Check_Type(string, T_STRING);
retstr = r_core_cmd_str(core, RSTRING(string)->ptr);
if (retstr == NULL || retstr[0]=='\0')
return rb_str_new2("");
return rb_str_new2(retstr);
}
static int run(void *user, const char *code, int len)
{
int err;
//"require 'irb'; $r = Radare.new(); IRB.start();", &err);
rb_eval_string_protect(code, &err);
if (err != 0) {
printf("error %d handled\n", err);
}
return (err==0)?R_TRUE:R_FALSE;
}
static int slurp_ruby(const char *file)
{
rb_load_file(file);
ruby_exec();
return R_TRUE;
}
static int run_file(void *user, const char *file)
{
return slurp_ruby(file);
}
static int init(void *user)
{
VALUE rb_RadareCmd;
core = user;
ruby_init();
ruby_init_loadpath();
rb_RadareCmd = rb_define_class("Radare", rb_cObject);
rb_define_method(rb_RadareCmd, "cmd", radare_ruby_cmd, 1);
rb_eval_string_protect("$r = Radare.new()", NULL);
if (!slurp_ruby(RUBYAPI)) {
printf("[ruby] error loading ruby api\n");
//return R_FALSE;
}
return R_TRUE;
}
static int rb_fini()
{
ruby_finalize();
return R_TRUE;
}
const char *help =
"Ruby plugin usage:\n"
" bytes = $r.cmd(\"p8 10\");\n";
static struct r_lang_handle_t r_lang_plugin_ruby = {
.name = "ruby",
.desc = "RUBY language extension",
.init = &init,
.help = &help,
.run = &run,
.run_file = &run_file,
.set_argv = NULL,
};
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_LANG,
.data = &r_lang_plugin_ruby,
};