blob: 33734e6654a356923f8824fb5a890353bf289a9d [file] [log] [blame]
Radek Krejcie9f13b12020-11-09 17:42:04 +01001/**
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
30COMMAND commands[];
31extern int done;
32
33#ifndef NDEBUG
34
35void
36cmd_debug_help(void)
37{
38 printf("Usage: debug (dict | xpath)+\n");
39}
40
41void
42cmd_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
aPiecek15cc4cf2023-04-17 15:23:21 +020057 while ((opt = getopt_long(argc, argv, commands[CMD_DEBUG].optstring, options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010058 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;
78 } else {
79 YLMSG_E("Unknown debug group \"%s\"\n", argv[optind + 1]);
80 goto cleanup;
81 }
82 }
83
84 ly_log_dbg_groups(dbg_groups);
85
86cleanup:
87 free_cmdline(argv);
88}
89
90#endif
91
92void
aPiecek647f62e2023-05-18 10:55:58 +020093cmd_free(void)
94{
95 uint16_t i;
96
97 for (i = 0; commands[i].name; i++) {
98 if (commands[i].free_func) {
99 commands[i].free_func();
100 }
101 }
102}
103
104void
Radek Krejcie9f13b12020-11-09 17:42:04 +0100105cmd_verb_help(void)
106{
107 printf("Usage: verb (error | warning | verbose | debug)\n");
108}
109
110void
111cmd_verb(struct ly_ctx **UNUSED(ctx), const char *cmdline)
112{
113 int argc = 0;
114 char **argv = NULL;
115 int opt, opt_index;
116 struct option options[] = {
117 {"help", no_argument, NULL, 'h'},
118 {NULL, 0, NULL, 0}
119 };
120
121 if (parse_cmdline(cmdline, &argc, &argv)) {
122 goto cleanup;
123 }
124
aPiecek15cc4cf2023-04-17 15:23:21 +0200125 while ((opt = getopt_long(argc, argv, commands[CMD_VERB].optstring, options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100126 switch (opt) {
127 case 'h':
128 cmd_verb_help();
129 goto cleanup;
130 default:
131 YLMSG_E("Unknown option.\n");
132 goto cleanup;
133 }
134 }
135
136 if (argc - optind > 1) {
137 YLMSG_E("Only a single verbosity level can be set.\n");
138 cmd_verb_help();
139 goto cleanup;
140 } else if (argc == optind) {
141 /* no argument - print current value */
142 LY_LOG_LEVEL level = ly_log_level(LY_LLERR);
Michal Vasko26bbb272022-08-02 14:54:33 +0200143
Radek Krejcie9f13b12020-11-09 17:42:04 +0100144 ly_log_level(level);
145 printf("Current verbosity level: ");
146 if (level == LY_LLERR) {
147 printf("error\n");
148 } else if (level == LY_LLWRN) {
149 printf("warning\n");
150 } else if (level == LY_LLVRB) {
151 printf("verbose\n");
152 } else if (level == LY_LLDBG) {
153 printf("debug\n");
154 }
155 goto cleanup;
156 }
157
158 if (!strcasecmp("error", argv[optind]) || !strcmp("0", argv[optind])) {
159 ly_log_level(LY_LLERR);
160 } else if (!strcasecmp("warning", argv[optind]) || !strcmp("1", argv[optind])) {
161 ly_log_level(LY_LLWRN);
162 } else if (!strcasecmp("verbose", argv[optind]) || !strcmp("2", argv[optind])) {
163 ly_log_level(LY_LLVRB);
164 } else if (!strcasecmp("debug", argv[optind]) || !strcmp("3", argv[optind])) {
165 ly_log_level(LY_LLDBG);
166 } else {
167 YLMSG_E("Unknown verbosity \"%s\"\n", argv[optind]);
168 goto cleanup;
169 }
170
171cleanup:
172 free_cmdline(argv);
173}
174
175void
176cmd_quit(struct ly_ctx **UNUSED(ctx), const char *UNUSED(cmdline))
177{
178 done = 1;
179 return;
180}
181
182void
183cmd_help_help(void)
184{
185 printf("Usage: help [cmd ...]\n");
186}
187
188void
189cmd_help(struct ly_ctx **UNUSED(ctx), const char *cmdline)
190{
191 int argc = 0;
192 char **argv = NULL;
193 int opt, opt_index;
194 struct option options[] = {
195 {"help", no_argument, NULL, 'h'},
196 {NULL, 0, NULL, 0}
197 };
198
199 if (parse_cmdline(cmdline, &argc, &argv)) {
200 goto cleanup;
201 }
202
aPiecek15cc4cf2023-04-17 15:23:21 +0200203 while ((opt = getopt_long(argc, argv, commands[CMD_HELP].optstring, options, &opt_index)) != -1) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100204 switch (opt) {
205 case 'h':
206 cmd_help_help();
207 goto cleanup;
208 default:
209 YLMSG_E("Unknown option.\n");
210 goto cleanup;
211 }
212 }
213
214 if (argc == optind) {
215generic_help:
216 printf("Available commands:\n");
217 for (uint16_t i = 0; commands[i].name; i++) {
218 if (commands[i].helpstring != NULL) {
219 printf(" %-15s %s\n", commands[i].name, commands[i].helpstring);
220 }
221 }
222 } else {
223 /* print specific help for the selected command(s) */
224
225 for (int c = 0; c < argc - optind; ++c) {
226 int8_t match = 0;
Michal Vasko26bbb272022-08-02 14:54:33 +0200227
Radek Krejcie9f13b12020-11-09 17:42:04 +0100228 /* get the command of the specified name */
229 for (uint16_t i = 0; commands[i].name; i++) {
230 if (strcmp(argv[optind + c], commands[i].name) == 0) {
231 match = 1;
232 if (commands[i].help_func != NULL) {
233 commands[i].help_func();
234 } else {
235 printf("%s: %s\n", argv[optind + c], commands[i].helpstring);
236 }
237 break;
238 }
239 }
240 if (!match) {
241 /* if unknown command specified, print the list of commands */
242 printf("Unknown command \'%s\'\n", argv[optind + c]);
243 goto generic_help;
244 }
245 }
246 }
247
248cleanup:
249 free_cmdline(argv);
250}
251
aPiecek15cc4cf2023-04-17 15:23:21 +0200252/* Also keep enum COMMAND_INDEX updated. */
Radek Krejcie9f13b12020-11-09 17:42:04 +0100253COMMAND commands[] = {
aPiecek647f62e2023-05-18 10:55:58 +0200254 {"help", cmd_help, cmd_help_help, NULL, "Display commands description", "h"},
255 {"add", cmd_add, cmd_add_help, NULL, "Add a new module from a specific file", "DF:hi"},
256 {"load", cmd_load, cmd_load_help, NULL, "Load a new schema from the searchdirs", "F:hi"},
257 {"print", cmd_print, cmd_print_help, NULL, "Print a module", "f:hL:o:P:q"},
258 {"data", cmd_data, cmd_data_help, NULL, "Load, validate and optionally print instance data", "d:ef:F:hmo:O:R:r:nt:x:"},
259 {"list", cmd_list, cmd_list_help, NULL, "List all the loaded modules", "f:h"},
260 {"feature", cmd_feature, cmd_feature_help, NULL, "Print all features of module(s) with their state", "haf"},
261 {"searchpath", cmd_searchpath, cmd_searchpath_help, NULL, "Print/set the search path(s) for schemas", "ch"},
262 {"extdata", cmd_extdata, cmd_extdata_help, cmd_extdata_free, "Set the specific data required by an extension", "ch"},
aPiecek21c1bc82023-05-18 15:38:31 +0200263 {"clear", cmd_clear, cmd_clear_help, NULL, "Clear the context - remove all the loaded modules", "iyhY:"},
aPiecek647f62e2023-05-18 10:55:58 +0200264 {"verb", cmd_verb, cmd_verb_help, NULL, "Change verbosity", "h"},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100265#ifndef NDEBUG
aPiecek647f62e2023-05-18 10:55:58 +0200266 {"debug", cmd_debug, cmd_debug_help, NULL, "Display specific debug message groups", "h"},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100267#endif
aPiecek647f62e2023-05-18 10:55:58 +0200268 {"quit", cmd_quit, NULL, NULL, "Quit the program", "h"},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100269 /* synonyms for previous commands */
aPiecek647f62e2023-05-18 10:55:58 +0200270 {"?", cmd_help, NULL, NULL, "Display commands description", "h"},
271 {"exit", cmd_quit, NULL, NULL, "Quit the program", "h"},
272 {NULL, NULL, NULL, NULL, NULL, NULL}
Radek Krejcie9f13b12020-11-09 17:42:04 +0100273};