| 1 | /* |
| 2 | Copyright (C) 2025 Sutou Kouhei <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 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_db.h" |
| 20 | #include "../grn_proc.h" |
| 21 | |
| 22 | #include <groonga/plugin.h> |
| 23 | |
| 24 | static grn_obj * |
| 25 | command_command_list(grn_ctx *ctx, |
| 26 | int nargs, |
| 27 | grn_obj **args, |
| 28 | grn_user_data *user_data) |
| 29 | { |
| 30 | const bool is_close_opened_object_mode = (grn_thread_get_limit() == 1); |
| 31 | grn_obj command_ids; |
| 32 | |
| 33 | GRN_RECORD_INIT(&command_ids, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 34 | GRN_DB_EACH_BEGIN_BY_KEY(ctx, cursor, id) |
| 35 | { |
| 36 | if (id == GRN_DB_MECAB) { |
| 37 | /* TokenMecab must not be a command. TokenMecab may exist as a |
| 38 | * dummy object for historical reason. */ |
| 39 | continue; |
| 40 | } |
| 41 | |
| 42 | void *name; |
| 43 | int name_size = grn_table_cursor_get_key(ctx, cursor, &name); |
| 44 | if (grn_obj_name_is_column(ctx, name, name_size)) { |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | if (is_close_opened_object_mode) { |
| 49 | grn_ctx_push_temporary_open_space(ctx); |
| 50 | } |
| 51 | |
| 52 | grn_obj *object = grn_ctx_at(ctx, id); |
| 53 | if (object) { |
| 54 | if (grn_obj_is_command_proc(ctx, object)) { |
| 55 | GRN_RECORD_PUT(ctx, &command_ids, id); |
| 56 | } |
| 57 | } else { |
| 58 | GRN_PLUGIN_CLEAR_ERROR(ctx); |
| 59 | } |
| 60 | |
| 61 | if (is_close_opened_object_mode) { |
| 62 | grn_ctx_pop_temporary_open_space(ctx); |
| 63 | } |
| 64 | } |
| 65 | GRN_DB_EACH_END(ctx, cursor); |
| 66 | |
| 67 | size_t n = GRN_RECORD_VECTOR_SIZE(&command_ids); |
| 68 | grn_ctx_output_map_open(ctx, "commands", (int)n); |
| 69 | for (size_t i = 0; i < n; i++) { |
| 70 | if (is_close_opened_object_mode) { |
| 71 | grn_ctx_push_temporary_open_space(ctx); |
| 72 | } |
| 73 | |
| 74 | grn_id command_id = GRN_RECORD_VALUE_AT(&command_ids, i); |
| 75 | grn_obj *command = grn_ctx_at(ctx, command_id); |
| 76 | |
| 77 | GRN_DEFINE_NAME(command); |
| 78 | |
| 79 | grn_ctx_output_str(ctx, name, name_size); |
| 80 | |
| 81 | grn_ctx_output_map_open(ctx, "command", 3); |
| 82 | |
| 83 | grn_ctx_output_cstr(ctx, "id"); |
| 84 | grn_ctx_output_uint32(ctx, command_id); |
| 85 | |
| 86 | grn_ctx_output_cstr(ctx, "name"); |
| 87 | grn_ctx_output_str(ctx, name, name_size); |
| 88 | |
| 89 | grn_ctx_output_cstr(ctx, "arguments"); |
| 90 | { |
| 91 | unsigned int n_vars; |
| 92 | grn_hash *vars = grn_expr_get_vars(ctx, command, &n_vars); |
| 93 | grn_ctx_output_array_open(ctx, "arguments", n_vars); |
| 94 | GRN_HASH_EACH_BEGIN(ctx, vars, cursor, id) |
| 95 | { |
| 96 | grn_ctx_output_map_open(ctx, "argument", 1); |
| 97 | { |
| 98 | void *key; |
| 99 | int key_size = grn_hash_cursor_get_key(ctx, cursor, &key); |
| 100 | grn_ctx_output_cstr(ctx, "name"); |
| 101 | grn_ctx_output_str(ctx, key, key_size); |
| 102 | |
| 103 | /* TODO: Output more information. */ |
| 104 | } |
| 105 | grn_ctx_output_map_close(ctx); |
| 106 | } |
| 107 | GRN_HASH_EACH_END(ctx, cursor); |
| 108 | grn_ctx_output_array_close(ctx); |
| 109 | } |
| 110 | |
| 111 | /* TODO: Output more information. */ |
| 112 | |
| 113 | grn_ctx_output_map_close(ctx); |
| 114 | |
| 115 | if (is_close_opened_object_mode) { |
| 116 | grn_ctx_pop_temporary_open_space(ctx); |
| 117 | } |
| 118 | } |
| 119 | grn_ctx_output_map_close(ctx); |
| 120 | |
| 121 | GRN_OBJ_FIN(ctx, &command_ids); |
| 122 | |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | grn_proc_init_command_list(grn_ctx *ctx) |
| 128 | { |
| 129 | grn_plugin_command_create(ctx, |
| 130 | "command_list", |
| 131 | -1, |
| 132 | command_command_list, |
| 133 | 0, |
| 134 | NULL); |
| 135 | } |