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