Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file cmd_list.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @author Radek Krejci <rkrejci@cesnet.cz> |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 5 | * @author Adam Piecek <piecek@cesnet.cz> |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 6 | * @brief 'list' command of the libyang's yanglint tool. |
| 7 | * |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 8 | * Copyright (c) 2015-2023 CESNET, z.s.p.o. |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 9 | * |
| 10 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 11 | * You may not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
| 13 | * |
| 14 | * https://opensource.org/licenses/BSD-3-Clause |
| 15 | */ |
| 16 | |
| 17 | #define _GNU_SOURCE |
| 18 | |
| 19 | #include "cmd.h" |
| 20 | |
| 21 | #include <getopt.h> |
| 22 | #include <stdio.h> |
| 23 | #include <strings.h> |
| 24 | |
| 25 | #include "libyang.h" |
| 26 | |
| 27 | #include "common.h" |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 28 | #include "yl_opt.h" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 29 | |
| 30 | void |
| 31 | cmd_list_help(void) |
| 32 | { |
| 33 | printf("Usage: list [-f (xml | json)]\n" |
| 34 | " Print the list of modules in the current context\n\n" |
| 35 | " -f FORMAT, --format=FORMAT\n" |
| 36 | " Print the list as ietf-yang-library data in the specified\n" |
| 37 | " data FORMAT. If format not specified, a simple list is\n" |
| 38 | " printed with an indication of imported (i) / implemented (I)\n" |
| 39 | " modules.\n"); |
| 40 | } |
| 41 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 42 | int |
| 43 | cmd_list_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 44 | { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 45 | int rc = 0, argc = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 46 | int opt, opt_index; |
| 47 | struct option options[] = { |
| 48 | {"format", required_argument, NULL, 'f'}, |
| 49 | {"help", no_argument, NULL, 'h'}, |
| 50 | {NULL, 0, NULL, 0} |
| 51 | }; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 52 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 53 | yo->data_out_format = LYD_UNKNOWN; |
| 54 | |
| 55 | if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) { |
| 56 | return rc; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 57 | } |
| 58 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 59 | while ((opt = getopt_long(argc, yo->argv, commands[CMD_LIST].optstring, options, &opt_index)) != -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 60 | switch (opt) { |
| 61 | case 'f': /* --format */ |
| 62 | if (!strcasecmp(optarg, "xml")) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 63 | yo->data_out_format = LYD_XML; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 64 | } else if (!strcasecmp(optarg, "json")) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 65 | yo->data_out_format = LYD_JSON; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 66 | } else { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 67 | YLMSG_E("Unknown output format %s.", optarg); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 68 | cmd_list_help(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 69 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 70 | } |
| 71 | break; |
| 72 | case 'h': |
| 73 | cmd_list_help(); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 74 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 75 | default: |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 76 | YLMSG_E("Unknown option."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 77 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 81 | *posv = &yo->argv[optind]; |
| 82 | *posc = argc - optind; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 83 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | int |
| 88 | cmd_list_dep(struct yl_opt *yo, int posc) |
| 89 | { |
| 90 | if (posc) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 91 | YLMSG_E("No positional arguments are allowed."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 92 | return 1; |
| 93 | } |
| 94 | if (ly_out_new_file(stdout, &yo->out)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 95 | YLMSG_E("Unable to print to the standard output."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 96 | return 1; |
| 97 | } |
| 98 | yo->out_stdout = 1; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
aPiecek | 8ec2e83 | 2023-06-12 15:35:34 +0200 | [diff] [blame] | 103 | /** |
| 104 | * @brief Print yang library data. |
| 105 | * |
| 106 | * @param[in] ctx Context for libyang. |
| 107 | * @param[in] data_out_format Output format of printed data. |
| 108 | * @param[in] out Output handler. |
| 109 | * @return 0 on success. |
| 110 | */ |
| 111 | static int |
| 112 | print_yang_lib_data(struct ly_ctx *ctx, LYD_FORMAT data_out_format, struct ly_out *out) |
| 113 | { |
| 114 | struct lyd_node *ylib; |
| 115 | |
| 116 | if (ly_ctx_get_yanglib_data(ctx, &ylib, "%u", ly_ctx_get_change_count(ctx))) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 117 | YLMSG_E("Getting context info (ietf-yang-library data) failed. If the YANG module is missing or not implemented, " |
| 118 | "use an option to add it internally."); |
aPiecek | 8ec2e83 | 2023-06-12 15:35:34 +0200 | [diff] [blame] | 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | lyd_print_all(out, ylib, data_out_format, 0); |
| 123 | lyd_free_all(ylib); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 128 | int |
| 129 | cmd_list_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv) |
| 130 | { |
| 131 | (void) posv; |
aPiecek | 8ec2e83 | 2023-06-12 15:35:34 +0200 | [diff] [blame] | 132 | uint32_t idx = 0, has_modules = 0; |
| 133 | const struct lys_module *mod; |
| 134 | |
| 135 | if (yo->data_out_format != LYD_UNKNOWN) { |
| 136 | /* ietf-yang-library data are printed in the specified format */ |
| 137 | if (print_yang_lib_data(*ctx, yo->data_out_format, yo->out)) { |
| 138 | return 1; |
| 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* iterate schemas in context and provide just the basic info */ |
| 144 | ly_print(yo->out, "List of the loaded models:\n"); |
| 145 | while ((mod = ly_ctx_get_module_iter(*ctx, &idx))) { |
| 146 | has_modules++; |
| 147 | |
| 148 | /* conformance print */ |
| 149 | if (mod->implemented) { |
| 150 | ly_print(yo->out, " I"); |
| 151 | } else { |
| 152 | ly_print(yo->out, " i"); |
| 153 | } |
| 154 | |
| 155 | /* module print */ |
| 156 | ly_print(yo->out, " %s", mod->name); |
| 157 | if (mod->revision) { |
| 158 | ly_print(yo->out, "@%s", mod->revision); |
| 159 | } |
| 160 | |
| 161 | /* submodules print */ |
| 162 | if (mod->parsed && mod->parsed->includes) { |
| 163 | uint64_t u = 0; |
| 164 | |
| 165 | ly_print(yo->out, " ("); |
| 166 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
| 167 | ly_print(yo->out, "%s%s", !u ? "" : ",", mod->parsed->includes[u].name); |
| 168 | if (mod->parsed->includes[u].rev[0]) { |
| 169 | ly_print(yo->out, "@%s", mod->parsed->includes[u].rev); |
| 170 | } |
| 171 | } |
| 172 | ly_print(yo->out, ")"); |
| 173 | } |
| 174 | |
| 175 | /* finish the line */ |
| 176 | ly_print(yo->out, "\n"); |
| 177 | } |
| 178 | |
| 179 | if (!has_modules) { |
| 180 | ly_print(yo->out, "\t(none)\n"); |
| 181 | } |
| 182 | |
| 183 | ly_print_flush(yo->out); |
| 184 | |
| 185 | return 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 186 | } |