1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
/* $EPIC: ruby.c,v 1.14 2009/07/05 04:29:44 jnelson Exp $ */
/*
* ruby.c -- Calling RUBY from epic.
*
* Copyright 2006 EPIC Software Labs.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notices, the above paragraph (the one permitting redistribution),
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the author(s) may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "irc.h"
#include "ircaux.h"
#include "array.h"
#include "alias.h"
#include "commands.h"
#include "functions.h"
#include "output.h"
#include "ifcmd.h"
#include "extlang.h"
#include <ruby.h>
VALUE rubyclass;
int is_ruby_running = 0;
#define RUBY_STARTUP \
VALUE x; \
char *my_string; \
\
x = rb_obj_as_string(string); \
my_string = STR2CSTR(x);
static VALUE epic_echo (VALUE module, VALUE string)
{
RUBY_STARTUP
yell("%s", my_string);
return Qnil;
}
static VALUE epic_say (VALUE module, VALUE string)
{
RUBY_STARTUP
say("%s", my_string);
return Qnil;
}
static VALUE epic_cmd (VALUE module, VALUE string)
{
RUBY_STARTUP
runcmds("$*", my_string);
return Qnil;
}
static VALUE epic_eval (VALUE module, VALUE string)
{
RUBY_STARTUP
runcmds(my_string, "");
return Qnil;
}
static VALUE epic_expr (VALUE module, VALUE string)
{
char *exprval;
RUBY_STARTUP
exprval = parse_inline(my_string, "");
return rb_str_new(exprval, strlen(exprval));
}
static VALUE epic_call (VALUE module, VALUE string)
{
char *funcval;
RUBY_STARTUP
funcval = call_function(my_string, "");
return rb_str_new(funcval, strlen(funcval));
}
/* Called by the epic hooks to activate tcl on-demand. */
void ruby_startstop (int value)
{
VALUE rubyval;
/* If it is already in the state we want, do nothing. */
if (is_ruby_running == value)
return;
/* Do a shutdown */
if (value == 0)
{
is_ruby_running = 0;
/* Do shutdown stuff */
return;
}
/* Do a startup */
++is_ruby_running;
ruby_init();
ruby_init_loadpath();
ruby_script(malloc_strdup(irc_version));
rubyclass = rb_define_class("EPIC", rb_cObject);
rb_define_singleton_method(rubyclass, "echo", epic_echo, 1);
rb_define_singleton_method(rubyclass, "say", epic_say, 1);
rb_define_singleton_method(rubyclass, "cmd", epic_cmd, 1);
rb_define_singleton_method(rubyclass, "eval", epic_eval, 1);
rb_define_singleton_method(rubyclass, "expr", epic_expr, 1);
rb_define_singleton_method(rubyclass, "call", epic_call, 1);
rb_gc_register_address(&rubyclass);
/* XXX Is it a hack to do it like this instead of in pure C? */
rubyval = rb_eval_string("EPICstderr = Object.new unless defined? EPICstderr\n"
"def EPICstderr.write(string) \n"
" str = string.chomp \n"
" EPIC.echo(\"RUBY-ERROR: #{str}\") \n"
"end \n"
"$stderr = EPICstderr");
if (rubyval == Qnil)
say("stderr assignment returned Qnil");
rubyval = rb_eval_string("EPICstdout = Object.new unless defined? EPICstdout\n"
"def EPICstdout.write(string) \n"
" str = string.chomp \n"
" EPIC.echo(str) \n"
"end \n"
"$stdout = EPICstdout");
if (rubyval == Qnil)
say("stderr assignment returned Qnil");
}
/*
* Used by the $ruby(...) function: evalulate ... as a RUBY statement, and
* return the result of the statement.
*/
static VALUE internal_rubyeval (VALUE *a)
{
int foo;
VALUE rubyval;
rubyval = rb_eval_string((char *)a);
if (rubyval == Qnil)
return Qnil;
else
return rubyval;
}
static VALUE eval_failed (VALUE args, VALUE error_info)
{
VALUE err_info_str;
char *ick;
err_info_str = rb_obj_as_string(error_info);
ick = STR2CSTR(err_info_str);
yell("RUBY-ERROR: %s", ick);
return Qnil;
}
char * rubyeval (char *input)
{
VALUE rubyval;
char * retval = NULL;
if (input && *input)
{
ruby_startstop(1);
rubyval = rb_rescue2(internal_rubyeval, (VALUE)input,
eval_failed, 0,
rb_eException, 0);
if (rubyval == Qnil)
retval = NULL;
else
{
VALUE x;
x = rb_obj_as_string(rubyval);
retval = STR2CSTR(x);
}
}
RETURN_STR(retval); /* XXX Is this malloced or not? */
}
/*
* The /RUBY function: Evalulate the args as a RUBY block and ignore the
* return value of the statement.
*/
BUILT_IN_COMMAND(rubycmd)
{
char *body, *x;
if (*args == '{')
{
if (!(body = next_expr(&args, '{')))
{
my_error("RUBY: unbalanced {");
return;
}
}
else
body = args;
x = rubyeval(body);
new_free(&x);
}
|