1 /* -*- c-basic-offset: 2 -*- */
2 /*
3   Copyright(C) 2010-2017 Brazil
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 version 2.1 as published by the Free Software Foundation.
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-1335  USA
17 */
18 
19 #pragma once
20 
21 #include <stddef.h>
22 
23 #include <groonga.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 # define GRN_PLUGIN_IMPL_NAME_RAW(type)         \
30   grn_plugin_impl_ ## type
31 # define GRN_PLUGIN_IMPL_NAME_TAGGED(type, tag) \
32   GRN_PLUGIN_IMPL_NAME_RAW(type ## _ ## tag)
33 # define GRN_PLUGIN_IMPL_NAME_TAGGED_EXPANDABLE(type, tag)      \
34   GRN_PLUGIN_IMPL_NAME_TAGGED(type, tag)
35 
36 #ifdef GRN_PLUGIN_FUNCTION_TAG
37 # define GRN_PLUGIN_IMPL_NAME(type)                             \
38   GRN_PLUGIN_IMPL_NAME_TAGGED_EXPANDABLE(type, GRN_PLUGIN_FUNCTION_TAG)
39 #else /* GRN_PLUGIN_FUNCTION_TAG */
40 # define GRN_PLUGIN_IMPL_NAME(type)             \
41   GRN_PLUGIN_IMPL_NAME_RAW(type)
42 #endif /* GRN_PLUGIN_FUNCTION_TAG */
43 
44 #define GRN_PLUGIN_INIT     GRN_PLUGIN_IMPL_NAME(init)
45 #define GRN_PLUGIN_REGISTER GRN_PLUGIN_IMPL_NAME(register)
46 #define GRN_PLUGIN_FIN      GRN_PLUGIN_IMPL_NAME(fin)
47 
48 #if defined(_WIN32) || defined(_WIN64)
49 #  define GRN_PLUGIN_EXPORT __declspec(dllexport)
50 #else /* defined(_WIN32) || defined(_WIN64) */
51 #  define GRN_PLUGIN_EXPORT
52 #endif /* defined(_WIN32) || defined(_WIN64) */
53 
54 GRN_PLUGIN_EXPORT grn_rc GRN_PLUGIN_INIT(grn_ctx *ctx);
55 GRN_PLUGIN_EXPORT grn_rc GRN_PLUGIN_REGISTER(grn_ctx *ctx);
56 GRN_PLUGIN_EXPORT grn_rc GRN_PLUGIN_FIN(grn_ctx *ctx);
57 
58 #define GRN_PLUGIN_DECLARE_FUNCTIONS(tag)                               \
59   extern grn_rc GRN_PLUGIN_IMPL_NAME_TAGGED(init, tag)(grn_ctx *ctx);   \
60   extern grn_rc GRN_PLUGIN_IMPL_NAME_TAGGED(register, tag)(grn_ctx *ctx); \
61   extern grn_rc GRN_PLUGIN_IMPL_NAME_TAGGED(fin, tag)(grn_ctx *ctx)
62 
63 /*
64   Don't call these functions directly. Use GRN_PLUGIN_MALLOC(),
65   GRN_PLUGIN_CALLOC(), GRN_PLUGIN_REALLOC() and GRN_PLUGIN_FREE() instead.
66  */
67 GRN_API void *grn_plugin_malloc(grn_ctx *ctx,
68                                 size_t size,
69                                 const char *file,
70                                 int line,
71                                 const char *func) GRN_ATTRIBUTE_ALLOC_SIZE(2);
72 GRN_API void *grn_plugin_calloc(grn_ctx *ctx,
73                                 size_t size,
74                                 const char *file,
75                                 int line,
76                                 const char *func) GRN_ATTRIBUTE_ALLOC_SIZE(2);
77 GRN_API void *grn_plugin_realloc(grn_ctx *ctx,
78                                  void *ptr,
79                                  size_t size,
80                                  const char *file,
81                                  int line,
82                                  const char *func) GRN_ATTRIBUTE_ALLOC_SIZE(3);
83 GRN_API void grn_plugin_free(grn_ctx *ctx, void *ptr, const char *file,
84                              int line, const char *func);
85 
86 #define GRN_PLUGIN_MALLOC(ctx, size) \
87   grn_plugin_malloc((ctx), (size), __FILE__, __LINE__, __FUNCTION__)
88 #define GRN_PLUGIN_MALLOCN(ctx, type, n) \
89   ((type *)(grn_plugin_malloc((ctx), sizeof(type) * (n), \
90                               __FILE__, __LINE__, __FUNCTION__)))
91 #define GRN_PLUGIN_CALLOC(ctx, size) \
92   grn_plugin_calloc((ctx), (size), __FILE__, __LINE__, __FUNCTION__)
93 #define GRN_PLUGIN_REALLOC(ctx, ptr, size) \
94   grn_plugin_realloc((ctx), (ptr), (size), __FILE__, __LINE__, __FUNCTION__)
95 #define GRN_PLUGIN_FREE(ctx, ptr) \
96   grn_plugin_free((ctx), (ptr), __FILE__, __LINE__, __FUNCTION__)
97 
98 #define GRN_PLUGIN_LOG(ctx, level, ...) \
99   GRN_LOG((ctx), (level), __VA_ARGS__)
100 
101 /*
102   Don't call grn_plugin_set_error() directly. This function is used in
103   GRN_PLUGIN_SET_ERROR().
104  */
105 GRN_API void grn_plugin_set_error(grn_ctx *ctx, grn_log_level level,
106                                   grn_rc error_code,
107                                   const char *file, int line, const char *func,
108                                   const char *format, ...) GRN_ATTRIBUTE_PRINTF(7);
109 GRN_API void grn_plugin_clear_error(grn_ctx *ctx);
110 
111 
112 /*
113   Don't call these functions directly. grn_plugin_backtrace() and
114   grn_plugin_logtrace() are used in GRN_PLUGIN_SET_ERROR().
115  */
116 GRN_API void grn_plugin_backtrace(grn_ctx *ctx);
117 GRN_API void grn_plugin_logtrace(grn_ctx *ctx, grn_log_level level);
118 
119 /*
120   Don't use GRN_PLUGIN_SET_ERROR() directly. This macro is used in
121   GRN_PLUGIN_ERROR().
122  */
123 #define GRN_PLUGIN_SET_ERROR(ctx, level, error_code, ...) do { \
124   grn_plugin_set_error(ctx, level, error_code, \
125                        __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__); \
126 } while (0)
127 
128 #define GRN_PLUGIN_ERROR(ctx, error_code, ...) \
129   GRN_PLUGIN_SET_ERROR(ctx, GRN_LOG_ERROR, error_code, __VA_ARGS__)
130 
131 #define GRN_PLUGIN_CLEAR_ERROR(ctx) do { \
132   grn_plugin_clear_error((ctx)); \
133 } while (0)
134 
135 typedef struct _grn_plugin_mutex grn_plugin_mutex;
136 
137 GRN_API grn_plugin_mutex *grn_plugin_mutex_open(grn_ctx *ctx);
138 
139 /*
140   grn_plugin_mutex_create() is deprecated. Use grn_plugin_mutex_open()
141   instead.
142 */
143 GRN_API grn_plugin_mutex *grn_plugin_mutex_create(grn_ctx *ctx);
144 
145 GRN_API void grn_plugin_mutex_close(grn_ctx *ctx, grn_plugin_mutex *mutex);
146 
147 /*
148   grn_plugin_mutex_destroy() is deprecated. Use grn_plugin_mutex_close()
149   instead.
150 */
151 GRN_API void grn_plugin_mutex_destroy(grn_ctx *ctx, grn_plugin_mutex *mutex);
152 
153 GRN_API void grn_plugin_mutex_lock(grn_ctx *ctx, grn_plugin_mutex *mutex);
154 
155 GRN_API void grn_plugin_mutex_unlock(grn_ctx *ctx, grn_plugin_mutex *mutex);
156 
157 GRN_API grn_obj *grn_plugin_proc_alloc(grn_ctx *ctx, grn_user_data *user_data,
158                                        grn_id domain, unsigned char flags);
159 
160 GRN_API grn_obj *grn_plugin_proc_get_vars(grn_ctx *ctx, grn_user_data *user_data);
161 
162 GRN_API grn_obj *grn_plugin_proc_get_var(grn_ctx *ctx, grn_user_data *user_data,
163                                          const char *name, int name_size);
164 GRN_API grn_bool grn_plugin_proc_get_var_bool(grn_ctx *ctx,
165                                               grn_user_data *user_data,
166                                               const char *name,
167                                               int name_size,
168                                               grn_bool default_value);
169 GRN_API int32_t grn_plugin_proc_get_var_int32(grn_ctx *ctx,
170                                               grn_user_data *user_data,
171                                               const char *name,
172                                               int name_size,
173                                               int32_t default_value);
174 GRN_API const char *grn_plugin_proc_get_var_string(grn_ctx *ctx,
175                                                    grn_user_data *user_data,
176                                                    const char *name,
177                                                    int name_size,
178                                                    size_t *size);
179 GRN_API grn_content_type grn_plugin_proc_get_var_content_type(grn_ctx *ctx,
180                                                               grn_user_data *user_data,
181                                                               const char *name,
182                                                               int name_size,
183                                                               grn_content_type default_value);
184 
185 GRN_API grn_obj *grn_plugin_proc_get_var_by_offset(grn_ctx *ctx,
186                                                    grn_user_data *user_data,
187                                                    unsigned int offset);
188 
189 GRN_API grn_obj *grn_plugin_proc_get_caller(grn_ctx *ctx,
190                                             grn_user_data *user_data);
191 
192 /* Deprecated since 5.0.9. Use grn_plugin_windows_base_dir() instead. */
193 GRN_API const char *grn_plugin_win32_base_dir(void);
194 GRN_API const char *grn_plugin_windows_base_dir(void);
195 
196 GRN_API int grn_plugin_charlen(grn_ctx *ctx, const char *str_ptr,
197                                unsigned int str_length, grn_encoding encoding);
198 
199 GRN_API int grn_plugin_isspace(grn_ctx *ctx, const char *str_ptr,
200                                unsigned int str_length, grn_encoding encoding);
201 
202 GRN_API grn_rc grn_plugin_expr_var_init(grn_ctx *ctx,
203                                         grn_expr_var *var,
204                                         const char *name,
205                                         int name_size);
206 
207 GRN_API grn_obj *grn_plugin_command_create(grn_ctx *ctx,
208                                            const char *name,
209                                            int name_size,
210                                            grn_proc_func func,
211                                            unsigned int n_vars,
212                                            grn_expr_var *vars);
213 
214 
215 #ifdef __cplusplus
216 }
217 #endif
218