Debian Codegraph @

lib/plugin.c

raw symbols SCIP
1 /*
2 Copyright (C) 2012-2018 Brazil
3 Copyright (C) 2022-2024 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "grn.h"
21 #include "grn_ctx_impl_mrb.h"
22 #include "grn_encoding.h"
23 #include "grn_proc.h"
24 #include <groonga/plugin.h>
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include <sys/stat.h>
31 #ifdef HAVE_DIRENT_H
32 # include <dirent.h>
33 #endif /* HAVE_DIRENT_H */
34
35 #ifndef S_ISREG
36 # ifdef _S_IFREG
37 # define S_ISREG(mode) (mode & _S_IFREG)
38 # endif /* _S_IFREG */
39 #endif /* !S_ISREG */
40
41 #include "grn_db.h"
42 #include "grn_plugin.h"
43 #include "grn_ctx_impl.h"
44 #include "grn_util.h"
45
46 #ifdef GRN_WITH_MRUBY
47 # include <mruby.h>
48 #endif /* GRN_WITH_MRUBY */
49
50 static grn_hash *grn_plugins = NULL;
51 static grn_critical_section grn_plugins_lock;
52 static grn_ctx grn_plugins_ctx;
53 static grn_obj grn_plugins_path;
54
55 #define GRN_PLUGINS_DIR_EACH_BEGIN(dir, dir_length) \
56 do { \
57 unsigned int i_; \
58 const unsigned int n_dirs_ = \
59 grn_vector_size(&grn_plugins_ctx, &grn_plugins_path); \
60 for (i_ = 0; i_ < n_dirs_; i_++) { \
61 const char *dir = NULL; \
62 unsigned int dir_length; \
63 dir_length = grn_vector_get_element(&grn_plugins_ctx, \
64 &grn_plugins_path, \
65 i_, \
66 &dir, \
67 NULL, \
68 NULL);
69 #define GRN_PLUGINS_DIR_EACH_END() \
70 } \
71 } \
72 while (false)
73
74 #ifdef HAVE_DLFCN_H
75 # include <dlfcn.h>
76 # define grn_dl_open(filename) dlopen(filename, RTLD_LAZY | RTLD_LOCAL)
77 # define grn_dl_open_error_label() dlerror()
78 # define grn_dl_close(dl) (dlclose(dl) == 0)
79 # define grn_dl_close_error_label() dlerror()
80 # define grn_dl_sym(dl, symbol) dlsym(dl, symbol)
81 # define grn_dl_sym_error_label() dlerror()
82 # define grn_dl_clear_error() dlerror()
83 #else
84 # define grn_dl_open(filename) LoadLibrary(filename)
85 # define grn_dl_open_error_label() "LoadLibrary"
86 # define grn_dl_close(dl) (FreeLibrary(dl) != 0)
87 # define grn_dl_close_error_label() "FreeLibrary"
88 # define grn_dl_sym(dl, symbol) ((void *)GetProcAddress(dl, symbol))
89 # define grn_dl_sym_error_label() "GetProcAddress"
90 # define grn_dl_clear_error()
91 #endif
92
93 #define GRN_PLUGIN_KEY_SIZE(filename) (strlen((filename)) + 1)
94
95 static char grn_plugins_path_env[GRN_ENV_BUFFER_SIZE];
96 static char grn_plugins_dir_env[GRN_ENV_BUFFER_SIZE];
97
98 void
99 grn_plugin_init_from_env(void)
100 {
101 grn_getenv("GRN_PLUGINS_PATH", grn_plugins_path_env, GRN_ENV_BUFFER_SIZE);
102 grn_getenv("GRN_PLUGINS_DIR", grn_plugins_dir_env, GRN_ENV_BUFFER_SIZE);
103 }
104
105 static int
106 compute_name_size(const char *name, int name_size)
107 {
108 if (name_size < 0) {
109 if (name) {
110 name_size = (int)strlen(name);
111 } else {
112 name_size = 0;
113 }
114 }
115 return name_size;
116 }
117
118 grn_id
119 grn_plugin_reference(grn_ctx *ctx, const char *filename)
120 {
121 grn_id id;
122 grn_plugin **plugin = NULL;
123
124 CRITICAL_SECTION_ENTER(grn_plugins_lock);
125 id = grn_hash_get(&grn_plugins_ctx,
126 grn_plugins,
127 filename,
128 (unsigned int)GRN_PLUGIN_KEY_SIZE(filename),
129 (void **)&plugin);
130 if (plugin) {
131 (*plugin)->refcount++;
132 }
133 CRITICAL_SECTION_LEAVE(grn_plugins_lock);
134
135 return id;
136 }
137
138 const char *
139 grn_plugin_path(grn_ctx *ctx, grn_id id)
140 {
141 const char *path;
142 grn_plugin *plugin;
143 int value_size;
144
145 if (id == GRN_ID_NIL) {
146 return NULL;
147 }
148
149 CRITICAL_SECTION_ENTER(grn_plugins_lock);
150 value_size = grn_hash_get_value(&grn_plugins_ctx, grn_plugins, id, &plugin);
151 CRITICAL_SECTION_LEAVE(grn_plugins_lock);
152
153 if (!plugin) {
154 return NULL;
155 }
156
157 path = plugin->path;
158 GRN_PLUGINS_DIR_EACH_BEGIN(dir, dir_length)
159 {
160 if (strncmp(dir, path, dir_length) == 0) {
161 const char *plugin_name = path + dir_length;
162 while (plugin_name[0] == '/') {
163 plugin_name++;
164 }
165 /* TODO: remove suffix too? */
166 return plugin_name;
167 }
168 }
169 GRN_PLUGINS_DIR_EACH_END();
170
171 return path;
172 }
173
174 #define GRN_PLUGIN_FUNC_PREFIX "grn_plugin_impl_"
175
176 static grn_rc
177 grn_plugin_call_init(grn_ctx *ctx, grn_id id)
178 {
179 grn_plugin *plugin;
180 int size;
181
182 size = grn_hash_get_value(&grn_plugins_ctx, grn_plugins, id, &plugin);
183 if (size == 0) {
184 return GRN_INVALID_ARGUMENT;
185 }
186
187 if (plugin->init_func) {
188 return plugin->init_func(ctx);
189 }
190
191 return GRN_SUCCESS;
192 }
193
194 #ifdef GRN_WITH_MRUBY
195 static grn_rc
196 grn_plugin_call_register_mrb(grn_ctx *ctx, grn_id id, grn_plugin *plugin)
197 {
198 grn_mrb_data *data;
199 mrb_state *mrb;
200 struct RClass *module;
201 struct RClass *plugin_loader_class;
202 int arena_index;
203
204 grn_ctx_impl_mrb_ensure_init(ctx);
205 if (ctx->rc != GRN_SUCCESS) {
206 return ctx->rc;
207 }
208
209 data = &(ctx->impl->mrb);
210 mrb = data->state;
211 module = data->module;
212
213 {
214 int added;
215 grn_hash_add(ctx,
216 ctx->impl->mrb.registered_plugins,
217 &id,
218 sizeof(grn_id),
219 NULL,
220 &added);
221 if (!added) {
222 return ctx->rc;
223 }
224 }
225
226 arena_index = mrb_gc_arena_save(mrb);
227 plugin_loader_class = mrb_class_get_under(mrb, module, "PluginLoader");
228 {
229 const char *utf8_path;
230 mrb_value mrb_path;
231 utf8_path = grn_encoding_convert_to_utf8_from_locale(ctx,
232 ctx->impl->plugin_path,
233 -1,
234 NULL);
235 mrb_path = mrb_str_new_cstr(mrb, utf8_path);
236 grn_encoding_converted_free(ctx, utf8_path);
237 mrb_funcall(mrb,
238 mrb_obj_value(plugin_loader_class),
239 "load_file",
240 1,
241 mrb_path);
242 }
243 mrb_gc_arena_restore(mrb, arena_index);
244 return ctx->rc;
245 }
246 #endif /*GRN_WITH_MRUBY */
247
248 static grn_rc
249 grn_plugin_call_register(grn_ctx *ctx, grn_id id)
250 {
251 grn_plugin *plugin;
252 int size;
253
254 CRITICAL_SECTION_ENTER(grn_plugins_lock);
255 size = grn_hash_get_value(&grn_plugins_ctx, grn_plugins, id, &plugin);
256 CRITICAL_SECTION_LEAVE(grn_plugins_lock);
257
258 if (size == 0) {
259 return GRN_INVALID_ARGUMENT;
260 }
261
262 #ifdef GRN_WITH_MRUBY
263 if (!plugin->dl) {
264 return grn_plugin_call_register_mrb(ctx, id, plugin);
265 }
266 #endif /* GRN_WITH_MRUBY */
267
268 if (plugin->register_func) {
269 return plugin->register_func(ctx);
270 }
271
272 return GRN_SUCCESS;
273 }
274
275 static grn_rc
276 grn_plugin_call_fin(grn_ctx *ctx, grn_id id)
277 {
278 grn_plugin *plugin;
279 int size;
280
281 size = grn_hash_get_value(&grn_plugins_ctx, grn_plugins, id, &plugin);
282 if (size == 0) {
283 return GRN_INVALID_ARGUMENT;
284 }
285
286 if (plugin->fin_func) {
287 return plugin->fin_func(ctx);
288 }
289
290 return GRN_SUCCESS;
291 }
292
293 static grn_rc
294 grn_plugin_initialize(
295 grn_ctx *ctx, grn_plugin *plugin, grn_dl dl, grn_id id, const char *path)
296 {
297 plugin->dl = dl;
298
299 #define GET_SYMBOL(type) \
300 do { \
301 grn_dl_clear_error(); \
302 plugin->type##_func = grn_dl_sym(dl, GRN_PLUGIN_FUNC_PREFIX #type); \
303 if (!plugin->type##_func) { \
304 const char *label; \
305 label = grn_dl_sym_error_label(); \
306 SERR("%s", label); \
307 } \
308 } while (0)
309
310 GET_SYMBOL(init);
311 GET_SYMBOL(register);
312 GET_SYMBOL(fin);
313
314 #undef GET_SYMBOL
315
316 if (!plugin->init_func || !plugin->register_func || !plugin->fin_func) {
317 ERR(GRN_INVALID_FORMAT,
318 "init func (%s) %sfound, "
319 "register func (%s) %sfound and "
320 "fin func (%s) %sfound",
321 GRN_PLUGIN_FUNC_PREFIX "init",
322 plugin->init_func ? "" : "not ",
323 GRN_PLUGIN_FUNC_PREFIX "register",
324 plugin->register_func ? "" : "not ",
325 GRN_PLUGIN_FUNC_PREFIX "fin",
326 plugin->fin_func ? "" : "not ");
327 }
328
329 if (!ctx->rc) {
330 ctx->impl->plugin_path = path;
331 grn_plugin_call_init(ctx, id);
332 ctx->impl->plugin_path = NULL;
333 }
334
335 return ctx->rc;
336 }
337
338 #ifdef GRN_WITH_MRUBY
339 static grn_id
340 grn_plugin_open_mrb(grn_ctx *ctx, const char *filename, size_t filename_size)
341 {
342 grn_ctx *plugins_ctx = &grn_plugins_ctx;
343 grn_id id = GRN_ID_NIL;
344 grn_plugin **plugin = NULL;
345
346 grn_ctx_impl_mrb_ensure_init(ctx);
347 if (ctx->rc != GRN_SUCCESS) {
348 return GRN_ID_NIL;
349 }
350
351 if (!ctx->impl->mrb.state) {
352 ERR(GRN_FUNCTION_NOT_IMPLEMENTED, "mruby support isn't enabled");
353 return GRN_ID_NIL;
354 }
355
356 id = grn_hash_add(plugins_ctx,
357 grn_plugins,
358 filename,
359 filename_size,
360 (void **)&plugin,
361 NULL);
362 if (!id) {
363 return id;
364 }
365
366 {
367 grn_ctx *ctx = plugins_ctx;
368 *plugin = GRN_CALLOC(sizeof(grn_plugin));
369 }
370 if (!*plugin) {
371 grn_hash_delete_by_id(plugins_ctx, grn_plugins, id, NULL);
372 return GRN_ID_NIL;
373 }
374
375 grn_memcpy((*plugin)->path, filename, filename_size);
376 (*plugin)->dl = NULL;
377 (*plugin)->init_func = NULL;
378 (*plugin)->register_func = NULL;
379 (*plugin)->fin_func = NULL;
380 (*plugin)->refcount = 1;
381
382 return id;
383 }
384 #endif /* GRN_WITH_MRUBY */
385
386 grn_id
387 grn_plugin_open(grn_ctx *ctx, const char *filename)
388 {
389 grn_ctx *plugins_ctx = &grn_plugins_ctx;
390 grn_id id = GRN_ID_NIL;
391 grn_dl dl;
392 grn_plugin **plugin = NULL;
393 unsigned int filename_size;
394
395 filename_size = (unsigned int)GRN_PLUGIN_KEY_SIZE(filename);
396
397 CRITICAL_SECTION_ENTER(grn_plugins_lock);
398 if ((id = grn_hash_get(plugins_ctx,
399 grn_plugins,
400 filename,
401 filename_size,
402 (void **)&plugin))) {
403 (*plugin)->refcount++;
404 goto exit;
405 }
406
407 #ifdef GRN_WITH_MRUBY
408 {
409 const char *mrb_suffix;
410 mrb_suffix = grn_plugin_get_ruby_suffix();
411 if (filename_size > strlen(mrb_suffix) &&
412 strcmp(filename + (strlen(filename) - strlen(mrb_suffix)),
413 mrb_suffix) == 0) {
414 id = grn_plugin_open_mrb(ctx, filename, filename_size);
415 goto exit;
416 }
417 }
418 #endif /* GRN_WITH_MRUBY */
419
420 if ((dl = grn_dl_open(filename))) {
421 if ((id = grn_hash_add(plugins_ctx,
422 grn_plugins,
423 filename,
424 filename_size,
425 (void **)&plugin,
426 NULL))) {
427 {
428 grn_ctx *ctx = plugins_ctx;
429 *plugin = GRN_CALLOC(sizeof(grn_plugin));
430 }
431 if (*plugin) {
432 grn_memcpy((*plugin)->path, filename, filename_size);
433 if (grn_plugin_initialize(ctx, *plugin, dl, id, filename)) {
434 {
435 grn_ctx *ctx = plugins_ctx;
436 GRN_FREE(*plugin);
437 }
438 *plugin = NULL;
439 }
440 }
441 if (!*plugin) {
442 grn_hash_delete_by_id(plugins_ctx, grn_plugins, id, NULL);
443 if (grn_dl_close(dl)) {
444 /* Now, __FILE__ set in plugin is invalid. */
445 ctx->errline = 0;
446 ctx->errfile = NULL;
447 } else {
448 const char *label;
449 label = grn_dl_close_error_label();
450 SERR("%s", label);
451 }
452 id = GRN_ID_NIL;
453 } else {
454 (*plugin)->refcount = 1;
455 }
456 } else {
457 if (!grn_dl_close(dl)) {
458 const char *label;
459 label = grn_dl_close_error_label();
460 SERR("%s", label);
461 }
462 }
463 } else {
464 const char *label;
465 label = grn_dl_open_error_label();
466 SERR("%s: <%.*s>", label, (int)filename_size, filename);
467 }
468
469 exit:
470 CRITICAL_SECTION_LEAVE(grn_plugins_lock);
471
472 return id;
473 }
474
475 grn_rc
476 grn_plugin_close(grn_ctx *ctx, grn_id id)
477 {
478 grn_ctx *plugins_ctx = &grn_plugins_ctx;
479 grn_rc rc;
480 grn_plugin *plugin;
481
482 if (id == GRN_ID_NIL) {
483 return GRN_INVALID_ARGUMENT;
484 }
485
486 CRITICAL_SECTION_ENTER(grn_plugins_lock);
487 if (!grn_hash_get_value(plugins_ctx, grn_plugins, id, &plugin)) {
488 rc = GRN_INVALID_ARGUMENT;
489 goto exit;
490 }
491 if (--plugin->refcount) {
492 rc = GRN_SUCCESS;
493 goto exit;
494 }
495 if (plugin->dl) {
496 grn_plugin_call_fin(ctx, id);
497 if (!grn_dl_close(plugin->dl)) {
498 const char *label;
499 label = grn_dl_close_error_label();
500 SERR("%s", label);
501 }
502 }
503 {
504 grn_ctx *ctx = plugins_ctx;
505 GRN_FREE(plugin);
506 }
507 rc = grn_hash_delete_by_id(plugins_ctx, grn_plugins, id, NULL);
508
509 exit:
510 CRITICAL_SECTION_LEAVE(grn_plugins_lock);
511
512 return rc;
513 }
514
515 void *
516 grn_plugin_sym(grn_ctx *ctx, grn_id id, const char *symbol)
517 {
518 grn_plugin *plugin;
519 grn_dl_symbol func;
520
521 if (id == GRN_ID_NIL) {
522 return NULL;
523 }
524
525 CRITICAL_SECTION_ENTER(grn_plugins_lock);
526 if (!grn_hash_get_value(&grn_plugins_ctx, grn_plugins, id, &plugin)) {
527 func = NULL;
528 goto exit;
529 }
530 grn_dl_clear_error();
531 if (!(func = grn_dl_sym(plugin->dl, symbol))) {
532 const char *label;
533 label = grn_dl_sym_error_label();
534 SERR("%s", label);
535 }
536
537 exit:
538 CRITICAL_SECTION_LEAVE(grn_plugins_lock);
539
540 return func;
541 }
542
543 #ifdef WIN32
544 static void
545 grn_plugins_init_path(grn_ctx *ctx, grn_obj *path, const char *path_env)
546 {
547 /* TODO */
548 }
549 #else /* WIN32 */
550 static void
551 grn_plugins_init_path(grn_ctx *ctx, grn_obj *path, const char *path_env)
552 {
553 const char separator = ':';
554 const char *start = path_env;
555 const char *current;
556
557 for (current = path_env; current[0]; current++) {
558 if (current[0] == separator) {
559 if (current - start > 0) {
560 grn_vector_add_element(&grn_plugins_ctx,
561 &grn_plugins_path,
562 start,
563 (uint32_t)(current - start),
564 0,
565 GRN_DB_TEXT);
566 }
567 start = current + 1;
568 }
569 }
570
571 if (start[0] && current - start > 0) {
572 grn_vector_add_element(&grn_plugins_ctx,
573 &grn_plugins_path,
574 start,
575 (uint32_t)(current - start),
576 0,
577 GRN_DB_TEXT);
578 }
579 }
580 #endif /* WIN32 */
581
582 #ifdef WIN32
583 static char *windows_plugins_dir = NULL;
584 static char windows_plugins_dir_buffer[PATH_MAX];
585 static const char *
586 grn_plugin_get_default_system_plugins_dir(void)
587 {
588 if (!windows_plugins_dir) {
589 const char *base_dir;
590 const char *relative_path = GRN_RELATIVE_PLUGINS_DIR;
591 size_t base_dir_length;
592
593 base_dir = grn_windows_base_dir();
594 base_dir_length = strlen(base_dir);
595 grn_strcpy(windows_plugins_dir_buffer, PATH_MAX, base_dir);
596 grn_strcat(windows_plugins_dir_buffer, PATH_MAX, "/");
597 grn_strcat(windows_plugins_dir_buffer, PATH_MAX, relative_path);
598 windows_plugins_dir = windows_plugins_dir_buffer;
599 }
600 return windows_plugins_dir;
601 }
602
603 #else /* WIN32 */
604 static const char *
605 grn_plugin_get_default_system_plugins_dir(void)
606 {
607 return GRN_PLUGINS_DIR;
608 }
609 #endif /* WIN32 */
610
611 const char *
612 grn_plugin_get_system_plugins_dir(void)
613 {
614 if (grn_plugins_dir_env[0]) {
615 return grn_plugins_dir_env;
616 } else {
617 return grn_plugin_get_default_system_plugins_dir();
618 }
619 }
620
621 grn_rc
622 grn_plugins_init(void)
623 {
624 CRITICAL_SECTION_INIT(grn_plugins_lock);
625 grn_ctx_init(&grn_plugins_ctx, 0);
626 GRN_TEXT_INIT(&grn_plugins_path, GRN_OBJ_VECTOR);
627 grn_plugins = grn_hash_create(&grn_plugins_ctx,
628 NULL,
629 PATH_MAX,
630 sizeof(grn_plugin *),
631 GRN_OBJ_KEY_VAR_SIZE);
632 if (!grn_plugins) {
633 GRN_OBJ_FIN(&grn_plugins_ctx, &grn_plugins_path);
634 grn_ctx_fin(&grn_plugins_ctx);
635 return GRN_NO_MEMORY_AVAILABLE;
636 }
637 if (grn_plugins_path_env[0]) {
638 grn_plugins_init_path(&grn_plugins_ctx,
639 &grn_plugins_path,
640 grn_plugins_path_env);
641 }
642 {
643 const char *system_plugins_dir = grn_plugin_get_system_plugins_dir();
644 grn_vector_add_element(&grn_plugins_ctx,
645 &grn_plugins_path,
646 system_plugins_dir,
647 (uint32_t)strlen(system_plugins_dir),
648 0,
649 GRN_DB_TEXT);
650 }
651 return GRN_SUCCESS;
652 }
653
654 grn_rc
655 grn_plugins_fin(void)
656 {
657 grn_rc rc;
658 if (!grn_plugins) {
659 return GRN_INVALID_ARGUMENT;
660 }
661 GRN_HASH_EACH(&grn_plugins_ctx, grn_plugins, id, NULL, NULL, NULL, {
662 grn_plugin_close(&grn_plugins_ctx, id);
663 });
664 rc = grn_hash_close(&grn_plugins_ctx, grn_plugins);
665 GRN_OBJ_FIN(&grn_plugins_ctx, &grn_plugins_path);
666 grn_ctx_fin(&grn_plugins_ctx);
667 CRITICAL_SECTION_FIN(grn_plugins_lock);
668 return rc;
669 }
670
671 const char *
672 grn_plugin_get_suffix(void)
673 {
674 return GRN_PLUGIN_SUFFIX;
675 }
676
677 const char *
678 grn_plugin_get_ruby_suffix(void)
679 {
680 return ".rb";
681 }
682
683 grn_rc
684 grn_plugin_register_by_path(grn_ctx *ctx, const char *path)
685 {
686 grn_obj *db;
687 if (!ctx || !ctx->impl || !(db = ctx->impl->db)) {
688 ERR(GRN_INVALID_ARGUMENT, "db not initialized");
689 return ctx->rc;
690 }
691 GRN_API_ENTER;
692 if (GRN_DB_P(db)) {
693 grn_id id;
694 id = grn_plugin_open(ctx, path);
695 if (id) {
696 ctx->impl->plugin_path = path;
697 ctx->rc = grn_plugin_call_register(ctx, id);
698 ctx->impl->plugin_path = NULL;
699 grn_plugin_close(ctx, id);
700 }
701 } else {
702 ERR(GRN_INVALID_ARGUMENT, "invalid db assigned");
703 }
704 GRN_API_RETURN(ctx->rc);
705 }
706
707 static char *
708 grn_plugin_find_path_raw(grn_ctx *ctx, const char *path)
709 {
710 struct stat path_stat;
711
712 if (stat(path, &path_stat) != 0) {
713 return NULL;
714 }
715
716 if (!S_ISREG(path_stat.st_mode)) {
717 return NULL;
718 }
719
720 return GRN_STRDUP(path);
721 }
722
723 #ifdef GRN_WITH_MRUBY
724 static char *
725 grn_plugin_find_path_mrb(grn_ctx *ctx, const char *path, size_t path_len)
726 {
727 char mrb_path[PATH_MAX];
728 const char *mrb_suffix;
729 size_t mrb_path_len;
730
731 grn_ctx_impl_mrb_ensure_init(ctx);
732 if (ctx->rc != GRN_SUCCESS) {
733 return NULL;
734 }
735
736 if (!ctx->impl->mrb.state) {
737 return NULL;
738 }
739
740 mrb_suffix = grn_plugin_get_ruby_suffix();
741 mrb_path_len = path_len + strlen(mrb_suffix);
742 if (mrb_path_len >= PATH_MAX) {
743 ERR(GRN_FILENAME_TOO_LONG,
744 "too long plugin path: <%s%s>",
745 path,
746 mrb_suffix);
747 return NULL;
748 }
749
750 grn_strcpy(mrb_path, PATH_MAX, path);
751 grn_strcat(mrb_path, PATH_MAX, mrb_suffix);
752 return grn_plugin_find_path_raw(ctx, mrb_path);
753 }
754 #else /* GRN_WITH_MRUBY */
755 static char *
756 grn_plugin_find_path_mrb(grn_ctx *ctx, const char *path, size_t path_len)
757 {
758 return NULL;
759 }
760 #endif /* GRN_WITH_MRUBY */
761
762 static char *
763 grn_plugin_find_path_so(grn_ctx *ctx, const char *path, size_t path_len)
764 {
765 char so_path[PATH_MAX];
766 const char *so_suffix;
767 size_t so_path_len;
768
769 so_suffix = grn_plugin_get_suffix();
770 so_path_len = path_len + strlen(so_suffix);
771 if (so_path_len >= PATH_MAX) {
772 ERR(GRN_FILENAME_TOO_LONG, "too long plugin path: <%s%s>", path, so_suffix);
773 return NULL;
774 }
775
776 grn_strcpy(so_path, PATH_MAX, path);
777 grn_strcat(so_path, PATH_MAX, so_suffix);
778 return grn_plugin_find_path_raw(ctx, so_path);
779 }
780
781 static char *
782 grn_plugin_find_path_libs_so(grn_ctx *ctx, const char *path, size_t path_len)
783 {
784 char libs_so_path[PATH_MAX];
785 const char *base_name;
786 const char *so_suffix;
787 const char *libs_path = "/.libs";
788 size_t libs_so_path_len;
789
790 base_name = strrchr(path, '/');
791 if (!base_name) {
792 return NULL;
793 }
794
795 so_suffix = grn_plugin_get_suffix();
796 libs_so_path_len = (size_t)(base_name - path) + strlen(libs_path) +
797 strlen(base_name) + strlen(so_suffix);
798 if (libs_so_path_len >= PATH_MAX) {
799 ERR(GRN_FILENAME_TOO_LONG,
800 "too long plugin path: <%.*s/.libs%s%s>",
801 (int)(base_name - path),
802 path,
803 base_name,
804 so_suffix);
805 return NULL;
806 }
807
808 libs_so_path[0] = '\0';
809 grn_strncat(libs_so_path, PATH_MAX, path, (size_t)(base_name - path));
810 grn_strcat(libs_so_path, PATH_MAX, libs_path);
811 grn_strcat(libs_so_path, PATH_MAX, base_name);
812 grn_strcat(libs_so_path, PATH_MAX, so_suffix);
813 return grn_plugin_find_path_raw(ctx, libs_so_path);
814 }
815
816 static char *
817 grn_plugin_find_path_one(grn_ctx *ctx, const char *path)
818 {
819 char *found_path = NULL;
820 size_t path_len;
821
822 found_path = grn_plugin_find_path_raw(ctx, path);
823 if (found_path) {
824 goto exit;
825 }
826
827 path_len = strlen(path);
828
829 found_path = grn_plugin_find_path_so(ctx, path, path_len);
830 if (found_path) {
831 goto exit;
832 }
833 if (ctx->rc) {
834 goto exit;
835 }
836
837 found_path = grn_plugin_find_path_libs_so(ctx, path, path_len);
838 if (found_path) {
839 goto exit;
840 }
841 if (ctx->rc) {
842 goto exit;
843 }
844
845 found_path = grn_plugin_find_path_mrb(ctx, path, path_len);
846 if (found_path) {
847 goto exit;
848 }
849 if (ctx->rc) {
850 goto exit;
851 }
852
853 exit:
854 return found_path;
855 }
856
857 static bool
858 grn_plugin_find_path_is_absolute_path(grn_ctx *ctx, const char *name)
859 {
860 /* UNIX */
861 if (name[0] == '/') {
862 return true;
863 }
864
865 /* Windows */
866 if ((('a' <= name[0] && name[0] <= 'z') ||
867 ('A' <= name[0] && name[0] <= 'Z')) &&
868 name[1] == ':' && name[2] == '/') {
869 return true;
870 }
871
872 return false;
873 }
874
875 char *
876 grn_plugin_find_path(grn_ctx *ctx, const char *name)
877 {
878 char *found_path = NULL;
879
880 GRN_API_ENTER;
881
882 if (grn_plugin_find_path_is_absolute_path(ctx, name)) {
883 found_path = grn_plugin_find_path_one(ctx, name);
884 } else {
885 GRN_PLUGINS_DIR_EACH_BEGIN(dir, dir_length)
886 {
887 char dir_last_char;
888 char path[PATH_MAX];
889 size_t name_length, max_name_length;
890
891 grn_strncpy(path, PATH_MAX, dir, dir_length);
892 path[dir_length] = '\0';
893
894 dir_last_char = dir[dir_length - 1];
895 if (dir_last_char != '/') {
896 grn_strcat(path, PATH_MAX, "/");
897 }
898
899 name_length = strlen(name);
900 max_name_length = PATH_MAX - strlen(path) - 1;
901 if (name_length > max_name_length) {
902 ERR(GRN_INVALID_ARGUMENT,
903 "plugin name is too long: %" GRN_FMT_SIZE " (max: %" GRN_FMT_SIZE
904 ") <%s%s>",
905 name_length,
906 max_name_length,
907 path,
908 name);
909 break;
910 }
911 grn_strcat(path, PATH_MAX, name);
912
913 found_path = grn_plugin_find_path_one(ctx, path);
914 if (found_path) {
915 break;
916 }
917 if (ctx->rc != GRN_SUCCESS) {
918 break;
919 }
920 }
921 GRN_PLUGINS_DIR_EACH_END();
922 }
923
924 GRN_API_RETURN(found_path);
925 }
926
927 static void
928 grn_plugin_set_name_resolve_error(grn_ctx *ctx,
929 const char *name,
930 const char *tag)
931 {
932 if (name[0] == '/') {
933 ERR(GRN_NO_SUCH_FILE_OR_DIRECTORY,
934 "%s cannot find plugin file: <%s>",
935 tag,
936 name);
937 } else {
938 const char *grn_encoding_plugins_dir;
939 size_t grn_encoding_plugins_dir_length;
940 const char *plugins_dir_path_separator;
941
942 grn_encoding_plugins_dir =
943 grn_encoding_convert_from_locale(ctx,
944 grn_plugin_get_system_plugins_dir(),
945 -1,
946 &grn_encoding_plugins_dir_length);
947 if (grn_encoding_plugins_dir[grn_encoding_plugins_dir_length - 1] != '/') {
948 plugins_dir_path_separator = "/";
949 } else {
950 plugins_dir_path_separator = "";
951 }
952 ERR(GRN_NO_SUCH_FILE_OR_DIRECTORY,
953 "%s cannot find plugin file: <%s%s%s%s>",
954 tag,
955 grn_encoding_plugins_dir,
956 plugins_dir_path_separator,
957 name,
958 grn_plugin_get_suffix());
959 grn_encoding_converted_free(ctx, grn_encoding_plugins_dir);
960 }
961 }
962
963 grn_rc
964 grn_plugin_register(grn_ctx *ctx, const char *name)
965 {
966 grn_rc rc;
967 char *path;
968
969 GRN_API_ENTER;
970 path = grn_plugin_find_path(ctx, name);
971 if (path) {
972 rc = grn_plugin_register_by_path(ctx, path);
973 GRN_FREE(path);
974 } else {
975 if (ctx->rc == GRN_SUCCESS) {
976 grn_plugin_set_name_resolve_error(ctx, name, "[plugin][register]");
977 }
978 rc = ctx->rc;
979 }
980 GRN_API_RETURN(rc);
981 }
982
983 grn_rc
984 grn_plugin_unregister_by_path(grn_ctx *ctx, const char *path)
985 {
986 grn_obj *db;
987 grn_id plugin_id;
988
989 if (!ctx || !ctx->impl) {
990 ERR(GRN_INVALID_ARGUMENT, "[plugin][unregister] ctx isn't initialized");
991 return ctx->rc;
992 }
993
994 db = ctx->impl->db;
995 if (!db) {
996 ERR(GRN_INVALID_ARGUMENT, "[plugin][unregister] DB isn't initialized");
997 return ctx->rc;
998 }
999
1000 GRN_API_ENTER;
1001
1002 CRITICAL_SECTION_ENTER(grn_plugins_lock);
1003 plugin_id = grn_hash_get(&grn_plugins_ctx,
1004 grn_plugins,
1005 path,
1006 (uint32_t)GRN_PLUGIN_KEY_SIZE(path),
1007 NULL);
1008 CRITICAL_SECTION_LEAVE(grn_plugins_lock);
1009
1010 if (plugin_id == GRN_ID_NIL) {
1011 GRN_API_RETURN(ctx->rc);
1012 }
1013
1014 {
1015 grn_table_cursor *cursor;
1016 grn_id id;
1017
1018 cursor =
1019 grn_table_cursor_open(ctx, db, NULL, 0, NULL, 0, 0, -1, GRN_CURSOR_BY_ID);
1020 if (!cursor) {
1021 GRN_API_RETURN(ctx->rc);
1022 }
1023
1024 while ((id = grn_table_cursor_next(ctx, cursor))) {
1025 grn_obj *obj;
1026 obj = grn_ctx_at(ctx, id);
1027 if (!obj) {
1028 continue;
1029 }
1030 if (obj->header.type == GRN_PROC && DB_OBJ(obj)->range == plugin_id) {
1031 grn_obj_remove(ctx, obj);
1032 } else {
1033 grn_obj_unlink(ctx, obj);
1034 }
1035 }
1036 grn_table_cursor_close(ctx, cursor);
1037 }
1038
1039 GRN_API_RETURN(ctx->rc);
1040 }
1041
1042 grn_rc
1043 grn_plugin_unregister(grn_ctx *ctx, const char *name)
1044 {
1045 grn_rc rc;
1046 char *path;
1047
1048 GRN_API_ENTER;
1049 path = grn_plugin_find_path(ctx, name);
1050 if (path) {
1051 rc = grn_plugin_unregister_by_path(ctx, path);
1052 GRN_FREE(path);
1053 } else {
1054 if (ctx->rc == GRN_SUCCESS) {
1055 grn_plugin_set_name_resolve_error(ctx, name, "[plugin][unregister]");
1056 }
1057 rc = ctx->rc;
1058 }
1059 GRN_API_RETURN(rc);
1060 }
1061
1062 void
1063 grn_plugin_ensure_registered(grn_ctx *ctx, grn_obj *proc)
1064 {
1065 #ifdef GRN_WITH_MRUBY
1066 grn_id plugin_id;
1067 grn_plugin *plugin = NULL;
1068
1069 if (!(proc->header.flags & GRN_OBJ_CUSTOM_NAME)) {
1070 return;
1071 }
1072
1073 plugin_id = DB_OBJ(proc)->range;
1074 CRITICAL_SECTION_ENTER(grn_plugins_lock);
1075 {
1076 const char *value;
1077 value = grn_hash_get_value_(&grn_plugins_ctx, grn_plugins, plugin_id, NULL);
1078 if (value) {
1079 plugin = *((grn_plugin **)value);
1080 }
1081 }
1082 CRITICAL_SECTION_LEAVE(grn_plugins_lock);
1083
1084 if (!plugin) {
1085 return;
1086 }
1087
1088 if (plugin->dl) {
1089 return;
1090 }
1091
1092 grn_ctx_impl_mrb_ensure_init(ctx);
1093 if (ctx->rc != GRN_SUCCESS) {
1094 return;
1095 }
1096
1097 if (!ctx->impl->mrb.state) {
1098 return;
1099 }
1100
1101 {
1102 grn_id id;
1103 int added;
1104 id = DB_OBJ(proc)->id;
1105 grn_hash_add(ctx,
1106 ctx->impl->mrb.checked_procs,
1107 &id,
1108 sizeof(grn_id),
1109 NULL,
1110 &added);
1111 if (!added) {
1112 return;
1113 }
1114 }
1115
1116 ctx->impl->plugin_path = plugin->path;
1117 grn_plugin_call_register_mrb(ctx, plugin_id, plugin);
1118 ctx->impl->plugin_path = NULL;
1119 #endif /* GRN_WITH_MRUBY */
1120 }
1121
1122 grn_rc
1123 grn_plugin_get_names(grn_ctx *ctx, grn_obj *names)
1124 {
1125 grn_hash *processed_paths;
1126 const char *system_plugins_dir;
1127 const char *native_plugin_suffix;
1128 const char *ruby_plugin_suffix;
1129 bool is_close_opened_object_mode = false;
1130
1131 GRN_API_ENTER;
1132
1133 if (ctx->rc) {
1134 GRN_API_RETURN(ctx->rc);
1135 }
1136
1137 if (grn_thread_get_limit() == 1) {
1138 is_close_opened_object_mode = true;
1139 }
1140
1141 processed_paths =
1142 grn_hash_create(ctx,
1143 NULL,
1144 GRN_TABLE_MAX_KEY_SIZE,
1145 0,
1146 GRN_OBJ_TABLE_HASH_KEY | GRN_OBJ_KEY_VAR_SIZE);
1147 if (!processed_paths) {
1148 GRN_API_RETURN(ctx->rc);
1149 }
1150
1151 system_plugins_dir = grn_plugin_get_system_plugins_dir();
1152 native_plugin_suffix = grn_plugin_get_suffix();
1153 ruby_plugin_suffix = grn_plugin_get_ruby_suffix();
1154
1155 GRN_TABLE_EACH_BEGIN_FLAGS(ctx,
1156 grn_ctx_db(ctx),
1157 cursor,
1158 id,
1159 GRN_CURSOR_BY_ID | GRN_CURSOR_ASCENDING)
1160 {
1161 void *name;
1162 int name_size;
1163 grn_obj *object;
1164 const char *path;
1165 grn_id processed_path_id;
1166
1167 if (grn_id_is_builtin(ctx, id)) {
1168 continue;
1169 }
1170
1171 name_size = grn_table_cursor_get_key(ctx, cursor, &name);
1172 if (grn_obj_name_is_column(ctx, name, name_size)) {
1173 continue;
1174 }
1175
1176 if (is_close_opened_object_mode) {
1177 grn_ctx_push_temporary_open_space(ctx);
1178 }
1179
1180 object = grn_ctx_at(ctx, id);
1181 if (!object) {
1182 ERRCLR(ctx);
1183 goto next_loop;
1184 }
1185
1186 if (!grn_obj_is_proc(ctx, object)) {
1187 goto next_loop;
1188 }
1189
1190 path = grn_obj_path(ctx, object);
1191 if (!path) {
1192 goto next_loop;
1193 }
1194
1195 processed_path_id =
1196 grn_hash_get(ctx, processed_paths, path, (uint32_t)strlen(path), NULL);
1197 if (processed_path_id != GRN_ID_NIL) {
1198 goto next_loop;
1199 }
1200
1201 grn_hash_add(ctx,
1202 processed_paths,
1203 path,
1204 (uint32_t)strlen(path),
1205 NULL,
1206 NULL);
1207
1208 {
1209 const char *relative_path;
1210 const char *libs_path = "/.libs/";
1211 const char *start_libs;
1212 char name[PATH_MAX];
1213
1214 name[0] = '\0';
1215 if (strncmp(path, system_plugins_dir, strlen(system_plugins_dir)) == 0) {
1216 relative_path = path + strlen(system_plugins_dir);
1217 } else {
1218 relative_path = path;
1219 }
1220 start_libs = strstr(relative_path, libs_path);
1221 if (start_libs) {
1222 grn_strncat(name,
1223 PATH_MAX,
1224 relative_path,
1225 (size_t)(start_libs - relative_path));
1226 grn_strcat(name, PATH_MAX, "/");
1227 grn_strcat(name, PATH_MAX, start_libs + strlen(libs_path));
1228 } else {
1229 grn_strcat(name, PATH_MAX, relative_path);
1230 }
1231 if (strlen(name) > strlen(native_plugin_suffix) &&
1232 strcmp(name + strlen(name) - strlen(native_plugin_suffix),
1233 native_plugin_suffix) == 0) {
1234 name[strlen(name) - strlen(native_plugin_suffix)] = '\0';
1235 } else if (strlen(name) > strlen(ruby_plugin_suffix) &&
1236 strcmp(name + strlen(name) - strlen(ruby_plugin_suffix),
1237 ruby_plugin_suffix) == 0) {
1238 name[strlen(name) - strlen(ruby_plugin_suffix)] = '\0';
1239 }
1240 grn_vector_add_element(ctx,
1241 names,
1242 name,
1243 (uint32_t)strlen(name),
1244 0,
1245 GRN_DB_TEXT);
1246 }
1247
1248 next_loop:
1249 if (is_close_opened_object_mode) {
1250 grn_ctx_pop_temporary_open_space(ctx);
1251 }
1252 }
1253 GRN_TABLE_EACH_END(ctx, cursor);
1254
1255 grn_hash_close(ctx, processed_paths);
1256
1257 GRN_API_RETURN(ctx->rc);
1258 }
1259
1260 void *
1261 grn_plugin_malloc(
1262 grn_ctx *ctx, size_t size, const char *file, int line, const char *func)
1263 {
1264 return grn_malloc(ctx, size, file, line, func);
1265 }
1266
1267 void *
1268 grn_plugin_calloc(
1269 grn_ctx *ctx, size_t size, const char *file, int line, const char *func)
1270 {
1271 return grn_calloc(ctx, size, file, line, func);
1272 }
1273
1274 void *
1275 grn_plugin_realloc(grn_ctx *ctx,
1276 void *ptr,
1277 size_t size,
1278 const char *file,
1279 int line,
1280 const char *func)
1281 {
1282 return grn_realloc(ctx, ptr, size, file, line, func);
1283 }
1284
1285 void
1286 grn_plugin_free(
1287 grn_ctx *ctx, void *ptr, const char *file, int line, const char *func)
1288 {
1289 grn_free(ctx, ptr, file, line, func);
1290 }
1291
1292 void
1293 grn_plugin_set_error(grn_ctx *ctx,
1294 grn_log_level level,
1295 grn_rc error_code,
1296 const char *file,
1297 int line,
1298 const char *func,
1299 const char *format,
1300 ...)
1301 {
1302 char old_error_message[GRN_CTX_MSGSIZE];
1303
1304 ctx->errlvl = (unsigned char)level;
1305 ctx->rc = error_code;
1306 ctx->errfile = file;
1307 ctx->errline = (unsigned int)line;
1308 ctx->errfunc = func;
1309
1310 grn_strcpy(old_error_message, GRN_CTX_MSGSIZE, ctx->errbuf);
1311
1312 {
1313 va_list ap;
1314 va_start(ap, format);
1315 grn_ctx_logv(ctx, format, ap);
1316 va_end(ap);
1317 }
1318
1319 if (grn_ctx_impl_should_log(ctx)) {
1320 grn_ctx_impl_set_current_error_message(ctx);
1321 if (grn_logger_pass(ctx, level)) {
1322 char new_error_message[GRN_CTX_MSGSIZE];
1323 grn_strcpy(new_error_message, GRN_CTX_MSGSIZE, ctx->errbuf);
1324 grn_strcpy(ctx->errbuf, GRN_CTX_MSGSIZE, old_error_message);
1325 {
1326 va_list ap;
1327 va_start(ap, format);
1328 grn_logger_putv(ctx, level, file, line, func, format, ap);
1329 va_end(ap);
1330 }
1331 grn_strcpy(ctx->errbuf, GRN_CTX_MSGSIZE, new_error_message);
1332 }
1333 if (level <= GRN_LOG_ERROR) {
1334 grn_plugin_logtrace(ctx, level);
1335 }
1336 }
1337 }
1338
1339 void
1340 grn_plugin_clear_error(grn_ctx *ctx)
1341 {
1342 ERRCLR(ctx);
1343 }
1344
1345 void
1346 grn_plugin_backtrace(grn_ctx *ctx)
1347 {
1348 }
1349
1350 void
1351 grn_plugin_logtrace(grn_ctx *ctx, grn_log_level level)
1352 {
1353 grn_ctx_log_back_trace(ctx, level);
1354 }
1355
1356 struct _grn_plugin_mutex {
1357 grn_critical_section critical_section;
1358 };
1359
1360 grn_plugin_mutex *
1361 grn_plugin_mutex_open(grn_ctx *ctx)
1362 {
1363 grn_plugin_mutex *const mutex =
1364 GRN_PLUGIN_MALLOC(ctx, sizeof(grn_plugin_mutex));
1365 if (mutex != NULL) {
1366 CRITICAL_SECTION_INIT(mutex->critical_section);
1367 }
1368 return mutex;
1369 }
1370
1371 grn_plugin_mutex *
1372 grn_plugin_mutex_create(grn_ctx *ctx)
1373 {
1374 return grn_plugin_mutex_open(ctx);
1375 }
1376
1377 void
1378 grn_plugin_mutex_close(grn_ctx *ctx, grn_plugin_mutex *mutex)
1379 {
1380 if (mutex != NULL) {
1381 CRITICAL_SECTION_FIN(mutex->critical_section);
1382 GRN_PLUGIN_FREE(ctx, mutex);
1383 }
1384 }
1385
1386 void
1387 grn_plugin_mutex_destroy(grn_ctx *ctx, grn_plugin_mutex *mutex)
1388 {
1389 grn_plugin_mutex_close(ctx, mutex);
1390 }
1391
1392 void
1393 grn_plugin_mutex_lock(grn_ctx *ctx, grn_plugin_mutex *mutex)
1394 {
1395 if (mutex != NULL) {
1396 CRITICAL_SECTION_ENTER(mutex->critical_section);
1397 }
1398 }
1399
1400 void
1401 grn_plugin_mutex_unlock(grn_ctx *ctx, grn_plugin_mutex *mutex)
1402 {
1403 if (mutex != NULL) {
1404 CRITICAL_SECTION_LEAVE(mutex->critical_section);
1405 }
1406 }
1407
1408 grn_obj *
1409 grn_plugin_proc_alloc(grn_ctx *ctx,
1410 grn_user_data *user_data,
1411 grn_id domain,
1412 unsigned char flags)
1413 {
1414 return grn_proc_alloc(ctx, user_data, domain, flags);
1415 }
1416
1417 grn_obj *
1418 grn_plugin_proc_get_vars(grn_ctx *ctx, grn_user_data *user_data)
1419 {
1420 return grn_proc_get_vars(ctx, user_data);
1421 }
1422
1423 grn_obj *
1424 grn_plugin_proc_get_var(grn_ctx *ctx,
1425 grn_user_data *user_data,
1426 const char *name,
1427 int name_size)
1428 {
1429 name_size = compute_name_size(name, name_size);
1430 return grn_proc_get_var(ctx, user_data, name, (unsigned int)name_size);
1431 }
1432
1433 bool
1434 grn_plugin_proc_get_var_bool(grn_ctx *ctx,
1435 grn_user_data *user_data,
1436 const char *name,
1437 int name_size,
1438 bool default_value)
1439 {
1440 grn_obj *var;
1441
1442 var = grn_plugin_proc_get_var(ctx, user_data, name, name_size);
1443 return grn_proc_option_value_bool(ctx, var, default_value);
1444 }
1445
1446 int32_t
1447 grn_plugin_proc_get_var_int32(grn_ctx *ctx,
1448 grn_user_data *user_data,
1449 const char *name,
1450 int name_size,
1451 int32_t default_value)
1452 {
1453 grn_obj *var;
1454
1455 var = grn_plugin_proc_get_var(ctx, user_data, name, name_size);
1456 return grn_proc_option_value_int32(ctx, var, default_value);
1457 }
1458
1459 double
1460 grn_plugin_proc_get_var_double(grn_ctx *ctx,
1461 grn_user_data *user_data,
1462 const char *name,
1463 int name_size,
1464 double default_value)
1465 {
1466 grn_obj *var;
1467
1468 var = grn_plugin_proc_get_var(ctx, user_data, name, name_size);
1469 return grn_proc_option_value_double(ctx, var, default_value);
1470 }
1471
1472 const char *
1473 grn_plugin_proc_get_var_string(grn_ctx *ctx,
1474 grn_user_data *user_data,
1475 const char *name,
1476 int name_size,
1477 size_t *size)
1478 {
1479 grn_obj *var;
1480
1481 var = grn_plugin_proc_get_var(ctx, user_data, name, name_size);
1482 return grn_proc_option_value_string(ctx, var, size);
1483 }
1484
1485 grn_content_type
1486 grn_plugin_proc_get_var_content_type(grn_ctx *ctx,
1487 grn_user_data *user_data,
1488 const char *name,
1489 int name_size,
1490 grn_content_type default_value)
1491 {
1492 grn_obj *var;
1493
1494 var = grn_plugin_proc_get_var(ctx, user_data, name, name_size);
1495 return grn_proc_option_value_content_type(ctx, var, default_value);
1496 }
1497
1498 grn_log_level
1499 grn_plugin_proc_get_var_log_level(grn_ctx *ctx,
1500 grn_user_data *user_data,
1501 const char *name,
1502 int name_size,
1503 grn_log_level default_value)
1504 {
1505 grn_obj *var;
1506
1507 var = grn_plugin_proc_get_var(ctx, user_data, name, name_size);
1508 return grn_proc_option_value_log_level(ctx, var, default_value);
1509 }
1510
1511 grn_obj *
1512 grn_plugin_proc_get_var_by_offset(grn_ctx *ctx,
1513 grn_user_data *user_data,
1514 unsigned int offset)
1515 {
1516 return grn_proc_get_var_by_offset(ctx, user_data, offset);
1517 }
1518
1519 grn_obj *
1520 grn_plugin_proc_get_caller(grn_ctx *ctx, grn_user_data *user_data)
1521 {
1522 grn_obj *caller = NULL;
1523 GRN_API_ENTER;
1524 grn_proc_get_info(ctx, user_data, NULL, NULL, &caller);
1525 GRN_API_RETURN(caller);
1526 }
1527
1528 const char *
1529 grn_plugin_win32_base_dir(void)
1530 {
1531 return grn_plugin_windows_base_dir();
1532 }
1533
1534 const char *
1535 grn_plugin_windows_base_dir(void)
1536 {
1537 #ifdef WIN32
1538 return grn_windows_base_dir();
1539 #else /* WIN32 */
1540 return NULL;
1541 #endif /* WIN32 */
1542 }
1543
1544 /*
1545 grn_plugin_charlen() takes the length of a string, unlike grn_charlen_().
1546 */
1547 int
1548 grn_plugin_charlen(grn_ctx *ctx,
1549 const char *str_ptr,
1550 unsigned int str_length,
1551 grn_encoding encoding)
1552 {
1553 return grn_charlen_(ctx, str_ptr, str_ptr + str_length, encoding);
1554 }
1555
1556 /*
1557 grn_plugin_isspace() takes the length of a string, unlike grn_isspace().
1558 */
1559 int
1560 grn_plugin_isspace(grn_ctx *ctx,
1561 const char *str_ptr,
1562 unsigned int str_length,
1563 grn_encoding encoding)
1564 {
1565 if ((str_ptr == NULL) || (str_length == 0)) {
1566 return 0;
1567 }
1568 switch ((unsigned char)str_ptr[0]) {
1569 case ' ':
1570 case '\f':
1571 case '\n':
1572 case '\r':
1573 case '\t':
1574 case '\v':
1575 return 1;
1576 case 0x81:
1577 if ((encoding == GRN_ENC_SJIS) && (str_length >= 2) &&
1578 ((unsigned char)str_ptr[1] == 0x40)) {
1579 return 2;
1580 }
1581 break;
1582 case 0xA1:
1583 if ((encoding == GRN_ENC_EUC_JP) && (str_length >= 2) &&
1584 ((unsigned char)str_ptr[1] == 0xA1)) {
1585 return 2;
1586 }
1587 break;
1588 case 0xE3:
1589 if ((encoding == GRN_ENC_UTF8) && (str_length >= 3) &&
1590 ((unsigned char)str_ptr[1] == 0x80) &&
1591 ((unsigned char)str_ptr[2] == 0x80)) {
1592 return 3;
1593 }
1594 break;
1595 default:
1596 break;
1597 }
1598 return 0;
1599 }
1600
1601 grn_rc
1602 grn_plugin_expr_var_init(grn_ctx *ctx,
1603 grn_expr_var *var,
1604 const char *name,
1605 int name_size)
1606 {
1607 var->name = name;
1608 var->name_size = (unsigned int)compute_name_size(name, name_size);
1609 GRN_TEXT_INIT(&var->value, 0);
1610 return GRN_SUCCESS;
1611 }
1612
1613 grn_obj *
1614 grn_plugin_command_create(grn_ctx *ctx,
1615 const char *name,
1616 int name_size,
1617 grn_proc_func func,
1618 unsigned int n_vars,
1619 grn_expr_var *vars)
1620 {
1621 grn_obj *proc;
1622 name_size = compute_name_size(name, name_size);
1623 proc = grn_proc_create(ctx,
1624 name,
1625 name_size,
1626 GRN_PROC_COMMAND,
1627 func,
1628 NULL,
1629 NULL,
1630 n_vars,
1631 vars);
1632 return proc;
1633 }
1634
1635 bool
1636 grn_plugin_proc_get_value_bool(grn_ctx *ctx,
1637 grn_obj *value,
1638 bool default_value,
1639 const char *tag)
1640 {
1641 return grn_proc_get_value_bool(ctx, value, default_value, tag);
1642 }
1643
1644 int32_t
1645 grn_plugin_proc_get_value_int32(grn_ctx *ctx,
1646 grn_obj *value,
1647 int32_t default_value_raw,
1648 const char *tag)
1649 {
1650 return grn_proc_get_value_int32(ctx, value, default_value_raw, tag);
1651 }
1652
1653 int64_t
1654 grn_plugin_proc_get_value_int64(grn_ctx *ctx,
1655 grn_obj *value,
1656 int64_t default_value_raw,
1657 const char *tag)
1658 {
1659 return grn_proc_get_value_int64(ctx, value, default_value_raw, tag);
1660 }
1661
1662 double
1663 grn_plugin_proc_get_value_double(grn_ctx *ctx,
1664 grn_obj *value,
1665 double default_value_raw,
1666 const char *tag)
1667 {
1668 return grn_proc_get_value_double(ctx, value, default_value_raw, tag);
1669 }
1670
1671 grn_operator
1672 grn_plugin_proc_get_value_mode(grn_ctx *ctx,
1673 grn_obj *value,
1674 grn_operator default_mode,
1675 const char *tag)
1676 {
1677 return grn_proc_get_value_mode(ctx, value, default_mode, tag);
1678 }
1679
1680 grn_operator
1681 grn_plugin_proc_get_value_operator(grn_ctx *ctx,
1682 grn_obj *value,
1683 grn_operator default_operator,
1684 const char *tag)
1685 {
1686 return grn_proc_get_value_operator(ctx, value, default_operator, tag);
1687 }