blob: 10e744694e682d43f64f2b6ba6821b54c8ed9f3b [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);
Michal Vasko26bbb272022-08-02 14:54:33 +0200131
Radek Krejcie9f13b12020-11-09 17:42:04 +0100132 ly_log_level(level);
133 printf("Current verbosity level: ");
134 if (level == LY_LLERR) {
135 printf("error\n");
136 } else if (level == LY_LLWRN) {
137 printf("warning\n");
138 } else if (level == LY_LLVRB) {
139 printf("verbose\n");
140 } else if (level == LY_LLDBG) {
141 printf("debug\n");
142 }
143 goto cleanup;
144 }
145
146 if (!strcasecmp("error", argv[optind]) || !strcmp("0", argv[optind])) {
147 ly_log_level(LY_LLERR);
148 } else if (!strcasecmp("warning", argv[optind]) || !strcmp("1", argv[optind])) {
149 ly_log_level(LY_LLWRN);
150 } else if (!strcasecmp("verbose", argv[optind]) || !strcmp("2", argv[optind])) {
151 ly_log_level(LY_LLVRB);
152 } else if (!strcasecmp("debug", argv[optind]) || !strcmp("3", argv[optind])) {
153 ly_log_level(LY_LLDBG);
154 } else {
155 YLMSG_E("Unknown verbosity \"%s\"\n", argv[optind]);
156 goto cleanup;
157 }
158
159cleanup:
160 free_cmdline(argv);
161}
162
163void
164cmd_quit(struct ly_ctx **UNUSED(ctx), const char *UNUSED(cmdline))
165{
166 done = 1;
167 return;
168}
169
170void
171cmd_help_help(void)
172{
173 printf("Usage: help [cmd ...]\n");
174}
175
176void
177cmd_help(struct ly_ctx **UNUSED(ctx), const char *cmdline)
178{
179 int argc = 0;
180 char **argv = NULL;
181 int opt, opt_index;
182 struct option options[] = {
183 {"help", no_argument, NULL, 'h'},
184 {NULL, 0, NULL, 0}
185 };
186
187 if (parse_cmdline(cmdline, &argc, &argv)) {
188 goto cleanup;
189 }
190
191 while ((opt = getopt_long(argc, argv, "h", options, &opt_index)) != -1) {
192 switch (opt) {
193 case 'h':
194 cmd_help_help();
195 goto cleanup;
196 default:
197 YLMSG_E("Unknown option.\n");
198 goto cleanup;
199 }
200 }
201
202 if (argc == optind) {
203generic_help:
204 printf("Available commands:\n");
205 for (uint16_t i = 0; commands[i].name; i++) {
206 if (commands[i].helpstring != NULL) {
207 printf(" %-15s %s\n", commands[i].name, commands[i].helpstring);
208 }
209 }
210 } else {
211 /* print specific help for the selected command(s) */
212
213 for (int c = 0; c < argc - optind; ++c) {
214 int8_t match = 0;
Michal Vasko26bbb272022-08-02 14:54:33 +0200215
Radek Krejcie9f13b12020-11-09 17:42:04 +0100216 /* get the command of the specified name */
217 for (uint16_t i = 0; commands[i].name; i++) {
218 if (strcmp(argv[optind + c], commands[i].name) == 0) {
219 match = 1;
220 if (commands[i].help_func != NULL) {
221 commands[i].help_func();
222 } else {
223 printf("%s: %s\n", argv[optind + c], commands[i].helpstring);
224 }
225 break;
226 }
227 }
228 if (!match) {
229 /* if unknown command specified, print the list of commands */
230 printf("Unknown command \'%s\'\n", argv[optind + c]);
231 goto generic_help;
232 }
233 }
234 }
235
236cleanup:
237 free_cmdline(argv);
238}
239
240COMMAND commands[] = {
241 {"help", cmd_help, cmd_help_help, "Display commands description"},
242 {"add", cmd_add, cmd_add_help, "Add a new module from a specific file"},
Michal Vasko538be422021-10-19 14:06:59 +0200243 {"load", cmd_load, cmd_load_help, "Load a new schema from the searchdirs"},
244 {"print", cmd_print, cmd_print_help, "Print a module"},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100245 {"data", cmd_data, cmd_data_help, "Load, validate and optionally print instance data"},
Michal Vasko538be422021-10-19 14:06:59 +0200246 {"list", cmd_list, cmd_list_help, "List all the loaded modules"},
247 {"feature", cmd_feature, cmd_feature_help, "Print all features of module(s) with their state"},
248 {"searchpath", cmd_searchpath, cmd_searchpath_help, "Print/set the search path(s) for schemas"},
249 {"clear", cmd_clear, cmd_clear_help, "Clear the context - remove all the loaded modules"},
Radek Krejcie9f13b12020-11-09 17:42:04 +0100250 {"verb", cmd_verb, cmd_verb_help, "Change verbosity"},
251#ifndef NDEBUG
252 {"debug", cmd_debug, cmd_debug_help, "Display specific debug message groups"},
253#endif
254 {"quit", cmd_quit, NULL, "Quit the program"},
255 /* synonyms for previous commands */
256 {"?", cmd_help, NULL, "Display commands description"},
257 {"exit", cmd_quit, NULL, "Quit the program"},
258 {NULL, NULL, NULL, NULL}
259};