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 | { |
| 34 | printf("Usage: print [-f (yang | yin | tree | info [-q -P PATH])] [-o OUTFILE]\n" |
| 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 | |
| 53 | void |
| 54 | cmd_print(struct ly_ctx **ctx, const char *cmdline) |
| 55 | { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 56 | int argc = 0; |
| 57 | char **argv = NULL; |
| 58 | int opt, opt_index; |
| 59 | struct option options[] = { |
| 60 | {"format", required_argument, NULL, 'f'}, |
| 61 | {"help", no_argument, NULL, 'h'}, |
| 62 | {"output", required_argument, NULL, 'o'}, |
| 63 | {"schema-node", required_argument, NULL, 'P'}, |
| 64 | {"single-node", no_argument, NULL, 'q'}, |
| 65 | {NULL, 0, NULL, 0} |
| 66 | }; |
| 67 | uint16_t options_print = 0; |
| 68 | const char *node_path = NULL; |
| 69 | LYS_OUTFORMAT format = LYS_OUT_TREE; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 70 | struct ly_out *out = NULL; |
Radek Krejci | 4b0f01b | 2021-03-28 17:37:21 +0200 | [diff] [blame^] | 71 | ly_bool out_stdout = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 72 | |
| 73 | if (parse_cmdline(cmdline, &argc, &argv)) { |
| 74 | goto cleanup; |
| 75 | } |
| 76 | |
| 77 | while ((opt = getopt_long(argc, argv, "f:ho:P:q", options, &opt_index)) != -1) { |
| 78 | switch (opt) { |
| 79 | case 'o': /* --output */ |
| 80 | if (out) { |
| 81 | if (ly_out_filepath(out, optarg) != NULL) { |
Radek Krejci | 540ce98 | 2020-11-26 12:01:30 +0100 | [diff] [blame] | 82 | 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] | 83 | goto cleanup; |
| 84 | } |
| 85 | } else { |
| 86 | if (ly_out_new_filepath(optarg, &out)) { |
Radek Krejci | 540ce98 | 2020-11-26 12:01:30 +0100 | [diff] [blame] | 87 | 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] | 88 | goto cleanup; |
| 89 | } |
| 90 | } |
| 91 | break; |
| 92 | |
| 93 | case 'f': /* --format */ |
| 94 | if (!strcasecmp(optarg, "yang")) { |
| 95 | format = LYS_OUT_YANG; |
| 96 | } else if (!strcasecmp(optarg, "yin")) { |
| 97 | format = LYS_OUT_YIN; |
| 98 | } else if (!strcasecmp(optarg, "info")) { |
| 99 | format = LYS_OUT_YANG_COMPILED; |
| 100 | } else if (!strcasecmp(optarg, "tree")) { |
| 101 | format = LYS_OUT_TREE; |
| 102 | } else { |
| 103 | YLMSG_E("Unknown output format %s\n", optarg); |
| 104 | cmd_print_help(); |
| 105 | goto cleanup; |
| 106 | } |
| 107 | break; |
| 108 | |
| 109 | case 'P': /* --schema-node */ |
| 110 | node_path = optarg; |
| 111 | break; |
| 112 | |
| 113 | case 'q': /* --single-node */ |
| 114 | options_print |= LYS_PRINT_NO_SUBSTMT; |
| 115 | break; |
| 116 | |
| 117 | case 'h': |
| 118 | cmd_print_help(); |
| 119 | goto cleanup; |
| 120 | default: |
| 121 | YLMSG_E("Unknown option.\n"); |
| 122 | goto cleanup; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* file name */ |
| 127 | if ((argc == optind) && !node_path) { |
| 128 | YLMSG_E("Missing the name of the module to print.\n"); |
| 129 | goto cleanup; |
| 130 | } |
| 131 | |
Radek Krejci | 540ce98 | 2020-11-26 12:01:30 +0100 | [diff] [blame] | 132 | if (!out) { |
| 133 | if (ly_out_new_file(stdout, &out)) { |
| 134 | YLMSG_E("Could not use stdout to print output.\n"); |
| 135 | goto cleanup; |
| 136 | } |
Radek Krejci | 4b0f01b | 2021-03-28 17:37:21 +0200 | [diff] [blame^] | 137 | out_stdout = 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | if (node_path) { |
| 141 | const struct lysc_node *node; |
| 142 | node = lys_find_path(*ctx, NULL, node_path, 0); |
| 143 | if (!node) { |
| 144 | node = lys_find_path(*ctx, NULL, node_path, 1); |
| 145 | |
| 146 | if (!node) { |
| 147 | YLMSG_E("The requested schema node \"%s\" does not exists.\n", node_path); |
| 148 | goto cleanup; |
| 149 | } |
| 150 | } |
| 151 | if (lys_print_node(out, node, format, 0, options_print)) { |
| 152 | YLMSG_E("Unable to print schema node %s.\n", node_path); |
| 153 | goto cleanup; |
| 154 | } |
| 155 | } else { |
| 156 | for (int i = 0; i < argc - optind; i++) { |
| 157 | struct lys_module *module; |
| 158 | char *revision; |
| 159 | |
| 160 | /* get revision */ |
| 161 | revision = strchr(argv[optind + i], '@'); |
| 162 | if (revision) { |
| 163 | revision[0] = '\0'; |
| 164 | ++revision; |
| 165 | } |
| 166 | |
| 167 | if (revision) { |
| 168 | module = ly_ctx_get_module(*ctx, argv[optind + i], revision); |
| 169 | } else { |
| 170 | module = ly_ctx_get_module_latest(*ctx, argv[optind + i]); |
| 171 | } |
| 172 | if (!module) { |
| 173 | /* TODO try to find it as a submodule */ |
| 174 | if (revision) { |
| 175 | YLMSG_E("No (sub)module \"%s\" in revision %s found.\n", argv[optind + i], revision); |
| 176 | } else { |
| 177 | YLMSG_E("No (sub)module \"%s\" found.\n", argv[optind + i]); |
| 178 | } |
| 179 | goto cleanup; |
| 180 | } |
| 181 | if (lys_print_module(out, module, format, 0, options_print)) { |
| 182 | YLMSG_E("Unable to print module %s.\n", argv[optind + i]); |
| 183 | goto cleanup; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | cleanup: |
| 189 | free_cmdline(argv); |
Radek Krejci | 4b0f01b | 2021-03-28 17:37:21 +0200 | [diff] [blame^] | 190 | ly_out_free(out, NULL, out_stdout ? 0 : 1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 191 | } |