Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | f8dc59a | 2020-11-25 13:47:44 +0100 | [diff] [blame] | 16 | #define _POSIX_C_SOURCE 200809L /* strdup */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 17 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 18 | #include <errno.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 22 | #include <string.h> |
| 23 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 24 | #include "libyang.h" |
| 25 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 26 | #include "cmd.h" |
| 27 | #include "common.h" |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 28 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 29 | #include "linenoise/linenoise.h" |
| 30 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 31 | /* from the main.c */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 32 | extern struct ly_ctx *ctx; |
| 33 | |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 34 | /** |
| 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 | */ |
| 41 | static void |
| 42 | cmd_completion_add_match(const char *match, char ***matches, unsigned int *match_count) |
| 43 | { |
| 44 | void *p; |
| 45 | |
aPiecek | ae7eae1 | 2023-06-22 09:12:39 +0200 | [diff] [blame] | 46 | p = realloc(*matches, (*match_count + 1) * sizeof **matches); |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 47 | if (!p) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 48 | YLMSG_E("Memory allocation failed (%s:%d, %s).", __FILE__, __LINE__, strerror(errno)); |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | *matches = p; |
aPiecek | ae7eae1 | 2023-06-22 09:12:39 +0200 | [diff] [blame] | 52 | (*matches)[*match_count] = strdup(match); |
| 53 | if (!((*matches)[*match_count])) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 54 | YLMSG_E("Memory allocation failed (%s:%d, %s).", __FILE__, __LINE__, strerror(errno)); |
aPiecek | ae7eae1 | 2023-06-22 09:12:39 +0200 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | ++(*match_count); |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @brief Provides completion for command names. |
| 62 | * |
| 63 | * @param[in] hint User input. |
| 64 | * @param[out] matches Matches provided to the user as a completion hint. |
| 65 | * @param[out] match_count Number of matches. |
| 66 | */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 67 | static void |
| 68 | get_cmd_completion(const char *hint, char ***matches, unsigned int *match_count) |
| 69 | { |
| 70 | int i; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 71 | |
| 72 | *match_count = 0; |
| 73 | *matches = NULL; |
| 74 | |
| 75 | for (i = 0; commands[i].name; i++) { |
| 76 | if (!strncmp(hint, commands[i].name, strlen(hint))) { |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 77 | cmd_completion_add_match(commands[i].name, matches, match_count); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 82 | /** |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 83 | * @brief Provides completion for arguments. |
| 84 | * |
| 85 | * @param[in] hint User input. |
| 86 | * @param[in] args Array of all possible arguments. The last element must be NULL. |
| 87 | * @param[out] matches Matches provided to the user as a completion hint. |
| 88 | * @param[out] match_count Number of matches. |
| 89 | */ |
| 90 | static void |
| 91 | get_arg_completion(const char *hint, const char **args, char ***matches, unsigned int *match_count) |
| 92 | { |
| 93 | int i; |
| 94 | |
| 95 | *match_count = 0; |
| 96 | *matches = NULL; |
| 97 | |
| 98 | for (i = 0; args[i]; i++) { |
| 99 | if (!strncmp(hint, args[i], strlen(hint))) { |
| 100 | cmd_completion_add_match(args[i], matches, match_count); |
| 101 | } |
| 102 | } |
| 103 | if (*match_count == 0) { |
| 104 | for (i = 0; args[i]; i++) { |
| 105 | cmd_completion_add_match(args[i], matches, match_count); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 111 | * @brief Provides completion for module names. |
| 112 | * |
| 113 | * @param[in] hint User input. |
| 114 | * @param[out] matches Matches provided to the user as a completion hint. |
| 115 | * @param[out] match_count Number of matches. |
| 116 | */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 117 | static void |
| 118 | get_model_completion(const char *hint, char ***matches, unsigned int *match_count) |
| 119 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 120 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 121 | uint32_t idx = 0; |
| 122 | const struct lys_module *module; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 123 | |
| 124 | *match_count = 0; |
| 125 | *matches = NULL; |
| 126 | |
| 127 | while ((module = ly_ctx_get_module_iter(ctx, &idx))) { |
| 128 | if (!strncmp(hint, module->name, strlen(hint))) { |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 129 | cmd_completion_add_match(module->name, matches, match_count); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | LY_ARRAY_FOR(module->parsed->includes, u) { |
| 133 | if (!strncmp(hint, module->parsed->includes[u].submodule->name, strlen(hint))) { |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 134 | cmd_completion_add_match(module->parsed->includes[u].submodule->name, matches, match_count); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 140 | /** |
| 141 | * @brief Add all child nodes of a single node to the completion hint. |
| 142 | * |
aPiecek | 40351e0 | 2023-06-22 09:11:18 +0200 | [diff] [blame] | 143 | * @param[in] parent Node of which children will be added to the hint. |
aPiecek | 27337da | 2023-06-19 14:49:29 +0200 | [diff] [blame] | 144 | * @param[out] matches Matches provided to the user as a completion hint. |
| 145 | * @param[out] match_count Number of matches. |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 146 | */ |
| 147 | static void |
aPiecek | 40351e0 | 2023-06-22 09:11:18 +0200 | [diff] [blame] | 148 | single_hint_add_children(const struct lysc_node *parent, char ***matches, unsigned int *match_count) |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 149 | { |
| 150 | const struct lysc_node *node = NULL; |
| 151 | char *match; |
| 152 | |
aPiecek | 40351e0 | 2023-06-22 09:11:18 +0200 | [diff] [blame] | 153 | if (!parent) { |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | |
aPiecek | 40351e0 | 2023-06-22 09:11:18 +0200 | [diff] [blame] | 157 | while ((node = lys_getnext(node, parent, NULL, LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHCHOICE))) { |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 158 | match = lysc_path(node, LYSC_PATH_LOG, NULL, 0); |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 159 | cmd_completion_add_match(match, matches, match_count); |
| 160 | free(match); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @brief Add module and/or node's children names to the hint. |
| 166 | * |
| 167 | * @param[in] module Compiled schema module. |
| 168 | * @param[in] parent Parent node of which children are potential matches. |
| 169 | * @param[in] hint_node_name Node name contained within the hint specified by user. |
| 170 | * @param[in,out] matches Matches provided to the user as a completion hint. |
| 171 | * @param[in,out] match_count Number of matches. |
| 172 | * @param[out] last_node Last processed node. |
| 173 | */ |
| 174 | static void |
| 175 | add_all_children_nodes(const struct lysc_module *module, const struct lysc_node *parent, |
| 176 | const char *hint_node_name, char ***matches, unsigned int *match_count, const struct lysc_node **last_node) |
| 177 | { |
| 178 | const struct lysc_node *node; |
| 179 | char *match, *node_name = NULL; |
| 180 | |
| 181 | *last_node = NULL; |
| 182 | |
| 183 | if (!parent && !module) { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | node = NULL; |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 188 | while ((node = lys_getnext(node, parent, module, LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHCHOICE))) { |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 189 | if (parent && (node->module != parent->module)) { |
| 190 | /* augmented node */ |
| 191 | if (asprintf(&node_name, "%s:%s", node->module->name, node->name) == -1) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 192 | YLMSG_E("Memory allocation failed (%s:%d, %s).", __FILE__, __LINE__, strerror(errno)); |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 193 | break; |
| 194 | } |
| 195 | } else { |
| 196 | node_name = strdup(node->name); |
| 197 | if (!node_name) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 198 | YLMSG_E("Memory allocation failed (%s:%d, %s).", __FILE__, __LINE__, strerror(errno)); |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 199 | break; |
| 200 | } |
| 201 | } |
| 202 | if (!hint_node_name || !strncmp(hint_node_name, node_name, strlen(hint_node_name))) { |
| 203 | /* adding just module names + their top level node(s) to the hint */ |
| 204 | *last_node = node; |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 205 | match = lysc_path(node, LYSC_PATH_LOG, NULL, 0); |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 206 | cmd_completion_add_match(match, matches, match_count); |
| 207 | free(match); |
| 208 | } |
| 209 | free(node_name); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * @brief Provides completion for schemas. |
| 215 | * |
| 216 | * @param[in] hint User input. |
| 217 | * @param[out] matches Matches provided to the user as a completion hint. |
| 218 | * @param[out] match_count Number of matches. |
| 219 | */ |
| 220 | static void |
| 221 | get_schema_completion(const char *hint, char ***matches, unsigned int *match_count) |
| 222 | { |
| 223 | const struct lys_module *module; |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 224 | uint32_t idx; |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 225 | const char *start; |
| 226 | char *end, *module_name = NULL, *path = NULL; |
| 227 | const struct lysc_node *parent, *last_node; |
| 228 | int rc = 0; |
| 229 | size_t len; |
| 230 | |
| 231 | *match_count = 0; |
| 232 | *matches = NULL; |
| 233 | |
| 234 | if (strlen(hint)) { |
| 235 | if (hint[0] != '/') { |
| 236 | return; |
| 237 | } |
| 238 | start = hint + 1; |
| 239 | } else { |
| 240 | start = hint; |
| 241 | } |
| 242 | |
| 243 | end = strchr(start, ':'); |
| 244 | if (!end) { |
| 245 | /* no module name */ |
| 246 | len = strlen(start); |
| 247 | |
| 248 | /* go through all the modules */ |
| 249 | idx = 0; |
| 250 | while ((module = ly_ctx_get_module_iter(ctx, &idx))) { |
| 251 | if (!module->implemented) { |
| 252 | continue; |
| 253 | } |
| 254 | |
| 255 | if (!len || !strncmp(start, module->name, len)) { |
| 256 | /* add all their (matching) top level nodes */ |
| 257 | add_all_children_nodes(module->compiled, NULL, NULL, matches, match_count, &last_node); |
| 258 | } |
| 259 | } |
| 260 | } else { |
| 261 | /* module name known */ |
| 262 | module_name = strndup(start, end - start); |
| 263 | if (!module_name) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 264 | YLMSG_E("Memory allocation failed (%s:%d, %s).", __FILE__, __LINE__, strerror(errno)); |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 265 | rc = 1; |
| 266 | goto cleanup; |
| 267 | } |
| 268 | |
| 269 | module = ly_ctx_get_module_implemented(ctx, module_name); |
| 270 | if (!module) { |
| 271 | goto cleanup; |
| 272 | } |
| 273 | |
| 274 | /* find the last '/', if it is at the beginning of the hint, only path up to the top level node is known, |
| 275 | * else the name of the last node starts after the found '/' */ |
| 276 | start = strrchr(hint, '/'); |
| 277 | if (!start) { |
| 278 | goto cleanup; |
| 279 | } |
| 280 | |
| 281 | if (start == hint) { |
| 282 | /* only the (incomplete) top level node path, add all (matching) top level nodes */ |
| 283 | add_all_children_nodes(module->compiled, NULL, end + 1, matches, match_count, &last_node); |
| 284 | goto cleanup; |
| 285 | } |
| 286 | |
| 287 | /* get rid of stuff after the last '/' to obtain the parent node */ |
| 288 | path = strndup(hint, start - hint); |
| 289 | if (!path) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 290 | YLMSG_E("Memory allocation failed (%s:%d, %s).", __FILE__, __LINE__, strerror(errno)); |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 291 | rc = 1; |
| 292 | goto cleanup; |
| 293 | } |
| 294 | |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 295 | /* get the last parent in the hint (it may not exist) */ |
| 296 | parent = find_schema_path(ctx, path); |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 297 | |
| 298 | /* add all (matching) child nodes of the parent */ |
| 299 | add_all_children_nodes(NULL, parent, start + 1, matches, match_count, &last_node); |
| 300 | } |
| 301 | |
| 302 | cleanup: |
| 303 | if (!rc && (*match_count == 1)) { |
| 304 | /* to avoid a single hint (space at the end), add all children as hints */ |
| 305 | single_hint_add_children(last_node, matches, match_count); |
| 306 | } |
| 307 | free(path); |
| 308 | free(module_name); |
| 309 | } |
| 310 | |
Michal Vasko | a1c986f | 2022-09-14 12:01:48 +0200 | [diff] [blame] | 311 | /** |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 312 | * @brief Get all possible argument hints for option. |
| 313 | * |
| 314 | * @param[in] hint User input. |
| 315 | * @param[out] matches Matches provided to the user as a completion hint. |
| 316 | * @param[out] match_count Number of matches. |
| 317 | */ |
| 318 | static void |
| 319 | get_print_format_arg(const char *hint, char ***matches, unsigned int *match_count) |
| 320 | { |
| 321 | const char *args[] = {"yang", "yin", "tree", "info", NULL}; |
| 322 | |
| 323 | get_arg_completion(hint, args, matches, match_count); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * @copydoc get_print_format_arg |
| 328 | */ |
| 329 | static void |
| 330 | get_data_type_arg(const char *hint, char ***matches, unsigned int *match_count) |
| 331 | { |
| 332 | const char *args[] = {"data", "config", "get", "getconfig", "edit", "rpc", "reply", "notif", NULL}; |
| 333 | |
| 334 | get_arg_completion(hint, args, matches, match_count); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * @copydoc get_print_format_arg |
| 339 | */ |
| 340 | static void |
| 341 | get_data_in_format_arg(const char *hint, char ***matches, unsigned int *match_count) |
| 342 | { |
| 343 | const char *args[] = {"xml", "json", "lyb", NULL}; |
| 344 | |
| 345 | get_arg_completion(hint, args, matches, match_count); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * @copydoc get_print_format_arg |
| 350 | */ |
| 351 | static void |
| 352 | get_data_default_arg(const char *hint, char ***matches, unsigned int *match_count) |
| 353 | { |
| 354 | const char *args[] = {"all", "all-tagged", "trim", "implicit-tagged", NULL}; |
| 355 | |
| 356 | get_arg_completion(hint, args, matches, match_count); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * @copydoc get_print_format_arg |
| 361 | */ |
| 362 | static void |
| 363 | get_list_format_arg(const char *hint, char ***matches, unsigned int *match_count) |
| 364 | { |
| 365 | const char *args[] = {"xml", "json", NULL}; |
| 366 | |
| 367 | get_arg_completion(hint, args, matches, match_count); |
| 368 | } |
| 369 | |
aPiecek | 207e5e4 | 2023-05-22 09:01:33 +0200 | [diff] [blame] | 370 | /** |
| 371 | * @copydoc get_print_format_arg |
| 372 | */ |
| 373 | static void |
| 374 | get_verb_arg(const char *hint, char ***matches, unsigned int *match_count) |
| 375 | { |
| 376 | const char *args[] = {"error", "warning", "verbose", "debug", NULL}; |
| 377 | |
| 378 | get_arg_completion(hint, args, matches, match_count); |
| 379 | } |
| 380 | |
aPiecek | 9e28e38 | 2023-05-22 08:40:25 +0200 | [diff] [blame] | 381 | /** |
| 382 | * @copydoc get_print_format_arg |
| 383 | */ |
| 384 | static void |
| 385 | get_debug_arg(const char *hint, char ***matches, unsigned int *match_count) |
| 386 | { |
| 387 | const char *args[] = {"dict", "xpath", "dep-sets", NULL}; |
| 388 | |
| 389 | get_arg_completion(hint, args, matches, match_count); |
| 390 | } |
| 391 | |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 392 | /** |
Michal Vasko | a1c986f | 2022-09-14 12:01:48 +0200 | [diff] [blame] | 393 | * @brief Get the string before the hint, which autocompletion is for. |
| 394 | * |
| 395 | * @param[in] buf Complete user input. |
| 396 | * @param[in] hint Hint part of the user input. |
| 397 | * @return Pointer to the last string. |
| 398 | */ |
| 399 | static const char * |
| 400 | get_last_str(const char *buf, const char *hint) |
| 401 | { |
| 402 | const char *ptr; |
| 403 | |
| 404 | if (buf == hint) { |
| 405 | return buf; |
| 406 | } |
| 407 | |
| 408 | ptr = hint - 1; |
| 409 | while (ptr[0] == ' ') { |
| 410 | --ptr; |
| 411 | if (buf == ptr) { |
| 412 | return buf; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | while (ptr[-1] != ' ') { |
| 417 | --ptr; |
| 418 | if (buf == ptr) { |
| 419 | return buf; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | return ptr; |
| 424 | } |
| 425 | |
roman | c1607ce | 2022-09-12 10:12:39 +0200 | [diff] [blame] | 426 | /* callback */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 427 | void |
| 428 | complete_cmd(const char *buf, const char *hint, linenoiseCompletions *lc) |
| 429 | { |
Michal Vasko | a1c986f | 2022-09-14 12:01:48 +0200 | [diff] [blame] | 430 | struct autocomplete { |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 431 | enum COMMAND_INDEX ci; /**< command index to global variable 'commands' */ |
| 432 | const char *opt; /**< optional option */ |
Michal Vasko | a1c986f | 2022-09-14 12:01:48 +0200 | [diff] [blame] | 433 | void (*ln_cb)(const char *, const char *, linenoiseCompletions *); /**< linenoise callback to call */ |
| 434 | void (*yl_cb)(const char *, char ***, unsigned int *); /**< yanglint callback to call */ |
| 435 | } ac[] = { |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 436 | {CMD_ADD, NULL, linenoisePathCompletion, NULL}, |
| 437 | {CMD_PRINT, "-f", NULL, get_print_format_arg}, |
| 438 | {CMD_PRINT, "-P", NULL, get_schema_completion}, |
| 439 | {CMD_PRINT, "-o", linenoisePathCompletion, NULL}, |
| 440 | {CMD_PRINT, NULL, NULL, get_model_completion}, |
| 441 | {CMD_SEARCHPATH, NULL, linenoisePathCompletion, NULL}, |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 442 | {CMD_EXTDATA, NULL, linenoisePathCompletion, NULL}, |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 443 | {CMD_CLEAR, "-Y", linenoisePathCompletion, NULL}, |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 444 | {CMD_DATA, "-t", NULL, get_data_type_arg}, |
| 445 | {CMD_DATA, "-O", linenoisePathCompletion, NULL}, |
aPiecek | 079bcde | 2023-05-05 11:48:25 +0200 | [diff] [blame] | 446 | {CMD_DATA, "-R", linenoisePathCompletion, NULL}, |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 447 | {CMD_DATA, "-f", NULL, get_data_in_format_arg}, |
| 448 | {CMD_DATA, "-F", NULL, get_data_in_format_arg}, |
| 449 | {CMD_DATA, "-d", NULL, get_data_default_arg}, |
| 450 | {CMD_DATA, "-o", linenoisePathCompletion, NULL}, |
| 451 | {CMD_DATA, NULL, linenoisePathCompletion, NULL}, |
| 452 | {CMD_LIST, NULL, NULL, get_list_format_arg}, |
| 453 | {CMD_FEATURE, NULL, NULL, get_model_completion}, |
aPiecek | 207e5e4 | 2023-05-22 09:01:33 +0200 | [diff] [blame] | 454 | {CMD_VERB, NULL, NULL, get_verb_arg}, |
aPiecek | 9e28e38 | 2023-05-22 08:40:25 +0200 | [diff] [blame] | 455 | {CMD_DEBUG, NULL, NULL, get_debug_arg}, |
Michal Vasko | a1c986f | 2022-09-14 12:01:48 +0200 | [diff] [blame] | 456 | }; |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 457 | size_t name_len; |
| 458 | const char *last, *name, *getoptstr; |
| 459 | char opt[3] = {'\0', ':', '\0'}; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 460 | char **matches = NULL; |
| 461 | unsigned int match_count = 0, i; |
| 462 | |
Michal Vasko | a1c986f | 2022-09-14 12:01:48 +0200 | [diff] [blame] | 463 | if (buf == hint) { |
| 464 | /* command autocomplete */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 465 | get_cmd_completion(hint, &matches, &match_count); |
Michal Vasko | a1c986f | 2022-09-14 12:01:48 +0200 | [diff] [blame] | 466 | |
| 467 | } else { |
| 468 | for (i = 0; i < (sizeof ac / sizeof *ac); ++i) { |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 469 | /* Find the right command. */ |
| 470 | name = commands[ac[i].ci].name; |
| 471 | name_len = strlen(name); |
| 472 | if (strncmp(buf, name, name_len) || (buf[name_len] != ' ')) { |
Michal Vasko | a1c986f | 2022-09-14 12:01:48 +0200 | [diff] [blame] | 473 | /* not this command */ |
| 474 | continue; |
| 475 | } |
| 476 | |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 477 | /* Select based on the right option. */ |
Michal Vasko | a1c986f | 2022-09-14 12:01:48 +0200 | [diff] [blame] | 478 | last = get_last_str(buf, hint); |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 479 | opt[0] = (last[0] == '-') && last[1] ? last[1] : '\0'; |
| 480 | getoptstr = commands[ac[i].ci].optstring; |
| 481 | if (!ac[i].opt && opt[0] && strstr(getoptstr, opt)) { |
| 482 | /* completion for the argument must be defined */ |
Michal Vasko | a1c986f | 2022-09-14 12:01:48 +0200 | [diff] [blame] | 483 | continue; |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 484 | } else if (ac[i].opt && opt[0] && strncmp(ac[i].opt, last, strlen(ac[i].opt))) { |
| 485 | /* completion for (another) option */ |
Michal Vasko | a1c986f | 2022-09-14 12:01:48 +0200 | [diff] [blame] | 486 | continue; |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 487 | } else if (ac[i].opt && !opt[0]) { |
| 488 | /* completion is defined for option */ |
| 489 | continue; |
Michal Vasko | a1c986f | 2022-09-14 12:01:48 +0200 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /* callback */ |
| 493 | if (ac[i].ln_cb) { |
| 494 | ac[i].ln_cb(buf, hint, lc); |
| 495 | } else { |
| 496 | ac[i].yl_cb(hint, &matches, &match_count); |
| 497 | } |
| 498 | break; |
| 499 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 500 | } |
| 501 | |
Michal Vasko | a1c986f | 2022-09-14 12:01:48 +0200 | [diff] [blame] | 502 | /* transform matches into autocompletion, if needed */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 503 | for (i = 0; i < match_count; ++i) { |
| 504 | linenoiseAddCompletion(lc, matches[i]); |
| 505 | free(matches[i]); |
| 506 | } |
| 507 | free(matches); |
| 508 | } |