| 1 | /* -*- c-basic-offset: 2 -*- */ |
| 2 | /* |
| 3 | Copyright(C) 2016 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-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #include "../grn_proc.h" |
| 20 | |
| 21 | #include <groonga/plugin.h> |
| 22 | |
| 23 | static grn_obj * |
| 24 | command_config_get(grn_ctx *ctx, |
| 25 | int nargs, |
| 26 | grn_obj **args, |
| 27 | grn_user_data *user_data) |
| 28 | { |
| 29 | grn_obj *key; |
| 30 | const char *value; |
| 31 | uint32_t value_size; |
| 32 | |
| 33 | key = grn_plugin_proc_get_var(ctx, user_data, "key" , -1); |
| 34 | if (GRN_TEXT_LEN(key) == 0) { |
| 35 | GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, |
| 36 | "[config][get] key is missing" ); |
| 37 | return NULL; |
| 38 | } |
| 39 | |
| 40 | grn_config_get(ctx, |
| 41 | GRN_TEXT_VALUE(key), GRN_TEXT_LEN(key), |
| 42 | &value, &value_size); |
| 43 | if (ctx->rc) { |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | grn_ctx_output_str(ctx, value, value_size); |
| 48 | |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | static grn_obj * |
| 53 | command_config_set(grn_ctx *ctx, |
| 54 | int nargs, |
| 55 | grn_obj **args, |
| 56 | grn_user_data *user_data) |
| 57 | { |
| 58 | grn_obj *key; |
| 59 | grn_obj *value; |
| 60 | |
| 61 | key = grn_plugin_proc_get_var(ctx, user_data, "key" , -1); |
| 62 | if (GRN_TEXT_LEN(key) == 0) { |
| 63 | GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, |
| 64 | "[config][set] key is missing" ); |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | value = grn_plugin_proc_get_var(ctx, user_data, "value" , -1); |
| 69 | grn_config_set(ctx, |
| 70 | GRN_TEXT_VALUE(key), GRN_TEXT_LEN(key), |
| 71 | GRN_TEXT_VALUE(value), GRN_TEXT_LEN(value)); |
| 72 | |
| 73 | grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS); |
| 74 | |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | static grn_obj * |
| 79 | command_config_delete(grn_ctx *ctx, |
| 80 | int nargs, |
| 81 | grn_obj **args, |
| 82 | grn_user_data *user_data) |
| 83 | { |
| 84 | grn_obj *key; |
| 85 | |
| 86 | key = grn_plugin_proc_get_var(ctx, user_data, "key" , -1); |
| 87 | if (GRN_TEXT_LEN(key) == 0) { |
| 88 | GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, |
| 89 | "[config][delete] key is missing" ); |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | grn_config_delete(ctx, |
| 94 | GRN_TEXT_VALUE(key), GRN_TEXT_LEN(key)); |
| 95 | |
| 96 | grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS); |
| 97 | |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | grn_proc_init_config_get(grn_ctx *ctx) |
| 103 | { |
| 104 | grn_expr_var vars[1]; |
| 105 | |
| 106 | grn_plugin_expr_var_init(ctx, &(vars[0]), "key" , -1); |
| 107 | grn_plugin_command_create(ctx, |
| 108 | "config_get" , -1, |
| 109 | command_config_get, |
| 110 | 1, |
| 111 | vars); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | grn_proc_init_config_set(grn_ctx *ctx) |
| 116 | { |
| 117 | grn_expr_var vars[2]; |
| 118 | |
| 119 | grn_plugin_expr_var_init(ctx, &(vars[0]), "key" , -1); |
| 120 | grn_plugin_expr_var_init(ctx, &(vars[1]), "value" , -1); |
| 121 | grn_plugin_command_create(ctx, |
| 122 | "config_set" , -1, |
| 123 | command_config_set, |
| 124 | 2, |
| 125 | vars); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | grn_proc_init_config_delete(grn_ctx *ctx) |
| 130 | { |
| 131 | grn_expr_var vars[1]; |
| 132 | |
| 133 | grn_plugin_expr_var_init(ctx, &(vars[0]), "key" , -1); |
| 134 | grn_plugin_command_create(ctx, |
| 135 | "config_delete" , -1, |
| 136 | command_config_delete, |
| 137 | 1, |
| 138 | vars); |
| 139 | } |
| 140 | |