Debian Codegraph @

src/grndb.c

raw symbols SCIP
1 /*
2 Copyright(C) 2014-2018 Brazil
3 Copyright(C) 2019 Kouhei Sutou <[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 #ifdef WIN32
21 # define GROONGA_MAIN
22 #endif /* WIN32 */
23
24 #include <stdio.h>
25
26 #include <grn_mrb.h>
27 #include <grn_ctx_impl.h>
28 #include <grn_ctx_impl_mrb.h>
29 #include <grn_encoding.h>
30
31 #include <mruby/variable.h>
32 #include <mruby/array.h>
33
34 static uint32_t
35 get_thread_limit(void *data)
36 {
37 return 1;
38 }
39
40 static int
41 run_command(grn_ctx *ctx, int argc, char **argv)
42 {
43 int exit_code = EXIT_SUCCESS;
44 grn_mrb_data *data = &(ctx->impl->mrb);
45 mrb_state *mrb = data->state;
46 mrb_value mrb_command_line_module;
47 mrb_value mrb_grndb_class;
48
49 mrb_command_line_module = mrb_const_get(mrb,
50 mrb_obj_value(data->module),
51 mrb_intern_cstr(mrb, "CommandLine"));
52 if (mrb->exc) {
53 goto exit;
54 }
55
56 mrb_grndb_class = mrb_const_get(mrb,
57 mrb_command_line_module,
58 mrb_intern_cstr(mrb, "Grndb"));
59 if (mrb->exc) {
60 goto exit;
61 }
62
63 {
64 int i;
65 mrb_value mrb_argv;
66 mrb_value mrb_grndb;
67 mrb_value mrb_result;
68
69 mrb_argv = mrb_ary_new_capa(mrb, argc);
70 for (i = 0; i < argc; i++) {
71 const char *utf8_arg;
72 mrb_value mrb_utf8_arg;
73
74 utf8_arg =
75 grn_encoding_convert_to_utf8_from_locale(ctx, argv[i], -1, NULL);
76 mrb_utf8_arg = mrb_str_new_cstr(mrb, utf8_arg);
77 grn_encoding_converted_free(ctx, utf8_arg);
78 mrb_ary_push(mrb, mrb_argv, mrb_utf8_arg);
79 }
80 mrb_grndb = mrb_funcall(mrb, mrb_grndb_class, "new", 1, mrb_argv);
81 if (mrb->exc) {
82 goto exit;
83 }
84
85 mrb_result = mrb_funcall(mrb, mrb_grndb, "run", 0);
86 if (mrb->exc) {
87 goto exit;
88 }
89
90 if (!mrb_bool(mrb_result)) {
91 exit_code = EXIT_FAILURE;
92 }
93 }
94
95 exit :
96 if (mrb->exc) {
97 mrb_print_error(mrb);
98 exit_code = EXIT_FAILURE;
99 }
100
101 return exit_code;
102 }
103
104 static int
105 run(grn_ctx *ctx, int argc, char **argv)
106 {
107 int exit_code = EXIT_SUCCESS;
108 const char *grndb_rb = "command_line/grndb.rb";
109 grn_mrb_data *data = &(ctx->impl->mrb);
110 mrb_state *mrb = data->state;
111
112 mrb_gv_set(mrb, mrb_intern_lit(mrb, "$0"), mrb_str_new_cstr(mrb, argv[0]));
113
114 grn_mrb_load(ctx, grndb_rb);
115 if (ctx->rc != GRN_SUCCESS) {
116 fprintf(stderr, "Failed to load Ruby script: <%s>: %s",
117 grndb_rb, ctx->errbuf);
118 goto exit;
119 }
120
121 {
122 int arena_index;
123
124 arena_index = mrb_gc_arena_save(mrb);
125 exit_code = run_command(ctx, argc, argv);
126 mrb_gc_arena_restore(mrb, arena_index);
127 }
128
129 exit :
130 if (ctx->rc != GRN_SUCCESS) {
131 exit_code = EXIT_FAILURE;
132 }
133 return exit_code;
134 }
135
136 int
137 main(int argc, char **argv)
138 {
139 int exit_code = EXIT_SUCCESS;
140 const char *log_path = GRN_LOG_PATH;
141 const char *log_level_name = NULL;
142 const char *log_flags_name = NULL;
143
144 grn_thread_set_get_limit_func(get_thread_limit, NULL);
145
146 {
147 int i;
148 for (i = 1; i < argc; i++) {
149 const char *arg = argv[i];
150
151 if (arg[0] != '-') {
152 continue;
153 }
154
155 if (arg[1] == '-' && arg[2] == '\0') {
156 break;
157 }
158
159 #define log_path_prefix "--log-path"
160 #define log_level_prefix "--log-level"
161 #define log_flags_prefix "--log-flags"
162 if (strcmp(arg, log_path_prefix) == 0) {
163 if (i + 1 < argc) {
164 log_path = argv[i + 1];
165 i++;
166 }
167 } else if (strncmp(arg,
168 log_path_prefix "=",
169 strlen(log_path_prefix "=")) == 0) {
170 log_path = arg + strlen(log_path_prefix "=");
171 } else if (strcmp(arg, log_level_prefix) == 0) {
172 if (i + 1 < argc) {
173 log_level_name = argv[i + 1];
174 i++;
175 }
176 } else if (strncmp(arg,
177 log_level_prefix "=",
178 strlen(log_level_prefix "=")) == 0) {
179 log_level_name = arg + strlen(log_level_prefix "=");
180 } else if (strcmp(arg, log_flags_prefix) == 0) {
181 if (i + 1 < argc) {
182 log_flags_name = argv[i + 1];
183 i++;
184 }
185 } else if (strncmp(arg,
186 log_flags_prefix "=",
187 strlen(log_flags_prefix "=")) == 0) {
188 log_flags_name = arg + strlen(log_flags_prefix "=");
189 }
190 #undef log_path_prefix
191 #undef log_level_prefix
192 #undef log_flags_prefix
193 }
194 }
195
196 grn_default_logger_set_path(log_path);
197 if (log_level_name) {
198 grn_log_level log_level = GRN_LOG_DEFAULT_LEVEL;
199 if (!grn_log_level_parse(log_level_name, &log_level)) {
200 fprintf(stderr, "%s: failed to parse log level: <%s>\n",
201 argv[0], log_level_name);
202 return EXIT_FAILURE;
203 }
204 grn_default_logger_set_max_level(log_level);
205 }
206
207 if (log_flags_name) {
208 int log_flags;
209 if (!grn_log_flags_parse(log_flags_name, -1, &log_flags)) {
210 fprintf(stderr, "%s: failed to parse log flags: <%s>\n",
211 argv[0], log_flags_name);
212 return EXIT_FAILURE;
213 }
214 grn_default_logger_set_flags(log_flags);
215 }
216
217 if (grn_init() != GRN_SUCCESS) {
218 return EXIT_FAILURE;
219 }
220
221 {
222 grn_ctx ctx;
223 grn_ctx_init(&ctx, 0);
224 grn_ctx_impl_mrb_ensure_init(&ctx);
225 if (ctx.rc == GRN_SUCCESS) {
226 exit_code = run(&ctx, argc, argv);
227 } else {
228 fprintf(stderr, "%d: %s\n", ctx.rc, ctx.errbuf);
229 exit_code = EXIT_FAILURE;
230 }
231 grn_ctx_fin(&ctx);
232 }
233
234 grn_fin();
235
236 return exit_code;
237 }