31 static grn_critical_section grn_plugins_lock;
33 #define PATHLEN(filename) (strlen(filename) + 1)
37 # define grn_dl_open(filename) dlopen(filename, RTLD_LAZY | RTLD_LOCAL)
38 # define grn_dl_open_error_label() dlerror()
39 # define grn_dl_close(dl) (dlclose(dl) == 0)
40 # define grn_dl_close_error_label() dlerror()
41 # define grn_dl_sym(dl, symbol) dlsym(dl, symbol)
42 # define grn_dl_sym_error_label() dlerror()
43 # define grn_dl_clear_error() dlerror()
45 # define grn_dl_open(filename) LoadLibrary(filename)
46 # define grn_dl_open_error_label() "LoadLibrary"
47 # define grn_dl_close(dl) (FreeLibrary(dl) != 0)
48 # define grn_dl_close_error_label() "FreeLibrary"
49 # define grn_dl_sym(dl, symbol) ((void *)GetProcAddress(dl, symbol))
50 # define grn_dl_sym_error_label() "GetProcAddress"
51 # define grn_dl_clear_error()
60 CRITICAL_SECTION_ENTER(grn_plugins_lock);
64 (*plugin)->refcount++;
66 CRITICAL_SECTION_LEAVE(grn_plugins_lock);
76 const char *system_plugins_dir;
77 size_t system_plugins_dir_size;
79 CRITICAL_SECTION_ENTER(grn_plugins_lock);
81 CRITICAL_SECTION_LEAVE(grn_plugins_lock);
88 system_plugins_dir_size = strlen(system_plugins_dir);
89 if (strncmp(system_plugins_dir, path, system_plugins_dir_size) == 0) {
90 const char *plugin_name = path + system_plugins_dir_size;
91 while (plugin_name[0] ==
'/') {
101 #define GRN_PLUGIN_FUNC_PREFIX "grn_plugin_impl_"
148 #define GET_SYMBOL(type) do { \
149 grn_dl_clear_error(); \
150 plugin->type ## _func = grn_dl_sym(dl, GRN_PLUGIN_FUNC_PREFIX #type); \
151 if (!plugin->type ## _func) { \
153 label = grn_dl_sym_error_label(); \
166 "init func (%s) %sfound, "
167 "register func (%s) %sfound and "
168 "fin func (%s) %sfound",
176 grn_plugin_call_init(ctx,
id);
190 CRITICAL_SECTION_ENTER(grn_plugins_lock);
192 (
void **)&plugin))) {
193 (*plugin)->refcount++;
199 (
void **)&plugin, NULL))) {
202 if (grn_plugin_initialize(ctx, *plugin, dl,
id, filename)) {
220 (*plugin)->refcount = 1;
236 CRITICAL_SECTION_LEAVE(grn_plugins_lock);
251 CRITICAL_SECTION_ENTER(grn_plugins_lock);
260 grn_plugin_call_fin(ctx,
id);
270 CRITICAL_SECTION_LEAVE(grn_plugins_lock);
285 CRITICAL_SECTION_ENTER(grn_plugins_lock);
298 CRITICAL_SECTION_LEAVE(grn_plugins_lock);
306 CRITICAL_SECTION_INIT(grn_plugins_lock);
322 CRITICAL_SECTION_FIN(grn_plugins_lock);
336 if (!ctx || !ctx->
impl || !(db = ctx->
impl->
db)) {
346 ctx->
rc = grn_plugin_call_register(ctx,
id);
357 static char *win32_plugins_dir = NULL;
358 static char win32_plugins_dir_buffer[
PATH_MAX];
362 if (!win32_plugins_dir) {
364 const char *relative_path = GRN_RELATIVE_PLUGINS_DIR;
366 size_t base_dir_length;
369 base_dir_length = strlen(base_dir);
370 strcpy(win32_plugins_dir_buffer, base_dir);
371 strcat(win32_plugins_dir_buffer,
"/");
372 strcat(win32_plugins_dir_buffer, relative_path);
373 win32_plugins_dir = win32_plugins_dir_buffer;
375 return win32_plugins_dir;
382 return GRN_PLUGINS_DIR;
389 const char *plugins_dir;
392 int name_length, max_name_length;
395 char *found_path = NULL;
399 if (name[0] ==
'/') {
402 plugins_dir = getenv(
"GRN_PLUGINS_DIR");
406 strcpy(path, plugins_dir);
408 dir_last_char = plugins_dir[strlen(path) - 1];
409 if (dir_last_char !=
'/') {
414 name_length = strlen(name);
415 max_name_length =
PATH_MAX - strlen(path) - 1;
416 if (name_length > max_name_length) {
418 "plugin name is too long: %d (max: %d) <%s%s>",
419 name_length, max_name_length,
425 plugin_file = fopen(path,
"r");
430 path_len = strlen(path);
434 "too long plugin path: <%s%s>",
438 strcpy(complemented_path, path);
440 plugin_file = fopen(complemented_path,
"r");
445 const char *base_name;
447 base_name = strrchr(path,
'/');
449 path_len = base_name - path + strlen(
"/.libs") + strlen(base_name);
453 "too long plugin path: <%.*s/.libs%s%s>",
457 complemented_libs_path[0] =
'\0';
458 strncat(complemented_libs_path, path, base_name - path);
459 strcat(complemented_libs_path,
"/.libs");
460 strcat(complemented_libs_path, base_name);
462 plugin_file = fopen(complemented_libs_path,
"r");
465 found_path =
GRN_STRDUP(complemented_libs_path);
488 const char *prefix, *prefix_separator, *suffix;
489 if (name[0] ==
'/') {
491 prefix_separator =
"";
494 prefix = getenv(
"GRN_PLUGINS_DIR");
498 if (prefix[strlen(prefix) - 1] !=
'/') {
499 prefix_separator =
"/";
501 prefix_separator =
"";
506 "cannot find plugin file: <%s%s%s%s>",
507 prefix, prefix_separator, name, suffix);
518 return grn_malloc(ctx, size, file, line, func);
523 const char *file,
int line,
const char *func)
525 return grn_realloc(ctx, ptr, size, file, line, func);
532 grn_free(ctx, ptr, file, line, func);
540 grn_plugin_ctx_log(
grn_ctx *ctx,
const char *format, va_list ap)
547 const char *file,
int line,
const char *func,
548 const char *format, ...)
551 ctx->
rc = error_code;
559 va_start(ap, format);
560 grn_plugin_ctx_log(ctx, format, ap);
655 return grn_charlen_(ctx, str_ptr, str_ptr + str_length, encoding);
665 if ((str_ptr == NULL) || (str_length == 0)) {
668 switch ((
unsigned char)str_ptr[0]) {
678 ((
unsigned char)str_ptr[1] == 0x40)) {
684 ((
unsigned char)str_ptr[1] == 0xA1)) {
690 ((
unsigned char)str_ptr[1] == 0x80) &&
691 ((
unsigned char)str_ptr[2] == 0x80)) {