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