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