Skip to content

Commit e9f13b1

Browse files
committed
yanglint REFACTOR reflect changes in libyang 2.0 in yanglint
1 parent 92769a7 commit e9f13b1

19 files changed

Lines changed: 2935 additions & 2592 deletions

tools/lint/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
set(lintsrc
44
main.c
55
main_ni.c
6-
commands.c
6+
cmd.c
7+
cmd_add.c
8+
cmd_clear.c
9+
cmd_data.c
10+
cmd_list.c
11+
cmd_load.c
12+
cmd_print.c
13+
cmd_searchpath.c
14+
common.c
715
completion.c
816
configuration.c
917
linenoise/linenoise.c)

tools/lint/cmd.c

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
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+
{
38+
printf("Usage: debug (dict | xpath)+\n");
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+
57+
while ((opt = getopt_long(argc, argv, "h", options, &opt_index)) != -1) {
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;
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+
86+
cleanup:
87+
free_cmdline(argv);
88+
}
89+
90+
#endif
91+
92+
void
93+
cmd_verb_help(void)
94+
{
95+
printf("Usage: verb (error | warning | verbose | debug)\n");
96+
}
97+
98+
void
99+
cmd_verb(struct ly_ctx **UNUSED(ctx), const char *cmdline)
100+
{
101+
int argc = 0;
102+
char **argv = NULL;
103+
int opt, opt_index;
104+
struct option options[] = {
105+
{"help", no_argument, NULL, 'h'},
106+
{NULL, 0, NULL, 0}
107+
};
108+
109+
if (parse_cmdline(cmdline, &argc, &argv)) {
110+
goto cleanup;
111+
}
112+
113+
while ((opt = getopt_long(argc, argv, "h", options, &opt_index)) != -1) {
114+
switch (opt) {
115+
case 'h':
116+
cmd_verb_help();
117+
goto cleanup;
118+
default:
119+
YLMSG_E("Unknown option.\n");
120+
goto cleanup;
121+
}
122+
}
123+
124+
if (argc - optind > 1) {
125+
YLMSG_E("Only a single verbosity level can be set.\n");
126+
cmd_verb_help();
127+
goto cleanup;
128+
} else if (argc == optind) {
129+
/* no argument - print current value */
130+
LY_LOG_LEVEL level = ly_log_level(LY_LLERR);
131+
ly_log_level(level);
132+
printf("Current verbosity level: ");
133+
if (level == LY_LLERR) {
134+
printf("error\n");
135+
} else if (level == LY_LLWRN) {
136+
printf("warning\n");
137+
} else if (level == LY_LLVRB) {
138+
printf("verbose\n");
139+
} else if (level == LY_LLDBG) {
140+
printf("debug\n");
141+
}
142+
goto cleanup;
143+
}
144+
145+
if (!strcasecmp("error", argv[optind]) || !strcmp("0", argv[optind])) {
146+
ly_log_level(LY_LLERR);
147+
} else if (!strcasecmp("warning", argv[optind]) || !strcmp("1", argv[optind])) {
148+
ly_log_level(LY_LLWRN);
149+
} else if (!strcasecmp("verbose", argv[optind]) || !strcmp("2", argv[optind])) {
150+
ly_log_level(LY_LLVRB);
151+
} else if (!strcasecmp("debug", argv[optind]) || !strcmp("3", argv[optind])) {
152+
ly_log_level(LY_LLDBG);
153+
} else {
154+
YLMSG_E("Unknown verbosity \"%s\"\n", argv[optind]);
155+
goto cleanup;
156+
}
157+
158+
cleanup:
159+
free_cmdline(argv);
160+
}
161+
162+
void
163+
cmd_quit(struct ly_ctx **UNUSED(ctx), const char *UNUSED(cmdline))
164+
{
165+
done = 1;
166+
return;
167+
}
168+
169+
void
170+
cmd_help_help(void)
171+
{
172+
printf("Usage: help [cmd ...]\n");
173+
}
174+
175+
void
176+
cmd_help(struct ly_ctx **UNUSED(ctx), const char *cmdline)
177+
{
178+
int argc = 0;
179+
char **argv = NULL;
180+
int opt, opt_index;
181+
struct option options[] = {
182+
{"help", no_argument, NULL, 'h'},
183+
{NULL, 0, NULL, 0}
184+
};
185+
186+
if (parse_cmdline(cmdline, &argc, &argv)) {
187+
goto cleanup;
188+
}
189+
190+
while ((opt = getopt_long(argc, argv, "h", options, &opt_index)) != -1) {
191+
switch (opt) {
192+
case 'h':
193+
cmd_help_help();
194+
goto cleanup;
195+
default:
196+
YLMSG_E("Unknown option.\n");
197+
goto cleanup;
198+
}
199+
}
200+
201+
if (argc == optind) {
202+
generic_help:
203+
printf("Available commands:\n");
204+
for (uint16_t i = 0; commands[i].name; i++) {
205+
if (commands[i].helpstring != NULL) {
206+
printf(" %-15s %s\n", commands[i].name, commands[i].helpstring);
207+
}
208+
}
209+
} else {
210+
/* print specific help for the selected command(s) */
211+
212+
for (int c = 0; c < argc - optind; ++c) {
213+
int8_t match = 0;
214+
/* get the command of the specified name */
215+
for (uint16_t i = 0; commands[i].name; i++) {
216+
if (strcmp(argv[optind + c], commands[i].name) == 0) {
217+
match = 1;
218+
if (commands[i].help_func != NULL) {
219+
commands[i].help_func();
220+
} else {
221+
printf("%s: %s\n", argv[optind + c], commands[i].helpstring);
222+
}
223+
break;
224+
}
225+
}
226+
if (!match) {
227+
/* if unknown command specified, print the list of commands */
228+
printf("Unknown command \'%s\'\n", argv[optind + c]);
229+
goto generic_help;
230+
}
231+
}
232+
}
233+
234+
cleanup:
235+
free_cmdline(argv);
236+
}
237+
238+
COMMAND commands[] = {
239+
{"help", cmd_help, cmd_help_help, "Display commands description"},
240+
{"add", cmd_add, cmd_add_help, "Add a new module from a specific file"},
241+
{"load", cmd_load, cmd_load_help, "Load a new model from the searchdirs"},
242+
{"print", cmd_print, cmd_print_help, "Print a schema module"},
243+
{"data", cmd_data, cmd_data_help, "Load, validate and optionally print instance data"},
244+
{"list", cmd_list, cmd_list_help, "List all the loaded models"},
245+
{"searchpath", cmd_searchpath, cmd_searchpath_help, "Print/set the search path(s) for models"},
246+
{"clear", cmd_clear, cmd_clear_help, "Clear the context - remove all the loaded models"},
247+
{"verb", cmd_verb, cmd_verb_help, "Change verbosity"},
248+
#ifndef NDEBUG
249+
{"debug", cmd_debug, cmd_debug_help, "Display specific debug message groups"},
250+
#endif
251+
{"quit", cmd_quit, NULL, "Quit the program"},
252+
/* synonyms for previous commands */
253+
{"?", cmd_help, NULL, "Display commands description"},
254+
{"exit", cmd_quit, NULL, "Quit the program"},
255+
{NULL, NULL, NULL, NULL}
256+
};

tools/lint/cmd.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @file cmd.h
3+
* @author Michal Vasko <mvasko@cesnet.cz>
4+
* @author Radek Krejci <rkrejci@cesnet.cz>
5+
* @brief libyang's yanglint tool commands header
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+
#ifndef COMMANDS_H_
17+
#define COMMANDS_H_
18+
19+
#include "libyang.h"
20+
21+
/**
22+
* @brief command information
23+
*/
24+
typedef struct {
25+
char *name; /* User printable name of the function. */
26+
void (*func)(struct ly_ctx **ctx, const char *); /* Function to call to do the command. */
27+
void (*help_func)(void); /* Display command help. */
28+
char *helpstring; /* Documentation for this function. */
29+
} COMMAND;
30+
31+
/**
32+
* @brief The list of available commands.
33+
*/
34+
extern COMMAND commands[];
35+
36+
/* cmd_add.c */
37+
void cmd_add(struct ly_ctx **ctx, const char *cmdline);
38+
void cmd_add_help(void);
39+
40+
/* cmd_clear.c */
41+
void cmd_clear(struct ly_ctx **ctx, const char *cmdline);
42+
void cmd_clear_help(void);
43+
44+
/* cmd_data.c */
45+
void cmd_data(struct ly_ctx **ctx, const char *cmdline);
46+
void cmd_data_help(void);
47+
48+
/* cmd_list.c */
49+
void cmd_list(struct ly_ctx **ctx, const char *cmdline);
50+
void cmd_list_help(void);
51+
52+
/* cmd_load.c */
53+
void cmd_load(struct ly_ctx **ctx, const char *cmdline);
54+
void cmd_load_help(void);
55+
56+
/* cmd_print.c */
57+
void cmd_print(struct ly_ctx **ctx, const char *cmdline);
58+
void cmd_print_help(void);
59+
60+
/* cmd_searchpath.c */
61+
void cmd_searchpath(struct ly_ctx **ctx, const char *cmdline);
62+
void cmd_searchpath_help(void);
63+
64+
#endif /* COMMANDS_H_ */

0 commit comments

Comments
 (0)