Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file main_ni.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 5 | * @author Adam Piecek <piecek@cesnet.cz> |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 6 | * @brief libyang's yanglint tool - non-interactive code |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 7 | * |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 8 | * Copyright (c) 2020 - 2023 CESNET, z.s.p.o. |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [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 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 17 | #define _GNU_SOURCE |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 18 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <getopt.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 21 | #include <stdint.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 24 | #include <string.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 25 | #include <strings.h> |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 26 | #include <sys/stat.h> |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 27 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 28 | #include "libyang.h" |
| 29 | |
aPiecek | bc39de1 | 2023-06-08 08:10:29 +0200 | [diff] [blame] | 30 | #include "cmd.h" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 31 | #include "common.h" |
aPiecek | 8f0b788 | 2021-05-21 10:18:36 +0200 | [diff] [blame] | 32 | #include "out.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 33 | #include "tools/config.h" |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 34 | #include "yl_opt.h" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 35 | |
| 36 | static void |
| 37 | version(void) |
| 38 | { |
| 39 | printf("yanglint %s\n", PROJECT_VERSION); |
| 40 | } |
| 41 | |
| 42 | static void |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 43 | help(int shortout) |
| 44 | { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 45 | |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 46 | printf("Example usage:\n" |
| 47 | " yanglint [-f { yang | yin | info}] <schema>...\n" |
| 48 | " Validates the YANG module <schema>(s) and all its dependencies, optionally printing\n" |
| 49 | " them in the specified format.\n\n" |
| 50 | " yanglint [-f { xml | json }] <schema>... <file>...\n" |
| 51 | " Validates the YANG modeled data <file>(s) according to the <schema>(s) optionally\n" |
| 52 | " printing them in the specified format.\n\n" |
| 53 | " yanglint -t (nc-)rpc/notif [-O <operational-file>] <schema>... <file>\n" |
| 54 | " Validates the YANG/NETCONF RPC/notification <file> according to the <schema>(s) using\n" |
aPiecek | a40764b | 2023-04-27 15:34:56 +0200 | [diff] [blame] | 55 | " <operational-file> with possible references to the operational datastore data.\n" |
| 56 | " To validate nested-notification or action, the <operational-file> is required.\n\n" |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 57 | " yanglint -t nc-reply -R <rpc-file> [-O <operational-file>] <schema>... <file>\n" |
| 58 | " Validates the NETCONF rpc-reply <file> of RPC <rpc-file> according to the <schema>(s)\n" |
| 59 | " using <operational-file> with possible references to the operational datastore data.\n\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 60 | " yanglint\n" |
| 61 | " Starts interactive mode with more features.\n\n"); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 62 | |
| 63 | if (shortout) { |
| 64 | return; |
| 65 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 66 | printf("Options:\n" |
| 67 | " -h, --help Show this help message and exit.\n" |
| 68 | " -v, --version Show version number and exit.\n" |
Michal Vasko | b0d307b | 2021-11-25 11:15:16 +0100 | [diff] [blame] | 69 | " -V, --verbose Increase libyang verbosity and show verbose messages. If specified\n" |
| 70 | " a second time, show even debug messages.\n" |
| 71 | " -Q, --quiet Decrease libyang verbosity and hide warnings. If specified a second\n" |
| 72 | " time, hide errors so no libyang messages are printed.\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 73 | |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 74 | printf(" -f FORMAT, --format=FORMAT\n" |
| 75 | " Convert input into FORMAT. Supported formats: \n" |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 76 | " yang, yin, tree, info and feature-param for schemas,\n" |
Michal Vasko | d8b0e77 | 2022-03-18 13:36:04 +0100 | [diff] [blame] | 77 | " xml, json, and lyb for data.\n\n"); |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 78 | |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 79 | printf(" -I FORMAT, --in-format=FORMAT\n" |
| 80 | " Load the data in one of the following formats:\n" |
| 81 | " xml, json, lyb\n" |
| 82 | " If input format not specified, it is detected from the file extension.\n\n"); |
| 83 | |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 84 | printf(" -p PATH, --path=PATH\n" |
| 85 | " Search path for schema (YANG/YIN) modules. The option can be\n" |
| 86 | " used multiple times. The current working directory and the\n" |
aPiecek | 4f306da | 2023-03-27 09:06:07 +0200 | [diff] [blame] | 87 | " path of the module being added is used implicitly. Subdirectories\n" |
| 88 | " are also searched\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 89 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 90 | printf(" -D, --disable-searchdir\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 91 | " Do not implicitly search in current working directory for\n" |
| 92 | " schema modules. If specified a second time, do not even\n" |
| 93 | " search in the module directory (all modules must be \n" |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 94 | " explicitly specified).\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 95 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 96 | printf(" -F FEATURES, --features=FEATURES\n" |
Michal Vasko | 5974087 | 2021-11-22 10:01:34 +0100 | [diff] [blame] | 97 | " Specific module features to support in the form <module-name>:(<feature>,)*\n" |
| 98 | " Use <feature> '*' to enable all features of a module. This option can be\n" |
| 99 | " specified multiple times, to enable features in multiple modules. If this\n" |
| 100 | " option is not specified, all the features in all the implemented modules\n" |
| 101 | " are enabled.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 102 | |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 103 | printf(" -i, --make-implemented\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 104 | " Make the imported modules \"referenced\" from any loaded\n" |
| 105 | " module also implemented. If specified a second time, all the\n" |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 106 | " modules are set implemented.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 107 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 108 | printf(" -P PATH, --schema-node=PATH\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 109 | " Print only the specified subtree of the schema.\n" |
| 110 | " The PATH is the XPath subset mentioned in documentation as\n" |
| 111 | " the Path format. The option can be combined with --single-node\n" |
| 112 | " option to print information only about the specified node.\n" |
| 113 | " -q, --single-node\n" |
| 114 | " Supplement to the --schema-node option to print information\n" |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 115 | " only about a single node specified as PATH argument.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 116 | |
Michal Vasko | fe74d1e | 2021-09-30 13:17:36 +0200 | [diff] [blame] | 117 | printf(" -s SUBMODULE, --submodule=SUBMODULE\n" |
| 118 | " Print the specific submodule instead of the main module.\n\n"); |
| 119 | |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 120 | printf(" -x FILE, --ext-data=FILE\n" |
| 121 | " File containing the specific data required by an extension. Required by\n" |
Michal Vasko | 1466280 | 2022-11-08 08:45:46 +0100 | [diff] [blame] | 122 | " the schema-mount extension, for example, when the operational data are\n" |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 123 | " expected in the file. File format is guessed.\n\n"); |
| 124 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 125 | printf(" -n, --not-strict\n" |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 126 | " Do not require strict data parsing (silently skip unknown data),\n" |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 127 | " has no effect for schemas.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 128 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 129 | printf(" -e, --present Validate only with the schema modules whose data actually\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 130 | " exist in the provided input data files. Takes effect only\n" |
| 131 | " with the 'data' or 'config' TYPEs. Used to avoid requiring\n" |
| 132 | " mandatory nodes from modules which data are not present in the\n" |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 133 | " provided input data files.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 134 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 135 | printf(" -t TYPE, --type=TYPE\n" |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 136 | " Specify data tree type in the input data file(s):\n" |
| 137 | " data - Complete datastore with status data (default type).\n" |
| 138 | " config - Configuration datastore (without status data).\n" |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 139 | " get - Data returned by the NETCONF <get> operation.\n" |
| 140 | " getconfig - Data returned by the NETCONF <get-config> operation.\n" |
| 141 | " edit - Config content of the NETCONF <edit-config> operation.\n" |
| 142 | " rpc - Invocation of a YANG RPC/action, defined as input.\n" |
| 143 | " nc-rpc - Similar to 'rpc' but expect and check also the NETCONF\n" |
| 144 | " envelopes <rpc> or <action>.\n" |
| 145 | " reply - Reply to a YANG RPC/action, defined as output. Note that\n" |
| 146 | " the reply data are expected inside a container representing\n" |
| 147 | " the original RPC/action invocation.\n" |
| 148 | " nc-reply - Similar to 'reply' but expect and check also the NETCONF\n" |
| 149 | " envelope <rpc-reply> with output data nodes as direct\n" |
| 150 | " descendants. The original RPC/action invocation is expected\n" |
| 151 | " in a separate parameter '-R' and is parsed as 'nc-rpc'.\n" |
| 152 | " notif - Notification instance of a YANG notification.\n" |
| 153 | " nc-notif - Similar to 'notif' but expect and check also the NETCONF\n" |
| 154 | " envelope <notification> with element <eventTime> and its\n" |
| 155 | " sibling as the actual notification.\n\n"); |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 156 | |
| 157 | printf(" -d MODE, --default=MODE\n" |
| 158 | " Print data with default values, according to the MODE\n" |
| 159 | " (to print attributes, ietf-netconf-with-defaults model\n" |
| 160 | " must be loaded):\n" |
| 161 | " all - Add missing default nodes.\n" |
| 162 | " all-tagged - Add missing default nodes and mark all the default\n" |
| 163 | " nodes with the attribute.\n" |
| 164 | " trim - Remove all nodes with a default value.\n" |
| 165 | " implicit-tagged - Add missing nodes and mark them with the attribute.\n\n"); |
| 166 | |
aPiecek | 1b62976 | 2023-04-12 13:52:30 +0200 | [diff] [blame] | 167 | printf(" -E XPATH, --data-xpath=XPATH\n" |
aPiecek | 4195527 | 2023-05-10 16:10:17 +0200 | [diff] [blame] | 168 | " Evaluate XPATH expression over the data and print the nodes satisfying\n" |
aPiecek | 1b62976 | 2023-04-12 13:52:30 +0200 | [diff] [blame] | 169 | " the expression. The output format is specific and the option cannot\n" |
| 170 | " be combined with the -f and -d options. Also all the data\n" |
| 171 | " inputs are merged into a single data tree where the expression\n" |
| 172 | " is evaluated, so the -m option is always set implicitly.\n\n"); |
| 173 | |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 174 | printf(" -l, --list Print info about the loaded schemas.\n" |
| 175 | " (i - imported module, I - implemented module)\n" |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 176 | " In case the '-f' option with data encoding is specified,\n" |
| 177 | " the list is printed as \"ietf-yang-library\" data.\n\n"); |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 178 | |
| 179 | printf(" -L LINE_LENGTH, --tree-line-length=LINE_LENGTH\n" |
| 180 | " The limit of the maximum line length on which the 'tree'\n" |
| 181 | " format will try to be printed.\n\n"); |
| 182 | |
| 183 | printf(" -o OUTFILE, --output=OUTFILE\n" |
| 184 | " Write the output to OUTFILE instead of stdout.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 185 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 186 | printf(" -O FILE, --operational=FILE\n" |
Michal Vasko | 9e81ce5 | 2022-04-29 10:16:21 +0200 | [diff] [blame] | 187 | " Provide optional data to extend validation of the '(nc-)rpc',\n" |
| 188 | " '(nc-)reply' or '(nc-)notif' TYPEs. The FILE is supposed to contain\n" |
| 189 | " the operational datastore referenced from the operation.\n" |
aPiecek | a40764b | 2023-04-27 15:34:56 +0200 | [diff] [blame] | 190 | " In case of a nested notification or action, its parent existence\n" |
| 191 | " is also checked in these operational data.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 192 | |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 193 | printf(" -R FILE, --reply-rpc=FILE\n" |
| 194 | " Provide source RPC for parsing of the 'nc-reply' TYPE. The FILE\n" |
| 195 | " is supposed to contain the source 'nc-rpc' operation of the reply.\n\n"); |
| 196 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 197 | printf(" -m, --merge Merge input data files into a single tree and validate at\n" |
Michal Vasko | 06158fb | 2022-03-29 12:13:16 +0200 | [diff] [blame] | 198 | " once. The option has effect only for 'data' and 'config' TYPEs.\n\n"); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 199 | |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 200 | printf(" -y, --yang-library\n" |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 201 | " Load and implement internal \"ietf-yang-library\" YANG module.\n" |
| 202 | " Note that this module includes definitions of mandatory state\n" |
Michal Vasko | 22e0f3a | 2021-09-22 12:19:23 +0200 | [diff] [blame] | 203 | " data that can result in unexpected data validation errors.\n\n"); |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 204 | |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 205 | printf(" -Y FILE, --yang-library-file=FILE\n" |
| 206 | " Parse FILE with \"ietf-yang-library\" data and use them to\n" |
| 207 | " create an exact YANG schema context. If specified, the '-F'\n" |
| 208 | " parameter (enabled features) is ignored.\n\n"); |
| 209 | |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 210 | printf(" -X, --extended-leafref\n" |
| 211 | " Allow usage of deref() XPath function within leafref\n\n"); |
| 212 | |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 213 | #ifndef NDEBUG |
| 214 | printf(" -G GROUPS, --debug=GROUPS\n" |
| 215 | " Enable printing of specific debugging message group\n" |
| 216 | " (nothing will be printed unless verbosity is set to debug):\n" |
Michal Vasko | 27a4acb | 2021-11-22 10:03:46 +0100 | [diff] [blame] | 217 | " <group>[,<group>]* (dict, xpath, dep-sets)\n\n"); |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 218 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 221 | static void |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 222 | libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 223 | { |
| 224 | char *levstr; |
| 225 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 226 | switch (level) { |
| 227 | case LY_LLERR: |
| 228 | levstr = "err :"; |
| 229 | break; |
| 230 | case LY_LLWRN: |
| 231 | levstr = "warn:"; |
| 232 | break; |
| 233 | case LY_LLVRB: |
| 234 | levstr = "verb:"; |
| 235 | break; |
| 236 | default: |
| 237 | levstr = "dbg :"; |
| 238 | break; |
| 239 | } |
| 240 | if (path) { |
| 241 | fprintf(stderr, "libyang %s %s (%s)\n", levstr, msg, path); |
| 242 | } else { |
| 243 | fprintf(stderr, "libyang %s %s\n", levstr, msg); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
Michal Vasko | 686d8fc | 2021-11-22 10:03:23 +0100 | [diff] [blame] | 247 | static struct schema_features * |
| 248 | get_features_not_applied(const struct ly_set *fset) |
| 249 | { |
| 250 | for (uint32_t u = 0; u < fset->count; ++u) { |
| 251 | struct schema_features *sf = fset->objs[u]; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 252 | |
Michal Vasko | 686d8fc | 2021-11-22 10:03:23 +0100 | [diff] [blame] | 253 | if (!sf->applied) { |
| 254 | return sf; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return NULL; |
| 259 | } |
| 260 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 261 | static int |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 262 | fill_context_inputs(int argc, char *argv[], struct yl_opt *yo, struct ly_ctx **ctx) |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 263 | { |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 264 | struct ly_in *in = NULL; |
Michal Vasko | 686d8fc | 2021-11-22 10:03:23 +0100 | [diff] [blame] | 265 | struct schema_features *sf; |
Michal Vasko | c91fb12 | 2021-11-23 09:00:17 +0100 | [diff] [blame] | 266 | struct lys_module *mod; |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 267 | const char *all_features[] = {"*", NULL}; |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 268 | char *dir = NULL, *module = NULL, *filepath = NULL; |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 269 | |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 270 | /* Create libyang context. */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 271 | if (yo->yang_lib_file) { |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 272 | /* ignore features */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 273 | ly_set_erase(&yo->schema_features, free_features); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 274 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 275 | if (ly_ctx_new_ylpath(yo->searchpaths, yo->yang_lib_file, LYD_UNKNOWN, yo->ctx_options, ctx)) { |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 276 | YLMSG_E("Unable to modify libyang context with yang-library data.\n"); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 277 | return -1; |
| 278 | } |
| 279 | } else { |
| 280 | /* set imp feature flag if all should be enabled */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 281 | yo->ctx_options |= !yo->schema_features.count ? LY_CTX_ENABLE_IMP_FEATURES : 0; |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 282 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 283 | if (ly_ctx_new(yo->searchpaths, yo->ctx_options, ctx)) { |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 284 | YLMSG_E("Unable to create libyang context\n"); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 285 | return -1; |
| 286 | } |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 287 | } |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 288 | |
aPiecek | 912f3d5 | 2022-10-12 10:02:33 +0200 | [diff] [blame] | 289 | /* set callback providing run-time extension instance data */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 290 | if (yo->schema_context_filename) { |
| 291 | ly_ctx_set_ext_data_clb(*ctx, ext_data_clb, yo->schema_context_filename); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 292 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 293 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 294 | for (int i = 0; i < argc - optind; i++) { |
| 295 | LYS_INFORMAT format_schema = LYS_IN_UNKNOWN; |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 296 | LYD_FORMAT format_data = yo->data_in_format; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 297 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 298 | filepath = argv[optind + i]; |
| 299 | |
| 300 | if (!filepath) { |
| 301 | goto error; |
| 302 | } |
| 303 | if (get_input(filepath, &format_schema, &format_data, &in)) { |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame] | 304 | goto error; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | if (format_schema) { |
| 308 | LY_ERR ret; |
| 309 | uint8_t path_unset = 1; /* flag to unset the path from the searchpaths list (if not already present) */ |
Michal Vasko | 5974087 | 2021-11-22 10:01:34 +0100 | [diff] [blame] | 310 | const char **features; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 311 | |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 312 | /* parse the input */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 313 | if (parse_schema_path(filepath, &dir, &module)) { |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame] | 314 | goto error; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 315 | } |
| 316 | |
aPiecek | a6d45b5 | 2023-05-19 11:48:59 +0200 | [diff] [blame] | 317 | mod = NULL; |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 318 | if (yo->yang_lib_file) { |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 319 | /* just get the module, it should already be parsed */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 320 | mod = ly_ctx_get_module_implemented(*ctx, module); |
aPiecek | a6d45b5 | 2023-05-19 11:48:59 +0200 | [diff] [blame] | 321 | } |
| 322 | if (!mod) { |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 323 | /* add temporarily also the path of the module itself */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 324 | if (ly_ctx_set_searchdir(*ctx, dir) == LY_EEXIST) { |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 325 | path_unset = 0; |
| 326 | } |
| 327 | |
| 328 | /* get features list for this module */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 329 | if (!yo->schema_features.count) { |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 330 | features = all_features; |
| 331 | } else { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 332 | get_features(&yo->schema_features, module, &features); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /* parse module */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 336 | ret = lys_parse(*ctx, in, format_schema, features, &mod); |
| 337 | ly_ctx_unset_searchdir_last(*ctx, path_unset); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 338 | if (ret) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 339 | YLMSG_E("Parsing schema module \"%s\" failed.\n", filepath); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 340 | goto error; |
| 341 | } |
Michal Vasko | 703370c | 2021-10-19 14:07:16 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 344 | /* temporary cleanup */ |
| 345 | free(dir); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 346 | dir = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 347 | free(module); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 348 | module = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 349 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 350 | if (yo->schema_out_format || yo->feature_param_format) { |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 351 | /* module will be printed */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 352 | if (ly_set_add(&yo->schema_modules, (void *)mod, 1, NULL)) { |
| 353 | YLMSG_E("Storing parsed schema module (%s) for print failed.\n", filepath); |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame] | 354 | goto error; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 355 | } |
| 356 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 357 | } else if (format_data) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 358 | if (!fill_cmdline_file(&yo->data_inputs, in, filepath, format_data)) { |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame] | 359 | goto error; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 360 | } |
Radek Krejci | f53b0d5 | 2020-12-03 11:29:24 +0100 | [diff] [blame] | 361 | in = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 362 | } |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 363 | |
| 364 | ly_in_free(in, 1); |
| 365 | in = NULL; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 366 | } |
| 367 | |
Michal Vasko | c91fb12 | 2021-11-23 09:00:17 +0100 | [diff] [blame] | 368 | /* check that all specified features were applied, apply now if possible */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 369 | while ((sf = get_features_not_applied(&yo->schema_features))) { |
Michal Vasko | c91fb12 | 2021-11-23 09:00:17 +0100 | [diff] [blame] | 370 | /* try to find implemented or the latest revision of this module */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 371 | mod = ly_ctx_get_module_implemented(*ctx, sf->mod_name); |
Michal Vasko | c91fb12 | 2021-11-23 09:00:17 +0100 | [diff] [blame] | 372 | if (!mod) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 373 | mod = ly_ctx_get_module_latest(*ctx, sf->mod_name); |
Michal Vasko | 686d8fc | 2021-11-22 10:03:23 +0100 | [diff] [blame] | 374 | } |
Michal Vasko | c91fb12 | 2021-11-23 09:00:17 +0100 | [diff] [blame] | 375 | if (!mod) { |
| 376 | YLMSG_E("Specified features not applied, module \"%s\" not loaded.\n", sf->mod_name); |
| 377 | goto error; |
| 378 | } |
| 379 | |
| 380 | /* we have the module, implement it if needed and enable the specific features */ |
| 381 | if (lys_set_implemented(mod, (const char **)sf->features)) { |
| 382 | YLMSG_E("Implementing module \"%s\" failed.\n", mod->name); |
| 383 | goto error; |
| 384 | } |
| 385 | sf->applied = 1; |
Michal Vasko | 686d8fc | 2021-11-22 10:03:23 +0100 | [diff] [blame] | 386 | } |
| 387 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 388 | return 0; |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame] | 389 | |
| 390 | error: |
| 391 | ly_in_free(in, 1); |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 392 | free(dir); |
| 393 | free(module); |
Radek Krejci | f2afd67 | 2020-11-26 12:29:23 +0100 | [diff] [blame] | 394 | return -1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 395 | } |
| 396 | |
aPiecek | 0cbc902 | 2023-06-08 16:58:56 +0200 | [diff] [blame] | 397 | #ifndef NDEBUG |
| 398 | /** |
| 399 | * @brief Enable specific debugging messages. |
| 400 | * |
| 401 | * @param[in] groups String in the form <group>[,group>]*. |
| 402 | * @param[in,out] yo Options for yanglint. |
| 403 | * return 0 on success. |
| 404 | */ |
| 405 | static int |
| 406 | set_debug_groups(char *groups, struct yl_opt *yo) |
| 407 | { |
| 408 | int rc; |
| 409 | char *str, *end; |
| 410 | |
| 411 | /* Process all debug arguments except the last one. */ |
| 412 | for (str = groups; (end = strchr(str, ',')); str = end + 1) { |
| 413 | /* Temporary modify input string. */ |
| 414 | *end = '\0'; |
| 415 | rc = cmd_debug_exec(NULL, yo, str); |
| 416 | *end = ','; |
| 417 | if (rc) { |
| 418 | return -1; |
| 419 | } |
| 420 | } |
| 421 | /* Process single/last debug argument. */ |
| 422 | if (cmd_debug_exec(NULL, yo, str)) { |
| 423 | return -1; |
| 424 | } |
| 425 | /* All debug arguments are valid, so they can apply. */ |
| 426 | if (cmd_debug_fin(NULL, yo)) { |
| 427 | return -1; |
| 428 | } |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | #endif |
| 434 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 435 | /** |
| 436 | * @brief Process command line options and store the settings into the context. |
| 437 | * |
| 438 | * return -1 in case of error; |
| 439 | * return 0 in case of success and ready to process |
| 440 | * return 1 in case of success, but expect to exit. |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 441 | */ |
| 442 | static int |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 443 | fill_context(int argc, char *argv[], struct yl_opt *yo, struct ly_ctx **ctx) |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 444 | { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 445 | int ret; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 446 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 447 | int opt, opt_index; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 448 | struct option options[] = { |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 449 | {"help", no_argument, NULL, 'h'}, |
| 450 | {"version", no_argument, NULL, 'v'}, |
| 451 | {"verbose", no_argument, NULL, 'V'}, |
| 452 | {"quiet", no_argument, NULL, 'Q'}, |
| 453 | {"format", required_argument, NULL, 'f'}, |
| 454 | {"path", required_argument, NULL, 'p'}, |
Michal Vasko | d8b0e77 | 2022-03-18 13:36:04 +0100 | [diff] [blame] | 455 | {"disable-searchdir", no_argument, NULL, 'D'}, |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 456 | {"features", required_argument, NULL, 'F'}, |
| 457 | {"make-implemented", no_argument, NULL, 'i'}, |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 458 | {"in-format", required_argument, NULL, 'I'}, |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 459 | {"schema-node", required_argument, NULL, 'P'}, |
| 460 | {"single-node", no_argument, NULL, 'q'}, |
| 461 | {"submodule", required_argument, NULL, 's'}, |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 462 | {"ext-data", required_argument, NULL, 'x'}, |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 463 | {"not-strict", no_argument, NULL, 'n'}, |
| 464 | {"present", no_argument, NULL, 'e'}, |
| 465 | {"type", required_argument, NULL, 't'}, |
| 466 | {"default", required_argument, NULL, 'd'}, |
aPiecek | 1b62976 | 2023-04-12 13:52:30 +0200 | [diff] [blame] | 467 | {"data-xpath", required_argument, NULL, 'E'}, |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 468 | {"list", no_argument, NULL, 'l'}, |
| 469 | {"tree-line-length", required_argument, NULL, 'L'}, |
| 470 | {"output", required_argument, NULL, 'o'}, |
| 471 | {"operational", required_argument, NULL, 'O'}, |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 472 | {"reply-rpc", required_argument, NULL, 'R'}, |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 473 | {"merge", no_argument, NULL, 'm'}, |
| 474 | {"yang-library", no_argument, NULL, 'y'}, |
| 475 | {"yang-library-file", required_argument, NULL, 'Y'}, |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 476 | {"extended-leafref", no_argument, NULL, 'X'}, |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 477 | #ifndef NDEBUG |
| 478 | {"debug", required_argument, NULL, 'G'}, |
| 479 | #endif |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 480 | {NULL, 0, NULL, 0} |
| 481 | }; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 482 | uint8_t data_type_set = 0; |
aPiecek | 7941e32 | 2023-05-10 15:44:41 +0200 | [diff] [blame] | 483 | uint32_t temp_lo = 0; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 484 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 485 | yo->ctx_options = YL_DEFAULT_CTX_OPTIONS; |
| 486 | yo->data_parse_options = YL_DEFAULT_DATA_PARSE_OPTIONS; |
| 487 | yo->data_validate_options = YL_DEFAULT_DATA_VALIDATE_OPTIONS; |
| 488 | yo->line_length = 0; |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 489 | |
Michal Vasko | 27a4acb | 2021-11-22 10:03:46 +0100 | [diff] [blame] | 490 | opterr = 0; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 491 | #ifndef NDEBUG |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 492 | while ((opt = getopt_long(argc, argv, "hvVQf:I:p:DF:iP:qs:neE:t:d:lL:o:O:R:myY:Xx:G:", options, &opt_index)) != -1) |
Michal Vasko | 27a4acb | 2021-11-22 10:03:46 +0100 | [diff] [blame] | 493 | #else |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 494 | while ((opt = getopt_long(argc, argv, "hvVQf:I:p:DF:iP:qs:neE:t:d:lL:o:O:R:myY:Xx:", options, &opt_index)) != -1) |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 495 | #endif |
Michal Vasko | 03fe620 | 2022-07-22 16:23:13 +0200 | [diff] [blame] | 496 | { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 497 | switch (opt) { |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 498 | case 'h': /* --help */ |
| 499 | help(0); |
| 500 | return 1; |
| 501 | |
| 502 | case 'v': /* --version */ |
| 503 | version(); |
| 504 | return 1; |
| 505 | |
| 506 | case 'V': { /* --verbose */ |
| 507 | LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR); |
Michal Vasko | 2bf4af4 | 2023-01-04 12:08:38 +0100 | [diff] [blame] | 508 | |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 509 | if (verbosity < LY_LLDBG) { |
Michal Vasko | b0d307b | 2021-11-25 11:15:16 +0100 | [diff] [blame] | 510 | ++verbosity; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 511 | } |
Michal Vasko | b0d307b | 2021-11-25 11:15:16 +0100 | [diff] [blame] | 512 | ly_log_level(verbosity); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 513 | break; |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 514 | } /* case 'V' */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 515 | |
Michal Vasko | b0d307b | 2021-11-25 11:15:16 +0100 | [diff] [blame] | 516 | case 'Q': { /* --quiet */ |
| 517 | LY_LOG_LEVEL verbosity = ly_log_level(LY_LLERR); |
Michal Vasko | 2bf4af4 | 2023-01-04 12:08:38 +0100 | [diff] [blame] | 518 | |
Michal Vasko | b0d307b | 2021-11-25 11:15:16 +0100 | [diff] [blame] | 519 | if (verbosity == LY_LLERR) { |
| 520 | /* turn logging off */ |
| 521 | ly_log_options(LY_LOSTORE_LAST); |
| 522 | } else if (verbosity > LY_LLERR) { |
| 523 | --verbosity; |
| 524 | } |
| 525 | ly_log_level(verbosity); |
| 526 | break; |
| 527 | } /* case 'Q' */ |
| 528 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 529 | case 'f': /* --format */ |
aPiecek | 113e0f0 | 2023-06-09 08:47:48 +0200 | [diff] [blame] | 530 | if (yl_opt_update_out_format(optarg, yo)) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 531 | help(1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 532 | return -1; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 533 | } |
| 534 | break; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 535 | |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 536 | case 'I': /* --in-format */ |
aPiecek | b5dff49 | 2023-06-09 09:38:08 +0200 | [diff] [blame] | 537 | if (yo_opt_update_data_in_format(optarg, yo)) { |
aPiecek | ef0a339 | 2023-05-16 10:34:32 +0200 | [diff] [blame] | 538 | YLMSG_E("Unknown input format %s\n", optarg); |
| 539 | help(1); |
| 540 | return -1; |
| 541 | } |
| 542 | break; |
| 543 | |
aPiecek | 113e0f0 | 2023-06-09 08:47:48 +0200 | [diff] [blame] | 544 | case 'p': /* --path */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 545 | if (searchpath_strcat(&yo->searchpaths, optarg)) { |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 546 | YLMSG_E("Storing searchpath failed.\n"); |
| 547 | return -1; |
| 548 | } |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 549 | break; |
aPiecek | 113e0f0 | 2023-06-09 08:47:48 +0200 | [diff] [blame] | 550 | /* case 'p' */ |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 551 | |
| 552 | case 'D': /* --disable-search */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 553 | if (yo->ctx_options & LY_CTX_DISABLE_SEARCHDIRS) { |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 554 | YLMSG_W("The -D option specified too many times.\n"); |
| 555 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 556 | if (yo->ctx_options & LY_CTX_DISABLE_SEARCHDIR_CWD) { |
| 557 | yo->ctx_options &= ~LY_CTX_DISABLE_SEARCHDIR_CWD; |
| 558 | yo->ctx_options |= LY_CTX_DISABLE_SEARCHDIRS; |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 559 | } else { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 560 | yo->ctx_options |= LY_CTX_DISABLE_SEARCHDIR_CWD; |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 561 | } |
| 562 | break; |
| 563 | |
| 564 | case 'F': /* --features */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 565 | if (parse_features(optarg, &yo->schema_features)) { |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 566 | return -1; |
| 567 | } |
| 568 | break; |
| 569 | |
Michal Vasko | 8d6d0ad | 2021-10-19 12:32:27 +0200 | [diff] [blame] | 570 | case 'i': /* --make-implemented */ |
aPiecek | 7f22c10 | 2023-06-09 09:48:44 +0200 | [diff] [blame^] | 571 | yo_opt_update_make_implemented(yo); |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 572 | break; |
| 573 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 574 | case 'P': /* --schema-node */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 575 | yo->schema_node_path = optarg; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 576 | break; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 577 | |
| 578 | case 'q': /* --single-node */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 579 | yo->schema_print_options |= LYS_PRINT_NO_SUBSTMT; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 580 | break; |
| 581 | |
Michal Vasko | fe74d1e | 2021-09-30 13:17:36 +0200 | [diff] [blame] | 582 | case 's': /* --submodule */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 583 | yo->submodule = optarg; |
Michal Vasko | fe74d1e | 2021-09-30 13:17:36 +0200 | [diff] [blame] | 584 | break; |
| 585 | |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 586 | case 'x': /* --ext-data */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 587 | yo->schema_context_filename = optarg; |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 588 | break; |
| 589 | |
Michal Vasko | 667ce6b | 2021-01-25 15:00:27 +0100 | [diff] [blame] | 590 | case 'n': /* --not-strict */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 591 | yo->data_parse_options &= ~LYD_PARSE_STRICT; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 592 | break; |
| 593 | |
| 594 | case 'e': /* --present */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 595 | yo->data_validate_options |= LYD_VALIDATE_PRESENT; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 596 | break; |
| 597 | |
| 598 | case 't': /* --type */ |
| 599 | if (data_type_set) { |
| 600 | YLMSG_E("The data type (-t) cannot be set multiple times.\n"); |
| 601 | return -1; |
| 602 | } |
| 603 | |
aPiecek | 3167f38 | 2023-06-09 09:23:10 +0200 | [diff] [blame] | 604 | if (yl_opt_update_data_type(optarg, yo)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 605 | YLMSG_E("Unknown data tree type %s\n", optarg); |
| 606 | help(1); |
| 607 | return -1; |
| 608 | } |
| 609 | |
| 610 | data_type_set = 1; |
| 611 | break; |
| 612 | |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 613 | case 'd': /* --default */ |
aPiecek | 20cc2fe | 2023-06-09 09:32:28 +0200 | [diff] [blame] | 614 | if (yo_opt_update_data_default(optarg, yo)) { |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 615 | YLMSG_E("Unknown default mode %s\n", optarg); |
| 616 | help(1); |
| 617 | return -1; |
| 618 | } |
| 619 | break; |
| 620 | |
aPiecek | 1b62976 | 2023-04-12 13:52:30 +0200 | [diff] [blame] | 621 | case 'E': /* --data-xpath */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 622 | if (ly_set_add(&yo->data_xpath, optarg, 0, NULL)) { |
aPiecek | 1b62976 | 2023-04-12 13:52:30 +0200 | [diff] [blame] | 623 | YLMSG_E("Storing XPath \"%s\" failed.\n", optarg); |
| 624 | return -1; |
| 625 | } |
| 626 | break; |
| 627 | |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 628 | case 'l': /* --list */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 629 | yo->list = 1; |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 630 | break; |
| 631 | |
| 632 | case 'L': /* --tree-line-length */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 633 | yo->line_length = atoi(optarg); |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 634 | break; |
| 635 | |
| 636 | case 'o': /* --output */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 637 | if (yo->out) { |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 638 | YLMSG_E("Only a single output can be specified.\n"); |
| 639 | return -1; |
| 640 | } else { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 641 | if (ly_out_new_filepath(optarg, &yo->out)) { |
Michal Vasko | c40548b | 2021-09-30 13:05:47 +0200 | [diff] [blame] | 642 | YLMSG_E("Unable open output file %s (%s)\n", optarg, strerror(errno)); |
| 643 | return -1; |
| 644 | } |
| 645 | } |
| 646 | break; |
| 647 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 648 | case 'O': /* --operational */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 649 | if (yo->data_operational.path) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 650 | YLMSG_E("The operational datastore (-O) cannot be set multiple times.\n"); |
| 651 | return -1; |
| 652 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 653 | yo->data_operational.path = optarg; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 654 | break; |
| 655 | |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 656 | case 'R': /* --reply-rpc */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 657 | if (yo->reply_rpc.path) { |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 658 | YLMSG_E("The PRC of the reply (-R) cannot be set multiple times.\n"); |
| 659 | return -1; |
| 660 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 661 | yo->reply_rpc.path = optarg; |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 662 | break; |
| 663 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 664 | case 'm': /* --merge */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 665 | yo->data_merge = 1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 666 | break; |
| 667 | |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 668 | case 'y': /* --yang-library */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 669 | yo->ctx_options &= ~LY_CTX_NO_YANGLIBRARY; |
Michal Vasko | 24aaf8e | 2022-02-04 11:15:26 +0100 | [diff] [blame] | 670 | break; |
| 671 | |
| 672 | case 'Y': /* --yang-library-file */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 673 | yo->ctx_options &= ~LY_CTX_NO_YANGLIBRARY; |
| 674 | yo->yang_lib_file = optarg; |
Michal Vasko | c431a0a | 2021-01-25 14:31:58 +0100 | [diff] [blame] | 675 | break; |
| 676 | |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 677 | case 'X': /* --extended-leafref */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 678 | yo->ctx_options |= LY_CTX_LEAFREF_EXTENDED; |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 679 | break; |
| 680 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 681 | #ifndef NDEBUG |
aPiecek | 0cbc902 | 2023-06-08 16:58:56 +0200 | [diff] [blame] | 682 | case 'G': /* --debug */ |
| 683 | if (set_debug_groups(optarg, yo)) { |
| 684 | return -1; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 685 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 686 | break; |
aPiecek | 0cbc902 | 2023-06-08 16:58:56 +0200 | [diff] [blame] | 687 | /* case 'G' */ |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 688 | #endif |
Michal Vasko | 27a4acb | 2021-11-22 10:03:46 +0100 | [diff] [blame] | 689 | default: |
| 690 | YLMSG_E("Invalid option or missing argument: -%c\n", optopt); |
| 691 | return -1; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 692 | } /* switch */ |
| 693 | } |
| 694 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 695 | /* additional checks for the options combinations */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 696 | if (!yo->list && (optind >= argc)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 697 | help(1); |
| 698 | YLMSG_E("Missing <schema> to process.\n"); |
| 699 | return 1; |
| 700 | } |
| 701 | |
aPiecek | bc39de1 | 2023-06-08 08:10:29 +0200 | [diff] [blame] | 702 | if (cmd_data_dep(yo, 0)) { |
Michal Vasko | 3f08fb9 | 2022-04-21 09:52:35 +0200 | [diff] [blame] | 703 | return -1; |
| 704 | } |
aPiecek | 04c8c2e | 2023-06-08 08:38:22 +0200 | [diff] [blame] | 705 | if (cmd_print_dep(yo, 0)) { |
| 706 | return -1; |
aPiecek | 2cfeeae | 2021-04-21 13:17:34 +0200 | [diff] [blame] | 707 | } |
| 708 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 709 | if (yo->schema_out_format == LYS_OUT_TREE) { |
aPiecek | 7ac66d5 | 2021-05-20 07:54:07 +0200 | [diff] [blame] | 710 | /* print tree from lysc_nodes */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 711 | yo->ctx_options |= LY_CTX_SET_PRIV_PARSED; |
aPiecek | 7ac66d5 | 2021-05-20 07:54:07 +0200 | [diff] [blame] | 712 | } |
| 713 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 714 | /* process input files provided as standalone command line arguments, |
| 715 | * schema modules are parsed and inserted into the context, |
| 716 | * data files are just checked and prepared into internal structures for further processing */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 717 | ret = fill_context_inputs(argc, argv, yo, ctx); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 718 | if (ret) { |
| 719 | return ret; |
| 720 | } |
| 721 | |
| 722 | /* the second batch of checks */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 723 | if (yo->schema_print_options && !yo->schema_out_format) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 724 | YLMSG_W("Schema printer options specified, but the schema output format is missing.\n"); |
| 725 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 726 | if (yo->schema_parse_options && !yo->schema_modules.count) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 727 | YLMSG_W("Schema parser options specified, but no schema input file provided.\n"); |
| 728 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 729 | if (yo->data_print_options && !yo->data_out_format) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 730 | YLMSG_W("data printer options specified, but the data output format is missing.\n"); |
| 731 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 732 | if (((yo->data_parse_options != YL_DEFAULT_DATA_PARSE_OPTIONS) || yo->data_type) && !yo->data_inputs.count) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 733 | YLMSG_W("Data parser options specified, but no data input file provided.\n"); |
| 734 | } |
| 735 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 736 | if (yo->schema_node_path) { |
aPiecek | 7941e32 | 2023-05-10 15:44:41 +0200 | [diff] [blame] | 737 | /* turn off logging so that the message is not repeated */ |
| 738 | ly_temp_log_options(&temp_lo); |
| 739 | /* search operation input */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 740 | yo->schema_node = lys_find_path(*ctx, NULL, yo->schema_node_path, 0); |
| 741 | if (!yo->schema_node) { |
aPiecek | 7941e32 | 2023-05-10 15:44:41 +0200 | [diff] [blame] | 742 | /* restore logging so an error may be displayed */ |
| 743 | ly_temp_log_options(NULL); |
| 744 | /* search operation output */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 745 | yo->schema_node = lys_find_path(*ctx, NULL, yo->schema_node_path, 1); |
| 746 | if (!yo->schema_node) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 747 | YLMSG_E("Invalid schema path.\n"); |
| 748 | return -1; |
| 749 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 750 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 751 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 752 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | int |
| 757 | main_ni(int argc, char *argv[]) |
| 758 | { |
| 759 | int ret = EXIT_SUCCESS, r; |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 760 | struct yl_opt yo = {0}; |
| 761 | struct ly_ctx *ctx = NULL; |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 762 | char *features_output = NULL; |
| 763 | struct ly_set set = {0}; |
| 764 | uint32_t u; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 765 | |
| 766 | /* set callback for printing libyang messages */ |
| 767 | ly_set_log_clb(libyang_verbclb, 1); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 768 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 769 | r = fill_context(argc, argv, &yo, &ctx); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 770 | if (r < 0) { |
| 771 | ret = EXIT_FAILURE; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 772 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 773 | if (r) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 774 | goto cleanup; |
| 775 | } |
| 776 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 777 | /* do the required job - parse, validate, print */ |
| 778 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 779 | if (yo.list) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 780 | /* print the list of schemas */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 781 | ret = print_list(yo.out, ctx, yo.data_out_format); |
aPiecek | 3426262 | 2023-03-27 08:28:25 +0200 | [diff] [blame] | 782 | goto cleanup; |
Radek Krejci | 047d64e | 2020-12-03 12:57:10 +0100 | [diff] [blame] | 783 | } else { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 784 | if (yo.feature_param_format) { |
| 785 | for (u = 0; u < yo.schema_modules.count; u++) { |
| 786 | if (collect_features(yo.schema_modules.objs[u], &set)) { |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 787 | YLMSG_E("Unable to read features from a module.\n"); |
| 788 | goto cleanup; |
| 789 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 790 | if (generate_features_output(yo.schema_modules.objs[u], &set, &features_output)) { |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 791 | YLMSG_E("Unable to generate feature command output.\n"); |
| 792 | goto cleanup; |
| 793 | } |
| 794 | ly_set_erase(&set, NULL); |
| 795 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 796 | ly_print(yo.out, "%s\n", features_output); |
| 797 | } else if (yo.schema_out_format) { |
| 798 | if (yo.schema_node) { |
| 799 | ret = lys_print_node(yo.out, yo.schema_node, yo.schema_out_format, yo.line_length, yo.schema_print_options); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 800 | if (ret) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 801 | YLMSG_E("Unable to print schema node %s.\n", yo.schema_node_path); |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 802 | goto cleanup; |
| 803 | } |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 804 | } else if (yo.submodule) { |
| 805 | const struct lysp_submodule *submod = ly_ctx_get_submodule_latest(ctx, yo.submodule); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 806 | |
Michal Vasko | fe74d1e | 2021-09-30 13:17:36 +0200 | [diff] [blame] | 807 | if (!submod) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 808 | YLMSG_E("Unable to find submodule %s.\n", yo.submodule); |
Michal Vasko | fe74d1e | 2021-09-30 13:17:36 +0200 | [diff] [blame] | 809 | goto cleanup; |
| 810 | } |
| 811 | |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 812 | ret = lys_print_submodule(yo.out, submod, yo.schema_out_format, yo.line_length, yo.schema_print_options); |
Michal Vasko | fe74d1e | 2021-09-30 13:17:36 +0200 | [diff] [blame] | 813 | if (ret) { |
| 814 | YLMSG_E("Unable to print submodule %s.\n", submod->name); |
| 815 | goto cleanup; |
| 816 | } |
Radek Krejci | 047d64e | 2020-12-03 12:57:10 +0100 | [diff] [blame] | 817 | } else { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 818 | for (u = 0; u < yo.schema_modules.count; ++u) { |
| 819 | ret = lys_print_module(yo.out, (struct lys_module *)yo.schema_modules.objs[u], yo.schema_out_format, |
| 820 | yo.line_length, yo.schema_print_options); |
aPiecek | 8f0b788 | 2021-05-21 10:18:36 +0200 | [diff] [blame] | 821 | /* for YANG Tree Diagrams printing it's more readable to print a blank line between modules. */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 822 | if ((yo.schema_out_format == LYS_OUT_TREE) && (u + 1 < yo.schema_modules.count)) { |
| 823 | ly_print(yo.out, "\n"); |
aPiecek | 8f0b788 | 2021-05-21 10:18:36 +0200 | [diff] [blame] | 824 | } |
Radek Krejci | 047d64e | 2020-12-03 12:57:10 +0100 | [diff] [blame] | 825 | if (ret) { |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 826 | YLMSG_E("Unable to print module %s.\n", ((struct lys_module *)yo.schema_modules.objs[u])->name); |
Radek Krejci | 047d64e | 2020-12-03 12:57:10 +0100 | [diff] [blame] | 827 | goto cleanup; |
| 828 | } |
| 829 | } |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 830 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 831 | } |
Radek Krejci | 047d64e | 2020-12-03 12:57:10 +0100 | [diff] [blame] | 832 | |
| 833 | /* do the data validation despite the schema was printed */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 834 | if (yo.data_inputs.size) { |
| 835 | ret = process_data(ctx, yo.data_type, yo.data_merge, yo.data_out_format, yo.out, yo.data_parse_options, |
| 836 | yo.data_validate_options, yo.data_print_options, &yo.data_operational, &yo.reply_rpc, &yo.data_inputs, |
| 837 | &yo.data_xpath); |
Michal Vasko | 0f1e456 | 2022-03-29 11:57:57 +0200 | [diff] [blame] | 838 | if (ret) { |
Radek Krejci | 047d64e | 2020-12-03 12:57:10 +0100 | [diff] [blame] | 839 | goto cleanup; |
| 840 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 841 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 842 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 843 | |
| 844 | cleanup: |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 845 | /* cleanup */ |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 846 | yl_opt_erase(&yo); |
| 847 | ly_ctx_destroy(ctx); |
roman | 300b878 | 2022-08-11 12:49:21 +0200 | [diff] [blame] | 848 | free(features_output); |
| 849 | ly_set_erase(&set, NULL); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 850 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 851 | return ret; |
| 852 | } |