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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/* -*- c-basic-offset: 2 -*- */
/*
Copyright(C) 2013-2015 Brazil
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "grn_mrb.h"
#include "grn_ctx_impl.h"
#include "grn_util.h"
#include <string.h>
#ifdef GRN_WITH_MRUBY
# include <mruby/proc.h>
# include <mruby/compile.h>
# include <mruby/string.h>
#endif
#include <ctype.h>
#ifdef WIN32
# include <share.h>
#endif /* WIN32 */
#define BUFFER_SIZE 2048
#define E_LOAD_ERROR (mrb_class_get(mrb, "LoadError"))
static char grn_mrb_ruby_scripts_dir[GRN_ENV_BUFFER_SIZE];
void
grn_mrb_init_from_env(void)
{
grn_getenv("GRN_RUBY_SCRIPTS_DIR",
grn_mrb_ruby_scripts_dir,
GRN_ENV_BUFFER_SIZE);
}
#ifdef GRN_WITH_MRUBY
# ifdef WIN32
static char *win32_ruby_scripts_dir = NULL;
static char win32_ruby_scripts_dir_buffer[PATH_MAX];
static const char *
grn_mrb_get_default_system_ruby_scripts_dir(void)
{
if (!win32_ruby_scripts_dir) {
const char *base_dir;
const char *relative_path = GRN_RELATIVE_RUBY_SCRIPTS_DIR;
size_t base_dir_length;
base_dir = grn_win32_base_dir();
base_dir_length = strlen(base_dir);
grn_strcpy(win32_ruby_scripts_dir_buffer, PATH_MAX, base_dir);
grn_strcat(win32_ruby_scripts_dir_buffer, PATH_MAX, "/");
grn_strcat(win32_ruby_scripts_dir_buffer, PATH_MAX, relative_path);
win32_ruby_scripts_dir = win32_ruby_scripts_dir_buffer;
}
return win32_ruby_scripts_dir;
}
# else /* WIN32 */
static const char *
grn_mrb_get_default_system_ruby_scripts_dir(void)
{
return GRN_RUBY_SCRIPTS_DIR;
}
# endif /* WIN32 */
const char *
grn_mrb_get_system_ruby_scripts_dir(grn_ctx *ctx)
{
if (grn_mrb_ruby_scripts_dir[0]) {
return grn_mrb_ruby_scripts_dir;
} else {
return grn_mrb_get_default_system_ruby_scripts_dir();
}
}
static grn_bool
grn_mrb_is_absolute_path(const char *path)
{
if (path[0] == '/') {
return GRN_TRUE;
}
if (isalpha(path[0]) && path[1] == ':' && path[2] == '/') {
return GRN_TRUE;
}
return GRN_FALSE;
}
static grn_bool
grn_mrb_expand_script_path(grn_ctx *ctx, const char *path,
char *expanded_path, size_t expanded_path_size)
{
const char *ruby_scripts_dir;
char dir_last_char;
int path_length, max_path_length;
if (grn_mrb_is_absolute_path(path)) {
expanded_path[0] = '\0';
} else if (path[0] == '.' && path[1] == '/') {
grn_strcpy(expanded_path, expanded_path_size, ctx->impl->mrb.base_directory);
grn_strcat(expanded_path, expanded_path_size, "/");
} else {
ruby_scripts_dir = grn_mrb_get_system_ruby_scripts_dir(ctx);
grn_strcpy(expanded_path, expanded_path_size, ruby_scripts_dir);
dir_last_char = ruby_scripts_dir[strlen(expanded_path) - 1];
if (dir_last_char != '/') {
grn_strcat(expanded_path, expanded_path_size, "/");
}
}
path_length = strlen(path);
max_path_length = PATH_MAX - strlen(expanded_path) - 1;
if (path_length > max_path_length) {
ERR(GRN_INVALID_ARGUMENT,
"script path is too long: %d (max: %d) <%s%s>",
path_length, max_path_length,
expanded_path, path);
return GRN_FALSE;
}
grn_strcat(expanded_path, expanded_path_size, path);
return GRN_TRUE;
}
mrb_value
grn_mrb_load(grn_ctx *ctx, const char *path)
{
grn_mrb_data *data = &(ctx->impl->mrb);
mrb_state *mrb = data->state;
char expanded_path[PATH_MAX];
FILE *file;
mrb_value result;
struct mrb_parser_state *parser;
if (!mrb) {
return mrb_nil_value();
}
if (!grn_mrb_expand_script_path(ctx, path, expanded_path, PATH_MAX)) {
return mrb_nil_value();
}
file = grn_fopen(expanded_path, "r");
if (!file) {
char message[BUFFER_SIZE];
mrb_value exception;
grn_snprintf(message, BUFFER_SIZE, BUFFER_SIZE,
"fopen: failed to open mruby script file: <%s>",
expanded_path);
SERR(message);
exception = mrb_exc_new(mrb, E_LOAD_ERROR,
ctx->errbuf, strlen(ctx->errbuf));
mrb->exc = mrb_obj_ptr(exception);
return mrb_nil_value();
}
{
char current_base_directory[PATH_MAX];
char *last_directory;
grn_strcpy(current_base_directory, PATH_MAX, data->base_directory);
grn_strcpy(data->base_directory, PATH_MAX, expanded_path);
last_directory = strrchr(data->base_directory, '/');
if (last_directory) {
last_directory[0] = '\0';
}
parser = mrb_parser_new(mrb);
mrb_parser_set_filename(parser, expanded_path);
parser->s = parser->send = NULL;
parser->f = file;
mrb_parser_parse(parser, NULL);
fclose(file);
{
struct RProc *proc;
proc = mrb_generate_code(mrb, parser);
result = mrb_toplevel_run(mrb, proc);
}
mrb_parser_free(parser);
grn_strcpy(data->base_directory, PATH_MAX, current_base_directory);
}
return result;
}
mrb_value
grn_mrb_eval(grn_ctx *ctx, const char *script, int script_length)
{
grn_mrb_data *data = &(ctx->impl->mrb);
mrb_state *mrb = data->state;
mrb_value result;
struct mrb_parser_state *parser;
if (!mrb) {
return mrb_nil_value();
}
if (script_length < 0) {
script_length = strlen(script);
}
parser = mrb_parse_nstring(mrb, script, script_length, NULL);
{
struct RProc *proc;
struct RClass *eval_context_class;
mrb_value eval_context;
proc = mrb_generate_code(mrb, parser);
eval_context_class = mrb_class_get_under(mrb, data->module, "EvalContext");
eval_context = mrb_obj_new(mrb, eval_context_class, 0, NULL);
result = mrb_context_run(mrb, proc, eval_context, 0);
}
mrb_parser_free(parser);
return result;
}
grn_rc
grn_mrb_to_grn(grn_ctx *ctx, mrb_value mrb_object, grn_obj *grn_object)
{
grn_rc rc = GRN_SUCCESS;
grn_mrb_data *data = &(ctx->impl->mrb);
mrb_state *mrb = data->state;
switch (mrb_type(mrb_object)) {
case MRB_TT_FALSE :
if (mrb_nil_p(mrb_object)) {
grn_obj_reinit(ctx, grn_object, GRN_DB_VOID, 0);
} else {
grn_obj_reinit(ctx, grn_object, GRN_DB_BOOL, 0);
GRN_BOOL_SET(ctx, grn_object, GRN_FALSE);
}
break;
case MRB_TT_TRUE :
grn_obj_reinit(ctx, grn_object, GRN_DB_BOOL, 0);
GRN_BOOL_SET(ctx, grn_object, GRN_TRUE);
break;
case MRB_TT_FIXNUM :
grn_obj_reinit(ctx, grn_object, GRN_DB_INT32, 0);
GRN_INT32_SET(ctx, grn_object, mrb_fixnum(mrb_object));
break;
case MRB_TT_STRING :
grn_obj_reinit(ctx, grn_object, GRN_DB_TEXT, 0);
GRN_TEXT_SET(ctx, grn_object,
RSTRING_PTR(mrb_object),
RSTRING_LEN(mrb_object));
break;
case MRB_TT_SYMBOL :
{
const char *name;
int name_length;
grn_obj_reinit(ctx, grn_object, GRN_DB_TEXT, 0);
GRN_BULK_REWIND(grn_object);
GRN_TEXT_PUTC(ctx, grn_object, ':');
name = mrb_sym2name_len(mrb, mrb_symbol(mrb_object), &name_length);
GRN_TEXT_PUT(ctx, grn_object, name, name_length);
}
break;
default :
rc = GRN_INVALID_ARGUMENT;
break;
}
return rc;
}
#endif
|