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