| 1 | /* |
| 2 | Copyright (C) 2016 Brazil |
| 3 | Copyright (C) 2022 Sutou Kouhei <[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 | #include "../grn_proc.h" |
| 21 | |
| 22 | #include <groonga/plugin.h> |
| 23 | |
| 24 | static grn_obj * |
| 25 | command_config_get(grn_ctx *ctx, |
| 26 | int nargs, |
| 27 | grn_obj **args, |
| 28 | grn_user_data *user_data) |
| 29 | { |
| 30 | grn_obj *key; |
| 31 | const char *value; |
| 32 | uint32_t value_size; |
| 33 | |
| 34 | key = grn_plugin_proc_get_var(ctx, user_data, "key", -1); |
| 35 | if (GRN_TEXT_LEN(key) == 0) { |
| 36 | GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, |
| 37 | "[config][get] key is missing"); |
| 38 | return NULL; |
| 39 | } |
| 40 | |
| 41 | grn_config_get(ctx, |
| 42 | GRN_TEXT_VALUE(key), (int32_t)GRN_TEXT_LEN(key), |
| 43 | &value, &value_size); |
| 44 | if (ctx->rc) { |
| 45 | return NULL; |
| 46 | } |
| 47 | |
| 48 | grn_ctx_output_str(ctx, value, value_size); |
| 49 | |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | static grn_obj * |
| 54 | command_config_set(grn_ctx *ctx, |
| 55 | int nargs, |
| 56 | grn_obj **args, |
| 57 | grn_user_data *user_data) |
| 58 | { |
| 59 | grn_obj *key; |
| 60 | grn_obj *value; |
| 61 | |
| 62 | key = grn_plugin_proc_get_var(ctx, user_data, "key", -1); |
| 63 | if (GRN_TEXT_LEN(key) == 0) { |
| 64 | GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, |
| 65 | "[config][set] key is missing"); |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | value = grn_plugin_proc_get_var(ctx, user_data, "value", -1); |
| 70 | grn_config_set(ctx, |
| 71 | GRN_TEXT_VALUE(key), (int32_t)GRN_TEXT_LEN(key), |
| 72 | GRN_TEXT_VALUE(value), (int32_t)GRN_TEXT_LEN(value)); |
| 73 | |
| 74 | grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS); |
| 75 | |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | static grn_obj * |
| 80 | command_config_delete(grn_ctx *ctx, |
| 81 | int nargs, |
| 82 | grn_obj **args, |
| 83 | grn_user_data *user_data) |
| 84 | { |
| 85 | grn_obj *key; |
| 86 | |
| 87 | key = grn_plugin_proc_get_var(ctx, user_data, "key", -1); |
| 88 | if (GRN_TEXT_LEN(key) == 0) { |
| 89 | GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, |
| 90 | "[config][delete] key is missing"); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | grn_config_delete(ctx, GRN_TEXT_VALUE(key), (int32_t)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 | } |