-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_call_rb.c
93 lines (79 loc) · 1.92 KB
/
c_call_rb.c
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
#include <stdio.h>
#include "ruby.h"
void crb_init() {
int argc2 = 0;
char **argv2 = NULL;
ruby_sysinit(&argc2, &argv2);
RUBY_INIT_STACK;
ruby_init();
ruby_init_loadpath();
}
void crb_cleanup() {
ruby_cleanup(0);
}
int crb_eval_string(const char *rbstr) {
int error;
rb_eval_string_protect(rbstr, &error);
return error;
}
#define MAX 4096
char evalStr[MAX];
char *debug_flag = "false";
int help_flag = 0;
int main(int argc, char **argv) {
int i,j;
int res;
crb_init();
for (i=1; i < argc; i++) {
if (strcmp("--", argv[i])==0) {
VALUE vargv;
vargv = rb_gv_get("$*");
for (j = i+1; j < argc; j++)
{
rb_ary_push(vargv, rb_str_new2(argv[j]));
}
break;
} else if ((strcmp("--debug", argv[i])==0) || (strcmp("-d", argv[i])==0)) {
debug_flag = "true";
} else if ((strcmp("--help", argv[i])==0) || (strcmp("-h", argv[i])==0)) {
help_flag = 1;
}
}
snprintf(evalStr, MAX-400,
"require 'rbconfig'\n"
"$DEBUG=%s\n"
"\n"
"puts %%Q{ARGV: #{ARGV}} if $DEBUG\n"
"$:.unshift '.'\n"
"puts %%Q{LOAD_PATH: #{$:}} if $DEBUG\n"
"argv0 = %%q*%s*\n"
"puts %%Q{ARGV0: #{argv0}} if $DEBUG\n"
"dir, rbfn = File.split(argv0)\n"
"rbfn = rbfn.chomp(RbConfig::CONFIG['EXEEXT'])\n"
"puts %%Q{Chdir: #{dir} RubyFile: #{rbfn}} if $DEBUG\n"
"\n"
"Dir.chdir(dir) do\n"
" require rbfn\n"
"end\n"
,debug_flag, argv[0]);
if (help_flag) {
printf(
"%s syntax:\n"
"\n"
" -h --help show this message\n"
" -d --debug set $DEBUG to true\n"
" -- [arg1 [arg2 [...]]] all arguments pass to ruby program after '--'\n"
"\n"
//"---- this program will run ----\n"
//"%s\n"
//"--------\n"
, argv[0] /*, evalStr */);
exit(0);
}
res = crb_eval_string(evalStr);
crb_cleanup();
if (res != 0) {
fprintf(stderr, "\nerror %d\n", res);
}
return res;
}