aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file printer_tree.c |
| 3 | * @author Adam Piecek <piecek@cesnet.cz> |
| 4 | * @brief RFC tree printer for libyang data structure |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2021 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #include <assert.h> |
| 16 | #include <string.h> |
| 17 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 18 | #include "common.h" |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 19 | #include "compat.h" |
| 20 | #include "out_internal.h" |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 21 | #include "tree_schema_internal.h" |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 22 | #include "xpath.h" |
| 23 | |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 24 | struct trt_tree_ctx; |
| 25 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 26 | /****************************************************************************** |
| 27 | * Declarations start |
| 28 | *****************************************************************************/ |
| 29 | |
| 30 | /* |
| 31 | * +---------+ +---------+ +---------+ |
| 32 | * output | trp | | trb | | tro | |
| 33 | * <---+ Print +<---+ Browse +<-->+ Obtain | |
| 34 | * | | | | | | |
| 35 | * +---------+ +----+----+ +---------+ |
| 36 | * ^ |
| 37 | * | |
| 38 | * +----+----+ |
| 39 | * | trm | |
| 40 | * | Manager | |
| 41 | * | | |
| 42 | * +----+----+ |
| 43 | * ^ |
| 44 | * | input |
| 45 | * + |
| 46 | * |
| 47 | * Glossary: |
| 48 | * trt - type |
| 49 | * trp - functions for Printing |
| 50 | * trb - functions for Browse the tree |
| 51 | * tro - functions for Obtaining information from libyang |
| 52 | * trm - Main functions, Manager |
| 53 | * trg - General functions |
| 54 | * |
| 55 | * - Manager functions (trm) are able to print individual sections of the YANG tree diagram |
| 56 | * (eg module, notifications, rpcs ...) and they call Browse functions (trb). |
| 57 | * - Browse functions contain a general algorithm (Preorder DFS) for traversing the tree. |
| 58 | * They call the Obtain functions (tro) to get information about the node |
| 59 | * or eg to get a sibling or child for the current node. |
| 60 | * This obtained information is passed on to the Print functions (trp) for printing. |
| 61 | * Gap offsets before the node type are also calculated in the Browse functions. |
| 62 | * - Print functions (trp) take care of the printing itself. |
| 63 | * They can also split one node into multiple lines if the node does not fit on one line. |
| 64 | * |
| 65 | * For future adjustments: |
| 66 | * it is assumed that the changes are likely to take place mainly for tro functions |
| 67 | * because they are the only ones dependent on libyang implementation. |
| 68 | * In special cases, changes will also need to be made to the trp functions |
| 69 | * if a special algorithm is needed to print (right now this is prepared for printing list's keys |
| 70 | * and if-features). |
| 71 | */ |
| 72 | |
| 73 | /** |
| 74 | * @brief List of available actions. |
| 75 | */ |
| 76 | typedef enum { |
| 77 | TRD_PRINT = 0, |
| 78 | TRD_CHAR_COUNT |
| 79 | } trt_ly_out_clb_arg_flag; |
| 80 | |
| 81 | /** |
| 82 | * @brief Specific argument to be passed to the ly_write_clb. |
| 83 | */ |
| 84 | struct ly_out_clb_arg { |
| 85 | trt_ly_out_clb_arg_flag mode; /**< flag specifying which action to take. */ |
| 86 | struct ly_out *out; /**< The ly_out pointer delivered to the printer tree module via the main interface. */ |
| 87 | size_t counter; /**< Counter of printed characters. */ |
| 88 | LY_ERR last_error; /**< The last error that occurred. If no error has occurred, it will be LY_SUCCESS. */ |
| 89 | }; |
| 90 | |
| 91 | /** |
| 92 | * @brief Initialize struct ly_out_clb_arg with default settings. |
| 93 | */ |
| 94 | #define TRP_INIT_LY_OUT_CLB_ARG(MODE, OUT, COUNTER, LAST_ERROR) \ |
| 95 | (struct ly_out_clb_arg){.mode = MODE, .out = OUT, .counter = COUNTER, .last_error = LAST_ERROR} |
| 96 | |
| 97 | /****************************************************************************** |
| 98 | * Print getters |
| 99 | *****************************************************************************/ |
| 100 | |
| 101 | /** |
| 102 | * @brief Callback functions that prints special cases. |
| 103 | * |
| 104 | * It just groups together tree context with trt_fp_print. |
| 105 | */ |
| 106 | struct trt_cf_print { |
| 107 | const struct trt_tree_ctx *ctx; /**< Context of libyang tree. */ |
| 108 | void (*pf)(const struct trt_tree_ctx *, struct ly_out *); /**< Pointing to function which printing list's keys or features. */ |
| 109 | }; |
| 110 | |
| 111 | /** |
| 112 | * @brief Callback functions for printing special cases. |
| 113 | * |
| 114 | * Functions with the suffix 'trp' can print most of the text on output, just by setting the pointer to the string. |
| 115 | * But in some cases, it's not that simple, because its entire string is fragmented in memory. |
| 116 | * For example, for printing list's keys or if-features. |
| 117 | * However, this depends on how the libyang library is implemented. |
| 118 | * This implementation of the printer_tree module goes through a lysp tree, but if it goes through a lysc tree, |
| 119 | * these special cases would be different. |
| 120 | * Functions must print including spaces or delimiters between names. |
| 121 | */ |
| 122 | struct trt_fp_print { |
| 123 | void (*print_features_names)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list of features without {}? wrapper. */ |
| 124 | void (*print_keys)(const struct trt_tree_ctx *, struct ly_out *); /**< Print list's keys without [] wrapper. */ |
| 125 | }; |
| 126 | |
| 127 | /** |
| 128 | * @brief Package which only groups getter function. |
| 129 | */ |
| 130 | struct trt_pck_print { |
| 131 | const struct trt_tree_ctx *tree_ctx; /**< Context of libyang tree. */ |
| 132 | struct trt_fp_print fps; /**< Print function. */ |
| 133 | }; |
| 134 | |
| 135 | /** |
| 136 | * @brief Initialize struct trt_pck_print by parameters. |
| 137 | */ |
| 138 | #define TRP_INIT_PCK_PRINT(TREE_CTX, FP_PRINT) \ |
| 139 | (struct trt_pck_print){.tree_ctx = TREE_CTX, .fps = FP_PRINT} |
| 140 | |
| 141 | /****************************************************************************** |
| 142 | * Indent |
| 143 | *****************************************************************************/ |
| 144 | |
| 145 | /** |
| 146 | * @brief Constants which are defined in the RFC or are observable from the pyang tool. |
| 147 | */ |
| 148 | typedef enum { |
| 149 | TRD_INDENT_EMPTY = 0, /**< If the node is a case node, there is no space before the \<name\>. */ |
| 150 | TRD_INDENT_LONG_LINE_BREAK = 2, /**< The new line should be indented so that it starts below \<name\> with a whitespace offset of at least two characters. */ |
| 151 | TRD_INDENT_LINE_BEGIN = 2, /**< Indent below the keyword (module, augment ...). */ |
| 152 | TRD_INDENT_BTW_SIBLINGS = 2, /**< Indent between | and | characters. */ |
| 153 | TRD_INDENT_BEFORE_KEYS = 1, /**< "..."___\<keys\>. */ |
| 154 | TRD_INDENT_BEFORE_TYPE = 4, /**< "..."___\<type\>, but if mark is set then indent == 3. */ |
| 155 | TRD_INDENT_BEFORE_IFFEATURES = 1 /**< "..."___\<iffeatures\>. */ |
| 156 | } trt_cnf_indent; |
| 157 | |
| 158 | /** |
| 159 | * @brief Type of indent in node. |
| 160 | */ |
| 161 | typedef enum { |
| 162 | TRD_INDENT_IN_NODE_NORMAL = 0, /**< Node fits on one line. */ |
| 163 | TRD_INDENT_IN_NODE_DIVIDED, /**< The node must be split into multiple rows. */ |
| 164 | TRD_INDENT_IN_NODE_FAILED /**< Cannot be crammed into one line. The condition for the maximum line length is violated. */ |
| 165 | } trt_indent_in_node_type; |
| 166 | |
| 167 | /** Constant to indicate the need to break a line. */ |
| 168 | #define TRD_LINEBREAK -1 |
| 169 | |
| 170 | /** |
| 171 | * @brief Records the alignment between the individual elements of the node. |
| 172 | * |
| 173 | * See trp_indent_in_node_are_eq, trp_indent_in_node_place_break. |
| 174 | */ |
| 175 | struct trt_indent_in_node { |
| 176 | trt_indent_in_node_type type; /**< Type of indent in node. */ |
| 177 | int16_t btw_name_opts; /**< Indent between node name and opts. */ |
| 178 | int16_t btw_opts_type; /**< Indent between opts and type. */ |
| 179 | int16_t btw_type_iffeatures; /**< Indent between type and features. Ignored if \<type\> missing. */ |
| 180 | }; |
| 181 | |
| 182 | /** |
| 183 | * @brief Type of wrappers to be printed. |
| 184 | */ |
| 185 | typedef enum { |
| 186 | TRD_WRAPPER_TOP = 0, /**< Related to the module. */ |
| 187 | TRD_WRAPPER_BODY /**< Related to e.g. Augmentations or Groupings */ |
| 188 | } trd_wrapper_type; |
| 189 | |
| 190 | /** |
| 191 | * @brief For resolving sibling symbol ('|') placement. |
| 192 | * |
| 193 | * Bit indicates where the sibling symbol must be printed. |
| 194 | * This place is in multiples of TRD_INDENT_BTW_SIBLINGS. |
| 195 | * |
| 196 | * See: TRP_INIT_WRAPPER_TOP, TRP_INIT_WRAPPER_BODY, trp_wrapper_set_mark, |
| 197 | * trp_wrapper_set_shift, trp_wrapper_if_last_sibling, trp_wrapper_eq, |
| 198 | * trp_print_wrapper |
| 199 | */ |
| 200 | struct trt_wrapper { |
| 201 | trd_wrapper_type type; /**< Location of the wrapper. */ |
| 202 | uint64_t bit_marks1; /**< The set bits indicate where the '|' character is to be printed. |
| 203 | It follows that the maximum immersion of the printable node is 64. */ |
| 204 | uint32_t actual_pos; /**< Actual position in bit_marks. */ |
| 205 | }; |
| 206 | |
| 207 | /** |
| 208 | * @brief Get wrapper related to the module section. |
| 209 | * |
| 210 | * @code |
| 211 | * module: <module-name> |
| 212 | * +--<node> |
| 213 | * | |
| 214 | * @endcode |
| 215 | */ |
| 216 | #define TRP_INIT_WRAPPER_TOP \ |
| 217 | (struct trt_wrapper) {.type = TRD_WRAPPER_TOP, .actual_pos = 0, .bit_marks1 = 0} |
| 218 | |
| 219 | /** |
| 220 | * @brief Get wrapper related to subsection e.g. Augmenations or Groupings. |
| 221 | * |
| 222 | * @code |
| 223 | * module: <module-name> |
| 224 | * +--<node> |
| 225 | * |
| 226 | * augment <target-node>: |
| 227 | * +--<node> |
| 228 | * @endcode |
| 229 | */ |
| 230 | #define TRP_INIT_WRAPPER_BODY \ |
| 231 | (struct trt_wrapper) {.type = TRD_WRAPPER_BODY, .actual_pos = 0, .bit_marks1 = 0} |
| 232 | |
| 233 | /** |
| 234 | * @brief Package which only groups wrapper and indent in node. |
| 235 | */ |
| 236 | struct trt_pck_indent { |
| 237 | struct trt_wrapper wrapper; /**< Coded " | | " sequence. */ |
| 238 | struct trt_indent_in_node in_node; /**< Indent in node. */ |
| 239 | }; |
| 240 | |
| 241 | /** |
| 242 | * @brief Initialize struct trt_pck_indent by parameters. |
| 243 | */ |
| 244 | #define TRP_INIT_PCK_INDENT(WRAPPER, INDENT_IN_NODE) \ |
| 245 | (struct trt_pck_indent){.wrapper = WRAPPER, .in_node = INDENT_IN_NODE} |
| 246 | |
| 247 | /****************************************************************************** |
| 248 | * status |
| 249 | *****************************************************************************/ |
| 250 | |
| 251 | /** |
| 252 | * @brief Status of the node. |
| 253 | * |
| 254 | * See: trp_print_status |
| 255 | */ |
| 256 | typedef enum { |
| 257 | TRD_STATUS_TYPE_EMPTY = 0, |
| 258 | TRD_STATUS_TYPE_CURRENT, |
| 259 | TRD_STATUS_TYPE_DEPRECATED, |
| 260 | TRD_STATUS_TYPE_OBSOLETE |
| 261 | } trt_status_type; |
| 262 | |
| 263 | /****************************************************************************** |
| 264 | * flags |
| 265 | *****************************************************************************/ |
| 266 | |
| 267 | /** |
| 268 | * @brief Flag of the node. |
| 269 | * |
| 270 | * See: trp_print_flags, trp_get_flags_strlen |
| 271 | */ |
| 272 | typedef enum { |
| 273 | TRD_FLAGS_TYPE_EMPTY = 0, |
| 274 | TRD_FLAGS_TYPE_RW, /**< rw */ |
| 275 | TRD_FLAGS_TYPE_RO, /**< ro */ |
| 276 | TRD_FLAGS_TYPE_RPC_INPUT_PARAMS, /**< -w */ |
| 277 | TRD_FLAGS_TYPE_USES_OF_GROUPING, /**< -u */ |
| 278 | TRD_FLAGS_TYPE_RPC, /**< -x */ |
| 279 | TRD_FLAGS_TYPE_NOTIF, /**< -n */ |
| 280 | TRD_FLAGS_TYPE_MOUNT_POINT /**< mp */ |
| 281 | } trt_flags_type; |
| 282 | |
| 283 | /****************************************************************************** |
| 284 | * node_name and opts |
| 285 | *****************************************************************************/ |
| 286 | |
| 287 | #define TRD_NODE_NAME_PREFIX_CHOICE "(" |
| 288 | #define TRD_NODE_NAME_PREFIX_CASE ":(" |
| 289 | #define TRD_NODE_NAME_TRIPLE_DOT "..." |
| 290 | |
| 291 | /** |
| 292 | * @brief Type of the node. |
| 293 | * |
| 294 | * Used mainly to complete the correct \<opts\> next to or around the \<name\>. |
| 295 | */ |
| 296 | typedef enum { |
| 297 | TRD_NODE_ELSE = 0, /**< For some node which does not require special treatment. \<name\> */ |
| 298 | TRD_NODE_CASE, /**< For case node. :(\<name\>) */ |
| 299 | TRD_NODE_CHOICE, /**< For choice node. (\<name\>) */ |
| 300 | TRD_NODE_OPTIONAL_CHOICE, /**< For choice node with optional mark. (\<name\>)? */ |
| 301 | TRD_NODE_OPTIONAL, /**< For an optional leaf, anydata, or anyxml. \<name\>? */ |
| 302 | TRD_NODE_CONTAINER, /**< For a presence container. \<name\>! */ |
| 303 | TRD_NODE_LISTLEAFLIST, /**< For a leaf-list or list (without keys). \<name\>* */ |
| 304 | TRD_NODE_KEYS, /**< For a list's keys. \<name\>* [\<keys\>] */ |
| 305 | TRD_NODE_TOP_LEVEL1, /**< For a top-level data node in a mounted module. \<name\>/ */ |
| 306 | TRD_NODE_TOP_LEVEL2, /**< For a top-level data node of a module identified in a mount point parent reference. \<name\>@ */ |
| 307 | TRD_NODE_TRIPLE_DOT /**< For collapsed sibling nodes and their children. Special case which doesn't belong here very well. */ |
| 308 | } trt_node_type; |
| 309 | |
| 310 | /** |
| 311 | * @brief Type of node and his name. |
| 312 | * |
| 313 | * See: TRP_EMPTY_NODE_NAME, TRP_NODE_NAME_IS_EMPTY, |
| 314 | * trp_print_node_name, trp_mark_is_used, trp_print_opts_keys |
| 315 | */ |
| 316 | struct trt_node_name { |
| 317 | trt_node_type type; /**< Type of the node relevant for printing. */ |
| 318 | const char *module_prefix; /**< Prefix defined in the module where the node is defined. */ |
| 319 | const char *str; /**< Name of the node. */ |
| 320 | }; |
| 321 | |
| 322 | /** |
| 323 | * @brief Create struct trt_node_name as empty. |
| 324 | */ |
| 325 | #define TRP_EMPTY_NODE_NAME \ |
| 326 | (struct trt_node_name){.type = TRD_NODE_ELSE, .module_prefix = NULL, .str = NULL} |
| 327 | |
| 328 | /** |
| 329 | * @brief Check if struct trt_node_name is empty. |
| 330 | */ |
| 331 | #define TRP_NODE_NAME_IS_EMPTY(NODE_NAME) \ |
| 332 | !NODE_NAME.str |
| 333 | |
| 334 | /**< Every opts mark has a length of one. */ |
| 335 | #define TRD_OPTS_MARK_LENGTH 1 |
| 336 | |
| 337 | /****************************************************************************** |
| 338 | * type |
| 339 | *****************************************************************************/ |
| 340 | |
| 341 | /** |
| 342 | * @brief Type of the \<type\> |
| 343 | */ |
| 344 | typedef enum { |
| 345 | TRD_TYPE_NAME = 0, /**< Type is just a name that does not require special treatment. */ |
| 346 | TRD_TYPE_TARGET, /**< Should have a form "-> TARGET", where TARGET is the leafref path. */ |
| 347 | TRD_TYPE_LEAFREF, /**< This type is set automatically by the 'trp' algorithm. So set type as TRD_TYPE_TARGET. */ |
| 348 | TRD_TYPE_EMPTY /**< Type is not used at all. */ |
| 349 | } trt_type_type; |
| 350 | |
| 351 | /** |
| 352 | * @brief \<type\> in the \<node\>. |
| 353 | * |
| 354 | * See: TRP_EMPTY_TRT_TYPE, TRP_TRT_TYPE_IS_EMPTY, trp_print_type |
| 355 | */ |
| 356 | struct trt_type { |
| 357 | trt_type_type type; /**< Type of the \<type\>. */ |
| 358 | const char *str; /**< Path or name of the type. */ |
| 359 | }; |
| 360 | |
| 361 | /** |
| 362 | * @brief Create empty struct trt_type. |
| 363 | */ |
| 364 | #define TRP_EMPTY_TRT_TYPE \ |
| 365 | (struct trt_type) {.type = TRD_TYPE_EMPTY, .str = NULL} |
| 366 | |
| 367 | /** |
| 368 | * @brief Check if struct trt_type is empty. |
| 369 | */ |
| 370 | #define TRP_TRT_TYPE_IS_EMPTY(TYPE_OF_TYPE) \ |
| 371 | TYPE_OF_TYPE.type == TRD_TYPE_EMPTY |
| 372 | |
| 373 | /** |
| 374 | * @brief Initialize struct trt_type by parameters. |
| 375 | */ |
| 376 | #define TRP_INIT_TRT_TYPE(TYPE_OF_TYPE, STRING) \ |
| 377 | (struct trt_type) {.type = TYPE_OF_TYPE, .str = STRING} |
| 378 | |
| 379 | /****************************************************************************** |
| 380 | * node |
| 381 | *****************************************************************************/ |
| 382 | |
| 383 | /** |
| 384 | * @brief \<node\> data for printing. |
| 385 | * |
| 386 | * It contains RFC's: \<status\>--\<flags\> \<name\>\<opts\> \<type\> \<if-features\>. |
| 387 | * Item \<opts\> is moved to part struct trt_node_name. |
| 388 | * For printing [\<keys\>] and if-features is required special functions which prints them. |
| 389 | * |
| 390 | * See: TRP_EMPTY_NODE, trp_node_is_empty, trp_node_body_is_empty, trp_print_node_up_to_name, |
| 391 | * trp_print_divided_node_up_to_name, trp_print_node |
| 392 | */ |
| 393 | struct trt_node { |
| 394 | trt_status_type status; /**< \<status\>. */ |
| 395 | trt_flags_type flags; /**< \<flags\>. */ |
| 396 | struct trt_node_name name; /**< \<node\> with \<opts\> mark or [\<keys\>]. */ |
| 397 | struct trt_type type; /**< \<type\> contains the name of the type or type for leafref. */ |
| 398 | ly_bool iffeatures; /**< \<if-features\>. Value 1 means that iffeatures are present and will be printed by print_features_names callback. */ |
| 399 | ly_bool last_one; /**< Information about whether the node is the last. */ |
| 400 | }; |
| 401 | |
| 402 | /** |
| 403 | * @brief Create struct trt_node as empty. |
| 404 | */ |
| 405 | #define TRP_EMPTY_NODE \ |
| 406 | (struct trt_node) {.status = TRD_STATUS_TYPE_EMPTY, .flags = TRD_FLAGS_TYPE_EMPTY, \ |
| 407 | .name = TRP_EMPTY_NODE_NAME, .type = TRP_EMPTY_TRT_TYPE, .iffeatures = 0, .last_one = 1} |
| 408 | |
| 409 | /** |
| 410 | * @brief Package which only groups indent and node. |
| 411 | */ |
| 412 | struct trt_pair_indent_node { |
| 413 | struct trt_indent_in_node indent; |
| 414 | struct trt_node node; |
| 415 | }; |
| 416 | |
| 417 | /** |
| 418 | * @brief Initialize struct trt_pair_indent_node by parameters. |
| 419 | */ |
| 420 | #define TRP_INIT_PAIR_INDENT_NODE(INDENT_IN_NODE, NODE) \ |
| 421 | (struct trt_pair_indent_node){.indent = INDENT_IN_NODE, .node = NODE} |
| 422 | |
| 423 | /****************************************************************************** |
| 424 | * statement |
| 425 | *****************************************************************************/ |
| 426 | |
| 427 | #define TRD_TOP_KEYWORD_MODULE "module" |
| 428 | #define TRD_TOP_KEYWORD_SUBMODULE "submodule" |
| 429 | |
| 430 | #define TRD_BODY_KEYWORD_AUGMENT "augment" |
| 431 | #define TRD_BODY_KEYWORD_RPC "rpcs" |
| 432 | #define TRD_BODY_KEYWORD_NOTIF "notifications" |
| 433 | #define TRD_BODY_KEYWORD_GROUPING "grouping" |
| 434 | #define TRD_BODY_KEYWORD_YANG_DATA "yang-data" |
| 435 | |
| 436 | /** |
| 437 | * @brief Type of the trt_keyword. |
| 438 | */ |
| 439 | typedef enum { |
| 440 | TRD_KEYWORD_EMPTY = 0, |
| 441 | TRD_KEYWORD_MODULE, |
| 442 | TRD_KEYWORD_SUBMODULE, |
| 443 | TRD_KEYWORD_AUGMENT, |
| 444 | TRD_KEYWORD_RPC, |
| 445 | TRD_KEYWORD_NOTIF, |
| 446 | TRD_KEYWORD_GROUPING, |
| 447 | TRD_KEYWORD_YANG_DATA |
| 448 | } trt_keyword_type; |
| 449 | |
| 450 | /** |
| 451 | * @brief Main sign of the tree nodes. |
| 452 | * |
| 453 | * See: TRP_EMPTY_KEYWORD_STMT, TRP_KEYWORD_STMT_IS_EMPTY |
| 454 | * trt_print_keyword_stmt_begin, trt_print_keyword_stmt_str, |
| 455 | * trt_print_keyword_stmt_end, trp_print_keyword_stmt |
| 456 | * trp_keyword_type_strlen |
| 457 | * |
| 458 | */ |
| 459 | struct trt_keyword_stmt { |
| 460 | trt_keyword_type type; /**< String containing some of the top or body keyword. */ |
| 461 | const char *str; /**< Name or path, it determines the type. */ |
| 462 | }; |
| 463 | |
| 464 | /** |
| 465 | * @brief Create struct trt_keyword_stmt as empty. |
| 466 | */ |
| 467 | #define TRP_EMPTY_KEYWORD_STMT \ |
| 468 | (struct trt_keyword_stmt) {.type = TRD_KEYWORD_EMPTY, .str = NULL} |
| 469 | |
| 470 | /** |
| 471 | * @brief Check if struct trt_keyword_stmt is empty. |
| 472 | */ |
| 473 | #define TRP_KEYWORD_STMT_IS_EMPTY(KEYWORD_TYPE) \ |
| 474 | KEYWORD_TYPE.type == TRD_KEYWORD_EMPTY |
| 475 | |
| 476 | /** |
| 477 | * @brief Initialize struct trt_keyword_stmt by parameters. |
| 478 | */ |
| 479 | #define TRP_INIT_KEYWORD_STMT(KEYWORD_TYPE, STRING) \ |
| 480 | (struct trt_keyword_stmt) {.type = KEYWORD_TYPE, .str = STRING} |
| 481 | |
| 482 | /****************************************************************************** |
| 483 | * Modify getters |
| 484 | *****************************************************************************/ |
| 485 | |
| 486 | struct trt_parent_cache; |
| 487 | |
| 488 | /** |
| 489 | * @brief Functions that change the state of the tree_ctx structure. |
| 490 | * |
| 491 | * The 'tro' functions are set here, which provide data for the 'trp' printing functions |
| 492 | * and are also called from the 'trb' browsing functions when walking through a tree. |
| 493 | * These callback functions need to be checked or reformulated |
| 494 | * if changes to the libyang library affect the printing tree. |
| 495 | * For all, if the value cannot be returned, |
| 496 | * its empty version obtained by relevant TRP_EMPTY macro is returned. |
| 497 | */ |
| 498 | struct trt_fp_modify_ctx { |
| 499 | ly_bool (*parent)(struct trt_tree_ctx *); /**< Jump to parent node. Return true if parent exists. */ |
| 500 | void (*first_sibling)(struct trt_tree_ctx *); /**< Jump on the first of the siblings. */ |
| 501 | struct trt_node (*next_sibling)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to next sibling of the current node. */ |
| 502 | struct trt_node (*next_child)(struct trt_parent_cache, struct trt_tree_ctx *); /**< Jump to the child of the current node. */ |
| 503 | struct trt_keyword_stmt (*next_augment)(struct trt_tree_ctx *); /**< Jump to the augment section. */ |
| 504 | struct trt_keyword_stmt (*get_rpcs)(struct trt_tree_ctx *); /**< Jump to the rpcs section. */ |
| 505 | struct trt_keyword_stmt (*get_notifications)(struct trt_tree_ctx *); /**< Jump to the notifications section. */ |
| 506 | struct trt_keyword_stmt (*next_grouping)(struct trt_tree_ctx *); /**< Jump to the grouping section. */ |
| 507 | struct trt_keyword_stmt (*next_yang_data)(struct trt_tree_ctx *); /**< Jump to the yang-data section. */ |
| 508 | }; |
| 509 | |
| 510 | /****************************************************************************** |
| 511 | * Read getters |
| 512 | *****************************************************************************/ |
| 513 | |
| 514 | /** |
| 515 | * @brief Functions that do not change the state of the tree_structure. |
| 516 | * |
| 517 | * For details see trt_fp_modify_ctx. |
| 518 | */ |
| 519 | struct trt_fp_read { |
| 520 | struct trt_keyword_stmt (*module_name)(const struct trt_tree_ctx *); /**< Get name of the module. */ |
| 521 | struct trt_node (*node)(struct trt_parent_cache, const struct trt_tree_ctx *); /**< Get current node. */ |
| 522 | ly_bool (*if_sibling_exists)(const struct trt_tree_ctx *); /**< Check if node's sibling exists. */ |
| 523 | }; |
| 524 | |
| 525 | /****************************************************************************** |
| 526 | * All getters |
| 527 | *****************************************************************************/ |
| 528 | |
| 529 | /** |
| 530 | * @brief A set of all necessary functions that must be provided for the printer. |
| 531 | */ |
| 532 | struct trt_fp_all { |
| 533 | struct trt_fp_modify_ctx modify; /**< Function pointers which modify state of trt_tree_ctx. */ |
| 534 | struct trt_fp_read read; /**< Function pointers which only reads state of trt_tree_ctx. */ |
| 535 | struct trt_fp_print print; /**< Functions pointers for printing special items in node. */ |
| 536 | }; |
| 537 | |
| 538 | /****************************************************************************** |
| 539 | * Printer context |
| 540 | *****************************************************************************/ |
| 541 | |
| 542 | /** |
| 543 | * @brief Main structure for trp component (printer part). |
| 544 | */ |
| 545 | struct trt_printer_ctx { |
| 546 | struct ly_out *out; /**< Handler to printing. */ |
| 547 | struct trt_fp_all fp; /**< 'tro' functions callbacks. */ |
| 548 | size_t max_line_length; /**< The maximum number of characters that can be |
| 549 | printed on one line, including the last. */ |
| 550 | }; |
| 551 | |
| 552 | /****************************************************************************** |
| 553 | * Tro functions |
| 554 | *****************************************************************************/ |
| 555 | |
| 556 | /** |
| 557 | * @brief The name of the section to which the node belongs. |
| 558 | */ |
| 559 | typedef enum { |
| 560 | TRD_SECT_MODULE = 0, /**< The node belongs to the "module: <module_name>:" label. */ |
| 561 | TRD_SECT_AUGMENT, /**< The node belongs to some "augment <target-node>:" label. */ |
| 562 | TRD_SECT_RPCS, /**< The node belongs to the "rpcs:" label. */ |
| 563 | TRD_SECT_NOTIF, /**< The node belongs to the "notifications:" label. */ |
| 564 | TRD_SECT_GROUPING, /**< The node belongs to some "grouping <grouping-name>:" label. */ |
| 565 | TRD_SECT_YANG_DATA /**< The node belongs to some "yang-data <yang-data-name>:" label. */ |
| 566 | } trt_actual_section; |
| 567 | |
| 568 | /** |
| 569 | * @brief Types of nodes that have some effect on their children. |
| 570 | */ |
| 571 | typedef enum { |
| 572 | TRD_ANCESTOR_ELSE = 0, |
| 573 | TRD_ANCESTOR_RPC_INPUT, |
| 574 | TRD_ANCESTOR_RPC_OUTPUT, |
| 575 | TRD_ANCESTOR_NOTIF |
| 576 | } trt_ancestor_type; |
| 577 | |
| 578 | /** |
| 579 | * @brief Saved information when browsing the tree downwards. |
| 580 | * |
| 581 | * This structure helps prevent frequent retrieval of information from the tree. |
| 582 | * Browsing functions (trb) are designed to preserve this structures during their recursive calls. |
| 583 | * Browsing functions (trb) do not interfere in any way with this data. |
| 584 | * This structure is used by Obtaining functions (tro) which, thanks to this structure, can return a node with the correct data. |
| 585 | * The word parent is in the name, because this data refers to the last parent and at the same time the states of its ancestors data. |
| 586 | * Only the function jumping on the child (next_child(...)) creates this structure, |
| 587 | * because the pointer to the current node moves down the tree. |
| 588 | * It's like passing the genetic code to children. |
| 589 | * Some data must be inherited and there are two approaches to this problem. |
| 590 | * Either it will always be determined which inheritance states belong to the current node |
| 591 | * (which can lead to regular travel to the root node) or the inheritance states will be stored during the recursive calls. |
| 592 | * So the problem was solved by the second option. |
| 593 | * Why does the structure contain this data? Because it walks through the lysp tree. |
| 594 | * In the future, this data may change if another type of tree (such as the lysc tree) is traversed. |
| 595 | * |
| 596 | * See: TRO_EMPTY_PARENT_CACHE, tro_parent_cache_for_child |
| 597 | */ |
| 598 | struct trt_parent_cache { |
| 599 | trt_ancestor_type ancestor; /**< Some types of nodes have a special effect on their children. */ |
| 600 | uint16_t lys_status; /**< Inherited status CURR, DEPRC, OBSLT. */ |
| 601 | uint16_t lys_config; /**< Inherited config W or R. */ |
| 602 | const struct lysp_node_list *last_list; /**< The last LYS_LIST passed. */ |
| 603 | }; |
| 604 | |
| 605 | /** |
| 606 | * @brief Return trt_parent_cache filled with default values. |
| 607 | */ |
| 608 | #define TRP_EMPTY_PARENT_CACHE \ |
| 609 | (struct trt_parent_cache) {.ancestor = TRD_ANCESTOR_ELSE, .lys_status = LYS_STATUS_CURR, \ |
| 610 | .lys_config = LYS_CONFIG_W, .last_list = NULL} |
| 611 | |
| 612 | /** |
| 613 | * @brief Main structure for browsing the libyang tree |
| 614 | */ |
| 615 | struct trt_tree_ctx { |
| 616 | trt_actual_section section; /**< To which section pn points. */ |
| 617 | const struct lys_module *module; /**< Schema tree structures. */ |
| 618 | const struct lysp_node *pn; /**< Actual pointer to parsed node. */ |
| 619 | const struct lysp_node *tpn; /**< Pointer to actual top-node. */ |
| 620 | }; |
| 621 | |
| 622 | /** |
| 623 | * @brief Used for updating trt_tree_ctx |
| 624 | */ |
| 625 | struct trt_tree_ctx_node_patch { |
| 626 | const struct lysp_node *pn; /**< Actual pointer to parsed node. */ |
| 627 | const struct lysp_node *tpn; /**< Pointer to actual top-node. */ |
| 628 | }; |
| 629 | |
| 630 | /** |
| 631 | * @brief Initialize struct trt_keyword_stmt by parameters. |
| 632 | */ |
| 633 | #define TRP_INIT_TREE_CTX_NODE_PATCH(PN, TPN) \ |
| 634 | (struct trt_tree_ctx_node_patch){.pn = PN, .tpn = TPN} |
| 635 | |
| 636 | /** Getter function for tro_lysp_node_charptr function. */ |
| 637 | typedef const char *(*trt_get_charptr_func)(const struct lysp_node *pn); |
| 638 | |
| 639 | /****************************************************************************** |
| 640 | * Definition of the general Trg functions |
| 641 | *****************************************************************************/ |
| 642 | |
| 643 | /** |
| 644 | * @brief Print a substring but limited to the maximum length. |
| 645 | * @param[in] str is pointer to source. |
| 646 | * @param[in] len is number of characters to be printed. |
| 647 | * @param[in,out] out is output handler. |
| 648 | * @return str parameter shifted by len. |
| 649 | */ |
| 650 | static const char * |
| 651 | trg_print_substr(const char *str, size_t len, struct ly_out *out) |
| 652 | { |
| 653 | for (size_t i = 0; i < len; i++) { |
| 654 | ly_print_(out, "%c", str[0]); |
| 655 | str++; |
| 656 | } |
| 657 | return str; |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * @brief Pointer is not NULL and does not point to an empty string. |
| 662 | * @param[in] str is pointer to string to be checked. |
| 663 | * @return 1 if str pointing to non empty string otherwise 0. |
| 664 | */ |
| 665 | static ly_bool |
| 666 | trg_charptr_has_data(const char *str) |
| 667 | { |
| 668 | return (str) && (str[0] != '\0'); |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * @brief Check if 'word' in 'src' is present where words are delimited by 'delim'. |
| 673 | * @param[in] src is source where words are separated by delim. |
| 674 | * @param[in] word to be searched. |
| 675 | * @param[in] delim is delimiter between words in src. |
| 676 | * @return 1 if src contains word otherwise 0. |
| 677 | */ |
| 678 | static ly_bool |
| 679 | trg_word_is_present(const char *src, const char *word, char delim) |
| 680 | { |
| 681 | const char *hit; |
| 682 | |
| 683 | if ((!src) || (src[0] == '\0') || (!word)) { |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | hit = strstr(src, word); |
| 688 | |
| 689 | if (hit) { |
| 690 | /* word was founded at the begin of src |
| 691 | * OR it match somewhere after delim |
| 692 | */ |
| 693 | if ((hit == src) || (hit[-1] == delim)) { |
| 694 | /* end of word was founded at the end of src |
| 695 | * OR end of word was match somewhere before delim |
| 696 | */ |
| 697 | char delim_or_end = (hit + strlen(word))[0]; |
| 698 | if ((delim_or_end == '\0') || (delim_or_end == delim)) { |
| 699 | return 1; |
| 700 | } |
| 701 | } |
| 702 | /* after -> hit is just substr and it's not the whole word */ |
| 703 | /* jump to the next word */ |
| 704 | for ( ; (src[0] != '\0') && (src[0] != delim); src++) {} |
| 705 | /* skip delim */ |
| 706 | src = src[0] == '\0' ? src : src + 1; |
| 707 | /* continue with searching */ |
| 708 | return trg_word_is_present(src, word, delim); |
| 709 | } else { |
| 710 | return 0; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | /****************************************************************************** |
| 715 | * Definition of printer functions |
| 716 | *****************************************************************************/ |
| 717 | |
| 718 | /** |
| 719 | * @brief Write callback for ly_out_new_clb function. |
| 720 | * |
| 721 | * @param[in] user_data is type of struct ly_out_clb_arg*. |
| 722 | * @param[in] buf contains input characters |
| 723 | * @param[in] count is number of characters in buf. |
| 724 | * @return Number of printed bytes. |
| 725 | * @return Negative value in case of error. |
| 726 | */ |
| 727 | static ssize_t |
| 728 | trp_ly_out_clb_func(void *user_data, const void *buf, size_t count) |
| 729 | { |
| 730 | LY_ERR erc = LY_SUCCESS; |
| 731 | struct ly_out_clb_arg *data = (struct ly_out_clb_arg *)user_data; |
| 732 | |
| 733 | switch (data->mode) { |
| 734 | case TRD_PRINT: |
| 735 | erc = ly_write_(data->out, buf, count); |
| 736 | break; |
| 737 | case TRD_CHAR_COUNT: |
| 738 | data->counter = data->counter + count; |
| 739 | break; |
| 740 | default: |
| 741 | break; |
| 742 | } |
| 743 | |
| 744 | if (erc != LY_SUCCESS) { |
| 745 | data->last_error = erc; |
| 746 | return -1; |
| 747 | } else { |
| 748 | return count; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * @brief Check that indent in node can be considered as equivalent. |
| 754 | * @param[in] first is the first indent in node. |
| 755 | * @param[in] second is the second indent in node. |
| 756 | * @return 1 if indents are equivalent otherwise 0. |
| 757 | */ |
| 758 | static ly_bool |
| 759 | trp_indent_in_node_are_eq(struct trt_indent_in_node first, struct trt_indent_in_node second) |
| 760 | { |
| 761 | const ly_bool a = first.type == second.type; |
| 762 | const ly_bool b = first.btw_name_opts == second.btw_name_opts; |
| 763 | const ly_bool c = first.btw_opts_type == second.btw_opts_type; |
| 764 | const ly_bool d = first.btw_type_iffeatures == second.btw_type_iffeatures; |
| 765 | |
| 766 | return a && b && c && d; |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * @brief Setting ' ' symbol because node is last sibling. |
| 771 | * @param[in] wr is wrapper over which the shift operation is to be performed. |
| 772 | * @return New shifted wrapper. |
| 773 | */ |
| 774 | static struct trt_wrapper |
| 775 | trp_wrapper_set_shift(struct trt_wrapper wr) |
| 776 | { |
| 777 | assert(wr.actual_pos < 64); |
| 778 | /* +--<node> |
| 779 | * +--<node> |
| 780 | */ |
| 781 | wr.actual_pos++; |
| 782 | return wr; |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * @brief Setting '|' symbol because node is divided or it is not last sibling. |
| 787 | * @param[in] wr is source of wrapper. |
| 788 | * @return New wrapper which is marked at actual position and shifted. |
| 789 | */ |
| 790 | static struct trt_wrapper |
| 791 | trp_wrapper_set_mark(struct trt_wrapper wr) |
| 792 | { |
| 793 | assert(wr.actual_pos < 64); |
| 794 | wr.bit_marks1 |= 1U << wr.actual_pos; |
| 795 | return trp_wrapper_set_shift(wr); |
| 796 | } |
| 797 | |
| 798 | /** |
| 799 | * @brief Setting ' ' symbol if node is last sibling otherwise set '|'. |
| 800 | * @param[in] wr is actual wrapper. |
| 801 | * @param[in] last_one is flag. Value 1 saying if the node is the last and has no more siblings. |
| 802 | * @return New wrapper for the actual node. |
| 803 | */ |
| 804 | static struct trt_wrapper |
| 805 | trp_wrapper_if_last_sibling(struct trt_wrapper wr, ly_bool last_one) |
| 806 | { |
| 807 | return last_one ? trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr); |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * @brief Test if the wrappers are equivalent. |
| 812 | * @param[in] first is the first wrapper. |
| 813 | * @param[in] second is the second wrapper. |
| 814 | * @return 1 if the wrappers are equivalent otherwise 0. |
| 815 | */ |
| 816 | static ly_bool |
| 817 | trp_wrapper_eq(struct trt_wrapper first, struct trt_wrapper second) |
| 818 | { |
| 819 | const ly_bool a = first.type == second.type; |
| 820 | const ly_bool b = first.bit_marks1 == second.bit_marks1; |
| 821 | const ly_bool c = first.actual_pos == second.actual_pos; |
| 822 | |
| 823 | return a && b && c; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * @brief Print " | " sequence on line. |
| 828 | * @param[in] wr is wrapper to be printed. |
| 829 | * @param[in,out] out is output handler. |
| 830 | */ |
| 831 | static void |
| 832 | trp_print_wrapper(struct trt_wrapper wr, struct ly_out *out) |
| 833 | { |
| 834 | uint32_t lb; |
| 835 | |
| 836 | if (wr.type == TRD_WRAPPER_TOP) { |
| 837 | lb = TRD_INDENT_LINE_BEGIN; |
| 838 | } else if (wr.type == TRD_WRAPPER_BODY) { |
| 839 | lb = TRD_INDENT_LINE_BEGIN * 2; |
| 840 | } else { |
| 841 | lb = TRD_INDENT_LINE_BEGIN; |
| 842 | } |
| 843 | |
| 844 | ly_print_(out, "%*c", lb, ' '); |
| 845 | |
| 846 | if (trp_wrapper_eq(wr, TRP_INIT_WRAPPER_TOP)) { |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | for (uint32_t i = 0; i < wr.actual_pos; i++) { |
| 851 | /** Test if the bit on the index is set. */ |
| 852 | if ((wr.bit_marks1 >> i) & 1U) { |
| 853 | ly_print_(out, "|"); |
| 854 | } else { |
| 855 | ly_print_(out, " "); |
| 856 | } |
| 857 | |
| 858 | if (i != wr.actual_pos) { |
| 859 | ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' '); |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * @brief Check if struct trt_node is empty. |
| 866 | * @param[in] node is item to test. |
| 867 | * @return 1 if node is considered empty otherwise 0. |
| 868 | */ |
| 869 | static ly_bool |
| 870 | trp_node_is_empty(struct trt_node node) |
| 871 | { |
| 872 | const ly_bool a = !node.iffeatures; |
| 873 | const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type); |
| 874 | const ly_bool c = TRP_NODE_NAME_IS_EMPTY(node.name); |
| 875 | const ly_bool d = node.flags == TRD_FLAGS_TYPE_EMPTY; |
| 876 | const ly_bool e = node.status == TRD_STATUS_TYPE_EMPTY; |
| 877 | |
| 878 | return a && b && c && d && e; |
| 879 | } |
| 880 | |
| 881 | /** |
| 882 | * @brief Check if [\<keys\>], \<type\> and \<iffeatures\> are empty/not_set. |
| 883 | * @param[in] node is item to test. |
| 884 | * @return 1 if node has no \<keys\> \<type\> or \<iffeatures\> otherwise 0. |
| 885 | */ |
| 886 | static ly_bool |
| 887 | trp_node_body_is_empty(struct trt_node node) |
| 888 | { |
| 889 | const ly_bool a = !node.iffeatures; |
| 890 | const ly_bool b = TRP_TRT_TYPE_IS_EMPTY(node.type); |
| 891 | const ly_bool c = node.name.type != TRD_NODE_KEYS; |
| 892 | |
| 893 | return a && b && c; |
| 894 | } |
| 895 | |
| 896 | /** |
| 897 | * @brief Print \<status\> of the node. |
| 898 | * @param[in] status_type is type of status. |
| 899 | * @param[in,out] out is output handler. |
| 900 | */ |
| 901 | static void |
| 902 | trp_print_status(trt_status_type status_type, struct ly_out *out) |
| 903 | { |
| 904 | switch (status_type) { |
| 905 | case TRD_STATUS_TYPE_CURRENT: |
| 906 | ly_print_(out, "%c", '+'); |
| 907 | break; |
| 908 | case TRD_STATUS_TYPE_DEPRECATED: |
| 909 | ly_print_(out, "%c", 'x'); |
| 910 | break; |
| 911 | case TRD_STATUS_TYPE_OBSOLETE: |
| 912 | ly_print_(out, "%c", 'o'); |
| 913 | break; |
| 914 | default: |
| 915 | break; |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * @brief Print \<flags\>. |
| 921 | * @param[in] flags_type is type of \<flags\>. |
| 922 | * @param[in,out] out is output handler. |
| 923 | */ |
| 924 | static void |
| 925 | trp_print_flags(trt_flags_type flags_type, struct ly_out *out) |
| 926 | { |
| 927 | switch (flags_type) { |
| 928 | case TRD_FLAGS_TYPE_RW: |
| 929 | ly_print_(out, "%s", "rw"); |
| 930 | break; |
| 931 | case TRD_FLAGS_TYPE_RO: |
| 932 | ly_print_(out, "%s", "ro"); |
| 933 | break; |
| 934 | case TRD_FLAGS_TYPE_RPC_INPUT_PARAMS: |
| 935 | ly_print_(out, "%s", "-w"); |
| 936 | break; |
| 937 | case TRD_FLAGS_TYPE_USES_OF_GROUPING: |
| 938 | ly_print_(out, "%s", "-u"); |
| 939 | break; |
| 940 | case TRD_FLAGS_TYPE_RPC: |
| 941 | ly_print_(out, "%s", "-x"); |
| 942 | break; |
| 943 | case TRD_FLAGS_TYPE_NOTIF: |
| 944 | ly_print_(out, "%s", "-n"); |
| 945 | break; |
| 946 | case TRD_FLAGS_TYPE_MOUNT_POINT: |
| 947 | ly_print_(out, "%s", "mp"); |
| 948 | break; |
| 949 | default: |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 950 | ly_print_(out, "%s", "--"); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 951 | break; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * @brief Get size of the \<flags\>. |
| 957 | * @param[in] flags_type is type of \<flags\>. |
| 958 | * @return 0 if flags_type is not set otherwise 2. |
| 959 | */ |
| 960 | static size_t |
| 961 | trp_get_flags_strlen(trt_flags_type flags_type) |
| 962 | { |
| 963 | return flags_type == TRD_FLAGS_TYPE_EMPTY ? 0 : 2; |
| 964 | } |
| 965 | |
| 966 | /** |
| 967 | * @brief Print entire struct trt_node_name structure. |
| 968 | * @param[in] node_name is item to print. |
| 969 | * @param[in,out] out is output handler. |
| 970 | */ |
| 971 | static void |
| 972 | trp_print_node_name(struct trt_node_name node_name, struct ly_out *out) |
| 973 | { |
| 974 | const char *mod_prefix; |
| 975 | const char *colon; |
| 976 | const char trd_node_name_suffix_choice[] = ")"; |
| 977 | const char trd_node_name_suffix_case[] = ")"; |
| 978 | const char trd_opts_optional[] = "?"; /**< For an optional leaf, choice, anydata, or anyxml. */ |
| 979 | const char trd_opts_container[] = "!"; /**< For a presence container. */ |
| 980 | const char trd_opts_list[] = "*"; /**< For a leaf-list or list. */ |
| 981 | const char trd_opts_slash[] = "/"; /**< For a top-level data node in a mounted module. */ |
| 982 | const char trd_opts_at_sign[] = "@"; /**< For a top-level data node of a module identified in a mount point parent reference. */ |
| 983 | |
| 984 | if (TRP_NODE_NAME_IS_EMPTY(node_name)) { |
| 985 | return; |
| 986 | } |
| 987 | |
| 988 | if (node_name.module_prefix) { |
| 989 | mod_prefix = node_name.module_prefix; |
| 990 | colon = ":"; |
| 991 | } else { |
| 992 | mod_prefix = ""; |
| 993 | colon = ""; |
| 994 | } |
| 995 | |
| 996 | switch (node_name.type) { |
| 997 | case TRD_NODE_ELSE: |
| 998 | ly_print_(out, "%s%s%s", mod_prefix, colon, node_name.str); |
| 999 | break; |
| 1000 | case TRD_NODE_CASE: |
| 1001 | ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CASE, mod_prefix, colon, node_name.str, trd_node_name_suffix_case); |
| 1002 | break; |
| 1003 | case TRD_NODE_CHOICE: |
| 1004 | ly_print_(out, "%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice); |
| 1005 | break; |
| 1006 | case TRD_NODE_OPTIONAL_CHOICE: |
| 1007 | ly_print_(out, "%s%s%s%s%s%s", TRD_NODE_NAME_PREFIX_CHOICE, mod_prefix, colon, node_name.str, trd_node_name_suffix_choice, trd_opts_optional); |
| 1008 | break; |
| 1009 | case TRD_NODE_OPTIONAL: |
| 1010 | ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_optional); |
| 1011 | break; |
| 1012 | case TRD_NODE_CONTAINER: |
| 1013 | ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_container); |
| 1014 | break; |
| 1015 | case TRD_NODE_LISTLEAFLIST: |
| 1016 | ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list); |
| 1017 | break; |
| 1018 | case TRD_NODE_KEYS: |
| 1019 | ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_list); |
| 1020 | break; |
| 1021 | case TRD_NODE_TOP_LEVEL1: |
| 1022 | ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_slash); |
| 1023 | break; |
| 1024 | case TRD_NODE_TOP_LEVEL2: |
| 1025 | ly_print_(out, "%s%s%s%s", mod_prefix, colon, node_name.str, trd_opts_at_sign); |
| 1026 | break; |
| 1027 | case TRD_NODE_TRIPLE_DOT: |
| 1028 | ly_print_(out, "%s", TRD_NODE_NAME_TRIPLE_DOT); |
| 1029 | break; |
| 1030 | default: |
| 1031 | break; |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | /** |
| 1036 | * @brief Check if mark (?, !, *, /, @) is implicitly contained in struct trt_node_name. |
| 1037 | * @param[in] node_name is structure containing the 'mark'. |
| 1038 | * @return 1 if contain otherwise 0. |
| 1039 | */ |
| 1040 | static ly_bool |
| 1041 | trp_mark_is_used(struct trt_node_name node_name) |
| 1042 | { |
| 1043 | if (TRP_NODE_NAME_IS_EMPTY(node_name)) { |
| 1044 | return 0; |
| 1045 | } |
| 1046 | |
| 1047 | switch (node_name.type) { |
| 1048 | case TRD_NODE_ELSE: |
| 1049 | case TRD_NODE_CASE: |
| 1050 | case TRD_NODE_KEYS: |
| 1051 | return 0; |
| 1052 | default: |
| 1053 | return 1; |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | /** |
| 1058 | * @brief Print opts keys. |
| 1059 | * @param[in] node_name contains type of the node with his name. |
| 1060 | * @param[in] btw_name_opts is number of spaces between name and [keys]. |
| 1061 | * @param[in] cf is basically a pointer to the function that prints the keys. |
| 1062 | * @param[in,out] out is output handler. |
| 1063 | */ |
| 1064 | static void |
| 1065 | trp_print_opts_keys(struct trt_node_name node_name, int16_t btw_name_opts, struct trt_cf_print cf, struct ly_out *out) |
| 1066 | { |
| 1067 | if (node_name.type != TRD_NODE_KEYS) { |
| 1068 | return; |
| 1069 | } |
| 1070 | |
| 1071 | /* <name><mark>___<keys>*/ |
| 1072 | if (btw_name_opts > 0) { |
| 1073 | ly_print_(out, "%*c", btw_name_opts, ' '); |
| 1074 | } |
| 1075 | ly_print_(out, "["); |
| 1076 | cf.pf(cf.ctx, out); |
| 1077 | ly_print_(out, "]"); |
| 1078 | } |
| 1079 | |
| 1080 | /** |
| 1081 | * @brief Print entire struct trt_type structure. |
| 1082 | * @param[in] type is item to print. |
| 1083 | * @param[in,out] out is output handler. |
| 1084 | */ |
| 1085 | static void |
| 1086 | trp_print_type(struct trt_type type, struct ly_out *out) |
| 1087 | { |
| 1088 | if (TRP_TRT_TYPE_IS_EMPTY(type)) { |
| 1089 | return; |
| 1090 | } |
| 1091 | |
| 1092 | switch (type.type) { |
| 1093 | case TRD_TYPE_NAME: |
| 1094 | ly_print_(out, "%s", type.str); |
| 1095 | break; |
| 1096 | case TRD_TYPE_TARGET: |
| 1097 | ly_print_(out, "-> %s", type.str); |
| 1098 | break; |
| 1099 | case TRD_TYPE_LEAFREF: |
| 1100 | ly_print_(out, "leafref"); |
| 1101 | default: |
| 1102 | break; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * @brief Print all iffeatures of node |
| 1108 | * |
| 1109 | * @param[in] iffeature_flag contains if if-features is present. |
| 1110 | * @param[in] cf is basically a pointer to the function that prints the list of features. |
| 1111 | * @param[in,out] out is output handler. |
| 1112 | */ |
| 1113 | static void |
| 1114 | trp_print_iffeatures(ly_bool iffeature_flag, struct trt_cf_print cf, struct ly_out *out) |
| 1115 | { |
| 1116 | if (iffeature_flag) { |
| 1117 | ly_print_(out, "{"); |
| 1118 | cf.pf(cf.ctx, out); |
| 1119 | ly_print_(out, "}?"); |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | /** |
| 1124 | * @brief Print just \<status\>--\<flags\> \<name\> with opts mark. |
| 1125 | * @param[in] node contains items to print. |
| 1126 | * @param[in] out is output handler. |
| 1127 | */ |
| 1128 | static void |
| 1129 | trp_print_node_up_to_name(struct trt_node node, struct ly_out *out) |
| 1130 | { |
| 1131 | if (node.name.type == TRD_NODE_TRIPLE_DOT) { |
| 1132 | trp_print_node_name(node.name, out); |
| 1133 | return; |
| 1134 | } |
| 1135 | /* <status>--<flags> */ |
| 1136 | trp_print_status(node.status, out); |
| 1137 | ly_print_(out, "--"); |
| 1138 | /* If the node is a case node, there is no space before the <name> */ |
| 1139 | /* also case node has no flags. */ |
| 1140 | if (node.name.type != TRD_NODE_CASE) { |
| 1141 | trp_print_flags(node.flags, out); |
| 1142 | ly_print_(out, " "); |
| 1143 | } |
| 1144 | /* <name> */ |
| 1145 | trp_print_node_name(node.name, out); |
| 1146 | } |
| 1147 | |
| 1148 | /** |
| 1149 | * @brief Print alignment (spaces) instead of \<status\>--\<flags\> \<name\> for divided node. |
| 1150 | * @param[in] node contains items to print. |
| 1151 | * @param[in] out is output handler. |
| 1152 | */ |
| 1153 | static void |
| 1154 | trp_print_divided_node_up_to_name(struct trt_node node, struct ly_out *out) |
| 1155 | { |
| 1156 | uint32_t space = trp_get_flags_strlen(node.flags); |
| 1157 | |
| 1158 | if (node.name.type == TRD_NODE_CASE) { |
| 1159 | /* :(<name> */ |
| 1160 | space += strlen(TRD_NODE_NAME_PREFIX_CASE); |
| 1161 | } else if (node.name.type == TRD_NODE_CHOICE) { |
| 1162 | /* (<name> */ |
| 1163 | space += strlen(TRD_NODE_NAME_PREFIX_CHOICE); |
| 1164 | } else { |
| 1165 | /* _<name> */ |
| 1166 | space += strlen(" "); |
| 1167 | } |
| 1168 | |
| 1169 | /* <name> |
| 1170 | * __ |
| 1171 | */ |
| 1172 | space += TRD_INDENT_LONG_LINE_BREAK; |
| 1173 | |
| 1174 | ly_print_(out, "%*c", space, ' '); |
| 1175 | } |
| 1176 | |
| 1177 | /** |
| 1178 | * @brief Print struct trt_node structure. |
| 1179 | * @param[in] node is item to print. |
| 1180 | * @param[in] pck package of functions for printing [\<keys\>] and \<iffeatures\>. |
| 1181 | * @param[in] indent is the indent in node. |
| 1182 | * @param[in,out] out is output handler. |
| 1183 | */ |
| 1184 | static void |
| 1185 | trp_print_node(struct trt_node node, struct trt_pck_print pck, struct trt_indent_in_node indent, struct ly_out *out) |
| 1186 | { |
| 1187 | ly_bool triple_dot; |
| 1188 | ly_bool divided; |
| 1189 | struct trt_cf_print cf_print_keys; |
| 1190 | struct trt_cf_print cf_print_iffeatures; |
| 1191 | |
| 1192 | if (trp_node_is_empty(node)) { |
| 1193 | return; |
| 1194 | } |
| 1195 | |
| 1196 | /* <status>--<flags> <name><opts> <type> <if-features> */ |
| 1197 | triple_dot = node.name.type == TRD_NODE_TRIPLE_DOT; |
| 1198 | divided = indent.type == TRD_INDENT_IN_NODE_DIVIDED; |
| 1199 | |
| 1200 | if (triple_dot) { |
| 1201 | trp_print_node_name(node.name, out); |
| 1202 | return; |
| 1203 | } else if (!divided) { |
| 1204 | trp_print_node_up_to_name(node, out); |
| 1205 | } else { |
| 1206 | trp_print_divided_node_up_to_name(node, out); |
| 1207 | } |
| 1208 | |
| 1209 | /* <opts> */ |
| 1210 | /* <name>___<opts>*/ |
| 1211 | cf_print_keys.ctx = pck.tree_ctx; |
| 1212 | cf_print_keys.pf = pck.fps.print_keys; |
| 1213 | |
| 1214 | trp_print_opts_keys(node.name, indent.btw_name_opts, cf_print_keys, out); |
| 1215 | |
| 1216 | /* <opts>__<type> */ |
| 1217 | if (indent.btw_opts_type > 0) { |
| 1218 | ly_print_(out, "%*c", indent.btw_opts_type, ' '); |
| 1219 | } |
| 1220 | |
| 1221 | /* <type> */ |
| 1222 | trp_print_type(node.type, out); |
| 1223 | |
| 1224 | /* <type>__<iffeatures> */ |
| 1225 | if (indent.btw_type_iffeatures > 0) { |
| 1226 | ly_print_(out, "%*c", indent.btw_type_iffeatures, ' '); |
| 1227 | } |
| 1228 | |
| 1229 | /* <iffeatures> */ |
| 1230 | cf_print_iffeatures.ctx = pck.tree_ctx; |
| 1231 | cf_print_iffeatures.pf = pck.fps.print_features_names; |
| 1232 | |
| 1233 | trp_print_iffeatures(node.iffeatures, cf_print_iffeatures, out); |
| 1234 | } |
| 1235 | |
| 1236 | /** |
| 1237 | * @brief Print .keyword based on .type. |
| 1238 | * @param[in] ks is keyword statement to print. |
| 1239 | * @param[in,out] out is output handler |
| 1240 | */ |
| 1241 | static void |
| 1242 | trt_print_keyword_stmt_begin(struct trt_keyword_stmt ks, struct ly_out *out) |
| 1243 | { |
| 1244 | switch (ks.type) { |
| 1245 | case TRD_KEYWORD_MODULE: |
| 1246 | ly_print_(out, "%s: ", TRD_TOP_KEYWORD_MODULE); |
| 1247 | return; |
| 1248 | case TRD_KEYWORD_SUBMODULE: |
| 1249 | ly_print_(out, "%s: ", TRD_TOP_KEYWORD_SUBMODULE); |
| 1250 | return; |
| 1251 | default: |
| 1252 | ly_print_(out, "%*c", TRD_INDENT_LINE_BEGIN, ' '); |
| 1253 | switch (ks.type) { |
| 1254 | case TRD_KEYWORD_AUGMENT: |
| 1255 | ly_print_(out, "%s ", TRD_BODY_KEYWORD_AUGMENT); |
| 1256 | break; |
| 1257 | case TRD_KEYWORD_RPC: |
| 1258 | ly_print_(out, "%s", TRD_BODY_KEYWORD_RPC); |
| 1259 | break; |
| 1260 | case TRD_KEYWORD_NOTIF: |
| 1261 | ly_print_(out, "%s", TRD_BODY_KEYWORD_NOTIF); |
| 1262 | break; |
| 1263 | case TRD_KEYWORD_GROUPING: |
| 1264 | ly_print_(out, "%s ", TRD_BODY_KEYWORD_GROUPING); |
| 1265 | break; |
| 1266 | case TRD_KEYWORD_YANG_DATA: |
| 1267 | ly_print_(out, "%s ", TRD_BODY_KEYWORD_YANG_DATA); |
| 1268 | break; |
| 1269 | default: |
| 1270 | break; |
| 1271 | } |
| 1272 | break; |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | /** |
| 1277 | * @brief Get string length of stored keyword. |
| 1278 | * @param[in] type is type of the keyword statement. |
| 1279 | * @return length of the keyword statement name. |
| 1280 | */ |
| 1281 | static size_t |
| 1282 | trp_keyword_type_strlen(trt_keyword_type type) |
| 1283 | { |
| 1284 | switch (type) { |
| 1285 | case TRD_KEYWORD_MODULE: |
| 1286 | return sizeof(TRD_TOP_KEYWORD_MODULE) - 1; |
| 1287 | case TRD_KEYWORD_SUBMODULE: |
| 1288 | return sizeof(TRD_TOP_KEYWORD_SUBMODULE) - 1; |
| 1289 | case TRD_KEYWORD_AUGMENT: |
| 1290 | return sizeof(TRD_BODY_KEYWORD_AUGMENT) - 1; |
| 1291 | case TRD_KEYWORD_RPC: |
| 1292 | return sizeof(TRD_BODY_KEYWORD_RPC) - 1; |
| 1293 | case TRD_KEYWORD_NOTIF: |
| 1294 | return sizeof(TRD_BODY_KEYWORD_NOTIF) - 1; |
| 1295 | case TRD_KEYWORD_GROUPING: |
| 1296 | return sizeof(TRD_BODY_KEYWORD_GROUPING) - 1; |
| 1297 | case TRD_KEYWORD_YANG_DATA: |
| 1298 | return sizeof(TRD_BODY_KEYWORD_YANG_DATA) - 1; |
| 1299 | default: |
| 1300 | return 0; |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | /** |
| 1305 | * @brief Print .str which is string of name or path. |
| 1306 | * @param[in] ks is keyword statement structure. |
| 1307 | * @param[in] mll is max line length. |
| 1308 | * @param[in,out] out is output handler. |
| 1309 | */ |
| 1310 | static void |
| 1311 | trt_print_keyword_stmt_str(struct trt_keyword_stmt ks, size_t mll, struct ly_out *out) |
| 1312 | { |
| 1313 | uint32_t ind_initial; |
| 1314 | uint32_t ind_divided; |
| 1315 | /* flag if path must be splitted to more lines */ |
| 1316 | ly_bool linebreak_was_set; |
| 1317 | /* flag if at least one subpath was printed */ |
| 1318 | ly_bool subpath_printed; |
| 1319 | /* the sum of the sizes of the substrings on the current line */ |
| 1320 | uint32_t how_far; |
| 1321 | /* pointer to start of the subpath */ |
| 1322 | const char *sub_ptr; |
| 1323 | /* size of subpath from sub_ptr */ |
| 1324 | size_t sub_len; |
| 1325 | |
| 1326 | if ((!ks.str) || (ks.str[0] == '\0')) { |
| 1327 | return; |
| 1328 | } |
| 1329 | |
| 1330 | /* module name cannot be splitted */ |
| 1331 | if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) { |
| 1332 | ly_print_(out, "%s", ks.str); |
| 1333 | return; |
| 1334 | } |
| 1335 | |
| 1336 | /* after -> for trd_keyword_stmt_body do */ |
| 1337 | |
| 1338 | /* set begin indentation */ |
| 1339 | ind_initial = TRD_INDENT_LINE_BEGIN + trp_keyword_type_strlen(ks.type) + 1; |
| 1340 | ind_divided = ind_initial + TRD_INDENT_LONG_LINE_BREAK; |
| 1341 | linebreak_was_set = 0; |
| 1342 | subpath_printed = 0; |
| 1343 | how_far = 0; |
| 1344 | sub_ptr = ks.str; |
| 1345 | sub_len = 0; |
| 1346 | |
| 1347 | while (sub_ptr[0] != '\0') { |
| 1348 | uint32_t ind; |
| 1349 | /* skip slash */ |
| 1350 | const char *tmp = sub_ptr[0] == '/' ? sub_ptr + 1 : sub_ptr; |
| 1351 | /* get position of the end of substr */ |
| 1352 | tmp = strchr(tmp, '/'); |
| 1353 | /* set correct size if this is a last substring */ |
| 1354 | sub_len = !tmp ? strlen(sub_ptr) : (size_t)(tmp - sub_ptr); |
| 1355 | /* actualize sum of the substring's sizes on the current line */ |
| 1356 | how_far += sub_len; |
| 1357 | /* correction due to colon character if it this is last substring */ |
| 1358 | how_far = *(sub_ptr + sub_len) == '\0' ? how_far + 1 : how_far; |
| 1359 | /* choose indentation which depends on |
| 1360 | * whether the string is printed on multiple lines or not |
| 1361 | */ |
| 1362 | ind = linebreak_was_set ? ind_divided : ind_initial; |
| 1363 | if (ind + how_far <= mll) { |
| 1364 | /* printing before max line length */ |
| 1365 | sub_ptr = trg_print_substr(sub_ptr, sub_len, out); |
| 1366 | subpath_printed = 1; |
| 1367 | } else { |
| 1368 | /* printing on new line */ |
| 1369 | if (subpath_printed == 0) { |
| 1370 | /* first subpath is too long but print it at first line anyway */ |
| 1371 | sub_ptr = trg_print_substr(sub_ptr, sub_len, out); |
| 1372 | subpath_printed = 1; |
| 1373 | continue; |
| 1374 | } |
| 1375 | ly_print_(out, "\n"); |
| 1376 | ly_print_(out, "%*c", ind_divided, ' '); |
| 1377 | linebreak_was_set = 1; |
| 1378 | sub_ptr = trg_print_substr(sub_ptr, sub_len, out); |
| 1379 | how_far = sub_len; |
| 1380 | subpath_printed = 1; |
| 1381 | } |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | /** |
| 1386 | * @brief Print separator based on .type. |
| 1387 | * @param[in] ks is keyword statement structure. |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 1388 | * @param[in] grp_has_data is flag only for grouping section. |
| 1389 | * Set to 1 if grouping section has some nodes. |
| 1390 | * Set to 0 if it doesn't have nodes or it's not grouping section. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1391 | * @param[in,out] out is output handler. |
| 1392 | */ |
| 1393 | static void |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 1394 | trt_print_keyword_stmt_end(struct trt_keyword_stmt ks, ly_bool grp_has_data, struct ly_out *out) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1395 | { |
| 1396 | if ((ks.type != TRD_KEYWORD_MODULE) && (ks.type != TRD_KEYWORD_SUBMODULE)) { |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 1397 | if ((ks.type == TRD_KEYWORD_GROUPING) && !grp_has_data) { |
| 1398 | return; |
| 1399 | } else { |
| 1400 | ly_print_(out, ":"); |
| 1401 | } |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | /** |
| 1406 | * @brief Print entire struct trt_keyword_stmt structure. |
| 1407 | * @param[in] ks is item to print. |
| 1408 | * @param[in] mll is max line length. |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 1409 | * @param[in] grp_has_data is flag only for grouping section. |
| 1410 | * Set to 1 if grouping section has some nodes. |
| 1411 | * Set to 0 if it doesn't have nodes or it's not grouping section. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1412 | * @param[in,out] out is output handler. |
| 1413 | */ |
| 1414 | static void |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 1415 | trp_print_keyword_stmt(struct trt_keyword_stmt ks, size_t mll, ly_bool grp_has_data, struct ly_out *out) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1416 | { |
| 1417 | if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) { |
| 1418 | return; |
| 1419 | } |
| 1420 | trt_print_keyword_stmt_begin(ks, out); |
| 1421 | trt_print_keyword_stmt_str(ks, mll, out); |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 1422 | trt_print_keyword_stmt_end(ks, grp_has_data, out); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | /****************************************************************************** |
| 1426 | * Main trp functions |
| 1427 | *****************************************************************************/ |
| 1428 | |
| 1429 | /** |
| 1430 | * @brief Printing one line including wrapper and node which can be incomplete (divided). |
| 1431 | * @param[in] node is \<node\> representation. |
| 1432 | * @param[in] pck contains special printing functions callback. |
| 1433 | * @param[in] indent contains wrapper and indent in node numbers. |
| 1434 | * @param[in,out] out is output handler. |
| 1435 | */ |
| 1436 | static void |
| 1437 | trp_print_line(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, struct ly_out *out) |
| 1438 | { |
| 1439 | trp_print_wrapper(indent.wrapper, out); |
| 1440 | trp_print_node(node, pck, indent.in_node, out); |
| 1441 | } |
| 1442 | |
| 1443 | /** |
| 1444 | * @brief Printing one line including wrapper and \<status\>--\<flags\> \<name\>\<option_mark\>. |
| 1445 | * @param[in] node is \<node\> representation. |
| 1446 | * @param[in] wr is wrapper for printing indentation before node. |
| 1447 | * @param[in] out is output handler. |
| 1448 | */ |
| 1449 | static void |
| 1450 | trp_print_line_up_to_node_name(struct trt_node node, struct trt_wrapper wr, struct ly_out *out) |
| 1451 | { |
| 1452 | trp_print_wrapper(wr, out); |
| 1453 | trp_print_node_up_to_name(node, out); |
| 1454 | } |
| 1455 | |
| 1456 | /** |
| 1457 | * @brief Check if leafref target must be change to string 'leafref' because his target string is too long. |
| 1458 | * @param[in] node containing leafref target. |
| 1459 | * @param[in] wr is wrapper for printing indentation before node. |
| 1460 | * @param[in] mll is max line length. |
| 1461 | * @param[in] out is output handler. |
| 1462 | * @return true if leafref must be changed to string 'leafref'. |
| 1463 | */ |
| 1464 | static ly_bool |
| 1465 | trp_leafref_target_is_too_long(struct trt_node node, struct trt_wrapper wr, size_t mll, struct ly_out *out) |
| 1466 | { |
| 1467 | struct ly_out_clb_arg *data; |
| 1468 | |
| 1469 | if (node.type.type != TRD_TYPE_TARGET) { |
| 1470 | return 0; |
| 1471 | } |
| 1472 | |
| 1473 | /* set ly_out to counting characters */ |
| 1474 | data = out->method.clb.arg; |
| 1475 | |
| 1476 | data->counter = 0; |
| 1477 | data->mode = TRD_CHAR_COUNT; |
| 1478 | /* count number of printed bytes */ |
| 1479 | trp_print_wrapper(wr, out); |
| 1480 | ly_print_(out, "%*c", TRD_INDENT_BTW_SIBLINGS, ' '); |
| 1481 | trp_print_divided_node_up_to_name(node, out); |
| 1482 | data->mode = TRD_PRINT; |
| 1483 | |
| 1484 | return data->counter + strlen(node.type.str) > mll; |
| 1485 | } |
| 1486 | |
| 1487 | /** |
| 1488 | * @brief Get default indent in node based on node values. |
| 1489 | * @param[in] node is \<node\> representation. |
| 1490 | * @return Default indent in node assuming that the node will not be divided. |
| 1491 | */ |
| 1492 | static struct trt_indent_in_node |
| 1493 | trp_default_indent_in_node(struct trt_node node) |
| 1494 | { |
| 1495 | struct trt_indent_in_node ret; |
| 1496 | |
| 1497 | ret.type = TRD_INDENT_IN_NODE_NORMAL; |
| 1498 | |
| 1499 | /* btw_name_opts */ |
| 1500 | ret.btw_name_opts = node.name.type == TRD_NODE_KEYS ? TRD_INDENT_BEFORE_KEYS : 0; |
| 1501 | |
| 1502 | /* btw_opts_type */ |
| 1503 | if (!(TRP_TRT_TYPE_IS_EMPTY(node.type))) { |
| 1504 | ret.btw_opts_type = trp_mark_is_used(node.name) ? |
| 1505 | TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH : |
| 1506 | TRD_INDENT_BEFORE_TYPE; |
| 1507 | } else { |
| 1508 | ret.btw_opts_type = 0; |
| 1509 | } |
| 1510 | |
| 1511 | /* btw_type_iffeatures */ |
| 1512 | ret.btw_type_iffeatures = node.iffeatures ? TRD_INDENT_BEFORE_IFFEATURES : 0; |
| 1513 | |
| 1514 | return ret; |
| 1515 | } |
| 1516 | |
| 1517 | /** |
| 1518 | * @brief Setting linebreaks in trt_indent_in_node. |
| 1519 | * |
| 1520 | * The order where the linebreak tag can be placed is from the end. |
| 1521 | * |
| 1522 | * @param[in] indent containing alignment lengths or already linebreak marks. |
| 1523 | * @return indent with a newly placed linebreak tag. |
| 1524 | * @return .type set to TRD_INDENT_IN_NODE_FAILED if it is not possible to place a more linebreaks. |
| 1525 | */ |
| 1526 | static struct trt_indent_in_node |
| 1527 | trp_indent_in_node_place_break(struct trt_indent_in_node indent) |
| 1528 | { |
| 1529 | /* somewhere must be set a line break in node */ |
| 1530 | struct trt_indent_in_node ret = indent; |
| 1531 | |
| 1532 | /* gradually break the node from the end */ |
| 1533 | if ((indent.btw_type_iffeatures != TRD_LINEBREAK) && (indent.btw_type_iffeatures != 0)) { |
| 1534 | ret.btw_type_iffeatures = TRD_LINEBREAK; |
| 1535 | } else if ((indent.btw_opts_type != TRD_LINEBREAK) && (indent.btw_opts_type != 0)) { |
| 1536 | ret.btw_opts_type = TRD_LINEBREAK; |
| 1537 | } else if ((indent.btw_name_opts != TRD_LINEBREAK) && (indent.btw_name_opts != 0)) { |
| 1538 | /* set line break between name and opts */ |
| 1539 | ret.btw_name_opts = TRD_LINEBREAK; |
| 1540 | } else { |
| 1541 | /* it is not possible to place a more line breaks, |
| 1542 | * unfortunately the max_line_length constraint is violated |
| 1543 | */ |
| 1544 | ret.type = TRD_INDENT_IN_NODE_FAILED; |
| 1545 | } |
| 1546 | return ret; |
| 1547 | } |
| 1548 | |
| 1549 | /** |
| 1550 | * @brief Get the first half of the node based on the linebreak mark. |
| 1551 | * |
| 1552 | * Items in the second half of the node will be empty. |
| 1553 | * |
| 1554 | * @param[in] node the whole \<node\> to be split. |
| 1555 | * @param[in] indent contains information in which part of the \<node\> the first half ends. |
| 1556 | * @return first half of the node, indent is unchanged. |
| 1557 | */ |
| 1558 | static struct trt_pair_indent_node |
| 1559 | trp_first_half_node(struct trt_node node, struct trt_indent_in_node indent) |
| 1560 | { |
| 1561 | struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node); |
| 1562 | |
| 1563 | if (indent.btw_name_opts == TRD_LINEBREAK) { |
| 1564 | ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type; |
| 1565 | ret.node.type = TRP_EMPTY_TRT_TYPE; |
| 1566 | ret.node.iffeatures = 0; |
| 1567 | } else if (indent.btw_opts_type == TRD_LINEBREAK) { |
| 1568 | ret.node.type = TRP_EMPTY_TRT_TYPE; |
| 1569 | ret.node.iffeatures = 0; |
| 1570 | } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) { |
| 1571 | ret.node.iffeatures = 0; |
| 1572 | } |
| 1573 | |
| 1574 | return ret; |
| 1575 | } |
| 1576 | |
| 1577 | /** |
| 1578 | * @brief Get the second half of the node based on the linebreak mark. |
| 1579 | * |
| 1580 | * Items in the first half of the node will be empty. |
| 1581 | * Indentations belonging to the first node will be reset to zero. |
| 1582 | * |
| 1583 | * @param[in] node the whole \<node\> to be split. |
| 1584 | * @param[in] indent contains information in which part of the \<node\> the second half starts. |
| 1585 | * @return second half of the node, indent is newly set. |
| 1586 | */ |
| 1587 | static struct trt_pair_indent_node |
| 1588 | trp_second_half_node(struct trt_node node, struct trt_indent_in_node indent) |
| 1589 | { |
| 1590 | struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent, node); |
| 1591 | |
| 1592 | if (indent.btw_name_opts < 0) { |
| 1593 | /* Logically, the information up to token <opts> should be deleted, |
| 1594 | * but the the trp_print_node function needs it to create |
| 1595 | * the correct indent. |
| 1596 | */ |
| 1597 | ret.indent.btw_name_opts = 0; |
| 1598 | ret.indent.btw_opts_type = TRP_TRT_TYPE_IS_EMPTY(node.type) ? 0 : TRD_INDENT_BEFORE_TYPE; |
| 1599 | ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES; |
| 1600 | } else if (indent.btw_opts_type == TRD_LINEBREAK) { |
| 1601 | ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type; |
| 1602 | ret.indent.btw_name_opts = 0; |
| 1603 | ret.indent.btw_opts_type = 0; |
| 1604 | ret.indent.btw_type_iffeatures = !node.iffeatures ? 0 : TRD_INDENT_BEFORE_IFFEATURES; |
| 1605 | } else if (indent.btw_type_iffeatures == TRD_LINEBREAK) { |
| 1606 | ret.node.name.type = node.name.type == TRD_NODE_KEYS ? TRD_NODE_LISTLEAFLIST : node.name.type; |
| 1607 | ret.node.type = TRP_EMPTY_TRT_TYPE; |
| 1608 | ret.indent.btw_name_opts = 0; |
| 1609 | ret.indent.btw_opts_type = 0; |
| 1610 | ret.indent.btw_type_iffeatures = 0; |
| 1611 | } |
| 1612 | return ret; |
| 1613 | } |
| 1614 | |
| 1615 | /** |
| 1616 | * @brief Get the correct alignment for the node. |
| 1617 | * |
| 1618 | * This function is recursively called itself. |
| 1619 | * It's like a backend function for a function trp_try_normal_indent_in_node. |
| 1620 | * |
| 1621 | * @param[in] node is \<node\> representation. |
| 1622 | * @param[in] pck contains speciall callback functions for printing. |
| 1623 | * @param[in] indent contains wrapper and indent in node numbers. |
| 1624 | * @param[in] mll is max line length. |
| 1625 | * @param[in,out] cnt counting number of characters to print. |
| 1626 | * @param[in,out] out is output handler. |
| 1627 | * @return pair of node and indentation numbers of that node. |
| 1628 | */ |
| 1629 | static struct trt_pair_indent_node |
| 1630 | trp_try_normal_indent_in_node_(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, size_t mll, size_t *cnt, struct ly_out *out) |
| 1631 | { |
| 1632 | struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node); |
| 1633 | |
| 1634 | trp_print_line(node, pck, indent, out); |
| 1635 | |
| 1636 | if (*cnt <= mll) { |
| 1637 | /* success */ |
| 1638 | return ret; |
| 1639 | } else { |
| 1640 | ret.indent = trp_indent_in_node_place_break(ret.indent); |
| 1641 | if (ret.indent.type != TRD_INDENT_IN_NODE_FAILED) { |
| 1642 | /* erase information in node due to line break */ |
| 1643 | ret = trp_first_half_node(node, ret.indent); |
| 1644 | /* check if line fits, recursive call */ |
| 1645 | *cnt = 0; |
| 1646 | ret = trp_try_normal_indent_in_node_(ret.node, pck, TRP_INIT_PCK_INDENT(indent.wrapper, ret.indent), mll, cnt, out); |
| 1647 | /* make sure that the result will be with the status divided |
| 1648 | * or eventually with status failed */ |
| 1649 | ret.indent.type = ret.indent.type == TRD_INDENT_IN_NODE_FAILED ? TRD_INDENT_IN_NODE_FAILED : TRD_INDENT_IN_NODE_DIVIDED; |
| 1650 | } |
| 1651 | return ret; |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | /** |
| 1656 | * @brief Get the correct alignment for the node. |
| 1657 | * |
| 1658 | * @param[in] node is \<node\> representation. |
| 1659 | * @param[in] pck contains speciall callback functions for printing. |
| 1660 | * @param[in] indent contains wrapper and indent in node numbers. |
| 1661 | * @param[in] mll is max line length. |
| 1662 | * @param[in,out] out is output handler. |
| 1663 | * @return .type == TRD_INDENT_IN_NODE_DIVIDED - the node does not fit in the line, some indent variable has negative value as a line break sign. |
| 1664 | * @return .type == TRD_INDENT_IN_NODE_NORMAL - the node fits into the line, all indent variables values has non-negative number. |
| 1665 | * @return .type == TRD_INDENT_IN_NODE_FAILED - the node does not fit into the line, all indent variables has negative or zero values, function failed. |
| 1666 | */ |
| 1667 | static struct trt_pair_indent_node |
| 1668 | trp_try_normal_indent_in_node(struct trt_node node, struct trt_pck_print pck, struct trt_pck_indent indent, size_t mll, struct ly_out *out) |
| 1669 | { |
| 1670 | struct trt_pair_indent_node ret = TRP_INIT_PAIR_INDENT_NODE(indent.in_node, node); |
| 1671 | struct ly_out_clb_arg *data; |
| 1672 | |
| 1673 | /* set ly_out to counting characters */ |
| 1674 | data = out->method.clb.arg; |
| 1675 | |
| 1676 | data->counter = 0; |
| 1677 | data->mode = TRD_CHAR_COUNT; |
| 1678 | ret = trp_try_normal_indent_in_node_(node, pck, indent, mll, &data->counter, out); |
| 1679 | data->mode = TRD_PRINT; |
| 1680 | |
| 1681 | return ret; |
| 1682 | } |
| 1683 | |
| 1684 | /** |
| 1685 | * @brief Auxiliary function for trp_print_entire_node that prints split nodes. |
| 1686 | * @param[in] node is node representation. |
| 1687 | * @param[in] ppck contains speciall callback functions for printing. |
| 1688 | * @param[in] ipck contains wrapper and indent in node numbers. |
| 1689 | * @param[in] mll is max line length. |
| 1690 | * @param[in,out] out is output handler. |
| 1691 | */ |
| 1692 | static void |
| 1693 | trp_print_divided_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out) |
| 1694 | { |
| 1695 | ly_bool entire_node_was_printed; |
| 1696 | struct trt_pair_indent_node ind_node = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out); |
| 1697 | |
| 1698 | if (ind_node.indent.type == TRD_INDENT_IN_NODE_FAILED) { |
| 1699 | /* nothing can be done, continue as usual */ |
| 1700 | ind_node.indent.type = TRD_INDENT_IN_NODE_DIVIDED; |
| 1701 | } |
| 1702 | |
| 1703 | trp_print_line(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), out); |
| 1704 | entire_node_was_printed = trp_indent_in_node_are_eq(ipck.in_node, ind_node.indent); |
| 1705 | |
| 1706 | if (!entire_node_was_printed) { |
| 1707 | ly_print_(out, "\n"); |
| 1708 | /* continue with second half node */ |
| 1709 | ind_node = trp_second_half_node(node, ind_node.indent); |
| 1710 | /* continue with printing node */ |
| 1711 | trp_print_divided_node(ind_node.node, ppck, TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node.indent), mll, out); |
| 1712 | } else { |
| 1713 | return; |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | /** |
| 1718 | * @brief Printing of the wrapper and the whole node, which can be divided into several lines. |
| 1719 | * @param[in] node is node representation. |
| 1720 | * @param[in] ppck contains speciall callback functions for printing. |
| 1721 | * @param[in] ipck contains wrapper and indent in node numbers. |
| 1722 | * @param[in] mll is max line length. |
| 1723 | * @param[in,out] out is output handler. |
| 1724 | */ |
| 1725 | static void |
| 1726 | trp_print_entire_node(struct trt_node node, struct trt_pck_print ppck, struct trt_pck_indent ipck, size_t mll, struct ly_out *out) |
| 1727 | { |
| 1728 | struct trt_pair_indent_node ind_node1; |
| 1729 | struct trt_pair_indent_node ind_node2; |
| 1730 | struct trt_pck_indent tmp; |
| 1731 | |
| 1732 | if (trp_leafref_target_is_too_long(node, ipck.wrapper, mll, out)) { |
| 1733 | node.type.type = TRD_TYPE_LEAFREF; |
| 1734 | } |
| 1735 | |
| 1736 | /* check if normal indent is possible */ |
| 1737 | ind_node1 = trp_try_normal_indent_in_node(node, ppck, ipck, mll, out); |
| 1738 | |
| 1739 | if (ind_node1.indent.type == TRD_INDENT_IN_NODE_NORMAL) { |
| 1740 | /* node fits to one line */ |
| 1741 | trp_print_line(node, ppck, ipck, out); |
| 1742 | } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_DIVIDED) { |
| 1743 | /* node will be divided */ |
| 1744 | /* print first half */ |
| 1745 | tmp = TRP_INIT_PCK_INDENT(ipck.wrapper, ind_node1.indent); |
| 1746 | /* pretend that this is normal node */ |
| 1747 | tmp.in_node.type = TRD_INDENT_IN_NODE_NORMAL; |
| 1748 | |
| 1749 | trp_print_line(ind_node1.node, ppck, tmp, out); |
| 1750 | ly_print_(out, "\n"); |
| 1751 | |
| 1752 | /* continue with second half on new line */ |
| 1753 | ind_node2 = trp_second_half_node(node, ind_node1.indent); |
| 1754 | tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent); |
| 1755 | |
| 1756 | trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out); |
| 1757 | } else if (ind_node1.indent.type == TRD_INDENT_IN_NODE_FAILED) { |
| 1758 | /* node name is too long */ |
| 1759 | trp_print_line_up_to_node_name(node, ipck.wrapper, out); |
| 1760 | |
| 1761 | if (trp_node_body_is_empty(node)) { |
| 1762 | return; |
| 1763 | } else { |
| 1764 | ly_print_(out, "\n"); |
| 1765 | |
| 1766 | ind_node2 = trp_second_half_node(node, ind_node1.indent); |
| 1767 | ind_node2.indent.type = TRD_INDENT_IN_NODE_DIVIDED; |
| 1768 | tmp = TRP_INIT_PCK_INDENT(trp_wrapper_if_last_sibling(ipck.wrapper, node.last_one), ind_node2.indent); |
| 1769 | |
| 1770 | trp_print_divided_node(ind_node2.node, ppck, tmp, mll, out); |
| 1771 | } |
| 1772 | |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | /****************************************************************************** |
| 1777 | * Definition of Tro reading functions |
| 1778 | *****************************************************************************/ |
| 1779 | |
| 1780 | /** |
| 1781 | * @brief Get new trt_parent_cache if we apply the transfer to the child node in the tree. |
| 1782 | * @param[in] ca is parent cache for current node. |
| 1783 | * @param[in] pn is pointer to the current tree node. |
| 1784 | * @return Cache for the current node. |
| 1785 | */ |
| 1786 | static struct trt_parent_cache |
| 1787 | tro_parent_cache_for_child(struct trt_parent_cache ca, const struct lysp_node *pn) |
| 1788 | { |
| 1789 | struct trt_parent_cache ret; |
| 1790 | |
| 1791 | ret.ancestor = |
| 1792 | pn->nodetype & (LYS_INPUT) ? TRD_ANCESTOR_RPC_INPUT : |
| 1793 | pn->nodetype & (LYS_OUTPUT) ? TRD_ANCESTOR_RPC_OUTPUT : |
| 1794 | pn->nodetype & (LYS_NOTIF) ? TRD_ANCESTOR_NOTIF : |
| 1795 | ca.ancestor; |
| 1796 | |
| 1797 | ret.lys_status = |
| 1798 | pn->flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT) ? pn->flags : |
| 1799 | ca.lys_status; |
| 1800 | |
| 1801 | ret.lys_config = |
| 1802 | ca.ancestor == TRD_ANCESTOR_RPC_INPUT ? 0 : /* because <flags> will be -w */ |
| 1803 | ca.ancestor == TRD_ANCESTOR_RPC_OUTPUT ? LYS_CONFIG_R : |
| 1804 | pn->flags & (LYS_CONFIG_R | LYS_CONFIG_W) ? pn->flags : |
| 1805 | ca.lys_config; |
| 1806 | |
| 1807 | ret.last_list = |
| 1808 | pn->nodetype & (LYS_LIST) ? (struct lysp_node_list *)pn : |
| 1809 | ca.last_list; |
| 1810 | |
| 1811 | return ret; |
| 1812 | } |
| 1813 | |
| 1814 | /** |
| 1815 | * @brief Read next sibling of the current node. |
| 1816 | * @param[in] origin_tc is context of the tree. |
| 1817 | * @return A patch structure that has prepared pointers for updating struct trt_tree_ctx. |
| 1818 | * If sibling exists then .pn points to him, otherwise is set to NULL. |
| 1819 | * The .tpn points to its sibling if it exists and if .pn points to the same node as .tpn, |
| 1820 | * otherwise .tpn value from origin_tc is copied. |
| 1821 | */ |
| 1822 | static struct trt_tree_ctx_node_patch |
| 1823 | tro_read_next_sibling(const struct trt_tree_ctx *origin_tc) |
| 1824 | { |
| 1825 | assert(origin_tc && origin_tc->pn); |
| 1826 | |
| 1827 | struct trt_tree_ctx_node_patch tc = TRP_INIT_TREE_CTX_NODE_PATCH(origin_tc->pn, origin_tc->tpn); |
| 1828 | |
| 1829 | if (tc.pn->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 1830 | if (tc.tpn == tc.pn) { |
| 1831 | /* just go to the top-node's sibling */ |
| 1832 | tc.pn = tc.pn->next; |
| 1833 | tc.tpn = tc.pn; |
| 1834 | } else { |
| 1835 | /* try to go to the notif node as sibling */ |
| 1836 | if (!tc.pn->next) { |
| 1837 | tc.pn = (const struct lysp_node *)lysp_node_notifs(tc.pn->parent); |
| 1838 | } else { |
| 1839 | tc.pn = tc.pn->next; |
| 1840 | } |
| 1841 | } |
| 1842 | } else if (tc.pn->nodetype & LYS_INPUT) { |
| 1843 | const struct lysp_node_action *parent = (struct lysp_node_action *)tc.pn->parent; |
| 1844 | /* if output action has data */ |
| 1845 | if (parent->output.child) { |
| 1846 | /* then next sibling is output action */ |
| 1847 | tc.pn = &parent->output.node; |
| 1848 | } else { |
| 1849 | /* else input action has no sibling */ |
| 1850 | tc.pn = NULL; |
| 1851 | } |
| 1852 | /* if current node is output action */ |
| 1853 | } else if (tc.pn->nodetype & LYS_OUTPUT) { |
| 1854 | /* then next sibling does not exist */ |
| 1855 | tc.pn = NULL; |
| 1856 | /* if current node is notification */ |
| 1857 | } else if (tc.pn->nodetype & LYS_NOTIF) { |
| 1858 | if (tc.tpn == tc.pn) { |
| 1859 | tc.pn = tc.pn->next; |
| 1860 | tc.tpn = tc.pn; |
| 1861 | } else { |
| 1862 | tc.pn = tc.pn->next; |
| 1863 | } |
| 1864 | } else { |
| 1865 | /* else actual node is some node with 'next' element */ |
| 1866 | if (tc.tpn == tc.pn) { |
| 1867 | tc.tpn = tc.pn->next; |
| 1868 | } |
| 1869 | tc.pn = tc.pn->next; |
| 1870 | } |
| 1871 | |
| 1872 | return tc; |
| 1873 | } |
| 1874 | |
| 1875 | /** |
| 1876 | * @brief Find out if the current node has siblings. |
| 1877 | * @param[in] tc is context of the tree. |
| 1878 | * @return 1 if sibling exists otherwise 0. |
| 1879 | */ |
| 1880 | static ly_bool |
| 1881 | tro_read_if_sibling_exists(const struct trt_tree_ctx *tc) |
| 1882 | { |
| 1883 | return tro_read_next_sibling(tc).pn != NULL; |
| 1884 | } |
| 1885 | |
| 1886 | /** |
| 1887 | * @brief Check if list statement has keys. |
| 1888 | * @param[in] pn is pointer to the list. |
| 1889 | * @return 1 if has keys, otherwise 0. |
| 1890 | */ |
| 1891 | static ly_bool |
| 1892 | tro_lysp_list_has_keys(const struct lysp_node_list *pn) |
| 1893 | { |
| 1894 | return trg_charptr_has_data(pn->key); |
| 1895 | } |
| 1896 | |
| 1897 | /** |
| 1898 | * @brief Check if it contains at least one feature. |
| 1899 | * @param[in] iffs is pointer to the if-features. |
| 1900 | * @return 1 if has if-features, otherwise 0. |
| 1901 | */ |
| 1902 | static ly_bool |
| 1903 | tro_lysp_node_to_iffeature(const struct lysp_qname *iffs) |
| 1904 | { |
| 1905 | LY_ARRAY_COUNT_TYPE u; |
| 1906 | ly_bool ret = 0; |
| 1907 | |
| 1908 | LY_ARRAY_FOR(iffs, u) { |
| 1909 | ret = 1; |
| 1910 | break; |
| 1911 | } |
| 1912 | return ret; |
| 1913 | } |
| 1914 | |
| 1915 | /** |
| 1916 | * @brief Find out if leaf is also the key in last list. |
| 1917 | * @param[in] pn is pointer to leaf. |
| 1918 | * @param[in] ca_last_list is pointer to last visited list. Obtained from trt_parent_cache. |
| 1919 | * @return 1 if leaf is also the key, otherwise 0. |
| 1920 | */ |
| 1921 | static ly_bool |
| 1922 | tro_lysp_leaf_is_key(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list) |
| 1923 | { |
| 1924 | const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn; |
| 1925 | const struct lysp_node_list *list = ca_last_list; |
| 1926 | |
| 1927 | if (!list) { |
| 1928 | return 0; |
| 1929 | } |
| 1930 | return trg_charptr_has_data(list->key) ? |
| 1931 | trg_word_is_present(list->key, leaf->name, ' ') : 0; |
| 1932 | } |
| 1933 | |
| 1934 | /** |
| 1935 | * @brief Check if container's type is presence. |
| 1936 | * @param[in] pn is pointer to container. |
| 1937 | * @return 1 if container has presence statement, otherwise 0. |
| 1938 | */ |
| 1939 | static ly_bool |
| 1940 | tro_lysp_container_has_presence(const struct lysp_node *pn) |
| 1941 | { |
| 1942 | return trg_charptr_has_data(((struct lysp_node_container *)pn)->presence); |
| 1943 | } |
| 1944 | |
| 1945 | /** |
| 1946 | * @brief Get leaflist's path without lysp_node type control. |
| 1947 | * @param[in] pn is pointer to the leaflist. |
| 1948 | */ |
| 1949 | static const char * |
| 1950 | tro_lysp_leaflist_refpath(const struct lysp_node *pn) |
| 1951 | { |
| 1952 | const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn; |
| 1953 | |
| 1954 | return list->type.path ? list->type.path->expr : NULL; |
| 1955 | } |
| 1956 | |
| 1957 | /** |
| 1958 | * @brief Get leaflist's type name without lysp_node type control. |
| 1959 | * @param[in] pn is pointer to the leaflist. |
| 1960 | */ |
| 1961 | static const char * |
| 1962 | tro_lysp_leaflist_type_name(const struct lysp_node *pn) |
| 1963 | { |
| 1964 | const struct lysp_node_leaflist *list = (const struct lysp_node_leaflist *)pn; |
| 1965 | |
| 1966 | return list->type.name; |
| 1967 | } |
| 1968 | |
| 1969 | /** |
| 1970 | * @brief Get leaf's path without lysp_node type control. |
| 1971 | * @param[in] pn is pointer to the leaf node. |
| 1972 | */ |
| 1973 | static const char * |
| 1974 | tro_lysp_leaf_refpath(const struct lysp_node *pn) |
| 1975 | { |
| 1976 | const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn; |
| 1977 | |
| 1978 | return leaf->type.path ? leaf->type.path->expr : NULL; |
| 1979 | } |
| 1980 | |
| 1981 | /** |
| 1982 | * @brief Get leaf's type name without lysp_node type control. |
| 1983 | * @param[in] pn is pointer to the leaf's type name. |
| 1984 | */ |
| 1985 | static const char * |
| 1986 | tro_lysp_leaf_type_name(const struct lysp_node *pn) |
| 1987 | { |
| 1988 | const struct lysp_node_leaf *leaf = (const struct lysp_node_leaf *)pn; |
| 1989 | |
| 1990 | return leaf->type.name; |
| 1991 | } |
| 1992 | |
| 1993 | /** |
| 1994 | * @brief Get pointer to data using node type specification and getter function. |
| 1995 | * |
| 1996 | * @param[in] flags is node type specification. If it is the correct node, the getter function is called. |
| 1997 | * @param[in] f is getter function which provides the desired char pointer from the structure. |
| 1998 | * @param[in] pn pointer to node. |
| 1999 | * @return NULL if node has wrong type or getter function return pointer to NULL. |
| 2000 | * @return Pointer to desired char pointer obtained from the node. |
| 2001 | */ |
| 2002 | static const char * |
| 2003 | tro_lysp_node_charptr(uint16_t flags, trt_get_charptr_func f, const struct lysp_node *pn) |
| 2004 | { |
| 2005 | if (pn->nodetype & flags) { |
| 2006 | const char *ret = f(pn); |
| 2007 | return trg_charptr_has_data(ret) ? ret : NULL; |
| 2008 | } else { |
| 2009 | return NULL; |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | /** |
| 2014 | * @brief Transformation of the lysp flags to Yang tree \<status\>. |
| 2015 | * @param[in] flags is node's flags obtained from the tree. |
| 2016 | */ |
| 2017 | static trt_status_type |
| 2018 | tro_lysp_flags2status(uint16_t flags) |
| 2019 | { |
| 2020 | return flags & LYS_STATUS_OBSLT ? TRD_STATUS_TYPE_OBSOLETE : |
| 2021 | flags & LYS_STATUS_DEPRC ? TRD_STATUS_TYPE_DEPRECATED : |
| 2022 | TRD_STATUS_TYPE_CURRENT; |
| 2023 | } |
| 2024 | |
| 2025 | /** |
| 2026 | * @brief Transformation of the lysp flags to Yang tree \<flags\> but more specifically 'ro' or 'rw'. |
| 2027 | * @param[in] flags is node's flags obtained from the tree. |
| 2028 | */ |
| 2029 | static trt_flags_type |
| 2030 | tro_lysp_flags2config(uint16_t flags) |
| 2031 | { |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2032 | return flags & LYS_CONFIG_R ? TRD_FLAGS_TYPE_RO : |
| 2033 | flags & LYS_CONFIG_W ? TRD_FLAGS_TYPE_RW : |
| 2034 | TRD_FLAGS_TYPE_EMPTY; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | /** |
| 2038 | * @brief Get name of the module. |
| 2039 | * @param[in] tc is context of the tree. |
| 2040 | */ |
| 2041 | static struct trt_keyword_stmt |
| 2042 | tro_read_module_name(const struct trt_tree_ctx *tc) |
| 2043 | { |
| 2044 | assert(tc && tc->module && tc->module->name); |
| 2045 | return (struct trt_keyword_stmt) { |
| 2046 | .type = TRD_KEYWORD_MODULE, .str = tc->module->name |
| 2047 | }; |
| 2048 | } |
| 2049 | |
| 2050 | /** |
| 2051 | * @brief Resolve \<status\> of the current node. |
| 2052 | * @param[in] nodetype is node's type obtained from the tree. |
| 2053 | * @param[in] flags is node's flags obtained from the tree. |
| 2054 | * @param[in] ca_lys_status is inherited status obtained from trt_parent_cache. |
| 2055 | * @return The status type. |
| 2056 | */ |
| 2057 | static trt_status_type |
| 2058 | tro_resolve_status(uint16_t nodetype, uint16_t flags, uint16_t ca_lys_status) |
| 2059 | { |
| 2060 | /* LYS_INPUT and LYS_OUTPUT is special case */ |
| 2061 | if (nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 2062 | return tro_lysp_flags2status(ca_lys_status); |
| 2063 | /* if ancestor's status is deprc or obslt and also node's status is not set */ |
| 2064 | } else if ((ca_lys_status & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) && !(flags & (LYS_STATUS_CURR | LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) { |
| 2065 | /* get ancestor's status */ |
| 2066 | return tro_lysp_flags2status(ca_lys_status); |
| 2067 | } else { |
| 2068 | /* else get node's status */ |
| 2069 | return tro_lysp_flags2status(flags); |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | /** |
| 2074 | * @brief Resolve \<flags\> of the current node. |
| 2075 | * @param[in] nodetype is node's type obtained from the tree. |
| 2076 | * @param[in] flags is node's flags obtained from the tree. |
| 2077 | * @param[in] ca_ancestor is ancestor type obtained from trt_parent_cache. |
| 2078 | * @param[in] ca_lys_config is inherited config item obtained from trt_parent_cache. |
| 2079 | * @return The flags type. |
| 2080 | */ |
| 2081 | static trt_flags_type |
| 2082 | tro_resolve_flags(uint16_t nodetype, uint16_t flags, trt_ancestor_type ca_ancestor, uint16_t ca_lys_config) |
| 2083 | { |
| 2084 | if ((nodetype & LYS_INPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_INPUT)) { |
| 2085 | return TRD_FLAGS_TYPE_RPC_INPUT_PARAMS; |
| 2086 | } else if ((nodetype & LYS_OUTPUT) || (ca_ancestor == TRD_ANCESTOR_RPC_OUTPUT)) { |
| 2087 | return TRD_FLAGS_TYPE_RO; |
| 2088 | } else if (ca_ancestor == TRD_ANCESTOR_NOTIF) { |
| 2089 | return TRD_FLAGS_TYPE_RO; |
| 2090 | } else if (nodetype & LYS_NOTIF) { |
| 2091 | return TRD_FLAGS_TYPE_NOTIF; |
| 2092 | } else if (nodetype & LYS_USES) { |
| 2093 | return TRD_FLAGS_TYPE_USES_OF_GROUPING; |
| 2094 | } else if (nodetype & (LYS_RPC | LYS_ACTION)) { |
| 2095 | return TRD_FLAGS_TYPE_RPC; |
| 2096 | /* if config is not set then look at ancestor's config and get his config */ |
| 2097 | } else if (!(flags & (LYS_CONFIG_R | LYS_CONFIG_W))) { |
| 2098 | return tro_lysp_flags2config(ca_lys_config); |
| 2099 | } else { |
| 2100 | return tro_lysp_flags2config(flags); |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | /** |
| 2105 | * @brief Resolve node type of the current node. |
| 2106 | * @param[in] pn is pointer to the current node in the tree. |
| 2107 | * @param[in] ca_last_list is pointer to the last visited list. Obtained from the trt_parent_cache. |
| 2108 | */ |
| 2109 | static trt_node_type |
| 2110 | tro_resolve_node_type(const struct lysp_node *pn, const struct lysp_node_list *ca_last_list) |
| 2111 | { |
| 2112 | if (pn->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 2113 | return TRD_NODE_ELSE; |
| 2114 | } else if (pn->nodetype & LYS_CASE) { |
| 2115 | return TRD_NODE_CASE; |
| 2116 | } else if ((pn->nodetype & LYS_CHOICE) && !(pn->flags & LYS_MAND_TRUE)) { |
| 2117 | return TRD_NODE_OPTIONAL_CHOICE; |
| 2118 | } else if (pn->nodetype & LYS_CHOICE) { |
| 2119 | return TRD_NODE_CHOICE; |
| 2120 | } else if ((pn->nodetype & LYS_CONTAINER) && (tro_lysp_container_has_presence(pn))) { |
| 2121 | return TRD_NODE_CONTAINER; |
| 2122 | } else if ((pn->nodetype & LYS_LIST) && (tro_lysp_list_has_keys((const struct lysp_node_list *)pn))) { |
| 2123 | return TRD_NODE_KEYS; |
| 2124 | } else if (pn->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 2125 | return TRD_NODE_LISTLEAFLIST; |
| 2126 | } else if ((pn->nodetype & (LYS_ANYDATA | LYS_ANYXML)) && !(pn->flags & LYS_MAND_TRUE)) { |
| 2127 | return TRD_NODE_OPTIONAL; |
| 2128 | } else if ((pn->nodetype & LYS_LEAF) && !(pn->flags & LYS_MAND_TRUE) && (!tro_lysp_leaf_is_key(pn, ca_last_list))) { |
| 2129 | return TRD_NODE_OPTIONAL; |
| 2130 | } else { |
| 2131 | return TRD_NODE_ELSE; |
| 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | /** |
| 2136 | * @brief Transformation of current lysp_node to struct trt_node. |
| 2137 | * @param[in] ca contains stored important data when browsing the tree downwards. |
| 2138 | * @param[in] tc is context of the tree. |
| 2139 | */ |
| 2140 | static struct trt_node |
| 2141 | tro_read_node(struct trt_parent_cache ca, const struct trt_tree_ctx *tc) |
| 2142 | { |
| 2143 | assert(tc && tc->pn && tc->pn->nodetype != LYS_UNKNOWN); |
| 2144 | const struct lysp_node *pn = tc->pn; |
| 2145 | struct trt_node ret = TRP_EMPTY_NODE; |
| 2146 | const char *tmp; |
| 2147 | |
| 2148 | /* <status> */ |
| 2149 | ret.status = tro_resolve_status(pn->nodetype, pn->flags, ca.lys_status); |
| 2150 | |
| 2151 | /* TODO: TRD_FLAGS_TYPE_MOUNT_POINT aka "mp" is not supported right now. */ |
| 2152 | /* <flags> */ |
| 2153 | ret.flags = tro_resolve_flags(pn->nodetype, pn->flags, ca.ancestor, ca.lys_config); |
| 2154 | |
| 2155 | /* TODO: TRD_NODE_TOP_LEVEL1 aka '/' is not supported right now. */ |
| 2156 | /* TODO: TRD_NODE_TOP_LEVEL2 aka '@' is not supported right now. */ |
| 2157 | /* set type of the node */ |
| 2158 | ret.name.type = tro_resolve_node_type(pn, ca.last_list); |
| 2159 | |
| 2160 | /* TODO: ret.name.module_prefix is not supported right now. */ |
| 2161 | ret.name.module_prefix = NULL; |
| 2162 | |
| 2163 | /* set node's name */ |
| 2164 | ret.name.str = pn->name; |
| 2165 | |
| 2166 | /* <type> */ |
| 2167 | tmp = NULL; |
| 2168 | |
| 2169 | if ((tmp = tro_lysp_node_charptr(LYS_LEAFLIST, tro_lysp_leaflist_refpath, pn))) { |
| 2170 | ret.type = TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp); |
| 2171 | } else if ((tmp = tro_lysp_node_charptr(LYS_LEAFLIST, tro_lysp_leaflist_type_name, pn))) { |
| 2172 | ret.type = TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp); |
| 2173 | } else if ((tmp = tro_lysp_node_charptr(LYS_LEAF, tro_lysp_leaf_refpath, pn))) { |
| 2174 | ret.type = TRP_INIT_TRT_TYPE(TRD_TYPE_TARGET, tmp); |
| 2175 | } else if ((tmp = tro_lysp_node_charptr(LYS_LEAF, tro_lysp_leaf_type_name, pn))) { |
| 2176 | ret.type = TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, tmp); |
| 2177 | } else if ((pn->nodetype & LYS_ANYDATA) == LYS_ANYDATA) { |
| 2178 | ret.type = TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anydata"); |
| 2179 | } else if (pn->nodetype & LYS_ANYXML) { |
| 2180 | ret.type = TRP_INIT_TRT_TYPE(TRD_TYPE_NAME, "anyxml"); |
| 2181 | } else { |
| 2182 | ret.type = TRP_EMPTY_TRT_TYPE; |
| 2183 | } |
| 2184 | |
| 2185 | /* <iffeature> */ |
| 2186 | ret.iffeatures = tro_lysp_node_to_iffeature(pn->iffeatures); |
| 2187 | |
| 2188 | ret.last_one = !tro_read_if_sibling_exists(tc); |
| 2189 | |
| 2190 | return ret; |
| 2191 | } |
| 2192 | |
| 2193 | /****************************************************************************** |
| 2194 | * Modify Tro getters |
| 2195 | *****************************************************************************/ |
| 2196 | |
| 2197 | /** |
| 2198 | * @brief Change current node pointer to its parent but only if parent exists. |
| 2199 | * @param[in,out] tc is tree context. Contains pointer to the current node. |
| 2200 | * @return 1 if the node had parents and the change was successful. |
| 2201 | * @return 0 if the node did not have parents. The pointer to the current node did not change. |
| 2202 | */ |
| 2203 | static ly_bool |
| 2204 | tro_modi_parent(struct trt_tree_ctx *tc) |
| 2205 | { |
| 2206 | assert(tc && tc->pn); |
| 2207 | /* If no parent exists, stay in actual node. */ |
| 2208 | if (tc->pn != tc->tpn) { |
| 2209 | tc->pn = tc->pn->parent; |
| 2210 | return 1; |
| 2211 | } else { |
| 2212 | return 0; |
| 2213 | } |
| 2214 | } |
| 2215 | |
| 2216 | /** |
| 2217 | * @brief Change the current node pointer to its child but only if exists. |
| 2218 | * @param[in] ca contains inherited data from ancestors. |
| 2219 | * @param[in,out] tc is context of the tree. Contains pointer to the current node. |
| 2220 | * @return Non-empty \<node\> representation of the current node's child. The tc parameter is modified. |
| 2221 | * @return Empty \<node\> representation if child don't exists. The tc parameter is not modified. |
| 2222 | */ |
| 2223 | static struct trt_node |
| 2224 | tro_modi_next_child(struct trt_parent_cache ca, struct trt_tree_ctx *tc) |
| 2225 | { |
| 2226 | assert(tc && tc->pn); |
| 2227 | |
| 2228 | struct trt_parent_cache new_ca = tro_parent_cache_for_child(ca, tc->pn); |
| 2229 | |
| 2230 | if (tc->pn->nodetype & (LYS_ACTION | LYS_RPC)) { |
| 2231 | const struct lysp_node_action *act = (const struct lysp_node_action *)tc->pn; |
| 2232 | if (act->input.child) { |
| 2233 | /* go to LYS_INPUT */ |
| 2234 | tc->pn = &act->input.node; |
| 2235 | return tro_read_node(new_ca, tc); |
| 2236 | } else if (act->output.child) { |
| 2237 | /* go to LYS_OUTPUT */ |
| 2238 | tc->pn = &act->output.node; |
| 2239 | return tro_read_node(new_ca, tc); |
| 2240 | } else { |
| 2241 | /* input action and output action are not set */ |
| 2242 | return TRP_EMPTY_NODE; |
| 2243 | } |
| 2244 | } else { |
| 2245 | const struct lysp_node *pn = lysp_node_child(tc->pn); |
| 2246 | if (pn) { |
| 2247 | tc->pn = pn; |
| 2248 | return tro_read_node(new_ca, tc); |
| 2249 | } else { |
| 2250 | /* current node can't have children or has no children */ |
| 2251 | /* but maybe has some actions or notifs */ |
| 2252 | const struct lysp_node_action *actions = lysp_node_actions(tc->pn); |
| 2253 | const struct lysp_node_notif *notifs = lysp_node_notifs(tc->pn); |
| 2254 | |
| 2255 | if (actions) { |
| 2256 | tc->pn = (const struct lysp_node *)actions; |
| 2257 | return tro_read_node(new_ca, tc); |
| 2258 | } else if (notifs) { |
| 2259 | tc->pn = (const struct lysp_node *)notifs; |
| 2260 | return tro_read_node(new_ca, tc); |
| 2261 | } else { |
| 2262 | return TRP_EMPTY_NODE; |
| 2263 | } |
| 2264 | } |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | /** |
| 2269 | * @brief Change the current node pointer to the first child of node's parent. |
| 2270 | * If current node is already first sibling/child then nothing will change. |
| 2271 | * @param[in,out] tc is tree context. |
| 2272 | */ |
| 2273 | static void |
| 2274 | tro_modi_first_sibling(struct trt_tree_ctx *tc) |
| 2275 | { |
| 2276 | assert(tc && tc->pn && tc->module && tc->module->parsed); |
| 2277 | |
| 2278 | if (tro_modi_parent(tc)) { |
| 2279 | tro_modi_next_child(TRP_EMPTY_PARENT_CACHE, tc); |
| 2280 | } else { |
| 2281 | /* current node is top-node */ |
| 2282 | |
| 2283 | struct lysp_module *pm = tc->module->parsed; |
| 2284 | |
| 2285 | switch (tc->section) { |
| 2286 | case TRD_SECT_MODULE: |
| 2287 | tc->pn = pm->data; |
| 2288 | break; |
| 2289 | case TRD_SECT_AUGMENT: |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2290 | tc->pn = (const struct lysp_node *)pm->augments; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2291 | break; |
| 2292 | case TRD_SECT_RPCS: |
| 2293 | tc->pn = (const struct lysp_node *)pm->rpcs; |
| 2294 | break; |
| 2295 | case TRD_SECT_NOTIF: |
| 2296 | tc->pn = (const struct lysp_node *)pm->notifs; |
| 2297 | break; |
| 2298 | case TRD_SECT_GROUPING: |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2299 | tc->pn = (const struct lysp_node *)pm->groupings; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2300 | break; |
| 2301 | case TRD_SECT_YANG_DATA: |
| 2302 | /*TODO: yang-data is not supported now */ |
| 2303 | break; |
| 2304 | } |
| 2305 | |
| 2306 | /* update pointer to top-node */ |
| 2307 | tc->tpn = tc->pn; |
| 2308 | } |
| 2309 | } |
| 2310 | |
| 2311 | /** |
| 2312 | * @brief Change the pointer to the current node to its next sibling only if exists. |
| 2313 | * @param[in] ca contains inherited data from ancestors. |
| 2314 | * @param[in,out] tc is tree context. Contains pointer to the current node. |
| 2315 | * @return Non-empty \<node\> representation if sibling exists. The tc is modified. |
| 2316 | * @return Empty \<node\> representation otherwise. The tc is not modified. |
| 2317 | */ |
| 2318 | static struct trt_node |
| 2319 | tro_modi_next_sibling(struct trt_parent_cache ca, struct trt_tree_ctx *tc) |
| 2320 | { |
| 2321 | struct trt_tree_ctx_node_patch patch = tro_read_next_sibling(tc); |
| 2322 | |
| 2323 | /* if next sibling exists */ |
| 2324 | if (patch.pn) { |
| 2325 | /* update trt_tree_ctx */ |
| 2326 | tc->pn = patch.pn; |
| 2327 | tc->tpn = patch.tpn; |
| 2328 | return tro_read_node(ca, tc); |
| 2329 | } else { |
| 2330 | return TRP_EMPTY_NODE; |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | /** |
| 2335 | * @brief Get next (or first) augment section if exists. |
| 2336 | * @param[in,out] tc is tree context. |
| 2337 | * @return Section's representation if (next augment) section exists. |
| 2338 | * The tc is modified and his pointer points to the first node in augment section. |
| 2339 | * @return Empty section structure otherwise. |
| 2340 | */ |
| 2341 | static struct trt_keyword_stmt |
| 2342 | tro_modi_next_augment(struct trt_tree_ctx *tc) |
| 2343 | { |
| 2344 | assert(tc && tc->module && tc->module->parsed); |
| 2345 | const struct lysp_node_augment *augs; |
| 2346 | |
| 2347 | /* if next_augment func was called for the first time */ |
| 2348 | if (tc->section != TRD_SECT_AUGMENT) { |
| 2349 | tc->section = TRD_SECT_AUGMENT; |
| 2350 | augs = tc->module->parsed->augments; |
| 2351 | } else { |
| 2352 | /* get augment sibling from top-node pointer */ |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2353 | augs = (const struct lysp_node_augment *)tc->tpn->next; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2354 | } |
| 2355 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2356 | if (augs) { |
| 2357 | tc->pn = &augs->node; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2358 | tc->tpn = tc->pn; |
| 2359 | return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_AUGMENT, augs->nodeid); |
| 2360 | } else { |
| 2361 | return TRP_EMPTY_KEYWORD_STMT; |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | /** |
| 2366 | * @brief Get rpcs section if exists. |
| 2367 | * @param[in,out] tc is tree context. |
| 2368 | * @return Section representation if it exists. |
| 2369 | * The tc is modified and his pointer points to the first node in rpcs section. |
| 2370 | * @return Empty section representation otherwise. |
| 2371 | */ |
| 2372 | static struct trt_keyword_stmt |
| 2373 | tro_modi_get_rpcs(struct trt_tree_ctx *tc) |
| 2374 | { |
| 2375 | assert(tc && tc->module && tc->module->parsed); |
| 2376 | const struct lysp_node_action *actions = tc->module->parsed->rpcs; |
| 2377 | |
| 2378 | if (actions) { |
| 2379 | tc->section = TRD_SECT_RPCS; |
| 2380 | tc->pn = &actions->node; |
| 2381 | tc->tpn = tc->pn; |
| 2382 | return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_RPC, NULL); |
| 2383 | } else { |
| 2384 | return TRP_EMPTY_KEYWORD_STMT; |
| 2385 | } |
| 2386 | } |
| 2387 | |
| 2388 | /** |
| 2389 | * @brief Get notification section if exists |
| 2390 | * @param[in,out] tc is tree context. |
| 2391 | * @return Section representation if it exists. |
| 2392 | * The tc is modified and his pointer points to the first node in notification section. |
| 2393 | * @return Empty section representation otherwise. |
| 2394 | */ |
| 2395 | static struct trt_keyword_stmt |
| 2396 | tro_modi_get_notifications(struct trt_tree_ctx *tc) |
| 2397 | { |
| 2398 | assert(tc && tc->module && tc->module->parsed); |
| 2399 | const struct lysp_node_notif *notifs = tc->module->parsed->notifs; |
| 2400 | |
| 2401 | if (notifs) { |
| 2402 | tc->section = TRD_SECT_NOTIF; |
| 2403 | tc->pn = ¬ifs->node; |
| 2404 | tc->tpn = tc->pn; |
| 2405 | return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_NOTIF, NULL); |
| 2406 | } else { |
| 2407 | return TRP_EMPTY_KEYWORD_STMT; |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | /** |
| 2412 | * @brief Get next (or first) grouping section if exists |
| 2413 | * @param[in,out] tc is tree context. |
| 2414 | * @return The next (or first) section representation if it exists. |
| 2415 | * The tc is modified and his pointer points to the first node in this grouping section. |
| 2416 | * @return Empty section representation otherwise. |
| 2417 | */ |
| 2418 | static struct trt_keyword_stmt |
| 2419 | tro_modi_next_grouping(struct trt_tree_ctx *tc) |
| 2420 | { |
| 2421 | assert(tc && tc->module && tc->module->parsed); |
| 2422 | const struct lysp_node_grp *grps; |
| 2423 | |
| 2424 | if (tc->section != TRD_SECT_GROUPING) { |
| 2425 | tc->section = TRD_SECT_GROUPING; |
| 2426 | grps = tc->module->parsed->groupings; |
| 2427 | } else { |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2428 | grps = (const struct lysp_node_grp *)tc->tpn->next; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2429 | } |
| 2430 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2431 | if (grps) { |
| 2432 | tc->pn = &grps->node; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2433 | tc->tpn = tc->pn; |
| 2434 | return TRP_INIT_KEYWORD_STMT(TRD_KEYWORD_GROUPING, grps->name); |
| 2435 | } else { |
| 2436 | return TRP_EMPTY_KEYWORD_STMT; |
| 2437 | } |
| 2438 | } |
| 2439 | |
| 2440 | /** |
| 2441 | * @brief Get next yang-data section if exists. |
| 2442 | * |
| 2443 | * Not implemented. |
| 2444 | * |
| 2445 | * @param[in,out] tc is tree context. |
| 2446 | * @return Section representation if it exists. |
| 2447 | * @return Empty section representation otherwise. |
| 2448 | */ |
| 2449 | static struct trt_keyword_stmt |
| 2450 | tro_modi_next_yang_data(struct trt_tree_ctx *tc) |
| 2451 | { |
| 2452 | tc->section = TRD_SECT_YANG_DATA; |
| 2453 | /* TODO: yang-data is not supported */ |
| 2454 | return TRP_EMPTY_KEYWORD_STMT; |
| 2455 | } |
| 2456 | |
| 2457 | /****************************************************************************** |
| 2458 | * Print Tro getters |
| 2459 | *****************************************************************************/ |
| 2460 | |
| 2461 | /** |
| 2462 | * @brief Print current node's iffeatures. |
| 2463 | * @param[in] tc is tree context. |
| 2464 | * @param[in,out] out is output handler. |
| 2465 | */ |
| 2466 | static void |
| 2467 | tro_print_features_names(const struct trt_tree_ctx *tc, struct ly_out *out) |
| 2468 | { |
| 2469 | const struct lysp_qname *iffs = tc->pn->iffeatures; |
| 2470 | |
| 2471 | LY_ARRAY_COUNT_TYPE i; |
| 2472 | |
| 2473 | LY_ARRAY_FOR(iffs, i) { |
| 2474 | if (i == 0) { |
| 2475 | ly_print_(out, "%s", iffs[i].str); |
| 2476 | } else { |
| 2477 | ly_print_(out, ",%s", iffs[i].str); |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | } |
| 2482 | |
| 2483 | /** |
| 2484 | * @brief Print current list's keys. |
| 2485 | * |
| 2486 | * Well, actually printing keys in the lysp_tree is trivial, because char* points to all keys. |
| 2487 | * However, special functions have been reserved for this, because in principle |
| 2488 | * the list of elements can have more implementations. |
| 2489 | * |
| 2490 | * @param[in] tc is tree context. |
| 2491 | * @param[in,out] out is output handler. |
| 2492 | */ |
| 2493 | static void |
| 2494 | tro_print_keys(const struct trt_tree_ctx *tc, struct ly_out *out) |
| 2495 | { |
| 2496 | const struct lysp_node *pn = tc->pn; |
| 2497 | const struct lysp_node_list *list; |
| 2498 | |
| 2499 | if (pn->nodetype != LYS_LIST) { |
| 2500 | return; |
| 2501 | } |
| 2502 | |
| 2503 | list = (const struct lysp_node_list *)pn; |
| 2504 | |
| 2505 | if (trg_charptr_has_data(list->key)) { |
| 2506 | ly_print_(out, "%s", list->key); |
| 2507 | } |
| 2508 | } |
| 2509 | |
| 2510 | /****************************************************************************** |
| 2511 | * Definition of tree browsing functions |
| 2512 | *****************************************************************************/ |
| 2513 | |
| 2514 | /** |
| 2515 | * @brief Get size of node name. |
| 2516 | * @param[in] name contains name and mark. |
| 2517 | * @return positive value total size of the node name. |
| 2518 | * @return negative value as an indication that option mark is included in the total size. |
| 2519 | */ |
| 2520 | static int32_t |
| 2521 | trb_strlen_of_name_and_mark(struct trt_node_name name) |
| 2522 | { |
| 2523 | size_t name_len = strlen(name.str); |
| 2524 | |
| 2525 | if ((name.type == TRD_NODE_CHOICE) || (name.type == TRD_NODE_CASE)) { |
| 2526 | /* counting also parentheses */ |
| 2527 | name_len += 2; |
| 2528 | } |
| 2529 | |
| 2530 | return trp_mark_is_used(name) ? |
| 2531 | ((int32_t)(name_len + TRD_OPTS_MARK_LENGTH)) * (-1) : |
| 2532 | (int32_t)name_len; |
| 2533 | } |
| 2534 | |
| 2535 | /** |
| 2536 | * @brief Calculate the btw_opts_type indent size for a particular node. |
| 2537 | * @param[in] name is the node for which we get btw_opts_type. |
| 2538 | * @param[in] max_len4all is the maximum value of btw_opts_type that it can have. |
| 2539 | * @return btw_opts_type for node. |
| 2540 | */ |
| 2541 | static int16_t |
| 2542 | trb_calc_btw_opts_type(struct trt_node_name name, int16_t max_len4all) |
| 2543 | { |
| 2544 | int32_t name_len; |
| 2545 | int16_t min_len; |
| 2546 | int16_t ret; |
| 2547 | |
| 2548 | name_len = trb_strlen_of_name_and_mark(name); |
| 2549 | |
| 2550 | /* negative value indicate that in name is some opt mark */ |
| 2551 | min_len = name_len < 0 ? |
| 2552 | TRD_INDENT_BEFORE_TYPE - TRD_OPTS_MARK_LENGTH : |
| 2553 | TRD_INDENT_BEFORE_TYPE; |
| 2554 | ret = abs(max_len4all) - abs(name_len); |
| 2555 | |
| 2556 | /* correction -> negative indicate that name is too long. */ |
| 2557 | return ret < 0 ? min_len : ret; |
| 2558 | } |
| 2559 | |
| 2560 | /** |
| 2561 | * @brief Print node. |
| 2562 | * |
| 2563 | * This function is wrapper for trp_print_entire_node function. |
| 2564 | * But difference is that take max_gap_before_type parameter which will be used to set the unified alignment. |
| 2565 | * |
| 2566 | * @param[in] max_gap_before_type is number of indent before \<type\>. |
| 2567 | * @param[in] wr is wrapper for printing indentation before node. |
| 2568 | * @param[in] ca contains inherited data from ancestors. |
| 2569 | * @param[in] pc contains mainly functions for printing. |
| 2570 | * @param[in] tc is tree context. |
| 2571 | */ |
| 2572 | static void |
| 2573 | trb_print_entire_node(uint32_t max_gap_before_type, struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 2574 | { |
| 2575 | struct trt_node node = pc->fp.read.node(ca, tc); |
| 2576 | struct trt_indent_in_node ind = trp_default_indent_in_node(node); |
| 2577 | |
| 2578 | if ((max_gap_before_type > 0) && (node.type.type != TRD_TYPE_EMPTY)) { |
| 2579 | /* print actual node with unified indent */ |
| 2580 | ind.btw_opts_type = trb_calc_btw_opts_type(node.name, max_gap_before_type); |
| 2581 | } |
| 2582 | /* after -> print actual node with default indent */ |
| 2583 | trp_print_entire_node(node, TRP_INIT_PCK_PRINT(tc, pc->fp.print), |
| 2584 | TRP_INIT_PCK_INDENT(wr, ind), pc->max_line_length, pc->out); |
| 2585 | } |
| 2586 | |
| 2587 | /** |
| 2588 | * @brief Check if parent of the current node is the last of his siblings. |
| 2589 | * |
| 2590 | * To mantain stability use this function only if the current node is the first of the siblings. |
| 2591 | * Side-effect -> current node is set to the first sibling if node has a parent otherwise no side-effect. |
| 2592 | * |
| 2593 | * @param[in] fp contains all 'tro' callback functions. |
| 2594 | * @param[in,out] tc is tree context. |
| 2595 | * @return 1 if parent is last sibling otherwise 0. |
| 2596 | */ |
| 2597 | static ly_bool |
| 2598 | trb_parent_is_last_sibling(struct trt_fp_all fp, struct trt_tree_ctx *tc) |
| 2599 | { |
| 2600 | if (fp.modify.parent(tc)) { |
| 2601 | ly_bool ret = fp.read.if_sibling_exists(tc); |
| 2602 | fp.modify.next_child(TRP_EMPTY_PARENT_CACHE, tc); |
| 2603 | return !ret; |
| 2604 | } else { |
| 2605 | return !fp.read.if_sibling_exists(tc); |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | /** |
| 2610 | * @brief Find sibling with the biggest node name and return that size. |
| 2611 | * |
| 2612 | * Side-effect -> Current node is set to the first sibling. |
| 2613 | * |
| 2614 | * @param[in] ca contains inherited data from ancestors. |
| 2615 | * @param[in] pc contains mainly functions for printing. |
| 2616 | * @param[in,out] tc is tree context. |
| 2617 | * @return positive number lesser than upper_limit as a sign that only the node name is included in the size. |
| 2618 | * @return negative number whose absolute value is less than upper_limit and sign that node name and his opt mark is included in the size. |
| 2619 | */ |
| 2620 | static int32_t |
| 2621 | trb_maxlen_node_name(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 2622 | { |
| 2623 | int32_t ret = 0; |
| 2624 | |
| 2625 | pc->fp.modify.first_sibling(tc); |
| 2626 | |
| 2627 | for (struct trt_node node = pc->fp.read.node(ca, tc); |
| 2628 | !trp_node_is_empty(node); |
| 2629 | node = pc->fp.modify.next_sibling(ca, tc)) { |
| 2630 | int32_t maxlen = trb_strlen_of_name_and_mark(node.name); |
| 2631 | ret = abs(maxlen) > abs(ret) ? maxlen : ret; |
| 2632 | } |
| 2633 | pc->fp.modify.first_sibling(tc); |
| 2634 | return ret; |
| 2635 | } |
| 2636 | |
| 2637 | /** |
| 2638 | * @brief Find maximal indent between \<opts\> and \<type\> for siblings. |
| 2639 | * |
| 2640 | * Side-effect -> Current node is set to the first sibling. |
| 2641 | * |
| 2642 | * @param[in] ca contains inherited data from ancestors. |
| 2643 | * @param[in] pc contains mainly functions for printing. |
| 2644 | * @param[in,out] tc is tree context. |
| 2645 | * @return max btw_opts_type value for rest of the siblings |
| 2646 | */ |
| 2647 | static int16_t |
| 2648 | trb_max_btw_opts_type4siblings(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 2649 | { |
| 2650 | int32_t maxlen_node_name = trb_maxlen_node_name(ca, pc, tc); |
| 2651 | int16_t ind_before_type = maxlen_node_name < 0 ? |
| 2652 | TRD_INDENT_BEFORE_TYPE - 1 : /* mark was present */ |
| 2653 | TRD_INDENT_BEFORE_TYPE; |
| 2654 | |
| 2655 | return abs(maxlen_node_name) + ind_before_type; |
| 2656 | } |
| 2657 | |
| 2658 | /** |
| 2659 | * @brief Find out if it is possible to unify the alignment before \<type\>. |
| 2660 | * |
| 2661 | * The goal is for all node siblings to have the same alignment for \<type\> as if they were in a column. |
| 2662 | * All siblings who cannot adapt because they do not fit on the line at all are ignored. |
| 2663 | * Side-effect -> Current node is set to the first sibling. |
| 2664 | * |
| 2665 | * @param[in] ca contains inherited data from ancestors. |
| 2666 | * @param[in] pc contains mainly functions for printing. |
| 2667 | * @param[in,out] tc is tree context. |
| 2668 | * @return 0 if all siblings cannot fit on the line. |
| 2669 | * @return positive number indicating the maximum number of spaces before \<type\> if the length of the node name is 0. |
| 2670 | * To calculate the btw_opts_type indent size for a particular node, use the trb_calc_btw_opts_type function. |
| 2671 | */ |
| 2672 | static uint32_t |
| 2673 | trb_try_unified_indent(struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 2674 | { |
| 2675 | return trb_max_btw_opts_type4siblings(ca, pc, tc); |
| 2676 | } |
| 2677 | |
| 2678 | /** |
| 2679 | * @brief For the current node: recursively print all of its child nodes and all of its siblings, including their children. |
| 2680 | * |
| 2681 | * This function is an auxiliary function for trb_print_subtree_nodes. |
| 2682 | * The parent of the current node is expected to exist. |
| 2683 | * Nodes are printed, including unified sibling node alignment (align \<type\> to column). |
| 2684 | * Side-effect -> current node is set to the last sibling. |
| 2685 | * |
| 2686 | * @param[in] wr is wrapper for printing identation before node. |
| 2687 | * @param[in] ca contains inherited data from ancestors. |
| 2688 | * @param[in] pc contains mainly functions for printing. |
| 2689 | * @param[in,out] tc is tree context. |
| 2690 | */ |
| 2691 | static void |
| 2692 | trb_print_nodes(struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 2693 | { |
| 2694 | uint32_t max_gap_before_type; |
| 2695 | ly_bool sibling_flag = 0; |
| 2696 | ly_bool child_flag = 0; |
| 2697 | |
| 2698 | /* if node is last sibling, then do not add '|' to wrapper */ |
| 2699 | wr = trb_parent_is_last_sibling(pc->fp, tc) ? |
| 2700 | trp_wrapper_set_shift(wr) : trp_wrapper_set_mark(wr); |
| 2701 | |
| 2702 | /* try unified indentation in node */ |
| 2703 | max_gap_before_type = trb_try_unified_indent(ca, pc, tc); |
| 2704 | |
| 2705 | /* print all siblings */ |
| 2706 | do { |
| 2707 | struct trt_parent_cache new_ca; |
| 2708 | struct trt_node node; |
| 2709 | /* print linebreak before printing actual node */ |
| 2710 | ly_print_(pc->out, "\n"); |
| 2711 | /* print node */ |
| 2712 | trb_print_entire_node(max_gap_before_type, wr, ca, pc, tc); |
| 2713 | |
| 2714 | new_ca = tro_parent_cache_for_child(ca, tc->pn); |
| 2715 | /* go to the actual node's child or stay in actual node */ |
| 2716 | node = pc->fp.modify.next_child(ca, tc); |
| 2717 | child_flag = !trp_node_is_empty(node); |
| 2718 | |
| 2719 | if (child_flag) { |
| 2720 | /* print all childs - recursive call */ |
| 2721 | trb_print_nodes(wr, new_ca, pc, tc); |
| 2722 | /* get back from child node to actual node */ |
| 2723 | pc->fp.modify.parent(tc); |
| 2724 | } |
| 2725 | |
| 2726 | /* go to the actual node's sibling */ |
| 2727 | node = pc->fp.modify.next_sibling(ca, tc); |
| 2728 | sibling_flag = !trp_node_is_empty(node); |
| 2729 | |
| 2730 | /* go to the next sibling or stay in actual node */ |
| 2731 | } while (sibling_flag); |
| 2732 | } |
| 2733 | |
| 2734 | /** |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2735 | * @brief Get address of the current node. |
| 2736 | * @param[in] tc contains current node. |
| 2737 | * @return Address of lysp_node, or NULL. |
| 2738 | */ |
| 2739 | static const void * |
| 2740 | trb_tree_ctx_get_node(struct trt_tree_ctx *tc) |
| 2741 | { |
| 2742 | return (const void *)tc->pn; |
| 2743 | } |
| 2744 | |
| 2745 | /** |
| 2746 | * @brief Get address of current node's child. |
| 2747 | * @param[in,out] tc contains current node. |
| 2748 | */ |
| 2749 | static const void * |
| 2750 | trb_tree_ctx_get_child(struct trt_tree_ctx *tc) |
| 2751 | { |
| 2752 | if (!trb_tree_ctx_get_node(tc)) { |
| 2753 | return NULL; |
| 2754 | } |
| 2755 | |
| 2756 | return lysp_node_child(tc->pn); |
| 2757 | } |
| 2758 | |
| 2759 | /** |
| 2760 | * @brief Set current node on its child. |
| 2761 | * @param[in,out] tc contains current node. |
| 2762 | */ |
| 2763 | static void |
| 2764 | trb_tree_ctx_set_child(struct trt_tree_ctx *tc) |
| 2765 | { |
| 2766 | tc->pn = trb_tree_ctx_get_child(tc); |
| 2767 | } |
| 2768 | |
| 2769 | /** |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2770 | * @brief Print subtree of nodes. |
| 2771 | * |
| 2772 | * The current node is expected to be the root of the subtree. |
| 2773 | * Before root node is no linebreak printing. This must be addressed by the caller. |
| 2774 | * Root node will also be printed. Behind last printed node is no linebreak. |
| 2775 | * |
| 2776 | * @param[in] max_gap_before_type is result from trb_try_unified_indent function for root node. Set parameter to 0 if distance does not matter. |
| 2777 | * @param[in] wr is wrapper saying how deep in the whole tree is the root of the subtree. |
| 2778 | * @param[in] ca is parent_cache from root's parent. If root is top-level node, insert TRP_EMPTY_PARENT_CACHE. |
| 2779 | * @param[in] pc is pointer to the printer (trp) context. |
| 2780 | * @param[in,out] tc is pointer to the tree (tro) context. |
| 2781 | */ |
| 2782 | static void |
| 2783 | trb_print_subtree_nodes(uint32_t max_gap_before_type, struct trt_wrapper wr, struct trt_parent_cache ca, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 2784 | { |
| 2785 | struct trt_parent_cache new_ca; |
| 2786 | struct trt_node node; |
| 2787 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2788 | if (!trb_tree_ctx_get_node(tc)) { |
| 2789 | return; |
| 2790 | } |
| 2791 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2792 | trb_print_entire_node(max_gap_before_type, wr, ca, pc, tc); |
| 2793 | /* go to the actual node's child */ |
| 2794 | new_ca = tro_parent_cache_for_child(ca, tc->pn); |
| 2795 | node = pc->fp.modify.next_child(ca, tc); |
| 2796 | |
| 2797 | if (!trp_node_is_empty(node)) { |
| 2798 | /* print root's nodes */ |
| 2799 | trb_print_nodes(wr, new_ca, pc, tc); |
| 2800 | /* get back from child node to actual node */ |
| 2801 | pc->fp.modify.parent(tc); |
| 2802 | } |
| 2803 | } |
| 2804 | |
| 2805 | /** |
| 2806 | * @brief Get number of siblings. |
| 2807 | * |
| 2808 | * Side-effect -> current node is set to the first sibling. |
| 2809 | * |
| 2810 | * @param[in] fp contains callback functions which modify tree context |
| 2811 | * @param[in,out] tc is the tree context. |
| 2812 | * @return Number of siblings of the current node. |
| 2813 | */ |
| 2814 | static uint32_t |
| 2815 | trb_get_number_of_siblings(struct trt_fp_modify_ctx fp, struct trt_tree_ctx *tc) |
| 2816 | { |
| 2817 | uint32_t ret = 1; |
| 2818 | struct trt_node node = TRP_EMPTY_NODE; |
| 2819 | |
| 2820 | /* including actual node */ |
| 2821 | fp.first_sibling(tc); |
| 2822 | while (!trp_node_is_empty(node = fp.next_sibling(TRP_EMPTY_PARENT_CACHE, tc))) { |
| 2823 | ret++; |
| 2824 | } |
| 2825 | fp.first_sibling(tc); |
| 2826 | return ret; |
| 2827 | } |
| 2828 | |
| 2829 | /** |
| 2830 | * @brief Print all parents and their children. |
| 2831 | * |
| 2832 | * This function is suitable for printing top-level nodes that do not have ancestors. |
| 2833 | * Function call print_subtree_nodes for all top-level siblings. |
| 2834 | * Use this function after 'module' keyword or 'augment' and so. |
| 2835 | * |
| 2836 | * @param[in] wr_t is type of the wrapper. |
| 2837 | * @param[pc] pc contains mainly functions for printing. |
| 2838 | * @param[in,out] tc is tree context. |
| 2839 | */ |
| 2840 | static void |
| 2841 | trb_print_family_tree(trd_wrapper_type wr_t, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 2842 | { |
| 2843 | struct trt_wrapper wr; |
| 2844 | struct trt_parent_cache ca; |
| 2845 | uint32_t total_parents; |
| 2846 | uint32_t max_gap_before_type; |
| 2847 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2848 | if (!trb_tree_ctx_get_node(tc)) { |
| 2849 | return; |
| 2850 | } |
| 2851 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2852 | wr = wr_t == TRD_WRAPPER_TOP ? TRP_INIT_WRAPPER_TOP : TRP_INIT_WRAPPER_BODY; |
| 2853 | ca = TRP_EMPTY_PARENT_CACHE; |
| 2854 | total_parents = trb_get_number_of_siblings(pc->fp.modify, tc); |
| 2855 | max_gap_before_type = trb_try_unified_indent(ca, pc, tc); |
| 2856 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2857 | if ((tc->section == TRD_SECT_GROUPING) && (tc->tpn == tc->pn->parent)) { |
| 2858 | ca.lys_config = 0x0; |
| 2859 | } |
| 2860 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2861 | for (uint32_t i = 0; i < total_parents; i++) { |
| 2862 | ly_print_(pc->out, "\n"); |
| 2863 | trb_print_subtree_nodes(max_gap_before_type, wr, ca, pc, tc); |
| 2864 | pc->fp.modify.next_sibling(ca, tc); |
| 2865 | } |
| 2866 | } |
| 2867 | |
| 2868 | /****************************************************************************** |
| 2869 | * Definition of trm main functions |
| 2870 | *****************************************************************************/ |
| 2871 | |
| 2872 | /** |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2873 | * @brief Settings if lysp_node are used for browsing through the tree. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2874 | * |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2875 | * @param[in] module YANG schema tree structure representing |
| 2876 | * YANG module. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2877 | * @param[in] out is output handler. |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2878 | * @param[in] max_line_length is the maximum line length limit |
| 2879 | * that should not be exceeded. |
| 2880 | * @param[in,out] pc will be adapted to lysp_tree. |
| 2881 | * @param[in,out] tc will be adapted to lysp_tree. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2882 | */ |
| 2883 | static void |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2884 | trm_lysp_tree_ctx(const struct lys_module *module, struct ly_out *out, size_t max_line_length, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2885 | { |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2886 | *tc = (struct trt_tree_ctx) { |
| 2887 | .section = TRD_SECT_MODULE, |
| 2888 | .module = module, |
| 2889 | .pn = module->parsed->data, |
| 2890 | .tpn = module->parsed->data, |
| 2891 | }; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2892 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2893 | pc->out = out; |
| 2894 | |
| 2895 | pc->fp.modify = (struct trt_fp_modify_ctx) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2896 | .parent = tro_modi_parent, |
| 2897 | .first_sibling = tro_modi_first_sibling, |
| 2898 | .next_sibling = tro_modi_next_sibling, |
| 2899 | .next_child = tro_modi_next_child, |
| 2900 | .next_augment = tro_modi_next_augment, |
| 2901 | .get_rpcs = tro_modi_get_rpcs, |
| 2902 | .get_notifications = tro_modi_get_notifications, |
| 2903 | .next_grouping = tro_modi_next_grouping, |
| 2904 | .next_yang_data = tro_modi_next_yang_data |
| 2905 | }; |
| 2906 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2907 | pc->fp.read = (struct trt_fp_read) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2908 | .module_name = tro_read_module_name, |
| 2909 | .node = tro_read_node, |
| 2910 | .if_sibling_exists = tro_read_if_sibling_exists |
| 2911 | }; |
| 2912 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2913 | pc->fp.print = (struct trt_fp_print) { |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2914 | .print_features_names = tro_print_features_names, |
| 2915 | .print_keys = tro_print_keys |
| 2916 | }; |
| 2917 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2918 | pc->max_line_length = max_line_length; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | /** |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2922 | * @brief Printing section 'module', 'rpcs' or 'notifications'. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2923 | * |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2924 | * First node must be the first child of 'module', |
| 2925 | * 'rpcs' or 'notifications'. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2926 | * |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2927 | * @param[in] ks is section representation. |
| 2928 | * @param[in] pc contains mainly functions for printing. |
| 2929 | * @param[in,out] tc is the tree context. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2930 | */ |
| 2931 | static void |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2932 | trm_print_section_as_family_tree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 2933 | { |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 2934 | if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) { |
| 2935 | return; |
| 2936 | } |
| 2937 | |
| 2938 | trp_print_keyword_stmt(ks, pc->max_line_length, 0, pc->out); |
| 2939 | if ((ks.type == TRD_KEYWORD_MODULE) || (ks.type == TRD_KEYWORD_SUBMODULE)) { |
| 2940 | trb_print_family_tree(TRD_WRAPPER_TOP, pc, tc); |
| 2941 | } else { |
| 2942 | trb_print_family_tree(TRD_WRAPPER_BODY, pc, tc); |
| 2943 | } |
| 2944 | } |
| 2945 | |
| 2946 | /** |
| 2947 | * @brief Printing section 'augment', 'grouping' or 'yang-data'. |
| 2948 | * |
| 2949 | * First node is augment, grouping or yang-data itself. |
| 2950 | * |
| 2951 | * @param[in] ks is section representation. |
| 2952 | * @param[in] pc contains mainly functions for printing. |
| 2953 | * @param[in,out] tc is the tree context. |
| 2954 | */ |
| 2955 | static void |
| 2956 | trm_print_section_as_subtree(struct trt_keyword_stmt ks, struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 2957 | { |
| 2958 | ly_bool grp_has_data = 0; |
| 2959 | |
| 2960 | if (TRP_KEYWORD_STMT_IS_EMPTY(ks)) { |
| 2961 | return; |
| 2962 | } |
| 2963 | |
| 2964 | if (ks.type == TRD_KEYWORD_GROUPING) { |
| 2965 | grp_has_data = trb_tree_ctx_get_child(tc) ? 1 : 0; |
| 2966 | } |
| 2967 | |
| 2968 | trp_print_keyword_stmt(ks, pc->max_line_length, grp_has_data, pc->out); |
| 2969 | trb_tree_ctx_set_child(tc); |
| 2970 | trb_print_family_tree(TRD_WRAPPER_BODY, pc, tc); |
| 2971 | } |
| 2972 | |
| 2973 | /** |
| 2974 | * @brief Print 'module' keyword, its name and all nodes. |
| 2975 | * @param[in] pc contains mainly functions for printing. |
| 2976 | * @param[in,out] tc is the tree context. |
| 2977 | */ |
| 2978 | static void |
| 2979 | trm_print_module_section(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 2980 | { |
| 2981 | trm_print_section_as_family_tree(pc->fp.read.module_name(tc), pc, tc); |
| 2982 | } |
| 2983 | |
| 2984 | /** |
| 2985 | * @brief For all augment sections: print 'augment' keyword, |
| 2986 | * its target node and all nodes. |
| 2987 | * @param[in] pc contains mainly functions for printing. |
| 2988 | * @param[in,out] tc is the tree context. |
| 2989 | */ |
| 2990 | static void |
| 2991 | trm_print_augmentations(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 2992 | { |
| 2993 | ly_bool once; |
| 2994 | |
| 2995 | if (!pc->fp.modify.next_augment) { |
| 2996 | return; |
| 2997 | } |
| 2998 | |
| 2999 | once = 1; |
| 3000 | for (struct trt_keyword_stmt ks = pc->fp.modify.next_augment(tc); |
| 3001 | !(TRP_KEYWORD_STMT_IS_EMPTY(ks)); |
| 3002 | ks = pc->fp.modify.next_augment(tc)) { |
| 3003 | |
| 3004 | if (once) { |
| 3005 | ly_print_(pc->out, "\n"); |
| 3006 | ly_print_(pc->out, "\n"); |
| 3007 | once = 0; |
| 3008 | } else { |
| 3009 | ly_print_(pc->out, "\n"); |
| 3010 | } |
| 3011 | |
| 3012 | trm_print_section_as_subtree(ks, pc, tc); |
| 3013 | } |
| 3014 | } |
| 3015 | |
| 3016 | /** |
| 3017 | * @brief For rpcs section: print 'rpcs' keyword and all its nodes. |
| 3018 | * @param[in] pc contains mainly functions for printing. |
| 3019 | * @param[in,out] tc is the tree context. |
| 3020 | */ |
| 3021 | static void |
| 3022 | trm_print_rpcs(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 3023 | { |
| 3024 | struct trt_keyword_stmt rpc; |
| 3025 | |
| 3026 | assert(pc->fp.modify.get_rpcs); |
| 3027 | |
| 3028 | rpc = pc->fp.modify.get_rpcs(tc); |
| 3029 | |
| 3030 | if (!(TRP_KEYWORD_STMT_IS_EMPTY(rpc))) { |
| 3031 | ly_print_(pc->out, "\n"); |
| 3032 | ly_print_(pc->out, "\n"); |
| 3033 | trm_print_section_as_family_tree(rpc, pc, tc); |
| 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | /** |
| 3038 | * @brief For notifications section: print 'notifications' keyword |
| 3039 | * and all its nodes. |
| 3040 | * @param[in] pc contains mainly functions for printing. |
| 3041 | * @param[in,out] tc is the tree context. |
| 3042 | */ |
| 3043 | static void |
| 3044 | trm_print_notifications(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 3045 | { |
| 3046 | struct trt_keyword_stmt notifs; |
| 3047 | |
| 3048 | assert(pc->fp.modify.get_notifications); |
| 3049 | |
| 3050 | notifs = pc->fp.modify.get_notifications(tc); |
| 3051 | |
| 3052 | if (!(TRP_KEYWORD_STMT_IS_EMPTY(notifs))) { |
| 3053 | ly_print_(pc->out, "\n"); |
| 3054 | ly_print_(pc->out, "\n"); |
| 3055 | trm_print_section_as_family_tree(notifs, pc, tc); |
| 3056 | } |
| 3057 | } |
| 3058 | |
| 3059 | /** |
| 3060 | * @brief For all grouping sections: print 'grouping' keyword, its name |
| 3061 | * and all nodes. |
| 3062 | * @param[in] pc contains mainly functions for printing. |
| 3063 | * @param[in,out] tc is the tree context. |
| 3064 | */ |
| 3065 | static void |
| 3066 | trm_print_groupings(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 3067 | { |
| 3068 | ly_bool once; |
| 3069 | |
| 3070 | if (!pc->fp.modify.next_grouping) { |
| 3071 | return; |
| 3072 | } |
| 3073 | |
| 3074 | once = 1; |
| 3075 | for (struct trt_keyword_stmt ks = pc->fp.modify.next_grouping(tc); |
| 3076 | !(TRP_KEYWORD_STMT_IS_EMPTY(ks)); |
| 3077 | ks = pc->fp.modify.next_grouping(tc)) { |
| 3078 | if (once) { |
| 3079 | ly_print_(pc->out, "\n"); |
| 3080 | ly_print_(pc->out, "\n"); |
| 3081 | once = 0; |
| 3082 | } else { |
| 3083 | ly_print_(pc->out, "\n"); |
| 3084 | } |
| 3085 | trm_print_section_as_subtree(ks, pc, tc); |
| 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | /** |
| 3090 | * @brief For all yang-data sections: print 'yang-data' keyword |
| 3091 | * and all its nodes. |
| 3092 | * @param[in] pc contains mainly functions for printing. |
| 3093 | * @param[in,out] tc is the tree context. |
| 3094 | */ |
| 3095 | static void |
| 3096 | trm_print_yang_data(struct trt_printer_ctx *UNUSED(pc), struct trt_tree_ctx *UNUSED(tc)) |
| 3097 | { |
| 3098 | /* TODO yang-data is not implemented */ |
| 3099 | return; |
| 3100 | } |
| 3101 | |
| 3102 | /** |
| 3103 | * @brief Print sections module, augment, rpcs, notifications, |
| 3104 | * grouping, yang-data. |
| 3105 | * @param[in] pc contains mainly functions for printing. |
| 3106 | * @param[in,out] tc is the tree context. |
| 3107 | */ |
| 3108 | static void |
| 3109 | trm_print_sections(struct trt_printer_ctx *pc, struct trt_tree_ctx *tc) |
| 3110 | { |
| 3111 | trm_print_module_section(pc, tc); |
| 3112 | trm_print_augmentations(pc, tc); |
| 3113 | trm_print_rpcs(pc, tc); |
| 3114 | trm_print_notifications(pc, tc); |
| 3115 | trm_print_groupings(pc, tc); |
| 3116 | trm_print_yang_data(pc, tc); |
| 3117 | ly_print_(pc->out, "\n"); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | /****************************************************************************** |
| 3121 | * Definition of module interface |
| 3122 | *****************************************************************************/ |
| 3123 | |
| 3124 | LY_ERR |
| 3125 | tree_print_parsed_module(struct ly_out *out, const struct lys_module *module, uint32_t UNUSED(options), size_t line_length) |
| 3126 | { |
| 3127 | struct trt_printer_ctx pc; |
| 3128 | struct trt_tree_ctx tc; |
| 3129 | struct ly_out *new_out; |
| 3130 | LY_ERR erc; |
| 3131 | struct ly_out_clb_arg clb_arg = TRP_INIT_LY_OUT_CLB_ARG(TRD_PRINT, out, 0, LY_SUCCESS); |
| 3132 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 3133 | LY_CHECK_ARG_RET3(module->ctx, out, module, module->parsed, LY_EINVAL); |
| 3134 | |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3135 | if ((erc = ly_out_new_clb(&trp_ly_out_clb_func, &clb_arg, &new_out))) { |
| 3136 | return erc; |
| 3137 | } |
| 3138 | |
| 3139 | line_length = line_length == 0 ? SIZE_MAX : line_length; |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 3140 | trm_lysp_tree_ctx(module, new_out, line_length, &pc, &tc); |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3141 | |
| 3142 | trm_print_sections(&pc, &tc); |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 3143 | erc = clb_arg.last_error; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3144 | |
| 3145 | ly_out_free(new_out, NULL, 1); |
| 3146 | |
aPiecek | dc8fd57 | 2021-04-19 10:47:23 +0200 | [diff] [blame^] | 3147 | return erc; |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | LY_ERR |
| 3151 | tree_print_submodule(struct ly_out *UNUSED(out), const struct lys_module *UNUSED(module), const struct lysp_submodule *UNUSED(submodp), uint32_t UNUSED(options), size_t UNUSED(line_length)) |
| 3152 | // LY_ERR tree_print_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp, uint32_t options, size_t line_length) |
| 3153 | { |
| 3154 | /** Not implemented right now. */ |
| 3155 | return LY_SUCCESS; |
| 3156 | } |
| 3157 | |
| 3158 | LY_ERR |
| 3159 | tree_print_compiled_node(struct ly_out *UNUSED(out), const struct lysc_node *UNUSED(node), uint32_t UNUSED(options), size_t UNUSED(line_length)) |
| 3160 | // LY_ERR tree_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options, size_t line_length) |
| 3161 | { |
| 3162 | /** Not implemented right now. */ |
| 3163 | return LY_SUCCESS; |
| 3164 | } |