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" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 28 | #include "linenoise/linenoise.h" |
| 29 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 30 | /* from the main.c */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 31 | extern struct ly_ctx *ctx; |
| 32 | |
| 33 | static void |
| 34 | get_cmd_completion(const char *hint, char ***matches, unsigned int *match_count) |
| 35 | { |
| 36 | int i; |
| 37 | void *p; |
| 38 | |
| 39 | *match_count = 0; |
| 40 | *matches = NULL; |
| 41 | |
| 42 | for (i = 0; commands[i].name; i++) { |
| 43 | if (!strncmp(hint, commands[i].name, strlen(hint))) { |
| 44 | ++(*match_count); |
| 45 | p = realloc(*matches, *match_count * sizeof **matches); |
| 46 | if (!p) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 47 | YLMSG_E("Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno)); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | *matches = p; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 51 | (*matches)[*match_count - 1] = strdup(commands[i].name); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static int |
| 57 | last_is_opt(const char *hint) |
| 58 | { |
| 59 | const char *ptr; |
| 60 | |
| 61 | /* last is option */ |
| 62 | if (hint[0] == '-') { |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | do { |
| 67 | --hint; |
| 68 | } while (hint[0] == ' '); |
| 69 | |
| 70 | /* last is option argument */ |
| 71 | ptr = strrchr(hint, ' '); |
| 72 | if (ptr) { |
| 73 | ++ptr; |
| 74 | if (ptr[0] == '-') { |
| 75 | return 1; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static void |
| 83 | get_model_completion(const char *hint, char ***matches, unsigned int *match_count) |
| 84 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 85 | LY_ARRAY_COUNT_TYPE u; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 86 | uint32_t idx = 0; |
| 87 | const struct lys_module *module; |
| 88 | void *p; |
| 89 | |
| 90 | *match_count = 0; |
| 91 | *matches = NULL; |
| 92 | |
| 93 | while ((module = ly_ctx_get_module_iter(ctx, &idx))) { |
| 94 | if (!strncmp(hint, module->name, strlen(hint))) { |
| 95 | ++(*match_count); |
| 96 | p = realloc(*matches, *match_count * sizeof **matches); |
| 97 | if (!p) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 98 | YLMSG_E("Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno)); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | *matches = p; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 102 | (*matches)[*match_count - 1] = strdup(module->name); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | LY_ARRAY_FOR(module->parsed->includes, u) { |
| 106 | if (!strncmp(hint, module->parsed->includes[u].submodule->name, strlen(hint))) { |
| 107 | ++(*match_count); |
| 108 | p = realloc(*matches, *match_count * sizeof **matches); |
| 109 | if (!p) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 110 | YLMSG_E("Memory allocation failed (%s:%d, %s)", __FILE__, __LINE__, strerror(errno)); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 111 | return; |
| 112 | } |
| 113 | *matches = p; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 114 | (*matches)[*match_count - 1] = strdup(module->parsed->includes[u].submodule->name); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | complete_cmd(const char *buf, const char *hint, linenoiseCompletions *lc) |
| 122 | { |
| 123 | char **matches = NULL; |
| 124 | unsigned int match_count = 0, i; |
| 125 | |
| 126 | if (!strncmp(buf, "add ", 4)) { |
| 127 | linenoisePathCompletion(buf, hint, lc); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 128 | } else if ((!strncmp(buf, "searchpath ", 11) || !strncmp(buf, "data ", 5) || |
| 129 | !strncmp(buf, "config ", 7) || !strncmp(buf, "filter ", 7) || |
| 130 | !strncmp(buf, "xpath ", 6) || !strncmp(buf, "clear ", 6)) && !last_is_opt(hint)) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 131 | linenoisePathCompletion(buf, hint, lc); |
| 132 | } else if ((!strncmp(buf, "print ", 6) || !strncmp(buf, "feature ", 8)) && !last_is_opt(hint)) { |
| 133 | get_model_completion(hint, &matches, &match_count); |
| 134 | } else if (!strchr(buf, ' ') && hint[0]) { |
| 135 | get_cmd_completion(hint, &matches, &match_count); |
| 136 | } |
| 137 | |
| 138 | for (i = 0; i < match_count; ++i) { |
| 139 | linenoiseAddCompletion(lc, matches[i]); |
| 140 | free(matches[i]); |
| 141 | } |
| 142 | free(matches); |
| 143 | } |