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; |
| 96 | |
| 97 | for (int i = 0; i < argc - optind; i++) { |
| 98 | name = argv[optind + i]; |
| 99 | /* get revision */ |
| 100 | revision = strchr(name, '@'); |
| 101 | if (revision) { |
| 102 | revision[0] = '\0'; |
| 103 | ++revision; |
| 104 | } |
| 105 | |
| 106 | erc = cmd_print_module(out, ctx, name, revision, format, line_length, options); |
| 107 | |
| 108 | if (erc == LY_ENOTFOUND) { |
| 109 | search_submodul = 1; |
| 110 | erc = cmd_print_submodule(out, ctx, name, revision, format, line_length, options); |
| 111 | } else { |
| 112 | search_submodul = 0; |
| 113 | } |
| 114 | |
| 115 | if (erc == LY_SUCCESS) { |
| 116 | continue; |
| 117 | } else if (erc == LY_ENOTFOUND) { |
| 118 | if (revision) { |
| 119 | YLMSG_E("No (sub)module \"%s\" in revision %s found.\n", name, revision); |
| 120 | } else { |
| 121 | YLMSG_E("No (sub)module \"%s\" found.\n", name); |
| 122 | } |
| 123 | break; |
| 124 | } else { |
| 125 | if (search_submodul) { |
| 126 | YLMSG_E("Unable to print submodule %s.\n", name); |
| 127 | } else { |
| 128 | YLMSG_E("Unable to print module %s.\n", name); |
| 129 | } |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 135 | void |
| 136 | cmd_print(struct ly_ctx **ctx, const char *cmdline) |
| 137 | { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 138 | int argc = 0; |
| 139 | char **argv = NULL; |
| 140 | int opt, opt_index; |
| 141 | struct option options[] = { |
| 142 | {"format", required_argument, NULL, 'f'}, |
| 143 | {"help", no_argument, NULL, 'h'}, |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 144 | {"tree-line-length", required_argument, NULL, 'L'}, |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 145 | {"output", required_argument, NULL, 'o'}, |
| 146 | {"schema-node", required_argument, NULL, 'P'}, |
| 147 | {"single-node", no_argument, NULL, 'q'}, |
| 148 | {NULL, 0, NULL, 0} |
| 149 | }; |
| 150 | uint16_t options_print = 0; |
| 151 | const char *node_path = NULL; |
| 152 | LYS_OUTFORMAT format = LYS_OUT_TREE; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 153 | struct ly_out *out = NULL; |
Radek Krejci | 4b0f01b | 2021-03-28 17:37:21 +0200 | [diff] [blame] | 154 | ly_bool out_stdout = 0; |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 155 | size_t line_length = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 156 | |
| 157 | if (parse_cmdline(cmdline, &argc, &argv)) { |
| 158 | goto cleanup; |
| 159 | } |
| 160 | |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 161 | 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] | 162 | switch (opt) { |
| 163 | case 'o': /* --output */ |
| 164 | if (out) { |
| 165 | if (ly_out_filepath(out, optarg) != NULL) { |
Radek Krejci | 540ce98 | 2020-11-26 12:01:30 +0100 | [diff] [blame] | 166 | 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] | 167 | goto cleanup; |
| 168 | } |
| 169 | } else { |
| 170 | if (ly_out_new_filepath(optarg, &out)) { |
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 | } |
| 175 | break; |
| 176 | |
| 177 | case 'f': /* --format */ |
| 178 | if (!strcasecmp(optarg, "yang")) { |
| 179 | format = LYS_OUT_YANG; |
| 180 | } else if (!strcasecmp(optarg, "yin")) { |
| 181 | format = LYS_OUT_YIN; |
| 182 | } else if (!strcasecmp(optarg, "info")) { |
| 183 | format = LYS_OUT_YANG_COMPILED; |
| 184 | } else if (!strcasecmp(optarg, "tree")) { |
| 185 | format = LYS_OUT_TREE; |
| 186 | } else { |
| 187 | YLMSG_E("Unknown output format %s\n", optarg); |
| 188 | cmd_print_help(); |
| 189 | goto cleanup; |
| 190 | } |
| 191 | break; |
| 192 | |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 193 | case 'L': /* --tree-line-length */ |
| 194 | line_length = atoi(optarg); |
| 195 | break; |
| 196 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 197 | case 'P': /* --schema-node */ |
| 198 | node_path = optarg; |
| 199 | break; |
| 200 | |
| 201 | case 'q': /* --single-node */ |
| 202 | options_print |= LYS_PRINT_NO_SUBSTMT; |
| 203 | break; |
| 204 | |
| 205 | case 'h': |
| 206 | cmd_print_help(); |
| 207 | goto cleanup; |
| 208 | default: |
| 209 | YLMSG_E("Unknown option.\n"); |
| 210 | goto cleanup; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /* file name */ |
| 215 | if ((argc == optind) && !node_path) { |
| 216 | YLMSG_E("Missing the name of the module to print.\n"); |
| 217 | goto cleanup; |
| 218 | } |
| 219 | |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 220 | if ((format != LYS_OUT_TREE) && line_length) { |
| 221 | YLMSG_E("--tree-line-length take effect only in case of the tree output format.\n"); |
| 222 | goto cleanup; |
| 223 | } |
| 224 | |
Radek Krejci | 540ce98 | 2020-11-26 12:01:30 +0100 | [diff] [blame] | 225 | if (!out) { |
| 226 | if (ly_out_new_file(stdout, &out)) { |
| 227 | YLMSG_E("Could not use stdout to print output.\n"); |
| 228 | goto cleanup; |
| 229 | } |
Radek Krejci | 4b0f01b | 2021-03-28 17:37:21 +0200 | [diff] [blame] | 230 | out_stdout = 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 231 | } |
| 232 | |
aPiecek | c1603ba | 2021-04-19 13:52:22 +0200 | [diff] [blame] | 233 | if (format == LYS_OUT_TREE) { |
| 234 | /* print tree from lysc_nodes */ |
| 235 | ly_ctx_set_options(*ctx, LY_CTX_SET_PRIV_PARSED); |
| 236 | } |
| 237 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 238 | if (node_path) { |
| 239 | const struct lysc_node *node; |
| 240 | node = lys_find_path(*ctx, NULL, node_path, 0); |
| 241 | if (!node) { |
| 242 | node = lys_find_path(*ctx, NULL, node_path, 1); |
| 243 | |
| 244 | if (!node) { |
| 245 | YLMSG_E("The requested schema node \"%s\" does not exists.\n", node_path); |
| 246 | goto cleanup; |
| 247 | } |
| 248 | } |
| 249 | if (lys_print_node(out, node, format, 0, options_print)) { |
| 250 | YLMSG_E("Unable to print schema node %s.\n", node_path); |
| 251 | goto cleanup; |
| 252 | } |
| 253 | } else { |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 254 | cmd_print_modules(argc, argv, out, ctx, format, line_length, options_print); |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame] | 255 | goto cleanup; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | cleanup: |
| 259 | free_cmdline(argv); |
Radek Krejci | 4b0f01b | 2021-03-28 17:37:21 +0200 | [diff] [blame] | 260 | ly_out_free(out, NULL, out_stdout ? 0 : 1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 261 | } |