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 | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 34 | printf("Usage: print [-f (yang | yin | tree [-q -P PATH] | info [-q -P PATH])] [-o OUTFILE]\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 35 | " [<module-name1>[@revision]] ...\n" |
| 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" |
| 41 | " -P PATH, --schema-node=PATH\n" |
| 42 | " Print only the specified subtree of the schema.\n" |
| 43 | " The PATH is the XPath subset mentioned in documentation as\n" |
| 44 | " the Path format. The option can be combined with --single-node\n" |
| 45 | " option to print information only about the specified node.\n" |
| 46 | " -q, --single-node\n" |
| 47 | " Supplement to the --schema-node option to print information\n" |
| 48 | " only about a single node specified as PATH argument.\n" |
| 49 | " -o OUTFILE, --output=OUTFILE\n" |
| 50 | " Write the output to OUTFILE instead of stdout.\n"); |
| 51 | } |
| 52 | |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame^] | 53 | static LY_ERR |
| 54 | 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) |
| 55 | { |
| 56 | LY_ERR erc; |
| 57 | const struct lysp_submodule *submodule; |
| 58 | |
| 59 | submodule = revision ? |
| 60 | ly_ctx_get_submodule(*ctx, name, revision) : |
| 61 | ly_ctx_get_submodule_latest(*ctx, name); |
| 62 | |
| 63 | erc = submodule ? |
| 64 | lys_print_submodule(out, submodule, format, line_length, options) : |
| 65 | LY_ENOTFOUND; |
| 66 | |
| 67 | return erc; |
| 68 | } |
| 69 | |
| 70 | static LY_ERR |
| 71 | 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) |
| 72 | { |
| 73 | LY_ERR erc; |
| 74 | struct lys_module *module; |
| 75 | |
| 76 | module = revision ? |
| 77 | ly_ctx_get_module(*ctx, name, revision) : |
| 78 | ly_ctx_get_module_latest(*ctx, name); |
| 79 | |
| 80 | erc = module ? |
| 81 | lys_print_module(out, module, format, line_length, options) : |
| 82 | LY_ENOTFOUND; |
| 83 | |
| 84 | return erc; |
| 85 | } |
| 86 | |
| 87 | static void |
| 88 | 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) |
| 89 | { |
| 90 | LY_ERR erc; |
| 91 | char *name, *revision; |
| 92 | ly_bool search_submodul; |
| 93 | |
| 94 | for (int i = 0; i < argc - optind; i++) { |
| 95 | name = argv[optind + i]; |
| 96 | /* get revision */ |
| 97 | revision = strchr(name, '@'); |
| 98 | if (revision) { |
| 99 | revision[0] = '\0'; |
| 100 | ++revision; |
| 101 | } |
| 102 | |
| 103 | erc = cmd_print_module(out, ctx, name, revision, format, line_length, options); |
| 104 | |
| 105 | if (erc == LY_ENOTFOUND) { |
| 106 | search_submodul = 1; |
| 107 | erc = cmd_print_submodule(out, ctx, name, revision, format, line_length, options); |
| 108 | } else { |
| 109 | search_submodul = 0; |
| 110 | } |
| 111 | |
| 112 | if (erc == LY_SUCCESS) { |
| 113 | continue; |
| 114 | } else if (erc == LY_ENOTFOUND) { |
| 115 | if (revision) { |
| 116 | YLMSG_E("No (sub)module \"%s\" in revision %s found.\n", name, revision); |
| 117 | } else { |
| 118 | YLMSG_E("No (sub)module \"%s\" found.\n", name); |
| 119 | } |
| 120 | break; |
| 121 | } else { |
| 122 | if (search_submodul) { |
| 123 | YLMSG_E("Unable to print submodule %s.\n", name); |
| 124 | } else { |
| 125 | YLMSG_E("Unable to print module %s.\n", name); |
| 126 | } |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 132 | void |
| 133 | cmd_print(struct ly_ctx **ctx, const char *cmdline) |
| 134 | { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 135 | int argc = 0; |
| 136 | char **argv = NULL; |
| 137 | int opt, opt_index; |
| 138 | struct option options[] = { |
| 139 | {"format", required_argument, NULL, 'f'}, |
| 140 | {"help", no_argument, NULL, 'h'}, |
| 141 | {"output", required_argument, NULL, 'o'}, |
| 142 | {"schema-node", required_argument, NULL, 'P'}, |
| 143 | {"single-node", no_argument, NULL, 'q'}, |
| 144 | {NULL, 0, NULL, 0} |
| 145 | }; |
| 146 | uint16_t options_print = 0; |
| 147 | const char *node_path = NULL; |
| 148 | LYS_OUTFORMAT format = LYS_OUT_TREE; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 149 | struct ly_out *out = NULL; |
Radek Krejci | 4b0f01b | 2021-03-28 17:37:21 +0200 | [diff] [blame] | 150 | ly_bool out_stdout = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 151 | |
| 152 | if (parse_cmdline(cmdline, &argc, &argv)) { |
| 153 | goto cleanup; |
| 154 | } |
| 155 | |
| 156 | while ((opt = getopt_long(argc, argv, "f:ho:P:q", options, &opt_index)) != -1) { |
| 157 | switch (opt) { |
| 158 | case 'o': /* --output */ |
| 159 | if (out) { |
| 160 | if (ly_out_filepath(out, optarg) != NULL) { |
Radek Krejci | 540ce98 | 2020-11-26 12:01:30 +0100 | [diff] [blame] | 161 | 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] | 162 | goto cleanup; |
| 163 | } |
| 164 | } else { |
| 165 | if (ly_out_new_filepath(optarg, &out)) { |
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 | } |
| 170 | break; |
| 171 | |
| 172 | case 'f': /* --format */ |
| 173 | if (!strcasecmp(optarg, "yang")) { |
| 174 | format = LYS_OUT_YANG; |
| 175 | } else if (!strcasecmp(optarg, "yin")) { |
| 176 | format = LYS_OUT_YIN; |
| 177 | } else if (!strcasecmp(optarg, "info")) { |
| 178 | format = LYS_OUT_YANG_COMPILED; |
| 179 | } else if (!strcasecmp(optarg, "tree")) { |
| 180 | format = LYS_OUT_TREE; |
| 181 | } else { |
| 182 | YLMSG_E("Unknown output format %s\n", optarg); |
| 183 | cmd_print_help(); |
| 184 | goto cleanup; |
| 185 | } |
| 186 | break; |
| 187 | |
| 188 | case 'P': /* --schema-node */ |
| 189 | node_path = optarg; |
| 190 | break; |
| 191 | |
| 192 | case 'q': /* --single-node */ |
| 193 | options_print |= LYS_PRINT_NO_SUBSTMT; |
| 194 | break; |
| 195 | |
| 196 | case 'h': |
| 197 | cmd_print_help(); |
| 198 | goto cleanup; |
| 199 | default: |
| 200 | YLMSG_E("Unknown option.\n"); |
| 201 | goto cleanup; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /* file name */ |
| 206 | if ((argc == optind) && !node_path) { |
| 207 | YLMSG_E("Missing the name of the module to print.\n"); |
| 208 | goto cleanup; |
| 209 | } |
| 210 | |
Radek Krejci | 540ce98 | 2020-11-26 12:01:30 +0100 | [diff] [blame] | 211 | if (!out) { |
| 212 | if (ly_out_new_file(stdout, &out)) { |
| 213 | YLMSG_E("Could not use stdout to print output.\n"); |
| 214 | goto cleanup; |
| 215 | } |
Radek Krejci | 4b0f01b | 2021-03-28 17:37:21 +0200 | [diff] [blame] | 216 | out_stdout = 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 217 | } |
| 218 | |
aPiecek | c1603ba | 2021-04-19 13:52:22 +0200 | [diff] [blame] | 219 | if (format == LYS_OUT_TREE) { |
| 220 | /* print tree from lysc_nodes */ |
| 221 | ly_ctx_set_options(*ctx, LY_CTX_SET_PRIV_PARSED); |
| 222 | } |
| 223 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 224 | if (node_path) { |
| 225 | const struct lysc_node *node; |
| 226 | node = lys_find_path(*ctx, NULL, node_path, 0); |
| 227 | if (!node) { |
| 228 | node = lys_find_path(*ctx, NULL, node_path, 1); |
| 229 | |
| 230 | if (!node) { |
| 231 | YLMSG_E("The requested schema node \"%s\" does not exists.\n", node_path); |
| 232 | goto cleanup; |
| 233 | } |
| 234 | } |
| 235 | if (lys_print_node(out, node, format, 0, options_print)) { |
| 236 | YLMSG_E("Unable to print schema node %s.\n", node_path); |
| 237 | goto cleanup; |
| 238 | } |
| 239 | } else { |
aPiecek | 0bea987 | 2021-04-21 11:13:20 +0200 | [diff] [blame^] | 240 | cmd_print_modules(argc, argv, out, ctx, format, 0, options_print); |
| 241 | goto cleanup; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | cleanup: |
| 245 | free_cmdline(argv); |
Radek Krejci | 4b0f01b | 2021-03-28 17:37:21 +0200 | [diff] [blame] | 246 | ly_out_free(out, NULL, out_stdout ? 0 : 1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 247 | } |