blob: f0dd041ee5366459bb1f10646539f74e8c656fef [file] [log] [blame]
Radek Krejcied5acc52019-04-25 15:57:04 +02001/**
2 * @file completion.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libyang's yanglint tool auto completion
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejcif8dc59a2020-11-25 13:47:44 +010016#define _POSIX_C_SOURCE 200809L /* strdup */
Radek Krejcied5acc52019-04-25 15:57:04 +020017
Radek Krejcied5acc52019-04-25 15:57:04 +020018#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
Radek Krejcied5acc52019-04-25 15:57:04 +020022#include <string.h>
23
Radek Krejcied5acc52019-04-25 15:57:04 +020024#include "libyang.h"
25
Radek Krejcie9f13b12020-11-09 17:42:04 +010026#include "cmd.h"
27#include "common.h"
romanc1607ce2022-09-12 10:12:39 +020028#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "linenoise/linenoise.h"
30
Radek Krejcie9f13b12020-11-09 17:42:04 +010031/* from the main.c */
Radek Krejcied5acc52019-04-25 15:57:04 +020032extern struct ly_ctx *ctx;
33
romanc1607ce2022-09-12 10:12:39 +020034/**
35 * @brief Add a match to the completions array.
36 *
37 * @param[in] match Match to be added.
38 * @param[in,out] matches Matches provided to the user as a completion hint.
39 * @param[in,out] match_count Number of matches.
40 */
41static void
42cmd_completion_add_match(const char *match, char ***matches, unsigned int *match_count)
43{
44 void *p;
45
46 ++(*match_count);
47 p = realloc(*matches, *match_count * sizeof **matches);
48 if (!p) {
49 YLMSG_E("Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
50 return;
51 }
52 *matches = p;
53 (*matches)[*match_count - 1] = strdup(match);
54}
55
56/**
57 * @brief Provides completion for command names.
58 *
59 * @param[in] hint User input.
60 * @param[out] matches Matches provided to the user as a completion hint.
61 * @param[out] match_count Number of matches.
62 */
Radek Krejcied5acc52019-04-25 15:57:04 +020063static void
64get_cmd_completion(const char *hint, char ***matches, unsigned int *match_count)
65{
66 int i;
Radek Krejcied5acc52019-04-25 15:57:04 +020067
68 *match_count = 0;
69 *matches = NULL;
70
71 for (i = 0; commands[i].name; i++) {
72 if (!strncmp(hint, commands[i].name, strlen(hint))) {
romanc1607ce2022-09-12 10:12:39 +020073 cmd_completion_add_match(commands[i].name, matches, match_count);
Radek Krejcied5acc52019-04-25 15:57:04 +020074 }
75 }
76}
77
romanc1607ce2022-09-12 10:12:39 +020078/**
aPiecek15cc4cf2023-04-17 15:23:21 +020079 * @brief Provides completion for arguments.
80 *
81 * @param[in] hint User input.
82 * @param[in] args Array of all possible arguments. The last element must be NULL.
83 * @param[out] matches Matches provided to the user as a completion hint.
84 * @param[out] match_count Number of matches.
85 */
86static void
87get_arg_completion(const char *hint, const char **args, char ***matches, unsigned int *match_count)
88{
89 int i;
90
91 *match_count = 0;
92 *matches = NULL;
93
94 for (i = 0; args[i]; i++) {
95 if (!strncmp(hint, args[i], strlen(hint))) {
96 cmd_completion_add_match(args[i], matches, match_count);
97 }
98 }
99 if (*match_count == 0) {
100 for (i = 0; args[i]; i++) {
101 cmd_completion_add_match(args[i], matches, match_count);
102 }
103 }
104}
105
106/**
romanc1607ce2022-09-12 10:12:39 +0200107 * @brief Provides completion for module names.
108 *
109 * @param[in] hint User input.
110 * @param[out] matches Matches provided to the user as a completion hint.
111 * @param[out] match_count Number of matches.
112 */
Radek Krejcied5acc52019-04-25 15:57:04 +0200113static void
114get_model_completion(const char *hint, char ***matches, unsigned int *match_count)
115{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200116 LY_ARRAY_COUNT_TYPE u;
Radek Krejcied5acc52019-04-25 15:57:04 +0200117 uint32_t idx = 0;
118 const struct lys_module *module;
Radek Krejcied5acc52019-04-25 15:57:04 +0200119
120 *match_count = 0;
121 *matches = NULL;
122
123 while ((module = ly_ctx_get_module_iter(ctx, &idx))) {
124 if (!strncmp(hint, module->name, strlen(hint))) {
romanc1607ce2022-09-12 10:12:39 +0200125 cmd_completion_add_match(module->name, matches, match_count);
Radek Krejcied5acc52019-04-25 15:57:04 +0200126 }
127
128 LY_ARRAY_FOR(module->parsed->includes, u) {
129 if (!strncmp(hint, module->parsed->includes[u].submodule->name, strlen(hint))) {
romanc1607ce2022-09-12 10:12:39 +0200130 cmd_completion_add_match(module->parsed->includes[u].submodule->name, matches, match_count);
Radek Krejcied5acc52019-04-25 15:57:04 +0200131 }
132 }
133 }
134}
135
romanc1607ce2022-09-12 10:12:39 +0200136/**
137 * @brief Add all child nodes of a single node to the completion hint.
138 *
139 * @param[in] last_node Node of which children will be added to the hint.
aPiecek27337da2023-06-19 14:49:29 +0200140 * @param[out] matches Matches provided to the user as a completion hint.
141 * @param[out] match_count Number of matches.
romanc1607ce2022-09-12 10:12:39 +0200142 */
143static void
144single_hint_add_children(const struct lysc_node *last_node, char ***matches, unsigned int *match_count)
145{
146 const struct lysc_node *node = NULL;
147 char *match;
148
149 if (!last_node) {
150 return;
151 }
152
romanf00089c2022-10-06 16:01:31 +0200153 while ((node = lys_getnext(node, last_node, NULL, LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHCHOICE))) {
154 match = lysc_path(node, LYSC_PATH_LOG, NULL, 0);
romanc1607ce2022-09-12 10:12:39 +0200155 cmd_completion_add_match(match, matches, match_count);
156 free(match);
157 }
158}
159
160/**
161 * @brief Add module and/or node's children names to the hint.
162 *
163 * @param[in] module Compiled schema module.
164 * @param[in] parent Parent node of which children are potential matches.
165 * @param[in] hint_node_name Node name contained within the hint specified by user.
166 * @param[in,out] matches Matches provided to the user as a completion hint.
167 * @param[in,out] match_count Number of matches.
168 * @param[out] last_node Last processed node.
169 */
170static void
171add_all_children_nodes(const struct lysc_module *module, const struct lysc_node *parent,
172 const char *hint_node_name, char ***matches, unsigned int *match_count, const struct lysc_node **last_node)
173{
174 const struct lysc_node *node;
175 char *match, *node_name = NULL;
176
177 *last_node = NULL;
178
179 if (!parent && !module) {
180 return;
181 }
182
183 node = NULL;
romanf00089c2022-10-06 16:01:31 +0200184 while ((node = lys_getnext(node, parent, module, LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHCHOICE))) {
romanc1607ce2022-09-12 10:12:39 +0200185 if (parent && (node->module != parent->module)) {
186 /* augmented node */
187 if (asprintf(&node_name, "%s:%s", node->module->name, node->name) == -1) {
188 YLMSG_E("Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
189 break;
190 }
191 } else {
192 node_name = strdup(node->name);
193 if (!node_name) {
194 YLMSG_E("Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
195 break;
196 }
197 }
198 if (!hint_node_name || !strncmp(hint_node_name, node_name, strlen(hint_node_name))) {
199 /* adding just module names + their top level node(s) to the hint */
200 *last_node = node;
romanf00089c2022-10-06 16:01:31 +0200201 match = lysc_path(node, LYSC_PATH_LOG, NULL, 0);
romanc1607ce2022-09-12 10:12:39 +0200202 cmd_completion_add_match(match, matches, match_count);
203 free(match);
204 }
205 free(node_name);
206 }
207}
208
209/**
210 * @brief Provides completion for schemas.
211 *
212 * @param[in] hint User input.
213 * @param[out] matches Matches provided to the user as a completion hint.
214 * @param[out] match_count Number of matches.
215 */
216static void
217get_schema_completion(const char *hint, char ***matches, unsigned int *match_count)
218{
219 const struct lys_module *module;
romanf00089c2022-10-06 16:01:31 +0200220 uint32_t idx;
romanc1607ce2022-09-12 10:12:39 +0200221 const char *start;
222 char *end, *module_name = NULL, *path = NULL;
223 const struct lysc_node *parent, *last_node;
224 int rc = 0;
225 size_t len;
226
227 *match_count = 0;
228 *matches = NULL;
229
230 if (strlen(hint)) {
231 if (hint[0] != '/') {
232 return;
233 }
234 start = hint + 1;
235 } else {
236 start = hint;
237 }
238
239 end = strchr(start, ':');
240 if (!end) {
241 /* no module name */
242 len = strlen(start);
243
244 /* go through all the modules */
245 idx = 0;
246 while ((module = ly_ctx_get_module_iter(ctx, &idx))) {
247 if (!module->implemented) {
248 continue;
249 }
250
251 if (!len || !strncmp(start, module->name, len)) {
252 /* add all their (matching) top level nodes */
253 add_all_children_nodes(module->compiled, NULL, NULL, matches, match_count, &last_node);
254 }
255 }
256 } else {
257 /* module name known */
258 module_name = strndup(start, end - start);
259 if (!module_name) {
260 YLMSG_E("Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
261 rc = 1;
262 goto cleanup;
263 }
264
265 module = ly_ctx_get_module_implemented(ctx, module_name);
266 if (!module) {
267 goto cleanup;
268 }
269
270 /* find the last '/', if it is at the beginning of the hint, only path up to the top level node is known,
271 * else the name of the last node starts after the found '/' */
272 start = strrchr(hint, '/');
273 if (!start) {
274 goto cleanup;
275 }
276
277 if (start == hint) {
278 /* only the (incomplete) top level node path, add all (matching) top level nodes */
279 add_all_children_nodes(module->compiled, NULL, end + 1, matches, match_count, &last_node);
280 goto cleanup;
281 }
282
283 /* get rid of stuff after the last '/' to obtain the parent node */
284 path = strndup(hint, start - hint);
285 if (!path) {
286 YLMSG_E("Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
287 rc = 1;
288 goto cleanup;
289 }
290
romanf00089c2022-10-06 16:01:31 +0200291 /* get the last parent in the hint (it may not exist) */
292 parent = find_schema_path(ctx, path);
romanc1607ce2022-09-12 10:12:39 +0200293
294 /* add all (matching) child nodes of the parent */
295 add_all_children_nodes(NULL, parent, start + 1, matches, match_count, &last_node);
296 }
297
298cleanup:
299 if (!rc && (*match_count == 1)) {
300 /* to avoid a single hint (space at the end), add all children as hints */
301 single_hint_add_children(last_node, matches, match_count);
302 }
303 free(path);
304 free(module_name);
305}
306
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200307/**
aPiecek15cc4cf2023-04-17 15:23:21 +0200308 * @brief Get all possible argument hints for option.
309 *
310 * @param[in] hint User input.
311 * @param[out] matches Matches provided to the user as a completion hint.
312 * @param[out] match_count Number of matches.
313 */
314static void
315get_print_format_arg(const char *hint, char ***matches, unsigned int *match_count)
316{
317 const char *args[] = {"yang", "yin", "tree", "info", NULL};
318
319 get_arg_completion(hint, args, matches, match_count);
320}
321
322/**
323 * @copydoc get_print_format_arg
324 */
325static void
326get_data_type_arg(const char *hint, char ***matches, unsigned int *match_count)
327{
328 const char *args[] = {"data", "config", "get", "getconfig", "edit", "rpc", "reply", "notif", NULL};
329
330 get_arg_completion(hint, args, matches, match_count);
331}
332
333/**
334 * @copydoc get_print_format_arg
335 */
336static void
337get_data_in_format_arg(const char *hint, char ***matches, unsigned int *match_count)
338{
339 const char *args[] = {"xml", "json", "lyb", NULL};
340
341 get_arg_completion(hint, args, matches, match_count);
342}
343
344/**
345 * @copydoc get_print_format_arg
346 */
347static void
348get_data_default_arg(const char *hint, char ***matches, unsigned int *match_count)
349{
350 const char *args[] = {"all", "all-tagged", "trim", "implicit-tagged", NULL};
351
352 get_arg_completion(hint, args, matches, match_count);
353}
354
355/**
356 * @copydoc get_print_format_arg
357 */
358static void
359get_list_format_arg(const char *hint, char ***matches, unsigned int *match_count)
360{
361 const char *args[] = {"xml", "json", NULL};
362
363 get_arg_completion(hint, args, matches, match_count);
364}
365
aPiecek207e5e42023-05-22 09:01:33 +0200366/**
367 * @copydoc get_print_format_arg
368 */
369static void
370get_verb_arg(const char *hint, char ***matches, unsigned int *match_count)
371{
372 const char *args[] = {"error", "warning", "verbose", "debug", NULL};
373
374 get_arg_completion(hint, args, matches, match_count);
375}
376
aPiecek9e28e382023-05-22 08:40:25 +0200377#ifndef NDEBUG
378/**
379 * @copydoc get_print_format_arg
380 */
381static void
382get_debug_arg(const char *hint, char ***matches, unsigned int *match_count)
383{
384 const char *args[] = {"dict", "xpath", "dep-sets", NULL};
385
386 get_arg_completion(hint, args, matches, match_count);
387}
388
389#endif
390
aPiecek15cc4cf2023-04-17 15:23:21 +0200391/**
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200392 * @brief Get the string before the hint, which autocompletion is for.
393 *
394 * @param[in] buf Complete user input.
395 * @param[in] hint Hint part of the user input.
396 * @return Pointer to the last string.
397 */
398static const char *
399get_last_str(const char *buf, const char *hint)
400{
401 const char *ptr;
402
403 if (buf == hint) {
404 return buf;
405 }
406
407 ptr = hint - 1;
408 while (ptr[0] == ' ') {
409 --ptr;
410 if (buf == ptr) {
411 return buf;
412 }
413 }
414
415 while (ptr[-1] != ' ') {
416 --ptr;
417 if (buf == ptr) {
418 return buf;
419 }
420 }
421
422 return ptr;
423}
424
romanc1607ce2022-09-12 10:12:39 +0200425/* callback */
Radek Krejcied5acc52019-04-25 15:57:04 +0200426void
427complete_cmd(const char *buf, const char *hint, linenoiseCompletions *lc)
428{
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200429 struct autocomplete {
aPiecek15cc4cf2023-04-17 15:23:21 +0200430 enum COMMAND_INDEX ci; /**< command index to global variable 'commands' */
431 const char *opt; /**< optional option */
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200432 void (*ln_cb)(const char *, const char *, linenoiseCompletions *); /**< linenoise callback to call */
433 void (*yl_cb)(const char *, char ***, unsigned int *); /**< yanglint callback to call */
434 } ac[] = {
aPiecek15cc4cf2023-04-17 15:23:21 +0200435 {CMD_ADD, NULL, linenoisePathCompletion, NULL},
436 {CMD_PRINT, "-f", NULL, get_print_format_arg},
437 {CMD_PRINT, "-P", NULL, get_schema_completion},
438 {CMD_PRINT, "-o", linenoisePathCompletion, NULL},
439 {CMD_PRINT, NULL, NULL, get_model_completion},
440 {CMD_SEARCHPATH, NULL, linenoisePathCompletion, NULL},
aPiecek647f62e2023-05-18 10:55:58 +0200441 {CMD_EXTDATA, NULL, linenoisePathCompletion, NULL},
aPiecek21c1bc82023-05-18 15:38:31 +0200442 {CMD_CLEAR, "-Y", linenoisePathCompletion, NULL},
aPiecek15cc4cf2023-04-17 15:23:21 +0200443 {CMD_DATA, "-t", NULL, get_data_type_arg},
444 {CMD_DATA, "-O", linenoisePathCompletion, NULL},
aPiecek079bcde2023-05-05 11:48:25 +0200445 {CMD_DATA, "-R", linenoisePathCompletion, NULL},
aPiecek15cc4cf2023-04-17 15:23:21 +0200446 {CMD_DATA, "-f", NULL, get_data_in_format_arg},
447 {CMD_DATA, "-F", NULL, get_data_in_format_arg},
448 {CMD_DATA, "-d", NULL, get_data_default_arg},
449 {CMD_DATA, "-o", linenoisePathCompletion, NULL},
450 {CMD_DATA, NULL, linenoisePathCompletion, NULL},
451 {CMD_LIST, NULL, NULL, get_list_format_arg},
452 {CMD_FEATURE, NULL, NULL, get_model_completion},
aPiecek207e5e42023-05-22 09:01:33 +0200453 {CMD_VERB, NULL, NULL, get_verb_arg},
aPiecek9e28e382023-05-22 08:40:25 +0200454#ifndef NDEBUG
455 {CMD_DEBUG, NULL, NULL, get_debug_arg},
456#endif
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200457 };
aPiecek15cc4cf2023-04-17 15:23:21 +0200458 size_t name_len;
459 const char *last, *name, *getoptstr;
460 char opt[3] = {'\0', ':', '\0'};
Radek Krejcied5acc52019-04-25 15:57:04 +0200461 char **matches = NULL;
462 unsigned int match_count = 0, i;
463
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200464 if (buf == hint) {
465 /* command autocomplete */
Radek Krejcied5acc52019-04-25 15:57:04 +0200466 get_cmd_completion(hint, &matches, &match_count);
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200467
468 } else {
469 for (i = 0; i < (sizeof ac / sizeof *ac); ++i) {
aPiecek15cc4cf2023-04-17 15:23:21 +0200470 /* Find the right command. */
471 name = commands[ac[i].ci].name;
472 name_len = strlen(name);
473 if (strncmp(buf, name, name_len) || (buf[name_len] != ' ')) {
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200474 /* not this command */
475 continue;
476 }
477
aPiecek15cc4cf2023-04-17 15:23:21 +0200478 /* Select based on the right option. */
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200479 last = get_last_str(buf, hint);
aPiecek15cc4cf2023-04-17 15:23:21 +0200480 opt[0] = (last[0] == '-') && last[1] ? last[1] : '\0';
481 getoptstr = commands[ac[i].ci].optstring;
482 if (!ac[i].opt && opt[0] && strstr(getoptstr, opt)) {
483 /* completion for the argument must be defined */
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200484 continue;
aPiecek15cc4cf2023-04-17 15:23:21 +0200485 } else if (ac[i].opt && opt[0] && strncmp(ac[i].opt, last, strlen(ac[i].opt))) {
486 /* completion for (another) option */
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200487 continue;
aPiecek15cc4cf2023-04-17 15:23:21 +0200488 } else if (ac[i].opt && !opt[0]) {
489 /* completion is defined for option */
490 continue;
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200491 }
492
493 /* callback */
494 if (ac[i].ln_cb) {
495 ac[i].ln_cb(buf, hint, lc);
496 } else {
497 ac[i].yl_cb(hint, &matches, &match_count);
498 }
499 break;
500 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200501 }
502
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200503 /* transform matches into autocompletion, if needed */
Radek Krejcied5acc52019-04-25 15:57:04 +0200504 for (i = 0; i < match_count; ++i) {
505 linenoiseAddCompletion(lc, matches[i]);
506 free(matches[i]);
507 }
508 free(matches);
509}