blob: 33c8d1ed9bdd23ee209df789433876e6b668a681 [file] [log] [blame]
aPieceka83b8e02023-06-07 15:25:16 +02001/**
2 * @file cmd_verb.c
3 * @author Adam Piecek <piecek@cesnet.cz>
4 * @brief 'verb' command of the libyang's yanglint tool.
5 *
6 * Copyright (c) 2023-2023 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "cmd.h"
16
17#include <getopt.h>
18#include <stdint.h>
19#include <stdio.h>
20#include <strings.h>
21
22#include "libyang.h"
23
24#include "common.h"
25#include "yl_opt.h"
26
27void
28cmd_verb_help(void)
29{
30 printf("Usage: verb (error | warning | verbose | debug)\n");
31}
32
33int
34cmd_verb_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
35{
36 int rc = 0, argc = 0;
37 int opt, opt_index;
38 struct option options[] = {
39 {"help", no_argument, NULL, 'h'},
40 {NULL, 0, NULL, 0}
41 };
42
43 if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
44 return rc;
45 }
46
47 while ((opt = getopt_long(argc, yo->argv, commands[CMD_VERB].optstring, options, &opt_index)) != -1) {
48 if (opt == 'h') {
49 cmd_verb_help();
50 return 1;
51 } else {
Michal Vasko1407d7d2023-08-21 09:57:54 +020052 YLMSG_E("Unknown option.");
aPieceka83b8e02023-06-07 15:25:16 +020053 return 1;
54 }
55 }
56
57 *posv = &yo->argv[optind];
58 *posc = argc - optind;
59
60 return 0;
61}
62
63int
64cmd_verb_dep(struct yl_opt *yo, int posc)
65{
66 (void) yo;
67
68 if (posc > 1) {
Michal Vasko1407d7d2023-08-21 09:57:54 +020069 YLMSG_E("Only a single verbosity level can be set.");
aPieceka83b8e02023-06-07 15:25:16 +020070 cmd_verb_help();
71 return 1;
72 }
73
74 return 0;
75}
76
77int
78cmd_verb_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
79{
80 (void) ctx, (void) yo;
81
82 if (!posv) {
83 /* no argument - print current value */
84 LY_LOG_LEVEL level = ly_log_level(LY_LLERR);
85
86 ly_log_level(level);
87 printf("Current verbosity level: ");
88 if (level == LY_LLERR) {
89 printf("error\n");
90 } else if (level == LY_LLWRN) {
91 printf("warning\n");
92 } else if (level == LY_LLVRB) {
93 printf("verbose\n");
94 } else if (level == LY_LLDBG) {
95 printf("debug\n");
96 }
97 return 0;
98 } else {
99 if (!strcasecmp("error", posv) || !strcmp("0", posv)) {
100 ly_log_level(LY_LLERR);
101 } else if (!strcasecmp("warning", posv) || !strcmp("1", posv)) {
102 ly_log_level(LY_LLWRN);
103 } else if (!strcasecmp("verbose", posv) || !strcmp("2", posv)) {
104 ly_log_level(LY_LLVRB);
105 } else if (!strcasecmp("debug", posv) || !strcmp("3", posv)) {
106 ly_log_level(LY_LLDBG);
107 } else {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200108 YLMSG_E("Unknown verbosity \"%s\".", posv);
aPieceka83b8e02023-06-07 15:25:16 +0200109 return 1;
110 }
111 }
112
113 return 0;
114}