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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
/* Copyright (C) 2022 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <histedit.h>
#include "knot/common/log.h"
#include "utils/common/lookup.h"
#include "utils/knotc/interactive.h"
#include "utils/knotc/commands.h"
#include "contrib/openbsd/strlcat.h"
#include "contrib/string.h"
#define PROGRAM_NAME "knotc"
#define HISTORY_FILE ".knotc_history"
extern params_t params;
typedef struct {
const char **args;
int count;
bool dname;
} dup_check_ctx_t;
static void cmds_lookup(EditLine *el, const char *str, size_t str_len)
{
lookup_t lookup;
int ret = lookup_init(&lookup);
if (ret != KNOT_EOK) {
return;
}
// Fill the lookup with command names.
for (const cmd_desc_t *desc = cmd_table; desc->name != NULL; desc++) {
ret = lookup_insert(&lookup, desc->name, NULL);
if (ret != KNOT_EOK) {
goto cmds_lookup_finish;
}
}
(void)lookup_complete(&lookup, str, str_len, el, true);
cmds_lookup_finish:
lookup_deinit(&lookup);
}
static void remove_duplicates(lookup_t *lookup, dup_check_ctx_t *check_ctx)
{
if (check_ctx == NULL) {
return;
}
knot_dname_txt_storage_t dname = "";
for (int i = 0; i < check_ctx->count; i++) {
const char *arg = (check_ctx->args)[i];
size_t len = strlen(arg);
if (check_ctx->dname && len > 1 && arg[len - 1] != '.') {
strlcat(dname, arg, sizeof(dname));
strlcat(dname, ".", sizeof(dname));
arg = dname;
}
(void)lookup_remove(lookup, arg);
}
}
static void local_zones_lookup(EditLine *el, const char *str, size_t str_len,
dup_check_ctx_t *check_ctx)
{
lookup_t lookup;
int ret = lookup_init(&lookup);
if (ret != KNOT_EOK) {
return;
}
knot_dname_txt_storage_t buff;
// Fill the lookup with local zone names.
for (conf_iter_t iter = conf_iter(conf(), C_ZONE);
iter.code == KNOT_EOK; conf_iter_next(conf(), &iter)) {
conf_val_t val = conf_iter_id(conf(), &iter);
char *name = knot_dname_to_str(buff, conf_dname(&val), sizeof(buff));
ret = lookup_insert(&lookup, name, NULL);
if (ret != KNOT_EOK) {
conf_iter_finish(conf(), &iter);
goto local_zones_lookup_finish;
}
}
remove_duplicates(&lookup, check_ctx);
(void)lookup_complete(&lookup, str, str_len, el, true);
local_zones_lookup_finish:
lookup_deinit(&lookup);
}
static void list_separators(EditLine *el, const char *separators)
{
lookup_t lookup;
if (lookup_init(&lookup) != KNOT_EOK) {
return;
}
size_t count = strlen(separators);
for (int i = 0; i < count; i++) {
char sep[2] = { separators[i] };
(void)lookup_insert(&lookup, sep, NULL);
}
(void)lookup_complete(&lookup, "", 0, el, false);
lookup_deinit(&lookup);
}
static bool rmt_lookup(EditLine *el, const char *str, size_t str_len,
const char *section, const char *item, const char *id,
dup_check_ctx_t *check_ctx, bool add_space, const char *flags,
knot_ctl_idx_t idx)
{
const cmd_desc_t *desc = cmd_table;
while (desc->name != NULL && desc->cmd != CTL_CONF_LIST) {
desc++;
}
assert(desc->name != NULL);
knot_ctl_data_t query = {
[KNOT_CTL_IDX_CMD] = ctl_cmd_to_str(desc->cmd),
[KNOT_CTL_IDX_SECTION] = section,
[KNOT_CTL_IDX_ITEM] = item,
[KNOT_CTL_IDX_ID] = id,
[KNOT_CTL_IDX_FLAGS] = flags
};
lookup_t lookup;
knot_ctl_t *ctl = NULL;
bool found = false;
if (set_ctl(&ctl, params.socket, DEFAULT_CTL_TIMEOUT_MS, desc) != KNOT_EOK ||
knot_ctl_send(ctl, KNOT_CTL_TYPE_DATA, &query) != KNOT_EOK ||
knot_ctl_send(ctl, KNOT_CTL_TYPE_BLOCK, NULL) != KNOT_EOK ||
lookup_init(&lookup) != KNOT_EOK) {
unset_ctl(ctl);
return found;
}
while (true) {
knot_ctl_type_t type;
knot_ctl_data_t reply;
if (knot_ctl_receive(ctl, &type, &reply) != KNOT_EOK) {
goto rmt_lookup_finish;
}
if (type != KNOT_CTL_TYPE_DATA && type != KNOT_CTL_TYPE_EXTRA) {
break;
}
const char *error = reply[KNOT_CTL_IDX_ERROR];
if (error != NULL) {
printf("\nnotice: (%s)\n", error);
goto rmt_lookup_finish;
}
// Insert the received name into the lookup.
if (lookup_insert(&lookup, reply[idx], NULL) != KNOT_EOK) {
goto rmt_lookup_finish;
}
}
remove_duplicates(&lookup, check_ctx);
if (lookup_complete(&lookup, str, str_len, el, add_space) == KNOT_EOK &&
str != NULL && strcmp(lookup.found.key, str) == 0) {
found = true;
}
rmt_lookup_finish:
lookup_deinit(&lookup);
unset_ctl(ctl);
return found;
}
static bool id_lookup(EditLine *el, const char *str, size_t str_len,
const char *section, const cmd_desc_t *cmd_desc,
dup_check_ctx_t *ctx, bool add_space, bool zones)
{
char flags[4] = "";
if (zones) {
strlcat(flags, CTL_FLAG_LIST_ZONES, sizeof(flags));
} else if (cmd_desc->flags & CMD_FREQ_TXN) {
strlcat(flags, CTL_FLAG_LIST_TXN, sizeof(flags));
}
return rmt_lookup(el, str, str_len, section, NULL, NULL, ctx, add_space,
flags, KNOT_CTL_IDX_ID);
}
static void val_lookup(EditLine *el, const char *str, size_t str_len,
const char *section, const char *item, const char *id,
dup_check_ctx_t *ctx, bool list_schema)
{
char flags[4] = CTL_FLAG_LIST_TXN;
if (list_schema) {
strlcat(flags, CTL_FLAG_LIST_SCHEMA, sizeof(flags));
}
(void)rmt_lookup(el, str, str_len, section, item, id, ctx, true,
flags, KNOT_CTL_IDX_DATA);
}
static bool list_lookup(EditLine *el, const char *str, const char *section)
{
const char *flags = CTL_FLAG_LIST_SCHEMA;
knot_ctl_idx_t idx = (section == NULL) ? KNOT_CTL_IDX_SECTION : KNOT_CTL_IDX_ITEM;
return rmt_lookup(el, str, strlen(str), section, NULL, NULL, NULL,
section != NULL, flags, idx);
}
static void item_lookup(EditLine *el, const char *str, const cmd_desc_t *cmd_desc)
{
// List all sections.
if (str == NULL) {
(void)list_lookup(el, "", NULL);
return;
}
// Check for id specification.
char *id = (strchr(str, '['));
if (id != NULL) {
char *section = strndup(str, id - str);
// Check for completed id specification.
char *id_stop = (strchr(id, ']'));
if (id_stop != NULL) {
// Complete the item name.
if (*(id_stop + 1) == '.') {
(void)list_lookup(el, id_stop + 2, section);
} else {
list_separators(el, ".");
}
} else {
// Complete the section id.
if (id_lookup(el, id + 1, strlen(id + 1), section, cmd_desc,
NULL, false, false)) {
list_separators(el, "]");
}
}
free(section);
} else {
// Check for item specification.
char *dot = (strchr(str, '.'));
if (dot != NULL) {
// Complete the item name.
char *section = strndup(str, dot - str);
(void)list_lookup(el, dot + 1, section);
free(section);
} else {
// Complete the section name.
if (list_lookup(el, str, NULL)) {
list_separators(el, "[.");
}
}
}
}
static unsigned char complete(EditLine *el, int ch)
{
int argc, token, pos;
const char **argv;
const LineInfo *li = el_line(el);
Tokenizer *tok = tok_init(NULL);
// Parse the line.
int ret = tok_line(tok, li, &argc, &argv, &token, &pos);
if (ret != 0) {
goto complete_exit;
}
// Show possible commands.
if (argc == 0) {
print_commands();
goto complete_exit;
}
// Complete the command name.
if (token == 0) {
cmds_lookup(el, argv[0], pos);
goto complete_exit;
}
// Find the command descriptor.
const cmd_desc_t *desc = cmd_table;
while (desc->name != NULL && strcmp(desc->name, argv[0]) != 0) {
desc++;
}
if (desc->name == NULL) {
goto complete_exit;
}
// Finish if a command with no or unsupported arguments.
if (desc->flags == CMD_FNONE || desc->flags == CMD_FREAD ||
desc->flags == CMD_FWRITE) {
goto complete_exit;
}
ret = set_config(desc, ¶ms);
if (ret != KNOT_EOK) {
goto complete_exit;
}
// Complete the zone name.
if (desc->flags & (CMD_FREQ_ZONE | CMD_FOPT_ZONE)) {
if (token > 1 && !(desc->flags & CMD_FOPT_ZONE)) {
goto complete_exit;
}
dup_check_ctx_t ctx = { &argv[1], token - 1, true };
if (desc->flags & CMD_FREAD) {
local_zones_lookup(el, argv[token], pos, &ctx);
} else {
id_lookup(el, argv[token], pos, "zone", desc, &ctx, true, true);
}
goto complete_exit;
// Complete the section/id/item name or item value.
} else if (desc->flags & (CMD_FOPT_ITEM | CMD_FREQ_ITEM)) {
if (token == 1) {
item_lookup(el, argv[1], desc);
} else if (desc->flags & CMD_FOPT_DATA) {
char section[YP_MAX_TXT_KEY_LEN + 1] = "";
char item[YP_MAX_TXT_KEY_LEN + 1] = "";
char id[KNOT_DNAME_TXT_MAXLEN + 1] = "";
assert(YP_MAX_TXT_KEY_LEN == 127);
assert(KNOT_DNAME_TXT_MAXLEN == 1004);
if (sscanf(argv[1], "%127[^[][%1004[^]]].%127s", section, id, item) == 3 ||
sscanf(argv[1], "%127[^.].%127s", section, item) == 2) {
dup_check_ctx_t ctx = { &argv[2], token - 2 };
val_lookup(el, argv[token], pos, section, item, id,
&ctx, desc->flags & CMD_FLIST_SCHEMA);
}
}
goto complete_exit;
}
complete_exit:
conf_update(NULL, CONF_UPD_FNONE);
tok_reset(tok);
tok_end(tok);
return CC_REDISPLAY;
}
static char *prompt(EditLine *el)
{
return PROGRAM_NAME"> ";
}
int interactive_loop(params_t *process_params)
{
char *hist_file = NULL;
const char *home = getenv("HOME");
if (home != NULL) {
hist_file = sprintf_alloc("%s/%s", home, HISTORY_FILE);
}
if (hist_file == NULL) {
log_notice("failed to get home directory");
}
EditLine *el = el_init(PROGRAM_NAME, stdin, stdout, stderr);
if (el == NULL) {
log_error("interactive mode not available");
free(hist_file);
return KNOT_ERROR;
}
History *hist = history_init();
if (hist == NULL) {
log_error("interactive mode not available");
el_end(el);
free(hist_file);
return KNOT_ERROR;
}
HistEvent hev = { 0 };
history(hist, &hev, H_SETSIZE, 1000);
history(hist, &hev, H_SETUNIQUE, 1);
el_set(el, EL_HIST, history, hist);
history(hist, &hev, H_LOAD, hist_file);
el_set(el, EL_TERMINAL, NULL);
el_set(el, EL_EDITOR, "emacs");
el_set(el, EL_PROMPT, prompt);
el_set(el, EL_SIGNAL, 1);
el_source(el, NULL);
// Warning: these two el_sets()'s always leak -- in libedit2 library!
// For more details see this commit's message.
el_set(el, EL_ADDFN, PROGRAM_NAME"-complete",
"Perform "PROGRAM_NAME" completion.", complete);
el_set(el, EL_BIND, "^I", PROGRAM_NAME"-complete", NULL);
int count;
const char *line;
while ((line = el_gets(el, &count)) != NULL && count > 0) {
Tokenizer *tok = tok_init(NULL);
// Tokenize the current line.
int argc;
const char **argv;
const LineInfo *li = el_line(el);
int ret = tok_line(tok, li, &argc, &argv, NULL, NULL);
if (ret == 0 && argc != 0) {
history(hist, &hev, H_ENTER, line);
history(hist, &hev, H_SAVE, hist_file);
// Process the command.
ret = process_cmd(argc, argv, process_params);
}
tok_reset(tok);
tok_end(tok);
// Check for the exit command.
if (ret == KNOT_CTL_ESTOP) {
break;
}
}
history_end(hist);
free(hist_file);
el_end(el);
return KNOT_EOK;
}
|