blob: 5c6a46d38e4b764553ca86f093f145b4089ca831 [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
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
86cleanup:
87 free_cmdline(argv);
88}
89
90#endif
91
92void
93cmd_verb_help(void)
94{
95 printf("Usage: verb (error | warning | verbose | debug)\n");
96}
97
98void
99cmd_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
158cleanup:
159 free_cmdline(argv);
160}
161
162void
163cmd_quit(struct ly_ctx **UNUSED(ctx), const char *UNUSED(cmdline))
164{
165 done = 1;
166 return;
167}
168
169void
170cmd_help_help(void)
171{
172 printf("Usage: help [cmd ...]\n");
173}
174
175void
176cmd_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) {
202generic_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
234cleanup:
235 free_cmdline(argv);
236}
237
238COMMAND 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};