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> |
| 5 | * @brief 'print' command of the libyang's yanglint tool. |
| 6 | * |
| 7 | * Copyright (c) 2015-2020 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #define _GNU_SOURCE |
| 17 | |
| 18 | #include "cmd.h" |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <getopt.h> |
| 22 | #include <stdint.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <strings.h> |
| 26 | |
| 27 | #include "libyang.h" |
| 28 | |
| 29 | #include "common.h" |
| 30 | |
| 31 | void |
| 32 | cmd_print_help(void) |
| 33 | { |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 34 | printf("Usage: print [-f (yang | yin | tree [-q -P PATH -L LINE_LENGTH ] | info [-q -P PATH])]\n" |
| 35 | " [-o OUTFILE] [<module-name1>[@revision]] ...\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 36 | " Print a schema module. The <module-name> is not required\n" |
| 37 | " only in case the -P option is specified.\n\n" |
| 38 | " -f FORMAT, --format=FORMAT\n" |
| 39 | " Print the module in the specified FORMAT. If format not\n" |
| 40 | " specified, the 'tree' format is used.\n" |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 41 | " -L LINE_LENGTH, --tree-line-length=LINE_LENGTH\n" |
| 42 | " The limit of the maximum line length on which the 'tree'\n" |
| 43 | " format will try to be printed.\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 44 | " -P PATH, --schema-node=PATH\n" |
| 45 | " Print only the specified subtree of the schema.\n" |
| 46 | " The PATH is the XPath subset mentioned in documentation as\n" |
| 47 | " the Path format. The option can be combined with --single-node\n" |
| 48 | " option to print information only about the specified node.\n" |
| 49 | " -q, --single-node\n" |
| 50 | " Supplement to the --schema-node option to print information\n" |
| 51 | " only about a single node specified as PATH argument.\n" |
| 52 | " -o OUTFILE, --output=OUTFILE\n" |
| 53 | " Write the output to OUTFILE instead of stdout.\n"); |
| 54 | } |
| 55 | |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 56 | static LY_ERR |
| 57 | cmd_print_submodule(struct ly_out *out, struct ly_ctx **ctx, char *name, char *revision, LYS_OUTFORMAT format, size_t line_length, uint32_t options) |
| 58 | { |
| 59 | LY_ERR erc; |
| 60 | const struct lysp_submodule *submodule; |
| 61 | |
| 62 | submodule = revision ? |
| 63 | ly_ctx_get_submodule(*ctx, name, revision) : |
| 64 | ly_ctx_get_submodule_latest(*ctx, name); |
| 65 | |
| 66 | erc = submodule ? |
| 67 | lys_print_submodule(out, submodule, format, line_length, options) : |
| 68 | LY_ENOTFOUND; |
| 69 | |
| 70 | return erc; |
| 71 | } |
| 72 | |
| 73 | static LY_ERR |
| 74 | cmd_print_module(struct ly_out *out, struct ly_ctx **ctx, char *name, char *revision, LYS_OUTFORMAT format, size_t line_length, uint32_t options) |
| 75 | { |
| 76 | LY_ERR erc; |
| 77 | struct lys_module *module; |
| 78 | |
| 79 | module = revision ? |
| 80 | ly_ctx_get_module(*ctx, name, revision) : |
| 81 | ly_ctx_get_module_latest(*ctx, name); |
| 82 | |
| 83 | erc = module ? |
| 84 | lys_print_module(out, module, format, line_length, options) : |
| 85 | LY_ENOTFOUND; |
| 86 | |
| 87 | return erc; |
| 88 | } |
| 89 | |
| 90 | static void |
| 91 | cmd_print_modules(int argc, char **argv, struct ly_out *out, struct ly_ctx **ctx, LYS_OUTFORMAT format, size_t line_length, uint32_t options) |
| 92 | { |
| 93 | LY_ERR erc; |
| 94 | char *name, *revision; |
| 95 | ly_bool search_submodul; |
aPiecek | 8f0b788 | 2021-05-21 10:18:36 +0200 | [diff] [blame] | 96 | const int stop = argc - optind; |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 97 | |
aPiecek | 8f0b788 | 2021-05-21 10:18:36 +0200 | [diff] [blame] | 98 | for (int i = 0; i < stop; i++) { |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 99 | name = argv[optind + i]; |
| 100 | /* get revision */ |
| 101 | revision = strchr(name, '@'); |
| 102 | if (revision) { |
| 103 | revision[0] = '\0'; |
| 104 | ++revision; |
| 105 | } |
| 106 | |
| 107 | erc = cmd_print_module(out, ctx, name, revision, format, line_length, options); |
| 108 | |
| 109 | if (erc == LY_ENOTFOUND) { |
| 110 | search_submodul = 1; |
| 111 | erc = cmd_print_submodule(out, ctx, name, revision, format, line_length, options); |
| 112 | } else { |
| 113 | search_submodul = 0; |
| 114 | } |
| 115 | |
| 116 | if (erc == LY_SUCCESS) { |
aPiecek | 8f0b788 | 2021-05-21 10:18:36 +0200 | [diff] [blame] | 117 | /* for YANG Tree Diagrams printing it's more readable to print a blank line between modules. */ |
| 118 | if ((format == LYS_OUT_TREE) && (i + 1 < stop)) { |
| 119 | ly_print(out, "\n"); |
| 120 | } |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 121 | continue; |
| 122 | } else if (erc == LY_ENOTFOUND) { |
| 123 | if (revision) { |
| 124 | YLMSG_E("No (sub)module \"%s\" in revision %s found.\n", name, revision); |
| 125 | } else { |
| 126 | YLMSG_E("No (sub)module \"%s\" found.\n", name); |
| 127 | } |
| 128 | break; |
| 129 | } else { |
| 130 | if (search_submodul) { |
| 131 | YLMSG_E("Unable to print submodule %s.\n", name); |
| 132 | } else { |
| 133 | YLMSG_E("Unable to print module %s.\n", name); |
| 134 | } |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 140 | void |
| 141 | cmd_print(struct ly_ctx **ctx, const char *cmdline) |
| 142 | { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 143 | int argc = 0; |
| 144 | char **argv = NULL; |
| 145 | int opt, opt_index; |
| 146 | struct option options[] = { |
| 147 | {"format", required_argument, NULL, 'f'}, |
| 148 | {"help", no_argument, NULL, 'h'}, |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 149 | {"tree-line-length", required_argument, NULL, 'L'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 150 | {"output", required_argument, NULL, 'o'}, |
| 151 | {"schema-node", required_argument, NULL, 'P'}, |
| 152 | {"single-node", no_argument, NULL, 'q'}, |
| 153 | {NULL, 0, NULL, 0} |
| 154 | }; |
| 155 | uint16_t options_print = 0; |
| 156 | const char *node_path = NULL; |
| 157 | LYS_OUTFORMAT format = LYS_OUT_TREE; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 158 | struct ly_out *out = NULL; |
Radek Krejci | 4b0f01b | 2021-03-28 17:37:21 +0200 | [diff] [blame] | 159 | ly_bool out_stdout = 0; |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 160 | size_t line_length = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 161 | |
| 162 | if (parse_cmdline(cmdline, &argc, &argv)) { |
| 163 | goto cleanup; |
| 164 | } |
| 165 | |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 166 | while ((opt = getopt_long(argc, argv, "f:hL:o:P:q", options, &opt_index)) != -1) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 167 | switch (opt) { |
| 168 | case 'o': /* --output */ |
| 169 | if (out) { |
| 170 | if (ly_out_filepath(out, optarg) != NULL) { |
Radek Krejci | 540ce98 | 2020-11-26 12:01:30 +0100 | [diff] [blame] | 171 | YLMSG_E("Unable to use output file %s for printing output.\n", optarg); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 172 | goto cleanup; |
| 173 | } |
| 174 | } else { |
| 175 | if (ly_out_new_filepath(optarg, &out)) { |
Radek Krejci | 540ce98 | 2020-11-26 12:01:30 +0100 | [diff] [blame] | 176 | YLMSG_E("Unable to use output file %s for printing output.\n", optarg); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 177 | goto cleanup; |
| 178 | } |
| 179 | } |
| 180 | break; |
| 181 | |
| 182 | case 'f': /* --format */ |
| 183 | if (!strcasecmp(optarg, "yang")) { |
| 184 | format = LYS_OUT_YANG; |
| 185 | } else if (!strcasecmp(optarg, "yin")) { |
| 186 | format = LYS_OUT_YIN; |
| 187 | } else if (!strcasecmp(optarg, "info")) { |
| 188 | format = LYS_OUT_YANG_COMPILED; |
| 189 | } else if (!strcasecmp(optarg, "tree")) { |
| 190 | format = LYS_OUT_TREE; |
| 191 | } else { |
| 192 | YLMSG_E("Unknown output format %s\n", optarg); |
| 193 | cmd_print_help(); |
| 194 | goto cleanup; |
| 195 | } |
| 196 | break; |
| 197 | |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 198 | case 'L': /* --tree-line-length */ |
| 199 | line_length = atoi(optarg); |
| 200 | break; |
| 201 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 202 | case 'P': /* --schema-node */ |
| 203 | node_path = optarg; |
| 204 | break; |
| 205 | |
| 206 | case 'q': /* --single-node */ |
| 207 | options_print |= LYS_PRINT_NO_SUBSTMT; |
| 208 | break; |
| 209 | |
| 210 | case 'h': |
| 211 | cmd_print_help(); |
| 212 | goto cleanup; |
| 213 | default: |
| 214 | YLMSG_E("Unknown option.\n"); |
| 215 | goto cleanup; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* file name */ |
| 220 | if ((argc == optind) && !node_path) { |
| 221 | YLMSG_E("Missing the name of the module to print.\n"); |
| 222 | goto cleanup; |
| 223 | } |
| 224 | |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 225 | if ((format != LYS_OUT_TREE) && line_length) { |
| 226 | YLMSG_E("--tree-line-length take effect only in case of the tree output format.\n"); |
| 227 | goto cleanup; |
| 228 | } |
| 229 | |
Radek Krejci | 540ce98 | 2020-11-26 12:01:30 +0100 | [diff] [blame] | 230 | if (!out) { |
| 231 | if (ly_out_new_file(stdout, &out)) { |
| 232 | YLMSG_E("Could not use stdout to print output.\n"); |
| 233 | goto cleanup; |
| 234 | } |
Radek Krejci | 4b0f01b | 2021-03-28 17:37:21 +0200 | [diff] [blame] | 235 | out_stdout = 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 236 | } |
| 237 | |
aPiecek | c1603ba | 2021-04-19 13:52:22 +0200 | [diff] [blame] | 238 | if (format == LYS_OUT_TREE) { |
| 239 | /* print tree from lysc_nodes */ |
| 240 | ly_ctx_set_options(*ctx, LY_CTX_SET_PRIV_PARSED); |
| 241 | } |
| 242 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 243 | if (node_path) { |
| 244 | const struct lysc_node *node; |
| 245 | node = lys_find_path(*ctx, NULL, node_path, 0); |
| 246 | if (!node) { |
| 247 | node = lys_find_path(*ctx, NULL, node_path, 1); |
| 248 | |
| 249 | if (!node) { |
| 250 | YLMSG_E("The requested schema node \"%s\" does not exists.\n", node_path); |
| 251 | goto cleanup; |
| 252 | } |
| 253 | } |
| 254 | if (lys_print_node(out, node, format, 0, options_print)) { |
| 255 | YLMSG_E("Unable to print schema node %s.\n", node_path); |
| 256 | goto cleanup; |
| 257 | } |
| 258 | } else { |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 259 | cmd_print_modules(argc, argv, out, ctx, format, line_length, options_print); |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 260 | goto cleanup; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | cleanup: |
| 264 | free_cmdline(argv); |
Radek Krejci | 4b0f01b | 2021-03-28 17:37:21 +0200 | [diff] [blame] | 265 | ly_out_free(out, NULL, out_stdout ? 0 : 1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 266 | } |