| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <grn_mrb.h> |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
|
| 24 |
static int |
| 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 |
| 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 |
} |