File: proc_command_list.c

package info (click to toggle)
groonga 16.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 188,416 kB
  • sloc: ansic: 772,827; cpp: 52,396; ruby: 40,556; javascript: 10,250; yacc: 7,045; sh: 5,627; python: 2,821; makefile: 1,679
file content (135 lines) | stat: -rw-r--r-- 3,881 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
  Copyright (C) 2025  Sutou Kouhei <kou@clear-code.com>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "../grn_db.h"
#include "../grn_proc.h"

#include <groonga/plugin.h>

static grn_obj *
command_command_list(grn_ctx *ctx,
                     int nargs,
                     grn_obj **args,
                     grn_user_data *user_data)
{
  const bool is_close_opened_object_mode = (grn_thread_get_limit() == 1);
  grn_obj command_ids;

  GRN_RECORD_INIT(&command_ids, GRN_OBJ_VECTOR, GRN_ID_NIL);
  GRN_DB_EACH_BEGIN_BY_KEY(ctx, cursor, id)
  {
    if (id == GRN_DB_MECAB) {
      /* TokenMecab must not be a command. TokenMecab may exist as a
       * dummy object for historical reason. */
      continue;
    }

    void *name;
    int name_size = grn_table_cursor_get_key(ctx, cursor, &name);
    if (grn_obj_name_is_column(ctx, name, name_size)) {
      continue;
    }

    if (is_close_opened_object_mode) {
      grn_ctx_push_temporary_open_space(ctx);
    }

    grn_obj *object = grn_ctx_at(ctx, id);
    if (object) {
      if (grn_obj_is_command_proc(ctx, object)) {
        GRN_RECORD_PUT(ctx, &command_ids, id);
      }
    } else {
      GRN_PLUGIN_CLEAR_ERROR(ctx);
    }

    if (is_close_opened_object_mode) {
      grn_ctx_pop_temporary_open_space(ctx);
    }
  }
  GRN_DB_EACH_END(ctx, cursor);

  size_t n = GRN_RECORD_VECTOR_SIZE(&command_ids);
  grn_ctx_output_map_open(ctx, "commands", (int)n);
  for (size_t i = 0; i < n; i++) {
    if (is_close_opened_object_mode) {
      grn_ctx_push_temporary_open_space(ctx);
    }

    grn_id command_id = GRN_RECORD_VALUE_AT(&command_ids, i);
    grn_obj *command = grn_ctx_at(ctx, command_id);

    GRN_DEFINE_NAME(command);

    grn_ctx_output_str(ctx, name, name_size);

    grn_ctx_output_map_open(ctx, "command", 3);

    grn_ctx_output_cstr(ctx, "id");
    grn_ctx_output_uint32(ctx, command_id);

    grn_ctx_output_cstr(ctx, "name");
    grn_ctx_output_str(ctx, name, name_size);

    grn_ctx_output_cstr(ctx, "arguments");
    {
      unsigned int n_vars;
      grn_hash *vars = grn_expr_get_vars(ctx, command, &n_vars);
      grn_ctx_output_array_open(ctx, "arguments", n_vars);
      GRN_HASH_EACH_BEGIN(ctx, vars, cursor, id)
      {
        grn_ctx_output_map_open(ctx, "argument", 1);
        {
          void *key;
          int key_size = grn_hash_cursor_get_key(ctx, cursor, &key);
          grn_ctx_output_cstr(ctx, "name");
          grn_ctx_output_str(ctx, key, key_size);

          /* TODO: Output more information. */
        }
        grn_ctx_output_map_close(ctx);
      }
      GRN_HASH_EACH_END(ctx, cursor);
      grn_ctx_output_array_close(ctx);
    }

    /* TODO: Output more information. */

    grn_ctx_output_map_close(ctx);

    if (is_close_opened_object_mode) {
      grn_ctx_pop_temporary_open_space(ctx);
    }
  }
  grn_ctx_output_map_close(ctx);

  GRN_OBJ_FIN(ctx, &command_ids);

  return NULL;
}

void
grn_proc_init_command_list(grn_ctx *ctx)
{
  grn_plugin_command_create(ctx,
                            "command_list",
                            -1,
                            command_command_list,
                            0,
                            NULL);
}