aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 1 | /** |
| 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 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 15 | #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 | |
aPiecek | 0cbc902 | 2023-06-08 16:58:56 +0200 | [diff] [blame] | 28 | struct 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 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 38 | void |
| 39 | cmd_debug_help(void) |
| 40 | { |
aPiecek | 0cbc902 | 2023-06-08 16:58:56 +0200 | [diff] [blame] | 41 | 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"); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | int |
| 55 | cmd_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 Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 74 | YLMSG_E("Unknown option."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 75 | return 1; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | *posv = &yo->argv[optind]; |
| 80 | *posc = argc - optind; |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | int |
| 86 | cmd_debug_dep(struct yl_opt *yo, int posc) |
| 87 | { |
| 88 | (void) yo; |
| 89 | |
aPiecek | 0cbc902 | 2023-06-08 16:58:56 +0200 | [diff] [blame] | 90 | if (yo->interactive && !posc) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 91 | /* no argument */ |
| 92 | cmd_debug_help(); |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | int |
aPiecek | cf9e768 | 2023-06-19 14:03:50 +0200 | [diff] [blame] | 100 | cmd_debug_store(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv) |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 101 | { |
| 102 | (void) ctx; |
aPiecek | 0cbc902 | 2023-06-08 16:58:56 +0200 | [diff] [blame] | 103 | uint32_t i; |
| 104 | ly_bool set; |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 105 | |
| 106 | assert(posv); |
| 107 | |
aPiecek | 0cbc902 | 2023-06-08 16:58:56 +0200 | [diff] [blame] | 108 | 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 Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 118 | YLMSG_E("Unknown debug group \"%s\".", posv); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | int |
aPiecek | cf9e768 | 2023-06-19 14:03:50 +0200 | [diff] [blame] | 126 | cmd_debug_setlog(struct ly_ctx *ctx, struct yl_opt *yo) |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 127 | { |
| 128 | (void) ctx; |
| 129 | return ly_log_dbg_groups(yo->dbg_groups); |
| 130 | } |