Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file cmd.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 5 | * @brief libyang's yanglint tool general commands |
| 6 | * |
| 7 | * Copyright (c) 2015-2020 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #define _GNU_SOURCE |
| 17 | |
| 18 | #include "cmd.h" |
| 19 | |
| 20 | #include <getopt.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include <strings.h> |
| 25 | |
| 26 | #include "common.h" |
| 27 | #include "compat.h" |
| 28 | #include "libyang.h" |
| 29 | |
| 30 | COMMAND commands[]; |
| 31 | extern int done; |
| 32 | |
| 33 | #ifndef NDEBUG |
| 34 | |
| 35 | void |
| 36 | cmd_debug_help(void) |
| 37 | { |
aPiecek | 9e28e38 | 2023-05-22 08:40:25 +0200 | [diff] [blame] | 38 | printf("Usage: debug (dict | xpath | dep-sets)+\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void |
| 42 | cmd_debug(struct ly_ctx **UNUSED(ctx), const char *cmdline) |
| 43 | { |
| 44 | int argc = 0; |
| 45 | char **argv = NULL; |
| 46 | int opt, opt_index; |
| 47 | struct option options[] = { |
| 48 | {"help", no_argument, NULL, 'h'}, |
| 49 | {NULL, 0, NULL, 0} |
| 50 | }; |
| 51 | uint32_t dbg_groups = 0; |
| 52 | |
| 53 | if (parse_cmdline(cmdline, &argc, &argv)) { |
| 54 | goto cleanup; |
| 55 | } |
| 56 | |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 57 | while ((opt = getopt_long(argc, argv, commands[CMD_DEBUG].optstring, options, &opt_index)) != -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 58 | switch (opt) { |
| 59 | case 'h': |
| 60 | cmd_debug_help(); |
| 61 | goto cleanup; |
| 62 | default: |
| 63 | YLMSG_E("Unknown option.\n"); |
| 64 | goto cleanup; |
| 65 | } |
| 66 | } |
| 67 | if (argc == optind) { |
| 68 | /* no argument */ |
| 69 | cmd_debug_help(); |
| 70 | goto cleanup; |
| 71 | } |
| 72 | |
| 73 | for (int i = 0; i < argc - optind; i++) { |
| 74 | if (!strcasecmp("dict", argv[optind + i])) { |
| 75 | dbg_groups |= LY_LDGDICT; |
| 76 | } else if (!strcasecmp("xpath", argv[optind + i])) { |
| 77 | dbg_groups |= LY_LDGXPATH; |
aPiecek | 9e28e38 | 2023-05-22 08:40:25 +0200 | [diff] [blame] | 78 | } else if (!strcasecmp("dep-sets", argv[optind + i])) { |
| 79 | dbg_groups |= LY_LDGDEPSETS; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 80 | } else { |
| 81 | YLMSG_E("Unknown debug group \"%s\"\n", argv[optind + 1]); |
| 82 | goto cleanup; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | ly_log_dbg_groups(dbg_groups); |
| 87 | |
| 88 | cleanup: |
| 89 | free_cmdline(argv); |
| 90 | } |
| 91 | |
| 92 | #endif |
| 93 | |
| 94 | void |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 95 | cmd_free(void) |
| 96 | { |
| 97 | uint16_t i; |
| 98 | |
| 99 | for (i = 0; commands[i].name; i++) { |
| 100 | if (commands[i].free_func) { |
| 101 | commands[i].free_func(); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 107 | cmd_verb_help(void) |
| 108 | { |
| 109 | printf("Usage: verb (error | warning | verbose | debug)\n"); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | cmd_verb(struct ly_ctx **UNUSED(ctx), const char *cmdline) |
| 114 | { |
| 115 | int argc = 0; |
| 116 | char **argv = NULL; |
| 117 | int opt, opt_index; |
| 118 | struct option options[] = { |
| 119 | {"help", no_argument, NULL, 'h'}, |
| 120 | {NULL, 0, NULL, 0} |
| 121 | }; |
| 122 | |
| 123 | if (parse_cmdline(cmdline, &argc, &argv)) { |
| 124 | goto cleanup; |
| 125 | } |
| 126 | |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 127 | while ((opt = getopt_long(argc, argv, commands[CMD_VERB].optstring, options, &opt_index)) != -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 128 | switch (opt) { |
| 129 | case 'h': |
| 130 | cmd_verb_help(); |
| 131 | goto cleanup; |
| 132 | default: |
| 133 | YLMSG_E("Unknown option.\n"); |
| 134 | goto cleanup; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (argc - optind > 1) { |
| 139 | YLMSG_E("Only a single verbosity level can be set.\n"); |
| 140 | cmd_verb_help(); |
| 141 | goto cleanup; |
| 142 | } else if (argc == optind) { |
| 143 | /* no argument - print current value */ |
| 144 | LY_LOG_LEVEL level = ly_log_level(LY_LLERR); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 145 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 146 | ly_log_level(level); |
| 147 | printf("Current verbosity level: "); |
| 148 | if (level == LY_LLERR) { |
| 149 | printf("error\n"); |
| 150 | } else if (level == LY_LLWRN) { |
| 151 | printf("warning\n"); |
| 152 | } else if (level == LY_LLVRB) { |
| 153 | printf("verbose\n"); |
| 154 | } else if (level == LY_LLDBG) { |
| 155 | printf("debug\n"); |
| 156 | } |
| 157 | goto cleanup; |
| 158 | } |
| 159 | |
| 160 | if (!strcasecmp("error", argv[optind]) || !strcmp("0", argv[optind])) { |
| 161 | ly_log_level(LY_LLERR); |
| 162 | } else if (!strcasecmp("warning", argv[optind]) || !strcmp("1", argv[optind])) { |
| 163 | ly_log_level(LY_LLWRN); |
| 164 | } else if (!strcasecmp("verbose", argv[optind]) || !strcmp("2", argv[optind])) { |
| 165 | ly_log_level(LY_LLVRB); |
| 166 | } else if (!strcasecmp("debug", argv[optind]) || !strcmp("3", argv[optind])) { |
| 167 | ly_log_level(LY_LLDBG); |
| 168 | } else { |
| 169 | YLMSG_E("Unknown verbosity \"%s\"\n", argv[optind]); |
| 170 | goto cleanup; |
| 171 | } |
| 172 | |
| 173 | cleanup: |
| 174 | free_cmdline(argv); |
| 175 | } |
| 176 | |
| 177 | void |
| 178 | cmd_quit(struct ly_ctx **UNUSED(ctx), const char *UNUSED(cmdline)) |
| 179 | { |
| 180 | done = 1; |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | void |
| 185 | cmd_help_help(void) |
| 186 | { |
| 187 | printf("Usage: help [cmd ...]\n"); |
| 188 | } |
| 189 | |
| 190 | void |
| 191 | cmd_help(struct ly_ctx **UNUSED(ctx), const char *cmdline) |
| 192 | { |
| 193 | int argc = 0; |
| 194 | char **argv = NULL; |
| 195 | int opt, opt_index; |
| 196 | struct option options[] = { |
| 197 | {"help", no_argument, NULL, 'h'}, |
| 198 | {NULL, 0, NULL, 0} |
| 199 | }; |
| 200 | |
| 201 | if (parse_cmdline(cmdline, &argc, &argv)) { |
| 202 | goto cleanup; |
| 203 | } |
| 204 | |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 205 | while ((opt = getopt_long(argc, argv, commands[CMD_HELP].optstring, options, &opt_index)) != -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 206 | switch (opt) { |
| 207 | case 'h': |
| 208 | cmd_help_help(); |
| 209 | goto cleanup; |
| 210 | default: |
| 211 | YLMSG_E("Unknown option.\n"); |
| 212 | goto cleanup; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if (argc == optind) { |
| 217 | generic_help: |
| 218 | printf("Available commands:\n"); |
| 219 | for (uint16_t i = 0; commands[i].name; i++) { |
| 220 | if (commands[i].helpstring != NULL) { |
| 221 | printf(" %-15s %s\n", commands[i].name, commands[i].helpstring); |
| 222 | } |
| 223 | } |
| 224 | } else { |
| 225 | /* print specific help for the selected command(s) */ |
| 226 | |
| 227 | for (int c = 0; c < argc - optind; ++c) { |
| 228 | int8_t match = 0; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 229 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 230 | /* get the command of the specified name */ |
| 231 | for (uint16_t i = 0; commands[i].name; i++) { |
| 232 | if (strcmp(argv[optind + c], commands[i].name) == 0) { |
| 233 | match = 1; |
| 234 | if (commands[i].help_func != NULL) { |
| 235 | commands[i].help_func(); |
| 236 | } else { |
| 237 | printf("%s: %s\n", argv[optind + c], commands[i].helpstring); |
| 238 | } |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | if (!match) { |
| 243 | /* if unknown command specified, print the list of commands */ |
| 244 | printf("Unknown command \'%s\'\n", argv[optind + c]); |
| 245 | goto generic_help; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | cleanup: |
| 251 | free_cmdline(argv); |
| 252 | } |
| 253 | |
aPiecek | 15cc4cf | 2023-04-17 15:23:21 +0200 | [diff] [blame] | 254 | /* Also keep enum COMMAND_INDEX updated. */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 255 | COMMAND commands[] = { |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 256 | {"help", cmd_help, cmd_help_help, NULL, "Display commands description", "h"}, |
| 257 | {"add", cmd_add, cmd_add_help, NULL, "Add a new module from a specific file", "DF:hi"}, |
| 258 | {"load", cmd_load, cmd_load_help, NULL, "Load a new schema from the searchdirs", "F:hi"}, |
| 259 | {"print", cmd_print, cmd_print_help, NULL, "Print a module", "f:hL:o:P:q"}, |
| 260 | {"data", cmd_data, cmd_data_help, NULL, "Load, validate and optionally print instance data", "d:ef:F:hmo:O:R:r:nt:x:"}, |
| 261 | {"list", cmd_list, cmd_list_help, NULL, "List all the loaded modules", "f:h"}, |
| 262 | {"feature", cmd_feature, cmd_feature_help, NULL, "Print all features of module(s) with their state", "haf"}, |
| 263 | {"searchpath", cmd_searchpath, cmd_searchpath_help, NULL, "Print/set the search path(s) for schemas", "ch"}, |
| 264 | {"extdata", cmd_extdata, cmd_extdata_help, cmd_extdata_free, "Set the specific data required by an extension", "ch"}, |
aPiecek | 21c1bc8 | 2023-05-18 15:38:31 +0200 | [diff] [blame] | 265 | {"clear", cmd_clear, cmd_clear_help, NULL, "Clear the context - remove all the loaded modules", "iyhY:"}, |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 266 | {"verb", cmd_verb, cmd_verb_help, NULL, "Change verbosity", "h"}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 267 | #ifndef NDEBUG |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 268 | {"debug", cmd_debug, cmd_debug_help, NULL, "Display specific debug message groups", "h"}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 269 | #endif |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 270 | {"quit", cmd_quit, NULL, NULL, "Quit the program", "h"}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 271 | /* synonyms for previous commands */ |
aPiecek | 647f62e | 2023-05-18 10:55:58 +0200 | [diff] [blame] | 272 | {"?", cmd_help, NULL, NULL, "Display commands description", "h"}, |
| 273 | {"exit", cmd_quit, NULL, NULL, "Quit the program", "h"}, |
| 274 | {NULL, NULL, NULL, NULL, NULL, NULL} |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 275 | }; |