blob: 9e2b366a640231ff508fd4e0dc54e6ccff67009b [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/**
romanc1607ce2022-09-12 10:12:39 +020079 * @brief Provides completion for module names.
80 *
81 * @param[in] hint User input.
82 * @param[out] matches Matches provided to the user as a completion hint.
83 * @param[out] match_count Number of matches.
84 */
Radek Krejcied5acc52019-04-25 15:57:04 +020085static void
86get_model_completion(const char *hint, char ***matches, unsigned int *match_count)
87{
Michal Vaskofd69e1d2020-07-03 11:57:17 +020088 LY_ARRAY_COUNT_TYPE u;
Radek Krejcied5acc52019-04-25 15:57:04 +020089 uint32_t idx = 0;
90 const struct lys_module *module;
Radek Krejcied5acc52019-04-25 15:57:04 +020091
92 *match_count = 0;
93 *matches = NULL;
94
95 while ((module = ly_ctx_get_module_iter(ctx, &idx))) {
96 if (!strncmp(hint, module->name, strlen(hint))) {
romanc1607ce2022-09-12 10:12:39 +020097 cmd_completion_add_match(module->name, matches, match_count);
Radek Krejcied5acc52019-04-25 15:57:04 +020098 }
99
100 LY_ARRAY_FOR(module->parsed->includes, u) {
101 if (!strncmp(hint, module->parsed->includes[u].submodule->name, strlen(hint))) {
romanc1607ce2022-09-12 10:12:39 +0200102 cmd_completion_add_match(module->parsed->includes[u].submodule->name, matches, match_count);
Radek Krejcied5acc52019-04-25 15:57:04 +0200103 }
104 }
105 }
106}
107
romanc1607ce2022-09-12 10:12:39 +0200108/**
109 * @brief Add all child nodes of a single node to the completion hint.
110 *
111 * @param[in] last_node Node of which children will be added to the hint.
112 * @param matches[out] Matches provided to the user as a completion hint.
113 * @param match_count[out] Number of matches.
114 */
115static void
116single_hint_add_children(const struct lysc_node *last_node, char ***matches, unsigned int *match_count)
117{
118 const struct lysc_node *node = NULL;
119 char *match;
120
121 if (!last_node) {
122 return;
123 }
124
125 while ((node = lys_getnext(node, last_node, NULL, 0))) {
126 match = lysc_path(node, LYSC_PATH_DATA, NULL, 0);
127 cmd_completion_add_match(match, matches, match_count);
128 free(match);
129 }
130}
131
132/**
133 * @brief Add module and/or node's children names to the hint.
134 *
135 * @param[in] module Compiled schema module.
136 * @param[in] parent Parent node of which children are potential matches.
137 * @param[in] hint_node_name Node name contained within the hint specified by user.
138 * @param[in,out] matches Matches provided to the user as a completion hint.
139 * @param[in,out] match_count Number of matches.
140 * @param[out] last_node Last processed node.
141 */
142static void
143add_all_children_nodes(const struct lysc_module *module, const struct lysc_node *parent,
144 const char *hint_node_name, char ***matches, unsigned int *match_count, const struct lysc_node **last_node)
145{
146 const struct lysc_node *node;
147 char *match, *node_name = NULL;
148
149 *last_node = NULL;
150
151 if (!parent && !module) {
152 return;
153 }
154
155 node = NULL;
156 while ((node = lys_getnext(node, parent, module, 0))) {
157 if (parent && (node->module != parent->module)) {
158 /* augmented node */
159 if (asprintf(&node_name, "%s:%s", node->module->name, node->name) == -1) {
160 YLMSG_E("Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
161 break;
162 }
163 } else {
164 node_name = strdup(node->name);
165 if (!node_name) {
166 YLMSG_E("Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
167 break;
168 }
169 }
170 if (!hint_node_name || !strncmp(hint_node_name, node_name, strlen(hint_node_name))) {
171 /* adding just module names + their top level node(s) to the hint */
172 *last_node = node;
173 match = lysc_path(node, LYSC_PATH_DATA, NULL, 0);
174 cmd_completion_add_match(match, matches, match_count);
175 free(match);
176 }
177 free(node_name);
178 }
179}
180
181/**
182 * @brief Provides completion for schemas.
183 *
184 * @param[in] hint User input.
185 * @param[out] matches Matches provided to the user as a completion hint.
186 * @param[out] match_count Number of matches.
187 */
188static void
189get_schema_completion(const char *hint, char ***matches, unsigned int *match_count)
190{
191 const struct lys_module *module;
192 uint32_t idx, prev_lo;
193 const char *start;
194 char *end, *module_name = NULL, *path = NULL;
195 const struct lysc_node *parent, *last_node;
196 int rc = 0;
197 size_t len;
198
199 *match_count = 0;
200 *matches = NULL;
201
202 if (strlen(hint)) {
203 if (hint[0] != '/') {
204 return;
205 }
206 start = hint + 1;
207 } else {
208 start = hint;
209 }
210
211 end = strchr(start, ':');
212 if (!end) {
213 /* no module name */
214 len = strlen(start);
215
216 /* go through all the modules */
217 idx = 0;
218 while ((module = ly_ctx_get_module_iter(ctx, &idx))) {
219 if (!module->implemented) {
220 continue;
221 }
222
223 if (!len || !strncmp(start, module->name, len)) {
224 /* add all their (matching) top level nodes */
225 add_all_children_nodes(module->compiled, NULL, NULL, matches, match_count, &last_node);
226 }
227 }
228 } else {
229 /* module name known */
230 module_name = strndup(start, end - start);
231 if (!module_name) {
232 YLMSG_E("Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
233 rc = 1;
234 goto cleanup;
235 }
236
237 module = ly_ctx_get_module_implemented(ctx, module_name);
238 if (!module) {
239 goto cleanup;
240 }
241
242 /* find the last '/', if it is at the beginning of the hint, only path up to the top level node is known,
243 * else the name of the last node starts after the found '/' */
244 start = strrchr(hint, '/');
245 if (!start) {
246 goto cleanup;
247 }
248
249 if (start == hint) {
250 /* only the (incomplete) top level node path, add all (matching) top level nodes */
251 add_all_children_nodes(module->compiled, NULL, end + 1, matches, match_count, &last_node);
252 goto cleanup;
253 }
254
255 /* get rid of stuff after the last '/' to obtain the parent node */
256 path = strndup(hint, start - hint);
257 if (!path) {
258 YLMSG_E("Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno));
259 rc = 1;
260 goto cleanup;
261 }
262
263 /* silently get the last parent in the hint (it may not exist) */
264 prev_lo = ly_log_options(0);
265 parent = lys_find_path(ctx, NULL, path, 0);
266 ly_log_options(prev_lo);
267
268 /* add all (matching) child nodes of the parent */
269 add_all_children_nodes(NULL, parent, start + 1, matches, match_count, &last_node);
270 }
271
272cleanup:
273 if (!rc && (*match_count == 1)) {
274 /* to avoid a single hint (space at the end), add all children as hints */
275 single_hint_add_children(last_node, matches, match_count);
276 }
277 free(path);
278 free(module_name);
279}
280
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200281/**
282 * @brief Get the string before the hint, which autocompletion is for.
283 *
284 * @param[in] buf Complete user input.
285 * @param[in] hint Hint part of the user input.
286 * @return Pointer to the last string.
287 */
288static const char *
289get_last_str(const char *buf, const char *hint)
290{
291 const char *ptr;
292
293 if (buf == hint) {
294 return buf;
295 }
296
297 ptr = hint - 1;
298 while (ptr[0] == ' ') {
299 --ptr;
300 if (buf == ptr) {
301 return buf;
302 }
303 }
304
305 while (ptr[-1] != ' ') {
306 --ptr;
307 if (buf == ptr) {
308 return buf;
309 }
310 }
311
312 return ptr;
313}
314
romanc1607ce2022-09-12 10:12:39 +0200315/* callback */
Radek Krejcied5acc52019-04-25 15:57:04 +0200316void
317complete_cmd(const char *buf, const char *hint, linenoiseCompletions *lc)
318{
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200319 struct autocomplete {
320 const char *cmd; /**< command */
321 const char *opt; /**< optional option */
322 int last_opt; /**< whether to autocomplete even if an option is last in the hint */
323
324 void (*ln_cb)(const char *, const char *, linenoiseCompletions *); /**< linenoise callback to call */
325 void (*yl_cb)(const char *, char ***, unsigned int *); /**< yanglint callback to call */
326 } ac[] = {
327 {"add", NULL, 1, linenoisePathCompletion, NULL},
328 {"searchpath", NULL, 0, linenoisePathCompletion, NULL},
329 {"data", NULL, 0, linenoisePathCompletion, NULL},
330 {"print", NULL, 0, NULL, get_model_completion},
331 {"feature", NULL, 0, NULL, get_model_completion},
332 {"print", "-P", 1, NULL, get_schema_completion},
333 };
334 size_t cmd_len;
335 const char *last;
Radek Krejcied5acc52019-04-25 15:57:04 +0200336 char **matches = NULL;
337 unsigned int match_count = 0, i;
338
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200339 if (buf == hint) {
340 /* command autocomplete */
Radek Krejcied5acc52019-04-25 15:57:04 +0200341 get_cmd_completion(hint, &matches, &match_count);
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200342
343 } else {
344 for (i = 0; i < (sizeof ac / sizeof *ac); ++i) {
345 cmd_len = strlen(ac[i].cmd);
346 if (strncmp(buf, ac[i].cmd, cmd_len) || (buf[cmd_len] != ' ')) {
347 /* not this command */
348 continue;
349 }
350
351 last = get_last_str(buf, hint);
352 if (ac[i].opt && strncmp(ac[i].opt, last, strlen(ac[i].opt))) {
353 /* autocompletion for (another) option */
354 continue;
355 }
356 if (!ac[i].last_opt && (last[0] == '-')) {
357 /* autocompletion for the command, not an option */
358 continue;
359 }
360 if ((last != buf) && (last[0] != '-')) {
361 /* autocompleted */
362 return;
363 }
364
365 /* callback */
366 if (ac[i].ln_cb) {
367 ac[i].ln_cb(buf, hint, lc);
368 } else {
369 ac[i].yl_cb(hint, &matches, &match_count);
370 }
371 break;
372 }
Radek Krejcied5acc52019-04-25 15:57:04 +0200373 }
374
Michal Vaskoa1c986f2022-09-14 12:01:48 +0200375 /* transform matches into autocompletion, if needed */
Radek Krejcied5acc52019-04-25 15:57:04 +0200376 for (i = 0; i < match_count; ++i) {
377 linenoiseAddCompletion(lc, matches[i]);
378 free(matches[i]);
379 }
380 free(matches);
381}