blob: a1a89897e765c61b2730331daf944f16db63f387 [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
aPieceka83b8e02023-06-07 15:25:16 +020015#include "cmd.h"
16
17#include <assert.h>
18#include <getopt.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <strings.h>
22
23#include "libyang.h"
24
25#include "common.h"
26#include "yl_opt.h"
27
aPiecek0cbc9022023-06-08 16:58:56 +020028struct debug_groups {
29 char *name;
30 uint32_t flag;
31} const dg [] = {
32 {"dict", LY_LDGDICT},
33 {"xpath", LY_LDGXPATH},
34 {"dep-sets", LY_LDGDEPSETS},
35};
36#define DG_LENGTH (sizeof dg / sizeof *dg)
37
aPieceka83b8e02023-06-07 15:25:16 +020038void
39cmd_debug_help(void)
40{
aPiecek0cbc9022023-06-08 16:58:56 +020041 uint32_t i;
42
43 printf("Usage: debug (");
44 for (i = 0; i < DG_LENGTH; i++) {
45 if ((i + 1) == DG_LENGTH) {
46 printf("%s", dg[i].name);
47 } else {
48 printf("%s | ", dg[i].name);
49 }
50 }
51 printf(")+\n");
aPieceka83b8e02023-06-07 15:25:16 +020052}
53
54int
55cmd_debug_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc)
56{
57 int rc = 0, argc = 0;
58 int opt, opt_index;
59 struct option options[] = {
60 {"help", no_argument, NULL, 'h'},
61 {NULL, 0, NULL, 0}
62 };
63
64 if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) {
65 return rc;
66 }
67
68 while ((opt = getopt_long(argc, yo->argv, commands[CMD_DEBUG].optstring, options, &opt_index)) != -1) {
69 switch (opt) {
70 case 'h':
71 cmd_debug_help();
72 return 1;
73 default:
Michal Vasko1407d7d2023-08-21 09:57:54 +020074 YLMSG_E("Unknown option.");
aPieceka83b8e02023-06-07 15:25:16 +020075 return 1;
76 }
77 }
78
79 *posv = &yo->argv[optind];
80 *posc = argc - optind;
81
82 return 0;
83}
84
85int
86cmd_debug_dep(struct yl_opt *yo, int posc)
87{
88 (void) yo;
89
aPiecek0cbc9022023-06-08 16:58:56 +020090 if (yo->interactive && !posc) {
aPieceka83b8e02023-06-07 15:25:16 +020091 /* no argument */
92 cmd_debug_help();
93 return 1;
94 }
95
96 return 0;
97}
98
99int
aPiecekcf9e7682023-06-19 14:03:50 +0200100cmd_debug_store(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv)
aPieceka83b8e02023-06-07 15:25:16 +0200101{
102 (void) ctx;
aPiecek0cbc9022023-06-08 16:58:56 +0200103 uint32_t i;
104 ly_bool set;
aPieceka83b8e02023-06-07 15:25:16 +0200105
106 assert(posv);
107
aPiecek0cbc9022023-06-08 16:58:56 +0200108 set = 0;
109 for (i = 0; i < DG_LENGTH; i++) {
110 if (!strcasecmp(posv, dg[i].name)) {
111 yo->dbg_groups |= dg[i].flag;
112 set = 1;
113 break;
114 }
115 }
116
117 if (!set) {
Michal Vasko1407d7d2023-08-21 09:57:54 +0200118 YLMSG_E("Unknown debug group \"%s\".", posv);
aPieceka83b8e02023-06-07 15:25:16 +0200119 return 1;
120 }
121
122 return 0;
123}
124
125int
aPiecekcf9e7682023-06-19 14:03:50 +0200126cmd_debug_setlog(struct ly_ctx *ctx, struct yl_opt *yo)
aPieceka83b8e02023-06-07 15:25:16 +0200127{
128 (void) ctx;
129 return ly_log_dbg_groups(yo->dbg_groups);
130}