Debian Codegraph @

lib/command.c

raw symbols SCIP
1 /*
2 Copyright (C) 2015 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 <string.h>
21
22 #include "grn.h"
23 #include "grn_db.h"
24 #include "grn_ctx_impl.h"
25
26 struct _grn_command_input {
27 grn_obj *command;
28 grn_hash *arguments;
29 };
30
31 grn_command_input *
32 grn_command_input_open(grn_ctx *ctx, grn_obj *command)
33 {
34 grn_command_input *input = NULL;
35
36 GRN_API_ENTER;
37 input = GRN_CALLOC(sizeof(grn_command_input));
38 if (!input) {
39 ERR(GRN_NO_MEMORY_AVAILABLE,
40 "[command-input] failed to allocate grn_command_input");
41 goto exit;
42 }
43
44 input->command = command;
45 /* TODO: Allocate by self. */
46 {
47 uint32_t n;
48 input->arguments = grn_expr_get_vars(ctx, input->command, &n);
49 }
50
51 exit :
52 GRN_API_RETURN(input);
53 }
54
55 grn_rc
56 grn_command_input_close(grn_ctx *ctx, grn_command_input *input)
57 {
58 GRN_API_ENTER;
59
60 /* TODO: Free input->arguments by self. */
61 /* grn_expr_clear_vars(ctx, input->command); */
62 GRN_FREE(input);
63
64 GRN_API_RETURN(ctx->rc);
65 }
66
67 grn_obj *
68 grn_command_input_add(grn_ctx *ctx,
69 grn_command_input *input,
70 const char *name,
71 int name_size,
72 bool *added)
73 {
74 grn_obj *argument = NULL;
75 /* TODO: Use bool */
76 int internal_added = false;
77
78 GRN_API_ENTER;
79
80 uint32_t real_name_size;
81 if (name_size == -1) {
82 real_name_size = (uint32_t)strlen(name);
83 } else {
84 real_name_size = (uint32_t)name_size;
85 }
86 if (input->arguments) {
87 grn_hash_add(ctx, input->arguments, name, real_name_size, (void **)&argument,
88 &internal_added);
89 if (internal_added) {
90 GRN_TEXT_INIT(argument, 0);
91 }
92 }
93 if (added) {
94 *added = internal_added;
95 }
96
97 GRN_API_RETURN(argument);
98 }
99
100 grn_obj *
101 grn_command_input_get(grn_ctx *ctx,
102 grn_command_input *input,
103 const char *name,
104 int name_size)
105 {
106 grn_obj *argument = NULL;
107
108 GRN_API_ENTER;
109
110 uint32_t real_name_size;
111 if (name_size == -1) {
112 real_name_size = (uint32_t)strlen(name);
113 } else {
114 real_name_size = (uint32_t)name_size;
115 }
116 if (input->arguments) {
117 grn_hash_get(ctx,
118 input->arguments,
119 name,
120 real_name_size,
121 (void **)&argument);
122 }
123
124 GRN_API_RETURN(argument);
125 }
126
127 grn_obj *
128 grn_command_input_at(grn_ctx *ctx,
129 grn_command_input *input,
130 unsigned int offset)
131 {
132 grn_obj *argument = NULL;
133
134 GRN_API_ENTER;
135 if (input->arguments) {
136 argument = (grn_obj *)grn_hash_get_value_(ctx, input->arguments,
137 offset + 1, NULL);
138 }
139 GRN_API_RETURN(argument);
140 }
141
142 grn_obj *
143 grn_command_input_get_arguments(grn_ctx *ctx,
144 grn_command_input *input)
145 {
146 GRN_API_ENTER;
147 GRN_API_RETURN((grn_obj *)(input->arguments));
148 }
149
150 grn_rc
151 grn_command_register(grn_ctx *ctx,
152 const char *command_name,
153 int command_name_size,
154 grn_command_run_func *run,
155 grn_expr_var *vars,
156 unsigned int n_vars,
157 void *user_data)
158 {
159 GRN_API_ENTER;
160
161 if (command_name_size == -1) {
162 command_name_size = (int)strlen(command_name);
163 }
164
165 {
166 grn_obj *command_object;
167 command_object = grn_proc_create(ctx,
168 command_name,
169 command_name_size,
170 GRN_PROC_COMMAND,
171 NULL, NULL, NULL, n_vars, vars);
172 if (!command_object) {
173 GRN_PLUGIN_ERROR(ctx, GRN_COMMAND_ERROR,
174 "[command][%.*s] failed to grn_proc_create()",
175 command_name_size, command_name);
176 GRN_API_RETURN(ctx->rc);
177 }
178
179 {
180 grn_proc *command = (grn_proc *)command_object;
181 command->callbacks.command.run = run;
182 command->user_data = user_data;
183 }
184 }
185
186 GRN_API_RETURN(GRN_SUCCESS);
187 }
188
189 grn_rc
190 grn_command_run(grn_ctx *ctx,
191 grn_obj *command,
192 grn_command_input *input)
193 {
194 grn_proc *proc;
195
196 GRN_API_ENTER;
197
198 proc = (grn_proc *)command;
199 if (proc->callbacks.command.run) {
200 proc->callbacks.command.run(ctx, command, input, proc->user_data);
201 } else {
202 /* TODO: REMOVE ME. For backward compatibility. */
203 uint32_t stack_curr = ctx->impl->stack_curr;
204 grn_proc_call(ctx, command, 0, command);
205 if (ctx->impl->stack_curr > stack_curr) {
206 grn_ctx_pop(ctx);
207 }
208 }
209
210 GRN_API_RETURN(ctx->rc);
211 }