1 /* -*- c-basic-offset: 2 -*- */
2 /*
3 Copyright(C) 2014 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 #include <grn_mrb.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 static int
run(grn_ctx * ctx,const char * db_path,const char * ruby_script_path)25 run(grn_ctx *ctx, const char *db_path, const char *ruby_script_path)
26 {
27 grn_obj *db;
28
29 db = grn_db_open(ctx, db_path);
30 if (!db) {
31 if (ctx->rc == GRN_NO_SUCH_FILE_OR_DIRECTORY) {
32 db = grn_db_create(ctx, db_path, NULL);
33 if (!db) {
34 fprintf(stderr, "Failed to create database: <%s>: %s",
35 db_path, ctx->errbuf);
36 return EXIT_FAILURE;
37 }
38 } else {
39 fprintf(stderr, "Failed to open database: <%s>: %s",
40 db_path, ctx->errbuf);
41 return EXIT_FAILURE;
42 }
43 }
44
45 grn_mrb_load(ctx, ruby_script_path);
46 if (ctx->rc != GRN_SUCCESS) {
47 fprintf(stderr, "Failed to load Ruby script: <%s>: %s",
48 ruby_script_path, ctx->errbuf);
49 }
50
51 grn_obj_close(ctx, db);
52
53 if (ctx->rc == GRN_SUCCESS) {
54 return EXIT_SUCCESS;
55 } else {
56 return EXIT_FAILURE;
57 }
58 }
59
60 int
main(int argc,char ** argv)61 main(int argc, char **argv)
62 {
63 int exit_code = EXIT_SUCCESS;
64
65 if (argc != 3) {
66 fprintf(stderr, "Usage: %s DB_PATH RUBY_SCRIPT_PATH\n", argv[0]);
67 return EXIT_FAILURE;
68 }
69
70 grn_default_logger_set_path(GRN_LOG_PATH);
71
72 if (grn_init() != GRN_SUCCESS) {
73 return EXIT_FAILURE;
74 }
75
76 {
77 grn_ctx ctx;
78 grn_ctx_init(&ctx, 0);
79 exit_code = run(&ctx, argv[1], argv[2]);
80 grn_ctx_fin(&ctx);
81 }
82
83 grn_fin();
84
85 return exit_code;
86 }
87