comparison lib/ruby-c.c @ 16:598fcbe482b5

imported patch 19_kinput2-v3.1-ruby.patch
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Mon, 08 Mar 2010 20:38:17 +0900
parents
children
comparison
equal deleted inserted replaced
15:89750191b165 16:598fcbe482b5
1 /* $Id: ruby-c.c,v 1.2 2003/06/10 02:11:25 komatsu Exp $ */
2 /*
3 * AUTHOR: Hiroyuki Komatsu <komatsu@taiyaki.org> http://taiyaki.org/
4 * LICENSE: GPL2
5 */
6
7 #include <stdarg.h>
8 #include "ruby-c.h"
9 #define RUBY_EVALF_BUF 1024
10
11 VALUE
12 ruby_eval (char *statement)
13 {
14 int state;
15 VALUE result;
16
17 result = rb_eval_string_protect (statement, &state);
18 if (state) {
19 EXTERN VALUE ruby_errinfo;
20
21 fprintf(stderr, "Eval Error in \"%s\"\n", statement);
22 fprintf(stderr, "%s\n", STR2CSTR(ruby_errinfo));
23 }
24 return result;
25 }
26
27 VALUE
28 ruby_evalf (char *statement_format, ...)
29 {
30 va_list ap;
31 char statement[RUBY_EVALF_BUF];
32 int length;
33
34 va_start (ap, statement_format);
35 length = vsnprintf (statement, RUBY_EVALF_BUF, statement_format, ap);
36 va_end (ap);
37
38 if (length == -1) { /* Before glibc 2.0.6 */
39 fprintf (stderr, "ruby_evalf: Error, Out of Buffer\n");
40 fprintf (stderr, "Buffer size is %d\n", RUBY_EVALF_BUF);
41 return Qnil;
42 } else if (length > RUBY_EVALF_BUF -1) { /* C99 */
43 VALUE result;
44 char *d_statement = malloc (sizeof (char) * length + 1);
45 va_start (ap, statement_format);
46 vsnprintf (d_statement, length + 1, statement_format, ap);
47 va_end (ap);
48 result = ruby_eval (d_statement);
49 free (d_statement);
50 return result;
51 } else {
52 return ruby_eval (statement);
53 }
54 }
55
56 void
57 ruby_load (char *filename)
58 {
59 int state;
60 rb_load_protect (rb_str_new2 (filename), 0, &state);
61 if (state) {
62 fprintf(stderr, "Load Error for \"%s\"\n", filename);
63 fprintf(stderr, "%s\n", STR2CSTR(ruby_errinfo));
64 }
65 }