blob: 3661bfae6f1aca34fb918b317889771a9a8c31d5 [file] [log] [blame]
aPieceka83b8e02023-06-07 15:25:16 +02001/**
2 * @file cmd_debug.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#ifndef NDEBUG
16
17#include "cmd.h"
18
19#include <assert.h>
20#include <getopt.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <strings.h>
24
25#include "libyang.h"
26
27#include "common.h"
28#include "yl_opt.h"
29
aPiecek0cbc9022023-06-08 16:58:56 +020030struct debug_groups {
31 char *name;
32 uint32_t flag;
33} const dg [] = {
34 {"dict", LY_LDGDICT},
35 {"xpath", LY_LDGXPATH},
36 {"dep-sets", LY_LDGDEPSETS},
37};
38#define DG_LENGTH (sizeof dg / sizeof *dg)
39
aPieceka83b8e02023-06-07 15:25:16 +020040void
41cmd_debug_help(void)
42{
aPiecek0cbc9022023-06-08 16:58:56 +020043 uint32_t i;
44
45 printf("Usage: debug (");
46 for (i = 0; i < DG_LENGTH; i++) {
47 if ((i + 1) == DG_LENGTH) {
48 printf("%s", dg[i].name);
49 } else {
50 printf("%s | ", dg[i].name);
51 }
52 }
53 printf(")+\n");
aPieceka83b8e02023-06-07 15:25:16 +020054}
55
56int
57cmd_debug_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
58{
59 int rc = 0, argc = 0;
60 int opt, opt_index;
61 struct option options[] = {
62 {"help", no_argument, NULL, 'h'},
63 {NULL, 0, NULL, 0}
64 };
65
66 if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
67 return rc;
68 }
69
70 while ((opt = getopt_long(argc, yo->argv, commands[CMD_DEBUG].optstring, options, &opt_index)) != -1) {
71 switch (opt) {
72 case 'h':
73 cmd_debug_help();
74 return 1;
75 default:
Michal Vasko1407d7d2023-08-21 09:57:54 +020076 YLMSG_E("Unknown option.");
aPieceka83b8e02023-06-07 15:25:16 +020077 return 1;
78 }
79 }
80
81 *posv = &yo->argv[optind];
82 *posc = argc - optind;
83
84 return 0;
85}
86
87int
88cmd_debug_dep(struct yl_opt *yo, int posc)
89{
90 (void) yo;
91
aPiecek0cbc9022023-06-08 16:58:56 +020092 if (yo->interactive && !posc) {
aPieceka83b8e02023-06-07 15:25:16 +020093 /* no argument */
94 cmd_debug_help();
95 return 1;
96 }
97
98 return 0;
99}
100
101int
aPiecekcf9e7682023-06-19 14:03:50 +0200102cmd_debug_store(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
aPieceka83b8e02023-06-07 15:25:16 +0200103{
104 (void) ctx;
aPiecek0cbc9022023-06-08 16:58:56 +0200105 uint32_t i;
106 ly_bool set;
aPieceka83b8e02023-06-07 15:25:16 +0200107
108 assert(posv);
109
aPiecek0cbc9022023-06-08 16:58:56 +0200110 set = 0;
111 for (i = 0; i < DG_LENGTH; i++) {
112 if (!strcasecmp(posv, dg[i].name)) {
113 yo->dbg_groups |= dg[i].flag;
114 set = 1;
115 break;
116 }
117 }
118
119 if (!set) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200120 YLMSG_E("Unknown debug group \"%s\".", posv);
aPieceka83b8e02023-06-07 15:25:16 +0200121 return 1;
122 }
123
124 return 0;
125}
126
127int
aPiecekcf9e7682023-06-19 14:03:50 +0200128cmd_debug_setlog(struct ly_ctx *ctx, struct yl_opt *yo)
aPieceka83b8e02023-06-07 15:25:16 +0200129{
130 (void) ctx;
131 return ly_log_dbg_groups(yo->dbg_groups);
132}
133
134#endif