Debian Codegraph @

lib/mrb.c

raw symbols SCIP
1 /*
2 Copyright(C) 2013-2018 Brazil
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "grn_mrb.h"
20 #include "grn_ctx_impl.h"
21 #include "grn_encoding.h"
22 #include "grn_util.h"
23
24 #include <string.h>
25
26 #ifdef GRN_WITH_MRUBY
27 # include <mruby/compile.h>
28 # include <mruby/opcode.h>
29 # include <mruby/proc.h>
30 # include <mruby/string.h>
31 #endif
32
33 #include <ctype.h>
34
35 #ifdef WIN32
36 # include <share.h>
37 #endif /* WIN32 */
38
39 #define BUFFER_SIZE 2048
40 #define E_LOAD_ERROR (mrb_class_get(mrb, "LoadError"))
41
42 static char grn_mrb_ruby_scripts_dir[GRN_ENV_BUFFER_SIZE];
43 static bool grn_mrb_order_by_estimated_size_enable = false;
44
45 void
46 grn_mrb_init_from_env(void)
47 {
48 grn_getenv("GRN_RUBY_SCRIPTS_DIR",
49 grn_mrb_ruby_scripts_dir,
50 GRN_ENV_BUFFER_SIZE);
51 {
52 char grn_order_by_estimated_size_enable_env[GRN_ENV_BUFFER_SIZE];
53 grn_getenv("GRN_ORDER_BY_ESTIMATED_SIZE_ENABLE",
54 grn_order_by_estimated_size_enable_env,
55 GRN_ENV_BUFFER_SIZE);
56 if (strcmp(grn_order_by_estimated_size_enable_env, "yes") == 0) {
57 grn_mrb_order_by_estimated_size_enable = true;
58 } else {
59 grn_mrb_order_by_estimated_size_enable = false;
60 }
61 }
62 }
63
64 bool
65 grn_mrb_is_order_by_estimated_size_enabled(void)
66 {
67 return grn_mrb_order_by_estimated_size_enable;
68 }
69
70 #ifdef GRN_WITH_MRUBY
71 # ifdef WIN32
72 static char *windows_ruby_scripts_dir = NULL;
73 static char windows_ruby_scripts_dir_buffer[PATH_MAX];
74 static const char *
75 grn_mrb_get_default_system_ruby_scripts_dir(grn_ctx *ctx)
76 {
77 if (!windows_ruby_scripts_dir) {
78 const char *base_dir;
79 const char *relative_path = GRN_RELATIVE_RUBY_SCRIPTS_DIR;
80
81 base_dir = grn_windows_base_dir();
82 grn_strcpy(windows_ruby_scripts_dir_buffer, PATH_MAX, base_dir);
83 grn_strcat(windows_ruby_scripts_dir_buffer, PATH_MAX, "/");
84 grn_strcat(windows_ruby_scripts_dir_buffer, PATH_MAX, relative_path);
85 windows_ruby_scripts_dir = windows_ruby_scripts_dir_buffer;
86 }
87 return windows_ruby_scripts_dir;
88 }
89
90 # else /* WIN32 */
91 static const char *
92 grn_mrb_get_default_system_ruby_scripts_dir(grn_ctx *ctx)
93 {
94 return GRN_RUBY_SCRIPTS_DIR;
95 }
96 # endif /* WIN32 */
97
98 const char *
99 grn_mrb_get_system_ruby_scripts_dir(grn_ctx *ctx)
100 {
101 if (grn_mrb_ruby_scripts_dir[0]) {
102 return grn_mrb_ruby_scripts_dir;
103 } else {
104 return grn_mrb_get_default_system_ruby_scripts_dir(ctx);
105 }
106 }
107
108 static bool
109 grn_mrb_is_absolute_path(const char *path)
110 {
111 if (path[0] == '/') {
112 return true;
113 }
114
115 if (isalpha((unsigned char)path[0]) && path[1] == ':' && path[2] == '/') {
116 return true;
117 }
118
119 return false;
120 }
121
122 static bool
123 grn_mrb_expand_script_path(grn_ctx *ctx, const char *path,
124 char *expanded_path, size_t expanded_path_size)
125 {
126 const char *ruby_scripts_dir;
127 char dir_last_char;
128 int path_length;
129 int max_path_length;
130
131 if (grn_mrb_is_absolute_path(path)) {
132 expanded_path[0] = '\0';
133 } else if (path[0] == '.' && path[1] == '/') {
134 grn_strcpy(expanded_path, expanded_path_size, ctx->impl->mrb.base_directory);
135 grn_strcat(expanded_path, expanded_path_size, "/");
136 } else {
137 ruby_scripts_dir = grn_mrb_get_system_ruby_scripts_dir(ctx);
138 grn_strcpy(expanded_path, expanded_path_size, ruby_scripts_dir);
139
140 dir_last_char = ruby_scripts_dir[strlen(expanded_path) - 1];
141 if (dir_last_char != '/') {
142 grn_strcat(expanded_path, expanded_path_size, "/");
143 }
144 }
145
146 path_length = strlen(path);
147 max_path_length = PATH_MAX - strlen(expanded_path) - 1;
148 if (path_length > max_path_length) {
149 const char *grn_encoding_path;
150 grn_encoding_path =
151 grn_encoding_convert_from_locale(ctx,
152 path,
153 path_length,
154 NULL);
155 ERR(GRN_INVALID_ARGUMENT,
156 "script path is too long: %d (max: %d) <%s%s>",
157 path_length, max_path_length,
158 expanded_path, grn_encoding_path);
159 grn_encoding_converted_free(ctx, grn_encoding_path);
160 return false;
161 }
162
163 grn_strcat(expanded_path, expanded_path_size, path);
164
165 return true;
166 }
167
168 mrb_value
169 grn_mrb_load(grn_ctx *ctx, const char *path)
170 {
171 grn_mrb_data *data = &(ctx->impl->mrb);
172 mrb_state *mrb = data->state;
173 char expanded_path[PATH_MAX];
174 FILE *file;
175 mrb_value result;
176 struct mrb_parser_state *parser;
177
178 if (!mrb) {
179 return mrb_nil_value();
180 }
181
182 if (!grn_mrb_expand_script_path(ctx, path, expanded_path, PATH_MAX)) {
183 return mrb_nil_value();
184 }
185
186 file = grn_fopen(expanded_path, "r");
187 if (!file) {
188 const char *grn_encoding_expanded_path;
189 mrb_value exception;
190
191 grn_encoding_expanded_path =
192 grn_encoding_convert_from_locale(ctx, expanded_path, -1, NULL);
193 SERR("fopen: failed to open mruby script file: <%s>",
194 grn_encoding_expanded_path);
195 grn_encoding_converted_free(ctx, grn_encoding_expanded_path);
196 exception = mrb_exc_new(mrb, E_LOAD_ERROR,
197 ctx->errbuf, strlen(ctx->errbuf));
198 mrb->exc = mrb_obj_ptr(exception);
199 return mrb_nil_value();
200 }
201
202 {
203 char current_base_directory[PATH_MAX];
204 char *last_directory;
205
206 grn_strcpy(current_base_directory, PATH_MAX, data->base_directory);
207 grn_strcpy(data->base_directory, PATH_MAX, expanded_path);
208 last_directory = strrchr(data->base_directory, '/');
209 if (last_directory) {
210 last_directory[0] = '\0';
211 }
212
213 parser = mrb_parser_new(mrb);
214 {
215 const char *utf8_expanded_path;
216 utf8_expanded_path =
217 grn_encoding_convert_to_utf8_from_locale(ctx,
218 expanded_path,
219 -1,
220 NULL);
221 mrb_parser_set_filename(parser, utf8_expanded_path);
222 grn_encoding_converted_free(ctx, utf8_expanded_path);
223 }
224 parser->s = parser->send = NULL;
225 parser->f = file;
226 mrb_parser_parse(parser, NULL);
227 fclose(file);
228
229 {
230 struct RProc *proc;
231 int arena_index;
232 proc = mrb_generate_code(mrb, parser);
233 MRB_PROC_SET_TARGET_CLASS(proc, mrb->object_class);
234 arena_index = mrb_gc_arena_save(mrb);
235 result = mrb_yield_with_class(mrb,
236 mrb_obj_value(proc),
237 0,
238 NULL,
239 mrb_top_self(mrb),
240 mrb->object_class);
241 mrb_gc_arena_restore(mrb, arena_index);
242 }
243 mrb_parser_free(parser);
244
245 grn_strcpy(data->base_directory, PATH_MAX, current_base_directory);
246 }
247
248 return result;
249 }
250 #endif