Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file cmd_print.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 'print' 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 <errno.h> |
| 22 | #include <getopt.h> |
| 23 | #include <stdint.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <strings.h> |
| 27 | |
| 28 | #include "libyang.h" |
| 29 | |
| 30 | #include "common.h" |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 31 | #include "yl_opt.h" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 32 | |
| 33 | void |
| 34 | cmd_print_help(void) |
| 35 | { |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 36 | printf("Usage: print [-f (yang | yin | tree [-q -P PATH -L LINE_LENGTH ] | info [-q -P PATH])]\n" |
| 37 | " [-o OUTFILE] [<module-name1>[@revision]] ...\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 38 | " Print a schema module. The <module-name> is not required\n" |
aPiecek | dbe35d2 | 2023-03-31 10:33:15 +0200 | [diff] [blame] | 39 | " only in case the -P option is specified. For yang, yin and tree\n" |
| 40 | " formats, a submodule can also be printed.\n\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 41 | " -f FORMAT, --format=FORMAT\n" |
| 42 | " Print the module in the specified FORMAT. If format not\n" |
| 43 | " specified, the 'tree' format is used.\n" |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 44 | " -L LINE_LENGTH, --tree-line-length=LINE_LENGTH\n" |
| 45 | " The limit of the maximum line length on which the 'tree'\n" |
| 46 | " format will try to be printed.\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 47 | " -P PATH, --schema-node=PATH\n" |
| 48 | " Print only the specified subtree of the schema.\n" |
| 49 | " The PATH is the XPath subset mentioned in documentation as\n" |
| 50 | " the Path format. The option can be combined with --single-node\n" |
| 51 | " option to print information only about the specified node.\n" |
| 52 | " -q, --single-node\n" |
| 53 | " Supplement to the --schema-node option to print information\n" |
| 54 | " only about a single node specified as PATH argument.\n" |
| 55 | " -o OUTFILE, --output=OUTFILE\n" |
| 56 | " Write the output to OUTFILE instead of stdout.\n"); |
| 57 | } |
| 58 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 59 | int |
| 60 | cmd_print_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc) |
| 61 | { |
| 62 | int rc = 0, argc = 0; |
| 63 | int opt, opt_index; |
| 64 | struct option options[] = { |
| 65 | {"format", required_argument, NULL, 'f'}, |
| 66 | {"help", no_argument, NULL, 'h'}, |
| 67 | {"tree-line-length", required_argument, NULL, 'L'}, |
| 68 | {"output", required_argument, NULL, 'o'}, |
| 69 | {"schema-node", required_argument, NULL, 'P'}, |
| 70 | {"single-node", no_argument, NULL, 'q'}, |
| 71 | {NULL, 0, NULL, 0} |
| 72 | }; |
| 73 | |
| 74 | yo->schema_out_format = LYS_OUT_TREE; |
| 75 | |
| 76 | if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) { |
| 77 | return rc; |
| 78 | } |
| 79 | |
| 80 | while ((opt = getopt_long(argc, yo->argv, commands[CMD_PRINT].optstring, options, &opt_index)) != -1) { |
| 81 | switch (opt) { |
| 82 | case 'o': /* --output */ |
| 83 | if (yo->out) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 84 | YLMSG_E("Only a single output can be specified."); |
aPiecek | 94560e1 | 2023-06-08 10:02:06 +0200 | [diff] [blame] | 85 | return 1; |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 86 | } else { |
| 87 | if (ly_out_new_filepath(optarg, &yo->out)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 88 | YLMSG_E("Unable open output file %s (%s).", optarg, strerror(errno)); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 89 | return 1; |
| 90 | } |
| 91 | } |
| 92 | break; |
| 93 | |
| 94 | case 'f': /* --format */ |
aPiecek | 113e0f0 | 2023-06-09 08:47:48 +0200 | [diff] [blame] | 95 | if (yl_opt_update_schema_out_format(optarg, yo)) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 96 | cmd_print_help(); |
| 97 | return 1; |
| 98 | } |
| 99 | break; |
| 100 | |
| 101 | case 'L': /* --tree-line-length */ |
| 102 | yo->line_length = atoi(optarg); |
| 103 | break; |
| 104 | |
| 105 | case 'P': /* --schema-node */ |
| 106 | yo->schema_node_path = optarg; |
| 107 | break; |
| 108 | |
| 109 | case 'q': /* --single-node */ |
| 110 | yo->schema_print_options |= LYS_PRINT_NO_SUBSTMT; |
| 111 | break; |
| 112 | |
| 113 | case 'h': |
| 114 | cmd_print_help(); |
| 115 | return 1; |
| 116 | default: |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 117 | YLMSG_E("Unknown option."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 118 | return 1; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | *posv = &yo->argv[optind]; |
| 123 | *posc = argc - optind; |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int |
| 129 | cmd_print_dep(struct yl_opt *yo, int posc) |
| 130 | { |
| 131 | /* file name */ |
aPiecek | 04c8c2e | 2023-06-08 08:38:22 +0200 | [diff] [blame] | 132 | if (yo->interactive && !posc && !yo->schema_node_path) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 133 | YLMSG_E("Missing the name of the module to print."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | if ((yo->schema_out_format != LYS_OUT_TREE) && yo->line_length) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 138 | YLMSG_W("--tree-line-length take effect only in case of the tree output format."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | if (!yo->out) { |
| 142 | if (ly_out_new_file(stdout, &yo->out)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 143 | YLMSG_E("Could not use stdout to print output."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 144 | } |
| 145 | yo->out_stdout = 1; |
| 146 | } |
| 147 | |
aPiecek | 816107f | 2023-06-15 15:31:53 +0200 | [diff] [blame] | 148 | if (yo->schema_out_format == LYS_OUT_TREE) { |
| 149 | /* print tree from lysc_nodes */ |
| 150 | yo->ctx_options |= LY_CTX_SET_PRIV_PARSED; |
| 151 | } |
| 152 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 156 | static LY_ERR |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 157 | print_submodule(struct ly_out *out, struct ly_ctx **ctx, char *name, char *revision, LYS_OUTFORMAT format, size_t line_length, uint32_t options) |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 158 | { |
| 159 | LY_ERR erc; |
| 160 | const struct lysp_submodule *submodule; |
| 161 | |
| 162 | submodule = revision ? |
| 163 | ly_ctx_get_submodule(*ctx, name, revision) : |
| 164 | ly_ctx_get_submodule_latest(*ctx, name); |
| 165 | |
| 166 | erc = submodule ? |
| 167 | lys_print_submodule(out, submodule, format, line_length, options) : |
| 168 | LY_ENOTFOUND; |
| 169 | |
aPiecek | 816107f | 2023-06-15 15:31:53 +0200 | [diff] [blame] | 170 | if (!erc) { |
| 171 | return 0; |
| 172 | } else if ((erc == LY_ENOTFOUND) && revision) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 173 | YLMSG_E("No submodule \"%s\" found.", name); |
aPiecek | 816107f | 2023-06-15 15:31:53 +0200 | [diff] [blame] | 174 | } else { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 175 | YLMSG_E("Unable to print submodule %s.", name); |
aPiecek | 816107f | 2023-06-15 15:31:53 +0200 | [diff] [blame] | 176 | } |
| 177 | |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 178 | return erc; |
| 179 | } |
| 180 | |
| 181 | static LY_ERR |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 182 | print_module(struct ly_out *out, struct ly_ctx **ctx, char *name, char *revision, LYS_OUTFORMAT format, size_t line_length, uint32_t options) |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 183 | { |
| 184 | LY_ERR erc; |
| 185 | struct lys_module *module; |
| 186 | |
| 187 | module = revision ? |
| 188 | ly_ctx_get_module(*ctx, name, revision) : |
| 189 | ly_ctx_get_module_latest(*ctx, name); |
| 190 | |
| 191 | erc = module ? |
| 192 | lys_print_module(out, module, format, line_length, options) : |
| 193 | LY_ENOTFOUND; |
| 194 | |
aPiecek | 816107f | 2023-06-15 15:31:53 +0200 | [diff] [blame] | 195 | if (!erc) { |
| 196 | return 0; |
| 197 | } else if ((erc == LY_ENOTFOUND) && revision) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 198 | YLMSG_E("No module \"%s\" found.", name); |
aPiecek | 816107f | 2023-06-15 15:31:53 +0200 | [diff] [blame] | 199 | } else { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 200 | YLMSG_E("Unable to print module %s.", name); |
aPiecek | 816107f | 2023-06-15 15:31:53 +0200 | [diff] [blame] | 201 | } |
| 202 | |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 203 | return erc; |
| 204 | } |
| 205 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 206 | static int |
| 207 | cmd_print_module(const char *posv, struct ly_out *out, struct ly_ctx **ctx, LYS_OUTFORMAT format, |
| 208 | size_t line_length, uint32_t options) |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 209 | { |
| 210 | LY_ERR erc; |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 211 | char *name = NULL, *revision; |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 212 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 213 | name = strdup(posv); |
| 214 | /* get revision */ |
| 215 | revision = strchr(name, '@'); |
| 216 | if (revision) { |
| 217 | revision[0] = '\0'; |
| 218 | ++revision; |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 219 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 220 | |
| 221 | erc = print_module(out, ctx, name, revision, format, line_length, options); |
| 222 | |
| 223 | if (erc == LY_ENOTFOUND) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 224 | erc = print_submodule(out, ctx, name, revision, format, line_length, options); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | free(name); |
aPiecek | 816107f | 2023-06-15 15:31:53 +0200 | [diff] [blame] | 228 | return erc; |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 229 | } |
| 230 | |
aPiecek | e73292e | 2023-06-15 13:25:35 +0200 | [diff] [blame] | 231 | /** |
| 232 | * @brief Print schema node path. |
| 233 | * |
| 234 | * @param[in] ctx Context for libyang. |
| 235 | * @param[in] yo Context for yanglint. |
| 236 | * @return 0 on success. |
| 237 | */ |
| 238 | static int |
| 239 | print_node(struct ly_ctx *ctx, struct yl_opt *yo) |
| 240 | { |
| 241 | const struct lysc_node *node; |
| 242 | uint32_t temp_lo = 0; |
| 243 | |
| 244 | if (yo->interactive) { |
| 245 | /* Use the same approach as for completion. */ |
| 246 | node = find_schema_path(ctx, yo->schema_node_path); |
| 247 | if (!node) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 248 | YLMSG_E("The requested schema node \"%s\" does not exists.", yo->schema_node_path); |
aPiecek | e73292e | 2023-06-15 13:25:35 +0200 | [diff] [blame] | 249 | return 1; |
| 250 | } |
| 251 | } else { |
| 252 | /* turn off logging so that the message is not repeated */ |
| 253 | ly_temp_log_options(&temp_lo); |
| 254 | /* search operation input */ |
| 255 | node = lys_find_path(ctx, NULL, yo->schema_node_path, 0); |
| 256 | if (!node) { |
| 257 | /* restore logging so an error may be displayed */ |
| 258 | ly_temp_log_options(NULL); |
| 259 | /* search operation output */ |
| 260 | node = lys_find_path(ctx, NULL, yo->schema_node_path, 1); |
| 261 | if (!node) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 262 | YLMSG_E("Invalid schema path."); |
aPiecek | e73292e | 2023-06-15 13:25:35 +0200 | [diff] [blame] | 263 | return 1; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (lys_print_node(yo->out, node, yo->schema_out_format, yo->line_length, yo->schema_print_options)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 269 | YLMSG_E("Unable to print schema node %s.", yo->schema_node_path); |
aPiecek | e73292e | 2023-06-15 13:25:35 +0200 | [diff] [blame] | 270 | return 1; |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 276 | int |
| 277 | cmd_print_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 278 | { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 279 | int rc = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 280 | |
aPiecek | 816107f | 2023-06-15 15:31:53 +0200 | [diff] [blame] | 281 | if (yo->ctx_options & LY_CTX_SET_PRIV_PARSED) { |
aPiecek | c1603ba | 2021-04-19 13:52:22 +0200 | [diff] [blame] | 282 | /* print tree from lysc_nodes */ |
| 283 | ly_ctx_set_options(*ctx, LY_CTX_SET_PRIV_PARSED); |
| 284 | } |
| 285 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 286 | if (yo->schema_node_path) { |
aPiecek | e73292e | 2023-06-15 13:25:35 +0200 | [diff] [blame] | 287 | rc = print_node(*ctx, yo); |
aPiecek | 816107f | 2023-06-15 15:31:53 +0200 | [diff] [blame] | 288 | } else if (!yo->interactive && yo->submodule) { |
| 289 | rc = print_submodule(yo->out, ctx, yo->submodule, NULL, yo->schema_out_format, yo->line_length, |
| 290 | yo->schema_print_options); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 291 | } else { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 292 | rc = cmd_print_module(posv, yo->out, ctx, yo->schema_out_format, yo->line_length, yo->schema_print_options); |
aPiecek | e73292e | 2023-06-15 13:25:35 +0200 | [diff] [blame] | 293 | if (!yo->last_one && (yo->schema_out_format == LYS_OUT_TREE)) { |
| 294 | ly_print(yo->out, "\n"); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 295 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 296 | } |
| 297 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 298 | return rc; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 299 | } |