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) { |
aPiecek | 94560e1 | 2023-06-08 10:02:06 +0200 | [diff] [blame] | 84 | YLMSG_E("Only a single output can be specified.\n"); |
| 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)) { |
aPiecek | 94560e1 | 2023-06-08 10:02:06 +0200 | [diff] [blame] | 88 | YLMSG_E("Unable open output file %s (%s)\n", 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: |
| 117 | YLMSG_E("Unknown option.\n"); |
| 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) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 133 | YLMSG_E("Missing the name of the module to print.\n"); |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | if ((yo->schema_out_format != LYS_OUT_TREE) && yo->line_length) { |
aPiecek | 04c8c2e | 2023-06-08 08:38:22 +0200 | [diff] [blame] | 138 | YLMSG_W("--tree-line-length take effect only in case of the tree output format.\n"); |
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)) { |
| 143 | YLMSG_E("Could not use stdout to print output.\n"); |
| 144 | } |
| 145 | yo->out_stdout = 1; |
| 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 151 | static LY_ERR |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 152 | 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] | 153 | { |
| 154 | LY_ERR erc; |
| 155 | const struct lysp_submodule *submodule; |
| 156 | |
| 157 | submodule = revision ? |
| 158 | ly_ctx_get_submodule(*ctx, name, revision) : |
| 159 | ly_ctx_get_submodule_latest(*ctx, name); |
| 160 | |
| 161 | erc = submodule ? |
| 162 | lys_print_submodule(out, submodule, format, line_length, options) : |
| 163 | LY_ENOTFOUND; |
| 164 | |
| 165 | return erc; |
| 166 | } |
| 167 | |
| 168 | static LY_ERR |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 169 | 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] | 170 | { |
| 171 | LY_ERR erc; |
| 172 | struct lys_module *module; |
| 173 | |
| 174 | module = revision ? |
| 175 | ly_ctx_get_module(*ctx, name, revision) : |
| 176 | ly_ctx_get_module_latest(*ctx, name); |
| 177 | |
| 178 | erc = module ? |
| 179 | lys_print_module(out, module, format, line_length, options) : |
| 180 | LY_ENOTFOUND; |
| 181 | |
| 182 | return erc; |
| 183 | } |
| 184 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 185 | static int |
| 186 | cmd_print_module(const char *posv, struct ly_out *out, struct ly_ctx **ctx, LYS_OUTFORMAT format, |
| 187 | size_t line_length, uint32_t options) |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 188 | { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 189 | int rc = 0; |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 190 | LY_ERR erc; |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 191 | char *name = NULL, *revision; |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 192 | ly_bool search_submodul; |
| 193 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 194 | name = strdup(posv); |
| 195 | /* get revision */ |
| 196 | revision = strchr(name, '@'); |
| 197 | if (revision) { |
| 198 | revision[0] = '\0'; |
| 199 | ++revision; |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 200 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 201 | |
| 202 | erc = print_module(out, ctx, name, revision, format, line_length, options); |
| 203 | |
| 204 | if (erc == LY_ENOTFOUND) { |
| 205 | search_submodul = 1; |
| 206 | erc = print_submodule(out, ctx, name, revision, format, line_length, options); |
| 207 | } else { |
| 208 | search_submodul = 0; |
| 209 | } |
| 210 | |
| 211 | if (erc == LY_SUCCESS) { |
| 212 | rc = 0; |
| 213 | } else if (erc == LY_ENOTFOUND) { |
| 214 | if (revision) { |
| 215 | YLMSG_E("No (sub)module \"%s\" in revision %s found.\n", name, revision); |
| 216 | } else { |
| 217 | YLMSG_E("No (sub)module \"%s\" found.\n", name); |
| 218 | } |
| 219 | rc = 1; |
| 220 | } else { |
| 221 | if (search_submodul) { |
| 222 | YLMSG_E("Unable to print submodule %s.\n", name); |
| 223 | } else { |
| 224 | YLMSG_E("Unable to print module %s.\n", name); |
| 225 | } |
| 226 | rc = 1; |
| 227 | } |
| 228 | |
| 229 | free(name); |
| 230 | return rc; |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 231 | } |
| 232 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 233 | int |
| 234 | 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] | 235 | { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 236 | int rc = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 237 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 238 | if (yo->schema_out_format == LYS_OUT_TREE) { |
aPiecek | c1603ba | 2021-04-19 13:52:22 +0200 | [diff] [blame] | 239 | /* print tree from lysc_nodes */ |
| 240 | ly_ctx_set_options(*ctx, LY_CTX_SET_PRIV_PARSED); |
| 241 | } |
| 242 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 243 | if (yo->schema_node_path) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 244 | const struct lysc_node *node; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 245 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 246 | node = find_schema_path(*ctx, yo->schema_node_path); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 247 | if (!node) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 248 | YLMSG_E("The requested schema node \"%s\" does not exists.\n", yo->schema_node_path); |
| 249 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 250 | } |
roman | f00089c | 2022-10-06 16:01:31 +0200 | [diff] [blame] | 251 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 252 | if (lys_print_node(yo->out, node, yo->schema_out_format, yo->line_length, yo->schema_print_options)) { |
| 253 | YLMSG_E("Unable to print schema node %s.\n", yo->schema_node_path); |
| 254 | return 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 255 | } |
| 256 | } else { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 257 | rc = cmd_print_module(posv, yo->out, ctx, yo->schema_out_format, yo->line_length, yo->schema_print_options); |
| 258 | if (!yo->last_one) { |
| 259 | /* for YANG Tree Diagrams printing it's more readable to print a blank line between modules. */ |
| 260 | if (yo->schema_out_format == LYS_OUT_TREE) { |
| 261 | ly_print(yo->out, "\n"); |
| 262 | } |
| 263 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 264 | } |
| 265 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 266 | return rc; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 267 | } |